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