]>
Commit | Line | Data |
---|---|---|
67e58f39 SP |
1 | /* The tunable framework. See the README to know how to use the tunable in |
2 | a glibc module. | |
3 | ||
581c785b | 4 | Copyright (C) 2016-2022 Free Software Foundation, Inc. |
67e58f39 SP |
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 | |
5a82c748 | 19 | <https://www.gnu.org/licenses/>. */ |
67e58f39 SP |
20 | |
21 | #ifndef _TUNABLES_H_ | |
22 | #define _TUNABLES_H_ | |
23 | ||
24 | #if !HAVE_TUNABLES | |
25 | static inline void | |
26 | __always_inline | |
3c589b1a | 27 | __tunables_init (char **unused __attribute__ ((unused))) |
67e58f39 SP |
28 | { |
29 | /* This is optimized out if tunables are not enabled. */ | |
30 | } | |
31 | #else | |
05830806 | 32 | # include <stdbool.h> |
67e58f39 | 33 | # include <stddef.h> |
05830806 | 34 | # include <stdint.h> |
67e58f39 | 35 | |
61117bfa SP |
36 | typedef intmax_t tunable_num_t; |
37 | ||
05830806 | 38 | typedef union |
67e58f39 | 39 | { |
61117bfa | 40 | tunable_num_t numval; |
05830806 SN |
41 | const char *strval; |
42 | } tunable_val_t; | |
67e58f39 | 43 | |
05830806 | 44 | typedef void (*tunable_callback_t) (tunable_val_t *); |
67e58f39 SP |
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 | ||
44330b6d SP |
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 | |
67e58f39 SP |
51 | |
52 | # include "dl-tunable-list.h" | |
53 | ||
54 | extern void __tunables_init (char **); | |
86f65dff | 55 | extern void __tunables_print (void); |
44330b6d | 56 | extern void __tunable_get_val (tunable_id_t, void *, tunable_callback_t); |
61117bfa SP |
57 | extern void __tunable_set_val (tunable_id_t, tunable_val_t *, tunable_num_t *, |
58 | tunable_num_t *); | |
81efada5 | 59 | rtld_hidden_proto (__tunables_init) |
86f65dff | 60 | rtld_hidden_proto (__tunables_print) |
44330b6d | 61 | rtld_hidden_proto (__tunable_get_val) |
dfb8e514 | 62 | rtld_hidden_proto (__tunable_set_val) |
44330b6d SP |
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) | |
61117bfa SP |
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) \ | |
dfb8e514 | 73 | TUNABLE_SET_WITH_BOUNDS_FULL (TOP_NAMESPACE, TUNABLE_NAMESPACE, __id, \ |
61117bfa | 74 | __val, __min, __max) |
44330b6d SP |
75 | #else |
76 | # define TUNABLE_GET(__top, __ns, __id, __type, __cb) \ | |
77 | TUNABLE_GET_FULL (__top, __ns, __id, __type, __cb) | |
61117bfa SP |
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) | |
44330b6d | 82 | #endif |
81efada5 | 83 | |
44330b6d SP |
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) \ | |
67e58f39 | 87 | ({ \ |
44330b6d SP |
88 | tunable_id_t id = TUNABLE_ENUM_NAME (__top, __ns, __id); \ |
89 | __type ret; \ | |
90 | __tunable_get_val (id, &ret, __cb); \ | |
91 | ret; \ | |
67e58f39 SP |
92 | }) |
93 | ||
44330b6d | 94 | /* Set a tunable value. */ |
61117bfa | 95 | # define TUNABLE_SET_FULL(__top, __ns, __id, __val) \ |
67e58f39 | 96 | ({ \ |
44330b6d | 97 | __tunable_set_val (TUNABLE_ENUM_NAME (__top, __ns, __id), \ |
61117bfa | 98 | & (tunable_val_t) {.numval = __val}, NULL, NULL); \ |
dfb8e514 L |
99 | }) |
100 | ||
101 | /* Set a tunable value together with min/max values. */ | |
61117bfa | 102 | # define TUNABLE_SET_WITH_BOUNDS_FULL(__top, __ns, __id,__val, __min, __max) \ |
dfb8e514 L |
103 | ({ \ |
104 | __tunable_set_val (TUNABLE_ENUM_NAME (__top, __ns, __id), \ | |
61117bfa SP |
105 | & (tunable_val_t) {.numval = __val}, \ |
106 | & (tunable_num_t) {__min}, \ | |
107 | & (tunable_num_t) {__max}); \ | |
67e58f39 SP |
108 | }) |
109 | ||
110 | /* Namespace sanity for callback functions. Use this macro to keep the | |
111 | namespace of the modules clean. */ | |
44330b6d | 112 | # define TUNABLE_CALLBACK(__name) _dl_tunable_ ## __name |
6765d5d3 SP |
113 | |
114 | # define TUNABLES_FRONTEND_valstring 1 | |
115 | /* The default value for TUNABLES_FRONTEND. */ | |
116 | # define TUNABLES_FRONTEND_yes TUNABLES_FRONTEND_valstring | |
28cfa3a4 | 117 | |
d1a3dcab SP |
118 | static __always_inline bool |
119 | tunable_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 | ||
127 | static __always_inline bool | |
128 | tunable_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 | ||
28cfa3a4 | 136 | /* Compare two name strings, bounded by the name hardcoded in glibc. */ |
c2d8f0b7 | 137 | static __always_inline bool |
28cfa3a4 SP |
138 | tunable_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 | ||
67e58f39 SP |
151 | #endif |
152 | #endif |