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 static int str_match_len(const struct value_pair *vp, const char *str)
348 return max(strlen(vp->ival), opt_len(str));
351 #define val_store(ptr, val, off, or, data) \
353 ptr = td_var((data), (off)); \
360 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
361 int first, int more, int curr)
365 long long ull, *ullp;
369 int ret = 0, is_time = 0;
370 const struct value_pair *vp;
371 struct value_pair posval[PARSE_MAX_VP];
372 int i, all_skipped = 1;
374 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
377 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
378 log_err("Option %s requires an argument\n", o->name);
384 case FIO_OPT_STR_MULTI: {
385 fio_opt_str_fn *fn = o->cb;
387 posval_sort(o, posval);
390 for (i = 0; i < PARSE_MAX_VP; i++) {
392 if (!vp->ival || vp->ival[0] == '\0')
395 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
399 *(unsigned int *) o->roff1 |= vp->oval;
401 *(unsigned int *) o->roff1 = vp->oval;
405 val_store(ilp, vp->oval, o->off1, vp->or, data);
411 if (ret && !all_skipped)
412 show_option_values(o);
417 case FIO_OPT_STR_VAL_TIME:
420 case FIO_OPT_STR_VAL: {
421 fio_opt_str_val_fn *fn = o->cb;
424 strncpy(tmp, ptr, sizeof(tmp) - 1);
425 p = strchr(tmp, ',');
430 ret = check_str_time(tmp, &ull);
432 ret = check_str_bytes(tmp, &ull, data);
437 if (o->maxval && ull > o->maxval) {
438 log_err("max value out of range: %llu"
439 " (%u max)\n", ull, o->maxval);
442 if (o->minval && ull < o->minval) {
443 log_err("min value out of range: %llu"
444 " (%u min)\n", ull, o->minval);
449 ret = fn(data, &ull);
451 if (o->type == FIO_OPT_INT) {
454 *(unsigned int *) o->roff1 = ull;
456 val_store(ilp, ull, o->off1, 0, data);
460 *(unsigned int *) o->roff2 = ull;
462 val_store(ilp, ull, o->off2, 0, data);
466 *(unsigned int *) o->roff3 = ull;
468 val_store(ilp, ull, o->off3, 0, data);
473 *(unsigned int *) o->roff2 = ull;
475 val_store(ilp, ull, o->off2, 0, data);
479 *(unsigned int *) o->roff3 = ull;
481 val_store(ilp, ull, o->off3, 0, data);
487 *(unsigned long long *) o->roff1 = ull;
489 val_store(ullp, ull, o->off1, 0, data);
493 *(unsigned long long *) o->roff2 = ull;
495 val_store(ullp, ull, o->off2, 0, data);
501 case FIO_OPT_FLOAT_LIST: {
505 ilp = td_var(data, o->off2);
508 if (curr >= o->maxlen) {
509 log_err("the list exceeding max length %d\n",
513 if (!str_to_float(ptr, &uf)) {
514 log_err("not a floating point value: %s\n", ptr);
517 if (!isnan(o->maxfp) && uf > o->maxfp) {
518 log_err("value out of range: %f"
519 " (range max: %f)\n", uf, o->maxfp);
522 if (!isnan(o->minfp) && uf < o->minfp) {
523 log_err("value out of range: %f"
524 " (range min: %f)\n", uf, o->minfp);
528 flp = td_var(data, o->off1);
533 case FIO_OPT_STR_STORE: {
534 fio_opt_str_fn *fn = o->cb;
536 if (o->roff1 || o->off1) {
538 cp = (char **) o->roff1;
540 cp = td_var(data, o->off1);
547 else if (o->posval[0].ival) {
548 posval_sort(o, posval);
551 for (i = 0; i < PARSE_MAX_VP; i++) {
553 if (!vp->ival || vp->ival[0] == '\0')
556 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
562 rest = strstr(*cp ?: ptr, ":");
576 show_option_values(o);
585 case FIO_OPT_RANGE: {
589 strncpy(tmp, ptr, sizeof(tmp) - 1);
591 /* Handle bsrange with separate read,write values: */
592 p1 = strchr(tmp, ',');
596 p1 = strchr(tmp, '-');
598 p1 = strchr(tmp, ':');
610 if (!check_range_bytes(p1, &ul1, data) &&
611 !check_range_bytes(p2, &ul2, data)) {
614 unsigned long foo = ul1;
622 *(unsigned int *) o->roff1 = ul1;
624 val_store(ilp, ul1, o->off1, 0, data);
626 *(unsigned int *) o->roff2 = ul2;
628 val_store(ilp, ul2, o->off2, 0, data);
631 if (o->roff3 && o->roff4) {
632 *(unsigned int *) o->roff3 = ul1;
633 *(unsigned int *) o->roff4 = ul2;
634 } else if (o->off3 && o->off4) {
635 val_store(ilp, ul1, o->off3, 0, data);
636 val_store(ilp, ul2, o->off4, 0, data);
640 if (o->roff5 && o->roff6) {
641 *(unsigned int *) o->roff5 = ul1;
642 *(unsigned int *) o->roff6 = ul2;
643 } else if (o->off5 && o->off6) {
644 val_store(ilp, ul1, o->off5, 0, data);
645 val_store(ilp, ul2, o->off6, 0, data);
650 if (o->roff3 && o->roff4) {
651 *(unsigned int *) o->roff3 = ul1;
652 *(unsigned int *) o->roff4 = ul2;
653 } else if (o->off3 && o->off4) {
654 val_store(ilp, ul1, o->off3, 0, data);
655 val_store(ilp, ul2, o->off4, 0, data);
659 if (o->roff5 && o->roff6) {
660 *(unsigned int *) o->roff5 = ul1;
661 *(unsigned int *) o->roff6 = ul2;
662 } else if (o->off5 && o->off6) {
663 val_store(ilp, ul1, o->off5, 0, data);
664 val_store(ilp, ul2, o->off6, 0, data);
673 case FIO_OPT_STR_SET: {
674 fio_opt_int_fn *fn = o->cb;
677 ret = check_int(ptr, &il);
678 else if (o->type == FIO_OPT_BOOL)
686 if (o->maxval && il > (int) o->maxval) {
687 log_err("max value out of range: %d (%d max)\n",
691 if (o->minval && il < o->minval) {
692 log_err("min value out of range: %d (%d min)\n",
705 *(unsigned int *)o->roff1 = il;
707 val_store(ilp, il, o->off1, 0, data);
711 *(unsigned int *) o->roff2 = il;
713 val_store(ilp, il, o->off2, 0, data);
718 case FIO_OPT_DEPRECATED:
719 log_info("Option %s is deprecated\n", o->name);
722 log_err("Bad option type %u\n", o->type);
730 ret = o->verify(o, data);
732 log_err("Correct format for offending option\n");
733 log_err("%20s: %s\n", o->name, o->help);
734 show_option_help(o, 1);
741 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
743 char *o_ptr, *ptr, *ptr2;
746 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
750 o_ptr = ptr = strdup(__ptr);
753 * See if we have another set of parameters, hidden after a comma.
754 * Do this before parsing this round, to check if we should
755 * copy set 1 options to set 2.
764 (o->type != FIO_OPT_STR_STORE) &&
765 (o->type != FIO_OPT_STR) &&
766 (o->type != FIO_OPT_FLOAT_LIST)) {
767 ptr2 = strchr(ptr, ',');
768 if (ptr2 && *(ptr2 + 1) == '\0')
770 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
772 ptr2 = strchr(ptr, ':');
774 ptr2 = strchr(ptr, '-');
776 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
777 ptr2 = strchr(ptr, ':');
781 * Don't return early if parsing the first option fails - if
782 * we are doing multiple arguments, we can allow the first one
785 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
801 static struct fio_option *get_option(char *opt,
802 struct fio_option *options, char **post)
804 struct fio_option *o;
807 ret = strchr(opt, '=');
813 strip_blank_end(ret);
814 o = find_option(options, ret);
816 o = find_option(options, opt);
823 static int opt_cmp(const void *p1, const void *p2)
825 struct fio_option *o;
832 s = strdup(*((char **) p1));
833 o = get_option(s, __fio_options, &foo);
839 s = strdup(*((char **) p2));
840 o = get_option(s, __fio_options, &foo);
846 return prio2 - prio1;
849 void sort_options(char **opts, struct fio_option *options, int num_opts)
851 __fio_options = options;
852 qsort(opts, num_opts, sizeof(char *), opt_cmp);
853 __fio_options = NULL;
856 int parse_cmd_option(const char *opt, const char *val,
857 struct fio_option *options, void *data)
859 struct fio_option *o;
861 o = find_option(options, opt);
863 log_err("Bad option <%s>\n", opt);
867 if (!handle_option(o, val, data))
870 log_err("fio: failed parsing %s=%s\n", opt, val);
874 int parse_option(char *opt, const char *input,
875 struct fio_option *options, struct fio_option **o, void *data)
880 log_err("fio: failed parsing %s\n", input);
885 *o = get_option(opt, options, &post);
888 int len = strlen(opt);
889 if (opt + len + 1 != post)
890 memmove(opt + len + 1, post, strlen(post));
896 if (!handle_option(*o, post, data))
899 log_err("fio: failed parsing %s\n", input);
904 * Option match, levenshtein distance. Handy for not quite remembering what
905 * the option name is.
907 static int string_distance(const char *s1, const char *s2)
909 unsigned int s1_len = strlen(s1);
910 unsigned int s2_len = strlen(s2);
911 unsigned int *p, *q, *r;
914 p = malloc(sizeof(unsigned int) * (s2_len + 1));
915 q = malloc(sizeof(unsigned int) * (s2_len + 1));
918 for (i = 1; i <= s2_len; i++)
921 for (i = 1; i <= s1_len; i++) {
923 for (j = 1; j <= s2_len; j++) {
924 unsigned int sub = p[j - 1];
926 if (s1[i - 1] != s2[j - 1])
929 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
942 static struct fio_option *find_child(struct fio_option *options,
943 struct fio_option *o)
945 struct fio_option *__o;
947 for (__o = options + 1; __o->name; __o++)
948 if (__o->parent && !strcmp(__o->parent, o->name))
954 static void __print_option(struct fio_option *o, struct fio_option *org,
968 p += sprintf(p, "%s", " ");
970 sprintf(p, "%s", o->name);
972 log_info("%-24s: %s\n", name, o->help);
975 static void print_option(struct fio_option *o)
977 struct fio_option *parent;
978 struct fio_option *__o;
979 unsigned int printed;
982 __print_option(o, NULL, 0);
989 while ((__o = find_child(o, parent)) != NULL) {
990 __print_option(__o, o, level);
999 int show_cmd_help(struct fio_option *options, const char *name)
1001 struct fio_option *o, *closest;
1002 unsigned int best_dist = -1U;
1006 if (!name || !strcmp(name, "all"))
1011 for (o = &options[0]; o->name; o++) {
1014 if (o->type == FIO_OPT_DEPRECATED)
1016 if (!exec_profile && o->prof_name)
1020 if (!strcmp(name, o->name) ||
1021 (o->alias && !strcmp(name, o->alias)))
1026 dist = string_distance(name, o->name);
1027 if (dist < best_dist) {
1034 if (show_all || match) {
1037 log_info("%20s: %s\n", o->name, o->help);
1048 show_option_help(o, 0);
1054 log_err("No such command: %s", name);
1057 * Only print an appropriately close option, one where the edit
1058 * distance isn't too big. Otherwise we get crazy matches.
1060 if (closest && best_dist < 3) {
1061 log_info(" - showing closest match\n");
1062 log_info("%20s: %s\n", closest->name, closest->help);
1063 show_option_help(closest, 0);
1071 * Handle parsing of default parameters.
1073 void fill_default_options(void *data, struct fio_option *options)
1075 struct fio_option *o;
1077 dprint(FD_PARSE, "filling default options\n");
1079 for (o = &options[0]; o->name; o++)
1081 handle_option(o, o->def, data);
1084 void option_init(struct fio_option *o)
1086 if (o->type == FIO_OPT_DEPRECATED)
1088 if (o->type == FIO_OPT_BOOL) {
1092 if (o->type == FIO_OPT_INT) {
1094 o->maxval = UINT_MAX;
1096 if (o->type == FIO_OPT_FLOAT_LIST) {
1100 if (o->type == FIO_OPT_STR_SET && o->def) {
1101 log_err("Option %s: string set option with"
1102 " default will always be true\n", o->name);
1104 if (!o->cb && (!o->off1 && !o->roff1))
1105 log_err("Option %s: neither cb nor offset given\n", o->name);
1106 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1107 o->type == FIO_OPT_STR_MULTI)
1109 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
1110 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
1111 log_err("Option %s: both cb and offset given\n", o->name);
1114 log_info("Options %s: no category defined. Setting to misc\n", o->name);
1115 o->category = FIO_OPT_C_GENERAL;
1120 * Sanitize the options structure. For now it just sets min/max for bool
1121 * values and whether both callback and offsets are given.
1123 void options_init(struct fio_option *options)
1125 struct fio_option *o;
1127 dprint(FD_PARSE, "init options\n");
1129 for (o = &options[0]; o->name; o++) {
1132 o->inv_opt = find_option(options, o->inverse);
1136 void options_free(struct fio_option *options, void *data)
1138 struct fio_option *o;
1141 dprint(FD_PARSE, "free options\n");
1143 for (o = &options[0]; o->name; o++) {
1144 if (o->type != FIO_OPT_STR_STORE || !o->off1)
1147 ptr = td_var(data, o->off1);