[Serdev] Finding a memory leak

Vaclav Kubart vaclav.kubart at iptel.org
Wed Apr 12 12:22:45 UTC 2006


Hi,
I don't know how do it others - I tried to search something in dumped
memory blocks when ser stops, but it seems too hard for me (too much
dumps).

I'm using modified shm_mem.h which loggs allocations and deallocations
in simple way (simple to parse). 

I have a utility which goes through such log file and puts together
allocations and deallocations logged by my modified shm_mem.h and
unmatched allocations dumps on output with lines in log file where they
were...

Example log part:

...
Apr  6 16:01:04 localhost /home/kubartv/SER/sbin/ser[6978]: INFO: ../../mem/shm_mem.h:152: ALLOC 0xb5b36f34 size 21 from ucontact.c(81) 
Apr  6 16:01:04 localhost /home/kubartv/SER/sbin/ser[6978]: INFO: ../../mem/shm_mem.h:152: ALLOC 0xb5b36f7c size 12 from ucontact.c(89) 
Apr  6 16:01:04 localhost /home/kubartv/SER/sbin/ser[6978]: MSILO: offline messages for sip:smith at test-domain.com dumped 
Apr  6 16:01:04 localhost /home/kubartv/SER/sbin/ser[6978]: INFO: usr_avp.c:605: FREE 0xb5b36db0 from usr_avp.c(605) 
Apr  6 16:01:04 localhost /home/kubartv/SER/sbin/ser[6980]: INFO: ../../mem/shm_mem.h:152: ALLOC 0xb5b36fb8 size 2684 from h_table.c(230) 
Apr  6 16:01:04 localhost /home/kubartv/SER/sbin/ser[6980]: INFO: ../../mem/shm_mem.h:152: ALLOC 0xb5b37a64 size 2604 from sip_msg.c(409) 
...

The utility puts together places with ALLOC and FREE of the same
address.

bash$ ./memlog daemon.log 
allocated blocks:
1: Apr  6 16:00:41 localhost ser: INFO: mem/shm_mem.h:152: ALLOC 0xb58eb440 size 4 from timer.c(224) 
2: Apr  6 16:00:41 localhost ser: INFO: mem/shm_mem.h:152: ALLOC 0xb58eb474 size 4 from timer.c(237) 
3: Apr  6 16:00:41 localhost ser: INFO: mem/shm_mem.h:152: ALLOC 0xb58eb4a8 size 139272 from timer.c(238) 
4: Apr  6 16:00:41 localhost ser: INFO: mem/shm_mem.h:152: ALLOC 0xb590d4e0 size 4 from timer.c(256) 
...

The utility is not too nice, but it seems to be working...

May be your log format will be a bit different, thus you might need to
modify "parsing" the log file.

I attached my shm_mem.h and source for the utility.

It is good to clear the log (remove log file and restart syslog) before
running ser. And I do marks in log file when starts watched action and
run the utility only on the requested part of log...

	Vaclav

On Wed, Apr 12, 2006 at 01:57:34PM +0200, Pupeno wrote:
> Hello,
> I am trying to see if my module for SER has a memory leak, so I've set
> memlog to 2 and now I have tons of messages about memory.
> Doing a bit of search I've found things such as:
> 
>  2(12621)     403. N  address=0x813b3d8 frag=0x813b3c0 size=4 used=1
>  2(12621)             alloc'd from dialplan.c: mod_init(199)
>  2(12621)         start check=f0f0f0f0, end check= c0c0c0c0, abcdefed
> 
> but as far as I know this is not sign of a memory leak, right ?
> What should I be looking for ?
> 
> Thank you.
> -- 
> Pupeno <pupeno at pupeno.com> http://pupeno.com



> _______________________________________________
> Serdev mailing list
> serdev at lists.iptel.org
> http://lists.iptel.org/mailman/listinfo/serdev

-------------- next part --------------
/* $Id: shm_mem.h,v 1.27 2005/08/29 12:34:57 andrei Exp $*
 *
 * shared mem stuff
 *
 * Copyright (C) 2001-2003 FhG Fokus
 *
 * This file is part of ser, a free SIP server.
 *
 * ser is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version
 *
 * For a license to use the ser software under conditions
 * other than those described here, or to purchase support for this
 * software, please contact iptel.org by e-mail at the following addresses:
 *    info at iptel.org
 *
 * ser is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
/*
 * History:
 * --------
 *  2003-06-29  added shm_realloc & replaced shm_resize (andrei)
 *  2003-11-19  reverted shm_resize to the old version, using
 *               realloc causes terrible fragmentation  (andrei)
 * 2005-03-02   added shm_info() & re-eneabled locking on shm_status (andrei)
 */


#ifdef SHM_MEM

#ifndef shm_mem_h
#define shm_mem_h

#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>

#ifndef SHM_MMAP

#include <sys/shm.h>

#endif

#include <sys/sem.h>
#include <string.h>
#include <errno.h>

/* fix DBG MALLOC stuff */

/* fix debug defines, DBG_F_MALLOC <=> DBG_QM_MALLOC */
#ifdef F_MALLOC
	#ifdef DBG_F_MALLOC
		#ifndef DBG_QM_MALLOC
			#define DBG_QM_MALLOC
		#endif
	#elif defined(DBG_QM_MALLOC)
		#define DBG_F_MALLOC
	#endif
#endif



#include "../dprint.h"
#include "../lock_ops.h" /* we don't include locking.h on purpose */

