Skip to content

Commit

Permalink
Merge pull request #21071 from tajila/jfr
Browse files Browse the repository at this point in the history
Prevent doube stop of JFR recording
  • Loading branch information
gacholio authored Feb 5, 2025
2 parents 6410fb4 + 2cdab8d commit f3885f4
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,9 @@ private static DiagnosticProperties doJFR(String diagnosticCommand) {
Timer timer = new Timer();
TimerTask jfrDumpTask = new TimerTask() {
public void run() {
VM.stopJFR();
if (VM.isJFRRecordingStarted()) {
VM.stopJFR();
}
}
};
timer.schedule(jfrDumpTask, duration);
Expand Down
20 changes: 12 additions & 8 deletions runtime/jcl/common/com_ibm_oti_vm_VM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,10 @@ Java_com_ibm_oti_vm_VM_startJFR(JNIEnv *env, jclass unused)
J9JavaVM *vm = currentThread->javaVM;
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;

/* this is to initalize JFR late after VM startup */
rc = vmFuncs->initializeJFR(vm, TRUE);
if (!vmFuncs->isJFRRecordingStarted(vm)) {
/* this is to initalize JFR late after VM startup */
rc = vmFuncs->initializeJFR(vm, TRUE);
}

return rc;
}
Expand All @@ -268,12 +270,14 @@ Java_com_ibm_oti_vm_VM_stopJFR(JNIEnv *env, jclass unused)
J9JavaVM *vm = currentThread->javaVM;
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;

vmFuncs->internalEnterVMFromJNI(currentThread);
vmFuncs->acquireExclusiveVMAccess(currentThread);
vmFuncs->jfrDump(currentThread, TRUE);
vmFuncs->releaseExclusiveVMAccess(currentThread);
vmFuncs->tearDownJFR(vm);
vmFuncs->internalExitVMToJNI(currentThread);
if (vmFuncs->isJFRRecordingStarted(vm)) {
vmFuncs->internalEnterVMFromJNI(currentThread);
vmFuncs->acquireExclusiveVMAccess(currentThread);
vmFuncs->jfrDump(currentThread, TRUE);
vmFuncs->releaseExclusiveVMAccess(currentThread);
vmFuncs->tearDownJFR(vm);
vmFuncs->internalExitVMToJNI(currentThread);
}
}

void JNICALL
Expand Down
12 changes: 11 additions & 1 deletion test/functional/cmdLineTests/jfr/jfr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-ex

<!DOCTYPE suite SYSTEM "cmdlinetester.dtd">

<suite id="JFR Tests" timeout="1800">
<suite id="JFR Tests" timeout="2400">
<envvar name="OPENJ9_METADATA_BLOB_FILE_PATH" value="$METADATA_BLOB_PATH$" />
<test id="triggerExecutionSample">
<command>$EXE$ -XX:StartFlightRecording --add-exports java.base/com.ibm.oti.vm=ALL-UNNAMED -cp $RESJAR$ org.openj9.test.TriggerExecutionSample</command>
Expand All @@ -45,6 +45,16 @@ SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-ex
<output type="success" caseSensitive="yes" regex="no">All runs complete.</output>
<output type="failure" caseSensitive="yes" regex="no">Failed</output>
</test>
<test id="VM API Test aggressive start and stop - approx 2mins">
<command>$EXE$ --add-exports java.base/com.ibm.oti.vm=ALL-UNNAMED -cp $RESJAR$ org.openj9.test.VMAPITest 1</command>
<output type="success" caseSensitive="yes" regex="no">All runs complete.</output>
<output type="failure" caseSensitive="yes" regex="no">Failed</output>
</test>
<test id="VM API Test2 - approx 2mins">
<command>$EXE$ --add-exports java.base/com.ibm.oti.vm=ALL-UNNAMED -cp $RESJAR$ org.openj9.test.VMAPITest2</command>
<output type="success" caseSensitive="yes" regex="no">All runs complete.</output>
<output type="failure" caseSensitive="yes" regex="no">Failed</output>
</test>
<test id="Test JFR enablement 1">
<command>$EXE$ --add-exports java.base/com.ibm.oti.vm=ALL-UNNAMED -cp $RESJAR$ org.openj9.test.JFRCMDLineTest</command>
<output type="required" caseSensitive="yes" regex="no">All runs complete</output>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
public class VMAPITest {
public static void main(String[] args) throws Throwable {
final WorkLoad workLoad = new WorkLoad(200, 20000, 200);
int sleepDuration = 1000;

if (args.length > 1) {
sleepDuration = Integer.parseInt(args[0]);
}

if (VM.isJFRRecordingStarted()) {
System.out.println("Failed should not be recording.");
Expand Down Expand Up @@ -55,7 +60,7 @@ public static void main(String[] args) throws Throwable {
return;
}

Thread.sleep(1000);
Thread.sleep(sleepDuration);
VM.jfrDump();
VM.stopJFR();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright IBM Corp. and others 2024
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*/
package org.openj9.test;

import com.ibm.oti.vm.VM;

public class VMAPITest2 {
public static void main(String[] args) throws Throwable {
final WorkLoad workLoad = new WorkLoad(200, 20000, 200);

Thread app = new Thread(() -> {
workLoad.runWork();
});
app.start();

try {
Thread.sleep(3000);
} catch(Throwable t) {
t.printStackTrace();
}

if (VM.isJFRRecordingStarted()) {
System.out.println("Failed should not be recording 1.");
System.exit(0);
}

/* attempt a stop recording before it has started */
VM.stopJFR();

if (VM.isJFRRecordingStarted()) {
System.out.println("Failed should not be recording 2.");
System.exit(0);
}

VM.startJFR();
if (!VM.isJFRRecordingStarted()) {
System.out.println("Failed should be recording 3.");
System.exit(0);
}


/* attempt another start recording after one has already started */
VM.startJFR();
if (!VM.isJFRRecordingStarted()) {
System.out.println("Failed should be recording 4.");
System.exit(0);
}

VM.stopJFR();
if (VM.isJFRRecordingStarted()) {
System.out.println("Failed should not be recording 5.");
System.exit(0);
}

/* attempt a stop recording before it has started */
VM.stopJFR();
}
}

0 comments on commit f3885f4

Please sign in to comment.