author | mgronlun |
Wed, 30 Oct 2019 19:43:52 +0100 | |
changeset 58863 | c16ac7a2eba4 |
parent 55302 | 686dedba1d9a |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
58863 | 2 |
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. |
50113 | 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 jdk.jfr.internal.dcmd; |
|
26 |
||
55302
686dedba1d9a
8216064: -XX:StartFlightRecording:settings= doesn't work properly
egahlin
parents:
52413
diff
changeset
|
27 |
import java.io.FileNotFoundException; |
50113 | 28 |
import java.io.IOException; |
50745 | 29 |
import java.nio.file.Files; |
50113 | 30 |
import java.nio.file.InvalidPathException; |
50745 | 31 |
import java.nio.file.Path; |
50113 | 32 |
import java.nio.file.Paths; |
33 |
import java.text.ParseException; |
|
34 |
import java.time.Duration; |
|
50745 | 35 |
import java.util.Arrays; |
50113 | 36 |
import java.util.HashMap; |
37 |
import java.util.Map; |
|
38 |
||
39 |
import jdk.jfr.FlightRecorder; |
|
40 |
import jdk.jfr.Recording; |
|
41 |
import jdk.jfr.internal.JVM; |
|
50745 | 42 |
import jdk.jfr.internal.LogLevel; |
43 |
import jdk.jfr.internal.LogTag; |
|
44 |
import jdk.jfr.internal.Logger; |
|
45 |
import jdk.jfr.internal.OldObjectSample; |
|
46 |
import jdk.jfr.internal.PrivateAccess; |
|
50113 | 47 |
import jdk.jfr.internal.SecuritySupport.SafePath; |
48 |
import jdk.jfr.internal.Type; |
|
49 |
import jdk.jfr.internal.jfc.JFC; |
|
50 |
||
51 |
/** |
|
52 |
* JFR.start |
|
53 |
* |
|
54 |
*/ |
|
55 |
//Instantiated by native |
|
56 |
final class DCmdStart extends AbstractDCmd { |
|
57 |
||
58 |
/** |
|
59 |
* Execute JFR.start. |
|
60 |
* |
|
61 |
* @param name optional name that can be used to identify recording. |
|
50745 | 62 |
* @param settings names of settings files to use, i.e. "default" or |
50113 | 63 |
* "default.jfc". |
64 |
* @param delay delay before recording is started, in nanoseconds. Must be |
|
65 |
* at least 1 second. |
|
66 |
* @param duration duration of the recording, in nanoseconds. Must be at |
|
67 |
* least 1 second. |
|
68 |
* @param disk if recording should be persisted to disk |
|
69 |
* @param path file path where recording data should be written |
|
70 |
* @param maxAge how long recording data should be kept in the disk |
|
71 |
* repository, or <code>0</code> if no limit should be set. |
|
72 |
* |
|
73 |
* @param maxSize the minimum amount data to keep in the disk repository |
|
74 |
* before it is discarded, or <code>0</code> if no limit should be |
|
75 |
* set. |
|
76 |
* |
|
77 |
* @param dumpOnExit if recording should dump on exit |
|
78 |
* |
|
79 |
* @return result output |
|
80 |
* |
|
81 |
* @throws DCmdException if recording could not be started |
|
82 |
*/ |
|
83 |
@SuppressWarnings("resource") |
|
58863 | 84 |
public String execute(String name, String[] settings, Long delay, Long duration, Boolean disk, String path, Long maxAge, Long maxSize, Long flush, Boolean dumpOnExit, Boolean pathToGcRoots) throws DCmdException { |
52413 | 85 |
if (Logger.shouldLog(LogTag.JFR_DCMD, LogLevel.DEBUG)) { |
50745 | 86 |
Logger.log(LogTag.JFR_DCMD, LogLevel.DEBUG, "Executing DCmdStart: name=" + name + |
87 |
", settings=" + Arrays.asList(settings) + |
|
88 |
", delay=" + delay + |
|
89 |
", duration=" + duration + |
|
90 |
", disk=" + disk+ |
|
91 |
", filename=" + path + |
|
92 |
", maxage=" + maxAge + |
|
58863 | 93 |
", flush=" + flush + |
50745 | 94 |
", maxsize=" + maxSize + |
95 |
", dumponexit =" + dumpOnExit + |
|
96 |
", path-to-gc-roots=" + pathToGcRoots); |
|
97 |
} |
|
50113 | 98 |
if (name != null) { |
99 |
try { |
|
100 |
Integer.parseInt(name); |
|
101 |
throw new DCmdException("Name of recording can't be numeric"); |
|
102 |
} catch (NumberFormatException nfe) { |
|
103 |
// ok, can't be mixed up with name |
|
104 |
} |
|
105 |
} |
|
106 |
||
107 |
if (duration == null && Boolean.FALSE.equals(dumpOnExit) && path != null) { |
|
108 |
throw new DCmdException("Filename can only be set for a time bound recording or if dumponexit=true. Set duration/dumponexit or omit filename."); |
|
109 |
} |
|
55302
686dedba1d9a
8216064: -XX:StartFlightRecording:settings= doesn't work properly
egahlin
parents:
52413
diff
changeset
|
110 |
if (settings.length == 1 && settings[0].length() == 0) { |
686dedba1d9a
8216064: -XX:StartFlightRecording:settings= doesn't work properly
egahlin
parents:
52413
diff
changeset
|
111 |
throw new DCmdException("No settings specified. Use settings=none to start without any settings"); |
686dedba1d9a
8216064: -XX:StartFlightRecording:settings= doesn't work properly
egahlin
parents:
52413
diff
changeset
|
112 |
} |
50113 | 113 |
Map<String, String> s = new HashMap<>(); |
50745 | 114 |
for (String configName : settings) { |
50113 | 115 |
try { |
116 |
s.putAll(JFC.createKnown(configName).getSettings()); |
|
55302
686dedba1d9a
8216064: -XX:StartFlightRecording:settings= doesn't work properly
egahlin
parents:
52413
diff
changeset
|
117 |
} catch(FileNotFoundException e) { |
686dedba1d9a
8216064: -XX:StartFlightRecording:settings= doesn't work properly
egahlin
parents:
52413
diff
changeset
|
118 |
throw new DCmdException("Could not find settings file'" + configName + "'", e); |
50113 | 119 |
} catch (IOException | ParseException e) { |
55302
686dedba1d9a
8216064: -XX:StartFlightRecording:settings= doesn't work properly
egahlin
parents:
52413
diff
changeset
|
120 |
throw new DCmdException("Could not parse settings file '" + settings[0] + "'", e); |
50113 | 121 |
} |
122 |
} |
|
123 |
||
50745 | 124 |
OldObjectSample.updateSettingPathToGcRoots(s, pathToGcRoots); |
50113 | 125 |
|
126 |
if (duration != null) { |
|
127 |
if (duration < 1000L * 1000L * 1000L) { |
|
128 |
// to avoid typo, duration below 1s makes no sense |
|
129 |
throw new DCmdException("Could not start recording, duration must be at least 1 second."); |
|
130 |
} |
|
131 |
} |
|
132 |
||
133 |
if (delay != null) { |
|
134 |
if (delay < 1000L * 1000L * 1000) { |
|
135 |
// to avoid typo, delay shorter than 1s makes no sense. |
|
136 |
throw new DCmdException("Could not start recording, delay must be at least 1 second."); |
|
137 |
} |
|
138 |
} |
|
139 |
||
58863 | 140 |
if (flush != null) { |
141 |
if (Boolean.FALSE.equals(disk)) { |
|
142 |
throw new DCmdException("Flush can only be set for recordings that are to disk."); |
|
143 |
} |
|
144 |
} |
|
145 |
||
50113 | 146 |
if (!FlightRecorder.isInitialized() && delay == null) { |
147 |
initializeWithForcedInstrumentation(s); |
|
148 |
} |
|
149 |
||
150 |
Recording recording = new Recording(); |
|
50226
408021edf22f
8195819: Remove recording=x from jcmd JFR.check output
egahlin
parents:
50113
diff
changeset
|
151 |
if (name != null) { |
50113 | 152 |
recording.setName(name); |
153 |
} |
|
154 |
||
155 |
if (disk != null) { |
|
156 |
recording.setToDisk(disk.booleanValue()); |
|
157 |
} |
|
58863 | 158 |
|
50113 | 159 |
recording.setSettings(s); |
50745 | 160 |
SafePath safePath = null; |
50113 | 161 |
|
162 |
if (path != null) { |
|
163 |
try { |
|
50745 | 164 |
if (dumpOnExit == null) { |
165 |
// default to dumponexit=true if user specified filename |
|
166 |
dumpOnExit = Boolean.TRUE; |
|
167 |
} |
|
168 |
Path p = Paths.get(path); |
|
169 |
if (Files.isDirectory(p) && Boolean.TRUE.equals(dumpOnExit)) { |
|
170 |
// Decide destination filename at dump time |
|
171 |
// Purposely avoid generating filename in Recording#setDestination due to |
|
172 |
// security concerns |
|
173 |
PrivateAccess.getInstance().getPlatformRecording(recording).setDumpOnExitDirectory(new SafePath(p)); |
|
174 |
} else { |
|
175 |
safePath = resolvePath(recording, path); |
|
176 |
recording.setDestination(safePath.toPath()); |
|
177 |
} |
|
50113 | 178 |
} catch (IOException | InvalidPathException e) { |
179 |
recording.close(); |
|
180 |
throw new DCmdException("Could not start recording, not able to write to file %s. %s ", path, e.getMessage()); |
|
181 |
} |
|
182 |
} |
|
183 |
||
184 |
if (maxAge != null) { |
|
185 |
recording.setMaxAge(Duration.ofNanos(maxAge)); |
|
186 |
} |
|
187 |
||
58863 | 188 |
if (flush != null) { |
189 |
recording.setFlushInterval(Duration.ofNanos(flush)); |
|
190 |
} |
|
191 |
||
50113 | 192 |
if (maxSize != null) { |
193 |
recording.setMaxSize(maxSize); |
|
194 |
} |
|
195 |
||
196 |
if (duration != null) { |
|
197 |
recording.setDuration(Duration.ofNanos(duration)); |
|
198 |
} |
|
199 |
||
200 |
if (dumpOnExit != null) { |
|
201 |
recording.setDumpOnExit(dumpOnExit); |
|
202 |
} |
|
203 |
||
204 |
if (delay != null) { |
|
205 |
Duration dDelay = Duration.ofNanos(delay); |
|
206 |
recording.scheduleStart(dDelay); |
|
207 |
print("Recording " + recording.getId() + " scheduled to start in "); |
|
208 |
printTimespan(dDelay, " "); |
|
209 |
print("."); |
|
210 |
} else { |
|
211 |
recording.start(); |
|
212 |
print("Started recording " + recording.getId() + "."); |
|
213 |
} |
|
214 |
||
215 |
if (recording.isToDisk() && duration == null && maxAge == null && maxSize == null) { |
|
216 |
print(" No limit specified, using maxsize=250MB as default."); |
|
217 |
recording.setMaxSize(250*1024L*1024L); |
|
218 |
} |
|
219 |
||
50745 | 220 |
if (safePath != null && duration != null) { |
50113 | 221 |
println(" The result will be written to:"); |
222 |
println(); |
|
50745 | 223 |
printPath(safePath); |
50113 | 224 |
} else { |
225 |
println(); |
|
226 |
println(); |
|
227 |
String cmd = duration == null ? "dump" : "stop"; |
|
228 |
String fileOption = path == null ? "filename=FILEPATH " : ""; |
|
229 |
String recordingspecifier = "name=" + recording.getId(); |
|
230 |
// if user supplied a name, use it. |
|
231 |
if (name != null) { |
|
232 |
recordingspecifier = "name=" + quoteIfNeeded(name); |
|
233 |
} |
|
50887 | 234 |
print("Use jcmd " + getPid() + " JFR." + cmd + " " + recordingspecifier + " " + fileOption + "to copy recording data to file."); |
50113 | 235 |
println(); |
236 |
} |
|
58863 | 237 |
|
50113 | 238 |
return getResult(); |
239 |
} |
|
240 |
||
50887 | 241 |
|
50113 | 242 |
// Instruments JDK-events on class load to reduce startup time |
243 |
private void initializeWithForcedInstrumentation(Map<String, String> settings) { |
|
244 |
if (!hasJDKEvents(settings)) { |
|
245 |
return; |
|
246 |
} |
|
247 |
JVM jvm = JVM.getJVM(); |
|
248 |
try { |
|
249 |
jvm.setForceInstrumentation(true); |
|
250 |
FlightRecorder.getFlightRecorder(); |
|
251 |
} finally { |
|
252 |
jvm.setForceInstrumentation(false); |
|
253 |
} |
|
254 |
} |
|
255 |
||
256 |
private boolean hasJDKEvents(Map<String, String> settings) { |
|
257 |
String[] eventNames = new String[7]; |
|
258 |
eventNames[0] = "FileRead"; |
|
259 |
eventNames[1] = "FileWrite"; |
|
260 |
eventNames[2] = "SocketRead"; |
|
261 |
eventNames[3] = "SocketWrite"; |
|
262 |
eventNames[4] = "JavaErrorThrow"; |
|
263 |
eventNames[5] = "JavaExceptionThrow"; |
|
264 |
eventNames[6] = "FileForce"; |
|
265 |
for (String eventName : eventNames) { |
|
266 |
if ("true".equals(settings.get(Type.EVENT_NAME_PREFIX + eventName + "#enabled"))) { |
|
267 |
return true; |
|
268 |
} |
|
269 |
} |
|
270 |
return false; |
|
271 |
} |
|
272 |
} |