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