Skip to content

Commit

Permalink
[GR-48293] [GR-48294] Fix JFR periodic events and ThreadCPULoad event.
Browse files Browse the repository at this point in the history
PullRequest: graal/15374
  • Loading branch information
christianhaeubl committed Sep 1, 2023
2 parents dc25589 + 110585f commit 63eaafa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public static JfrManager get() {
public RuntimeSupport.Hook startupHook() {
return isFirstIsolate -> {
parseFlightRecorderLogging(SubstrateOptions.FlightRecorderLogging.getValue());
periodicEventSetup();
if (isJFREnabled()) {
periodicEventSetup();
initRecording();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import com.oracle.svm.core.threadlocal.FastThreadLocalLong;
import com.oracle.svm.core.util.TimeUtils;

import static com.oracle.svm.core.thread.PlatformThreads.fromVMThread;

public class ThreadCPULoadEvent {
private static final FastThreadLocalLong cpuTimeTL = FastThreadLocalFactory.createLong("ThreadCPULoadEvent.cpuTimeTL");
private static final FastThreadLocalLong userTimeTL = FastThreadLocalFactory.createLong("ThreadCPULoadEvent.userTimeTL");
Expand Down Expand Up @@ -119,7 +121,7 @@ public static void emit(IsolateThread isolateThread) {

JfrNativeEventWriter.beginSmallEvent(data, JfrEvent.ThreadCPULoad);
JfrNativeEventWriter.putLong(data, JfrTicks.elapsedTicks());
JfrNativeEventWriter.putEventThread(data);
JfrNativeEventWriter.putThread(data, fromVMThread(isolateThread));
JfrNativeEventWriter.putFloat(data, userTime / totalAvailableTime);
JfrNativeEventWriter.putFloat(data, systemTime / totalAvailableTime);
JfrNativeEventWriter.endSmallEvent(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

package com.oracle.svm.test.jfr;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.lang.management.ManagementFactory;
Expand All @@ -36,6 +35,7 @@
import java.util.List;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;

import com.oracle.svm.core.util.TimeUtils;
Expand All @@ -47,6 +47,7 @@ public class TestThreadCPULoadEvent extends JfrRecordingTest {
private static final int TIMEOUT = 10000;
private static final String THREAD_NAME_1 = "Thread-1";
private static final String THREAD_NAME_2 = "Thread-2";
private static final String THREAD_NAME_3 = "Thread-3";

@Test
public void test() throws Throwable {
Expand All @@ -55,15 +56,20 @@ public void test() throws Throwable {

WeakReference<Thread> thread1 = createAndStartBusyWaitThread(THREAD_NAME_1, 10, 250);
WeakReference<Thread> thread2 = createAndStartBusyWaitThread(THREAD_NAME_2, 250, 10);
Thread thread3 = createAndStartBusyWaitThread(THREAD_NAME_3, 20, TIMEOUT).get();

/* For threads 1 and 2, the event is emitted when the thread exits. */
waitUntilCollected(thread1);
waitUntilCollected(thread2);

/* For thread 3, the event is emitted upon chunk end. */
stopRecording(recording, TestThreadCPULoadEvent::validateEvents);

Assert.assertTrue(thread3.isAlive());
thread3.interrupt();
}

private static void validateEvents(List<RecordedEvent> events) {
assertEquals(2, events.size());
Map<String, Float> userTimes = new HashMap<>();
Map<String, Float> cpuTimes = new HashMap<>();

Expand All @@ -73,11 +79,10 @@ private static void validateEvents(List<RecordedEvent> events) {
float systemTime = e.<Float> getValue("system");
assertTrue("User time is outside 0..1 range", 0.0 <= userTime && userTime <= 1.0);
assertTrue("System time is outside 0..1 range", 0.0 <= systemTime && systemTime <= 1.0);

userTimes.put(threadName, userTime);
cpuTimes.put(threadName, userTime + systemTime);
}

assertTrue(cpuTimes.containsKey(THREAD_NAME_3));
assertTrue(userTimes.get(THREAD_NAME_1) < userTimes.get(THREAD_NAME_2));
assertTrue(cpuTimes.get(THREAD_NAME_1) < cpuTimes.get(THREAD_NAME_2));
}
Expand Down

0 comments on commit 63eaafa

Please sign in to comment.