author | egahlin |
Thu, 01 Aug 2019 16:48:26 +0200 | |
branch | JEP-349-branch |
changeset 57614 | b49f5c13baa7 |
parent 57449 | 099789ceff7d |
child 57690 | 9316d02dd4a5 |
permissions | -rw-r--r-- |
57361 | 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.consumer; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.security.AccessControlContext; |
|
30 |
import java.security.AccessController; |
|
31 |
import java.time.Duration; |
|
57449 | 32 |
import java.time.Instant; |
57386 | 33 |
import java.util.Map; |
57361 | 34 |
import java.util.function.Consumer; |
35 |
||
36 |
import jdk.jfr.Configuration; |
|
37 |
import jdk.jfr.Event; |
|
38 |
import jdk.jfr.EventSettings; |
|
39 |
import jdk.jfr.EventType; |
|
40 |
import jdk.jfr.Recording; |
|
41 |
import jdk.jfr.internal.PlatformRecording; |
|
42 |
import jdk.jfr.internal.PrivateAccess; |
|
43 |
import jdk.jfr.internal.Utils; |
|
44 |
||
45 |
/** |
|
57614 | 46 |
* A recording stream produces events from the current JVM (Java Virtual |
57449 | 47 |
* Machine). |
57614 | 48 |
* <p> |
49 |
* The following example records events using the default configuration and |
|
50 |
* prints the Garbage Collection, CPU Load and JVM Information event. |
|
51 |
* |
|
52 |
* <pre> |
|
53 |
* <code> |
|
54 |
* var c = Configuration.getConfiguration("default"); |
|
55 |
* try (var rs = new RecordingStream(c)) { |
|
56 |
* rs.onEvent("jdk.GarbageCollection", System.out::println); |
|
57 |
* rs.onEvent("jdk.CPULoad", System.out::println); |
|
58 |
* rs.onEvent("jdk.JVMInformation", System.out::println); |
|
59 |
* rs.start(); |
|
60 |
* } |
|
61 |
* } |
|
62 |
* </code> |
|
63 |
* </pre> |
|
64 |
* |
|
57361 | 65 |
*/ |
57614 | 66 |
public final class RecordingStream implements AutoCloseable, EventStream { |
57361 | 67 |
|
68 |
private final Recording recording; |
|
69 |
private final EventDirectoryStream stream; |
|
70 |
||
71 |
/** |
|
57614 | 72 |
* Creates an event stream for the current JVM (Java Virtual Machine). |
57361 | 73 |
* <p> |
74 |
* The following example shows how to create a recording stream that prints |
|
75 |
* CPU usage and information about garbage collections. |
|
76 |
* |
|
77 |
* <pre> |
|
78 |
* <code> |
|
57614 | 79 |
* try (var rs = new RecordingStream()) { |
80 |
* rs.enable("jdk.GarbageCollection"); |
|
81 |
* rs.enable("jdk.CPULoad").withPeriod(Duration.ofSeconds(1)); |
|
82 |
* rs.onEvent(System.out::println); |
|
83 |
* rs.start(); |
|
57361 | 84 |
* } |
85 |
* </code> |
|
86 |
* </pre> |
|
87 |
* |
|
88 |
* @throws IllegalStateException if Flight Recorder can't be created (for |
|
89 |
* example, if the Java Virtual Machine (JVM) lacks Flight Recorder |
|
90 |
* support, or if the file repository can't be created or accessed) |
|
91 |
* |
|
92 |
* @throws SecurityException if a security manager exists and the caller |
|
93 |
* does not have |
|
94 |
* {@code FlightRecorderPermission("accessFlightRecorder")} |
|
95 |
*/ |
|
96 |
public RecordingStream() { |
|
97 |
Utils.checkAccessFlightRecorder(); |
|
98 |
AccessControlContext acc = AccessController.getContext(); |
|
99 |
this.recording = new Recording(); |
|
100 |
this.recording.setFlushInterval(Duration.ofMillis(1000)); |
|
101 |
try { |
|
57449 | 102 |
this.stream = new EventDirectoryStream(acc, null, null); |
57361 | 103 |
} catch (IOException ioe) { |
104 |
throw new IllegalStateException(ioe.getMessage()); |
|
105 |
} |
|
106 |
} |
|
107 |
||
108 |
/** |
|
109 |
* Creates a recording stream using settings from a configuration. |
|
110 |
* <p> |
|
111 |
* The following example shows how to create a recording stream that uses a |
|
112 |
* predefined configuration. |
|
113 |
* |
|
114 |
* <pre> |
|
115 |
* <code> |
|
57614 | 116 |
* var c = Configuration.getConfiguration("default"); |
117 |
* try (RecordingStream rs = new RecordingStream(c)) { |
|
118 |
* rs.onEvent(System.out::println); |
|
119 |
* rs.start(); |
|
57361 | 120 |
* } |
121 |
* </code> |
|
122 |
* </pre> |
|
123 |
* |
|
124 |
* @param configuration configuration that contains the settings to be use, |
|
125 |
* not {@code null} |
|
126 |
* |
|
127 |
* @throws IllegalStateException if Flight Recorder can't be created (for |
|
128 |
* example, if the Java Virtual Machine (JVM) lacks Flight Recorder |
|
129 |
* support, or if the file repository can't be created or accessed) |
|
130 |
* |
|
131 |
* @throws SecurityException if a security manager is used and |
|
132 |
* FlightRecorderPermission "accessFlightRecorder" is not set. |
|
133 |
* |
|
134 |
* @see Configuration |
|
135 |
*/ |
|
136 |
public RecordingStream(Configuration configuration) { |
|
137 |
this(); |
|
138 |
recording.setSettings(configuration.getSettings()); |
|
139 |
} |
|
140 |
||
141 |
/** |
|
142 |
* Enables the event with the specified name. |
|
143 |
* <p> |
|
144 |
* If multiple events have the same name (for example, the same class is |
|
145 |
* loaded in different class loaders), then all events that match the name |
|
146 |
* are enabled. To enable a specific class, use the {@link #enable(Class)} |
|
147 |
* method or a {@code String} representation of the event type ID. |
|
148 |
* |
|
149 |
* @param name the settings for the event, not {@code null} |
|
150 |
* |
|
151 |
* @return an event setting for further configuration, not {@code null} |
|
152 |
* |
|
153 |
* @see EventType |
|
154 |
*/ |
|
155 |
public EventSettings enable(String name) { |
|
156 |
return recording.enable(name); |
|
157 |
} |
|
158 |
||
159 |
/** |
|
57386 | 160 |
* Replaces all settings for this recording stream |
161 |
* <p> |
|
57614 | 162 |
* The following example records 20 second using the "default" configuration |
163 |
* and then changes to settings for the "profile" configuration. |
|
57386 | 164 |
* |
165 |
* <pre> |
|
166 |
* <code> |
|
57614 | 167 |
* var defaultConfiguration = Configuration.getConfiguration("default"); |
168 |
* var profileConfiguration = Configuration.getConfiguration("profile"); |
|
169 |
* try (var rs = new RecordingStream(defaultConfiguration) { |
|
170 |
* rs.startAsync(); |
|
171 |
* Thread.sleep(20_000); |
|
172 |
* rs.setSettings(profileConfiguration.getSettings()); |
|
173 |
* Thread.sleep(20_000); |
|
174 |
* } |
|
57386 | 175 |
* </code> |
176 |
* </pre> |
|
177 |
* |
|
178 |
* @param settings the settings to set, not {@code null} |
|
179 |
*/ |
|
180 |
public void setSettings(Map<String, String> settings) { |
|
181 |
recording.setSettings(settings); |
|
182 |
}; |
|
183 |
||
184 |
/** |
|
57361 | 185 |
* Enables event. |
186 |
* |
|
187 |
* @param eventClass the event to enable, not {@code null} |
|
188 |
* |
|
189 |
* @throws IllegalArgumentException if {@code eventClass} is an abstract |
|
190 |
* class or not a subclass of {@link Event} |
|
191 |
* |
|
192 |
* @return an event setting for further configuration, not {@code null} |
|
193 |
*/ |
|
194 |
public EventSettings enable(Class<? extends Event> eventClass) { |
|
195 |
return recording.enable(eventClass); |
|
196 |
} |
|
197 |
||
198 |
/** |
|
199 |
* Disables event with the specified name. |
|
200 |
* <p> |
|
201 |
* If multiple events with same name (for example, the same class is loaded |
|
202 |
* in different class loaders), then all events that match the name is |
|
203 |
* disabled. To disable a specific class, use the {@link #disable(Class)} |
|
204 |
* method or a {@code String} representation of the event type ID. |
|
205 |
* |
|
206 |
* @param name the settings for the event, not {@code null} |
|
207 |
* |
|
208 |
* @return an event setting for further configuration, not {@code null} |
|
209 |
* |
|
210 |
*/ |
|
211 |
public EventSettings disable(String name) { |
|
212 |
return recording.disable(name); |
|
213 |
} |
|
214 |
||
215 |
/** |
|
216 |
* Disables event. |
|
217 |
* |
|
218 |
* @param eventClass the event to enable, not {@code null} |
|
219 |
* |
|
220 |
* @throws IllegalArgumentException if {@code eventClass} is an abstract |
|
221 |
* class or not a subclass of {@link Event} |
|
222 |
* |
|
223 |
* @return an event setting for further configuration, not {@code null} |
|
224 |
* |
|
225 |
*/ |
|
226 |
public EventSettings disable(Class<? extends Event> eventClass) { |
|
227 |
return recording.disable(eventClass); |
|
228 |
} |
|
57449 | 229 |
|
57361 | 230 |
/** |
57449 | 231 |
* Determines how far back data is kept for the stream, if the stream can't |
57361 | 232 |
* keep up. |
233 |
* <p> |
|
234 |
* To control the amount of recording data stored on disk, the maximum |
|
235 |
* length of time to retain the data can be specified. Data stored on disk |
|
236 |
* that is older than the specified length of time is removed by the Java |
|
237 |
* Virtual Machine (JVM). |
|
238 |
* <p> |
|
239 |
* If neither maximum limit or the maximum age is set, the size of the |
|
240 |
* recording may grow indefinitely if events are on |
|
241 |
* |
|
242 |
* @param maxAge the length of time that data is kept, or {@code null} if |
|
243 |
* infinite |
|
244 |
* |
|
245 |
* @throws IllegalArgumentException if <code>maxAge</code> is negative |
|
246 |
* |
|
247 |
* @throws IllegalStateException if the recording is in the {@code CLOSED} |
|
248 |
* state |
|
249 |
*/ |
|
250 |
public void setMaxAge(Duration maxAge) { |
|
251 |
recording.setMaxAge(maxAge); |
|
252 |
} |
|
253 |
||
254 |
/** |
|
255 |
* Determines how much data is kept in the disk repository if the stream |
|
256 |
* can't keep up. |
|
257 |
* <p> |
|
258 |
* To control the amount of recording data that is stored on disk, the |
|
259 |
* maximum amount of data to retain can be specified. When the maximum limit |
|
260 |
* is exceeded, the Java Virtual Machine (JVM) removes the oldest chunk to |
|
261 |
* make room for a more recent chunk. |
|
262 |
* <p> |
|
263 |
* If neither maximum limit or the maximum age is set, the size of the |
|
264 |
* recording may grow indefinitely. |
|
265 |
* |
|
266 |
* @param maxSize the amount of data to retain, {@code 0} if infinite |
|
267 |
* |
|
268 |
* @throws IllegalArgumentException if <code>maxSize</code> is negative |
|
269 |
* |
|
270 |
* @throws IllegalStateException if the recording is in {@code CLOSED} state |
|
271 |
*/ |
|
272 |
public void setMaxSize(long maxSize) { |
|
273 |
recording.setMaxSize(maxSize); |
|
274 |
} |
|
275 |
||
276 |
@Override |
|
277 |
public void onEvent(String eventName, Consumer<RecordedEvent> action) { |
|
278 |
stream.onEvent(eventName, action); |
|
279 |
} |
|
280 |
||
281 |
@Override |
|
282 |
public void onEvent(Consumer<RecordedEvent> action) { |
|
283 |
stream.onEvent(action); |
|
284 |
} |
|
285 |
||
286 |
@Override |
|
287 |
public void onFlush(Runnable action) { |
|
288 |
stream.onFlush(action); |
|
289 |
} |
|
290 |
||
291 |
@Override |
|
292 |
public void onClose(Runnable action) { |
|
293 |
stream.onClose(action); |
|
294 |
} |
|
295 |
||
296 |
@Override |
|
297 |
public void close() { |
|
298 |
recording.close(); |
|
299 |
stream.close(); |
|
300 |
} |
|
301 |
||
302 |
@Override |
|
303 |
public boolean remove(Object action) { |
|
304 |
return stream.remove(action); |
|
305 |
} |
|
306 |
||
307 |
@Override |
|
308 |
public void start() { |
|
309 |
PlatformRecording pr = PrivateAccess.getInstance().getPlatformRecording(recording); |
|
310 |
long startNanos = pr.start(); |
|
311 |
stream.start(startNanos); |
|
312 |
} |
|
313 |
||
314 |
@Override |
|
315 |
public void startAsync() { |
|
316 |
PlatformRecording pr = PrivateAccess.getInstance().getPlatformRecording(recording); |
|
317 |
long startNanos = pr.start(); |
|
318 |
stream.startAsync(startNanos); |
|
319 |
} |
|
320 |
||
321 |
@Override |
|
322 |
public void awaitTermination(Duration timeout) { |
|
323 |
stream.awaitTermination(timeout); |
|
324 |
} |
|
325 |
||
326 |
/** |
|
327 |
* Determines how often events are made available for streaming. |
|
328 |
* |
|
329 |
* @param interval the interval at which events are made available to the |
|
57449 | 330 |
* stream, no {@code null} |
57361 | 331 |
* |
332 |
* @throws IllegalArgumentException if <code>interval</code> is negative |
|
333 |
* |
|
334 |
* @throws IllegalStateException if the stream is closed |
|
335 |
*/ |
|
336 |
public void setInterval(Duration duration) { |
|
337 |
recording.setFlushInterval(duration); |
|
338 |
} |
|
339 |
||
340 |
@Override |
|
341 |
public void awaitTermination() { |
|
342 |
stream.awaitTermination(); |
|
343 |
} |
|
57376 | 344 |
|
345 |
@Override |
|
346 |
public void setReuse(boolean reuse) { |
|
57432
ba454a26d2c1
SetOrdered and setReuse now work on a directory stream
egahlin
parents:
57425
diff
changeset
|
347 |
stream.setReuse(reuse); |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
348 |
} |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
349 |
|
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
350 |
@Override |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
351 |
public void setOrdered(boolean ordered) { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
352 |
stream.setOrdered(ordered); |
57376 | 353 |
} |
57449 | 354 |
|
355 |
@Override |
|
356 |
public void setStartTime(Instant startTime) { |
|
357 |
stream.setStartTime(startTime); |
|
358 |
} |
|
57361 | 359 |
} |