Skip to content

Commit

Permalink
Merge pull request eclipse-openj9#17306 from pshipton/offload2
Browse files Browse the repository at this point in the history
Support offloading for jdk17+
  • Loading branch information
keithc-ca committed May 9, 2023
2 parents 899eedf + 8a269e9 commit 61cabd5
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 14 deletions.
5 changes: 5 additions & 0 deletions runtime/j9vm/exports.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,11 @@ if(NOT JAVA_SPEC_VERSION LESS 17)
JVM_DumpClassListToFile
JVM_DumpDynamicArchive
)
if(J9VM_OPT_JAVA_OFFLOAD_SUPPORT)
jvm_add_exports(jvm
JVM_ValidateJNILibrary
)
endif()
endif()

if(NOT JAVA_SPEC_VERSION LESS 18)
Expand Down
8 changes: 8 additions & 0 deletions runtime/j9vm/j7vmi.c
Original file line number Diff line number Diff line change
Expand Up @@ -2338,6 +2338,14 @@ jobject JNICALL JVM_UnloadLibrary(jint arg0)
{
#if JAVA_SPEC_VERSION >= 15
Trc_SC_UnloadLibrary_Entry(handle);

#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT) && (JAVA_SPEC_VERSION >= 17)
{
UDATA doSwitching = ((UDATA)handle) & J9_NATIVE_LIBRARY_SWITCH_MASK;
handle = (void *)(((UDATA)handle) ^ doSwitching);
}
#endif /* defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT) && (JAVA_SPEC_VERSION >= 17) */

#if defined(WIN32)
FreeLibrary((HMODULE)handle);
#elif defined(J9UNIX) || defined(J9ZOS390) /* defined(WIN32) */
Expand Down
3 changes: 3 additions & 0 deletions runtime/j9vm/j9scar.tdf
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,6 @@ TraceExit=Trc_SC_VirtualThreadUnmountEnd_Exit Overhead=1 Level=3 Template="threa
TraceEvent=Trc_SC_VirtualThread_Info Overhead=1 Level=3 Test Template="virtualThreadObj = 0x%p, virtualThreadState = 0x%p, virtualThreadInspectorCount = %lld, carrierThreadObj = 0x%p, continuationObj = 0x%p, J9VMContinuation = 0x%p"

TraceEvent=Trc_SC_VirtualThread_RootNodeSet Overhead=1 Level=1 Test Template="Virtual threadlist root node set, global ref = 0x%p"

TraceEntry=Trc_SC_ValidateJNILibrary_Entry NoEnv Overhead=1 Level=1 Template="JVM_ValidateJNILibrary(name=%s, handle=%p, isStatic=%d)"
TraceExit=Trc_SC_ValidateJNILibrary_Exit NoEnv Overhead=1 Level=1 Template="JVM_ValidateJNILibrary -- return %p"
3 changes: 3 additions & 0 deletions runtime/j9vm/j9vmnatives.xml
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,9 @@ SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-excepti
<!-- Additions for Java 17 (General) -->
<export name="JVM_DumpClassListToFile"/>
<export name="JVM_DumpDynamicArchive"/>
<export name="JVM_ValidateJNILibrary">
<include-if condition="spec.flags.opt_javaOffloadSupport"/>
</export>
</exports>

<exports group="jdk18">
Expand Down
57 changes: 55 additions & 2 deletions runtime/j9vm/jvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -4005,8 +4005,41 @@ JVM_LoadLibrary(const char *libName, jboolean throwOnFailure)
}


#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT) && (JAVA_SPEC_VERSION >= 17)
/**
* Validates the JNI library for offload.
* If successful, returns a library handle which may be modified from the handle passed in,
* otherwise returns NULL.
*
* @param libName a null terminated string containing the library name.
* @param handle the handle of a JNI library providing the libName.
* @param isStatic indicates if the library is a static JNI library.
*
* @return the shared library's handle if successful, which may be modified, NULL if out of memory.
*
* DLL: jvm
*/
void * JNICALL
JVM_ValidateJNILibrary(const char *libName, void *handle, jboolean isStatic)
{
void *result = NULL;
UDATA doSwitching = 0;
PORT_ACCESS_FROM_JAVAVM(BFUjavaVM);

Trc_SC_ValidateJNILibrary_Entry(libName, handle, isStatic);

doSwitching = validateLibrary(BFUjavaVM, libName, (UDATA)handle, isStatic);
result = (void *)(((UDATA)handle) | doSwitching);
/* Ensure there are no switching bits out of range of the mask. */
Assert_SC_true((UDATA)handle == (((UDATA)result) & ~(UDATA)J9_NATIVE_LIBRARY_SWITCH_MASK));

Trc_SC_ValidateJNILibrary_Exit(result);
return result;
}
#endif /* defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT) && (JAVA_SPEC_VERSION >= 17) */


