From: Jeff Lucovsky Date: Fri, 11 Feb 2022 14:02:39 +0000 (-0500) Subject: threads: Honor per-thread stack size setting X-Git-Tag: suricata-5.0.9~61 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=83d1852d4e87c216f6421d66a3c3e801e3c3125f;p=thirdparty%2Fsuricata.git threads: Honor per-thread stack size setting Issue: 4550 This commit adjusts the per-thread stack size if a size has been configured. If the setting has not been configured, the default per-thread stack size provided by the runtime mechanisms are used. (cherry picked from commit 6232c94235a60f2f89d444dfd5a9e20efc0a5a60) --- diff --git a/src/tm-threads.c b/src/tm-threads.c index 27186287ea..c954c7e437 100644 --- a/src/tm-threads.c +++ b/src/tm-threads.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2017 Open Information Security Foundation +/* Copyright (C) 2007-2022 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -1881,8 +1881,7 @@ TmEcode TmThreadSpawn(ThreadVars *tv) { pthread_attr_t attr; if (tv->tm_func == NULL) { - printf("ERROR: no thread function set\n"); - return TM_ECODE_FAILED; + FatalError(SC_ERR_TM_THREADS_ERROR, "No thread function set"); } /* Initialize and set thread detached attribute */ @@ -1890,12 +1889,37 @@ TmEcode TmThreadSpawn(ThreadVars *tv) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); + /* Adjust thread stack size if configured */ + if (threading_set_stack_size) { + SCLogDebug("Setting per-thread stack size to %" PRIu64, threading_set_stack_size); + if (pthread_attr_setstacksize(&attr, (size_t)threading_set_stack_size)) { + FatalError(SC_ERR_TM_THREADS_ERROR, + "Unable to increase stack size to %" PRIu64 " in thread attributes", + threading_set_stack_size); + } + } + int rc = pthread_create(&tv->t, &attr, tv->tm_func, (void *)tv); if (rc) { - printf("ERROR; return code from pthread_create() is %" PRId32 "\n", rc); - return TM_ECODE_FAILED; + FatalError(SC_ERR_THREAD_CREATE, + "Unable to create thread with pthread_create() is %" PRId32, rc); } +#if DEBUG && HAVE_PTHREAD_GETATTR_NP + if (threading_set_stack_size) { + if (pthread_getattr_np(tv->t, &attr) == 0) { + size_t stack_size; + void *stack_addr; + pthread_attr_getstack(&attr, &stack_addr, &stack_size); + SCLogDebug("stack: %p; size %" PRIu64, stack_addr, (uintmax_t)stack_size); + } else { + SCLogDebug("Unable to retrieve current stack-size for display; return code from " + "pthread_getattr_np() is %" PRId32, + rc); + } + } +#endif + TmThreadWaitForFlag(tv, THV_INIT_DONE | THV_RUNNING_DONE); TmThreadAppend(tv, tv->type);