// Start with a full GC to minimize risk of getting extra GC between // getBeanCollectionCount() and recording.start().
doSystemGc();
GCHelper.CollectionSummary startBeanCount = GCHelper.CollectionSummary.createFromMxBeans();
recording.start();
for (Thread t : workerThreads) {
t.start();
} for (Thread t : workerThreads) {
t.join();
}
// End with a full GC to minimize risk of getting extra GC between // recording.stop and getBeanCollectionCount().
doSystemGc(); // Add an extra System.gc() to make sure we get at least one full garbage_collection batch at // the end of the test. This extra System.gc() is only necessary when using "+ExplicitGCInvokesConcurrent".
doSystemGc();
// For some GC configurations, the JFR recording may have stopped before we received the last gc event. try {
gcBatches = GCHelper.GcBatch.createFromEvents(events);
eventCounts = GCHelper.CollectionSummary.createFromEvents(gcBatches);
privatevoid verifyCollectionCount(String collector, long eventCounts, long beanCounts) { if (GCHelper.gcG1Old.equals(oldCollector)) { // MXBean does not report old collections for G1Old, so we have nothing to compare with. return;
} // JFR events and GarbageCollectorMXBean events are not updated at the same time. // This means that number of collections may diff. // We allow a diff of +- 1 collection count. long minCount = Math.max(0, beanCounts - 1); long maxCount = beanCounts + 1;
Asserts.assertGreaterThanOrEqual(eventCounts, minCount, "Too few event counts for collector " + collector);
Asserts.assertLessThanOrEqual(eventCounts, maxCount, "Too many event counts for collector " + collector);
}
/** *VerifiesthatalleventsbelongingtoasingleGCareok. *AGcBatchcontainsallflightrecordereventsthatbelongtoasingleGC.
*/ privatevoid verifySingleGcBatch(List<GCHelper.GcBatch> batches) { for (GCHelper.GcBatch batch : batches) { //System.out.println("batch:\r\n" + batch.getLog()); try {
RecordedEvent endEvent = batch.getEndEvent();
Asserts.assertNotNull(endEvent, "No end event in batch.");
Asserts.assertNotNull(batch.getName(), "No method name in end event."); long longestPause = Events.assertField(endEvent, "longestPause").atLeast(0L).getValue();
Events.assertField(endEvent, "sumOfPauses").atLeast(longestPause).getValue();
Instant batchStartTime = endEvent.getStartTime();
Instant batchEndTime = endEvent.getEndTime(); for (RecordedEvent event : batch.getEvents()) {
String name = event.getEventType().getName(); if (name.contains("AllocationRequiringGC")) { // Unlike other events, these are sent *before* a GC.
Asserts.assertLessThanOrEqual(event.getStartTime(), batchStartTime, "Timestamp in event after start event, should be sent before GC start");
} else {
Asserts.assertGreaterThanOrEqual(event.getStartTime(), batchStartTime, "startTime in event before batch start event, should be sent after GC start");
} // GCCPUTime is generated after GC is completed. if (!EventNames.GCCPUTime.equals(name)) {
Asserts.assertLessThanOrEqual(event.getEndTime(), batchEndTime, "endTime in event after batch end event, should be sent before GC end");
}
}
// Verify that all required events has been received.
String[] requiredEvents = GCHelper.requiredEvents.get(batch.getName());
Asserts.assertNotNull(requiredEvents, "No required events specified for " + batch.getName()); for (String requiredEvent : requiredEvents) { boolean b = batch.containsEvent(requiredEvent);
Asserts.assertTrue(b, String.format("%s does not contain event %s", batch, requiredEvent));
}
// Verify that we have exactly one heap_summary "Before GC" and one "After GC". int countBeforeGc = 0; int countAfterGc = 0; for (RecordedEvent event : batch.getEvents()) { if (GCHelper.event_heap_summary.equals(event.getEventType().getName())) {
String when = Events.assertField(event, "when").notEmpty().getValue(); if ("Before GC".equals(when)) {
countBeforeGc++;
} elseif ("After GC".equals(when)) {
countAfterGc++;
} else {
Asserts.fail("Unknown value for heap_summary.when: '" + when + "'");
}
}
}
Asserts.assertEquals(1, countBeforeGc, "Unexpected number of heap_summary.before_gc");
Asserts.assertEquals(1, countAfterGc, "Unexpected number of heap_summary.after_gc");
} catch (Throwable e) {
GCHelper.log("verifySingleGcBatch failed for gcEvent:");
GCHelper.log(batch.getLog()); throw e;
}
}
}
publicvoid run() { long currCollections = GCHelper.CollectionSummary.createFromMxBeans().sum(); long endCollections = totalCollections + currCollections;
Random r = new Random(0); while (true) { for (int i = 0; i < 1000; i++) {
dummyBuffer = newbyte[r.nextInt(10000)];
} if (GCHelper.CollectionSummary.createFromMxBeans().sum() >= endCollections) { break;
}
}
}
}
public SystemGcWaitRunner(int totalCollections, int minWaitCollections, long maxWaitMillis) { this.totalCollections = totalCollections; this.minWaitCollections = minWaitCollections; this.maxWaitMillis = maxWaitMillis;
}
publicstatic SystemGcWaitRunner create(int deltaCollections, int minWaitCollections, long maxWaitMillis) { returnnew SystemGcWaitRunner(deltaCollections, minWaitCollections, maxWaitMillis);
}
publicvoid run() { long currCount = GCHelper.CollectionSummary.createFromMxBeans().sum(); long endCount = totalCollections + currCount; long nextSystemGcCount = currCount + minWaitCollections; long now = System.currentTimeMillis(); long nextSystemGcMillis = now + maxWaitMillis;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.