hprof_util.c
来自「一个小公司要求给写的很简单的任务管理系统。」· C语言 代码 · 共 1,736 行 · 第 1/4 页
C
1,736 行
}voidsetEventCallbacks(jvmtiEventCallbacks *pcallbacks){ jvmtiError error; error = JVMTI_FUNC_PTR(gdata->jvmti,SetEventCallbacks) (gdata->jvmti, pcallbacks, (int)sizeof(jvmtiEventCallbacks)); if (error != JVMTI_ERROR_NONE) { HPROF_JVMTI_ERROR(error, "Cannot set jvmti callbacks"); }}void *getThreadLocalStorage(jthread thread){ jvmtiError error; void *ptr; HPROF_ASSERT(thread!=NULL); ptr = NULL; error = JVMTI_FUNC_PTR(gdata->jvmti,GetThreadLocalStorage) (gdata->jvmti, thread, &ptr); if ( error == JVMTI_ERROR_WRONG_PHASE ) { /* Treat this as ok */ error = JVMTI_ERROR_NONE; ptr = NULL; } if ( error != JVMTI_ERROR_NONE ) { HPROF_JVMTI_ERROR(error, "Cannot get thread local storage"); } return ptr;}voidsetThreadLocalStorage(jthread thread, void *ptr){ jvmtiError error; HPROF_ASSERT(thread!=NULL); error = JVMTI_FUNC_PTR(gdata->jvmti,SetThreadLocalStorage) (gdata->jvmti, thread, (const void *)ptr); if ( error == JVMTI_ERROR_WRONG_PHASE ) { /* Treat this as ok */ error = JVMTI_ERROR_NONE; } if ( error != JVMTI_ERROR_NONE ) { HPROF_JVMTI_ERROR(error, "Cannot set thread local storage"); }}voidgetThreadState(jthread thread, jint *threadState){ jvmtiError error; HPROF_ASSERT(thread!=NULL); HPROF_ASSERT(threadState!=NULL); *threadState = 0; error = JVMTI_FUNC_PTR(gdata->jvmti,GetThreadState) (gdata->jvmti, thread, threadState); if ( error != JVMTI_ERROR_NONE ) { HPROF_JVMTI_ERROR(error, "Cannot get thread state"); }}voidgetThreadInfo(jthread thread, jvmtiThreadInfo *info)/* WARNING: Must be called inside WITH_LOCAL_REFS */{ jvmtiError error; HPROF_ASSERT(thread!=NULL); HPROF_ASSERT(info!=NULL); (void)memset((void*)info, 0, sizeof(jvmtiThreadInfo)); error = JVMTI_FUNC_PTR(gdata->jvmti,GetThreadInfo) (gdata->jvmti, thread, info); if ( error != JVMTI_ERROR_NONE ) { HPROF_JVMTI_ERROR(error, "Cannot get thread info"); }}voidgetThreadGroupInfo(jthreadGroup thread_group, jvmtiThreadGroupInfo *info)/* WARNING: Must be called inside WITH_LOCAL_REFS */{ jvmtiError error; HPROF_ASSERT(info!=NULL); (void)memset((void*)info, 0, sizeof(jvmtiThreadGroupInfo)); error = JVMTI_FUNC_PTR(gdata->jvmti,GetThreadGroupInfo) (gdata->jvmti, thread_group, info); if ( error != JVMTI_ERROR_NONE ) { HPROF_JVMTI_ERROR(error, "Cannot get thread group info"); }}voidgetLoadedClasses(jclass **ppclasses, jint *pcount)/* WARNING: Must be called inside WITH_LOCAL_REFS */{ jvmtiError error; *ppclasses = NULL; *pcount = 0; error = JVMTI_FUNC_PTR(gdata->jvmti,GetLoadedClasses) (gdata->jvmti, pcount, ppclasses); if ( error != JVMTI_ERROR_NONE ) { HPROF_JVMTI_ERROR(error, "Cannot get all loaded class list"); }}static voidgetLineNumberTable(jmethodID method, jvmtiLineNumberEntry **ppentries, jint *pcount){ jvmtiError error; HPROF_ASSERT(method!=NULL); *ppentries = NULL; *pcount = 0; error = JVMTI_FUNC_PTR(gdata->jvmti,GetLineNumberTable) (gdata->jvmti, method, pcount, ppentries); if ( error == JVMTI_ERROR_ABSENT_INFORMATION ) { error = JVMTI_ERROR_NONE; *ppentries = NULL; *pcount = 0; } if ( error != JVMTI_ERROR_NONE ) { HPROF_JVMTI_ERROR(error, "Cannot get source line numbers"); }}static jintmap_loc2line(jlocation location, jvmtiLineNumberEntry *table, jint count){ jint line_number; int i; int start; int half; HPROF_ASSERT(location>=0); HPROF_ASSERT(count>=0); line_number = -1; if ( count == 0 ) { return line_number; } /* Do a binary search */ half = count >> 1; start = 0; while ( half > 0 ) { jlocation start_location; start_location = table[start + half].start_location; if ( location > start_location ) { start = start + half; } else if ( location == start_location ) { start = start + half; break; } half = half >> 1; } HPROF_ASSERT(start < count); /* Now start the table search */ for ( i = start ; i < count ; i++ ) { if ( location < table[i].start_location ) { HPROF_ASSERT( ((int)location) < ((int)table[i].start_location) ); break; } line_number = table[i].line_number; } HPROF_ASSERT(line_number > 0); return line_number;}jintgetLineNumber(jmethodID method, jlocation location){ jvmtiLineNumberEntry *line_table; jint line_count; jint lineno; HPROF_ASSERT(method!=NULL); if ( location < 0 ) { HPROF_ASSERT(location > -4); return (jint)location; } lineno = -1; getLineNumberTable(method, &line_table, &line_count); lineno = map_loc2line(location, line_table, line_count); jvmtiDeallocate(line_table); return lineno;}jlonggetMaxMemory(JNIEnv *env){ jlong max; HPROF_ASSERT(env!=NULL); max = (jlong)0; WITH_LOCAL_REFS(env, 1) { jclass clazz; jmethodID getRuntime; jobject runtime; jmethodID maxMemory; clazz = findClass(env, "java/lang/Runtime"); getRuntime = getStaticMethodID(env, clazz, "getRuntime", "()Ljava/lang/Runtime;"); runtime = callStaticObjectMethod(env, clazz, getRuntime); maxMemory = getMethodID(env, clazz, "maxMemory", "()J"); max = callLongMethod(env, runtime, maxMemory); } END_WITH_LOCAL_REFS; return max; }voidcreateAgentThread(JNIEnv *env, const char *name, jvmtiStartFunction func){ jvmtiError error; HPROF_ASSERT(name!=NULL); HPROF_ASSERT(func!=NULL); WITH_LOCAL_REFS(env, 1) { jclass clazz; jmethodID threadConstructor; jmethodID threadSetDaemon; jthread thread; jstring nameString; jthreadGroup systemThreadGroup; jthreadGroup * groups; jint groupCount; thread = NULL; systemThreadGroup = NULL; groups = NULL; clazz = class_get_class(env, gdata->thread_cnum); HPROF_ASSERT(clazz!=NULL); threadConstructor = getMethodID(env, clazz, "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;)V"); threadSetDaemon = getMethodID(env, clazz, "setDaemon", "(Z)V"); error = JVMTI_FUNC_PTR(gdata->jvmti,GetTopThreadGroups) (gdata->jvmti, &groupCount, &groups); if ( error == JVMTI_ERROR_NONE ) { if ( groupCount > 0 ) { systemThreadGroup = groups[0]; } jvmtiDeallocate(groups); nameString = newStringUTF(env, name); HPROF_ASSERT(nameString!=NULL); thread = newThreadObject(env, clazz, threadConstructor, systemThreadGroup, nameString); HPROF_ASSERT(thread!=NULL); callVoidMethod(env, thread, threadSetDaemon, JNI_TRUE); error = JVMTI_FUNC_PTR(gdata->jvmti,RunAgentThread) (gdata->jvmti, thread, func, NULL, JVMTI_THREAD_MAX_PRIORITY); /* After the thread is running... */ /* Make sure the TLS table has this thread as an agent thread */ tls_agent_thread(env, thread); } } END_WITH_LOCAL_REFS; if ( error != JVMTI_ERROR_NONE ) { HPROF_JVMTI_ERROR(error, "Cannot create agent thread"); }}jlonggetThreadCpuTime(jthread thread){ jvmtiError error; jlong cpuTime; HPROF_ASSERT(thread!=NULL); cpuTime = -1; error = JVMTI_FUNC_PTR(gdata->jvmti,GetThreadCpuTime) (gdata->jvmti, thread, &cpuTime); if ( error != JVMTI_ERROR_NONE ) { HPROF_JVMTI_ERROR(error, "Cannot get cpu time"); } return cpuTime;}/* Get frame count */void getFrameCount(jthread thread, jint *pcount){ jvmtiError error; HPROF_ASSERT(thread!=NULL); HPROF_ASSERT(pcount!=NULL); *pcount = 0; error = JVMTI_FUNC_PTR(gdata->jvmti,GetFrameCount) (gdata->jvmti, thread, pcount); if ( error != JVMTI_ERROR_NONE ) { *pcount = 0; }}/* Get call trace */void getStackTrace(jthread thread, jvmtiFrameInfo *pframes, jint depth, jint *pcount){ jvmtiError error; HPROF_ASSERT(thread!=NULL); HPROF_ASSERT(pframes!=NULL); HPROF_ASSERT(depth >= 0); HPROF_ASSERT(pcount!=NULL); *pcount = 0; error = JVMTI_FUNC_PTR(gdata->jvmti,GetStackTrace) (gdata->jvmti, thread, 0, depth, pframes, pcount); if ( error != JVMTI_ERROR_NONE ) { *pcount = 0; }}void getThreadListStackTraces(jint count, jthread *threads, jint depth, jvmtiStackInfo **stack_info){ jvmtiError error; HPROF_ASSERT(threads!=NULL); HPROF_ASSERT(stack_info!=NULL); HPROF_ASSERT(depth >= 0); HPROF_ASSERT(count > 0); *stack_info = NULL; error = JVMTI_FUNC_PTR(gdata->jvmti,GetThreadListStackTraces) (gdata->jvmti, count, threads, depth, stack_info); if ( error != JVMTI_ERROR_NONE ) { HPROF_JVMTI_ERROR(error, "Cannot get thread list stack info"); }}void followReferences(jvmtiHeapCallbacks *pHeapCallbacks, void *user_data){ jvmtiError error; error = JVMTI_FUNC_PTR(gdata->jvmti,FollowReferences) (gdata->jvmti, 0, NULL, NULL, pHeapCallbacks, user_data); if ( error != JVMTI_ERROR_NONE ) { HPROF_JVMTI_ERROR(error, "Cannot follow references"); }}/* GC control */void runGC(void){ jvmtiError error; error = JVMTI_FUNC_PTR(gdata->jvmti,ForceGarbageCollection) (gdata->jvmti); if ( error != JVMTI_ERROR_NONE ) { HPROF_JVMTI_ERROR(error, "Cannot force garbage collection"); }}/* ------------------------------------------------------------------- *//* Getting the initial JVMTI environment */voidgetJvmti(void){ jvmtiEnv *jvmti = NULL; jint res; jint jvmtiCompileTimeMajorVersion; jint jvmtiCompileTimeMinorVersion; jint jvmtiCompileTimeMicroVersion; res = JVM_FUNC_PTR(gdata->jvm,GetEnv) (gdata->jvm, (void **)&jvmti, JVMTI_VERSION_1); if (res != JNI_OK) { char buf[256]; (void)md_snprintf(buf, sizeof(buf), "Unable to access JVMTI Version 1 (0x%x)," " is your JDK a 5.0 or newer version?" " JNIEnv's GetEnv() returned %d", JVMTI_VERSION_1, res); buf[sizeof(buf)-1] = 0; HPROF_ERROR(JNI_FALSE, buf); error_exit_process(1); /* Kill entire process, no core dump */ } gdata->jvmti = jvmti; /* Check to make sure the version of jvmti.h we compiled with * matches the runtime version we are using. */ jvmtiCompileTimeMajorVersion = ( JVMTI_VERSION & JVMTI_VERSION_MASK_MAJOR ) >> JVMTI_VERSION_SHIFT_MAJOR; jvmtiCompileTimeMinorVersion = ( JVMTI_VERSION & JVMTI_VERSION_MASK_MINOR ) >> JVMTI_VERSION_SHIFT_MINOR; jvmtiCompileTimeMicroVersion = ( JVMTI_VERSION & JVMTI_VERSION_MASK_MICRO ) >> JVMTI_VERSION_SHIFT_MICRO; if ( !compatible_versions(jvmtiMajorVersion(), jvmtiMinorVersion(), jvmtiCompileTimeMajorVersion, jvmtiCompileTimeMinorVersion) ) { char buf[256]; (void)md_snprintf(buf, sizeof(buf), "This " AGENTNAME " native library will not work with this VM's " "version of JVMTI (%d.%d.%d), it needs JVMTI %d.%d[.%d]." , jvmtiMajorVersion(), jvmtiMinorVersion(), jvmtiMicroVersion(), jvmtiCompileTimeMajorVersion, jvmtiCompileTimeMinorVersion, jvmtiCompileTimeMicroVersion); buf[sizeof(buf)-1] = 0; HPROF_ERROR(JNI_FALSE, buf); error_exit_process(1); /* Kill entire process, no core dump wanted */ }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?