X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Fdefaults.c;h=ee48c436879a7d356f6e649b5cd3b89a419533bc;hp=d74a42b0aa786cc95ba55f3e219db249bd004444;hb=cb8d0d245b5739e7493dcf27314f6e42615f14b1;hpb=0a9c649428b2ea8d4e743aebc12585eeb58fcf64 diff --git a/src/common/defaults.c b/src/common/defaults.c index d74a42b0a..ee48c4368 100644 --- a/src/common/defaults.c +++ b/src/common/defaults.c @@ -1,52 +1,145 @@ /* - * Copyright (C) 2012 - Simon Marchi + * Copyright (C) 2012 Simon Marchi * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License, version 2 only, as - * published by the Free Software Foundation. + * SPDX-License-Identifier: GPL-2.0-only * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along with - * this program; if not, write to the Free Software Foundation, Inc., 51 - * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#define _LGPL_SOURCE #include #include +#include +#include +#include #include "defaults.h" #include "macros.h" +#include "align.h" +#include "error.h" + +static int pthread_attr_init_done; +static pthread_attr_t tattr; + +LTTNG_HIDDEN +size_t default_get_channel_subbuf_size(void) +{ + return max(_DEFAULT_CHANNEL_SUBBUF_SIZE, PAGE_SIZE); +} + +LTTNG_HIDDEN +size_t default_get_metadata_subbuf_size(void) +{ + return max(DEFAULT_METADATA_SUBBUF_SIZE, PAGE_SIZE); +} + +LTTNG_HIDDEN +size_t default_get_kernel_channel_subbuf_size(void) +{ + return max(DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE, PAGE_SIZE); +} -size_t default_channel_subbuf_size; -size_t default_metadata_subbuf_size; -size_t default_kernel_channel_subbuf_size; -size_t default_ust_pid_channel_subbuf_size; -size_t default_ust_uid_channel_subbuf_size; +LTTNG_HIDDEN +size_t default_get_ust_pid_channel_subbuf_size(void) +{ + return max(DEFAULT_UST_PID_CHANNEL_SUBBUF_SIZE, PAGE_SIZE); +} + +LTTNG_HIDDEN +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) +{ + if (pthread_attr_init_done) { + return &tattr; + } + + WARN("Uninitialized pthread attributes, using libc defaults."); + return NULL; +} -static void __attribute__((constructor)) init_defaults(void) +static void __attribute__((constructor)) init_default_pthread_attr(void) { + int ret; + struct rlimit rlim; + size_t pthread_ss, system_ss, selected_ss; + + ret = pthread_attr_init(&tattr); + if (ret) { + errno = ret; + PERROR("pthread_attr_init"); + goto error; + } + + /* Get system stack size limits. */ + ret = getrlimit(RLIMIT_STACK, &rlim); + if (ret < 0) { + PERROR("getrlimit"); + goto error_destroy; + } + DBG("Stack size limits: soft %lld, hard %lld bytes", + (long long) rlim.rlim_cur, + (long long) rlim.rlim_max); + /* - * The libringbuffer won't accept subbuf sizes smaller than the page size. - * If the default subbuf size is smaller, replace it by the page size. + * getrlimit() may return a stack size of "-1", meaning "unlimited". + * In this case, we impose a known-good default minimum value which will + * override the libc's default stack size if it is smaller. */ - long page_size = sysconf(_SC_PAGESIZE); - - if (page_size < 0) { - page_size = 0; - } - - default_channel_subbuf_size = - max(_DEFAULT_CHANNEL_SUBBUF_SIZE, page_size); - default_metadata_subbuf_size = - max(DEFAULT_METADATA_SUBBUF_SIZE, page_size); - default_kernel_channel_subbuf_size = - max(DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE, page_size); - default_ust_pid_channel_subbuf_size = - max(DEFAULT_UST_PID_CHANNEL_SUBBUF_SIZE, page_size); - default_ust_uid_channel_subbuf_size = - max(DEFAULT_UST_UID_CHANNEL_SUBBUF_SIZE, page_size); + system_ss = rlim.rlim_cur != -1 ? rlim.rlim_cur : + DEFAULT_LTTNG_THREAD_STACK_SIZE; + + /* Get pthread default thread stack size. */ + ret = pthread_attr_getstacksize(&tattr, &pthread_ss); + if (ret < 0) { + PERROR("pthread_attr_getstacksize"); + goto error_destroy; + } + DBG("Default pthread stack size is %zu bytes", pthread_ss); + + selected_ss = max_t(size_t, pthread_ss, system_ss); + if (selected_ss < DEFAULT_LTTNG_THREAD_STACK_SIZE) { + DBG("Default stack size is too small, setting it to %zu bytes", + (size_t) DEFAULT_LTTNG_THREAD_STACK_SIZE); + selected_ss = DEFAULT_LTTNG_THREAD_STACK_SIZE; + } + + if (rlim.rlim_max > 0 && selected_ss > rlim.rlim_max) { + WARN("Your system's stack size restrictions (%zu bytes) may be too low for the LTTng daemons to function properly, please set the stack size limit to at least %zu bytes to ensure reliable operation", + (size_t) rlim.rlim_max, (size_t) DEFAULT_LTTNG_THREAD_STACK_SIZE); + selected_ss = (size_t) rlim.rlim_max; + } + + ret = pthread_attr_setstacksize(&tattr, selected_ss); + if (ret < 0) { + PERROR("pthread_attr_setstacksize"); + goto error_destroy; + } + pthread_attr_init_done = 1; +error: + return; +error_destroy: + ret = pthread_attr_destroy(&tattr); + if (ret) { + errno = ret; + PERROR("pthread_attr_destroy"); + } +} + +static void __attribute__((destructor)) fini_default_pthread_attr(void) +{ + int ret; + + if (!pthread_attr_init_done) { + return; + } + + ret = pthread_attr_destroy(&tattr); + if (ret) { + errno = ret; + PERROR("pthread_attr_destroy"); + } }