1 /* 2 * Copyright (c) 2013, 2019, 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 java.util.List; 29 30 import jdk.jfr.Recording; 31 import jdk.jfr.consumer.RecordedClass; 32 import jdk.jfr.consumer.RecordedEvent; 33 import jdk.jfr.consumer.RecordedThread; 34 import jdk.testlibrary.Asserts; 35 import jdk.testlibrary.jfr.EventNames; 36 import jdk.testlibrary.jfr.Events; 37 38 /* 39 * @test 40 * @key jfr 41 * @library /lib/testlibrary 42 * @run main/othervm jdk.jfr.event.runtime.TestJavaMonitorWaitTimeOut 43 */ 44 public class TestJavaMonitorWaitTimeOut { 45 46 static final class Lock { 47 48 } 49 50 private static final String THREAD_NAME = "Notifier"; 51 52 static class Notifierhread extends Thread { 53 private final Object lock; 54 55 Notifierhread(Object lock) { 56 super(THREAD_NAME); 57 this.lock = lock; 58 } 59 60 public void run() { 61 synchronized (lock) { 62 lock.notify(); 63 } 64 } 65 } 66 67 public static void main(String[] args) throws Throwable { 68 try (Recording recording = new Recording()) { 69 recording.enable(EventNames.JavaMonitorWait).withoutThreshold().withoutStackTrace(); 70 recording.start(); 71 Lock lock = new Lock(); 72 73 synchronized (lock) { 74 // event without notifier, should be null 75 lock.wait(10); 76 } 77 Notifierhread s = new Notifierhread(lock); 78 79 synchronized (lock) { 80 s.start(); 81 // event with a notifier 82 lock.wait(1_000_000); 83 s.join(); 84 } 85 86 synchronized (lock) { 87 // event without a notifier, should be null 88 lock.wait(11); 89 } 90 recording.stop(); 91 List<RecordedEvent> events = Events.fromRecording(recording); 92 for (RecordedEvent e : events) { 93 if (isWaitEvent(e)) { 94 System.out.println(e); 95 } 96 } 97 assertTimeOutEvent(events, 10, null); 98 assertTimeOutEvent(events, 1_000_000, THREAD_NAME); 99 assertTimeOutEvent(events, 11, null); 100 } 101 } 102 103 private static boolean isWaitEvent(RecordedEvent event) { 104 RecordedClass t = event.getValue("monitorClass"); 105 return t != null && t.getName().equals(Lock.class.getName()); 106 } 107 108 private static void assertTimeOutEvent(List<RecordedEvent> events, long timeout, String expectedThreadName) { 109 for (RecordedEvent e : events) { 110 if (isWaitEvent(e)) { 111 Long l = e.getValue("timeout"); 112 if (l == timeout) { 113 RecordedThread notifier = e.getValue("notifier"); 114 String threadName = null; 115 if (notifier != null) { 116 threadName = notifier.getJavaName(); 117 } 118 Asserts.assertEquals(threadName, expectedThreadName, "Invalid thread"); 119 return; 120 } 121 } 122 } 123 Asserts.fail("Could not find event with monitorClass" + Lock.class.getName()); 124 } 125 }