1/* The tunable framework. See the README to know how to use the tunable in
2 a glibc module.
3
4 Copyright (C) 2016-2023 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#include <stdbool.h>
25#include <stddef.h>
26#include <stdint.h>
27
28typedef intmax_t tunable_num_t;
29
30typedef union
31{
32 tunable_num_t numval;
33 const char *strval;
34} tunable_val_t;
35
36typedef void (*tunable_callback_t) (tunable_val_t *);
37
38/* Full name for a tunable is top_ns.tunable_ns.id. */
39#define TUNABLE_NAME_S(top,ns,id) #top "." #ns "." #id
40
41#define TUNABLE_ENUM_NAME(__top,__ns,__id) TUNABLE_ENUM_NAME1 (__top,__ns,__id)
42#define TUNABLE_ENUM_NAME1(__top,__ns,__id) __top ## _ ## __ns ## _ ## __id
43
44#include "dl-tunable-list.h"
45
46extern void __tunables_init (char **);
47extern void __tunables_print (void);
48extern void __tunable_get_val (tunable_id_t, void *, tunable_callback_t);
49extern void __tunable_set_val (tunable_id_t, tunable_val_t *, tunable_num_t *,
50 tunable_num_t *);
51rtld_hidden_proto (__tunables_init)
52rtld_hidden_proto (__tunables_print)
53rtld_hidden_proto (__tunable_get_val)
54rtld_hidden_proto (__tunable_set_val)
55
56/* Define TUNABLE_GET and TUNABLE_SET in short form if TOP_NAMESPACE and
57 TUNABLE_NAMESPACE are defined. This is useful shorthand to get and set
58 tunables within a module. */
59#if defined TOP_NAMESPACE && defined TUNABLE_NAMESPACE
60# define TUNABLE_GET(__id, __type, __cb) \
61 TUNABLE_GET_FULL (TOP_NAMESPACE, TUNABLE_NAMESPACE, __id, __type, __cb)
62# define TUNABLE_SET(__id, __val) \
63 TUNABLE_SET_FULL (TOP_NAMESPACE, TUNABLE_NAMESPACE, __id, __val)
64# define TUNABLE_SET_WITH_BOUNDS(__id, __val, __min, __max) \
65 TUNABLE_SET_WITH_BOUNDS_FULL (TOP_NAMESPACE, TUNABLE_NAMESPACE, __id, \
66 __val, __min, __max)
67#else
68# define TUNABLE_GET(__top, __ns, __id, __type, __cb) \
69 TUNABLE_GET_FULL (__top, __ns, __id, __type, __cb)
70# define TUNABLE_SET(__top, __ns, __id, __val) \
71 TUNABLE_SET_FULL (__top, __ns, __id, __val)
72# define TUNABLE_SET_WITH_BOUNDS(__top, __ns, __id, __val, __min, __max) \
73 TUNABLE_SET_WITH_BOUNDS_FULL (__top, __ns, __id, __val, __min, __max)
74#endif
75
76/* Get and return a tunable value. If the tunable was set externally and __CB
77 is defined then call __CB before returning the value. */
78#define TUNABLE_GET_FULL(__top, __ns, __id, __type, __cb) \
79({ \
80 tunable_id_t id = TUNABLE_ENUM_NAME (__top, __ns, __id); \
81 __type ret; \
82 __tunable_get_val (id, &ret, __cb); \
83 ret; \
84})
85
86/* Set a tunable value. */
87#define TUNABLE_SET_FULL(__top, __ns, __id, __val) \
88({ \
89 __tunable_set_val (TUNABLE_ENUM_NAME (__top, __ns, __id), \
90 & (tunable_val_t) {.numval = __val}, NULL, NULL); \
91})
92
93/* Set a tunable value together with min/max values. */
94#define TUNABLE_SET_WITH_BOUNDS_FULL(__top, __ns, __id,__val, __min, __max) \
95({ \
96 __tunable_set_val (TUNABLE_ENUM_NAME (__top, __ns, __id), \
97 & (tunable_val_t) {.numval = __val}, \
98 & (tunable_num_t) {__min}, \
99 & (tunable_num_t) {__max}); \
100})
101
102/* Namespace sanity for callback functions. Use this macro to keep the
103 namespace of the modules clean. */
104#define TUNABLE_CALLBACK(__name) _dl_tunable_ ## __name
105
106static __always_inline bool
107tunable_val_lt (tunable_num_t lhs, tunable_num_t rhs, bool unsigned_cmp)
108{
109 if (unsigned_cmp)
110 return (uintmax_t) lhs < (uintmax_t) rhs;
111 else
112 return lhs < rhs;
113}
114
115static __always_inline bool
116tunable_val_gt (tunable_num_t lhs, tunable_num_t rhs, bool unsigned_cmp)
117{
118 if (unsigned_cmp)
119 return (uintmax_t) lhs > (uintmax_t) rhs;
120 else
121 return lhs > rhs;
122}
123
124/* Compare two name strings, bounded by the name hardcoded in glibc. */
125static __always_inline bool
126tunable_is_name (const char *orig, const char *envname)
127{
128 for (;*orig != '\0' && *envname != '\0'; envname++, orig++)
129 if (*orig != *envname)
130 break;
131
132 /* The ENVNAME is immediately followed by a value. */
133 if (*orig == '\0' && *envname == '=')
134 return true;
135 else
136 return false;
137}
138
139#endif
140