From fd3ceec4752210a16c089ab4efd8201c78b39642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Tue, 11 May 2021 19:54:05 +0200 Subject: [PATCH] Add debug tracing capability to isc_mempool_create/destroy Previously, we only had capability to trace the mempool gets and puts, but for debugging, it's sometimes also important to keep track how many and where do the memory pools get created and destroyed. This commit adds such tracking capability. --- lib/isc/include/isc/mem.h | 8 ++++++-- lib/isc/mem.c | 20 ++++++++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/isc/include/isc/mem.h b/lib/isc/include/isc/mem.h index 945151e0f2..ec0c98ee45 100644 --- a/lib/isc/include/isc/mem.h +++ b/lib/isc/include/isc/mem.h @@ -361,8 +361,11 @@ isc_mem_renderjson(void *memobj0); * Memory pools */ +#define isc_mempool_create(c, s, mp) \ + isc__mempool_create((c), (s), (mp)_ISC_MEM_FILELINE) void -isc_mempool_create(isc_mem_t *mctx, size_t size, isc_mempool_t **mpctxp); +isc__mempool_create(isc_mem_t *mctx, size_t size, + isc_mempool_t **mpctxp _ISC_MEM_FLARG); /*%< * Create a memory pool. * @@ -381,8 +384,9 @@ isc_mempool_create(isc_mem_t *mctx, size_t size, isc_mempool_t **mpctxp); *\li #ISC_R_SUCCESS -- all is well. */ +#define isc_mempool_destroy(mp) isc__mempool_destroy((mp)_ISC_MEM_FILELINE) void -isc_mempool_destroy(isc_mempool_t **mpctxp); +isc__mempool_destroy(isc_mempool_t **mpctxp _ISC_MEM_FLARG); /*%< * Destroy a memory pool. * diff --git a/lib/isc/mem.c b/lib/isc/mem.c index 0d9e7763d7..71e9cdfa28 100644 --- a/lib/isc/mem.c +++ b/lib/isc/mem.c @@ -1084,7 +1084,8 @@ isc_mem_getname(isc_mem_t *ctx) { */ void -isc_mempool_create(isc_mem_t *mctx, size_t size, isc_mempool_t **mpctxp) { +isc__mempool_create(isc_mem_t *mctx, size_t size, + isc_mempool_t **mpctxp FLARG) { REQUIRE(VALID_CONTEXT(mctx)); REQUIRE(size > 0U); REQUIRE(mpctxp != NULL && *mpctxp == NULL); @@ -1117,6 +1118,13 @@ isc_mempool_create(isc_mem_t *mctx, size_t size, isc_mempool_t **mpctxp) { atomic_init(&mpctx->fillcount, 1); atomic_init(&mpctx->gets, 0); +#if ISC_MEM_TRACKLINES + if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) { + fprintf(stderr, "create pool %p file %s line %u mctx %p\n", + mpctx, file, line, mctx); + } +#endif /* ISC_MEM_TRACKLINES */ + *mpctxp = (isc_mempool_t *)mpctx; MCTXLOCK(mctx); @@ -1138,7 +1146,7 @@ isc_mempool_setname(isc_mempool_t *mpctx, const char *name) { } void -isc_mempool_destroy(isc_mempool_t **mpctxp) { +isc__mempool_destroy(isc_mempool_t **mpctxp FLARG) { REQUIRE(mpctxp != NULL); REQUIRE(VALID_MEMPOOL(*mpctxp)); @@ -1149,6 +1157,14 @@ isc_mempool_destroy(isc_mempool_t **mpctxp) { mpctx = *mpctxp; *mpctxp = NULL; + +#if ISC_MEM_TRACKLINES + if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) { + fprintf(stderr, "destroy pool %p file %s line %u mctx %p\n", + mpctx, file, line, mctx); + } +#endif + if (atomic_load_acquire(&mpctx->allocated) > 0) { UNEXPECTED_ERROR(__FILE__, __LINE__, "isc_mempool_destroy(): mempool %s "