Skip to content

Commit

Permalink
8309390: [JVMCI] improve copying system properties into libgraal
Browse files Browse the repository at this point in the history
Reviewed-by: never, kvn
  • Loading branch information
Doug Simon committed Jun 13, 2023
1 parent 63843b1 commit c0aa6bf
Show file tree
Hide file tree
Showing 19 changed files with 451 additions and 195 deletions.
1 change: 0 additions & 1 deletion src/hotspot/share/classfile/vmSymbols.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,6 @@
do_alias(appendToClassPathForInstrumentation_signature, string_void_signature) \
template(serializePropertiesToByteArray_name, "serializePropertiesToByteArray") \
template(serializeAgentPropertiesToByteArray_name, "serializeAgentPropertiesToByteArray") \
template(serializeSavedPropertiesToByteArray_name, "serializeSavedPropertiesToByteArray") \
template(encodeThrowable_name, "encodeThrowable") \
template(encodeThrowable_signature, "(Ljava/lang/Throwable;JI)I") \
template(decodeAndThrowThrowable_name, "decodeAndThrowThrowable") \
Expand Down
76 changes: 2 additions & 74 deletions src/hotspot/share/jvmci/jvmciEnv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "oops/typeArrayOop.inline.hpp"
#include "prims/jvmtiExport.hpp"
#include "runtime/deoptimization.hpp"
#include "runtime/fieldDescriptor.inline.hpp"
#include "runtime/jniHandles.inline.hpp"
#include "runtime/javaCalls.hpp"
#include "runtime/thread.inline.hpp"
Expand All @@ -45,9 +46,6 @@
#include "jvmci/jvmciCompiler.hpp"
#include "jvmci/jvmciRuntime.hpp"

jbyte* JVMCIEnv::_serialized_saved_properties = nullptr;
int JVMCIEnv::_serialized_saved_properties_len = 0;

JVMCICompileState::JVMCICompileState(CompileTask* task, JVMCICompiler* compiler):
_task(task),
_compiler(compiler),
Expand Down Expand Up @@ -117,76 +115,6 @@ bool JVMCICompileState::jvmti_state_changed() const {
return false;
}

jbyte* JVMCIEnv::get_serialized_saved_properties(int& props_len, TRAPS) {
jbyte* props = _serialized_saved_properties;
if (props == nullptr) {
// load VMSupport
Symbol* klass = vmSymbols::jdk_internal_vm_VMSupport();
Klass* k = SystemDictionary::resolve_or_fail(klass, true, CHECK_NULL);

InstanceKlass* ik = InstanceKlass::cast(k);
if (ik->should_be_initialized()) {
ik->initialize(CHECK_NULL);
}

// invoke the serializeSavedPropertiesToByteArray method
JavaValue result(T_OBJECT);
JavaCallArguments args;

Symbol* signature = vmSymbols::void_byte_array_signature();
JavaCalls::call_static(&result,
ik,
vmSymbols::serializeSavedPropertiesToByteArray_name(),
signature,
&args,
CHECK_NULL);

oop res = result.get_oop();
assert(res->is_typeArray(), "must be");
assert(TypeArrayKlass::cast(res->klass())->element_type() == T_BYTE, "must be");
typeArrayOop ba = typeArrayOop(res);
props_len = ba->length();

// Copy serialized saved properties from HotSpot object into C heap
props = NEW_C_HEAP_ARRAY(jbyte, props_len, mtJVMCI);
memcpy(props, ba->byte_at_addr(0), props_len);

_serialized_saved_properties_len = props_len;
_serialized_saved_properties = props;
} else {
props_len = _serialized_saved_properties_len;
}
return props;
}

