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);
61 } else if (!o->posval[0].ival) {
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);
468 if (o->posval[0].ival) {
469 posval_sort(o, posval);
472 for (i = 0; i < PARSE_MAX_VP; i++) {
474 if (!vp->ival || vp->ival[0] == '\0')
476 if (vp->oval == ull) {
482 log_err("fio: value %llu not allowed:\n", ull);
483 show_option_values(o);
489 ret = fn(data, &ull);
491 if (o->type == FIO_OPT_INT) {
494 *(unsigned int *) o->roff1 = ull;
496 val_store(ilp, ull, o->off1, 0, data);
500 *(unsigned int *) o->roff2 = ull;
502 val_store(ilp, ull, o->off2, 0, data);
506 *(unsigned int *) o->roff3 = ull;
508 val_store(ilp, ull, o->off3, 0, data);
513 *(unsigned int *) o->roff2 = ull;
515 val_store(ilp, ull, o->off2, 0, data);
519 *(unsigned int *) o->roff3 = ull;
521 val_store(ilp, ull, o->off3, 0, data);
527 *(unsigned long long *) o->roff1 = ull;
529 val_store(ullp, ull, o->off1, 0, data);
533 *(unsigned long long *) o->roff2 = ull;
535 val_store(ullp, ull, o->off2, 0, data);
541 case FIO_OPT_FLOAT_LIST: {
546 ** Initialize precision to 0 and zero out list
547 ** in case specified list is shorter than default
550 ilp = td_var(data, o->off2);
553 flp = td_var(data, o->off1);
554 for(i = 0; i < o->maxlen; i++)
557 if (curr >= o->maxlen) {
558 log_err("the list exceeding max length %d\n",
562 if (!str_to_float(ptr, &uf)) {
563 log_err("not a floating point value: %s\n", ptr);
567 log_err("value out of range: %f"
568 " (range max: %f)\n", uf, o->maxfp);
572 log_err("value out of range: %f"
573 " (range min: %f)\n", uf, o->minfp);
577 flp = td_var(data, o->off1);
581 ** Calculate precision for output by counting
582 ** number of digits after period. Find first
583 ** period in entire remaining list each time
585 cp2 = strchr(ptr, '.');
589 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
592 ilp = td_var(data, o->off2);
599 case FIO_OPT_STR_STORE: {
600 fio_opt_str_fn *fn = o->cb;
602 if (o->roff1 || o->off1) {
604 cp = (char **) o->roff1;
606 cp = td_var(data, o->off1);
613 else if (o->posval[0].ival) {
614 posval_sort(o, posval);
617 for (i = 0; i < PARSE_MAX_VP; i++) {
619 if (!vp->ival || vp->ival[0] == '\0')
622 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
628 rest = strstr(*cp ?: ptr, ":");
642 show_option_values(o);
651 case FIO_OPT_RANGE: {
655 strncpy(tmp, ptr, sizeof(tmp) - 1);
657 /* Handle bsrange with separate read,write values: */
658 p1 = strchr(tmp, ',');
662 p1 = strchr(tmp, '-');
664 p1 = strchr(tmp, ':');
676 if (!check_range_bytes(p1, &ul1, data) &&
677 !check_range_bytes(p2, &ul2, data)) {
680 unsigned long foo = ul1;
688 *(unsigned int *) o->roff1 = ul1;
690 val_store(ilp, ul1, o->off1, 0, data);
692 *(unsigned int *) o->roff2 = ul2;
694 val_store(ilp, ul2, o->off2, 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);
716 if (o->roff3 && o->roff4) {
717 *(unsigned int *) o->roff3 = ul1;
718 *(unsigned int *) o->roff4 = ul2;
719 } else if (o->off3 && o->off4) {
720 val_store(ilp, ul1, o->off3, 0, data);
721 val_store(ilp, ul2, o->off4, 0, data);
725 if (o->roff5 && o->roff6) {
726 *(unsigned int *) o->roff5 = ul1;
727 *(unsigned int *) o->roff6 = ul2;
728 } else if (o->off5 && o->off6) {
729 val_store(ilp, ul1, o->off5, 0, data);
730 val_store(ilp, ul2, o->off6, 0, data);
739 case FIO_OPT_STR_SET: {
740 fio_opt_int_fn *fn = o->cb;
743 ret = check_int(ptr, &il);
744 else if (o->type == FIO_OPT_BOOL)
752 if (o->maxval && il > (int) o->maxval) {
753 log_err("max value out of range: %d (%d max)\n",
757 if (o->minval && il < o->minval) {
758 log_err("min value out of range: %d (%d min)\n",
771 *(unsigned int *)o->roff1 = il;
773 val_store(ilp, il, o->off1, 0, data);
777 *(unsigned int *) o->roff2 = il;
779 val_store(ilp, il, o->off2, 0, data);
784 case FIO_OPT_DEPRECATED:
785 log_info("Option %s is deprecated\n", o->name);
788 log_err("Bad option type %u\n", o->type);
796 ret = o->verify(o, data);
798 log_err("Correct format for offending option\n");
799 log_err("%20s: %s\n", o->name, o->help);
800 show_option_help(o, 1);
807 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
809 char *o_ptr, *ptr, *ptr2;
812 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
816 o_ptr = ptr = strdup(__ptr);
819 * See if we have another set of parameters, hidden after a comma.
820 * Do this before parsing this round, to check if we should
821 * copy set 1 options to set 2.
830 (o->type != FIO_OPT_STR_STORE) &&
831 (o->type != FIO_OPT_STR) &&
832 (o->type != FIO_OPT_FLOAT_LIST)) {
833 ptr2 = strchr(ptr, ',');
834 if (ptr2 && *(ptr2 + 1) == '\0')
836 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
838 ptr2 = strchr(ptr, ':');
840 ptr2 = strchr(ptr, '-');
842 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
843 ptr2 = strchr(ptr, ':');
847 * Don't return early if parsing the first option fails - if
848 * we are doing multiple arguments, we can allow the first one
851 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
867 static struct fio_option *get_option(char *opt,
868 struct fio_option *options, char **post)
870 struct fio_option *o;
873 ret = strchr(opt, '=');
879 strip_blank_end(ret);
880 o = find_option(options, ret);
882 o = find_option(options, opt);
889 static int opt_cmp(const void *p1, const void *p2)
891 struct fio_option *o;
898 s = strdup(*((char **) p1));
899 o = get_option(s, fio_options, &foo);
905 s = strdup(*((char **) p2));
906 o = get_option(s, fio_options, &foo);
912 return prio2 - prio1;
915 void sort_options(char **opts, struct fio_option *options, int num_opts)
917 fio_options = options;
918 qsort(opts, num_opts, sizeof(char *), opt_cmp);
922 int parse_cmd_option(const char *opt, const char *val,
923 struct fio_option *options, void *data)
925 struct fio_option *o;
927 o = find_option(options, opt);
929 log_err("Bad option <%s>\n", opt);
933 if (!handle_option(o, val, data))
936 log_err("fio: failed parsing %s=%s\n", opt, val);
940 int parse_option(char *opt, const char *input,
941 struct fio_option *options, struct fio_option **o, void *data)
946 log_err("fio: failed parsing %s\n", input);
951 *o = get_option(opt, options, &post);
954 int len = strlen(opt);
955 if (opt + len + 1 != post)
956 memmove(opt + len + 1, post, strlen(post));
962 if (!handle_option(*o, post, data))
965 log_err("fio: failed parsing %s\n", input);
970 * Option match, levenshtein distance. Handy for not quite remembering what
971 * the option name is.
973 static int string_distance(const char *s1, const char *s2)
975 unsigned int s1_len = strlen(s1);
976 unsigned int s2_len = strlen(s2);
977 unsigned int *p, *q, *r;
980 p = malloc(sizeof(unsigned int) * (s2_len + 1));
981 q = malloc(sizeof(unsigned int) * (s2_len + 1));
984 for (i = 1; i <= s2_len; i++)
987 for (i = 1; i <= s1_len; i++) {
989 for (j = 1; j <= s2_len; j++) {
990 unsigned int sub = p[j - 1];
992 if (s1[i - 1] != s2[j - 1])
995 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
1008 static struct fio_option *find_child(struct fio_option *options,
1009 struct fio_option *o)
1011 struct fio_option *__o;
1013 for (__o = options + 1; __o->name; __o++)
1014 if (__o->parent && !strcmp(__o->parent, o->name))
1020 static void __print_option(struct fio_option *o, struct fio_option *org,
1034 p += sprintf(p, "%s", " ");
1036 sprintf(p, "%s", o->name);
1038 log_info("%-24s: %s\n", name, o->help);
1041 static void print_option(struct fio_option *o)
1043 struct fio_option *parent;
1044 struct fio_option *__o;
1045 unsigned int printed;
1048 __print_option(o, NULL, 0);
1055 while ((__o = find_child(o, parent)) != NULL) {
1056 __print_option(__o, o, level);
1065 int show_cmd_help(struct fio_option *options, const char *name)
1067 struct fio_option *o, *closest;
1068 unsigned int best_dist = -1U;
1072 if (!name || !strcmp(name, "all"))
1077 for (o = &options[0]; o->name; o++) {
1080 if (o->type == FIO_OPT_DEPRECATED)
1082 if (!exec_profile && o->prof_name)
1086 if (!strcmp(name, o->name) ||
1087 (o->alias && !strcmp(name, o->alias)))
1092 dist = string_distance(name, o->name);
1093 if (dist < best_dist) {
1100 if (show_all || match) {
1103 log_info("%20s: %s\n", o->name, o->help);
1114 show_option_help(o, 0);
1120 log_err("No such command: %s", name);
1123 * Only print an appropriately close option, one where the edit
1124 * distance isn't too big. Otherwise we get crazy matches.
1126 if (closest && best_dist < 3) {
1127 log_info(" - showing closest match\n");
1128 log_info("%20s: %s\n", closest->name, closest->help);
1129 show_option_help(closest, 0);
1137 * Handle parsing of default parameters.
1139 void fill_default_options(void *data, struct fio_option *options)
1141 struct fio_option *o;
1143 dprint(FD_PARSE, "filling default options\n");
1145 for (o = &options[0]; o->name; o++)
1147 handle_option(o, o->def, data);
1150 void option_init(struct fio_option *o)
1152 if (o->type == FIO_OPT_DEPRECATED)
1154 if (o->type == FIO_OPT_BOOL) {
1158 if (o->type == FIO_OPT_INT) {
1160 o->maxval = UINT_MAX;
1162 if (o->type == FIO_OPT_FLOAT_LIST) {
1166 if (o->type == FIO_OPT_STR_SET && o->def) {
1167 log_err("Option %s: string set option with"
1168 " default will always be true\n", o->name);
1170 if (!o->cb && (!o->off1 && !o->roff1))
1171 log_err("Option %s: neither cb nor offset given\n", o->name);
1173 log_info("Option %s: no category defined. Setting to misc\n", o->name);
1174 o->category = FIO_OPT_C_GENERAL;
1175 o->group = FIO_OPT_G_INVALID;
1177 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1178 o->type == FIO_OPT_STR_MULTI)
1180 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
1181 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
1182 log_err("Option %s: both cb and offset given\n", o->name);
1187 * Sanitize the options structure. For now it just sets min/max for bool
1188 * values and whether both callback and offsets are given.
1190 void options_init(struct fio_option *options)
1192 struct fio_option *o;
1194 dprint(FD_PARSE, "init options\n");
1196 for (o = &options[0]; o->name; o++) {
1199 o->inv_opt = find_option(options, o->inverse);
1203 void options_free(struct fio_option *options, void *data)
1205 struct fio_option *o;
1208 dprint(FD_PARSE, "free options\n");
1210 for (o = &options[0]; o->name; o++) {
1211 if (o->type != FIO_OPT_STR_STORE || !o->off1)
1214 ptr = td_var(data, o->off1);