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