author | mgronlun |
Wed, 30 Oct 2019 19:43:52 +0100 | |
changeset 58863 | c16ac7a2eba4 |
parent 58480 | 8ca46e186a63 |
child 59010 | d4cae08239f7 |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
58863 | 2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. |
50113 | 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.LinkedHashMap; |
|
45 |
import java.util.LinkedList; |
|
46 |
import java.util.List; |
|
47 |
import java.util.Map; |
|
48 |
import java.util.StringJoiner; |
|
49 |
import java.util.TimerTask; |
|
50 |
import java.util.TreeMap; |
|
51 |
||
52 |
import jdk.jfr.Configuration; |
|
53 |
import jdk.jfr.FlightRecorderListener; |
|
54 |
import jdk.jfr.Recording; |
|
55 |
import jdk.jfr.RecordingState; |
|
50745 | 56 |
import jdk.jfr.internal.SecuritySupport.SafePath; |
50113 | 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; |
|
50745 | 73 |
private SafePath dumpOnExitDirectory = new SafePath("."); |
50113 | 74 |
// Timestamp information |
75 |
private Instant stopTime; |
|
76 |
private Instant startTime; |
|
77 |
||
78 |
// Misc, information |
|
79 |
private RecordingState state = RecordingState.NEW; |
|
80 |
private long size; |
|
81 |
private final LinkedList<RepositoryChunk> chunks = new LinkedList<>(); |
|
82 |
private volatile Recording recording; |
|
83 |
private TimerTask stopTask; |
|
84 |
private TimerTask startTask; |
|
85 |
private AccessControlContext noDestinationDumpOnExitAccessControlContext; |
|
86 |
private boolean shuoldWriteActiveRecordingEvent = true; |
|
58863 | 87 |
private Duration flushInterval = Duration.ofSeconds(1); |
50113 | 88 |
|
89 |
PlatformRecording(PlatformRecorder recorder, long id) { |
|
90 |
// Typically the access control context is taken |
|
91 |
// when you call dump(Path) or setDdestination(Path), |
|
92 |
// but if no destination is set and dumponexit=true |
|
93 |
// the control context of the recording is taken when the |
|
50745 | 94 |
// Recording object is constructed. This works well for |
50113 | 95 |
// -XX:StartFlightRecording and JFR.dump |
96 |
this.noDestinationDumpOnExitAccessControlContext = AccessController.getContext(); |
|
97 |
this.id = id; |
|
98 |
this.recorder = recorder; |
|
99 |
this.name = String.valueOf(id); |
|
100 |
} |
|
101 |
||
58863 | 102 |
public long start() { |
50113 | 103 |
RecordingState oldState; |
104 |
RecordingState newState; |
|
58863 | 105 |
long startNanos = -1; |
50113 | 106 |
synchronized (recorder) { |
50197
f4735ff8d17d
8195818: JFR.start should increase autogenerated name by one
egahlin
parents:
50194
diff
changeset
|
107 |
oldState = getState(); |
50113 | 108 |
if (!Utils.isBefore(state, RecordingState.RUNNING)) { |
109 |
throw new IllegalStateException("Recording can only be started once."); |
|
110 |
} |
|
111 |
if (startTask != null) { |
|
112 |
startTask.cancel(); |
|
113 |
startTask = null; |
|
114 |
startTime = null; |
|
115 |
} |
|
58863 | 116 |
startNanos = recorder.start(this); |
50113 | 117 |
Logger.log(LogTag.JFR, LogLevel.INFO, () -> { |
118 |
// Only print non-default values so it easy to see |
|
119 |
// which options were added |
|
50745 | 120 |
StringJoiner options = new StringJoiner(", "); |
121 |
if (!toDisk) { |
|
122 |
options.add("disk=false"); |
|
123 |
} |
|
124 |
if (maxAge != null) { |
|
125 |
options.add("maxage=" + Utils.formatTimespan(maxAge, "")); |
|
126 |
} |
|
127 |
if (maxSize != 0) { |
|
53013 | 128 |
options.add("maxsize=" + Utils.formatBytesCompact(maxSize)); |
50745 | 129 |
} |
130 |
if (dumpOnExit) { |
|
131 |
options.add("dumponexit=true"); |
|
132 |
} |
|
133 |
if (duration != null) { |
|
134 |
options.add("duration=" + Utils.formatTimespan(duration, "")); |
|
135 |
} |
|
136 |
if (destination != null) { |
|
55256
3b22c7e00573
8224217: RecordingInfo should use textual representation of path
egahlin
parents:
53013
diff
changeset
|
137 |
options.add("filename=" + destination.getRealPathText()); |
50745 | 138 |
} |
139 |
String optionText = options.toString(); |
|
140 |
if (optionText.length() != 0) { |
|
141 |
optionText = "{" + optionText + "}"; |
|
142 |
} |
|
143 |
return "Started recording \"" + getName() + "\" (" + getId() + ") " + optionText; |
|
144 |
}); |
|
50197
f4735ff8d17d
8195818: JFR.start should increase autogenerated name by one
egahlin
parents:
50194
diff
changeset
|
145 |
newState = getState(); |
50113 | 146 |
} |
147 |
notifyIfStateChanged(oldState, newState); |
|
58863 | 148 |
|
149 |
return startNanos; |
|
50113 | 150 |
} |
151 |
||
152 |
public boolean stop(String reason) { |
|
153 |
RecordingState oldState; |
|
154 |
RecordingState newState; |
|
155 |
synchronized (recorder) { |
|
50197
f4735ff8d17d
8195818: JFR.start should increase autogenerated name by one
egahlin
parents:
50194
diff
changeset
|
156 |
oldState = getState(); |
50113 | 157 |
if (stopTask != null) { |
158 |
stopTask.cancel(); |
|
159 |
stopTask = null; |
|
160 |
} |
|
50745 | 161 |
recorder.stop(this); |
50197
f4735ff8d17d
8195818: JFR.start should increase autogenerated name by one
egahlin
parents:
50194
diff
changeset
|
162 |
String endText = reason == null ? "" : ". Reason \"" + reason + "\"."; |
50745 | 163 |
Logger.log(LogTag.JFR, LogLevel.INFO, "Stopped recording \"" + getName() + "\" (" + getId() + ")" + endText); |
50113 | 164 |
this.stopTime = Instant.now(); |
50197
f4735ff8d17d
8195818: JFR.start should increase autogenerated name by one
egahlin
parents:
50194
diff
changeset
|
165 |
newState = getState(); |
50113 | 166 |
} |
167 |
WriteableUserPath dest = getDestination(); |
|
50745 | 168 |
|
50113 | 169 |
if (dest != null) { |
170 |
try { |
|
50745 | 171 |
dumpStopped(dest); |
55256
3b22c7e00573
8224217: RecordingInfo should use textual representation of path
egahlin
parents:
53013
diff
changeset
|
172 |
Logger.log(LogTag.JFR, LogLevel.INFO, "Wrote recording \"" + getName() + "\" (" + getId() + ") to " + dest.getRealPathText()); |
50113 | 173 |
notifyIfStateChanged(newState, oldState); |
174 |
close(); // remove if copied out |
|
50745 | 175 |
} catch(IOException e) { |
50113 | 176 |
// throw e; // BUG8925030 |
177 |
} |
|
178 |
} else { |
|
179 |
notifyIfStateChanged(newState, oldState); |
|
180 |
} |
|
181 |
return true; |
|
182 |
} |
|
183 |
||
184 |
public void scheduleStart(Duration delay) { |
|
185 |
synchronized (recorder) { |
|
186 |
ensureOkForSchedule(); |
|
187 |
||
188 |
startTime = Instant.now().plus(delay); |
|
189 |
LocalDateTime now = LocalDateTime.now().plus(delay); |
|
190 |
setState(RecordingState.DELAYED); |
|
191 |
startTask = createStartTask(); |
|
192 |
recorder.getTimer().schedule(startTask, delay.toMillis()); |
|
50745 | 193 |
Logger.log(LogTag.JFR, LogLevel.INFO, "Scheduled recording \"" + getName() + "\" (" + getId() + ") to start at " + now); |
50113 | 194 |
} |
195 |
} |
|
196 |
||
197 |
private void ensureOkForSchedule() { |
|
198 |
if (getState() != RecordingState.NEW) { |
|
199 |
throw new IllegalStateException("Only a new recoridng can be scheduled for start"); |
|
200 |
} |
|
201 |
} |
|
202 |
||
203 |
private TimerTask createStartTask() { |
|
204 |
// Taking ref. to recording here. |
|
205 |
// Opens up for memory leaks. |
|
206 |
return new TimerTask() { |
|
207 |
@Override |
|
208 |
public void run() { |
|
209 |
synchronized (recorder) { |
|
210 |
if (getState() != RecordingState.DELAYED) { |
|
211 |
return; |
|
212 |
} |
|
213 |
start(); |
|
214 |
} |
|
215 |
} |
|
216 |
}; |
|
217 |
} |
|
218 |
||
219 |
void scheduleStart(Instant startTime) { |
|
220 |
synchronized (recorder) { |
|
221 |
ensureOkForSchedule(); |
|
222 |
this.startTime = startTime; |
|
223 |
setState(RecordingState.DELAYED); |
|
224 |
startTask = createStartTask(); |
|
225 |
recorder.getTimer().schedule(startTask, startTime.toEpochMilli()); |
|
226 |
} |
|
227 |
} |
|
228 |
||
229 |
public Map<String, String> getSettings() { |
|
230 |
synchronized (recorder) { |
|
231 |
return settings; |
|
232 |
} |
|
233 |
} |
|
234 |
||
235 |
public long getSize() { |
|
236 |
return size; |
|
237 |
} |
|
238 |
||
239 |
public Instant getStopTime() { |
|
240 |
synchronized (recorder) { |
|
241 |
return stopTime; |
|
242 |
} |
|
243 |
} |
|
244 |
||
245 |
public Instant getStartTime() { |
|
246 |
synchronized (recorder) { |
|
247 |
return startTime; |
|
248 |
} |
|
249 |
} |
|
250 |
||
251 |
public Long getMaxSize() { |
|
252 |
synchronized (recorder) { |
|
253 |
return maxSize; |
|
254 |
} |
|
255 |
} |
|
256 |
||
257 |
public Duration getMaxAge() { |
|
258 |
synchronized (recorder) { |
|
259 |
return maxAge; |
|
260 |
} |
|
261 |
} |
|
262 |
||
263 |
public String getName() { |
|
264 |
synchronized (recorder) { |
|
265 |
return name; |
|
266 |
} |
|
267 |
} |
|
268 |
||
269 |
public RecordingState getState() { |
|
270 |
synchronized (recorder) { |
|
271 |
return state; |
|
272 |
} |
|
273 |
} |
|
274 |
||
275 |
@Override |
|
276 |
public void close() { |
|
277 |
RecordingState oldState; |
|
278 |
RecordingState newState; |
|
279 |
||
280 |
synchronized (recorder) { |
|
281 |
oldState = getState(); |
|
282 |
if (RecordingState.CLOSED != getState()) { |
|
283 |
if (startTask != null) { |
|
284 |
startTask.cancel(); |
|
285 |
startTask = null; |
|
286 |
} |
|
287 |
recorder.finish(this); |
|
288 |
for (RepositoryChunk c : chunks) { |
|
289 |
removed(c); |
|
290 |
} |
|
291 |
chunks.clear(); |
|
292 |
setState(RecordingState.CLOSED); |
|
50745 | 293 |
Logger.log(LogTag.JFR, LogLevel.INFO, "Closed recording \"" + getName() + "\" (" + getId() + ")"); |
50113 | 294 |
} |
295 |
newState = getState(); |
|
296 |
} |
|
297 |
notifyIfStateChanged(newState, oldState); |
|
298 |
} |
|
299 |
||
50745 | 300 |
// To be used internally when doing dumps. |
301 |
// Caller must have recorder lock and close recording before releasing lock |
|
302 |
public PlatformRecording newSnapshotClone(String reason, Boolean pathToGcRoots) throws IOException { |
|
303 |
if(!Thread.holdsLock(recorder)) { |
|
304 |
throw new InternalError("Caller must have recorder lock"); |
|
305 |
} |
|
306 |
RecordingState state = getState(); |
|
307 |
if (state == RecordingState.CLOSED) { |
|
308 |
throw new IOException("Recording \"" + name + "\" (id=" + id + ") has been closed, no contents to write"); |
|
309 |
} |
|
310 |
if (state == RecordingState.DELAYED || state == RecordingState.NEW) { |
|
311 |
throw new IOException("Recording \"" + name + "\" (id=" + id + ") has not started, no contents to write"); |
|
312 |
} |
|
313 |
if (state == RecordingState.STOPPED) { |
|
314 |
PlatformRecording clone = recorder.newTemporaryRecording(); |
|
315 |
for (RepositoryChunk r : chunks) { |
|
316 |
clone.add(r); |
|
50113 | 317 |
} |
50745 | 318 |
return clone; |
319 |
} |
|
50113 | 320 |
|
50745 | 321 |
// Recording is RUNNING, create a clone |
322 |
PlatformRecording clone = recorder.newTemporaryRecording(); |
|
323 |
clone.setShouldWriteActiveRecordingEvent(false); |
|
324 |
clone.setName(getName()); |
|
325 |
clone.setToDisk(true); |
|
326 |
// We purposely don't clone settings here, since |
|
327 |
// a union a == a |
|
328 |
if (!isToDisk()) { |
|
329 |
// force memory contents to disk |
|
330 |
clone.start(); |
|
331 |
} else { |
|
332 |
// using existing chunks on disk |
|
333 |
for (RepositoryChunk c : chunks) { |
|
334 |
clone.add(c); |
|
50113 | 335 |
} |
50745 | 336 |
clone.setState(RecordingState.RUNNING); |
337 |
clone.setStartTime(getStartTime()); |
|
50113 | 338 |
} |
50745 | 339 |
if (pathToGcRoots == null) { |
340 |
clone.setSettings(getSettings()); // needed for old object sample |
|
341 |
clone.stop(reason); // dumps to destination path here |
|
342 |
} else { |
|
343 |
// Risk of violating lock order here, since |
|
344 |
// clone.stop() will take recorder lock inside |
|
345 |
// metadata lock, but OK if we already |
|
346 |
// have recorder lock when we entered metadata lock |
|
347 |
synchronized (MetadataRepository.getInstance()) { |
|
348 |
clone.setSettings(OldObjectSample.createSettingsForSnapshot(this, pathToGcRoots)); |
|
349 |
clone.stop(reason); |
|
50113 | 350 |
} |
50745 | 351 |
} |
352 |
return clone; |
|
50113 | 353 |
} |
354 |
||
355 |
public boolean isToDisk() { |
|
356 |
synchronized (recorder) { |
|
357 |
return toDisk; |
|
358 |
} |
|
359 |
} |
|
360 |
||
361 |
public void setMaxSize(long maxSize) { |
|
362 |
synchronized (recorder) { |
|
363 |
if (getState() == RecordingState.CLOSED) { |
|
364 |
throw new IllegalStateException("Can't set max age when recording is closed"); |
|
365 |
} |
|
366 |
this.maxSize = maxSize; |
|
367 |
trimToSize(); |
|
368 |
} |
|
369 |
} |
|
370 |
||
50745 | 371 |
public void setDestination(WriteableUserPath userSuppliedPath) throws IOException { |
50113 | 372 |
synchronized (recorder) { |
58480
8ca46e186a63
8225694: Destination option missing in FlightRecorderMXBeanImpl
cito
parents:
55378
diff
changeset
|
373 |
checkSetDestination(userSuppliedPath); |
8ca46e186a63
8225694: Destination option missing in FlightRecorderMXBeanImpl
cito
parents:
55378
diff
changeset
|
374 |
this.destination = userSuppliedPath; |
8ca46e186a63
8225694: Destination option missing in FlightRecorderMXBeanImpl
cito
parents:
55378
diff
changeset
|
375 |
} |
8ca46e186a63
8225694: Destination option missing in FlightRecorderMXBeanImpl
cito
parents:
55378
diff
changeset
|
376 |
} |
8ca46e186a63
8225694: Destination option missing in FlightRecorderMXBeanImpl
cito
parents:
55378
diff
changeset
|
377 |
|
8ca46e186a63
8225694: Destination option missing in FlightRecorderMXBeanImpl
cito
parents:
55378
diff
changeset
|
378 |
public void checkSetDestination(WriteableUserPath userSuppliedPath) throws IOException { |
8ca46e186a63
8225694: Destination option missing in FlightRecorderMXBeanImpl
cito
parents:
55378
diff
changeset
|
379 |
synchronized (recorder) { |
50113 | 380 |
if (Utils.isState(getState(), RecordingState.STOPPED, RecordingState.CLOSED)) { |
381 |
throw new IllegalStateException("Destination can't be set on a recording that has been stopped/closed"); |
|
382 |
} |
|
383 |
} |
|
384 |
} |
|
385 |
||
386 |
public WriteableUserPath getDestination() { |
|
387 |
synchronized (recorder) { |
|
388 |
return destination; |
|
389 |
} |
|
390 |
} |
|
391 |
||
392 |
void setState(RecordingState state) { |
|
393 |
synchronized (recorder) { |
|
394 |
this.state = state; |
|
395 |
} |
|
396 |
} |
|
397 |
||
398 |
void setStartTime(Instant startTime) { |
|
399 |
synchronized (recorder) { |
|
400 |
this.startTime = startTime; |
|
401 |
} |
|
402 |
} |
|
403 |
||
404 |
void setStopTime(Instant timeStamp) { |
|
405 |
synchronized (recorder) { |
|
406 |
stopTime = timeStamp; |
|
407 |
} |
|
408 |
} |
|
409 |
||
410 |
public long getId() { |
|
411 |
synchronized (recorder) { |
|
412 |
return id; |
|
413 |
} |
|
414 |
} |
|
415 |
||
416 |
public void setName(String name) { |
|
417 |
synchronized (recorder) { |
|
418 |
ensureNotClosed(); |
|
419 |
this.name = name; |
|
420 |
} |
|
421 |
} |
|
422 |
||
423 |
private void ensureNotClosed() { |
|
424 |
if (getState() == RecordingState.CLOSED) { |
|
425 |
throw new IllegalStateException("Can't change name on a closed recording"); |
|
426 |
} |
|
427 |
} |
|
428 |
||
429 |
public void setDumpOnExit(boolean dumpOnExit) { |
|
430 |
synchronized (recorder) { |
|
431 |
this.dumpOnExit = dumpOnExit; |
|
432 |
} |
|
433 |
} |
|
434 |
||
435 |
public boolean getDumpOnExit() { |
|
436 |
synchronized (recorder) { |
|
437 |
return dumpOnExit; |
|
438 |
} |
|
439 |
} |
|
440 |
||
441 |
public void setToDisk(boolean toDisk) { |
|
442 |
synchronized (recorder) { |
|
443 |
if (Utils.isState(getState(), RecordingState.NEW, RecordingState.DELAYED)) { |
|
444 |
this.toDisk = toDisk; |
|
445 |
} else { |
|
446 |
throw new IllegalStateException("Recording option disk can't be changed after recording has started"); |
|
447 |
} |
|
448 |
} |
|
449 |
} |
|
450 |
||
451 |
public void setSetting(String id, String value) { |
|
452 |
synchronized (recorder) { |
|
453 |
this.settings.put(id, value); |
|
454 |
if (getState() == RecordingState.RUNNING) { |
|
455 |
recorder.updateSettings(); |
|
456 |
} |
|
457 |
} |
|
458 |
} |
|
459 |
||
460 |
public void setSettings(Map<String, String> settings) { |
|
461 |
setSettings(settings, true); |
|
462 |
} |
|
463 |
||
464 |
private void setSettings(Map<String, String> settings, boolean update) { |
|
52413 | 465 |
if (Logger.shouldLog(LogTag.JFR_SETTING, LogLevel.INFO) && update) { |
50113 | 466 |
TreeMap<String, String> ordered = new TreeMap<>(settings); |
467 |
Logger.log(LogTag.JFR_SETTING, LogLevel.INFO, "New settings for recording \"" + getName() + "\" (" + getId() + ")"); |
|
468 |
for (Map.Entry<String, String> entry : ordered.entrySet()) { |
|
50745 | 469 |
String text = entry.getKey() + "=\"" + entry.getValue() + "\""; |
50113 | 470 |
Logger.log(LogTag.JFR_SETTING, LogLevel.INFO, text); |
471 |
} |
|
472 |
} |
|
473 |
synchronized (recorder) { |
|
474 |
this.settings = new LinkedHashMap<>(settings); |
|
475 |
if (getState() == RecordingState.RUNNING && update) { |
|
476 |
recorder.updateSettings(); |
|
477 |
} |
|
478 |
} |
|
479 |
} |
|
480 |
||
481 |
private void notifyIfStateChanged(RecordingState newState, RecordingState oldState) { |
|
482 |
if (oldState == newState) { |
|
483 |
return; |
|
484 |
} |
|
485 |
for (FlightRecorderListener cl : PlatformRecorder.getListeners()) { |
|
486 |
try { |
|
487 |
cl.recordingStateChanged(getRecording()); |
|
488 |
} catch (RuntimeException re) { |
|
489 |
Logger.log(JFR, WARN, "Error notifying recorder listener:" + re.getMessage()); |
|
490 |
} |
|
491 |
} |
|
492 |
} |
|
493 |
||
494 |
public void setRecording(Recording recording) { |
|
495 |
this.recording = recording; |
|
496 |
} |
|
497 |
||
498 |
public Recording getRecording() { |
|
499 |
return recording; |
|
500 |
} |
|
501 |
||
502 |
@Override |
|
503 |
public String toString() { |
|
504 |
return getName() + " (id=" + getId() + ") " + getState(); |
|
505 |
} |
|
506 |
||
507 |
public void setConfiguration(Configuration c) { |
|
508 |
setSettings(c.getSettings()); |
|
509 |
} |
|
510 |
||
511 |
public void setMaxAge(Duration maxAge) { |
|
512 |
synchronized (recorder) { |
|
513 |
if (getState() == RecordingState.CLOSED) { |
|
514 |
throw new IllegalStateException("Can't set max age when recording is closed"); |
|
515 |
} |
|
516 |
this.maxAge = maxAge; |
|
517 |
if (maxAge != null) { |
|
518 |
trimToAge(Instant.now().minus(maxAge)); |
|
519 |
} |
|
520 |
} |
|
521 |
} |
|
522 |
||
523 |
void appendChunk(RepositoryChunk chunk) { |
|
524 |
if (!chunk.isFinished()) { |
|
525 |
throw new Error("not finished chunk " + chunk.getStartTime()); |
|
526 |
} |
|
527 |
synchronized (recorder) { |
|
528 |
if (!toDisk) { |
|
529 |
return; |
|
530 |
} |
|
531 |
if (maxAge != null) { |
|
532 |
trimToAge(chunk.getEndTime().minus(maxAge)); |
|
533 |
} |
|
534 |
chunks.addLast(chunk); |
|
535 |
added(chunk); |
|
536 |
trimToSize(); |
|
537 |
} |
|
538 |
} |
|
539 |
||
540 |
private void trimToSize() { |
|
541 |
if (maxSize == 0) { |
|
542 |
return; |
|
543 |
} |
|
544 |
while (size > maxSize && chunks.size() > 1) { |
|
545 |
RepositoryChunk c = chunks.removeFirst(); |
|
546 |
removed(c); |
|
547 |
} |
|
548 |
} |
|
549 |
||
550 |
private void trimToAge(Instant oldest) { |
|
551 |
while (!chunks.isEmpty()) { |
|
552 |
RepositoryChunk oldestChunk = chunks.peek(); |
|
553 |
if (oldestChunk.getEndTime().isAfter(oldest)) { |
|
554 |
return; |
|
555 |
} |
|
556 |
chunks.removeFirst(); |
|
557 |
removed(oldestChunk); |
|
558 |
} |
|
559 |
} |
|
560 |
||
561 |
void add(RepositoryChunk c) { |
|
562 |
chunks.add(c); |
|
563 |
added(c); |
|
564 |
} |
|
565 |
||
566 |
private void added(RepositoryChunk c) { |
|
567 |
c.use(); |
|
568 |
size += c.getSize(); |
|
50745 | 569 |
Logger.log(JFR, DEBUG, () -> "Recording \"" + name + "\" (" + id + ") added chunk " + c.toString() + ", current size=" + size); |
50113 | 570 |
} |
571 |
||
572 |
private void removed(RepositoryChunk c) { |
|
573 |
size -= c.getSize(); |
|
50745 | 574 |
Logger.log(JFR, DEBUG, () -> "Recording \"" + name + "\" (" + id + ") removed chunk " + c.toString() + ", current size=" + size); |
50113 | 575 |
c.release(); |
576 |
} |
|
577 |
||
578 |
public List<RepositoryChunk> getChunks() { |
|
579 |
return chunks; |
|
580 |
} |
|
581 |
||
582 |
public InputStream open(Instant start, Instant end) throws IOException { |
|
583 |
synchronized (recorder) { |
|
584 |
if (getState() != RecordingState.STOPPED) { |
|
585 |
throw new IOException("Recording must be stopped before it can be read."); |
|
586 |
} |
|
587 |
List<RepositoryChunk> chunksToUse = new ArrayList<RepositoryChunk>(); |
|
588 |
for (RepositoryChunk chunk : chunks) { |
|
589 |
if (chunk.isFinished()) { |
|
590 |
Instant chunkStart = chunk.getStartTime(); |
|
591 |
Instant chunkEnd = chunk.getEndTime(); |
|
592 |
if (start == null || !chunkEnd.isBefore(start)) { |
|
593 |
if (end == null || !chunkStart.isAfter(end)) { |
|
594 |
chunksToUse.add(chunk); |
|
595 |
} |
|
596 |
} |
|
597 |
} |
|
598 |
} |
|
599 |
if (chunksToUse.isEmpty()) { |
|
600 |
return null; |
|
601 |
} |
|
602 |
return new ChunkInputStream(chunksToUse); |
|
603 |
} |
|
604 |
} |
|
605 |
||
606 |
public Duration getDuration() { |
|
607 |
synchronized (recorder) { |
|
608 |
return duration; |
|
609 |
} |
|
610 |
} |
|
611 |
||
612 |
void setInternalDuration(Duration duration) { |
|
613 |
this.duration = duration; |
|
614 |
} |
|
615 |
||
616 |
public void setDuration(Duration duration) { |
|
617 |
synchronized (recorder) { |
|
618 |
if (Utils.isState(getState(), RecordingState.STOPPED, RecordingState.CLOSED)) { |
|
619 |
throw new IllegalStateException("Duration can't be set after a recording has been stopped/closed"); |
|
620 |
} |
|
621 |
setInternalDuration(duration); |
|
622 |
if (getState() != RecordingState.NEW) { |
|
623 |
updateTimer(); |
|
624 |
} |
|
625 |
} |
|
626 |
} |
|
627 |
||
628 |
void updateTimer() { |
|
629 |
if (stopTask != null) { |
|
630 |
stopTask.cancel(); |
|
631 |
stopTask = null; |
|
632 |
} |
|
633 |
if (getState() == RecordingState.CLOSED) { |
|
634 |
return; |
|
635 |
} |
|
636 |
if (duration != null) { |
|
637 |
stopTask = createStopTask(); |
|
638 |
recorder.getTimer().schedule(stopTask, new Date(startTime.plus(duration).toEpochMilli())); |
|
639 |
} |
|
640 |
} |
|
641 |
||
642 |
TimerTask createStopTask() { |
|
643 |
return new TimerTask() { |
|
644 |
@Override |
|
645 |
public void run() { |
|
646 |
try { |
|
647 |
stop("End of duration reached"); |
|
648 |
} catch (Throwable t) { |
|
649 |
// Prevent malicious user to propagate exception callback in the wrong context |
|
50745 | 650 |
Logger.log(LogTag.JFR, LogLevel.ERROR, "Could not stop recording."); |
50113 | 651 |
} |
652 |
} |
|
653 |
}; |
|
654 |
} |
|
655 |
||
656 |
public Recording newCopy(boolean stop) { |
|
657 |
return recorder.newCopy(this, stop); |
|
658 |
} |
|
659 |
||
660 |
void setStopTask(TimerTask stopTask) { |
|
661 |
synchronized (recorder) { |
|
662 |
this.stopTask = stopTask; |
|
663 |
} |
|
664 |
} |
|
665 |
||
666 |
void clearDestination() { |
|
50745 | 667 |
destination = null; |
50113 | 668 |
} |
669 |
||
670 |
public AccessControlContext getNoDestinationDumpOnExitAccessControlContext() { |
|
671 |
return noDestinationDumpOnExitAccessControlContext; |
|
672 |
} |
|
673 |
||
674 |
void setShouldWriteActiveRecordingEvent(boolean shouldWrite) { |
|
50745 | 675 |
this.shuoldWriteActiveRecordingEvent = shouldWrite; |
50113 | 676 |
} |
677 |
||
678 |
boolean shouldWriteMetadataEvent() { |
|
679 |
return shuoldWriteActiveRecordingEvent; |
|
680 |
} |
|
50745 | 681 |
|
682 |
// Dump running and stopped recordings |
|
683 |
public void dump(WriteableUserPath writeableUserPath) throws IOException { |
|
684 |
synchronized (recorder) { |
|
685 |
try(PlatformRecording p = newSnapshotClone("Dumped by user", null)) { |
|
686 |
p.dumpStopped(writeableUserPath); |
|
687 |
} |
|
688 |
} |
|
689 |
} |
|
690 |
||
691 |
public void dumpStopped(WriteableUserPath userPath) throws IOException { |
|
692 |
synchronized (recorder) { |
|
693 |
userPath.doPriviligedIO(() -> { |
|
694 |
try (ChunksChannel cc = new ChunksChannel(chunks); FileChannel fc = FileChannel.open(userPath.getReal(), StandardOpenOption.WRITE, StandardOpenOption.APPEND)) { |
|
695 |
cc.transferTo(fc); |
|
696 |
fc.force(true); |
|
697 |
} |
|
698 |
return null; |
|
699 |
}); |
|
700 |
} |
|
701 |
} |
|
702 |
||
703 |
public void filter(Instant begin, Instant end, Long maxSize) { |
|
704 |
synchronized (recorder) { |
|
705 |
List<RepositoryChunk> result = removeAfter(end, removeBefore(begin, new ArrayList<>(chunks))); |
|
706 |
if (maxSize != null) { |
|
707 |
if (begin != null && end == null) { |
|
708 |
result = reduceFromBeginning(maxSize, result); |
|
709 |
} else { |
|
710 |
result = reduceFromEnd(maxSize, result); |
|
711 |
} |
|
712 |
} |
|
713 |
int size = 0; |
|
714 |
for (RepositoryChunk r : result) { |
|
715 |
size += r.getSize(); |
|
716 |
r.use(); |
|
717 |
} |
|
718 |
this.size = size; |
|
719 |
for (RepositoryChunk r : chunks) { |
|
720 |
r.release(); |
|
721 |
} |
|
722 |
chunks.clear(); |
|
723 |
chunks.addAll(result); |
|
724 |
} |
|
725 |
} |
|
726 |
||
727 |
private static List<RepositoryChunk> removeBefore(Instant time, List<RepositoryChunk> input) { |
|
728 |
if (time == null) { |
|
729 |
return input; |
|
730 |
} |
|
731 |
List<RepositoryChunk> result = new ArrayList<>(input.size()); |
|
732 |
for (RepositoryChunk r : input) { |
|
733 |
if (!r.getEndTime().isBefore(time)) { |
|
734 |
result.add(r); |
|
735 |
} |
|
736 |
} |
|
737 |
return result; |
|
738 |
} |
|
739 |
||
740 |
private static List<RepositoryChunk> removeAfter(Instant time, List<RepositoryChunk> input) { |
|
741 |
if (time == null) { |
|
742 |
return input; |
|
743 |
} |
|
744 |
List<RepositoryChunk> result = new ArrayList<>(input.size()); |
|
745 |
for (RepositoryChunk r : input) { |
|
746 |
if (!r.getStartTime().isAfter(time)) { |
|
747 |
result.add(r); |
|
748 |
} |
|
749 |
} |
|
750 |
return result; |
|
751 |
} |
|
752 |
||
753 |
private static List<RepositoryChunk> reduceFromBeginning(Long maxSize, List<RepositoryChunk> input) { |
|
754 |
if (maxSize == null || input.isEmpty()) { |
|
755 |
return input; |
|
756 |
} |
|
757 |
List<RepositoryChunk> result = new ArrayList<>(input.size()); |
|
758 |
long total = 0; |
|
759 |
for (RepositoryChunk r : input) { |
|
760 |
total += r.getSize(); |
|
761 |
if (total > maxSize) { |
|
762 |
break; |
|
763 |
} |
|
764 |
result.add(r); |
|
765 |
} |
|
766 |
// always keep at least one chunk |
|
767 |
if (result.isEmpty()) { |
|
768 |
result.add(input.get(0)); |
|
769 |
} |
|
770 |
return result; |
|
771 |
} |
|
772 |
||
773 |
private static List<RepositoryChunk> reduceFromEnd(Long maxSize, List<RepositoryChunk> input) { |
|
774 |
Collections.reverse(input); |
|
775 |
List<RepositoryChunk> result = reduceFromBeginning(maxSize, input); |
|
776 |
Collections.reverse(result); |
|
777 |
return result; |
|
778 |
} |
|
779 |
||
780 |
public void setDumpOnExitDirectory(SafePath directory) { |
|
781 |
this.dumpOnExitDirectory = directory; |
|
782 |
} |
|
783 |
||
784 |
public SafePath getDumpOnExitDirectory() { |
|
785 |
return this.dumpOnExitDirectory; |
|
786 |
} |
|
58863 | 787 |
|
788 |
public void setFlushInterval(Duration interval) { |
|
789 |
synchronized (recorder) { |
|
790 |
if (getState() == RecordingState.CLOSED) { |
|
791 |
throw new IllegalStateException("Can't set stream interval when recording is closed"); |
|
792 |
} |
|
793 |
this.flushInterval = interval; |
|
794 |
} |
|
795 |
} |
|
796 |
||
797 |
public Duration getFlushInterval() { |
|
798 |
synchronized (recorder) { |
|
799 |
return flushInterval; |
|
800 |
} |
|
801 |
} |
|
802 |
||
803 |
public long getStreamIntervalMillis() { |
|
804 |
synchronized (recorder) { |
|
805 |
if (flushInterval != null) { |
|
806 |
return flushInterval.toMillis(); |
|
807 |
} |
|
808 |
return Long.MAX_VALUE; |
|
809 |
} |
|
810 |
} |
|
50113 | 811 |
} |