Skip to content

Commit

Permalink
Improve GC measures
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielThomas committed Sep 3, 2024
1 parent ae60d90 commit 81be494
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public class JavaFlightRecorder {
private static final String JavaThreadStatistics = PREFIX + "JavaThreadStatistics";
private static final String VirtualThreadPinned = PREFIX + "VirtualThreadPinned";
private static final String VirtualThreadSubmitFailed = PREFIX + "VirtualThreadSubmitFailed";
private static final String YoungGarbageCollection = PREFIX + "YoungGarbageCollection";
private static final String ZAllocationStall = PREFIX + "ZAllocationStall";
private static final String ZYoungGarbageCollection = PREFIX + "ZYoungGarbageCollection";
private static final String ZOldGarbageCollection = PREFIX + "ZOldGarbageCollection";

private JavaFlightRecorder() {
}
Expand All @@ -62,7 +62,7 @@ public static AutoCloseable monitorDefaultEvents(Registry registry, Executor exe
collectCompilerStatistics(registry, rs);
collectThreadStatistics(registry, rs);
collectVirtualThreadEvents(registry, rs);
collectZgcEvents(registry, rs);
collectGcEvents(registry, rs);
executor.execute(rs::start);
return rs::close;
}
Expand Down Expand Up @@ -113,17 +113,15 @@ private static void collectVirtualThreadEvents(Registry registry, RecordingStrea
);
}

private static void collectZgcEvents(Registry registry, RecordingStream rs) {
consume(ZYoungGarbageCollection, rs, event ->
registry.timer("jvm.zgc.youngCollection", "tenuringThreshold", event.getString("tenuringThreshold"))
.record(event.getDuration()));

consume(ZOldGarbageCollection, rs, event ->
registry.timer("jvm.zgc.oldCollection")
.record(event.getDuration()));
private static void collectGcEvents(Registry registry, RecordingStream rs) {
Consumer<RecordedEvent> tenuringThreshold = event ->
registry.gauge("jvm.gc.tenuringThreshold")
.set(event.getLong("tenuringThreshold"));
consume(YoungGarbageCollection, rs, tenuringThreshold);
consume(ZYoungGarbageCollection, rs, tenuringThreshold);

consume(ZAllocationStall, rs, event ->
registry.timer("jvm.zgc.allocationStall", "type", event.getString("type"))
registry.timer("jvm.gc.allocationStall", "type", event.getString("type"))
.record(event.getDuration()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

public class JavaFlightRecorderTest {

public static volatile Object obj;

@Test
public void isSupported() {
assertTrue(JavaFlightRecorder.isSupported());
Expand All @@ -41,13 +43,23 @@ public void checkDefaultMeasures() throws Exception {
Registry registry = new DefaultRegistry();
ExecutorService executor = Executors.newSingleThreadExecutor();
try (var closable = JavaFlightRecorder.monitorDefaultEvents(registry, executor)) {
// allocate rapidly to trigger a GC, black holing using the approach from
// https://github.com/openjdk/jdk/blob/master/test/hotspot/jtreg/gc/testlibrary/Allocation.java
for (int i = 0; i < 100; i++) {
obj = new byte[4 * 1024 * 1024];
obj = null;
}
Thread.sleep(6000);
}
executor.shutdownNow();

Map<Id, Measurement> measures = registry.measurements()
.collect(Collectors.toMap(Measurement::id, m -> m));

Measurement tenuringThreshold = measures.get(Id.create("jvm.gc.tenuringThreshold"));
assertNotEquals(null, tenuringThreshold);
assertTrue(tenuringThreshold.value() > 0);

Measurement classesLoaded = measures.get(Id.create("jvm.classloading.classesLoaded"));
Measurement classesUnloaded = measures.get(Id.create("jvm.classloading.classesUnloaded"));
assertNotEquals(null, classesLoaded);
Expand Down

0 comments on commit 81be494

Please sign in to comment.