From 91f5600c2e4ca9ca43beff4d8952a0f1b5dbda44 Mon Sep 17 00:00:00 2001 From: localvar Date: Thu, 21 Nov 2019 08:22:59 +0000 Subject: [PATCH] add memory check to GO connector also update log format --- src/connector/go/src/taosSql/utils.go | 24 +++++++++++++++++++++++- src/util/src/tmem.c | 8 ++++---- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/connector/go/src/taosSql/utils.go b/src/connector/go/src/taosSql/utils.go index a5a90059b5..a104322fcc 100755 --- a/src/connector/go/src/taosSql/utils.go +++ b/src/connector/go/src/taosSql/utils.go @@ -12,15 +12,25 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ - package taosSql +/* +#cgo CFLAGS : -I/usr/include +#include +#cgo LDFLAGS: -L/usr/lib -ltaos +void taosSetAllocMode(int mode, const char* path, _Bool autoDump); +void taosDumpMemoryLeak(); +*/ +import "C" + + import ( "database/sql/driver" "errors" "fmt" "sync/atomic" "time" + "unsafe" ) // Returns the bool value of the input. @@ -398,3 +408,15 @@ func namedValueToValue(named []driver.NamedValue) ([]driver.Value, error) { } +/****************************************************************************** +* Utils for C memory issues debugging * +******************************************************************************/ +func SetAllocMode(mode int32, path string) { + cpath := C.CString(path) + defer C.free(unsafe.Pointer(cpath)) + C.taosSetAllocMode(C.int(mode), cpath, false) +} + +func DumpMemoryLeak() { + C.taosDumpMemoryLeak() +} diff --git a/src/util/src/tmem.c b/src/util/src/tmem.c index 5a42933ebe..2625e4e5e6 100644 --- a/src/util/src/tmem.c +++ b/src/util/src/tmem.c @@ -45,7 +45,7 @@ static bool random_alloc_fail(size_t size, const char* file, uint32_t line) { } if (fpAllocLog != NULL) { - fprintf(fpAllocLog, "memory allocation(%zu bytes) at line %d of '%s' will fail.\n", size, line, file); + fprintf(fpAllocLog, "%s:%d: memory allocation of %zu bytes will fail.\n", file, line, size); } return true; @@ -137,7 +137,7 @@ static void free_detect_leak(void* ptr, const char* file, uint32_t line) { SMemBlock* blk = (SMemBlock*)(((char*)ptr) - sizeof(SMemBlock)); if (blk->magic != MEMBLK_MAGIC) { if (fpAllocLog != NULL) { - fprintf(fpAllocLog, "%s:%d: memory not allocated by 'taos_malloc'.\n", file, line); + fprintf(fpAllocLog, "%s:%d: memory is allocated by default allocator.\n", file, line); } free(ptr); return; @@ -196,7 +196,7 @@ static void* realloc_detect_leak(void* ptr, size_t size, const char* file, uint3 SMemBlock* blk = ((char*)ptr) - sizeof(SMemBlock); if (blk->magic != MEMBLK_MAGIC) { if (fpAllocLog != NULL) { - fprintf(fpAllocLog, "%s:%d: memory not allocated by 'taos_malloc'.\n", file, line); + fprintf(fpAllocLog, "%s:%d: memory is allocated by default allocator.\n", file, line); } return realloc(ptr, size); } @@ -265,7 +265,7 @@ static ssize_t getline_detect_leak(char **lineptr, size_t *n, FILE *stream, cons static void dump_memory_leak() { const char* hex = "0123456789ABCDEF"; - const char* fmt = ":%d: addr=0x%p, size=%d, content(first 16 bytes)="; + const char* fmt = ":%d: addr=%p, size=%d, content(first 16 bytes)="; size_t numOfBlk = 0, totalSize = 0; if (fpAllocLog == NULL) { -- GitLab