1 /*
 2  * Copyright (c) 2020, 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.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 package jdk.jfr.event.runtime;
25 
26 import java.util.List;
27 
28 import jdk.jfr.Recording;
29 import jdk.jfr.consumer.RecordedEvent;
30 import jdk.jfr.consumer.RecordedThread;
31 import jdk.test.lib.Asserts;
32 import jdk.test.lib.jfr.EventNames;
33 import jdk.test.lib.jfr.Events;
34 
35 /**
36  * @test
37  * @key jfr
38  * @requires vm.hasJFR
39  * @library /test/lib
40  * @compile --enable-preview -source ${jdk.version} TestThreadStartEvent.java LatchedThread.java
41  * @run main/othervm --enable-preview jdk.jfr.event.runtime.TestThreadStartEvent
42  */
43 public class TestThreadStartEvent {
44     private final static String EVENT_NAME_THREAD_START = EventNames.ThreadStart;
45 
46     public static void main(String[] args) throws Throwable {
47         try (Recording recording = new Recording()) {
48             recording.enable(EVENT_NAME_THREAD_START);
49 
50             // Start a thread before recording
51             LatchedThread beforeThread = new LatchedThread("Before Thread");
52             beforeThread.start();
53             beforeThread.awaitStarted();
54             recording.start();
55 
56             // Start and end a thread during recording
57             LatchedThread duringThread = new LatchedThread("During Thread");
58             duringThread.start();
59             duringThread.stopAndJoin();
60 
61             // Start a thread and end it after the recording has stopped
62             LatchedThread afterThread = new LatchedThread("After Thread");
63             afterThread.start();
64             afterThread.awaitStarted();
65 
66             recording.stop();
67             afterThread.stopAndJoin();
68 
69             List<RecordedEvent> events = Events.fromRecording(recording);
70             assertEvent(events, duringThread);
71             assertEvent(events, afterThread);
72             Asserts.assertNull(findEventByThreadName(events, beforeThread.getName()));
73         }
74     }
75 
76     private static void assertEvent(List<RecordedEvent> events, LatchedThread thread) throws Exception {
77         RecordedEvent event = findEventByThreadName(events, thread.getName());
78         System.out.println(event);
79         RecordedThread t = event.getThread();
80         Thread current = Thread.currentThread();
81         Events.assertFrame(event, TestThreadStartEvent.class, "main");
82         Asserts.assertEquals(event.getThread("thread").getJavaName(), thread.getName());
83         Asserts.assertEquals(event.getThread("parentThread").getJavaName(), current.getName());
84         Asserts.assertEquals(t.getThreadGroup().getName(), LatchedThread.THREAD_GROUP.getName());
85         Asserts.assertEquals(t.isVirtual(), false);
86     }
87 
88     private static RecordedEvent findEventByThreadName(List<RecordedEvent> events, String name) {
89         for (RecordedEvent e : events) {
90             if (e.getThread().getJavaName().equals(name)) {
91                 return e;
92             }
93         }
94         return null;
95     }
96 }