X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Fdefaults.c;h=00a0265414baaf693a0eb687e8ce05b9a8f23907;hp=bd6e18351bc0e05ee0821e08139729cecac439f3;hb=1a1a34b40ab10a195633b1ed5e2e9b42fdae0a78;hpb=0452bf084c6d8416b88c2bb941e99eb4eec453b8 diff --git a/src/common/defaults.c b/src/common/defaults.c index bd6e18351..00a026541 100644 --- a/src/common/defaults.c +++ b/src/common/defaults.c @@ -18,10 +18,18 @@ #define _LGPL_SOURCE #include #include +#include +#include +#include #include "defaults.h" #include "macros.h" #include "align.h" +#include "error.h" + +static bool pthread_attr_init_done; +static pthread_attr_t tattr; +static pthread_mutex_t tattr_lock = PTHREAD_MUTEX_INITIALIZER; LTTNG_HIDDEN size_t default_get_channel_subbuf_size(void) @@ -52,3 +60,59 @@ size_t default_get_ust_uid_channel_subbuf_size(void) { return max(DEFAULT_UST_UID_CHANNEL_SUBBUF_SIZE, PAGE_SIZE); } + +LTTNG_HIDDEN +pthread_attr_t *default_pthread_attr(void) +{ + int ret = 0; + size_t ptstacksize; + struct rlimit rlim; + + pthread_mutex_lock(&tattr_lock); + + /* Return cached value. */ + if (pthread_attr_init_done) { + goto end; + } + + /* Get system stack size limits. */ + ret = getrlimit(RLIMIT_STACK, &rlim); + if (ret < 0) { + PERROR("getrlimit"); + goto error; + } + DBG("Stack size limits: soft %lld, hard %lld bytes", + (long long) rlim.rlim_cur, + (long long) rlim.rlim_max); + + /* Get pthread default thread stack size. */ + ret = pthread_attr_getstacksize(&tattr, &ptstacksize); + if (ret < 0) { + PERROR("pthread_attr_getstacksize"); + goto error; + } + DBG("Default pthread stack size is %zu bytes", ptstacksize); + + /* Check if the default pthread stack size honors ulimits. */ + if (ptstacksize < rlim.rlim_cur) { + DBG("Your libc doesn't honor stack size limits, setting thread stack size to soft limit (%lld bytes)", + (long long) rlim.rlim_cur); + + /* Create pthread_attr_t struct with ulimit stack size. */ + ret = pthread_attr_setstacksize(&tattr, rlim.rlim_cur); + if (ret < 0) { + PERROR("pthread_attr_setstacksize"); + goto error; + } + } + + /* Enable cached value. */ + pthread_attr_init_done = true; +end: + pthread_mutex_unlock(&tattr_lock); + return &tattr; +error: + pthread_mutex_unlock(&tattr_lock); + WARN("Failed to initialize pthread attributes, using libc defaults."); + return NULL; +}