Detect and print when the OS doesn't support huge pages
[fio.git] / parse.c
CommitLineData
cb2c86fd
JA
1/*
2 * This file contains the ini and command liner parser main.
3 */
4#include <stdio.h>
5#include <stdlib.h>
6#include <unistd.h>
7#include <ctype.h>
8#include <string.h>
9#include <errno.h>
10#include <limits.h>
11
12#include "parse.h"
13
f085737f
JA
14static int vp_cmp(const void *p1, const void *p2)
15{
16 const struct value_pair *vp1 = p1;
17 const struct value_pair *vp2 = p2;
18
19 return strlen(vp2->ival) - strlen(vp1->ival);
20}
21
22static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
23{
24 const struct value_pair *vp;
25 int entries;
26
27 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
28
29 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
30 vp = &o->posval[entries];
31 if (!vp->ival || vp->ival[0] == '\0')
32 break;
33
34 memcpy(&vpmap[entries], vp, sizeof(*vp));
35 }
36
37 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
38}
39
b1ec1da6
JA
40static void show_option_range(struct fio_option *o)
41{
42 if (!o->minval && !o->maxval)
43 return;
44
ec1aee01 45 printf("%20s: min=%d, max=%d\n", "range", o->minval, o->maxval);
b1ec1da6
JA
46}
47
48static void show_option_values(struct fio_option *o)
49{
b1ec1da6
JA
50 int i = 0;
51
52 do {
7837213b 53 const struct value_pair *vp = &o->posval[i];
b1ec1da6 54
7837213b
JA
55 if (!vp->ival)
56 break;
b1ec1da6 57
7837213b
JA
58 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
59 if (vp->help)
60 printf(" %s", vp->help);
61 printf("\n");
b1ec1da6
JA
62 i++;
63 } while (i < PARSE_MAX_VP);
64
65 if (i)
66 printf("\n");
67}
68
cb2c86fd
JA
69static unsigned long get_mult_time(char c)
70{
71 switch (c) {
72 case 'm':
73 case 'M':
74 return 60;
75 case 'h':
76 case 'H':
77 return 60 * 60;
78 case 'd':
79 case 'D':
80 return 24 * 60 * 60;
81 default:
82 return 1;
83 }
84}
85
86static unsigned long get_mult_bytes(char c)
87{
88 switch (c) {
89 case 'k':
90 case 'K':
91 return 1024;
92 case 'm':
93 case 'M':
94 return 1024 * 1024;
95 case 'g':
96 case 'G':
97 return 1024 * 1024 * 1024;
f3502ba2
JA
98 case 'e':
99 case 'E':
100 return 1024 * 1024 * 1024 * 1024UL;
cb2c86fd
JA
101 default:
102 return 1;
103 }
104}
105
106/*
e1f36503 107 * convert string into decimal value, noting any size suffix
cb2c86fd 108 */
63f29372 109static int str_to_decimal(const char *str, long long *val, int kilo)
cb2c86fd 110{
cb2c86fd
JA
111 int len;
112
cb2c86fd 113 len = strlen(str);
f90eff5a
JA
114 if (!len)
115 return 1;
cb2c86fd 116
675de85a 117 *val = strtoll(str, NULL, 10);
cda866ca 118 if (*val == LONG_MAX && errno == ERANGE)
cb2c86fd
JA
119 return 1;
120
121 if (kilo)
122 *val *= get_mult_bytes(str[len - 1]);
123 else
124 *val *= get_mult_time(str[len - 1]);
787f7e95 125
cb2c86fd
JA
126 return 0;
127}
128
63f29372 129static int check_str_bytes(const char *p, long long *val)
cb2c86fd 130{
cb2c86fd
JA
131 return str_to_decimal(p, val, 1);
132}
133
63f29372 134static int check_str_time(const char *p, long long *val)
cb2c86fd 135{
cb2c86fd
JA
136 return str_to_decimal(p, val, 0);
137}
138
139void strip_blank_front(char **p)
140{
141 char *s = *p;
142
143 while (isspace(*s))
144 s++;
4d651dad
JA
145
146 *p = s;
cb2c86fd
JA
147}
148
149void strip_blank_end(char *p)
150{
523bfadb
JA
151 char *s;
152
153 s = strchr(p, ';');
154 if (s)
155 *s = '\0';
156 s = strchr(p, '#');
157 if (s)
158 *s = '\0';
159 if (s)
160 p = s;
161
162 s = p + strlen(p) - 1;
cb2c86fd
JA
163 while (isspace(*s) || iscntrl(*s))
164 s--;
165
166 *(s + 1) = '\0';
167}
168
63f29372 169static int check_range_bytes(const char *str, long *val)
cb2c86fd
JA
170{
171 char suffix;
172
787f7e95
JA
173 if (!strlen(str))
174 return 1;
175
cb2c86fd
JA
176 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
177 *val *= get_mult_bytes(suffix);
178 return 0;
179 }
180
181 if (sscanf(str, "%lu", val) == 1)
182 return 0;
183
184 return 1;
185}
186
63f29372 187static int check_int(const char *p, int *val)
cb2c86fd 188{
787f7e95
JA
189 if (!strlen(p))
190 return 1;
e1f36503
JA
191 if (sscanf(p, "%u", val) == 1)
192 return 0;
cb2c86fd 193
e1f36503
JA
194 return 1;
195}
cb2c86fd 196
e1f36503
JA
197static struct fio_option *find_option(struct fio_option *options,
198 const char *opt)
199{
4945ba12 200 struct fio_option *o;
cb2c86fd 201
4945ba12 202 for (o = &options[0]; o->name; o++) {
e1f36503
JA
203 if (!strcmp(o->name, opt))
204 return o;
03b74b3e
JA
205 else if (o->alias && !strcmp(o->alias, opt))
206 return o;
33963c6c 207 }
cb2c86fd 208
e1f36503 209 return NULL;
cb2c86fd
JA
210}
211
17abbe89
JA
212#define val_store(ptr, val, off, data) \
213 do { \
214 ptr = td_var((data), (off)); \
215 *ptr = (val); \
216 } while (0)
217
f90eff5a 218static int __handle_option(struct fio_option *o, const char *ptr, void *data,
787f7e95 219 int first, int more)
cb2c86fd 220{
63f29372
JA
221 int il, *ilp;
222 long long ull, *ullp;
223 long ul1, ul2;
b4692828 224 char **cp;
e1f36503 225 int ret = 0, is_time = 0;
cb2c86fd 226
08e26e35
JA
227 if (!ptr && o->type != FIO_OPT_STR_SET) {
228 fprintf(stderr, "Option %s requires an argument\n", o->name);
229 return 1;
230 }
231
e1f36503
JA
232 switch (o->type) {
233 case FIO_OPT_STR: {
234 fio_opt_str_fn *fn = o->cb;
b1ec1da6 235 const struct value_pair *vp;
f085737f 236 struct value_pair posval[PARSE_MAX_VP];
b1ec1da6
JA
237 int i;
238
f085737f
JA
239 posval_sort(o, posval);
240
b1ec1da6 241 for (i = 0; i < PARSE_MAX_VP; i++) {
f085737f 242 vp = &posval[i];
b1ec1da6
JA
243 if (!vp->ival || vp->ival[0] == '\0')
244 break;
6612a27b 245 ret = 1;
b1ec1da6
JA
246 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
247 ret = 0;
248 if (!o->off1)
249 break;
250 val_store(ilp, vp->oval, o->off1, data);
251 break;
252 }
253 }
cb2c86fd 254
b1ec1da6
JA
255 if (ret)
256 show_option_values(o);
257 else if (fn)
258 ret = fn(data, ptr);
e1f36503
JA
259 break;
260 }
261 case FIO_OPT_STR_VAL_TIME:
262 is_time = 1;
75e6f36f
JA
263 case FIO_OPT_STR_VAL:
264 case FIO_OPT_STR_VAL_INT: {
e1f36503
JA
265 fio_opt_str_val_fn *fn = o->cb;
266
267 if (is_time)
268 ret = check_str_time(ptr, &ull);
269 else
270 ret = check_str_bytes(ptr, &ull);
271
272 if (ret)
273 break;
274
db8e0165
JA
275 if (o->maxval && ull > o->maxval) {
276 fprintf(stderr, "max value out of range: %lld (%d max)\n", ull, o->maxval);
277 return 1;
278 }
279 if (o->minval && ull < o->minval) {
280 fprintf(stderr, "min value out of range: %lld (%d min)\n", ull, o->minval);
281 return 1;
282 }
e1f36503
JA
283
284 if (fn)
285 ret = fn(data, &ull);
286 else {
75e6f36f 287 if (o->type == FIO_OPT_STR_VAL_INT) {
17abbe89
JA
288 if (first)
289 val_store(ilp, ull, o->off1, data);
787f7e95 290 if (!more && o->off2)
17abbe89 291 val_store(ilp, ull, o->off2, data);
75e6f36f 292 } else {
17abbe89
JA
293 if (first)
294 val_store(ullp, ull, o->off1, data);
787f7e95 295 if (!more && o->off2)
17abbe89 296 val_store(ullp, ull, o->off2, data);
75e6f36f 297 }
e1f36503
JA
298 }
299 break;
300 }
af52b345
JA
301 case FIO_OPT_STR_STORE: {
302 fio_opt_str_fn *fn = o->cb;
303
e1f36503
JA
304 cp = td_var(data, o->off1);
305 *cp = strdup(ptr);
af52b345
JA
306 if (fn) {
307 ret = fn(data, ptr);
308 if (ret) {
309 free(*cp);
310 *cp = NULL;
311 }
312 }
e1f36503 313 break;
af52b345 314 }
e1f36503 315 case FIO_OPT_RANGE: {
b765a372 316 char tmp[128];
e1f36503
JA
317 char *p1, *p2;
318
0bbab0e7 319 strncpy(tmp, ptr, sizeof(tmp) - 1);
b765a372
JA
320
321 p1 = strchr(tmp, '-');
e1f36503 322 if (!p1) {
0c9baf91
JA
323 p1 = strchr(tmp, ':');
324 if (!p1) {
325 ret = 1;
326 break;
327 }
e1f36503
JA
328 }
329
330 p2 = p1 + 1;
331 *p1 = '\0';
b765a372 332 p1 = tmp;
e1f36503
JA
333
334 ret = 1;
335 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
336 ret = 0;
e1f36503 337 if (ul1 > ul2) {
f90eff5a
JA
338 unsigned long foo = ul1;
339
340 ul1 = ul2;
341 ul2 = foo;
342 }
343
344 if (first) {
17abbe89
JA
345 val_store(ilp, ul1, o->off1, data);
346 val_store(ilp, ul2, o->off2, data);
347 }
787f7e95 348 if (!more && o->off3 && o->off4) {
17abbe89
JA
349 val_store(ilp, ul1, o->off3, data);
350 val_store(ilp, ul2, o->off4, data);
e1f36503 351 }
17abbe89
JA
352 }
353
e1f36503
JA
354 break;
355 }
13335ddb
JA
356 case FIO_OPT_INT:
357 case FIO_OPT_BOOL: {
e1f36503
JA
358 fio_opt_int_fn *fn = o->cb;
359
360 ret = check_int(ptr, &il);
361 if (ret)
362 break;
363
db8e0165
JA
364 if (o->maxval && il > (int) o->maxval) {
365 fprintf(stderr, "max value out of range: %d (%d max)\n", il, o->maxval);
366 return 1;
367 }
368 if (o->minval && il < o->minval) {
369 fprintf(stderr, "min value out of range: %d (%d min)\n", il, o->minval);
370 return 1;
371 }
e1f36503 372
76a43db4
JA
373 if (o->neg)
374 il = !il;
375
e1f36503
JA
376 if (fn)
377 ret = fn(data, &il);
378 else {
17abbe89
JA
379 if (first)
380 val_store(ilp, il, o->off1, data);
787f7e95 381 if (!more && o->off2)
17abbe89 382 val_store(ilp, il, o->off2, data);
e1f36503
JA
383 }
384 break;
385 }
386 case FIO_OPT_STR_SET: {
387 fio_opt_str_set_fn *fn = o->cb;
388
389 if (fn)
390 ret = fn(data);
391 else {
17abbe89
JA
392 if (first)
393 val_store(ilp, 1, o->off1, data);
787f7e95 394 if (!more && o->off2)
17abbe89 395 val_store(ilp, 1, o->off2, data);
e1f36503
JA
396 }
397 break;
398 }
399 default:
1e97cce9 400 fprintf(stderr, "Bad option type %u\n", o->type);
e1f36503
JA
401 ret = 1;
402 }
cb2c86fd 403
e1f36503 404 return ret;
cb2c86fd
JA
405}
406
f90eff5a
JA
407static int handle_option(struct fio_option *o, const char *ptr, void *data)
408{
92b586f8 409 const char *ptr2 = NULL;
787f7e95 410 int r1, r2;
f90eff5a
JA
411
412 /*
787f7e95
JA
413 * See if we have a second set of parameters, hidden after a comma.
414 * Do this before parsing the first round, to check if we should
415 * copy set 1 options to set 2.
f90eff5a 416 */
ad231bc4
JB
417 if (ptr &&
418 (o->type != FIO_OPT_STR_STORE) &&
419 (o->type != FIO_OPT_STR)) {
92b586f8 420 ptr2 = strchr(ptr, ',');
0c9baf91
JA
421 if (!ptr2)
422 ptr2 = strchr(ptr, ':');
b486f76a
JA
423 if (!ptr2)
424 ptr2 = strchr(ptr, '-');
0c9baf91 425 }
787f7e95
JA
426
427 /*
428 * Don't return early if parsing the first option fails - if
429 * we are doing multiple arguments, we can allow the first one
430 * being empty.
431 */
432 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
433
f90eff5a 434 if (!ptr2)
787f7e95 435 return r1;
f90eff5a
JA
436
437 ptr2++;
787f7e95
JA
438 r2 = __handle_option(o, ptr2, data, 0, 0);
439
440 return r1 && r2;
f90eff5a
JA
441}
442
b4692828
JA
443int parse_cmd_option(const char *opt, const char *val,
444 struct fio_option *options, void *data)
445{
446 struct fio_option *o;
447
448 o = find_option(options, opt);
449 if (!o) {
450 fprintf(stderr, "Bad option %s\n", opt);
451 return 1;
452 }
453
b1508cf9
JA
454 if (!handle_option(o, val, data))
455 return 0;
456
457 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
458 return 1;
b4692828
JA
459}
460
e1f36503 461int parse_option(const char *opt, struct fio_option *options, void *data)
cb2c86fd 462{
b4692828 463 struct fio_option *o;
e1f36503 464 char *pre, *post;
0401bca6 465 char *tmp;
e1f36503 466
0401bca6 467 tmp = strdup(opt);
e1f36503
JA
468
469 pre = strchr(tmp, '=');
470 if (pre) {
471 post = pre;
472 *pre = '\0';
473 pre = tmp;
474 post++;
475 o = find_option(options, pre);
476 } else {
477 o = find_option(options, tmp);
478 post = NULL;
479 }
cb2c86fd 480
e1f36503
JA
481 if (!o) {
482 fprintf(stderr, "Bad option %s\n", tmp);
0401bca6 483 free(tmp);
e1f36503
JA
484 return 1;
485 }
486
0401bca6
JA
487 if (!handle_option(o, post, data)) {
488 free(tmp);
b1508cf9 489 return 0;
0401bca6 490 }
b1508cf9
JA
491
492 fprintf(stderr, "fio: failed parsing %s\n", opt);
0401bca6 493 free(tmp);
b1508cf9 494 return 1;
e1f36503 495}
fd28ca49 496
0e9f7fac
JA
497/*
498 * Option match, levenshtein distance. Handy for not quite remembering what
499 * the option name is.
500 */
501static int string_distance(const char *s1, const char *s2)
502{
503 unsigned int s1_len = strlen(s1);
504 unsigned int s2_len = strlen(s2);
505 unsigned int *p, *q, *r;
506 unsigned int i, j;
507
508 p = malloc(sizeof(unsigned int) * (s2_len + 1));
509 q = malloc(sizeof(unsigned int) * (s2_len + 1));
510
511 p[0] = 0;
512 for (i = 1; i <= s2_len; i++)
513 p[i] = p[i - 1] + 1;
514
515 for (i = 1; i <= s1_len; i++) {
516 q[0] = p[0] + 1;
517 for (j = 1; j <= s2_len; j++) {
518 unsigned int sub = p[j - 1];
519
520 if (s1[i - 1] != s2[j - 1])
521 sub++;
522
523 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
524 }
525 r = p;
526 p = q;
527 q = r;
528 }
529
530 i = p[s2_len];
531 free(p);
532 free(q);
533 return i;
534}
535
536static void show_option_help(struct fio_option *o)
fd28ca49 537{
fd28ca49
JA
538 const char *typehelp[] = {
539 "string (opt=bla)",
540 "string with possible k/m/g postfix (opt=4k)",
541 "string with range and postfix (opt=1k-4k)",
542 "string with time postfix (opt=10s)",
543 "string (opt=bla)",
544 "string with dual range (opt=1k-4k,4k-8k)",
545 "integer value (opt=100)",
13335ddb 546 "boolean value (opt=1)",
fd28ca49
JA
547 "no argument (opt)",
548 };
0e9f7fac 549
d2bb7fea
JA
550 if (o->alias)
551 printf("%20s: %s\n", "alias", o->alias);
552
0e9f7fac
JA
553 printf("%20s: %s\n", "type", typehelp[o->type]);
554 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
555 show_option_range(o);
556 show_option_values(o);
557}
558
559int show_cmd_help(struct fio_option *options, const char *name)
560{
561 struct fio_option *o, *closest;
562 unsigned int best_dist;
29fc6afe 563 int found = 0;
320beefe
JA
564 int show_all = 0;
565
566 if (!name || !strcmp(name, "all"))
567 show_all = 1;
fd28ca49 568
0e9f7fac
JA
569 closest = NULL;
570 best_dist = -1;
4945ba12 571 for (o = &options[0]; o->name; o++) {
320beefe
JA
572 int match = 0;
573
0e9f7fac 574 if (name) {
7f9348f8
JA
575 if (!strcmp(name, o->name) ||
576 (o->alias && !strcmp(name, o->alias)))
0e9f7fac
JA
577 match = 1;
578 else {
579 unsigned int dist;
580
581 dist = string_distance(name, o->name);
582 if (dist < best_dist) {
583 best_dist = dist;
584 closest = o;
585 }
586 }
587 }
fd28ca49
JA
588
589 if (show_all || match) {
29fc6afe 590 found = 1;
c167dedc
JA
591 if (match)
592 printf("%20s: %s\n", o->name, o->help);
593 if (show_all) {
594 printf("%-20s: %s\n", o->name, o->help);
4945ba12 595 continue;
c167dedc 596 }
fd28ca49
JA
597 }
598
70df2f19
JA
599 if (!match)
600 continue;
601
0e9f7fac 602 show_option_help(o);
fd28ca49
JA
603 }
604
29fc6afe
JA
605 if (found)
606 return 0;
fd28ca49 607
0e9f7fac
JA
608 printf("No such command: %s", name);
609 if (closest) {
610 printf(" - showing closest match\n");
611 printf("%20s: %s\n", closest->name, closest->help);
612 show_option_help(closest);
613 } else
614 printf("\n");
615
29fc6afe 616 return 1;
fd28ca49 617}
ee738499 618
13335ddb
JA
619/*
620 * Handle parsing of default parameters.
621 */
ee738499
JA
622void fill_default_options(void *data, struct fio_option *options)
623{
4945ba12 624 struct fio_option *o;
ee738499 625
4945ba12 626 for (o = &options[0]; o->name; o++)
ee738499
JA
627 if (o->def)
628 handle_option(o, o->def, data);
ee738499 629}
13335ddb
JA
630
631/*
632 * Sanitize the options structure. For now it just sets min/max for bool
5b0a8880 633 * values and whether both callback and offsets are given.
13335ddb
JA
634 */
635void options_init(struct fio_option *options)
636{
4945ba12 637 struct fio_option *o;
13335ddb 638
4945ba12 639 for (o = &options[0]; o->name; o++) {
13335ddb
JA
640 if (o->type == FIO_OPT_BOOL) {
641 o->minval = 0;
642 o->maxval = 1;
643 }
0f3e35ef
JA
644 if (o->type == FIO_OPT_STR_SET && o->def)
645 fprintf(stderr, "Option %s: string set option with default will always be true\n", o->name);
b1ec1da6
JA
646 if (!o->cb && !o->off1)
647 fprintf(stderr, "Option %s: neither cb nor offset given\n", o->name);
af52b345 648 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
b1ec1da6 649 continue;
5b0a8880
JA
650 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
651 fprintf(stderr, "Option %s: both cb and offset given\n", o->name);
13335ddb
JA
652 }
653}