2 * Copyright (C) 2012 Simon Marchi <simon.marchi@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
12 #include <sys/resource.h>
19 static int pthread_attr_init_done
;
20 static pthread_attr_t tattr
;
22 static size_t get_page_size(void)
24 const long ret
= sysconf(_SC_PAGE_SIZE
);
28 * Fatal error since there is no safe way to recover from this.
30 PERROR("Failed to get system page size using sysconf(_SC_PAGE_SIZE)");
37 size_t default_get_channel_subbuf_size(void)
39 return max(_DEFAULT_CHANNEL_SUBBUF_SIZE
, get_page_size());
42 size_t default_get_metadata_subbuf_size(void)
44 return max(DEFAULT_METADATA_SUBBUF_SIZE
, get_page_size());
47 size_t default_get_kernel_channel_subbuf_size(void)
49 return max(DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
, get_page_size());
52 size_t default_get_ust_pid_channel_subbuf_size(void)
54 return max(DEFAULT_UST_PID_CHANNEL_SUBBUF_SIZE
, get_page_size());
57 size_t default_get_ust_uid_channel_subbuf_size(void)
59 return max(DEFAULT_UST_UID_CHANNEL_SUBBUF_SIZE
, get_page_size());
62 pthread_attr_t
*default_pthread_attr(void)
64 if (pthread_attr_init_done
) {
68 WARN("Uninitialized pthread attributes, using libc defaults.");
72 static void __attribute__((constructor
)) init_default_pthread_attr(void)
76 size_t pthread_ss
, system_ss
, selected_ss
;
78 ret
= pthread_attr_init(&tattr
);
81 PERROR("pthread_attr_init");
85 /* Get system stack size limits. */
86 ret
= getrlimit(RLIMIT_STACK
, &rlim
);
91 DBG("Stack size limits: soft %lld, hard %lld bytes",
92 (long long) rlim
.rlim_cur
,
93 (long long) rlim
.rlim_max
);
96 * getrlimit() may return a stack size of "-1", meaning "unlimited".
97 * In this case, we impose a known-good default minimum value which will
98 * override the libc's default stack size if it is smaller.
100 system_ss
= rlim
.rlim_cur
!= -1 ? rlim
.rlim_cur
:
101 DEFAULT_LTTNG_THREAD_STACK_SIZE
;
103 /* Get pthread default thread stack size. */
104 ret
= pthread_attr_getstacksize(&tattr
, &pthread_ss
);
106 PERROR("pthread_attr_getstacksize");
109 DBG("Default pthread stack size is %zu bytes", pthread_ss
);
111 selected_ss
= max_t(size_t, 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
;
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
, (size_t) DEFAULT_LTTNG_THREAD_STACK_SIZE
);
121 selected_ss
= (size_t) rlim
.rlim_max
;
124 ret
= pthread_attr_setstacksize(&tattr
, selected_ss
);
126 PERROR("pthread_attr_setstacksize");
129 pthread_attr_init_done
= 1;
133 ret
= pthread_attr_destroy(&tattr
);
136 PERROR("pthread_attr_destroy");
140 static void __attribute__((destructor
)) fini_default_pthread_attr(void)
144 if (!pthread_attr_init_done
) {
148 ret
= pthread_attr_destroy(&tattr
);
151 PERROR("pthread_attr_destroy");