From 4a853cde861873a8e933114cd81bf0bcb73154fc Mon Sep 17 00:00:00 2001 From: Cedric BAIL Date: Wed, 18 Jan 2012 17:47:05 +0000 Subject: [PATCH] eina: be able to track and display our memory over head when using malloc. NOTE: it is worth enabling it and see EINA_LOG_INFO displaying how much memory we are wasting. Should help tune our memory allocator usage. SVN revision: 67296 --- legacy/eina/configure.ac | 21 +++++++++++++ .../mp/chained_pool/eina_chained_mempool.c | 31 +++++++++++++++++-- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/legacy/eina/configure.ac b/legacy/eina/configure.ac index d3784bf22f..367ffe1d22 100644 --- a/legacy/eina/configure.ac +++ b/legacy/eina/configure.ac @@ -510,6 +510,27 @@ if test "x${have_dirfd}" = "xyes" ; then AC_DEFINE([HAVE_DIRFD], [1], [ Define to 1 if you have the `dirfd' function or macro.]) fi +# check debug information for malloc +AC_CHECK_FUNCS([malloc_usable_size], [malloc_true_size="yes"], [malloc_true_size="no"]) +if test "x${malloc_true_size}" = "xyes" ; then + AC_DEFINE([HAVE_MALLOC_USABLE_SIZE], [1], [We will be able to know the exact amount of wasted memory]) +fi + +want_debug_malloc="no" +AC_ARG_ENABLE([debug-malloc], + [AC_HELP_STRING([--enable-debug-malloc], [enable debugging of malloc usage overhead in our allocator @<:@default=enabled@:>@])], + [ + if test "x${enableval}" = "xyes" ; then + want_debug_malloc="yes" + else + want_debug_malloc="no" + fi + ], [want_debug_malloc="no"]) + +if test "x${malloc_true_size}" = "xyes" -a "x${want_debug_malloc}" = "xyes"; then + AC_DEFINE([EINA_DEBUG_MALLOC], [1], [Turn on debugging overhead in mempool]) +fi + # dlopen and dladdr dlopen_libs="" case "$host_os" in diff --git a/legacy/eina/src/modules/mp/chained_pool/eina_chained_mempool.c b/legacy/eina/src/modules/mp/chained_pool/eina_chained_mempool.c index 009b62b2d8..020d0ad3e0 100644 --- a/legacy/eina/src/modules/mp/chained_pool/eina_chained_mempool.c +++ b/legacy/eina/src/modules/mp/chained_pool/eina_chained_mempool.c @@ -31,6 +31,10 @@ # endif #endif +#ifdef EINA_DEBUG_MALLOC +# include +#endif + #ifdef EFL_HAVE_WIN32_THREADS # define WIN32_LEAN_AND_MEAN # include @@ -51,7 +55,7 @@ # include #endif -#ifdef DEBUG +#if defined DEBUG || defined EINA_DEBUG_MALLOC #include #include "eina_log.h" @@ -74,6 +78,9 @@ struct _Chained_Mempool int alloc_size; int group_size; int usage; +#ifdef EINA_DEBUG_MALLOC + int minimal_size; +#endif #ifdef EFL_DEBUG_THREADS pthread_t self; #endif @@ -125,6 +132,20 @@ _eina_chained_mp_pool_new(Chained_Mempool *pool) return NULL; } +#ifdef EINA_DEBUG_MALLOC + { + size_t sz; + + sz = malloc_usable_size(p); + if (sz - pool->minimal_size > 0) + INF("Just allocated %0.2f%% to much memory in '%s' for one block of size %i that means %i bytes to much.", + ((float)(sz - pool->minimal_size) * 100) / (float) (pool->alloc_size), + pool->name, + pool->alloc_size, + sz - pool->minimal_size); + } +#endif + alignof = eina_mempool_alignof(sizeof(Chained_Pool)); ptr = (unsigned char *)p + alignof; p->usage = 0; @@ -444,6 +465,10 @@ eina_chained_mempool_init(const char *context, memcpy((char *)mp->name, context, length); } +#ifdef EINA_DEBUG_MALLOC + mp->minimal_size = item_size * mp->pool_size + sizeof(Chained_Pool); +#endif + mp->item_alloc = eina_mempool_alignof(item_size); mp->group_size = mp->item_alloc * mp->pool_size; mp->alloc_size = mp->group_size + eina_mempool_alignof(sizeof(Chained_Pool)); @@ -517,7 +542,7 @@ static Eina_Mempool_Backend _eina_chained_mp_backend = { Eina_Bool chained_init(void) { -#ifdef DEBUG +#if defined DEBUG || defined EINA_DEBUG_MALLOC _eina_chained_mp_log_dom = eina_log_domain_register("eina_mempool", EINA_LOG_COLOR_DEFAULT); if (_eina_chained_mp_log_dom < 0) @@ -533,7 +558,7 @@ Eina_Bool chained_init(void) void chained_shutdown(void) { eina_mempool_unregister(&_eina_chained_mp_backend); -#ifdef DEBUG +#if defined DEBUG || defined EINA_DEBUG_MALLOC eina_log_domain_unregister(_eina_chained_mp_log_dom); _eina_chained_mp_log_dom = -1; #endif