2 * Copyright (C) 2012 EfficiOS Inc.
4 * SPDX-License-Identifier: LGPL-2.1-only
12 #include <sys/resource.h>
16 #include "defaults.hpp"
20 static int pthread_attr_init_done
;
21 static pthread_attr_t tattr
;
23 static size_t get_page_size(void)
25 const long ret
= sysconf(_SC_PAGE_SIZE
);
29 * Fatal error since there is no safe way to recover from this.
31 PERROR("Failed to get system page size using sysconf(_SC_PAGE_SIZE)");
38 size_t default_get_channel_subbuf_size(void)
40 return std::max
<size_t>(_DEFAULT_CHANNEL_SUBBUF_SIZE
, get_page_size());
43 size_t default_get_metadata_subbuf_size(void)
45 return std::max
<size_t>(DEFAULT_METADATA_SUBBUF_SIZE
, get_page_size());
48 size_t default_get_kernel_channel_subbuf_size(void)
50 return std::max
<size_t>(DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
, get_page_size());
53 size_t default_get_ust_pid_channel_subbuf_size(void)
55 return std::max
<size_t>(DEFAULT_UST_PID_CHANNEL_SUBBUF_SIZE
, get_page_size());
58 size_t default_get_ust_uid_channel_subbuf_size(void)
60 return std::max
<size_t>(DEFAULT_UST_UID_CHANNEL_SUBBUF_SIZE
, get_page_size());
63 pthread_attr_t
*default_pthread_attr(void)
65 if (pthread_attr_init_done
) {
69 WARN("Uninitialized pthread attributes, using libc defaults.");
73 static void __attribute__((constructor
)) init_default_pthread_attr(void)
77 size_t pthread_ss
, system_ss
, selected_ss
;
79 ret
= pthread_attr_init(&tattr
);
82 PERROR("pthread_attr_init");
86 /* Get system stack size limits. */
87 ret
= getrlimit(RLIMIT_STACK
, &rlim
);
92 DBG("Stack size limits: soft %lld, hard %lld bytes",
93 (long long) rlim
.rlim_cur
,
94 (long long) rlim
.rlim_max
);
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.
101 system_ss
= rlim
.rlim_cur
!= -1 ? rlim
.rlim_cur
:
102 DEFAULT_LTTNG_THREAD_STACK_SIZE
;
104 /* Get pthread default thread stack size. */
105 ret
= pthread_attr_getstacksize(&tattr
, &pthread_ss
);
107 PERROR("pthread_attr_getstacksize");
110 DBG("Default pthread stack size is %zu bytes", pthread_ss
);
112 selected_ss
= std::max(pthread_ss
, system_ss
);
113 if (selected_ss
< DEFAULT_LTTNG_THREAD_STACK_SIZE
) {
114 DBG("Default stack size is too small, setting it to %zu bytes",
115 (size_t) DEFAULT_LTTNG_THREAD_STACK_SIZE
);
116 selected_ss
= DEFAULT_LTTNG_THREAD_STACK_SIZE
;
119 if (rlim
.rlim_max
> 0 && selected_ss
> rlim
.rlim_max
) {
120 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",
121 (size_t) rlim
.rlim_max
, (size_t) DEFAULT_LTTNG_THREAD_STACK_SIZE
);
122 selected_ss
= (size_t) rlim
.rlim_max
;
125 ret
= pthread_attr_setstacksize(&tattr
, selected_ss
);
127 PERROR("pthread_attr_setstacksize");
130 pthread_attr_init_done
= 1;
134 ret
= pthread_attr_destroy(&tattr
);
137 PERROR("pthread_attr_destroy");
141 static void __attribute__((destructor
)) fini_default_pthread_attr(void)
145 if (!pthread_attr_init_done
) {
149 ret
= pthread_attr_destroy(&tattr
);
152 PERROR("pthread_attr_destroy");