< 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


  28  * all prepared classes are put in a table.  As class prepare
  29  * events come in they are added to the table.  After an unload
  30  * event or series of them, the VM can be asked for the list
  31  * of classes; this list is compared against the table keep by
  32  * this module, any classes no longer present are known to
  33  * have been unloaded.
  34  *
  35  * For efficient access, classes are keep in a hash table.
  36  * Each slot in the hash table has a linked list of KlassNode.
  37  *
  38  * Comparing current set of classes is compared with previous
  39  * set by transferring all classes in the current set into
  40  * a new table, any that remain in the old table have been
  41  * unloaded.
  42  */
  43 
  44 #include "util.h"
  45 #include "bag.h"
  46 #include "classTrack.h"
  47 
  48 /* ClassTrack hash table slot count */
  49 #define CT_HASH_SLOT_COUNT 263    /* Prime which eauals 4k+3 for some k */
  50 
  51 typedef struct KlassNode {
  52     jclass klass;            /* weak global reference */
  53     char *signature;         /* class signature */
  54     struct KlassNode *next;  /* next node in this slot */
  55 } KlassNode;
  56 
  57 /*
  58  * Hash table of prepared classes.  Each entry is a pointer
  59  * to a linked list of KlassNode.
  60  */
  61 static KlassNode **table;
  62 
  63 /*
  64  * Return slot in hash table to use for this class.
  65  */
  66 static jint
  67 hashKlass(jclass klass)
  68 {
  69     jint hashCode = objectHashCode(klass);
  70     return abs(hashCode) % CT_HASH_SLOT_COUNT;
  71 }
  72 
  73 /*
  74  * Transfer a node (which represents klass) from the current
  75  * table to the new table.
  76  */
  77 static void
  78 transferClass(JNIEnv *env, jclass klass, KlassNode **newTable) {
  79     jint slot = hashKlass(klass);
  80     KlassNode **head = &table[slot];
  81     KlassNode **newHead = &newTable[slot];
  82     KlassNode **nodePtr;
  83     KlassNode *node;
  84 
  85     /* Search the node list of the current table for klass */
  86     for (nodePtr = head; node = *nodePtr, node != NULL; nodePtr = &(node->next)) {
  87         if (isSameObject(env, klass, node->klass)) {
  88             /* Match found transfer node */
  89 
  90             /* unlink from old list */
  91             *nodePtr = node->next;
  92 
  93             /* insert in new list */
  94             node->next = *newHead;
  95             *newHead = node;
  96 
  97             return;
  98         }
  99     }
 100 
 101     /* we haven't found the class, only unloads should have happenned,
 102      * so the only reason a class should not have been found is
 103      * that it is not prepared yet, in which case we don't want it.
 104      * Asset that the above is true.
 105      */
 106 /**** the HotSpot VM doesn't create prepare events for some internal classes ***
 107     JDI_ASSERT_MSG((classStatus(klass) &
 108                 (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY))==0,
 109                classSignature(klass));
 110 ***/
 111 }
 112 
 113 /*
 114  * Delete a hash table of classes.
 115  * The signatures of classes in the table are returned.
 116  */
 117 static struct bag *
 118 deleteTable(JNIEnv *env, KlassNode *oldTable[])
 119 {
 120     struct bag *signatures = bagCreateBag(sizeof(char*), 10);
 121     jint slot;
 122 
 123     if (signatures == NULL) {
 124         EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"signatures");
 125     }
 126 
 127     for (slot = 0; slot < CT_HASH_SLOT_COUNT; slot++) {
 128         KlassNode *node = oldTable[slot];
 129 
 130         while (node != NULL) {
 131             KlassNode *next;
 132             char **sigSpot;
 133 
 134             /* Add signature to the signature bag */
 135             sigSpot = bagAdd(signatures);
 136             if (sigSpot == NULL) {
 137                 EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"signature bag");
 138             }
 139             *sigSpot = node->signature;
 140 
 141             /* Free weak ref and the node itself */
 142             JNI_FUNC_PTR(env,DeleteWeakGlobalRef)(env, node->klass);
 143             next = node->next;
 144             jvmtiDeallocate(node);
 145 
 146             node = next;
 147         }



 148     }
 149     jvmtiDeallocate(oldTable);
 150 
 151     return signatures;










 152 }
 153 
 154 /*
 155  * Called after class unloads have occurred.  Creates a new hash table
 156  * of currently loaded prepared classes.
 157  * The signatures of classes which were unloaded (not present in the
 158  * new table) are returned.
 159  */
 160 struct bag *
 161 classTrack_processUnloads(JNIEnv *env)
 162 {
 163     KlassNode **newTable;
 164     struct bag *unloadedSignatures;
 165 
 166     unloadedSignatures = NULL;
 167     newTable = jvmtiAllocate(CT_HASH_SLOT_COUNT * sizeof(KlassNode *));
 168     if (newTable == NULL) {
 169         EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY, "classTrack table");
 170     } else {
 171 
 172         (void)memset(newTable, 0, CT_HASH_SLOT_COUNT * sizeof(KlassNode *));
 173 
 174         WITH_LOCAL_REFS(env, 1) {
 175 
 176             jint classCount;
 177             jclass *classes;
 178             jvmtiError error;
 179             int i;
 180 
 181             error = allLoadedClasses(&classes, &classCount);
 182             if ( error != JVMTI_ERROR_NONE ) {
 183                 jvmtiDeallocate(newTable);
 184                 EXIT_ERROR(error,"loaded classes");
 185             } else {
 186 
 187                 /* Transfer each current class into the new table */
 188                 for (i=0; i<classCount; i++) {
 189                     jclass klass = classes[i];
 190                     transferClass(env, klass, newTable);
 191                 }
 192                 jvmtiDeallocate(classes);
 193 
 194                 /* Delete old table, install new one */
 195                 unloadedSignatures = deleteTable(env, table);
 196                 table = newTable;
 197             }
 198 
 199         } END_WITH_LOCAL_REFS(env)
 200 
 201     }
 202 
 203     return unloadedSignatures;
 204 }
 205 
 206 /*
 207  * Add a class to the prepared class hash table.
 208  * Assumes no duplicates.
 209  */
 210 void
 211 classTrack_addPreparedClass(JNIEnv *env, jclass klass)
 212 {
 213     jint slot = hashKlass(klass);
 214     KlassNode **head = &table[slot];
 215     KlassNode *node;
 216     jvmtiError error;
 217 
 218     if (gdata->assertOn) {
 219         /* Check this is not a duplicate */
 220         for (node = *head; node != NULL; node = node->next) {
 221             if (isSameObject(env, klass, node->klass)) {
 222                 JDI_ASSERT_FAILED("Attempting to insert duplicate class");
 223                 break;
 224             }






 225         }



 226     }
 227 
 228     node = jvmtiAllocate(sizeof(KlassNode));
 229     if (node == NULL) {
 230         EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"KlassNode");
 231     }
 232     error = classSignature(klass, &(node->signature), NULL);
 233     if (error != JVMTI_ERROR_NONE) {
 234         jvmtiDeallocate(node);
 235         EXIT_ERROR(error,"signature");
 236     }
 237     if ((node->klass = JNI_FUNC_PTR(env,NewWeakGlobalRef)(env, klass)) == NULL) {


 238         jvmtiDeallocate(node->signature);
 239         jvmtiDeallocate(node);
 240         EXIT_ERROR(AGENT_ERROR_NULL_POINTER,"NewWeakGlobalRef");
 241     }
 242 
 243     /* Insert the new node */
 244     node->next = *head;
 245     *head = node;


























 246 }
 247 
 248 /*
 249  * Called once to build the initial prepared class hash table.
 250  */
 251 void
 252 classTrack_initialize(JNIEnv *env)
 253 {































 254     WITH_LOCAL_REFS(env, 1) {
 255 
 256         jint classCount;
 257         jclass *classes;
 258         jvmtiError error;
 259         jint i;
 260 
 261         error = allLoadedClasses(&classes, &classCount);
 262         if ( error == JVMTI_ERROR_NONE ) {
 263             table = jvmtiAllocate(CT_HASH_SLOT_COUNT * sizeof(KlassNode *));
 264             if (table != NULL) {
 265                 (void)memset(table, 0, CT_HASH_SLOT_COUNT * sizeof(KlassNode *));
 266                 for (i=0; i<classCount; i++) {
 267                     jclass klass = classes[i];
 268                     jint status;
 269                     jint wanted =
 270                         (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY);
 271 
 272                     /* We only want prepared classes and arrays */
 273                     status = classStatus(klass);
 274                     if ( (status & wanted) != 0 ) {
 275                         classTrack_addPreparedClass(env, klass);
 276                     }
 277                 }
 278             } else {
 279                 jvmtiDeallocate(classes);
 280                 EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"KlassNode");
 281             }
 282             jvmtiDeallocate(classes);
 283         } else {
 284             EXIT_ERROR(error,"loaded classes array");
 285         }
 286 
 287     } END_WITH_LOCAL_REFS(env)

 288 






 289 }
 290 



 291 void
 292 classTrack_reset(void)
 293 {























 294 }


  28  * all prepared classes are put in a table.  As class prepare
  29  * events come in they are added to the table.  After an unload
  30  * event or series of them, the VM can be asked for the list
  31  * of classes; this list is compared against the table keep by
  32  * this module, any classes no longer present are known to
  33  * have been unloaded.
  34  *
  35  * For efficient access, classes are keep in a hash table.
  36  * Each slot in the hash table has a linked list of KlassNode.
  37  *
  38  * Comparing current set of classes is compared with previous
  39  * set by transferring all classes in the current set into
  40  * a new table, any that remain in the old table have been
  41  * unloaded.
  42  */
  43 
  44 #include "util.h"
  45 #include "bag.h"
  46 #include "classTrack.h"
  47 
  48 /* ClassTrack table slot count */
  49 #define CT_SLOT_COUNT 263    /* Prime which eauals 4k+3 for some k */
  50 
  51 typedef struct KlassNode {
  52     jlong klass_tag;         /* Klass's tag in tracking env */
  53     char *signature;         /* class signature */
  54     struct KlassNode *next;  /* next node in this slot */
  55 } KlassNode;
  56 
  57 /*
  58  * Table mapping tag % CT_SLOT_COUNT to linked-list of KlassNode*.

  59  */
  60 static KlassNode** table;
  61 
  62 /*
  63  * The JVMTI tracking env to keep track of klass tags, for class-unloads
  64  */
  65 static jvmtiEnv* trackingEnv;





  66 
  67 /*
  68  * The current highest tag number

  69  */
  70 static jlong currentClassTag;











  71 
  72 /*
  73  * Lock to keep table, currentClassTag and deletedSignatureBag consistent
  74  */
  75 static jrawMonitorID deletedSignatureLock;






  76 
  77 /*
  78  * A bag containing all the deleted classes' signatures. Must be accessed under
  79  * deletedSignatureLock.

  80  */
  81 struct bag* deletedSignatureBag;





  82 
  83 /*
  84  * Callback when classes are freed, Finds the signature and remembers it in deletedSignatureBag.

  85  */
  86 static void JNICALL
  87 cbTrackingObjectFree(jvmtiEnv* jvmti_env, jlong tag)
  88 {
  89     debugMonitorEnter(deletedSignatureLock);
  90     if (currentClassTag == -1) {
  91       // Class tracking not initialized, nobody's interested
  92      debugMonitorExit(deletedSignatureLock);
  93      return;
  94     }
  95 
  96     // Find deleted KlassNode
  97     size_t idx = tag % CT_SLOT_COUNT;
  98     KlassNode** klass_ptr = &table[idx];
  99     KlassNode* klass = *klass_ptr;
 100 
 101     // Scan linked-list.
 102     while (klass != NULL && klass->klass_tag != tag) {
 103         klass_ptr = &klass->next;
 104         klass = *klass_ptr;











 105     }
 106     if (klass != NULL || klass->klass_tag != tag) { // klass not found - ignore.
 107         debugMonitorExit(deletedSignatureLock);
 108         return;
 109     }

 110 
 111     // At this point we have the KlassNode corresponding to the tag
 112     // in klass, and the pointer to it in klass_node.
 113     // Remember the unloaded signature.
 114     *(char**)bagAdd(deletedSignatureBag) = klass->signature;
 115 
 116     // Unlink the KlassNode.
 117     *klass_ptr = klass->next;
 118     jvmtiDeallocate(klass);
 119 
 120     // Done.
 121     debugMonitorExit(deletedSignatureLock);
 122 }
 123 
 124 /*
 125  * Called after class unloads have occurred.
 126  * The signatures of classes which were unloaded are returned.


 127  */
 128 struct bag *
 129 classTrack_processUnloads(JNIEnv *env)
 130 {
 131     debugMonitorEnter(deletedSignatureLock);
 132     if (currentClassTag == -1) {
 133         // Class tracking not initialized, nobody's interested
 134         debugMonitorExit(deletedSignatureLock);
 135         return bagCreateBag(sizeof(char*), 0);
 136     }
 137     struct bag* deleted = deletedSignatureBag;
 138     deletedSignatureBag = bagCreateBag(sizeof(char*), 10);
 139     debugMonitorExit(deletedSignatureLock);
 140     return deleted;































 141 }
 142 
 143 /*
 144  * Add a class to the prepared class table.

 145  */
 146 void
 147 classTrack_addPreparedClass(JNIEnv *env, jclass klass)
 148 {



 149     jvmtiError error;
 150 
 151     debugMonitorEnter(deletedSignatureLock);
 152     if (currentClassTag == -1) {
 153       // Class tracking not initialized yet, nobody's interested
 154      debugMonitorExit(deletedSignatureLock);
 155      return;

 156     }
 157 
 158     /* Check this is not a duplicate */
 159     jlong tag;
 160     error = JVMTI_FUNC_PTR(trackingEnv, GetTag)(trackingEnv, klass, &tag);
 161     if (error != JVMTI_ERROR_NONE) {
 162       EXIT_ERROR(error, "Unable to GetTag with class trackingEnv");
 163     }
 164     if (tag != 0l) {
 165       debugMonitorExit(deletedSignatureLock);
 166       return; // Already added
 167     }
 168 
 169     KlassNode* node = jvmtiAllocate(sizeof(KlassNode));
 170     if (node == NULL) {
 171         EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"KlassNode");
 172     }
 173     error = classSignature(klass, &(node->signature), NULL);
 174     if (error != JVMTI_ERROR_NONE) {
 175         jvmtiDeallocate(node);
 176         EXIT_ERROR(error,"signature");
 177     }
 178     node->klass_tag = ++currentClassTag;
 179     error = JVMTI_FUNC_PTR(trackingEnv, SetTag)(trackingEnv, klass, node->klass_tag);
 180     if (error != JVMTI_ERROR_NONE) {
 181         jvmtiDeallocate(node->signature);
 182         jvmtiDeallocate(node);
 183         EXIT_ERROR(error,"SetTag");
 184     }
 185 
 186     /* Insert the new node */
 187     size_t idx = node->klass_tag % CT_SLOT_COUNT;
 188     node->next = table[idx];
 189     table[idx] = node;
 190     debugMonitorExit(deletedSignatureLock);
 191 }
 192 
 193 static jboolean
 194 setupEvents()
 195 {
 196     jvmtiCapabilities caps;
 197     memset(&caps, 0, sizeof(caps));
 198     caps.can_generate_object_free_events = 1;
 199     jvmtiError error = JVMTI_FUNC_PTR(trackingEnv, AddCapabilities)(trackingEnv, &caps);
 200     if (error != JVMTI_ERROR_NONE) {
 201         return JNI_FALSE;
 202     }
 203     jvmtiEventCallbacks cb;
 204     memset(&cb, 0, sizeof(cb));
 205     cb.ObjectFree = cbTrackingObjectFree;
 206     error = JVMTI_FUNC_PTR(trackingEnv, SetEventCallbacks)(trackingEnv, &cb, sizeof(cb));
 207     if (error != JVMTI_ERROR_NONE) {
 208         return JNI_FALSE;
 209     }
 210     error = JVMTI_FUNC_PTR(trackingEnv, SetEventNotificationMode)(trackingEnv, JVMTI_ENABLE, JVMTI_EVENT_OBJECT_FREE, NULL);
 211     if (error != JVMTI_ERROR_NONE) {
 212         return JNI_FALSE;
 213     }
 214     return JNI_TRUE;
 215 }
 216 
 217 /*
 218  * Called once to initialize class-tracking.
 219  */
 220 void
 221 classTrack_initialize(JNIEnv *env)
 222 {
 223     deletedSignatureLock = debugMonitorCreate("Deleted class tag lock");
 224     currentClassTag = -1l;
 225     table = NULL;
 226     trackingEnv = NULL;
 227     deletedSignatureBag = NULL;
 228 }
 229 
 230 /*
 231  * Called to activate class-tracking when a listener registers for EI_GC_FINISH.
 232  */
 233 void
 234 classTrack_activate(JNIEnv *env)
 235 {
 236     trackingEnv = getSpecialJvmti();
 237     if (trackingEnv == NULL) {
 238         EXIT_ERROR(AGENT_ERROR_INTERNAL, "Failed to allocate tag-tracking jvmtiEnv");
 239     }
 240 
 241     deletedSignatureBag = bagCreateBag(sizeof(char*), 10);
 242 
 243     if (!setupEvents()) {
 244         EXIT_ERROR(AGENT_ERROR_INTERNAL, "Unable to setup ObjectFree tracking");
 245     }
 246     currentClassTag = 0l;
 247     table = jvmtiAllocate(CT_SLOT_COUNT * sizeof(KlassNode*));
 248     if (table != NULL) {
 249         (void)memset(table, 0, CT_SLOT_COUNT * sizeof(KlassNode*));
 250     } else {
 251         EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY, "failed allocating class-track table");
 252     }
 253 
 254     WITH_LOCAL_REFS(env, 1) {
 255 
 256         jint classCount;
 257         jclass *classes;
 258         jvmtiError error;
 259         jint i;
 260 
 261         error = allLoadedClasses(&classes, &classCount);
 262         if ( error == JVMTI_ERROR_NONE ) {
 263             for (i = 0; i < classCount; i++) {



 264                 jclass klass = classes[i];
 265                 jint status;
 266                 jint wanted = JVMTI_CLASS_STATUS_PREPARED | JVMTI_CLASS_STATUS_ARRAY;



 267                 status = classStatus(klass);
 268                 if ((status & wanted) != 0) {
 269                     classTrack_addPreparedClass(env, klass);
 270                 }
 271             }




 272             jvmtiDeallocate(classes);
 273         } else {
 274             EXIT_ERROR(error,"loaded classes array");
 275         }
 276 
 277     } END_WITH_LOCAL_REFS(env)
 278 }
 279 
 280 static jboolean
 281 cleanDeleted(void *signatureVoid, void *arg)
 282 {
 283   char* sig = (char*)signatureVoid;
 284   jvmtiDeallocate(sig);
 285   return JNI_TRUE;
 286 }
 287 
 288 /*
 289  * Called when agent detaches.
 290  */
 291 void
 292 classTrack_reset(void)
 293 {
 294   int idx;
 295   debugMonitorEnter(deletedSignatureLock);
 296 
 297   for (idx = 0; idx < CT_SLOT_COUNT; ++idx) {
 298     KlassNode* node = table[idx];
 299     while (node != NULL) {
 300       KlassNode* next = node->next;
 301       jvmtiDeallocate(node->signature);
 302       jvmtiDeallocate(node);
 303       node = next;
 304     }
 305   }
 306   jvmtiDeallocate(table);
 307 
 308   bagEnumerateOver(deletedSignatureBag, cleanDeleted, NULL);
 309   bagDestroyBag(deletedSignatureBag);
 310 
 311   currentClassTag = -1;
 312 
 313   (void)JVMTI_FUNC_PTR(trackingEnv,DisposeEnvironment)(trackingEnv);
 314   trackingEnv = NULL;
 315 
 316   debugMonitorExit(deletedSignatureLock);
 317 }
< prev index next >