[sr-dev] git:master: mem: summarize in-use memory on exit

Andrei Pelinescu-Onciul andrei at iptel.org
Tue Sep 29 19:04:04 CEST 2009


Module: sip-router
Branch: master
Commit: ed20ee1dd197ecaead7549f762e5d331a14db34e
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=ed20ee1dd197ecaead7549f762e5d331a14db34e

Author: Dragos Vingarzan <dragos.vingarzan at gmail.com>
Committer: Andrei Pelinescu-Onciul <andrei at iptel.org>
Date:   Mon Sep 28 22:01:17 2009 +0200

mem: summarize in-use memory on exit

Additional qm_sums and fm_sums as extension to qm_status/fm_status, with
summarized values per allocation source.
Slightly modified SER-224 patch version (no locking, sr changes --andrei).

Closes SER-224.

Signed-off-by: Andrei Pelinescu-Onciul <andrei at iptel.org>

---

 main.c         |    4 +++
 mem/f_malloc.c |   82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 mem/f_malloc.h |    6 ++++
 mem/mem.h      |    5 +++
 mem/q_malloc.c |   70 +++++++++++++++++++++++++++++++++++++++++++++++
 mem/q_malloc.h |    6 ++++
 mem/shm_mem.h  |   16 +++++++++++
 7 files changed, 189 insertions(+), 0 deletions(-)

diff --git a/main.c b/main.c
index 78c8fb5..6d85a57 100644
--- a/main.c
+++ b/main.c
@@ -532,6 +532,8 @@ void cleanup(show_status)
 	if (show_status){
 		LOG(memlog, "Memory status (pkg):\n");
 		pkg_status();
+		LOG(memlog, "Memory still-in-use summary (pkg):\n");
+		pkg_sums();
 	}
 #endif
 #ifdef SHM_MEM
@@ -540,6 +542,8 @@ void cleanup(show_status)
 	if (show_status){
 			LOG(memlog, "Memory status (shm):\n");
 			shm_status();
+			LOG(memlog, "Memory still-in-use summary (shm):\n");
+			shm_sums();
 	}
 	/* zero all shmem alloc vars that we still use */
 	shm_mem_destroy();
diff --git a/mem/f_malloc.c b/mem/f_malloc.c
index d157fc9..8be84e7 100644
--- a/mem/f_malloc.c
+++ b/mem/f_malloc.c
@@ -40,6 +40,7 @@
  *  2006-04-07  s/DBG/MDBG (andrei)
  *  2007-02-23  added fm_available() (andrei)
  *  2007-06-23  added hash bitmap (andrei)
+ *  2009-09-28  added fm_sums() (patch from Dragos Vingarzan)
  */
 
 
@@ -711,4 +712,85 @@ unsigned long fm_available(struct fm_block* qm)
 #endif
 }
 
+
+#ifdef DBG_F_MALLOC
+
+typedef struct _mem_counter{
+	const char *file;
+	const char *func;
+	unsigned long line;
+	
+	unsigned long size;
+	int count;
+	
+	struct _mem_counter *next;
+} mem_counter;
+
+static mem_counter* get_mem_counter(mem_counter **root,struct fm_frag* f)
+{
+	mem_counter *x;
+	
+	if (!*root) goto make_new;
+	for(x=*root;x;x=x->next)
+		if (x->file == f->file && x->func == f->func && x->line == f->line)
+			return x;
+make_new:	
+	x = malloc(sizeof(mem_counter));
+	x->file = f->file;
+	x->func = f->func;
+	x->line = f->line;
+	x->count = 0;
+	x->size = 0;
+	x->next = *root;
+	*root = x;
+	return x;
+}
+
+
+
+void fm_sums(struct fm_block* qm)
+{
+	struct fm_frag* f;
+	struct fm_frag* free_frag;
+	int i, hash;
+	mem_counter *root,*x;
+	
+	root=0;
+	if (!qm) return;
+
+	LOG(memlog, "summarizing all alloc'ed. fragments:\n");
+	
+	for (f=qm->first_frag, i=0; (char*)f<(char*)qm->last_frag;
+			f=FRAG_NEXT(f), i++){
+		if (f->u.nxt_free==0){
+			/* it might be in-use or the last free fragm. in a free list 
+			   => search the free frags of the same size for a possible
+			   match --andrei*/
+			hash=GET_HASH(f->size);
+			for(free_frag=qm->free_hash[hash].first;
+					free_frag && (free_frag!=f);
+					free_frag=free_frag->u.nxt_free);
+			if (free_frag==0){ /* not found among the free frag */
+				x = get_mem_counter(&root,f);
+				x->count++;
+				x->size+=f->size;
+			}
+		}
+	}
+	x = root;
+	while(x){
+		LOG(memlog, " count=%6d size=%10lu bytes from %s: %s(%ld)\n",
+			x->count,x->size,
+			x->file, x->func, x->line
+			);
+		root = x->next;
+		free(x);
+		x = root;
+	}
+	LOG(memlog, "-----------------------------\n");
+}
+#endif /* DBG_F_MALLOC */
+
+
+
 #endif
