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,
130 unsigned long long mult = 1;
133 * Go forward until we hit a non-digit, or +/- sign
135 while ((p - str) <= len) {
136 if (!isdigit((int) *p) && (*p != '+') && (*p != '-'))
141 if (!isalpha((int) *p)) {
149 for (int i = 0; i < strlen(c); i++)
150 c[i] = tolower(c[i]);
152 if (!strncmp("us", c, 2) || !strncmp("usec", c, 4))
154 else if (!strncmp("ms", c, 2) || !strncmp("msec", c, 4))
156 else if (!strcmp("s", c))
158 else if (!strcmp("m", c))
159 mult = 60 * 1000000UL;
160 else if (!strcmp("h", c))
161 mult = 60 * 60 * 1000000UL;
162 else if (!strcmp("d", c))
163 mult = 24 * 60 * 60 * 1000000UL;
169 static int is_separator(char c)
182 static unsigned long long __get_mult_bytes(const char *p, void *data,
185 unsigned int kb_base = fio_get_kb_base(data);
186 unsigned long long ret = 1;
187 unsigned int i, pow = 0, mult = kb_base;
195 for (i = 0; i < strlen(c); i++) {
196 c[i] = tolower(c[i]);
197 if (is_separator(c[i])) {
203 if (!strncmp("pib", c, 3)) {
206 } else if (!strncmp("tib", c, 3)) {
209 } else if (!strncmp("gib", c, 3)) {
212 } else if (!strncmp("mib", c, 3)) {
215 } else if (!strncmp("kib", c, 3)) {
218 } else if (!strncmp("p", c, 1) || !strncmp("pb", c, 2))
220 else if (!strncmp("t", c, 1) || !strncmp("tb", c, 2))
222 else if (!strncmp("g", c, 1) || !strncmp("gb", c, 2))
224 else if (!strncmp("m", c, 1) || !strncmp("mb", c, 2))
226 else if (!strncmp("k", c, 1) || !strncmp("kb", c, 2))
228 else if (!strncmp("%", c, 1)) {
235 ret *= (unsigned long long) mult;
241 static unsigned long long get_mult_bytes(const char *str, int len, void *data,
248 return __get_mult_bytes(str, data, percent);
251 * Go forward until we hit a non-digit, or +/- sign
253 while ((p - str) <= len) {
254 if (!isdigit((int) *p) &&
255 (((*p != '+') && (*p != '-')) || digit_seen))
257 digit_seen |= isdigit((int) *p);
261 if (!isalpha((int) *p) && (*p != '%'))
264 return __get_mult_bytes(p, data, percent);
268 * Convert string into a floating number. Return 1 for success and 0 otherwise.
270 int str_to_float(const char *str, double *val)
272 return (1 == sscanf(str, "%lf", val));
276 * convert string into decimal value, noting any size suffix
278 int str_to_decimal(const char *str, long long *val, int kilo, void *data,
287 if (strstr(str, "0x") || strstr(str, "0X"))
292 *val = strtoll(str, NULL, base);
293 if (*val == LONG_MAX && errno == ERANGE)
297 unsigned long long mult;
300 mult = get_mult_bytes(str, len, data, &perc);
306 *val *= get_mult_time(str, len, is_seconds);
311 int check_str_bytes(const char *p, long long *val, void *data)
313 return str_to_decimal(p, val, 1, data, 0);
316 int check_str_time(const char *p, long long *val, int is_seconds)
318 return str_to_decimal(p, val, 0, NULL, is_seconds);
321 void strip_blank_front(char **p)
327 while (isspace((int) *s))
333 void strip_blank_end(char *p)
350 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
356 static int check_range_bytes(const char *str, long *val, void *data)
360 if (!str_to_decimal(str, &__val, 1, data, 0)) {
368 static int check_int(const char *p, int *val)
372 if (strstr(p, "0x") || strstr(p, "0X")) {
373 if (sscanf(p, "%x", val) == 1)
376 if (sscanf(p, "%u", val) == 1)
383 static size_t opt_len(const char *str)
387 postfix = strchr(str, ':');
391 return (int)(postfix - str);
394 static int str_match_len(const struct value_pair *vp, const char *str)
396 return max(strlen(vp->ival), opt_len(str));
399 #define val_store(ptr, val, off, or, data, o) \
401 ptr = td_var((data), (o), (off)); \
408 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
409 int first, int more, int curr)
413 long long ull, *ullp;
417 int ret = 0, is_time = 0;
418 const struct value_pair *vp;
419 struct value_pair posval[PARSE_MAX_VP];
420 int i, all_skipped = 1;
422 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
425 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
426 log_err("Option %s requires an argument\n", o->name);
432 case FIO_OPT_STR_MULTI: {
433 fio_opt_str_fn *fn = o->cb;
435 posval_sort(o, posval);
438 for (i = 0; i < PARSE_MAX_VP; i++) {
440 if (!vp->ival || vp->ival[0] == '\0')
443 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
446 val_store(ilp, vp->oval, o->off1, vp->orval, data, o);
451 if (ret && !all_skipped)
452 show_option_values(o);
457 case FIO_OPT_STR_VAL_TIME:
460 case FIO_OPT_STR_VAL: {
461 fio_opt_str_val_fn *fn = o->cb;
464 strncpy(tmp, ptr, sizeof(tmp) - 1);
465 p = strchr(tmp, ',');
470 ret = check_str_time(tmp, &ull, o->is_seconds);
472 ret = check_str_bytes(tmp, &ull, data);
474 dprint(FD_PARSE, " ret=%d, out=%llu\n", ret, ull);
479 if (o->maxval && ull > o->maxval) {
480 log_err("max value out of range: %llu"
481 " (%u max)\n", ull, o->maxval);
484 if (o->minval && ull < o->minval) {
485 log_err("min value out of range: %llu"
486 " (%u min)\n", ull, o->minval);
489 if (o->posval[0].ival) {
490 posval_sort(o, posval);
493 for (i = 0; i < PARSE_MAX_VP; i++) {
495 if (!vp->ival || vp->ival[0] == '\0')
497 if (vp->oval == ull) {
503 log_err("fio: value %llu not allowed:\n", ull);
504 show_option_values(o);
510 ret = fn(data, &ull);
512 if (o->type == FIO_OPT_INT) {
514 val_store(ilp, ull, o->off1, 0, data, o);
517 val_store(ilp, ull, o->off2, 0, data, o);
521 val_store(ilp, ull, o->off3, 0, data, o);
526 val_store(ilp, ull, o->off2, 0, data, o);
530 val_store(ilp, ull, o->off3, 0, data, o);
535 val_store(ullp, ull, o->off1, 0, data, o);
538 val_store(ullp, ull, o->off2, 0, data, o);
544 case FIO_OPT_FLOAT_LIST: {
549 ** Initialize precision to 0 and zero out list
550 ** in case specified list is shorter than default
554 ilp = td_var(data, o, o->off2);
558 flp = td_var(data, o, o->off1);
559 for(i = 0; i < o->maxlen; i++)
562 if (curr >= o->maxlen) {
563 log_err("the list exceeding max length %d\n",
567 if (!str_to_float(ptr, &uf)) {
568 log_err("not a floating point value: %s\n", ptr);
572 log_err("value out of range: %f"
573 " (range max: %f)\n", uf, o->maxfp);
577 log_err("value out of range: %f"
578 " (range min: %f)\n", uf, o->minfp);
582 flp = td_var(data, o, o->off1);
585 dprint(FD_PARSE, " out=%f\n", uf);
588 ** Calculate precision for output by counting
589 ** number of digits after period. Find first
590 ** period in entire remaining list each time
592 cp2 = strchr(ptr, '.');
596 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
600 ilp = td_var(data, o, o->off2);
608 case FIO_OPT_STR_STORE: {
609 fio_opt_str_fn *fn = o->cb;
615 cp = td_var(data, o, o->off1);
621 else if (o->posval[0].ival) {
622 posval_sort(o, posval);
625 for (i = 0; i < PARSE_MAX_VP; i++) {
627 if (!vp->ival || vp->ival[0] == '\0' || !cp)
630 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
636 rest = strstr(*cp ?: ptr, ":");
650 show_option_values(o);
659 case FIO_OPT_RANGE: {
663 strncpy(tmp, ptr, sizeof(tmp) - 1);
665 /* Handle bsrange with separate read,write values: */
666 p1 = strchr(tmp, ',');
670 p1 = strchr(tmp, '-');
672 p1 = strchr(tmp, ':');
684 if (!check_range_bytes(p1, &ul1, data) &&
685 !check_range_bytes(p2, &ul2, data)) {
688 unsigned long foo = ul1;
695 val_store(ilp, ul1, o->off1, 0, data, o);
696 val_store(ilp, ul2, o->off2, 0, data, o);
699 if (o->off3 && o->off4) {
700 val_store(ilp, ul1, o->off3, 0, data, o);
701 val_store(ilp, ul2, o->off4, 0, data, o);
705 if (o->off5 && o->off6) {
706 val_store(ilp, ul1, o->off5, 0, data, o);
707 val_store(ilp, ul2, o->off6, 0, data, o);
712 if (o->off3 && o->off4) {
713 val_store(ilp, ul1, o->off3, 0, data, o);
714 val_store(ilp, ul2, o->off4, 0, data, o);
718 if (o->off5 && o->off6) {
719 val_store(ilp, ul1, o->off5, 0, data, o);
720 val_store(ilp, ul2, o->off6, 0, data, o);
729 case FIO_OPT_STR_SET: {
730 fio_opt_int_fn *fn = o->cb;
733 ret = check_int(ptr, &il);
734 else if (o->type == FIO_OPT_BOOL)
739 dprint(FD_PARSE, " ret=%d, out=%d\n", ret, il);
744 if (o->maxval && il > (int) o->maxval) {
745 log_err("max value out of range: %d (%d max)\n",
749 if (o->minval && il < o->minval) {
750 log_err("min value out of range: %d (%d min)\n",
762 val_store(ilp, il, o->off1, 0, data, o);
765 val_store(ilp, il, o->off2, 0, data, o);
770 case FIO_OPT_DEPRECATED:
771 log_info("Option %s is deprecated\n", o->name);
775 log_err("Bad option type %u\n", o->type);
783 ret = o->verify(o, data);
785 log_err("Correct format for offending option\n");
786 log_err("%20s: %s\n", o->name, o->help);
787 show_option_help(o, 1);
794 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
796 char *o_ptr, *ptr, *ptr2;
799 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
803 o_ptr = ptr = strdup(__ptr);
806 * See if we have another set of parameters, hidden after a comma.
807 * Do this before parsing this round, to check if we should
808 * copy set 1 options to set 2.
817 (o->type != FIO_OPT_STR_STORE) &&
818 (o->type != FIO_OPT_STR) &&
819 (o->type != FIO_OPT_FLOAT_LIST)) {
820 ptr2 = strchr(ptr, ',');
821 if (ptr2 && *(ptr2 + 1) == '\0')
823 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
825 ptr2 = strchr(ptr, ':');
827 ptr2 = strchr(ptr, '-');
829 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
830 ptr2 = strchr(ptr, ':');
834 * Don't return early if parsing the first option fails - if
835 * we are doing multiple arguments, we can allow the first one
838 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
854 static struct fio_option *get_option(char *opt,
855 struct fio_option *options, char **post)
857 struct fio_option *o;
860 ret = strchr(opt, '=');
866 strip_blank_end(ret);
867 o = find_option(options, ret);
869 o = find_option(options, opt);
876 static int opt_cmp(const void *p1, const void *p2)
878 struct fio_option *o;
885 s = strdup(*((char **) p1));
886 o = get_option(s, __fio_options, &foo);
892 s = strdup(*((char **) p2));
893 o = get_option(s, __fio_options, &foo);
899 return prio2 - prio1;
902 void sort_options(char **opts, struct fio_option *options, int num_opts)
904 __fio_options = options;
905 qsort(opts, num_opts, sizeof(char *), opt_cmp);
906 __fio_options = NULL;
909 int parse_cmd_option(const char *opt, const char *val,
910 struct fio_option *options, void *data)
912 struct fio_option *o;
914 o = find_option(options, opt);
916 log_err("Bad option <%s>\n", opt);
920 if (!handle_option(o, val, data))
923 log_err("fio: failed parsing %s=%s\n", opt, val);
927 int parse_option(char *opt, const char *input,
928 struct fio_option *options, struct fio_option **o, void *data,
934 log_err("fio: failed parsing %s\n", input);
939 *o = get_option(opt, options, &post);
942 int len = strlen(opt);
943 if (opt + len + 1 != post)
944 memmove(opt + len + 1, post, strlen(post));
950 if (handle_option(*o, post, data)) {
951 log_err("fio: failed parsing %s\n", input);
958 if (!strcmp("description", (*o)->name))
963 log_info("--%s%s", (*o)->name, post ? "" : " ");
965 log_info("=%s%s%s ", delim, post, delim);
972 * Option match, levenshtein distance. Handy for not quite remembering what
973 * the option name is.
975 static int string_distance(const char *s1, const char *s2)
977 unsigned int s1_len = strlen(s1);
978 unsigned int s2_len = strlen(s2);
979 unsigned int *p, *q, *r;
982 p = malloc(sizeof(unsigned int) * (s2_len + 1));
983 q = malloc(sizeof(unsigned int) * (s2_len + 1));
986 for (i = 1; i <= s2_len; i++)
989 for (i = 1; i <= s1_len; i++) {
991 for (j = 1; j <= s2_len; j++) {
992 unsigned int sub = p[j - 1];
994 if (s1[i - 1] != s2[j - 1])
997 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
1010 static struct fio_option *find_child(struct fio_option *options,
1011 struct fio_option *o)
1013 struct fio_option *__o;
1015 for (__o = options + 1; __o->name; __o++)
1016 if (__o->parent && !strcmp(__o->parent, o->name))
1022 static void __print_option(struct fio_option *o, struct fio_option *org,
1036 p += sprintf(p, "%s", " ");
1038 sprintf(p, "%s", o->name);
1040 log_info("%-24s: %s\n", name, o->help);
1043 static void print_option(struct fio_option *o)
1045 struct fio_option *parent;
1046 struct fio_option *__o;
1047 unsigned int printed;
1050 __print_option(o, NULL, 0);
1057 while ((__o = find_child(o, parent)) != NULL) {
1058 __print_option(__o, o, level);
1067 int show_cmd_help(struct fio_option *options, const char *name)
1069 struct fio_option *o, *closest;
1070 unsigned int best_dist = -1U;
1074 if (!name || !strcmp(name, "all"))
1079 for (o = &options[0]; o->name; o++) {
1082 if (o->type == FIO_OPT_DEPRECATED)
1084 if (!exec_profile && o->prof_name)
1086 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1090 if (!strcmp(name, o->name) ||
1091 (o->alias && !strcmp(name, o->alias)))
1096 dist = string_distance(name, o->name);
1097 if (dist < best_dist) {
1104 if (show_all || match) {
1107 log_info("%20s: %s\n", o->name, o->help);
1118 show_option_help(o, 0);
1124 log_err("No such command: %s", name);
1127 * Only print an appropriately close option, one where the edit
1128 * distance isn't too big. Otherwise we get crazy matches.
1130 if (closest && best_dist < 3) {
1131 log_info(" - showing closest match\n");
1132 log_info("%20s: %s\n", closest->name, closest->help);
1133 show_option_help(closest, 0);
1141 * Handle parsing of default parameters.
1143 void fill_default_options(void *data, struct fio_option *options)
1145 struct fio_option *o;
1147 dprint(FD_PARSE, "filling default options\n");
1149 for (o = &options[0]; o->name; o++)
1151 handle_option(o, o->def, data);
1154 void option_init(struct fio_option *o)
1156 if (o->type == FIO_OPT_DEPRECATED)
1158 if (o->type == FIO_OPT_BOOL) {
1162 if (o->type == FIO_OPT_INT) {
1164 o->maxval = UINT_MAX;
1166 if (o->type == FIO_OPT_FLOAT_LIST) {
1170 if (o->type == FIO_OPT_STR_SET && o->def && !o->no_warn_def) {
1171 log_err("Option %s: string set option with"
1172 " default will always be true\n", o->name);
1174 if (!o->cb && !o->off1)
1175 log_err("Option %s: neither cb nor offset given\n", o->name);
1177 log_info("Option %s: no category defined. Setting to misc\n", o->name);
1178 o->category = FIO_OPT_C_GENERAL;
1179 o->group = FIO_OPT_G_INVALID;
1181 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1182 o->type == FIO_OPT_STR_MULTI)
1184 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
1185 log_err("Option %s: both cb and offset given\n", o->name);
1189 * Sanitize the options structure. For now it just sets min/max for bool
1190 * values and whether both callback and offsets are given.
1192 void options_init(struct fio_option *options)
1194 struct fio_option *o;
1196 dprint(FD_PARSE, "init options\n");
1198 for (o = &options[0]; o->name; o++) {
1201 o->inv_opt = find_option(options, o->inverse);
1205 void options_free(struct fio_option *options, void *data)
1207 struct fio_option *o;
1210 dprint(FD_PARSE, "free options\n");
1212 for (o = &options[0]; o->name; o++) {
1213 if (o->type != FIO_OPT_STR_STORE || !o->off1)
1216 ptr = td_var(data, o, o->off1);