< prev index next >

src/jdk.jdwp.agent/share/native/libjdwp/classTrack.c

Print this page
rev 57742 : 8227269: Slow class loading when running with JDWP

@@ -43,252 +43,275 @@
 
 #include "util.h"
 #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;
+static KlassNode** table;
 
 /*
- * Return slot in hash table to use for this class.
+ * The JVMTI tracking env to keep track of klass tags, for class-unloads
  */
-static jint
-hashKlass(jclass klass)
-{
-    jint hashCode = objectHashCode(klass);
-    return abs(hashCode) % CT_HASH_SLOT_COUNT;
-}
+static jvmtiEnv* trackingEnv;
 
 /*
- * Transfer a node (which represents klass) from the current
- * table to the new table.
+ * The current highest tag number
  */
-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 */
+static jlong currentClassTag;
 
-            /* unlink from old list */
-            *nodePtr = node->next;
-
-            /* insert in new list */
-            node->next = *newHead;
-            *newHead = node;
-
-            return;
-        }
-    }
+/*
+ * Lock to keep table, currentClassTag and deletedSignatureBag consistent
+ */
+static jrawMonitorID deletedSignatureLock;
 
-    /* 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.
+/*
+ * A bag containing all the deleted classes' signatures. Must be accessed under
+ * deletedSignatureLock.
      */
-/**** 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));
-***/
-}
+struct bag* deletedSignatureBag;
 
 /*
- * Delete a hash table of classes.
- * The signatures of classes in the table are returned.
+ * Callback when classes are freed, Finds the signature and remembers it in deletedSignatureBag.
  */
