hprof_site.c

来自「一个小公司要求给写的很简单的任务管理系统。」· C语言 代码 · 共 903 行 · 第 1/3 页

C
903
字号
static jintobjectReference(jvmtiHeapReferenceKind reference_kind,                   const jvmtiHeapReferenceInfo* reference_info,                   jlong class_tag, jlong size, jlong* tag_ptr,                   jlong* referrer_tag_ptr, jint length){    ObjectIndex   object_index;    jint          reference_index;    RefIndex      ref_index;    RefIndex      prev_ref_index;    ObjectIndex   referrer_object_index;    jlong         object_tag;    HPROF_ASSERT(tag_ptr!=NULL);    HPROF_ASSERT(class_tag!=(jlong)0);    HPROF_ASSERT(referrer_tag_ptr!=NULL);    HPROF_ASSERT((*referrer_tag_ptr)!=(jlong)0);    if ( class_tag == (jlong)0 || (*referrer_tag_ptr) == (jlong)0 ) {        /* We can't do anything with a class_tag==0, just skip it */        return JVMTI_VISIT_OBJECTS;    }       switch ( reference_kind ) {        case JVMTI_HEAP_REFERENCE_CLASS_LOADER:        case JVMTI_HEAP_REFERENCE_INTERFACE:        default:            /* Currently we don't need these */            return JVMTI_VISIT_OBJECTS;        case JVMTI_HEAP_REFERENCE_FIELD:        case JVMTI_HEAP_REFERENCE_STATIC_FIELD:            reference_index = reference_info->field.index;            break;        case JVMTI_HEAP_REFERENCE_ARRAY_ELEMENT:            reference_index = reference_info->array.index;            break;        case JVMTI_HEAP_REFERENCE_CONSTANT_POOL:            reference_index = reference_info->constant_pool.index;            break;        case JVMTI_HEAP_REFERENCE_SIGNERS:        case JVMTI_HEAP_REFERENCE_PROTECTION_DOMAIN:            reference_index = 0;            break;    }       /* We assume the referrer is tagged */    referrer_object_index = tag_extract((*referrer_tag_ptr));       /* Now check the referree */    object_tag = *tag_ptr;    if ( object_tag != (jlong)0 ) {        object_index = tag_extract(object_tag);    } else {        /* Create and set the tag. */        object_tag = make_new_tag(class_tag, size, gdata->system_trace_index,                                  gdata->unknown_thread_serial_num,                                   &object_index, NULL);        *tag_ptr   = object_tag;    }    HPROF_ASSERT(object_index!=0);    /* Save reference information */    prev_ref_index = object_get_references(referrer_object_index);    ref_index = reference_obj(prev_ref_index, reference_kind,                    object_index, reference_index, length);    object_set_references(referrer_object_index, ref_index);    return JVMTI_VISIT_OBJECTS;}/* FollowReferences heap_reference_callback */static jint JNICALL cbReference(jvmtiHeapReferenceKind reference_kind,                   const jvmtiHeapReferenceInfo* reference_info,                   jlong class_tag, jlong referrer_class_tag,                  jlong size, jlong* tag_ptr,                   jlong* referrer_tag_ptr, jint length, void* user_data){    ObjectIndex   object_index;   /* Only calls to Allocate, Deallocate, RawMonitorEnter & RawMonitorExit    *   are allowed here (see the JVMTI Spec).    */    HPROF_ASSERT(tag_ptr!=NULL);    HPROF_ASSERT(class_tag!=(jlong)0);    if ( class_tag == (jlong)0 ) {        /* We can't do anything with a class_tag==0, just skip it */        return JVMTI_VISIT_OBJECTS;    }       switch ( reference_kind ) {                case JVMTI_HEAP_REFERENCE_FIELD:        case JVMTI_HEAP_REFERENCE_ARRAY_ELEMENT:        case JVMTI_HEAP_REFERENCE_CLASS_LOADER:        case JVMTI_HEAP_REFERENCE_SIGNERS:        case JVMTI_HEAP_REFERENCE_PROTECTION_DOMAIN:        case JVMTI_HEAP_REFERENCE_INTERFACE:        case JVMTI_HEAP_REFERENCE_STATIC_FIELD:        case JVMTI_HEAP_REFERENCE_CONSTANT_POOL:            return objectReference(reference_kind, reference_info,                   class_tag, size, tag_ptr, referrer_tag_ptr, length);                    case JVMTI_HEAP_REFERENCE_JNI_GLOBAL: {                SerialNumber trace_serial_num;                SerialNumber gref_serial_num;                TraceIndex   trace_index;                SiteIndex    object_site_index;                                setup_tag_on_root(tag_ptr, class_tag, size,                                  gdata->unknown_thread_serial_num,                                  &object_index, &object_site_index);                if ( object_site_index != 0 ) {                    SiteKey     *pkey;                                        pkey = get_pkey(object_site_index);                    trace_index = pkey->trace_index;                } else {                    trace_index = gdata->system_trace_index;                }                trace_serial_num = trace_get_serial_number(trace_index);                gref_serial_num  = gdata->gref_serial_number_counter++;                io_heap_root_jni_global(object_index, gref_serial_num,                                         trace_serial_num);            }            break;                    case JVMTI_HEAP_REFERENCE_SYSTEM_CLASS: {                char        *sig;                SerialNumber class_serial_num;                SiteIndex    object_site_index;                    setup_tag_on_root(tag_ptr, class_tag, size,                                  gdata->unknown_thread_serial_num,                                  &object_index, &object_site_index);                sig = "Unknown";                class_serial_num = 0;                if ( object_site_index != 0 ) {                    SiteKey *pkey;                                        pkey = get_pkey(object_site_index);                    sig = string_get(class_get_signature(pkey->cnum));                    class_serial_num = class_get_serial_number(pkey->cnum);                }                io_heap_root_system_class(object_index, sig, class_serial_num);            }            break;                    case JVMTI_HEAP_REFERENCE_MONITOR:            setup_tag_on_root(tag_ptr, class_tag, size,                               gdata->unknown_thread_serial_num,                              &object_index, NULL);            io_heap_root_monitor(object_index);            break;                    case JVMTI_HEAP_REFERENCE_STACK_LOCAL:  {                SerialNumber thread_serial_num;                jlong        thread_tag;                thread_tag = reference_info->stack_local.thread_tag;                localReference(tag_ptr, class_tag, thread_tag, size,                             &object_index, &thread_serial_num);                io_heap_root_java_frame(object_index, thread_serial_num,                              reference_info->stack_local.depth);            }            break;        case JVMTI_HEAP_REFERENCE_JNI_LOCAL: {                SerialNumber thread_serial_num;                jlong        thread_tag;                thread_tag = reference_info->jni_local.thread_tag;                localReference(tag_ptr, class_tag, thread_tag, size,                             &object_index, &thread_serial_num);                io_heap_root_jni_local(object_index, thread_serial_num,                              reference_info->jni_local.depth);            }            break;                    case JVMTI_HEAP_REFERENCE_THREAD: {                SerialNumber thread_serial_num;                SerialNumber trace_serial_num;                TraceIndex   trace_index;                SiteIndex    object_site_index;                TlsIndex     tls_index;                        /* It is assumed that tag_ptr is referring to a                  *      java.lang.Thread object here.                 */                if ( (*tag_ptr) != (jlong)0 ) {                    setup_tag_on_root(tag_ptr, class_tag, size, 0,                                      &object_index, &object_site_index);                    trace_index       = site_get_trace_index(object_site_index);                    /* Hopefully the ThreadStart event put this thread's                     *   correct serial number on it's object.                     */                    thread_serial_num = object_get_thread_serial_number(object_index);                } else {                    /* Rare situation that a Thread object is not tagged.                     *   Create special unique thread serial number in this                     *   case, probably means we never saw a thread start                     *   or thread end, or even an allocation of the thread                     *   object.                     */                    thread_serial_num = gdata->thread_serial_number_counter++;                    setup_tag_on_root(tag_ptr, class_tag, size,                                      thread_serial_num,                                      &object_index, &object_site_index);                    trace_index = gdata->system_trace_index;                }                /* Get tls_index and set in_heap_dump, if we find it. */                tls_index = tls_find(thread_serial_num);                if ( tls_index != 0 ) {                    tls_set_in_heap_dump(tls_index, 1);                }                trace_serial_num = trace_get_serial_number(trace_index);                /* Issue thread object (must be before thread root) */                io_heap_root_thread_object(object_index,                                 thread_serial_num, trace_serial_num);                /* Issue thread root */                io_heap_root_thread(object_index, thread_serial_num);            }            break;                    case JVMTI_HEAP_REFERENCE_OTHER:            setup_tag_on_root(tag_ptr, class_tag, size,                              gdata->unknown_thread_serial_num,                              &object_index, NULL);            io_heap_root_unknown(object_index);            break;              default:            /* Ignore anything else */            break;    }        return JVMTI_VISIT_OBJECTS;}voidsite_heapdump(JNIEnv *env){       rawMonitorEnter(gdata->data_access_lock); {        jvmtiHeapCallbacks heapCallbacks;                /* Remove class dumped status, all classes must be dumped */        class_all_status_remove(CLASS_DUMPED);        /* Clear in_heap_dump flag */        tls_clear_in_heap_dump();                /* Dump the last thread traces and get the lists back we need */        tls_dump_traces(env);               /* Write header for heap dump */        io_heap_header(gdata->total_live_instances, gdata->total_live_bytes);        /* Setup a clean reference table */        reference_init();                /* Walk over all reachable objects and dump out roots */        gdata->gref_serial_number_counter = gdata->gref_serial_number_start;        /* Issue thread object for fake non-existent unknown thread         *   just in case someone refers to it. Real threads are handled         *   during iterate over reachable objects.         */        io_heap_root_thread_object(0, gdata->unknown_thread_serial_num,                         trace_get_serial_number(gdata->system_trace_index));        /* Iterate over heap and get the real stuff */        (void)memset(&heapCallbacks, 0, sizeof(heapCallbacks));        /* Select callbacks */        heapCallbacks.heap_reference_callback       = &cbReference;        if ( gdata->primfields == JNI_TRUE ) {            heapCallbacks.primitive_field_callback  = &cbPrimFieldData;        }        if ( gdata->primarrays == JNI_TRUE ) {            heapCallbacks.array_primitive_value_callback  = &cbPrimArrayData;        }        followReferences(&heapCallbacks, (void*)NULL);        /* Process reference information. */        object_reference_dump(env);        object_clear_references();        reference_cleanup();        /* Dump the last thread traces and get the lists back we need */        tls_dump_traces(env);        /* Write out footer for heap dump */        io_heap_footer();            } rawMonitorExit(gdata->data_access_lock);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?