lib: do not allocate/free thread funcnames

This avoids memory heap fragmentation and imposses less load on the
system memory allocator.

* thread.h: FUNCNAME_LEN defined to 64 (ISO C99 says max 63)

Signed-off-by: Jorge Boncompte [DTI2] <jorge@dti2.net>
[changed FUNCNAME_LEN to a less arbitrary value]
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
This commit is contained in:
Jorge Boncompte [DTI2] 2012-05-07 16:53:14 +00:00 committed by David Lamparter
parent 64018324d5
commit 22714f99c4
3 changed files with 16 additions and 29 deletions

View File

@ -21,7 +21,6 @@ struct memory_list memory_list_lib[] =
{ MTYPE_THREAD, "Thread" },
{ MTYPE_THREAD_MASTER, "Thread master" },
{ MTYPE_THREAD_STATS, "Thread stats" },
{ MTYPE_THREAD_FUNCNAME, "Thread function name" },
{ MTYPE_VTY, "VTY" },
{ MTYPE_VTY_OUT_BUF, "VTY output buffer" },
{ MTYPE_VTY_HIST, "VTY history" },

View File

@ -235,7 +235,7 @@ cpu_record_hash_alloc (struct cpu_thread_history *a)
struct cpu_thread_history *new;
new = XCALLOC (MTYPE_THREAD_STATS, sizeof (struct cpu_thread_history));
new->func = a->func;
new->funcname = XSTRDUP(MTYPE_THREAD_FUNCNAME, a->funcname);
strcpy(new->funcname, a->funcname);
return new;
}
@ -244,7 +244,6 @@ cpu_record_hash_free (void *a)
{
struct cpu_thread_history *hist = a;
XFREE (MTYPE_THREAD_FUNCNAME, hist->funcname);
XFREE (MTYPE_THREAD_STATS, hist);
}
@ -303,7 +302,7 @@ cpu_record_print(struct vty *vty, thread_type filter)
void *args[3] = {&tmp, vty, &filter};
memset(&tmp, 0, sizeof tmp);
tmp.funcname = (char *)"TOTAL";
strcpy(tmp.funcname, "TOTAL");
tmp.types = filter;
#ifdef HAVE_RUSAGE
@ -577,8 +576,6 @@ thread_list_free (struct thread_master *m, struct thread_list *list)
for (t = list->head; t; t = next)
{
next = t->next;
if (t->funcname)
XFREE (MTYPE_THREAD_FUNCNAME, t->funcname);
XFREE (MTYPE_THREAD, t);
list->count--;
m->alloc--;
@ -636,11 +633,11 @@ thread_timer_remain_second (struct thread *thread)
}
/* Trim blankspace and "()"s */
static char *
strip_funcname (const char *funcname)
void
strip_funcname (char *dest, const char *funcname)
{
char buff[100];
char tmp, *ret, *e, *b = buff;
char buff[FUNCNAME_LEN];
char tmp, *e, *b = buff;
strncpy(buff, funcname, sizeof(buff));
buff[ sizeof(buff) -1] = '\0';
@ -656,10 +653,8 @@ strip_funcname (const char *funcname)
tmp = *e;
*e = '\0';
ret = XSTRDUP (MTYPE_THREAD_FUNCNAME, b);
strcpy (dest, b);
*e = tmp;
return ret;
}
/* Get new thread. */
@ -669,12 +664,7 @@ thread_get (struct thread_master *m, u_char type,
{
struct thread *thread = thread_trim_head (&m->unuse);
if (thread)
{
if (thread->funcname)
XFREE(MTYPE_THREAD_FUNCNAME, thread->funcname);
}
else
if (! thread)
{
thread = XCALLOC (MTYPE_THREAD, sizeof (struct thread));
m->alloc++;
@ -685,7 +675,7 @@ thread_get (struct thread_master *m, u_char type,
thread->func = func;
thread->arg = arg;
thread->funcname = strip_funcname(funcname);
strip_funcname (thread->funcname, funcname);
return thread;
}
@ -953,7 +943,6 @@ thread_run (struct thread_master *m, struct thread *thread,
{
*fetch = *thread;
thread->type = THREAD_UNUSED;
thread->funcname = NULL; /* thread_call will free fetch's copied pointer */
thread_add_unuse (m, thread);
return fetch;
}
@ -1187,7 +1176,7 @@ thread_call (struct thread *thread)
struct cpu_thread_history tmp;
tmp.func = thread->func;
tmp.funcname = thread->funcname;
strcpy(tmp.funcname, thread->funcname);
thread->hist = hash_get (cpu_record, &tmp,
(void * (*) (void *))cpu_record_hash_alloc);
@ -1227,8 +1216,6 @@ thread_call (struct thread *thread)
realtime/1000, cputime/1000);
}
#endif /* CONSUMED_TIME_CHECK */
XFREE (MTYPE_THREAD_FUNCNAME, thread->funcname);
}
/* Execute thread */
@ -1249,10 +1236,8 @@ funcname_thread_execute (struct thread_master *m,
dummy.func = func;
dummy.arg = arg;
dummy.u.val = val;
dummy.funcname = strip_funcname (funcname);
strip_funcname (dummy.funcname, funcname);
thread_call (&dummy);
XFREE (MTYPE_THREAD_FUNCNAME, dummy.funcname);
return NULL;
}

View File

@ -62,6 +62,9 @@ struct thread_master
typedef unsigned char thread_type;
/* ISO C99 maximum function name length is 63 */
#define FUNCNAME_LEN 64
/* Thread itself. */
struct thread
{
@ -79,13 +82,12 @@ struct thread
} u;
struct timeval real;
struct cpu_thread_history *hist; /* cache pointer to cpu_history */
char* funcname;
char funcname[FUNCNAME_LEN];
};
struct cpu_thread_history
{
int (*func)(struct thread *);
char *funcname;
unsigned int total_calls;
struct time_stats
{
@ -95,6 +97,7 @@ struct cpu_thread_history
struct time_stats cpu;
#endif
thread_type types;
char funcname[FUNCNAME_LEN];
};
/* Clocks supported by Quagga */