forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore-setting.c
338 lines (316 loc) · 9.34 KB
/
core-setting.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* Copyright (C) 2017-2021 Canonical, Ltd.
* Copyright (C) 2022-2023 Colin Ian King.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "stress-ng.h"
static stress_setting_t *setting_head; /* setting list head */
static stress_setting_t *setting_tail; /* setting list tail */
#if defined(DEBUG_SETTINGS)
#define DBG(...) pr_inf(__VA_ARGS__)
#else
#define DBG(...)
#endif
/*
* stress_settings_free()
* free the saved settings
*/
void stress_settings_free(void)
{
stress_setting_t *setting = setting_head;
while (setting) {
stress_setting_t *next = setting->next;
if (setting->type_id == TYPE_ID_STR)
free(setting->u.str);
free(setting->name);
free(setting);
setting = next;
}
setting_head = NULL;
setting_tail = NULL;
}
/*
* stress_set_setting_generic()
* set a new setting
*/
static int stress_set_setting_generic(
const char *name,
const stress_type_id_t type_id,
const void *value,
const bool global)
{
stress_setting_t *setting;
if (!value) {
(void)fprintf(stderr, "invalid setting '%s' value address (null)\n", name);
_exit(EXIT_NOT_SUCCESS);
}
setting = calloc(1, sizeof *setting);
if (!setting)
goto err;
setting->name = strdup(name);
setting->proc = g_stressor_current;
setting->type_id = type_id;
setting->global = global;
if (!setting->name) {
free(setting);
goto err;
}
DBG("%s: %s, global = %s\n", __func__, name, global ? "true" : "false");
switch (type_id) {
case TYPE_ID_UINT8:
setting->u.uint8 = *(const uint8_t *)value;
DBG("%s: UINT8: %s -> %" PRIu8 "\n", __func__, name, setting->u.uint8);
break;
case TYPE_ID_INT8:
setting->u.int8 = *(const int8_t *)value;
DBG("%s: INT8: %s -> %" PRId8 "\n", __func__, name, setting->u.int8);
break;
case TYPE_ID_UINT16:
setting->u.uint16 = *(const uint16_t *)value;
DBG("%s: UINT16: %s -> %" PRIu16 "\n", __func__, name, setting->u.uint16);
break;
case TYPE_ID_INT16:
setting->u.int16 = *(const int16_t *)value;
DBG("%s: INT16: %s -> %" PRId16 "\n", __func__, name, setting->u.int16);
break;
case TYPE_ID_UINT32:
setting->u.uint32 = *(const uint32_t *)value;
DBG("%s: UINT32: %s -> %" PRIu32 "\n", __func__, name, setting->u.uint32);
break;
case TYPE_ID_INT32:
setting->u.int32 = *(const int32_t *)value;
DBG("%s: INT32: %s -> %" PRId32 "\n", __func__, name, setting->u.int32);
break;
case TYPE_ID_UINT64:
setting->u.uint64 = *(const uint64_t *)value;
DBG("%s: UINT64: %s -> %" PRIu64 "\n", __func__, name, setting->u.uint64);
break;
case TYPE_ID_INT64:
setting->u.int64 = *(const int64_t *)value;
DBG("%s: INT64: %s -> %" PRId64 "\n", __func__, name, setting->u.int64);
break;
case TYPE_ID_SIZE_T:
setting->u.size = *(const size_t *)value;
DBG("%s: SIZE_T: %s -> %zu\n", __func__, name, setting->u.size);
break;
case TYPE_ID_SSIZE_T:
setting->u.ssize = *(const ssize_t *)value;
DBG("%s: SSIZE_T: %s -> %zd\n", __func__, name, setting->u.ssize);
break;
case TYPE_ID_UINT:
setting->u.uint = *(const unsigned int *)value;
DBG("%s: UINT: %s -> %u\n", __func__, name, setting->u.uint);
break;
case TYPE_ID_INT:
setting->u.sint = *(const int *)value;
DBG("%s: UINT: %s -> %d\n", __func__, name, setting->u.sint);
break;
case TYPE_ID_ULONG:
setting->u.ulong = *(const unsigned long *)value;
DBG("%s: ULONG: %s -> %lu\n", __func__, name, setting->u.ulong);
break;
case TYPE_ID_LONG:
setting->u.slong = *(const long *)value;
DBG("%s: LONG: %s -> %ld\n", __func__, name, setting->u.slong);
break;
case TYPE_ID_OFF_T:
setting->u.off = *(const long *)value;
DBG("%s: OFF_T: %s -> %lu\n", __func__, name, (unsigned long)setting->u.off);
break;
case TYPE_ID_STR:
setting->u.str = stress_const_optdup(value);
if (!setting->u.str) {
free(setting->name);
free(setting);
goto err;
}
DBG("%s: STR: %s -> %s\n", __func__, name, setting->u.str);
break;
case TYPE_ID_BOOL:
setting->u.boolean = *(const bool *)value;
DBG("%s: BOOL: %s -> %d\n", __func__, name, setting->u.boolean);
break;
case TYPE_ID_UINTPTR_T:
setting->u.uintptr = *(const uintptr_t *)value;
DBG("%s: UINTPTR_R: %s -> %p\n", __func__, name, (void *)setting->u.uintptr);
break;
case TYPE_ID_UNDEFINED:
default:
DBG("%s: UNDEF: %s -> ?\n", __func__, name);
break;
}
if (setting_tail) {
setting_tail->next = setting;
} else {
setting_head = setting;
}
setting_tail = setting;
return 0;
err:
(void)fprintf(stderr, "cannot allocate setting '%s'\n", name);
_exit(EXIT_NO_RESOURCE);
return 0;
}
/*
* stress_set_setting()
* set a new setting
*/
int stress_set_setting(
const char *name,
const stress_type_id_t type_id,
const void *value)
{
return stress_set_setting_generic(name, type_id, value, false);
}
/*
* stress_set_setting_global()
* set a new global setting;
*/
int stress_set_setting_global(
const char *name,
const stress_type_id_t type_id,
const void *value)
{
return stress_set_setting_generic(name, type_id, value, true);
}
/*
* stress_get_setting()
* get an existing setting
*/
bool stress_get_setting(const char *name, void *value)
{
stress_setting_t *setting;
bool set = false;
bool found = false;
DBG("%s: get %s\n", __func__, name);
for (setting = setting_head; setting; setting = setting->next) {
if (setting->proc == g_stressor_current)
found = true;
if (found && ((setting->proc != g_stressor_current) && (!setting->global)))
break;
if (!strcmp(setting->name, name)) {
switch (setting->type_id) {
case TYPE_ID_UINT8:
set = true;
*(uint8_t *)value = setting->u.uint8;
DBG("%s: UINT8: %s -> %" PRIu8 "\n", __func__, name, setting->u.uint8);
break;
case TYPE_ID_INT8:
set = true;
*(int8_t *)value = setting->u.int8;
DBG("%s: INT8: %s -> %" PRId8 "\n", __func__, name, setting->u.int8);
break;
case TYPE_ID_UINT16:
set = true;
*(uint16_t *)value = setting->u.uint16;
DBG("%s: UINT16: %s -> %" PRIu16 "\n", __func__, name, setting->u.uint16);
break;
case TYPE_ID_INT16:
set = true;
*(int16_t *)value = setting->u.int16;
DBG("%s: INT16: %s -> %" PRId16 "\n", __func__, name, setting->u.int16);
break;
case TYPE_ID_UINT32:
set = true;
*(uint32_t *)value = setting->u.uint32;
DBG("%s: UINT32: %s -> %" PRIu32 "\n", __func__, name, setting->u.uint32);
break;
case TYPE_ID_INT32:
set = true;
*(int32_t *)value = setting->u.int32;
DBG("%s: INT32: %s -> %" PRId32 "\n", __func__, name, setting->u.int32);
break;
case TYPE_ID_UINT64:
set = true;
*(uint64_t *)value = setting->u.uint64;
DBG("%s: UINT64: %s -> %" PRIu64 "\n", __func__, name, setting->u.uint64);
break;
case TYPE_ID_INT64:
set = true;
*(int64_t *)value = setting->u.int64;
DBG("%s: INT64: %s -> %" PRId64 "\n", __func__, name, setting->u.int64);
break;
case TYPE_ID_SIZE_T:
set = true;
*(size_t *)value = setting->u.size;
DBG("%s: SIZE_T: %s -> %zu\n", __func__, name, setting->u.size);
break;
case TYPE_ID_SSIZE_T:
set = true;
*(ssize_t *)value = setting->u.ssize;
DBG("%s: SSIZE_T: %s -> %zd\n", __func__, name, setting->u.ssize);
break;
case TYPE_ID_UINT:
set = true;
*(unsigned int *)value = setting->u.uint;
DBG("%s: UINT: %s -> %u\n", __func__, name, setting->u.uint);
break;
case TYPE_ID_INT:
set = true;
*(int *)value = setting->u.sint;
DBG("%s: UINT: %s -> %d\n", __func__, name, setting->u.sint);
break;
case TYPE_ID_ULONG:
set = true;
*(unsigned long *)value = setting->u.ulong;
DBG("%s: ULONG: %s -> %lu\n", __func__, name, setting->u.ulong);
break;
case TYPE_ID_LONG:
set = true;
*(long *)value = setting->u.slong;
DBG("%s: LONG: %s -> %ld\n", __func__, name, setting->u.slong);
break;
case TYPE_ID_OFF_T:
set = true;
*(long *)value = setting->u.off;
DBG("%s: OFF_T: %s -> %lu\n", __func__, name, (unsigned long)setting->u.off);
break;
case TYPE_ID_STR:
set = true;
*(const char **)value = setting->u.str;
DBG("%s: STR: %s -> %s\n", __func__, name, setting->u.str);
break;
case TYPE_ID_BOOL:
set = true;
*(bool *)value = setting->u.boolean;
DBG("%s: BOOL: %s -> %d\n", __func__, name, setting->u.boolean);
break;
case TYPE_ID_UINTPTR_T:
set = true;
*(uintptr_t *)value = setting->u.uintptr;
DBG("%s: UINTPTR_T: %s -> %p\n", __func__, name, (void *)setting->u.uintptr);
break;
case TYPE_ID_UNDEFINED:
default:
set = true;
DBG("%s: UNDEF: %s -> ?\n", __func__, name);
break;
}
}
}
return set;
}
/*
* stress_set_setting_true()
* create a setting of name name to true, ignore opt
*/
int stress_set_setting_true(const char *name, const char *opt)
{
bool val = true;
(void)opt;
return stress_set_setting(name, TYPE_ID_BOOL, &val);
}