2 * This file contains the ini and command liner parser main.
19 static struct fio_option *__fio_options;
21 static int vp_cmp(const void *p1, const void *p2)
23 const struct value_pair *vp1 = p1;
24 const struct value_pair *vp2 = p2;
26 return strlen(vp2->ival) - strlen(vp1->ival);
29 static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
31 const struct value_pair *vp;
34 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
36 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
37 vp = &o->posval[entries];
38 if (!vp->ival || vp->ival[0] == '\0')
41 memcpy(&vpmap[entries], vp, sizeof(*vp));
44 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
47 static void show_option_range(struct fio_option *o,
48 int (*logger)(const char *format, ...))
50 if (o->type == FIO_OPT_FLOAT_LIST) {
51 if (isnan(o->minfp) && isnan(o->maxfp))
54 logger("%20s: min=%f", "range", o->minfp);
56 logger(", max=%f", o->maxfp);
59 if (!o->minval && !o->maxval)
62 logger("%20s: min=%d", "range", o->minval);
64 logger(", max=%d", o->maxval);
69 static void show_option_values(struct fio_option *o)
73 for (i = 0; i < PARSE_MAX_VP; i++) {
74 const struct value_pair *vp = &o->posval[i];
79 log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
81 log_info(" %s", vp->help);
89 static void show_option_help(struct fio_option *o, int is_err)
91 const char *typehelp[] = {
95 "string with possible k/m/g postfix (opt=4k)",
96 "string with time postfix (opt=10s)",
98 "string with dual range (opt=1k-4k,4k-8k)",
99 "integer value (opt=100)",
100 "boolean value (opt=1)",
101 "list of floating point values separated by ':' (opt=5.9:7.8)",
105 int (*logger)(const char *format, ...);
113 logger("%20s: %s\n", "alias", o->alias);
115 logger("%20s: %s\n", "type", typehelp[o->type]);
116 logger("%20s: %s\n", "default", o->def ? o->def : "no default");
118 logger("%20s: only for profile '%s'\n", "valid", o->prof_name);
119 show_option_range(o, logger);
120 show_option_values(o);
123 static unsigned long get_mult_time(char c)
140 static unsigned long long __get_mult_bytes(const char *p, void *data,
143 unsigned int kb_base = fio_get_kb_base(data);
144 unsigned long long ret = 1;
145 unsigned int i, pow = 0, mult = kb_base;
153 for (i = 0; i < strlen(c); i++)
154 c[i] = tolower(c[i]);
156 if (!strncmp("pib", c, 3)) {
159 } else if (!strncmp("tib", c, 3)) {
162 } else if (!strncmp("gib", c, 3)) {
165 } else if (!strncmp("mib", c, 3)) {
168 } else if (!strncmp("kib", c, 3)) {
171 } else if (!strncmp("p", c, 1) || !strncmp("pb", c, 2))
173 else if (!strncmp("t", c, 1) || !strncmp("tb", c, 2))
175 else if (!strncmp("g", c, 1) || !strncmp("gb", c, 2))
177 else if (!strncmp("m", c, 1) || !strncmp("mb", c, 2))
179 else if (!strncmp("k", c, 1) || !strncmp("kb", c, 2))
181 else if (!strncmp("%", c, 1)) {
188 ret *= (unsigned long long) mult;
194 static unsigned long long get_mult_bytes(const char *str, int len, void *data,
201 return __get_mult_bytes(str, data, percent);
204 * Go forward until we hit a non-digit, or +/- sign
206 while ((p - str) <= len) {
207 if (!isdigit((int) *p) &&
208 (((*p != '+') && (*p != '-')) || digit_seen))
210 digit_seen |= isdigit((int) *p);
214 if (!isalpha((int) *p) && (*p != '%'))
217 return __get_mult_bytes(p, data, percent);
221 * Convert string into a floating number. Return 1 for success and 0 otherwise.
223 int str_to_float(const char *str, double *val)
225 return (1 == sscanf(str, "%lf", val));
229 * convert string into decimal value, noting any size suffix
231 int str_to_decimal(const char *str, long long *val, int kilo, void *data)
239 if (strstr(str, "0x") || strstr(str, "0X"))
244 *val = strtoll(str, NULL, base);
245 if (*val == LONG_MAX && errno == ERANGE)
249 unsigned long long mult;
252 mult = get_mult_bytes(str, len, data, &perc);
258 *val *= get_mult_time(str[len - 1]);
263 int check_str_bytes(const char *p, long long *val, void *data)
265 return str_to_decimal(p, val, 1, data);
268 static int check_str_time(const char *p, long long *val)
270 return str_to_decimal(p, val, 0, NULL);
273 void strip_blank_front(char **p)
279 while (isspace((int) *s))
285 void strip_blank_end(char *p)
302 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
308 static int check_range_bytes(const char *str, long *val, void *data)
312 if (!str_to_decimal(str, &__val, 1, data)) {
320 static int check_int(const char *p, int *val)
324 if (strstr(p, "0x") || strstr(p, "0X")) {
325 if (sscanf(p, "%x", val) == 1)
328 if (sscanf(p, "%u", val) == 1)
335 static int opt_len(const char *str)
339 postfix = strchr(str, ':');
343 return (int)(postfix - str);
346 #define val_store(ptr, val, off, or, data) \
348 ptr = td_var((data), (off)); \
355 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
356 int first, int more, int curr)
360 long long ull, *ullp;
364 int ret = 0, is_time = 0;
365 const struct value_pair *vp;
366 struct value_pair posval[PARSE_MAX_VP];
367 int i, all_skipped = 1;
369 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
372 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
373 log_err("Option %s requires an argument\n", o->name);
379 case FIO_OPT_STR_MULTI: {
380 fio_opt_str_fn *fn = o->cb;
382 posval_sort(o, posval);
385 for (i = 0; i < PARSE_MAX_VP; i++) {
387 if (!vp->ival || vp->ival[0] == '\0')
390 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
394 *(unsigned int *) o->roff1 |= vp->oval;
396 *(unsigned int *) o->roff1 = vp->oval;
400 val_store(ilp, vp->oval, o->off1, vp->or, data);
406 if (ret && !all_skipped)
407 show_option_values(o);
412 case FIO_OPT_STR_VAL_TIME:
415 case FIO_OPT_STR_VAL: {
416 fio_opt_str_val_fn *fn = o->cb;
419 strncpy(tmp, ptr, sizeof(tmp) - 1);
420 p = strchr(tmp, ',');
425 ret = check_str_time(tmp, &ull);
427 ret = check_str_bytes(tmp, &ull, data);
432 if (o->maxval && ull > o->maxval) {
433 log_err("max value out of range: %llu"
434 " (%u max)\n", ull, o->maxval);
437 if (o->minval && ull < o->minval) {
438 log_err("min value out of range: %llu"
439 " (%u min)\n", ull, o->minval);
444 ret = fn(data, &ull);
446 if (o->type == FIO_OPT_INT) {
449 *(unsigned int *) o->roff1 = ull;
451 val_store(ilp, ull, o->off1, 0, data);
455 *(unsigned int *) o->roff2 = ull;
457 val_store(ilp, ull, o->off2, 0, data);
461 *(unsigned int *) o->roff3 = ull;
463 val_store(ilp, ull, o->off3, 0, data);
468 *(unsigned int *) o->roff2 = ull;
470 val_store(ilp, ull, o->off2, 0, data);
474 *(unsigned int *) o->roff3 = ull;
476 val_store(ilp, ull, o->off3, 0, data);
482 *(unsigned long long *) o->roff1 = ull;
484 val_store(ullp, ull, o->off1, 0, data);
488 *(unsigned long long *) o->roff2 = ull;
490 val_store(ullp, ull, o->off2, 0, data);
496 case FIO_OPT_FLOAT_LIST: {
500 ilp = td_var(data, o->off2);
503 if (curr >= o->maxlen) {
504 log_err("the list exceeding max length %d\n",
508 if (!str_to_float(ptr, &uf)) {
509 log_err("not a floating point value: %s\n", ptr);
512 if (!isnan(o->maxfp) && uf > o->maxfp) {
513 log_err("value out of range: %f"
514 " (range max: %f)\n", uf, o->maxfp);
517 if (!isnan(o->minfp) && uf < o->minfp) {
518 log_err("value out of range: %f"
519 " (range min: %f)\n", uf, o->minfp);
523 flp = td_var(data, o->off1);
528 case FIO_OPT_STR_STORE: {
529 fio_opt_str_fn *fn = o->cb;
531 if (o->roff1 || o->off1) {
533 cp = (char **) o->roff1;
535 cp = td_var(data, o->off1);
542 else if (o->posval[0].ival) {
543 posval_sort(o, posval);
546 for (i = 0; i < PARSE_MAX_VP; i++) {
548 if (!vp->ival || vp->ival[0] == '\0')
551 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
557 rest = strstr(*cp ?: ptr, ":");
571 show_option_values(o);
580 case FIO_OPT_RANGE: {
584 strncpy(tmp, ptr, sizeof(tmp) - 1);
586 /* Handle bsrange with separate read,write values: */
587 p1 = strchr(tmp, ',');
591 p1 = strchr(tmp, '-');
593 p1 = strchr(tmp, ':');
605 if (!check_range_bytes(p1, &ul1, data) &&
606 !check_range_bytes(p2, &ul2, data)) {
609 unsigned long foo = ul1;
617 *(unsigned int *) o->roff1 = ul1;
619 val_store(ilp, ul1, o->off1, 0, data);
621 *(unsigned int *) o->roff2 = ul2;
623 val_store(ilp, ul2, o->off2, 0, data);
626 if (o->roff3 && o->roff4) {
627 *(unsigned int *) o->roff3 = ul1;
628 *(unsigned int *) o->roff4 = ul2;
629 } else if (o->off3 && o->off4) {
630 val_store(ilp, ul1, o->off3, 0, data);
631 val_store(ilp, ul2, o->off4, 0, data);
635 if (o->roff5 && o->roff6) {
636 *(unsigned int *) o->roff5 = ul1;
637 *(unsigned int *) o->roff6 = ul2;
638 } else if (o->off5 && o->off6) {
639 val_store(ilp, ul1, o->off5, 0, data);
640 val_store(ilp, ul2, o->off6, 0, data);
645 if (o->roff3 && o->roff4) {
646 *(unsigned int *) o->roff3 = ul1;
647 *(unsigned int *) o->roff4 = ul2;
648 } else if (o->off3 && o->off4) {
649 val_store(ilp, ul1, o->off3, 0, data);
650 val_store(ilp, ul2, o->off4, 0, data);
654 if (o->roff5 && o->roff6) {
655 *(unsigned int *) o->roff5 = ul1;
656 *(unsigned int *) o->roff6 = ul2;
657 } else if (o->off5 && o->off6) {
658 val_store(ilp, ul1, o->off5, 0, data);
659 val_store(ilp, ul2, o->off6, 0, data);
668 case FIO_OPT_STR_SET: {
669 fio_opt_int_fn *fn = o->cb;
672 ret = check_int(ptr, &il);
673 else if (o->type == FIO_OPT_BOOL)
681 if (o->maxval && il > (int) o->maxval) {
682 log_err("max value out of range: %d (%d max)\n",
686 if (o->minval && il < o->minval) {
687 log_err("min value out of range: %d (%d min)\n",
700 *(unsigned int *)o->roff1 = il;
702 val_store(ilp, il, o->off1, 0, data);
706 *(unsigned int *) o->roff2 = il;
708 val_store(ilp, il, o->off2, 0, data);
713 case FIO_OPT_DEPRECATED:
714 log_info("Option %s is deprecated\n", o->name);
717 log_err("Bad option type %u\n", o->type);
725 ret = o->verify(o, data);
727 log_err("Correct format for offending option\n");
728 log_err("%20s: %s\n", o->name, o->help);
729 show_option_help(o, 1);
736 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
738 char *o_ptr, *ptr, *ptr2;
741 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
745 o_ptr = ptr = strdup(__ptr);
748 * See if we have another set of parameters, hidden after a comma.
749 * Do this before parsing this round, to check if we should
750 * copy set 1 options to set 2.
759 (o->type != FIO_OPT_STR_STORE) &&
760 (o->type != FIO_OPT_STR) &&
761 (o->type != FIO_OPT_FLOAT_LIST)) {
762 ptr2 = strchr(ptr, ',');
763 if (ptr2 && *(ptr2 + 1) == '\0')
765 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
767 ptr2 = strchr(ptr, ':');
769 ptr2 = strchr(ptr, '-');
771 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
772 ptr2 = strchr(ptr, ':');
776 * Don't return early if parsing the first option fails - if
777 * we are doing multiple arguments, we can allow the first one
780 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
796 static struct fio_option *get_option(char *opt,
797 struct fio_option *options, char **post)
799 struct fio_option *o;
802 ret = strchr(opt, '=');
808 strip_blank_end(ret);
809 o = find_option(options, ret);
811 o = find_option(options, opt);
818 static int opt_cmp(const void *p1, const void *p2)
820 struct fio_option *o;
827 s = strdup(*((char **) p1));
828 o = get_option(s, __fio_options, &foo);
834 s = strdup(*((char **) p2));
835 o = get_option(s, __fio_options, &foo);
841 return prio2 - prio1;
844 void sort_options(char **opts, struct fio_option *options, int num_opts)
846 __fio_options = options;
847 qsort(opts, num_opts, sizeof(char *), opt_cmp);
848 __fio_options = NULL;
851 int parse_cmd_option(const char *opt, const char *val,
852 struct fio_option *options, void *data)
854 struct fio_option *o;
856 o = find_option(options, opt);
858 log_err("Bad option <%s>\n", opt);
862 if (!handle_option(o, val, data))
865 log_err("fio: failed parsing %s=%s\n", opt, val);
869 int parse_option(char *opt, const char *input,
870 struct fio_option *options, struct fio_option **o, void *data)
875 log_err("fio: failed parsing %s\n", input);
880 *o = get_option(opt, options, &post);
883 int len = strlen(opt);
884 if (opt + len + 1 != post)
885 memmove(opt + len + 1, post, strlen(post));
891 if (!handle_option(*o, post, data))
894 log_err("fio: failed parsing %s\n", input);
899 * Option match, levenshtein distance. Handy for not quite remembering what
900 * the option name is.
902 static int string_distance(const char *s1, const char *s2)
904 unsigned int s1_len = strlen(s1);
905 unsigned int s2_len = strlen(s2);
906 unsigned int *p, *q, *r;
909 p = malloc(sizeof(unsigned int) * (s2_len + 1));
910 q = malloc(sizeof(unsigned int) * (s2_len + 1));
913 for (i = 1; i <= s2_len; i++)
916 for (i = 1; i <= s1_len; i++) {
918 for (j = 1; j <= s2_len; j++) {
919 unsigned int sub = p[j - 1];
921 if (s1[i - 1] != s2[j - 1])
924 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
937 static struct fio_option *find_child(struct fio_option *options,
938 struct fio_option *o)
940 struct fio_option *__o;
942 for (__o = options + 1; __o->name; __o++)
943 if (__o->parent && !strcmp(__o->parent, o->name))
949 static void __print_option(struct fio_option *o, struct fio_option *org,
963 p += sprintf(p, "%s", " ");
965 sprintf(p, "%s", o->name);
967 log_info("%-24s: %s\n", name, o->help);
970 static void print_option(struct fio_option *o)
972 struct fio_option *parent;
973 struct fio_option *__o;
974 unsigned int printed;
977 __print_option(o, NULL, 0);
984 while ((__o = find_child(o, parent)) != NULL) {
985 __print_option(__o, o, level);
994 int show_cmd_help(struct fio_option *options, const char *name)
996 struct fio_option *o, *closest;
997 unsigned int best_dist = -1U;
1001 if (!name || !strcmp(name, "all"))
1006 for (o = &options[0]; o->name; o++) {
1009 if (o->type == FIO_OPT_DEPRECATED)
1011 if (!exec_profile && o->prof_name)
1015 if (!strcmp(name, o->name) ||
1016 (o->alias && !strcmp(name, o->alias)))
1021 dist = string_distance(name, o->name);
1022 if (dist < best_dist) {
1029 if (show_all || match) {
1032 log_info("%20s: %s\n", o->name, o->help);
1043 show_option_help(o, 0);
1049 log_err("No such command: %s", name);
1052 * Only print an appropriately close option, one where the edit
1053 * distance isn't too big. Otherwise we get crazy matches.
1055 if (closest && best_dist < 3) {
1056 log_info(" - showing closest match\n");
1057 log_info("%20s: %s\n", closest->name, closest->help);
1058 show_option_help(closest, 0);
1066 * Handle parsing of default parameters.
1068 void fill_default_options(void *data, struct fio_option *options)
1070 struct fio_option *o;
1072 dprint(FD_PARSE, "filling default options\n");
1074 for (o = &options[0]; o->name; o++)
1076 handle_option(o, o->def, data);
1079 void option_init(struct fio_option *o)
1081 if (o->type == FIO_OPT_DEPRECATED)
1083 if (o->type == FIO_OPT_BOOL) {
1087 if (o->type == FIO_OPT_INT) {
1089 o->maxval = UINT_MAX;
1091 if (o->type == FIO_OPT_FLOAT_LIST) {
1095 if (o->type == FIO_OPT_STR_SET && o->def) {
1096 log_err("Option %s: string set option with"
1097 " default will always be true\n", o->name);
1099 if (!o->cb && (!o->off1 && !o->roff1))
1100 log_err("Option %s: neither cb nor offset given\n", o->name);
1101 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1102 o->type == FIO_OPT_STR_MULTI)
1104 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
1105 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
1106 log_err("Option %s: both cb and offset given\n", o->name);
1109 log_info("Options %s: no category defined. Setting to misc\n", o->name);
1110 o->category = FIO_OPT_C_GENERAL;
1115 * Sanitize the options structure. For now it just sets min/max for bool
1116 * values and whether both callback and offsets are given.
1118 void options_init(struct fio_option *options)
1120 struct fio_option *o;
1122 dprint(FD_PARSE, "init options\n");
1124 for (o = &options[0]; o->name; o++) {
1127 o->inv_opt = find_option(options, o->inverse);
1131 void options_free(struct fio_option *options, void *data)
1133 struct fio_option *o;
1136 dprint(FD_PARSE, "free options\n");
1138 for (o = &options[0]; o->name; o++) {
1139 if (o->type != FIO_OPT_STR_STORE || !o->off1)
1142 ptr = td_var(data, o->off1);