From fc6737ab04c4a6828f3bf60f86a1b0dd45efe6a6 Mon Sep 17 00:00:00 2001 From: localvar Date: Thu, 14 Nov 2019 10:45:00 +0000 Subject: [PATCH] add option 'autoDump' for memory leak detection. --- .../jni/com_taosdata_jdbc_TSDBJNIConnector.h | 2 +- src/client/src/TSDBJNIConnector.c | 6 ++--- src/inc/tutil.h | 2 +- src/system/detail/src/dnodeService.c | 4 ++-- src/util/src/tmem.c | 23 +++++++++++-------- 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/client/jni/com_taosdata_jdbc_TSDBJNIConnector.h b/src/client/jni/com_taosdata_jdbc_TSDBJNIConnector.h index d11a030188..2b99dde7af 100644 --- a/src/client/jni/com_taosdata_jdbc_TSDBJNIConnector.h +++ b/src/client/jni/com_taosdata_jdbc_TSDBJNIConnector.h @@ -15,7 +15,7 @@ extern "C" { * Signature: (Ljava/lang/String;)V */ JNIEXPORT void JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_detectMemoryLeakImp - (JNIEnv *, jclass, jstring); + (JNIEnv *, jclass, jstring, jboolean); /* * Class: com_taosdata_jdbc_TSDBJNIConnector diff --git a/src/client/src/TSDBJNIConnector.c b/src/client/src/TSDBJNIConnector.c index 57f312f573..b76aaaf640 100644 --- a/src/client/src/TSDBJNIConnector.c +++ b/src/client/src/TSDBJNIConnector.c @@ -111,13 +111,13 @@ void jniGetGlobalMethod(JNIEnv *env) { jniTrace("native method register finished"); } -JNIEXPORT void JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_detectMemoryLeakImp(JNIEnv *env, jobject jobj, jstring jPath) { +JNIEXPORT void JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_detectMemoryLeakImp(JNIEnv *env, jobject jobj, jstring jPath, jboolean jAutoDump) { if (jPath != NULL) { const char *path = (*env)->GetStringUTFChars(env, jPath, NULL); - taos_detect_memory_leak(path); + taos_detect_memory_leak(path, !!jAutoDump); (*env)->ReleaseStringUTFChars(env, jPath, path); } else { - taos_detect_memory_leak(NULL); + taos_detect_memory_leak(NULL, !!jAutoDump); } jniGetGlobalMethod(env); diff --git a/src/inc/tutil.h b/src/inc/tutil.h index 0d454faf23..383a62be2f 100644 --- a/src/inc/tutil.h +++ b/src/inc/tutil.h @@ -187,7 +187,7 @@ static FORCE_INLINE void taosEncryptPass(uint8_t *inBuf, unsigned int inLen, cha char *taosIpStr(uint32_t ipInt); -extern void taos_detect_memory_leak(const char* path); +extern void taos_detect_memory_leak(const char* path, bool autoDump); extern void taos_dump_memory_leak(); #if TAOS_MEM_CHECK == 1 diff --git a/src/system/detail/src/dnodeService.c b/src/system/detail/src/dnodeService.c index 1d0c6f4893..2d81d49740 100644 --- a/src/system/detail/src/dnodeService.c +++ b/src/system/detail/src/dnodeService.c @@ -64,9 +64,9 @@ int main(int argc, char *argv[]) { #if TAOS_MEM_CHECK == 2 } else if (strcmp(argv[i], "--check-mem-leak") == 0) { if ((i < argc - 1) && (argv[i+1][0] != '-')) { - taos_detect_memory_leak(argv[++i]); + taos_detect_memory_leak(argv[++i], true); } else { - taos_detect_memory_leak(NULL); + taos_detect_memory_leak(NULL, true); } #endif } diff --git a/src/util/src/tmem.c b/src/util/src/tmem.c index 87a43a697d..b97d099644 100644 --- a/src/util/src/tmem.c +++ b/src/util/src/tmem.c @@ -307,13 +307,15 @@ void taos_dump_memory_leak() { static void dump_memory_leak_at_sig(int sig) { fprintf(fpMemLeak, "signal %d received, exiting...\n", sig); - taos_dump_memory_leak(); + struct sigaction act = {0}; act.sa_handler = SIG_DFL; sigaction(sig, &act, NULL); + + taos_dump_memory_leak(); } -void taos_detect_memory_leak(const char* path) { +void taos_detect_memory_leak(const char* path, bool autoDump) { if (fpMemLeak != NULL) { printf("memory leak detection already enabled.\n"); return; @@ -326,13 +328,14 @@ void taos_detect_memory_leak(const char* path) { return; } - atexit(taos_dump_memory_leak); - - struct sigaction act = {0}; - act.sa_handler = dump_memory_leak_at_sig; - sigaction(SIGFPE, &act, NULL); - sigaction(SIGSEGV, &act, NULL); - sigaction(SIGILL, &act, NULL); + if (autoDump) { + atexit(taos_dump_memory_leak); + struct sigaction act = {0}; + act.sa_handler = dump_memory_leak_at_sig; + sigaction(SIGFPE, &act, NULL); + sigaction(SIGSEGV, &act, NULL); + sigaction(SIGILL, &act, NULL); + } } #endif @@ -342,7 +345,7 @@ void taos_dump_memory_leak() { // do nothing } -void taos_detect_memory_leak(const char* path) { +void taos_detect_memory_leak(const char* path, bool autoDump) { printf("memory leak detection not enabled, please set 'TAOS_MEM_CHECK' to 2."); } #endif \ No newline at end of file -- GitLab