Skip to content

Commit

Permalink
mem: f_malloc extended for per module memory stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Mititelu authored and miconda committed Nov 2, 2015
1 parent 1755c1b commit ea189de
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 39 deletions.
130 changes: 96 additions & 34 deletions mem/f_malloc.c
Expand Up @@ -265,7 +265,8 @@ static inline
#ifdef DBG_F_MALLOC
void fm_split_frag(struct fm_block* qm, struct fm_frag* frag,
unsigned long size,
const char* file, const char* func, unsigned int line)
const char* file, const char* func, unsigned int line,
const char* mname)
#else
void fm_split_frag(struct fm_block* qm, struct fm_frag* frag,
unsigned long size)
Expand All @@ -291,6 +292,7 @@ void fm_split_frag(struct fm_block* qm, struct fm_frag* frag,
n->file=file;
n->func="frag. from fm_split_frag";
n->line=line;
n->mname=mname;
#endif
n->check=ST_CHECK_PATTERN;
/* reinsert n in free list*/
Expand Down Expand Up @@ -419,8 +421,8 @@ struct fm_frag* fm_search_defrag(struct fm_block* qm, unsigned long size)
* \return address of allocated memory
*/
#ifdef DBG_F_MALLOC
void* fm_malloc(void* qmp, unsigned long size,
const char* file, const char* func, unsigned int line)
void* fm_malloc(void* qmp, unsigned long size, const char* file,
const char* func, unsigned int line, const char* mname)
#else
void* fm_malloc(void* qmp, unsigned long size)
#endif
Expand Down Expand Up @@ -496,7 +498,7 @@ void* fm_malloc(void* qmp, unsigned long size)

/*see if use full frag or split it in two*/
#ifdef DBG_F_MALLOC
fm_split_frag(qm, frag, size, file, func, line);
fm_split_frag(qm, frag, size, file, func, line, mname);
#else
fm_split_frag(qm, frag, size);
#endif
Expand All @@ -506,6 +508,7 @@ void* fm_malloc(void* qmp, unsigned long size)
#ifdef DBG_F_MALLOC
frag->file=file;
frag->func=func;
frag->mname=mname;
frag->line=line;
MDBG("fm_malloc(%p, %lu) returns address %p \n", qm, size,
(char*)frag+sizeof(struct fm_frag));
Expand Down Expand Up @@ -557,8 +560,8 @@ static void fm_join_frag(struct fm_block* qm, struct fm_frag* f)
* \param p freed memory
*/
#ifdef DBG_F_MALLOC
void fm_free(void* qmp, void* p, const char* file, const char* func,
unsigned int line)
void fm_free(void* qmp, void* p, const char* file, const char* func,
unsigned int line, const char* mname)
#else
void fm_free(void* qmp, void* p)
#endif
Expand Down Expand Up @@ -602,6 +605,7 @@ void fm_free(void* qmp, void* p)
f->file=file;
f->func=func;
f->line=line;
f->mname=mname;
#endif
#ifdef MEM_JOIN_FREE
if(unlikely(cfg_get(core, core_cfg, mem_join)!=0))
Expand All @@ -622,7 +626,8 @@ void fm_free(void* qmp, void* p)
*/
#ifdef DBG_F_MALLOC
void* fm_realloc(void* qmp, void* p, unsigned long size,
const char* file, const char* func, unsigned int line)
const char* file, const char* func, unsigned int line,
const char *mname)
#else
void* fm_realloc(void* qmp, void* p, unsigned long size)
#endif
Expand All @@ -648,15 +653,15 @@ void* fm_realloc(void* qmp, void* p, unsigned long size)
if (size==0) {
if (p)
#ifdef DBG_F_MALLOC
fm_free(qm, p, file, func, line);
fm_free(qm, p, file, func, line, mname);
#else
fm_free(qm, p);
#endif
return 0;
}
if (p==0)
#ifdef DBG_F_MALLOC
return fm_malloc(qm, size, file, func, line);
return fm_malloc(qm, size, file, func, line, mname);
#else
return fm_malloc(qm, size);
#endif
Expand All @@ -671,7 +676,7 @@ void* fm_realloc(void* qmp, void* p, unsigned long size)
/* shrink */
#ifdef DBG_F_MALLOC
MDBG("fm_realloc: shrinking from %lu to %lu\n", f->size, size);
fm_split_frag(qm, f, size, file, "frag. from fm_realloc", line);
fm_split_frag(qm, f, size, file, "frag. from fm_realloc", line, mname);
#else
fm_split_frag(qm, f, size);
#endif
Expand All @@ -695,15 +700,15 @@ void* fm_realloc(void* qmp, void* p, unsigned long size)
if (f->size > size){
#ifdef DBG_F_MALLOC
fm_split_frag(qm, f, size, file, "fragm. from fm_realloc",
line);
line, mname);
#else
fm_split_frag(qm, f, size);
#endif
}
}else{
/* could not join => realloc */
#ifdef DBG_F_MALLOC
ptr=fm_malloc(qm, size, file, func, line);
ptr=fm_malloc(qm, size, file, func, line, mname);
#else
ptr=fm_malloc(qm, size);
#endif
Expand All @@ -712,7 +717,7 @@ void* fm_realloc(void* qmp, void* p, unsigned long size)
memcpy(ptr, p, orig_size);
}
#ifdef DBG_F_MALLOC
fm_free(qm, p, file, func, line);
fm_free(qm, p, file, func, line, mname);
#else
fm_free(qm, p);
#endif
Expand Down Expand Up @@ -873,17 +878,6 @@ unsigned long fm_available(void* qmp)

