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