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.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 recoding with cutoff for that 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             JVM.getJVM().emitOldObjectSamples(ticks, WhiteBox.getWriteAllObjectSamples());
  58         }
  59     }
  60 
  61     // Emit if old object is enabled for at least one recording, and use the largest
  62     // cutoff for an enabled recoding
  63     public static void emit(List<PlatformRecording> recordings, Boolean pathToGcRoots) {
  64         boolean enabled = false;
  65         long cutoffNanos = Boolean.TRUE.equals(pathToGcRoots) ? Long.MAX_VALUE : 0L;
  66         for (PlatformRecording r : recordings) {
  67             if (r.getState() == RecordingState.RUNNING) {
  68                 if (isEnabled(r)) {
  69                     enabled = true;
  70                     long c = CutoffSetting.parseValueSafe(r.getSettings().get(OLD_OBJECT_CUTOFF));
  71                     cutoffNanos = Math.max(c, cutoffNanos);
  72                 }
  73             }
  74         }
  75         if (enabled) {
  76             long ticks = Utils.nanosToTicks(cutoffNanos);
  77             JVM.getJVM().emitOldObjectSamples(ticks, WhiteBox.getWriteAllObjectSamples());
  78         }
  79     }
  80 
  81     public static void updateSettingPathToGcRoots(Map<String, String> s, Boolean pathToGcRoots) {
  82         if (pathToGcRoots != null) {
  83             s.put(OLD_OBJECT_CUTOFF, pathToGcRoots ? "infinity" : "0 ns");
  84         }
  85     }
  86 
  87     public static Map<String, String> createSettingsForSnapshot(PlatformRecording recording, Boolean pathToGcRoots) {
  88         Map<String, String> settings = new HashMap<>(recording.getSettings());
  89         updateSettingPathToGcRoots(settings, pathToGcRoots);
  90         return settings;
  91     }
  92 
  93     private static boolean isEnabled(PlatformRecording r) {
  94         Map<String, String> settings = r.getSettings();
  95         String s = settings.get(OLD_OBJECT_ENABLED);
  96         return "true".equals(s);
  97     }
  98 }