Skip to content

Commit 1adf84a

Browse files
committed
Command line options --start-jfr <pid>[@settings] and --dump-jfr <pid> and --stop-jfr <pid>
1 parent 08219fa commit 1adf84a

File tree

7 files changed

+279
-7
lines changed

7 files changed

+279
-7
lines changed

visualvm/jfr/nbproject/project.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@
9696
<specification-version>1.48</specification-version>
9797
</run-dependency>
9898
</dependency>
99+
<dependency>
100+
<code-name-base>org.netbeans.modules.sendopts</code-name-base>
101+
<build-prerequisite/>
102+
<compile-dependency/>
103+
<run-dependency>
104+
<release-version>2</release-version>
105+
<specification-version>2.43</specification-version>
106+
</run-dependency>
107+
</dependency>
99108
<dependency>
100109
<code-name-base>org.openide.awt</code-name-base>
101110
<build-prerequisite/>

visualvm/jfr/src/org/graalvm/visualvm/jfr/JFRSnapshotSupport.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ static void register() {
126126
jfrDumpProvider.initialize();
127127
}
128128

129-
public static void takeJfrDump(Application application, boolean openView, boolean stopJfr) {
130-
jfrDumpProvider.createJfrDump(application, openView, stopJfr);
129+
public static void takeJfrDump(Application application, boolean stopJfr, boolean openView) {
130+
jfrDumpProvider.createJfrDump(application, stopJfr, openView);
131131
}
132132

133133
public static void takeRemoteJfrDump(Application application, String dumpFile, boolean customizeDumpFile) {
@@ -153,6 +153,11 @@ public static void jfrStartRecording(Application application) {
153153
checkNotifyCommercialFeatures(application);
154154
jfrDumpProvider.jfrStartRecording(application);
155155
}
156+
157+
public static void jfrStartRecording(Application application, String params) {
158+
checkNotifyCommercialFeatures(application);
159+
jfrDumpProvider.jfrStartRecording(application, params);
160+
}
156161

157162
public static void remoteJfrStartRecording(Application application) {
158163
checkNotifyCommercialFeatures(application);

visualvm/jfr/src/org/graalvm/visualvm/jfr/impl/Bundle.properties

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,11 @@ MSG_JFR_Stop=S&top JFR
9999
LBL_JFR_Stop=Stop JFR Recording
100100

101101
LBL_Stopping_JFR_Recording=Stopping JFR Recording...
102+
103+
Argument_Start_ShortDescr=start JFR of the provided process
104+
105+
Argument_Dump_ShortDescr=dump JFR data of the provided process
106+
107+
Argument_Stop_ShortDescr=stop JFR of the provided process
108+
109+
MSG_NO_APP_PID = Cannot find application with pid {0}.
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright (c) 2021, 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+
package org.graalvm.visualvm.jfr.impl;
26+
27+
import java.util.HashSet;
28+
import java.util.Map;
29+
import java.util.Set;
30+
import org.graalvm.visualvm.application.Application;
31+
import org.graalvm.visualvm.application.ApplicationFinder;
32+
import org.graalvm.visualvm.jfr.JFRSnapshotSupport;
33+
import org.netbeans.api.sendopts.CommandException;
34+
import org.netbeans.spi.sendopts.Env;
35+
import org.netbeans.spi.sendopts.Option;
36+
import org.netbeans.spi.sendopts.OptionProcessor;
37+
import org.openide.DialogDisplayer;
38+
import org.openide.NotifyDescriptor;
39+
import org.openide.util.NbBundle;
40+
import org.openide.util.lookup.ServiceProvider;
41+
42+
/**
43+
*
44+
* @author Jiri Sedlacek
45+
*/
46+
@ServiceProvider(service=OptionProcessor.class)
47+
public class JFRArguments extends OptionProcessor {
48+
49+
private static final String START_LONG_NAME = "start-jfr"; // NOI18N
50+
private static final Option START_JFR_ARGUMENT = Option.shortDescription(Option.requiredArgument(Option.NO_SHORT_NAME, START_LONG_NAME), "org.graalvm.visualvm.jfr.impl", "Argument_Start_ShortDescr"); // NOI18N
51+
private static final String DUMP_LONG_NAME = "dump-jfr"; // NOI18N
52+
private static final Option DUMP_JFR_ARGUMENT = Option.shortDescription(Option.requiredArgument(Option.NO_SHORT_NAME, DUMP_LONG_NAME), "org.graalvm.visualvm.jfr.impl", "Argument_Dump_ShortDescr"); // NOI18N
53+
private static final String STOP_LONG_NAME = "stop-jfr"; // NOI18N
54+
private static final Option STOP_JFR_ARGUMENT = Option.shortDescription(Option.requiredArgument(Option.NO_SHORT_NAME, STOP_LONG_NAME), "org.graalvm.visualvm.jfr.impl", "Argument_Stop_ShortDescr"); // NOI18N
55+
56+
57+
@Override
58+
protected Set<Option> getOptions() {
59+
Set<Option> options = new HashSet();
60+
options.add(START_JFR_ARGUMENT);
61+
options.add(DUMP_JFR_ARGUMENT);
62+
options.add(STOP_JFR_ARGUMENT);
63+
return options;
64+
}
65+
66+
@Override
67+
protected void process(Env env, Map<Option, String[]> maps) throws CommandException {
68+
String[] startJFR = maps.get(START_JFR_ARGUMENT);
69+
if (startJFR != null) {
70+
final String[] _startJFR = startJFR.length == 1 ? startJFR[0].split("@") : null; // NOI18N
71+
if (_startJFR != null && _startJFR.length == 2) startJFR[0] = _startJFR[0];
72+
new Finder(startJFR, START_LONG_NAME) {
73+
@Override
74+
public void found(Application application) {
75+
if (_startJFR != null && _startJFR.length == 2) {
76+
JFRSnapshotSupport.jfrStartRecording(application, decode(_startJFR[1]));
77+
} else {
78+
JFRSnapshotSupport.jfrStartRecording(application);
79+
}
80+
}
81+
}.find();
82+
return;
83+
}
84+
85+
final String[] dumpJFR = maps.get(DUMP_JFR_ARGUMENT);
86+
final String[] stopJFR = maps.get(STOP_JFR_ARGUMENT);
87+
if (dumpJFR != null) {
88+
new Finder(dumpJFR, DUMP_LONG_NAME) {
89+
@Override
90+
public void found(Application application) {
91+
boolean stop = stopJFR != null && stopJFR.length == 1 && stopJFR[0].equals(dumpJFR[0]);
92+
JFRSnapshotSupport.takeJfrDump(application, stop, true);
93+
}
94+
}.find();
95+
return;
96+
}
97+
98+
if (stopJFR != null) {
99+
new Finder(stopJFR, STOP_LONG_NAME) {
100+
@Override
101+
public void found(Application application) {
102+
JFRSnapshotSupport.jfrStopRecording(application);
103+
}
104+
}.find();
105+
}
106+
}
107+
108+
109+
private static String decode(String value) {
110+
value = value.replace("%27", "\'"); // NOI18N
111+
value = value.replace("%22", "\""); // NOI18N
112+
value = value.replace("%20", " "); // NOI18N
113+
return value;
114+
}
115+
116+
117+
private static abstract class Finder extends ApplicationFinder {
118+
119+
Finder(String[] pids, String longName) throws CommandException {
120+
super(resolvePid(pids, longName));
121+
}
122+
123+
124+
public final void notFound(int pid, String id) {
125+
NotifyDescriptor desc = new NotifyDescriptor.Message(NbBundle.getMessage(JFRArguments.class, "MSG_NO_APP_PID", new Object[] { Integer.toString(pid) }), NotifyDescriptor.WARNING_MESSAGE); // NOI18N
126+
DialogDisplayer.getDefault().notifyLater(desc);
127+
}
128+
129+
130+
private static int resolvePid(String[] pids, String longName) throws CommandException {
131+
if (pids.length == 1) {
132+
try {
133+
return Integer.valueOf(pids[0]);
134+
} catch (NumberFormatException e) {
135+
throw new CommandException(0, "Incorrect pid format for --" + longName + ": " + e.getMessage()); // NOI18N
136+
}
137+
} else {
138+
throw new CommandException(0, "--" + longName + " requires exactly one value"); // NOI18N
139+
}
140+
}
141+
142+
}
143+
144+
}

visualvm/jfr/src/org/graalvm/visualvm/jfr/impl/JFRDumpAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected void actionPerformed(Set<DataSource> dataSources, ActionEvent actionEv
7474
boolean tagged = (actionEvent.getModifiers() & Toolkit.
7575
getDefaultToolkit().getMenuShortcutKeyMask()) != 0;
7676
if (application.isLocalApplication()) {
77-
JFRSnapshotSupport.takeJfrDump(application, !tagged, false);
77+
JFRSnapshotSupport.takeJfrDump(application, false, !tagged);
7878
} else {
7979
JFRSnapshotSupport.takeRemoteJfrDump(application, null, !tagged);
8080
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 2021, 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+
package org.graalvm.visualvm.jfr.impl;
26+
27+
import java.util.HashMap;
28+
import java.util.Map;
29+
30+
/**
31+
*
32+
* @author Jiri Sedlacek
33+
*/
34+
final class JFRParameters {
35+
36+
static final String NAME = "name"; // NOI18N
37+
static final String SETTINGS = "settings"; // NOI18N
38+
39+
40+
private final Map<String, String> parameters;
41+
42+
43+
private JFRParameters(String parametersS) {
44+
if (parametersS == null || parametersS.isEmpty()) {
45+
parameters = null;
46+
} else {
47+
parameters = new HashMap();
48+
parseParameters(parametersS, parameters);
49+
}
50+
}
51+
52+
53+
static JFRParameters parse(String parameters) {
54+
return new JFRParameters(parameters);
55+
}
56+
57+
58+
String get(String key) {
59+
return parameters == null ? null : parameters.get(key);
60+
}
61+
62+
63+
public String toString() {
64+
return parameters == null ? "[no parameters]" : parameters.toString(); // NOI18N
65+
}
66+
67+
68+
private static void parseParameters(String parametersS, Map<String, String> parameters) {
69+
for (String parameter : parametersS.split(",")) { // NOI18N
70+
71+
// name
72+
int idx = parameter.indexOf(NAME + "="); // NOI18N
73+
if (idx == 0) parameters.put(NAME, parameter.substring(NAME.length() + 1));
74+
75+
// settings
76+
idx = parameter.indexOf(SETTINGS + "="); // NOI18N
77+
if (idx == 0) parameters.put(SETTINGS, parameter.substring(SETTINGS.length() + 1));
78+
79+
}
80+
}
81+
82+
}

visualvm/jfr/src/org/graalvm/visualvm/jfr/impl/JFRRecordingProvider.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,30 @@ public class JFRRecordingProvider {
7474

7575
private final static Logger LOGGER = Logger.getLogger(JFRRecordingProvider.class.getName());
7676

77-
public void jfrStartRecording(final Application application) {
77+
public void jfrStartRecording(Application application) {
78+
jfrStartRecording(application, null);
79+
}
80+
81+
public void jfrStartRecording(Application application, String params) {
82+
JFRParameters parameters = JFRParameters.parse(params);
83+
jfrStartRecording(application,
84+
parameters.get(JFRParameters.NAME),
85+
parameters.get(JFRParameters.SETTINGS),
86+
null, // delay
87+
null, // duration
88+
null, // disk
89+
null, // path
90+
null, // maxAge
91+
null, // maxSize
92+
null // dumpOnExit
93+
);
94+
}
95+
96+
public void jfrStartRecording(final Application application, final String name,
97+
final String settings, final String delay,
98+
final String duration, final Boolean disk,
99+
final String path, final String maxAge,
100+
final String maxSize, final Boolean dumpOnExit) {
78101
VisualVM.getInstance().runTask(new Runnable() {
79102
public void run() {
80103
Jvm jvm = JvmFactory.getJVMFor(application);
@@ -83,8 +106,9 @@ public void run() {
83106
pHandle = ProgressHandle.createHandle(NbBundle.getMessage(JFRRecordingProvider.class, "LBL_Starting_JFR_Recording")); // NOI18N
84107
pHandle.setInitialDelay(0);
85108
pHandle.start();
86-
if (!jvm.startJfrRecording(null, null, null, null, null, null,
87-
null, null, null)) {
109+
String[] _settings = settings == null ? null : new String[] { settings };
110+
if (!jvm.startJfrRecording(name, _settings, delay, duration,
111+
disk, path, maxAge, maxSize, dumpOnExit)) {
88112
notifyJfrDumpFailed(application);
89113
} else {
90114
Set<DataSource> ds = ActionUtils.getSelectedDataSources();
@@ -137,7 +161,7 @@ public void remoteJfrStopRecording(Application application) {
137161
jfrStopRecording(application);
138162
}
139163

140-
public void createJfrDump(final Application application, final boolean openView, final boolean stopJfr) {
164+
public void createJfrDump(final Application application, final boolean stopJfr, final boolean openView) {
141165
VisualVM.getInstance().runTask(new Runnable() {
142166
public void run() {
143167
Jvm jvm = JvmFactory.getJVMFor(application);

0 commit comments

Comments
 (0)