# HG changeset patch # User rkennke # Date 1576833442 -3600 # Fri Dec 20 10:17:22 2019 +0100 # Node ID b470088d891c0e53d247ffb7dcc6ff699cc86cce # Parent b2aca65cc0992debe03eb652f88622b64d2cf7d4 8227269: Slow class loading when running with JDWP diff --git a/src/jdk.jdwp.agent/share/native/libjdwp/classTrack.c b/src/jdk.jdwp.agent/share/native/libjdwp/classTrack.c --- a/src/jdk.jdwp.agent/share/native/libjdwp/classTrack.c +++ b/src/jdk.jdwp.agent/share/native/libjdwp/classTrack.c @@ -45,187 +45,128 @@ #include "bag.h" #include "classTrack.h" -/* ClassTrack hash table slot count */ -#define CT_HASH_SLOT_COUNT 263 /* Prime which eauals 4k+3 for some k */ +/* ClassTrack table slot count */ +#define CT_SLOT_COUNT 263 /* Prime which eauals 4k+3 for some k */ typedef struct KlassNode { - jclass klass; /* weak global reference */ + jlong klass_tag; /* Klass's tag in tracking env */ char *signature; /* class signature */ struct KlassNode *next; /* next node in this slot */ } KlassNode; /* - * Hash table of prepared classes. Each entry is a pointer - * to a linked list of KlassNode. + * Table mapping tag % CT_SLOT_COUNT to linked-list of KlassNode*. + */ +static KlassNode** table; + +/* + * The JVMTI tracking env to keep track of klass tags, for class-unloads + */ +static jvmtiEnv* trackingEnv; + +/* + * The current highest tag number */ -static KlassNode **table; +static jlong currentClassTag; + +/* + * Lock to protect deletedSignatureBag + */ +static jrawMonitorID deletedSignatureLock; + +/* + * A bag containing all the deleted classes' signatures. Must be accessed under + * deletedTagLock, + */ +struct bag* deletedSignatureBag; /* - * Return slot in hash table to use for this class. + * Callback when classes are freed, Finds the signature and remembers it in deletedSignatureBag. */ -static jint -hashKlass(jclass klass) +static void JNICALL +cbTrackingObjectFree(jvmtiEnv* jvmti_env, jlong tag) { - jint hashCode = objectHashCode(klass); - return abs(hashCode) % CT_HASH_SLOT_COUNT; + debugMonitorEnter(deletedSignatureLock); + + // Find deleted KlassNode + size_t idx = tag % CT_SLOT_COUNT; + KlassNode** klass_ptr = &table[idx]; + KlassNode* klass = *klass_ptr; + + // Tag not found? Ignore. + if (klass == NULL) { + debugMonitorExit(deletedSignatureLock); + return; + } + + // Scan linked-list. + jlong found_tag = klass->klass_tag; + while (klass != NULL && found_tag != tag) { + klass_ptr = &klass->next; + klass = *klass_ptr; + found_tag = klass->klass_tag; + } + + // Tag not found? Ignore. + if (found_tag != tag) { + debugMonitorExit(deletedSignatureLock); + return; + } + + // At this point we have the KlassNode corresponding to the tag + // in klass, and the pointer to it in klass_node. + // Remember the unloaded signature. + *(char**)bagAdd(deletedSignatureBag) = klass->signature; + + // Unlink the KlassNode. + *klass_ptr = klass->next; + jvmtiDeallocate(klass); + + // Done. + debugMonitorExit(deletedSignatureLock); } /* - * Transfer a node (which represents klass) from the current - * table to the new table. - */ -static void -transferClass(JNIEnv *env, jclass klass, KlassNode **newTable) { - jint slot = hashKlass(klass); - KlassNode **head = &table[slot]; - KlassNode **newHead = &newTable[slot]; - KlassNode **nodePtr; - KlassNode *node; - - /* Search the node list of the current table for klass */ - for (nodePtr = head; node = *nodePtr, node != NULL; nodePtr = &(node->next)) { - if (isSameObject(env, klass, node->klass)) { - /* Match found transfer node */ - - /* unlink from old list */ - *nodePtr = node->next; - - /* insert in new list */ - node->next = *newHead; - *newHead = node; - - return; - } - } - - /* we haven't found the class, only unloads should have happenned, - * so the only reason a class should not have been found is - * that it is not prepared yet, in which case we don't want it. - * Asset that the above is true. - */ -/**** the HotSpot VM doesn't create prepare events for some internal classes *** - JDI_ASSERT_MSG((classStatus(klass) & - (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY))==0, - classSignature(klass)); -***/ -} - -/* - * Delete a hash table of classes. - * The signatures of classes in the table are returned. - */ -static struct bag * -deleteTable(JNIEnv *env, KlassNode *oldTable[]) -{ - struct bag *signatures = bagCreateBag(sizeof(char*), 10); - jint slot; - - if (signatures == NULL) { - EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"signatures"); - } - - for (slot = 0; slot < CT_HASH_SLOT_COUNT; slot++) { - KlassNode *node = oldTable[slot]; - - while (node != NULL) { - KlassNode *next; - char **sigSpot; - - /* Add signature to the signature bag */ - sigSpot = bagAdd(signatures); - if (sigSpot == NULL) { - EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"signature bag"); - } - *sigSpot = node->signature; - - /* Free weak ref and the node itself */ - JNI_FUNC_PTR(env,DeleteWeakGlobalRef)(env, node->klass); - next = node->next; - jvmtiDeallocate(node); - - node = next; - } - } - jvmtiDeallocate(oldTable); - - return signatures; -} - -/* - * Called after class unloads have occurred. Creates a new hash table - * of currently loaded prepared classes. - * The signatures of classes which were unloaded (not present in the - * new table) are returned. + * Called after class unloads have occurred. + * The signatures of classes which were unloaded are returned. */ struct bag * classTrack_processUnloads(JNIEnv *env) { - KlassNode **newTable; - struct bag *unloadedSignatures; - - unloadedSignatures = NULL; - newTable = jvmtiAllocate(CT_HASH_SLOT_COUNT * sizeof(KlassNode *)); - if (newTable == NULL) { - EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY, "classTrack table"); - } else { - - (void)memset(newTable, 0, CT_HASH_SLOT_COUNT * sizeof(KlassNode *)); - - WITH_LOCAL_REFS(env, 1) { - - jint classCount; - jclass *classes; - jvmtiError error; - int i; - - error = allLoadedClasses(&classes, &classCount); - if ( error != JVMTI_ERROR_NONE ) { - jvmtiDeallocate(newTable); - EXIT_ERROR(error,"loaded classes"); - } else { - - /* Transfer each current class into the new table */ - for (i=0; iassertOn) { /* Check this is not a duplicate */ - for (node = *head; node != NULL; node = node->next) { - if (isSameObject(env, klass, node->klass)) { - JDI_ASSERT_FAILED("Attempting to insert duplicate class"); - break; - } + jlong tag; + error = JVMTI_FUNC_PTR(trackingEnv, GetTag)(trackingEnv, klass, &tag); + if (error != JVMTI_ERROR_NONE) { + EXIT_ERROR(error, "Unable to GetTag with class trackingEnv"); + } + if (tag != 0l) { + return; // Already added } } - node = jvmtiAllocate(sizeof(KlassNode)); + debugMonitorEnter(deletedSignatureLock); + KlassNode* node = jvmtiAllocate(sizeof(KlassNode)); if (node == NULL) { EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"KlassNode"); } @@ -234,23 +175,78 @@ jvmtiDeallocate(node); EXIT_ERROR(error,"signature"); } - if ((node->klass = JNI_FUNC_PTR(env,NewWeakGlobalRef)(env, klass)) == NULL) { + node->klass_tag = ++currentClassTag; + error = JVMTI_FUNC_PTR(trackingEnv, SetTag)(trackingEnv, klass, node->klass_tag); + if (error != JVMTI_ERROR_NONE) { jvmtiDeallocate(node->signature); jvmtiDeallocate(node); - EXIT_ERROR(AGENT_ERROR_NULL_POINTER,"NewWeakGlobalRef"); + EXIT_ERROR(error,"SetTag"); } /* Insert the new node */ - node->next = *head; - *head = node; + size_t idx = node->klass_tag % CT_SLOT_COUNT; + node->next = table[idx]; + table[idx] = node; + debugMonitorExit(deletedSignatureLock); +} + +static jboolean +setupEvents() +{ + jvmtiCapabilities caps; + memset(&caps, 0, sizeof(caps)); + caps.can_generate_object_free_events = 1; + jvmtiError error = JVMTI_FUNC_PTR(trackingEnv, AddCapabilities)(trackingEnv, &caps); + if (error != JVMTI_ERROR_NONE) { + return JNI_FALSE; + } + jvmtiEventCallbacks cb; + memset(&cb, 0, sizeof(cb)); + cb.ObjectFree = cbTrackingObjectFree; + error = JVMTI_FUNC_PTR(trackingEnv, SetEventCallbacks)(trackingEnv, &cb, sizeof(cb)); + if (error != JVMTI_ERROR_NONE) { + return JNI_FALSE; + } + error = JVMTI_FUNC_PTR(trackingEnv, SetEventNotificationMode)(trackingEnv, JVMTI_ENABLE, JVMTI_EVENT_OBJECT_FREE, NULL); + if (error != JVMTI_ERROR_NONE) { + return JNI_FALSE; + } + return JNI_TRUE; } /* - * Called once to build the initial prepared class hash table. + * Called once to initialize class-tracking. */ void classTrack_initialize(JNIEnv *env) { + trackingEnv = getSpecialJvmti(); + if (trackingEnv == NULL) { + EXIT_ERROR(AGENT_ERROR_INTERNAL, "Failed to allocate tag-tracking jvmtiEnv"); + } + deletedSignatureLock = debugMonitorCreate("Deleted class tag lock"); + deletedSignatureBag = bagCreateBag(sizeof(char*), 10); + currentClassTag = -1l; + table = NULL; +} + +/* + * Called to activate class-tracking when a listener registers for EI_GC_FINISH. + */ +void +classTrack_activate(JNIEnv *env) +{ + if (!setupEvents()) { + EXIT_ERROR(AGENT_ERROR_INTERNAL, "Unable to setup ObjectFree tracking"); + } + currentClassTag = 0l; + table = jvmtiAllocate(CT_SLOT_COUNT * sizeof(KlassNode*)); + if (table != NULL) { + (void)memset(table, 0, CT_SLOT_COUNT * sizeof(KlassNode*)); + } else { + EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY, "failed allocating class-track table"); + } + WITH_LOCAL_REFS(env, 1) { jint classCount; @@ -260,24 +256,14 @@ error = allLoadedClasses(&classes, &classCount); if ( error == JVMTI_ERROR_NONE ) { - table = jvmtiAllocate(CT_HASH_SLOT_COUNT * sizeof(KlassNode *)); - if (table != NULL) { - (void)memset(table, 0, CT_HASH_SLOT_COUNT * sizeof(KlassNode *)); - for (i=0; inext; + jvmtiDeallocate(node->signature); + jvmtiDeallocate(node); + node = next; + } + } + jvmtiDeallocate(table); + + debugMonitorEnter(deletedSignatureLock); + bagEnumerateOver(deletedSignatureBag, cleanDeleted, NULL); + bagDestroyBag(deletedSignatureBag); + debugMonitorExit(deletedSignatureLock); + + currentClassTag = -1; + debugMonitorDestroy(deletedSignatureLock); + (void)JVMTI_FUNC_PTR(trackingEnv,DisposeEnvironment)(trackingEnv); + trackingEnv = NULL; } diff --git a/src/jdk.jdwp.agent/share/native/libjdwp/classTrack.h b/src/jdk.jdwp.agent/share/native/libjdwp/classTrack.h --- a/src/jdk.jdwp.agent/share/native/libjdwp/classTrack.h +++ b/src/jdk.jdwp.agent/share/native/libjdwp/classTrack.h @@ -46,6 +46,12 @@ classTrack_initialize(JNIEnv *env); /* + * Activates class tracking. + */ +void +classTrack_activate(JNIEnv *env); + +/* * Reset class tracking. */ void diff --git a/src/jdk.jdwp.agent/share/native/libjdwp/eventHandler.c b/src/jdk.jdwp.agent/share/native/libjdwp/eventHandler.c --- a/src/jdk.jdwp.agent/share/native/libjdwp/eventHandler.c +++ b/src/jdk.jdwp.agent/share/native/libjdwp/eventHandler.c @@ -1625,6 +1625,9 @@ node->handlerID = external? ++requestIdCounter : 0; error = eventFilterRestricted_install(node); + if (node->ei == EI_GC_FINISH) { + classTrack_activate(getEnv()); + } if (error == JVMTI_ERROR_NONE) { insert(getHandlerChain(node->ei), node); } diff --git a/src/jdk.jdwp.agent/share/native/libjdwp/util.c b/src/jdk.jdwp.agent/share/native/libjdwp/util.c --- a/src/jdk.jdwp.agent/share/native/libjdwp/util.c +++ b/src/jdk.jdwp.agent/share/native/libjdwp/util.c @@ -1742,7 +1742,7 @@ } /* Get the jvmti environment to be used with tags */ -static jvmtiEnv * +jvmtiEnv * getSpecialJvmti(void) { jvmtiEnv *jvmti; diff --git a/src/jdk.jdwp.agent/share/native/libjdwp/util.h b/src/jdk.jdwp.agent/share/native/libjdwp/util.h --- a/src/jdk.jdwp.agent/share/native/libjdwp/util.h +++ b/src/jdk.jdwp.agent/share/native/libjdwp/util.h @@ -414,4 +414,6 @@ void saveGlobalRef(JNIEnv *env, jobject obj, jobject *pobj); void tossGlobalRef(JNIEnv *env, jobject *pobj); +jvmtiEnv* getSpecialJvmti(void); + #endif