author | egahlin |
Fri, 29 Nov 2019 17:31:01 +0100 | |
changeset 59327 | 2c3578aa0bdf |
parent 59310 | 72f3dd43dd28 |
permissions | -rw-r--r-- |
58863 | 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.internal.consumer; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.security.AccessControlContext; |
|
30 |
import java.security.AccessController; |
|
31 |
import java.security.PrivilegedAction; |
|
32 |
import java.time.Duration; |
|
33 |
import java.time.Instant; |
|
34 |
import java.util.Objects; |
|
35 |
import java.util.concurrent.atomic.AtomicLong; |
|
36 |
import java.util.function.Consumer; |
|
37 |
||
38 |
import jdk.jfr.consumer.EventStream; |
|
39 |
import jdk.jfr.consumer.RecordedEvent; |
|
40 |
import jdk.jfr.internal.LogLevel; |
|
41 |
import jdk.jfr.internal.LogTag; |
|
42 |
import jdk.jfr.internal.Logger; |
|
59226 | 43 |
import jdk.jfr.internal.PlatformRecording; |
58863 | 44 |
import jdk.jfr.internal.SecuritySupport; |
45 |
||
46 |
/* |
|
47 |
* Purpose of this class is to simplify the implementation of |
|
48 |
* an event stream. |
|
49 |
*/ |
|
50 |
abstract class AbstractEventStream implements EventStream { |
|
59310
72f3dd43dd28
8234888: EventStream::close doesn't abort streaming thread
egahlin
parents:
59226
diff
changeset
|
51 |
private final static AtomicLong counter = new AtomicLong(0); |
58863 | 52 |
|
53 |
private final Object terminated = new Object(); |
|
54 |
private final Runnable flushOperation = () -> dispatcher().runFlushActions(); |
|
55 |
private final AccessControlContext accessControllerContext; |
|
56 |
private final StreamConfiguration configuration = new StreamConfiguration(); |
|
59226 | 57 |
private final PlatformRecording recording; |
58863 | 58 |
|
59 |
private volatile Thread thread; |
|
60 |
private Dispatcher dispatcher; |
|
61 |
||
62 |
private volatile boolean closed; |
|
63 |
||
59226 | 64 |
AbstractEventStream(AccessControlContext acc, PlatformRecording recording) throws IOException { |
58863 | 65 |
this.accessControllerContext = Objects.requireNonNull(acc); |
59226 | 66 |
this.recording = recording; |
58863 | 67 |
} |
68 |
||
69 |
@Override |
|
70 |
abstract public void start(); |
|
71 |
||
72 |
@Override |
|
73 |
abstract public void startAsync(); |
|
74 |
||
75 |
@Override |
|
76 |
abstract public void close(); |
|
77 |
||
78 |
protected final Dispatcher dispatcher() { |
|
59327
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
79 |
if (configuration.hasChanged()) { // quick check |
58863 | 80 |
synchronized (configuration) { |
81 |
dispatcher = new Dispatcher(configuration); |
|
59327
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
82 |
configuration.setChanged(false); |
58863 | 83 |
} |
84 |
} |
|
85 |
return dispatcher; |
|
86 |
} |
|
87 |
||
88 |
@Override |
|
89 |
public final void setOrdered(boolean ordered) { |
|
90 |
configuration.setOrdered(ordered); |
|
91 |
} |
|
92 |
||
93 |
@Override |
|
94 |
public final void setReuse(boolean reuse) { |
|
95 |
configuration.setReuse(reuse); |
|
96 |
} |
|
97 |
||
98 |
@Override |
|
99 |
public final void setStartTime(Instant startTime) { |
|
100 |
Objects.nonNull(startTime); |
|
101 |
synchronized (configuration) { |
|
102 |
if (configuration.started) { |
|
103 |
throw new IllegalStateException("Stream is already started"); |
|
104 |
} |
|
105 |
if (startTime.isBefore(Instant.EPOCH)) { |
|
106 |
startTime = Instant.EPOCH; |
|
107 |
} |
|
108 |
configuration.setStartTime(startTime); |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
@Override |
|
113 |
public final void setEndTime(Instant endTime) { |
|
114 |
Objects.requireNonNull(endTime); |
|
115 |
synchronized (configuration) { |
|
116 |
if (configuration.started) { |
|
117 |
throw new IllegalStateException("Stream is already started"); |
|
118 |
} |
|
119 |
configuration.setEndTime(endTime); |
|
120 |
} |
|
121 |
} |
|
122 |
||
123 |
@Override |
|
124 |
public final void onEvent(Consumer<RecordedEvent> action) { |
|
125 |
Objects.requireNonNull(action); |
|
126 |
configuration.addEventAction(action); |
|
127 |
} |
|
128 |
||
129 |
@Override |
|
130 |
public final void onEvent(String eventName, Consumer<RecordedEvent> action) { |
|
131 |
Objects.requireNonNull(eventName); |
|
132 |
Objects.requireNonNull(action); |
|
133 |
configuration.addEventAction(eventName, action); |
|
134 |
} |
|
135 |
||
136 |
@Override |
|
137 |
public final void onFlush(Runnable action) { |
|
138 |
Objects.requireNonNull(action); |
|
139 |
configuration.addFlushAction(action); |
|
140 |
} |
|
141 |
||
142 |
@Override |
|
143 |
public final void onClose(Runnable action) { |
|
144 |
Objects.requireNonNull(action); |
|
145 |
configuration.addCloseAction(action); |
|
146 |
} |
|
147 |
||
148 |
@Override |
|
149 |
public final void onError(Consumer<Throwable> action) { |
|
150 |
Objects.requireNonNull(action); |
|
151 |
configuration.addErrorAction(action); |
|
152 |
} |
|
153 |
||
154 |
@Override |
|
155 |
public final boolean remove(Object action) { |
|
156 |
Objects.requireNonNull(action); |
|
157 |
return configuration.remove(action); |
|
158 |
} |
|
159 |
||
160 |
@Override |
|
161 |
public final void awaitTermination() throws InterruptedException { |
|
162 |
awaitTermination(Duration.ofMillis(0)); |
|
163 |
} |
|
164 |
||
165 |
@Override |
|
166 |
public final void awaitTermination(Duration timeout) throws InterruptedException { |
|
167 |
Objects.requireNonNull(timeout); |
|
168 |
if (timeout.isNegative()) { |
|
169 |
throw new IllegalArgumentException("timeout value is negative"); |
|
170 |
} |
|
171 |
||
172 |
long base = System.currentTimeMillis(); |
|
173 |
long now = 0; |
|
174 |
||
175 |
long millis; |
|
176 |
try { |
|
177 |
millis = Math.multiplyExact(timeout.getSeconds(), 1000); |
|
178 |
} catch (ArithmeticException a) { |
|
179 |
millis = Long.MAX_VALUE; |
|
180 |
} |
|
181 |
int nanos = timeout.toNanosPart(); |
|
182 |
if (nanos == 0 && millis == 0) { |
|
183 |
synchronized (terminated) { |
|
184 |
while (!isClosed()) { |
|
185 |
terminated.wait(0); |
|
186 |
} |
|
187 |
} |
|
188 |
} else { |
|
189 |
while (!isClosed()) { |
|
190 |
long delay = millis - now; |
|
191 |
if (delay <= 0) { |
|
192 |
break; |
|
193 |
} |
|
194 |
synchronized (terminated) { |
|
195 |
terminated.wait(delay, nanos); |
|
196 |
} |
|
197 |
now = System.currentTimeMillis() - base; |
|
198 |
} |
|
199 |
} |
|
200 |
} |
|
201 |
||
202 |
protected abstract void process() throws IOException; |
|
203 |
||
204 |
protected final void setClosed(boolean closed) { |
|
205 |
this.closed = closed; |
|
206 |
} |
|
207 |
||
208 |
protected final boolean isClosed() { |
|
209 |
return closed; |
|
210 |
} |
|
211 |
||
212 |
public final void startAsync(long startNanos) { |
|
213 |
startInternal(startNanos); |
|
214 |
Runnable r = () -> run(accessControllerContext); |
|
215 |
thread = SecuritySupport.createThreadWitNoPermissions(nextThreadName(), r); |
|
216 |
thread.start(); |
|
217 |
} |
|
218 |
||
219 |
public final void start(long startNanos) { |
|
220 |
startInternal(startNanos); |
|
221 |
thread = Thread.currentThread(); |
|
222 |
run(accessControllerContext); |
|
223 |
} |
|
224 |
||
225 |
protected final Runnable getFlushOperation() { |
|
226 |
return flushOperation; |
|
227 |
} |
|
228 |
||
229 |
private void startInternal(long startNanos) { |
|
230 |
synchronized (configuration) { |
|
231 |
if (configuration.started) { |
|
232 |
throw new IllegalStateException("Event stream can only be started once"); |
|
233 |
} |
|
59226 | 234 |
if (recording != null && configuration.startTime == null) { |
58863 | 235 |
configuration.setStartNanos(startNanos); |
236 |
} |
|
237 |
configuration.setStarted(true); |
|
238 |
} |
|
239 |
} |
|
240 |
||
241 |
private void execute() { |
|
242 |
try { |
|
243 |
process(); |
|
244 |
} catch (IOException ioe) { |
|
245 |
// This can happen if a chunk file is removed, or |
|
246 |
// a file is access that has been closed |
|
247 |
// This is "normal" behavior for streaming and the |
|
248 |
// stream will be closed when this happens. |
|
249 |
} finally { |
|
250 |
Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "Execution of stream ended."); |
|
251 |
try { |
|
252 |
close(); |
|
253 |
} finally { |
|
254 |
synchronized (terminated) { |
|
255 |
terminated.notifyAll(); |
|
256 |
} |
|
257 |
} |
|
258 |
} |
|
259 |
} |
|
260 |
||
261 |
private void run(AccessControlContext accessControlContext) { |
|
262 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
|
263 |
@Override |
|
264 |
public Void run() { |
|
265 |
execute(); |
|
266 |
return null; |
|
267 |
} |
|
268 |
}, accessControlContext); |
|
269 |
} |
|
270 |
||
271 |
private String nextThreadName() { |
|
272 |
counter.incrementAndGet(); |
|
273 |
return "JFR Event Stream " + counter; |
|
274 |
} |
|
275 |
} |