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