Skip to content

Commit

Permalink
Add triggerExecutionSample native
Browse files Browse the repository at this point in the history
- Add JPP flag for JFR
- Add com.ibm.oti.vm.VM::triggerExecutionSample that calls
  jfrExecutionSample on all Java threads
- Add test for triggerExecutionSample

Depend on:
- ibmruntimes/openj9-openjdk-jdk#817
- adoptium/TKG#589

Signed-off-by: Gengchen Tuo <gengchen.tuo@ibm.com>
  • Loading branch information
thallium authored and JasonFengJ9 committed Jul 28, 2024
1 parent 487b005 commit 81754c7
Show file tree
Hide file tree
Showing 16 changed files with 2,481 additions and 10 deletions.
2 changes: 1 addition & 1 deletion jcl/jpp_configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<!-- START SET DEFINITIONS -->
<set
label="newflags"
flags="CRAC_SUPPORT,CRIU_SUPPORT,INLINE-TYPES,OPENJCEPLUS_SUPPORT,OPENJDK_METHODHANDLES"/>
flags="CRAC_SUPPORT,CRIU_SUPPORT,INLINE-TYPES,JFR_SUPPORT,OPENJCEPLUS_SUPPORT,OPENJDK_METHODHANDLES"/>

<set
label="oldflags"
Expand Down
7 changes: 7 additions & 0 deletions jcl/src/java.base/share/classes/com/ibm/oti/vm/VM.java
Original file line number Diff line number Diff line change
Expand Up @@ -637,4 +637,11 @@ public static ConstantPool getConstantPoolFromAnnotationBytes(Class<?> clazz, by
public static Properties internalGetProperties() {
return getVMLangAccess().internalGetProperties();
}

/*[IF JFR_SUPPORT]*/
/**
* Trigger ExecutionSample JFR event on all Java threads.
*/
public static native void triggerExecutionSample();
/*[ENDIF] JFR_SUPPORT */
}
4 changes: 4 additions & 0 deletions jcl/src/java.base/share/classes/java/lang/System.java
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,10 @@ private static void ensureProperties(boolean isInitialization) {
/*[ENDIF] CRAC_SUPPORT */
/*[ENDIF] CRIU_SUPPORT */

/*[IF JFR_SUPPORT]*/
initializedProperties.put("org.eclipse.openj9.jfr.isJFREnabled", "true"); //$NON-NLS-1$ //$NON-NLS-2$
/*[ENDIF] JFR_SUPPORT */

String[] list = getPropertyList();
for (int i = 0; i < list.length; i += 2) {
String key = list[i];
Expand Down
2 changes: 1 addition & 1 deletion runtime/jcl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ target_sources(jclse
${CMAKE_CURRENT_SOURCE_DIR}/common/bpinit.c
${CMAKE_CURRENT_SOURCE_DIR}/common/clsldr.cpp
${CMAKE_CURRENT_SOURCE_DIR}/common/com_ibm_jvm_Stats.c
${CMAKE_CURRENT_SOURCE_DIR}/common/com_ibm_oti_vm_VM.c
${CMAKE_CURRENT_SOURCE_DIR}/common/com_ibm_oti_vm_VM.cpp
${CMAKE_CURRENT_SOURCE_DIR}/common/compiler.c
${CMAKE_CURRENT_SOURCE_DIR}/common/dump.c
${CMAKE_CURRENT_SOURCE_DIR}/common/exhelp.c
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@
*******************************************************************************/

#include <string.h>
#include "jni.h"
#include "jcl.h"
#include "jclglob.h"
#include "jclprots.h"
#include "jcl_internal.h"
#include "jni.h"
#include "omrlinkedlist.h"
#include "util_api.h"
#include "VMHelpers.hpp"

/* private static native byte[][] getVMArgsImpl(); */

extern "C" {
jobjectArray JNICALL
Java_com_ibm_oti_vm_VM_getVMArgsImpl(JNIEnv *env, jobject recv)
{
Expand All @@ -53,9 +55,9 @@ Java_com_ibm_oti_vm_VM_getVMArgsImpl(JNIEnv *env, jobject recv)

/* Create the result array and fill it in, including only options that begin with "-" */

byteArrayClass = (*env)->FindClass(env, "[B");
byteArrayClass = env->FindClass("[B");
if (NULL != byteArrayClass) {
result = (*env)->NewObjectArray(env, resultSize, byteArrayClass, NULL);
result = env->NewObjectArray(resultSize, byteArrayClass, NULL);
if (NULL != result) {
jint writeIndex = 0;

Expand All @@ -64,15 +66,15 @@ Java_com_ibm_oti_vm_VM_getVMArgsImpl(JNIEnv *env, jobject recv)

if ('-' == optionString[0]) {
jint optionLength = (jint) strlen(optionString);
jbyteArray option = (*env)->NewByteArray(env, optionLength);
jbyteArray option = env->NewByteArray(optionLength);

if (NULL == option) {
/* Don't use break here to avoid triggering the assertion below */
return NULL;
}
(*env)->SetByteArrayRegion(env, option, 0, optionLength, (jbyte*)optionString);
(*env)->SetObjectArrayElement(env, result, writeIndex, option);
(*env)->DeleteLocalRef(env, option);
env->SetByteArrayRegion(option, 0, optionLength, (jbyte*)optionString);
env->SetObjectArrayElement(result, writeIndex, option);
env->DeleteLocalRef(option);
writeIndex += 1;
}
}
Expand Down Expand Up @@ -190,3 +192,30 @@ Java_com_ibm_oti_vm_VM_isJVMInSingleThreadedMode(JNIEnv *env, jclass unused)
#endif /* defined(J9VM_OPT_CRIU_SUPPORT) */
return result;
}

#if defined(J9VM_OPT_JFR)
void JNICALL
Java_com_ibm_oti_vm_VM_triggerExecutionSample(JNIEnv *env, jclass unused) {
J9VMThread *currentThread = (J9VMThread *)env;
J9JavaVM *vm = currentThread->javaVM;
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;

vmFuncs->internalEnterVMFromJNI(currentThread);
vmFuncs->acquireExclusiveVMAccess(currentThread);

J9VMThread *walkThread = J9_LINKED_LIST_START_DO(vm->mainThread);
while (NULL != walkThread) {
if (VM_VMHelpers::threadCanRunJavaCode(walkThread)
&& (currentThread != walkThread)
) {
vmFuncs->jfrExecutionSample(currentThread, walkThread);
}
walkThread = J9_LINKED_LIST_NEXT_DO(vm->mainThread, walkThread);
}

vmFuncs->releaseExclusiveVMAccess(currentThread);
vmFuncs->internalExitVMToJNI(currentThread);
}
#endif /* defined(J9VM_OPT_JFR) */

} /* extern "C" */
7 changes: 7 additions & 0 deletions runtime/jcl/exports.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -718,3 +718,10 @@ if(NOT JAVA_SPEC_VERSION LESS 20)
Java_java_lang_Thread_setScopedValueCache
)
endif()

# J9VM_OPT_JFR
if(J9VM_OPT_JFR)
omr_add_exports(jclse
Java_com_ibm_oti_vm_VM_triggerExecutionSample
)
endif()
4 changes: 4 additions & 0 deletions runtime/jcl/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-ex
<xi:include href="uma/se19only_exports.xml"></xi:include>
<xi:include href="uma/se19_exports.xml"></xi:include>
<xi:include href="uma/se20_exports.xml"></xi:include>
<xi:include href="uma/jfr_exports.xml"></xi:include>

<xi:include href="uma/vendor_jcl_exports.xml">
<xi:fallback/>
Expand Down Expand Up @@ -140,6 +141,9 @@ SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-ex
<group name="se20">
<include-if condition="spec.java20"/>
</group>
<group name="jfr">
<include-if condition="spec.flags.opt_jfr"/>
</group>
</exports>

<includes>
Expand Down
24 changes: 24 additions & 0 deletions runtime/jcl/uma/jfr_exports.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!--
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
-->
<exports group="jfr">
<export name="Java_com_ibm_oti_vm_VM_triggerExecutionSample" />
</exports>
3 changes: 3 additions & 0 deletions runtime/oti/j9nonbuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -5188,6 +5188,9 @@ typedef struct J9InternalVMFunctions {
#if defined(J9VM_ZOS_3164_INTEROPERABILITY) && (JAVA_SPEC_VERSION >= 17)
I_32 (*invoke31BitJNI_OnXLoad)(struct J9JavaVM *vm, void *handle, jboolean isOnLoad, void *reserved);
#endif /* defined(J9VM_ZOS_3164_INTEROPERABILITY) && (JAVA_SPEC_VERSION >= 17) */
#if defined(J9VM_OPT_JFR)
void (*jfrExecutionSample)(struct J9VMThread *currentThread, struct J9VMThread *sampleThread);
#endif /* defined(J9VM_OPT_JFR) */
} J9InternalVMFunctions;

/* Jazz 99339: define a new structure to replace JavaVM so as to pass J9NativeLibrary to JVMTIEnv */
Expand Down
5 changes: 5 additions & 0 deletions runtime/oti/jclprots.h
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,11 @@ Java_com_ibm_oti_vm_VM_getJ9ConstantPoolFromJ9Class(JNIEnv *env, jclass unused,
jboolean JNICALL
Java_com_ibm_oti_vm_VM_isJVMInSingleThreadedMode(JNIEnv *env, jclass unused);

#if defined(J9VM_OPT_JFR)
void JNICALL
Java_com_ibm_oti_vm_VM_triggerExecutionSample(JNIEnv *env, jclass unused);
#endif /* defined(J9VM_OPT_JFR) */

#if JAVA_SPEC_VERSION >= 16
jboolean JNICALL
Java_java_lang_ref_Reference_refersTo(JNIEnv *env, jobject reference, jobject target);
Expand Down
4 changes: 4 additions & 0 deletions runtime/vm/intfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "j9protos.h"
#include "vm_api.h"
#include "vm_internal.h"
#if defined(J9VM_OPT_VM_LOCAL_STORAGE)

#include "j9vmls.h"
Expand Down Expand Up @@ -454,4 +455,7 @@ J9InternalVMFunctions J9InternalFunctions = {
#if defined(J9VM_ZOS_3164_INTEROPERABILITY) && (JAVA_SPEC_VERSION >= 17)
invoke31BitJNI_OnXLoad,
#endif /* defined(J9VM_ZOS_3164_INTEROPERABILITY) && (JAVA_SPEC_VERSION >= 17) */
#if defined(J9VM_OPT_JFR)
jfrExecutionSample,
#endif /* defined(J9VM_OPT_JFR) */
};
83 changes: 83 additions & 0 deletions test/functional/cmdLineTests/jfr/build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?xml version="1.0"?>

<!--
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
-->
<project name="jfr" default="build" basedir=".">
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<description>
Build cmdLineTests_jfr
</description>

<import file="${TEST_ROOT}/functional/cmdLineTests/buildTools.xml"/>

<!-- set properties for this build -->
<property name="DEST" value="${BUILD_ROOT}/functional/cmdLineTests/jfr" />
<property name="src" location="./src"/>
<property name="build" location="./bin"/>

<condition property="jfrEnabled">
<and>
<contains string="${TEST_FLAG}" substring="JFR" />
<!-- For the time being, JFR tests are only limited to JDK17+. -->
<not>
<matches string="${JDK_VERSION}" pattern="^(8|9|1[0-6])$$" />
</not>
</and>
</condition>

<target name="init">
<mkdir dir="${DEST}" />
<mkdir dir="${build}" />
</target>

<target name="compile" depends="init" description="Using java ${JDK_VERSION} to compile the source" if="jfrEnabled">
<echo>Ant version is ${ant.version}</echo>
<echo>============COMPILER SETTINGS============</echo>
<echo>===fork: yes</echo>
<echo>===executable: ${compiler.javac}</echo>
<echo>===debug: on</echo>
<echo>===destdir: ${DEST}</echo>
<javac srcdir="${src}" destdir="${build}" debug="true" fork="true" executable="${compiler.javac}" includeAntRuntime="false" encoding="ISO-8859-1">
<compilerarg line="--add-exports java.base/com.ibm.oti.vm=ALL-UNNAMED" />
<src path="${src}"/>
</javac>
</target>

<target name="dist" depends="compile" description="generate the distribution">
<jar jarfile="${DEST}/jfr.jar" filesonly="true">
<fileset dir="${build}" />
<fileset dir="${src}" />
</jar>
<copy todir="${DEST}">
<fileset dir="${src}/../" includes="*.xml,*.mk,metadata.blob" />
</copy>
</target>

<target name="clean" depends="dist" description="clean up">
<!-- Delete the ${build} directory trees -->
<delete dir="${build}" />
</target>

<target name="build" depends="buildCmdLineTestTools">
<antcall target="clean" inheritall="true" />
</target>
</project>
33 changes: 33 additions & 0 deletions test/functional/cmdLineTests/jfr/jfr.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>

<!--
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
-->

<!DOCTYPE suite SYSTEM "cmdlinetester.dtd">

<suite id="JFR Tests" timeout="300">
<envvar name="OPENJ9_METADATA_BLOB_FILE_PATH" value="$METADATA_BLOB_PATH$"/>
<test id="triggerExecutionSample">
<command>$EXE$ -XX:+FlightRecorder --add-exports java.base/com.ibm.oti.vm=ALL-UNNAMED -cp $RESJAR$ org.openj9.test.TriggerExecutionSample</command>
<return type="success" value="0" />
</test>
</suite>
Loading

0 comments on commit 81754c7

Please sign in to comment.