author | egahlin |
Fri, 17 May 2019 16:02:27 +0200 | |
branch | JEP-349-branch |
changeset 57360 | 5d043a159d5c |
parent 53014 | 339d2fbe8675 |
child 57470 | 025c9b8eaefd |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
57360 | 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.INFO; |
|
29 |
import static jdk.jfr.internal.LogLevel.TRACE; |
|
30 |
import static jdk.jfr.internal.LogLevel.WARN; |
|
31 |
import static jdk.jfr.internal.LogTag.JFR; |
|
32 |
import static jdk.jfr.internal.LogTag.JFR_SYSTEM; |
|
33 |
||
34 |
import java.security.AccessControlContext; |
|
35 |
import java.security.AccessController; |
|
36 |
import java.time.Duration; |
|
37 |
import java.time.Instant; |
|
38 |
import java.util.ArrayList; |
|
39 |
import java.util.Collections; |
|
50745 | 40 |
import java.util.HashMap; |
50113 | 41 |
import java.util.HashSet; |
42 |
import java.util.List; |
|
43 |
import java.util.Map; |
|
44 |
import java.util.Set; |
|
45 |
import java.util.Timer; |
|
46 |
import java.util.TimerTask; |
|
47 |
import java.util.concurrent.CopyOnWriteArrayList; |
|
48 |
||
49 |
import jdk.jfr.EventType; |
|
50 |
import jdk.jfr.FlightRecorder; |
|
51 |
import jdk.jfr.FlightRecorderListener; |
|
52 |
import jdk.jfr.Recording; |
|
53 |
import jdk.jfr.RecordingState; |
|
54 |
import jdk.jfr.events.ActiveRecordingEvent; |
|
55 |
import jdk.jfr.events.ActiveSettingEvent; |
|
56 |
import jdk.jfr.internal.SecuritySupport.SecureRecorderListener; |
|
57 |
import jdk.jfr.internal.instrument.JDKEvents; |
|
58 |
||
59 |
public final class PlatformRecorder { |
|
60 |
||
61 |
private final List<PlatformRecording> recordings = new ArrayList<>(); |
|
62 |
private final static List<SecureRecorderListener> changeListeners = new ArrayList<>(); |
|
63 |
private final Repository repository; |
|
64 |
private final Timer timer; |
|
65 |
private final static JVM jvm = JVM.getJVM(); |
|
66 |
private final EventType activeRecordingEvent; |
|
67 |
private final EventType activeSettingEvent; |
|
68 |
private final Thread shutdownHook; |
|
69 |
||
70 |
private long recordingCounter = 0; |
|
71 |
private RepositoryChunk currentChunk; |
|
72 |
||
73 |
public PlatformRecorder() throws Exception { |
|
74 |
repository = Repository.getRepository(); |
|
75 |
Logger.log(JFR_SYSTEM, INFO, "Initialized disk repository"); |
|
76 |
repository.ensureRepository(); |
|
77 |
jvm.createNativeJFR(); |
|
78 |
Logger.log(JFR_SYSTEM, INFO, "Created native"); |
|
79 |
JDKEvents.initialize(); |
|
80 |
Logger.log(JFR_SYSTEM, INFO, "Registered JDK events"); |
|
81 |
JDKEvents.addInstrumentation(); |
|
82 |
startDiskMonitor(); |
|
83 |
activeRecordingEvent = EventType.getEventType(ActiveRecordingEvent.class); |
|
84 |
activeSettingEvent = EventType.getEventType(ActiveSettingEvent.class); |
|
85 |
shutdownHook = SecuritySupport.createThreadWitNoPermissions("JFR: Shutdown Hook", new ShutdownHook(this)); |
|
86 |
SecuritySupport.setUncaughtExceptionHandler(shutdownHook, new ShutdownHook.ExceptionHandler()); |
|
87 |
SecuritySupport.registerShutdownHook(shutdownHook); |
|
88 |
timer = createTimer(); |
|
89 |
} |
|
90 |
||
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50745
diff
changeset
|
91 |
|
50113 | 92 |
private static Timer createTimer() { |
93 |
try { |
|
94 |
List<Timer> result = new CopyOnWriteArrayList<>(); |
|
95 |
Thread t = SecuritySupport.createThreadWitNoPermissions("Permissionless thread", ()-> { |
|
96 |
result.add(new Timer("JFR Recording Scheduler", true)); |
|
97 |
}); |
|
57360 | 98 |
jvm.exclude(t); |
50113 | 99 |
t.start(); |
100 |
t.join(); |
|
101 |
return result.get(0); |
|
102 |
} catch (InterruptedException e) { |
|
103 |
throw new IllegalStateException("Not able to create timer task. " + e.getMessage(), e); |
|
104 |
} |
|
105 |
} |
|
106 |
||
107 |
public synchronized PlatformRecording newRecording(Map<String, String> settings) { |
|
50197
f4735ff8d17d
8195818: JFR.start should increase autogenerated name by one
egahlin
parents:
50194
diff
changeset
|
108 |
return newRecording(settings, ++recordingCounter); |
f4735ff8d17d
8195818: JFR.start should increase autogenerated name by one
egahlin
parents:
50194
diff
changeset
|
109 |
} |
f4735ff8d17d
8195818: JFR.start should increase autogenerated name by one
egahlin
parents:
50194
diff
changeset
|
110 |
|
50745 | 111 |
// To be used internally when doing dumps. |
112 |
// Caller must have recorder lock and close recording before releasing lock |
|
113 |
public PlatformRecording newTemporaryRecording() { |
|
114 |
if(!Thread.holdsLock(this)) { |
|
115 |
throw new InternalError("Caller must have recorder lock"); |
|
116 |
} |
|
117 |
return newRecording(new HashMap<>(), 0); |
|
118 |
} |
|
119 |
||
120 |
private synchronized PlatformRecording newRecording(Map<String, String> settings, long id) { |
|
50197
f4735ff8d17d
8195818: JFR.start should increase autogenerated name by one
egahlin
parents:
50194
diff
changeset
|
121 |
PlatformRecording recording = new PlatformRecording(this, id); |
50113 | 122 |
if (!settings.isEmpty()) { |
123 |
recording.setSettings(settings); |
|
124 |
} |
|
125 |
recordings.add(recording); |
|
126 |
return recording; |
|
127 |
} |
|
128 |
||
129 |
synchronized void finish(PlatformRecording recording) { |
|
130 |
if (recording.getState() == RecordingState.RUNNING) { |
|
131 |
recording.stop("Recording closed"); |
|
132 |
} |
|
133 |
recordings.remove(recording); |
|
134 |
} |
|
135 |
||
136 |
public synchronized List<PlatformRecording> getRecordings() { |
|
137 |
return Collections.unmodifiableList(new ArrayList<PlatformRecording>(recordings)); |
|
138 |
} |
|
139 |
||
140 |
public synchronized static void addListener(FlightRecorderListener changeListener) { |
|
141 |
AccessControlContext context = AccessController.getContext(); |
|
142 |
SecureRecorderListener sl = new SecureRecorderListener(context, changeListener); |
|
143 |
boolean runInitialized; |
|
144 |
synchronized (PlatformRecorder.class) { |
|
145 |
runInitialized = FlightRecorder.isInitialized(); |
|
146 |
changeListeners.add(sl); |
|
147 |
} |
|
148 |
if (runInitialized) { |
|
149 |
sl.recorderInitialized(FlightRecorder.getFlightRecorder()); |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
public synchronized static boolean removeListener(FlightRecorderListener changeListener) { |
|
154 |
for (SecureRecorderListener s : new ArrayList<>(changeListeners)) { |
|
155 |
if (s.getChangeListener() == changeListener) { |
|
156 |
changeListeners.remove(s); |
|
157 |
return true; |
|
158 |
} |
|
159 |
} |
|
160 |
return false; |
|
161 |
} |
|
162 |
||
163 |
static synchronized List<FlightRecorderListener> getListeners() { |
|
164 |
return new ArrayList<>(changeListeners); |
|
165 |
} |
|
166 |
||
167 |
Timer getTimer() { |
|
168 |
return timer; |
|
169 |
} |
|
170 |
||
171 |
public static void notifyRecorderInitialized(FlightRecorder recorder) { |
|
172 |
Logger.log(JFR_SYSTEM, TRACE, "Notifying listeners that Flight Recorder is initialized"); |
|
173 |
for (FlightRecorderListener r : getListeners()) { |
|
174 |
r.recorderInitialized(recorder); |
|
175 |
} |
|
176 |
} |
|
177 |
||
178 |
// called by shutdown hook |
|
179 |
synchronized void destroy() { |
|
180 |
try { |
|
181 |
timer.cancel(); |
|
182 |
} catch (Exception ex) { |
|
183 |
Logger.log(JFR_SYSTEM, WARN, "Shutdown hook could not cancel timer"); |
|
184 |
} |
|
185 |
||
186 |
for (PlatformRecording p : getRecordings()) { |
|
187 |
if (p.getState() == RecordingState.RUNNING) { |
|
188 |
try { |
|
189 |
p.stop("Shutdown"); |
|
190 |
} catch (Exception ex) { |
|
191 |
Logger.log(JFR, WARN, "Recording " + p.getName() + ":" + p.getId() + " could not be stopped"); |
|
192 |
} |
|
193 |
} |
|
194 |
} |
|
195 |
||
196 |
JDKEvents.remove(); |
|
197 |
||
198 |
if (jvm.hasNativeJFR()) { |
|
199 |
if (jvm.isRecording()) { |
|
200 |
jvm.endRecording_(); |
|
201 |
} |
|
202 |
jvm.destroyNativeJFR(); |
|
203 |
} |
|
204 |
repository.clear(); |
|
205 |
} |
|
206 |
||
57360 | 207 |
synchronized long start(PlatformRecording recording) { |
50113 | 208 |
// State can only be NEW or DELAYED because of previous checks |
209 |
Instant now = Instant.now(); |
|
210 |
recording.setStartTime(now); |
|
211 |
recording.updateTimer(); |
|
212 |
Duration duration = recording.getDuration(); |
|
213 |
if (duration != null) { |
|
214 |
recording.setStopTime(now.plus(duration)); |
|
215 |
} |
|
216 |
boolean toDisk = recording.isToDisk(); |
|
217 |
boolean beginPhysical = true; |
|
57360 | 218 |
long streamInterval = recording.getStreamIntervalMillis(); |
50113 | 219 |
for (PlatformRecording s : getRecordings()) { |
220 |
if (s.getState() == RecordingState.RUNNING) { |
|
221 |
beginPhysical = false; |
|
222 |
if (s.isToDisk()) { |
|
223 |
toDisk = true; |
|
224 |
} |
|
57360 | 225 |
streamInterval = Math.min(streamInterval, s.getStreamIntervalMillis()); |
50113 | 226 |
} |
227 |
} |
|
57360 | 228 |
long startNanos = -1; |
50113 | 229 |
if (beginPhysical) { |
230 |
RepositoryChunk newChunk = null; |
|
231 |
if (toDisk) { |
|
232 |
newChunk = repository.newChunk(now); |
|
233 |
MetadataRepository.getInstance().setOutput(newChunk.getUnfishedFile().toString()); |
|
234 |
} else { |
|
235 |
MetadataRepository.getInstance().setOutput(null); |
|
236 |
} |
|
237 |
currentChunk = newChunk; |
|
57360 | 238 |
jvm.beginRecording_(); |
239 |
startNanos = jvm.getChunkStartNanos(); |
|
50113 | 240 |
recording.setState(RecordingState.RUNNING); |
241 |
updateSettings(); |
|
242 |
writeMetaEvents(); |
|
243 |
} else { |
|
244 |
RepositoryChunk newChunk = null; |
|
245 |
if (toDisk) { |
|
246 |
newChunk = repository.newChunk(now); |
|
247 |
RequestEngine.doChunkEnd(); |
|
57360 | 248 |
MetadataRepository.getInstance().setOutput(newChunk.getUnfishedFile().toString()); |
249 |
startNanos = jvm.getChunkStartNanos(); |
|
50113 | 250 |
} |
251 |
recording.setState(RecordingState.RUNNING); |
|
252 |
updateSettings(); |
|
253 |
writeMetaEvents(); |
|
254 |
if (currentChunk != null) { |
|
255 |
finishChunk(currentChunk, now, recording); |
|
256 |
} |
|
257 |
currentChunk = newChunk; |
|
258 |
} |
|
57360 | 259 |
if (toDisk) { |
260 |
RequestEngine.setFlushInterval(streamInterval); |
|
261 |
} |
|
262 |
RequestEngine.doChunkBegin(); |
|
50113 | 263 |
|
57360 | 264 |
return startNanos; |
50113 | 265 |
} |
266 |
||
50745 | 267 |
synchronized void stop(PlatformRecording recording) { |
50113 | 268 |
RecordingState state = recording.getState(); |
269 |
||
270 |
if (Utils.isAfter(state, RecordingState.RUNNING)) { |
|
271 |
throw new IllegalStateException("Can't stop an already stopped recording."); |
|
272 |
} |
|
273 |
if (Utils.isBefore(state, RecordingState.RUNNING)) { |
|
274 |
throw new IllegalStateException("Recording must be started before it can be stopped."); |
|
275 |
} |
|
276 |
Instant now = Instant.now(); |
|
277 |
boolean toDisk = false; |
|
278 |
boolean endPhysical = true; |
|
57360 | 279 |
long streamInterval = Long.MAX_VALUE; |
50113 | 280 |
for (PlatformRecording s : getRecordings()) { |
281 |
RecordingState rs = s.getState(); |
|
282 |
if (s != recording && RecordingState.RUNNING == rs) { |
|
283 |
endPhysical = false; |
|
284 |
if (s.isToDisk()) { |
|
285 |
toDisk = true; |
|
286 |
} |
|
57360 | 287 |
streamInterval = Math.min(streamInterval, s.getStreamIntervalMillis()); |
50113 | 288 |
} |
289 |
} |
|
50745 | 290 |
OldObjectSample.emit(recording); |
50113 | 291 |
|
292 |
if (endPhysical) { |
|
293 |
RequestEngine.doChunkEnd(); |
|
294 |
if (recording.isToDisk()) { |
|
295 |
if (currentChunk != null) { |
|
296 |
MetadataRepository.getInstance().setOutput(null); |
|
297 |
finishChunk(currentChunk, now, null); |
|
298 |
currentChunk = null; |
|
299 |
} |
|
300 |
} else { |
|
301 |
// last memory |
|
50745 | 302 |
dumpMemoryToDestination(recording); |
50113 | 303 |
} |
304 |
jvm.endRecording_(); |
|
305 |
disableEvents(); |
|
306 |
} else { |
|
307 |
RepositoryChunk newChunk = null; |
|
308 |
RequestEngine.doChunkEnd(); |
|
309 |
updateSettingsButIgnoreRecording(recording); |
|
310 |
if (toDisk) { |
|
311 |
newChunk = repository.newChunk(now); |
|
312 |
MetadataRepository.getInstance().setOutput(newChunk.getUnfishedFile().toString()); |
|
313 |
} else { |
|
314 |
MetadataRepository.getInstance().setOutput(null); |
|
315 |
} |
|
316 |
writeMetaEvents(); |
|
317 |
if (currentChunk != null) { |
|
318 |
finishChunk(currentChunk, now, null); |
|
319 |
} |
|
320 |
currentChunk = newChunk; |
|
321 |
RequestEngine.doChunkBegin(); |
|
322 |
} |
|
57360 | 323 |
|
324 |
if (toDisk) { |
|
325 |
RequestEngine.setFlushInterval(streamInterval); |
|
326 |
} else { |
|
327 |
RequestEngine.setFlushInterval(Long.MAX_VALUE); |
|
328 |
} |
|
329 |
||
50113 | 330 |
recording.setState(RecordingState.STOPPED); |
331 |
} |
|
332 |
||
50745 | 333 |
private void dumpMemoryToDestination(PlatformRecording recording) { |
334 |
WriteableUserPath dest = recording.getDestination(); |
|
50113 | 335 |
if (dest != null) { |
336 |
MetadataRepository.getInstance().setOutput(dest.getText()); |
|
337 |
recording.clearDestination(); |
|
338 |
} |
|
339 |
} |
|
340 |
private void disableEvents() { |
|
341 |
MetadataRepository.getInstance().disableEvents(); |
|
342 |
} |
|
343 |
||
344 |
void updateSettings() { |
|
345 |
updateSettingsButIgnoreRecording(null); |
|
346 |
} |
|
347 |
||
348 |
void updateSettingsButIgnoreRecording(PlatformRecording ignoreMe) { |
|
349 |
List<PlatformRecording> recordings = getRunningRecordings(); |
|
350 |
List<Map<String, String>> list = new ArrayList<>(recordings.size()); |
|
351 |
for (PlatformRecording r : recordings) { |
|
352 |
if (r != ignoreMe) { |
|
353 |
list.add(r.getSettings()); |
|
354 |
} |
|
355 |
} |
|
356 |
MetadataRepository.getInstance().setSettings(list); |
|
357 |
} |
|
358 |
||
359 |
synchronized void rotateDisk() { |
|
360 |
Instant now = Instant.now(); |
|
361 |
RepositoryChunk newChunk = repository.newChunk(now); |
|
362 |
RequestEngine.doChunkEnd(); |
|
363 |
MetadataRepository.getInstance().setOutput(newChunk.getUnfishedFile().toString()); |
|
364 |
writeMetaEvents(); |
|
365 |
if (currentChunk != null) { |
|
366 |
finishChunk(currentChunk, now, null); |
|
367 |
} |
|
368 |
currentChunk = newChunk; |
|
369 |
RequestEngine.doChunkBegin(); |
|
370 |
} |
|
371 |
||
372 |
private List<PlatformRecording> getRunningRecordings() { |
|
373 |
List<PlatformRecording> runningRecordings = new ArrayList<>(); |
|
374 |
for (PlatformRecording recording : getRecordings()) { |
|
375 |
if (recording.getState() == RecordingState.RUNNING) { |
|
376 |
runningRecordings.add(recording); |
|
377 |
} |
|
378 |
} |
|
379 |
return runningRecordings; |
|
380 |
} |
|
381 |
||
382 |
private List<RepositoryChunk> makeChunkList(Instant startTime, Instant endTime) { |
|
383 |
Set<RepositoryChunk> chunkSet = new HashSet<>(); |
|
384 |
for (PlatformRecording r : getRecordings()) { |
|
385 |
chunkSet.addAll(r.getChunks()); |
|
386 |
} |
|
387 |
if (chunkSet.size() > 0) { |
|
388 |
List<RepositoryChunk> chunks = new ArrayList<>(chunkSet.size()); |
|
389 |
for (RepositoryChunk rc : chunkSet) { |
|
390 |
if (rc.inInterval(startTime, endTime)) { |
|
391 |
chunks.add(rc); |
|
392 |
} |
|
393 |
} |
|
394 |
// n*log(n), should be able to do n*log(k) with a priority queue, |
|
395 |
// where k = number of recordings, n = number of chunks |
|
396 |
Collections.sort(chunks, RepositoryChunk.END_TIME_COMPARATOR); |
|
397 |
return chunks; |
|
398 |
} |
|
399 |
||
400 |
return Collections.emptyList(); |
|
401 |
} |
|
402 |
||
403 |
private void startDiskMonitor() { |
|
404 |
Thread t = SecuritySupport.createThreadWitNoPermissions("JFR Periodic Tasks", () -> periodicTask()); |
|
405 |
SecuritySupport.setDaemonThread(t, true); |
|
406 |
t.start(); |
|
407 |
} |
|
408 |
||
409 |
private void finishChunk(RepositoryChunk chunk, Instant time, PlatformRecording ignoreMe) { |
|
410 |
chunk.finish(time); |
|
411 |
for (PlatformRecording r : getRecordings()) { |
|
412 |
if (r != ignoreMe && r.getState() == RecordingState.RUNNING) { |
|
413 |
r.appendChunk(chunk); |
|
414 |
} |
|
415 |
} |
|
57360 | 416 |
FilePurger.purge(); |
50113 | 417 |
} |
418 |
||
419 |
private void writeMetaEvents() { |
|
420 |
||
421 |
if (activeRecordingEvent.isEnabled()) { |
|
422 |
for (PlatformRecording r : getRecordings()) { |
|
423 |
if (r.getState() == RecordingState.RUNNING && r.shouldWriteMetadataEvent()) { |
|
424 |
ActiveRecordingEvent event = new ActiveRecordingEvent(); |
|
425 |
event.id = r.getId(); |
|
426 |
event.name = r.getName(); |
|
427 |
WriteableUserPath p = r.getDestination(); |
|
428 |
event.destination = p == null ? null : p.getText(); |
|
429 |
Duration d = r.getDuration(); |
|
430 |
event.recordingDuration = d == null ? Long.MAX_VALUE : d.toMillis(); |
|
431 |
Duration age = r.getMaxAge(); |
|
432 |
event.maxAge = age == null ? Long.MAX_VALUE : age.toMillis(); |
|
433 |
Long size = r.getMaxSize(); |
|
434 |
event.maxSize = size == null ? Long.MAX_VALUE : size; |
|
435 |
Instant start = r.getStartTime(); |
|
436 |
event.recordingStart = start == null ? Long.MAX_VALUE : start.toEpochMilli(); |
|
57360 | 437 |
Duration fi = r.getFlushInterval(); |
438 |
event.flushInterval = fi == null ? Long.MAX_VALUE : fi.toMillis(); |
|
50113 | 439 |
event.commit(); |
440 |
} |
|
441 |
} |
|
442 |
} |
|
443 |
if (activeSettingEvent.isEnabled()) { |
|
444 |
for (EventControl ec : MetadataRepository.getInstance().getEventControls()) { |
|
445 |
ec.writeActiveSettingEvent(); |
|
446 |
} |
|
447 |
} |
|
448 |
} |
|
449 |
||
450 |
private void periodicTask() { |
|
53014
339d2fbe8675
8215284: Reduce noise induced by periodic task getFileSize()
mgronlun
parents:
52334
diff
changeset
|
451 |
if (!jvm.hasNativeJFR()) { |
339d2fbe8675
8215284: Reduce noise induced by periodic task getFileSize()
mgronlun
parents:
52334
diff
changeset
|
452 |
return; |
339d2fbe8675
8215284: Reduce noise induced by periodic task getFileSize()
mgronlun
parents:
52334
diff
changeset
|
453 |
} |
50113 | 454 |
while (true) { |
455 |
synchronized (this) { |
|
53014
339d2fbe8675
8215284: Reduce noise induced by periodic task getFileSize()
mgronlun
parents:
52334
diff
changeset
|
456 |
if (jvm.shouldRotateDisk()) { |
339d2fbe8675
8215284: Reduce noise induced by periodic task getFileSize()
mgronlun
parents:
52334
diff
changeset
|
457 |
rotateDisk(); |
50113 | 458 |
} |
459 |
} |
|
460 |
long minDelta = RequestEngine.doPeriodic(); |
|
461 |
long wait = Math.min(minDelta, Options.getWaitInterval()); |
|
462 |
takeNap(wait); |
|
463 |
} |
|
464 |
} |
|
465 |
||
466 |
private void takeNap(long duration) { |
|
467 |
try { |
|
468 |
synchronized (JVM.FILE_DELTA_CHANGE) { |
|
469 |
JVM.FILE_DELTA_CHANGE.wait(duration < 10 ? 10 : duration); |
|
470 |
} |
|
471 |
} catch (InterruptedException e) { |
|
57360 | 472 |
// Ignore |
50113 | 473 |
} |
474 |
} |
|
475 |
||
476 |
synchronized Recording newCopy(PlatformRecording r, boolean stop) { |
|
477 |
Recording newRec = new Recording(); |
|
478 |
PlatformRecording copy = PrivateAccess.getInstance().getPlatformRecording(newRec); |
|
479 |
copy.setSettings(r.getSettings()); |
|
480 |
copy.setMaxAge(r.getMaxAge()); |
|
481 |
copy.setMaxSize(r.getMaxSize()); |
|
482 |
copy.setDumpOnExit(r.getDumpOnExit()); |
|
483 |
copy.setName("Clone of " + r.getName()); |
|
484 |
copy.setToDisk(r.isToDisk()); |
|
485 |
copy.setInternalDuration(r.getDuration()); |
|
486 |
copy.setStartTime(r.getStartTime()); |
|
487 |
copy.setStopTime(r.getStopTime()); |
|
488 |
||
489 |
if (r.getState() == RecordingState.NEW) { |
|
490 |
return newRec; |
|
491 |
} |
|
492 |
if (r.getState() == RecordingState.DELAYED) { |
|
493 |
copy.scheduleStart(r.getStartTime()); |
|
494 |
return newRec; |
|
495 |
} |
|
496 |
copy.setState(r.getState()); |
|
497 |
// recording has started, copy chunks |
|
498 |
for (RepositoryChunk c : r.getChunks()) { |
|
499 |
copy.add(c); |
|
500 |
} |
|
501 |
if (r.getState() == RecordingState.RUNNING) { |
|
502 |
if (stop) { |
|
503 |
copy.stop("Stopped when cloning recording '" + r.getName() + "'"); |
|
504 |
} else { |
|
505 |
if (r.getStopTime() != null) { |
|
506 |
TimerTask stopTask = copy.createStopTask(); |
|
507 |
copy.setStopTask(copy.createStopTask()); |
|
508 |
getTimer().schedule(stopTask, r.getStopTime().toEpochMilli()); |
|
509 |
} |
|
510 |
} |
|
511 |
} |
|
512 |
return newRec; |
|
513 |
} |
|
514 |
||
50745 | 515 |
public synchronized void fillWithRecordedData(PlatformRecording target, Boolean pathToGcRoots) { |
50113 | 516 |
boolean running = false; |
517 |
boolean toDisk = false; |
|
50745 | 518 |
|
50113 | 519 |
for (PlatformRecording r : recordings) { |
520 |
if (r.getState() == RecordingState.RUNNING) { |
|
521 |
running = true; |
|
50745 | 522 |
if (r.isToDisk()) { |
523 |
toDisk = true; |
|
524 |
} |
|
50113 | 525 |
} |
526 |
} |
|
527 |
// If needed, flush data from memory |
|
528 |
if (running) { |
|
529 |
if (toDisk) { |
|
50745 | 530 |
OldObjectSample.emit(recordings, pathToGcRoots); |
50113 | 531 |
rotateDisk(); |
532 |
} else { |
|
50745 | 533 |
try (PlatformRecording snapshot = newTemporaryRecording()) { |
534 |
snapshot.setToDisk(true); |
|
535 |
snapshot.setShouldWriteActiveRecordingEvent(false); |
|
50113 | 536 |
snapshot.start(); |
50745 | 537 |
OldObjectSample.emit(recordings, pathToGcRoots); |
538 |
snapshot.stop("Snapshot dump"); |
|
539 |
fillWithDiskChunks(target); |
|
50113 | 540 |
} |
50745 | 541 |
return; |
50113 | 542 |
} |
543 |
} |
|
50745 | 544 |
fillWithDiskChunks(target); |
50113 | 545 |
} |
546 |
||
50745 | 547 |
private void fillWithDiskChunks(PlatformRecording target) { |
50113 | 548 |
for (RepositoryChunk c : makeChunkList(null, null)) { |
50745 | 549 |
target.add(c); |
50113 | 550 |
} |
50745 | 551 |
target.setState(RecordingState.STOPPED); |
50113 | 552 |
Instant startTime = null; |
553 |
Instant endTime = null; |
|
554 |
||
50745 | 555 |
for (RepositoryChunk c : target.getChunks()) { |
50113 | 556 |
if (startTime == null || c.getStartTime().isBefore(startTime)) { |
557 |
startTime = c.getStartTime(); |
|
558 |
} |
|
559 |
if (endTime == null || c.getEndTime().isAfter(endTime)) { |
|
560 |
endTime = c.getEndTime(); |
|
561 |
} |
|
562 |
} |
|
563 |
Instant now = Instant.now(); |
|
564 |
if (startTime == null) { |
|
565 |
startTime = now; |
|
566 |
} |
|
567 |
if (endTime == null) { |
|
568 |
endTime = now; |
|
569 |
} |
|
50745 | 570 |
target.setStartTime(startTime); |
571 |
target.setStopTime(endTime); |
|
572 |
target.setInternalDuration(Duration.between(startTime, endTime)); |
|
50113 | 573 |
} |
574 |
} |