1 /*
   2  * Copyright (c) 2016, 2019, 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.internal.JVMSupport;
  35 import jdk.jfr.internal.LogLevel;
  36 import jdk.jfr.internal.LogTag;
  37 import jdk.jfr.internal.Logger;
  38 import jdk.jfr.internal.MetadataRepository;
  39 import jdk.jfr.internal.Utils;
  40 import jdk.jfr.internal.instrument.JDKEvents;
  41 
  42 /**
  43  * The management API in module jdk.management.jfr should be built on top of the
  44  * public API in jdk.jfr. Before putting more functionality here, consider if it
  45  * should not be part of the public API, and if not, please provide motivation
  46  *
  47  */
  48 public final class ManagementSupport {
  49 
  50     // Purpose of this method is to expose the event types to the
  51     // FlightRecorderMXBean without instantiating Flight Recorder.
  52     //
  53     // This allows:
  54     //
  55     // 1) openJDK tests to run without commercial features being
  56     // unlocked, which would be the case if FlightRecorder#getEventTypes would
  57     // be used.
  58     //
  59     // 2 discoverability, so event settings can be exposed without the need to
  60     // create a new Recording in FlightrecorderMXBean.
  61     //
  62     // 3) 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 }