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