2 * This file contains the ini and command liner parser main.
20 #include "lib/ieee754.h"
23 #ifdef CONFIG_ARITHMETIC
27 static struct fio_option *__fio_options;
29 static int vp_cmp(const void *p1, const void *p2)
31 const struct value_pair *vp1 = p1;
32 const struct value_pair *vp2 = p2;
34 return strlen(vp2->ival) - strlen(vp1->ival);
37 static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
39 const struct value_pair *vp;
42 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
44 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
45 vp = &o->posval[entries];
46 if (!vp->ival || vp->ival[0] == '\0')
49 memcpy(&vpmap[entries], vp, sizeof(*vp));
52 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
55 static void show_option_range(struct fio_option *o,
56 size_t (*logger)(const char *format, ...))
58 if (o->type == FIO_OPT_FLOAT_LIST) {
59 if (o->minfp == DBL_MIN && o->maxfp == DBL_MAX)
62 logger("%20s: min=%f", "range", o->minfp);
63 if (o->maxfp != DBL_MAX)
64 logger(", max=%f", o->maxfp);
66 } else if (!o->posval[0].ival) {
67 if (!o->minval && !o->maxval)
70 logger("%20s: min=%d", "range", o->minval);
72 logger(", max=%d", o->maxval);
77 static void show_option_values(struct fio_option *o)
81 for (i = 0; i < PARSE_MAX_VP; i++) {
82 const struct value_pair *vp = &o->posval[i];
87 log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
89 log_info(" %s", vp->help);
97 static void show_option_help(struct fio_option *o, int is_err)
99 const char *typehelp[] = {
103 "string with possible k/m/g postfix (opt=4k)",
104 "string with time postfix (opt=10s)",
106 "string with dual range (opt=1k-4k,4k-8k)",
107 "integer value (opt=100)",
108 "boolean value (opt=1)",
109 "list of floating point values separated by ':' (opt=5.9:7.8)",
114 size_t (*logger)(const char *format, ...);
122 logger("%20s: %s\n", "alias", o->alias);
124 logger("%20s: %s\n", "type", typehelp[o->type]);
125 logger("%20s: %s\n", "default", o->def ? o->def : "no default");
127 logger("%20s: only for profile '%s'\n", "valid", o->prof_name);
128 show_option_range(o, logger);
129 show_option_values(o);
132 static unsigned long long get_mult_time(const char *str, int len,
137 unsigned long long mult = 1;
140 * Go forward until we hit a non-digit, or +/- sign
142 while ((p - str) <= len) {
143 if (!isdigit((int) *p) && (*p != '+') && (*p != '-'))
148 if (!isalpha((int) *p)) {
156 for (int i = 0; i < strlen(c); i++)
157 c[i] = tolower(c[i]);
159 if (!strncmp("us", c, 2) || !strncmp("usec", c, 4))
161 else if (!strncmp("ms", c, 2) || !strncmp("msec", c, 4))
163 else if (!strcmp("s", c))
165 else if (!strcmp("m", c))
166 mult = 60 * 1000000UL;
167 else if (!strcmp("h", c))
168 mult = 60 * 60 * 1000000UL;
169 else if (!strcmp("d", c))
170 mult = 24 * 60 * 60 * 1000000UL;
176 static int is_separator(char c)
189 static unsigned long long __get_mult_bytes(const char *p, void *data,
192 unsigned int kb_base = fio_get_kb_base(data);
193 unsigned long long ret = 1;
194 unsigned int i, pow = 0, mult = kb_base;
202 for (i = 0; i < strlen(c); i++) {
203 c[i] = tolower(c[i]);
204 if (is_separator(c[i])) {
210 if (!strncmp("pib", c, 3)) {
213 } else if (!strncmp("tib", c, 3)) {
216 } else if (!strncmp("gib", c, 3)) {
219 } else if (!strncmp("mib", c, 3)) {
222 } else if (!strncmp("kib", c, 3)) {
225 } else if (!strncmp("p", c, 1) || !strncmp("pb", c, 2))
227 else if (!strncmp("t", c, 1) || !strncmp("tb", c, 2))
229 else if (!strncmp("g", c, 1) || !strncmp("gb", c, 2))
231 else if (!strncmp("m", c, 1) || !strncmp("mb", c, 2))
233 else if (!strncmp("k", c, 1) || !strncmp("kb", c, 2))
235 else if (!strncmp("%", c, 1)) {
242 ret *= (unsigned long long) mult;
248 static unsigned long long get_mult_bytes(const char *str, int len, void *data,
255 return __get_mult_bytes(str, data, percent);
258 * Go forward until we hit a non-digit, or +/- sign
260 while ((p - str) <= len) {
261 if (!isdigit((int) *p) &&
262 (((*p != '+') && (*p != '-')) || digit_seen))
264 digit_seen |= isdigit((int) *p);
268 if (!isalpha((int) *p) && (*p != '%'))
271 return __get_mult_bytes(p, data, percent);
274 extern int evaluate_arithmetic_expression(const char *buffer, long long *ival,
275 double *dval, double implied_units,
279 * Convert string into a floating number. Return 1 for success and 0 otherwise.
281 int str_to_float(const char *str, double *val, int is_time)
283 #ifdef CONFIG_ARITHMETIC
289 rc = evaluate_arithmetic_expression(str, &ival, &dval, 1.0, is_time);
296 return 1 == sscanf(str, "%lf", val);
300 * convert string into decimal value, noting any size suffix
302 int str_to_decimal(const char *str, long long *val, int kilo, void *data,
303 int is_seconds, int is_time)
307 #ifdef CONFIG_ARITHMETIC
310 double implied_units = 1.0;
317 #ifdef CONFIG_ARITHMETIC
319 implied_units = 1000000.0;
321 rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units, is_time);
322 if (str[0] == '(' && !rc) {
323 if (!kilo && is_seconds)
324 *val = ival / 1000000LL;
331 if (strstr(str, "0x") || strstr(str, "0X"))
336 *val = strtoll(str, NULL, base);
337 if (*val == LONG_MAX && errno == ERANGE)
342 unsigned long long mult;
345 mult = get_mult_bytes(str, len, data, &perc);
351 *val *= get_mult_time(str, len, is_seconds);
356 int check_str_bytes(const char *p, long long *val, void *data)
358 return str_to_decimal(p, val, 1, data, 0, 0);
361 int check_str_time(const char *p, long long *val, int is_seconds)
363 return str_to_decimal(p, val, 0, NULL, is_seconds, 1);
366 void strip_blank_front(char **p)
372 while (isspace((int) *s))
378 void strip_blank_end(char *p)
395 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
401 static int check_range_bytes(const char *str, long *val, void *data)
405 if (!str_to_decimal(str, &__val, 1, data, 0, 0)) {
413 static int check_int(const char *p, int *val)
417 if (strstr(p, "0x") || strstr(p, "0X")) {
418 if (sscanf(p, "%x", val) == 1)
421 if (sscanf(p, "%u", val) == 1)
428 static size_t opt_len(const char *str)
432 postfix = strchr(str, ':');
436 return (int)(postfix - str);
439 static int str_match_len(const struct value_pair *vp, const char *str)
441 return max(strlen(vp->ival), opt_len(str));
444 #define val_store(ptr, val, off, or, data, o) \
446 ptr = td_var((data), (o), (off)); \
453 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
454 int first, int more, int curr)
458 long long ull, *ullp;
462 int ret = 0, is_time = 0;
463 const struct value_pair *vp;
464 struct value_pair posval[PARSE_MAX_VP];
465 int i, all_skipped = 1;
467 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
470 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
471 log_err("Option %s requires an argument\n", o->name);
477 case FIO_OPT_STR_MULTI: {
478 fio_opt_str_fn *fn = o->cb;
480 posval_sort(o, posval);
483 for (i = 0; i < PARSE_MAX_VP; i++) {
485 if (!vp->ival || vp->ival[0] == '\0')
490 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
493 val_store(ilp, vp->oval, o->off1, vp->orval, data, o);
498 if (ret && !all_skipped)
499 show_option_values(o);
504 case FIO_OPT_STR_VAL_TIME:
507 case FIO_OPT_STR_VAL: {
508 fio_opt_str_val_fn *fn = o->cb;
511 if (!is_time && o->is_time)
512 is_time = o->is_time;
514 tmp[sizeof(tmp) - 1] = '\0';
515 strncpy(tmp, ptr, sizeof(tmp) - 1);
516 p = strchr(tmp, ',');
521 ret = check_str_time(tmp, &ull, o->is_seconds);
523 ret = check_str_bytes(tmp, &ull, data);
525 dprint(FD_PARSE, " ret=%d, out=%llu\n", ret, ull);
529 if (o->pow2 && !is_power_of_2(ull)) {
530 log_err("%s: must be a power-of-2\n", o->name);
534 if (o->maxval && ull > o->maxval) {
535 log_err("max value out of range: %llu"
536 " (%u max)\n", ull, o->maxval);
539 if (o->minval && ull < o->minval) {
540 log_err("min value out of range: %llu"
541 " (%u min)\n", ull, o->minval);
544 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')
552 if (vp->oval == ull) {
558 log_err("fio: value %llu not allowed:\n", ull);
559 show_option_values(o);
565 ret = fn(data, &ull);
567 if (o->type == FIO_OPT_INT) {
569 val_store(ilp, ull, o->off1, 0, data, o);
572 val_store(ilp, ull, o->off2, 0, data, o);
576 val_store(ilp, ull, o->off3, 0, data, o);
581 val_store(ilp, ull, o->off2, 0, data, o);
585 val_store(ilp, ull, o->off3, 0, data, o);
590 val_store(ullp, ull, o->off1, 0, data, o);
593 val_store(ullp, ull, o->off2, 0, data, o);
599 case FIO_OPT_FLOAT_LIST: {
604 ** Initialize precision to 0 and zero out list
605 ** in case specified list is shorter than default
609 ilp = td_var(data, o, o->off2);
613 flp = td_var(data, o, o->off1);
614 for(i = 0; i < o->maxlen; i++)
617 if (curr >= o->maxlen) {
618 log_err("the list exceeding max length %d\n",
622 if (!str_to_float(ptr, &uf, 0)) { /* this breaks if we ever have lists of times */
623 log_err("not a floating point value: %s\n", ptr);
627 log_err("value out of range: %f"
628 " (range max: %f)\n", uf, o->maxfp);
632 log_err("value out of range: %f"
633 " (range min: %f)\n", uf, o->minfp);
637 flp = td_var(data, o, o->off1);
640 dprint(FD_PARSE, " out=%f\n", uf);
643 ** Calculate precision for output by counting
644 ** number of digits after period. Find first
645 ** period in entire remaining list each time
647 cp2 = strchr(ptr, '.');
651 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
655 ilp = td_var(data, o, o->off2);
663 case FIO_OPT_STR_STORE: {
664 fio_opt_str_fn *fn = o->cb;
670 cp = td_var(data, o, o->off1);
676 else if (o->posval[0].ival) {
677 posval_sort(o, posval);
680 for (i = 0; i < PARSE_MAX_VP; i++) {
682 if (!vp->ival || vp->ival[0] == '\0' || !cp)
685 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
691 rest = strstr(*cp ?: ptr, ":");
705 show_option_values(o);
714 case FIO_OPT_RANGE: {
718 tmp[sizeof(tmp) - 1] = '\0';
719 strncpy(tmp, ptr, sizeof(tmp) - 1);
721 /* Handle bsrange with separate read,write values: */
722 p1 = strchr(tmp, ',');
726 p1 = strchr(tmp, '-');
728 p1 = strchr(tmp, ':');
740 if (!check_range_bytes(p1, &ul1, data) &&
741 !check_range_bytes(p2, &ul2, data)) {
744 unsigned long foo = ul1;
751 val_store(ilp, ul1, o->off1, 0, data, o);
752 val_store(ilp, ul2, o->off2, 0, data, o);
755 if (o->off3 && o->off4) {
756 val_store(ilp, ul1, o->off3, 0, data, o);
757 val_store(ilp, ul2, o->off4, 0, data, o);
761 if (o->off5 && o->off6) {
762 val_store(ilp, ul1, o->off5, 0, data, o);
763 val_store(ilp, ul2, o->off6, 0, data, o);
768 if (o->off3 && o->off4) {
769 val_store(ilp, ul1, o->off3, 0, data, o);
770 val_store(ilp, ul2, o->off4, 0, data, o);
774 if (o->off5 && o->off6) {
775 val_store(ilp, ul1, o->off5, 0, data, o);
776 val_store(ilp, ul2, o->off6, 0, data, o);
785 case FIO_OPT_STR_SET: {
786 fio_opt_int_fn *fn = o->cb;
789 ret = check_int(ptr, &il);
790 else if (o->type == FIO_OPT_BOOL)
795 dprint(FD_PARSE, " ret=%d, out=%d\n", ret, il);
800 if (o->maxval && il > (int) o->maxval) {
801 log_err("max value out of range: %d (%d max)\n",
805 if (o->minval && il < o->minval) {
806 log_err("min value out of range: %d (%d min)\n",
818 val_store(ilp, il, o->off1, 0, data, o);
821 val_store(ilp, il, o->off2, 0, data, o);
826 case FIO_OPT_DEPRECATED:
827 log_info("Option %s is deprecated\n", o->name);
831 log_err("Bad option type %u\n", o->type);
839 ret = o->verify(o, data);
841 log_err("Correct format for offending option\n");
842 log_err("%20s: %s\n", o->name, o->help);
843 show_option_help(o, 1);
850 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
852 char *o_ptr, *ptr, *ptr2;
855 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
859 o_ptr = ptr = strdup(__ptr);
862 * See if we have another set of parameters, hidden after a comma.
863 * Do this before parsing this round, to check if we should
864 * copy set 1 options to set 2.
873 (o->type != FIO_OPT_STR_STORE) &&
874 (o->type != FIO_OPT_STR) &&
875 (o->type != FIO_OPT_FLOAT_LIST)) {
876 ptr2 = strchr(ptr, ',');
877 if (ptr2 && *(ptr2 + 1) == '\0')
879 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
881 ptr2 = strchr(ptr, ':');
883 ptr2 = strchr(ptr, '-');
885 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
886 ptr2 = strchr(ptr, ':');
890 * Don't return early if parsing the first option fails - if
891 * we are doing multiple arguments, we can allow the first one
894 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
910 struct fio_option *find_option(struct fio_option *options, const char *opt)
912 struct fio_option *o;
914 for (o = &options[0]; o->name; o++) {
915 if (!o_match(o, opt))
917 if (o->type == FIO_OPT_UNSUPPORTED) {
918 log_err("Option <%s>: %s\n", o->name, o->help);
929 static struct fio_option *get_option(char *opt,
930 struct fio_option *options, char **post)
932 struct fio_option *o;
935 ret = strchr(opt, '=');
941 strip_blank_end(ret);
942 o = find_option(options, ret);
944 o = find_option(options, opt);
951 static int opt_cmp(const void *p1, const void *p2)
953 struct fio_option *o;
960 s = strdup(*((char **) p1));
961 o = get_option(s, __fio_options, &foo);
967 s = strdup(*((char **) p2));
968 o = get_option(s, __fio_options, &foo);
974 return prio2 - prio1;
977 void sort_options(char **opts, struct fio_option *options, int num_opts)
979 __fio_options = options;
980 qsort(opts, num_opts, sizeof(char *), opt_cmp);
981 __fio_options = NULL;
984 static void add_to_dump_list(struct fio_option *o, struct flist_head *dump_list,
987 struct print_option *p;
992 p = malloc(sizeof(*p));
993 p->name = strdup(o->name);
995 p->value = strdup(post);
999 flist_add_tail(&p->list, dump_list);
1002 int parse_cmd_option(const char *opt, const char *val,
1003 struct fio_option *options, void *data,
1004 struct flist_head *dump_list)
1006 struct fio_option *o;
1008 o = find_option(options, opt);
1010 log_err("Bad option <%s>\n", opt);
1014 if (handle_option(o, val, data)) {
1015 log_err("fio: failed parsing %s=%s\n", opt, val);
1019 add_to_dump_list(o, dump_list, val);
1023 int parse_option(char *opt, const char *input,
1024 struct fio_option *options, struct fio_option **o, void *data,
1025 struct flist_head *dump_list)
1030 log_err("fio: failed parsing %s\n", input);
1035 *o = get_option(opt, options, &post);
1038 int len = strlen(opt);
1039 if (opt + len + 1 != post)
1040 memmove(opt + len + 1, post, strlen(post));
1046 if (handle_option(*o, post, data)) {
1047 log_err("fio: failed parsing %s\n", input);
1051 add_to_dump_list(*o, dump_list, post);
1056 * Option match, levenshtein distance. Handy for not quite remembering what
1057 * the option name is.
1059 int string_distance(const char *s1, const char *s2)
1061 unsigned int s1_len = strlen(s1);
1062 unsigned int s2_len = strlen(s2);
1063 unsigned int *p, *q, *r;
1066 p = malloc(sizeof(unsigned int) * (s2_len + 1));
1067 q = malloc(sizeof(unsigned int) * (s2_len + 1));
1070 for (i = 1; i <= s2_len; i++)
1071 p[i] = p[i - 1] + 1;
1073 for (i = 1; i <= s1_len; i++) {
1075 for (j = 1; j <= s2_len; j++) {
1076 unsigned int sub = p[j - 1];
1079 if (s1[i - 1] != s2[j - 1])
1082 pmin = min(q[j - 1] + 1, sub);
1083 q[j] = min(p[j] + 1, pmin);
1097 * Make a guess of whether the distance from 's1' is significant enough
1098 * to warrant printing the guess. We set this to a 1/2 match.
1100 int string_distance_ok(const char *opt, int distance)
1105 len = (len + 1) / 2;
1106 return distance <= len;
1109 static struct fio_option *find_child(struct fio_option *options,
1110 struct fio_option *o)
1112 struct fio_option *__o;
1114 for (__o = options + 1; __o->name; __o++)
1115 if (__o->parent && !strcmp(__o->parent, o->name))
1121 static void __print_option(struct fio_option *o, struct fio_option *org,
1135 p += sprintf(p, "%s", " ");
1137 sprintf(p, "%s", o->name);
1139 log_info("%-24s: %s\n", name, o->help);
1142 static void print_option(struct fio_option *o)
1144 struct fio_option *parent;
1145 struct fio_option *__o;
1146 unsigned int printed;
1149 __print_option(o, NULL, 0);
1156 while ((__o = find_child(o, parent)) != NULL) {
1157 __print_option(__o, o, level);
1166 int show_cmd_help(struct fio_option *options, const char *name)
1168 struct fio_option *o, *closest;
1169 unsigned int best_dist = -1U;
1173 if (!name || !strcmp(name, "all"))
1178 for (o = &options[0]; o->name; o++) {
1181 if (o->type == FIO_OPT_DEPRECATED)
1183 if (!exec_profile && o->prof_name)
1185 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1189 if (!strcmp(name, o->name) ||
1190 (o->alias && !strcmp(name, o->alias)))
1195 dist = string_distance(name, o->name);
1196 if (dist < best_dist) {
1203 if (show_all || match) {
1206 log_info("%20s: %s\n", o->name, o->help);
1217 show_option_help(o, 0);
1223 log_err("No such command: %s", name);
1226 * Only print an appropriately close option, one where the edit
1227 * distance isn't too big. Otherwise we get crazy matches.
1229 if (closest && best_dist < 3) {
1230 log_info(" - showing closest match\n");
1231 log_info("%20s: %s\n", closest->name, closest->help);
1232 show_option_help(closest, 0);
1240 * Handle parsing of default parameters.
1242 void fill_default_options(void *data, struct fio_option *options)
1244 struct fio_option *o;
1246 dprint(FD_PARSE, "filling default options\n");
1248 for (o = &options[0]; o->name; o++)
1250 handle_option(o, o->def, data);
1253 void option_init(struct fio_option *o)
1255 if (o->type == FIO_OPT_DEPRECATED || o->type == FIO_OPT_UNSUPPORTED)
1257 if (o->name && !o->lname)
1258 log_err("Option %s: missing long option name\n", o->name);
1259 if (o->type == FIO_OPT_BOOL) {
1263 if (o->type == FIO_OPT_INT) {
1265 o->maxval = UINT_MAX;
1267 if (o->type == FIO_OPT_FLOAT_LIST) {
1271 if (o->type == FIO_OPT_STR_SET && o->def && !o->no_warn_def) {
1272 log_err("Option %s: string set option with"
1273 " default will always be true\n", o->name);
1275 if (!o->cb && !o->off1)
1276 log_err("Option %s: neither cb nor offset given\n", o->name);
1278 log_info("Option %s: no category defined. Setting to misc\n", o->name);
1279 o->category = FIO_OPT_C_GENERAL;
1280 o->group = FIO_OPT_G_INVALID;
1282 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1283 o->type == FIO_OPT_STR_MULTI)
1288 * Sanitize the options structure. For now it just sets min/max for bool
1289 * values and whether both callback and offsets are given.
1291 void options_init(struct fio_option *options)
1293 struct fio_option *o;
1295 dprint(FD_PARSE, "init options\n");
1297 for (o = &options[0]; o->name; o++) {
1300 o->inv_opt = find_option(options, o->inverse);
1304 void options_free(struct fio_option *options, void *data)
1306 struct fio_option *o;
1309 dprint(FD_PARSE, "free options\n");
1311 for (o = &options[0]; o->name; o++) {
1312 if (o->type != FIO_OPT_STR_STORE || !o->off1)
1315 ptr = td_var(data, o, o->off1);