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