2 * This file contains the ini and command liner parser main.
19 #include "lib/ieee754.h"
21 #ifdef CONFIG_ARITHMETIC
25 static struct fio_option *__fio_options;
27 static int vp_cmp(const void *p1, const void *p2)
29 const struct value_pair *vp1 = p1;
30 const struct value_pair *vp2 = p2;
32 return strlen(vp2->ival) - strlen(vp1->ival);
35 static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
37 const struct value_pair *vp;
40 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
42 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
43 vp = &o->posval[entries];
44 if (!vp->ival || vp->ival[0] == '\0')
47 memcpy(&vpmap[entries], vp, sizeof(*vp));
50 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
53 static void show_option_range(struct fio_option *o,
54 int (*logger)(const char *format, ...))
56 if (o->type == FIO_OPT_FLOAT_LIST) {
57 if (o->minfp == DBL_MIN && o->maxfp == DBL_MAX)
60 logger("%20s: min=%f", "range", o->minfp);
61 if (o->maxfp != DBL_MAX)
62 logger(", max=%f", o->maxfp);
64 } else if (!o->posval[0].ival) {
65 if (!o->minval && !o->maxval)
68 logger("%20s: min=%d", "range", o->minval);
70 logger(", max=%d", o->maxval);
75 static void show_option_values(struct fio_option *o)
79 for (i = 0; i < PARSE_MAX_VP; i++) {
80 const struct value_pair *vp = &o->posval[i];
85 log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
87 log_info(" %s", vp->help);
95 static void show_option_help(struct fio_option *o, int is_err)
97 const char *typehelp[] = {
101 "string with possible k/m/g postfix (opt=4k)",
102 "string with time postfix (opt=10s)",
104 "string with dual range (opt=1k-4k,4k-8k)",
105 "integer value (opt=100)",
106 "boolean value (opt=1)",
107 "list of floating point values separated by ':' (opt=5.9:7.8)",
111 int (*logger)(const char *format, ...);
119 logger("%20s: %s\n", "alias", o->alias);
121 logger("%20s: %s\n", "type", typehelp[o->type]);
122 logger("%20s: %s\n", "default", o->def ? o->def : "no default");
124 logger("%20s: only for profile '%s'\n", "valid", o->prof_name);
125 show_option_range(o, logger);
126 show_option_values(o);
129 static unsigned long long get_mult_time(const char *str, int len,
134 unsigned long long mult = 1;
137 * Go forward until we hit a non-digit, or +/- sign
139 while ((p - str) <= len) {
140 if (!isdigit((int) *p) && (*p != '+') && (*p != '-'))
145 if (!isalpha((int) *p)) {
153 for (int i = 0; i < strlen(c); i++)
154 c[i] = tolower(c[i]);
156 if (!strncmp("us", c, 2) || !strncmp("usec", c, 4))
158 else if (!strncmp("ms", c, 2) || !strncmp("msec", c, 4))
160 else if (!strcmp("s", c))
162 else if (!strcmp("m", c))
163 mult = 60 * 1000000UL;
164 else if (!strcmp("h", c))
165 mult = 60 * 60 * 1000000UL;
166 else if (!strcmp("d", c))
167 mult = 24 * 60 * 60 * 1000000UL;
173 static int is_separator(char c)
186 static unsigned long long __get_mult_bytes(const char *p, void *data,
189 unsigned int kb_base = fio_get_kb_base(data);
190 unsigned long long ret = 1;
191 unsigned int i, pow = 0, mult = kb_base;
199 for (i = 0; i < strlen(c); i++) {
200 c[i] = tolower(c[i]);
201 if (is_separator(c[i])) {
207 if (!strncmp("pib", c, 3)) {
210 } else if (!strncmp("tib", c, 3)) {
213 } else if (!strncmp("gib", c, 3)) {
216 } else if (!strncmp("mib", c, 3)) {
219 } else if (!strncmp("kib", c, 3)) {
222 } else if (!strncmp("p", c, 1) || !strncmp("pb", c, 2))
224 else if (!strncmp("t", c, 1) || !strncmp("tb", c, 2))
226 else if (!strncmp("g", c, 1) || !strncmp("gb", c, 2))
228 else if (!strncmp("m", c, 1) || !strncmp("mb", c, 2))
230 else if (!strncmp("k", c, 1) || !strncmp("kb", c, 2))
232 else if (!strncmp("%", c, 1)) {
239 ret *= (unsigned long long) mult;
245 static unsigned long long get_mult_bytes(const char *str, int len, void *data,
252 return __get_mult_bytes(str, data, percent);
255 * Go forward until we hit a non-digit, or +/- sign
257 while ((p - str) <= len) {
258 if (!isdigit((int) *p) &&
259 (((*p != '+') && (*p != '-')) || digit_seen))
261 digit_seen |= isdigit((int) *p);
265 if (!isalpha((int) *p) && (*p != '%'))
268 return __get_mult_bytes(p, data, percent);
271 extern int evaluate_arithmetic_expression(const char *buffer, long long *ival,
275 * Convert string into a floating number. Return 1 for success and 0 otherwise.
277 int str_to_float(const char *str, double *val)
279 #ifdef CONFIG_ARITHMETIC
285 rc = evaluate_arithmetic_expression(str, &ival, &dval);
292 return 1 == sscanf(str, "%lf", val);
296 * convert string into decimal value, noting any size suffix
298 int str_to_decimal(const char *str, long long *val, int kilo, void *data,
303 #ifdef CONFIG_ARITHMETIC
312 #ifdef CONFIG_ARITHMETIC
314 rc = evaluate_arithmetic_expression(str, &ival, &dval);
315 if (str[0] == '(' && !rc)
320 if (strstr(str, "0x") || strstr(str, "0X"))
325 *val = strtoll(str, NULL, base);
326 if (*val == LONG_MAX && errno == ERANGE)
331 unsigned long long mult;
334 mult = get_mult_bytes(str, len, data, &perc);
340 *val *= get_mult_time(str, len, is_seconds);
345 int check_str_bytes(const char *p, long long *val, void *data)
347 return str_to_decimal(p, val, 1, data, 0);
350 int check_str_time(const char *p, long long *val, int is_seconds)
352 return str_to_decimal(p, val, 0, NULL, is_seconds);
355 void strip_blank_front(char **p)
361 while (isspace((int) *s))
367 void strip_blank_end(char *p)
384 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
390 static int check_range_bytes(const char *str, long *val, void *data)
394 if (!str_to_decimal(str, &__val, 1, data, 0)) {
402 static int check_int(const char *p, int *val)
406 if (strstr(p, "0x") || strstr(p, "0X")) {
407 if (sscanf(p, "%x", val) == 1)
410 if (sscanf(p, "%u", val) == 1)
417 static size_t opt_len(const char *str)
421 postfix = strchr(str, ':');
425 return (int)(postfix - str);
428 static int str_match_len(const struct value_pair *vp, const char *str)
430 return max(strlen(vp->ival), opt_len(str));
433 #define val_store(ptr, val, off, or, data, o) \
435 ptr = td_var((data), (o), (off)); \
442 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
443 int first, int more, int curr)
447 long long ull, *ullp;
451 int ret = 0, is_time = 0;
452 const struct value_pair *vp;
453 struct value_pair posval[PARSE_MAX_VP];
454 int i, all_skipped = 1;
456 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
459 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
460 log_err("Option %s requires an argument\n", o->name);
466 case FIO_OPT_STR_MULTI: {
467 fio_opt_str_fn *fn = o->cb;
469 posval_sort(o, posval);
472 for (i = 0; i < PARSE_MAX_VP; i++) {
474 if (!vp->ival || vp->ival[0] == '\0')
477 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
480 val_store(ilp, vp->oval, o->off1, vp->orval, data, o);
485 if (ret && !all_skipped)
486 show_option_values(o);
491 case FIO_OPT_STR_VAL_TIME:
494 case FIO_OPT_STR_VAL: {
495 fio_opt_str_val_fn *fn = o->cb;
498 strncpy(tmp, ptr, sizeof(tmp) - 1);
499 p = strchr(tmp, ',');
504 ret = check_str_time(tmp, &ull, o->is_seconds);
506 ret = check_str_bytes(tmp, &ull, data);
508 dprint(FD_PARSE, " ret=%d, out=%llu\n", ret, ull);
513 if (o->maxval && ull > o->maxval) {
514 log_err("max value out of range: %llu"
515 " (%u max)\n", ull, o->maxval);
518 if (o->minval && ull < o->minval) {
519 log_err("min value out of range: %llu"
520 " (%u min)\n", ull, o->minval);
523 if (o->posval[0].ival) {
524 posval_sort(o, posval);
527 for (i = 0; i < PARSE_MAX_VP; i++) {
529 if (!vp->ival || vp->ival[0] == '\0')
531 if (vp->oval == ull) {
537 log_err("fio: value %llu not allowed:\n", ull);
538 show_option_values(o);
544 ret = fn(data, &ull);
546 if (o->type == FIO_OPT_INT) {
548 val_store(ilp, ull, o->off1, 0, data, o);
551 val_store(ilp, ull, o->off2, 0, data, o);
555 val_store(ilp, ull, o->off3, 0, data, o);
560 val_store(ilp, ull, o->off2, 0, data, o);
564 val_store(ilp, ull, o->off3, 0, data, o);
569 val_store(ullp, ull, o->off1, 0, data, o);
572 val_store(ullp, ull, o->off2, 0, data, o);
578 case FIO_OPT_FLOAT_LIST: {
583 ** Initialize precision to 0 and zero out list
584 ** in case specified list is shorter than default
588 ilp = td_var(data, o, o->off2);
592 flp = td_var(data, o, o->off1);
593 for(i = 0; i < o->maxlen; i++)
596 if (curr >= o->maxlen) {
597 log_err("the list exceeding max length %d\n",
601 if (!str_to_float(ptr, &uf)) {
602 log_err("not a floating point value: %s\n", ptr);
606 log_err("value out of range: %f"
607 " (range max: %f)\n", uf, o->maxfp);
611 log_err("value out of range: %f"
612 " (range min: %f)\n", uf, o->minfp);
616 flp = td_var(data, o, o->off1);
619 dprint(FD_PARSE, " out=%f\n", uf);
622 ** Calculate precision for output by counting
623 ** number of digits after period. Find first
624 ** period in entire remaining list each time
626 cp2 = strchr(ptr, '.');
630 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
634 ilp = td_var(data, o, o->off2);
642 case FIO_OPT_STR_STORE: {
643 fio_opt_str_fn *fn = o->cb;
649 cp = td_var(data, o, o->off1);
655 else if (o->posval[0].ival) {
656 posval_sort(o, posval);
659 for (i = 0; i < PARSE_MAX_VP; i++) {
661 if (!vp->ival || vp->ival[0] == '\0' || !cp)
664 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
670 rest = strstr(*cp ?: ptr, ":");
684 show_option_values(o);
693 case FIO_OPT_RANGE: {
697 strncpy(tmp, ptr, sizeof(tmp) - 1);
699 /* Handle bsrange with separate read,write values: */
700 p1 = strchr(tmp, ',');
704 p1 = strchr(tmp, '-');
706 p1 = strchr(tmp, ':');
718 if (!check_range_bytes(p1, &ul1, data) &&
719 !check_range_bytes(p2, &ul2, data)) {
722 unsigned long foo = ul1;
729 val_store(ilp, ul1, o->off1, 0, data, o);
730 val_store(ilp, ul2, o->off2, 0, data, o);
733 if (o->off3 && o->off4) {
734 val_store(ilp, ul1, o->off3, 0, data, o);
735 val_store(ilp, ul2, o->off4, 0, data, o);
739 if (o->off5 && o->off6) {
740 val_store(ilp, ul1, o->off5, 0, data, o);
741 val_store(ilp, ul2, o->off6, 0, data, o);
746 if (o->off3 && o->off4) {
747 val_store(ilp, ul1, o->off3, 0, data, o);
748 val_store(ilp, ul2, o->off4, 0, data, o);
752 if (o->off5 && o->off6) {
753 val_store(ilp, ul1, o->off5, 0, data, o);
754 val_store(ilp, ul2, o->off6, 0, data, o);
763 case FIO_OPT_STR_SET: {
764 fio_opt_int_fn *fn = o->cb;
767 ret = check_int(ptr, &il);
768 else if (o->type == FIO_OPT_BOOL)
773 dprint(FD_PARSE, " ret=%d, out=%d\n", ret, il);
778 if (o->maxval && il > (int) o->maxval) {
779 log_err("max value out of range: %d (%d max)\n",
783 if (o->minval && il < o->minval) {
784 log_err("min value out of range: %d (%d min)\n",
796 val_store(ilp, il, o->off1, 0, data, o);
799 val_store(ilp, il, o->off2, 0, data, o);
804 case FIO_OPT_DEPRECATED:
805 log_info("Option %s is deprecated\n", o->name);
809 log_err("Bad option type %u\n", o->type);
817 ret = o->verify(o, data);
819 log_err("Correct format for offending option\n");
820 log_err("%20s: %s\n", o->name, o->help);
821 show_option_help(o, 1);
828 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
830 char *o_ptr, *ptr, *ptr2;
833 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
837 o_ptr = ptr = strdup(__ptr);
840 * See if we have another set of parameters, hidden after a comma.
841 * Do this before parsing this round, to check if we should
842 * copy set 1 options to set 2.
851 (o->type != FIO_OPT_STR_STORE) &&
852 (o->type != FIO_OPT_STR) &&
853 (o->type != FIO_OPT_FLOAT_LIST)) {
854 ptr2 = strchr(ptr, ',');
855 if (ptr2 && *(ptr2 + 1) == '\0')
857 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
859 ptr2 = strchr(ptr, ':');
861 ptr2 = strchr(ptr, '-');
863 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
864 ptr2 = strchr(ptr, ':');
868 * Don't return early if parsing the first option fails - if
869 * we are doing multiple arguments, we can allow the first one
872 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
888 static struct fio_option *get_option(char *opt,
889 struct fio_option *options, char **post)
891 struct fio_option *o;
894 ret = strchr(opt, '=');
900 strip_blank_end(ret);
901 o = find_option(options, ret);
903 o = find_option(options, opt);
910 static int opt_cmp(const void *p1, const void *p2)
912 struct fio_option *o;
919 s = strdup(*((char **) p1));
920 o = get_option(s, __fio_options, &foo);
926 s = strdup(*((char **) p2));
927 o = get_option(s, __fio_options, &foo);
933 return prio2 - prio1;
936 void sort_options(char **opts, struct fio_option *options, int num_opts)
938 __fio_options = options;
939 qsort(opts, num_opts, sizeof(char *), opt_cmp);
940 __fio_options = NULL;
943 int parse_cmd_option(const char *opt, const char *val,
944 struct fio_option *options, void *data)
946 struct fio_option *o;
948 o = find_option(options, opt);
950 log_err("Bad option <%s>\n", opt);
954 if (!handle_option(o, val, data))
957 log_err("fio: failed parsing %s=%s\n", opt, val);
961 int parse_option(char *opt, const char *input,
962 struct fio_option *options, struct fio_option **o, void *data,
968 log_err("fio: failed parsing %s\n", input);
973 *o = get_option(opt, options, &post);
976 int len = strlen(opt);
977 if (opt + len + 1 != post)
978 memmove(opt + len + 1, post, strlen(post));
984 if (handle_option(*o, post, data)) {
985 log_err("fio: failed parsing %s\n", input);
992 if (!strcmp("description", (*o)->name))
997 log_info("--%s%s", (*o)->name, post ? "" : " ");
999 log_info("=%s%s%s ", delim, post, delim);
1006 * Option match, levenshtein distance. Handy for not quite remembering what
1007 * the option name is.
1009 static int string_distance(const char *s1, const char *s2)
1011 unsigned int s1_len = strlen(s1);
1012 unsigned int s2_len = strlen(s2);
1013 unsigned int *p, *q, *r;
1016 p = malloc(sizeof(unsigned int) * (s2_len + 1));
1017 q = malloc(sizeof(unsigned int) * (s2_len + 1));
1020 for (i = 1; i <= s2_len; i++)
1021 p[i] = p[i - 1] + 1;
1023 for (i = 1; i <= s1_len; i++) {
1025 for (j = 1; j <= s2_len; j++) {
1026 unsigned int sub = p[j - 1];
1028 if (s1[i - 1] != s2[j - 1])
1031 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
1044 static struct fio_option *find_child(struct fio_option *options,
1045 struct fio_option *o)
1047 struct fio_option *__o;
1049 for (__o = options + 1; __o->name; __o++)
1050 if (__o->parent && !strcmp(__o->parent, o->name))
1056 static void __print_option(struct fio_option *o, struct fio_option *org,
1070 p += sprintf(p, "%s", " ");
1072 sprintf(p, "%s", o->name);
1074 log_info("%-24s: %s\n", name, o->help);
1077 static void print_option(struct fio_option *o)
1079 struct fio_option *parent;
1080 struct fio_option *__o;
1081 unsigned int printed;
1084 __print_option(o, NULL, 0);
1091 while ((__o = find_child(o, parent)) != NULL) {
1092 __print_option(__o, o, level);
1101 int show_cmd_help(struct fio_option *options, const char *name)
1103 struct fio_option *o, *closest;
1104 unsigned int best_dist = -1U;
1108 if (!name || !strcmp(name, "all"))
1113 for (o = &options[0]; o->name; o++) {
1116 if (o->type == FIO_OPT_DEPRECATED)
1118 if (!exec_profile && o->prof_name)
1120 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1124 if (!strcmp(name, o->name) ||
1125 (o->alias && !strcmp(name, o->alias)))
1130 dist = string_distance(name, o->name);
1131 if (dist < best_dist) {
1138 if (show_all || match) {
1141 log_info("%20s: %s\n", o->name, o->help);
1152 show_option_help(o, 0);
1158 log_err("No such command: %s", name);
1161 * Only print an appropriately close option, one where the edit
1162 * distance isn't too big. Otherwise we get crazy matches.
1164 if (closest && best_dist < 3) {
1165 log_info(" - showing closest match\n");
1166 log_info("%20s: %s\n", closest->name, closest->help);
1167 show_option_help(closest, 0);
1175 * Handle parsing of default parameters.
1177 void fill_default_options(void *data, struct fio_option *options)
1179 struct fio_option *o;
1181 dprint(FD_PARSE, "filling default options\n");
1183 for (o = &options[0]; o->name; o++)
1185 handle_option(o, o->def, data);
1188 void option_init(struct fio_option *o)
1190 if (o->type == FIO_OPT_DEPRECATED)
1192 if (o->type == FIO_OPT_BOOL) {
1196 if (o->type == FIO_OPT_INT) {
1198 o->maxval = UINT_MAX;
1200 if (o->type == FIO_OPT_FLOAT_LIST) {
1204 if (o->type == FIO_OPT_STR_SET && o->def && !o->no_warn_def) {
1205 log_err("Option %s: string set option with"
1206 " default will always be true\n", o->name);
1208 if (!o->cb && !o->off1)
1209 log_err("Option %s: neither cb nor offset given\n", o->name);
1211 log_info("Option %s: no category defined. Setting to misc\n", o->name);
1212 o->category = FIO_OPT_C_GENERAL;
1213 o->group = FIO_OPT_G_INVALID;
1215 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1216 o->type == FIO_OPT_STR_MULTI)
1218 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
1219 log_err("Option %s: both cb and offset given\n", o->name);
1223 * Sanitize the options structure. For now it just sets min/max for bool
1224 * values and whether both callback and offsets are given.
1226 void options_init(struct fio_option *options)
1228 struct fio_option *o;
1230 dprint(FD_PARSE, "init options\n");
1232 for (o = &options[0]; o->name; o++) {
1235 o->inv_opt = find_option(options, o->inverse);
1239 void options_free(struct fio_option *options, void *data)
1241 struct fio_option *o;
1244 dprint(FD_PARSE, "free options\n");
1246 for (o = &options[0]; o->name; o++) {
1247 if (o->type != FIO_OPT_STR_STORE || !o->off1)
1250 ptr = td_var(data, o, o->off1);