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