eina: be able to track and display our memory over head when using malloc.
authorcedric <cedric@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Wed, 18 Jan 2012 17:47:05 +0000 (17:47 +0000)
committercedric <cedric@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Wed, 18 Jan 2012 17:47:05 +0000 (17:47 +0000)
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.

git-svn-id: svn+ssh://svn.enlightenment.org/var/svn/e/trunk/eina@67296 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

configure.ac
src/modules/mp/chained_pool/eina_chained_mempool.c

index d3784bf..367ffe1 100644 (file)
@@ -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
index 009b62b..020d0ad 100644 (file)
 # endif
 #endif
 
+#ifdef EINA_DEBUG_MALLOC
+# include <malloc.h>
+#endif
+
 #ifdef EFL_HAVE_WIN32_THREADS
 # define WIN32_LEAN_AND_MEAN
 # include <windows.h>
@@ -51,7 +55,7 @@
 # include <valgrind/memcheck.h>
 #endif
 
-#ifdef DEBUG
+#if defined DEBUG || defined EINA_DEBUG_MALLOC
 #include <assert.h>
 #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