Cleanup: always use sysconf to get the page size
[lttng-tools.git] / src / common / defaults.c
... / ...
CommitLineData
1/*
2 * Copyright (C) 2012 Simon Marchi <simon.marchi@polymtl.ca>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8#define _LGPL_SOURCE
9#include <stddef.h>
10#include <unistd.h>
11#include <stdbool.h>
12#include <sys/resource.h>
13#include <pthread.h>
14
15#include "defaults.h"
16#include "macros.h"
17#include "error.h"
18
19static int pthread_attr_init_done;
20static pthread_attr_t tattr;
21
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
37size_t default_get_channel_subbuf_size(void)
38{
39 return max(_DEFAULT_CHANNEL_SUBBUF_SIZE, get_page_size());
40}
41
42size_t default_get_metadata_subbuf_size(void)
43{
44 return max(DEFAULT_METADATA_SUBBUF_SIZE, get_page_size());
45}
46
47size_t default_get_kernel_channel_subbuf_size(void)
48{
49 return max(DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE, get_page_size());
50}
51
52size_t default_get_ust_pid_channel_subbuf_size(void)
53{
54 return max(DEFAULT_UST_PID_CHANNEL_SUBBUF_SIZE, get_page_size());
55}
56
57size_t default_get_ust_uid_channel_subbuf_size(void)
58{
59 return max(DEFAULT_UST_UID_CHANNEL_SUBBUF_SIZE, get_page_size());
60}
61
62pthread_attr_t *default_pthread_attr(void)
63{
64 if (pthread_attr_init_done) {
65 return &tattr;
66 }
67
68 WARN("Uninitialized pthread attributes, using libc defaults.");
69 return NULL;
70}
71
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;
83 }
84
85 /* Get system stack size limits. */
86 ret = getrlimit(RLIMIT_STACK, &rlim);
87 if (ret < 0) {
88 PERROR("getrlimit");
89 goto error_destroy;
90 }
91 DBG("Stack size limits: soft %lld, hard %lld bytes",
92 (long long) rlim.rlim_cur,
93 (long long) rlim.rlim_max);
94
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
103 /* Get pthread default thread stack size. */
104 ret = pthread_attr_getstacksize(&tattr, &pthread_ss);
105 if (ret < 0) {
106 PERROR("pthread_attr_getstacksize");
107 goto error_destroy;
108 }
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;
116 }
117
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;
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;
130error:
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 }
153}
This page took 0.02234 seconds and 4 git commands to generate.