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 static jdk.testlibrary.Asserts.assertFalse;
  29 import static jdk.testlibrary.Asserts.assertTrue;
  30 
  31 import java.time.Duration;
  32 import java.util.List;
  33 import java.util.concurrent.CountDownLatch;
  34 import java.util.concurrent.locks.LockSupport;
  35 
  36 import jdk.jfr.Recording;
  37 import jdk.jfr.consumer.RecordedEvent;
  38 import jdk.testlibrary.jfr.EventNames;
  39 import jdk.testlibrary.jfr.Events;
  40 import jdk.testlibrary.management.ThreadMXBeanTool;
  41 import jdk.testlibrary.thread.TestThread;
  42 
  43 
  44 /*
  45  * @test
  46  * @key jfr
  47  * @library /lib/testlibrary
  48  *
  49  * @run main/othervm jdk.jfr.event.runtime.TestThreadParkEvent
  50  */
  51 
  52 public class TestThreadParkEvent {
  53     private static final String EVENT_NAME = EventNames.ThreadPark;
  54     private static final long THRESHOLD_MILLIS = 1;
  55 
  56     static class Blocker {
  57     }
  58 
  59     public static void main(String[] args) throws Throwable {
  60         final CountDownLatch stop = new CountDownLatch(1);
  61         final Blocker blocker = new Blocker();
  62         TestThread parkThread = new TestThread(new Runnable() {
  63             public void run() {
  64                 while (stop.getCount() > 0) {
  65                     LockSupport.park(blocker);
  66                 }
  67             }
  68         });
  69 
  70         Recording recording = new Recording();
  71         recording.enable(EVENT_NAME).withThreshold(Duration.ofMillis(THRESHOLD_MILLIS));
  72         try {
  73             recording.start();
  74             parkThread.start();
  75             ThreadMXBeanTool.waitUntilBlockingOnObject(parkThread, Thread.State.WAITING, blocker);
  76             // sleep so we know the event is recorded
  77             Thread.sleep(2 * THRESHOLD_MILLIS);
  78         } finally {
  79             stop.countDown();
  80             LockSupport.unpark(parkThread);
  81             parkThread.join();
  82             recording.stop();
  83         }
  84 
  85         List<RecordedEvent> events = Events.fromRecording(recording);
  86         Events.hasEvents(events);
  87         boolean isAnyFound = false;
  88         for (RecordedEvent event : events) {
  89             System.out.println("Event:" + event);
  90             String klassName = Events.assertField(event, "parkedClass.name").notNull().getValue();
  91             if (klassName.equals(blocker.getClass().getName().replace('.', '/'))) {
  92                 assertFalse(isAnyFound, "Found more than 1 event");
  93                 isAnyFound = true;
  94                 Events.assertField(event, "timeout").equal(0L);
  95                 Events.assertField(event, "address").notEqual(0L);
  96                 Events.assertEventThread(event, parkThread);
  97             }
  98         }
  99         assertTrue(isAnyFound, "Correct event not found");
 100     }
 101 
 102 }