2 * This file contains the ini and command liner parser main.
15 #include "compiler/compiler.h"
21 #include "lib/ieee754.h"
24 #ifdef CONFIG_ARITHMETIC
28 static const char *opt_type_names[] = {
44 static struct fio_option *__fio_options;
46 static int vp_cmp(const void *p1, const void *p2)
48 const struct value_pair *vp1 = p1;
49 const struct value_pair *vp2 = p2;
51 return strlen(vp2->ival) - strlen(vp1->ival);
54 static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
56 const struct value_pair *vp;
59 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
61 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
62 vp = &o->posval[entries];
63 if (!vp->ival || vp->ival[0] == '\0')
66 memcpy(&vpmap[entries], vp, sizeof(*vp));
69 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
72 static void show_option_range(struct fio_option *o,
73 size_t (*logger)(const char *format, ...))
75 if (o->type == FIO_OPT_FLOAT_LIST) {
76 if (o->minfp == DBL_MIN && o->maxfp == DBL_MAX)
79 logger("%20s: min=%f", "range", o->minfp);
80 if (o->maxfp != DBL_MAX)
81 logger(", max=%f", o->maxfp);
83 } else if (!o->posval[0].ival) {
84 if (!o->minval && !o->maxval)
87 logger("%20s: min=%d", "range", o->minval);
89 logger(", max=%d", o->maxval);
94 static void show_option_values(struct fio_option *o)
98 for (i = 0; i < PARSE_MAX_VP; i++) {
99 const struct value_pair *vp = &o->posval[i];
104 log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
106 log_info(" %s", vp->help);
114 static void show_option_help(struct fio_option *o, int is_err)
116 const char *typehelp[] = {
120 "string with possible k/m/g postfix (opt=4k)",
121 "string with time postfix (opt=10s)",
123 "string with dual range (opt=1k-4k,4k-8k)",
124 "integer value (opt=100)",
125 "boolean value (opt=1)",
126 "list of floating point values separated by ':' (opt=5.9:7.8)",
131 size_t (*logger)(const char *format, ...);
139 logger("%20s: %s\n", "alias", o->alias);
141 logger("%20s: %s\n", "type", typehelp[o->type]);
142 logger("%20s: %s\n", "default", o->def ? o->def : "no default");
144 logger("%20s: only for profile '%s'\n", "valid", o->prof_name);
145 show_option_range(o, logger);
146 show_option_values(o);
149 static unsigned long long get_mult_time(const char *str, int len,
154 unsigned long long mult = 1;
158 * Go forward until we hit a non-digit, or +/- sign
160 while ((p - str) <= len) {
161 if (!isdigit((int) *p) && (*p != '+') && (*p != '-'))
166 if (!isalpha((int) *p)) {
174 for (i = 0; i < strlen(c); i++)
175 c[i] = tolower((unsigned char)c[i]);
177 if (!strncmp("us", c, 2) || !strncmp("usec", c, 4))
179 else if (!strncmp("ms", c, 2) || !strncmp("msec", c, 4))
181 else if (!strcmp("s", c))
183 else if (!strcmp("m", c))
184 mult = 60 * 1000000UL;
185 else if (!strcmp("h", c))
186 mult = 60 * 60 * 1000000UL;
187 else if (!strcmp("d", c))
188 mult = 24 * 60 * 60 * 1000000ULL;
194 static int is_separator(char c)
207 static unsigned long long __get_mult_bytes(const char *p, void *data,
210 unsigned int kb_base = fio_get_kb_base(data);
211 unsigned long long ret = 1;
212 unsigned int i, pow = 0, mult = kb_base;
220 for (i = 0; i < strlen(c); i++) {
221 c[i] = tolower((unsigned char)c[i]);
222 if (is_separator(c[i])) {
228 /* If kb_base is 1000, use true units.
229 * If kb_base is 1024, use opposite units.
231 if (!strncmp("pib", c, 3)) {
235 else if (kb_base == 1024)
237 } else if (!strncmp("tib", c, 3)) {
241 else if (kb_base == 1024)
243 } else if (!strncmp("gib", c, 3)) {
247 else if (kb_base == 1024)
249 } else if (!strncmp("mib", c, 3)) {
253 else if (kb_base == 1024)
255 } else if (!strncmp("kib", c, 3)) {
259 else if (kb_base == 1024)
261 } else if (!strncmp("p", c, 1) || !strncmp("pb", c, 2)) {
263 } else if (!strncmp("t", c, 1) || !strncmp("tb", c, 2)) {
265 } else if (!strncmp("g", c, 1) || !strncmp("gb", c, 2)) {
267 } else if (!strncmp("m", c, 1) || !strncmp("mb", c, 2)) {
269 } else if (!strncmp("k", c, 1) || !strncmp("kb", c, 2)) {
271 } else if (!strncmp("%", c, 1)) {
278 ret *= (unsigned long long) mult;
284 static unsigned long long get_mult_bytes(const char *str, int len, void *data,
291 return __get_mult_bytes(str, data, percent);
294 * Go forward until we hit a non-digit, or +/- sign
296 while ((p - str) <= len) {
297 if (!isdigit((int) *p) &&
298 (((*p != '+') && (*p != '-')) || digit_seen))
300 digit_seen |= isdigit((int) *p);
304 if (!isalpha((int) *p) && (*p != '%'))
307 return __get_mult_bytes(p, data, percent);
310 extern int evaluate_arithmetic_expression(const char *buffer, long long *ival,
311 double *dval, double implied_units,
315 * Convert string into a floating number. Return 1 for success and 0 otherwise.
317 int str_to_float(const char *str, double *val, int is_time)
319 #ifdef CONFIG_ARITHMETIC
325 rc = evaluate_arithmetic_expression(str, &ival, &dval, 1.0, is_time);
332 return 1 == sscanf(str, "%lf", val);
336 * convert string into decimal value, noting any size suffix
338 int str_to_decimal(const char *str, long long *val, int kilo, void *data,
339 int is_seconds, int is_time)
343 #ifdef CONFIG_ARITHMETIC
346 double implied_units = 1.0;
353 #ifdef CONFIG_ARITHMETIC
355 implied_units = 1000000.0;
357 rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units, is_time);
358 if (str[0] == '(' && !rc) {
359 if (!kilo && is_seconds)
360 *val = ival / 1000000LL;
367 if (strstr(str, "0x") || strstr(str, "0X"))
372 *val = strtoll(str, NULL, base);
373 if (*val == LONG_MAX && errno == ERANGE)
378 unsigned long long mult;
381 mult = get_mult_bytes(str, len, data, &perc);
387 *val *= get_mult_time(str, len, is_seconds);
392 int check_str_bytes(const char *p, long long *val, void *data)
394 return str_to_decimal(p, val, 1, data, 0, 0);
397 int check_str_time(const char *p, long long *val, int is_seconds)
399 return str_to_decimal(p, val, 0, NULL, is_seconds, 1);
402 void strip_blank_front(char **p)
408 while (isspace((int) *s))
414 void strip_blank_end(char *p)
431 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
437 static int check_range_bytes(const char *str, long *val, void *data)
441 if (!str_to_decimal(str, &__val, 1, data, 0, 0)) {
449 static int check_int(const char *p, int *val)
453 if (strstr(p, "0x") || strstr(p, "0X")) {
454 if (sscanf(p, "%x", val) == 1)
457 if (sscanf(p, "%u", val) == 1)
464 static size_t opt_len(const char *str)
468 postfix = strchr(str, ':');
472 return (int)(postfix - str);
475 static int str_match_len(const struct value_pair *vp, const char *str)
477 return max(strlen(vp->ival), opt_len(str));
480 #define val_store(ptr, val, off, or, data, o) \
482 ptr = td_var((data), (o), (off)); \
489 static const char *opt_type_name(struct fio_option *o)
491 compiletime_assert(ARRAY_SIZE(opt_type_names) - 1 == FIO_OPT_UNSUPPORTED,
492 "opt_type_names[] index");
494 if (o->type <= FIO_OPT_UNSUPPORTED)
495 return opt_type_names[o->type];
497 return "OPT_UNKNOWN?";
500 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
501 int first, int more, int curr)
505 long long ull, *ullp;
509 int ret = 0, is_time = 0;
510 const struct value_pair *vp;
511 struct value_pair posval[PARSE_MAX_VP];
512 int i, all_skipped = 1;
514 dprint(FD_PARSE, "__handle_option=%s, type=%s, ptr=%s\n", o->name,
515 opt_type_name(o), ptr);
517 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
518 log_err("Option %s requires an argument\n", o->name);
524 case FIO_OPT_STR_MULTI: {
525 fio_opt_str_fn *fn = o->cb;
527 posval_sort(o, posval);
530 for (i = 0; i < PARSE_MAX_VP; i++) {
532 if (!vp->ival || vp->ival[0] == '\0')
537 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
540 val_store(ilp, vp->oval, o->off1, vp->orval, data, o);
545 if (ret && !all_skipped)
546 show_option_values(o);
551 case FIO_OPT_STR_VAL_TIME:
554 case FIO_OPT_STR_VAL: {
555 fio_opt_str_val_fn *fn = o->cb;
558 if (!is_time && o->is_time)
559 is_time = o->is_time;
561 tmp[sizeof(tmp) - 1] = '\0';
562 strncpy(tmp, ptr, sizeof(tmp) - 1);
563 p = strchr(tmp, ',');
568 ret = check_str_time(tmp, &ull, o->is_seconds);
570 ret = check_str_bytes(tmp, &ull, data);
572 dprint(FD_PARSE, " ret=%d, out=%llu\n", ret, ull);
576 if (o->pow2 && !is_power_of_2(ull)) {
577 log_err("%s: must be a power-of-2\n", o->name);
581 if (o->maxval && ull > o->maxval) {
582 log_err("max value out of range: %llu"
583 " (%u max)\n", ull, o->maxval);
586 if (o->minval && ull < o->minval) {
587 log_err("min value out of range: %lld"
588 " (%d min)\n", ull, o->minval);
591 if (o->posval[0].ival) {
592 posval_sort(o, posval);
595 for (i = 0; i < PARSE_MAX_VP; i++) {
597 if (!vp->ival || vp->ival[0] == '\0')
599 if (vp->oval == ull) {
605 log_err("fio: value %llu not allowed:\n", ull);
606 show_option_values(o);
612 ret = fn(data, &ull);
614 if (o->type == FIO_OPT_INT) {
616 val_store(ilp, ull, o->off1, 0, data, o);
619 val_store(ilp, ull, o->off2, 0, data, o);
623 val_store(ilp, ull, o->off3, 0, data, o);
628 val_store(ilp, ull, o->off2, 0, data, o);
632 val_store(ilp, ull, o->off3, 0, data, o);
637 val_store(ullp, ull, o->off1, 0, data, o);
640 val_store(ullp, ull, o->off2, 0, data, o);
646 case FIO_OPT_FLOAT_LIST: {
651 ** Initialize precision to 0 and zero out list
652 ** in case specified list is shorter than default
656 ilp = td_var(data, o, o->off2);
660 flp = td_var(data, o, o->off1);
661 for(i = 0; i < o->maxlen; i++)
664 if (curr >= o->maxlen) {
665 log_err("the list exceeding max length %d\n",
669 if (!str_to_float(ptr, &uf, 0)) { /* this breaks if we ever have lists of times */
670 log_err("not a floating point value: %s\n", ptr);
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);
684 flp = td_var(data, o, o->off1);
687 dprint(FD_PARSE, " out=%f\n", uf);
690 ** Calculate precision for output by counting
691 ** number of digits after period. Find first
692 ** period in entire remaining list each time
694 cp2 = strchr(ptr, '.');
698 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
702 ilp = td_var(data, o, o->off2);
710 case FIO_OPT_STR_STORE: {
711 fio_opt_str_fn *fn = o->cb;
717 cp = td_var(data, o, o->off1);
723 else if (o->posval[0].ival) {
724 posval_sort(o, posval);
727 for (i = 0; i < PARSE_MAX_VP; i++) {
729 if (!vp->ival || vp->ival[0] == '\0' || !cp)
732 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
738 rest = strstr(*cp ?: ptr, ":");
752 show_option_values(o);
761 case FIO_OPT_RANGE: {
765 tmp[sizeof(tmp) - 1] = '\0';
766 strncpy(tmp, ptr, sizeof(tmp) - 1);
768 /* Handle bsrange with separate read,write values: */
769 p1 = strchr(tmp, ',');
773 p1 = strchr(tmp, '-');
775 p1 = strchr(tmp, ':');
787 if (!check_range_bytes(p1, &ul1, data) &&
788 !check_range_bytes(p2, &ul2, data)) {
791 unsigned long foo = ul1;
798 val_store(ilp, ul1, o->off1, 0, data, o);
799 val_store(ilp, ul2, o->off2, 0, data, o);
802 if (o->off3 && o->off4) {
803 val_store(ilp, ul1, o->off3, 0, data, o);
804 val_store(ilp, ul2, o->off4, 0, data, o);
808 if (o->off5 && o->off6) {
809 val_store(ilp, ul1, o->off5, 0, data, o);
810 val_store(ilp, ul2, o->off6, 0, data, o);
815 if (o->off3 && o->off4) {
816 val_store(ilp, ul1, o->off3, 0, data, o);
817 val_store(ilp, ul2, o->off4, 0, data, o);
821 if (o->off5 && o->off6) {
822 val_store(ilp, ul1, o->off5, 0, data, o);
823 val_store(ilp, ul2, o->off6, 0, data, o);
832 case FIO_OPT_STR_SET: {
833 fio_opt_int_fn *fn = o->cb;
836 ret = check_int(ptr, &il);
837 else if (o->type == FIO_OPT_BOOL)
842 dprint(FD_PARSE, " ret=%d, out=%d\n", ret, il);
847 if (o->maxval && il > (int) o->maxval) {
848 log_err("max value out of range: %d (%d max)\n",
852 if (o->minval && il < o->minval) {
853 log_err("min value out of range: %d (%d min)\n",
865 val_store(ilp, il, o->off1, 0, data, o);
868 val_store(ilp, il, o->off2, 0, data, o);
873 case FIO_OPT_DEPRECATED:
874 log_info("Option %s is deprecated\n", o->name);
878 log_err("Bad option type %u\n", o->type);
886 ret = o->verify(o, data);
888 log_err("Correct format for offending option\n");
889 log_err("%20s: %s\n", o->name, o->help);
890 show_option_help(o, 1);
897 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
899 char *o_ptr, *ptr, *ptr2;
902 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
906 o_ptr = ptr = strdup(__ptr);
909 * See if we have another set of parameters, hidden after a comma.
910 * Do this before parsing this round, to check if we should
911 * copy set 1 options to set 2.
920 (o->type != FIO_OPT_STR_STORE) &&
921 (o->type != FIO_OPT_STR) &&
922 (o->type != FIO_OPT_FLOAT_LIST)) {
923 ptr2 = strchr(ptr, ',');
924 if (ptr2 && *(ptr2 + 1) == '\0')
926 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
928 ptr2 = strchr(ptr, ':');
930 ptr2 = strchr(ptr, '-');
932 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
933 ptr2 = strchr(ptr, ':');
937 * Don't return early if parsing the first option fails - if
938 * we are doing multiple arguments, we can allow the first one
941 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
957 struct fio_option *find_option(struct fio_option *options, const char *opt)
959 struct fio_option *o;
961 for (o = &options[0]; o->name; o++) {
962 if (!o_match(o, opt))
964 if (o->type == FIO_OPT_UNSUPPORTED) {
965 log_err("Option <%s>: %s\n", o->name, o->help);
976 static struct fio_option *get_option(char *opt,
977 struct fio_option *options, char **post)
979 struct fio_option *o;
982 ret = strchr(opt, '=');
988 strip_blank_end(ret);
989 o = find_option(options, ret);
991 o = find_option(options, opt);
998 static int opt_cmp(const void *p1, const void *p2)
1000 struct fio_option *o;
1007 s = strdup(*((char **) p1));
1008 o = get_option(s, __fio_options, &foo);
1014 s = strdup(*((char **) p2));
1015 o = get_option(s, __fio_options, &foo);
1021 return prio2 - prio1;
1024 void sort_options(char **opts, struct fio_option *options, int num_opts)
1026 __fio_options = options;
1027 qsort(opts, num_opts, sizeof(char *), opt_cmp);
1028 __fio_options = NULL;
1031 static void add_to_dump_list(struct fio_option *o, struct flist_head *dump_list,
1034 struct print_option *p;
1039 p = malloc(sizeof(*p));
1040 p->name = strdup(o->name);
1042 p->value = strdup(post);
1046 flist_add_tail(&p->list, dump_list);
1049 int parse_cmd_option(const char *opt, const char *val,
1050 struct fio_option *options, void *data,
1051 struct flist_head *dump_list)
1053 struct fio_option *o;
1055 o = find_option(options, opt);
1057 log_err("Bad option <%s>\n", opt);
1061 if (handle_option(o, val, data)) {
1062 log_err("fio: failed parsing %s=%s\n", opt, val);
1066 add_to_dump_list(o, dump_list, val);
1070 int parse_option(char *opt, const char *input,
1071 struct fio_option *options, struct fio_option **o, void *data,
1072 struct flist_head *dump_list)
1077 log_err("fio: failed parsing %s\n", input);
1082 *o = get_option(opt, options, &post);
1085 int len = strlen(opt);
1086 if (opt + len + 1 != post)
1087 memmove(opt + len + 1, post, strlen(post));
1093 if (handle_option(*o, post, data)) {
1094 log_err("fio: failed parsing %s\n", input);
1098 add_to_dump_list(*o, dump_list, post);
1103 * Option match, levenshtein distance. Handy for not quite remembering what
1104 * the option name is.
1106 int string_distance(const char *s1, const char *s2)
1108 unsigned int s1_len = strlen(s1);
1109 unsigned int s2_len = strlen(s2);
1110 unsigned int *p, *q, *r;
1113 p = malloc(sizeof(unsigned int) * (s2_len + 1));
1114 q = malloc(sizeof(unsigned int) * (s2_len + 1));
1117 for (i = 1; i <= s2_len; i++)
1118 p[i] = p[i - 1] + 1;
1120 for (i = 1; i <= s1_len; i++) {
1122 for (j = 1; j <= s2_len; j++) {
1123 unsigned int sub = p[j - 1];
1126 if (s1[i - 1] != s2[j - 1])
1129 pmin = min(q[j - 1] + 1, sub);
1130 q[j] = min(p[j] + 1, pmin);
1144 * Make a guess of whether the distance from 's1' is significant enough
1145 * to warrant printing the guess. We set this to a 1/2 match.
1147 int string_distance_ok(const char *opt, int distance)
1152 len = (len + 1) / 2;
1153 return distance <= len;
1156 static struct fio_option *find_child(struct fio_option *options,
1157 struct fio_option *o)
1159 struct fio_option *__o;
1161 for (__o = options + 1; __o->name; __o++)
1162 if (__o->parent && !strcmp(__o->parent, o->name))
1168 static void __print_option(struct fio_option *o, struct fio_option *org,
1182 p += sprintf(p, "%s", " ");
1184 sprintf(p, "%s", o->name);
1186 log_info("%-24s: %s\n", name, o->help);
1189 static void print_option(struct fio_option *o)
1191 struct fio_option *parent;
1192 struct fio_option *__o;
1193 unsigned int printed;
1196 __print_option(o, NULL, 0);
1203 while ((__o = find_child(o, parent)) != NULL) {
1204 __print_option(__o, o, level);
1213 int show_cmd_help(struct fio_option *options, const char *name)
1215 struct fio_option *o, *closest;
1216 unsigned int best_dist = -1U;
1220 if (!name || !strcmp(name, "all"))
1225 for (o = &options[0]; o->name; o++) {
1228 if (o->type == FIO_OPT_DEPRECATED)
1230 if (!exec_profile && o->prof_name)
1232 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1236 if (!strcmp(name, o->name) ||
1237 (o->alias && !strcmp(name, o->alias)))
1242 dist = string_distance(name, o->name);
1243 if (dist < best_dist) {
1250 if (show_all || match) {
1253 log_info("%20s: %s\n", o->name, o->help);
1264 show_option_help(o, 0);
1270 log_err("No such command: %s", name);
1273 * Only print an appropriately close option, one where the edit
1274 * distance isn't too big. Otherwise we get crazy matches.
1276 if (closest && best_dist < 3) {
1277 log_info(" - showing closest match\n");
1278 log_info("%20s: %s\n", closest->name, closest->help);
1279 show_option_help(closest, 0);
1287 * Handle parsing of default parameters.
1289 void fill_default_options(void *data, struct fio_option *options)
1291 struct fio_option *o;
1293 dprint(FD_PARSE, "filling default options\n");
1295 for (o = &options[0]; o->name; o++)
1297 handle_option(o, o->def, data);
1300 static void option_init(struct fio_option *o)
1302 if (o->type == FIO_OPT_DEPRECATED || o->type == FIO_OPT_UNSUPPORTED)
1304 if (o->name && !o->lname)
1305 log_err("Option %s: missing long option name\n", o->name);
1306 if (o->type == FIO_OPT_BOOL) {
1310 if (o->type == FIO_OPT_INT) {
1312 o->maxval = UINT_MAX;
1314 if (o->type == FIO_OPT_FLOAT_LIST) {
1318 if (o->type == FIO_OPT_STR_SET && o->def && !o->no_warn_def) {
1319 log_err("Option %s: string set option with"
1320 " default will always be true\n", o->name);
1322 if (!o->cb && !o->off1)
1323 log_err("Option %s: neither cb nor offset given\n", o->name);
1325 log_info("Option %s: no category defined. Setting to misc\n", o->name);
1326 o->category = FIO_OPT_C_GENERAL;
1327 o->group = FIO_OPT_G_INVALID;
1329 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1330 o->type == FIO_OPT_STR_MULTI)
1335 * Sanitize the options structure. For now it just sets min/max for bool
1336 * values and whether both callback and offsets are given.
1338 void options_init(struct fio_option *options)
1340 struct fio_option *o;
1342 dprint(FD_PARSE, "init options\n");
1344 for (o = &options[0]; o->name; o++) {
1347 o->inv_opt = find_option(options, o->inverse);
1351 void options_mem_dupe(struct fio_option *options, void *data)
1353 struct fio_option *o;
1356 dprint(FD_PARSE, "dup options\n");
1358 for (o = &options[0]; o->name; o++) {
1359 if (o->type != FIO_OPT_STR_STORE)
1362 ptr = td_var(data, o, o->off1);
1364 *ptr = strdup(*ptr);
1368 void options_free(struct fio_option *options, void *data)
1370 struct fio_option *o;
1373 dprint(FD_PARSE, "free options\n");
1375 for (o = &options[0]; o->name; o++) {
1376 if (o->type != FIO_OPT_STR_STORE || !o->off1 || o->no_free)
1379 ptr = td_var(data, o, o->off1);