From 17200dd1f33b4cd8236d43c6d93f31ca23875fc8 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Tue, 9 Aug 2022 08:40:08 +0200 Subject: [PATCH] MINOR: debug: also store the function name in struct mem_stats The calling function name is now stored in the structure, and it's reported when the "all" argument is passed. The first column is significantly enlarged because some names are really wide :-( --- include/haproxy/bug.h | 7 +++++++ include/haproxy/pool.h | 3 +++ src/debug.c | 18 ++++++++++++++---- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/include/haproxy/bug.h b/include/haproxy/bug.h index 1ca15a641..f4cc71b4b 100644 --- a/include/haproxy/bug.h +++ b/include/haproxy/bug.h @@ -242,6 +242,7 @@ enum { struct mem_stats { size_t calls; size_t size; + const char *func; const char *file; int line; int type; @@ -254,6 +255,7 @@ struct mem_stats { static struct mem_stats _ __attribute__((used,__section__("mem_stats"),__aligned__(sizeof(void*)))) = { \ .file = __FILE__, .line = __LINE__, \ .type = MEM_STATS_TYPE_CALLOC, \ + .func = __func__, \ }; \ HA_WEAK("__start_mem_stats"); \ HA_WEAK("__stop_mem_stats"); \ @@ -271,6 +273,7 @@ struct mem_stats { static struct mem_stats _ __attribute__((used,__section__("mem_stats"),__aligned__(sizeof(void*)))) = { \ .file = __FILE__, .line = __LINE__, \ .type = MEM_STATS_TYPE_FREE, \ + .func = __func__, \ }; \ HA_WEAK("__start_mem_stats"); \ HA_WEAK("__stop_mem_stats"); \ @@ -285,6 +288,7 @@ struct mem_stats { static struct mem_stats _ __attribute__((used,__section__("mem_stats"),__aligned__(sizeof(void*)))) = { \ .file = __FILE__, .line = __LINE__, \ .type = MEM_STATS_TYPE_FREE, \ + .func = __func__, \ }; \ HA_WEAK("__start_mem_stats"); \ HA_WEAK("__stop_mem_stats"); \ @@ -303,6 +307,7 @@ struct mem_stats { static struct mem_stats _ __attribute__((used,__section__("mem_stats"),__aligned__(sizeof(void*)))) = { \ .file = __FILE__, .line = __LINE__, \ .type = MEM_STATS_TYPE_MALLOC, \ + .func = __func__, \ }; \ HA_WEAK("__start_mem_stats"); \ HA_WEAK("__stop_mem_stats"); \ @@ -317,6 +322,7 @@ struct mem_stats { static struct mem_stats _ __attribute__((used,__section__("mem_stats"),__aligned__(sizeof(void*)))) = { \ .file = __FILE__, .line = __LINE__, \ .type = MEM_STATS_TYPE_REALLOC, \ + .func = __func__, \ }; \ HA_WEAK("__start_mem_stats"); \ HA_WEAK("__stop_mem_stats"); \ @@ -331,6 +337,7 @@ struct mem_stats { static struct mem_stats _ __attribute__((used,__section__("mem_stats"),__aligned__(sizeof(void*)))) = { \ .file = __FILE__, .line = __LINE__, \ .type = MEM_STATS_TYPE_STRDUP, \ + .func = __func__, \ }; \ HA_WEAK("__start_mem_stats"); \ HA_WEAK("__stop_mem_stats"); \ diff --git a/include/haproxy/pool.h b/include/haproxy/pool.h index 579b53cb8..ec974d1b1 100644 --- a/include/haproxy/pool.h +++ b/include/haproxy/pool.h @@ -258,6 +258,7 @@ static inline void *pool_get_from_cache(struct pool_head *pool, const void *call static struct mem_stats _ __attribute__((used,__section__("mem_stats"),__aligned__(sizeof(void*)))) = { \ .file = __FILE__, .line = __LINE__, \ .type = MEM_STATS_TYPE_P_FREE, \ + .func = __func__, \ }; \ _.extra = __pool; \ HA_WEAK("__start_mem_stats"); \ @@ -275,6 +276,7 @@ static inline void *pool_get_from_cache(struct pool_head *pool, const void *call static struct mem_stats _ __attribute__((used,__section__("mem_stats"),__aligned__(sizeof(void*)))) = { \ .file = __FILE__, .line = __LINE__, \ .type = MEM_STATS_TYPE_P_ALLOC, \ + .func = __func__, \ }; \ _.extra = __pool; \ HA_WEAK("__start_mem_stats"); \ @@ -290,6 +292,7 @@ static inline void *pool_get_from_cache(struct pool_head *pool, const void *call static struct mem_stats _ __attribute__((used,__section__("mem_stats"),__aligned__(sizeof(void*)))) = { \ .file = __FILE__, .line = __LINE__, \ .type = MEM_STATS_TYPE_P_ALLOC, \ + .func = __func__, \ }; \ _.extra = __pool; \ HA_WEAK("__start_mem_stats"); \ diff --git a/src/debug.c b/src/debug.c index 074af8117..043130688 100644 --- a/src/debug.c +++ b/src/debug.c @@ -1279,8 +1279,6 @@ static int debug_iohandler_memstats(struct appctx *appctx) if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW))) goto end; - chunk_reset(&trash); - /* we have two inner loops here, one for the proxy, the other one for * the buffer. */ @@ -1289,6 +1287,7 @@ static int debug_iohandler_memstats(struct appctx *appctx) const char *name; const char *p; const char *info = NULL; + const char *func = NULL; if (!ptr->size && !ptr->calls && !ctx->show_all) continue; @@ -1299,6 +1298,8 @@ static int debug_iohandler_memstats(struct appctx *appctx) name = p + 1; } + func = ptr->func; + switch (ptr->type) { case MEM_STATS_TYPE_CALLOC: type = "CALLOC"; break; case MEM_STATS_TYPE_FREE: type = "FREE"; break; @@ -1316,9 +1317,18 @@ static int debug_iohandler_memstats(struct appctx *appctx) // (unsigned long)ptr->size, (unsigned long)ptr->calls, // (unsigned long)(ptr->calls ? (ptr->size / ptr->calls) : 0)); - chunk_printf(&trash, "%s:%d", name, ptr->line); - while (trash.data < 25) + chunk_reset(&trash); + if (ctx->show_all) + chunk_appendf(&trash, "%s(", func); + + chunk_appendf(&trash, "%s:%d", name, ptr->line); + + if (ctx->show_all) + chunk_appendf(&trash, ")"); + + while (trash.data < (ctx->show_all ? 45 : 25)) trash.area[trash.data++] = ' '; + chunk_appendf(&trash, "%7s size: %12lu calls: %9lu size/call: %6lu %s\n", type, (unsigned long)ptr->size, (unsigned long)ptr->calls,