2 * This file contains the ini and command liner parser main.
18 static struct fio_option *fio_options;
19 extern unsigned int fio_get_kb_base(void *);
21 static int vp_cmp(const void *p1, const void *p2)
23 const struct value_pair *vp1 = p1;
24 const struct value_pair *vp2 = p2;
26 return strlen(vp2->ival) - strlen(vp1->ival);
29 static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
31 const struct value_pair *vp;
34 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
36 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
37 vp = &o->posval[entries];
38 if (!vp->ival || vp->ival[0] == '\0')
41 memcpy(&vpmap[entries], vp, sizeof(*vp));
44 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
47 static void show_option_range(struct fio_option *o,
48 int (*logger)(const char *format, ...))
50 if (o->type == FIO_OPT_FLOAT_LIST){
51 if (isnan(o->minfp) && isnan(o->maxfp))
54 logger("%20s: min=%f", "range", o->minfp);
56 logger(", max=%f", o->maxfp);
59 if (!o->minval && !o->maxval)
62 logger("%20s: min=%d", "range", o->minval);
64 logger(", max=%d", o->maxval);
69 static void show_option_values(struct fio_option *o)
73 for (i = 0; i < PARSE_MAX_VP; i++) {
74 const struct value_pair *vp = &o->posval[i];
79 log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
81 log_info(" %s", vp->help);
89 static void show_option_help(struct fio_option *o, int is_err)
91 const char *typehelp[] = {
95 "string with possible k/m/g postfix (opt=4k)",
96 "string with time postfix (opt=10s)",
98 "string with dual range (opt=1k-4k,4k-8k)",
99 "integer value (opt=100)",
100 "boolean value (opt=1)",
101 "list of floating point values separated by ':' (opt=5.9:7.8)",
105 int (*logger)(const char *format, ...);
113 logger("%20s: %s\n", "alias", o->alias);
115 logger("%20s: %s\n", "type", typehelp[o->type]);
116 logger("%20s: %s\n", "default", o->def ? o->def : "no default");
118 logger("%20s: only for profile '%s'\n", "valid", o->prof_name);
119 show_option_range(o, logger);
120 show_option_values(o);
123 static unsigned long get_mult_time(char c)
140 static unsigned long long __get_mult_bytes(const char *p, void *data,
143 unsigned int kb_base = fio_get_kb_base(data);
144 unsigned long long ret = 1;
145 unsigned int i, pow = 0, mult = kb_base;
153 for (i = 0; i < strlen(c); i++)
154 c[i] = tolower(c[i]);
156 if (!strcmp("pib", c)) {
159 } else if (!strcmp("tib", c)) {
162 } else if (!strcmp("gib", c)) {
165 } else if (!strcmp("mib", c)) {
168 } else if (!strcmp("kib", c)) {
171 } else if (!strcmp("p", c) || !strcmp("pb", c))
173 else if (!strcmp("t", c) || !strcmp("tb", c))
175 else if (!strcmp("g", c) || !strcmp("gb", c))
177 else if (!strcmp("m", c) || !strcmp("mb", c))
179 else if (!strcmp("k", c) || !strcmp("kb", c))
181 else if (!strcmp("%", c)) {
188 ret *= (unsigned long long) mult;
194 static unsigned long long get_mult_bytes(const char *str, int len, void *data,
201 return __get_mult_bytes(str, data, percent);
204 * Go forward until we hit a non-digit, or +/- sign
206 while ((p - str) <= len) {
207 if (!isdigit((int) *p) &&
208 (((*p != '+') && (*p != '-')) || digit_seen))
210 digit_seen |= isdigit((int) *p);
214 if (!isalpha((int) *p) && (*p != '%'))
217 return __get_mult_bytes(p, data, percent);
221 * Convert string into a floating number. Return 1 for success and 0 otherwise.
223 static int str_to_float(const char *str, double *val)
225 return (1 == sscanf(str, "%lf", val));
229 * convert string into decimal value, noting any size suffix
231 int str_to_decimal(const char *str, long long *val, int kilo, void *data)
239 if (strstr(str, "0x") || strstr(str, "0X"))
244 *val = strtoll(str, NULL, base);
245 if (*val == LONG_MAX && errno == ERANGE)
249 unsigned long long mult;
252 mult = get_mult_bytes(str, len, data, &perc);
258 *val *= get_mult_time(str[len - 1]);
263 static int check_str_bytes(const char *p, long long *val, void *data)
265 return str_to_decimal(p, val, 1, data);
268 static int check_str_time(const char *p, long long *val)
270 return str_to_decimal(p, val, 0, NULL);
273 void strip_blank_front(char **p)
279 while (isspace((int) *s))
285 void strip_blank_end(char *p)
302 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
308 static int check_range_bytes(const char *str, long *val, void *data)
312 if (!str_to_decimal(str, &__val, 1, data)) {
320 static int check_int(const char *p, int *val)
324 if (strstr(p, "0x") || strstr(p, "0X")) {
325 if (sscanf(p, "%x", val) == 1)
328 if (sscanf(p, "%u", val) == 1)
335 static int opt_len(const char *str)
339 postfix = strchr(str, ':');
343 return (int)(postfix - str);
346 #define val_store(ptr, val, off, or, data) \
348 ptr = td_var((data), (off)); \
355 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
356 int first, int more, int curr)
360 long long ull, *ullp;
364 int ret = 0, is_time = 0;
365 const struct value_pair *vp;
366 struct value_pair posval[PARSE_MAX_VP];
367 int i, all_skipped = 1;
369 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
372 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
373 log_err("Option %s requires an argument\n", o->name);
379 case FIO_OPT_STR_MULTI: {
380 fio_opt_str_fn *fn = o->cb;
382 posval_sort(o, posval);
385 for (i = 0; i < PARSE_MAX_VP; i++) {
387 if (!vp->ival || vp->ival[0] == '\0')
390 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
394 *(unsigned int *) o->roff1 |= vp->oval;
396 *(unsigned int *) o->roff1 = vp->oval;
400 val_store(ilp, vp->oval, o->off1, vp->or, data);
406 if (ret && !all_skipped)
407 show_option_values(o);
412 case FIO_OPT_STR_VAL_TIME:
415 case FIO_OPT_STR_VAL: {
416 fio_opt_str_val_fn *fn = o->cb;
419 ret = check_str_time(ptr, &ull);
421 ret = check_str_bytes(ptr, &ull, data);
426 if (o->maxval && ull > o->maxval) {
427 log_err("max value out of range: %lld"
428 " (%d max)\n", ull, o->maxval);
431 if (o->minval && ull < o->minval) {
432 log_err("min value out of range: %lld"
433 " (%d min)\n", ull, o->minval);
438 ret = fn(data, &ull);
440 if (o->type == FIO_OPT_INT) {
443 *(unsigned int *) o->roff1 = ull;
445 val_store(ilp, ull, o->off1, 0, data);
449 *(unsigned int *) o->roff2 = ull;
451 val_store(ilp, ull, o->off2, 0, data);
456 *(unsigned long long *) o->roff1 = ull;
458 val_store(ullp, ull, o->off1, 0, data);
462 *(unsigned long long *) o->roff2 = ull;
464 val_store(ullp, ull, o->off2, 0, data);
470 case FIO_OPT_FLOAT_LIST: {
474 ilp = td_var(data, o->off2);
477 if (curr >= o->maxlen) {
478 log_err("the list exceeding max length %d\n",
482 if(!str_to_float(ptr, &uf)){
483 log_err("not a floating point value: %s\n", ptr);
486 if (!isnan(o->maxfp) && uf > o->maxfp) {
487 log_err("value out of range: %f"
488 " (range max: %f)\n", uf, o->maxfp);
491 if (!isnan(o->minfp) && uf < o->minfp) {
492 log_err("value out of range: %f"
493 " (range min: %f)\n", uf, o->minfp);
497 flp = td_var(data, o->off1);
502 case FIO_OPT_STR_STORE: {
503 fio_opt_str_fn *fn = o->cb;
505 if (o->roff1 || o->off1) {
507 cp = (char **) o->roff1;
509 cp = td_var(data, o->off1);
518 else if (o->posval[0].ival) {
519 posval_sort(o, posval);
522 for (i = 0; i < PARSE_MAX_VP; i++) {
524 if (!vp->ival || vp->ival[0] == '\0')
527 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
533 rest = strstr(*cp ?: ptr, ":");
547 show_option_values(o);
556 case FIO_OPT_RANGE: {
560 strncpy(tmp, ptr, sizeof(tmp) - 1);
562 /* Handle bsrange with separate read,write values: */
563 p1 = strchr(tmp, ',');
567 p1 = strchr(tmp, '-');
569 p1 = strchr(tmp, ':');
581 if (!check_range_bytes(p1, &ul1, data) &&
582 !check_range_bytes(p2, &ul2, data)) {
585 unsigned long foo = ul1;
593 *(unsigned int *) o->roff1 = ul1;
595 val_store(ilp, ul1, o->off1, 0, data);
597 *(unsigned int *) o->roff2 = ul2;
599 val_store(ilp, ul2, o->off2, 0, data);
601 if (o->roff3 && o->roff4) {
602 *(unsigned int *) o->roff3 = ul1;
603 *(unsigned int *) o->roff4 = ul2;
604 } else if (o->off3 && o->off4) {
605 val_store(ilp, ul1, o->off3, 0, data);
606 val_store(ilp, ul2, o->off4, 0, data);
613 case FIO_OPT_STR_SET: {
614 fio_opt_int_fn *fn = o->cb;
617 ret = check_int(ptr, &il);
618 else if (o->type == FIO_OPT_BOOL)
626 if (o->maxval && il > (int) o->maxval) {
627 log_err("max value out of range: %d (%d max)\n",
631 if (o->minval && il < o->minval) {
632 log_err("min value out of range: %d (%d min)\n",
645 *(unsigned int *)o->roff1 = il;
647 val_store(ilp, il, o->off1, 0, data);
651 *(unsigned int *) o->roff2 = il;
653 val_store(ilp, il, o->off2, 0, data);
658 case FIO_OPT_DEPRECATED:
659 log_info("Option %s is deprecated\n", o->name);
662 log_err("Bad option type %u\n", o->type);
670 ret = o->verify(o, data);
672 log_err("Correct format for offending option\n");
673 log_err("%20s: %s\n", o->name, o->help);
674 show_option_help(o, 1);
681 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
683 char *o_ptr, *ptr, *ptr2;
686 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
690 o_ptr = ptr = strdup(__ptr);
693 * See if we have another set of parameters, hidden after a comma.
694 * Do this before parsing this round, to check if we should
695 * copy set 1 options to set 2.
704 (o->type != FIO_OPT_STR_STORE) &&
705 (o->type != FIO_OPT_STR) &&
706 (o->type != FIO_OPT_FLOAT_LIST)) {
707 ptr2 = strchr(ptr, ',');
708 if (ptr2 && *(ptr2 + 1) == '\0')
710 if (o->type != FIO_OPT_STR_MULTI) {
712 ptr2 = strchr(ptr, ':');
714 ptr2 = strchr(ptr, '-');
716 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
717 ptr2 = strchr(ptr, ':');
721 * Don't return early if parsing the first option fails - if
722 * we are doing multiple arguments, we can allow the first one
725 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
741 static struct fio_option *get_option(char *opt,
742 struct fio_option *options, char **post)
744 struct fio_option *o;
747 ret = strchr(opt, '=');
753 strip_blank_end(ret);
754 o = find_option(options, ret);
756 o = find_option(options, opt);
763 static int opt_cmp(const void *p1, const void *p2)
765 struct fio_option *o;
772 s = strdup(*((char **) p1));
773 o = get_option(s, fio_options, &foo);
779 s = strdup(*((char **) p2));
780 o = get_option(s, fio_options, &foo);
786 return prio2 - prio1;
789 void sort_options(char **opts, struct fio_option *options, int num_opts)
791 fio_options = options;
792 qsort(opts, num_opts, sizeof(char *), opt_cmp);
796 int parse_cmd_option(const char *opt, const char *val,
797 struct fio_option *options, void *data)
799 struct fio_option *o;
801 o = find_option(options, opt);
803 log_err("Bad option <%s>\n", opt);
807 if (!handle_option(o, val, data))
810 log_err("fio: failed parsing %s=%s\n", opt, val);
814 int parse_option(char *opt, const char *input,
815 struct fio_option *options, struct fio_option **o, void *data)
820 log_err("fio: failed parsing %s\n", input);
825 *o = get_option(opt, options, &post);
828 int len = strlen(opt);
829 if (opt + len + 1 != post)
830 memmove(opt + len + 1, post, strlen(post));
836 if (!handle_option(*o, post, data)) {
840 log_err("fio: failed parsing %s\n", input);
845 * Option match, levenshtein distance. Handy for not quite remembering what
846 * the option name is.
848 static int string_distance(const char *s1, const char *s2)
850 unsigned int s1_len = strlen(s1);
851 unsigned int s2_len = strlen(s2);
852 unsigned int *p, *q, *r;
855 p = malloc(sizeof(unsigned int) * (s2_len + 1));
856 q = malloc(sizeof(unsigned int) * (s2_len + 1));
859 for (i = 1; i <= s2_len; i++)
862 for (i = 1; i <= s1_len; i++) {
864 for (j = 1; j <= s2_len; j++) {
865 unsigned int sub = p[j - 1];
867 if (s1[i - 1] != s2[j - 1])
870 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
883 static struct fio_option *find_child(struct fio_option *options,
884 struct fio_option *o)
886 struct fio_option *__o;
888 for (__o = options + 1; __o->name; __o++)
889 if (__o->parent && !strcmp(__o->parent, o->name))
895 static void __print_option(struct fio_option *o, struct fio_option *org,
909 p += sprintf(p, "%s", " ");
911 sprintf(p, "%s", o->name);
913 log_info("%-24s: %s\n", name, o->help);
916 static void print_option(struct fio_option *o)
918 struct fio_option *parent;
919 struct fio_option *__o;
920 unsigned int printed;
923 __print_option(o, NULL, 0);
930 while ((__o = find_child(o, parent)) != NULL) {
931 __print_option(__o, o, level);
940 int show_cmd_help(struct fio_option *options, const char *name)
942 struct fio_option *o, *closest;
943 unsigned int best_dist = -1U;
947 if (!name || !strcmp(name, "all"))
952 for (o = &options[0]; o->name; o++) {
955 if (o->type == FIO_OPT_DEPRECATED)
957 if (!exec_profile && o->prof_name)
961 if (!strcmp(name, o->name) ||
962 (o->alias && !strcmp(name, o->alias)))
967 dist = string_distance(name, o->name);
968 if (dist < best_dist) {
975 if (show_all || match) {
978 log_info("%20s: %s\n", o->name, o->help);
989 show_option_help(o, 0);
995 log_err("No such command: %s", name);
998 * Only print an appropriately close option, one where the edit
999 * distance isn't too big. Otherwise we get crazy matches.
1001 if (closest && best_dist < 3) {
1002 log_info(" - showing closest match\n");
1003 log_info("%20s: %s\n", closest->name, closest->help);
1004 show_option_help(closest, 0);
1012 * Handle parsing of default parameters.
1014 void fill_default_options(void *data, struct fio_option *options)
1016 struct fio_option *o;
1018 dprint(FD_PARSE, "filling default options\n");
1020 for (o = &options[0]; o->name; o++)
1022 handle_option(o, o->def, data);
1025 void option_init(struct fio_option *o)
1027 if (o->type == FIO_OPT_DEPRECATED)
1029 if (o->type == FIO_OPT_BOOL) {
1033 if (o->type == FIO_OPT_FLOAT_LIST) {
1037 if (o->type == FIO_OPT_STR_SET && o->def) {
1038 log_err("Option %s: string set option with"
1039 " default will always be true\n", o->name);
1041 if (!o->cb && (!o->off1 && !o->roff1))
1042 log_err("Option %s: neither cb nor offset given\n", o->name);
1043 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1044 o->type == FIO_OPT_STR_MULTI)
1046 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
1047 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
1048 log_err("Option %s: both cb and offset given\n", o->name);
1053 * Sanitize the options structure. For now it just sets min/max for bool
1054 * values and whether both callback and offsets are given.
1056 void options_init(struct fio_option *options)
1058 struct fio_option *o;
1060 dprint(FD_PARSE, "init options\n");
1062 for (o = &options[0]; o->name; o++)
1066 void options_free(struct fio_option *options, void *data)
1068 struct fio_option *o;
1071 dprint(FD_PARSE, "free options\n");
1073 for (o = &options[0]; o->name; o++) {
1074 if (o->type != FIO_OPT_STR_STORE || !o->off1)
1077 ptr = td_var(data, o->off1);