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 java.time.Duration; 29 import java.util.List; 30 import java.util.concurrent.CountDownLatch; 31 32 import jdk.jfr.Recording; 33 import jdk.jfr.consumer.RecordedEvent; 34 import jdk.testlibrary.jfr.EventNames; 35 import jdk.testlibrary.jfr.Events; 36 37 /* 38 * @test 39 * @key jfr 40 * @library /lib/testlibrary 41 * @run main/othervm jdk.jfr.event.runtime.TestThreadStartEndEvents 42 */ 43 44 /** 45 * Starts and stops a number of threads in order. 46 * Verifies that events are in the same order. 47 */ 48 public class TestThreadStartEndEvents { 49 private final static String EVENT_NAME_THREAD_START = EventNames.ThreadStart; 50 private final static String EVENT_NAME_THREAD_END = EventNames.ThreadEnd; 51 private static final String THREAD_NAME_PREFIX = "TestThread-"; 52 53 public static void main(String[] args) throws Throwable { 54 // Test Java Thread Start event 55 Recording recording = new Recording(); 56 recording.enable(EVENT_NAME_THREAD_START).withThreshold(Duration.ofMillis(0)); 57 recording.enable(EVENT_NAME_THREAD_END).withThreshold(Duration.ofMillis(0)); 58 recording.start(); 59 LatchedThread[] threads = startThreads(); 60 stopThreads(threads); 61 recording.stop(); 62 63 int currThreadIndex = 0; 64 long currentThreadId = Thread.currentThread().getId(); 65 List<RecordedEvent> events = Events.fromRecording(recording); 66 Events.hasEvents(events); 67 for (RecordedEvent event : events) { 68 System.out.println("Event:" + event); 69 if (event.getThread().getJavaThreadId() != currentThreadId) { 70 continue; 71 } 72 // Threads should be started and stopped in the correct order. 73 Events.assertEventThread(event, threads[currThreadIndex % threads.length]); 74 String eventName = currThreadIndex < threads.length ? EVENT_NAME_THREAD_START : EVENT_NAME_THREAD_END; 75 if (!eventName.equals(event.getEventType().getName())) { 76 throw new Exception("Expected event of tyoe " + eventName + " but got " + event.getEventType().getName()); 77 } 78 currThreadIndex++; 79 } 80 } 81 82 private static LatchedThread[] startThreads() { 83 LatchedThread threads[] = new LatchedThread[10]; 84 ThreadGroup threadGroup = new ThreadGroup("TestThreadGroup"); 85 for (int i = 0; i < threads.length; i++) { 86 threads[i] = new LatchedThread(threadGroup, THREAD_NAME_PREFIX + i); 87 threads[i].startThread(); 88 System.out.println("Started thread id=" + threads[i].getId()); 89 } 90 return threads; 91 } 92 93 private static void stopThreads(LatchedThread[] threads) { 94 for (LatchedThread thread : threads) { 95 thread.stopThread(); 96 while (thread.isAlive()) { 97 try { 98 Thread.sleep(5); 99 } catch (InterruptedException e) { 100 e.printStackTrace(); 101 } 102 } 103 } 104 } 105 106 private static class LatchedThread extends Thread { 107 private final CountDownLatch start = new CountDownLatch(1); 108 private final CountDownLatch stop = new CountDownLatch(1); 109 110 public LatchedThread(ThreadGroup threadGroup, String name) { 111 super(threadGroup, name); 112 } 113 114 public void run() { 115 start.countDown(); 116 try { 117 stop.await(); 118 } catch (InterruptedException e) { 119 e.printStackTrace(); 120 } 121 } 122 123 public void startThread() { 124 this.start(); 125 try { 126 start.await(); 127 } catch (InterruptedException e) { 128 e.printStackTrace(); 129 } 130 } 131 132 public void stopThread() { 133 stop.countDown(); 134 } 135 } 136 137 }