#ifdef VQ_MALLOC
#	include "vq_malloc.h"
	extern struct vqm_block* shm_block;
#	define MY_MALLOC vqm_malloc
#	define MY_FREE vqm_free
#	define MY_STATUS vqm_status
#	define  shm_malloc_init vqm_malloc_init
#	warn "no proper vq_realloc implementation, try another memory allocator"
#elif defined F_MALLOC
#	include "f_malloc.h"
	extern struct fm_block* shm_block;
#	define MY_MALLOC fm_malloc
#	define MY_FREE fm_free
#	define MY_REALLOC fm_realloc
#	define MY_STATUS fm_status
#	define MY_MEMINFO	fm_info
#	define  shm_malloc_init fm_malloc_init
#else
#	include "q_malloc.h"
	extern struct qm_block* shm_block;
#	define MY_MALLOC qm_malloc
#	define MY_FREE qm_free
#	define MY_REALLOC qm_realloc
#	define MY_STATUS qm_status
#	define MY_MEMINFO	qm_info
#	define  shm_malloc_init qm_malloc_init
#endif

#include <stdio.h>
#include <stdlib.h>
	
/*#define write_debug(s,args...)	do { \
	FILE *f = fopen("/tmp/mem.log", "a"); \
	if (f) { \
		fprintf(f,s,##args); \
		fclose(f); \
		INFO(s,##args); \
	} } while(0);
*/	
#define	 write_debug(s,args...)	INFO(s,##args)

	extern gen_lock_t* mem_lock;


int shm_mem_init(); /* calls shm_getmem & shm_mem_init_mallocs */
int shm_getmem();   /* allocates the memory (mmap or sysv shmap) */
int shm_mem_init_mallocs(void* mempool, unsigned long size); /* initialize
																the mallocs
																& the lock */
void shm_mem_destroy();



#define shm_lock()    lock_get(mem_lock)
#define shm_unlock()  lock_release(mem_lock)


#ifdef DBG_QM_MALLOC

#ifdef __SUNPRO_C
		#define __FUNCTION__ ""  /* gcc specific */
#endif


#define shm_malloc_unsafe(_size ) \
	MY_MALLOC(shm_block, (_size), __FILE__, __FUNCTION__, __LINE__ )


inline static void* _shm_malloc(unsigned int size, 
	const char *file, const char *function, int line )
{
	void *p;
	
	shm_lock();
	p=MY_MALLOC(shm_block, size, file, function, line );
	shm_unlock();
	write_debug("ALLOC %p size %u from %s(%d)\n", p, size, file, line);
	
	return p; 
}


inline static void* _shm_realloc(void *ptr, unsigned int size, 
		const char* file, const char* function, int line )
{
	void *p;
	shm_lock();
	p=MY_REALLOC(shm_block, ptr, size, file, function, line);
	shm_unlock();
	return p;
}

#define shm_malloc( _size ) _shm_malloc((_size), \
	__FILE__, __FUNCTION__, __LINE__ )

#define shm_realloc( _ptr, _size ) _shm_realloc( (_ptr), (_size), \
	__FILE__, __FUNCTION__, __LINE__ )



#define shm_free_unsafe( _p  ) \
	do { \
		write_debug("FREE %p from %s(%d)\n", _p, __FILE__, __LINE__); \
		MY_FREE( shm_block, (_p), __FILE__, __FUNCTION__, __LINE__ ); \
	} while(0)

#define shm_free(_p) \
do { \
		shm_lock(); \
		shm_free_unsafe( (_p)); \
		shm_unlock(); \
}while(0)



void* _shm_resize(void* ptr, unsigned int size, const char* f, const char* fn,
					int line);
#define shm_resize(_p, _s ) _shm_resize((_p), (_s), \
		__FILE__, __FUNCTION__, __LINE__ )
/*#define shm_resize(_p, _s ) shm_realloc( (_p), (_s))*/



#else /*DBQ_QM_MALLOC*/


#define shm_malloc_unsafe(_size) MY_MALLOC(shm_block, (_size))

inline static void* shm_malloc(unsigned int size)
{
	void *p;
	
	shm_lock();
	p=shm_malloc_unsafe(size);
	shm_unlock();

	 return p; 
}


inline static void* shm_realloc(void *ptr, unsigned int size)
{
	void *p;
	shm_lock();
	p=MY_REALLOC(shm_block, ptr, size);
	shm_unlock();
	return p;
}



#define shm_free_unsafe( _p ) MY_FREE(shm_block, (_p))

#define shm_free(_p) \
do { \
		shm_lock(); \
		shm_free_unsafe( _p ); \
		shm_unlock(); \
}while(0)



void* _shm_resize(void* ptr, unsigned int size);
#define shm_resize(_p, _s) _shm_resize( (_p), (_s))
/*#define shm_resize(_p, _s) shm_realloc( (_p), (_s))*/


#endif


#define shm_status() \
do { \
		shm_lock(); \
		MY_STATUS(shm_block); \
		shm_unlock(); \
}while(0)


#define shm_info(mi) \
do{\
	shm_lock(); \
	MY_MEMINFO(shm_block, mi); \
	shm_unlock(); \
}while(0)


#endif

#endif

-------------- next part --------------
A non-text attachment was scrubbed...
Name: memlog.c
Type: text/x-csrc
Size: 3645 bytes
Desc: not available
Url : http://lists.iptel.org/pipermail/serdev/attachments/20060412/7e2891f2/memlog.c


More information about the Serdev mailing list