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);
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("value %d not in allowed range\n",ull);
488 ret = fn(data, &ull);
490 if (o->type == FIO_OPT_INT) {
493 *(unsigned int *) o->roff1 = ull;
495 val_store(ilp, ull, o->off1, 0, data);
499 *(unsigned int *) o->roff2 = ull;
501 val_store(ilp, ull, o->off2, 0, data);
505 *(unsigned int *) o->roff3 = ull;
507 val_store(ilp, ull, o->off3, 0, data);
512 *(unsigned int *) o->roff2 = ull;
514 val_store(ilp, ull, o->off2, 0, data);
518 *(unsigned int *) o->roff3 = ull;
520 val_store(ilp, ull, o->off3, 0, data);
526 *(unsigned long long *) o->roff1 = ull;
528 val_store(ullp, ull, o->off1, 0, data);
532 *(unsigned long long *) o->roff2 = ull;
534 val_store(ullp, ull, o->off2, 0, data);
540 case FIO_OPT_FLOAT_LIST: {
545 ** Initialize precision to 0 and zero out list
546 ** in case specified list is shorter than default
549 ilp = td_var(data, o->off2);
552 flp = td_var(data, 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->off1);
580 ** Calculate precision for output by counting
581 ** number of digits after period. Find first
582 ** period in entire remaining list each time
584 cp2 = strchr(ptr, '.');
588 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
591 ilp = td_var(data, o->off2);
598 case FIO_OPT_STR_STORE: {
599 fio_opt_str_fn *fn = o->cb;
601 if (o->roff1 || o->off1) {
603 cp = (char **) o->roff1;
605 cp = td_var(data, 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;
687 *(unsigned int *) o->roff1 = ul1;
689 val_store(ilp, ul1, o->off1, 0, data);
691 *(unsigned int *) o->roff2 = ul2;
693 val_store(ilp, ul2, o->off2, 0, data);
696 if (o->roff3 && o->roff4) {
697 *(unsigned int *) o->roff3 = ul1;
698 *(unsigned int *) o->roff4 = ul2;
699 } else if (o->off3 && o->off4) {
700 val_store(ilp, ul1, o->off3, 0, data);
701 val_store(ilp, ul2, o->off4, 0, data);
705 if (o->roff5 && o->roff6) {
706 *(unsigned int *) o->roff5 = ul1;
707 *(unsigned int *) o->roff6 = ul2;
708 } else if (o->off5 && o->off6) {
709 val_store(ilp, ul1, o->off5, 0, data);
710 val_store(ilp, ul2, o->off6, 0, data);
715 if (o->roff3 && o->roff4) {
716 *(unsigned int *) o->roff3 = ul1;
717 *(unsigned int *) o->roff4 = ul2;
718 } else if (o->off3 && o->off4) {
719 val_store(ilp, ul1, o->off3, 0, data);
720 val_store(ilp, ul2, o->off4, 0, data);
724 if (o->roff5 && o->roff6) {
725 *(unsigned int *) o->roff5 = ul1;
726 *(unsigned int *) o->roff6 = ul2;
727 } else if (o->off5 && o->off6) {
728 val_store(ilp, ul1, o->off5, 0, data);
729 val_store(ilp, ul2, o->off6, 0, data);
738 case FIO_OPT_STR_SET: {
739 fio_opt_int_fn *fn = o->cb;
742 ret = check_int(ptr, &il);
743 else if (o->type == FIO_OPT_BOOL)
751 if (o->maxval && il > (int) o->maxval) {
752 log_err("max value out of range: %d (%d max)\n",
756 if (o->minval && il < o->minval) {
757 log_err("min value out of range: %d (%d min)\n",
770 *(unsigned int *)o->roff1 = il;
772 val_store(ilp, il, o->off1, 0, data);
776 *(unsigned int *) o->roff2 = il;
778 val_store(ilp, il, o->off2, 0, data);
783 case FIO_OPT_DEPRECATED:
784 log_info("Option %s is deprecated\n", o->name);
787 log_err("Bad option type %u\n", o->type);
795 ret = o->verify(o, data);
797 log_err("Correct format for offending option\n");
798 log_err("%20s: %s\n", o->name, o->help);
799 show_option_help(o, 1);
806 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
808 char *o_ptr, *ptr, *ptr2;
811 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
815 o_ptr = ptr = strdup(__ptr);
818 * See if we have another set of parameters, hidden after a comma.
819 * Do this before parsing this round, to check if we should
820 * copy set 1 options to set 2.
829 (o->type != FIO_OPT_STR_STORE) &&
830 (o->type != FIO_OPT_STR) &&
831 (o->type != FIO_OPT_FLOAT_LIST)) {
832 ptr2 = strchr(ptr, ',');
833 if (ptr2 && *(ptr2 + 1) == '\0')
835 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
837 ptr2 = strchr(ptr, ':');
839 ptr2 = strchr(ptr, '-');
841 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
842 ptr2 = strchr(ptr, ':');
846 * Don't return early if parsing the first option fails - if
847 * we are doing multiple arguments, we can allow the first one
850 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
866 static struct fio_option *get_option(char *opt,
867 struct fio_option *options, char **post)
869 struct fio_option *o;
872 ret = strchr(opt, '=');
878 strip_blank_end(ret);
879 o = find_option(options, ret);
881 o = find_option(options, opt);
888 static int opt_cmp(const void *p1, const void *p2)
890 struct fio_option *o;
897 s = strdup(*((char **) p1));
898 o = get_option(s, fio_options, &foo);
904 s = strdup(*((char **) p2));
905 o = get_option(s, fio_options, &foo);
911 return prio2 - prio1;
914 void sort_options(char **opts, struct fio_option *options, int num_opts)
916 fio_options = options;
917 qsort(opts, num_opts, sizeof(char *), opt_cmp);
921 int parse_cmd_option(const char *opt, const char *val,
922 struct fio_option *options, void *data)
924 struct fio_option *o;
926 o = find_option(options, opt);
928 log_err("Bad option <%s>\n", opt);
932 if (!handle_option(o, val, data))
935 log_err("fio: failed parsing %s=%s\n", opt, val);
939 int parse_option(char *opt, const char *input,
940 struct fio_option *options, struct fio_option **o, void *data)
945 log_err("fio: failed parsing %s\n", input);
950 *o = get_option(opt, options, &post);
953 int len = strlen(opt);
954 if (opt + len + 1 != post)
955 memmove(opt + len + 1, post, strlen(post));
961 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);
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 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
1177 log_err("Option %s: both cb and offset given\n", o->name);
1182 * Sanitize the options structure. For now it just sets min/max for bool
1183 * values and whether both callback and offsets are given.
1185 void options_init(struct fio_option *options)
1187 struct fio_option *o;
1189 dprint(FD_PARSE, "init options\n");
1191 for (o = &options[0]; o->name; o++)
1195 void options_free(struct fio_option *options, void *data)
1197 struct fio_option *o;
1200 dprint(FD_PARSE, "free options\n");
1202 for (o = &options[0]; o->name; o++) {
1203 if (o->type != FIO_OPT_STR_STORE || !o->off1)
1206 ptr = td_var(data, o->off1);