8227269: Slow class loading when running with JDWP
Reviewed-by: sspitsyn, cjplummer
This commit is contained in:
parent
c9f5004bae
commit
1dd60a35d1
@ -22,273 +22,204 @@
|
|||||||
* or visit www.oracle.com if you need additional information or have any
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
* questions.
|
* questions.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This module tracks classes that have been prepared, so as to
|
* This module tracks classes that have been prepared, so as to
|
||||||
* be able to compute which have been unloaded. On VM start-up
|
* be able to report which have been unloaded. On VM start-up
|
||||||
* all prepared classes are put in a table. As class prepare
|
* and whenever new classes are loaded, all prepared classes'
|
||||||
* events come in they are added to the table. After an unload
|
* signatures are attached as JVMTI tag to the class object.
|
||||||
* event or series of them, the VM can be asked for the list
|
* Class unloading is tracked by registering
|
||||||
* of classes; this list is compared against the table keep by
|
* ObjectFree callback on class objects. When this happens, we find
|
||||||
* this module, any classes no longer present are known to
|
* the signature of the unloaded class(es) and report them back
|
||||||
* have been unloaded.
|
* to the event handler to synthesize class-unload-events.
|
||||||
*
|
|
||||||
* For efficient access, classes are keep in a hash table.
|
|
||||||
* Each slot in the hash table has a linked list of KlassNode.
|
|
||||||
*
|
|
||||||
* Comparing current set of classes is compared with previous
|
|
||||||
* set by transferring all classes in the current set into
|
|
||||||
* a new table, any that remain in the old table have been
|
|
||||||
* unloaded.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "bag.h"
|
#include "bag.h"
|
||||||
#include "classTrack.h"
|
#include "classTrack.h"
|
||||||
|
|
||||||
/* ClassTrack hash table slot count */
|
#define NOT_TAGGED 0
|
||||||
#define CT_HASH_SLOT_COUNT 263 /* Prime which eauals 4k+3 for some k */
|
|
||||||
|
|
||||||
typedef struct KlassNode {
|
|
||||||
jclass klass; /* weak global reference */
|
|
||||||
char *signature; /* class signature */
|
|
||||||
struct KlassNode *next; /* next node in this slot */
|
|
||||||
} KlassNode;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Hash table of prepared classes. Each entry is a pointer
|
* The JVMTI tracking env to keep track of klass tags for class-unloads
|
||||||
* to a linked list of KlassNode.
|
|
||||||
*/
|
*/
|
||||||
static KlassNode **table;
|
static jvmtiEnv* trackingEnv;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Return slot in hash table to use for this class.
|
* A bag containing all the deleted classes' signatures. Must be accessed under
|
||||||
|
* classTrackLock.
|
||||||
*/
|
*/
|
||||||
static jint
|
struct bag* deletedSignatures;
|
||||||
hashKlass(jclass klass)
|
|
||||||
|
/*
|
||||||
|
* Lock to keep integrity of deletedSignatures.
|
||||||
|
*/
|
||||||
|
static jrawMonitorID classTrackLock;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Invoke the callback when classes are freed, find and record the signature
|
||||||
|
* in deletedSignatures. Those are only used in addPreparedClass() by the
|
||||||
|
* same thread.
|
||||||
|
*/
|
||||||
|
static void JNICALL
|
||||||
|
cbTrackingObjectFree(jvmtiEnv* jvmti_env, jlong tag)
|
||||||
{
|
{
|
||||||
jint hashCode = objectHashCode(klass);
|
debugMonitorEnter(classTrackLock);
|
||||||
return abs(hashCode) % CT_HASH_SLOT_COUNT;
|
if (deletedSignatures == NULL) {
|
||||||
|
debugMonitorExit(classTrackLock);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
*(char**)bagAdd(deletedSignatures) = (char*)tag;
|
||||||
|
|
||||||
|
debugMonitorExit(classTrackLock);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Transfer a node (which represents klass) from the current
|
* Called after class unloads have occurred.
|
||||||
* table to the new table.
|
* The signatures of classes which were unloaded are returned.
|
||||||
*/
|
|
||||||
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.
|
|
||||||
*/
|
*/
|
||||||
struct bag *
|
struct bag *
|
||||||
classTrack_processUnloads(JNIEnv *env)
|
classTrack_processUnloads(JNIEnv *env)
|
||||||
{
|
{
|
||||||
KlassNode **newTable;
|
debugMonitorEnter(classTrackLock);
|
||||||
struct bag *unloadedSignatures;
|
if (deletedSignatures == NULL) {
|
||||||
|
// Class tracking not initialized, nobody's interested.
|
||||||
unloadedSignatures = NULL;
|
debugMonitorExit(classTrackLock);
|
||||||
newTable = jvmtiAllocate(CT_HASH_SLOT_COUNT * sizeof(KlassNode *));
|
return NULL;
|
||||||
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)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
struct bag* deleted = deletedSignatures;
|
||||||
return unloadedSignatures;
|
deletedSignatures = bagCreateBag(sizeof(char*), 10);
|
||||||
|
debugMonitorExit(classTrackLock);
|
||||||
|
return deleted;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Add a class to the prepared class hash table.
|
* Add a class to the prepared class table.
|
||||||
* Assumes no duplicates.
|
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
classTrack_addPreparedClass(JNIEnv *env, jclass klass)
|
classTrack_addPreparedClass(JNIEnv *env_unused, jclass klass)
|
||||||
{
|
{
|
||||||
jint slot = hashKlass(klass);
|
|
||||||
KlassNode **head = &table[slot];
|
|
||||||
KlassNode *node;
|
|
||||||
jvmtiError error;
|
jvmtiError error;
|
||||||
|
jvmtiEnv* env = trackingEnv;
|
||||||
|
|
||||||
if (gdata->assertOn) {
|
if (gdata && gdata->assertOn) {
|
||||||
/* Check this is not a duplicate */
|
// Check this is not already tagged.
|
||||||
for (node = *head; node != NULL; node = node->next) {
|
jlong tag;
|
||||||
if (isSameObject(env, klass, node->klass)) {
|
error = JVMTI_FUNC_PTR(trackingEnv, GetTag)(env, klass, &tag);
|
||||||
JDI_ASSERT_FAILED("Attempting to insert duplicate class");
|
if (error != JVMTI_ERROR_NONE) {
|
||||||
break;
|
EXIT_ERROR(error, "Unable to GetTag with class trackingEnv");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
JDI_ASSERT(tag == NOT_TAGGED);
|
||||||
}
|
}
|
||||||
|
|
||||||
node = jvmtiAllocate(sizeof(KlassNode));
|
char* signature;
|
||||||
if (node == NULL) {
|
error = classSignature(klass, &signature, NULL);
|
||||||
EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"KlassNode");
|
|
||||||
}
|
|
||||||
error = classSignature(klass, &(node->signature), NULL);
|
|
||||||
if (error != JVMTI_ERROR_NONE) {
|
if (error != JVMTI_ERROR_NONE) {
|
||||||
jvmtiDeallocate(node);
|
|
||||||
EXIT_ERROR(error,"signature");
|
EXIT_ERROR(error,"signature");
|
||||||
}
|
}
|
||||||
if ((node->klass = JNI_FUNC_PTR(env,NewWeakGlobalRef)(env, klass)) == NULL) {
|
error = JVMTI_FUNC_PTR(trackingEnv, SetTag)(env, klass, (jlong)signature);
|
||||||
jvmtiDeallocate(node->signature);
|
if (error != JVMTI_ERROR_NONE) {
|
||||||
jvmtiDeallocate(node);
|
jvmtiDeallocate(signature);
|
||||||
EXIT_ERROR(AGENT_ERROR_NULL_POINTER,"NewWeakGlobalRef");
|
EXIT_ERROR(error,"SetTag");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Insert the new node */
|
static jboolean
|
||||||
node->next = *head;
|
setupEvents()
|
||||||
*head = node;
|
{
|
||||||
|
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
|
void
|
||||||
classTrack_initialize(JNIEnv *env)
|
classTrack_initialize(JNIEnv *env)
|
||||||
{
|
{
|
||||||
WITH_LOCAL_REFS(env, 1) {
|
deletedSignatures = NULL;
|
||||||
|
classTrackLock = debugMonitorCreate("Deleted class tag lock");
|
||||||
|
trackingEnv = getSpecialJvmti();
|
||||||
|
if (trackingEnv == NULL) {
|
||||||
|
EXIT_ERROR(AGENT_ERROR_INTERNAL, "Failed to allocate tag-tracking jvmtiEnv");
|
||||||
|
}
|
||||||
|
|
||||||
jint classCount;
|
|
||||||
jclass *classes;
|
|
||||||
jvmtiError error;
|
|
||||||
jint i;
|
|
||||||
|
|
||||||
error = allLoadedClasses(&classes, &classCount);
|
if (!setupEvents()) {
|
||||||
if ( error == JVMTI_ERROR_NONE ) {
|
EXIT_ERROR(AGENT_ERROR_INTERNAL, "Unable to setup ObjectFree tracking");
|
||||||
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++) {
|
|
||||||
jclass klass = classes[i];
|
|
||||||
jint status;
|
|
||||||
jint wanted =
|
|
||||||
(JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY);
|
|
||||||
|
|
||||||
/* We only want prepared classes and arrays */
|
jint classCount;
|
||||||
status = classStatus(klass);
|
jclass *classes;
|
||||||
if ( (status & wanted) != 0 ) {
|
jvmtiError error;
|
||||||
classTrack_addPreparedClass(env, klass);
|
jint i;
|
||||||
}
|
|
||||||
}
|
error = allLoadedClasses(&classes, &classCount);
|
||||||
} else {
|
if ( error == JVMTI_ERROR_NONE ) {
|
||||||
jvmtiDeallocate(classes);
|
for (i = 0; i < classCount; i++) {
|
||||||
EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"KlassNode");
|
jclass klass = classes[i];
|
||||||
|
jint status;
|
||||||
|
jint wanted = JVMTI_CLASS_STATUS_PREPARED | JVMTI_CLASS_STATUS_ARRAY;
|
||||||
|
status = classStatus(klass);
|
||||||
|
if ((status & wanted) != 0) {
|
||||||
|
classTrack_addPreparedClass(env, klass);
|
||||||
}
|
}
|
||||||
jvmtiDeallocate(classes);
|
|
||||||
} else {
|
|
||||||
EXIT_ERROR(error,"loaded classes array");
|
|
||||||
}
|
}
|
||||||
|
jvmtiDeallocate(classes);
|
||||||
} END_WITH_LOCAL_REFS(env)
|
} else {
|
||||||
|
EXIT_ERROR(error,"loaded classes array");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Called to activate class-tracking when a listener registers for EI_GC_FINISH.
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
classTrack_activate(JNIEnv *env)
|
||||||
|
{
|
||||||
|
debugMonitorEnter(classTrackLock);
|
||||||
|
deletedSignatures = bagCreateBag(sizeof(char*), 1000);
|
||||||
|
debugMonitorExit(classTrackLock);
|
||||||
|
}
|
||||||
|
|
||||||
|
static jboolean
|
||||||
|
cleanDeleted(void *signatureVoid, void *arg)
|
||||||
|
{
|
||||||
|
char* sig = *(char**)signatureVoid;
|
||||||
|
jvmtiDeallocate(sig);
|
||||||
|
return JNI_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Called when agent detaches.
|
||||||
|
*/
|
||||||
void
|
void
|
||||||
classTrack_reset(void)
|
classTrack_reset(void)
|
||||||
{
|
{
|
||||||
|
debugMonitorEnter(classTrackLock);
|
||||||
|
|
||||||
|
if (deletedSignatures != NULL) {
|
||||||
|
bagEnumerateOver(deletedSignatures, cleanDeleted, NULL);
|
||||||
|
bagDestroyBag(deletedSignatures);
|
||||||
|
deletedSignatures = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
debugMonitorExit(classTrackLock);
|
||||||
}
|
}
|
||||||
|
@ -45,6 +45,12 @@ classTrack_addPreparedClass(JNIEnv *env, jclass klass);
|
|||||||
void
|
void
|
||||||
classTrack_initialize(JNIEnv *env);
|
classTrack_initialize(JNIEnv *env);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Activates class tracking.
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
classTrack_activate(JNIEnv *env);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Reset class tracking.
|
* Reset class tracking.
|
||||||
*/
|
*/
|
||||||
|
@ -1625,6 +1625,9 @@ installHandler(HandlerNode *node,
|
|||||||
|
|
||||||
node->handlerID = external? ++requestIdCounter : 0;
|
node->handlerID = external? ++requestIdCounter : 0;
|
||||||
error = eventFilterRestricted_install(node);
|
error = eventFilterRestricted_install(node);
|
||||||
|
if (node->ei == EI_GC_FINISH) {
|
||||||
|
classTrack_activate(getEnv());
|
||||||
|
}
|
||||||
if (error == JVMTI_ERROR_NONE) {
|
if (error == JVMTI_ERROR_NONE) {
|
||||||
insert(getHandlerChain(node->ei), node);
|
insert(getHandlerChain(node->ei), node);
|
||||||
}
|
}
|
||||||
|
@ -1742,7 +1742,7 @@ isMethodObsolete(jmethodID method)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Get the jvmti environment to be used with tags */
|
/* Get the jvmti environment to be used with tags */
|
||||||
static jvmtiEnv *
|
jvmtiEnv *
|
||||||
getSpecialJvmti(void)
|
getSpecialJvmti(void)
|
||||||
{
|
{
|
||||||
jvmtiEnv *jvmti;
|
jvmtiEnv *jvmti;
|
||||||
|
@ -414,4 +414,6 @@ void createLocalRefSpace(JNIEnv *env, jint capacity);
|
|||||||
void saveGlobalRef(JNIEnv *env, jobject obj, jobject *pobj);
|
void saveGlobalRef(JNIEnv *env, jobject obj, jobject *pobj);
|
||||||
void tossGlobalRef(JNIEnv *env, jobject *pobj);
|
void tossGlobalRef(JNIEnv *env, jobject *pobj);
|
||||||
|
|
||||||
|
jvmtiEnv* getSpecialJvmti(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user