From 38ecc1f70dd58cb86f92076fdedbd88005d0b988 Mon Sep 17 00:00:00 2001 From: Mark Date: Wed, 8 Apr 2020 12:54:10 +0300 Subject: [PATCH] Add slab allocation tracing --- include/sys/mem/slab.h | 17 +++++++++++++++-- sys/mem/slab.c | 20 ++++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/include/sys/mem/slab.h b/include/sys/mem/slab.h index 397c8b3..91cb3f1 100644 --- a/include/sys/mem/slab.h +++ b/include/sys/mem/slab.h @@ -12,5 +12,18 @@ struct slab_cache; struct slab_cache *slab_cache_get(size_t obj_size); void slab_stat(struct slab_stat *st); -void *slab_calloc(struct slab_cache *cp); -void slab_free(struct slab_cache *cp, void *ptr); + +#define SLAB_TRACE_ALLOC 1 +#if defined(SLAB_TRACE_ALLOC) +#define slab_calloc(cp) slab_calloc_trace(__FILE__, __LINE__, cp) +#define slab_free(cp, ptr) slab_free_trace(__FILE__, __LINE__, cp, ptr) + +void *slab_calloc_trace(const char *filename, int line, struct slab_cache *cp); +void slab_free_trace(const char *filename, int line, struct slab_cache *cp, void *ptr); +#else +#define slab_calloc(cp) slab_calloc_int(cp) +#define slab_free(cp, ptr) slab_free_int(cp, ptr) +#endif + +void *slab_calloc_int(struct slab_cache *cp); +void slab_free_int(struct slab_cache *cp, void *ptr); diff --git a/sys/mem/slab.c b/sys/mem/slab.c index 7fa2455..efc80a5 100644 --- a/sys/mem/slab.c +++ b/sys/mem/slab.c @@ -130,7 +130,7 @@ static inline void *slab_alloc_from(struct slab_cache *cp, struct slab *slabp) { return objp; } -void *slab_calloc(struct slab_cache *cp) { +void *slab_calloc_int(struct slab_cache *cp) { struct list_head *slabs_partial, *slabs_empty, *entry; struct slab *slabp; try_again: @@ -162,7 +162,7 @@ try_again: return slab_alloc_from(cp, slabp); } -void slab_free(struct slab_cache *cp, void *objp) { +void slab_free_int(struct slab_cache *cp, void *objp) { struct slab *slabp; // Get page-aligned address uintptr_t page = (uintptr_t) objp; @@ -219,3 +219,19 @@ void slab_stat(struct slab_stat *st) { } } } + +// Tracing +#if defined(SLAB_TRACE_ALLOC) + +void *slab_calloc_trace(const char *filename, int line, struct slab_cache *cp) { + void *objp = slab_calloc_int(cp); + debugf(DEBUG_DEFAULT, "\033[43;30m%s:%d: slab allocate %u = %p\033[0m\n", filename, line, cp->object_size, objp); + return objp; +} + +void slab_free_trace(const char *filename, int line, struct slab_cache *cp, void *ptr) { + debugf(DEBUG_DEFAULT, "\033[44;32m%s:%d: slab free %p (%u)\033[0m\n", filename, line, ptr, cp->object_size); + slab_free_int(cp, ptr); +} + +#endif