]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
CLEANUP: debug: use struct ha_caller for memstat
authorWilly Tarreau <w@1wt.eu>
Tue, 6 Sep 2022 06:05:59 +0000 (08:05 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 8 Sep 2022 12:19:15 +0000 (14:19 +0200)
The memstats code currently defines its own file/function/line number,
type and extra pointer. We don't need to keep them separate and we can
easily replace them all with just a struct ha_caller. Note that the
extra pointer could be converted to a pool ID stored into arg8 or
arg32 and be dropped as well, but this would first require to define
IDs for pools (which we currently do not have).

include/haproxy/bug.h
include/haproxy/pool.h
src/debug.c

index 45570027c499a63b8f614888fcfd0b685086176c..54b41ee421bbcd1596c42edf051a4ca66f295cdd 100644 (file)
@@ -259,10 +259,7 @@ enum {
 struct mem_stats {
        size_t calls;
        size_t size;
-       const char *func;
-       const char *file;
-       int line;
-       int type;
+       struct ha_caller caller;
        const void *extra; // extra info specific to this call (e.g. pool ptr)
 } __attribute__((aligned(sizeof(void*))));
 
@@ -270,9 +267,11 @@ struct mem_stats {
 #define calloc(x,y)  ({                                                        \
        size_t __x = (x); size_t __y = (y);                             \
        static struct mem_stats _ __attribute__((used,__section__("mem_stats"),__aligned__(sizeof(void*)))) = { \
-               .file = __FILE__, .line = __LINE__,                     \
-               .type = MEM_STATS_TYPE_CALLOC,                          \
-               .func = __func__,                                       \
+               .caller = {                                             \
+                       .file = __FILE__, .line = __LINE__,             \
+                       .what = MEM_STATS_TYPE_CALLOC,                  \
+                       .func = __func__,                               \
+               },                                                      \
        };                                                              \
        HA_WEAK("__start_mem_stats");                                   \
        HA_WEAK("__stop_mem_stats");                                    \
@@ -291,9 +290,11 @@ struct mem_stats {
 #define will_free(x, y)  ({                                            \
        void *__x = (x); size_t __y = (y);                              \
        static struct mem_stats _ __attribute__((used,__section__("mem_stats"),__aligned__(sizeof(void*)))) = { \
-               .file = __FILE__, .line = __LINE__,                     \
-               .type = MEM_STATS_TYPE_FREE,                            \
-               .func = __func__,                                       \
+               .caller = {                                             \
+                       .file = __FILE__, .line = __LINE__,             \
+                       .what = MEM_STATS_TYPE_FREE,                    \
+                       .func = __func__,                               \
+               },                                                      \
        };                                                              \
        HA_WEAK("__start_mem_stats");                                   \
        HA_WEAK("__stop_mem_stats");                                    \
@@ -307,9 +308,11 @@ struct mem_stats {
 #define ha_free(x)  ({                                                 \
        typeof(x) __x = (x);                                            \
        static struct mem_stats _ __attribute__((used,__section__("mem_stats"),__aligned__(sizeof(void*)))) = { \
-               .file = __FILE__, .line = __LINE__,                     \
-               .type = MEM_STATS_TYPE_FREE,                            \
-               .func = __func__,                                       \
+               .caller = {                                             \
+                       .file = __FILE__, .line = __LINE__,             \
+                       .what = MEM_STATS_TYPE_FREE,                    \
+                       .func = __func__,                               \
+               },                                                      \
        };                                                              \
        HA_WEAK("__start_mem_stats");                                   \
        HA_WEAK("__stop_mem_stats");                                    \
@@ -326,9 +329,11 @@ struct mem_stats {
 #define malloc(x)  ({                                                  \
        size_t __x = (x);                                               \
        static struct mem_stats _ __attribute__((used,__section__("mem_stats"),__aligned__(sizeof(void*)))) = { \
-               .file = __FILE__, .line = __LINE__,                     \
-               .type = MEM_STATS_TYPE_MALLOC,                          \
-               .func = __func__,                                       \
+               .caller = {                                             \
+                       .file = __FILE__, .line = __LINE__,             \
+                       .what = MEM_STATS_TYPE_MALLOC,                  \
+                       .func = __func__,                               \
+               },                                                      \
        };                                                              \
        HA_WEAK("__start_mem_stats");                                   \
        HA_WEAK("__stop_mem_stats");                                    \
@@ -341,9 +346,11 @@ struct mem_stats {
 #define realloc(x,y)  ({                                               \
        void *__x = (x); size_t __y = (y);                              \
        static struct mem_stats _ __attribute__((used,__section__("mem_stats"),__aligned__(sizeof(void*)))) = { \
-               .file = __FILE__, .line = __LINE__,                     \
-               .type = MEM_STATS_TYPE_REALLOC,                         \
-               .func = __func__,                                       \
+               .caller = {                                             \
+                       .file = __FILE__, .line = __LINE__,             \
+                       .what = MEM_STATS_TYPE_REALLOC,                 \
+                       .func = __func__,                               \
+               },                                                      \
        };                                                              \
        HA_WEAK("__start_mem_stats");                                   \
        HA_WEAK("__stop_mem_stats");                                    \
@@ -356,9 +363,11 @@ struct mem_stats {
 #define strdup(x)  ({                                                  \
        const char *__x = (x); size_t __y = strlen(__x);                \
        static struct mem_stats _ __attribute__((used,__section__("mem_stats"),__aligned__(sizeof(void*)))) = { \
-               .file = __FILE__, .line = __LINE__,                     \
-               .type = MEM_STATS_TYPE_STRDUP,                          \
-               .func = __func__,                                       \
+               .caller = {                                             \
+                       .file = __FILE__, .line = __LINE__,             \
+                       .what = MEM_STATS_TYPE_STRDUP,                  \
+                       .func = __func__,                               \
+               },                                                      \
        };                                                              \
        HA_WEAK("__start_mem_stats");                                   \
        HA_WEAK("__stop_mem_stats");                                    \
index ec974d1b1bc51bf9d1f6b2156599cb16e08663c7..1af32ae04d682fe069b3743ba07b971ae4b6443d 100644 (file)
@@ -256,9 +256,11 @@ static inline void *pool_get_from_cache(struct pool_head *pool, const void *call
        struct pool_head *__pool = (pool);                              \
        typeof(ptr) __ptr = (ptr);                                      \
        static struct mem_stats _ __attribute__((used,__section__("mem_stats"),__aligned__(sizeof(void*)))) = { \
-               .file = __FILE__, .line = __LINE__,                     \
-               .type = MEM_STATS_TYPE_P_FREE,                          \
-               .func = __func__,                                       \
+               .caller = {                                             \
+                       .file = __FILE__, .line = __LINE__,             \
+                       .what = MEM_STATS_TYPE_P_FREE,                  \
+                       .func = __func__,                               \
+               },                                                      \
        };                                                              \
        _.extra = __pool;                                               \
        HA_WEAK("__start_mem_stats");                                   \
@@ -274,9 +276,11 @@ static inline void *pool_get_from_cache(struct pool_head *pool, const void *call
        struct pool_head *__pool = (pool);                              \
        size_t __x = __pool->size;                                      \
        static struct mem_stats _ __attribute__((used,__section__("mem_stats"),__aligned__(sizeof(void*)))) = { \
-               .file = __FILE__, .line = __LINE__,                     \
-               .type = MEM_STATS_TYPE_P_ALLOC,                         \
-               .func = __func__,                                       \
+               .caller = {                                             \
+                       .file = __FILE__, .line = __LINE__,             \
+                       .what = MEM_STATS_TYPE_P_ALLOC,                 \
+                       .func = __func__,                               \
+               },                                                      \
        };                                                              \
        _.extra = __pool;                                               \
        HA_WEAK("__start_mem_stats");                                   \
@@ -290,9 +294,11 @@ static inline void *pool_get_from_cache(struct pool_head *pool, const void *call
        struct pool_head *__pool = (pool);                              \
        size_t __x = __pool->size;                                      \
        static struct mem_stats _ __attribute__((used,__section__("mem_stats"),__aligned__(sizeof(void*)))) = { \
-               .file = __FILE__, .line = __LINE__,                     \
-               .type = MEM_STATS_TYPE_P_ALLOC,                         \
-               .func = __func__,                                       \
+               .caller = {                                             \
+                       .file = __FILE__, .line = __LINE__,             \
+                       .what = MEM_STATS_TYPE_P_ALLOC,                 \
+                       .func = __func__,                               \
+               },                                                      \
        };                                                              \
        _.extra = __pool;                                               \
        HA_WEAK("__start_mem_stats");                                   \
index 8e2ca42f57bccf93f7d439485cb95b22872318c8..0d06376b9dd3493c7ccdb5997eea2378c076c4ab 100644 (file)
@@ -1294,15 +1294,15 @@ static int debug_iohandler_memstats(struct appctx *appctx)
                        if (!ptr->size && !ptr->calls && !ctx->show_all)
                                continue;
 
-                       for (p = name = ptr->file; *p; p++) {
+                       for (p = name = ptr->caller.file; *p; p++) {
                                if (*p == '/')
                                        name = p + 1;
                        }
 
                        if (ctx->show_all)
-                               w = snprintf(&tmp, 0, "%s(%s:%d) ", ptr->func, name, ptr->line);
+                               w = snprintf(&tmp, 0, "%s(%s:%d) ", ptr->caller.func, name, ptr->caller.line);
                        else
-                               w = snprintf(&tmp, 0, "%s:%d ", name, ptr->line);
+                               w = snprintf(&tmp, 0, "%s:%d ", name, ptr->caller.line);
 
                        if (w > ctx->width)
                                ctx->width = w;
@@ -1323,14 +1323,14 @@ static int debug_iohandler_memstats(struct appctx *appctx)
                        continue;
 
                /* basename only */
-               for (p = name = ptr->file; *p; p++) {
+               for (p = name = ptr->caller.file; *p; p++) {
                        if (*p == '/')
                                name = p + 1;
                }
 
-               func = ptr->func;
+               func = ptr->caller.func;
 
-               switch (ptr->type) {
+               switch (ptr->caller.what) {
                case MEM_STATS_TYPE_CALLOC:  type = "CALLOC";  break;
                case MEM_STATS_TYPE_FREE:    type = "FREE";    break;
                case MEM_STATS_TYPE_MALLOC:  type = "MALLOC";  break;
@@ -1351,7 +1351,7 @@ static int debug_iohandler_memstats(struct appctx *appctx)
                if (ctx->show_all)
                        chunk_appendf(&trash, "%s(", func);
 
-               chunk_appendf(&trash, "%s:%d", name, ptr->line);
+               chunk_appendf(&trash, "%s:%d", name, ptr->caller.line);
 
                if (ctx->show_all)
                        chunk_appendf(&trash, ")");