/**
* void* JNICALL JVM_FindLibraryEntry(UDATA handle, char *functionName)
* Returns a pointer to a function specified by the string
* functionName within a given library specified by handle.
*
Expand All @@ -4023,10 +4056,21 @@ JVM_LoadLibrary(const char *libName, jboolean throwOnFailure)
void* JNICALL
JVM_FindLibraryEntry(void* handle, const char *functionName)
{
void* result;
void *result = NULL;
#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT) && (JAVA_SPEC_VERSION >= 17)
UDATA doSwitching = ((UDATA)handle) & J9_NATIVE_LIBRARY_SWITCH_MASK;
BOOLEAN decorate = FALSE;
#endif /* defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT) && (JAVA_SPEC_VERSION >= 17) */

Trc_SC_FindLibraryEntry_Entry(handle, functionName);

#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT) && (JAVA_SPEC_VERSION >= 17)
handle = (void *)(((UDATA)handle) ^ doSwitching);
#define JNI_PREFIX "Java_"
decorate = (0 != doSwitching) && (0 == strncmp(JNI_PREFIX, functionName, LITERAL_STRLEN(JNI_PREFIX)));
#undef JNI_PREFIX
#endif /* defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT) && (JAVA_SPEC_VERSION >= 17) */

#if defined(WIN32)
result = GetProcAddress ((HINSTANCE)handle, (LPCSTR)functionName);
#elif defined(J9UNIX) || defined(J9ZOS390) /* defined(WIN32) */
Expand All @@ -4035,6 +4079,15 @@ JVM_FindLibraryEntry(void* handle, const char *functionName)
#error "Please implement jvm.c:JVM_FindLibraryEntry(void* handle, const char *functionName)"
#endif /* defined(WIN32) */

#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT) && (JAVA_SPEC_VERSION >= 17)
if ((NULL != result) && decorate) {
UDATA newResult = ((UDATA)result) | doSwitching;
/* Ensure there are no switching bits out of range of the mask. */
Assert_SC_true(((UDATA)result) == (newResult & ~(UDATA)J9_NATIVE_LIBRARY_SWITCH_MASK));
result = (void *)newResult;
}
#endif /* defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT) && (JAVA_SPEC_VERSION >= 17) */

Trc_SC_FindLibraryEntry_Exit(result);

