1 /*
2  * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
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 java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 
32 import jdk.jfr.Enabled;
33 import jdk.jfr.RecordingState;
34 import jdk.jfr.internal.settings.CutoffSetting;
35 import jdk.jfr.internal.test.WhiteBox;
36 
37 // The Old Object event could have been implemented as a periodic event, but
38 // due to chunk rotations and how settings are calculated when multiple recordings
39 // are running at the same time, it would lead to unacceptable overhead.
40 //
41 // Instead, the event is only emitted before a recording stops and
42 // if that recording has the event enabled.
43 //
44 // This requires special handling and the purpose of this class is to provide that
45 //
46 public final class OldObjectSample {
47 
48     private static final String EVENT_NAME = Type.EVENT_NAME_PREFIX + "OldObjectSample";
49     private static final String OLD_OBJECT_CUTOFF = EVENT_NAME + "#" + Cutoff.NAME;
50     private static final String OLD_OBJECT_ENABLED = EVENT_NAME + "#" + Enabled.NAME;
51 
52     // Emit if old object is enabled in recording with cutoff for that recording
emit(PlatformRecording recording)53     public static void emit(PlatformRecording recording) {
54         if (isEnabled(recording)) {
55             long nanos = CutoffSetting.parseValueSafe(recording.getSettings().get(OLD_OBJECT_CUTOFF));
56             long ticks = Utils.nanosToTicks(nanos);
57             emit(ticks);
58         }
59     }
60 
61 
62     // Emit if old object is enabled for at least one recording, and use the largest
63     // cutoff for an enabled recording
emit(List<PlatformRecording> recordings, Boolean pathToGcRoots)64     public static void emit(List<PlatformRecording> recordings, Boolean pathToGcRoots) {
65         boolean enabled = false;
66         long cutoffNanos = Boolean.TRUE.equals(pathToGcRoots) ? Long.MAX_VALUE : 0L;
67         for (PlatformRecording r : recordings) {
68             if (r.getState() == RecordingState.RUNNING) {
69                 if (isEnabled(r)) {
70                     enabled = true;
71                     long c = CutoffSetting.parseValueSafe(r.getSettings().get(OLD_OBJECT_CUTOFF));
72                     cutoffNanos = Math.max(c, cutoffNanos);
73                 }
74             }
75         }
76         if (enabled) {
77             long ticks = Utils.nanosToTicks(cutoffNanos);
78             emit(ticks);
79         }
80     }
81 
emit(long ticks)82     private static void emit(long ticks) {
83         boolean emitAll = WhiteBox.getWriteAllObjectSamples();
84         boolean skipBFS = WhiteBox.getSkipBFS();
85         JVM.getJVM().emitOldObjectSamples(ticks, emitAll, skipBFS);
86     }
87 
updateSettingPathToGcRoots(Map<String, String> s, Boolean pathToGcRoots)88     public static void updateSettingPathToGcRoots(Map<String, String> s, Boolean pathToGcRoots) {
89         if (pathToGcRoots != null) {
90             s.put(OLD_OBJECT_CUTOFF, pathToGcRoots ? "infinity" : "0 ns");
91         }
92     }
93 
createSettingsForSnapshot(PlatformRecording recording, Boolean pathToGcRoots)94     public static Map<String, String> createSettingsForSnapshot(PlatformRecording recording, Boolean pathToGcRoots) {
95         Map<String, String> settings = new HashMap<>(recording.getSettings());
96         updateSettingPathToGcRoots(settings, pathToGcRoots);
97         return settings;
98     }
99 
isEnabled(PlatformRecording r)100     private static boolean isEnabled(PlatformRecording r) {
101         Map<String, String> settings = r.getSettings();
102         String s = settings.get(OLD_OBJECT_ENABLED);
103         return "true".equals(s);
104     }
105 }
106