author | egahlin |
Mon, 16 Sep 2019 09:45:22 +0200 | |
branch | JEP-349-branch |
changeset 58145 | bc54ed8d908a |
parent 58129 | 7b751fe181a5 |
child 58152 | ed7e71dd188b |
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.nio.file.Path; |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
30 |
import java.security.AccessControlContext; |
57434 | 31 |
import java.security.AccessController; |
57361 | 32 |
import java.time.Duration; |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
33 |
import java.time.Instant; |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
34 |
import java.util.Objects; |
57361 | 35 |
import java.util.function.Consumer; |
36 |
||
57717
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
37 |
import jdk.jfr.internal.SecuritySupport; |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
38 |
import jdk.jfr.internal.Utils; |
58145
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58129
diff
changeset
|
39 |
import jdk.jfr.internal.consumer.EventDirectoryStream; |
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58129
diff
changeset
|
40 |
import jdk.jfr.internal.consumer.EventFileStream; |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
41 |
import jdk.jfr.internal.consumer.FileAccess; |
57627 | 42 |
|
57361 | 43 |
/** |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
44 |
* Represents a stream of events. |
58129 | 45 |
* <p> |
46 |
* A stream is a sequence of events and the way to interact with a stream is to |
|
47 |
* register actions. |
|
48 |
* <p> |
|
49 |
* To receive a notification when an event arrives, register an action using the |
|
50 |
* {@link #onEvent(Consumer)} method. To filter the stream for an event with a |
|
51 |
* specific name, use {@link #onEvent(String, Consumer)} method. |
|
52 |
* |
|
53 |
* By default, the same {@code RecordedEvent} object can be used for |
|
54 |
* representing two or more distinct events. The object can be delivered |
|
55 |
* multiple times to the same action as well as to other actions. If the life |
|
56 |
* cycle of the event object is needed outside the scope of an action, the |
|
57 |
* {@link #setReuse(boolean)} method should be set to {@code false} so that a |
|
58 |
* new object is allocated for each event. |
|
59 |
* |
|
60 |
* <p> |
|
61 |
* Events are delivered in batches. To receive a notification when a batch is |
|
62 |
* complete, register an action using the {@link #onFlush(Runnable)} method. |
|
63 |
* This is an opportunity to aggregate or push data to external systems while |
|
64 |
* the Java Virtual Machine (JVM) is preparing the next batch. |
|
65 |
* <p> |
|
66 |
* Events within a batch are sorted chronologically by their end time. If |
|
67 |
* ordering is not a concern, sorting can be disabled using the |
|
68 |
* {@link #setOrdered(boolean)} method. |
|
69 |
* <p> |
|
70 |
* To dispatch events to registered actions, the stream must be started. To |
|
71 |
* start processing in the current thread, invoke the {@link #start()} method. |
|
72 |
* To process actions asynchronously in a separate thread, invoke the |
|
73 |
* {@link #startAsync()} method. To await completion of the stream, use the |
|
74 |
* awaitTermination {@link #awaitTermination()} or the {link |
|
75 |
* {@link #awaitTermination(Duration)} method. |
|
76 |
* <p> |
|
77 |
* When a stream ends it is automatically closed. To manually stop processing of |
|
78 |
* events, close the stream with the {@link #close()} method. A stream can also |
|
79 |
* be automatically closed in exceptional circumstances, for instance if the JVM |
|
80 |
* exits. To receive a notification in any of these occasions, use the |
|
81 |
* {@link #onClose(Runnable)} method to register an action. |
|
82 |
* <p> |
|
83 |
* If an unexpected exception occurs in an action, it is possible to catch the |
|
84 |
* exception in an error handler. An error handler can be registered using the |
|
85 |
* {@link #onError(Runnable)} method. If no error handler is registered, the |
|
86 |
* default behavior is to print the exception and its backtrace to the standard |
|
87 |
* error stream. |
|
88 |
* <p> |
|
89 |
* The following example demonstrates how an {@code EventStream} can be used to |
|
90 |
* listen to garbage collection and CPU Load events |
|
91 |
* <p> |
|
92 |
* |
|
57361 | 93 |
*/ |
94 |
public interface EventStream extends AutoCloseable { |
|
95 |
||
96 |
/** |
|
58129 | 97 |
* Creates a stream from the repository of the current Java Virtual Machine |
98 |
* (JVM). |
|
57627 | 99 |
* <p> |
100 |
* By default, the stream starts with the next event flushed by Flight |
|
101 |
* Recorder. |
|
102 |
* |
|
103 |
* @return an event stream, not {@code null} |
|
104 |
* |
|
105 |
* @throws IOException if a stream can't be opened, or an I/O error occurs |
|
106 |
* when trying to access the repository |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
107 |
* |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
108 |
* @throws SecurityException if a security manager exists and the caller |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
109 |
* does not have |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
110 |
* {@code FlightRecorderPermission("accessFlightRecorder")} |
57627 | 111 |
*/ |
112 |
public static EventStream openRepository() throws IOException { |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
113 |
Utils.checkAccessFlightRecorder(); |
57717
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
114 |
return new EventDirectoryStream(AccessController.getContext(), null, SecuritySupport.PRIVILIGED, false); |
57627 | 115 |
} |
116 |
||
117 |
/** |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
118 |
* Creates an event stream from a disk repository. |
57449 | 119 |
* <p> |
57627 | 120 |
* By default, the stream starts with the next event flushed by Flight |
121 |
* Recorder. |
|
57361 | 122 |
* |
123 |
* @param directory location of the disk repository, not {@code null} |
|
57449 | 124 |
* |
57361 | 125 |
* @return an event stream, not {@code null} |
57449 | 126 |
* |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
127 |
* @throws IOException if a stream can't be opened, or an I/O error occurs |
57627 | 128 |
* when trying to access the repository |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
129 |
* |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
130 |
* @throws SecurityException if a security manager exists and its |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
131 |
* {@code checkRead} method denies read access to the directory, or |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
132 |
* files in the directory. |
57361 | 133 |
*/ |
134 |
public static EventStream openRepository(Path directory) throws IOException { |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
135 |
Objects.nonNull(directory); |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
136 |
AccessControlContext acc = AccessController.getContext(); |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
137 |
return new EventDirectoryStream(acc, directory, FileAccess.UNPRIVILIGED, false); |
57361 | 138 |
} |
139 |
||
140 |
/** |
|
57449 | 141 |
* Creates an event stream from a file. |
142 |
* <p> |
|
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57449
diff
changeset
|
143 |
* By default, the stream starts with the first event in the file. |
57361 | 144 |
* |
145 |
* @param file location of the file, not {@code null} |
|
57449 | 146 |
* |
57361 | 147 |
* @return an event stream, not {@code null} |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
148 |
* |
58129 | 149 |
* @throws IOException if the file can't be opened, or an I/O error occurs |
57376 | 150 |
* during reading |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
151 |
* |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
152 |
* @throws SecurityException if a security manager exists and its |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
153 |
* {@code checkRead} method denies read access to the file |
57361 | 154 |
*/ |
155 |
public static EventStream openFile(Path file) throws IOException { |
|
57985 | 156 |
return new EventFileStream(AccessController.getContext(), file); |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
157 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
158 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
159 |
/** |
58129 | 160 |
* Registers an action to perform on all events in the stream. |
57361 | 161 |
* |
58129 | 162 |
* @param action an action to perform on each {@code RecordedEvent}, not |
163 |
* {@code null} |
|
57361 | 164 |
*/ |
165 |
void onEvent(Consumer<RecordedEvent> action); |
|
166 |
||
167 |
/** |
|
58129 | 168 |
* Registers an action to perform on all events matching a name. |
57361 | 169 |
* |
170 |
* @param eventName the name of the event, not {@code null} |
|
57449 | 171 |
* |
58129 | 172 |
* @param action an action to perform on each {@code RecordedEvent} matching |
173 |
* the event name, not {@code null} |
|
57361 | 174 |
*/ |
175 |
void onEvent(String eventName, Consumer<RecordedEvent> action); |
|
176 |
||
177 |
/** |
|
58129 | 178 |
* Registers an action to perform after the stream has been flushed. |
57361 | 179 |
* |
58129 | 180 |
* @param action an action to perform after the stream has been |
181 |
* flushed, not {@code null} |
|
57361 | 182 |
*/ |
183 |
void onFlush(Runnable action); |
|
184 |
||
185 |
/** |
|
58129 | 186 |
* Registers an action to perform if an exception occurs. |
57971 | 187 |
* <p> |
58129 | 188 |
* if an action is not registered, an exception stack trace is printed to |
189 |
* standard error. |
|
57971 | 190 |
* <p> |
58129 | 191 |
* Registering an action overrides the default behavior. If multiple actions |
192 |
* have been registered, they are performed in the order of registration. |
|
193 |
* <p> |
|
194 |
* If this method itself throws an exception, resulting behavior is |
|
195 |
* undefined. |
|
57971 | 196 |
* |
58129 | 197 |
* @param action an action to perform if an exception occurs, not |
57971 | 198 |
* {@code null} |
199 |
*/ |
|
200 |
void onError(Consumer<Throwable> action); |
|
201 |
||
202 |
/** |
|
58129 | 203 |
* Registers an action to perform when the stream is closed. |
57449 | 204 |
* <p> |
58129 | 205 |
* If the stream is already closed, the action will be performed immediately |
57449 | 206 |
* in the current thread. |
57361 | 207 |
* |
58129 | 208 |
* @param action an action to perform after the stream is closed, not |
209 |
* {@code null} |
|
210 |
* @see #close() |
|
57361 | 211 |
*/ |
212 |
void onClose(Runnable action); |
|
213 |
||
214 |
/** |
|
58129 | 215 |
* Releases all resources associated with this stream. |
57361 | 216 |
*/ |
217 |
void close(); |
|
218 |
||
219 |
/** |
|
58129 | 220 |
* Unregisters an action. |
57361 | 221 |
* <p> |
58129 | 222 |
* If the action has been registered multiple times, all instances are |
223 |
* unregistered. |
|
57361 | 224 |
* |
58129 | 225 |
* @param action the action to unregister, not {@code null} |
57449 | 226 |
* |
58129 | 227 |
* @return {@code true} if the action was unregistered, {@code false} |
228 |
* otherwise |
|
57361 | 229 |
* |
230 |
* @see #onEvent(Consumer) |
|
231 |
* @see #onEvent(String, Consumer) |
|
58020
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
232 |
* @see #onFlush(Runnable) |
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
233 |
* @see #onClose(Runnable) |
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
234 |
* @see #onError(Consumer) |
57361 | 235 |
*/ |
236 |
boolean remove(Object action); |
|
237 |
||
238 |
/** |
|
57449 | 239 |
* Specifies that the event object in an {@link #onEvent(Consumer)} action |
58129 | 240 |
* can be reused. |
57376 | 241 |
* <p> |
58129 | 242 |
* If reuse is set to {@code true), an action should not keep a reference |
243 |
* to the event object after the action has completed. |
|
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
244 |
* |
58129 | 245 |
* @param reuse {@code true} if an event object can be reused, {@code false} |
246 |
* otherwise |
|
57376 | 247 |
*/ |
58129 | 248 |
void setReuse(boolean reuse); |
57376 | 249 |
|
250 |
/** |
|
57449 | 251 |
* Specifies that events arrives in chronological order, sorted by the time |
58129 | 252 |
* they were committed to the stream. |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
253 |
* |
57449 | 254 |
* @param ordered if event objects arrive in chronological order to |
255 |
* {@code #onEvent(Consumer)} |
|
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
256 |
*/ |
58129 | 257 |
void setOrdered(boolean ordered); |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
258 |
|
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
259 |
/** |
58129 | 260 |
* Specifies the start time of the stream. |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
261 |
* <p> |
58129 | 262 |
* The start time must be set before starting the stream |
57449 | 263 |
* |
264 |
* @param startTime the start time, not {@code null} |
|
265 |
* |
|
58129 | 266 |
* @throws IllegalStateException if the stream is already started |
267 |
* |
|
268 |
* @see #start() |
|
269 |
* @see #startAsync() |
|
57449 | 270 |
*/ |
58129 | 271 |
void setStartTime(Instant startTime); |
57449 | 272 |
|
273 |
/** |
|
58129 | 274 |
* Specifies the end time of the stream. |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
275 |
* <p> |
58129 | 276 |
* The end time must be set before starting the stream. |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
277 |
* <p> |
58129 | 278 |
* At end time, the stream is closed. |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
279 |
* |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
280 |
* @param endTime the end time, not {@code null} |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
281 |
* |
58129 | 282 |
* @throws IllegalStateException if the stream is already started |
283 |
* |
|
284 |
* @see #start() |
|
285 |
* @see #startAsync() |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
286 |
*/ |
58129 | 287 |
void setEndTime(Instant endTime); |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
288 |
|
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
289 |
/** |
58129 | 290 |
* Start processing of actions. |
57361 | 291 |
* <p> |
58129 | 292 |
* Actions are performed in the current thread. |
57361 | 293 |
* |
58129 | 294 |
* @throws IllegalStateException if the stream is already started or closed |
57361 | 295 |
*/ |
296 |
void start(); |
|
297 |
||
298 |
/** |
|
58129 | 299 |
* Start asynchronous processing of actions. |
57361 | 300 |
* <p> |
58129 | 301 |
* Actions are performed in a single separate thread. |
57361 | 302 |
* |
58129 | 303 |
* @throws IllegalStateException if the stream is already started or closed |
57361 | 304 |
*/ |
305 |
void startAsync(); |
|
306 |
||
307 |
/** |
|
58129 | 308 |
* Blocks until all actions are completed, or the stream is closed, or the |
309 |
* timeout occurs, or the current thread is interrupted, whichever happens |
|
310 |
* first. |
|
57361 | 311 |
* |
312 |
* @param timeout the maximum time to wait, not {@code null} |
|
313 |
* |
|
58020
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
314 |
* @throws IllegalArgumentException if timeout is negative |
58129 | 315 |
* @throws InterruptedException if interrupted while waiting |
58020
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
316 |
* |
57361 | 317 |
* @see #start() |
318 |
* @see #startAsync() |
|
58129 | 319 |
* @see Thread#interrupt() |
57361 | 320 |
*/ |
58020
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
321 |
void awaitTermination(Duration timeout) throws InterruptedException; |
57361 | 322 |
|
323 |
/** |
|
58129 | 324 |
* Blocks until all actions are completed, or the stream is closed, or the |
325 |
* current thread is interrupted, whichever happens first. |
|
57361 | 326 |
* |
58129 | 327 |
* @throws InterruptedException if interrupted while waiting |
58020
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
328 |
* |
57361 | 329 |
* @see #start() |
330 |
* @see #startAsync() |
|
58129 | 331 |
* @see Thread#interrupt() |
57361 | 332 |
*/ |
58020
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
333 |
void awaitTermination() throws InterruptedException; |
57361 | 334 |
} |