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  *
  47  * @library /lib /
  48  *
  49  * @run main/othervm jdk.jfr.event.runtime.TestJavaBlockedEvent
  50  */
  51 public class TestJavaBlockedEvent {
  52     private static final String EVENT_NAME = EventNames.JavaMonitorEnter;
  53     private static final long THRESHOLD_MILLIS = 1;
  54 
  55     static class Lock {
  56     }
  57 
  58     public static void main(String[] args) throws Throwable {
  59         Recording recording = new Recording();
  60 
  61         recording.enable(EVENT_NAME).withThreshold(Duration.ofMillis(THRESHOLD_MILLIS));
  62         recording.start();
  63         final Lock lock = new Lock();
  64         final CountDownLatch blockerInLock = new CountDownLatch(1);
  65         final CountDownLatch blockingFinished = new CountDownLatch(1);
  66         final CountDownLatch blockedFinished = new CountDownLatch(1);
  67         TestThread blockerThread = new TestThread(new XRun() {
  68             @Override
  69             public void xrun() throws Throwable {
  70                 synchronized (lock) {
  71                     blockerInLock.countDown();
  72                     blockingFinished.await();
  73                 }
  74                 blockedFinished.await();
  75             }
  76         });
  77 
  78         TestThread blockedThread = new TestThread(new XRun() {
  79             @Override
  80             public void xrun() throws Throwable {
  81                 blockerInLock.await();
  82                 synchronized (lock) {
  83                     blockedFinished.countDown();
  84                 }
  85             }
  86         });
  87         blockerThread.start();
  88         blockedThread.start();
  89 
  90         blockerInLock.await();
  91         ThreadMXBeanTool.waitUntilBlockingOnObject(blockedThread, Thread.State.BLOCKED, lock);
  92         Thread.sleep(2 * THRESHOLD_MILLIS);
  93         blockingFinished.countDown();
  94         blockedFinished.await();
  95 
  96         blockedThread.join();
  97         blockerThread.join();
  98         recording.stop();
  99 
 100         List<RecordedEvent> events = Events.fromRecording(recording);
 101         boolean isAnyFound = false;
 102         for (RecordedEvent event : events) {
 103             System.out.println("Event:" + event);
 104             if (event.getThread().getJavaThreadId() == blockedThread.getId()) {
 105                 if (isMyLock(Events.assertField(event, "monitorClass.name").getValue())) {
 106                     Events.assertEventThread(event, "previousOwner", blockerThread);
 107                     Events.assertField(event, "address").above(0L);
 108                     assertFalse(isAnyFound, "Found multiple events");
 109                     isAnyFound = true;
 110                 }
 111             }
 112         }
 113         assertTrue(isAnyFound, "No blocking event from " + blockedThread.getName());
 114     }
 115 
 116     private static boolean isMyLock(String className) {
 117         return Lock.class.getName().replace('.', '/').equals(className);
 118     }
 119 }