1 /*
   2  * Copyright (c) 2016, 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.management;
  27 
  28 import java.time.Duration;
  29 import java.util.ArrayList;
  30 import java.util.Collections;
  31 import java.util.List;
  32 
  33 import jdk.jfr.EventType;
  34 import jdk.jfr.Recording;
  35 import jdk.jfr.internal.JVMSupport;
  36 import jdk.jfr.internal.LogLevel;
  37 import jdk.jfr.internal.LogTag;
  38 import jdk.jfr.internal.Logger;
  39 import jdk.jfr.internal.MetadataRepository;
  40 import jdk.jfr.internal.PlatformRecording;
  41 import jdk.jfr.internal.PrivateAccess;
  42 import jdk.jfr.internal.Utils;
  43 import jdk.jfr.internal.WriteableUserPath;
  44 import jdk.jfr.internal.instrument.JDKEvents;
  45 
  46 /**
  47  * The management API in module jdk.management.jfr should be built on top of the
  48  * public API in jdk.jfr. Before putting more functionality here, consider if it
  49  * should not be part of the public API, and if not, please provide motivation
  50  *
  51  */
  52 public final class ManagementSupport {
  53 
  54     // Purpose of this method is to expose the event types to the
  55     // FlightRecorderMXBean without instantiating Flight Recorder.
  56     //
  57     // This allows:
  58     //
  59     // 1) discoverability, so event settings can be exposed without the need to
  60     // create a new Recording in FlightrecorderMXBean.
  61     //
  62     // 2) a graphical JMX client to list all attributes to the user, without
  63     // loading JFR memory buffers. This is especially important when there is
  64     // no intent to use Flight Recorder.
  65     //
  66     // An alternative design would be to make FlightRecorder#getEventTypes
  67     // static, but it would the make the API look strange
  68     //
  69     public static List<EventType> getEventTypes() {
  70         // would normally be checked when a Flight Recorder instance is created
  71         Utils.checkAccessFlightRecorder();
  72         if (JVMSupport.isNotAvailable()) {
  73             return new ArrayList<>();
  74         }
  75         JDKEvents.initialize(); // make sure JDK events are available
  76         return Collections.unmodifiableList(MetadataRepository.getInstance().getRegisteredEventTypes());
  77     }
  78 
  79     // Reuse internal code for parsing a timespan
  80     public static long parseTimespan(String s) {
  81         return Utils.parseTimespan(s);
  82     }
  83 
  84     // Reuse internal code for formatting settings
  85     public static final String formatTimespan(Duration dValue, String separation) {
  86         return Utils.formatTimespan(dValue, separation);
  87     }
  88 
  89     // Reuse internal logging mechanism
  90     public static void logError(String message) {
  91         Logger.log(LogTag.JFR, LogLevel.ERROR, message);
  92     }
  93 
  94     // Get the textual representation when the destination was set, which
  95     // requires access to jdk.jfr.internal.PlatformRecording
  96     public static String getDestinationOriginalText(Recording recording) {
  97         PlatformRecording pr = PrivateAccess.getInstance().getPlatformRecording(recording);
  98         WriteableUserPath wup = pr.getDestination();
  99         return wup == null ? null : wup.getOriginalText();
 100     }
 101 }