50745
|
1 |
package jdk.jfr.internal;
|
|
2 |
|
|
3 |
import java.util.HashMap;
|
|
4 |
import java.util.List;
|
|
5 |
import java.util.Map;
|
|
6 |
|
|
7 |
import jdk.jfr.Enabled;
|
|
8 |
import jdk.jfr.RecordingState;
|
|
9 |
import jdk.jfr.internal.settings.CutoffSetting;
|
|
10 |
import jdk.jfr.internal.test.WhiteBox;
|
|
11 |
|
|
12 |
// The Old Object event could have been implemented as a periodic event, but
|
|
13 |
// due to chunk rotations and how settings are calculated when multiple recordings
|
|
14 |
// are running at the same time, it would lead to unacceptable overhead.
|
|
15 |
//
|
|
16 |
// Instead, the event is only emitted before a recording stops and
|
|
17 |
// if that recording has the event enabled.
|
|
18 |
//
|
|
19 |
// This requires special handling and the purpose of this class is to provide that
|
|
20 |
//
|
|
21 |
public final class OldObjectSample {
|
|
22 |
|
|
23 |
private static final String EVENT_NAME = Type.EVENT_NAME_PREFIX + "OldObjectSample";
|
|
24 |
private static final String OLD_OBJECT_CUTOFF = EVENT_NAME + "#" + Cutoff.NAME;
|
|
25 |
private static final String OLD_OBJECT_ENABLED = EVENT_NAME + "#" + Enabled.NAME;
|
|
26 |
|
|
27 |
// Emit if old object is enabled in recoding with cutoff for that recording
|
|
28 |
public static void emit(PlatformRecording recording) {
|
|
29 |
if (isEnabled(recording)) {
|
|
30 |
long nanos = CutoffSetting.parseValueSafe(recording.getSettings().get(OLD_OBJECT_CUTOFF));
|
|
31 |
long ticks = Utils.nanosToTicks(nanos);
|
|
32 |
JVM.getJVM().emitOldObjectSamples(ticks, WhiteBox.getWriteAllObjectSamples());
|
|
33 |
}
|
|
34 |
}
|
|
35 |
|
|
36 |
// Emit if old object is enabled for at least one recording, and use the largest
|
|
37 |
// cutoff for an enabled recoding
|
|
38 |
public static void emit(List<PlatformRecording> recordings, Boolean pathToGcRoots) {
|
|
39 |
boolean enabled = false;
|
|
40 |
long cutoffNanos = Boolean.TRUE.equals(pathToGcRoots) ? Long.MAX_VALUE : 0L;
|
|
41 |
for (PlatformRecording r : recordings) {
|
|
42 |
if (r.getState() == RecordingState.RUNNING) {
|
|
43 |
if (isEnabled(r)) {
|
|
44 |
enabled = true;
|
|
45 |
long c = CutoffSetting.parseValueSafe(r.getSettings().get(OLD_OBJECT_CUTOFF));
|
|
46 |
cutoffNanos = Math.max(c, cutoffNanos);
|
|
47 |
}
|
|
48 |
}
|
|
49 |
}
|
|
50 |
if (enabled) {
|
|
51 |
long ticks = Utils.nanosToTicks(cutoffNanos);
|
|
52 |
JVM.getJVM().emitOldObjectSamples(ticks, WhiteBox.getWriteAllObjectSamples());
|
|
53 |
}
|
|
54 |
}
|
|
55 |
|
|
56 |
public static void updateSettingPathToGcRoots(Map<String, String> s, Boolean pathToGcRoots) {
|
|
57 |
if (pathToGcRoots != null) {
|
|
58 |
s.put(OLD_OBJECT_CUTOFF, pathToGcRoots ? "infinity" : "0 ns");
|
|
59 |
}
|
|
60 |
}
|
|
61 |
|
|
62 |
public static Map<String, String> createSettingsForSnapshot(PlatformRecording recording, Boolean pathToGcRoots) {
|
|
63 |
Map<String, String> settings = new HashMap<>(recording.getSettings());
|
|
64 |
updateSettingPathToGcRoots(settings, pathToGcRoots);
|
|
65 |
return settings;
|
|
66 |
}
|
|
67 |
|
|
68 |
private static boolean isEnabled(PlatformRecording r) {
|
|
69 |
Map<String, String> settings = r.getSettings();
|
|
70 |
String s = settings.get(OLD_OBJECT_ENABLED);
|
|
71 |
return "true".equals(s);
|
|
72 |
}
|
|
73 |
}
|