2 * This file contains the ini and command liner parser main.
19 #include "lib/ieee754.h"
21 static struct fio_option *__fio_options;
23 static int vp_cmp(const void *p1, const void *p2)
25 const struct value_pair *vp1 = p1;
26 const struct value_pair *vp2 = p2;
28 return strlen(vp2->ival) - strlen(vp1->ival);
31 static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
33 const struct value_pair *vp;
36 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
38 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
39 vp = &o->posval[entries];
40 if (!vp->ival || vp->ival[0] == '\0')
43 memcpy(&vpmap[entries], vp, sizeof(*vp));
46 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
49 static void show_option_range(struct fio_option *o,
50 int (*logger)(const char *format, ...))
52 if (o->type == FIO_OPT_FLOAT_LIST) {
53 if (o->minfp == DBL_MIN && o->maxfp == DBL_MAX)
56 logger("%20s: min=%f", "range", o->minfp);
57 if (o->maxfp != DBL_MAX)
58 logger(", max=%f", o->maxfp);
60 } else if (!o->posval[0].ival) {
61 if (!o->minval && !o->maxval)
64 logger("%20s: min=%d", "range", o->minval);
66 logger(", max=%d", o->maxval);
71 static void show_option_values(struct fio_option *o)
75 for (i = 0; i < PARSE_MAX_VP; i++) {
76 const struct value_pair *vp = &o->posval[i];
81 log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
83 log_info(" %s", vp->help);
91 static void show_option_help(struct fio_option *o, int is_err)
93 const char *typehelp[] = {
97 "string with possible k/m/g postfix (opt=4k)",
98 "string with time postfix (opt=10s)",
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)",
107 int (*logger)(const char *format, ...);
115 logger("%20s: %s\n", "alias", o->alias);
117 logger("%20s: %s\n", "type", typehelp[o->type]);
118 logger("%20s: %s\n", "default", o->def ? o->def : "no default");
120 logger("%20s: only for profile '%s'\n", "valid", o->prof_name);
121 show_option_range(o, logger);
122 show_option_values(o);
125 static unsigned long long get_mult_time(const char *str, int len)
129 unsigned long long mult = 1;
132 * Go forward until we hit a non-digit, or +/- sign
134 while ((p - str) <= len) {
135 if (!isdigit((int) *p) && (*p != '+') && (*p != '-'))
140 if (!isalpha((int) *p))
144 for (int i = 0; i < strlen(c); i++)
145 c[i] = tolower(c[i]);
147 if (!strncmp("us", c, 2) || !strncmp("usec", c, 4))
149 else if (!strncmp("ms", c, 2) || !strncmp("msec", c, 4))
151 else if (!strcmp("s", c))
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;
164 static int is_separator(char c)
177 static unsigned long long __get_mult_bytes(const char *p, void *data,
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;
190 for (i = 0; i < strlen(c); i++) {
191 c[i] = tolower(c[i]);
192 if (is_separator(c[i])) {
198 if (!strncmp("pib", c, 3)) {
201 } else if (!strncmp("tib", c, 3)) {
204 } else if (!strncmp("gib", c, 3)) {
207 } else if (!strncmp("mib", c, 3)) {
210 } else if (!strncmp("kib", c, 3)) {
213 } else if (!strncmp("p", c, 1) || !strncmp("pb", c, 2))
215 else if (!strncmp("t", c, 1) || !strncmp("tb", c, 2))
217 else if (!strncmp("g", c, 1) || !strncmp("gb", c, 2))
219 else if (!strncmp("m", c, 1) || !strncmp("mb", c, 2))
221 else if (!strncmp("k", c, 1) || !strncmp("kb", c, 2))
223 else if (!strncmp("%", c, 1)) {
230 ret *= (unsigned long long) mult;
236 static unsigned long long get_mult_bytes(const char *str, int len, void *data,
243 return __get_mult_bytes(str, data, percent);
246 * Go forward until we hit a non-digit, or +/- sign
248 while ((p - str) <= len) {
249 if (!isdigit((int) *p) &&
250 (((*p != '+') && (*p != '-')) || digit_seen))
252 digit_seen |= isdigit((int) *p);
256 if (!isalpha((int) *p) && (*p != '%'))
259 return __get_mult_bytes(p, data, percent);
263 * Convert string into a floating number. Return 1 for success and 0 otherwise.
265 int str_to_float(const char *str, double *val)
267 return (1 == sscanf(str, "%lf", val));
271 * convert string into decimal value, noting any size suffix
273 int str_to_decimal(const char *str, long long *val, int kilo, void *data)
281 if (strstr(str, "0x") || strstr(str, "0X"))
286 *val = strtoll(str, NULL, base);
287 if (*val == LONG_MAX && errno == ERANGE)
291 unsigned long long mult;
294 mult = get_mult_bytes(str, len, data, &perc);
300 *val *= get_mult_time(str, len);
305 int check_str_bytes(const char *p, long long *val, void *data)
307 return str_to_decimal(p, val, 1, data);
310 int check_str_time(const char *p, long long *val)
312 return str_to_decimal(p, val, 0, NULL);
315 void strip_blank_front(char **p)
321 while (isspace((int) *s))
327 void strip_blank_end(char *p)
344 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
350 static int check_range_bytes(const char *str, long *val, void *data)
354 if (!str_to_decimal(str, &__val, 1, data)) {
362 static int check_int(const char *p, int *val)
366 if (strstr(p, "0x") || strstr(p, "0X")) {
367 if (sscanf(p, "%x", val) == 1)
370 if (sscanf(p, "%u", val) == 1)
377 static int opt_len(const char *str)
381 postfix = strchr(str, ':');
385 return (int)(postfix - str);
388 static int str_match_len(const struct value_pair *vp, const char *str)
390 return max(strlen(vp->ival), opt_len(str));
393 #define val_store(ptr, val, off, or, data, o) \
395 ptr = td_var((data), (o), (off)); \
402 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
403 int first, int more, int curr)
407 long long ull, *ullp;
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;
416 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
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);
426 case FIO_OPT_STR_MULTI: {
427 fio_opt_str_fn *fn = o->cb;
429 posval_sort(o, posval);
432 for (i = 0; i < PARSE_MAX_VP; i++) {
434 if (!vp->ival || vp->ival[0] == '\0')
437 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
440 val_store(ilp, vp->oval, o->off1, vp->orval, data, o);
445 if (ret && !all_skipped)
446 show_option_values(o);
451 case FIO_OPT_STR_VAL_TIME:
454 case FIO_OPT_STR_VAL: {
455 fio_opt_str_val_fn *fn = o->cb;
458 strncpy(tmp, ptr, sizeof(tmp) - 1);
459 p = strchr(tmp, ',');
464 ret = check_str_time(tmp, &ull);
466 ret = check_str_bytes(tmp, &ull, data);
468 dprint(FD_PARSE, " ret=%d, out=%llu\n", ret, ull);
473 if (o->maxval && ull > o->maxval) {
474 log_err("max value out of range: %llu"
475 " (%u max)\n", ull, o->maxval);
478 if (o->minval && ull < o->minval) {
479 log_err("min value out of range: %llu"
480 " (%u min)\n", ull, o->minval);
483 if (o->posval[0].ival) {
484 posval_sort(o, posval);
487 for (i = 0; i < PARSE_MAX_VP; i++) {
489 if (!vp->ival || vp->ival[0] == '\0')
491 if (vp->oval == ull) {
497 log_err("fio: value %llu not allowed:\n", ull);
498 show_option_values(o);
504 ret = fn(data, &ull);
506 if (o->type == FIO_OPT_INT) {
508 val_store(ilp, ull, o->off1, 0, data, o);
511 val_store(ilp, ull, o->off2, 0, data, o);
515 val_store(ilp, ull, o->off3, 0, data, o);
520 val_store(ilp, ull, o->off2, 0, data, o);
524 val_store(ilp, ull, o->off3, 0, data, o);
529 val_store(ullp, ull, o->off1, 0, data, o);
532 val_store(ullp, ull, o->off2, 0, data, o);
538 case FIO_OPT_FLOAT_LIST: {
543 ** Initialize precision to 0 and zero out list
544 ** in case specified list is shorter than default
548 ilp = td_var(data, o, o->off2);
552 flp = td_var(data, o, o->off1);
553 for(i = 0; i < o->maxlen; i++)
556 if (curr >= o->maxlen) {
557 log_err("the list exceeding max length %d\n",
561 if (!str_to_float(ptr, &uf)) {
562 log_err("not a floating point value: %s\n", ptr);
566 log_err("value out of range: %f"
567 " (range max: %f)\n", uf, o->maxfp);
571 log_err("value out of range: %f"
572 " (range min: %f)\n", uf, o->minfp);
576 flp = td_var(data, o, o->off1);
579 dprint(FD_PARSE, " out=%f\n", uf);
582 ** Calculate precision for output by counting
583 ** number of digits after period. Find first
584 ** period in entire remaining list each time
586 cp2 = strchr(ptr, '.');
590 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
594 ilp = td_var(data, o, o->off2);
602 case FIO_OPT_STR_STORE: {
603 fio_opt_str_fn *fn = o->cb;
606 cp = td_var(data, o, o->off1);
612 else if (o->posval[0].ival) {
613 posval_sort(o, posval);
616 for (i = 0; i < PARSE_MAX_VP; i++) {
618 if (!vp->ival || vp->ival[0] == '\0')
621 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
627 rest = strstr(*cp ?: ptr, ":");
641 show_option_values(o);
650 case FIO_OPT_RANGE: {
654 strncpy(tmp, ptr, sizeof(tmp) - 1);
656 /* Handle bsrange with separate read,write values: */
657 p1 = strchr(tmp, ',');
661 p1 = strchr(tmp, '-');
663 p1 = strchr(tmp, ':');
675 if (!check_range_bytes(p1, &ul1, data) &&
676 !check_range_bytes(p2, &ul2, data)) {
679 unsigned long foo = ul1;
686 val_store(ilp, ul1, o->off1, 0, data, o);
687 val_store(ilp, ul2, o->off2, 0, data, o);
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);
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);
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);
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);
720 case FIO_OPT_STR_SET: {
721 fio_opt_int_fn *fn = o->cb;
724 ret = check_int(ptr, &il);
725 else if (o->type == FIO_OPT_BOOL)
730 dprint(FD_PARSE, " ret=%d, out=%d\n", ret, il);
735 if (o->maxval && il > (int) o->maxval) {
736 log_err("max value out of range: %d (%d max)\n",
740 if (o->minval && il < o->minval) {
741 log_err("min value out of range: %d (%d min)\n",
753 val_store(ilp, il, o->off1, 0, data, o);
756 val_store(ilp, il, o->off2, 0, data, o);
761 case FIO_OPT_DEPRECATED:
762 log_info("Option %s is deprecated\n", o->name);
766 log_err("Bad option type %u\n", o->type);
774 ret = o->verify(o, data);
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);
785 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
787 char *o_ptr, *ptr, *ptr2;
790 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
794 o_ptr = ptr = strdup(__ptr);
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.
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')
814 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
816 ptr2 = strchr(ptr, ':');
818 ptr2 = strchr(ptr, '-');
820 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
821 ptr2 = strchr(ptr, ':');
825 * Don't return early if parsing the first option fails - if
826 * we are doing multiple arguments, we can allow the first one
829 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
845 static struct fio_option *get_option(char *opt,
846 struct fio_option *options, char **post)
848 struct fio_option *o;
851 ret = strchr(opt, '=');
857 strip_blank_end(ret);
858 o = find_option(options, ret);
860 o = find_option(options, opt);
867 static int opt_cmp(const void *p1, const void *p2)
869 struct fio_option *o;
876 s = strdup(*((char **) p1));
877 o = get_option(s, __fio_options, &foo);
883 s = strdup(*((char **) p2));
884 o = get_option(s, __fio_options, &foo);
890 return prio2 - prio1;
893 void sort_options(char **opts, struct fio_option *options, int num_opts)
895 __fio_options = options;
896 qsort(opts, num_opts, sizeof(char *), opt_cmp);
897 __fio_options = NULL;
900 int parse_cmd_option(const char *opt, const char *val,
901 struct fio_option *options, void *data)
903 struct fio_option *o;
905 o = find_option(options, opt);
907 log_err("Bad option <%s>\n", opt);
911 if (!handle_option(o, val, data))
914 log_err("fio: failed parsing %s=%s\n", opt, val);
918 int parse_option(char *opt, const char *input,
919 struct fio_option *options, struct fio_option **o, void *data,
925 log_err("fio: failed parsing %s\n", input);
930 *o = get_option(opt, options, &post);
933 int len = strlen(opt);
934 if (opt + len + 1 != post)
935 memmove(opt + len + 1, post, strlen(post));
941 if (handle_option(*o, post, data)) {
942 log_err("fio: failed parsing %s\n", input);
949 if (!strcmp("description", (*o)->name))
954 log_info("--%s%s", (*o)->name, post ? "" : " ");
956 log_info("=%s%s%s ", delim, post, delim);
963 * Option match, levenshtein distance. Handy for not quite remembering what
964 * the option name is.
966 static int string_distance(const char *s1, const char *s2)
968 unsigned int s1_len = strlen(s1);
969 unsigned int s2_len = strlen(s2);
970 unsigned int *p, *q, *r;
973 p = malloc(sizeof(unsigned int) * (s2_len + 1));
974 q = malloc(sizeof(unsigned int) * (s2_len + 1));
977 for (i = 1; i <= s2_len; i++)
980 for (i = 1; i <= s1_len; i++) {
982 for (j = 1; j <= s2_len; j++) {
983 unsigned int sub = p[j - 1];
985 if (s1[i - 1] != s2[j - 1])
988 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
1001 static struct fio_option *find_child(struct fio_option *options,
1002 struct fio_option *o)
1004 struct fio_option *__o;
1006 for (__o = options + 1; __o->name; __o++)
1007 if (__o->parent && !strcmp(__o->parent, o->name))
1013 static void __print_option(struct fio_option *o, struct fio_option *org,
1027 p += sprintf(p, "%s", " ");
1029 sprintf(p, "%s", o->name);
1031 log_info("%-24s: %s\n", name, o->help);
1034 static void print_option(struct fio_option *o)
1036 struct fio_option *parent;
1037 struct fio_option *__o;
1038 unsigned int printed;
1041 __print_option(o, NULL, 0);
1048 while ((__o = find_child(o, parent)) != NULL) {
1049 __print_option(__o, o, level);
1058 int show_cmd_help(struct fio_option *options, const char *name)
1060 struct fio_option *o, *closest;
1061 unsigned int best_dist = -1U;
1065 if (!name || !strcmp(name, "all"))
1070 for (o = &options[0]; o->name; o++) {
1073 if (o->type == FIO_OPT_DEPRECATED)
1075 if (!exec_profile && o->prof_name)
1077 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1081 if (!strcmp(name, o->name) ||
1082 (o->alias && !strcmp(name, o->alias)))
1087 dist = string_distance(name, o->name);
1088 if (dist < best_dist) {
1095 if (show_all || match) {
1098 log_info("%20s: %s\n", o->name, o->help);
1109 show_option_help(o, 0);
1115 log_err("No such command: %s", name);
1118 * Only print an appropriately close option, one where the edit
1119 * distance isn't too big. Otherwise we get crazy matches.
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);
1132 * Handle parsing of default parameters.
1134 void fill_default_options(void *data, struct fio_option *options)
1136 struct fio_option *o;
1138 dprint(FD_PARSE, "filling default options\n");
1140 for (o = &options[0]; o->name; o++)
1142 handle_option(o, o->def, data);
1145 void option_init(struct fio_option *o)
1147 if (o->type == FIO_OPT_DEPRECATED)
1149 if (o->type == FIO_OPT_BOOL) {
1153 if (o->type == FIO_OPT_INT) {
1155 o->maxval = UINT_MAX;
1157 if (o->type == FIO_OPT_FLOAT_LIST) {
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);
1165 if (!o->cb && !o->off1)
1166 log_err("Option %s: neither cb nor offset given\n", o->name);
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;
1172 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1173 o->type == FIO_OPT_STR_MULTI)
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);
1180 * Sanitize the options structure. For now it just sets min/max for bool
1181 * values and whether both callback and offsets are given.
1183 void options_init(struct fio_option *options)
1185 struct fio_option *o;
1187 dprint(FD_PARSE, "init options\n");
1189 for (o = &options[0]; o->name; o++) {
1192 o->inv_opt = find_option(options, o->inverse);
1196 void options_free(struct fio_option *options, void *data)
1198 struct fio_option *o;
1201 dprint(FD_PARSE, "free options\n");
1203 for (o = &options[0]; o->name; o++) {
1204 if (o->type != FIO_OPT_STR_STORE || !o->off1)
1207 ptr = td_var(data, o, o->off1);