2 * Copyright (C) 2012 - Simon Marchi <simon.marchi@polymtl.ca>
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.
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
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.
22 #include <sys/resource.h>
30 static int pthread_attr_init_done
;
31 static pthread_attr_t tattr
;
34 size_t default_get_channel_subbuf_size(void)
36 return max(_DEFAULT_CHANNEL_SUBBUF_SIZE
, PAGE_SIZE
);
40 size_t default_get_metadata_subbuf_size(void)
42 return max(DEFAULT_METADATA_SUBBUF_SIZE
, PAGE_SIZE
);
46 size_t default_get_kernel_channel_subbuf_size(void)
48 return max(DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
, PAGE_SIZE
);
52 size_t default_get_ust_pid_channel_subbuf_size(void)
54 return max(DEFAULT_UST_PID_CHANNEL_SUBBUF_SIZE
, PAGE_SIZE
);
58 size_t default_get_ust_uid_channel_subbuf_size(void)
60 return max(DEFAULT_UST_UID_CHANNEL_SUBBUF_SIZE
, PAGE_SIZE
);
64 pthread_attr_t
*default_pthread_attr(void)
66 if (pthread_attr_init_done
) {
70 WARN("Uninitialized pthread attributes, using libc defaults.");
74 static void __attribute__((constructor
)) init_default_pthread_attr(void)
78 size_t pthread_ss
, system_ss
, selected_ss
;
80 ret
= pthread_attr_init(&tattr
);
83 PERROR("pthread_attr_init");
87 /* Get system stack size limits. */
88 ret
= getrlimit(RLIMIT_STACK
, &rlim
);
93 DBG("Stack size limits: soft %lld, hard %lld bytes",
94 (long long) rlim
.rlim_cur
,
95 (long long) rlim
.rlim_max
);
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.
102 system_ss
= rlim
.rlim_cur
!= -1 ? rlim
.rlim_cur
:
103 DEFAULT_LTTNG_THREAD_STACK_SIZE
;
105 /* Get pthread default thread stack size. */
106 ret
= pthread_attr_getstacksize(&tattr
, &pthread_ss
);
108 PERROR("pthread_attr_getstacksize");
111 DBG("Default pthread stack size is %zu bytes", pthread_ss
);
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
;
120 if (rlim
.rlim_max
> 0 && selected_ss
> rlim
.rlim_max
) {
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
;
126 ret
= pthread_attr_setstacksize(&tattr
, selected_ss
);
128 PERROR("pthread_attr_setstacksize");
131 pthread_attr_init_done
= 1;
135 ret
= pthread_attr_destroy(&tattr
);
138 PERROR("pthread_attr_destroy");
142 static void __attribute__((destructor
)) fini_default_pthread_attr(void)
146 if (!pthread_attr_init_done
) {
150 ret
= pthread_attr_destroy(&tattr
);
153 PERROR("pthread_attr_destroy");