Skip to content
Permalink
Browse files

lib: Limit depth of unused thread list

The master->unused list was unbounded during normal operation.
A full BGP feed on my machine left 11k threads on the unused
list, taking up over 2mb of data.  This seemed a bit excessive,
reduce to a limit of 10.

Also fix a crash that this exposed where we assumed that a thread
structure was not deleted.

Future committers can make this configurable? or modify
the value to something better for their system.  I am
dubious of the value of this.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
  • Loading branch information...
donaldsharp committed Aug 23, 2018
1 parent a6f235f commit 6ed04aa2ebcb350046cbffa2e7452e8588aa7690
Showing with 16 additions and 8 deletions.
  1. +16 −8 lib/thread.c
@@ -532,17 +532,23 @@ static struct thread *thread_trim_head(struct thread_list *list)
return NULL;
}

#define THREAD_UNUSED_DEPTH 10

/* Move thread to unuse list. */
static void thread_add_unuse(struct thread_master *m, struct thread *thread)
{
assert(m != NULL && thread != NULL);
assert(thread->next == NULL);
assert(thread->prev == NULL);
thread->ref = NULL;

thread->type = THREAD_UNUSED;
thread->hist->total_active--;
thread_list_add(&m->unuse, thread);
memset(thread, 0, sizeof(struct thread));
thread->type = THREAD_UNUSED;

if (m->unuse.count < THREAD_UNUSED_DEPTH)
thread_list_add(&m->unuse, thread);
else
XFREE(MTYPE_THREAD, thread);
}

/* Free all unused thread. */
@@ -1175,17 +1181,19 @@ void thread_cancel_event(struct thread_master *master, void *arg)
*/
void thread_cancel(struct thread *thread)
{
assert(thread->master->owner == pthread_self());
struct thread_master *master = thread->master;

pthread_mutex_lock(&thread->master->mtx);
assert(master->owner == pthread_self());

pthread_mutex_lock(&master->mtx);
{
struct cancel_req *cr =
XCALLOC(MTYPE_TMP, sizeof(struct cancel_req));
cr->thread = thread;
listnode_add(thread->master->cancel_req, cr);
do_thread_cancel(thread->master);
listnode_add(master->cancel_req, cr);
do_thread_cancel(master);
}
pthread_mutex_unlock(&thread->master->mtx);
pthread_mutex_unlock(&master->mtx);
}

/**

0 comments on commit 6ed04aa

Please sign in to comment.
You can’t perform that action at this time.