Skip to content

Commit b75d7ac

Browse files
committed
8351187: Add JFR monitor notification event
Reviewed-by: dholmes, lmesnik, mgronlun
1 parent 26f7833 commit b75d7ac

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

src/jdk.jfr/share/conf/jfr/default.jfc

+6
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@
9595
<setting name="threshold" control="locking-threshold">20 ms</setting>
9696
</event>
9797

98+
<event name="jdk.JavaMonitorNotify">
99+
<setting name="enabled">false</setting>
100+
<setting name="stackTrace">true</setting>
101+
<setting name="threshold">0 ms</setting>
102+
</event>
103+
98104
<event name="jdk.JavaMonitorInflate">
99105
<setting name="enabled">false</setting>
100106
<setting name="stackTrace">true</setting>

src/jdk.jfr/share/conf/jfr/profile.jfc

+6
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@
9595
<setting name="threshold" control="locking-threshold">10 ms</setting>
9696
</event>
9797

98+
<event name="jdk.JavaMonitorNotify">
99+
<setting name="enabled">false</setting>
100+
<setting name="stackTrace">true</setting>
101+
<setting name="threshold">0 ms</setting>
102+
</event>
103+
98104
<event name="jdk.JavaMonitorInflate">
99105
<setting name="enabled">false</setting>
100106
<setting name="stackTrace">true</setting>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright (c) 2013, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
package jdk.jfr.event.runtime;
25+
26+
import static jdk.test.lib.Asserts.assertFalse;
27+
import static jdk.test.lib.Asserts.assertTrue;
28+
29+
import java.time.Duration;
30+
import java.util.List;
31+
import java.util.concurrent.CountDownLatch;
32+
import java.util.concurrent.CopyOnWriteArrayList;
33+
34+
import jdk.jfr.Recording;
35+
import jdk.jfr.consumer.RecordingStream;
36+
import jdk.jfr.consumer.RecordedClass;
37+
import jdk.jfr.consumer.RecordedEvent;
38+
import jdk.test.lib.jfr.EventNames;
39+
import jdk.test.lib.jfr.Events;
40+
import jdk.test.lib.thread.TestThread;
41+
import jdk.test.lib.thread.XRun;
42+
43+
/**
44+
* @test
45+
* @requires vm.flagless
46+
* @requires vm.hasJFR
47+
* @library /test/lib
48+
* @run main/othervm jdk.jfr.event.runtime.TestJavaMonitorNotifyEvent
49+
*/
50+
public class TestJavaMonitorNotifyEvent {
51+
52+
private static final String FIELD_KLASS_NAME = "monitorClass.name";
53+
private static final String FIELD_ADDRESS = "address";
54+
private static final String FIELD_NOTIFIED_COUNT = "notifiedCount";
55+
56+
private final static String EVENT_NAME = EventNames.JavaMonitorNotify;
57+
private static final long WAIT_TIME = 123456;
58+
59+
static class Lock {
60+
}
61+
62+
public static void main(String[] args) throws Throwable {
63+
final Lock lock = new Lock();
64+
final String lockClassName = lock.getClass().getName().replace('.', '/');
65+
final long mainThreadId = Thread.currentThread().threadId();
66+
67+
List<RecordedEvent> events = new CopyOnWriteArrayList<>();
68+
try (RecordingStream rs = new RecordingStream()) {
69+
rs.enable(EVENT_NAME).withoutThreshold();
70+
rs.onEvent(EVENT_NAME, e -> {
71+
long threadId = e.getThread().getJavaThreadId();
72+
Object clazz = e.getValue(FIELD_KLASS_NAME);
73+
if (clazz.equals(lockClassName) && (threadId == mainThreadId)) {
74+
events.add(e);
75+
rs.close();
76+
}
77+
});
78+
rs.startAsync();
79+
80+
final CountDownLatch latch = new CountDownLatch(1);
81+
TestThread waitThread = new TestThread(new XRun() {
82+
@Override
83+
public void xrun() throws Throwable {
84+
synchronized (lock) {
85+
latch.countDown();
86+
lock.wait(WAIT_TIME);
87+
}
88+
}
89+
});
90+
try {
91+
waitThread.start();
92+
latch.await();
93+
synchronized (lock) {
94+
lock.notifyAll();
95+
}
96+
} finally {
97+
waitThread.join();
98+
}
99+
100+
rs.awaitTermination();
101+
102+
System.out.println(events);
103+
assertFalse(events.isEmpty());
104+
for (RecordedEvent ev : events) {
105+
Events.assertField(ev, FIELD_ADDRESS).notEqual(0L);
106+
Events.assertField(ev, FIELD_NOTIFIED_COUNT).equal(1);
107+
}
108+
}
109+
}
110+
}

test/lib/jdk/test/lib/jfr/EventNames.java

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class EventNames {
5858
public static final String ThreadPark = PREFIX + "ThreadPark";
5959
public static final String JavaMonitorEnter = PREFIX + "JavaMonitorEnter";
6060
public static final String JavaMonitorWait = PREFIX + "JavaMonitorWait";
61+
public static final String JavaMonitorNotify = PREFIX + "JavaMonitorNotify";
6162
public static final String JavaMonitorInflate = PREFIX + "JavaMonitorInflate";
6263
public static final String JavaMonitorDeflate = PREFIX + "JavaMonitorDeflate";
6364
public static final String JavaMonitorStatistics = PREFIX + "JavaMonitorStatistics";

0 commit comments

Comments
 (0)