2 * This file contains the ini and command liner parser main.
19 #include "lib/ieee754.h"
22 #ifdef CONFIG_ARITHMETIC
26 static struct fio_option *__fio_options;
28 static int vp_cmp(const void *p1, const void *p2)
30 const struct value_pair *vp1 = p1;
31 const struct value_pair *vp2 = p2;
33 return strlen(vp2->ival) - strlen(vp1->ival);
36 static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
38 const struct value_pair *vp;
41 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
43 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
44 vp = &o->posval[entries];
45 if (!vp->ival || vp->ival[0] == '\0')
48 memcpy(&vpmap[entries], vp, sizeof(*vp));
51 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
54 static void show_option_range(struct fio_option *o,
55 int (*logger)(const char *format, ...))
57 if (o->type == FIO_OPT_FLOAT_LIST) {
58 if (o->minfp == DBL_MIN && o->maxfp == DBL_MAX)
61 logger("%20s: min=%f", "range", o->minfp);
62 if (o->maxfp != DBL_MAX)
63 logger(", max=%f", o->maxfp);
65 } else if (!o->posval[0].ival) {
66 if (!o->minval && !o->maxval)
69 logger("%20s: min=%d", "range", o->minval);
71 logger(", max=%d", o->maxval);
76 static void show_option_values(struct fio_option *o)
80 for (i = 0; i < PARSE_MAX_VP; i++) {
81 const struct value_pair *vp = &o->posval[i];
86 log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
88 log_info(" %s", vp->help);
96 static void show_option_help(struct fio_option *o, int is_err)
98 const char *typehelp[] = {
102 "string with possible k/m/g postfix (opt=4k)",
103 "string with time postfix (opt=10s)",
105 "string with dual range (opt=1k-4k,4k-8k)",
106 "integer value (opt=100)",
107 "boolean value (opt=1)",
108 "list of floating point values separated by ':' (opt=5.9:7.8)",
112 int (*logger)(const char *format, ...);
120 logger("%20s: %s\n", "alias", o->alias);
122 logger("%20s: %s\n", "type", typehelp[o->type]);
123 logger("%20s: %s\n", "default", o->def ? o->def : "no default");
125 logger("%20s: only for profile '%s'\n", "valid", o->prof_name);
126 show_option_range(o, logger);
127 show_option_values(o);
130 static unsigned long long get_mult_time(const char *str, int len,
135 unsigned long long mult = 1;
138 * Go forward until we hit a non-digit, or +/- sign
140 while ((p - str) <= len) {
141 if (!isdigit((int) *p) && (*p != '+') && (*p != '-'))
146 if (!isalpha((int) *p)) {
154 for (int i = 0; i < strlen(c); i++)
155 c[i] = tolower(c[i]);
157 if (!strncmp("us", c, 2) || !strncmp("usec", c, 4))
159 else if (!strncmp("ms", c, 2) || !strncmp("msec", c, 4))
161 else if (!strcmp("s", c))
163 else if (!strcmp("m", c))
164 mult = 60 * 1000000UL;
165 else if (!strcmp("h", c))
166 mult = 60 * 60 * 1000000UL;
167 else if (!strcmp("d", c))
168 mult = 24 * 60 * 60 * 1000000UL;
174 static int is_separator(char c)
187 static unsigned long long __get_mult_bytes(const char *p, void *data,
190 unsigned int kb_base = fio_get_kb_base(data);
191 unsigned long long ret = 1;
192 unsigned int i, pow = 0, mult = kb_base;
200 for (i = 0; i < strlen(c); i++) {
201 c[i] = tolower(c[i]);
202 if (is_separator(c[i])) {
208 if (!strncmp("pib", c, 3)) {
211 } else if (!strncmp("tib", c, 3)) {
214 } else if (!strncmp("gib", c, 3)) {
217 } else if (!strncmp("mib", c, 3)) {
220 } else if (!strncmp("kib", c, 3)) {
223 } else if (!strncmp("p", c, 1) || !strncmp("pb", c, 2))
225 else if (!strncmp("t", c, 1) || !strncmp("tb", c, 2))
227 else if (!strncmp("g", c, 1) || !strncmp("gb", c, 2))
229 else if (!strncmp("m", c, 1) || !strncmp("mb", c, 2))
231 else if (!strncmp("k", c, 1) || !strncmp("kb", c, 2))
233 else if (!strncmp("%", c, 1)) {
240 ret *= (unsigned long long) mult;
246 static unsigned long long get_mult_bytes(const char *str, int len, void *data,
253 return __get_mult_bytes(str, data, percent);
256 * Go forward until we hit a non-digit, or +/- sign
258 while ((p - str) <= len) {
259 if (!isdigit((int) *p) &&
260 (((*p != '+') && (*p != '-')) || digit_seen))
262 digit_seen |= isdigit((int) *p);
266 if (!isalpha((int) *p) && (*p != '%'))
269 return __get_mult_bytes(p, data, percent);
272 extern int evaluate_arithmetic_expression(const char *buffer, long long *ival,
273 double *dval, double implied_units,
277 * Convert string into a floating number. Return 1 for success and 0 otherwise.
279 int str_to_float(const char *str, double *val, int is_time)
281 #ifdef CONFIG_ARITHMETIC
287 rc = evaluate_arithmetic_expression(str, &ival, &dval, 1.0, is_time);
294 return 1 == sscanf(str, "%lf", val);
298 * convert string into decimal value, noting any size suffix
300 int str_to_decimal(const char *str, long long *val, int kilo, void *data,
301 int is_seconds, int is_time)
305 #ifdef CONFIG_ARITHMETIC
308 double implied_units = 1.0;
315 #ifdef CONFIG_ARITHMETIC
317 implied_units = 1000000.0;
319 rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units, is_time);
320 if (str[0] == '(' && !rc) {
321 if (!kilo && is_seconds)
322 *val = ival / 1000000LL;
329 if (strstr(str, "0x") || strstr(str, "0X"))
334 *val = strtoll(str, NULL, base);
335 if (*val == LONG_MAX && errno == ERANGE)
340 unsigned long long mult;
343 mult = get_mult_bytes(str, len, data, &perc);
349 *val *= get_mult_time(str, len, is_seconds);
354 int check_str_bytes(const char *p, long long *val, void *data)
356 return str_to_decimal(p, val, 1, data, 0, 0);
359 int check_str_time(const char *p, long long *val, int is_seconds)
361 return str_to_decimal(p, val, 0, NULL, is_seconds, 1);
364 void strip_blank_front(char **p)
370 while (isspace((int) *s))
376 void strip_blank_end(char *p)
393 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
399 static int check_range_bytes(const char *str, long *val, void *data)
403 if (!str_to_decimal(str, &__val, 1, data, 0, 0)) {
411 static int check_int(const char *p, int *val)
415 if (strstr(p, "0x") || strstr(p, "0X")) {
416 if (sscanf(p, "%x", val) == 1)
419 if (sscanf(p, "%u", val) == 1)
426 static size_t opt_len(const char *str)
430 postfix = strchr(str, ':');
434 return (int)(postfix - str);
437 static int str_match_len(const struct value_pair *vp, const char *str)
439 return max(strlen(vp->ival), opt_len(str));
442 #define val_store(ptr, val, off, or, data, o) \
444 ptr = td_var((data), (o), (off)); \
451 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
452 int first, int more, int curr)
456 long long ull, *ullp;
460 int ret = 0, is_time = 0;
461 const struct value_pair *vp;
462 struct value_pair posval[PARSE_MAX_VP];
463 int i, all_skipped = 1;
465 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
468 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
469 log_err("Option %s requires an argument\n", o->name);
475 case FIO_OPT_STR_MULTI: {
476 fio_opt_str_fn *fn = o->cb;
478 posval_sort(o, posval);
481 for (i = 0; i < PARSE_MAX_VP; i++) {
483 if (!vp->ival || vp->ival[0] == '\0')
486 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
489 val_store(ilp, vp->oval, o->off1, vp->orval, data, o);
494 if (ret && !all_skipped)
495 show_option_values(o);
500 case FIO_OPT_STR_VAL_TIME:
503 case FIO_OPT_STR_VAL: {
504 fio_opt_str_val_fn *fn = o->cb;
507 if (!is_time && o->is_time)
508 is_time = o->is_time;
510 tmp[sizeof(tmp) - 1] = '\0';
511 strncpy(tmp, ptr, sizeof(tmp) - 1);
512 p = strchr(tmp, ',');
517 ret = check_str_time(tmp, &ull, o->is_seconds);
519 ret = check_str_bytes(tmp, &ull, data);
521 dprint(FD_PARSE, " ret=%d, out=%llu\n", ret, ull);
525 if (o->pow2 && !is_power_of_2(ull)) {
526 log_err("%s: must be a power-of-2\n", o->name);
530 if (o->maxval && ull > o->maxval) {
531 log_err("max value out of range: %llu"
532 " (%u max)\n", ull, o->maxval);
535 if (o->minval && ull < o->minval) {
536 log_err("min value out of range: %llu"
537 " (%u min)\n", ull, o->minval);
540 if (o->posval[0].ival) {
541 posval_sort(o, posval);
544 for (i = 0; i < PARSE_MAX_VP; i++) {
546 if (!vp->ival || vp->ival[0] == '\0')
548 if (vp->oval == ull) {
554 log_err("fio: value %llu not allowed:\n", ull);
555 show_option_values(o);
561 ret = fn(data, &ull);
563 if (o->type == FIO_OPT_INT) {
565 val_store(ilp, ull, o->off1, 0, data, o);
568 val_store(ilp, ull, o->off2, 0, data, o);
572 val_store(ilp, ull, o->off3, 0, data, o);
577 val_store(ilp, ull, o->off2, 0, data, o);
581 val_store(ilp, ull, o->off3, 0, data, o);
586 val_store(ullp, ull, o->off1, 0, data, o);
589 val_store(ullp, ull, o->off2, 0, data, o);
595 case FIO_OPT_FLOAT_LIST: {
600 ** Initialize precision to 0 and zero out list
601 ** in case specified list is shorter than default
605 ilp = td_var(data, o, o->off2);
609 flp = td_var(data, o, o->off1);
610 for(i = 0; i < o->maxlen; i++)
613 if (curr >= o->maxlen) {
614 log_err("the list exceeding max length %d\n",
618 if (!str_to_float(ptr, &uf, 0)) { /* this breaks if we ever have lists of times */
619 log_err("not a floating point value: %s\n", ptr);
623 log_err("value out of range: %f"
624 " (range max: %f)\n", uf, o->maxfp);
628 log_err("value out of range: %f"
629 " (range min: %f)\n", uf, o->minfp);
633 flp = td_var(data, o, o->off1);
636 dprint(FD_PARSE, " out=%f\n", uf);
639 ** Calculate precision for output by counting
640 ** number of digits after period. Find first
641 ** period in entire remaining list each time
643 cp2 = strchr(ptr, '.');
647 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
651 ilp = td_var(data, o, o->off2);
659 case FIO_OPT_STR_STORE: {
660 fio_opt_str_fn *fn = o->cb;
666 cp = td_var(data, o, o->off1);
672 else if (o->posval[0].ival) {
673 posval_sort(o, posval);
676 for (i = 0; i < PARSE_MAX_VP; i++) {
678 if (!vp->ival || vp->ival[0] == '\0' || !cp)
681 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
687 rest = strstr(*cp ?: ptr, ":");
701 show_option_values(o);
710 case FIO_OPT_RANGE: {
714 tmp[sizeof(tmp) - 1] = '\0';
715 strncpy(tmp, ptr, sizeof(tmp) - 1);
717 /* Handle bsrange with separate read,write values: */
718 p1 = strchr(tmp, ',');
722 p1 = strchr(tmp, '-');
724 p1 = strchr(tmp, ':');
736 if (!check_range_bytes(p1, &ul1, data) &&
737 !check_range_bytes(p2, &ul2, data)) {
740 unsigned long foo = ul1;
747 val_store(ilp, ul1, o->off1, 0, data, o);
748 val_store(ilp, ul2, o->off2, 0, data, o);
751 if (o->off3 && o->off4) {
752 val_store(ilp, ul1, o->off3, 0, data, o);
753 val_store(ilp, ul2, o->off4, 0, data, o);
757 if (o->off5 && o->off6) {
758 val_store(ilp, ul1, o->off5, 0, data, o);
759 val_store(ilp, ul2, o->off6, 0, data, o);
764 if (o->off3 && o->off4) {
765 val_store(ilp, ul1, o->off3, 0, data, o);
766 val_store(ilp, ul2, o->off4, 0, data, o);
770 if (o->off5 && o->off6) {
771 val_store(ilp, ul1, o->off5, 0, data, o);
772 val_store(ilp, ul2, o->off6, 0, data, o);
781 case FIO_OPT_STR_SET: {
782 fio_opt_int_fn *fn = o->cb;
785 ret = check_int(ptr, &il);
786 else if (o->type == FIO_OPT_BOOL)
791 dprint(FD_PARSE, " ret=%d, out=%d\n", ret, il);
796 if (o->maxval && il > (int) o->maxval) {
797 log_err("max value out of range: %d (%d max)\n",
801 if (o->minval && il < o->minval) {
802 log_err("min value out of range: %d (%d min)\n",
814 val_store(ilp, il, o->off1, 0, data, o);
817 val_store(ilp, il, o->off2, 0, data, o);
822 case FIO_OPT_DEPRECATED:
823 log_info("Option %s is deprecated\n", o->name);
827 log_err("Bad option type %u\n", o->type);
835 ret = o->verify(o, data);
837 log_err("Correct format for offending option\n");
838 log_err("%20s: %s\n", o->name, o->help);
839 show_option_help(o, 1);
846 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
848 char *o_ptr, *ptr, *ptr2;
851 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
855 o_ptr = ptr = strdup(__ptr);
858 * See if we have another set of parameters, hidden after a comma.
859 * Do this before parsing this round, to check if we should
860 * copy set 1 options to set 2.
869 (o->type != FIO_OPT_STR_STORE) &&
870 (o->type != FIO_OPT_STR) &&
871 (o->type != FIO_OPT_FLOAT_LIST)) {
872 ptr2 = strchr(ptr, ',');
873 if (ptr2 && *(ptr2 + 1) == '\0')
875 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
877 ptr2 = strchr(ptr, ':');
879 ptr2 = strchr(ptr, '-');
881 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
882 ptr2 = strchr(ptr, ':');
886 * Don't return early if parsing the first option fails - if
887 * we are doing multiple arguments, we can allow the first one
890 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
906 static struct fio_option *get_option(char *opt,
907 struct fio_option *options, char **post)
909 struct fio_option *o;
912 ret = strchr(opt, '=');
918 strip_blank_end(ret);
919 o = find_option(options, ret);
921 o = find_option(options, opt);
928 static int opt_cmp(const void *p1, const void *p2)
930 struct fio_option *o;
937 s = strdup(*((char **) p1));
938 o = get_option(s, __fio_options, &foo);
944 s = strdup(*((char **) p2));
945 o = get_option(s, __fio_options, &foo);
951 return prio2 - prio1;
954 void sort_options(char **opts, struct fio_option *options, int num_opts)
956 __fio_options = options;
957 qsort(opts, num_opts, sizeof(char *), opt_cmp);
958 __fio_options = NULL;
961 int parse_cmd_option(const char *opt, const char *val,
962 struct fio_option *options, void *data)
964 struct fio_option *o;
966 o = find_option(options, opt);
968 log_err("Bad option <%s>\n", opt);
972 if (!handle_option(o, val, data))
975 log_err("fio: failed parsing %s=%s\n", opt, val);
979 int parse_option(char *opt, const char *input,
980 struct fio_option *options, struct fio_option **o, void *data,
986 log_err("fio: failed parsing %s\n", input);
991 *o = get_option(opt, options, &post);
994 int len = strlen(opt);
995 if (opt + len + 1 != post)
996 memmove(opt + len + 1, post, strlen(post));
1002 if (handle_option(*o, post, data)) {
1003 log_err("fio: failed parsing %s\n", input);
1010 if (!strcmp("description", (*o)->name))
1015 log_info("--%s%s", (*o)->name, post ? "" : " ");
1017 log_info("=%s%s%s ", delim, post, delim);
1024 * Option match, levenshtein distance. Handy for not quite remembering what
1025 * the option name is.
1027 int string_distance(const char *s1, const char *s2)
1029 unsigned int s1_len = strlen(s1);
1030 unsigned int s2_len = strlen(s2);
1031 unsigned int *p, *q, *r;
1034 p = malloc(sizeof(unsigned int) * (s2_len + 1));
1035 q = malloc(sizeof(unsigned int) * (s2_len + 1));
1038 for (i = 1; i <= s2_len; i++)
1039 p[i] = p[i - 1] + 1;
1041 for (i = 1; i <= s1_len; i++) {
1043 for (j = 1; j <= s2_len; j++) {
1044 unsigned int sub = p[j - 1];
1047 if (s1[i - 1] != s2[j - 1])
1050 pmin = min(q[j - 1] + 1, sub);
1051 q[j] = min(p[j] + 1, pmin);
1065 * Make a guess of whether the distance from 's1' is significant enough
1066 * to warrant printing the guess. We set this to a 1/2 match.
1068 int string_distance_ok(const char *opt, int distance)
1073 len = (len + 1) / 2;
1074 return distance <= len;
1077 static struct fio_option *find_child(struct fio_option *options,
1078 struct fio_option *o)
1080 struct fio_option *__o;
1082 for (__o = options + 1; __o->name; __o++)
1083 if (__o->parent && !strcmp(__o->parent, o->name))
1089 static void __print_option(struct fio_option *o, struct fio_option *org,
1103 p += sprintf(p, "%s", " ");
1105 sprintf(p, "%s", o->name);
1107 log_info("%-24s: %s\n", name, o->help);
1110 static void print_option(struct fio_option *o)
1112 struct fio_option *parent;
1113 struct fio_option *__o;
1114 unsigned int printed;
1117 __print_option(o, NULL, 0);
1124 while ((__o = find_child(o, parent)) != NULL) {
1125 __print_option(__o, o, level);
1134 int show_cmd_help(struct fio_option *options, const char *name)
1136 struct fio_option *o, *closest;
1137 unsigned int best_dist = -1U;
1141 if (!name || !strcmp(name, "all"))
1146 for (o = &options[0]; o->name; o++) {
1149 if (o->type == FIO_OPT_DEPRECATED)
1151 if (!exec_profile && o->prof_name)
1153 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1157 if (!strcmp(name, o->name) ||
1158 (o->alias && !strcmp(name, o->alias)))
1163 dist = string_distance(name, o->name);
1164 if (dist < best_dist) {
1171 if (show_all || match) {
1174 log_info("%20s: %s\n", o->name, o->help);
1185 show_option_help(o, 0);
1191 log_err("No such command: %s", name);
1194 * Only print an appropriately close option, one where the edit
1195 * distance isn't too big. Otherwise we get crazy matches.
1197 if (closest && best_dist < 3) {
1198 log_info(" - showing closest match\n");
1199 log_info("%20s: %s\n", closest->name, closest->help);
1200 show_option_help(closest, 0);
1208 * Handle parsing of default parameters.
1210 void fill_default_options(void *data, struct fio_option *options)
1212 struct fio_option *o;
1214 dprint(FD_PARSE, "filling default options\n");
1216 for (o = &options[0]; o->name; o++)
1218 handle_option(o, o->def, data);
1221 void option_init(struct fio_option *o)
1223 if (o->type == FIO_OPT_DEPRECATED)
1225 if (o->type == FIO_OPT_BOOL) {
1229 if (o->type == FIO_OPT_INT) {
1231 o->maxval = UINT_MAX;
1233 if (o->type == FIO_OPT_FLOAT_LIST) {
1237 if (o->type == FIO_OPT_STR_SET && o->def && !o->no_warn_def) {
1238 log_err("Option %s: string set option with"
1239 " default will always be true\n", o->name);
1241 if (!o->cb && !o->off1)
1242 log_err("Option %s: neither cb nor offset given\n", o->name);
1244 log_info("Option %s: no category defined. Setting to misc\n", o->name);
1245 o->category = FIO_OPT_C_GENERAL;
1246 o->group = FIO_OPT_G_INVALID;
1248 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1249 o->type == FIO_OPT_STR_MULTI)
1254 * Sanitize the options structure. For now it just sets min/max for bool
1255 * values and whether both callback and offsets are given.
1257 void options_init(struct fio_option *options)
1259 struct fio_option *o;
1261 dprint(FD_PARSE, "init options\n");
1263 for (o = &options[0]; o->name; o++) {
1266 o->inv_opt = find_option(options, o->inverse);
1270 void options_free(struct fio_option *options, void *data)
1272 struct fio_option *o;
1275 dprint(FD_PARSE, "free options\n");
1277 for (o = &options[0]; o->name; o++) {
1278 if (o->type != FIO_OPT_STR_STORE || !o->off1)
1281 ptr = td_var(data, o, o->off1);