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 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(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(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(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(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(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(struct fio_option *o, const char *ptr, void *data,
499 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(struct fio_option *o, const char *__ptr, void *data)
897 char *o_ptr, *ptr, *ptr2;
900 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
904 o_ptr = ptr = strdup(__ptr);
907 * See if we have another set of parameters, hidden after a comma.
908 * Do this before parsing this round, to check if we should
909 * copy set 1 options to set 2.
918 (o->type != FIO_OPT_STR_STORE) &&
919 (o->type != FIO_OPT_STR) &&
920 (o->type != FIO_OPT_FLOAT_LIST)) {
921 ptr2 = strchr(ptr, ',');
922 if (ptr2 && *(ptr2 + 1) == '\0')
924 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
926 ptr2 = strchr(ptr, ':');
928 ptr2 = strchr(ptr, '-');
930 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
931 ptr2 = strchr(ptr, ':');
935 * Don't return early if parsing the first option fails - if
936 * we are doing multiple arguments, we can allow the first one
939 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
955 struct fio_option *find_option(struct fio_option *options, const char *opt)
957 struct fio_option *o;
959 for (o = &options[0]; o->name; o++) {
960 if (!o_match(o, opt))
962 if (o->type == FIO_OPT_UNSUPPORTED) {
963 log_err("Option <%s>: %s\n", o->name, o->help);
974 static struct fio_option *get_option(char *opt,
975 struct fio_option *options, char **post)
977 struct fio_option *o;
980 ret = strchr(opt, '=');
986 strip_blank_end(ret);
987 o = find_option(options, ret);
989 o = find_option(options, opt);
996 static int opt_cmp(const void *p1, const void *p2)
998 struct fio_option *o;
1005 s = strdup(*((char **) p1));
1006 o = get_option(s, __fio_options, &foo);
1012 s = strdup(*((char **) p2));
1013 o = get_option(s, __fio_options, &foo);
1019 return prio2 - prio1;
1022 void sort_options(char **opts, struct fio_option *options, int num_opts)
1024 __fio_options = options;
1025 qsort(opts, num_opts, sizeof(char *), opt_cmp);
1026 __fio_options = NULL;
1029 static void add_to_dump_list(struct fio_option *o, struct flist_head *dump_list,
1032 struct print_option *p;
1037 p = malloc(sizeof(*p));
1038 p->name = strdup(o->name);
1040 p->value = strdup(post);
1044 flist_add_tail(&p->list, dump_list);
1047 int parse_cmd_option(const char *opt, const char *val,
1048 struct fio_option *options, void *data,
1049 struct flist_head *dump_list)
1051 struct fio_option *o;
1053 o = find_option(options, opt);
1055 log_err("Bad option <%s>\n", opt);
1059 if (handle_option(o, val, data)) {
1060 log_err("fio: failed parsing %s=%s\n", opt, val);
1064 add_to_dump_list(o, dump_list, val);
1068 int parse_option(char *opt, const char *input,
1069 struct fio_option *options, struct fio_option **o, void *data,
1070 struct flist_head *dump_list)
1075 log_err("fio: failed parsing %s\n", input);
1080 *o = get_option(opt, options, &post);
1083 int len = strlen(opt);
1084 if (opt + len + 1 != post)
1085 memmove(opt + len + 1, post, strlen(post));
1091 if (handle_option(*o, post, data)) {
1092 log_err("fio: failed parsing %s\n", input);
1096 add_to_dump_list(*o, dump_list, post);
1101 * Option match, levenshtein distance. Handy for not quite remembering what
1102 * the option name is.
1104 int string_distance(const char *s1, const char *s2)
1106 unsigned int s1_len = strlen(s1);
1107 unsigned int s2_len = strlen(s2);
1108 unsigned int *p, *q, *r;
1111 p = malloc(sizeof(unsigned int) * (s2_len + 1));
1112 q = malloc(sizeof(unsigned int) * (s2_len + 1));
1115 for (i = 1; i <= s2_len; i++)
1116 p[i] = p[i - 1] + 1;
1118 for (i = 1; i <= s1_len; i++) {
1120 for (j = 1; j <= s2_len; j++) {
1121 unsigned int sub = p[j - 1];
1124 if (s1[i - 1] != s2[j - 1])
1127 pmin = min(q[j - 1] + 1, sub);
1128 q[j] = min(p[j] + 1, pmin);
1142 * Make a guess of whether the distance from 's1' is significant enough
1143 * to warrant printing the guess. We set this to a 1/2 match.
1145 int string_distance_ok(const char *opt, int distance)
1150 len = (len + 1) / 2;
1151 return distance <= len;
1154 static struct fio_option *find_child(struct fio_option *options,
1155 struct fio_option *o)
1157 struct fio_option *__o;
1159 for (__o = options + 1; __o->name; __o++)
1160 if (__o->parent && !strcmp(__o->parent, o->name))
1166 static void __print_option(struct fio_option *o, struct fio_option *org,
1180 p += sprintf(p, "%s", " ");
1182 sprintf(p, "%s", o->name);
1184 log_info("%-24s: %s\n", name, o->help);
1187 static void print_option(struct fio_option *o)
1189 struct fio_option *parent;
1190 struct fio_option *__o;
1191 unsigned int printed;
1194 __print_option(o, NULL, 0);
1201 while ((__o = find_child(o, parent)) != NULL) {
1202 __print_option(__o, o, level);
1211 int show_cmd_help(struct fio_option *options, const char *name)
1213 struct fio_option *o, *closest;
1214 unsigned int best_dist = -1U;
1218 if (!name || !strcmp(name, "all"))
1223 for (o = &options[0]; o->name; o++) {
1226 if (o->type == FIO_OPT_DEPRECATED)
1228 if (!exec_profile && o->prof_name)
1230 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1234 if (!strcmp(name, o->name) ||
1235 (o->alias && !strcmp(name, o->alias)))
1240 dist = string_distance(name, o->name);
1241 if (dist < best_dist) {
1248 if (show_all || match) {
1251 log_info("%20s: %s\n", o->name, o->help);
1262 show_option_help(o, 0);
1268 log_err("No such command: %s", name);
1271 * Only print an appropriately close option, one where the edit
1272 * distance isn't too big. Otherwise we get crazy matches.
1274 if (closest && best_dist < 3) {
1275 log_info(" - showing closest match\n");
1276 log_info("%20s: %s\n", closest->name, closest->help);
1277 show_option_help(closest, 0);
1285 * Handle parsing of default parameters.
1287 void fill_default_options(void *data, struct fio_option *options)
1289 struct fio_option *o;
1291 dprint(FD_PARSE, "filling default options\n");
1293 for (o = &options[0]; o->name; o++)
1295 handle_option(o, o->def, data);
1298 static void option_init(struct fio_option *o)
1300 if (o->type == FIO_OPT_DEPRECATED || o->type == FIO_OPT_UNSUPPORTED)
1302 if (o->name && !o->lname)
1303 log_err("Option %s: missing long option name\n", o->name);
1304 if (o->type == FIO_OPT_BOOL) {
1308 if (o->type == FIO_OPT_INT) {
1310 o->maxval = UINT_MAX;
1312 if (o->type == FIO_OPT_FLOAT_LIST) {
1316 if (o->type == FIO_OPT_STR_SET && o->def && !o->no_warn_def) {
1317 log_err("Option %s: string set option with"
1318 " default will always be true\n", o->name);
1320 if (!o->cb && !o->off1)
1321 log_err("Option %s: neither cb nor offset given\n", o->name);
1323 log_info("Option %s: no category defined. Setting to misc\n", o->name);
1324 o->category = FIO_OPT_C_GENERAL;
1325 o->group = FIO_OPT_G_INVALID;
1327 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1328 o->type == FIO_OPT_STR_MULTI)
1333 * Sanitize the options structure. For now it just sets min/max for bool
1334 * values and whether both callback and offsets are given.
1336 void options_init(struct fio_option *options)
1338 struct fio_option *o;
1340 dprint(FD_PARSE, "init options\n");
1342 for (o = &options[0]; o->name; o++) {
1345 o->inv_opt = find_option(options, o->inverse);
1349 void options_mem_dupe(struct fio_option *options, void *data)
1351 struct fio_option *o;
1354 dprint(FD_PARSE, "dup options\n");
1356 for (o = &options[0]; o->name; o++) {
1357 if (o->type != FIO_OPT_STR_STORE)
1360 ptr = td_var(data, o, o->off1);
1362 *ptr = strdup(*ptr);
1366 void options_free(struct fio_option *options, void *data)
1368 struct fio_option *o;
1371 dprint(FD_PARSE, "free options\n");
1373 for (o = &options[0]; o->name; o++) {
1374 if (o->type != FIO_OPT_STR_STORE || !o->off1 || o->no_free)
1377 ptr = td_var(data, o, o->off1);