return result;
Expand Down
2 changes: 1 addition & 1 deletion runtime/jvmti/jvmtiStartup.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ loadAgentLibraryGeneric(J9JavaVM * vm, J9JVMTIAgentLibrary * agentLibrary, char
Trc_JVMTI_loadAgentLibraryGeneric_agentAttachedSuccessfully(agentLibrary->nativeLib.name);

#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT)
validateLibrary(vm, &agentLibrary->nativeLib);
agentLibrary->nativeLib.doSwitching = validateLibrary(vm, agentLibrary->nativeLib.name, agentLibrary->nativeLib.handle, JNI_FALSE);
#endif

/* Add the library to the linked list */
Expand Down
1 change: 1 addition & 0 deletions runtime/oti/j9consts.h
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@ extern "C" {
#define J9_NATIVE_LIBRARY_SWITCH_REQUIRED 0x1
#define J9_NATIVE_LIBRARY_SWITCH_JDBC 0x2
#define J9_NATIVE_LIBRARY_SWITCH_WITH_SUBTASKS 0x4
#define J9_NATIVE_LIBRARY_SWITCH_MASK 0x7

#define J9_METHOD_HANDLE_COMPILE_SHARED 0x0
#define J9_METHOD_HANDLE_COMPILE_CUSTOM 0x1
Expand Down
11 changes: 7 additions & 4 deletions runtime/oti/util_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -1310,11 +1310,14 @@ isObjectStackAllocated(J9VMThread *targetThread, j9object_t aObj);

/**
* @brief
* @param *javaVM
* @param *library
* @return void
* @param *javaVM the J9JavaVM.
* @param *libraryName a null terminated string containing the library name.
* @param *libraryHandle the handle of a JNI library providing the libraryName.
* @param isStatic indicates if the library is a static JNI library.
* @return UDATA the J9_NATIVE_LIBRARY_SWITCH bits.
*/
void validateLibrary(J9JavaVM *javaVM, J9NativeLibrary *library);
UDATA
validateLibrary(struct J9JavaVM *javaVM, const char *libraryName, UDATA libraryHandle, jboolean isStatic);
#endif

/* ---------------- optinfo.c ---------------- */
Expand Down
2 changes: 2 additions & 0 deletions runtime/redirector/forwarders.m4
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ _X(JVM_TotalMemory,JNICALL,true,jlong,void)
_X(JVM_TraceInstructions,JNICALL,true,void,jboolean on)
_X(JVM_TraceMethodCalls,JNICALL,true,void,jboolean on)
_X(JVM_UcsOpen,JNICALL,true,jint,const jchar *filename, jint flags, jint mode)
_IF([defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT) && (JAVA_SPEC_VERSION >= 17)],
[_X(JVM_ValidateJNILibrary,JNICALL,true,void *,const char *libName, void *handle, jboolean isStatic)])
_X(JVM_Write,JNICALL,true,jint,jint descriptor, const char *buffer, jint length)
_IF([defined(J9ZOS390)],
[_X(NewStringPlatform,,false,jint,JNIEnv *env, const char *instr, jstring *outstr, const char *encoding)])
Expand Down
4 changes: 3 additions & 1 deletion runtime/util/util.tdf
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ TraceAssert=Assert_Util_signalProtectionFailed noEnv Overhead=1 Level=1 Assert="
TraceAssert=Assert_Util_notNull noEnv Overhead=1 Level=1 Assert="(P1) != NULL"
TraceAssert=Assert_Util_unreachable noEnv Overhead=1 Level=1 Assert="(0 /* unreachable */)"

TraceEntry=Trc_Util_validateLibrary_Entry Noenv Overhead=1 Level=5 Template="validateLibrary entered (library=%p, name=%s)"
TraceEntry=Trc_Util_validateLibrary_Entry Obsolete Noenv Overhead=1 Level=5 Template="validateLibrary entered (library=%p, name=%s)"
TraceEvent=Trc_Util_validateLibrary_onLoadNotFound NoEnv Overhead=1 Level=4 Template="validateLibrary __OnLoad not implemented in %s"
TraceEvent=Trc_Util_validateLibrary_onLoadFound NoEnv Overhead=1 Level=5 Template="validateLibrary __OnLoad implemented (library=%s at=%p)"
TraceException=Trc_Util_validateLibrary_invalidLibraryNameLength Noenv Overhead=1 Level=1 Template="validateLibrary received invalid library name (length=%d)"
Expand All @@ -84,3 +84,5 @@ TraceEvent=Trc_Util_thrhelp_initializeCurrentOSStackFree Overhead=1 Level=1 Temp

TraceEntry=Trc_Util_getDefinedArgumentFromJavaVMInitArgs_Entry Noenv Overhead=1 Level=3 Template="getDefinedArgumentFromJavaVMInitArgs (vmInitArgs=%p, defArg=%s)"
TraceExit=Trc_Util_getDefinedArgumentFromJavaVMInitArgs_Exit Noenv Overhead=1 Level=3 Template="getDefinedArgumentFromJavaVMInitArgs (result=%s)"

TraceEntry=Trc_Util_validateLibrary_Entry Noenv Overhead=1 Level=5 Template="validateLibrary entered (library=%p, name=%s, isStatic=%d)"
16 changes: 13 additions & 3 deletions runtime/vm/bindnatv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,9 @@ lookupJNINative(J9VMThread *currentThread, J9NativeLibrary *nativeLibrary, J9Met
UDATA lookupResult = 0;
void *functionAddress = NULL;
J9JavaVM *vm = currentThread->javaVM;
#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT)
UDATA doSwitching = 0;
#endif /* defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT) */
PORT_ACCESS_FROM_JAVAVM(vm);

Trc_VM_lookupJNINative_Entry(currentThread, nativeLibrary, nativeMethod, symbolName, signature);
Expand All @@ -1111,6 +1114,10 @@ lookupJNINative(J9VMThread *currentThread, J9NativeLibrary *nativeLibrary, J9Met
UDATA args[] = {(UDATA)classLoaderObject, (UDATA)entryName};
internalRunStaticMethod(currentThread, findNativeMethod, TRUE, (sizeof(args) / sizeof(UDATA)), args);
functionAddress = (UDATA*)(*(U_64*)&(currentThread->returnValue));
#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT)
doSwitching = ((UDATA)functionAddress) & J9_NATIVE_LIBRARY_SWITCH_MASK;
functionAddress = (UDATA *)(((UDATA)functionAddress) & ~(UDATA)J9_NATIVE_LIBRARY_SWITCH_MASK);
#endif /* defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT) */
}
/* always clear pending exception, might retry later */
VM_VMHelpers::clearException(currentThread);
Expand All @@ -1123,6 +1130,9 @@ lookupJNINative(J9VMThread *currentThread, J9NativeLibrary *nativeLibrary, J9Met
#endif /* JAVA_SPEC_VERSION >= 17 */
{
lookupResult = j9sl_lookup_name(nativeLibrary->handle, symbolName, (UDATA*)&functionAddress, signature);
#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT)
doSwitching = nativeLibrary->doSwitching;
#endif /* defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT) */
}
if (0 == lookupResult) {
UDATA cpFlags = J9_STARTPC_JNI_NATIVE;
Expand All @@ -1133,11 +1143,11 @@ lookupJNINative(J9VMThread *currentThread, J9NativeLibrary *nativeLibrary, J9Met
internalReleaseVMAccess(currentThread);
#endif
#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT)
if ((NULL != nativeLibrary) && (0 != nativeLibrary->doSwitching)) {
if (0 != doSwitching) {
cpFlags |= J9_STARTPC_NATIVE_REQUIRES_SWITCHING;
if (J9_ARE_ANY_BITS_SET(nativeLibrary->doSwitching, J9_NATIVE_LIBRARY_SWITCH_JDBC | J9_NATIVE_LIBRARY_SWITCH_WITH_SUBTASKS)) {
if (J9_ARE_ANY_BITS_SET(doSwitching, J9_NATIVE_LIBRARY_SWITCH_JDBC | J9_NATIVE_LIBRARY_SWITCH_WITH_SUBTASKS)) {
J9Class *ramClass = J9_CLASS_FROM_METHOD(nativeMethod);
if (J9_ARE_ANY_BITS_SET(nativeLibrary->doSwitching, J9_NATIVE_LIBRARY_SWITCH_JDBC)) {
if (J9_ARE_ANY_BITS_SET(doSwitching, J9_NATIVE_LIBRARY_SWITCH_JDBC)) {
ramClass->classDepthAndFlags |= J9AccClassHasJDBCNatives;
} else {
ramClass->classFlags |= J9ClassHasOffloadAllowSubtasksNatives;
Expand Down
9 changes: 6 additions & 3 deletions runtime/vm/vmbootlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,9 @@ classLoaderRegisterLibrary(void *voidVMThread, J9ClassLoader *classLoader, const
if (NULL != libraryPtr) {
*libraryPtr = newNativeLibrary;
}
#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT)
newNativeLibrary->doSwitching = validateLibrary(javaVM, logicalName, newNativeLibrary->handle, JNI_TRUE);
#endif /* defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT) */
} else {
/* Return value is not 1.8 (or above); could mean either of 2 things:
* 1. JNI_OnLoad_L /was/ defined, but didn't return 1.8 (or above). Eg. a
Expand Down Expand Up @@ -711,11 +714,11 @@ classLoaderRegisterLibrary(void *voidVMThread, J9ClassLoader *classLoader, const
if (NULL != libraryPtr) {
*libraryPtr = newNativeLibrary;
}
#ifdef J9VM_OPT_JAVA_OFFLOAD_SUPPORT
#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT)
if (J9NATIVELIB_LOAD_OK == rc) {
validateLibrary(javaVM, newNativeLibrary);
newNativeLibrary->doSwitching = validateLibrary(javaVM, newNativeLibrary->name, newNativeLibrary->handle, JNI_FALSE);
}
#endif
#endif /* defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT) */
RELEASE_CLASS_LOADER_BLOCKS_MUTEX(javaVM);

/* Call JNI_OnLoad to get the required JNI version, only if the library has not
Expand Down

0 comments on commit 61cabd5

Please sign in to comment.