|
1 /* |
|
2 * Copyright (c) 2013, 2018, 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. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package jdk.jfr.event.runtime; |
|
27 |
|
28 import static jdk.test.lib.Asserts.assertFalse; |
|
29 import static jdk.test.lib.Asserts.assertTrue; |
|
30 |
|
31 import java.time.Duration; |
|
32 import java.util.List; |
|
33 import java.util.concurrent.CountDownLatch; |
|
34 |
|
35 import jdk.jfr.Recording; |
|
36 import jdk.jfr.consumer.RecordedEvent; |
|
37 import jdk.test.lib.jfr.EventNames; |
|
38 import jdk.test.lib.jfr.Events; |
|
39 import jdk.test.lib.management.ThreadMXBeanTool; |
|
40 import jdk.test.lib.thread.TestThread; |
|
41 import jdk.test.lib.thread.XRun; |
|
42 |
|
43 /* |
|
44 * @test |
|
45 * @key jfr |
|
46 * @library /test/lib |
|
47 * |
|
48 * @run main/othervm jdk.jfr.event.runtime.TestJavaBlockedEvent |
|
49 */ |
|
50 public class TestJavaBlockedEvent { |
|
51 private static final String EVENT_NAME = EventNames.JavaMonitorEnter; |
|
52 private static final long THRESHOLD_MILLIS = 1; |
|
53 |
|
54 static class Lock { |
|
55 } |
|
56 |
|
57 public static void main(String[] args) throws Throwable { |
|
58 Recording recording = new Recording(); |
|
59 |
|
60 recording.enable(EVENT_NAME).withThreshold(Duration.ofMillis(THRESHOLD_MILLIS)); |
|
61 recording.start(); |
|
62 final Lock lock = new Lock(); |
|
63 final CountDownLatch blockerInLock = new CountDownLatch(1); |
|
64 final CountDownLatch blockingFinished = new CountDownLatch(1); |
|
65 final CountDownLatch blockedFinished = new CountDownLatch(1); |
|
66 TestThread blockerThread = new TestThread(new XRun() { |
|
67 @Override |
|
68 public void xrun() throws Throwable { |
|
69 synchronized (lock) { |
|
70 blockerInLock.countDown(); |
|
71 blockingFinished.await(); |
|
72 } |
|
73 blockedFinished.await(); |
|
74 } |
|
75 }); |
|
76 |
|
77 TestThread blockedThread = new TestThread(new XRun() { |
|
78 @Override |
|
79 public void xrun() throws Throwable { |
|
80 blockerInLock.await(); |
|
81 synchronized (lock) { |
|
82 blockedFinished.countDown(); |
|
83 } |
|
84 } |
|
85 }); |
|
86 blockerThread.start(); |
|
87 blockedThread.start(); |
|
88 |
|
89 blockerInLock.await(); |
|
90 ThreadMXBeanTool.waitUntilBlockingOnObject(blockedThread, Thread.State.BLOCKED, lock); |
|
91 Thread.sleep(2 * THRESHOLD_MILLIS); |
|
92 blockingFinished.countDown(); |
|
93 blockedFinished.await(); |
|
94 |
|
95 blockedThread.join(); |
|
96 blockerThread.join(); |
|
97 recording.stop(); |
|
98 |
|
99 List<RecordedEvent> events = Events.fromRecording(recording); |
|
100 boolean isAnyFound = false; |
|
101 for (RecordedEvent event : events) { |
|
102 System.out.println("Event:" + event); |
|
103 if (event.getThread().getJavaThreadId() == blockedThread.getId()) { |
|
104 if (isMyLock(Events.assertField(event, "monitorClass.name").getValue())) { |
|
105 Events.assertEventThread(event, "previousOwner", blockerThread); |
|
106 Events.assertField(event, "address").above(0L); |
|
107 assertFalse(isAnyFound, "Found multiple events"); |
|
108 isAnyFound = true; |
|
109 } |
|
110 } |
|
111 } |
|
112 assertTrue(isAnyFound, "No blocking event from " + blockedThread.getName()); |
|
113 } |
|
114 |
|
115 private static boolean isMyLock(String className) { |
|
116 return Lock.class.getName().replace('.', '/').equals(className); |
|
117 } |
|
118 } |