-static struct bag *
-deleteTable(JNIEnv *env, KlassNode *oldTable[])
+static void JNICALL
+cbTrackingObjectFree(jvmtiEnv* jvmti_env, jlong tag)
 {
-    struct bag *signatures = bagCreateBag(sizeof(char*), 10);
-    jint slot;
-
-    if (signatures == NULL) {
-        EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"signatures");
+    debugMonitorEnter(deletedSignatureLock);
+    if (currentClassTag == -1) {
+      // Class tracking not initialized, nobody's interested
+     debugMonitorExit(deletedSignatureLock);
+     return;
     }
 
-    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;
+    // Find deleted KlassNode
+    size_t idx = tag % CT_SLOT_COUNT;
+    KlassNode** klass_ptr = &table[idx];
+    KlassNode* klass = *klass_ptr;
+
+    // Scan linked-list.
+    while (klass != NULL && klass->klass_tag != tag) {
+        klass_ptr = &klass->next;
+        klass = *klass_ptr;
         }
+    if (klass != NULL || klass->klass_tag != tag) { // klass not found - ignore.
+        debugMonitorExit(deletedSignatureLock);
+        return;
     }
-    jvmtiDeallocate(oldTable);
 
-    return signatures;
+    // 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);
 }
 
 /*
- * 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; i<classCount; i++) {
-                    jclass klass = classes[i];
-                    transferClass(env, klass, newTable);
-                }
-                jvmtiDeallocate(classes);
-
-                /* Delete old table, install new one */
-                unloadedSignatures = deleteTable(env, table);
-                table = newTable;
-            }
-
-        } END_WITH_LOCAL_REFS(env)
-
-    }
-
-    return unloadedSignatures;
+    debugMonitorEnter(deletedSignatureLock);
+    if (currentClassTag == -1) {
+        // Class tracking not initialized, nobody's interested
+        debugMonitorExit(deletedSignatureLock);
+        return bagCreateBag(sizeof(char*), 0);
+    }
+    struct bag* deleted = deletedSignatureBag;
+    deletedSignatureBag = bagCreateBag(sizeof(char*), 10);
+    debugMonitorExit(deletedSignatureLock);
+    return deleted;
 }
 
 /*
- * Add a class to the prepared class hash table.
- * Assumes no duplicates.
+ * Add a class to the prepared class table.
  */
 void
 classTrack_addPreparedClass(JNIEnv *env, jclass klass)
 {
-    jint slot = hashKlass(klass);
-    KlassNode **head = &table[slot];
-    KlassNode *node;
     jvmtiError error;
 
-    if (gdata->assertOn) {
-        /* 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;
+    debugMonitorEnter(deletedSignatureLock);
+    if (currentClassTag == -1) {
+      // Class tracking not initialized yet, nobody's interested
+     debugMonitorExit(deletedSignatureLock);
+     return;
             }
+
+    /* Check this is not a duplicate */
+    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) {
+      debugMonitorExit(deletedSignatureLock);
+      return; // Already added
     }
 
-    node = jvmtiAllocate(sizeof(KlassNode));
+    KlassNode* node = jvmtiAllocate(sizeof(KlassNode));
     if (node == NULL) {
         EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"KlassNode");
     }
     error = classSignature(klass, &(node->signature), NULL);
     if (error != JVMTI_ERROR_NONE) {
         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)
 {
+    deletedSignatureLock = debugMonitorCreate("Deleted class tag lock");
+    currentClassTag = -1l;
+    table = NULL;
+    trackingEnv = NULL;
+    deletedSignatureBag = NULL;
+}
+
+/*
+ * Called to activate class-tracking when a listener registers for EI_GC_FINISH.
+ */
+void
+classTrack_activate(JNIEnv *env)
+{
+    trackingEnv = getSpecialJvmti();
+    if (trackingEnv == NULL) {
+        EXIT_ERROR(AGENT_ERROR_INTERNAL, "Failed to allocate tag-tracking jvmtiEnv");
+    }
+
+    deletedSignatureBag = bagCreateBag(sizeof(char*), 10);
+
+    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;
         jclass *classes;
         jvmtiError error;
         jint i;
 
         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; i<classCount; i++) {
+            for (i = 0; i < classCount; i++) {
                     jclass klass = classes[i];
                     jint status;
-                    jint wanted =
-                        (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY);
-
-                    /* We only want prepared classes and arrays */
+                jint wanted = JVMTI_CLASS_STATUS_PREPARED | JVMTI_CLASS_STATUS_ARRAY;
                     status = classStatus(klass);
-                    if ( (status & wanted) != 0 ) {
+                if ((status & wanted) != 0) {
                         classTrack_addPreparedClass(env, klass);
                     }
                 }
-            } else {
-                jvmtiDeallocate(classes);
-                EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"KlassNode");
-            }
             jvmtiDeallocate(classes);
         } else {
             EXIT_ERROR(error,"loaded classes array");
         }
 
     } END_WITH_LOCAL_REFS(env)
+}
 
+static jboolean
+cleanDeleted(void *signatureVoid, void *arg)
+{
+  char* sig = (char*)signatureVoid;
+  jvmtiDeallocate(sig);
+  return JNI_TRUE;
 }
 
+/*
+ * Called when agent detaches.
+ */
 void
 classTrack_reset(void)
 {
+  int idx;
+  debugMonitorEnter(deletedSignatureLock);
+
+  for (idx = 0; idx < CT_SLOT_COUNT; ++idx) {
+    KlassNode* node = table[idx];
+    while (node != NULL) {
+      KlassNode* next = node->next;
+      jvmtiDeallocate(node->signature);
+      jvmtiDeallocate(node);
+      node = next;
+    }
+  }
+  jvmtiDeallocate(table);
+
+  bagEnumerateOver(deletedSignatureBag, cleanDeleted, NULL);
+  bagDestroyBag(deletedSignatureBag);
+
+  currentClassTag = -1;
+
+  (void)JVMTI_FUNC_PTR(trackingEnv,DisposeEnvironment)(trackingEnv);
+  trackingEnv = NULL;
+
+  debugMonitorExit(deletedSignatureLock);
 }
< prev index next >