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) {
75 if (!o->minfp && !o->maxfp)
78 logger("%20s: ", "range");
79 if (o->minfp != DBL_MIN) {
80 logger("min=%f", o->minfp);
83 if (o->maxfp != DBL_MAX)
84 logger("%smax=%f", sep, o->maxfp);
86 } else if (!o->posval[0].ival) {
87 if (!o->minval && !o->maxval)
90 logger("%20s: min=%d", "range", o->minval);
92 logger(", max=%d", o->maxval);
97 static void show_option_values(const struct fio_option *o)
101 for (i = 0; i < PARSE_MAX_VP; i++) {
102 const struct value_pair *vp = &o->posval[i];
107 log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
109 log_info(" %s", vp->help);
117 static void show_option_help(const struct fio_option *o, int is_err)
119 const char *typehelp[] = {
123 "string with possible k/m/g postfix (opt=4k)",
124 "string with time postfix (opt=10s)",
126 "string with dual range (opt=1k-4k,4k-8k)",
127 "integer value (opt=100)",
128 "boolean value (opt=1)",
129 "list of floating point values separated by ':' (opt=5.9:7.8)",
134 size_t (*logger)(const char *format, ...);
142 logger("%20s: %s\n", "alias", o->alias);
144 logger("%20s: %s\n", "type", typehelp[o->type]);
145 logger("%20s: %s\n", "default", o->def ? o->def : "no default");
147 logger("%20s: only for profile '%s'\n", "valid", o->prof_name);
148 show_option_range(o, logger);
149 show_option_values(o);
152 static unsigned long long get_mult_time(const char *str, int len,
157 unsigned long long mult = 1;
161 * Go forward until we hit a non-digit, or +/- sign
163 while ((p - str) <= len) {
164 if (!isdigit((int) *p) && (*p != '+') && (*p != '-'))
169 if (!isalpha((int) *p)) {
177 for (i = 0; i < strlen(c); i++)
178 c[i] = tolower((unsigned char)c[i]);
180 if (!strncmp("us", c, 2) || !strncmp("usec", c, 4))
182 else if (!strncmp("ms", c, 2) || !strncmp("msec", c, 4))
184 else if (!strcmp("s", c))
186 else if (!strcmp("m", c))
187 mult = 60 * 1000000UL;
188 else if (!strcmp("h", c))
189 mult = 60 * 60 * 1000000UL;
190 else if (!strcmp("d", c))
191 mult = 24 * 60 * 60 * 1000000ULL;
197 static int is_separator(char c)
210 static unsigned long long __get_mult_bytes(const char *p, void *data,
213 unsigned int kb_base = fio_get_kb_base(data);
214 unsigned long long ret = 1;
215 unsigned int i, pow = 0, mult = kb_base;
223 for (i = 0; i < strlen(c); i++) {
224 c[i] = tolower((unsigned char)c[i]);
225 if (is_separator(c[i])) {
231 /* If kb_base is 1000, use true units.
232 * If kb_base is 1024, use opposite units.
234 if (!strncmp("pib", c, 3)) {
238 else if (kb_base == 1024)
240 } else if (!strncmp("tib", c, 3)) {
244 else if (kb_base == 1024)
246 } else if (!strncmp("gib", c, 3)) {
250 else if (kb_base == 1024)
252 } else if (!strncmp("mib", c, 3)) {
256 else if (kb_base == 1024)
258 } else if (!strncmp("kib", c, 3)) {
262 else if (kb_base == 1024)
264 } else if (!strncmp("p", c, 1) || !strncmp("pb", c, 2)) {
266 } else if (!strncmp("t", c, 1) || !strncmp("tb", c, 2)) {
268 } else if (!strncmp("g", c, 1) || !strncmp("gb", c, 2)) {
270 } else if (!strncmp("m", c, 1) || !strncmp("mb", c, 2)) {
272 } else if (!strncmp("k", c, 1) || !strncmp("kb", c, 2)) {
274 } else if (!strncmp("%", c, 1)) {
281 ret *= (unsigned long long) mult;
287 static unsigned long long get_mult_bytes(const char *str, int len, void *data,
294 return __get_mult_bytes(str, data, percent);
297 * Go forward until we hit a non-digit, or +/- sign
299 while ((p - str) <= len) {
300 if (!isdigit((int) *p) &&
301 (((*p != '+') && (*p != '-')) || digit_seen))
303 digit_seen |= isdigit((int) *p);
307 if (!isalpha((int) *p) && (*p != '%'))
310 return __get_mult_bytes(p, data, percent);
313 extern int evaluate_arithmetic_expression(const char *buffer, long long *ival,
314 double *dval, double implied_units,
318 * Convert string into a floating number. Return 1 for success and 0 otherwise.
320 int str_to_float(const char *str, double *val, int is_time)
322 #ifdef CONFIG_ARITHMETIC
328 rc = evaluate_arithmetic_expression(str, &ival, &dval, 1.0, is_time);
335 return 1 == sscanf(str, "%lf", val);
339 * convert string into decimal value, noting any size suffix
341 int str_to_decimal(const char *str, long long *val, int kilo, void *data,
342 int is_seconds, int is_time)
346 #ifdef CONFIG_ARITHMETIC
349 double implied_units = 1.0;
356 #ifdef CONFIG_ARITHMETIC
358 implied_units = 1000000.0;
360 rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units, is_time);
361 if (str[0] == '(' && !rc) {
362 if (!kilo && is_seconds)
363 *val = ival / 1000000LL;
370 if (strstr(str, "0x") || strstr(str, "0X"))
375 *val = strtoll(str, NULL, base);
376 if (*val == LONG_MAX && errno == ERANGE)
381 unsigned long long mult;
384 mult = get_mult_bytes(str, len, data, &perc);
390 *val *= get_mult_time(str, len, is_seconds);
395 int check_str_bytes(const char *p, long long *val, void *data)
397 return str_to_decimal(p, val, 1, data, 0, 0);
400 int check_str_time(const char *p, long long *val, int is_seconds)
402 return str_to_decimal(p, val, 0, NULL, is_seconds, 1);
405 void strip_blank_front(char **p)
411 while (isspace((int) *s))
417 void strip_blank_end(char *p)
434 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
440 static int check_range_bytes(const char *str, long *val, void *data)
444 if (!str_to_decimal(str, &__val, 1, data, 0, 0)) {
452 static int check_int(const char *p, int *val)
456 if (strstr(p, "0x") || strstr(p, "0X")) {
457 if (sscanf(p, "%x", val) == 1)
460 if (sscanf(p, "%u", val) == 1)
467 static size_t opt_len(const char *str)
471 postfix = strchr(str, ':');
475 return (int)(postfix - str);
478 static int str_match_len(const struct value_pair *vp, const char *str)
480 return max(strlen(vp->ival), opt_len(str));
483 #define val_store(ptr, val, off, or, data, o) \
485 ptr = td_var((data), (o), (off)); \
492 static const char *opt_type_name(const struct fio_option *o)
494 compiletime_assert(ARRAY_SIZE(opt_type_names) - 1 == FIO_OPT_UNSUPPORTED,
495 "opt_type_names[] index");
497 if (o->type <= FIO_OPT_UNSUPPORTED)
498 return opt_type_names[o->type];
500 return "OPT_UNKNOWN?";
503 static int __handle_option(const struct fio_option *o, const char *ptr,
504 void *data, int first, int more, int curr)
508 long long ull, *ullp;
512 int ret = 0, is_time = 0;
513 const struct value_pair *vp;
514 struct value_pair posval[PARSE_MAX_VP];
515 int i, all_skipped = 1;
517 dprint(FD_PARSE, "__handle_option=%s, type=%s, ptr=%s\n", o->name,
518 opt_type_name(o), ptr);
520 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
521 log_err("Option %s requires an argument\n", o->name);
527 case FIO_OPT_STR_MULTI: {
528 fio_opt_str_fn *fn = o->cb;
530 posval_sort(o, posval);
533 for (i = 0; i < PARSE_MAX_VP; i++) {
535 if (!vp->ival || vp->ival[0] == '\0')
540 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
543 val_store(ilp, vp->oval, o->off1, vp->orval, data, o);
548 if (ret && !all_skipped)
549 show_option_values(o);
554 case FIO_OPT_STR_VAL_TIME:
557 case FIO_OPT_STR_VAL: {
558 fio_opt_str_val_fn *fn = o->cb;
561 if (!is_time && o->is_time)
562 is_time = o->is_time;
564 tmp[sizeof(tmp) - 1] = '\0';
565 strncpy(tmp, ptr, sizeof(tmp) - 1);
566 p = strchr(tmp, ',');
571 ret = check_str_time(tmp, &ull, o->is_seconds);
573 ret = check_str_bytes(tmp, &ull, data);
575 dprint(FD_PARSE, " ret=%d, out=%llu\n", ret, ull);
579 if (o->pow2 && !is_power_of_2(ull)) {
580 log_err("%s: must be a power-of-2\n", o->name);
584 if (o->maxval && ull > o->maxval) {
585 log_err("max value out of range: %llu"
586 " (%u max)\n", ull, o->maxval);
589 if (o->minval && ull < o->minval) {
590 log_err("min value out of range: %lld"
591 " (%d min)\n", ull, o->minval);
594 if (o->posval[0].ival) {
595 posval_sort(o, posval);
598 for (i = 0; i < PARSE_MAX_VP; i++) {
600 if (!vp->ival || vp->ival[0] == '\0')
602 if (vp->oval == ull) {
608 log_err("fio: value %llu not allowed:\n", ull);
609 show_option_values(o);
615 ret = fn(data, &ull);
617 if (o->type == FIO_OPT_INT) {
619 val_store(ilp, ull, o->off1, 0, data, o);
622 val_store(ilp, ull, o->off2, 0, data, o);
626 val_store(ilp, ull, o->off3, 0, data, o);
631 val_store(ilp, ull, o->off2, 0, data, o);
635 val_store(ilp, ull, o->off3, 0, data, o);
640 val_store(ullp, ull, o->off1, 0, data, o);
643 val_store(ullp, ull, o->off2, 0, data, o);
649 case FIO_OPT_FLOAT_LIST: {
654 ** Initialize precision to 0 and zero out list
655 ** in case specified list is shorter than default
659 ilp = td_var(data, o, o->off2);
663 flp = td_var(data, o, o->off1);
664 for(i = 0; i < o->maxlen; i++)
667 if (curr >= o->maxlen) {
668 log_err("the list exceeding max length %d\n",
672 if (!str_to_float(ptr, &uf, 0)) { /* this breaks if we ever have lists of times */
673 log_err("not a floating point value: %s\n", ptr);
676 if (o->minfp || o->maxfp) {
678 log_err("value out of range: %f"
679 " (range max: %f)\n", uf, o->maxfp);
683 log_err("value out of range: %f"
684 " (range min: %f)\n", uf, o->minfp);
689 flp = td_var(data, o, o->off1);
692 dprint(FD_PARSE, " out=%f\n", uf);
695 ** Calculate precision for output by counting
696 ** number of digits after period. Find first
697 ** period in entire remaining list each time
699 cp2 = strchr(ptr, '.');
703 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
707 ilp = td_var(data, o, o->off2);
715 case FIO_OPT_STR_STORE: {
716 fio_opt_str_fn *fn = o->cb;
722 cp = td_var(data, o, o->off1);
728 else if (o->posval[0].ival) {
729 posval_sort(o, posval);
732 for (i = 0; i < PARSE_MAX_VP; i++) {
734 if (!vp->ival || vp->ival[0] == '\0' || !cp)
737 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
743 rest = strstr(*cp ?: ptr, ":");
757 show_option_values(o);
766 case FIO_OPT_RANGE: {
770 tmp[sizeof(tmp) - 1] = '\0';
771 strncpy(tmp, ptr, sizeof(tmp) - 1);
773 /* Handle bsrange with separate read,write values: */
774 p1 = strchr(tmp, ',');
778 p1 = strchr(tmp, '-');
780 p1 = strchr(tmp, ':');
792 if (!check_range_bytes(p1, &ul1, data) &&
793 !check_range_bytes(p2, &ul2, data)) {
796 unsigned long foo = ul1;
803 val_store(ilp, ul1, o->off1, 0, data, o);
804 val_store(ilp, ul2, o->off2, 0, data, o);
807 if (o->off3 && o->off4) {
808 val_store(ilp, ul1, o->off3, 0, data, o);
809 val_store(ilp, ul2, o->off4, 0, data, o);
813 if (o->off5 && o->off6) {
814 val_store(ilp, ul1, o->off5, 0, data, o);
815 val_store(ilp, ul2, o->off6, 0, data, o);
820 if (o->off3 && o->off4) {
821 val_store(ilp, ul1, o->off3, 0, data, o);
822 val_store(ilp, ul2, o->off4, 0, data, o);
826 if (o->off5 && o->off6) {
827 val_store(ilp, ul1, o->off5, 0, data, o);
828 val_store(ilp, ul2, o->off6, 0, data, o);
837 case FIO_OPT_STR_SET: {
838 fio_opt_int_fn *fn = o->cb;
841 ret = check_int(ptr, &il);
842 else if (o->type == FIO_OPT_BOOL)
847 dprint(FD_PARSE, " ret=%d, out=%d\n", ret, il);
852 if (o->maxval && il > (int) o->maxval) {
853 log_err("max value out of range: %d (%d max)\n",
857 if (o->minval && il < o->minval) {
858 log_err("min value out of range: %d (%d min)\n",
870 val_store(ilp, il, o->off1, 0, data, o);
873 val_store(ilp, il, o->off2, 0, data, o);
878 case FIO_OPT_DEPRECATED:
879 log_info("Option %s is deprecated\n", o->name);
883 log_err("Bad option type %u\n", o->type);
891 ret = o->verify(o, data);
893 log_err("Correct format for offending option\n");
894 log_err("%20s: %s\n", o->name, o->help);
895 show_option_help(o, 1);
902 static int handle_option(const struct fio_option *o, const char *__ptr,
905 char *o_ptr, *ptr, *ptr2;
908 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
912 o_ptr = ptr = strdup(__ptr);
915 * See if we have another set of parameters, hidden after a comma.
916 * Do this before parsing this round, to check if we should
917 * copy set 1 options to set 2.
926 (o->type != FIO_OPT_STR_STORE) &&
927 (o->type != FIO_OPT_STR) &&
928 (o->type != FIO_OPT_FLOAT_LIST)) {
929 ptr2 = strchr(ptr, ',');
930 if (ptr2 && *(ptr2 + 1) == '\0')
932 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
934 ptr2 = strchr(ptr, ':');
936 ptr2 = strchr(ptr, '-');
938 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
939 ptr2 = strchr(ptr, ':');
943 * Don't return early if parsing the first option fails - if
944 * we are doing multiple arguments, we can allow the first one
947 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
963 struct fio_option *find_option(struct fio_option *options, const char *opt)
965 struct fio_option *o;
967 for (o = &options[0]; o->name; o++) {
968 if (!o_match(o, opt))
970 if (o->type == FIO_OPT_UNSUPPORTED) {
971 log_err("Option <%s>: %s\n", o->name, o->help);
981 const struct fio_option *
982 find_option_c(const struct fio_option *options, const char *opt)
984 return find_option((struct fio_option *)options, opt);
987 static const struct fio_option *
988 get_option(char *opt, const struct fio_option *options, char **post)
990 const struct fio_option *o;
993 ret = strchr(opt, '=');
999 strip_blank_end(ret);
1000 o = find_option_c(options, ret);
1002 o = find_option_c(options, opt);
1009 static int opt_cmp(const void *p1, const void *p2)
1011 const struct fio_option *o;
1018 s = strdup(*((char **) p1));
1019 o = get_option(s, __fio_options, &foo);
1025 s = strdup(*((char **) p2));
1026 o = get_option(s, __fio_options, &foo);
1032 return prio2 - prio1;
1035 void sort_options(char **opts, const struct fio_option *options, int num_opts)
1037 __fio_options = options;
1038 qsort(opts, num_opts, sizeof(char *), opt_cmp);
1039 __fio_options = NULL;
1042 static void add_to_dump_list(const struct fio_option *o,
1043 struct flist_head *dump_list, const char *post)
1045 struct print_option *p;
1050 p = malloc(sizeof(*p));
1051 p->name = strdup(o->name);
1053 p->value = strdup(post);
1057 flist_add_tail(&p->list, dump_list);
1060 int parse_cmd_option(const char *opt, const char *val,
1061 const struct fio_option *options, void *data,
1062 struct flist_head *dump_list)
1064 const struct fio_option *o;
1066 o = find_option_c(options, opt);
1068 log_err("Bad option <%s>\n", opt);
1072 if (handle_option(o, val, data)) {
1073 log_err("fio: failed parsing %s=%s\n", opt, val);
1077 add_to_dump_list(o, dump_list, val);
1081 int parse_option(char *opt, const char *input, const struct fio_option *options,
1082 const struct fio_option **o, void *data,
1083 struct flist_head *dump_list)
1088 log_err("fio: failed parsing %s\n", input);
1093 *o = get_option(opt, options, &post);
1096 int len = strlen(opt);
1097 if (opt + len + 1 != post)
1098 memmove(opt + len + 1, post, strlen(post));
1104 if (handle_option(*o, post, data)) {
1105 log_err("fio: failed parsing %s\n", input);
1109 add_to_dump_list(*o, dump_list, post);
1114 * Option match, levenshtein distance. Handy for not quite remembering what
1115 * the option name is.
1117 int string_distance(const char *s1, const char *s2)
1119 unsigned int s1_len = strlen(s1);
1120 unsigned int s2_len = strlen(s2);
1121 unsigned int *p, *q, *r;
1124 p = malloc(sizeof(unsigned int) * (s2_len + 1));
1125 q = malloc(sizeof(unsigned int) * (s2_len + 1));
1128 for (i = 1; i <= s2_len; i++)
1129 p[i] = p[i - 1] + 1;
1131 for (i = 1; i <= s1_len; i++) {
1133 for (j = 1; j <= s2_len; j++) {
1134 unsigned int sub = p[j - 1];
1137 if (s1[i - 1] != s2[j - 1])
1140 pmin = min(q[j - 1] + 1, sub);
1141 q[j] = min(p[j] + 1, pmin);
1155 * Make a guess of whether the distance from 's1' is significant enough
1156 * to warrant printing the guess. We set this to a 1/2 match.
1158 int string_distance_ok(const char *opt, int distance)
1163 len = (len + 1) / 2;
1164 return distance <= len;
1167 static const struct fio_option *find_child(const struct fio_option *options,
1168 const struct fio_option *o)
1170 const struct fio_option *__o;
1172 for (__o = options + 1; __o->name; __o++)
1173 if (__o->parent && !strcmp(__o->parent, o->name))
1179 static void __print_option(const struct fio_option *o,
1180 const struct fio_option *org,
1194 p += sprintf(p, "%s", " ");
1196 sprintf(p, "%s", o->name);
1198 log_info("%-24s: %s\n", name, o->help);
1201 static void print_option(const struct fio_option *o)
1203 const struct fio_option *parent;
1204 const struct fio_option *__o;
1205 unsigned int printed;
1208 __print_option(o, NULL, 0);
1215 while ((__o = find_child(o, parent)) != NULL) {
1216 __print_option(__o, o, level);
1225 int show_cmd_help(const struct fio_option *options, const char *name)
1227 const struct fio_option *o, *closest;
1228 unsigned int best_dist = -1U;
1232 if (!name || !strcmp(name, "all"))
1237 for (o = &options[0]; o->name; o++) {
1240 if (o->type == FIO_OPT_DEPRECATED)
1242 if (!exec_profile && o->prof_name)
1244 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1248 if (!strcmp(name, o->name) ||
1249 (o->alias && !strcmp(name, o->alias)))
1254 dist = string_distance(name, o->name);
1255 if (dist < best_dist) {
1262 if (show_all || match) {
1265 log_info("%20s: %s\n", o->name, o->help);
1276 show_option_help(o, 0);
1282 log_err("No such command: %s", name);
1285 * Only print an appropriately close option, one where the edit
1286 * distance isn't too big. Otherwise we get crazy matches.
1288 if (closest && best_dist < 3) {
1289 log_info(" - showing closest match\n");
1290 log_info("%20s: %s\n", closest->name, closest->help);
1291 show_option_help(closest, 0);
1299 * Handle parsing of default parameters.
1301 void fill_default_options(void *data, const struct fio_option *options)
1303 const struct fio_option *o;
1305 dprint(FD_PARSE, "filling default options\n");
1307 for (o = &options[0]; o->name; o++)
1309 handle_option(o, o->def, data);
1312 static void option_init(struct fio_option *o)
1314 if (o->type == FIO_OPT_DEPRECATED || o->type == FIO_OPT_UNSUPPORTED)
1316 if (o->name && !o->lname)
1317 log_err("Option %s: missing long option name\n", o->name);
1318 if (o->type == FIO_OPT_BOOL) {
1322 if (o->type == FIO_OPT_INT) {
1324 o->maxval = UINT_MAX;
1326 if (o->type == FIO_OPT_STR_SET && o->def && !o->no_warn_def) {
1327 log_err("Option %s: string set option with"
1328 " default will always be true\n", o->name);
1330 if (!o->cb && !o->off1)
1331 log_err("Option %s: neither cb nor offset given\n", o->name);
1333 log_info("Option %s: no category defined. Setting to misc\n", o->name);
1334 o->category = FIO_OPT_C_GENERAL;
1335 o->group = FIO_OPT_G_INVALID;
1337 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1338 o->type == FIO_OPT_STR_MULTI)
1343 * Sanitize the options structure. For now it just sets min/max for bool
1344 * values and whether both callback and offsets are given.
1346 void options_init(struct fio_option *options)
1348 struct fio_option *o;
1350 dprint(FD_PARSE, "init options\n");
1352 for (o = &options[0]; o->name; o++) {
1355 o->inv_opt = find_option(options, o->inverse);
1359 void options_mem_dupe(const struct fio_option *options, void *data)
1361 const struct fio_option *o;
1364 dprint(FD_PARSE, "dup options\n");
1366 for (o = &options[0]; o->name; o++) {
1367 if (o->type != FIO_OPT_STR_STORE)
1370 ptr = td_var(data, o, o->off1);
1372 *ptr = strdup(*ptr);
1376 void options_free(const struct fio_option *options, void *data)
1378 const struct fio_option *o;
1381 dprint(FD_PARSE, "free options\n");
1383 for (o = &options[0]; o->name; o++) {
1384 if (o->type != FIO_OPT_STR_STORE || !o->off1 || o->no_free)
1387 ptr = td_var(data, o, o->off1);