common: compile libconfig as C++
[lttng-tools.git] / src / common / defaults.c
CommitLineData
8b3bd7a3 1/*
ab5be9fa 2 * Copyright (C) 2012 Simon Marchi <simon.marchi@polymtl.ca>
8b3bd7a3 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
8b3bd7a3 5 *
8b3bd7a3
SM
6 */
7
6c1c0768 8#define _LGPL_SOURCE
8b3bd7a3
SM
9#include <stddef.h>
10#include <unistd.h>
1a1a34b4
MJ
11#include <stdbool.h>
12#include <sys/resource.h>
13#include <pthread.h>
8b3bd7a3
SM
14
15#include "defaults.h"
16#include "macros.h"
1a1a34b4
MJ
17#include "error.h"
18
4d5fb75f 19static int pthread_attr_init_done;
1a1a34b4 20static pthread_attr_t tattr;
8b3bd7a3 21
81663f07
MJ
22static size_t get_page_size(void)
23{
24 const long ret = sysconf(_SC_PAGE_SIZE);
25
26 if (ret < 0) {
27 /*
28 * Fatal error since there is no safe way to recover from this.
29 */
30 PERROR("Failed to get system page size using sysconf(_SC_PAGE_SIZE)");
31 abort();
32 }
33
34 return (size_t) ret;
35}
36
78d8fb4f 37size_t default_get_channel_subbuf_size(void)
8b3bd7a3 38{
81663f07 39 return max(_DEFAULT_CHANNEL_SUBBUF_SIZE, get_page_size());
78d8fb4f
JG
40}
41
78d8fb4f
JG
42size_t default_get_metadata_subbuf_size(void)
43{
81663f07 44 return max(DEFAULT_METADATA_SUBBUF_SIZE, get_page_size());
78d8fb4f
JG
45}
46
78d8fb4f
JG
47size_t default_get_kernel_channel_subbuf_size(void)
48{
81663f07 49 return max(DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE, get_page_size());
78d8fb4f
JG
50}
51
78d8fb4f
JG
52size_t default_get_ust_pid_channel_subbuf_size(void)
53{
81663f07 54 return max(DEFAULT_UST_PID_CHANNEL_SUBBUF_SIZE, get_page_size());
78d8fb4f
JG
55}
56
78d8fb4f
JG
57size_t default_get_ust_uid_channel_subbuf_size(void)
58{
81663f07 59 return max(DEFAULT_UST_UID_CHANNEL_SUBBUF_SIZE, get_page_size());
8b3bd7a3 60}
1a1a34b4 61
1a1a34b4
MJ
62pthread_attr_t *default_pthread_attr(void)
63{
4d5fb75f
JG
64 if (pthread_attr_init_done) {
65 return &tattr;
66 }
1a1a34b4 67
b38afa27 68 WARN("Uninitialized pthread attributes, using libc defaults.");
4d5fb75f
JG
69 return NULL;
70}
1a1a34b4 71
4d5fb75f
JG
72static void __attribute__((constructor)) init_default_pthread_attr(void)
73{
74 int ret;
75 struct rlimit rlim;
76 size_t pthread_ss, system_ss, selected_ss;
77
78 ret = pthread_attr_init(&tattr);
79 if (ret) {
80 errno = ret;
81 PERROR("pthread_attr_init");
82 goto error;
1a1a34b4
MJ
83 }
84
85 /* Get system stack size limits. */
86 ret = getrlimit(RLIMIT_STACK, &rlim);
87 if (ret < 0) {
88 PERROR("getrlimit");
4d5fb75f 89 goto error_destroy;
1a1a34b4
MJ
90 }
91 DBG("Stack size limits: soft %lld, hard %lld bytes",
92 (long long) rlim.rlim_cur,
93 (long long) rlim.rlim_max);
94
4d5fb75f
JG
95 /*
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.
99 */
100 system_ss = rlim.rlim_cur != -1 ? rlim.rlim_cur :
101 DEFAULT_LTTNG_THREAD_STACK_SIZE;
102
1a1a34b4 103 /* Get pthread default thread stack size. */
4d5fb75f 104 ret = pthread_attr_getstacksize(&tattr, &pthread_ss);
1a1a34b4
MJ
105 if (ret < 0) {
106 PERROR("pthread_attr_getstacksize");
4d5fb75f 107 goto error_destroy;
1a1a34b4 108 }
4d5fb75f
JG
109 DBG("Default pthread stack size is %zu bytes", pthread_ss);
110
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;
1a1a34b4
MJ
116 }
117
89e06fb5 118 if (rlim.rlim_max > 0 && selected_ss > rlim.rlim_max) {
0d43a013 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",
4d5fb75f
JG
120 (size_t) rlim.rlim_max, (size_t) DEFAULT_LTTNG_THREAD_STACK_SIZE);
121 selected_ss = (size_t) rlim.rlim_max;
122 }
123
124 ret = pthread_attr_setstacksize(&tattr, selected_ss);
125 if (ret < 0) {
126 PERROR("pthread_attr_setstacksize");
127 goto error_destroy;
128 }
129 pthread_attr_init_done = 1;
1a1a34b4 130error:
4d5fb75f
JG
131 return;
132error_destroy:
133 ret = pthread_attr_destroy(&tattr);
134 if (ret) {
135 errno = ret;
136 PERROR("pthread_attr_destroy");
137 }
138}
139
140static void __attribute__((destructor)) fini_default_pthread_attr(void)
141{
142 int ret;
143
144 if (!pthread_attr_init_done) {
145 return;
146 }
147
148 ret = pthread_attr_destroy(&tattr);
149 if (ret) {
150 errno = ret;
151 PERROR("pthread_attr_destroy");
152 }
1a1a34b4 153}
This page took 0.057015 seconds and 4 git commands to generate.