2 * This file contains the ini and command liner parser main.
12 #include "compiler/compiler.h"
19 #include "lib/ieee754.h"
22 #ifdef CONFIG_ARITHMETIC
26 static const char *opt_type_names[] = {
42 static const struct fio_option *__fio_options;
44 static int vp_cmp(const void *p1, const void *p2)
46 const struct value_pair *vp1 = p1;
47 const struct value_pair *vp2 = p2;
49 return strlen(vp2->ival) - strlen(vp1->ival);
52 static void posval_sort(const struct fio_option *o, struct value_pair *vpmap)
54 const struct value_pair *vp;
57 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
59 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
60 vp = &o->posval[entries];
61 if (!vp->ival || vp->ival[0] == '\0')
64 memcpy(&vpmap[entries], vp, sizeof(*vp));
67 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
70 static void show_option_range(const struct fio_option *o,
71 size_t (*logger)(const char *format, ...))
73 if (o->type == FIO_OPT_FLOAT_LIST) {
74 if (!o->minfp && !o->maxfp)
77 if (o->minfp != DBL_MIN)
78 logger("%20s: min=%f", "range", o->minfp);
79 if (o->maxfp != DBL_MAX)
80 logger(", max=%f", o->maxfp);
82 } else if (!o->posval[0].ival) {
83 if (!o->minval && !o->maxval)
86 logger("%20s: min=%d", "range", o->minval);
88 logger(", max=%d", o->maxval);
93 static void show_option_values(const struct fio_option *o)
97 for (i = 0; i < PARSE_MAX_VP; i++) {
98 const struct value_pair *vp = &o->posval[i];
103 log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
105 log_info(" %s", vp->help);
113 static void show_option_help(const struct fio_option *o, int is_err)
115 const char *typehelp[] = {
119 "string with possible k/m/g postfix (opt=4k)",
120 "string with time postfix (opt=10s)",
122 "string with dual range (opt=1k-4k,4k-8k)",
123 "integer value (opt=100)",
124 "boolean value (opt=1)",
125 "list of floating point values separated by ':' (opt=5.9:7.8)",
130 size_t (*logger)(const char *format, ...);
138 logger("%20s: %s\n", "alias", o->alias);
140 logger("%20s: %s\n", "type", typehelp[o->type]);
141 logger("%20s: %s\n", "default", o->def ? o->def : "no default");
143 logger("%20s: only for profile '%s'\n", "valid", o->prof_name);
144 show_option_range(o, logger);
145 show_option_values(o);
148 static unsigned long long get_mult_time(const char *str, int len,
153 unsigned long long mult = 1;
157 * Go forward until we hit a non-digit, or +/- sign
159 while ((p - str) <= len) {
160 if (!isdigit((int) *p) && (*p != '+') && (*p != '-'))
165 if (!isalpha((int) *p)) {
173 for (i = 0; i < strlen(c); i++)
174 c[i] = tolower((unsigned char)c[i]);
176 if (!strncmp("us", c, 2) || !strncmp("usec", c, 4))
178 else if (!strncmp("ms", c, 2) || !strncmp("msec", c, 4))
180 else if (!strcmp("s", c))
182 else if (!strcmp("m", c))
183 mult = 60 * 1000000UL;
184 else if (!strcmp("h", c))
185 mult = 60 * 60 * 1000000UL;
186 else if (!strcmp("d", c))
187 mult = 24 * 60 * 60 * 1000000ULL;
193 static int is_separator(char c)
206 static unsigned long long __get_mult_bytes(const char *p, void *data,
209 unsigned int kb_base = fio_get_kb_base(data);
210 unsigned long long ret = 1;
211 unsigned int i, pow = 0, mult = kb_base;
219 for (i = 0; i < strlen(c); i++) {
220 c[i] = tolower((unsigned char)c[i]);
221 if (is_separator(c[i])) {
227 /* If kb_base is 1000, use true units.
228 * If kb_base is 1024, use opposite units.
230 if (!strncmp("pib", c, 3)) {
234 else if (kb_base == 1024)
236 } else if (!strncmp("tib", c, 3)) {
240 else if (kb_base == 1024)
242 } else if (!strncmp("gib", c, 3)) {
246 else if (kb_base == 1024)
248 } else if (!strncmp("mib", c, 3)) {
252 else if (kb_base == 1024)
254 } else if (!strncmp("kib", c, 3)) {
258 else if (kb_base == 1024)
260 } else if (!strncmp("p", c, 1) || !strncmp("pb", c, 2)) {
262 } else if (!strncmp("t", c, 1) || !strncmp("tb", c, 2)) {
264 } else if (!strncmp("g", c, 1) || !strncmp("gb", c, 2)) {
266 } else if (!strncmp("m", c, 1) || !strncmp("mb", c, 2)) {
268 } else if (!strncmp("k", c, 1) || !strncmp("kb", c, 2)) {
270 } else if (!strncmp("%", c, 1)) {
277 ret *= (unsigned long long) mult;
283 static unsigned long long get_mult_bytes(const char *str, int len, void *data,
290 return __get_mult_bytes(str, data, percent);
293 * Go forward until we hit a non-digit, or +/- sign
295 while ((p - str) <= len) {
296 if (!isdigit((int) *p) &&
297 (((*p != '+') && (*p != '-')) || digit_seen))
299 digit_seen |= isdigit((int) *p);
303 if (!isalpha((int) *p) && (*p != '%'))
306 return __get_mult_bytes(p, data, percent);
309 extern int evaluate_arithmetic_expression(const char *buffer, long long *ival,
310 double *dval, double implied_units,
314 * Convert string into a floating number. Return 1 for success and 0 otherwise.
316 int str_to_float(const char *str, double *val, int is_time)
318 #ifdef CONFIG_ARITHMETIC
324 rc = evaluate_arithmetic_expression(str, &ival, &dval, 1.0, is_time);
331 return 1 == sscanf(str, "%lf", val);
335 * convert string into decimal value, noting any size suffix
337 int str_to_decimal(const char *str, long long *val, int kilo, void *data,
338 int is_seconds, int is_time)
342 #ifdef CONFIG_ARITHMETIC
345 double implied_units = 1.0;
352 #ifdef CONFIG_ARITHMETIC
354 implied_units = 1000000.0;
356 rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units, is_time);
357 if (str[0] == '(' && !rc) {
358 if (!kilo && is_seconds)
359 *val = ival / 1000000LL;
366 if (strstr(str, "0x") || strstr(str, "0X"))
371 *val = strtoll(str, NULL, base);
372 if (*val == LONG_MAX && errno == ERANGE)
377 unsigned long long mult;
380 mult = get_mult_bytes(str, len, data, &perc);
386 *val *= get_mult_time(str, len, is_seconds);
391 int check_str_bytes(const char *p, long long *val, void *data)
393 return str_to_decimal(p, val, 1, data, 0, 0);
396 int check_str_time(const char *p, long long *val, int is_seconds)
398 return str_to_decimal(p, val, 0, NULL, is_seconds, 1);
401 void strip_blank_front(char **p)
407 while (isspace((int) *s))
413 void strip_blank_end(char *p)
430 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
436 static int check_range_bytes(const char *str, long *val, void *data)
440 if (!str_to_decimal(str, &__val, 1, data, 0, 0)) {
448 static int check_int(const char *p, int *val)
452 if (strstr(p, "0x") || strstr(p, "0X")) {
453 if (sscanf(p, "%x", val) == 1)
456 if (sscanf(p, "%u", val) == 1)
463 static size_t opt_len(const char *str)
467 postfix = strchr(str, ':');
471 return (int)(postfix - str);
474 static int str_match_len(const struct value_pair *vp, const char *str)
476 return max(strlen(vp->ival), opt_len(str));
479 #define val_store(ptr, val, off, or, data, o) \
481 ptr = td_var((data), (o), (off)); \
488 static const char *opt_type_name(const struct fio_option *o)
490 compiletime_assert(ARRAY_SIZE(opt_type_names) - 1 == FIO_OPT_UNSUPPORTED,
491 "opt_type_names[] index");
493 if (o->type <= FIO_OPT_UNSUPPORTED)
494 return opt_type_names[o->type];
496 return "OPT_UNKNOWN?";
499 static int __handle_option(const struct fio_option *o, const char *ptr,
500 void *data, int first, int more, int curr)
504 long long ull, *ullp;
508 int ret = 0, is_time = 0;
509 const struct value_pair *vp;
510 struct value_pair posval[PARSE_MAX_VP];
511 int i, all_skipped = 1;
513 dprint(FD_PARSE, "__handle_option=%s, type=%s, ptr=%s\n", o->name,
514 opt_type_name(o), ptr);
516 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
517 log_err("Option %s requires an argument\n", o->name);
523 case FIO_OPT_STR_MULTI: {
524 fio_opt_str_fn *fn = o->cb;
526 posval_sort(o, posval);
529 for (i = 0; i < PARSE_MAX_VP; i++) {
531 if (!vp->ival || vp->ival[0] == '\0')
536 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
539 val_store(ilp, vp->oval, o->off1, vp->orval, data, o);
544 if (ret && !all_skipped)
545 show_option_values(o);
550 case FIO_OPT_STR_VAL_TIME:
553 case FIO_OPT_STR_VAL: {
554 fio_opt_str_val_fn *fn = o->cb;
557 if (!is_time && o->is_time)
558 is_time = o->is_time;
560 tmp[sizeof(tmp) - 1] = '\0';
561 strncpy(tmp, ptr, sizeof(tmp) - 1);
562 p = strchr(tmp, ',');
567 ret = check_str_time(tmp, &ull, o->is_seconds);
569 ret = check_str_bytes(tmp, &ull, data);
571 dprint(FD_PARSE, " ret=%d, out=%llu\n", ret, ull);
575 if (o->pow2 && !is_power_of_2(ull)) {
576 log_err("%s: must be a power-of-2\n", o->name);
580 if (o->maxval && ull > o->maxval) {
581 log_err("max value out of range: %llu"
582 " (%u max)\n", ull, o->maxval);
585 if (o->minval && ull < o->minval) {
586 log_err("min value out of range: %lld"
587 " (%d min)\n", ull, o->minval);
590 if (o->posval[0].ival) {
591 posval_sort(o, posval);
594 for (i = 0; i < PARSE_MAX_VP; i++) {
596 if (!vp->ival || vp->ival[0] == '\0')
598 if (vp->oval == ull) {
604 log_err("fio: value %llu not allowed:\n", ull);
605 show_option_values(o);
611 ret = fn(data, &ull);
613 if (o->type == FIO_OPT_INT) {
615 val_store(ilp, ull, o->off1, 0, data, o);
618 val_store(ilp, ull, o->off2, 0, data, o);
622 val_store(ilp, ull, o->off3, 0, data, o);
627 val_store(ilp, ull, o->off2, 0, data, o);
631 val_store(ilp, ull, o->off3, 0, data, o);
636 val_store(ullp, ull, o->off1, 0, data, o);
639 val_store(ullp, ull, o->off2, 0, data, o);
645 case FIO_OPT_FLOAT_LIST: {
650 ** Initialize precision to 0 and zero out list
651 ** in case specified list is shorter than default
655 ilp = td_var(data, o, o->off2);
659 flp = td_var(data, o, o->off1);
660 for(i = 0; i < o->maxlen; i++)
663 if (curr >= o->maxlen) {
664 log_err("the list exceeding max length %d\n",
668 if (!str_to_float(ptr, &uf, 0)) { /* this breaks if we ever have lists of times */
669 log_err("not a floating point value: %s\n", ptr);
672 if (o->minfp || o->maxfp) {
674 log_err("value out of range: %f"
675 " (range max: %f)\n", uf, o->maxfp);
679 log_err("value out of range: %f"
680 " (range min: %f)\n", uf, o->minfp);
685 flp = td_var(data, o, o->off1);
688 dprint(FD_PARSE, " out=%f\n", uf);
691 ** Calculate precision for output by counting
692 ** number of digits after period. Find first
693 ** period in entire remaining list each time
695 cp2 = strchr(ptr, '.');
699 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
703 ilp = td_var(data, o, o->off2);
711 case FIO_OPT_STR_STORE: {
712 fio_opt_str_fn *fn = o->cb;
718 cp = td_var(data, o, o->off1);
724 else if (o->posval[0].ival) {
725 posval_sort(o, posval);
728 for (i = 0; i < PARSE_MAX_VP; i++) {
730 if (!vp->ival || vp->ival[0] == '\0' || !cp)
733 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
739 rest = strstr(*cp ?: ptr, ":");
753 show_option_values(o);
762 case FIO_OPT_RANGE: {
766 tmp[sizeof(tmp) - 1] = '\0';
767 strncpy(tmp, ptr, sizeof(tmp) - 1);
769 /* Handle bsrange with separate read,write values: */
770 p1 = strchr(tmp, ',');
774 p1 = strchr(tmp, '-');
776 p1 = strchr(tmp, ':');
788 if (!check_range_bytes(p1, &ul1, data) &&
789 !check_range_bytes(p2, &ul2, data)) {
792 unsigned long foo = ul1;
799 val_store(ilp, ul1, o->off1, 0, data, o);
800 val_store(ilp, ul2, o->off2, 0, data, o);
803 if (o->off3 && o->off4) {
804 val_store(ilp, ul1, o->off3, 0, data, o);
805 val_store(ilp, ul2, o->off4, 0, data, o);
809 if (o->off5 && o->off6) {
810 val_store(ilp, ul1, o->off5, 0, data, o);
811 val_store(ilp, ul2, o->off6, 0, data, o);
816 if (o->off3 && o->off4) {
817 val_store(ilp, ul1, o->off3, 0, data, o);
818 val_store(ilp, ul2, o->off4, 0, data, o);
822 if (o->off5 && o->off6) {
823 val_store(ilp, ul1, o->off5, 0, data, o);
824 val_store(ilp, ul2, o->off6, 0, data, o);
833 case FIO_OPT_STR_SET: {
834 fio_opt_int_fn *fn = o->cb;
837 ret = check_int(ptr, &il);
838 else if (o->type == FIO_OPT_BOOL)
843 dprint(FD_PARSE, " ret=%d, out=%d\n", ret, il);
848 if (o->maxval && il > (int) o->maxval) {
849 log_err("max value out of range: %d (%d max)\n",
853 if (o->minval && il < o->minval) {
854 log_err("min value out of range: %d (%d min)\n",
866 val_store(ilp, il, o->off1, 0, data, o);
869 val_store(ilp, il, o->off2, 0, data, o);
874 case FIO_OPT_DEPRECATED:
875 log_info("Option %s is deprecated\n", o->name);
879 log_err("Bad option type %u\n", o->type);
887 ret = o->verify(o, data);
889 log_err("Correct format for offending option\n");
890 log_err("%20s: %s\n", o->name, o->help);
891 show_option_help(o, 1);
898 static int handle_option(const struct fio_option *o, const char *__ptr,
901 char *o_ptr, *ptr, *ptr2;
904 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
908 o_ptr = ptr = strdup(__ptr);
911 * See if we have another set of parameters, hidden after a comma.
912 * Do this before parsing this round, to check if we should
913 * copy set 1 options to set 2.
922 (o->type != FIO_OPT_STR_STORE) &&
923 (o->type != FIO_OPT_STR) &&
924 (o->type != FIO_OPT_FLOAT_LIST)) {
925 ptr2 = strchr(ptr, ',');
926 if (ptr2 && *(ptr2 + 1) == '\0')
928 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
930 ptr2 = strchr(ptr, ':');
932 ptr2 = strchr(ptr, '-');
934 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
935 ptr2 = strchr(ptr, ':');
939 * Don't return early if parsing the first option fails - if
940 * we are doing multiple arguments, we can allow the first one
943 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
959 struct fio_option *find_option(struct fio_option *options, const char *opt)
961 struct fio_option *o;
963 for (o = &options[0]; o->name; o++) {
964 if (!o_match(o, opt))
966 if (o->type == FIO_OPT_UNSUPPORTED) {
967 log_err("Option <%s>: %s\n", o->name, o->help);
977 const struct fio_option *
978 find_option_c(const struct fio_option *options, const char *opt)
980 return find_option((struct fio_option *)options, opt);
983 static const struct fio_option *
984 get_option(char *opt, const struct fio_option *options, char **post)
986 const struct fio_option *o;
989 ret = strchr(opt, '=');
995 strip_blank_end(ret);
996 o = find_option_c(options, ret);
998 o = find_option_c(options, opt);
1005 static int opt_cmp(const void *p1, const void *p2)
1007 const struct fio_option *o;
1014 s = strdup(*((char **) p1));
1015 o = get_option(s, __fio_options, &foo);
1021 s = strdup(*((char **) p2));
1022 o = get_option(s, __fio_options, &foo);
1028 return prio2 - prio1;
1031 void sort_options(char **opts, const struct fio_option *options, int num_opts)
1033 __fio_options = options;
1034 qsort(opts, num_opts, sizeof(char *), opt_cmp);
1035 __fio_options = NULL;
1038 static void add_to_dump_list(const struct fio_option *o,
1039 struct flist_head *dump_list, const char *post)
1041 struct print_option *p;
1046 p = malloc(sizeof(*p));
1047 p->name = strdup(o->name);
1049 p->value = strdup(post);
1053 flist_add_tail(&p->list, dump_list);
1056 int parse_cmd_option(const char *opt, const char *val,
1057 const struct fio_option *options, void *data,
1058 struct flist_head *dump_list)
1060 const struct fio_option *o;
1062 o = find_option_c(options, opt);
1064 log_err("Bad option <%s>\n", opt);
1068 if (handle_option(o, val, data)) {
1069 log_err("fio: failed parsing %s=%s\n", opt, val);
1073 add_to_dump_list(o, dump_list, val);
1077 int parse_option(char *opt, const char *input, const struct fio_option *options,
1078 const struct fio_option **o, void *data,
1079 struct flist_head *dump_list)
1084 log_err("fio: failed parsing %s\n", input);
1089 *o = get_option(opt, options, &post);
1092 int len = strlen(opt);
1093 if (opt + len + 1 != post)
1094 memmove(opt + len + 1, post, strlen(post));
1100 if (handle_option(*o, post, data)) {
1101 log_err("fio: failed parsing %s\n", input);
1105 add_to_dump_list(*o, dump_list, post);
1110 * Option match, levenshtein distance. Handy for not quite remembering what
1111 * the option name is.
1113 int string_distance(const char *s1, const char *s2)
1115 unsigned int s1_len = strlen(s1);
1116 unsigned int s2_len = strlen(s2);
1117 unsigned int *p, *q, *r;
1120 p = malloc(sizeof(unsigned int) * (s2_len + 1));
1121 q = malloc(sizeof(unsigned int) * (s2_len + 1));
1124 for (i = 1; i <= s2_len; i++)
1125 p[i] = p[i - 1] + 1;
1127 for (i = 1; i <= s1_len; i++) {
1129 for (j = 1; j <= s2_len; j++) {
1130 unsigned int sub = p[j - 1];
1133 if (s1[i - 1] != s2[j - 1])
1136 pmin = min(q[j - 1] + 1, sub);
1137 q[j] = min(p[j] + 1, pmin);
1151 * Make a guess of whether the distance from 's1' is significant enough
1152 * to warrant printing the guess. We set this to a 1/2 match.
1154 int string_distance_ok(const char *opt, int distance)
1159 len = (len + 1) / 2;
1160 return distance <= len;
1163 static const struct fio_option *find_child(const struct fio_option *options,
1164 const struct fio_option *o)
1166 const struct fio_option *__o;
1168 for (__o = options + 1; __o->name; __o++)
1169 if (__o->parent && !strcmp(__o->parent, o->name))
1175 static void __print_option(const struct fio_option *o,
1176 const struct fio_option *org,
1190 p += sprintf(p, "%s", " ");
1192 sprintf(p, "%s", o->name);
1194 log_info("%-24s: %s\n", name, o->help);
1197 static void print_option(const struct fio_option *o)
1199 const struct fio_option *parent;
1200 const struct fio_option *__o;
1201 unsigned int printed;
1204 __print_option(o, NULL, 0);
1211 while ((__o = find_child(o, parent)) != NULL) {
1212 __print_option(__o, o, level);
1221 int show_cmd_help(const struct fio_option *options, const char *name)
1223 const struct fio_option *o, *closest;
1224 unsigned int best_dist = -1U;
1228 if (!name || !strcmp(name, "all"))
1233 for (o = &options[0]; o->name; o++) {
1236 if (o->type == FIO_OPT_DEPRECATED)
1238 if (!exec_profile && o->prof_name)
1240 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1244 if (!strcmp(name, o->name) ||
1245 (o->alias && !strcmp(name, o->alias)))
1250 dist = string_distance(name, o->name);
1251 if (dist < best_dist) {
1258 if (show_all || match) {
1261 log_info("%20s: %s\n", o->name, o->help);
1272 show_option_help(o, 0);
1278 log_err("No such command: %s", name);
1281 * Only print an appropriately close option, one where the edit
1282 * distance isn't too big. Otherwise we get crazy matches.
1284 if (closest && best_dist < 3) {
1285 log_info(" - showing closest match\n");
1286 log_info("%20s: %s\n", closest->name, closest->help);
1287 show_option_help(closest, 0);
1295 * Handle parsing of default parameters.
1297 void fill_default_options(void *data, const struct fio_option *options)
1299 const struct fio_option *o;
1301 dprint(FD_PARSE, "filling default options\n");
1303 for (o = &options[0]; o->name; o++)
1305 handle_option(o, o->def, data);
1308 static void option_init(struct fio_option *o)
1310 if (o->type == FIO_OPT_DEPRECATED || o->type == FIO_OPT_UNSUPPORTED)
1312 if (o->name && !o->lname)
1313 log_err("Option %s: missing long option name\n", o->name);
1314 if (o->type == FIO_OPT_BOOL) {
1318 if (o->type == FIO_OPT_INT) {
1320 o->maxval = UINT_MAX;
1322 if (o->type == FIO_OPT_STR_SET && o->def && !o->no_warn_def) {
1323 log_err("Option %s: string set option with"
1324 " default will always be true\n", o->name);
1326 if (!o->cb && !o->off1)
1327 log_err("Option %s: neither cb nor offset given\n", o->name);
1329 log_info("Option %s: no category defined. Setting to misc\n", o->name);
1330 o->category = FIO_OPT_C_GENERAL;
1331 o->group = FIO_OPT_G_INVALID;
1333 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1334 o->type == FIO_OPT_STR_MULTI)
1339 * Sanitize the options structure. For now it just sets min/max for bool
1340 * values and whether both callback and offsets are given.
1342 void options_init(struct fio_option *options)
1344 struct fio_option *o;
1346 dprint(FD_PARSE, "init options\n");
1348 for (o = &options[0]; o->name; o++) {
1351 o->inv_opt = find_option(options, o->inverse);
1355 void options_mem_dupe(const struct fio_option *options, void *data)
1357 const struct fio_option *o;
1360 dprint(FD_PARSE, "dup options\n");
1362 for (o = &options[0]; o->name; o++) {
1363 if (o->type != FIO_OPT_STR_STORE)
1366 ptr = td_var(data, o, o->off1);
1368 *ptr = strdup(*ptr);
1372 void options_free(const struct fio_option *options, void *data)
1374 const struct fio_option *o;
1377 dprint(FD_PARSE, "free options\n");
1379 for (o = &options[0]; o->name; o++) {
1380 if (o->type != FIO_OPT_STR_STORE || !o->off1 || o->no_free)
1383 ptr = td_var(data, o, o->off1);