Commit | Line | Data |
---|---|---|
8b3bd7a3 SM |
1 | /* |
2 | * Copyright (C) 2012 - Simon Marchi <simon.marchi@polymtl.ca> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License, version 2 only, as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | */ | |
17 | ||
6c1c0768 | 18 | #define _LGPL_SOURCE |
8b3bd7a3 SM |
19 | #include <stddef.h> |
20 | #include <unistd.h> | |
1a1a34b4 MJ |
21 | #include <stdbool.h> |
22 | #include <sys/resource.h> | |
23 | #include <pthread.h> | |
8b3bd7a3 SM |
24 | |
25 | #include "defaults.h" | |
26 | #include "macros.h" | |
78d8fb4f | 27 | #include "align.h" |
1a1a34b4 MJ |
28 | #include "error.h" |
29 | ||
4d5fb75f | 30 | static int pthread_attr_init_done; |
1a1a34b4 | 31 | static pthread_attr_t tattr; |
8b3bd7a3 | 32 | |
78d8fb4f JG |
33 | LTTNG_HIDDEN |
34 | size_t default_get_channel_subbuf_size(void) | |
8b3bd7a3 | 35 | { |
78d8fb4f JG |
36 | return max(_DEFAULT_CHANNEL_SUBBUF_SIZE, PAGE_SIZE); |
37 | } | |
38 | ||
39 | LTTNG_HIDDEN | |
40 | size_t default_get_metadata_subbuf_size(void) | |
41 | { | |
42 | return max(DEFAULT_METADATA_SUBBUF_SIZE, PAGE_SIZE); | |
43 | } | |
44 | ||
45 | LTTNG_HIDDEN | |
46 | size_t default_get_kernel_channel_subbuf_size(void) | |
47 | { | |
48 | return max(DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE, PAGE_SIZE); | |
49 | } | |
50 | ||
51 | LTTNG_HIDDEN | |
52 | size_t default_get_ust_pid_channel_subbuf_size(void) | |
53 | { | |
54 | return max(DEFAULT_UST_PID_CHANNEL_SUBBUF_SIZE, PAGE_SIZE); | |
55 | } | |
56 | ||
57 | LTTNG_HIDDEN | |
58 | size_t default_get_ust_uid_channel_subbuf_size(void) | |
59 | { | |
60 | return max(DEFAULT_UST_UID_CHANNEL_SUBBUF_SIZE, PAGE_SIZE); | |
8b3bd7a3 | 61 | } |
1a1a34b4 MJ |
62 | |
63 | LTTNG_HIDDEN | |
64 | pthread_attr_t *default_pthread_attr(void) | |
65 | { | |
4d5fb75f JG |
66 | if (pthread_attr_init_done) { |
67 | return &tattr; | |
68 | } | |
1a1a34b4 | 69 | |
b38afa27 | 70 | WARN("Uninitialized pthread attributes, using libc defaults."); |
4d5fb75f JG |
71 | return NULL; |
72 | } | |
1a1a34b4 | 73 | |
4d5fb75f JG |
74 | static void __attribute__((constructor)) init_default_pthread_attr(void) |
75 | { | |
76 | int ret; | |
77 | struct rlimit rlim; | |
78 | size_t pthread_ss, system_ss, selected_ss; | |
79 | ||
80 | ret = pthread_attr_init(&tattr); | |
81 | if (ret) { | |
82 | errno = ret; | |
83 | PERROR("pthread_attr_init"); | |
84 | goto error; | |
1a1a34b4 MJ |
85 | } |
86 | ||
87 | /* Get system stack size limits. */ | |
88 | ret = getrlimit(RLIMIT_STACK, &rlim); | |
89 | if (ret < 0) { | |
90 | PERROR("getrlimit"); | |
4d5fb75f | 91 | goto error_destroy; |
1a1a34b4 MJ |
92 | } |
93 | DBG("Stack size limits: soft %lld, hard %lld bytes", | |
94 | (long long) rlim.rlim_cur, | |
95 | (long long) rlim.rlim_max); | |
96 | ||
4d5fb75f JG |
97 | /* |
98 | * getrlimit() may return a stack size of "-1", meaning "unlimited". | |
99 | * In this case, we impose a known-good default minimum value which will | |
100 | * override the libc's default stack size if it is smaller. | |
101 | */ | |
102 | system_ss = rlim.rlim_cur != -1 ? rlim.rlim_cur : | |
103 | DEFAULT_LTTNG_THREAD_STACK_SIZE; | |
104 | ||
1a1a34b4 | 105 | /* Get pthread default thread stack size. */ |
4d5fb75f | 106 | ret = pthread_attr_getstacksize(&tattr, &pthread_ss); |
1a1a34b4 MJ |
107 | if (ret < 0) { |
108 | PERROR("pthread_attr_getstacksize"); | |
4d5fb75f | 109 | goto error_destroy; |
1a1a34b4 | 110 | } |
4d5fb75f JG |
111 | DBG("Default pthread stack size is %zu bytes", pthread_ss); |
112 | ||
113 | selected_ss = max_t(size_t, pthread_ss, system_ss); | |
114 | if (selected_ss < DEFAULT_LTTNG_THREAD_STACK_SIZE) { | |
115 | DBG("Default stack size is too small, setting it to %zu bytes", | |
116 | (size_t) DEFAULT_LTTNG_THREAD_STACK_SIZE); | |
117 | selected_ss = DEFAULT_LTTNG_THREAD_STACK_SIZE; | |
1a1a34b4 MJ |
118 | } |
119 | ||
89e06fb5 | 120 | if (rlim.rlim_max > 0 && selected_ss > rlim.rlim_max) { |
4d5fb75f JG |
121 | 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 leat %zu bytes to ensure reliable operation", |
122 | (size_t) rlim.rlim_max, (size_t) DEFAULT_LTTNG_THREAD_STACK_SIZE); | |
123 | selected_ss = (size_t) rlim.rlim_max; | |
124 | } | |
125 | ||
126 | ret = pthread_attr_setstacksize(&tattr, selected_ss); | |
127 | if (ret < 0) { | |
128 | PERROR("pthread_attr_setstacksize"); | |
129 | goto error_destroy; | |
130 | } | |
131 | pthread_attr_init_done = 1; | |
1a1a34b4 | 132 | error: |
4d5fb75f JG |
133 | return; |
134 | error_destroy: | |
135 | ret = pthread_attr_destroy(&tattr); | |
136 | if (ret) { | |
137 | errno = ret; | |
138 | PERROR("pthread_attr_destroy"); | |
139 | } | |
140 | } | |
141 | ||
142 | static void __attribute__((destructor)) fini_default_pthread_attr(void) | |
143 | { | |
144 | int ret; | |
145 | ||
146 | if (!pthread_attr_init_done) { | |
147 | return; | |
148 | } | |
149 | ||
150 | ret = pthread_attr_destroy(&tattr); | |
151 | if (ret) { | |
152 | errno = ret; | |
153 | PERROR("pthread_attr_destroy"); | |
154 | } | |
1a1a34b4 | 155 | } |