Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kex: per module memory debugging #225

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions events.c
Expand Up @@ -165,6 +165,15 @@ int sr_event_register_cb(int type, sr_event_cb_f f)
_sr_events_list.rcv_nosip = f;
else return -1;
break;
case SREV_MODULE_PKG_STATS:
if(_sr_events_list.mod_update_pkg_stats==0)
_sr_events_list.mod_update_pkg_stats = f;
else return -1;
case SREV_MODULE_SHM_STATS:
if(_sr_events_list.mod_update_shm_stats==0)
_sr_events_list.mod_update_shm_stats = f;
else return -1;
break;
default:
return -1;
}
Expand Down Expand Up @@ -284,6 +293,18 @@ int sr_event_exec(int type, void *data)
ret = _sr_events_list.rcv_nosip(data);
return ret;
} else return 1;
case SREV_MODULE_PKG_STATS:
if(unlikely(_sr_events_list.mod_update_pkg_stats!=0))
{
ret = _sr_events_list.mod_update_pkg_stats(data);
return ret;
} else return 1;
case SREV_MODULE_SHM_STATS:
if(unlikely(_sr_events_list.mod_update_shm_stats!=0))
{
ret = _sr_events_list.mod_update_shm_stats(data);
return ret;
} else return 1;
default:
return -1;
}
Expand Down Expand Up @@ -319,6 +340,10 @@ int sr_event_enabled(int type)
return (_sr_events_list.stun_in!=0)?1:0;
case SREV_RCV_NOSIP:
return (_sr_events_list.rcv_nosip!=0)?1:0;
case SREV_MODULE_PKG_STATS:
return (_sr_events_list.mod_update_pkg_stats!=0)?1:0;
case SREV_MODULE_SHM_STATS:
return (_sr_events_list.mod_update_shm_stats!=0)?1:0;
}
return 0;
}
Expand Down
4 changes: 4 additions & 0 deletions events.h
Expand Up @@ -34,6 +34,8 @@
#define SREV_TCP_WS_FRAME_IN 10
#define SREV_TCP_WS_FRAME_OUT 11
#define SREV_STUN_IN 12
#define SREV_MODULE_PKG_STATS 13
#define SREV_MODULE_SHM_STATS 14

#define SREV_CB_LIST_SIZE 3

Expand All @@ -52,6 +54,8 @@ typedef struct sr_event_cb {
sr_event_cb_f tcp_ws_frame_out;
sr_event_cb_f stun_in;
sr_event_cb_f rcv_nosip;
sr_event_cb_f mod_update_pkg_stats;
sr_event_cb_f mod_update_shm_stats;
} sr_event_cb_t;

void sr_event_cb_init(void);
Expand Down
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