author | egahlin |
Fri, 02 Aug 2019 20:04:02 +0200 | |
branch | JEP-349-branch |
changeset 57627 | b38c7a822244 |
parent 57604 | 838f9a7635b6 |
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.nio.file.Path; |
|
57434 | 30 |
import java.security.AccessController; |
57361 | 31 |
import java.time.Duration; |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
32 |
import java.time.Instant; |
57361 | 33 |
import java.util.function.Consumer; |
34 |
||
57627 | 35 |
import jdk.jfr.internal.Repository; |
36 |
||
57361 | 37 |
/** |
38 |
* Represents a stream of event that actions can be performed up on. |
|
39 |
*/ |
|
40 |
public interface EventStream extends AutoCloseable { |
|
41 |
||
42 |
/** |
|
57627 | 43 |
* Creates a stream from the disk repository of the current Java Virtual |
44 |
* Machine (JVM). |
|
45 |
* <p> |
|
46 |
* By default, the stream starts with the next event flushed by Flight |
|
47 |
* Recorder. |
|
48 |
* |
|
49 |
* @return an event stream, not {@code null} |
|
50 |
* |
|
51 |
* @throws IOException if a stream can't be opened, or an I/O error occurs |
|
52 |
* when trying to access the repository |
|
53 |
*/ |
|
54 |
public static EventStream openRepository() throws IOException { |
|
55 |
Repository r = Repository.getRepository(); |
|
56 |
r.ensureRepository(); |
|
57 |
Path path = r.getRepositoryPath().toPath(); |
|
58 |
return new EventDirectoryStream(AccessController.getContext(), path, AbstractEventStream.NEXT_EVENT); |
|
59 |
} |
|
60 |
||
61 |
/** |
|
57449 | 62 |
* Creates a stream from a disk repository. |
63 |
* <p> |
|
57627 | 64 |
* By default, the stream starts with the next event flushed by Flight |
65 |
* Recorder. |
|
57361 | 66 |
* |
67 |
* @param directory location of the disk repository, not {@code null} |
|
57449 | 68 |
* |
57361 | 69 |
* @return an event stream, not {@code null} |
57449 | 70 |
* |
57627 | 71 |
* @throws IOException if a stream can't be opened, or an I/O error occurs |
72 |
* when trying to access the repository |
|
57361 | 73 |
*/ |
74 |
public static EventStream openRepository(Path directory) throws IOException { |
|
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
75 |
return new EventDirectoryStream(AccessController.getContext(), directory, AbstractEventStream.NEXT_EVENT); |
57361 | 76 |
} |
77 |
||
78 |
/** |
|
57449 | 79 |
* Creates an event stream from a file. |
80 |
* <p> |
|
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57449
diff
changeset
|
81 |
* By default, the stream starts with the first event in the file. |
57361 | 82 |
* |
83 |
* @param file location of the file, not {@code null} |
|
57449 | 84 |
* |
57361 | 85 |
* @return an event stream, not {@code null} |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
86 |
* |
57449 | 87 |
* @throws IOException if a stream can't be opened, or an I/O error occurs |
57376 | 88 |
* during reading |
57361 | 89 |
*/ |
90 |
public static EventStream openFile(Path file) throws IOException { |
|
57449 | 91 |
return new EventFileStream(file, null, null); |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
92 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
93 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
94 |
/** |
57361 | 95 |
* Performs an action on all events in the stream. |
96 |
* |
|
97 |
* @param action an action to be performed on each {@code RecordedEvent}, |
|
98 |
* not {@code null} |
|
99 |
*/ |
|
100 |
void onEvent(Consumer<RecordedEvent> action); |
|
101 |
||
102 |
/** |
|
103 |
* Performs an action on all events in the stream with a specified name. |
|
104 |
* |
|
105 |
* @param eventName the name of the event, not {@code null} |
|
57449 | 106 |
* |
57361 | 107 |
* @param action an action to be performed on each {@code RecordedEvent} |
108 |
* that matches the event name, not {@code null} |
|
109 |
*/ |
|
110 |
void onEvent(String eventName, Consumer<RecordedEvent> action); |
|
111 |
||
112 |
/** |
|
113 |
* Performs an action when the event stream has been flushed. |
|
114 |
* |
|
115 |
* @param action an action to be performed after stream has been flushed, |
|
116 |
* not {@code null} |
|
117 |
*/ |
|
118 |
void onFlush(Runnable action); |
|
119 |
||
120 |
/** |
|
121 |
* Performs an action when the event stream is closed. |
|
57449 | 122 |
* <p> |
123 |
* If the stream is already closed, the action will be executed immediately |
|
124 |
* in the current thread. |
|
57361 | 125 |
* |
126 |
* @param action an action to be performed after the stream has been closed, |
|
127 |
* not {@code null} |
|
128 |
*/ |
|
129 |
void onClose(Runnable action); |
|
130 |
||
131 |
/** |
|
132 |
* Releases all resources associated with this event stream. |
|
133 |
*/ |
|
134 |
void close(); |
|
135 |
||
136 |
/** |
|
137 |
* Removes an action from the stream. |
|
138 |
* <p> |
|
139 |
* If the action has been added multiple times, all instance of it will be |
|
140 |
* removed. |
|
141 |
* |
|
142 |
* @param action the action to remove, not {@code null} |
|
57449 | 143 |
* |
57361 | 144 |
* @return {@code true} if the action was removed, {@code false} otherwise |
145 |
* |
|
146 |
* @see #onClose(Runnable) |
|
147 |
* @see #onFlush(Runnable) |
|
148 |
* @see #onEvent(Consumer) |
|
149 |
* @see #onEvent(String, Consumer) |
|
150 |
*/ |
|
151 |
boolean remove(Object action); |
|
152 |
||
153 |
/** |
|
57449 | 154 |
* Specifies that the event object in an {@link #onEvent(Consumer)} action |
155 |
* is to be reused. |
|
57376 | 156 |
* <p> |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
157 |
* 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
|
158 |
* 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
|
159 |
* |
57449 | 160 |
* @param resuse if event objects can be reused between calls to |
161 |
* {@code #onEvent(Consumer)} |
|
57376 | 162 |
* |
163 |
*/ |
|
164 |
public void setReuse(boolean reuse); |
|
165 |
||
166 |
/** |
|
57449 | 167 |
* Specifies that events arrives in chronological order, sorted by the time |
168 |
* they were committed to the event stream. |
|
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
169 |
* |
57449 | 170 |
* @param ordered if event objects arrive in chronological order to |
171 |
* {@code #onEvent(Consumer)} |
|
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
172 |
*/ |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
173 |
public void setOrdered(boolean ordered); |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
174 |
|
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57377
diff
changeset
|
175 |
/** |
57449 | 176 |
* Specifies start time of the event stream. |
177 |
* |
|
178 |
* @param startTime the start time, not {@code null} |
|
179 |
* |
|
180 |
* @throws IllegalStateException if the stream has already been started |
|
181 |
*/ |
|
182 |
public void setStartTime(Instant startTime); |
|
183 |
||
184 |
/** |
|
185 |
* Start processing events in the stream. |
|
57361 | 186 |
* <p> |
57449 | 187 |
* All actions performed on this stream will happen in the current thread. |
57361 | 188 |
* |
189 |
* @throws IllegalStateException if the stream is already started or if it |
|
190 |
* has been closed |
|
191 |
*/ |
|
192 |
void start(); |
|
193 |
||
194 |
/** |
|
57449 | 195 |
* Start processing events in the stream asynchronously. |
57361 | 196 |
* <p> |
197 |
* All actions on this stream will be performed in a separate thread. |
|
198 |
* |
|
57449 | 199 |
* @throws IllegalStateException if the stream is already started, or if it |
57361 | 200 |
* has been closed |
201 |
*/ |
|
202 |
void startAsync(); |
|
203 |
||
204 |
/** |
|
205 |
* Blocks the current thread until the stream is finished, closed, or it |
|
206 |
* times out. |
|
207 |
* |
|
208 |
* @param timeout the maximum time to wait, not {@code null} |
|
209 |
* |
|
210 |
* @see #start() |
|
211 |
* @see #startAsync() |
|
212 |
*/ |
|
213 |
void awaitTermination(Duration timeout); |
|
214 |
||
215 |
/** |
|
216 |
* Blocks the current thread until the stream is finished or closed. |
|
217 |
* |
|
218 |
* @see #start() |
|
219 |
* @see #startAsync() |
|
220 |
*/ |
|
221 |
void awaitTermination(); |
|
222 |
} |