void JVMCIEnv::copy_saved_properties(jbyte* properties, int properties_len, JVMCI_TRAPS) {
assert(!is_hotspot(), "can only copy saved properties from HotSpot to native image");
JavaThread* thread = JavaThread::current(); // For exception macros.

// Copy native buffer into shared library object
JVMCIPrimitiveArray buf = new_byteArray(properties_len, this);
if (has_pending_exception()) {
_runtime->fatal_exception(JVMCIENV, "Error in copy_saved_properties");
}
copy_bytes_from(properties, buf, 0, properties_len);
if (has_pending_exception()) {
_runtime->fatal_exception(JVMCIENV, "Error in copy_saved_properties");
}

// Initialize saved properties in shared library
jclass servicesClass = JNIJVMCI::Services::clazz();
jmethodID initializeSavedProperties = JNIJVMCI::Services::initializeSavedProperties_method();
bool exception = false;
{
JNIAccessMark jni(this, thread);
jni()->CallStaticVoidMethod(servicesClass, initializeSavedProperties, buf.as_jobject());
exception = jni()->ExceptionCheck();
}
if (exception) {
_runtime->fatal_exception(JVMCIENV, "Error calling jdk.vm.ci.services.Services.initializeSavedProperties");
}
}

void JVMCIEnv::init_env_mode_runtime(JavaThread* thread, JNIEnv* parent_env, bool jni_enomem_is_fatal) {
assert(thread != nullptr, "npe");
_env = nullptr;
Expand Down Expand Up @@ -1937,7 +1865,7 @@ nmethod* JVMCIEnv::get_nmethod(JVMCIObject obj) {
}
#define STATIC_INT_FIELD(className, name) STATIC_PRIMITIVE_FIELD(className, name, jint, Int, EMPTY_CAST)
#define STATIC_BOOLEAN_FIELD(className, name) STATIC_PRIMITIVE_FIELD(className, name, jboolean, Boolean, EMPTY_CAST)
#define METHOD(jniCallType, jniGetMethod, hsCallType, returnType, className, methodName, signatureSymbolName, args)
#define METHOD(jniCallType, jniGetMethod, hsCallType, returnType, className, methodName, signatureSymbolName)
#define CONSTRUCTOR(className, signature)

JVMCI_CLASSES_DO(START_CLASS, END_CLASS, CHAR_FIELD, INT_FIELD, BOOLEAN_FIELD, LONG_FIELD, FLOAT_FIELD, OBJECT_FIELD, PRIMARRAY_FIELD, OBJECTARRAY_FIELD, STATIC_OBJECT_FIELD, STATIC_OBJECTARRAY_FIELD, STATIC_INT_FIELD, STATIC_BOOLEAN_FIELD, METHOD, CONSTRUCTOR)
Expand Down
16 changes: 1 addition & 15 deletions src/hotspot/share/jvmci/jvmciEnv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,6 @@ class JVMCIEnv : public ResourceObj {
// The translated exception is pending in hotspot_env upon returning.
static void translate_from_jni_exception(JavaThread* THREAD, jthrowable throwable, JVMCIEnv* hotspot_env, JVMCIEnv* jni_env);

// Used by copy_saved_properties() to avoid OutOfMemoryErrors when
// initializing a libjvmci runtime in low HotSpot heap conditions.
// Must hold JVMCI_lock when initializing.
static jbyte* _serialized_saved_properties;
static int _serialized_saved_properties_len;

public:
// Opens a JVMCIEnv scope for a Java to VM call (e.g., via CompilerToVM).
// An exception occurring within the scope is left pending when the
Expand Down Expand Up @@ -237,14 +231,6 @@ class JVMCIEnv : public ResourceObj {
return _runtime;
}

// Gets the serialized saved properties from the HotSpot heap.
// The length of the returned array is saved in `len`.
jbyte* get_serialized_saved_properties(int& len, TRAPS);

// Initializes Services.savedProperties in the shared library from the given
// properties in the format produced by `get_serialized_saved_properties`.
void copy_saved_properties(jbyte* properties, int properties_len, JVMCI_TRAPS);

jboolean has_pending_exception();
void clear_pending_exception();

Expand Down Expand Up @@ -509,7 +495,7 @@ class JVMCIEnv : public ResourceObj {
#define STATIC_BOOLEAN_FIELD(className, name) STATIC_FIELD(className, name, jboolean)
#define STATIC_OBJECT_FIELD(className, name, signature) STATIC_OOPISH_FIELD(className, name, JVMCIObject, oop)
#define STATIC_OBJECTARRAY_FIELD(className, name, signature) STATIC_OOPISH_FIELD(className, name, JVMCIObjectArray, objArrayOop)
#define METHOD(jniCallType, jniGetMethod, hsCallType, returnType, className, methodName, signatureSymbolName, args)
#define METHOD(jniCallType, jniGetMethod, hsCallType, returnType, className, methodName, signatureSymbolName)
#define CONSTRUCTOR(className, signature)

JVMCI_CLASSES_DO(START_CLASS, END_CLASS, CHAR_FIELD, INT_FIELD, BOOLEAN_FIELD, LONG_FIELD, FLOAT_FIELD, OBJECT_FIELD, PRIMARRAY_FIELD, OBJECTARRAY_FIELD, STATIC_OBJECT_FIELD, STATIC_OBJECTARRAY_FIELD, STATIC_INT_FIELD, STATIC_BOOLEAN_FIELD, METHOD, CONSTRUCTOR)
Expand Down
17 changes: 10 additions & 7 deletions src/hotspot/share/jvmci/jvmciJavaClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ jmethodID JNIJVMCI::_HotSpotResolvedPrimitiveType_fromMetaspace_method;
#define STATIC_INT_FIELD(className, name) FIELD(className, name, "I", true)
#define STATIC_BOOLEAN_FIELD(className, name) FIELD(className, name, "Z", true)
#ifdef PRODUCT
#define METHOD(jniCallType, jniGetMethod, hsCallType, returnType, className, methodName, signatureSymbolName, args)
#define METHOD(jniCallType, jniGetMethod, hsCallType, returnType, className, methodName, signatureSymbolName)
#define CONSTRUCTOR(className, signature)
#else
#define METHOD(jniCallType, jniGetMethod, hsCallType, returnType, className, methodName, signatureSymbolName, args) \
#define METHOD(jniCallType, jniGetMethod, hsCallType, returnType, className, methodName, signatureSymbolName) \
check_resolve_method(#hsCallType, k, vmSymbols::methodName##_name(), vmSymbols::signatureSymbolName(), CHECK);
#define CONSTRUCTOR(className, signature) { \
TempNewSymbol sig = SymbolTable::new_symbol(signature); \
Expand Down Expand Up @@ -254,7 +254,7 @@ void HotSpotJVMCI::compute_offsets(TRAPS) {

#define STATIC_INT_FIELD(className, name) STATIC_PRIMITIVE_FIELD(className, name, jint)
#define STATIC_BOOLEAN_FIELD(className, name) STATIC_PRIMITIVE_FIELD(className, name, jboolean)
#define METHOD(jniCallType, jniGetMethod, hsCallType, returnType, className, methodName, signatureSymbolName, args)
#define METHOD(jniCallType, jniGetMethod, hsCallType, returnType, className, methodName, signatureSymbolName)
#define CONSTRUCTOR(className, signature)

/**
Expand Down Expand Up @@ -393,7 +393,7 @@ void JNIJVMCI::initialize_field_id(JNIEnv* env, jfieldID &fieldid, jclass clazz,
#define GET_JNI_CONSTRUCTOR(clazz, signature) \
GET_JNI_METHOD(GetMethodID, JNIJVMCI::clazz::_constructor, clazz::_class, "<init>", signature) \

#define METHOD(jniCallType, jniGetMethod, hsCallType, returnType, className, methodName, signatureSymbolName, args) \
#define METHOD(jniCallType, jniGetMethod, hsCallType, returnType, className, methodName, signatureSymbolName) \
GET_JNI_METHOD(jniGetMethod, \
className::_##methodName##_method, \
className::clazz(), \
Expand All @@ -406,6 +406,7 @@ void JNIJVMCI::initialize_field_id(JNIEnv* env, jfieldID &fieldid, jclass clazz,
extern "C" {
void JNICALL JVM_RegisterJVMCINatives(JNIEnv *env, jclass compilerToVMClass);
jobject JNICALL JVM_GetJVMCIRuntime(JNIEnv *env, jclass c);
jlong JNICALL JVM_ReadSystemPropertiesInfo(JNIEnv *env, jclass c, jintArray offsets_handle);
}

// Dumps symbols for public <init>() and <init>(String) methods of
Expand Down Expand Up @@ -565,10 +566,12 @@ static void register_natives_for_class(JNIEnv* env, jclass clazz, const char* na
void JNIJVMCI::register_natives(JNIEnv* env) {
if (env != JavaThread::current()->jni_environment()) {
JNINativeMethod CompilerToVM_nmethods[] = {{ CC"registerNatives", CC"()V", FN_PTR(JVM_RegisterJVMCINatives) }};
JNINativeMethod JVMCI_nmethods[] = {{ CC"initializeRuntime", CC"()Ljdk/vm/ci/runtime/JVMCIRuntime;", FN_PTR(JVM_GetJVMCIRuntime) }};
JNINativeMethod JVMCI_nmethods[] = {{ CC"initializeRuntime", CC"()Ljdk/vm/ci/runtime/JVMCIRuntime;", FN_PTR(JVM_GetJVMCIRuntime) }};
JNINativeMethod Services_nmethods[] = {{ CC"readSystemPropertiesInfo", CC"([I)J", FN_PTR(JVM_ReadSystemPropertiesInfo) }};

register_natives_for_class(env, nullptr, "jdk/vm/ci/hotspot/CompilerToVM", CompilerToVM_nmethods, 1);
register_natives_for_class(env, JVMCI::clazz(), "jdk/vm/ci/runtime/JVMCI", JVMCI_nmethods, 1);
register_natives_for_class(env, Services::clazz(), "jdk/vm/ci/services/Services", Services_nmethods, 1);
}
}

Expand All @@ -583,7 +586,7 @@ void JNIJVMCI::register_natives(JNIEnv* env) {
#define FIELD2(className, name) \
jfieldID JNIJVMCI::className::_##name##_field_id = 0; \
int HotSpotJVMCI::className::_##name##_offset = 0;
#define METHOD(jniCallType, jniGetMethod, hsCallType, returnType, className, methodName, signatureSymbolName, args)
#define METHOD(jniCallType, jniGetMethod, hsCallType, returnType, className, methodName, signatureSymbolName)
#define CONSTRUCTOR(className, signature)

// Generates the definitions of static fields used by the accessors. For example:
Expand Down Expand Up @@ -689,7 +692,7 @@ JVMCI_CLASSES_DO(EMPTY2, EMPTY0, FIELD2, FIELD2, FIELD2, FIELD2, FIELD2, FIELD3,

#define STATIC_INT_FIELD(className, name) STATIC_PRIMITIVE_FIELD(className, name, jint, Int, EMPTY_CAST)
#define STATIC_BOOLEAN_FIELD(className, name) STATIC_PRIMITIVE_FIELD(className, name, jboolean, Boolean, EMPTY_CAST)
#define METHOD(jniCallType, jniGetMethod, hsCallType, returnType, className, methodName, signatureSymbolName, args) \
#define METHOD(jniCallType, jniGetMethod, hsCallType, returnType, className, methodName, signatureSymbolName) \
jmethodID JNIJVMCI::className::_##methodName##_method;

#define CONSTRUCTOR(className, signature) \
Expand Down
Loading

1 comment on commit c0aa6bf

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.