Skip to content

Commit 516d0f5

Browse files
authored
Merge pull request eclipse-openj9#21070 from JasonFengJ9/jfrjcmd
JFR can invoke VM.setJFRRecordingFileName() before VM.startJFR()
2 parents f6f1da0 + c28c696 commit 516d0f5

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

jcl/src/java.base/share/classes/openj9/internal/tools/attach/target/DiagnosticUtils.java

+10-12
Original file line numberDiff line numberDiff line change
@@ -448,35 +448,33 @@ private static String parseStringParameter(String paramName, String[] parameters
448448

449449
private static DiagnosticProperties doJFR(String diagnosticCommand) {
450450
DiagnosticProperties result = null;
451-
452451
// split the command and arguments
453452
String[] parts = diagnosticCommand.split(DIAGNOSTICS_OPTION_SEPARATOR);
454453
IPC.logMessage("doJFR: ", diagnosticCommand);
455-
456454
// ensure there's at least one part for the command
457455
if (parts.length == 0) {
458456
return DiagnosticProperties.makeErrorProperties("Error: No JFR command specified");
459457
}
460-
461458
String command = parts[0].trim();
462459
String[] parameters = Arrays.copyOfRange(parts, 1, parts.length);
463-
464460
String fileName = parseStringParameter("filename", parameters, null);
465461
IPC.logMessage("doJFR: filename = ", fileName);
466-
boolean setFileName = (fileName != null) && !fileName.isEmpty();
467-
if (setFileName) {
468-
if (!VM.setJFRRecordingFileName(fileName)) {
469-
return DiagnosticProperties.makeErrorProperties("setJFRRecordingFileName failed");
470-
} else {
471-
jfrRecordingFileName = fileName;
472-
}
473-
}
474462

475463
try {
476464
if (command.equalsIgnoreCase(DIAGNOSTICS_JFR_START)) {
477465
if (VM.isJFRRecordingStarted()) {
478466
result = DiagnosticProperties.makeErrorProperties("One JFR recording is in progress [" + jfrRecordingFileName + "], only one recording is allowed at a time.");
479467
} else {
468+
// only JFR.start command is allowed to change the recording filename
469+
boolean setFileName = (fileName != null) && !fileName.isEmpty();
470+
if (setFileName) {
471+
// the recording filename should be set before VM.startJFR() which invokes JFRWriter:openJFRFile()
472+
if (!VM.setJFRRecordingFileName(fileName)) {
473+
return DiagnosticProperties.makeErrorProperties("setJFRRecordingFileName() failed");
474+
} else {
475+
jfrRecordingFileName = fileName;
476+
}
477+
}
480478
VM.startJFR();
481479
long duration = parseTimeParameter("duration", parameters);
482480
IPC.logMessage("doJFR: duration = " + duration);

runtime/vm/jfr.cpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -1127,12 +1127,15 @@ jfrSamplingThreadProc(void *entryArg)
11271127
jboolean
11281128
setJFRRecordingFileName(J9JavaVM *vm, char *fileName)
11291129
{
1130-
UDATA defaultFileNameLen = strlen(DEFAULT_JFR_FILE_NAME);
1131-
if ((defaultFileNameLen != strlen(vm->jfrState.jfrFileName))
1132-
|| (0 != strncmp(DEFAULT_JFR_FILE_NAME, vm->jfrState.jfrFileName, defaultFileNameLen))
1133-
) {
1134-
PORT_ACCESS_FROM_JAVAVM(vm);
1135-
j9mem_free_memory(vm->jfrState.jfrFileName);
1130+
if (NULL != vm->jfrState.jfrFileName) {
1131+
UDATA defaultFileNameLen = strlen(DEFAULT_JFR_FILE_NAME);
1132+
const char *jfrFileName = vm->jfrState.jfrFileName;
1133+
if ((defaultFileNameLen != strlen(jfrFileName))
1134+
|| (0 != strncmp(DEFAULT_JFR_FILE_NAME, jfrFileName, defaultFileNameLen))
1135+
) {
1136+
PORT_ACCESS_FROM_JAVAVM(vm);
1137+
j9mem_free_memory(vm->jfrState.jfrFileName);
1138+
}
11361139
}
11371140
vm->jfrState.jfrFileName = fileName;
11381141
VM_JFRWriter::closeJFRFile(vm);

0 commit comments

Comments
 (0)