author | mgronlun |
Wed, 30 Oct 2019 19:43:52 +0100 | |
changeset 58863 | c16ac7a2eba4 |
parent 55199 | 3a09397c147e |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
55199
3a09397c147e
8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
mseledtsov
parents:
51214
diff
changeset
|
2 |
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. |
50113 | 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.io.File; |
|
29 |
import java.io.IOException; |
|
30 |
import java.time.Duration; |
|
31 |
import java.util.ArrayList; |
|
32 |
import java.util.List; |
|
33 |
||
34 |
import jdk.jfr.AnnotationElement; |
|
35 |
import jdk.jfr.Event; |
|
36 |
import jdk.jfr.EventType; |
|
37 |
import jdk.jfr.FlightRecorder; |
|
38 |
import jdk.jfr.Recording; |
|
39 |
import jdk.jfr.ValueDescriptor; |
|
40 |
import jdk.jfr.consumer.RecordedEvent; |
|
41 |
import jdk.jfr.consumer.RecordingFile; |
|
55199
3a09397c147e
8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
mseledtsov
parents:
51214
diff
changeset
|
42 |
import jdk.test.lib.Utils; |
50113 | 43 |
|
51214 | 44 |
/** |
50113 | 45 |
* @test TestGetThreadId |
46 |
* @key jfr |
|
51214 | 47 |
* @requires vm.hasJFR |
50113 | 48 |
* @library /test/lib |
49 |
* @modules jdk.jfr/jdk.jfr.internal |
|
50 |
* @run main/othervm jdk.jfr.jvm.TestJavaEvent |
|
51 |
*/ |
|
52 |
public class TestJavaEvent { |
|
53 |
||
54 |
private static final int EVENTS_PER_THREAD = 50; |
|
55 |
private static final int THREAD_COUNT = 100; |
|
56 |
||
57 |
public static class MyEvent extends Event { |
|
58 |
float floatValue; |
|
59 |
double doubleValue; |
|
60 |
int intValue; |
|
61 |
long longValue; |
|
62 |
char charValue; |
|
63 |
byte byteValue; |
|
64 |
String stringValue; |
|
65 |
Thread threadValue; |
|
66 |
Class<?> classValue; |
|
67 |
||
68 |
public void setFloatValue(float value) { |
|
69 |
floatValue = value; |
|
70 |
} |
|
71 |
||
72 |
public void setDoubleValue(double value) { |
|
73 |
doubleValue = value; |
|
74 |
} |
|
75 |
||
76 |
public void setIntValue(int value) { |
|
77 |
intValue = value; |
|
78 |
} |
|
79 |
||
80 |
public void setLongValue(long value) { |
|
81 |
longValue = value; |
|
82 |
} |
|
83 |
||
84 |
public void setCharValue(char value) { |
|
85 |
charValue = value; |
|
86 |
} |
|
87 |
||
88 |
public void setByteValue(byte value) { |
|
89 |
byteValue = value; |
|
90 |
} |
|
91 |
||
92 |
public void setStringValue(String value) { |
|
93 |
stringValue = value; |
|
94 |
} |
|
95 |
||
96 |
public void setThreadValue(Thread value) { |
|
97 |
threadValue = value; |
|
98 |
} |
|
99 |
||
100 |
public void setClassValue(Class<?> value) { |
|
101 |
classValue = value; |
|
102 |
} |
|
103 |
} |
|
104 |
||
105 |
public static void main(String... args) throws IOException, InterruptedException { |
|
106 |
Recording r = new Recording(); |
|
107 |
r.enable(MyEvent.class).withThreshold(Duration.ofNanos(0)).withoutStackTrace(); |
|
108 |
r.start(); |
|
109 |
List<Thread> threads = new ArrayList<>(); |
|
110 |
for (int n = 0; n < THREAD_COUNT; n++) { |
|
111 |
Thread t = new Thread(TestJavaEvent::emitEvents); |
|
112 |
threads.add(t); |
|
113 |
t.start(); |
|
114 |
} |
|
115 |
for (Thread t : threads) { |
|
116 |
t.join(); |
|
117 |
} |
|
118 |
||
119 |
r.stop(); |
|
120 |
// prettyPrint(); |
|
55199
3a09397c147e
8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
mseledtsov
parents:
51214
diff
changeset
|
121 |
File file = Utils.createTempFile("test", ".jfr").toFile(); |
50113 | 122 |
r.dump(file.toPath()); |
123 |
int eventCount = 0; |
|
124 |
for (RecordedEvent e : RecordingFile.readAllEvents(file.toPath())) { |
|
125 |
if (e.getEventType().getName().equals(MyEvent.class.getName())) { |
|
126 |
eventCount++; |
|
127 |
} |
|
128 |
System.out.println(e); |
|
129 |
} |
|
130 |
System.out.println("Event count was " + eventCount + ", expected " + THREAD_COUNT * EVENTS_PER_THREAD); |
|
131 |
r.close(); |
|
132 |
} |
|
133 |
||
134 |
private static void emitEvents() { |
|
135 |
for (int n = 0; n < EVENTS_PER_THREAD; n++) { |
|
136 |
MyEvent event = new MyEvent(); |
|
137 |
event.begin(); |
|
138 |
event.end(); |
|
139 |
event.setFloatValue(1.12345f); |
|
140 |
event.setDoubleValue(1.234567890); |
|
141 |
event.setIntValue(123456); |
|
142 |
event.setLongValue(1234567890); |
|
143 |
event.setCharValue('c'); |
|
144 |
event.setByteValue((byte) 12); |
|
145 |
event.setStringValue("1234567890"); |
|
146 |
event.setThreadValue(Thread.currentThread()); |
|
147 |
event.setClassValue(Class.class); |
|
148 |
event.commit(); |
|
149 |
try { |
|
150 |
Thread.sleep(1); |
|
151 |
} catch (InterruptedException e) { |
|
152 |
// TODO Auto-generated catch block |
|
153 |
e.printStackTrace(); |
|
154 |
} |
|
155 |
} |
|
156 |
} |
|
157 |
||
158 |
static void prettyPrint() { |
|
159 |
for (EventType type : FlightRecorder.getFlightRecorder().getEventTypes()) { |
|
160 |
for (AnnotationElement a : type.getAnnotationElements()) { |
|
161 |
printAnnotation("", a); |
|
162 |
} |
|
163 |
System.out.print("class " + removePackage(type.getName())); |
|
164 |
System.out.print(" extends Event"); |
|
165 |
||
166 |
System.out.println(" {"); |
|
167 |
List<ValueDescriptor> values = type.getFields(); |
|
168 |
for (int i = 0; i < values.size(); i++) { |
|
169 |
ValueDescriptor v = values.get(i); |
|
170 |
for (AnnotationElement a : v.getAnnotationElements()) { |
|
171 |
printAnnotation(" ", a); |
|
172 |
} |
|
173 |
System.out.println(" " + removePackage(v.getTypeName() + brackets(v.isArray())) + " " + v.getName()); |
|
174 |
if (i != values.size() - 1) { |
|
175 |
System.out.println(); |
|
176 |
} |
|
177 |
} |
|
178 |
System.out.println("}"); |
|
179 |
System.out.println(); |
|
180 |
} |
|
181 |
} |
|
182 |
||
183 |
private static String brackets(boolean isArray) { |
|
184 |
return isArray ? "[]" : ""; |
|
185 |
} |
|
186 |
||
187 |
private static String removePackage(String s) { |
|
188 |
||
189 |
int index = s.lastIndexOf("."); |
|
190 |
return s.substring(index + 1); |
|
191 |
} |
|
192 |
||
193 |
private static void printAnnotation(String indent, AnnotationElement a) { |
|
194 |
String name = removePackage(a.getTypeName()); |
|
195 |
if (a.getValues().isEmpty()) { |
|
196 |
System.out.println(indent + "@" + name); |
|
197 |
return; |
|
198 |
} |
|
199 |
System.out.print(indent + "@" + name + "("); |
|
200 |
for (Object o : a.getValues()) { |
|
201 |
printAnnotationValue(o); |
|
202 |
} |
|
203 |
System.out.println(")"); |
|
204 |
} |
|
205 |
||
206 |
private static void printAnnotationValue(Object o) { |
|
207 |
if (o instanceof String) { |
|
208 |
System.out.print("\"" + o + "\""); |
|
209 |
} else { |
|
210 |
System.out.print(String.valueOf(o)); |
|
211 |
} |
|
212 |
} |
|
213 |
||
214 |
} |