Skip to content

Commit

Permalink
struct history tracker: Optimize memory allocations
Browse files Browse the repository at this point in the history
Especially useful when used with QM_MALLOC.
  • Loading branch information
liviuchircu committed Jul 13, 2017
1 parent 991b3ef commit eb22f4e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 17 deletions.
17 changes: 1 addition & 16 deletions lib/dbg/struct_hist.c
Expand Up @@ -125,11 +125,6 @@ struct struct_hist *sh_push(void *obj, struct struct_hist_list *list)

static void sh_free(struct struct_hist *sh)
{
int i;

for (i = 0; i < sh->max_len && sh->actions[i].log; i++)
shm_free(sh->actions[i].log);

shm_free(sh->actions);
shm_free(sh);
}
Expand Down Expand Up @@ -218,22 +213,12 @@ int sh_log(struct struct_hist *sh, enum struct_hist_verb verb, char *fmt, ...)
act->verb = verb;
act->t = get_uticks();
act->pid = my_pid();
if (act->log) {
memset(act->log, 0, MAX_SHLOG_SIZE);
} else {
act->log = shm_malloc(MAX_SHLOG_SIZE);
if (!act->log) {
lock_release(&sh->wlock);
LM_ERR("oom\n");
return -1;
}
}

n = vsnprintf(act->log, MAX_SHLOG_SIZE, fmt, ap);
lock_release(&sh->wlock);

if (n < 0 || n >= MAX_SHLOG_SIZE) {
LM_ERR("formatting failed with n=%d, %s\n", n, strerror(errno));
LM_INFO("formatting failed with n=%d, %s\n", n, strerror(errno));
}

va_end(ap);
Expand Down
4 changes: 3 additions & 1 deletion lib/dbg/struct_hist.h
Expand Up @@ -47,6 +47,8 @@

#define ENABLE_SH_LOGGING

#define MAX_SHLOG_SIZE 50 /* longer log lines will get truncated */

/**
* To be freely extended by any piece of OpenSIPS code which makes use of
* struct history logging
Expand All @@ -69,7 +71,7 @@ struct struct_hist_action {
enum struct_hist_verb verb;
utime_t t;
int pid;
char *log;
char log[MAX_SHLOG_SIZE];
};

#define ACTIONS_SIZE 5
Expand Down

0 comments on commit eb22f4e

Please sign in to comment.