2 * This file contains the ini and command liner parser main.
19 #include "lib/ieee754.h"
21 static struct fio_option *fio_options;
22 extern unsigned int fio_get_kb_base(void *);
24 static int vp_cmp(const void *p1, const void *p2)
26 const struct value_pair *vp1 = p1;
27 const struct value_pair *vp2 = p2;
29 return strlen(vp2->ival) - strlen(vp1->ival);
32 static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
34 const struct value_pair *vp;
37 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
39 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
40 vp = &o->posval[entries];
41 if (!vp->ival || vp->ival[0] == '\0')
44 memcpy(&vpmap[entries], vp, sizeof(*vp));
47 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
50 static void show_option_range(struct fio_option *o,
51 int (*logger)(const char *format, ...))
53 if (o->type == FIO_OPT_FLOAT_LIST){
54 if (o->minfp == DBL_MIN && o->maxfp == DBL_MAX)
57 logger("%20s: min=%f", "range", o->minfp);
58 if (o->maxfp != DBL_MAX)
59 logger(", max=%f", o->maxfp);
62 if (!o->minval && !o->maxval)
65 logger("%20s: min=%d", "range", o->minval);
67 logger(", max=%d", o->maxval);
72 static void show_option_values(struct fio_option *o)
76 for (i = 0; i < PARSE_MAX_VP; i++) {
77 const struct value_pair *vp = &o->posval[i];
82 log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
84 log_info(" %s", vp->help);
92 static void show_option_help(struct fio_option *o, int is_err)
94 const char *typehelp[] = {
98 "string with possible k/m/g postfix (opt=4k)",
99 "string with time postfix (opt=10s)",
101 "string with dual range (opt=1k-4k,4k-8k)",
102 "integer value (opt=100)",
103 "boolean value (opt=1)",
104 "list of floating point values separated by ':' (opt=5.9:7.8)",
108 int (*logger)(const char *format, ...);
116 logger("%20s: %s\n", "alias", o->alias);
118 logger("%20s: %s\n", "type", typehelp[o->type]);
119 logger("%20s: %s\n", "default", o->def ? o->def : "no default");
121 logger("%20s: only for profile '%s'\n", "valid", o->prof_name);
122 show_option_range(o, logger);
123 show_option_values(o);
126 static unsigned long get_mult_time(char c)
143 static int is_separator(char c)
156 static unsigned long long __get_mult_bytes(const char *p, void *data,
159 unsigned int kb_base = fio_get_kb_base(data);
160 unsigned long long ret = 1;
161 unsigned int i, pow = 0, mult = kb_base;
169 for (i = 0; i < strlen(c); i++) {
170 c[i] = tolower(c[i]);
171 if (is_separator(c[i])) {
177 if (!strcmp("pib", c)) {
180 } else if (!strcmp("tib", c)) {
183 } else if (!strcmp("gib", c)) {
186 } else if (!strcmp("mib", c)) {
189 } else if (!strcmp("kib", c)) {
192 } else if (!strcmp("p", c) || !strcmp("pb", c))
194 else if (!strcmp("t", c) || !strcmp("tb", c))
196 else if (!strcmp("g", c) || !strcmp("gb", c))
198 else if (!strcmp("m", c) || !strcmp("mb", c))
200 else if (!strcmp("k", c) || !strcmp("kb", c))
202 else if (!strcmp("%", c)) {
209 ret *= (unsigned long long) mult;
215 static unsigned long long get_mult_bytes(const char *str, int len, void *data,
222 return __get_mult_bytes(str, data, percent);
225 * Go forward until we hit a non-digit, or +/- sign
227 while ((p - str) <= len) {
228 if (!isdigit((int) *p) &&
229 (((*p != '+') && (*p != '-')) || digit_seen))
231 digit_seen |= isdigit((int) *p);
235 if (!isalpha((int) *p) && (*p != '%'))
238 return __get_mult_bytes(p, data, percent);
242 * Convert string into a floating number. Return 1 for success and 0 otherwise.
244 int str_to_float(const char *str, double *val)
246 return (1 == sscanf(str, "%lf", val));
250 * convert string into decimal value, noting any size suffix
252 int str_to_decimal(const char *str, long long *val, int kilo, void *data)
260 if (strstr(str, "0x") || strstr(str, "0X"))
265 *val = strtoll(str, NULL, base);
266 if (*val == LONG_MAX && errno == ERANGE)
270 unsigned long long mult;
273 mult = get_mult_bytes(str, len, data, &perc);
279 *val *= get_mult_time(str[len - 1]);
284 static int check_str_bytes(const char *p, long long *val, void *data)
286 return str_to_decimal(p, val, 1, data);
289 static int check_str_time(const char *p, long long *val)
291 return str_to_decimal(p, val, 0, NULL);
294 void strip_blank_front(char **p)
300 while (isspace((int) *s))
306 void strip_blank_end(char *p)
323 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
329 static int check_range_bytes(const char *str, long *val, void *data)
333 if (!str_to_decimal(str, &__val, 1, data)) {
341 static int check_int(const char *p, int *val)
345 if (strstr(p, "0x") || strstr(p, "0X")) {
346 if (sscanf(p, "%x", val) == 1)
349 if (sscanf(p, "%u", val) == 1)
356 static int opt_len(const char *str)
360 postfix = strchr(str, ':');
364 return (int)(postfix - str);
367 static int str_match_len(const struct value_pair *vp, const char *str)
369 return max(strlen(vp->ival), opt_len(str));
372 #define val_store(ptr, val, off, or, data) \
374 ptr = td_var((data), (off)); \
381 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
382 int first, int more, int curr)
386 long long ull, *ullp;
390 int ret = 0, is_time = 0;
391 const struct value_pair *vp;
392 struct value_pair posval[PARSE_MAX_VP];
393 int i, all_skipped = 1;
395 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
398 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
399 log_err("Option %s requires an argument\n", o->name);
405 case FIO_OPT_STR_MULTI: {
406 fio_opt_str_fn *fn = o->cb;
408 posval_sort(o, posval);
411 for (i = 0; i < PARSE_MAX_VP; i++) {
413 if (!vp->ival || vp->ival[0] == '\0')
416 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
420 *(unsigned int *) o->roff1 |= vp->oval;
422 *(unsigned int *) o->roff1 = vp->oval;
426 val_store(ilp, vp->oval, o->off1, vp->or, data);
432 if (ret && !all_skipped)
433 show_option_values(o);
438 case FIO_OPT_STR_VAL_TIME:
441 case FIO_OPT_STR_VAL: {
442 fio_opt_str_val_fn *fn = o->cb;
445 strncpy(tmp, ptr, sizeof(tmp) - 1);
446 p = strchr(tmp, ',');
451 ret = check_str_time(tmp, &ull);
453 ret = check_str_bytes(tmp, &ull, data);
458 if (o->maxval && ull > o->maxval) {
459 log_err("max value out of range: %llu"
460 " (%u max)\n", ull, o->maxval);
463 if (o->minval && ull < o->minval) {
464 log_err("min value out of range: %llu"
465 " (%u min)\n", ull, o->minval);
470 ret = fn(data, &ull);
472 if (o->type == FIO_OPT_INT) {
475 *(unsigned int *) o->roff1 = ull;
477 val_store(ilp, ull, o->off1, 0, data);
481 *(unsigned int *) o->roff2 = ull;
483 val_store(ilp, ull, o->off2, 0, data);
487 *(unsigned int *) o->roff3 = ull;
489 val_store(ilp, ull, o->off3, 0, data);
494 *(unsigned int *) o->roff2 = ull;
496 val_store(ilp, ull, o->off2, 0, data);
500 *(unsigned int *) o->roff3 = ull;
502 val_store(ilp, ull, o->off3, 0, data);
508 *(unsigned long long *) o->roff1 = ull;
510 val_store(ullp, ull, o->off1, 0, data);
514 *(unsigned long long *) o->roff2 = ull;
516 val_store(ullp, ull, o->off2, 0, data);
522 case FIO_OPT_FLOAT_LIST: {
527 ** Initialize precision to 0 and zero out list
528 ** in case specified list is shorter than default
531 ilp = td_var(data, o->off2);
534 flp = td_var(data, o->off1);
535 for(i = 0; i < o->maxlen; i++)
538 if (curr >= o->maxlen) {
539 log_err("the list exceeding max length %d\n",
543 if (!str_to_float(ptr, &uf)) {
544 log_err("not a floating point value: %s\n", ptr);
548 log_err("value out of range: %f"
549 " (range max: %f)\n", uf, o->maxfp);
553 log_err("value out of range: %f"
554 " (range min: %f)\n", uf, o->minfp);
558 flp = td_var(data, o->off1);
562 ** Calculate precision for output by counting
563 ** number of digits after period. Find first
564 ** period in entire remaining list each time
566 cp2 = strchr(ptr, '.');
570 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
573 ilp = td_var(data, o->off2);
580 case FIO_OPT_STR_STORE: {
581 fio_opt_str_fn *fn = o->cb;
583 if (o->roff1 || o->off1) {
585 cp = (char **) o->roff1;
587 cp = td_var(data, o->off1);
594 else if (o->posval[0].ival) {
595 posval_sort(o, posval);
598 for (i = 0; i < PARSE_MAX_VP; i++) {
600 if (!vp->ival || vp->ival[0] == '\0')
603 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
609 rest = strstr(*cp ?: ptr, ":");
623 show_option_values(o);
632 case FIO_OPT_RANGE: {
636 strncpy(tmp, ptr, sizeof(tmp) - 1);
638 /* Handle bsrange with separate read,write values: */
639 p1 = strchr(tmp, ',');
643 p1 = strchr(tmp, '-');
645 p1 = strchr(tmp, ':');
657 if (!check_range_bytes(p1, &ul1, data) &&
658 !check_range_bytes(p2, &ul2, data)) {
661 unsigned long foo = ul1;
669 *(unsigned int *) o->roff1 = ul1;
671 val_store(ilp, ul1, o->off1, 0, data);
673 *(unsigned int *) o->roff2 = ul2;
675 val_store(ilp, ul2, o->off2, 0, data);
678 if (o->roff3 && o->roff4) {
679 *(unsigned int *) o->roff3 = ul1;
680 *(unsigned int *) o->roff4 = ul2;
681 } else if (o->off3 && o->off4) {
682 val_store(ilp, ul1, o->off3, 0, data);
683 val_store(ilp, ul2, o->off4, 0, data);
687 if (o->roff5 && o->roff6) {
688 *(unsigned int *) o->roff5 = ul1;
689 *(unsigned int *) o->roff6 = ul2;
690 } else if (o->off5 && o->off6) {
691 val_store(ilp, ul1, o->off5, 0, data);
692 val_store(ilp, ul2, o->off6, 0, data);
697 if (o->roff3 && o->roff4) {
698 *(unsigned int *) o->roff3 = ul1;
699 *(unsigned int *) o->roff4 = ul2;
700 } else if (o->off3 && o->off4) {
701 val_store(ilp, ul1, o->off3, 0, data);
702 val_store(ilp, ul2, o->off4, 0, data);
706 if (o->roff5 && o->roff6) {
707 *(unsigned int *) o->roff5 = ul1;
708 *(unsigned int *) o->roff6 = ul2;
709 } else if (o->off5 && o->off6) {
710 val_store(ilp, ul1, o->off5, 0, data);
711 val_store(ilp, ul2, o->off6, 0, data);
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)
733 if (o->maxval && il > (int) o->maxval) {
734 log_err("max value out of range: %d (%d max)\n",
738 if (o->minval && il < o->minval) {
739 log_err("min value out of range: %d (%d min)\n",
752 *(unsigned int *)o->roff1 = il;
754 val_store(ilp, il, o->off1, 0, data);
758 *(unsigned int *) o->roff2 = il;
760 val_store(ilp, il, o->off2, 0, data);
765 case FIO_OPT_DEPRECATED:
766 log_info("Option %s is deprecated\n", o->name);
769 log_err("Bad option type %u\n", o->type);
777 ret = o->verify(o, data);
779 log_err("Correct format for offending option\n");
780 log_err("%20s: %s\n", o->name, o->help);
781 show_option_help(o, 1);
788 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
790 char *o_ptr, *ptr, *ptr2;
793 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
797 o_ptr = ptr = strdup(__ptr);
800 * See if we have another set of parameters, hidden after a comma.
801 * Do this before parsing this round, to check if we should
802 * copy set 1 options to set 2.
811 (o->type != FIO_OPT_STR_STORE) &&
812 (o->type != FIO_OPT_STR) &&
813 (o->type != FIO_OPT_FLOAT_LIST)) {
814 ptr2 = strchr(ptr, ',');
815 if (ptr2 && *(ptr2 + 1) == '\0')
817 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
819 ptr2 = strchr(ptr, ':');
821 ptr2 = strchr(ptr, '-');
823 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
824 ptr2 = strchr(ptr, ':');
828 * Don't return early if parsing the first option fails - if
829 * we are doing multiple arguments, we can allow the first one
832 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
848 static struct fio_option *get_option(char *opt,
849 struct fio_option *options, char **post)
851 struct fio_option *o;
854 ret = strchr(opt, '=');
860 strip_blank_end(ret);
861 o = find_option(options, ret);
863 o = find_option(options, opt);
870 static int opt_cmp(const void *p1, const void *p2)
872 struct fio_option *o;
879 s = strdup(*((char **) p1));
880 o = get_option(s, fio_options, &foo);
886 s = strdup(*((char **) p2));
887 o = get_option(s, fio_options, &foo);
893 return prio2 - prio1;
896 void sort_options(char **opts, struct fio_option *options, int num_opts)
898 fio_options = options;
899 qsort(opts, num_opts, sizeof(char *), opt_cmp);
903 int parse_cmd_option(const char *opt, const char *val,
904 struct fio_option *options, void *data)
906 struct fio_option *o;
908 o = find_option(options, opt);
910 log_err("Bad option <%s>\n", opt);
914 if (!handle_option(o, val, data))
917 log_err("fio: failed parsing %s=%s\n", opt, val);
921 int parse_option(char *opt, const char *input,
922 struct fio_option *options, struct fio_option **o, void *data)
927 log_err("fio: failed parsing %s\n", input);
932 *o = get_option(opt, options, &post);
935 int len = strlen(opt);
936 if (opt + len + 1 != post)
937 memmove(opt + len + 1, post, strlen(post));
943 if (!handle_option(*o, post, data)) {
947 log_err("fio: failed parsing %s\n", input);
952 * Option match, levenshtein distance. Handy for not quite remembering what
953 * the option name is.
955 static int string_distance(const char *s1, const char *s2)
957 unsigned int s1_len = strlen(s1);
958 unsigned int s2_len = strlen(s2);
959 unsigned int *p, *q, *r;
962 p = malloc(sizeof(unsigned int) * (s2_len + 1));
963 q = malloc(sizeof(unsigned int) * (s2_len + 1));
966 for (i = 1; i <= s2_len; i++)
969 for (i = 1; i <= s1_len; i++) {
971 for (j = 1; j <= s2_len; j++) {
972 unsigned int sub = p[j - 1];
974 if (s1[i - 1] != s2[j - 1])
977 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
990 static struct fio_option *find_child(struct fio_option *options,
991 struct fio_option *o)
993 struct fio_option *__o;
995 for (__o = options + 1; __o->name; __o++)
996 if (__o->parent && !strcmp(__o->parent, o->name))
1002 static void __print_option(struct fio_option *o, struct fio_option *org,
1016 p += sprintf(p, "%s", " ");
1018 sprintf(p, "%s", o->name);
1020 log_info("%-24s: %s\n", name, o->help);
1023 static void print_option(struct fio_option *o)
1025 struct fio_option *parent;
1026 struct fio_option *__o;
1027 unsigned int printed;
1030 __print_option(o, NULL, 0);
1037 while ((__o = find_child(o, parent)) != NULL) {
1038 __print_option(__o, o, level);
1047 int show_cmd_help(struct fio_option *options, const char *name)
1049 struct fio_option *o, *closest;
1050 unsigned int best_dist = -1U;
1054 if (!name || !strcmp(name, "all"))
1059 for (o = &options[0]; o->name; o++) {
1062 if (o->type == FIO_OPT_DEPRECATED)
1064 if (!exec_profile && o->prof_name)
1068 if (!strcmp(name, o->name) ||
1069 (o->alias && !strcmp(name, o->alias)))
1074 dist = string_distance(name, o->name);
1075 if (dist < best_dist) {
1082 if (show_all || match) {
1085 log_info("%20s: %s\n", o->name, o->help);
1096 show_option_help(o, 0);
1102 log_err("No such command: %s", name);
1105 * Only print an appropriately close option, one where the edit
1106 * distance isn't too big. Otherwise we get crazy matches.
1108 if (closest && best_dist < 3) {
1109 log_info(" - showing closest match\n");
1110 log_info("%20s: %s\n", closest->name, closest->help);
1111 show_option_help(closest, 0);
1119 * Handle parsing of default parameters.
1121 void fill_default_options(void *data, struct fio_option *options)
1123 struct fio_option *o;
1125 dprint(FD_PARSE, "filling default options\n");
1127 for (o = &options[0]; o->name; o++)
1129 handle_option(o, o->def, data);
1132 void option_init(struct fio_option *o)
1134 if (o->type == FIO_OPT_DEPRECATED)
1136 if (o->type == FIO_OPT_BOOL) {
1140 if (o->type == FIO_OPT_INT) {
1142 o->maxval = UINT_MAX;
1144 if (o->type == FIO_OPT_FLOAT_LIST) {
1148 if (o->type == FIO_OPT_STR_SET && o->def) {
1149 log_err("Option %s: string set option with"
1150 " default will always be true\n", o->name);
1152 if (!o->cb && (!o->off1 && !o->roff1))
1153 log_err("Option %s: neither cb nor offset given\n", o->name);
1154 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1155 o->type == FIO_OPT_STR_MULTI)
1157 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
1158 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
1159 log_err("Option %s: both cb and offset given\n", o->name);
1164 * Sanitize the options structure. For now it just sets min/max for bool
1165 * values and whether both callback and offsets are given.
1167 void options_init(struct fio_option *options)
1169 struct fio_option *o;
1171 dprint(FD_PARSE, "init options\n");
1173 for (o = &options[0]; o->name; o++)
1177 void options_free(struct fio_option *options, void *data)
1179 struct fio_option *o;
1182 dprint(FD_PARSE, "free options\n");
1184 for (o = &options[0]; o->name; o++) {
1185 if (o->type != FIO_OPT_STR_STORE || !o->off1)
1188 ptr = td_var(data, o->off1);