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,
272 double *dval, double implied_units,
275 #ifdef CONFIG_ARITHMETIC
277 * These two verification functions are just to gain confidence that
278 * the arithmetic processing code is always getting the same answer as the
279 * original number parsing code. Once sufficiently sure that the arithmetic
280 * code is always getting the right answers, these can be removed.
282 static void verify_exp_parser_float(const char *str, double implied_units, int is_time)
287 if (sscanf(str, "%lf", &tmpval) != 1)
290 if (evaluate_arithmetic_expression(str, &ival, &dval, implied_units, is_time) != 0) {
291 log_info("Arithmetic failed on '%s'\n", str);
294 if (dval != tmpval) {
295 log_info("Arithmetic failed on: '%s' got %lf, expected %lf\n",
300 static void verify_exp_parser_decimal(const char *str, long long val, int kilo, int is_seconds,
306 double implied_units = 1.0;
309 implied_units = 1000000.0;
311 rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units, is_time);
314 log_info("Arithmetic failed on '%s', expected %lld, got %lld\n",
317 log_info("Arithmetic failed on '%s'\n", str);
323 * Convert string into a floating number. Return 1 for success and 0 otherwise.
325 int str_to_float(const char *str, double *val, int is_time)
327 #ifdef CONFIG_ARITHMETIC
333 rc = evaluate_arithmetic_expression(str, &ival, &dval, 1.0, is_time);
339 verify_exp_parser_float(str, 1.0, is_time);
342 return 1 == sscanf(str, "%lf", val);
346 * convert string into decimal value, noting any size suffix
348 int str_to_decimal(const char *str, long long *val, int kilo, void *data,
349 int is_seconds, int is_time)
353 #ifdef CONFIG_ARITHMETIC
356 double implied_units = 1.0;
363 #ifdef CONFIG_ARITHMETIC
365 implied_units = 1000000.0;
367 rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units, is_time);
368 if (str[0] == '(' && !rc) {
369 if (!kilo && is_seconds)
370 *val = ival / 1000000LL;
377 if (strstr(str, "0x") || strstr(str, "0X"))
382 *val = strtoll(str, NULL, base);
383 if (*val == LONG_MAX && errno == ERANGE)
388 unsigned long long mult;
391 mult = get_mult_bytes(str, len, data, &perc);
397 *val *= get_mult_time(str, len, is_seconds);
398 #ifdef CONFIG_ARITHMETIC
399 verify_exp_parser_decimal(str, *val, kilo, is_seconds, is_time);
404 int check_str_bytes(const char *p, long long *val, void *data)
406 return str_to_decimal(p, val, 1, data, 0, 0);
409 int check_str_time(const char *p, long long *val, int is_seconds)
411 return str_to_decimal(p, val, 0, NULL, is_seconds, 1);
414 void strip_blank_front(char **p)
420 while (isspace((int) *s))
426 void strip_blank_end(char *p)
443 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
449 static int check_range_bytes(const char *str, long *val, void *data)
453 if (!str_to_decimal(str, &__val, 1, data, 0, 0)) {
461 static int check_int(const char *p, int *val)
465 if (strstr(p, "0x") || strstr(p, "0X")) {
466 if (sscanf(p, "%x", val) == 1)
469 if (sscanf(p, "%u", val) == 1)
476 static size_t opt_len(const char *str)
480 postfix = strchr(str, ':');
484 return (int)(postfix - str);
487 static int str_match_len(const struct value_pair *vp, const char *str)
489 return max(strlen(vp->ival), opt_len(str));
492 #define val_store(ptr, val, off, or, data, o) \
494 ptr = td_var((data), (o), (off)); \
501 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
502 int first, int more, int curr)
506 long long ull, *ullp;
510 int ret = 0, is_time = 0;
511 const struct value_pair *vp;
512 struct value_pair posval[PARSE_MAX_VP];
513 int i, all_skipped = 1;
515 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
518 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
519 log_err("Option %s requires an argument\n", o->name);
525 case FIO_OPT_STR_MULTI: {
526 fio_opt_str_fn *fn = o->cb;
528 posval_sort(o, posval);
531 for (i = 0; i < PARSE_MAX_VP; i++) {
533 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 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);
575 if (o->maxval && ull > o->maxval) {
576 log_err("max value out of range: %llu"
577 " (%u max)\n", ull, o->maxval);
580 if (o->minval && ull < o->minval) {
581 log_err("min value out of range: %llu"
582 " (%u min)\n", ull, o->minval);
585 if (o->posval[0].ival) {
586 posval_sort(o, posval);
589 for (i = 0; i < PARSE_MAX_VP; i++) {
591 if (!vp->ival || vp->ival[0] == '\0')
593 if (vp->oval == ull) {
599 log_err("fio: value %llu not allowed:\n", ull);
600 show_option_values(o);
606 ret = fn(data, &ull);
608 if (o->type == FIO_OPT_INT) {
610 val_store(ilp, ull, o->off1, 0, data, o);
613 val_store(ilp, ull, o->off2, 0, data, o);
617 val_store(ilp, ull, o->off3, 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(ullp, ull, o->off1, 0, data, o);
634 val_store(ullp, ull, o->off2, 0, data, o);
640 case FIO_OPT_FLOAT_LIST: {
645 ** Initialize precision to 0 and zero out list
646 ** in case specified list is shorter than default
650 ilp = td_var(data, o, o->off2);
654 flp = td_var(data, o, o->off1);
655 for(i = 0; i < o->maxlen; i++)
658 if (curr >= o->maxlen) {
659 log_err("the list exceeding max length %d\n",
663 if (!str_to_float(ptr, &uf, 0)) { /* this breaks if we ever have lists of times */
664 log_err("not a floating point value: %s\n", ptr);
668 log_err("value out of range: %f"
669 " (range max: %f)\n", uf, o->maxfp);
673 log_err("value out of range: %f"
674 " (range min: %f)\n", uf, o->minfp);
678 flp = td_var(data, o, o->off1);
681 dprint(FD_PARSE, " out=%f\n", uf);
684 ** Calculate precision for output by counting
685 ** number of digits after period. Find first
686 ** period in entire remaining list each time
688 cp2 = strchr(ptr, '.');
692 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
696 ilp = td_var(data, o, o->off2);
704 case FIO_OPT_STR_STORE: {
705 fio_opt_str_fn *fn = o->cb;
711 cp = td_var(data, o, o->off1);
717 else if (o->posval[0].ival) {
718 posval_sort(o, posval);
721 for (i = 0; i < PARSE_MAX_VP; i++) {
723 if (!vp->ival || vp->ival[0] == '\0' || !cp)
726 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
732 rest = strstr(*cp ?: ptr, ":");
746 show_option_values(o);
755 case FIO_OPT_RANGE: {
759 strncpy(tmp, ptr, sizeof(tmp) - 1);
761 /* Handle bsrange with separate read,write values: */
762 p1 = strchr(tmp, ',');
766 p1 = strchr(tmp, '-');
768 p1 = strchr(tmp, ':');
780 if (!check_range_bytes(p1, &ul1, data) &&
781 !check_range_bytes(p2, &ul2, data)) {
784 unsigned long foo = ul1;
791 val_store(ilp, ul1, o->off1, 0, data, o);
792 val_store(ilp, ul2, o->off2, 0, data, o);
795 if (o->off3 && o->off4) {
796 val_store(ilp, ul1, o->off3, 0, data, o);
797 val_store(ilp, ul2, o->off4, 0, data, o);
801 if (o->off5 && o->off6) {
802 val_store(ilp, ul1, o->off5, 0, data, o);
803 val_store(ilp, ul2, o->off6, 0, data, o);
808 if (o->off3 && o->off4) {
809 val_store(ilp, ul1, o->off3, 0, data, o);
810 val_store(ilp, ul2, o->off4, 0, data, o);
814 if (o->off5 && o->off6) {
815 val_store(ilp, ul1, o->off5, 0, data, o);
816 val_store(ilp, ul2, o->off6, 0, data, o);
825 case FIO_OPT_STR_SET: {
826 fio_opt_int_fn *fn = o->cb;
829 ret = check_int(ptr, &il);
830 else if (o->type == FIO_OPT_BOOL)
835 dprint(FD_PARSE, " ret=%d, out=%d\n", ret, il);
840 if (o->maxval && il > (int) o->maxval) {
841 log_err("max value out of range: %d (%d max)\n",
845 if (o->minval && il < o->minval) {
846 log_err("min value out of range: %d (%d min)\n",
858 val_store(ilp, il, o->off1, 0, data, o);
861 val_store(ilp, il, o->off2, 0, data, o);
866 case FIO_OPT_DEPRECATED:
867 log_info("Option %s is deprecated\n", o->name);
871 log_err("Bad option type %u\n", o->type);
879 ret = o->verify(o, data);
881 log_err("Correct format for offending option\n");
882 log_err("%20s: %s\n", o->name, o->help);
883 show_option_help(o, 1);
890 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
892 char *o_ptr, *ptr, *ptr2;
895 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
899 o_ptr = ptr = strdup(__ptr);
902 * See if we have another set of parameters, hidden after a comma.
903 * Do this before parsing this round, to check if we should
904 * copy set 1 options to set 2.
913 (o->type != FIO_OPT_STR_STORE) &&
914 (o->type != FIO_OPT_STR) &&
915 (o->type != FIO_OPT_FLOAT_LIST)) {
916 ptr2 = strchr(ptr, ',');
917 if (ptr2 && *(ptr2 + 1) == '\0')
919 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
921 ptr2 = strchr(ptr, ':');
923 ptr2 = strchr(ptr, '-');
925 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
926 ptr2 = strchr(ptr, ':');
930 * Don't return early if parsing the first option fails - if
931 * we are doing multiple arguments, we can allow the first one
934 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
950 static struct fio_option *get_option(char *opt,
951 struct fio_option *options, char **post)
953 struct fio_option *o;
956 ret = strchr(opt, '=');
962 strip_blank_end(ret);
963 o = find_option(options, ret);
965 o = find_option(options, opt);
972 static int opt_cmp(const void *p1, const void *p2)
974 struct fio_option *o;
981 s = strdup(*((char **) p1));
982 o = get_option(s, __fio_options, &foo);
988 s = strdup(*((char **) p2));
989 o = get_option(s, __fio_options, &foo);
995 return prio2 - prio1;
998 void sort_options(char **opts, struct fio_option *options, int num_opts)
1000 __fio_options = options;
1001 qsort(opts, num_opts, sizeof(char *), opt_cmp);
1002 __fio_options = NULL;
1005 int parse_cmd_option(const char *opt, const char *val,
1006 struct fio_option *options, void *data)
1008 struct fio_option *o;
1010 o = find_option(options, opt);
1012 log_err("Bad option <%s>\n", opt);
1016 if (!handle_option(o, val, data))
1019 log_err("fio: failed parsing %s=%s\n", opt, val);
1023 int parse_option(char *opt, const char *input,
1024 struct fio_option *options, struct fio_option **o, void *data,
1030 log_err("fio: failed parsing %s\n", input);
1035 *o = get_option(opt, options, &post);
1038 int len = strlen(opt);
1039 if (opt + len + 1 != post)
1040 memmove(opt + len + 1, post, strlen(post));
1046 if (handle_option(*o, post, data)) {
1047 log_err("fio: failed parsing %s\n", input);
1054 if (!strcmp("description", (*o)->name))
1059 log_info("--%s%s", (*o)->name, post ? "" : " ");
1061 log_info("=%s%s%s ", delim, post, delim);
1068 * Option match, levenshtein distance. Handy for not quite remembering what
1069 * the option name is.
1071 static int string_distance(const char *s1, const char *s2)
1073 unsigned int s1_len = strlen(s1);
1074 unsigned int s2_len = strlen(s2);
1075 unsigned int *p, *q, *r;
1078 p = malloc(sizeof(unsigned int) * (s2_len + 1));
1079 q = malloc(sizeof(unsigned int) * (s2_len + 1));
1082 for (i = 1; i <= s2_len; i++)
1083 p[i] = p[i - 1] + 1;
1085 for (i = 1; i <= s1_len; i++) {
1087 for (j = 1; j <= s2_len; j++) {
1088 unsigned int sub = p[j - 1];
1091 if (s1[i - 1] != s2[j - 1])
1094 pmin = min(q[j - 1] + 1, sub);
1095 q[j] = min(p[j] + 1, pmin);
1108 static struct fio_option *find_child(struct fio_option *options,
1109 struct fio_option *o)
1111 struct fio_option *__o;
1113 for (__o = options + 1; __o->name; __o++)
1114 if (__o->parent && !strcmp(__o->parent, o->name))
1120 static void __print_option(struct fio_option *o, struct fio_option *org,
1134 p += sprintf(p, "%s", " ");
1136 sprintf(p, "%s", o->name);
1138 log_info("%-24s: %s\n", name, o->help);
1141 static void print_option(struct fio_option *o)
1143 struct fio_option *parent;
1144 struct fio_option *__o;
1145 unsigned int printed;
1148 __print_option(o, NULL, 0);
1155 while ((__o = find_child(o, parent)) != NULL) {
1156 __print_option(__o, o, level);
1165 int show_cmd_help(struct fio_option *options, const char *name)
1167 struct fio_option *o, *closest;
1168 unsigned int best_dist = -1U;
1172 if (!name || !strcmp(name, "all"))
1177 for (o = &options[0]; o->name; o++) {
1180 if (o->type == FIO_OPT_DEPRECATED)
1182 if (!exec_profile && o->prof_name)
1184 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1188 if (!strcmp(name, o->name) ||
1189 (o->alias && !strcmp(name, o->alias)))
1194 dist = string_distance(name, o->name);
1195 if (dist < best_dist) {
1202 if (show_all || match) {
1205 log_info("%20s: %s\n", o->name, o->help);
1216 show_option_help(o, 0);
1222 log_err("No such command: %s", name);
1225 * Only print an appropriately close option, one where the edit
1226 * distance isn't too big. Otherwise we get crazy matches.
1228 if (closest && best_dist < 3) {
1229 log_info(" - showing closest match\n");
1230 log_info("%20s: %s\n", closest->name, closest->help);
1231 show_option_help(closest, 0);
1239 * Handle parsing of default parameters.
1241 void fill_default_options(void *data, struct fio_option *options)
1243 struct fio_option *o;
1245 dprint(FD_PARSE, "filling default options\n");
1247 for (o = &options[0]; o->name; o++)
1249 handle_option(o, o->def, data);
1252 void option_init(struct fio_option *o)
1254 if (o->type == FIO_OPT_DEPRECATED)
1256 if (o->type == FIO_OPT_BOOL) {
1260 if (o->type == FIO_OPT_INT) {
1262 o->maxval = UINT_MAX;
1264 if (o->type == FIO_OPT_FLOAT_LIST) {
1268 if (o->type == FIO_OPT_STR_SET && o->def && !o->no_warn_def) {
1269 log_err("Option %s: string set option with"
1270 " default will always be true\n", o->name);
1272 if (!o->cb && !o->off1)
1273 log_err("Option %s: neither cb nor offset given\n", o->name);
1275 log_info("Option %s: no category defined. Setting to misc\n", o->name);
1276 o->category = FIO_OPT_C_GENERAL;
1277 o->group = FIO_OPT_G_INVALID;
1279 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1280 o->type == FIO_OPT_STR_MULTI)
1282 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
1283 log_err("Option %s: both cb and offset given\n", o->name);
1287 * Sanitize the options structure. For now it just sets min/max for bool
1288 * values and whether both callback and offsets are given.
1290 void options_init(struct fio_option *options)
1292 struct fio_option *o;
1294 dprint(FD_PARSE, "init options\n");
1296 for (o = &options[0]; o->name; o++) {
1299 o->inv_opt = find_option(options, o->inverse);
1303 void options_free(struct fio_option *options, void *data)
1305 struct fio_option *o;
1308 dprint(FD_PARSE, "free options\n");
1310 for (o = &options[0]; o->name; o++) {
1311 if (o->type != FIO_OPT_STR_STORE || !o->off1)
1314 ptr = td_var(data, o, o->off1);