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