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