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