| 1 | /* |
| 2 | * Copyright (C) 2012 Simon Marchi <simon.marchi@polymtl.ca> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #define _LGPL_SOURCE |
| 9 | #include <stddef.h> |
| 10 | #include <unistd.h> |
| 11 | #include <stdbool.h> |
| 12 | #include <sys/resource.h> |
| 13 | #include <pthread.h> |
| 14 | |
| 15 | #include "defaults.h" |
| 16 | #include "macros.h" |
| 17 | #include "align.h" |
| 18 | #include "error.h" |
| 19 | |
| 20 | static int pthread_attr_init_done; |
| 21 | static pthread_attr_t tattr; |
| 22 | |
| 23 | LTTNG_HIDDEN |
| 24 | size_t default_get_channel_subbuf_size(void) |
| 25 | { |
| 26 | return max(_DEFAULT_CHANNEL_SUBBUF_SIZE, PAGE_SIZE); |
| 27 | } |
| 28 | |
| 29 | LTTNG_HIDDEN |
| 30 | size_t default_get_metadata_subbuf_size(void) |
| 31 | { |
| 32 | return max(DEFAULT_METADATA_SUBBUF_SIZE, PAGE_SIZE); |
| 33 | } |
| 34 | |
| 35 | LTTNG_HIDDEN |
| 36 | size_t default_get_kernel_channel_subbuf_size(void) |
| 37 | { |
| 38 | return max(DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE, PAGE_SIZE); |
| 39 | } |
| 40 | |
| 41 | LTTNG_HIDDEN |
| 42 | size_t default_get_ust_pid_channel_subbuf_size(void) |
| 43 | { |
| 44 | return max(DEFAULT_UST_PID_CHANNEL_SUBBUF_SIZE, PAGE_SIZE); |
| 45 | } |
| 46 | |
| 47 | LTTNG_HIDDEN |
| 48 | size_t default_get_ust_uid_channel_subbuf_size(void) |
| 49 | { |
| 50 | return max(DEFAULT_UST_UID_CHANNEL_SUBBUF_SIZE, PAGE_SIZE); |
| 51 | } |
| 52 | |
| 53 | LTTNG_HIDDEN |
| 54 | pthread_attr_t *default_pthread_attr(void) |
| 55 | { |
| 56 | if (pthread_attr_init_done) { |
| 57 | return &tattr; |
| 58 | } |
| 59 | |
| 60 | WARN("Uninitialized pthread attributes, using libc defaults."); |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | static void __attribute__((constructor)) init_default_pthread_attr(void) |
| 65 | { |
| 66 | int ret; |
| 67 | struct rlimit rlim; |
| 68 | size_t pthread_ss, system_ss, selected_ss; |
| 69 | |
| 70 | ret = pthread_attr_init(&tattr); |
| 71 | if (ret) { |
| 72 | errno = ret; |
| 73 | PERROR("pthread_attr_init"); |
| 74 | goto error; |
| 75 | } |
| 76 | |
| 77 | /* Get system stack size limits. */ |
| 78 | ret = getrlimit(RLIMIT_STACK, &rlim); |
| 79 | if (ret < 0) { |
| 80 | PERROR("getrlimit"); |
| 81 | goto error_destroy; |
| 82 | } |
| 83 | DBG("Stack size limits: soft %lld, hard %lld bytes", |
| 84 | (long long) rlim.rlim_cur, |
| 85 | (long long) rlim.rlim_max); |
| 86 | |
| 87 | /* |
| 88 | * getrlimit() may return a stack size of "-1", meaning "unlimited". |
| 89 | * In this case, we impose a known-good default minimum value which will |
| 90 | * override the libc's default stack size if it is smaller. |
| 91 | */ |
| 92 | system_ss = rlim.rlim_cur != -1 ? rlim.rlim_cur : |
| 93 | DEFAULT_LTTNG_THREAD_STACK_SIZE; |
| 94 | |
| 95 | /* Get pthread default thread stack size. */ |
| 96 | ret = pthread_attr_getstacksize(&tattr, &pthread_ss); |
| 97 | if (ret < 0) { |
| 98 | PERROR("pthread_attr_getstacksize"); |
| 99 | goto error_destroy; |
| 100 | } |
| 101 | DBG("Default pthread stack size is %zu bytes", pthread_ss); |
| 102 | |
| 103 | selected_ss = max_t(size_t, pthread_ss, system_ss); |
| 104 | if (selected_ss < DEFAULT_LTTNG_THREAD_STACK_SIZE) { |
| 105 | DBG("Default stack size is too small, setting it to %zu bytes", |
| 106 | (size_t) DEFAULT_LTTNG_THREAD_STACK_SIZE); |
| 107 | selected_ss = DEFAULT_LTTNG_THREAD_STACK_SIZE; |
| 108 | } |
| 109 | |
| 110 | if (rlim.rlim_max > 0 && selected_ss > rlim.rlim_max) { |
| 111 | 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", |
| 112 | (size_t) rlim.rlim_max, (size_t) DEFAULT_LTTNG_THREAD_STACK_SIZE); |
| 113 | selected_ss = (size_t) rlim.rlim_max; |
| 114 | } |
| 115 | |
| 116 | ret = pthread_attr_setstacksize(&tattr, selected_ss); |
| 117 | if (ret < 0) { |
| 118 | PERROR("pthread_attr_setstacksize"); |
| 119 | goto error_destroy; |
| 120 | } |
| 121 | pthread_attr_init_done = 1; |
| 122 | error: |
| 123 | return; |
| 124 | error_destroy: |
| 125 | ret = pthread_attr_destroy(&tattr); |
| 126 | if (ret) { |
| 127 | errno = ret; |
| 128 | PERROR("pthread_attr_destroy"); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | static void __attribute__((destructor)) fini_default_pthread_attr(void) |
| 133 | { |
| 134 | int ret; |
| 135 | |
| 136 | if (!pthread_attr_init_done) { |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | ret = pthread_attr_destroy(&tattr); |
| 141 | if (ret) { |
| 142 | errno = ret; |
| 143 | PERROR("pthread_attr_destroy"); |
| 144 | } |
| 145 | } |