From 32abecfbc09269344f09fddc14036d90ef721d48 Mon Sep 17 00:00:00 2001 From: localvar Date: Thu, 14 Nov 2019 09:51:45 +0000 Subject: [PATCH] make 'taos_dump_memory_leak' public for java --- .../jni/com_taosdata_jdbc_TSDBJNIConnector.h | 8 ++++++++ src/client/src/TSDBJNIConnector.c | 9 +++++++-- src/inc/tutil.h | 3 ++- src/system/detail/src/dnodeService.c | 4 ++-- src/util/src/tmem.c | 20 +++++++++++++------ 5 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/client/jni/com_taosdata_jdbc_TSDBJNIConnector.h b/src/client/jni/com_taosdata_jdbc_TSDBJNIConnector.h index d99fca5ce1..d11a030188 100644 --- a/src/client/jni/com_taosdata_jdbc_TSDBJNIConnector.h +++ b/src/client/jni/com_taosdata_jdbc_TSDBJNIConnector.h @@ -17,6 +17,14 @@ extern "C" { JNIEXPORT void JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_detectMemoryLeakImp (JNIEnv *, jclass, jstring); +/* + * Class: com_taosdata_jdbc_TSDBJNIConnector + * Method: + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT void JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_dumpMemoryLeakImp + (JNIEnv *, jclass); + /* * Class: com_taosdata_jdbc_TSDBJNIConnector * Method: initImp diff --git a/src/client/src/TSDBJNIConnector.c b/src/client/src/TSDBJNIConnector.c index 4123c2aa12..57f312f573 100644 --- a/src/client/src/TSDBJNIConnector.c +++ b/src/client/src/TSDBJNIConnector.c @@ -114,15 +114,20 @@ void jniGetGlobalMethod(JNIEnv *env) { JNIEXPORT void JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_detectMemoryLeakImp(JNIEnv *env, jobject jobj, jstring jPath) { if (jPath != NULL) { const char *path = (*env)->GetStringUTFChars(env, jPath, NULL); - taos_dump_memory_leak_at_exit(path); + taos_detect_memory_leak(path); (*env)->ReleaseStringUTFChars(env, jPath, path); } else { - taos_dump_memory_leak_at_exit(NULL); + taos_detect_memory_leak(NULL); } jniGetGlobalMethod(env); } +JNIEXPORT void JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_dumpMemoryLeakImp(JNIEnv *env, jobject jobj) { + taos_dump_memory_leak(); + jniGetGlobalMethod(env); +} + JNIEXPORT void JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_initImp(JNIEnv *env, jobject jobj, jstring jconfigDir) { if (jconfigDir != NULL) { const char *confDir = (*env)->GetStringUTFChars(env, jconfigDir, NULL); diff --git a/src/inc/tutil.h b/src/inc/tutil.h index 4066e5a756..0d454faf23 100644 --- a/src/inc/tutil.h +++ b/src/inc/tutil.h @@ -187,7 +187,8 @@ static FORCE_INLINE void taosEncryptPass(uint8_t *inBuf, unsigned int inLen, cha char *taosIpStr(uint32_t ipInt); -extern void taos_dump_memory_leak_at_exit(const char* path); +extern void taos_detect_memory_leak(const char* path); +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 55d5873695..1d0c6f4893 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_dump_memory_leak_at_exit(argv[++i]); + taos_detect_memory_leak(argv[++i]); } else { - taos_dump_memory_leak_at_exit(NULL); + taos_detect_memory_leak(NULL); } #endif } diff --git a/src/util/src/tmem.c b/src/util/src/tmem.c index 81dff55049..87a43a697d 100644 --- a/src/util/src/tmem.c +++ b/src/util/src/tmem.c @@ -261,11 +261,15 @@ ssize_t taos_getline(char **lineptr, size_t *n, FILE *stream, const char* file, return size; } -static void dump_memory_leak() { +void taos_dump_memory_leak() { const char* hex = "0123456789ABCDEF"; const char* fmt = ":%d: addr=0x%p, size=%d, content(first 16 bytes)='"; size_t numOfBlk = 0, totalSize = 0; + if (fpMemLeak == NULL) { + return; + } + fputs("memory blocks allocated but not freed before exit:\n\n", fpMemLeak); while (atomic_val_compare_exchange_ptr(&lock, 0, 1) != 0); @@ -303,13 +307,13 @@ static void dump_memory_leak() { static void dump_memory_leak_at_sig(int sig) { fprintf(fpMemLeak, "signal %d received, exiting...\n", sig); - dump_memory_leak(); + taos_dump_memory_leak(); struct sigaction act = {0}; act.sa_handler = SIG_DFL; sigaction(sig, &act, NULL); } -void taos_dump_memory_leak_at_exit(const char* path) { +void taos_detect_memory_leak(const char* path) { if (fpMemLeak != NULL) { printf("memory leak detection already enabled.\n"); return; @@ -322,7 +326,7 @@ void taos_dump_memory_leak_at_exit(const char* path) { return; } - atexit(dump_memory_leak); + atexit(taos_dump_memory_leak); struct sigaction act = {0}; act.sa_handler = dump_memory_leak_at_sig; @@ -334,7 +338,11 @@ void taos_dump_memory_leak_at_exit(const char* path) { #endif #if TAOS_MEM_CHECK != 2 -void taos_dump_memory_leak_at_exit(const char* path) { - printf("memory leak detection not enabled!") +void taos_dump_memory_leak() { + // do nothing +} + +void taos_detect_memory_leak(const char* path) { + printf("memory leak detection not enabled, please set 'TAOS_MEM_CHECK' to 2."); } #endif \ No newline at end of file