Cleanup: namespace 'align' macros
[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"
78d8fb4f 17#include "align.h"
1a1a34b4
MJ
18#include "error.h"
19
4d5fb75f 20static int pthread_attr_init_done;
1a1a34b4 21static pthread_attr_t tattr;
8b3bd7a3 22
78d8fb4f 23size_t default_get_channel_subbuf_size(void)
8b3bd7a3 24{
78d8fb4f
JG
25 return max(_DEFAULT_CHANNEL_SUBBUF_SIZE, PAGE_SIZE);
26}
27
78d8fb4f
JG
28size_t default_get_metadata_subbuf_size(void)
29{
30 return max(DEFAULT_METADATA_SUBBUF_SIZE, PAGE_SIZE);
31}
32
78d8fb4f
JG
33size_t default_get_kernel_channel_subbuf_size(void)
34{
35 return max(DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE, PAGE_SIZE);
36}
37
78d8fb4f
JG
38size_t default_get_ust_pid_channel_subbuf_size(void)
39{
40 return max(DEFAULT_UST_PID_CHANNEL_SUBBUF_SIZE, PAGE_SIZE);
41}
42
78d8fb4f
JG
43size_t default_get_ust_uid_channel_subbuf_size(void)
44{
45 return max(DEFAULT_UST_UID_CHANNEL_SUBBUF_SIZE, PAGE_SIZE);
8b3bd7a3 46}
1a1a34b4 47
1a1a34b4
MJ
48pthread_attr_t *default_pthread_attr(void)
49{
4d5fb75f
JG
50 if (pthread_attr_init_done) {
51 return &tattr;
52 }
1a1a34b4 53
b38afa27 54 WARN("Uninitialized pthread attributes, using libc defaults.");
4d5fb75f
JG
55 return NULL;
56}
1a1a34b4 57
4d5fb75f
JG
58static void __attribute__((constructor)) init_default_pthread_attr(void)
59{
60 int ret;
61 struct rlimit rlim;
62 size_t pthread_ss, system_ss, selected_ss;
63
64 ret = pthread_attr_init(&tattr);
65 if (ret) {
66 errno = ret;
67 PERROR("pthread_attr_init");
68 goto error;
1a1a34b4
MJ
69 }
70
71 /* Get system stack size limits. */
72 ret = getrlimit(RLIMIT_STACK, &rlim);
73 if (ret < 0) {
74 PERROR("getrlimit");
4d5fb75f 75 goto error_destroy;
1a1a34b4
MJ
76 }
77 DBG("Stack size limits: soft %lld, hard %lld bytes",
78 (long long) rlim.rlim_cur,
79 (long long) rlim.rlim_max);
80
4d5fb75f
JG
81 /*
82 * getrlimit() may return a stack size of "-1", meaning "unlimited".
83 * In this case, we impose a known-good default minimum value which will
84 * override the libc's default stack size if it is smaller.
85 */
86 system_ss = rlim.rlim_cur != -1 ? rlim.rlim_cur :
87 DEFAULT_LTTNG_THREAD_STACK_SIZE;
88
1a1a34b4 89 /* Get pthread default thread stack size. */
4d5fb75f 90 ret = pthread_attr_getstacksize(&tattr, &pthread_ss);
1a1a34b4
MJ
91 if (ret < 0) {
92 PERROR("pthread_attr_getstacksize");
4d5fb75f 93 goto error_destroy;
1a1a34b4 94 }
4d5fb75f
JG
95 DBG("Default pthread stack size is %zu bytes", pthread_ss);
96
97 selected_ss = max_t(size_t, pthread_ss, system_ss);
98 if (selected_ss < DEFAULT_LTTNG_THREAD_STACK_SIZE) {
99 DBG("Default stack size is too small, setting it to %zu bytes",
100 (size_t) DEFAULT_LTTNG_THREAD_STACK_SIZE);
101 selected_ss = DEFAULT_LTTNG_THREAD_STACK_SIZE;
1a1a34b4
MJ
102 }
103
89e06fb5 104 if (rlim.rlim_max > 0 && selected_ss > rlim.rlim_max) {
0d43a013 105 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
106 (size_t) rlim.rlim_max, (size_t) DEFAULT_LTTNG_THREAD_STACK_SIZE);
107 selected_ss = (size_t) rlim.rlim_max;
108 }
109
110 ret = pthread_attr_setstacksize(&tattr, selected_ss);
111 if (ret < 0) {
112 PERROR("pthread_attr_setstacksize");
113 goto error_destroy;
114 }
115 pthread_attr_init_done = 1;
1a1a34b4 116error:
4d5fb75f
JG
117 return;
118error_destroy:
119 ret = pthread_attr_destroy(&tattr);
120 if (ret) {
121 errno = ret;
122 PERROR("pthread_attr_destroy");
123 }
124}
125
126static void __attribute__((destructor)) fini_default_pthread_attr(void)
127{
128 int ret;
129
130 if (!pthread_attr_init_done) {
131 return;
132 }
133
134 ret = pthread_attr_destroy(&tattr);
135 if (ret) {
136 errno = ret;
137 PERROR("pthread_attr_destroy");
138 }
1a1a34b4 139}
This page took 0.05514 seconds and 4 git commands to generate.