author | egahlin |
Mon, 21 May 2018 18:19:11 +0200 | |
changeset 50197 | f4735ff8d17d |
parent 50194 | 2ae4cd9d809d |
child 50745 | a390cbb82d47 |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
2 |
* Copyright (c) 2016, 2018, 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; |
|
27 |
||
28 |
import static jdk.jfr.internal.LogLevel.DEBUG; |
|
29 |
import static jdk.jfr.internal.LogLevel.WARN; |
|
30 |
import static jdk.jfr.internal.LogTag.JFR; |
|
31 |
||
32 |
import java.io.IOException; |
|
33 |
import java.io.InputStream; |
|
34 |
import java.nio.channels.FileChannel; |
|
35 |
import java.nio.file.StandardOpenOption; |
|
36 |
import java.security.AccessControlContext; |
|
37 |
import java.security.AccessController; |
|
38 |
import java.time.Duration; |
|
39 |
import java.time.Instant; |
|
40 |
import java.time.LocalDateTime; |
|
41 |
import java.util.ArrayList; |
|
42 |
import java.util.Collections; |
|
43 |
import java.util.Date; |
|
44 |
import java.util.HashMap; |
|
45 |
import java.util.LinkedHashMap; |
|
46 |
import java.util.LinkedList; |
|
47 |
import java.util.List; |
|
48 |
import java.util.Map; |
|
49 |
import java.util.StringJoiner; |
|
50 |
import java.util.TimerTask; |
|
51 |
import java.util.TreeMap; |
|
52 |
||
53 |
import jdk.jfr.Configuration; |
|
54 |
import jdk.jfr.FlightRecorderListener; |
|
55 |
import jdk.jfr.Recording; |
|
56 |
import jdk.jfr.RecordingState; |
|
57 |
||
58 |
public final class PlatformRecording implements AutoCloseable { |
|
59 |
||
60 |
private final PlatformRecorder recorder; |
|
61 |
private final long id; |
|
62 |
// Recording settings |
|
63 |
private Map<String, String> settings = new LinkedHashMap<>(); |
|
64 |
private Duration duration; |
|
65 |
private Duration maxAge; |
|
66 |
private long maxSize; |
|
67 |
||
68 |
private WriteableUserPath destination; |
|
69 |
||
70 |
private boolean toDisk = true; |
|
71 |
private String name; |
|
72 |
private boolean dumpOnExit; |
|
73 |
// Timestamp information |
|
74 |
private Instant stopTime; |
|
75 |
private Instant startTime; |
|
76 |
||
77 |
// Misc, information |
|
78 |
private RecordingState state = RecordingState.NEW; |
|
79 |
private long size; |
|
80 |
private final LinkedList<RepositoryChunk> chunks = new LinkedList<>(); |
|
81 |
private volatile Recording recording; |
|
82 |
private TimerTask stopTask; |
|
83 |
private TimerTask startTask; |
|
84 |
private AccessControlContext noDestinationDumpOnExitAccessControlContext; |
|
85 |
private boolean shuoldWriteActiveRecordingEvent = true; |
|
86 |
||
87 |
PlatformRecording(PlatformRecorder recorder, long id) { |
|
88 |
// Typically the access control context is taken |
|
89 |
// when you call dump(Path) or setDdestination(Path), |
|
90 |
// but if no destination is set and dumponexit=true |
|
91 |
// the control context of the recording is taken when the |
|
92 |
// Recording object is constructed. This works well for |
|
93 |
// -XX:StartFlightRecording and JFR.dump |
|
94 |
this.noDestinationDumpOnExitAccessControlContext = AccessController.getContext(); |
|
95 |
this.id = id; |
|
96 |
this.recorder = recorder; |
|
97 |
this.name = String.valueOf(id); |
|
98 |
} |
|
99 |
||
100 |
public void start() { |
|
101 |
RecordingState oldState; |
|
102 |
RecordingState newState; |
|
103 |
synchronized (recorder) { |
|
50197
f4735ff8d17d
8195818: JFR.start should increase autogenerated name by one
egahlin
parents:
50194
diff
changeset
|
104 |
oldState = getState(); |
50113 | 105 |
if (!Utils.isBefore(state, RecordingState.RUNNING)) { |
106 |
throw new IllegalStateException("Recording can only be started once."); |
|
107 |
} |
|
108 |
if (startTask != null) { |
|
109 |
startTask.cancel(); |
|
110 |
startTask = null; |
|
111 |
startTime = null; |
|
112 |
} |
|
113 |
recorder.start(this); |
|
114 |
Logger.log(LogTag.JFR, LogLevel.INFO, () -> { |
|
115 |
// Only print non-default values so it easy to see |
|
116 |
// which options were added |
|
117 |
StringJoiner options = new StringJoiner(", "); |
|
118 |
if (!toDisk) { |
|
119 |
options.add("disk=false"); |
|
120 |
} |
|
121 |
if (maxAge != null) { |
|
122 |
options.add("maxage=" + Utils.formatTimespan(maxAge, "")); |
|
123 |
} |
|
124 |
if (maxSize != 0) { |
|
125 |
options.add("maxsize=" + Utils.formatBytes(maxSize, "")); |
|
126 |
} |
|
127 |
if (dumpOnExit) { |
|
128 |
options.add("dumponexit=true"); |
|
129 |
} |
|
130 |
if (duration != null) { |
|
131 |
options.add("duration=" + Utils.formatTimespan(duration, "")); |
|
132 |
} |
|
133 |
if (destination != null) { |
|
134 |
options.add("filename=" + destination.getText()); |
|
135 |
} |
|
136 |
String optionText = options.toString(); |
|
137 |
if (optionText.length() != 0) { |
|
138 |
optionText = "{" + optionText + "}"; |
|
139 |
} |
|
140 |
return "Started recording \"" + getName() + "\" (" + getId() + ") " + optionText; |
|
141 |
}); |
|
50197
f4735ff8d17d
8195818: JFR.start should increase autogenerated name by one
egahlin
parents:
50194
diff
changeset
|
142 |
newState = getState(); |
50113 | 143 |
} |
144 |
notifyIfStateChanged(oldState, newState); |
|
145 |
} |
|
146 |
||
147 |
public boolean stop(String reason) { |
|
148 |
return stop(reason, null); |
|
149 |
} |
|
150 |
||
151 |
public boolean stop(String reason, WriteableUserPath alternativePath) { |
|
152 |
return stop(reason, alternativePath, Collections.emptyMap()); |
|
153 |
} |
|
154 |
||
155 |
boolean stop(String reason, WriteableUserPath alternativePath, Map<String, String> overlaySettings) { |
|
156 |
RecordingState oldState; |
|
157 |
RecordingState newState; |
|
158 |
synchronized (recorder) { |
|
50197
f4735ff8d17d
8195818: JFR.start should increase autogenerated name by one
egahlin
parents:
50194
diff
changeset
|
159 |
oldState = getState(); |
50113 | 160 |
if (stopTask != null) { |
161 |
stopTask.cancel(); |
|
162 |
stopTask = null; |
|
163 |
} |
|
50194
2ae4cd9d809d
8198337: -XX:StartFlightRecording=dumponexit=true,disk=false doesn't work
egahlin
parents:
50113
diff
changeset
|
164 |
recorder.stop(this, alternativePath); |
50197
f4735ff8d17d
8195818: JFR.start should increase autogenerated name by one
egahlin
parents:
50194
diff
changeset
|
165 |
String endText = reason == null ? "" : ". Reason \"" + reason + "\"."; |
f4735ff8d17d
8195818: JFR.start should increase autogenerated name by one
egahlin
parents:
50194
diff
changeset
|
166 |
Logger.log(LogTag.JFR, LogLevel.INFO, "Stopped recording \"" + getName() + "\" (" + getId()+ ")" + endText); |
50113 | 167 |
this.stopTime = Instant.now(); |
50197
f4735ff8d17d
8195818: JFR.start should increase autogenerated name by one
egahlin
parents:
50194
diff
changeset
|
168 |
newState = getState(); |
50113 | 169 |
} |
170 |
WriteableUserPath dest = getDestination(); |
|
171 |
if (dest == null && alternativePath != null) { |
|
172 |
dest = alternativePath; |
|
173 |
} |
|
174 |
if (dest != null) { |
|
175 |
try { |
|
176 |
copyTo(dest, reason, overlaySettings); |
|
50197
f4735ff8d17d
8195818: JFR.start should increase autogenerated name by one
egahlin
parents:
50194
diff
changeset
|
177 |
Logger.log(LogTag.JFR, LogLevel.INFO, "Wrote recording \"" + getName() + "\" (" + getId()+ ") to " + dest.getText()); |
50113 | 178 |
notifyIfStateChanged(newState, oldState); |
179 |
close(); // remove if copied out |
|
180 |
} catch (IOException e) { |
|
181 |
// throw e; // BUG8925030 |
|
182 |
} |
|
183 |
} else { |
|
184 |
notifyIfStateChanged(newState, oldState); |
|
185 |
} |
|
186 |
return true; |
|
187 |
} |
|
188 |
||
189 |
public void scheduleStart(Duration delay) { |
|
190 |
synchronized (recorder) { |
|
191 |
ensureOkForSchedule(); |
|
192 |
||
193 |
startTime = Instant.now().plus(delay); |
|
194 |
LocalDateTime now = LocalDateTime.now().plus(delay); |
|
195 |
setState(RecordingState.DELAYED); |
|
196 |
startTask = createStartTask(); |
|
197 |
recorder.getTimer().schedule(startTask, delay.toMillis()); |
|
50197
f4735ff8d17d
8195818: JFR.start should increase autogenerated name by one
egahlin
parents:
50194
diff
changeset
|
198 |
Logger.log(LogTag.JFR, LogLevel.INFO, "Scheduled recording \"" + getName() + "\" (" + getId()+ ") to start at " + now); |
50113 | 199 |
} |
200 |
} |
|
201 |
||
202 |
private void ensureOkForSchedule() { |
|
203 |
if (getState() != RecordingState.NEW) { |
|
204 |
throw new IllegalStateException("Only a new recoridng can be scheduled for start"); |
|
205 |
} |
|
206 |
} |
|
207 |
||
208 |
private TimerTask createStartTask() { |
|
209 |
// Taking ref. to recording here. |
|
210 |
// Opens up for memory leaks. |
|
211 |
return new TimerTask() { |
|
212 |
@Override |
|
213 |
public void run() { |
|
214 |
synchronized (recorder) { |
|
215 |
if (getState() != RecordingState.DELAYED) { |
|
216 |
return; |
|
217 |
} |
|
218 |
start(); |
|
219 |
} |
|
220 |
} |
|
221 |
}; |
|
222 |
} |
|
223 |
||
224 |
void scheduleStart(Instant startTime) { |
|
225 |
synchronized (recorder) { |
|
226 |
ensureOkForSchedule(); |
|
227 |
this.startTime = startTime; |
|
228 |
setState(RecordingState.DELAYED); |
|
229 |
startTask = createStartTask(); |
|
230 |
recorder.getTimer().schedule(startTask, startTime.toEpochMilli()); |
|
231 |
} |
|
232 |
} |
|
233 |
||
234 |
public Map<String, String> getSettings() { |
|
235 |
synchronized (recorder) { |
|
236 |
return settings; |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
public long getSize() { |
|
241 |
return size; |
|
242 |
} |
|
243 |
||
244 |
public Instant getStopTime() { |
|
245 |
synchronized (recorder) { |
|
246 |
return stopTime; |
|
247 |
} |
|
248 |
} |
|
249 |
||
250 |
public Instant getStartTime() { |
|
251 |
synchronized (recorder) { |
|
252 |
return startTime; |
|
253 |
} |
|
254 |
} |
|
255 |
||
256 |
public Long getMaxSize() { |
|
257 |
synchronized (recorder) { |
|
258 |
return maxSize; |
|
259 |
} |
|
260 |
} |
|
261 |
||
262 |
public Duration getMaxAge() { |
|
263 |
synchronized (recorder) { |
|
264 |
return maxAge; |
|
265 |
} |
|
266 |
} |
|
267 |
||
268 |
public String getName() { |
|
269 |
synchronized (recorder) { |
|
270 |
return name; |
|
271 |
} |
|
272 |
} |
|
273 |
||
274 |
||
275 |
public RecordingState getState() { |
|
276 |
synchronized (recorder) { |
|
277 |
return state; |
|
278 |
} |
|
279 |
} |
|
280 |
||
281 |
@Override |
|
282 |
public void close() { |
|
283 |
RecordingState oldState; |
|
284 |
RecordingState newState; |
|
285 |
||
286 |
synchronized (recorder) { |
|
287 |
oldState = getState(); |
|
288 |
if (RecordingState.CLOSED != getState()) { |
|
289 |
if (startTask != null) { |
|
290 |
startTask.cancel(); |
|
291 |
startTask = null; |
|
292 |
} |
|
293 |
recorder.finish(this); |
|
294 |
for (RepositoryChunk c : chunks) { |
|
295 |
removed(c); |
|
296 |
} |
|
297 |
chunks.clear(); |
|
298 |
setState(RecordingState.CLOSED); |
|
299 |
Logger.log(LogTag.JFR, LogLevel.INFO, "Closed recording \"" + getName() + "\" (" + getId()+ ")"); |
|
300 |
} |
|
301 |
newState = getState(); |
|
302 |
} |
|
303 |
notifyIfStateChanged(newState, oldState); |
|
304 |
} |
|
305 |
||
306 |
public void copyTo(WriteableUserPath path, String reason, Map<String, String> dumpSettings) throws IOException { |
|
307 |
synchronized (recorder) { |
|
308 |
RecordingState state = getState(); |
|
309 |
if (state == RecordingState.CLOSED) { |
|
310 |
throw new IOException("Recording \"" + name + "\" (id=" + id + ") has been closed, no contents to write"); |
|
311 |
} |
|
312 |
if (state == RecordingState.DELAYED || state == RecordingState.NEW) { |
|
313 |
throw new IOException("Recording \"" + name + "\" (id=" + id + ") has not started, no contents to write"); |
|
314 |
} |
|
315 |
if (state == RecordingState.STOPPED) { |
|
316 |
// have all we need, just write it |
|
317 |
dumpToFile(path, reason, getId()); |
|
318 |
return; |
|
319 |
} |
|
320 |
||
321 |
// Recording is RUNNING, create a clone |
|
50197
f4735ff8d17d
8195818: JFR.start should increase autogenerated name by one
egahlin
parents:
50194
diff
changeset
|
322 |
try(PlatformRecording clone = recorder.newRecording(Collections.emptyMap(), 0)) { |
50113 | 323 |
clone.setShouldWriteActiveRecordingEvent(false); |
324 |
clone.setName(getName()); |
|
325 |
clone.setDestination(path); |
|
326 |
clone.setToDisk(true); |
|
327 |
// We purposely don't clone settings, since |
|
328 |
// a union a == a |
|
329 |
if (!isToDisk()) { |
|
330 |
// force memory contents to disk |
|
331 |
clone.start(); |
|
332 |
} else { |
|
333 |
// using existing chunks on disk |
|
334 |
for (RepositoryChunk c : chunks) { |
|
335 |
clone.add(c); |
|
336 |
} |
|
337 |
clone.setState(RecordingState.RUNNING); |
|
338 |
clone.setStartTime(getStartTime()); |
|
339 |
} |
|
340 |
if (dumpSettings.isEmpty()) { |
|
341 |
clone.setSettings(getSettings()); |
|
342 |
clone.stop(reason); // dumps to destination path here |
|
343 |
} else { |
|
344 |
// Risk of violating lock order here, since |
|
345 |
// clone.stop() will take recorder lock inside |
|
346 |
// metadata lock, but OK if we already |
|
347 |
// have recorder lock when we entered metadata lock |
|
348 |
Thread.holdsLock(recorder); |
|
349 |
synchronized(MetadataRepository.getInstance()) { |
|
350 |
Thread.holdsLock(recorder); |
|
351 |
Map<String, String> oldSettings = getSettings(); |
|
352 |
Map<String, String> newSettings = new HashMap<>(oldSettings); |
|
353 |
// replace with dump settings |
|
354 |
newSettings.putAll(dumpSettings); |
|
355 |
clone.setSettings(newSettings); |
|
356 |
clone.stop(reason); |
|
357 |
} |
|
358 |
} |
|
359 |
} |
|
360 |
return; |
|
361 |
} |
|
362 |
} |
|
363 |
||
364 |
private void dumpToFile(WriteableUserPath userPath, String reason, long id) throws IOException { |
|
365 |
userPath.doPriviligedIO(() -> { |
|
366 |
try (ChunksChannel cc = new ChunksChannel(chunks); FileChannel fc = FileChannel.open(userPath.getReal(), StandardOpenOption.WRITE, StandardOpenOption.APPEND)) { |
|
367 |
cc.transferTo(fc); |
|
368 |
fc.force(true); |
|
369 |
} |
|
370 |
return null; |
|
371 |
}); |
|
372 |
} |
|
373 |
||
374 |
public boolean isToDisk() { |
|
375 |
synchronized (recorder) { |
|
376 |
return toDisk; |
|
377 |
} |
|
378 |
} |
|
379 |
||
380 |
public void setMaxSize(long maxSize) { |
|
381 |
synchronized (recorder) { |
|
382 |
if (getState() == RecordingState.CLOSED) { |
|
383 |
throw new IllegalStateException("Can't set max age when recording is closed"); |
|
384 |
} |
|
385 |
this.maxSize = maxSize; |
|
386 |
trimToSize(); |
|
387 |
} |
|
388 |
} |
|
389 |
||
390 |
public void setDestination(WriteableUserPath userSuppliedPath) throws IOException { |
|
391 |
synchronized (recorder) { |
|
392 |
if (Utils.isState(getState(), RecordingState.STOPPED, RecordingState.CLOSED)) { |
|
393 |
throw new IllegalStateException("Destination can't be set on a recording that has been stopped/closed"); |
|
394 |
} |
|
395 |
this.destination = userSuppliedPath; |
|
396 |
} |
|
397 |
} |
|
398 |
||
399 |
public WriteableUserPath getDestination() { |
|
400 |
synchronized (recorder) { |
|
401 |
return destination; |
|
402 |
} |
|
403 |
} |
|
404 |
||
405 |
void setState(RecordingState state) { |
|
406 |
synchronized (recorder) { |
|
407 |
this.state = state; |
|
408 |
} |
|
409 |
} |
|
410 |
||
411 |
void setStartTime(Instant startTime) { |
|
412 |
synchronized (recorder) { |
|
413 |
this.startTime = startTime; |
|
414 |
} |
|
415 |
} |
|
416 |
||
417 |
void setStopTime(Instant timeStamp) { |
|
418 |
synchronized (recorder) { |
|
419 |
stopTime = timeStamp; |
|
420 |
} |
|
421 |
} |
|
422 |
||
423 |
public long getId() { |
|
424 |
synchronized (recorder) { |
|
425 |
return id; |
|
426 |
} |
|
427 |
} |
|
428 |
||
429 |
public void setName(String name) { |
|
430 |
synchronized (recorder) { |
|
431 |
ensureNotClosed(); |
|
432 |
this.name = name; |
|
433 |
} |
|
434 |
} |
|
435 |
||
436 |
private void ensureNotClosed() { |
|
437 |
if (getState() == RecordingState.CLOSED) { |
|
438 |
throw new IllegalStateException("Can't change name on a closed recording"); |
|
439 |
} |
|
440 |
} |
|
441 |
||
442 |
public void setDumpOnExit(boolean dumpOnExit) { |
|
443 |
synchronized (recorder) { |
|
444 |
this.dumpOnExit = dumpOnExit; |
|
445 |
} |
|
446 |
} |
|
447 |
||
448 |
public boolean getDumpOnExit() { |
|
449 |
synchronized (recorder) { |
|
450 |
return dumpOnExit; |
|
451 |
} |
|
452 |
} |
|
453 |
||
454 |
public void setToDisk(boolean toDisk) { |
|
455 |
synchronized (recorder) { |
|
456 |
if (Utils.isState(getState(), RecordingState.NEW, RecordingState.DELAYED)) { |
|
457 |
this.toDisk = toDisk; |
|
458 |
} else { |
|
459 |
throw new IllegalStateException("Recording option disk can't be changed after recording has started"); |
|
460 |
} |
|
461 |
} |
|
462 |
} |
|
463 |
||
464 |
public void setSetting(String id, String value) { |
|
465 |
synchronized (recorder) { |
|
466 |
this.settings.put(id, value); |
|
467 |
if (getState() == RecordingState.RUNNING) { |
|
468 |
recorder.updateSettings(); |
|
469 |
} |
|
470 |
} |
|
471 |
} |
|
472 |
||
473 |
public void setSettings(Map<String, String> settings) { |
|
474 |
setSettings(settings, true); |
|
475 |
} |
|
476 |
||
477 |
private void setSettings(Map<String, String> settings, boolean update) { |
|
478 |
if (LogTag.JFR_SETTING.shouldLog(LogLevel.INFO.level) && update) { |
|
479 |
TreeMap<String, String> ordered = new TreeMap<>(settings); |
|
480 |
Logger.log(LogTag.JFR_SETTING, LogLevel.INFO, "New settings for recording \"" + getName() + "\" (" + getId() + ")"); |
|
481 |
for (Map.Entry<String, String> entry : ordered.entrySet()) { |
|
482 |
String text = entry.getKey() + "=\"" + entry.getValue() + "\""; |
|
483 |
Logger.log(LogTag.JFR_SETTING, LogLevel.INFO, text); |
|
484 |
} |
|
485 |
} |
|
486 |
synchronized (recorder) { |
|
487 |
this.settings = new LinkedHashMap<>(settings); |
|
488 |
if (getState() == RecordingState.RUNNING && update) { |
|
489 |
recorder.updateSettings(); |
|
490 |
} |
|
491 |
} |
|
492 |
} |
|
493 |
||
494 |
||
495 |
private void notifyIfStateChanged(RecordingState newState, RecordingState oldState) { |
|
496 |
if (oldState == newState) { |
|
497 |
return; |
|
498 |
} |
|
499 |
for (FlightRecorderListener cl : PlatformRecorder.getListeners()) { |
|
500 |
try { |
|
501 |
cl.recordingStateChanged(getRecording()); |
|
502 |
} catch (RuntimeException re) { |
|
503 |
Logger.log(JFR, WARN, "Error notifying recorder listener:" + re.getMessage()); |
|
504 |
} |
|
505 |
} |
|
506 |
} |
|
507 |
||
508 |
public void setRecording(Recording recording) { |
|
509 |
this.recording = recording; |
|
510 |
} |
|
511 |
||
512 |
public Recording getRecording() { |
|
513 |
return recording; |
|
514 |
} |
|
515 |
||
516 |
@Override |
|
517 |
public String toString() { |
|
518 |
return getName() + " (id=" + getId() + ") " + getState(); |
|
519 |
} |
|
520 |
||
521 |
public void setConfiguration(Configuration c) { |
|
522 |
setSettings(c.getSettings()); |
|
523 |
} |
|
524 |
||
525 |
public void setMaxAge(Duration maxAge) { |
|
526 |
synchronized (recorder) { |
|
527 |
if (getState() == RecordingState.CLOSED) { |
|
528 |
throw new IllegalStateException("Can't set max age when recording is closed"); |
|
529 |
} |
|
530 |
this.maxAge = maxAge; |
|
531 |
if (maxAge != null) { |
|
532 |
trimToAge(Instant.now().minus(maxAge)); |
|
533 |
} |
|
534 |
} |
|
535 |
} |
|
536 |
||
537 |
void appendChunk(RepositoryChunk chunk) { |
|
538 |
if (!chunk.isFinished()) { |
|
539 |
throw new Error("not finished chunk " + chunk.getStartTime()); |
|
540 |
} |
|
541 |
synchronized (recorder) { |
|
542 |
if (!toDisk) { |
|
543 |
return; |
|
544 |
} |
|
545 |
if (maxAge != null) { |
|
546 |
trimToAge(chunk.getEndTime().minus(maxAge)); |
|
547 |
} |
|
548 |
chunks.addLast(chunk); |
|
549 |
added(chunk); |
|
550 |
trimToSize(); |
|
551 |
} |
|
552 |
} |
|
553 |
||
554 |
private void trimToSize() { |
|
555 |
if (maxSize == 0) { |
|
556 |
return; |
|
557 |
} |
|
558 |
while (size > maxSize && chunks.size() > 1) { |
|
559 |
RepositoryChunk c = chunks.removeFirst(); |
|
560 |
removed(c); |
|
561 |
} |
|
562 |
} |
|
563 |
||
564 |
private void trimToAge(Instant oldest) { |
|
565 |
while (!chunks.isEmpty()) { |
|
566 |
RepositoryChunk oldestChunk = chunks.peek(); |
|
567 |
if (oldestChunk.getEndTime().isAfter(oldest)) { |
|
568 |
return; |
|
569 |
} |
|
570 |
chunks.removeFirst(); |
|
571 |
removed(oldestChunk); |
|
572 |
} |
|
573 |
} |
|
574 |
||
575 |
void add(RepositoryChunk c) { |
|
576 |
chunks.add(c); |
|
577 |
added(c); |
|
578 |
} |
|
579 |
||
580 |
private void added(RepositoryChunk c) { |
|
581 |
c.use(); |
|
582 |
size += c.getSize(); |
|
583 |
Logger.log(JFR, DEBUG, ()-> "Recording \"" + name + "\" (" + id + ") added chunk " + c.toString() + ", current size=" + size); |
|
584 |
} |
|
585 |
||
586 |
private void removed(RepositoryChunk c) { |
|
587 |
size -= c.getSize(); |
|
588 |
Logger.log(JFR, DEBUG, ()-> "Recording \"" + name + "\" (" + id + ") removed chunk " + c.toString() + ", current size=" + size); |
|
589 |
c.release(); |
|
590 |
} |
|
591 |
||
592 |
public List<RepositoryChunk> getChunks() { |
|
593 |
return chunks; |
|
594 |
} |
|
595 |
||
596 |
public InputStream open(Instant start, Instant end) throws IOException { |
|
597 |
synchronized (recorder) { |
|
598 |
if (getState() != RecordingState.STOPPED) { |
|
599 |
throw new IOException("Recording must be stopped before it can be read."); |
|
600 |
} |
|
601 |
List<RepositoryChunk> chunksToUse = new ArrayList<RepositoryChunk>(); |
|
602 |
for (RepositoryChunk chunk : chunks) { |
|
603 |
if (chunk.isFinished()) { |
|
604 |
Instant chunkStart = chunk.getStartTime(); |
|
605 |
Instant chunkEnd = chunk.getEndTime(); |
|
606 |
if (start == null || !chunkEnd.isBefore(start)) { |
|
607 |
if (end == null || !chunkStart.isAfter(end)) { |
|
608 |
chunksToUse.add(chunk); |
|
609 |
} |
|
610 |
} |
|
611 |
} |
|
612 |
} |
|
613 |
if (chunksToUse.isEmpty()) { |
|
614 |
return null; |
|
615 |
} |
|
616 |
return new ChunkInputStream(chunksToUse); |
|
617 |
} |
|
618 |
} |
|
619 |
||
620 |
public Duration getDuration() { |
|
621 |
synchronized (recorder) { |
|
622 |
return duration; |
|
623 |
} |
|
624 |
} |
|
625 |
||
626 |
void setInternalDuration(Duration duration) { |
|
627 |
this.duration = duration; |
|
628 |
} |
|
629 |
||
630 |
public void setDuration(Duration duration) { |
|
631 |
synchronized (recorder) { |
|
632 |
if (Utils.isState(getState(), RecordingState.STOPPED, RecordingState.CLOSED)) { |
|
633 |
throw new IllegalStateException("Duration can't be set after a recording has been stopped/closed"); |
|
634 |
} |
|
635 |
setInternalDuration(duration); |
|
636 |
if (getState() != RecordingState.NEW) { |
|
637 |
updateTimer(); |
|
638 |
} |
|
639 |
} |
|
640 |
} |
|
641 |
||
642 |
void updateTimer() { |
|
643 |
if (stopTask != null) { |
|
644 |
stopTask.cancel(); |
|
645 |
stopTask = null; |
|
646 |
} |
|
647 |
if (getState() == RecordingState.CLOSED) { |
|
648 |
return; |
|
649 |
} |
|
650 |
if (duration != null) { |
|
651 |
stopTask = createStopTask(); |
|
652 |
recorder.getTimer().schedule(stopTask, new Date(startTime.plus(duration).toEpochMilli())); |
|
653 |
} |
|
654 |
} |
|
655 |
||
656 |
TimerTask createStopTask() { |
|
657 |
return new TimerTask() { |
|
658 |
@Override |
|
659 |
public void run() { |
|
660 |
try { |
|
661 |
stop("End of duration reached"); |
|
662 |
} catch (Throwable t) { |
|
663 |
// Prevent malicious user to propagate exception callback in the wrong context |
|
664 |
Logger.log(LogTag.JFR, LogLevel.ERROR, "Could not stop recording. " + t.getMessage()); |
|
665 |
} |
|
666 |
} |
|
667 |
}; |
|
668 |
} |
|
669 |
||
670 |
public Recording newCopy(boolean stop) { |
|
671 |
return recorder.newCopy(this, stop); |
|
672 |
} |
|
673 |
||
674 |
void setStopTask(TimerTask stopTask) { |
|
675 |
synchronized (recorder) { |
|
676 |
this.stopTask = stopTask; |
|
677 |
} |
|
678 |
} |
|
679 |
||
680 |
void clearDestination() { |
|
681 |
destination = null; |
|
682 |
} |
|
683 |
||
684 |
public AccessControlContext getNoDestinationDumpOnExitAccessControlContext() { |
|
685 |
return noDestinationDumpOnExitAccessControlContext; |
|
686 |
} |
|
687 |
||
688 |
void setShouldWriteActiveRecordingEvent(boolean shouldWrite) { |
|
689 |
this.shuoldWriteActiveRecordingEvent = shouldWrite; |
|
690 |
} |
|
691 |
||
692 |
boolean shouldWriteMetadataEvent() { |
|
693 |
return shuoldWriteActiveRecordingEvent; |
|
694 |
} |
|
695 |
} |