diff --git a/mem/f_malloc.h b/mem/f_malloc.h
index 9704441..8b30352 100644
--- a/mem/f_malloc.h
+++ b/mem/f_malloc.h
@@ -155,4 +155,10 @@ void  fm_info(struct fm_block*, struct mem_info*);
 
 unsigned long fm_available(struct fm_block*);
 
+#ifdef DBG_F_MALLOC
+void fm_sums(struct fm_block*);
+#else
+#define fm_sums(v) do{}while(0)
+#endif /* DBG_F_MALLOC */
+
 #endif
diff --git a/mem/mem.h b/mem/mem.h
index 7cb79a8..905200e 100644
--- a/mem/mem.h
+++ b/mem/mem.h
@@ -120,20 +120,24 @@
 #		define pkg_status()    fm_status(mem_block)
 #		define pkg_info(mi)    fm_info(mem_block, mi)
 #		define pkg_available() fm_available(mem_block)
+#		define pkg_sums()      fm_sums(mem_block)
 #	elif defined DL_MALLOC
 #		define pkg_status()  0
 #		define pkg_info(mi)  0
 #		define pkg_available()  0
+#		define pkg_sums()  0
 #	else
 #		define pkg_status()    qm_status(mem_block)
 #		define pkg_info(mi)    qm_info(mem_block, mi)
 #		define pkg_available() qm_available(mem_block)
+#		define pkg_sums()      qm_sums(mem_block)
 #	endif
 #elif defined(SHM_MEM) && defined(USE_SHM_MEM)
 #	include "shm_mem.h"
 #	define pkg_malloc(s) shm_malloc((s))
 #	define pkg_free(p)   shm_free((p))
 #	define pkg_status()  shm_status()
+#	define pkg_sums()    shm_sums()
 #else
 #	include <stdlib.h>
 #	include "memdbg.h"
@@ -147,6 +151,7 @@
 	    ____v123; } )
 #	define pkg_free(p)  do{ MDBG("free %p\n", (p)); free((p)); }while(0);
 #	define pkg_status()
+#	define pkg_sums()
 #endif
 
 int init_pkg_mallocs();
diff --git a/mem/q_malloc.c b/mem/q_malloc.c
index 7df06db..ab24249 100644
--- a/mem/q_malloc.c
+++ b/mem/q_malloc.c
@@ -40,6 +40,7 @@
  *  2006-02-03  fixed realloc out of mem. free bug (andrei)
  *  2006-04-07  s/DBG/MDBG (andrei)
  *  2007-02-23  added fm_available() (andrei)
+ *  2009-09-28  added fm_sums() (patch from Dragos Vingarzan)
  */
 
 
@@ -779,4 +780,73 @@ unsigned long qm_available(struct qm_block* qm)
 }
 
 
+
+#ifdef DBG_QM_MALLOC
+
+typedef struct _mem_counter{
+	const char *file;
+	const char *func;
+	unsigned long line;
+	
+	unsigned long size;
+	int count;
+	
+	struct _mem_counter *next;
+} mem_counter;
+
+static mem_counter* get_mem_counter(mem_counter **root, struct qm_frag* f)
+{
+	mem_counter *x;
+	if (!*root) goto make_new;
+	for(x=*root;x;x=x->next)
+		if (x->file == f->file && x->func == f->func && x->line == f->line)
+			return x;
+make_new:	
+	x = malloc(sizeof(mem_counter));
+	x->file = f->file;
+	x->func = f->func;
+	x->line = f->line;
+	x->count = 0;
+	x->size = 0;
+	x->next = *root;
+	*root = x;
+	return x;
+}
+
+
+
+void qm_sums(struct qm_block* qm)
+{
+	struct qm_frag* f;
+	int i;
+	mem_counter *root, *x;
+	
+	root=0;
+	if (!qm) return;
+	
+	LOG(memlog, "summarizing all alloc'ed. fragments:\n");
+	
+	for (f=qm->first_frag, i=0;(char*)f<(char*)qm->last_frag_end;
+			f=FRAG_NEXT(f),i++){
+		if (! f->u.is_free){
+			x = get_mem_counter(&root,f);
+			x->count++;
+			x->size+=f->size;
+		}
+	}
+	x = root;
+	while(x){
+		LOG(memlog, " count=%6d size=%10lu bytes from %s: %s(%ld)\n",
+			x->count,x->size,
+			x->file, x->func, x->line
+			);
+		root = x->next;
+		free(x);
+		x = root;
+	}
+	LOG(memlog, "-----------------------------\n");
+}
+#endif /* DBG_QM_MALLOC */
+
+
 #endif
