2 * This file contains the ini and command liner parser main.
18 static struct fio_option *fio_options;
19 extern unsigned int fio_get_kb_base(void *);
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 (!strcmp("pib", c)) {
159 } else if (!strcmp("tib", c)) {
162 } else if (!strcmp("gib", c)) {
165 } else if (!strcmp("mib", c)) {
168 } else if (!strcmp("kib", c)) {
171 } else if (!strcmp("p", c) || !strcmp("pb", c))
173 else if (!strcmp("t", c) || !strcmp("tb", c))
175 else if (!strcmp("g", c) || !strcmp("gb", c))
177 else if (!strcmp("m", c) || !strcmp("mb", c))
179 else if (!strcmp("k", c) || !strcmp("kb", c))
181 else if (!strcmp("%", c)) {
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 static 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 static 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);
544 else if (o->posval[0].ival) {
545 posval_sort(o, posval);
548 for (i = 0; i < PARSE_MAX_VP; i++) {
550 if (!vp->ival || vp->ival[0] == '\0')
553 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
559 rest = strstr(*cp ?: ptr, ":");
573 show_option_values(o);
582 case FIO_OPT_RANGE: {
586 strncpy(tmp, ptr, sizeof(tmp) - 1);
588 /* Handle bsrange with separate read,write values: */
589 p1 = strchr(tmp, ',');
593 p1 = strchr(tmp, '-');
595 p1 = strchr(tmp, ':');
607 if (!check_range_bytes(p1, &ul1, data) &&
608 !check_range_bytes(p2, &ul2, data)) {
611 unsigned long foo = ul1;
619 *(unsigned int *) o->roff1 = ul1;
621 val_store(ilp, ul1, o->off1, 0, data);
623 *(unsigned int *) o->roff2 = ul2;
625 val_store(ilp, ul2, o->off2, 0, data);
628 if (o->roff3 && o->roff4) {
629 *(unsigned int *) o->roff3 = ul1;
630 *(unsigned int *) o->roff4 = ul2;
631 } else if (o->off3 && o->off4) {
632 val_store(ilp, ul1, o->off3, 0, data);
633 val_store(ilp, ul2, o->off4, 0, data);
637 if (o->roff5 && o->roff6) {
638 *(unsigned int *) o->roff5 = ul1;
639 *(unsigned int *) o->roff6 = ul2;
640 } else if (o->off5 && o->off6) {
641 val_store(ilp, ul1, o->off5, 0, data);
642 val_store(ilp, ul2, o->off6, 0, data);
647 if (o->roff3 && o->roff4) {
648 *(unsigned int *) o->roff3 = ul1;
649 *(unsigned int *) o->roff4 = ul2;
650 } else if (o->off3 && o->off4) {
651 val_store(ilp, ul1, o->off3, 0, data);
652 val_store(ilp, ul2, o->off4, 0, data);
656 if (o->roff5 && o->roff6) {
657 *(unsigned int *) o->roff5 = ul1;
658 *(unsigned int *) o->roff6 = ul2;
659 } else if (o->off5 && o->off6) {
660 val_store(ilp, ul1, o->off5, 0, data);
661 val_store(ilp, ul2, o->off6, 0, data);
670 case FIO_OPT_STR_SET: {
671 fio_opt_int_fn *fn = o->cb;
674 ret = check_int(ptr, &il);
675 else if (o->type == FIO_OPT_BOOL)
683 if (o->maxval && il > (int) o->maxval) {
684 log_err("max value out of range: %d (%d max)\n",
688 if (o->minval && il < o->minval) {
689 log_err("min value out of range: %d (%d min)\n",
702 *(unsigned int *)o->roff1 = il;
704 val_store(ilp, il, o->off1, 0, data);
708 *(unsigned int *) o->roff2 = il;
710 val_store(ilp, il, o->off2, 0, data);
715 case FIO_OPT_DEPRECATED:
716 log_info("Option %s is deprecated\n", o->name);
719 log_err("Bad option type %u\n", o->type);
727 ret = o->verify(o, data);
729 log_err("Correct format for offending option\n");
730 log_err("%20s: %s\n", o->name, o->help);
731 show_option_help(o, 1);
738 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
740 char *o_ptr, *ptr, *ptr2;
743 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
747 o_ptr = ptr = strdup(__ptr);
750 * See if we have another set of parameters, hidden after a comma.
751 * Do this before parsing this round, to check if we should
752 * copy set 1 options to set 2.
761 (o->type != FIO_OPT_STR_STORE) &&
762 (o->type != FIO_OPT_STR) &&
763 (o->type != FIO_OPT_FLOAT_LIST)) {
764 ptr2 = strchr(ptr, ',');
765 if (ptr2 && *(ptr2 + 1) == '\0')
767 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
769 ptr2 = strchr(ptr, ':');
771 ptr2 = strchr(ptr, '-');
773 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
774 ptr2 = strchr(ptr, ':');
778 * Don't return early if parsing the first option fails - if
779 * we are doing multiple arguments, we can allow the first one
782 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
798 static struct fio_option *get_option(char *opt,
799 struct fio_option *options, char **post)
801 struct fio_option *o;
804 ret = strchr(opt, '=');
810 strip_blank_end(ret);
811 o = find_option(options, ret);
813 o = find_option(options, opt);
820 static int opt_cmp(const void *p1, const void *p2)
822 struct fio_option *o;
829 s = strdup(*((char **) p1));
830 o = get_option(s, fio_options, &foo);
836 s = strdup(*((char **) p2));
837 o = get_option(s, fio_options, &foo);
843 return prio2 - prio1;
846 void sort_options(char **opts, struct fio_option *options, int num_opts)
848 fio_options = options;
849 qsort(opts, num_opts, sizeof(char *), opt_cmp);
853 int parse_cmd_option(const char *opt, const char *val,
854 struct fio_option *options, void *data)
856 struct fio_option *o;
858 o = find_option(options, opt);
860 log_err("Bad option <%s>\n", opt);
864 if (!handle_option(o, val, data))
867 log_err("fio: failed parsing %s=%s\n", opt, val);
871 int parse_option(char *opt, const char *input,
872 struct fio_option *options, struct fio_option **o, void *data)
877 log_err("fio: failed parsing %s\n", input);
882 *o = get_option(opt, options, &post);
885 int len = strlen(opt);
886 if (opt + len + 1 != post)
887 memmove(opt + len + 1, post, strlen(post));
893 if (!handle_option(*o, post, data)) {
897 log_err("fio: failed parsing %s\n", input);
902 * Option match, levenshtein distance. Handy for not quite remembering what
903 * the option name is.
905 static int string_distance(const char *s1, const char *s2)
907 unsigned int s1_len = strlen(s1);
908 unsigned int s2_len = strlen(s2);
909 unsigned int *p, *q, *r;
912 p = malloc(sizeof(unsigned int) * (s2_len + 1));
913 q = malloc(sizeof(unsigned int) * (s2_len + 1));
916 for (i = 1; i <= s2_len; i++)
919 for (i = 1; i <= s1_len; i++) {
921 for (j = 1; j <= s2_len; j++) {
922 unsigned int sub = p[j - 1];
924 if (s1[i - 1] != s2[j - 1])
927 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
940 static struct fio_option *find_child(struct fio_option *options,
941 struct fio_option *o)
943 struct fio_option *__o;
945 for (__o = options + 1; __o->name; __o++)
946 if (__o->parent && !strcmp(__o->parent, o->name))
952 static void __print_option(struct fio_option *o, struct fio_option *org,
966 p += sprintf(p, "%s", " ");
968 sprintf(p, "%s", o->name);
970 log_info("%-24s: %s\n", name, o->help);
973 static void print_option(struct fio_option *o)
975 struct fio_option *parent;
976 struct fio_option *__o;
977 unsigned int printed;
980 __print_option(o, NULL, 0);
987 while ((__o = find_child(o, parent)) != NULL) {
988 __print_option(__o, o, level);
997 int show_cmd_help(struct fio_option *options, const char *name)
999 struct fio_option *o, *closest;
1000 unsigned int best_dist = -1U;
1004 if (!name || !strcmp(name, "all"))
1009 for (o = &options[0]; o->name; o++) {
1012 if (o->type == FIO_OPT_DEPRECATED)
1014 if (!exec_profile && o->prof_name)
1018 if (!strcmp(name, o->name) ||
1019 (o->alias && !strcmp(name, o->alias)))
1024 dist = string_distance(name, o->name);
1025 if (dist < best_dist) {
1032 if (show_all || match) {
1035 log_info("%20s: %s\n", o->name, o->help);
1046 show_option_help(o, 0);
1052 log_err("No such command: %s", name);
1055 * Only print an appropriately close option, one where the edit
1056 * distance isn't too big. Otherwise we get crazy matches.
1058 if (closest && best_dist < 3) {
1059 log_info(" - showing closest match\n");
1060 log_info("%20s: %s\n", closest->name, closest->help);
1061 show_option_help(closest, 0);
1069 * Handle parsing of default parameters.
1071 void fill_default_options(void *data, struct fio_option *options)
1073 struct fio_option *o;
1075 dprint(FD_PARSE, "filling default options\n");
1077 for (o = &options[0]; o->name; o++)
1079 handle_option(o, o->def, data);
1082 void option_init(struct fio_option *o)
1084 if (o->type == FIO_OPT_DEPRECATED)
1086 if (o->type == FIO_OPT_BOOL) {
1090 if (o->type == FIO_OPT_INT) {
1092 o->maxval = UINT_MAX;
1094 if (o->type == FIO_OPT_FLOAT_LIST) {
1098 if (o->type == FIO_OPT_STR_SET && o->def) {
1099 log_err("Option %s: string set option with"
1100 " default will always be true\n", o->name);
1102 if (!o->cb && (!o->off1 && !o->roff1))
1103 log_err("Option %s: neither cb nor offset given\n", o->name);
1104 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1105 o->type == FIO_OPT_STR_MULTI)
1107 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
1108 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
1109 log_err("Option %s: both cb and offset given\n", o->name);
1114 * Sanitize the options structure. For now it just sets min/max for bool
1115 * values and whether both callback and offsets are given.
1117 void options_init(struct fio_option *options)
1119 struct fio_option *o;
1121 dprint(FD_PARSE, "init options\n");
1123 for (o = &options[0]; o->name; o++)
1127 void options_free(struct fio_option *options, void *data)
1129 struct fio_option *o;
1132 dprint(FD_PARSE, "free options\n");
1134 for (o = &options[0]; o->name; o++) {
1135 if (o->type != FIO_OPT_STR_STORE || !o->off1)
1138 ptr = td_var(data, o->off1);