Skip to content

Commit f0ca37e

Browse files
fix end chunk periodic events and threadCPULoad events
1 parent b50519e commit f0ca37e

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jfr/JfrManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public static JfrManager get() {
8686
public RuntimeSupport.Hook startupHook() {
8787
return isFirstIsolate -> {
8888
parseFlightRecorderLogging(SubstrateOptions.FlightRecorderLogging.getValue());
89+
periodicEventSetup();
8990
if (isJFREnabled()) {
90-
periodicEventSetup();
9191
initRecording();
9292
}
9393
};

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jfr/events/ThreadCPULoadEvent.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import com.oracle.svm.core.threadlocal.FastThreadLocalLong;
4545
import com.oracle.svm.core.util.TimeUtils;
4646

47+
import static com.oracle.svm.core.thread.PlatformThreads.fromVMThread;
48+
4749
public class ThreadCPULoadEvent {
4850
private static final FastThreadLocalLong cpuTimeTL = FastThreadLocalFactory.createLong("ThreadCPULoadEvent.cpuTimeTL");
4951
private static final FastThreadLocalLong userTimeTL = FastThreadLocalFactory.createLong("ThreadCPULoadEvent.userTimeTL");
@@ -119,7 +121,7 @@ public static void emit(IsolateThread isolateThread) {
119121

120122
JfrNativeEventWriter.beginSmallEvent(data, JfrEvent.ThreadCPULoad);
121123
JfrNativeEventWriter.putLong(data, JfrTicks.elapsedTicks());
122-
JfrNativeEventWriter.putEventThread(data);
124+
JfrNativeEventWriter.putThread(data, fromVMThread(isolateThread));
123125
JfrNativeEventWriter.putFloat(data, userTime / totalAvailableTime);
124126
JfrNativeEventWriter.putFloat(data, systemTime / totalAvailableTime);
125127
JfrNativeEventWriter.endSmallEvent(data);

substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/jfr/TestThreadCPULoadEvent.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
package com.oracle.svm.test.jfr;
2828

29-
import static org.junit.Assert.assertEquals;
3029
import static org.junit.Assert.assertTrue;
3130

3231
import java.lang.management.ManagementFactory;
@@ -47,6 +46,9 @@ public class TestThreadCPULoadEvent extends JfrRecordingTest {
4746
private static final int TIMEOUT = 10000;
4847
private static final String THREAD_NAME_1 = "Thread-1";
4948
private static final String THREAD_NAME_2 = "Thread-2";
49+
private static final String THREAD_NAME_3 = "Thread-3";
50+
private static Thread thread3 = null;
51+
private static volatile boolean finished = false;
5052

5153
@Test
5254
public void test() throws Throwable {
@@ -55,15 +57,21 @@ public void test() throws Throwable {
5557

5658
WeakReference<Thread> thread1 = createAndStartBusyWaitThread(THREAD_NAME_1, 10, 250);
5759
WeakReference<Thread> thread2 = createAndStartBusyWaitThread(THREAD_NAME_2, 250, 10);
60+
thread3 = createAndStartLongLived(THREAD_NAME_3);
5861

5962
waitUntilCollected(thread1);
6063
waitUntilCollected(thread2);
6164

65+
/**
66+
* Thread 3 should be alive at the time of final chunk rotation to test whether this event
67+
* is emitted correctly upon chunk end.
68+
*/
6269
stopRecording(recording, TestThreadCPULoadEvent::validateEvents);
70+
finished = true;
6371
}
6472

6573
private static void validateEvents(List<RecordedEvent> events) {
66-
assertEquals(2, events.size());
74+
assertTrue(thread3.isAlive());
6775
Map<String, Float> userTimes = new HashMap<>();
6876
Map<String, Float> cpuTimes = new HashMap<>();
6977

@@ -73,11 +81,10 @@ private static void validateEvents(List<RecordedEvent> events) {
7381
float systemTime = e.<Float> getValue("system");
7482
assertTrue("User time is outside 0..1 range", 0.0 <= userTime && userTime <= 1.0);
7583
assertTrue("System time is outside 0..1 range", 0.0 <= systemTime && systemTime <= 1.0);
76-
7784
userTimes.put(threadName, userTime);
7885
cpuTimes.put(threadName, userTime + systemTime);
7986
}
80-
87+
assertTrue(cpuTimes.containsKey(THREAD_NAME_3));
8188
assertTrue(userTimes.get(THREAD_NAME_1) < userTimes.get(THREAD_NAME_2));
8289
assertTrue(cpuTimes.get(THREAD_NAME_1) < cpuTimes.get(THREAD_NAME_2));
8390
}
@@ -92,6 +99,21 @@ private static WeakReference<Thread> createAndStartBusyWaitThread(String name, i
9299
return new WeakReference<>(thread);
93100
}
94101

102+
private static Thread createAndStartLongLived(String name) {
103+
Thread thread = new Thread(() -> {
104+
while (!finished) {
105+
try {
106+
Thread.sleep(10);
107+
} catch (InterruptedException e) {
108+
throw new RuntimeException(e);
109+
}
110+
}
111+
});
112+
thread.setName(name);
113+
thread.start();
114+
return thread;
115+
}
116+
95117
private static void busyWait(long waitMs) {
96118
ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();
97119
long timeout = System.currentTimeMillis() + TIMEOUT;

0 commit comments

Comments
 (0)