Improve latency_target runs
[fio.git] / parse.c
... / ...
CommitLineData
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#include <stdlib.h>
12#include <math.h>
13#include <float.h>
14
15#include "parse.h"
16#include "debug.h"
17#include "options.h"
18#include "minmax.h"
19#include "lib/ieee754.h"
20
21static struct fio_option *__fio_options;
22
23static int vp_cmp(const void *p1, const void *p2)
24{
25 const struct value_pair *vp1 = p1;
26 const struct value_pair *vp2 = p2;
27
28 return strlen(vp2->ival) - strlen(vp1->ival);
29}
30
31static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
32{
33 const struct value_pair *vp;
34 int entries;
35
36 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
37
38 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
39 vp = &o->posval[entries];
40 if (!vp->ival || vp->ival[0] == '\0')
41 break;
42
43 memcpy(&vpmap[entries], vp, sizeof(*vp));
44 }
45
46 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
47}
48
49static void show_option_range(struct fio_option *o,
50 int (*logger)(const char *format, ...))
51{
52 if (o->type == FIO_OPT_FLOAT_LIST) {
53 if (o->minfp == DBL_MIN && o->maxfp == DBL_MAX)
54 return;
55
56 logger("%20s: min=%f", "range", o->minfp);
57 if (o->maxfp != DBL_MAX)
58 logger(", max=%f", o->maxfp);
59 logger("\n");
60 } else if (!o->posval[0].ival) {
61 if (!o->minval && !o->maxval)
62 return;
63
64 logger("%20s: min=%d", "range", o->minval);
65 if (o->maxval)
66 logger(", max=%d", o->maxval);
67 logger("\n");
68 }
69}
70
71static void show_option_values(struct fio_option *o)
72{
73 int i;
74
75 for (i = 0; i < PARSE_MAX_VP; i++) {
76 const struct value_pair *vp = &o->posval[i];
77
78 if (!vp->ival)
79 continue;
80
81 log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
82 if (vp->help)
83 log_info(" %s", vp->help);
84 log_info("\n");
85 }
86
87 if (i)
88 log_info("\n");
89}
90
91static void show_option_help(struct fio_option *o, int is_err)
92{
93 const char *typehelp[] = {
94 "invalid",
95 "string (opt=bla)",
96 "string (opt=bla)",
97 "string with possible k/m/g postfix (opt=4k)",
98 "string with time postfix (opt=10s)",
99 "string (opt=bla)",
100 "string with dual range (opt=1k-4k,4k-8k)",
101 "integer value (opt=100)",
102 "boolean value (opt=1)",
103 "list of floating point values separated by ':' (opt=5.9:7.8)",
104 "no argument (opt)",
105 "deprecated",
106 };
107 int (*logger)(const char *format, ...);
108
109 if (is_err)
110 logger = log_err;
111 else
112 logger = log_info;
113
114 if (o->alias)
115 logger("%20s: %s\n", "alias", o->alias);
116
117 logger("%20s: %s\n", "type", typehelp[o->type]);
118 logger("%20s: %s\n", "default", o->def ? o->def : "no default");
119 if (o->prof_name)
120 logger("%20s: only for profile '%s'\n", "valid", o->prof_name);
121 show_option_range(o, logger);
122 show_option_values(o);
123}
124
125static unsigned long long get_mult_time(const char *str, int len)
126{
127 const char *p = str;
128 char *c;
129 unsigned long long mult = 1;
130
131 /*
132 * Go forward until we hit a non-digit, or +/- sign
133 */
134 while ((p - str) <= len) {
135 if (!isdigit((int) *p) && (*p != '+') && (*p != '-'))
136 break;
137 p++;
138 }
139
140 if (!isalpha((int) *p))
141 return mult;
142
143 c = strdup(p);
144 for (int i = 0; i < strlen(c); i++)
145 c[i] = tolower(c[i]);
146
147 if (!strncmp("us", c, 2) || !strncmp("usec", c, 4))
148 mult = 1;
149 else if (!strncmp("ms", c, 2) || !strncmp("msec", c, 4))
150 mult = 1000;
151 else if (!strcmp("s", c))
152 mult = 1000000;
153 else if (!strcmp("m", c))
154 mult = 60 * 1000000UL;
155 else if (!strcmp("h", c))
156 mult = 60 * 60 * 1000000UL;
157 else if (!strcmp("d", c))
158 mult = 24 * 60 * 60 * 1000000UL;
159
160 free(c);
161 return mult;
162}
163
164static int is_separator(char c)
165{
166 switch (c) {
167 case ':':
168 case '-':
169 case ',':
170 case '/':
171 return 1;
172 default:
173 return 0;
174 }
175}
176
177static unsigned long long __get_mult_bytes(const char *p, void *data,
178 int *percent)
179{
180 unsigned int kb_base = fio_get_kb_base(data);
181 unsigned long long ret = 1;
182 unsigned int i, pow = 0, mult = kb_base;
183 char *c;
184
185 if (!p)
186 return 1;
187
188 c = strdup(p);
189
190 for (i = 0; i < strlen(c); i++) {
191 c[i] = tolower(c[i]);
192 if (is_separator(c[i])) {
193 c[i] = '\0';
194 break;
195 }
196 }
197
198 if (!strncmp("pib", c, 3)) {
199 pow = 5;
200 mult = 1000;
201 } else if (!strncmp("tib", c, 3)) {
202 pow = 4;
203 mult = 1000;
204 } else if (!strncmp("gib", c, 3)) {
205 pow = 3;
206 mult = 1000;
207 } else if (!strncmp("mib", c, 3)) {
208 pow = 2;
209 mult = 1000;
210 } else if (!strncmp("kib", c, 3)) {
211 pow = 1;
212 mult = 1000;
213 } else if (!strncmp("p", c, 1) || !strncmp("pb", c, 2))
214 pow = 5;
215 else if (!strncmp("t", c, 1) || !strncmp("tb", c, 2))
216 pow = 4;
217 else if (!strncmp("g", c, 1) || !strncmp("gb", c, 2))
218 pow = 3;
219 else if (!strncmp("m", c, 1) || !strncmp("mb", c, 2))
220 pow = 2;
221 else if (!strncmp("k", c, 1) || !strncmp("kb", c, 2))
222 pow = 1;
223 else if (!strncmp("%", c, 1)) {
224 *percent = 1;
225 free(c);
226 return ret;
227 }
228
229 while (pow--)
230 ret *= (unsigned long long) mult;
231
232 free(c);
233 return ret;
234}
235
236static unsigned long long get_mult_bytes(const char *str, int len, void *data,
237 int *percent)
238{
239 const char *p = str;
240 int digit_seen = 0;
241
242 if (len < 2)
243 return __get_mult_bytes(str, data, percent);
244
245 /*
246 * Go forward until we hit a non-digit, or +/- sign
247 */
248 while ((p - str) <= len) {
249 if (!isdigit((int) *p) &&
250 (((*p != '+') && (*p != '-')) || digit_seen))
251 break;
252 digit_seen |= isdigit((int) *p);
253 p++;
254 }
255
256 if (!isalpha((int) *p) && (*p != '%'))
257 p = NULL;
258
259 return __get_mult_bytes(p, data, percent);
260}
261
262/*
263 * Convert string into a floating number. Return 1 for success and 0 otherwise.
264 */
265int str_to_float(const char *str, double *val)
266{
267 return (1 == sscanf(str, "%lf", val));
268}
269
270/*
271 * convert string into decimal value, noting any size suffix
272 */
273int str_to_decimal(const char *str, long long *val, int kilo, void *data)
274{
275 int len, base;
276
277 len = strlen(str);
278 if (!len)
279 return 1;
280
281 if (strstr(str, "0x") || strstr(str, "0X"))
282 base = 16;
283 else
284 base = 10;
285
286 *val = strtoll(str, NULL, base);
287 if (*val == LONG_MAX && errno == ERANGE)
288 return 1;
289
290 if (kilo) {
291 unsigned long long mult;
292 int perc = 0;
293
294 mult = get_mult_bytes(str, len, data, &perc);
295 if (perc)
296 *val = -1ULL - *val;
297 else
298 *val *= mult;
299 } else
300 *val *= get_mult_time(str, len);
301
302 return 0;
303}
304
305int check_str_bytes(const char *p, long long *val, void *data)
306{
307 return str_to_decimal(p, val, 1, data);
308}
309
310int check_str_time(const char *p, long long *val)
311{
312 return str_to_decimal(p, val, 0, NULL);
313}
314
315void strip_blank_front(char **p)
316{
317 char *s = *p;
318
319 if (!strlen(s))
320 return;
321 while (isspace((int) *s))
322 s++;
323
324 *p = s;
325}
326
327void strip_blank_end(char *p)
328{
329 char *start = p, *s;
330
331 if (!strlen(p))
332 return;
333
334 s = strchr(p, ';');
335 if (s)
336 *s = '\0';
337 s = strchr(p, '#');
338 if (s)
339 *s = '\0';
340 if (s)
341 p = s;
342
343 s = p + strlen(p);
344 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
345 s--;
346
347 *(s + 1) = '\0';
348}
349
350static int check_range_bytes(const char *str, long *val, void *data)
351{
352 long long __val;
353
354 if (!str_to_decimal(str, &__val, 1, data)) {
355 *val = __val;
356 return 0;
357 }
358
359 return 1;
360}
361
362static int check_int(const char *p, int *val)
363{
364 if (!strlen(p))
365 return 1;
366 if (strstr(p, "0x") || strstr(p, "0X")) {
367 if (sscanf(p, "%x", val) == 1)
368 return 0;
369 } else {
370 if (sscanf(p, "%u", val) == 1)
371 return 0;
372 }
373
374 return 1;
375}
376
377static int opt_len(const char *str)
378{
379 char *postfix;
380
381 postfix = strchr(str, ':');
382 if (!postfix)
383 return strlen(str);
384
385 return (int)(postfix - str);
386}
387
388static int str_match_len(const struct value_pair *vp, const char *str)
389{
390 return max(strlen(vp->ival), opt_len(str));
391}
392
393#define val_store(ptr, val, off, or, data, o) \
394 do { \
395 ptr = td_var((data), (o), (off)); \
396 if ((or)) \
397 *ptr |= (val); \
398 else \
399 *ptr = (val); \
400 } while (0)
401
402static int __handle_option(struct fio_option *o, const char *ptr, void *data,
403 int first, int more, int curr)
404{
405 int il=0, *ilp;
406 fio_fp64_t *flp;
407 long long ull, *ullp;
408 long ul1, ul2;
409 double uf;
410 char **cp = NULL;
411 int ret = 0, is_time = 0;
412 const struct value_pair *vp;
413 struct value_pair posval[PARSE_MAX_VP];
414 int i, all_skipped = 1;
415
416 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
417 o->type, ptr);
418
419 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
420 log_err("Option %s requires an argument\n", o->name);
421 return 1;
422 }
423
424 switch (o->type) {
425 case FIO_OPT_STR:
426 case FIO_OPT_STR_MULTI: {
427 fio_opt_str_fn *fn = o->cb;
428
429 posval_sort(o, posval);
430
431 ret = 1;
432 for (i = 0; i < PARSE_MAX_VP; i++) {
433 vp = &posval[i];
434 if (!vp->ival || vp->ival[0] == '\0')
435 continue;
436 all_skipped = 0;
437 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
438 ret = 0;
439 if (o->off1)
440 val_store(ilp, vp->oval, o->off1, vp->orval, data, o);
441 continue;
442 }
443 }
444
445 if (ret && !all_skipped)
446 show_option_values(o);
447 else if (fn)
448 ret = fn(data, ptr);
449 break;
450 }
451 case FIO_OPT_STR_VAL_TIME:
452 is_time = 1;
453 case FIO_OPT_INT:
454 case FIO_OPT_STR_VAL: {
455 fio_opt_str_val_fn *fn = o->cb;
456 char tmp[128], *p;
457
458 strncpy(tmp, ptr, sizeof(tmp) - 1);
459 p = strchr(tmp, ',');
460 if (p)
461 *p = '\0';
462
463 if (is_time)
464 ret = check_str_time(tmp, &ull);
465 else
466 ret = check_str_bytes(tmp, &ull, data);
467
468 dprint(FD_PARSE, " ret=%d, out=%llu\n", ret, ull);
469
470 if (ret)
471 break;
472
473 if (o->maxval && ull > o->maxval) {
474 log_err("max value out of range: %llu"
475 " (%u max)\n", ull, o->maxval);
476 return 1;
477 }
478 if (o->minval && ull < o->minval) {
479 log_err("min value out of range: %llu"
480 " (%u min)\n", ull, o->minval);
481 return 1;
482 }
483 if (o->posval[0].ival) {
484 posval_sort(o, posval);
485
486 ret = 1;
487 for (i = 0; i < PARSE_MAX_VP; i++) {
488 vp = &posval[i];
489 if (!vp->ival || vp->ival[0] == '\0')
490 continue;
491 if (vp->oval == ull) {
492 ret = 0;
493 break;
494 }
495 }
496 if (ret) {
497 log_err("fio: value %llu not allowed:\n", ull);
498 show_option_values(o);
499 return 1;
500 }
501 }
502
503 if (fn)
504 ret = fn(data, &ull);
505 else {
506 if (o->type == FIO_OPT_INT) {
507 if (first)
508 val_store(ilp, ull, o->off1, 0, data, o);
509 if (curr == 1) {
510 if (o->off2)
511 val_store(ilp, ull, o->off2, 0, data, o);
512 }
513 if (curr == 2) {
514 if (o->off3)
515 val_store(ilp, ull, o->off3, 0, data, o);
516 }
517 if (!more) {
518 if (curr < 1) {
519 if (o->off2)
520 val_store(ilp, ull, o->off2, 0, data, o);
521 }
522 if (curr < 2) {
523 if (o->off3)
524 val_store(ilp, ull, o->off3, 0, data, o);
525 }
526 }
527 } else {
528 if (first)
529 val_store(ullp, ull, o->off1, 0, data, o);
530 if (!more) {
531 if (o->off2)
532 val_store(ullp, ull, o->off2, 0, data, o);
533 }
534 }
535 }
536 break;
537 }
538 case FIO_OPT_FLOAT_LIST: {
539 char *cp2;
540
541 if (first) {
542 /*
543 ** Initialize precision to 0 and zero out list
544 ** in case specified list is shorter than default
545 */
546 if (o->off2) {
547 ul2 = 0;
548 ilp = td_var(data, o, o->off2);
549 *ilp = ul2;
550 }
551
552 flp = td_var(data, o, o->off1);
553 for(i = 0; i < o->maxlen; i++)
554 flp[i].u.f = 0.0;
555 }
556 if (curr >= o->maxlen) {
557 log_err("the list exceeding max length %d\n",
558 o->maxlen);
559 return 1;
560 }
561 if (!str_to_float(ptr, &uf)) {
562 log_err("not a floating point value: %s\n", ptr);
563 return 1;
564 }
565 if (uf > o->maxfp) {
566 log_err("value out of range: %f"
567 " (range max: %f)\n", uf, o->maxfp);
568 return 1;
569 }
570 if (uf < o->minfp) {
571 log_err("value out of range: %f"
572 " (range min: %f)\n", uf, o->minfp);
573 return 1;
574 }
575
576 flp = td_var(data, o, o->off1);
577 flp[curr].u.f = uf;
578
579 dprint(FD_PARSE, " out=%f\n", uf);
580
581 /*
582 ** Calculate precision for output by counting
583 ** number of digits after period. Find first
584 ** period in entire remaining list each time
585 */
586 cp2 = strchr(ptr, '.');
587 if (cp2 != NULL) {
588 int len = 0;
589
590 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
591 len++;
592
593 if (o->off2) {
594 ilp = td_var(data, o, o->off2);
595 if (len > *ilp)
596 *ilp = len;
597 }
598 }
599
600 break;
601 }
602 case FIO_OPT_STR_STORE: {
603 fio_opt_str_fn *fn = o->cb;
604
605 if (o->off1) {
606 cp = td_var(data, o, o->off1);
607 *cp = strdup(ptr);
608 }
609
610 if (fn)
611 ret = fn(data, ptr);
612 else if (o->posval[0].ival) {
613 posval_sort(o, posval);
614
615 ret = 1;
616 for (i = 0; i < PARSE_MAX_VP; i++) {
617 vp = &posval[i];
618 if (!vp->ival || vp->ival[0] == '\0')
619 continue;
620 all_skipped = 0;
621 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
622 char *rest;
623
624 ret = 0;
625 if (vp->cb)
626 fn = vp->cb;
627 rest = strstr(*cp ?: ptr, ":");
628 if (rest) {
629 if (*cp)
630 *rest = '\0';
631 ptr = rest + 1;
632 } else
633 ptr = NULL;
634 break;
635 }
636 }
637 }
638
639 if (!all_skipped) {
640 if (ret && !*cp)
641 show_option_values(o);
642 else if (ret && *cp)
643 ret = 0;
644 else if (fn && ptr)
645 ret = fn(data, ptr);
646 }
647
648 break;
649 }
650 case FIO_OPT_RANGE: {
651 char tmp[128];
652 char *p1, *p2;
653
654 strncpy(tmp, ptr, sizeof(tmp) - 1);
655
656 /* Handle bsrange with separate read,write values: */
657 p1 = strchr(tmp, ',');
658 if (p1)
659 *p1 = '\0';
660
661 p1 = strchr(tmp, '-');
662 if (!p1) {
663 p1 = strchr(tmp, ':');
664 if (!p1) {
665 ret = 1;
666 break;
667 }
668 }
669
670 p2 = p1 + 1;
671 *p1 = '\0';
672 p1 = tmp;
673
674 ret = 1;
675 if (!check_range_bytes(p1, &ul1, data) &&
676 !check_range_bytes(p2, &ul2, data)) {
677 ret = 0;
678 if (ul1 > ul2) {
679 unsigned long foo = ul1;
680
681 ul1 = ul2;
682 ul2 = foo;
683 }
684
685 if (first) {
686 val_store(ilp, ul1, o->off1, 0, data, o);
687 val_store(ilp, ul2, o->off2, 0, data, o);
688 }
689 if (curr == 1) {
690 if (o->off3 && o->off4) {
691 val_store(ilp, ul1, o->off3, 0, data, o);
692 val_store(ilp, ul2, o->off4, 0, data, o);
693 }
694 }
695 if (curr == 2) {
696 if (o->off5 && o->off6) {
697 val_store(ilp, ul1, o->off5, 0, data, o);
698 val_store(ilp, ul2, o->off6, 0, data, o);
699 }
700 }
701 if (!more) {
702 if (curr < 1) {
703 if (o->off3 && o->off4) {
704 val_store(ilp, ul1, o->off3, 0, data, o);
705 val_store(ilp, ul2, o->off4, 0, data, o);
706 }
707 }
708 if (curr < 2) {
709 if (o->off5 && o->off6) {
710 val_store(ilp, ul1, o->off5, 0, data, o);
711 val_store(ilp, ul2, o->off6, 0, data, o);
712 }
713 }
714 }
715 }
716
717 break;
718 }
719 case FIO_OPT_BOOL:
720 case FIO_OPT_STR_SET: {
721 fio_opt_int_fn *fn = o->cb;
722
723 if (ptr)
724 ret = check_int(ptr, &il);
725 else if (o->type == FIO_OPT_BOOL)
726 ret = 1;
727 else
728 il = 1;
729
730 dprint(FD_PARSE, " ret=%d, out=%d\n", ret, il);
731
732 if (ret)
733 break;
734
735 if (o->maxval && il > (int) o->maxval) {
736 log_err("max value out of range: %d (%d max)\n",
737 il, o->maxval);
738 return 1;
739 }
740 if (o->minval && il < o->minval) {
741 log_err("min value out of range: %d (%d min)\n",
742 il, o->minval);
743 return 1;
744 }
745
746 if (o->neg)
747 il = !il;
748
749 if (fn)
750 ret = fn(data, &il);
751 else {
752 if (first)
753 val_store(ilp, il, o->off1, 0, data, o);
754 if (!more) {
755 if (o->off2)
756 val_store(ilp, il, o->off2, 0, data, o);
757 }
758 }
759 break;
760 }
761 case FIO_OPT_DEPRECATED:
762 log_info("Option %s is deprecated\n", o->name);
763 ret = 1;
764 break;
765 default:
766 log_err("Bad option type %u\n", o->type);
767 ret = 1;
768 }
769
770 if (ret)
771 return ret;
772
773 if (o->verify) {
774 ret = o->verify(o, data);
775 if (ret) {
776 log_err("Correct format for offending option\n");
777 log_err("%20s: %s\n", o->name, o->help);
778 show_option_help(o, 1);
779 }
780 }
781
782 return ret;
783}
784
785static int handle_option(struct fio_option *o, const char *__ptr, void *data)
786{
787 char *o_ptr, *ptr, *ptr2;
788 int ret, done;
789
790 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
791
792 o_ptr = ptr = NULL;
793 if (__ptr)
794 o_ptr = ptr = strdup(__ptr);
795
796 /*
797 * See if we have another set of parameters, hidden after a comma.
798 * Do this before parsing this round, to check if we should
799 * copy set 1 options to set 2.
800 */
801 done = 0;
802 ret = 1;
803 do {
804 int __ret;
805
806 ptr2 = NULL;
807 if (ptr &&
808 (o->type != FIO_OPT_STR_STORE) &&
809 (o->type != FIO_OPT_STR) &&
810 (o->type != FIO_OPT_FLOAT_LIST)) {
811 ptr2 = strchr(ptr, ',');
812 if (ptr2 && *(ptr2 + 1) == '\0')
813 *ptr2 = '\0';
814 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
815 if (!ptr2)
816 ptr2 = strchr(ptr, ':');
817 if (!ptr2)
818 ptr2 = strchr(ptr, '-');
819 }
820 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
821 ptr2 = strchr(ptr, ':');
822 }
823
824 /*
825 * Don't return early if parsing the first option fails - if
826 * we are doing multiple arguments, we can allow the first one
827 * being empty.
828 */
829 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
830 if (ret)
831 ret = __ret;
832
833 if (!ptr2)
834 break;
835
836 ptr = ptr2 + 1;
837 done++;
838 } while (1);
839
840 if (o_ptr)
841 free(o_ptr);
842 return ret;
843}
844
845static struct fio_option *get_option(char *opt,
846 struct fio_option *options, char **post)
847{
848 struct fio_option *o;
849 char *ret;
850
851 ret = strchr(opt, '=');
852 if (ret) {
853 *post = ret;
854 *ret = '\0';
855 ret = opt;
856 (*post)++;
857 strip_blank_end(ret);
858 o = find_option(options, ret);
859 } else {
860 o = find_option(options, opt);
861 *post = NULL;
862 }
863
864 return o;
865}
866
867static int opt_cmp(const void *p1, const void *p2)
868{
869 struct fio_option *o;
870 char *s, *foo;
871 int prio1, prio2;
872
873 prio1 = prio2 = 0;
874
875 if (*(char **)p1) {
876 s = strdup(*((char **) p1));
877 o = get_option(s, __fio_options, &foo);
878 if (o)
879 prio1 = o->prio;
880 free(s);
881 }
882 if (*(char **)p2) {
883 s = strdup(*((char **) p2));
884 o = get_option(s, __fio_options, &foo);
885 if (o)
886 prio2 = o->prio;
887 free(s);
888 }
889
890 return prio2 - prio1;
891}
892
893void sort_options(char **opts, struct fio_option *options, int num_opts)
894{
895 __fio_options = options;
896 qsort(opts, num_opts, sizeof(char *), opt_cmp);
897 __fio_options = NULL;
898}
899
900int parse_cmd_option(const char *opt, const char *val,
901 struct fio_option *options, void *data)
902{
903 struct fio_option *o;
904
905 o = find_option(options, opt);
906 if (!o) {
907 log_err("Bad option <%s>\n", opt);
908 return 1;
909 }
910
911 if (!handle_option(o, val, data))
912 return 0;
913
914 log_err("fio: failed parsing %s=%s\n", opt, val);
915 return 1;
916}
917
918int parse_option(char *opt, const char *input,
919 struct fio_option *options, struct fio_option **o, void *data,
920 int dump_cmdline)
921{
922 char *post;
923
924 if (!opt) {
925 log_err("fio: failed parsing %s\n", input);
926 *o = NULL;
927 return 1;
928 }
929
930 *o = get_option(opt, options, &post);
931 if (!*o) {
932 if (post) {
933 int len = strlen(opt);
934 if (opt + len + 1 != post)
935 memmove(opt + len + 1, post, strlen(post));
936 opt[len] = '=';
937 }
938 return 1;
939 }
940
941 if (handle_option(*o, post, data)) {
942 log_err("fio: failed parsing %s\n", input);
943 return 1;
944 }
945
946 if (dump_cmdline) {
947 const char *delim;
948
949 if (!strcmp("description", (*o)->name))
950 delim = "\"";
951 else
952 delim = "";
953
954 log_info("--%s%s", (*o)->name, post ? "" : " ");
955 if (post)
956 log_info("=%s%s%s ", delim, post, delim);
957 }
958
959 return 0;
960}
961
962/*
963 * Option match, levenshtein distance. Handy for not quite remembering what
964 * the option name is.
965 */
966static int string_distance(const char *s1, const char *s2)
967{
968 unsigned int s1_len = strlen(s1);
969 unsigned int s2_len = strlen(s2);
970 unsigned int *p, *q, *r;
971 unsigned int i, j;
972
973 p = malloc(sizeof(unsigned int) * (s2_len + 1));
974 q = malloc(sizeof(unsigned int) * (s2_len + 1));
975
976 p[0] = 0;
977 for (i = 1; i <= s2_len; i++)
978 p[i] = p[i - 1] + 1;
979
980 for (i = 1; i <= s1_len; i++) {
981 q[0] = p[0] + 1;
982 for (j = 1; j <= s2_len; j++) {
983 unsigned int sub = p[j - 1];
984
985 if (s1[i - 1] != s2[j - 1])
986 sub++;
987
988 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
989 }
990 r = p;
991 p = q;
992 q = r;
993 }
994
995 i = p[s2_len];
996 free(p);
997 free(q);
998 return i;
999}
1000
1001static struct fio_option *find_child(struct fio_option *options,
1002 struct fio_option *o)
1003{
1004 struct fio_option *__o;
1005
1006 for (__o = options + 1; __o->name; __o++)
1007 if (__o->parent && !strcmp(__o->parent, o->name))
1008 return __o;
1009
1010 return NULL;
1011}
1012
1013static void __print_option(struct fio_option *o, struct fio_option *org,
1014 int level)
1015{
1016 char name[256], *p;
1017 int depth;
1018
1019 if (!o)
1020 return;
1021 if (!org)
1022 org = o;
1023
1024 p = name;
1025 depth = level;
1026 while (depth--)
1027 p += sprintf(p, "%s", " ");
1028
1029 sprintf(p, "%s", o->name);
1030
1031 log_info("%-24s: %s\n", name, o->help);
1032}
1033
1034static void print_option(struct fio_option *o)
1035{
1036 struct fio_option *parent;
1037 struct fio_option *__o;
1038 unsigned int printed;
1039 unsigned int level;
1040
1041 __print_option(o, NULL, 0);
1042 parent = o;
1043 level = 0;
1044 do {
1045 level++;
1046 printed = 0;
1047
1048 while ((__o = find_child(o, parent)) != NULL) {
1049 __print_option(__o, o, level);
1050 o = __o;
1051 printed++;
1052 }
1053
1054 parent = o;
1055 } while (printed);
1056}
1057
1058int show_cmd_help(struct fio_option *options, const char *name)
1059{
1060 struct fio_option *o, *closest;
1061 unsigned int best_dist = -1U;
1062 int found = 0;
1063 int show_all = 0;
1064
1065 if (!name || !strcmp(name, "all"))
1066 show_all = 1;
1067
1068 closest = NULL;
1069 best_dist = -1;
1070 for (o = &options[0]; o->name; o++) {
1071 int match = 0;
1072
1073 if (o->type == FIO_OPT_DEPRECATED)
1074 continue;
1075 if (!exec_profile && o->prof_name)
1076 continue;
1077 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1078 continue;
1079
1080 if (name) {
1081 if (!strcmp(name, o->name) ||
1082 (o->alias && !strcmp(name, o->alias)))
1083 match = 1;
1084 else {
1085 unsigned int dist;
1086
1087 dist = string_distance(name, o->name);
1088 if (dist < best_dist) {
1089 best_dist = dist;
1090 closest = o;
1091 }
1092 }
1093 }
1094
1095 if (show_all || match) {
1096 found = 1;
1097 if (match)
1098 log_info("%20s: %s\n", o->name, o->help);
1099 if (show_all) {
1100 if (!o->parent)
1101 print_option(o);
1102 continue;
1103 }
1104 }
1105
1106 if (!match)
1107 continue;
1108
1109 show_option_help(o, 0);
1110 }
1111
1112 if (found)
1113 return 0;
1114
1115 log_err("No such command: %s", name);
1116
1117 /*
1118 * Only print an appropriately close option, one where the edit
1119 * distance isn't too big. Otherwise we get crazy matches.
1120 */
1121 if (closest && best_dist < 3) {
1122 log_info(" - showing closest match\n");
1123 log_info("%20s: %s\n", closest->name, closest->help);
1124 show_option_help(closest, 0);
1125 } else
1126 log_info("\n");
1127
1128 return 1;
1129}
1130
1131/*
1132 * Handle parsing of default parameters.
1133 */
1134void fill_default_options(void *data, struct fio_option *options)
1135{
1136 struct fio_option *o;
1137
1138 dprint(FD_PARSE, "filling default options\n");
1139
1140 for (o = &options[0]; o->name; o++)
1141 if (o->def)
1142 handle_option(o, o->def, data);
1143}
1144
1145void option_init(struct fio_option *o)
1146{
1147 if (o->type == FIO_OPT_DEPRECATED)
1148 return;
1149 if (o->type == FIO_OPT_BOOL) {
1150 o->minval = 0;
1151 o->maxval = 1;
1152 }
1153 if (o->type == FIO_OPT_INT) {
1154 if (!o->maxval)
1155 o->maxval = UINT_MAX;
1156 }
1157 if (o->type == FIO_OPT_FLOAT_LIST) {
1158 o->minfp = DBL_MIN;
1159 o->maxfp = DBL_MAX;
1160 }
1161 if (o->type == FIO_OPT_STR_SET && o->def) {
1162 log_err("Option %s: string set option with"
1163 " default will always be true\n", o->name);
1164 }
1165 if (!o->cb && !o->off1)
1166 log_err("Option %s: neither cb nor offset given\n", o->name);
1167 if (!o->category) {
1168 log_info("Option %s: no category defined. Setting to misc\n", o->name);
1169 o->category = FIO_OPT_C_GENERAL;
1170 o->group = FIO_OPT_G_INVALID;
1171 }
1172 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1173 o->type == FIO_OPT_STR_MULTI)
1174 return;
1175 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
1176 log_err("Option %s: both cb and offset given\n", o->name);
1177}
1178
1179/*
1180 * Sanitize the options structure. For now it just sets min/max for bool
1181 * values and whether both callback and offsets are given.
1182 */
1183void options_init(struct fio_option *options)
1184{
1185 struct fio_option *o;
1186
1187 dprint(FD_PARSE, "init options\n");
1188
1189 for (o = &options[0]; o->name; o++) {
1190 option_init(o);
1191 if (o->inverse)
1192 o->inv_opt = find_option(options, o->inverse);
1193 }
1194}
1195
1196void options_free(struct fio_option *options, void *data)
1197{
1198 struct fio_option *o;
1199 char **ptr;
1200
1201 dprint(FD_PARSE, "free options\n");
1202
1203 for (o = &options[0]; o->name; o++) {
1204 if (o->type != FIO_OPT_STR_STORE || !o->off1)
1205 continue;
1206
1207 ptr = td_var(data, o, o->off1);
1208 if (*ptr) {
1209 free(*ptr);
1210 *ptr = NULL;
1211 }
1212 }
1213}