Skip to content

Commit 6cab47f

Browse files
committed
Make sure --start-jfr starts recording only if needed, correctly decode parameters, allow to define settings in file
1 parent d3f9e43 commit 6cab47f

File tree

3 files changed

+69
-26
lines changed

3 files changed

+69
-26
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ public static boolean supportsRemoteJfrDump(Application application) {
150150
}
151151

152152
public static void jfrStartRecording(Application application) {
153-
checkNotifyCommercialFeatures(application);
154-
jfrDumpProvider.jfrStartRecording(application);
153+
jfrStartRecording(application, null);
155154
}
156155

157156
public static void jfrStartRecording(Application application, String params) {

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Set;
3030
import org.graalvm.visualvm.application.Application;
3131
import org.graalvm.visualvm.application.ApplicationFinder;
32+
import org.graalvm.visualvm.core.VisualVM;
3233
import org.graalvm.visualvm.jfr.JFRSnapshotSupport;
3334
import org.netbeans.api.sendopts.CommandException;
3435
import org.netbeans.spi.sendopts.Env;
@@ -71,12 +72,15 @@ protected void process(Env env, Map<Option, String[]> maps) throws CommandExcept
7172
if (_startJFR != null && _startJFR.length == 2) startJFR[0] = _startJFR[0];
7273
new Finder(startJFR, START_LONG_NAME) {
7374
@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-
}
75+
public void found(final Application application) {
76+
VisualVM.getInstance().runTask(new Runnable() {
77+
public void run() {
78+
if (JFRSnapshotSupport.supportsJfrStart(application)) {
79+
String params = _startJFR != null && _startJFR.length == 2 ? _startJFR[1] : null;
80+
JFRSnapshotSupport.jfrStartRecording(application, params);
81+
}
82+
}
83+
});
8084
}
8185
}.find();
8286
return;
@@ -106,14 +110,6 @@ public void found(Application application) {
106110
}
107111

108112

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-
117113
private static abstract class Finder extends ApplicationFinder {
118114

119115
Finder(String[] pids, String longName) throws CommandException {

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

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,26 @@
2424
*/
2525
package org.graalvm.visualvm.jfr.impl;
2626

27+
import java.io.FileInputStream;
28+
import java.io.IOException;
29+
import java.io.InputStreamReader;
2730
import java.util.HashMap;
2831
import java.util.Map;
32+
import java.util.Properties;
33+
import java.util.logging.Level;
34+
import java.util.logging.Logger;
2935

3036
/**
3137
*
3238
* @author Jiri Sedlacek
3339
*/
3440
final class JFRParameters {
3541

42+
private static final Logger LOGGER = Logger.getLogger(JFRParameters.class.getName());
43+
44+
45+
private static final String FILE = "settings-file"; // NOI18N
46+
3647
static final String NAME = "name"; // NOI18N
3748
static final String SETTINGS = "settings"; // NOI18N
3849

@@ -66,17 +77,54 @@ public String toString() {
6677

6778

6879
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-
80+
if (parametersS.startsWith(FILE + "=")) { // NOI18N
81+
// settings defined in file
82+
parseParametersFile(decode(parametersS.substring(FILE.length() + 1)), parameters);
83+
} else {
84+
for (String parameter : parametersS.split(",")) { // NOI18N
85+
86+
// name
87+
int idx = parameter.indexOf(NAME + "="); // NOI18N
88+
if (idx == 0) parameters.put(NAME, decode(parameter.substring(NAME.length() + 1)));
89+
90+
// settings
91+
idx = parameter.indexOf(SETTINGS + "="); // NOI18N
92+
if (idx == 0) parameters.put(SETTINGS, decode(parameter.substring(SETTINGS.length() + 1)));
93+
94+
}
7995
}
8096
}
8197

98+
private static void parseParametersFile(String file, Map<String, String> parameters) {
99+
Properties properties = loadProperties(file);
100+
101+
// name
102+
String prop = properties.getProperty(NAME);
103+
if (prop != null) parameters.put(NAME, decode(prop));
104+
105+
// settings
106+
prop = properties.getProperty(SETTINGS);
107+
if (prop != null) parameters.put(SETTINGS, decode(prop));
108+
}
109+
110+
protected static Properties loadProperties(String file) {
111+
Properties properties = new Properties();
112+
113+
try (InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8")) { // NOI18N
114+
properties.load(isr);
115+
} catch (IOException e) {
116+
LOGGER.log(Level.WARNING, "Failed to read JFR parameters", e); // NOI18N
117+
}
118+
119+
return properties;
120+
}
121+
122+
private static String decode(String value) {
123+
value = value.replace("%27", "\'"); // NOI18N
124+
value = value.replace("%22", "\""); // NOI18N
125+
value = value.replace("%20", " "); // NOI18N
126+
value = value.replace("%2C", ","); // NOI18N
127+
return value;
128+
}
129+
82130
}

0 commit comments

Comments
 (0)