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 kb_base is 1000, use true units.
211 * If kb_base is 1024, use opposite units.
213 if (!strncmp("pib", c, 3)) {
217 else if (kb_base == 1024)
219 } else if (!strncmp("tib", c, 3)) {
223 else if (kb_base == 1024)
225 } else if (!strncmp("gib", c, 3)) {
229 else if (kb_base == 1024)
231 } else if (!strncmp("mib", c, 3)) {
235 else if (kb_base == 1024)
237 } else if (!strncmp("kib", c, 3)) {
241 else if (kb_base == 1024)
243 } else if (!strncmp("p", c, 1) || !strncmp("pb", c, 2)) {
245 } else if (!strncmp("t", c, 1) || !strncmp("tb", c, 2)) {
247 } else if (!strncmp("g", c, 1) || !strncmp("gb", c, 2)) {
249 } else if (!strncmp("m", c, 1) || !strncmp("mb", c, 2)) {
251 } else if (!strncmp("k", c, 1) || !strncmp("kb", c, 2)) {
253 } else if (!strncmp("%", c, 1)) {
260 ret *= (unsigned long long) mult;
266 static unsigned long long get_mult_bytes(const char *str, int len, void *data,
273 return __get_mult_bytes(str, data, percent);
276 * Go forward until we hit a non-digit, or +/- sign
278 while ((p - str) <= len) {
279 if (!isdigit((int) *p) &&
280 (((*p != '+') && (*p != '-')) || digit_seen))
282 digit_seen |= isdigit((int) *p);
286 if (!isalpha((int) *p) && (*p != '%'))
289 return __get_mult_bytes(p, data, percent);
292 extern int evaluate_arithmetic_expression(const char *buffer, long long *ival,
293 double *dval, double implied_units,
297 * Convert string into a floating number. Return 1 for success and 0 otherwise.
299 int str_to_float(const char *str, double *val, int is_time)
301 #ifdef CONFIG_ARITHMETIC
307 rc = evaluate_arithmetic_expression(str, &ival, &dval, 1.0, is_time);
314 return 1 == sscanf(str, "%lf", val);
318 * convert string into decimal value, noting any size suffix
320 int str_to_decimal(const char *str, long long *val, int kilo, void *data,
321 int is_seconds, int is_time)
325 #ifdef CONFIG_ARITHMETIC
328 double implied_units = 1.0;
335 #ifdef CONFIG_ARITHMETIC
337 implied_units = 1000000.0;
339 rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units, is_time);
340 if (str[0] == '(' && !rc) {
341 if (!kilo && is_seconds)
342 *val = ival / 1000000LL;
349 if (strstr(str, "0x") || strstr(str, "0X"))
354 *val = strtoll(str, NULL, base);
355 if (*val == LONG_MAX && errno == ERANGE)
360 unsigned long long mult;
363 mult = get_mult_bytes(str, len, data, &perc);
369 *val *= get_mult_time(str, len, is_seconds);
374 int check_str_bytes(const char *p, long long *val, void *data)
376 return str_to_decimal(p, val, 1, data, 0, 0);
379 int check_str_time(const char *p, long long *val, int is_seconds)
381 return str_to_decimal(p, val, 0, NULL, is_seconds, 1);
384 void strip_blank_front(char **p)
390 while (isspace((int) *s))
396 void strip_blank_end(char *p)
413 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
419 static int check_range_bytes(const char *str, long *val, void *data)
423 if (!str_to_decimal(str, &__val, 1, data, 0, 0)) {
431 static int check_int(const char *p, int *val)
435 if (strstr(p, "0x") || strstr(p, "0X")) {
436 if (sscanf(p, "%x", val) == 1)
439 if (sscanf(p, "%u", val) == 1)
446 static size_t opt_len(const char *str)
450 postfix = strchr(str, ':');
454 return (int)(postfix - str);
457 static int str_match_len(const struct value_pair *vp, const char *str)
459 return max(strlen(vp->ival), opt_len(str));
462 #define val_store(ptr, val, off, or, data, o) \
464 ptr = td_var((data), (o), (off)); \
471 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
472 int first, int more, int curr)
476 long long ull, *ullp;
480 int ret = 0, is_time = 0;
481 const struct value_pair *vp;
482 struct value_pair posval[PARSE_MAX_VP];
483 int i, all_skipped = 1;
485 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
488 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
489 log_err("Option %s requires an argument\n", o->name);
495 case FIO_OPT_STR_MULTI: {
496 fio_opt_str_fn *fn = o->cb;
498 posval_sort(o, posval);
501 for (i = 0; i < PARSE_MAX_VP; i++) {
503 if (!vp->ival || vp->ival[0] == '\0')
508 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
511 val_store(ilp, vp->oval, o->off1, vp->orval, data, o);
516 if (ret && !all_skipped)
517 show_option_values(o);
522 case FIO_OPT_STR_VAL_TIME:
525 case FIO_OPT_STR_VAL: {
526 fio_opt_str_val_fn *fn = o->cb;
529 if (!is_time && o->is_time)
530 is_time = o->is_time;
532 tmp[sizeof(tmp) - 1] = '\0';
533 strncpy(tmp, ptr, sizeof(tmp) - 1);
534 p = strchr(tmp, ',');
539 ret = check_str_time(tmp, &ull, o->is_seconds);
541 ret = check_str_bytes(tmp, &ull, data);
543 dprint(FD_PARSE, " ret=%d, out=%llu\n", ret, ull);
547 if (o->pow2 && !is_power_of_2(ull)) {
548 log_err("%s: must be a power-of-2\n", o->name);
552 if (o->maxval && ull > o->maxval) {
553 log_err("max value out of range: %llu"
554 " (%u max)\n", ull, o->maxval);
557 if (o->minval && ull < o->minval) {
558 log_err("min value out of range: %llu"
559 " (%u min)\n", ull, o->minval);
562 if (o->posval[0].ival) {
563 posval_sort(o, posval);
566 for (i = 0; i < PARSE_MAX_VP; i++) {
568 if (!vp->ival || vp->ival[0] == '\0')
570 if (vp->oval == ull) {
576 log_err("fio: value %llu not allowed:\n", ull);
577 show_option_values(o);
583 ret = fn(data, &ull);
585 if (o->type == FIO_OPT_INT) {
587 val_store(ilp, ull, o->off1, 0, data, o);
590 val_store(ilp, ull, o->off2, 0, data, o);
594 val_store(ilp, ull, o->off3, 0, data, o);
599 val_store(ilp, ull, o->off2, 0, data, o);
603 val_store(ilp, ull, o->off3, 0, data, o);
608 val_store(ullp, ull, o->off1, 0, data, o);
611 val_store(ullp, ull, o->off2, 0, data, o);
617 case FIO_OPT_FLOAT_LIST: {
622 ** Initialize precision to 0 and zero out list
623 ** in case specified list is shorter than default
627 ilp = td_var(data, o, o->off2);
631 flp = td_var(data, o, o->off1);
632 for(i = 0; i < o->maxlen; i++)
635 if (curr >= o->maxlen) {
636 log_err("the list exceeding max length %d\n",
640 if (!str_to_float(ptr, &uf, 0)) { /* this breaks if we ever have lists of times */
641 log_err("not a floating point value: %s\n", ptr);
645 log_err("value out of range: %f"
646 " (range max: %f)\n", uf, o->maxfp);
650 log_err("value out of range: %f"
651 " (range min: %f)\n", uf, o->minfp);
655 flp = td_var(data, o, o->off1);
658 dprint(FD_PARSE, " out=%f\n", uf);
661 ** Calculate precision for output by counting
662 ** number of digits after period. Find first
663 ** period in entire remaining list each time
665 cp2 = strchr(ptr, '.');
669 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
673 ilp = td_var(data, o, o->off2);
681 case FIO_OPT_STR_STORE: {
682 fio_opt_str_fn *fn = o->cb;
688 cp = td_var(data, o, o->off1);
694 else if (o->posval[0].ival) {
695 posval_sort(o, posval);
698 for (i = 0; i < PARSE_MAX_VP; i++) {
700 if (!vp->ival || vp->ival[0] == '\0' || !cp)
703 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
709 rest = strstr(*cp ?: ptr, ":");
723 show_option_values(o);
732 case FIO_OPT_RANGE: {
736 tmp[sizeof(tmp) - 1] = '\0';
737 strncpy(tmp, ptr, sizeof(tmp) - 1);
739 /* Handle bsrange with separate read,write values: */
740 p1 = strchr(tmp, ',');
744 p1 = strchr(tmp, '-');
746 p1 = strchr(tmp, ':');
758 if (!check_range_bytes(p1, &ul1, data) &&
759 !check_range_bytes(p2, &ul2, data)) {
762 unsigned long foo = ul1;
769 val_store(ilp, ul1, o->off1, 0, data, o);
770 val_store(ilp, ul2, o->off2, 0, data, o);
773 if (o->off3 && o->off4) {
774 val_store(ilp, ul1, o->off3, 0, data, o);
775 val_store(ilp, ul2, o->off4, 0, data, o);
779 if (o->off5 && o->off6) {
780 val_store(ilp, ul1, o->off5, 0, data, o);
781 val_store(ilp, ul2, o->off6, 0, data, o);
786 if (o->off3 && o->off4) {
787 val_store(ilp, ul1, o->off3, 0, data, o);
788 val_store(ilp, ul2, o->off4, 0, data, o);
792 if (o->off5 && o->off6) {
793 val_store(ilp, ul1, o->off5, 0, data, o);
794 val_store(ilp, ul2, o->off6, 0, data, o);
803 case FIO_OPT_STR_SET: {
804 fio_opt_int_fn *fn = o->cb;
807 ret = check_int(ptr, &il);
808 else if (o->type == FIO_OPT_BOOL)
813 dprint(FD_PARSE, " ret=%d, out=%d\n", ret, il);
818 if (o->maxval && il > (int) o->maxval) {
819 log_err("max value out of range: %d (%d max)\n",
823 if (o->minval && il < o->minval) {
824 log_err("min value out of range: %d (%d min)\n",
836 val_store(ilp, il, o->off1, 0, data, o);
839 val_store(ilp, il, o->off2, 0, data, o);
844 case FIO_OPT_DEPRECATED:
845 log_info("Option %s is deprecated\n", o->name);
849 log_err("Bad option type %u\n", o->type);
857 ret = o->verify(o, data);
859 log_err("Correct format for offending option\n");
860 log_err("%20s: %s\n", o->name, o->help);
861 show_option_help(o, 1);
868 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
870 char *o_ptr, *ptr, *ptr2;
873 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
877 o_ptr = ptr = strdup(__ptr);
880 * See if we have another set of parameters, hidden after a comma.
881 * Do this before parsing this round, to check if we should
882 * copy set 1 options to set 2.
891 (o->type != FIO_OPT_STR_STORE) &&
892 (o->type != FIO_OPT_STR) &&
893 (o->type != FIO_OPT_FLOAT_LIST)) {
894 ptr2 = strchr(ptr, ',');
895 if (ptr2 && *(ptr2 + 1) == '\0')
897 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
899 ptr2 = strchr(ptr, ':');
901 ptr2 = strchr(ptr, '-');
903 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
904 ptr2 = strchr(ptr, ':');
908 * Don't return early if parsing the first option fails - if
909 * we are doing multiple arguments, we can allow the first one
912 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
928 struct fio_option *find_option(struct fio_option *options, const char *opt)
930 struct fio_option *o;
932 for (o = &options[0]; o->name; o++) {
933 if (!o_match(o, opt))
935 if (o->type == FIO_OPT_UNSUPPORTED) {
936 log_err("Option <%s>: %s\n", o->name, o->help);
947 static struct fio_option *get_option(char *opt,
948 struct fio_option *options, char **post)
950 struct fio_option *o;
953 ret = strchr(opt, '=');
959 strip_blank_end(ret);
960 o = find_option(options, ret);
962 o = find_option(options, opt);
969 static int opt_cmp(const void *p1, const void *p2)
971 struct fio_option *o;
978 s = strdup(*((char **) p1));
979 o = get_option(s, __fio_options, &foo);
985 s = strdup(*((char **) p2));
986 o = get_option(s, __fio_options, &foo);
992 return prio2 - prio1;
995 void sort_options(char **opts, struct fio_option *options, int num_opts)
997 __fio_options = options;
998 qsort(opts, num_opts, sizeof(char *), opt_cmp);
999 __fio_options = NULL;
1002 static void add_to_dump_list(struct fio_option *o, struct flist_head *dump_list,
1005 struct print_option *p;
1010 p = malloc(sizeof(*p));
1011 p->name = strdup(o->name);
1013 p->value = strdup(post);
1017 flist_add_tail(&p->list, dump_list);
1020 int parse_cmd_option(const char *opt, const char *val,
1021 struct fio_option *options, void *data,
1022 struct flist_head *dump_list)
1024 struct fio_option *o;
1026 o = find_option(options, opt);
1028 log_err("Bad option <%s>\n", opt);
1032 if (handle_option(o, val, data)) {
1033 log_err("fio: failed parsing %s=%s\n", opt, val);
1037 add_to_dump_list(o, dump_list, val);
1041 int parse_option(char *opt, const char *input,
1042 struct fio_option *options, struct fio_option **o, void *data,
1043 struct flist_head *dump_list)
1048 log_err("fio: failed parsing %s\n", input);
1053 *o = get_option(opt, options, &post);
1056 int len = strlen(opt);
1057 if (opt + len + 1 != post)
1058 memmove(opt + len + 1, post, strlen(post));
1064 if (handle_option(*o, post, data)) {
1065 log_err("fio: failed parsing %s\n", input);
1069 add_to_dump_list(*o, dump_list, post);
1074 * Option match, levenshtein distance. Handy for not quite remembering what
1075 * the option name is.
1077 int string_distance(const char *s1, const char *s2)
1079 unsigned int s1_len = strlen(s1);
1080 unsigned int s2_len = strlen(s2);
1081 unsigned int *p, *q, *r;
1084 p = malloc(sizeof(unsigned int) * (s2_len + 1));
1085 q = malloc(sizeof(unsigned int) * (s2_len + 1));
1088 for (i = 1; i <= s2_len; i++)
1089 p[i] = p[i - 1] + 1;
1091 for (i = 1; i <= s1_len; i++) {
1093 for (j = 1; j <= s2_len; j++) {
1094 unsigned int sub = p[j - 1];
1097 if (s1[i - 1] != s2[j - 1])
1100 pmin = min(q[j - 1] + 1, sub);
1101 q[j] = min(p[j] + 1, pmin);
1115 * Make a guess of whether the distance from 's1' is significant enough
1116 * to warrant printing the guess. We set this to a 1/2 match.
1118 int string_distance_ok(const char *opt, int distance)
1123 len = (len + 1) / 2;
1124 return distance <= len;
1127 static struct fio_option *find_child(struct fio_option *options,
1128 struct fio_option *o)
1130 struct fio_option *__o;
1132 for (__o = options + 1; __o->name; __o++)
1133 if (__o->parent && !strcmp(__o->parent, o->name))
1139 static void __print_option(struct fio_option *o, struct fio_option *org,
1153 p += sprintf(p, "%s", " ");
1155 sprintf(p, "%s", o->name);
1157 log_info("%-24s: %s\n", name, o->help);
1160 static void print_option(struct fio_option *o)
1162 struct fio_option *parent;
1163 struct fio_option *__o;
1164 unsigned int printed;
1167 __print_option(o, NULL, 0);
1174 while ((__o = find_child(o, parent)) != NULL) {
1175 __print_option(__o, o, level);
1184 int show_cmd_help(struct fio_option *options, const char *name)
1186 struct fio_option *o, *closest;
1187 unsigned int best_dist = -1U;
1191 if (!name || !strcmp(name, "all"))
1196 for (o = &options[0]; o->name; o++) {
1199 if (o->type == FIO_OPT_DEPRECATED)
1201 if (!exec_profile && o->prof_name)
1203 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1207 if (!strcmp(name, o->name) ||
1208 (o->alias && !strcmp(name, o->alias)))
1213 dist = string_distance(name, o->name);
1214 if (dist < best_dist) {
1221 if (show_all || match) {
1224 log_info("%20s: %s\n", o->name, o->help);
1235 show_option_help(o, 0);
1241 log_err("No such command: %s", name);
1244 * Only print an appropriately close option, one where the edit
1245 * distance isn't too big. Otherwise we get crazy matches.
1247 if (closest && best_dist < 3) {
1248 log_info(" - showing closest match\n");
1249 log_info("%20s: %s\n", closest->name, closest->help);
1250 show_option_help(closest, 0);
1258 * Handle parsing of default parameters.
1260 void fill_default_options(void *data, struct fio_option *options)
1262 struct fio_option *o;
1264 dprint(FD_PARSE, "filling default options\n");
1266 for (o = &options[0]; o->name; o++)
1268 handle_option(o, o->def, data);
1271 static void option_init(struct fio_option *o)
1273 if (o->type == FIO_OPT_DEPRECATED || o->type == FIO_OPT_UNSUPPORTED)
1275 if (o->name && !o->lname)
1276 log_err("Option %s: missing long option name\n", o->name);
1277 if (o->type == FIO_OPT_BOOL) {
1281 if (o->type == FIO_OPT_INT) {
1283 o->maxval = UINT_MAX;
1285 if (o->type == FIO_OPT_FLOAT_LIST) {
1289 if (o->type == FIO_OPT_STR_SET && o->def && !o->no_warn_def) {
1290 log_err("Option %s: string set option with"
1291 " default will always be true\n", o->name);
1293 if (!o->cb && !o->off1)
1294 log_err("Option %s: neither cb nor offset given\n", o->name);
1296 log_info("Option %s: no category defined. Setting to misc\n", o->name);
1297 o->category = FIO_OPT_C_GENERAL;
1298 o->group = FIO_OPT_G_INVALID;
1300 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1301 o->type == FIO_OPT_STR_MULTI)
1306 * Sanitize the options structure. For now it just sets min/max for bool
1307 * values and whether both callback and offsets are given.
1309 void options_init(struct fio_option *options)
1311 struct fio_option *o;
1313 dprint(FD_PARSE, "init options\n");
1315 for (o = &options[0]; o->name; o++) {
1318 o->inv_opt = find_option(options, o->inverse);
1322 void options_free(struct fio_option *options, void *data)
1324 struct fio_option *o;
1327 dprint(FD_PARSE, "free options\n");
1329 for (o = &options[0]; o->name; o++) {
1330 if (o->type != FIO_OPT_STR_STORE || !o->off1)
1333 ptr = td_var(data, o, o->off1);