author | egahlin |
Thu, 28 Nov 2019 16:38:25 +0100 | |
changeset 59310 | 72f3dd43dd28 |
parent 59226 | a0f39cc47387 |
child 59327 | 2c3578aa0bdf |
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() { |
|
79 |
if (configuration.hasChanged()) { |
|
80 |
synchronized (configuration) { |
|
81 |
dispatcher = new Dispatcher(configuration); |
|
82 |
} |
|
83 |
} |
|
84 |
return dispatcher; |
|
85 |
} |
|
86 |
||
87 |
@Override |
|
88 |
public final void setOrdered(boolean ordered) { |
|
89 |
configuration.setOrdered(ordered); |
|
90 |
} |
|
91 |
||
92 |
@Override |
|
93 |
public final void setReuse(boolean reuse) { |
|
94 |
configuration.setReuse(reuse); |
|
95 |
} |
|
96 |
||
97 |
@Override |
|
98 |
public final void setStartTime(Instant startTime) { |
|
99 |
Objects.nonNull(startTime); |
|
100 |
synchronized (configuration) { |
|
101 |
if (configuration.started) { |
|
102 |
throw new IllegalStateException("Stream is already started"); |
|
103 |
} |
|
104 |
if (startTime.isBefore(Instant.EPOCH)) { |
|
105 |
startTime = Instant.EPOCH; |
|
106 |
} |
|
107 |
configuration.setStartTime(startTime); |
|
108 |
} |
|
109 |
} |
|
110 |
||
111 |
@Override |
|
112 |
public final void setEndTime(Instant endTime) { |
|
113 |
Objects.requireNonNull(endTime); |
|
114 |
synchronized (configuration) { |
|
115 |
if (configuration.started) { |
|
116 |
throw new IllegalStateException("Stream is already started"); |
|
117 |
} |
|
118 |
configuration.setEndTime(endTime); |
|
119 |
} |
|
120 |
} |
|
121 |
||
122 |
@Override |
|
123 |
public final void onEvent(Consumer<RecordedEvent> action) { |
|
124 |
Objects.requireNonNull(action); |
|
125 |
configuration.addEventAction(action); |
|
126 |
} |
|
127 |
||
128 |
@Override |
|
129 |
public final void onEvent(String eventName, Consumer<RecordedEvent> action) { |
|
130 |
Objects.requireNonNull(eventName); |
|
131 |
Objects.requireNonNull(action); |
|
132 |
configuration.addEventAction(eventName, action); |
|
133 |
} |
|
134 |
||
135 |
@Override |
|
136 |
public final void onFlush(Runnable action) { |
|
137 |
Objects.requireNonNull(action); |
|
138 |
configuration.addFlushAction(action); |
|
139 |
} |
|
140 |
||
141 |
@Override |
|
142 |
public final void onClose(Runnable action) { |
|
143 |
Objects.requireNonNull(action); |
|
144 |
configuration.addCloseAction(action); |
|
145 |
} |
|
146 |
||
147 |
@Override |
|
148 |
public final void onError(Consumer<Throwable> action) { |
|
149 |
Objects.requireNonNull(action); |
|
150 |
configuration.addErrorAction(action); |
|
151 |
} |
|
152 |
||
153 |
@Override |
|
154 |
public final boolean remove(Object action) { |
|
155 |
Objects.requireNonNull(action); |
|
156 |
return configuration.remove(action); |
|
157 |
} |
|
158 |
||
159 |
@Override |
|
160 |
public final void awaitTermination() throws InterruptedException { |
|
161 |
awaitTermination(Duration.ofMillis(0)); |
|
162 |
} |
|
163 |
||
164 |
@Override |
|
165 |
public final void awaitTermination(Duration timeout) throws InterruptedException { |
|
166 |
Objects.requireNonNull(timeout); |
|
167 |
if (timeout.isNegative()) { |
|
168 |
throw new IllegalArgumentException("timeout value is negative"); |
|
169 |
} |
|
170 |
||
171 |
long base = System.currentTimeMillis(); |
|
172 |
long now = 0; |
|
173 |
||
174 |
long millis; |
|
175 |
try { |
|
176 |
millis = Math.multiplyExact(timeout.getSeconds(), 1000); |
|
177 |
} catch (ArithmeticException a) { |
|
178 |
millis = Long.MAX_VALUE; |
|
179 |
} |
|
180 |
int nanos = timeout.toNanosPart(); |
|
181 |
if (nanos == 0 && millis == 0) { |
|
182 |
synchronized (terminated) { |
|
183 |
while (!isClosed()) { |
|
184 |
terminated.wait(0); |
|
185 |
} |
|
186 |
} |
|
187 |
} else { |
|
188 |
while (!isClosed()) { |
|
189 |
long delay = millis - now; |
|
190 |
if (delay <= 0) { |
|
191 |
break; |
|
192 |
} |
|
193 |
synchronized (terminated) { |
|
194 |
terminated.wait(delay, nanos); |
|
195 |
} |
|
196 |
now = System.currentTimeMillis() - base; |
|
197 |
} |
|
198 |
} |
|
199 |
} |
|
200 |
||
201 |
protected abstract void process() throws IOException; |
|
202 |
||
203 |
protected final void setClosed(boolean closed) { |
|
204 |
this.closed = closed; |
|
205 |
} |
|
206 |
||
207 |
protected final boolean isClosed() { |
|
208 |
return closed; |
|
209 |
} |
|
210 |
||
211 |
public final void startAsync(long startNanos) { |
|
212 |
startInternal(startNanos); |
|
213 |
Runnable r = () -> run(accessControllerContext); |
|
214 |
thread = SecuritySupport.createThreadWitNoPermissions(nextThreadName(), r); |
|
215 |
thread.start(); |
|
216 |
} |
|
217 |
||
218 |
public final void start(long startNanos) { |
|
219 |
startInternal(startNanos); |
|
220 |
thread = Thread.currentThread(); |
|
221 |
run(accessControllerContext); |
|
222 |
} |
|
223 |
||
224 |
protected final Runnable getFlushOperation() { |
|
225 |
return flushOperation; |
|
226 |
} |
|
227 |
||
228 |
private void startInternal(long startNanos) { |
|
229 |
synchronized (configuration) { |
|
230 |
if (configuration.started) { |
|
231 |
throw new IllegalStateException("Event stream can only be started once"); |
|
232 |
} |
|
59226 | 233 |
if (recording != null && configuration.startTime == null) { |
58863 | 234 |
configuration.setStartNanos(startNanos); |
235 |
} |
|
236 |
configuration.setStarted(true); |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
private void execute() { |
|
241 |
try { |
|
242 |
process(); |
|
243 |
} catch (IOException ioe) { |
|
244 |
// This can happen if a chunk file is removed, or |
|
245 |
// a file is access that has been closed |
|
246 |
// This is "normal" behavior for streaming and the |
|
247 |
// stream will be closed when this happens. |
|
248 |
} finally { |
|
249 |
Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "Execution of stream ended."); |
|
250 |
try { |
|
251 |
close(); |
|
252 |
} finally { |
|
253 |
synchronized (terminated) { |
|
254 |
terminated.notifyAll(); |
|
255 |
} |
|
256 |
} |
|
257 |
} |
|
258 |
} |
|
259 |
||
260 |
private void run(AccessControlContext accessControlContext) { |
|
261 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
|
262 |
@Override |
|
263 |
public Void run() { |
|
264 |
execute(); |
|
265 |
return null; |
|
266 |
} |
|
267 |
}, accessControlContext); |
|
268 |
} |
|
269 |
||
270 |
private String nextThreadName() { |
|
271 |
counter.incrementAndGet(); |
|
272 |
return "JFR Event Stream " + counter; |
|
273 |
} |
|
274 |
} |