diff --git a/mem/q_malloc.h b/mem/q_malloc.h
index 7963cff..6ebd7f6 100644
--- a/mem/q_malloc.h
+++ b/mem/q_malloc.h
@@ -153,4 +153,10 @@ void  qm_info(struct qm_block*, struct mem_info*);
 
 unsigned long qm_available(struct qm_block* qm);
 
+#ifdef DBG_QM_MALLOC
+void qm_sums(struct qm_block* qm);
+#else
+#define qm_sums(v) do{}while(0)
+#endif /*DBQ_QM_MALLOC */
+
 #endif
diff --git a/mem/shm_mem.h b/mem/shm_mem.h
index a489bba..fd6b7f3 100644
--- a/mem/shm_mem.h
+++ b/mem/shm_mem.h
@@ -98,6 +98,7 @@
 #	define shm_free_unsafe shm_free
 #	define shm_available	sfm_available(shm_block)
 #	define shm_status() sfm_status(shm_block)
+#	define shm_sums() do{}while(0)
 #	define shm_malloc_init sfm_malloc_init
 #	define shm_malloc_destroy(b) sfm_malloc_destroy(b)
 #	define shm_malloc_on_fork()	sfm_pool_reset()
@@ -124,6 +125,7 @@
 #	define shm_free_unsafe shm_free
 #	define shm_available	sfm_available(shm_block)
 #	define shm_status() sfm_status(shm_block)
+#	define shm_sums() do{}while(0)
 #	define shm_malloc_init sfm_malloc_init
 #	define shm_malloc_destroy(b) sfm_malloc_destroy(b)
 #	define shm_malloc_on_fork()	sfm_pool_reset()
@@ -133,6 +135,7 @@
 #	define MY_MALLOC vqm_malloc
 #	define MY_FREE vqm_free
 #	define MY_STATUS vqm_status
+#	define MY_SUMS do{}while(0)
 #	define  shm_malloc_init vqm_malloc_init
 #	define shm_malloc_destroy(b) do{}while(0)
 #	define shm_malloc_on_fork() do{}while(0)
@@ -145,6 +148,7 @@
 #	define MY_REALLOC fm_realloc
 #	define MY_STATUS fm_status
 #	define MY_MEMINFO	fm_info
+#	define MY_SUMS	fm_sums
 #	define  shm_malloc_init fm_malloc_init
 #	define shm_malloc_destroy(b) do{}while(0)
 #	define shm_available() fm_available(shm_block)
@@ -156,6 +160,7 @@
 #	define MY_FREE mspace_free
 #	define MY_REALLOC mspace_realloc
 #	define MY_STATUS(...) 0
+#	define MY_SUMS do{}while(0)
 #	define MY_MEMINFO	mspace_info
 #	define  shm_malloc_init(buf, len) create_mspace_with_base(buf, len, 0)
 #	define shm_malloc_destroy(b) do{}while(0)
@@ -168,6 +173,7 @@
 #	define MY_REALLOC qm_realloc
 #	define MY_STATUS qm_status
 #	define MY_MEMINFO	qm_info
+#	define MY_SUMS	qm_sums
 #	define  shm_malloc_init qm_malloc_init
 #	define shm_malloc_destroy(b) do{}while(0)
 #	define shm_available() qm_available(shm_block)
@@ -318,6 +324,16 @@ do{\
 	shm_unlock(); \
 }while(0)
 
+#ifdef MY_SUMS
+#define shm_sums() \
+	do { \
+		shm_lock(); \
+		MY_SUMS(shm_block); \
+		shm_unlock(); \
+	}while(0)
+	
+#endif /* MY_SUMS */
+
 #endif /* ! SHM_SAFE_MALLOC */
 
 #endif /* shm_mem_h */




More information about the sr-dev mailing list