author | egahlin |
Fri, 16 Aug 2019 20:00:10 +0200 | |
branch | JEP-349-branch |
changeset 57780 | d8a436dfa041 |
parent 57717 | 4ce66d271065 |
child 57971 | aa7b1ea52413 |
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; |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
39 |
import jdk.jfr.internal.consumer.FileAccess; |
57627 | 40 |
|
57361 | 41 |
/** |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
42 |
* Represents a stream of events. |
57361 | 43 |
*/ |
44 |
public interface EventStream extends AutoCloseable { |
|
45 |
||
46 |
/** |
|
57627 | 47 |
* Creates a stream from the disk repository of the current Java Virtual |
48 |
* Machine (JVM). |
|
49 |
* <p> |
|
50 |
* By default, the stream starts with the next event flushed by Flight |
|
51 |
* Recorder. |
|
52 |
* |
|
53 |
* @return an event stream, not {@code null} |
|
54 |
* |
|
55 |
* @throws IOException if a stream can't be opened, or an I/O error occurs |
|
56 |
* 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
|
57 |
* |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
58 |
* @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
|
59 |
* does not have |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
60 |
* {@code FlightRecorderPermission("accessFlightRecorder")} |
57627 | 61 |
*/ |
62 |
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
|
63 |
Utils.checkAccessFlightRecorder(); |
57717
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
64 |
return new EventDirectoryStream(AccessController.getContext(), null, SecuritySupport.PRIVILIGED, false); |
57627 | 65 |
} |
66 |
||
67 |
/** |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
68 |
* Creates an event stream from a disk repository. |
57449 | 69 |
* <p> |
57627 | 70 |
* By default, the stream starts with the next event flushed by Flight |
71 |
* Recorder. |
|
57361 | 72 |
* |
73 |
* @param directory location of the disk repository, not {@code null} |
|
57449 | 74 |
* |
57361 | 75 |
* @return an event stream, not {@code null} |
57449 | 76 |
* |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
77 |
* @throws IOException if a stream can't be opened, or an I/O error occurs |
57627 | 78 |
* 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
|
79 |
* |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
80 |
* @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
|
81 |
* {@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
|
82 |
* files in the directory. |
57361 | 83 |
*/ |
84 |
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
|
85 |
Objects.nonNull(directory); |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
86 |
AccessControlContext acc = AccessController.getContext(); |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
87 |
return new EventDirectoryStream(acc, directory, FileAccess.UNPRIVILIGED, false); |
57361 | 88 |
} |
89 |
||
90 |
/** |
|
57449 | 91 |
* Creates an event stream from a file. |
92 |
* <p> |
|
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57449
diff
changeset
|
93 |
* By default, the stream starts with the first event in the file. |
57361 | 94 |
* |
95 |
* @param file location of the file, not {@code null} |
|
57449 | 96 |
* |
57361 | 97 |
* @return an event stream, not {@code null} |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
98 |
* |
57449 | 99 |
* @throws IOException if a stream can't be opened, or an I/O error occurs |
57376 | 100 |
* during reading |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
101 |
* |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
102 |
* @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
|
103 |
* {@code checkRead} method denies read access to the file |
57361 | 104 |
*/ |
105 |
public static EventStream openFile(Path file) throws IOException { |
|
57449 | 106 |
return new EventFileStream(file, null, null); |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
107 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
108 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
109 |
/** |
57361 | 110 |
* Performs an action on all events in the stream. |
111 |
* |
|
112 |
* @param action an action to be performed on each {@code RecordedEvent}, |
|
113 |
* not {@code null} |
|
114 |
*/ |
|
115 |
void onEvent(Consumer<RecordedEvent> action); |
|
116 |
||
117 |
/** |
|
118 |
* Performs an action on all events in the stream with a specified name. |
|
119 |
* |
|
120 |
* @param eventName the name of the event, not {@code null} |
|
57449 | 121 |
* |
57361 | 122 |
* @param action an action to be performed on each {@code RecordedEvent} |
123 |
* that matches the event name, not {@code null} |
|
124 |
*/ |
|
125 |
void onEvent(String eventName, Consumer<RecordedEvent> action); |
|
126 |
||
127 |
/** |
|
128 |
* Performs an action when the event stream has been flushed. |
|
129 |
* |
|
130 |
* @param action an action to be performed after stream has been flushed, |
|
131 |
* not {@code null} |
|
132 |
*/ |
|
133 |
void onFlush(Runnable action); |
|
134 |
||
135 |
/** |
|
136 |
* Performs an action when the event stream is closed. |
|
57449 | 137 |
* <p> |
138 |
* If the stream is already closed, the action will be executed immediately |
|
139 |
* in the current thread. |
|
57361 | 140 |
* |
141 |
* @param action an action to be performed after the stream has been closed, |
|
142 |
* not {@code null} |
|
143 |
*/ |
|
144 |
void onClose(Runnable action); |
|
145 |
||
146 |
/** |
|
147 |
* Releases all resources associated with this event stream. |
|
148 |
*/ |
|
149 |
void close(); |
|
150 |
||
151 |
/** |
|
152 |
* Removes an action from the stream. |
|
153 |
* <p> |
|
154 |
* If the action has been added multiple times, all instance of it will be |
|
155 |
* removed. |
|
156 |
* |
|
157 |
* @param action the action to remove, not {@code null} |
|
57449 | 158 |
* |
57361 | 159 |
* @return {@code true} if the action was removed, {@code false} otherwise |
160 |
* |
|
161 |
* @see #onClose(Runnable) |
|
162 |
* @see #onFlush(Runnable) |
|
163 |
* @see #onEvent(Consumer) |
|
164 |
* @see #onEvent(String, Consumer) |
|
165 |
*/ |
|
166 |
boolean remove(Object action); |
|
167 |
||
168 |
/** |
|
57449 | 169 |
* Specifies that the event object in an {@link #onEvent(Consumer)} action |
170 |
* is to be reused. |
|
57376 | 171 |
* <p> |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
172 |
* If reuse is set to {@code true), a callback should not keep a reference |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
173 |
* to the event object after the callback from {@code onEvent} has returned. |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
174 |
* |
57449 | 175 |
* @param resuse if event objects can be reused between calls to |
176 |
* {@code #onEvent(Consumer)} |
|
57376 | 177 |
* |
178 |
*/ |
|
179 |
public void setReuse(boolean reuse); |
|
180 |
||
181 |
/** |
|
57449 | 182 |
* Specifies that events arrives in chronological order, sorted by the time |
183 |
* they were committed to the event stream. |
|
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
184 |
* |
57449 | 185 |
* @param ordered if event objects arrive in chronological order to |
186 |
* {@code #onEvent(Consumer)} |
|
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
187 |
*/ |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
188 |
public void setOrdered(boolean ordered); |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
189 |
|
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
190 |
/** |
57449 | 191 |
* Specifies start time of the event stream. |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
192 |
* <p> |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
193 |
* The start time must be set before the stream is started. |
57449 | 194 |
* |
195 |
* @param startTime the start time, not {@code null} |
|
196 |
* |
|
197 |
* @throws IllegalStateException if the stream has already been started |
|
198 |
*/ |
|
199 |
public void setStartTime(Instant startTime); |
|
200 |
||
201 |
/** |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
202 |
* Specifies end time of the event stream. |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
203 |
* <p> |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
204 |
* The end time must be set before the stream is started. |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
205 |
* <p> |
57780 | 206 |
* When the end time is reached the stream is closed. |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
207 |
* |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
208 |
* @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
|
209 |
* |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
210 |
* @throws IllegalStateException if the stream has already been started |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
211 |
*/ |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
212 |
public void setEndTime(Instant endTime); |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
213 |
|
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57627
diff
changeset
|
214 |
/** |
57449 | 215 |
* Start processing events in the stream. |
57361 | 216 |
* <p> |
57449 | 217 |
* All actions performed on this stream will happen in the current thread. |
57361 | 218 |
* |
219 |
* @throws IllegalStateException if the stream is already started or if it |
|
220 |
* has been closed |
|
221 |
*/ |
|
222 |
void start(); |
|
223 |
||
224 |
/** |
|
57449 | 225 |
* Start processing events in the stream asynchronously. |
57361 | 226 |
* <p> |
227 |
* All actions on this stream will be performed in a separate thread. |
|
228 |
* |
|
57449 | 229 |
* @throws IllegalStateException if the stream is already started, or if it |
57361 | 230 |
* has been closed |
231 |
*/ |
|
232 |
void startAsync(); |
|
233 |
||
234 |
/** |
|
235 |
* Blocks the current thread until the stream is finished, closed, or it |
|
236 |
* times out. |
|
237 |
* |
|
238 |
* @param timeout the maximum time to wait, not {@code null} |
|
239 |
* |
|
240 |
* @see #start() |
|
241 |
* @see #startAsync() |
|
242 |
*/ |
|
243 |
void awaitTermination(Duration timeout); |
|
244 |
||
245 |
/** |
|
246 |
* Blocks the current thread until the stream is finished or closed. |
|
247 |
* |
|
248 |
* @see #start() |
|
249 |
* @see #startAsync() |
|
250 |
*/ |
|
251 |
void awaitTermination(); |
|
252 |
} |