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