Fix: Set thread stack size to ulimit soft value
[lttng-tools.git] / src / common / defaults.c
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
18 #define _LGPL_SOURCE
19 #include <stddef.h>
20 #include <unistd.h>
21 #include <stdbool.h>
22 #include <sys/resource.h>
23 #include <pthread.h>
24
25 #include "defaults.h"
26 #include "macros.h"
27 #include "align.h"
28 #include "error.h"
29
30 static bool pthread_attr_init_done;
31 static pthread_attr_t tattr;
32 static pthread_mutex_t tattr_lock = PTHREAD_MUTEX_INITIALIZER;
33
34 LTTNG_HIDDEN
35 size_t default_get_channel_subbuf_size(void)
36 {
37 return max(_DEFAULT_CHANNEL_SUBBUF_SIZE, PAGE_SIZE);
38 }
39
40 LTTNG_HIDDEN
41 size_t default_get_metadata_subbuf_size(void)
42 {
43 return max(DEFAULT_METADATA_SUBBUF_SIZE, PAGE_SIZE);
44 }
45
46 LTTNG_HIDDEN
47 size_t default_get_kernel_channel_subbuf_size(void)
48 {
49 return max(DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE, PAGE_SIZE);
50 }
51
52 LTTNG_HIDDEN
53 size_t default_get_ust_pid_channel_subbuf_size(void)
54 {
55 return max(DEFAULT_UST_PID_CHANNEL_SUBBUF_SIZE, PAGE_SIZE);
56 }
57
58 LTTNG_HIDDEN
59 size_t default_get_ust_uid_channel_subbuf_size(void)
60 {
61 return max(DEFAULT_UST_UID_CHANNEL_SUBBUF_SIZE, PAGE_SIZE);
62 }
63
64 LTTNG_HIDDEN
65 pthread_attr_t *default_pthread_attr(void)
66 {
67 int ret = 0;
68 size_t ptstacksize;
69 struct rlimit rlim;
70
71 pthread_mutex_lock(&tattr_lock);
72
73 /* Return cached value. */
74 if (pthread_attr_init_done) {
75 goto end;
76 }
77
78 /* Get system stack size limits. */
79 ret = getrlimit(RLIMIT_STACK, &rlim);
80 if (ret < 0) {
81 PERROR("getrlimit");
82 goto error;
83 }
84 DBG("Stack size limits: soft %lld, hard %lld bytes",
85 (long long) rlim.rlim_cur,
86 (long long) rlim.rlim_max);
87
88 /* Get pthread default thread stack size. */
89 ret = pthread_attr_getstacksize(&tattr, &ptstacksize);
90 if (ret < 0) {
91 PERROR("pthread_attr_getstacksize");
92 goto error;
93 }
94 DBG("Default pthread stack size is %zu bytes", ptstacksize);
95
96 /* Check if the default pthread stack size honors ulimits. */
97 if (ptstacksize < rlim.rlim_cur) {
98 DBG("Your libc doesn't honor stack size limits, setting thread stack size to soft limit (%lld bytes)",
99 (long long) rlim.rlim_cur);
100
101 /* Create pthread_attr_t struct with ulimit stack size. */
102 ret = pthread_attr_setstacksize(&tattr, rlim.rlim_cur);
103 if (ret < 0) {
104 PERROR("pthread_attr_setstacksize");
105 goto error;
106 }
107 }
108
109 /* Enable cached value. */
110 pthread_attr_init_done = true;
111 end:
112 pthread_mutex_unlock(&tattr_lock);
113 return &tattr;
114 error:
115 pthread_mutex_unlock(&tattr_lock);
116 WARN("Failed to initialize pthread attributes, using libc defaults.");
117 return NULL;
118 }
This page took 0.030635 seconds and 4 git commands to generate.