CLEANUP: debug: use struct ha_caller for memstat

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).
This commit is contained in:
Willy Tarreau 2022-09-06 08:05:59 +02:00
parent 7f2f1f294c
commit d96d214b4c
3 changed files with 53 additions and 38 deletions

View 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"); \

View 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"); \

View 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, ")");