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