提交 91f5600c 编写于 作者: weixin_48148422's avatar weixin_48148422

add memory check to GO connector

also update log format
上级 e7bdf0c2
...@@ -12,15 +12,25 @@ ...@@ -12,15 +12,25 @@
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package taosSql package taosSql
/*
#cgo CFLAGS : -I/usr/include
#include <stdlib.h>
#cgo LDFLAGS: -L/usr/lib -ltaos
void taosSetAllocMode(int mode, const char* path, _Bool autoDump);
void taosDumpMemoryLeak();
*/
import "C"
import ( import (
"database/sql/driver" "database/sql/driver"
"errors" "errors"
"fmt" "fmt"
"sync/atomic" "sync/atomic"
"time" "time"
"unsafe"
) )
// Returns the bool value of the input. // Returns the bool value of the input.
...@@ -398,3 +408,15 @@ func namedValueToValue(named []driver.NamedValue) ([]driver.Value, error) { ...@@ -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()
}
...@@ -45,7 +45,7 @@ static bool random_alloc_fail(size_t size, const char* file, uint32_t line) { ...@@ -45,7 +45,7 @@ static bool random_alloc_fail(size_t size, const char* file, uint32_t line) {
} }
if (fpAllocLog != NULL) { 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; return true;
...@@ -137,7 +137,7 @@ static void free_detect_leak(void* ptr, const char* file, uint32_t line) { ...@@ -137,7 +137,7 @@ static void free_detect_leak(void* ptr, const char* file, uint32_t line) {
SMemBlock* blk = (SMemBlock*)(((char*)ptr) - sizeof(SMemBlock)); SMemBlock* blk = (SMemBlock*)(((char*)ptr) - sizeof(SMemBlock));
if (blk->magic != MEMBLK_MAGIC) { if (blk->magic != MEMBLK_MAGIC) {
if (fpAllocLog != NULL) { 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); free(ptr);
return; return;
...@@ -196,7 +196,7 @@ static void* realloc_detect_leak(void* ptr, size_t size, const char* file, uint3 ...@@ -196,7 +196,7 @@ static void* realloc_detect_leak(void* ptr, size_t size, const char* file, uint3
SMemBlock* blk = ((char*)ptr) - sizeof(SMemBlock); SMemBlock* blk = ((char*)ptr) - sizeof(SMemBlock);
if (blk->magic != MEMBLK_MAGIC) { if (blk->magic != MEMBLK_MAGIC) {
if (fpAllocLog != NULL) { 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); return realloc(ptr, size);
} }
...@@ -265,7 +265,7 @@ static ssize_t getline_detect_leak(char **lineptr, size_t *n, FILE *stream, cons ...@@ -265,7 +265,7 @@ static ssize_t getline_detect_leak(char **lineptr, size_t *n, FILE *stream, cons
static void dump_memory_leak() { static void dump_memory_leak() {
const char* hex = "0123456789ABCDEF"; 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; size_t numOfBlk = 0, totalSize = 0;
if (fpAllocLog == NULL) { if (fpAllocLog == NULL) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册