hprof_tls.c

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

C
1,187
字号
    }    if ( pthread_serial_num != NULL ) {	*pthread_serial_num = thread_serial_num;    }    return status;}MonitorIndextls_get_monitor(TlsIndex index){    TlsInfo  *info;        info = get_info(index);    return info->monitor_index;}voidtls_set_thread_object_index(TlsIndex index, ObjectIndex thread_object_index){    TlsInfo  *info;        info = get_info(index);    info->thread_object_index = thread_object_index;}SerialNumbertls_get_thread_serial_number(TlsIndex index){    return get_key(index);}voidtls_set_monitor(TlsIndex index, MonitorIndex monitor_index){    TlsInfo  *info;        info = get_info(index);    info->monitor_index = monitor_index;}voidtls_cleanup(void){    table_cleanup(gdata->tls_table, &cleanup_item, NULL);    gdata->tls_table = NULL;}voidtls_delete_global_references(JNIEnv *env){    table_walk_items(gdata->tls_table, &delete_ref_item, (void*)env);}voidtls_thread_ended(JNIEnv *env, TlsIndex index){    HPROF_ASSERT(env!=NULL);       /* Sample thread stack for last time, do NOT free the entry yet. */    table_lock_enter(gdata->tls_table); {	SerialNumber thread_serial_num;        TlsInfo     *info;	jthread      thread;		thread_serial_num = get_key(index);	info              = get_info(index);	thread            = newLocalReference(env, info->globalref);	if (gdata->heap_dump && thread!=NULL) {	    setup_trace_buffers(info, gdata->max_trace_depth);	    info->last_trace = get_trace(thread, thread_serial_num,				    gdata->max_trace_depth, JNI_FALSE,				    info->frames_buffer, info->jframes_buffer);	}	if ( thread != NULL ) {	    deleteLocalReference(env, thread);	}    } table_lock_exit(gdata->tls_table);}/* Sample ALL threads and update the trace costs */voidtls_sample_all_threads(JNIEnv *env){    ThreadList    list;    jthread      *threads;    SerialNumber *serial_nums;        table_lock_enter(gdata->tls_table); {	int           max_count;	int           nbytes;	int           i; 	/* Get buffers to hold thread list and serial number list */	max_count   = table_element_count(gdata->tls_table);	nbytes      = (int)sizeof(jthread)*max_count;	threads     = (jthread*)HPROF_MALLOC(nbytes);	nbytes      = (int)sizeof(SerialNumber)*max_count;	serial_nums = (SerialNumber*)HPROF_MALLOC(nbytes);		/* Get list of threads and serial numbers */	list.threads     = threads;	list.infos       = NULL;	list.serial_nums = serial_nums;	list.count       = 0;	list.env         = env;        table_walk_items(gdata->tls_table, &get_thread_list, (void*)&list);		/* Increment the cost on the traces for these threads */	trace_increment_all_sample_costs(list.count, threads, serial_nums, 			      gdata->max_trace_depth, JNI_FALSE);	/* Loop over local refs and free them */	for ( i = 0 ; i < list.count ; i++ ) {	    if ( threads[i] != NULL ) {		deleteLocalReference(env, threads[i]);	    }	}    } table_lock_exit(gdata->tls_table);        /* Free up allocated space */    HPROF_FREE(threads);    HPROF_FREE(serial_nums);}voidtls_push_method(TlsIndex index, jmethodID method){    jlong    method_start_time;    TlsInfo *info;        HPROF_ASSERT(method!=NULL);    info        = get_info(index);    HPROF_ASSERT(info!=NULL);    method_start_time  = method_time();    HPROF_ASSERT(info->stack!=NULL);    push_method(info->stack, method_start_time, method);}  voidtls_pop_exception_catch(TlsIndex index, jthread thread, jmethodID method){    TlsInfo      *info;    StackElement  element;    void         *p;    FrameIndex    frame_index;    jlong         current_time;    HPROF_ASSERT(method!=NULL);    frame_index = frame_find_or_create(method, -1);    HPROF_ASSERT(frame_index != 0);        info = get_info(index);        HPROF_ASSERT(info!=NULL);    HPROF_ASSERT(info->stack!=NULL);    HPROF_ASSERT(frame_index!=0);    current_time = method_time();    info->stack = insure_method_on_stack(thread, info, current_time,			frame_index, method);    p = stack_top(info->stack);    if (p == NULL) {	HPROF_ERROR(JNI_FALSE, "expection pop, nothing on stack");	return;    }    element = *(StackElement*)p;    HPROF_ASSERT(element.frame_index!=0);    while ( element.frame_index != frame_index ) {	pop_method(index, current_time, element.method, frame_index);	p = stack_top(info->stack);	if ( p == NULL ) {	    break;	}	element = *(StackElement*)p;    }    if (p == NULL) {	HPROF_ERROR(JNI_FALSE, "exception pop stack empty");    }}voidtls_pop_method(TlsIndex index, jthread thread, jmethodID method){    TlsInfo      *info;    StackElement  element;    void         *p;    FrameIndex    frame_index;    jlong         current_time;        HPROF_ASSERT(method!=NULL);    frame_index = frame_find_or_create(method, -1);    HPROF_ASSERT(frame_index != 0);        info = get_info(index);    HPROF_ASSERT(info!=NULL);    HPROF_ASSERT(info->stack!=NULL);    current_time = method_time();    HPROF_ASSERT(frame_index!=0);    info->stack = insure_method_on_stack(thread, info, current_time,		frame_index, method);    p = stack_top(info->stack);    HPROF_ASSERT(p!=NULL);    element = *(StackElement*)p;    while ( element.frame_index != frame_index ) {	pop_method(index, current_time, element.method, frame_index);	p = stack_top(info->stack);	if ( p == NULL ) {	    break;	}	element = *(StackElement*)p;    }    pop_method(index, current_time, method, frame_index);}/* For all TLS entries, update the last_trace on all threads */static void         update_all_last_traces(JNIEnv *env){    jthread        *threads;    TlsInfo       **infos;    SerialNumber   *serial_nums;    TraceIndex     *traces;       if ( gdata->max_trace_depth == 0 ) {	return;    }    table_lock_enter(gdata->tls_table); {        	ThreadList      list;	int             max_count;	int             nbytes;	int             i; 	/* Get buffers to hold thread list and serial number list */	max_count   = table_element_count(gdata->tls_table);	nbytes      = (int)sizeof(jthread)*max_count;	threads     = (jthread*)HPROF_MALLOC(nbytes);	nbytes      = (int)sizeof(SerialNumber)*max_count;	serial_nums = (SerialNumber*)HPROF_MALLOC(nbytes);	nbytes      = (int)sizeof(TlsInfo*)*max_count;	infos       = (TlsInfo**)HPROF_MALLOC(nbytes);		/* Get list of threads, serial numbers, and info pointers */	list.threads     = threads;	list.serial_nums = serial_nums;	list.infos       = infos;	list.count       = 0;	list.env         = env;        table_walk_items(gdata->tls_table, &get_thread_list, (void*)&list);	/* Get all stack trace index's for all these threadss */	nbytes      = (int)sizeof(TraceIndex)*max_count;	traces      = (TraceIndex*)HPROF_MALLOC(nbytes);	trace_get_all_current(list.count, threads, serial_nums, 			      gdata->max_trace_depth, JNI_FALSE,			      traces, JNI_TRUE);	/* Loop over traces and update last_trace's */	for ( i = 0 ; i < list.count ; i++ ) {	    if ( threads[i] != NULL ) {		deleteLocalReference(env, threads[i]);	    }	    infos[i]->last_trace = traces[i];	}	    } table_lock_exit(gdata->tls_table);	    /* Free up all allocated space */    HPROF_FREE(threads);    HPROF_FREE(serial_nums);    HPROF_FREE(infos);    HPROF_FREE(traces);}void         tls_dump_traces(JNIEnv *env){    rawMonitorEnter(gdata->data_access_lock); {	update_all_last_traces(env);	trace_output_unmarked(env);    } rawMonitorExit(gdata->data_access_lock);}        voidtls_dump_monitor_state(JNIEnv *env){    HPROF_ASSERT(env!=NULL);        rawMonitorEnter(gdata->data_access_lock); {	tls_dump_traces(env);	io_write_monitor_dump_header();	table_walk_items(gdata->tls_table, &dump_thread_state, (void*)env);	table_walk_items(gdata->tls_table, &dump_monitor_state, (void*)env);	io_write_monitor_dump_footer();    } rawMonitorExit(gdata->data_access_lock);}void         tls_monitor_start_timer(TlsIndex index){    TlsInfo *info;        info = get_info(index);    HPROF_ASSERT(info!=NULL);    HPROF_ASSERT(info->globalref!=NULL);    info->monitor_start_time = monitor_time();}jlong        tls_monitor_stop_timer(TlsIndex index){    TlsInfo *info;    jlong    t;    info = get_info(index);    HPROF_ASSERT(info!=NULL);    t =  monitor_time() - info->monitor_start_time;    info->monitor_start_time = 0;    return t;}TraceIndex   tls_get_trace(TlsIndex index, JNIEnv *env, int depth, jboolean skip_init){    SerialNumber thread_serial_num;    TraceIndex   trace_index;    TlsInfo     *info;    jthread      thread;    thread_serial_num = get_key(index);    info              = get_info(index);    HPROF_ASSERT(info!=NULL);    setup_trace_buffers(info, depth);    thread = newLocalReference(env, info->globalref);    if ( thread != NULL ) {        trace_index = get_trace(thread, thread_serial_num, depth, skip_init,			info->frames_buffer, info->jframes_buffer);	deleteLocalReference(env, thread);    } else {        trace_index = gdata->system_trace_index;    }    return trace_index;}voidtls_set_in_heap_dump(TlsIndex index, jint in_heap_dump){    TlsInfo  *info;        info = get_info(index);    info->in_heap_dump = in_heap_dump;}jinttls_get_in_heap_dump(TlsIndex index){    TlsInfo  *info;        info = get_info(index);    return info->in_heap_dump;}static voidclean_in_heap_dump(TableIndex index, void *key_ptr, int key_len, void *info_ptr, void *arg){    TlsInfo *info;        HPROF_ASSERT(info_ptr!=NULL);    info  = (TlsInfo*)info_ptr;    info->in_heap_dump = 0;}voidtls_clear_in_heap_dump(void){    table_walk_items(gdata->tls_table, &clean_in_heap_dump, NULL);}TlsIndextls_find(SerialNumber thread_serial_num){    TlsIndex index;       if ( thread_serial_num == 0 ) {	return 0;    }    index = table_find_entry(gdata->tls_table, 	  (void*)&thread_serial_num, (int)sizeof(SerialNumber));    return index;}

⌨️ 快捷键说明

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