Fix: Set thread stack size to ulimit soft value
[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
30static bool pthread_attr_init_done;
31static pthread_attr_t tattr;
32static pthread_mutex_t tattr_lock = PTHREAD_MUTEX_INITIALIZER;
8b3bd7a3 33
78d8fb4f
JG
34LTTNG_HIDDEN
35size_t default_get_channel_subbuf_size(void)
8b3bd7a3 36{
78d8fb4f
JG
37 return max(_DEFAULT_CHANNEL_SUBBUF_SIZE, PAGE_SIZE);
38}
39
40LTTNG_HIDDEN
41size_t default_get_metadata_subbuf_size(void)
42{
43 return max(DEFAULT_METADATA_SUBBUF_SIZE, PAGE_SIZE);
44}
45
46LTTNG_HIDDEN
47size_t default_get_kernel_channel_subbuf_size(void)
48{
49 return max(DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE, PAGE_SIZE);
50}
51
52LTTNG_HIDDEN
53size_t default_get_ust_pid_channel_subbuf_size(void)
54{
55 return max(DEFAULT_UST_PID_CHANNEL_SUBBUF_SIZE, PAGE_SIZE);
56}
57
58LTTNG_HIDDEN
59size_t default_get_ust_uid_channel_subbuf_size(void)
60{
61 return max(DEFAULT_UST_UID_CHANNEL_SUBBUF_SIZE, PAGE_SIZE);
8b3bd7a3 62}
1a1a34b4
MJ
63
64LTTNG_HIDDEN
65pthread_attr_t *default_pthread_attr(void)
66{
67 int ret = 0;
68 size_t ptstacksize;
69 struct rlimit rlim;
70
71 pthread_mutex_lock(&tattr_lock);
72
73 /* Return cached value. */
74 if (pthread_attr_init_done) {
75 goto end;
76 }
77
78 /* Get system stack size limits. */
79 ret = getrlimit(RLIMIT_STACK, &rlim);
80 if (ret < 0) {
81 PERROR("getrlimit");
82 goto error;
83 }
84 DBG("Stack size limits: soft %lld, hard %lld bytes",
85 (long long) rlim.rlim_cur,
86 (long long) rlim.rlim_max);
87
88 /* Get pthread default thread stack size. */
89 ret = pthread_attr_getstacksize(&tattr, &ptstacksize);
90 if (ret < 0) {
91 PERROR("pthread_attr_getstacksize");
92 goto error;
93 }
94 DBG("Default pthread stack size is %zu bytes", ptstacksize);
95
96 /* Check if the default pthread stack size honors ulimits. */
97 if (ptstacksize < rlim.rlim_cur) {
98 DBG("Your libc doesn't honor stack size limits, setting thread stack size to soft limit (%lld bytes)",
99 (long long) rlim.rlim_cur);
100
101 /* Create pthread_attr_t struct with ulimit stack size. */
102 ret = pthread_attr_setstacksize(&tattr, rlim.rlim_cur);
103 if (ret < 0) {
104 PERROR("pthread_attr_setstacksize");
105 goto error;
106 }
107 }
108
109 /* Enable cached value. */
110 pthread_attr_init_done = true;
111end:
112 pthread_mutex_unlock(&tattr_lock);
113 return &tattr;
114error:
115 pthread_mutex_unlock(&tattr_lock);
116 WARN("Failed to initialize pthread attributes, using libc defaults.");
117 return NULL;
118}
This page took 0.037829 seconds and 4 git commands to generate.