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.assertTrue;
  29 
  30 import java.nio.file.Files;
  31 import java.nio.file.Paths;
  32 import java.time.Duration;
  33 import java.time.Instant;
  34 import java.util.List;
  35 import java.util.concurrent.CountDownLatch;
  36 import java.util.concurrent.locks.LockSupport;
  37 import java.util.function.Consumer;
  38 
  39 import jdk.jfr.Recording;
  40 import jdk.jfr.consumer.RecordedEvent;
  41 import jdk.test.lib.Asserts;
  42 import jdk.test.lib.jfr.EventNames;
  43 import jdk.test.lib.jfr.Events;
  44 import jdk.test.lib.management.ThreadMXBeanTool;
  45 import jdk.test.lib.thread.TestThread;
  46 
  47 /**
  48  * @test
  49  * @key jfr
  50  *
  51  * @library /lib /
  52  *
  53  * @run main/othervm jdk.jfr.event.runtime.TestThreadParkEvent
  54  */
  55 
  56 public class TestThreadParkEvent {
  57     private static final String EVENT_NAME = EventNames.ThreadPark;
  58     private static final long THRESHOLD_MILLIS = 1;
  59 
  60     static class Blocker {
  61     }
  62 
  63     public static void main(String[] args) throws Throwable {
  64         testParkNoTimeout();
  65         testParkTimeout();
  66         testParkUntil();
  67     }
  68 
  69     private static void testParkNoTimeout() throws Exception {
  70         RecordedEvent event = testPark(x -> LockSupport.park(x), Thread.State.WAITING);
  71         Events.assertMissingValue(event, "timeout");
  72         Events.assertMissingValue(event, "until");
  73     }
  74 
  75     private static void testParkTimeout() throws Exception {
  76         Duration expected = Duration.ofNanos(1_234_567_890_123L);
  77         RecordedEvent event = testPark(x -> LockSupport.parkNanos(x, expected.toNanos()), Thread.State.TIMED_WAITING);
  78         Events.assertDuration(event, "timeout", expected);
  79         Events.assertMissingValue(event, "until");
  80     }
  81 
  82     private static void testParkUntil() throws Exception {
  83         long epochMillis =  Instant.now().plusSeconds(1000000).toEpochMilli();
  84         RecordedEvent event = testPark(x -> LockSupport.parkUntil(x, epochMillis), Thread.State.TIMED_WAITING);
  85         Events.assertMissingValue(event, "timeout");
  86         Events.assertInstant(event, "until", Instant.ofEpochMilli(epochMillis));
  87     }
  88 
  89     static RecordedEvent testPark(Consumer<Blocker> parkOperation, Thread.State threadState) throws Exception {
  90 
  91         final CountDownLatch stop = new CountDownLatch(1);
  92         final Blocker blocker = new Blocker();
  93         TestThread parkThread = new TestThread(new Runnable() {
  94             public void run() {
  95                 while (stop.getCount() > 0) {
  96                     parkOperation.accept(blocker);
  97                 }
  98             }
  99         });
 100 
 101         Recording recording = new Recording();
 102         recording.enable(EVENT_NAME).withThreshold(Duration.ofMillis(THRESHOLD_MILLIS));
 103         try {
 104             recording.start();
 105             parkThread.start();
 106             ThreadMXBeanTool.waitUntilBlockingOnObject(parkThread, threadState, blocker);
 107             // sleep so we know the event is recorded
 108             Thread.sleep(2 * THRESHOLD_MILLIS);
 109         } finally {
 110             stop.countDown();
 111             LockSupport.unpark(parkThread);
 112             parkThread.join();
 113             recording.stop();
 114         }
 115         List<RecordedEvent> events = Events.fromRecording(recording);
 116         Events.hasEvents(events);
 117         RecordedEvent foundEvent = null;
 118         for (RecordedEvent event : events) {
 119             System.out.println("Event:" + event);
 120             String klassName = Events.assertField(event, "parkedClass.name").notNull().getValue();
 121             if (klassName.equals(blocker.getClass().getName().replace('.', '/'))) {
 122                 Asserts.assertNull(foundEvent , "Found more than 1 event");
 123                 Events.assertField(event, "address").notEqual(0L);
 124                 Events.assertEventThread(event, parkThread);
 125                 foundEvent = event;
 126             }
 127         }
 128         Asserts.assertNotNull(foundEvent, "Correct event not found");
 129         return foundEvent;
 130     }
 131 
 132 }