forked from filebench/filebench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fb_cvar.c
454 lines (366 loc) · 9.31 KB
/
fb_cvar.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
* fb_cvar.c
*
* Support for custom variables in Filebench.
*
* @Author Santhosh Kumar Koundinya (santhosh@fsl.cs.sunysb.edu)
*/
#include <sys/types.h>
#include <dirent.h>
#include <limits.h>
#include <dlfcn.h>
#include "ipc.h"
#include "fb_cvar.h"
/* Some helpers. */
static int alloc_cvar_lib_info(const char *filename);
static char *gettype(const char *filename);
static cvar_library_t *init_cvar_library(cvar_library_info_t *cvar_lib_info);
static void *load_library(const char *filename);
static void free_cvar_library(cvar_library_t *c);
static int init_cvar_library_ops(cvar_library_t *c);
/* Points to the head of an array of pointers to cvar_library_t. */
cvar_library_t **cvar_libraries;
/*
* Allocate space for a new custom variable in the shared memory location.
*/
cvar_t *
cvar_alloc(void)
{
cvar_t *cvar;
if ((cvar = (cvar_t *)ipc_malloc(FILEBENCH_CVAR)) == NULL) {
filebench_log(LOG_ERROR, "Out of memory for custom variable");
return (NULL);
}
/* place on the head of the global list */
cvar->next = filebench_shm->shm_cvar_list;
filebench_shm->shm_cvar_list = cvar;
return (cvar);
}
/*
* Initialize cvar_library_info structures in the shared memory.
* Return 0 on success and a non zero error code on failure.
*/
int
init_cvar_library_info(const char *dirpath)
{
DIR *libdir = NULL;
struct dirent *dirent;
char *filename = NULL;
int ret = -1;
int dirpath_len = strlen(dirpath);
int direntlen;
filename = (char *) malloc(dirpath_len + 1 + NAME_MAX + 1);
if (!filename) {
filebench_log(LOG_ERROR, "Out of memory");
goto out;
}
strcpy(filename, dirpath);
filename[dirpath_len] = '/';
filename[dirpath_len + NAME_MAX] = '\0';
libdir = opendir(dirpath);
if (!libdir) {
filebench_log(LOG_ERROR, "Failed to open cvar directory");
ret = 0;
goto out;
}
while ((dirent = readdir(libdir)) != NULL) {
if (!strcmp(".", dirent->d_name) || !strcmp("..", dirent->d_name))
continue;
direntlen = strlen(dirent->d_name);
if (strcmp(".so", dirent->d_name + direntlen - 3))
continue;
strncpy(filename + dirpath_len + 1, dirent->d_name, NAME_MAX);
ret = alloc_cvar_lib_info(filename);
if (ret)
goto out;
}
ret = 0;
out:
if (filename)
free(filename);
if (libdir)
closedir(libdir);
return ret;
}
/*
* Return 0 on success and a non zero error code on failure.
*/
static int
alloc_cvar_lib_info(const char *filename)
{
int ret = -1;
cvar_library_info_t *cli = NULL;
cvar_library_info_t *t;
cli = (cvar_library_info_t *) ipc_malloc(FILEBENCH_CVAR_LIB_INFO);
if (!cli)
goto out;
cli->filename = ipc_stralloc(filename);
if (!cli->filename)
goto out;
cli->type = ipc_stralloc(gettype(filename));
if (!cli->type)
goto out;
cli->next = NULL;
if (filebench_shm->shm_cvar_lib_info_list) {
for (t = filebench_shm->shm_cvar_lib_info_list; t->next != NULL;
t = t->next); /* Seek to the last entry. */
cli->index = t->index + 1;
t->next = cli;
} else {
cli->index = 0;
filebench_shm->shm_cvar_lib_info_list = cli;
}
ret = 0;
out:
if (ret && cli) {
/* NOTE: There is no mechanism to free cli->filename and cli->type. */
ipc_free(FILEBENCH_CVAR_LIB_INFO, (char *) cli);
}
return ret;
}
/*
* Returns the 'type' name from the library.
*/
static char
*gettype(const char *filename)
{
char libprefix[] = "lib";
const char *libname;
char *type;
int type_len;
const char *t;
libname = strrchr(filename, '/');
if (!libname)
libname = filename; /* filename is not a fully qualified path. */
else {
libname++;
/* Check for a malformed filename string. */
if (!libname) {
filebench_log(LOG_ERROR, "Malformed cvar library filename");
return NULL;
}
}
/* Strip the leading "lib". */
if (!strncmp(libprefix, libname, sizeof(libprefix) - 1))
libname += sizeof(char) * (sizeof(libprefix) - 1);
if (!libname) {
filebench_log(LOG_ERROR, "Malformed cvar library filename");
return NULL;
}
/* Look for the first '.'. */
type_len = 0;
for (t = libname; *t != '\0' && *t != '.'; t++)
type_len++;
type = (char *) malloc(type_len + 1);
if (!type) {
filebench_log(LOG_ERROR, "Out of memory");
return NULL;
}
strncpy(type, libname, type_len);
type[type_len] = '\0';
return type;
}
/*
* Load shared objects.
* Returns 0 on success and non-zero on error.
*/
int
init_cvar_libraries()
{
int count;
int ret = -1;
int i;
cvar_library_info_t *t;
if (!filebench_shm->shm_cvar_lib_info_list) {
/* Nothing to do. */
return 0;
}
count = 0;
for (t = filebench_shm->shm_cvar_lib_info_list; t != NULL;
t = t->next)
count++;
cvar_libraries = (cvar_library_t **)
malloc(sizeof(cvar_library_t *) * count);
if (!cvar_libraries) {
filebench_log(LOG_ERROR, "Out of memory");
goto out;
}
for (t = filebench_shm->shm_cvar_lib_info_list, i = 0; t != NULL;
t = t->next, i++) {
if ((cvar_libraries[i] = init_cvar_library(t)) == NULL) {
goto out;
}
}
ret = 0;
out:
return ret;
}
static cvar_library_t
*init_cvar_library(cvar_library_info_t *cvar_lib_info)
{
cvar_library_t *c = NULL;
int ret;
c = (cvar_library_t *) malloc(sizeof(cvar_library_t));
if (!c) {
filebench_log(LOG_ERROR, "Out of memory");
goto out;
}
c->cvar_lib_info = cvar_lib_info;
c->lib_handle = load_library(cvar_lib_info->filename);
if (!c->lib_handle)
goto cleanup;
ret = init_cvar_library_ops(c);
if (ret)
goto cleanup;
if (c->cvar_op.cvar_module_init) {
ret = c->cvar_op.cvar_module_init();
if (ret) {
filebench_log(LOG_ERROR, "Failed to initialize custom variable of type"
" %s. cvar_module_init failed with error %d",
cvar_lib_info->type, ret);
goto cleanup;
}
}
out:
return c;
cleanup:
if (c) {
free_cvar_library(c);
free(c);
c = NULL;
}
goto out;
}
static void
*load_library(const char *filename)
{
void *lib_handle = dlopen(filename, RTLD_LOCAL | RTLD_NOW);
if (!lib_handle)
filebench_log(LOG_ERROR, "Unable to load library %s: %s",
filename, dlerror());
return lib_handle;
}
static void
free_cvar_library(cvar_library_t *c)
{
if (c) {
if (c->lib_handle) {
dlclose(c->lib_handle);
}
}
}
static int
init_cvar_library_ops(cvar_library_t *c)
{
int ret = -1;
c->cvar_op.cvar_module_init = dlsym(c->lib_handle, FB_CVAR_MODULE_INIT);
c->cvar_op.cvar_alloc_handle = dlsym(c->lib_handle, FB_CVAR_ALLOC_HANDLE);
if (!c->cvar_op.cvar_alloc_handle) {
filebench_log(LOG_ERROR, "Unable to find " FB_CVAR_ALLOC_HANDLE
": %s.", dlerror());
goto out;
}
c->cvar_op.cvar_revalidate_handle = dlsym(c->lib_handle,
FB_CVAR_REVALIDATE_HANDLE);
if (!c->cvar_op.cvar_revalidate_handle) {
filebench_log(LOG_ERROR, "Unable to find " FB_CVAR_REVALIDATE_HANDLE
": %s", dlerror());
}
c->cvar_op.cvar_next_value = dlsym(c->lib_handle, FB_CVAR_NEXT_VALUE);
if (!c->cvar_op.cvar_next_value) {
filebench_log(LOG_ERROR, "Unable to find " FB_CVAR_NEXT_VALUE
": %s.", dlerror());
goto out;
}
c->cvar_op.cvar_free_handle = dlsym(c->lib_handle, FB_CVAR_FREE_HANDLE);
if (!c->cvar_op.cvar_free_handle) {
filebench_log(LOG_ERROR, "Unable to find " FB_CVAR_FREE_HANDLE
": %s.", dlerror());
goto out;
}
c->cvar_op.cvar_module_exit = dlsym(c->lib_handle, FB_CVAR_MODULE_EXIT);
c->cvar_op.cvar_usage = dlsym(c->lib_handle, FB_CVAR_USAGE);
c->cvar_op.cvar_version = dlsym(c->lib_handle, FB_CVAR_VERSION);
ret = 0;
out:
return ret;
}
int
init_cvar_handle(cvar_t *cvar, const char *type, const char *parameters)
{
int ret = -1;
cvar_library_t *cvar_lib;
cvar_library_info_t *t;
for (t = filebench_shm->shm_cvar_lib_info_list; t != NULL; t = t->next) {
if (!strcmp(type, t->type))
break;
}
if (!t) {
filebench_log(LOG_ERROR, "Undefined custom variable %s", type);
goto out;
}
cvar->cvar_lib_info = t;
cvar_lib = cvar_libraries[cvar->cvar_lib_info->index];
cvar->cvar_handle = cvar_lib->cvar_op.cvar_alloc_handle(parameters,
ipc_cvar_heapalloc, ipc_cvar_heapfree);
if (!cvar->cvar_handle)
goto out;
ret = 0;
out:
return ret;
}
double
get_cvar_value(cvar_t *cvar)
{
int ret;
double value = 0.0;
fbint_t round = cvar->round;
ipc_mutex_lock(&cvar->cvar_lock);
cvar_library_t *cvar_lib = cvar_libraries[cvar->cvar_lib_info->index];
ret = cvar_lib->cvar_op.cvar_next_value(cvar->cvar_handle, &value);
ipc_mutex_unlock(&cvar->cvar_lock);
if (ret) {
filebench_log(LOG_ERROR, "Unable to get next_value from custom variable"
" of type %s", cvar->cvar_lib_info->type);
filebench_shutdown(1);
}
if (round) {
fbint_t num, lower, upper;
num = (fbint_t) value;
lower = num - (num % round);
upper = lower + round;
value = (num - lower) > (upper - num) ? upper : lower;
}
if (value < cvar->min)
value = cvar->min;
else if (value > cvar->max)
value = cvar->max;
return value;
}
/*
* Return 0 on success and a non-zero error code on failure.
*/
int
revalidate_cvar_handles()
{
cvar_t *t;
cvar_library_t *cvar_lib;
int ret;
if (!filebench_shm->shm_cvar_list)
return 0; /* Nothing to do. */
for (t = filebench_shm->shm_cvar_list; t != NULL; t = t->next) {
cvar_lib = cvar_libraries[t->cvar_lib_info->index];
if (cvar_lib->cvar_op.cvar_revalidate_handle) {
ipc_mutex_lock(&t->cvar_lock);
ret = cvar_lib->cvar_op.cvar_revalidate_handle(t->cvar_handle);
ipc_mutex_unlock(&t->cvar_lock);
if (ret) {
filebench_log(LOG_ERROR, "Revalidation failed for cvar_handle "
"of type %s with error code %d", t->cvar_lib_info->type,
ret);
return ret;
}
}
}
return 0;
}