author | egahlin |
Wed, 14 Aug 2019 20:34:11 +0200 | |
branch | JEP-349-branch |
changeset 57747 | c942aab1fbec |
parent 51214 | 67736b4846a0 |
child 58076 | ca625d28c580 |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
2 |
* Copyright (c) 2014, 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 |
package jdk.jfr.jvm; |
|
26 |
||
27 |
||
28 |
import java.io.FileReader; |
|
29 |
import java.io.IOException; |
|
30 |
import java.nio.file.Files; |
|
31 |
import java.nio.file.Path; |
|
32 |
import java.nio.file.Paths; |
|
57747 | 33 |
import java.time.Duration; |
50113 | 34 |
import java.util.ArrayList; |
35 |
import java.util.concurrent.Callable; |
|
57747 | 36 |
import java.util.concurrent.atomic.AtomicBoolean; |
50113 | 37 |
|
38 |
import jdk.jfr.AnnotationElement; |
|
39 |
import jdk.jfr.Configuration; |
|
40 |
import jdk.jfr.Description; |
|
41 |
import jdk.jfr.Event; |
|
42 |
import jdk.jfr.EventFactory; |
|
43 |
import jdk.jfr.EventSettings; |
|
44 |
import jdk.jfr.EventType; |
|
45 |
import jdk.jfr.FlightRecorder; |
|
46 |
import jdk.jfr.FlightRecorderListener; |
|
47 |
import jdk.jfr.FlightRecorderPermission; |
|
48 |
import jdk.jfr.Label; |
|
49 |
import jdk.jfr.Recording; |
|
50 |
import jdk.jfr.RecordingState; |
|
51 |
import jdk.jfr.SettingControl; |
|
52 |
import jdk.jfr.ValueDescriptor; |
|
57747 | 53 |
import jdk.jfr.consumer.EventStream; |
50113 | 54 |
import jdk.jfr.consumer.RecordedClass; |
55 |
import jdk.jfr.consumer.RecordedEvent; |
|
56 |
import jdk.jfr.consumer.RecordedFrame; |
|
57 |
import jdk.jfr.consumer.RecordedMethod; |
|
58 |
import jdk.jfr.consumer.RecordedObject; |
|
59 |
import jdk.jfr.consumer.RecordedStackTrace; |
|
60 |
import jdk.jfr.consumer.RecordedThread; |
|
61 |
import jdk.jfr.consumer.RecordedThreadGroup; |
|
62 |
import jdk.jfr.consumer.RecordingFile; |
|
57747 | 63 |
import jdk.jfr.consumer.RecordingStream; |
50113 | 64 |
import jdk.management.jfr.ConfigurationInfo; |
65 |
import jdk.management.jfr.EventTypeInfo; |
|
66 |
import jdk.management.jfr.FlightRecorderMXBean; |
|
67 |
import jdk.management.jfr.RecordingInfo; |
|
68 |
import jdk.management.jfr.SettingDescriptorInfo; |
|
50225
2942ae532175
8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents:
50113
diff
changeset
|
69 |
import jdk.test.lib.Utils; |
50113 | 70 |
|
51214 | 71 |
/** |
50113 | 72 |
* @test TestUnsupportedVM |
73 |
* @key jfr |
|
51214 | 74 |
* @requires vm.hasJFR |
50113 | 75 |
* |
76 |
* @modules jdk.jfr |
|
77 |
* jdk.management.jfr |
|
78 |
* |
|
50251
dc01f2104e46
8203774: jdk/jfr/api/recorder/TestStartStopRecording.java failed due to compilation failed
egahlin
parents:
50225
diff
changeset
|
79 |
* @library /test/lib |
50113 | 80 |
* @run main/othervm -Dprepare-recording=true jdk.jfr.jvm.TestUnsupportedVM |
81 |
* @run main/othervm -Djfr.unsupported.vm=true jdk.jfr.jvm.TestUnsupportedVM |
|
82 |
*/ |
|
83 |
public class TestUnsupportedVM { |
|
84 |
||
85 |
private static Path RECORDING_FILE = Paths.get("recording.jfr"); |
|
86 |
private static Class<?> [] APIClasses = { |
|
87 |
AnnotationElement.class, |
|
88 |
Configuration.class, |
|
89 |
ConfigurationInfo.class, |
|
90 |
Event.class, |
|
91 |
EventFactory.class, |
|
92 |
EventSettings.class, |
|
93 |
EventType.class, |
|
94 |
EventTypeInfo.class, |
|
95 |
FlightRecorder.class, |
|
96 |
FlightRecorderPermission.class, |
|
97 |
FlightRecorderListener.class, |
|
98 |
FlightRecorderMXBean.class, |
|
99 |
RecordedClass.class, |
|
100 |
RecordedEvent.class, |
|
101 |
RecordedFrame.class, |
|
102 |
RecordedMethod.class, |
|
103 |
RecordedObject.class, |
|
104 |
RecordedStackTrace.class, |
|
105 |
RecordedThread.class, |
|
106 |
RecordedThreadGroup.class, |
|
107 |
Recording.class, |
|
108 |
RecordingFile.class, |
|
109 |
RecordingInfo.class, |
|
110 |
RecordingState.class, |
|
111 |
SettingControl.class, |
|
112 |
SettingDescriptorInfo.class, |
|
57747 | 113 |
ValueDescriptor.class, |
114 |
EventStream.class, |
|
115 |
RecordingStream.class |
|
50113 | 116 |
}; |
57747 | 117 |
|
50113 | 118 |
@Label("My Event") |
119 |
@Description("My fine event") |
|
120 |
static class MyEvent extends Event { |
|
121 |
int myValue; |
|
122 |
} |
|
123 |
||
124 |
public static void main(String... args) throws Exception { |
|
125 |
if (Boolean.getBoolean("prepare-recording")) { |
|
126 |
Recording r = new Recording(Configuration.getConfiguration("default")); |
|
127 |
r.start(); |
|
128 |
r.stop(); |
|
129 |
r.dump(RECORDING_FILE); |
|
130 |
r.close(); |
|
131 |
return; |
|
132 |
} |
|
133 |
||
57747 | 134 |
System.out.println("jfr.unsupported.vm=" + System.getProperty("jfr.unsupported.vm")); |
50113 | 135 |
// Class FlightRecorder |
136 |
if (FlightRecorder.isAvailable()) { |
|
137 |
throw new AssertionError("JFR should not be available on an unsupported VM"); |
|
138 |
} |
|
139 |
||
140 |
if (FlightRecorder.isInitialized()) { |
|
141 |
throw new AssertionError("JFR should not be initialized on an unsupported VM"); |
|
142 |
} |
|
143 |
||
144 |
assertIllegalStateException(() -> FlightRecorder.getFlightRecorder()); |
|
57747 | 145 |
assertIllegalStateException(() -> new RecordingStream()); |
50113 | 146 |
assertSwallow(() -> FlightRecorder.addListener(new FlightRecorderListener() {})); |
147 |
assertSwallow(() -> FlightRecorder.removeListener(new FlightRecorderListener() {})); |
|
148 |
assertSwallow(() -> FlightRecorder.register(MyEvent.class)); |
|
149 |
assertSwallow(() -> FlightRecorder.unregister(MyEvent.class)); |
|
150 |
assertSwallow(() -> FlightRecorder.addPeriodicEvent(MyEvent.class, new Runnable() { public void run() {} })); |
|
151 |
assertSwallow(() -> FlightRecorder.removePeriodicEvent(new Runnable() { public void run() {} })); |
|
152 |
||
153 |
// Class Configuration |
|
154 |
if (!Configuration.getConfigurations().isEmpty()) { |
|
155 |
throw new AssertionError("Configuration files should not exist on an unsupported VM"); |
|
156 |
} |
|
50225
2942ae532175
8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents:
50113
diff
changeset
|
157 |
Path jfcFile = Utils.createTempFile("empty", ".jfr"); |
50113 | 158 |
assertIOException(() -> Configuration.getConfiguration("default")); |
159 |
assertIOException(() -> Configuration.create(jfcFile)); |
|
160 |
assertIOException(() -> Configuration.create(new FileReader(jfcFile.toFile()))); |
|
161 |
||
162 |
// Class EventType |
|
163 |
assertInternalError(() -> EventType.getEventType(MyEvent.class)); |
|
164 |
||
165 |
// Class EventFactory |
|
166 |
assertInternalError(() -> EventFactory.create(new ArrayList<>(), new ArrayList<>())); |
|
167 |
||
168 |
// Create a static event |
|
169 |
MyEvent myEvent = new MyEvent(); |
|
170 |
myEvent.begin(); |
|
171 |
myEvent.end(); |
|
172 |
myEvent.shouldCommit(); |
|
173 |
myEvent.commit(); |
|
174 |
||
175 |
// Trigger class initialization failure |
|
176 |
for (Class<?> c : APIClasses) { |
|
177 |
assertNoClassInitFailure(c); |
|
178 |
} |
|
179 |
||
180 |
// jdk.jfr.consumer.* |
|
181 |
// Only run this part of tests if we are on VM |
|
182 |
// that can produce a recording file |
|
183 |
if (Files.exists(RECORDING_FILE)) { |
|
57747 | 184 |
boolean firstFileEvent = true; |
50113 | 185 |
for(RecordedEvent re : RecordingFile.readAllEvents(RECORDING_FILE)) { |
57747 | 186 |
// Print one event |
187 |
if (firstFileEvent) { |
|
188 |
System.out.println(re); |
|
189 |
firstFileEvent = false; |
|
190 |
} |
|
191 |
} |
|
192 |
AtomicBoolean firstStreamEvent = new AtomicBoolean(true); |
|
193 |
try (EventStream es = EventStream.openFile(RECORDING_FILE)) { |
|
194 |
es.onEvent(e -> { |
|
195 |
// Print one event |
|
196 |
if (firstStreamEvent.get()) { |
|
197 |
try { |
|
198 |
System.out.println(e); |
|
199 |
firstStreamEvent.set(false); |
|
200 |
} catch (Throwable t) { |
|
201 |
t.printStackTrace(); |
|
202 |
} |
|
203 |
} |
|
204 |
}); |
|
205 |
es.start(); |
|
206 |
if (firstStreamEvent.get()) { |
|
207 |
throw new AssertionError("Didn't print streaming event"); |
|
208 |
} |
|
209 |
} |
|
210 |
||
211 |
try (EventStream es = EventStream.openRepository()) { |
|
212 |
es.onEvent(e -> { |
|
213 |
System.out.println(e); |
|
214 |
}); |
|
215 |
es.startAsync(); |
|
216 |
es.awaitTermination(Duration.ofMillis(10)); |
|
50113 | 217 |
} |
218 |
} |
|
219 |
} |
|
220 |
||
221 |
private static void assertNoClassInitFailure(Class<?> clazz) { |
|
222 |
try { |
|
223 |
Class.forName(clazz.getName(), true, clazz.getClassLoader()); |
|
224 |
} catch (ClassNotFoundException e) { |
|
225 |
throw new AssertionError("Could not find public API class on unsupported VM"); |
|
226 |
} |
|
227 |
} |
|
228 |
||
229 |
private static void assertInternalError(Runnable r) { |
|
230 |
try { |
|
231 |
r.run(); |
|
232 |
} catch (InternalError e) { |
|
233 |
// OK, as expected |
|
234 |
return; |
|
235 |
} |
|
236 |
throw new AssertionError("Expected InternalError on an unsupported JVM"); |
|
237 |
} |
|
238 |
||
239 |
private static void assertIOException(Callable<?> c) { |
|
240 |
try { |
|
241 |
c.call(); |
|
242 |
} catch (Exception e) { |
|
243 |
if (e.getClass() == IOException.class) { |
|
244 |
return; |
|
245 |
} |
|
246 |
} |
|
247 |
throw new AssertionError("Expected IOException on an unsupported JVM"); |
|
248 |
} |
|
249 |
||
250 |
private static void assertIllegalStateException(Runnable r) throws Exception { |
|
251 |
try { |
|
252 |
r.run(); |
|
253 |
} catch (IllegalStateException ise) { |
|
254 |
if (!ise.getMessage().equals("Flight Recorder is not supported on this VM")) { |
|
255 |
throw new AssertionError("Expected 'Flight Recorder is not supported on this VM'"); |
|
256 |
} |
|
257 |
} |
|
258 |
} |
|
259 |
||
260 |
private static void assertSwallow(Runnable r) throws Exception { |
|
261 |
try { |
|
262 |
r.run(); |
|
263 |
} catch (Exception e) { |
|
264 |
throw new AssertionError("Unexpected exception '" + e.getMessage() + " on an unspported VM"); |
|
265 |
} |
|
266 |
} |
|
267 |
} |