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 == DBL_MIN && o->maxfp == DBL_MAX)
77 logger("%20s: min=%f", "range", o->minfp);
78 if (o->maxfp != DBL_MAX)
79 logger(", max=%f", o->maxfp);
81 } else if (!o->posval[0].ival) {
82 if (!o->minval && !o->maxval)
85 logger("%20s: min=%d", "range", o->minval);
87 logger(", max=%d", o->maxval);
92 static void show_option_values(const struct fio_option *o)
96 for (i = 0; i < PARSE_MAX_VP; i++) {
97 const struct value_pair *vp = &o->posval[i];
102 log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
104 log_info(" %s", vp->help);
112 static void show_option_help(const struct fio_option *o, int is_err)
114 const char *typehelp[] = {
118 "string with possible k/m/g postfix (opt=4k)",
119 "string with time postfix (opt=10s)",
121 "string with dual range (opt=1k-4k,4k-8k)",
122 "integer value (opt=100)",
123 "boolean value (opt=1)",
124 "list of floating point values separated by ':' (opt=5.9:7.8)",
129 size_t (*logger)(const char *format, ...);
137 logger("%20s: %s\n", "alias", o->alias);
139 logger("%20s: %s\n", "type", typehelp[o->type]);
140 logger("%20s: %s\n", "default", o->def ? o->def : "no default");
142 logger("%20s: only for profile '%s'\n", "valid", o->prof_name);
143 show_option_range(o, logger);
144 show_option_values(o);
147 static unsigned long long get_mult_time(const char *str, int len,
152 unsigned long long mult = 1;
156 * Go forward until we hit a non-digit, or +/- sign
158 while ((p - str) <= len) {
159 if (!isdigit((int) *p) && (*p != '+') && (*p != '-'))
164 if (!isalpha((int) *p)) {
172 for (i = 0; i < strlen(c); i++)
173 c[i] = tolower((unsigned char)c[i]);
175 if (!strncmp("us", c, 2) || !strncmp("usec", c, 4))
177 else if (!strncmp("ms", c, 2) || !strncmp("msec", c, 4))
179 else if (!strcmp("s", c))
181 else if (!strcmp("m", c))
182 mult = 60 * 1000000UL;
183 else if (!strcmp("h", c))
184 mult = 60 * 60 * 1000000UL;
185 else if (!strcmp("d", c))
186 mult = 24 * 60 * 60 * 1000000ULL;
192 static int is_separator(char c)
205 static unsigned long long __get_mult_bytes(const char *p, void *data,
208 unsigned int kb_base = fio_get_kb_base(data);
209 unsigned long long ret = 1;
210 unsigned int i, pow = 0, mult = kb_base;
218 for (i = 0; i < strlen(c); i++) {
219 c[i] = tolower((unsigned char)c[i]);
220 if (is_separator(c[i])) {
226 /* If kb_base is 1000, use true units.
227 * If kb_base is 1024, use opposite units.
229 if (!strncmp("pib", c, 3)) {
233 else if (kb_base == 1024)
235 } else if (!strncmp("tib", c, 3)) {
239 else if (kb_base == 1024)
241 } else if (!strncmp("gib", c, 3)) {
245 else if (kb_base == 1024)
247 } else if (!strncmp("mib", c, 3)) {
251 else if (kb_base == 1024)
253 } else if (!strncmp("kib", c, 3)) {
257 else if (kb_base == 1024)
259 } else if (!strncmp("p", c, 1) || !strncmp("pb", c, 2)) {
261 } else if (!strncmp("t", c, 1) || !strncmp("tb", c, 2)) {
263 } else if (!strncmp("g", c, 1) || !strncmp("gb", c, 2)) {
265 } else if (!strncmp("m", c, 1) || !strncmp("mb", c, 2)) {
267 } else if (!strncmp("k", c, 1) || !strncmp("kb", c, 2)) {
269 } else if (!strncmp("%", c, 1)) {
276 ret *= (unsigned long long) mult;
282 static unsigned long long get_mult_bytes(const char *str, int len, void *data,
289 return __get_mult_bytes(str, data, percent);
292 * Go forward until we hit a non-digit, or +/- sign
294 while ((p - str) <= len) {
295 if (!isdigit((int) *p) &&
296 (((*p != '+') && (*p != '-')) || digit_seen))
298 digit_seen |= isdigit((int) *p);
302 if (!isalpha((int) *p) && (*p != '%'))
305 return __get_mult_bytes(p, data, percent);
308 extern int evaluate_arithmetic_expression(const char *buffer, long long *ival,
309 double *dval, double implied_units,
313 * Convert string into a floating number. Return 1 for success and 0 otherwise.
315 int str_to_float(const char *str, double *val, int is_time)
317 #ifdef CONFIG_ARITHMETIC
323 rc = evaluate_arithmetic_expression(str, &ival, &dval, 1.0, is_time);
330 return 1 == sscanf(str, "%lf", val);
334 * convert string into decimal value, noting any size suffix
336 int str_to_decimal(const char *str, long long *val, int kilo, void *data,
337 int is_seconds, int is_time)
341 #ifdef CONFIG_ARITHMETIC
344 double implied_units = 1.0;
351 #ifdef CONFIG_ARITHMETIC
353 implied_units = 1000000.0;
355 rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units, is_time);
356 if (str[0] == '(' && !rc) {
357 if (!kilo && is_seconds)
358 *val = ival / 1000000LL;
365 if (strstr(str, "0x") || strstr(str, "0X"))
370 *val = strtoll(str, NULL, base);
371 if (*val == LONG_MAX && errno == ERANGE)
376 unsigned long long mult;
379 mult = get_mult_bytes(str, len, data, &perc);
385 *val *= get_mult_time(str, len, is_seconds);
390 int check_str_bytes(const char *p, long long *val, void *data)
392 return str_to_decimal(p, val, 1, data, 0, 0);
395 int check_str_time(const char *p, long long *val, int is_seconds)
397 return str_to_decimal(p, val, 0, NULL, is_seconds, 1);
400 void strip_blank_front(char **p)
406 while (isspace((int) *s))
412 void strip_blank_end(char *p)
429 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
435 static int check_range_bytes(const char *str, long *val, void *data)
439 if (!str_to_decimal(str, &__val, 1, data, 0, 0)) {
447 static int check_int(const char *p, int *val)
451 if (strstr(p, "0x") || strstr(p, "0X")) {
452 if (sscanf(p, "%x", val) == 1)
455 if (sscanf(p, "%u", val) == 1)
462 static size_t opt_len(const char *str)
466 postfix = strchr(str, ':');
470 return (int)(postfix - str);
473 static int str_match_len(const struct value_pair *vp, const char *str)
475 return max(strlen(vp->ival), opt_len(str));
478 #define val_store(ptr, val, off, or, data, o) \
480 ptr = td_var((data), (o), (off)); \
487 static const char *opt_type_name(const struct fio_option *o)
489 compiletime_assert(ARRAY_SIZE(opt_type_names) - 1 == FIO_OPT_UNSUPPORTED,
490 "opt_type_names[] index");
492 if (o->type <= FIO_OPT_UNSUPPORTED)
493 return opt_type_names[o->type];
495 return "OPT_UNKNOWN?";
498 static int __handle_option(const struct fio_option *o, const char *ptr,
499 void *data, int first, int more, int curr)
503 long long ull, *ullp;
507 int ret = 0, is_time = 0;
508 const struct value_pair *vp;
509 struct value_pair posval[PARSE_MAX_VP];
510 int i, all_skipped = 1;
512 dprint(FD_PARSE, "__handle_option=%s, type=%s, ptr=%s\n", o->name,
513 opt_type_name(o), ptr);
515 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
516 log_err("Option %s requires an argument\n", o->name);
522 case FIO_OPT_STR_MULTI: {
523 fio_opt_str_fn *fn = o->cb;
525 posval_sort(o, posval);
528 for (i = 0; i < PARSE_MAX_VP; i++) {
530 if (!vp->ival || vp->ival[0] == '\0')
535 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
538 val_store(ilp, vp->oval, o->off1, vp->orval, data, o);
543 if (ret && !all_skipped)
544 show_option_values(o);
549 case FIO_OPT_STR_VAL_TIME:
552 case FIO_OPT_STR_VAL: {
553 fio_opt_str_val_fn *fn = o->cb;
556 if (!is_time && o->is_time)
557 is_time = o->is_time;
559 tmp[sizeof(tmp) - 1] = '\0';
560 strncpy(tmp, ptr, sizeof(tmp) - 1);
561 p = strchr(tmp, ',');
566 ret = check_str_time(tmp, &ull, o->is_seconds);
568 ret = check_str_bytes(tmp, &ull, data);
570 dprint(FD_PARSE, " ret=%d, out=%llu\n", ret, ull);
574 if (o->pow2 && !is_power_of_2(ull)) {
575 log_err("%s: must be a power-of-2\n", o->name);
579 if (o->maxval && ull > o->maxval) {
580 log_err("max value out of range: %llu"
581 " (%u max)\n", ull, o->maxval);
584 if (o->minval && ull < o->minval) {
585 log_err("min value out of range: %lld"
586 " (%d min)\n", ull, o->minval);
589 if (o->posval[0].ival) {
590 posval_sort(o, posval);
593 for (i = 0; i < PARSE_MAX_VP; i++) {
595 if (!vp->ival || vp->ival[0] == '\0')
597 if (vp->oval == ull) {
603 log_err("fio: value %llu not allowed:\n", ull);
604 show_option_values(o);
610 ret = fn(data, &ull);
612 if (o->type == FIO_OPT_INT) {
614 val_store(ilp, ull, o->off1, 0, data, o);
617 val_store(ilp, ull, o->off2, 0, data, o);
621 val_store(ilp, ull, o->off3, 0, data, o);
626 val_store(ilp, ull, o->off2, 0, data, o);
630 val_store(ilp, ull, o->off3, 0, data, o);
635 val_store(ullp, ull, o->off1, 0, data, o);
638 val_store(ullp, ull, o->off2, 0, data, o);
644 case FIO_OPT_FLOAT_LIST: {
649 ** Initialize precision to 0 and zero out list
650 ** in case specified list is shorter than default
654 ilp = td_var(data, o, o->off2);
658 flp = td_var(data, o, o->off1);
659 for(i = 0; i < o->maxlen; i++)
662 if (curr >= o->maxlen) {
663 log_err("the list exceeding max length %d\n",
667 if (!str_to_float(ptr, &uf, 0)) { /* this breaks if we ever have lists of times */
668 log_err("not a floating point value: %s\n", ptr);
672 log_err("value out of range: %f"
673 " (range max: %f)\n", uf, o->maxfp);
677 log_err("value out of range: %f"
678 " (range min: %f)\n", uf, o->minfp);
682 flp = td_var(data, o, o->off1);
685 dprint(FD_PARSE, " out=%f\n", uf);
688 ** Calculate precision for output by counting
689 ** number of digits after period. Find first
690 ** period in entire remaining list each time
692 cp2 = strchr(ptr, '.');
696 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
700 ilp = td_var(data, o, o->off2);
708 case FIO_OPT_STR_STORE: {
709 fio_opt_str_fn *fn = o->cb;
715 cp = td_var(data, o, o->off1);
721 else if (o->posval[0].ival) {
722 posval_sort(o, posval);
725 for (i = 0; i < PARSE_MAX_VP; i++) {
727 if (!vp->ival || vp->ival[0] == '\0' || !cp)
730 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
736 rest = strstr(*cp ?: ptr, ":");
750 show_option_values(o);
759 case FIO_OPT_RANGE: {
763 tmp[sizeof(tmp) - 1] = '\0';
764 strncpy(tmp, ptr, sizeof(tmp) - 1);
766 /* Handle bsrange with separate read,write values: */
767 p1 = strchr(tmp, ',');
771 p1 = strchr(tmp, '-');
773 p1 = strchr(tmp, ':');
785 if (!check_range_bytes(p1, &ul1, data) &&
786 !check_range_bytes(p2, &ul2, data)) {
789 unsigned long foo = ul1;
796 val_store(ilp, ul1, o->off1, 0, data, o);
797 val_store(ilp, ul2, o->off2, 0, data, o);
800 if (o->off3 && o->off4) {
801 val_store(ilp, ul1, o->off3, 0, data, o);
802 val_store(ilp, ul2, o->off4, 0, data, o);
806 if (o->off5 && o->off6) {
807 val_store(ilp, ul1, o->off5, 0, data, o);
808 val_store(ilp, ul2, o->off6, 0, data, o);
813 if (o->off3 && o->off4) {
814 val_store(ilp, ul1, o->off3, 0, data, o);
815 val_store(ilp, ul2, o->off4, 0, data, o);
819 if (o->off5 && o->off6) {
820 val_store(ilp, ul1, o->off5, 0, data, o);
821 val_store(ilp, ul2, o->off6, 0, data, o);
830 case FIO_OPT_STR_SET: {
831 fio_opt_int_fn *fn = o->cb;
834 ret = check_int(ptr, &il);
835 else if (o->type == FIO_OPT_BOOL)
840 dprint(FD_PARSE, " ret=%d, out=%d\n", ret, il);
845 if (o->maxval && il > (int) o->maxval) {
846 log_err("max value out of range: %d (%d max)\n",
850 if (o->minval && il < o->minval) {
851 log_err("min value out of range: %d (%d min)\n",
863 val_store(ilp, il, o->off1, 0, data, o);
866 val_store(ilp, il, o->off2, 0, data, o);
871 case FIO_OPT_DEPRECATED:
872 log_info("Option %s is deprecated\n", o->name);
876 log_err("Bad option type %u\n", o->type);
884 ret = o->verify(o, data);
886 log_err("Correct format for offending option\n");
887 log_err("%20s: %s\n", o->name, o->help);
888 show_option_help(o, 1);
895 static int handle_option(const struct fio_option *o, const char *__ptr,
898 char *o_ptr, *ptr, *ptr2;
901 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
905 o_ptr = ptr = strdup(__ptr);
908 * See if we have another set of parameters, hidden after a comma.
909 * Do this before parsing this round, to check if we should
910 * copy set 1 options to set 2.
919 (o->type != FIO_OPT_STR_STORE) &&
920 (o->type != FIO_OPT_STR) &&
921 (o->type != FIO_OPT_FLOAT_LIST)) {
922 ptr2 = strchr(ptr, ',');
923 if (ptr2 && *(ptr2 + 1) == '\0')
925 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
927 ptr2 = strchr(ptr, ':');
929 ptr2 = strchr(ptr, '-');
931 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
932 ptr2 = strchr(ptr, ':');
936 * Don't return early if parsing the first option fails - if
937 * we are doing multiple arguments, we can allow the first one
940 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
956 struct fio_option *find_option(struct fio_option *options, const char *opt)
958 struct fio_option *o;
960 for (o = &options[0]; o->name; o++) {
961 if (!o_match(o, opt))
963 if (o->type == FIO_OPT_UNSUPPORTED) {
964 log_err("Option <%s>: %s\n", o->name, o->help);
974 const struct fio_option *
975 find_option_c(const struct fio_option *options, const char *opt)
977 return find_option((struct fio_option *)options, opt);
980 static const struct fio_option *
981 get_option(char *opt, const struct fio_option *options, char **post)
983 const struct fio_option *o;
986 ret = strchr(opt, '=');
992 strip_blank_end(ret);
993 o = find_option_c(options, ret);
995 o = find_option_c(options, opt);
1002 static int opt_cmp(const void *p1, const void *p2)
1004 const struct fio_option *o;
1011 s = strdup(*((char **) p1));
1012 o = get_option(s, __fio_options, &foo);
1018 s = strdup(*((char **) p2));
1019 o = get_option(s, __fio_options, &foo);
1025 return prio2 - prio1;
1028 void sort_options(char **opts, const struct fio_option *options, int num_opts)
1030 __fio_options = options;
1031 qsort(opts, num_opts, sizeof(char *), opt_cmp);
1032 __fio_options = NULL;
1035 static void add_to_dump_list(const struct fio_option *o,
1036 struct flist_head *dump_list, const char *post)
1038 struct print_option *p;
1043 p = malloc(sizeof(*p));
1044 p->name = strdup(o->name);
1046 p->value = strdup(post);
1050 flist_add_tail(&p->list, dump_list);
1053 int parse_cmd_option(const char *opt, const char *val,
1054 const struct fio_option *options, void *data,
1055 struct flist_head *dump_list)
1057 const struct fio_option *o;
1059 o = find_option_c(options, opt);
1061 log_err("Bad option <%s>\n", opt);
1065 if (handle_option(o, val, data)) {
1066 log_err("fio: failed parsing %s=%s\n", opt, val);
1070 add_to_dump_list(o, dump_list, val);
1074 int parse_option(char *opt, const char *input, const struct fio_option *options,
1075 const struct fio_option **o, void *data,
1076 struct flist_head *dump_list)
1081 log_err("fio: failed parsing %s\n", input);
1086 *o = get_option(opt, options, &post);
1089 int len = strlen(opt);
1090 if (opt + len + 1 != post)
1091 memmove(opt + len + 1, post, strlen(post));
1097 if (handle_option(*o, post, data)) {
1098 log_err("fio: failed parsing %s\n", input);
1102 add_to_dump_list(*o, dump_list, post);
1107 * Option match, levenshtein distance. Handy for not quite remembering what
1108 * the option name is.
1110 int string_distance(const char *s1, const char *s2)
1112 unsigned int s1_len = strlen(s1);
1113 unsigned int s2_len = strlen(s2);
1114 unsigned int *p, *q, *r;
1117 p = malloc(sizeof(unsigned int) * (s2_len + 1));
1118 q = malloc(sizeof(unsigned int) * (s2_len + 1));
1121 for (i = 1; i <= s2_len; i++)
1122 p[i] = p[i - 1] + 1;
1124 for (i = 1; i <= s1_len; i++) {
1126 for (j = 1; j <= s2_len; j++) {
1127 unsigned int sub = p[j - 1];
1130 if (s1[i - 1] != s2[j - 1])
1133 pmin = min(q[j - 1] + 1, sub);
1134 q[j] = min(p[j] + 1, pmin);
1148 * Make a guess of whether the distance from 's1' is significant enough
1149 * to warrant printing the guess. We set this to a 1/2 match.
1151 int string_distance_ok(const char *opt, int distance)
1156 len = (len + 1) / 2;
1157 return distance <= len;
1160 static const struct fio_option *find_child(const struct fio_option *options,
1161 const struct fio_option *o)
1163 const struct fio_option *__o;
1165 for (__o = options + 1; __o->name; __o++)
1166 if (__o->parent && !strcmp(__o->parent, o->name))
1172 static void __print_option(const struct fio_option *o,
1173 const struct fio_option *org,
1187 p += sprintf(p, "%s", " ");
1189 sprintf(p, "%s", o->name);
1191 log_info("%-24s: %s\n", name, o->help);
1194 static void print_option(const struct fio_option *o)
1196 const struct fio_option *parent;
1197 const struct fio_option *__o;
1198 unsigned int printed;
1201 __print_option(o, NULL, 0);
1208 while ((__o = find_child(o, parent)) != NULL) {
1209 __print_option(__o, o, level);
1218 int show_cmd_help(const struct fio_option *options, const char *name)
1220 const struct fio_option *o, *closest;
1221 unsigned int best_dist = -1U;
1225 if (!name || !strcmp(name, "all"))
1230 for (o = &options[0]; o->name; o++) {
1233 if (o->type == FIO_OPT_DEPRECATED)
1235 if (!exec_profile && o->prof_name)
1237 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1241 if (!strcmp(name, o->name) ||
1242 (o->alias && !strcmp(name, o->alias)))
1247 dist = string_distance(name, o->name);
1248 if (dist < best_dist) {
1255 if (show_all || match) {
1258 log_info("%20s: %s\n", o->name, o->help);
1269 show_option_help(o, 0);
1275 log_err("No such command: %s", name);
1278 * Only print an appropriately close option, one where the edit
1279 * distance isn't too big. Otherwise we get crazy matches.
1281 if (closest && best_dist < 3) {
1282 log_info(" - showing closest match\n");
1283 log_info("%20s: %s\n", closest->name, closest->help);
1284 show_option_help(closest, 0);
1292 * Handle parsing of default parameters.
1294 void fill_default_options(void *data, const struct fio_option *options)
1296 const struct fio_option *o;
1298 dprint(FD_PARSE, "filling default options\n");
1300 for (o = &options[0]; o->name; o++)
1302 handle_option(o, o->def, data);
1305 static void option_init(struct fio_option *o)
1307 if (o->type == FIO_OPT_DEPRECATED || o->type == FIO_OPT_UNSUPPORTED)
1309 if (o->name && !o->lname)
1310 log_err("Option %s: missing long option name\n", o->name);
1311 if (o->type == FIO_OPT_BOOL) {
1315 if (o->type == FIO_OPT_INT) {
1317 o->maxval = UINT_MAX;
1319 if (o->type == FIO_OPT_FLOAT_LIST) {
1323 if (o->type == FIO_OPT_STR_SET && o->def && !o->no_warn_def) {
1324 log_err("Option %s: string set option with"
1325 " default will always be true\n", o->name);
1327 if (!o->cb && !o->off1)
1328 log_err("Option %s: neither cb nor offset given\n", o->name);
1330 log_info("Option %s: no category defined. Setting to misc\n", o->name);
1331 o->category = FIO_OPT_C_GENERAL;
1332 o->group = FIO_OPT_G_INVALID;
1334 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1335 o->type == FIO_OPT_STR_MULTI)
1340 * Sanitize the options structure. For now it just sets min/max for bool
1341 * values and whether both callback and offsets are given.
1343 void options_init(struct fio_option *options)
1345 struct fio_option *o;
1347 dprint(FD_PARSE, "init options\n");
1349 for (o = &options[0]; o->name; o++) {
1352 o->inv_opt = find_option(options, o->inverse);
1356 void options_mem_dupe(const struct fio_option *options, void *data)
1358 const struct fio_option *o;
1361 dprint(FD_PARSE, "dup options\n");
1363 for (o = &options[0]; o->name; o++) {
1364 if (o->type != FIO_OPT_STR_STORE)
1367 ptr = td_var(data, o, o->off1);
1369 *ptr = strdup(*ptr);
1373 void options_free(const struct fio_option *options, void *data)
1375 const struct fio_option *o;
1378 dprint(FD_PARSE, "free options\n");
1380 for (o = &options[0]; o->name; o++) {
1381 if (o->type != FIO_OPT_STR_STORE || !o->off1 || o->no_free)
1384 ptr = td_var(data, o, o->off1);