1 /* 2 * Copyright (c) 2016, 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.assertTrue; 29 30 import java.nio.file.Paths; 31 import java.time.Duration; 32 import java.util.concurrent.CountDownLatch; 33 34 import jdk.jfr.Recording; 35 import jdk.jfr.consumer.RecordedEvent; 36 import jdk.test.lib.jfr.EventNames; 37 import jdk.test.lib.jfr.Events; 38 import jdk.test.lib.thread.TestThread; 39 import jdk.test.lib.thread.XRun; 40 41 /* 42 * @test TestJavaMonitorInflateEvent 43 * @key jfr 44 * @library /test/lib 45 * @run main/othervm jdk.jfr.event.runtime.TestJavaMonitorInflateEvent 46 */ 47 public class TestJavaMonitorInflateEvent { 48 49 private static final String FIELD_KLASS_NAME = "monitorClass.name"; 50 private static final String FIELD_ADDRESS = "address"; 51 private static final String FIELD_CAUSE = "cause"; 52 53 private static final String EVENT_NAME = EventNames.JavaMonitorInflate; 54 private static final long WAIT_TIME = 123456; 55 56 static class Lock { 57 } 58 59 public static void main(String[] args) throws Exception { 60 Recording recording = new Recording(); 61 recording.enable(EVENT_NAME).withThreshold(Duration.ofMillis(0)); 62 final Lock lock = new Lock(); 63 final CountDownLatch latch = new CountDownLatch(1); 64 // create a thread that waits 65 TestThread waitThread = new TestThread(new XRun() { 66 @Override 67 public void xrun() throws Throwable { 68 synchronized (lock) { 69 latch.countDown(); 70 lock.wait(WAIT_TIME); 71 } 72 } 73 }); 74 try { 75 recording.start(); 76 waitThread.start(); 77 latch.await(); 78 synchronized (lock) { 79 lock.notifyAll(); 80 } 81 } finally { 82 waitThread.join(); 83 recording.stop(); 84 } 85 final String thisThreadName = Thread.currentThread().getName(); 86 final String waitThreadName = waitThread.getName(); 87 final String lockClassName = lock.getClass().getName().replace('.', '/'); 88 boolean isAnyFound = false; 89 try { 90 // Find at least one event with the correct monitor class and check the other fields 91 for (RecordedEvent event : Events.fromRecording(recording)) { 92 assertTrue(EVENT_NAME.equals(event.getEventType().getName()), "mismatched event types?"); 93 // Check recorded inflation event is associated with the Lock class used in the test 94 final String recordedMonitorClassName = Events.assertField(event, FIELD_KLASS_NAME).getValue(); 95 if (!lockClassName.equals(recordedMonitorClassName)) { 96 continue; 97 } 98 // Check recorded thread matches one of the threads in the test 99 final String recordedThreadName = event.getThread().getJavaName(); 100 if (!(recordedThreadName.equals(waitThreadName) || recordedThreadName.equals(thisThreadName))) { 101 continue; 102 } 103 Events.assertField(event, FIELD_ADDRESS).notEqual(0L); 104 Events.assertField(event, FIELD_CAUSE).notNull(); 105 isAnyFound = true; 106 break; 107 } 108 assertTrue(isAnyFound, "Expected an inflation event from test"); 109 } catch (Throwable e) { 110 recording.dump(Paths.get("failed.jfr")); 111 throw e; 112 } finally { 113 recording.close(); 114 } 115 } 116 }