#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;
Expand All @@ -897,6 +891,7 @@ static mem_counter* get_mem_counter(mem_counter **root,struct fm_frag* f)
x->file = f->file;
x->func = f->func;
x->line = f->line;
x->mname = f->mname;
x->count = 0;
x->size = 0;
x->next = *root;
Expand Down Expand Up @@ -947,9 +942,60 @@ void fm_sums(void* qmp)
}
LOG_(DEFAULT_FACILITY, memlog, "fm_status: ",
"-----------------------------\n");

}


void fm_mod_get_stats(void *qmp, void **fm_rootp)
{
if (!fm_rootp) {
return ;
}

LM_DBG("get fm memory statistics\n");

struct fm_block *qm = (struct fm_block *) qmp;
mem_counter **fm_root = (mem_counter **) fm_rootp;
struct fm_frag* f;
int i;
mem_counter *x;

if (!qm) return ;

/* update fragment detail list */
for (f=qm->first_frag, i=0; (char*)f<(char*)qm->last_frag;
f=FRAG_NEXT(f), i++){
if (f->is_free==0){
x = get_mem_counter(fm_root,f);
x->count++;
x->size+=f->size;
}
}

return ;
}

void fm_mod_free_stats(void *fm_rootp)
{
if (!fm_rootp) {
return ;
}

LM_DBG("free fm memory statistics\n");

mem_counter *root = (mem_counter *) fm_rootp;
mem_counter *new, *old;
new = root;
old = root;

while (new) {
old = new;
new = new->next;
free(old);
}
}
#else
void fm_sums(void* qmp)
void fm_sums(void *qmp)
{
struct fm_block* qm;
int memlog;
Expand All @@ -959,6 +1005,18 @@ void fm_sums(void* qmp)
LOG_(DEFAULT_FACILITY, memlog, "fm_sums: ", "not available (%p)\n", qm);
return;
}

void fm_mod_get_stats(void *qmp, void **fm_rootp)
{
LM_WARN("Enable DBG_F_MALLOC for getting statistics\n");
return ;
}

void fm_mod_free_stats(void *fm_rootp)
{
LM_WARN("Enable DBG_F_MALLOC for freeing statistics\n");
return ;
}
#endif /* DBG_F_MALLOC */


Expand Down Expand Up @@ -1009,6 +1067,8 @@ int fm_malloc_init_pkg_manager(void)
ma.xavailable = fm_available;
ma.xsums = fm_sums;
ma.xdestroy = fm_malloc_destroy_pkg_manager;
ma.xstats = fm_mod_get_stats;
ma.xfstats = fm_mod_free_stats;

