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