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;
141 * Go forward until we hit a non-digit, or +/- sign
143 while ((p - str) <= len) {
144 if (!isdigit((int) *p) && (*p != '+') && (*p != '-'))
149 if (!isalpha((int) *p)) {
157 for (i = 0; i < strlen(c); i++)
158 c[i] = tolower(c[i]);
160 if (!strncmp("us", c, 2) || !strncmp("usec", c, 4))
162 else if (!strncmp("ms", c, 2) || !strncmp("msec", c, 4))
164 else if (!strcmp("s", c))
166 else if (!strcmp("m", c))
167 mult = 60 * 1000000UL;
168 else if (!strcmp("h", c))
169 mult = 60 * 60 * 1000000UL;
170 else if (!strcmp("d", c))
171 mult = 24 * 60 * 60 * 1000000ULL;
177 static int is_separator(char c)
190 static unsigned long long __get_mult_bytes(const char *p, void *data,
193 unsigned int kb_base = fio_get_kb_base(data);
194 unsigned long long ret = 1;
195 unsigned int i, pow = 0, mult = kb_base;
203 for (i = 0; i < strlen(c); i++) {
204 c[i] = tolower(c[i]);
205 if (is_separator(c[i])) {
211 /* If kb_base is 1000, use true units.
212 * If kb_base is 1024, use opposite units.
214 if (!strncmp("pib", c, 3)) {
218 else if (kb_base == 1024)
220 } else if (!strncmp("tib", c, 3)) {
224 else if (kb_base == 1024)
226 } else if (!strncmp("gib", c, 3)) {
230 else if (kb_base == 1024)
232 } else if (!strncmp("mib", c, 3)) {
236 else if (kb_base == 1024)
238 } else if (!strncmp("kib", c, 3)) {
242 else if (kb_base == 1024)
244 } else if (!strncmp("p", c, 1) || !strncmp("pb", c, 2)) {
246 } else if (!strncmp("t", c, 1) || !strncmp("tb", c, 2)) {
248 } else if (!strncmp("g", c, 1) || !strncmp("gb", c, 2)) {
250 } else if (!strncmp("m", c, 1) || !strncmp("mb", c, 2)) {
252 } else if (!strncmp("k", c, 1) || !strncmp("kb", c, 2)) {
254 } else if (!strncmp("%", c, 1)) {
261 ret *= (unsigned long long) mult;
267 static unsigned long long get_mult_bytes(const char *str, int len, void *data,
274 return __get_mult_bytes(str, data, percent);
277 * Go forward until we hit a non-digit, or +/- sign
279 while ((p - str) <= len) {
280 if (!isdigit((int) *p) &&
281 (((*p != '+') && (*p != '-')) || digit_seen))
283 digit_seen |= isdigit((int) *p);
287 if (!isalpha((int) *p) && (*p != '%'))
290 return __get_mult_bytes(p, data, percent);
293 extern int evaluate_arithmetic_expression(const char *buffer, long long *ival,
294 double *dval, double implied_units,
298 * Convert string into a floating number. Return 1 for success and 0 otherwise.
300 int str_to_float(const char *str, double *val, int is_time)
302 #ifdef CONFIG_ARITHMETIC
308 rc = evaluate_arithmetic_expression(str, &ival, &dval, 1.0, is_time);
315 return 1 == sscanf(str, "%lf", val);
319 * convert string into decimal value, noting any size suffix
321 int str_to_decimal(const char *str, long long *val, int kilo, void *data,
322 int is_seconds, int is_time)
326 #ifdef CONFIG_ARITHMETIC
329 double implied_units = 1.0;
336 #ifdef CONFIG_ARITHMETIC
338 implied_units = 1000000.0;
340 rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units, is_time);
341 if (str[0] == '(' && !rc) {
342 if (!kilo && is_seconds)
343 *val = ival / 1000000LL;
350 if (strstr(str, "0x") || strstr(str, "0X"))
355 *val = strtoll(str, NULL, base);
356 if (*val == LONG_MAX && errno == ERANGE)
361 unsigned long long mult;
364 mult = get_mult_bytes(str, len, data, &perc);
370 *val *= get_mult_time(str, len, is_seconds);
375 int check_str_bytes(const char *p, long long *val, void *data)
377 return str_to_decimal(p, val, 1, data, 0, 0);
380 int check_str_time(const char *p, long long *val, int is_seconds)
382 return str_to_decimal(p, val, 0, NULL, is_seconds, 1);
385 void strip_blank_front(char **p)
391 while (isspace((int) *s))
397 void strip_blank_end(char *p)
414 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
420 static int check_range_bytes(const char *str, long *val, void *data)
424 if (!str_to_decimal(str, &__val, 1, data, 0, 0)) {
432 static int check_int(const char *p, int *val)
436 if (strstr(p, "0x") || strstr(p, "0X")) {
437 if (sscanf(p, "%x", val) == 1)
440 if (sscanf(p, "%u", val) == 1)
447 static size_t opt_len(const char *str)
451 postfix = strchr(str, ':');
455 return (int)(postfix - str);
458 static int str_match_len(const struct value_pair *vp, const char *str)
460 return max(strlen(vp->ival), opt_len(str));
463 #define val_store(ptr, val, off, or, data, o) \
465 ptr = td_var((data), (o), (off)); \
472 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
473 int first, int more, int curr)
477 long long ull, *ullp;
481 int ret = 0, is_time = 0;
482 const struct value_pair *vp;
483 struct value_pair posval[PARSE_MAX_VP];
484 int i, all_skipped = 1;
486 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
489 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
490 log_err("Option %s requires an argument\n", o->name);
496 case FIO_OPT_STR_MULTI: {
497 fio_opt_str_fn *fn = o->cb;
499 posval_sort(o, posval);
502 for (i = 0; i < PARSE_MAX_VP; i++) {
504 if (!vp->ival || vp->ival[0] == '\0')
509 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
512 val_store(ilp, vp->oval, o->off1, vp->orval, data, o);
517 if (ret && !all_skipped)
518 show_option_values(o);
523 case FIO_OPT_STR_VAL_TIME:
526 case FIO_OPT_STR_VAL: {
527 fio_opt_str_val_fn *fn = o->cb;
530 if (!is_time && o->is_time)
531 is_time = o->is_time;
533 tmp[sizeof(tmp) - 1] = '\0';
534 strncpy(tmp, ptr, sizeof(tmp) - 1);
535 p = strchr(tmp, ',');
540 ret = check_str_time(tmp, &ull, o->is_seconds);
542 ret = check_str_bytes(tmp, &ull, data);
544 dprint(FD_PARSE, " ret=%d, out=%llu\n", ret, ull);
548 if (o->pow2 && !is_power_of_2(ull)) {
549 log_err("%s: must be a power-of-2\n", o->name);
553 if (o->maxval && ull > o->maxval) {
554 log_err("max value out of range: %llu"
555 " (%u max)\n", ull, o->maxval);
558 if (o->minval && ull < o->minval) {
559 log_err("min value out of range: %llu"
560 " (%u min)\n", ull, o->minval);
563 if (o->posval[0].ival) {
564 posval_sort(o, posval);
567 for (i = 0; i < PARSE_MAX_VP; i++) {
569 if (!vp->ival || vp->ival[0] == '\0')
571 if (vp->oval == ull) {
577 log_err("fio: value %llu not allowed:\n", ull);
578 show_option_values(o);
584 ret = fn(data, &ull);
586 if (o->type == FIO_OPT_INT) {
588 val_store(ilp, ull, o->off1, 0, data, o);
591 val_store(ilp, ull, o->off2, 0, data, o);
595 val_store(ilp, ull, o->off3, 0, data, o);
600 val_store(ilp, ull, o->off2, 0, data, o);
604 val_store(ilp, ull, o->off3, 0, data, o);
609 val_store(ullp, ull, o->off1, 0, data, o);
612 val_store(ullp, ull, o->off2, 0, data, o);
618 case FIO_OPT_FLOAT_LIST: {
623 ** Initialize precision to 0 and zero out list
624 ** in case specified list is shorter than default
628 ilp = td_var(data, o, o->off2);
632 flp = td_var(data, o, o->off1);
633 for(i = 0; i < o->maxlen; i++)
636 if (curr >= o->maxlen) {
637 log_err("the list exceeding max length %d\n",
641 if (!str_to_float(ptr, &uf, 0)) { /* this breaks if we ever have lists of times */
642 log_err("not a floating point value: %s\n", ptr);
646 log_err("value out of range: %f"
647 " (range max: %f)\n", uf, o->maxfp);
651 log_err("value out of range: %f"
652 " (range min: %f)\n", uf, o->minfp);
656 flp = td_var(data, o, o->off1);
659 dprint(FD_PARSE, " out=%f\n", uf);
662 ** Calculate precision for output by counting
663 ** number of digits after period. Find first
664 ** period in entire remaining list each time
666 cp2 = strchr(ptr, '.');
670 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
674 ilp = td_var(data, o, o->off2);
682 case FIO_OPT_STR_STORE: {
683 fio_opt_str_fn *fn = o->cb;
689 cp = td_var(data, o, o->off1);
695 else if (o->posval[0].ival) {
696 posval_sort(o, posval);
699 for (i = 0; i < PARSE_MAX_VP; i++) {
701 if (!vp->ival || vp->ival[0] == '\0' || !cp)
704 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
710 rest = strstr(*cp ?: ptr, ":");
724 show_option_values(o);
733 case FIO_OPT_RANGE: {
737 tmp[sizeof(tmp) - 1] = '\0';
738 strncpy(tmp, ptr, sizeof(tmp) - 1);
740 /* Handle bsrange with separate read,write values: */
741 p1 = strchr(tmp, ',');
745 p1 = strchr(tmp, '-');
747 p1 = strchr(tmp, ':');
759 if (!check_range_bytes(p1, &ul1, data) &&
760 !check_range_bytes(p2, &ul2, data)) {
763 unsigned long foo = ul1;
770 val_store(ilp, ul1, o->off1, 0, data, o);
771 val_store(ilp, ul2, o->off2, 0, data, o);
774 if (o->off3 && o->off4) {
775 val_store(ilp, ul1, o->off3, 0, data, o);
776 val_store(ilp, ul2, o->off4, 0, data, o);
780 if (o->off5 && o->off6) {
781 val_store(ilp, ul1, o->off5, 0, data, o);
782 val_store(ilp, ul2, o->off6, 0, data, o);
787 if (o->off3 && o->off4) {
788 val_store(ilp, ul1, o->off3, 0, data, o);
789 val_store(ilp, ul2, o->off4, 0, data, o);
793 if (o->off5 && o->off6) {
794 val_store(ilp, ul1, o->off5, 0, data, o);
795 val_store(ilp, ul2, o->off6, 0, data, o);
804 case FIO_OPT_STR_SET: {
805 fio_opt_int_fn *fn = o->cb;
808 ret = check_int(ptr, &il);
809 else if (o->type == FIO_OPT_BOOL)
814 dprint(FD_PARSE, " ret=%d, out=%d\n", ret, il);
819 if (o->maxval && il > (int) o->maxval) {
820 log_err("max value out of range: %d (%d max)\n",
824 if (o->minval && il < o->minval) {
825 log_err("min value out of range: %d (%d min)\n",
837 val_store(ilp, il, o->off1, 0, data, o);
840 val_store(ilp, il, o->off2, 0, data, o);
845 case FIO_OPT_DEPRECATED:
846 log_info("Option %s is deprecated\n", o->name);
850 log_err("Bad option type %u\n", o->type);
858 ret = o->verify(o, data);
860 log_err("Correct format for offending option\n");
861 log_err("%20s: %s\n", o->name, o->help);
862 show_option_help(o, 1);
869 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
871 char *o_ptr, *ptr, *ptr2;
874 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
878 o_ptr = ptr = strdup(__ptr);
881 * See if we have another set of parameters, hidden after a comma.
882 * Do this before parsing this round, to check if we should
883 * copy set 1 options to set 2.
892 (o->type != FIO_OPT_STR_STORE) &&
893 (o->type != FIO_OPT_STR) &&
894 (o->type != FIO_OPT_FLOAT_LIST)) {
895 ptr2 = strchr(ptr, ',');
896 if (ptr2 && *(ptr2 + 1) == '\0')
898 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
900 ptr2 = strchr(ptr, ':');
902 ptr2 = strchr(ptr, '-');
904 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
905 ptr2 = strchr(ptr, ':');
909 * Don't return early if parsing the first option fails - if
910 * we are doing multiple arguments, we can allow the first one
913 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
929 struct fio_option *find_option(struct fio_option *options, const char *opt)
931 struct fio_option *o;
933 for (o = &options[0]; o->name; o++) {
934 if (!o_match(o, opt))
936 if (o->type == FIO_OPT_UNSUPPORTED) {
937 log_err("Option <%s>: %s\n", o->name, o->help);
948 static struct fio_option *get_option(char *opt,
949 struct fio_option *options, char **post)
951 struct fio_option *o;
954 ret = strchr(opt, '=');
960 strip_blank_end(ret);
961 o = find_option(options, ret);
963 o = find_option(options, opt);
970 static int opt_cmp(const void *p1, const void *p2)
972 struct fio_option *o;
979 s = strdup(*((char **) p1));
980 o = get_option(s, __fio_options, &foo);
986 s = strdup(*((char **) p2));
987 o = get_option(s, __fio_options, &foo);
993 return prio2 - prio1;
996 void sort_options(char **opts, struct fio_option *options, int num_opts)
998 __fio_options = options;
999 qsort(opts, num_opts, sizeof(char *), opt_cmp);
1000 __fio_options = NULL;
1003 static void add_to_dump_list(struct fio_option *o, struct flist_head *dump_list,
1006 struct print_option *p;
1011 p = malloc(sizeof(*p));
1012 p->name = strdup(o->name);
1014 p->value = strdup(post);
1018 flist_add_tail(&p->list, dump_list);
1021 int parse_cmd_option(const char *opt, const char *val,
1022 struct fio_option *options, void *data,
1023 struct flist_head *dump_list)
1025 struct fio_option *o;
1027 o = find_option(options, opt);
1029 log_err("Bad option <%s>\n", opt);
1033 if (handle_option(o, val, data)) {
1034 log_err("fio: failed parsing %s=%s\n", opt, val);
1038 add_to_dump_list(o, dump_list, val);
1042 int parse_option(char *opt, const char *input,
1043 struct fio_option *options, struct fio_option **o, void *data,
1044 struct flist_head *dump_list)
1049 log_err("fio: failed parsing %s\n", input);
1054 *o = get_option(opt, options, &post);
1057 int len = strlen(opt);
1058 if (opt + len + 1 != post)
1059 memmove(opt + len + 1, post, strlen(post));
1065 if (handle_option(*o, post, data)) {
1066 log_err("fio: failed parsing %s\n", input);
1070 add_to_dump_list(*o, dump_list, post);
1075 * Option match, levenshtein distance. Handy for not quite remembering what
1076 * the option name is.
1078 int string_distance(const char *s1, const char *s2)
1080 unsigned int s1_len = strlen(s1);
1081 unsigned int s2_len = strlen(s2);
1082 unsigned int *p, *q, *r;
1085 p = malloc(sizeof(unsigned int) * (s2_len + 1));
1086 q = malloc(sizeof(unsigned int) * (s2_len + 1));
1089 for (i = 1; i <= s2_len; i++)
1090 p[i] = p[i - 1] + 1;
1092 for (i = 1; i <= s1_len; i++) {
1094 for (j = 1; j <= s2_len; j++) {
1095 unsigned int sub = p[j - 1];
1098 if (s1[i - 1] != s2[j - 1])
1101 pmin = min(q[j - 1] + 1, sub);
1102 q[j] = min(p[j] + 1, pmin);
1116 * Make a guess of whether the distance from 's1' is significant enough
1117 * to warrant printing the guess. We set this to a 1/2 match.
1119 int string_distance_ok(const char *opt, int distance)
1124 len = (len + 1) / 2;
1125 return distance <= len;
1128 static struct fio_option *find_child(struct fio_option *options,
1129 struct fio_option *o)
1131 struct fio_option *__o;
1133 for (__o = options + 1; __o->name; __o++)
1134 if (__o->parent && !strcmp(__o->parent, o->name))
1140 static void __print_option(struct fio_option *o, struct fio_option *org,
1154 p += sprintf(p, "%s", " ");
1156 sprintf(p, "%s", o->name);
1158 log_info("%-24s: %s\n", name, o->help);
1161 static void print_option(struct fio_option *o)
1163 struct fio_option *parent;
1164 struct fio_option *__o;
1165 unsigned int printed;
1168 __print_option(o, NULL, 0);
1175 while ((__o = find_child(o, parent)) != NULL) {
1176 __print_option(__o, o, level);
1185 int show_cmd_help(struct fio_option *options, const char *name)
1187 struct fio_option *o, *closest;
1188 unsigned int best_dist = -1U;
1192 if (!name || !strcmp(name, "all"))
1197 for (o = &options[0]; o->name; o++) {
1200 if (o->type == FIO_OPT_DEPRECATED)
1202 if (!exec_profile && o->prof_name)
1204 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1208 if (!strcmp(name, o->name) ||
1209 (o->alias && !strcmp(name, o->alias)))
1214 dist = string_distance(name, o->name);
1215 if (dist < best_dist) {
1222 if (show_all || match) {
1225 log_info("%20s: %s\n", o->name, o->help);
1236 show_option_help(o, 0);
1242 log_err("No such command: %s", name);
1245 * Only print an appropriately close option, one where the edit
1246 * distance isn't too big. Otherwise we get crazy matches.
1248 if (closest && best_dist < 3) {
1249 log_info(" - showing closest match\n");
1250 log_info("%20s: %s\n", closest->name, closest->help);
1251 show_option_help(closest, 0);
1259 * Handle parsing of default parameters.
1261 void fill_default_options(void *data, struct fio_option *options)
1263 struct fio_option *o;
1265 dprint(FD_PARSE, "filling default options\n");
1267 for (o = &options[0]; o->name; o++)
1269 handle_option(o, o->def, data);
1272 static void option_init(struct fio_option *o)
1274 if (o->type == FIO_OPT_DEPRECATED || o->type == FIO_OPT_UNSUPPORTED)
1276 if (o->name && !o->lname)
1277 log_err("Option %s: missing long option name\n", o->name);
1278 if (o->type == FIO_OPT_BOOL) {
1282 if (o->type == FIO_OPT_INT) {
1284 o->maxval = UINT_MAX;
1286 if (o->type == FIO_OPT_FLOAT_LIST) {
1290 if (o->type == FIO_OPT_STR_SET && o->def && !o->no_warn_def) {
1291 log_err("Option %s: string set option with"
1292 " default will always be true\n", o->name);
1294 if (!o->cb && !o->off1)
1295 log_err("Option %s: neither cb nor offset given\n", o->name);
1297 log_info("Option %s: no category defined. Setting to misc\n", o->name);
1298 o->category = FIO_OPT_C_GENERAL;
1299 o->group = FIO_OPT_G_INVALID;
1301 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1302 o->type == FIO_OPT_STR_MULTI)
1307 * Sanitize the options structure. For now it just sets min/max for bool
1308 * values and whether both callback and offsets are given.
1310 void options_init(struct fio_option *options)
1312 struct fio_option *o;
1314 dprint(FD_PARSE, "init options\n");
1316 for (o = &options[0]; o->name; o++) {
1319 o->inv_opt = find_option(options, o->inverse);
1323 void options_mem_dupe(struct fio_option *options, void *data)
1325 struct fio_option *o;
1328 dprint(FD_PARSE, "dup options\n");
1330 for (o = &options[0]; o->name; o++) {
1331 if (o->type != FIO_OPT_STR_STORE)
1334 ptr = td_var(data, o, o->off1);
1336 *ptr = strdup(*ptr);
1340 void options_free(struct fio_option *options, void *data)
1342 struct fio_option *o;
1345 dprint(FD_PARSE, "free options\n");
1347 for (o = &options[0]; o->name; o++) {
1348 if (o->type != FIO_OPT_STR_STORE || !o->off1)
1351 ptr = td_var(data, o, o->off1);