return pkg_init_api(&ma);
}
Expand All @@ -1021,38 +1081,38 @@ static struct fm_block *_fm_shm_block = 0;
/*SHM wrappers to sync the access to memory block*/
#ifdef DBG_F_MALLOC
void* fm_shm_malloc(void* qmp, unsigned long size,
const char* file, const char* func, unsigned int line)
const char* file, const char* func, unsigned int line, const char* mname)
{
void *r;
shm_lock();
r = fm_malloc(qmp, size, file, func, line);
r = fm_malloc(qmp, size, file, func, line, mname);
shm_unlock();
return r;
}
void* fm_shm_realloc(void* qmp, void* p, unsigned long size,
const char* file, const char* func, unsigned int line)
const char* file, const char* func, unsigned int line, const char* mname)
{
void *r;
shm_lock();
r = fm_realloc(qmp, p, size, file, func, line);
r = fm_realloc(qmp, p, size, file, func, line, mname);
shm_unlock();
return r;
}
void* fm_shm_resize(void* qmp, void* p, unsigned long size,
const char* file, const char* func, unsigned int line)
const char* file, const char* func, unsigned int line, const char* mname)
{
void *r;
shm_lock();
if(p) fm_free(qmp, p, file, func, line);
r = fm_malloc(qmp, size, file, func, line);
if(p) fm_free(qmp, p, file, func, line, mname);
r = fm_malloc(qmp, size, file, func, line, mname);
shm_unlock();
return r;
}
void fm_shm_free(void* qmp, void* p, const char* file, const char* func,
unsigned int line)
unsigned int line, const char* mname)
{
shm_lock();
fm_free(qmp, p, file, func, line);
fm_free(qmp, p, file, func, line, mname);
shm_unlock();
}
#else
Expand Down Expand Up @@ -1158,6 +1218,8 @@ int fm_malloc_init_shm_manager(void)
ma.xavailable = fm_shm_available;
ma.xsums = fm_shm_sums;
ma.xdestroy = fm_malloc_destroy_shm_manager;
ma.xstats = fm_mod_get_stats;
ma.xfstats = fm_mod_free_stats;

if(shm_init_api(&ma)<0) {
LM_ERR("cannot initialize the core shm api\n");
Expand Down
26 changes: 21 additions & 5 deletions mem/f_malloc.h
Expand Up @@ -86,6 +86,7 @@ struct fm_frag{
#ifdef DBG_F_MALLOC
const char* file;
const char* func;
const char* mname;
unsigned long line;
#endif
unsigned int check;
Expand Down Expand Up @@ -134,7 +135,8 @@ struct fm_block* fm_malloc_init(char* address, unsigned long size, int type);
*/
#ifdef DBG_F_MALLOC
void* fm_malloc(void* qmp, unsigned long size,
const char* file, const char* func, unsigned int line);
const char* file, const char* func, unsigned int line,
const char* mname);
#else
void* fm_malloc(void* qmp, unsigned long size);
#endif
Expand All @@ -148,8 +150,8 @@ void* fm_malloc(void* qmp, unsigned long size);
* \param p freed memory
*/
#ifdef DBG_F_MALLOC
void fm_free(void* qmp, void* p, const char* file, const char* func,
unsigned int line);
void fm_free(void* qmp, void* p, const char* file, const char* func,
unsigned int line, const char* mname);
#else
void fm_free(void* qmp, void* p);
#endif
Expand All @@ -165,8 +167,8 @@ void fm_free(void* qmp, void* p);
* \return reallocated memory block
*/
#ifdef DBG_F_MALLOC
void* fm_realloc(void* qmp, void* p, unsigned long size,
const char* file, const char* func, unsigned int line);
void* fm_realloc(void* qmp, void* p, unsigned long size,
const char* file, const char* func, unsigned int line, const char *mname);
#else
void* fm_realloc(void* qmp, void* p, unsigned long size);
#endif
Expand Down Expand Up @@ -204,6 +206,20 @@ unsigned long fm_available(void* qmp);
* \param qm memory block
*/
void fm_sums(void* qmp);
void fm_mod_get_stats(void* qm, void **fm_root);
void fm_mod_free_stats(void *root);

typedef struct _mem_counter{
const char *file;
const char *func;
const char *mname;
unsigned long line;

unsigned long size;
int count;

struct _mem_counter *next;
} mem_counter;

#endif
#endif

0 comments on commit ea189de

Please sign in to comment.