Skip to content

Commit 9d0f64a

Browse files
Cleanups.
1 parent 2cc0ccd commit 9d0f64a

File tree

5 files changed

+70
-58
lines changed

5 files changed

+70
-58
lines changed

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

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

2727
package com.oracle.svm.core.jfr.events;
2828

29-
import jdk.graal.compiler.word.Word;
3029
import org.graalvm.nativeimage.StackValue;
3130

3231
import com.oracle.svm.core.Uninterruptible;
@@ -37,21 +36,13 @@
3736
import com.oracle.svm.core.jfr.JfrNativeEventWriterDataAccess;
3837
import com.oracle.svm.core.jfr.JfrTicks;
3938
import com.oracle.svm.core.jfr.SubstrateJVM;
40-
import com.oracle.svm.core.monitor.JavaMonitor;
39+
import com.oracle.svm.core.monitor.JavaMonitorQueuedSynchronizer;
40+
41+
import jdk.graal.compiler.word.Word;
4142

4243
public class ThreadParkEvent {
4344
public static void emit(long startTicks, Object obj, boolean isAbsolute, long time) {
44-
if (HasJfrSupport.get()) {
45-
/*
46-
* Skip emission if corresponding JavaMonitorWait or JavaMonitorEnter events are already
47-
* emitted (this is an internal park).
48-
*/
49-
if (obj != null) {
50-
Class<?> clazz = obj.getClass();
51-
if (clazz.equals(JavaMonitor.class) || (clazz.getEnclosingClass() != null && clazz.getEnclosingClass().isAssignableFrom(JavaMonitor.class))) {
52-
return;
53-
}
54-
}
45+
if (HasJfrSupport.get() && !isInternalPark(obj)) {
5546
emit0(startTicks, obj, isAbsolute, time);
5647
}
5748
}
@@ -87,4 +78,22 @@ private static void emit0(long startTicks, Object obj, boolean isAbsolute, long
8778
JfrNativeEventWriter.endSmallEvent(data);
8879
}
8980
}
81+
82+
/**
83+
* Skip emission if this is an internal park ({@link JavaMonitorWaitEvent} or
84+
* {@link JavaMonitorEnterEvent} will be emitted instead).
85+
*/
86+
private static boolean isInternalPark(Object obj) {
87+
if (obj == null) {
88+
return false;
89+
}
90+
91+
Class<?> parkedClass = obj.getClass();
92+
if (JavaMonitorQueuedSynchronizer.class.isAssignableFrom(parkedClass)) {
93+
return true;
94+
}
95+
96+
Class<?> enclosingClass = parkedClass.getEnclosingClass();
97+
return enclosingClass != null && JavaMonitorQueuedSynchronizer.class.isAssignableFrom(enclosingClass);
98+
}
9099
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/monitor/JavaMonitorQueuedSynchronizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
* </ul>
6161
*/
6262
@BasedOnJDKClass(AbstractQueuedLongSynchronizer.class)
63-
abstract class JavaMonitorQueuedSynchronizer {
63+
public abstract class JavaMonitorQueuedSynchronizer {
6464
// Node status bits, also used as argument and return values
6565
static final int WAITING = 1; // must be 1
6666
static final int CANCELLED = 0x80000000; // must be negative

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,24 @@ public void test() throws Throwable {
5353
String[] events = new String[]{JfrEvent.JavaMonitorEnter.getName()};
5454
Recording recording = startRecording(events);
5555

56-
Runnable first = () -> {
56+
firstThread = new Thread(() -> {
5757
try {
5858
helper.doWork();
5959
} catch (InterruptedException e) {
6060
throw new RuntimeException(e);
6161
}
62-
};
62+
});
6363

64-
Runnable second = () -> {
64+
secondThread = new Thread(() -> {
6565
try {
6666
passedCheckpoint = true;
6767
helper.doWork();
6868
} catch (InterruptedException e) {
6969
throw new RuntimeException(e);
7070
}
71-
};
72-
firstThread = new Thread(first);
73-
secondThread = new Thread(second);
71+
});
7472

73+
/* Start the first thread so that it can then start the second thread. */
7574
firstThread.start();
7675

7776
firstThread.join();

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private void test(Object obj) throws Throwable {
7373
}
7474

7575
private void test(Object obj, String className) throws Throwable {
76-
Runnable first = () -> {
76+
firstThread = new Thread(() -> {
7777
try {
7878
synchronized (obj) {
7979
secondThread.start();
@@ -85,25 +85,25 @@ private void test(Object obj, String className) throws Throwable {
8585
} catch (InterruptedException e) {
8686
throw new RuntimeException(e);
8787
}
88-
};
88+
});
8989

90-
Runnable second = () -> {
90+
secondThread = new Thread(() -> {
9191
passedCheckpoint = true;
9292
synchronized (obj) {
9393
GraalDirectives.blackhole(obj);
9494
}
95-
};
95+
});
9696

9797
expectedClassName = className;
98-
passedCheckpoint = false;
99-
firstThread = new Thread(first);
100-
secondThread = new Thread(second);
10198

10299
/* Now that the data is prepared, start the JFR recording. */
103100
String[] events = new String[]{JfrEvent.JavaMonitorInflate.getName()};
104101
Recording recording = startRecording(events);
105102

106-
/* Generate event with "Monitor Enter" cause. */
103+
/*
104+
* Generate event with "Monitor Enter" cause. Start the first thread so that it can then
105+
* start the second thread.
106+
*/
107107
firstThread.start();
108108

109109
firstThread.join();

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

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,25 @@
2626

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

29-
import com.oracle.svm.core.jfr.JfrEvent;
30-
import jdk.jfr.Recording;
31-
import jdk.jfr.consumer.RecordedClass;
32-
import jdk.jfr.consumer.RecordedEvent;
33-
import org.junit.Test;
29+
import static org.junit.Assert.assertFalse;
30+
import static org.junit.Assert.assertTrue;
31+
import static org.junit.Assert.fail;
3432

3533
import java.util.List;
3634
import java.util.concurrent.locks.LockSupport;
3735

38-
import static org.junit.Assert.assertFalse;
39-
import static org.junit.Assert.assertTrue;
36+
import org.junit.Test;
37+
38+
import com.oracle.svm.core.jfr.JfrEvent;
39+
import com.oracle.svm.core.util.TimeUtils;
40+
41+
import jdk.jfr.Recording;
42+
import jdk.jfr.consumer.RecordedClass;
43+
import jdk.jfr.consumer.RecordedEvent;
4044

4145
public class TestOmitInternalParkEvents extends JfrRecordingTest {
42-
private static final int MILLIS = 500;
43-
private final Helper helper = new Helper();
44-
private Thread firstThread;
46+
private static final int MILLIS = 100;
47+
private final MonitorHelper monitorHelper = new MonitorHelper();
4548
private Thread secondThread;
4649
private volatile boolean passedCheckpoint;
4750

@@ -50,40 +53,39 @@ public void test() throws Throwable {
5053
String[] events = new String[]{JfrEvent.JavaMonitorEnter.getName(), JfrEvent.JavaMonitorWait.getName(), JfrEvent.ThreadPark.getName()};
5154
Recording recording = startRecording(events);
5255

53-
// Generate monitor enter events
54-
Runnable first = () -> {
56+
/* Generate monitor enter events. */
57+
Thread firstThread = new Thread(() -> {
5558
try {
56-
helper.doWork();
59+
monitorHelper.doWork();
5760
} catch (InterruptedException e) {
5861
throw new RuntimeException(e);
5962
}
60-
};
63+
});
6164

62-
Runnable second = () -> {
65+
secondThread = new Thread(() -> {
6366
try {
6467
passedCheckpoint = true;
65-
helper.doWork();
68+
monitorHelper.doWork();
6669
} catch (InterruptedException e) {
6770
throw new RuntimeException(e);
6871
}
69-
};
70-
firstThread = new Thread(first);
71-
secondThread = new Thread(second);
72+
});
7273

74+
/* Start the first thread so that it can then start the second thread. */
7375
firstThread.start();
7476

7577
firstThread.join();
7678
secondThread.join();
7779

78-
// Generate monitor wait events
80+
/* Generate monitor wait events. */
7981
try {
80-
helper.timeout();
82+
monitorHelper.waitUntilTimeout();
8183
} catch (InterruptedException e) {
82-
org.junit.Assert.fail(e.getMessage());
84+
fail(e.getMessage());
8385
}
8486

85-
// Generate thread park events
86-
LockSupport.parkNanos(this, MILLIS);
87+
/* Generate thread park events. */
88+
LockSupport.parkNanos(this, TimeUtils.millisToNanos(MILLIS));
8789

8890
stopRecording(recording, this::validateEvents);
8991
}
@@ -95,30 +97,32 @@ private void validateEvents(List<RecordedEvent> events) {
9597
RecordedClass parkedClass = event.getValue("parkedClass");
9698
if (parkedClass != null) {
9799
String parkedClassName = parkedClass.getName();
98-
assertFalse(parkedClassName.contains("com.oracle.svm.core.monitor"));
100+
assertFalse(parkedClassName.contains("JavaMonitor"));
99101
found = true;
100102
}
101103
}
102104
}
103105
assertTrue("Expected jdk.ThreadPark event not found", found);
104106
}
105107

106-
private final class Helper {
108+
private final class MonitorHelper {
107109
private synchronized void doWork() throws InterruptedException {
108110
if (Thread.currentThread().equals(secondThread)) {
109-
return; // second thread doesn't need to do work.
111+
/* The second thread doesn't need to do any work. */
112+
return;
110113
}
111-
// ensure ordering of critical section entry
114+
115+
/* Start the second thread while this thread holds the lock. */
112116
secondThread.start();
113117

114-
// spin until second thread blocks
118+
/* Spin until the second thread blocks. */
115119
while (!secondThread.getState().equals(Thread.State.BLOCKED) || !passedCheckpoint) {
116120
Thread.sleep(100);
117121
}
118122
Thread.sleep(MILLIS);
119123
}
120124

121-
private synchronized void timeout() throws InterruptedException {
125+
private synchronized void waitUntilTimeout() throws InterruptedException {
122126
wait(MILLIS);
123127
}
124128
}

0 commit comments

Comments
 (0)