1/* The tunable framework. See the README to know how to use the tunable in
2 a glibc module.
3
4 Copyright (C) 2016-2022 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
6
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <https://www.gnu.org/licenses/>. */
20
21#ifndef _TUNABLES_H_
22#define _TUNABLES_H_
23
24#if !HAVE_TUNABLES
25static inline void
26__always_inline
27__tunables_init (char **unused __attribute__ ((unused)))
28{
29 /* This is optimized out if tunables are not enabled. */
30}
31#else
32# include <stdbool.h>
33# include <stddef.h>
34# include <stdint.h>
35
36typedef intmax_t tunable_num_t;
37
38typedef union
39{
40 tunable_num_t numval;
41 const char *strval;
42} tunable_val_t;
43
44typedef void (*tunable_callback_t) (tunable_val_t *);
45
46/* Full name for a tunable is top_ns.tunable_ns.id. */
47# define TUNABLE_NAME_S(top,ns,id) #top "." #ns "." #id
48
49# define TUNABLE_ENUM_NAME(__top,__ns,__id) TUNABLE_ENUM_NAME1 (__top,__ns,__id)
50# define TUNABLE_ENUM_NAME1(__top,__ns,__id) __top ## _ ## __ns ## _ ## __id
51
52# include "dl-tunable-list.h"
53
54extern void __tunables_init (char **);
55extern void __tunables_print (void);
56extern void __tunable_get_val (tunable_id_t, void *, tunable_callback_t);
57extern void __tunable_set_val (tunable_id_t, tunable_val_t *, tunable_num_t *,
58 tunable_num_t *);
59rtld_hidden_proto (__tunables_init)
60rtld_hidden_proto (__tunables_print)
61rtld_hidden_proto (__tunable_get_val)
62rtld_hidden_proto (__tunable_set_val)
63
64/* Define TUNABLE_GET and TUNABLE_SET in short form if TOP_NAMESPACE and
65 TUNABLE_NAMESPACE are defined. This is useful shorthand to get and set
66 tunables within a module. */
67#if defined TOP_NAMESPACE && defined TUNABLE_NAMESPACE
68# define TUNABLE_GET(__id, __type, __cb) \
69 TUNABLE_GET_FULL (TOP_NAMESPACE, TUNABLE_NAMESPACE, __id, __type, __cb)
70# define TUNABLE_SET(__id, __val) \
71 TUNABLE_SET_FULL (TOP_NAMESPACE, TUNABLE_NAMESPACE, __id, __val)
72# define TUNABLE_SET_WITH_BOUNDS(__id, __val, __min, __max) \
73 TUNABLE_SET_WITH_BOUNDS_FULL (TOP_NAMESPACE, TUNABLE_NAMESPACE, __id, \
74 __val, __min, __max)
75#else
76# define TUNABLE_GET(__top, __ns, __id, __type, __cb) \
77 TUNABLE_GET_FULL (__top, __ns, __id, __type, __cb)
78# define TUNABLE_SET(__top, __ns, __id, __val) \
79 TUNABLE_SET_FULL (__top, __ns, __id, __val)
80# define TUNABLE_SET_WITH_BOUNDS(__top, __ns, __id, __val, __min, __max) \
81 TUNABLE_SET_WITH_BOUNDS_FULL (__top, __ns, __id, __val, __min, __max)
82#endif
83
84/* Get and return a tunable value. If the tunable was set externally and __CB
85 is defined then call __CB before returning the value. */
86# define TUNABLE_GET_FULL(__top, __ns, __id, __type, __cb) \
87({ \
88 tunable_id_t id = TUNABLE_ENUM_NAME (__top, __ns, __id); \
89 __type ret; \
90 __tunable_get_val (id, &ret, __cb); \
91 ret; \
92})
93
94/* Set a tunable value. */
95# define TUNABLE_SET_FULL(__top, __ns, __id, __val) \
96({ \
97 __tunable_set_val (TUNABLE_ENUM_NAME (__top, __ns, __id), \
98 & (tunable_val_t) {.numval = __val}, NULL, NULL); \
99})
100
101/* Set a tunable value together with min/max values. */
102# define TUNABLE_SET_WITH_BOUNDS_FULL(__top, __ns, __id,__val, __min, __max) \
103({ \
104 __tunable_set_val (TUNABLE_ENUM_NAME (__top, __ns, __id), \
105 & (tunable_val_t) {.numval = __val}, \
106 & (tunable_num_t) {__min}, \
107 & (tunable_num_t) {__max}); \
108})
109
110/* Namespace sanity for callback functions. Use this macro to keep the
111 namespace of the modules clean. */
112# define TUNABLE_CALLBACK(__name) _dl_tunable_ ## __name
113
114# define TUNABLES_FRONTEND_valstring 1
115/* The default value for TUNABLES_FRONTEND. */
116# define TUNABLES_FRONTEND_yes TUNABLES_FRONTEND_valstring
117
118static __always_inline bool
119tunable_val_lt (tunable_num_t lhs, tunable_num_t rhs, bool unsigned_cmp)
120{
121 if (unsigned_cmp)
122 return (uintmax_t) lhs < (uintmax_t) rhs;
123 else
124 return lhs < rhs;
125}
126
127static __always_inline bool
128tunable_val_gt (tunable_num_t lhs, tunable_num_t rhs, bool unsigned_cmp)
129{
130 if (unsigned_cmp)
131 return (uintmax_t) lhs > (uintmax_t) rhs;
132 else
133 return lhs > rhs;
134}
135
136/* Compare two name strings, bounded by the name hardcoded in glibc. */
137static __always_inline bool
138tunable_is_name (const char *orig, const char *envname)
139{
140 for (;*orig != '\0' && *envname != '\0'; envname++, orig++)
141 if (*orig != *envname)
142 break;
143
144 /* The ENVNAME is immediately followed by a value. */
145 if (*orig == '\0' && *envname == '=')
146 return true;
147 else
148 return false;
149}
150
151#endif
152#endif
153

source code of glibc/elf/dl-tunables.h