common: cleanup error message mentioning mkdir
[lttng-tools.git] / src / common / defaults.c
CommitLineData
8b3bd7a3
SM
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
6c1c0768 18#define _LGPL_SOURCE
8b3bd7a3
SM
19#include <stddef.h>
20#include <unistd.h>
1a1a34b4
MJ
21#include <stdbool.h>
22#include <sys/resource.h>
23#include <pthread.h>
8b3bd7a3
SM
24
25#include "defaults.h"
26#include "macros.h"
78d8fb4f 27#include "align.h"
1a1a34b4
MJ
28#include "error.h"
29
4d5fb75f 30static int pthread_attr_init_done;
1a1a34b4 31static pthread_attr_t tattr;
8b3bd7a3 32
78d8fb4f
JG
33LTTNG_HIDDEN
34size_t default_get_channel_subbuf_size(void)
8b3bd7a3 35{
78d8fb4f
JG
36 return max(_DEFAULT_CHANNEL_SUBBUF_SIZE, PAGE_SIZE);
37}
38
39LTTNG_HIDDEN
40size_t default_get_metadata_subbuf_size(void)
41{
42 return max(DEFAULT_METADATA_SUBBUF_SIZE, PAGE_SIZE);
43}
44
45LTTNG_HIDDEN
46size_t default_get_kernel_channel_subbuf_size(void)
47{
48 return max(DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE, PAGE_SIZE);
49}
50
51LTTNG_HIDDEN
52size_t default_get_ust_pid_channel_subbuf_size(void)
53{
54 return max(DEFAULT_UST_PID_CHANNEL_SUBBUF_SIZE, PAGE_SIZE);
55}
56
57LTTNG_HIDDEN
58size_t default_get_ust_uid_channel_subbuf_size(void)
59{
60 return max(DEFAULT_UST_UID_CHANNEL_SUBBUF_SIZE, PAGE_SIZE);
8b3bd7a3 61}
1a1a34b4
MJ
62
63LTTNG_HIDDEN
64pthread_attr_t *default_pthread_attr(void)
65{
4d5fb75f
JG
66 if (pthread_attr_init_done) {
67 return &tattr;
68 }
1a1a34b4 69
b38afa27 70 WARN("Uninitialized pthread attributes, using libc defaults.");
4d5fb75f
JG
71 return NULL;
72}
1a1a34b4 73
4d5fb75f
JG
74static void __attribute__((constructor)) init_default_pthread_attr(void)
75{
76 int ret;
77 struct rlimit rlim;
78 size_t pthread_ss, system_ss, selected_ss;
79
80 ret = pthread_attr_init(&tattr);
81 if (ret) {
82 errno = ret;
83 PERROR("pthread_attr_init");
84 goto error;
1a1a34b4
MJ
85 }
86
87 /* Get system stack size limits. */
88 ret = getrlimit(RLIMIT_STACK, &rlim);
89 if (ret < 0) {
90 PERROR("getrlimit");
4d5fb75f 91 goto error_destroy;
1a1a34b4
MJ
92 }
93 DBG("Stack size limits: soft %lld, hard %lld bytes",
94 (long long) rlim.rlim_cur,
95 (long long) rlim.rlim_max);
96
4d5fb75f
JG
97 /*
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.
101 */
102 system_ss = rlim.rlim_cur != -1 ? rlim.rlim_cur :
103 DEFAULT_LTTNG_THREAD_STACK_SIZE;
104
1a1a34b4 105 /* Get pthread default thread stack size. */
4d5fb75f 106 ret = pthread_attr_getstacksize(&tattr, &pthread_ss);
1a1a34b4
MJ
107 if (ret < 0) {
108 PERROR("pthread_attr_getstacksize");
4d5fb75f 109 goto error_destroy;
1a1a34b4 110 }
4d5fb75f
JG
111 DBG("Default pthread stack size is %zu bytes", pthread_ss);
112
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;
1a1a34b4
MJ
118 }
119
89e06fb5 120 if (rlim.rlim_max > 0 && selected_ss > rlim.rlim_max) {
0d43a013 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 least %zu bytes to ensure reliable operation",
4d5fb75f
JG
122 (size_t) rlim.rlim_max, (size_t) DEFAULT_LTTNG_THREAD_STACK_SIZE);
123 selected_ss = (size_t) rlim.rlim_max;
124 }
125
126 ret = pthread_attr_setstacksize(&tattr, selected_ss);
127 if (ret < 0) {
128 PERROR("pthread_attr_setstacksize");
129 goto error_destroy;
130 }
131 pthread_attr_init_done = 1;
1a1a34b4 132error:
4d5fb75f
JG
133 return;
134error_destroy:
135 ret = pthread_attr_destroy(&tattr);
136 if (ret) {
137 errno = ret;
138 PERROR("pthread_attr_destroy");
139 }
140}
141
142static void __attribute__((destructor)) fini_default_pthread_attr(void)
143{
144 int ret;
145
146 if (!pthread_attr_init_done) {
147 return;
148 }
149
150 ret = pthread_attr_destroy(&tattr);
151 if (ret) {
152 errno = ret;
153 PERROR("pthread_attr_destroy");
154 }
1a1a34b4 155}
This page took 0.047695 seconds and 4 git commands to generate.