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, FILE *out)
49 if (o->type == FIO_OPT_FLOAT_LIST){
50 if (isnan(o->minfp) && isnan(o->maxfp))
53 fprintf(out, "%20s: min=%f", "range", o->minfp);
55 fprintf(out, ", max=%f", o->maxfp);
58 if (!o->minval && !o->maxval)
61 fprintf(out, "%20s: min=%d", "range", o->minval);
63 fprintf(out, ", max=%d", o->maxval);
68 static void show_option_values(struct fio_option *o)
72 for (i = 0; i < PARSE_MAX_VP; i++) {
73 const struct value_pair *vp = &o->posval[i];
78 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
80 printf(" %s", vp->help);
88 static void show_option_help(struct fio_option *o, FILE *out)
90 const char *typehelp[] = {
94 "string with possible k/m/g postfix (opt=4k)",
95 "string with time postfix (opt=10s)",
97 "string with dual range (opt=1k-4k,4k-8k)",
98 "integer value (opt=100)",
99 "boolean value (opt=1)",
100 "list of floating point values separated by ':' (opt=5.9:7.8)",
106 fprintf(out, "%20s: %s\n", "alias", o->alias);
108 fprintf(out, "%20s: %s\n", "type", typehelp[o->type]);
109 fprintf(out, "%20s: %s\n", "default", o->def ? o->def : "no default");
111 fprintf(out, "%20s: only for profile '%s'\n", "valid", o->prof_name);
112 show_option_range(o, stdout);
113 show_option_values(o);
116 static unsigned long get_mult_time(char c)
133 static unsigned long long __get_mult_bytes(const char *p, void *data,
136 unsigned int kb_base = fio_get_kb_base(data);
137 unsigned long long ret = 1;
138 unsigned int i, pow = 0, mult = kb_base;
146 for (i = 0; i < strlen(c); i++)
147 c[i] = tolower(c[i]);
149 if (!strcmp("pib", c)) {
152 } else if (!strcmp("tib", c)) {
155 } else if (!strcmp("gib", c)) {
158 } else if (!strcmp("mib", c)) {
161 } else if (!strcmp("kib", c)) {
164 } else if (!strcmp("p", c) || !strcmp("pb", c))
166 else if (!strcmp("t", c) || !strcmp("tb", c))
168 else if (!strcmp("g", c) || !strcmp("gb", c))
170 else if (!strcmp("m", c) || !strcmp("mb", c))
172 else if (!strcmp("k", c) || !strcmp("kb", c))
174 else if (!strcmp("%", c)) {
180 ret *= (unsigned long long) mult;
186 static unsigned long long get_mult_bytes(const char *str, int len, void *data,
192 return __get_mult_bytes(str, data, percent);
195 * Go forward until we hit a non-digit
197 while ((p - str) <= len) {
198 if (!isdigit((int) *p))
203 if (!isalpha((int) *p) && (*p != '%'))
206 return __get_mult_bytes(p, data, percent);
210 * Convert string into a floating number. Return 1 for success and 0 otherwise.
212 int str_to_float(const char *str, double *val)
214 return (1 == sscanf(str, "%lf", val));
218 * convert string into decimal value, noting any size suffix
220 int str_to_decimal(const char *str, long long *val, int kilo, void *data)
228 if (strstr(str, "0x") || strstr(str, "0X"))
233 *val = strtoll(str, NULL, base);
234 if (*val == LONG_MAX && errno == ERANGE)
238 unsigned long long mult;
241 mult = get_mult_bytes(str, len, data, &perc);
247 *val *= get_mult_time(str[len - 1]);
252 static int check_str_bytes(const char *p, long long *val, void *data)
254 return str_to_decimal(p, val, 1, data);
257 static int check_str_time(const char *p, long long *val)
259 return str_to_decimal(p, val, 0, NULL);
262 void strip_blank_front(char **p)
266 while (isspace((int) *s))
272 void strip_blank_end(char *p)
286 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
292 static int check_range_bytes(const char *str, long *val, void *data)
296 if (!str_to_decimal(str, &__val, 1, data)) {
304 static int check_int(const char *p, int *val)
308 if (strstr(p, "0x") || strstr(p, "0X")) {
309 if (sscanf(p, "%x", val) == 1)
312 if (sscanf(p, "%u", val) == 1)
319 static int opt_len(const char *str)
323 postfix = strchr(str, ':');
327 return (int)(postfix - str);
330 #define val_store(ptr, val, off, or, data) \
332 ptr = td_var((data), (off)); \
339 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
340 int first, int more, int curr)
344 long long ull, *ullp;
348 int ret = 0, is_time = 0;
349 const struct value_pair *vp;
350 struct value_pair posval[PARSE_MAX_VP];
351 int i, all_skipped = 1;
353 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
356 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
357 fprintf(stderr, "Option %s requires an argument\n", o->name);
363 case FIO_OPT_STR_MULTI: {
364 fio_opt_str_fn *fn = o->cb;
366 posval_sort(o, posval);
369 for (i = 0; i < PARSE_MAX_VP; i++) {
371 if (!vp->ival || vp->ival[0] == '\0')
374 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
378 *(unsigned int *) o->roff1 |= vp->oval;
380 *(unsigned int *) o->roff1 = vp->oval;
384 val_store(ilp, vp->oval, o->off1, vp->or, data);
390 if (ret && !all_skipped)
391 show_option_values(o);
396 case FIO_OPT_STR_VAL_TIME:
399 case FIO_OPT_STR_VAL: {
400 fio_opt_str_val_fn *fn = o->cb;
403 ret = check_str_time(ptr, &ull);
405 ret = check_str_bytes(ptr, &ull, data);
410 if (o->maxval && ull > o->maxval) {
411 fprintf(stderr, "max value out of range: %lld"
412 " (%d max)\n", ull, o->maxval);
415 if (o->minval && ull < o->minval) {
416 fprintf(stderr, "min value out of range: %lld"
417 " (%d min)\n", ull, o->minval);
422 ret = fn(data, &ull);
424 if (o->type == FIO_OPT_INT) {
427 *(unsigned int *) o->roff1 = ull;
429 val_store(ilp, ull, o->off1, 0, data);
433 *(unsigned int *) o->roff2 = ull;
435 val_store(ilp, ull, o->off2, 0, data);
440 *(unsigned long long *) o->roff1 = ull;
442 val_store(ullp, ull, o->off1, 0, data);
446 *(unsigned long long *) o->roff2 = ull;
448 val_store(ullp, ull, o->off2, 0, data);
454 case FIO_OPT_FLOAT_LIST: {
458 ilp = td_var(data, o->off2);
461 if (curr >= o->maxlen) {
462 fprintf(stderr, "the list exceeding max length %d\n",
466 if(!str_to_float(ptr, &uf)){
467 fprintf(stderr, "not a floating point value: %s\n",
471 if (!isnan(o->maxfp) && uf > o->maxfp) {
472 fprintf(stderr, "value out of range: %f"
473 " (range max: %f)\n", uf, o->maxfp);
476 if (!isnan(o->minfp) && uf < o->minfp) {
477 fprintf(stderr, "value out of range: %f"
478 " (range min: %f)\n", uf, o->minfp);
482 flp = td_var(data, o->off1);
487 case FIO_OPT_STR_STORE: {
488 fio_opt_str_fn *fn = o->cb;
490 posval_sort(o, posval);
493 for (i = 0; i < PARSE_MAX_VP; i++) {
495 if (!vp->ival || vp->ival[0] == '\0')
498 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
505 cp = (char **) o->roff1;
507 cp = td_var(data, o->off1);
509 rest = strstr(*cp, ":");
519 if (ret && !all_skipped)
520 show_option_values(o);
526 case FIO_OPT_RANGE: {
530 strncpy(tmp, ptr, sizeof(tmp) - 1);
532 /* Handle bsrange with separate read,write values: */
533 p1 = strchr(tmp, ',');
537 p1 = strchr(tmp, '-');
539 p1 = strchr(tmp, ':');
551 if (!check_range_bytes(p1, &ul1, data) &&
552 !check_range_bytes(p2, &ul2, data)) {
555 unsigned long foo = ul1;
563 *(unsigned int *) o->roff1 = ul1;
565 val_store(ilp, ul1, o->off1, 0, data);
567 *(unsigned int *) o->roff2 = ul2;
569 val_store(ilp, ul2, o->off2, 0, data);
571 if (o->roff3 && o->roff4) {
572 *(unsigned int *) o->roff3 = ul1;
573 *(unsigned int *) o->roff4 = ul2;
574 } else if (o->off3 && o->off4) {
575 val_store(ilp, ul1, o->off3, 0, data);
576 val_store(ilp, ul2, o->off4, 0, data);
583 case FIO_OPT_STR_SET: {
584 fio_opt_int_fn *fn = o->cb;
587 ret = check_int(ptr, &il);
588 else if (o->type == FIO_OPT_BOOL)
596 if (o->maxval && il > (int) o->maxval) {
597 fprintf(stderr, "max value out of range: %d (%d max)\n",
601 if (o->minval && il < o->minval) {
602 fprintf(stderr, "min value out of range: %d (%d min)\n",
615 *(unsigned int *)o->roff1 = il;
617 val_store(ilp, il, o->off1, 0, data);
621 *(unsigned int *) o->roff2 = il;
623 val_store(ilp, il, o->off2, 0, data);
628 case FIO_OPT_DEPRECATED:
629 fprintf(stdout, "Option %s is deprecated\n", o->name);
632 fprintf(stderr, "Bad option type %u\n", o->type);
640 ret = o->verify(o, data);
642 fprintf(stderr,"Correct format for offending option\n");
643 fprintf(stderr, "%20s: %s\n", o->name, o->help);
644 show_option_help(o, stderr);
651 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
653 char *o_ptr, *ptr, *ptr2;
656 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
660 o_ptr = ptr = strdup(__ptr);
663 * See if we have another set of parameters, hidden after a comma.
664 * Do this before parsing this round, to check if we should
665 * copy set 1 options to set 2.
674 (o->type != FIO_OPT_STR_STORE) &&
675 (o->type != FIO_OPT_STR) &&
676 (o->type != FIO_OPT_FLOAT_LIST)) {
677 ptr2 = strchr(ptr, ',');
678 if (ptr2 && *(ptr2 + 1) == '\0')
680 if (o->type != FIO_OPT_STR_MULTI) {
682 ptr2 = strchr(ptr, ':');
684 ptr2 = strchr(ptr, '-');
686 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
687 ptr2 = strchr(ptr, ':');
691 * Don't return early if parsing the first option fails - if
692 * we are doing multiple arguments, we can allow the first one
695 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
711 static struct fio_option *get_option(const char *opt,
712 struct fio_option *options, char **post)
714 struct fio_option *o;
717 ret = strchr(opt, '=');
723 strip_blank_end(ret);
724 o = find_option(options, ret);
726 o = find_option(options, opt);
733 static int opt_cmp(const void *p1, const void *p2)
735 struct fio_option *o1, *o2;
739 s1 = strdup(*((char **) p1));
740 s2 = strdup(*((char **) p2));
742 o1 = get_option(s1, fio_options, &foo);
743 o2 = get_option(s2, fio_options, &foo);
753 return prio2 - prio1;
756 void sort_options(char **opts, struct fio_option *options, int num_opts)
758 fio_options = options;
759 qsort(opts, num_opts, sizeof(char *), opt_cmp);
763 int parse_cmd_option(const char *opt, const char *val,
764 struct fio_option *options, void *data)
766 struct fio_option *o;
768 o = find_option(options, opt);
770 fprintf(stderr, "Bad option <%s>\n", opt);
774 if (!handle_option(o, val, data))
777 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
782 * Return a copy of the input string with substrings of the form ${VARNAME}
783 * substituted with the value of the environment variable VARNAME. The
784 * substitution always occurs, even if VARNAME is empty or the corresponding
785 * environment variable undefined.
787 static char *option_dup_subs(const char *opt)
789 char out[OPT_LEN_MAX+1];
790 char in[OPT_LEN_MAX+1];
793 char *ch1, *ch2, *env;
794 ssize_t nchr = OPT_LEN_MAX;
797 if (strlen(opt) + 1 > OPT_LEN_MAX) {
798 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
802 in[OPT_LEN_MAX] = '\0';
803 strncpy(in, opt, OPT_LEN_MAX);
805 while (*inptr && nchr > 0) {
806 if (inptr[0] == '$' && inptr[1] == '{') {
807 ch2 = strchr(inptr, '}');
808 if (ch2 && inptr+1 < ch2) {
815 envlen = strlen(env);
816 if (envlen <= nchr) {
817 memcpy(outptr, env, envlen);
827 *outptr++ = *inptr++;
835 int parse_option(const char *opt, struct fio_option *options, void *data)
837 struct fio_option *o;
840 tmp = option_dup_subs(opt);
844 o = get_option(tmp, options, &post);
846 fprintf(stderr, "Bad option <%s>\n", tmp);
851 if (!handle_option(o, post, data)) {
856 fprintf(stderr, "fio: failed parsing %s\n", opt);
862 * Option match, levenshtein distance. Handy for not quite remembering what
863 * the option name is.
865 static int string_distance(const char *s1, const char *s2)
867 unsigned int s1_len = strlen(s1);
868 unsigned int s2_len = strlen(s2);
869 unsigned int *p, *q, *r;
872 p = malloc(sizeof(unsigned int) * (s2_len + 1));
873 q = malloc(sizeof(unsigned int) * (s2_len + 1));
876 for (i = 1; i <= s2_len; i++)
879 for (i = 1; i <= s1_len; i++) {
881 for (j = 1; j <= s2_len; j++) {
882 unsigned int sub = p[j - 1];
884 if (s1[i - 1] != s2[j - 1])
887 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
900 static struct fio_option *find_child(struct fio_option *options,
901 struct fio_option *o)
903 struct fio_option *__o;
905 for (__o = options + 1; __o->name; __o++)
906 if (__o->parent && !strcmp(__o->parent, o->name))
912 static void __print_option(struct fio_option *o, struct fio_option *org,
926 p += sprintf(p, "%s", " ");
928 sprintf(p, "%s", o->name);
930 printf("%-24s: %s\n", name, o->help);
933 static void print_option(struct fio_option *o)
935 struct fio_option *parent;
936 struct fio_option *__o;
937 unsigned int printed;
940 __print_option(o, NULL, 0);
947 while ((__o = find_child(o, parent)) != NULL) {
948 __print_option(__o, o, level);
957 int show_cmd_help(struct fio_option *options, const char *name)
959 struct fio_option *o, *closest;
960 unsigned int best_dist = -1U;
964 if (!name || !strcmp(name, "all"))
969 for (o = &options[0]; o->name; o++) {
972 if (o->type == FIO_OPT_DEPRECATED)
974 if (!exec_profile && o->prof_name)
978 if (!strcmp(name, o->name) ||
979 (o->alias && !strcmp(name, o->alias)))
984 dist = string_distance(name, o->name);
985 if (dist < best_dist) {
992 if (show_all || match) {
995 printf("%20s: %s\n", o->name, o->help);
1006 show_option_help(o, stdout);
1012 printf("No such command: %s", name);
1015 * Only print an appropriately close option, one where the edit
1016 * distance isn't too big. Otherwise we get crazy matches.
1018 if (closest && best_dist < 3) {
1019 printf(" - showing closest match\n");
1020 printf("%20s: %s\n", closest->name, closest->help);
1021 show_option_help(closest, stdout);
1029 * Handle parsing of default parameters.
1031 void fill_default_options(void *data, struct fio_option *options)
1033 struct fio_option *o;
1035 dprint(FD_PARSE, "filling default options\n");
1037 for (o = &options[0]; o->name; o++)
1039 handle_option(o, o->def, data);
1042 void option_init(struct fio_option *o)
1044 if (o->type == FIO_OPT_DEPRECATED)
1046 if (o->type == FIO_OPT_BOOL) {
1050 if (o->type == FIO_OPT_FLOAT_LIST) {
1054 if (o->type == FIO_OPT_STR_SET && o->def) {
1055 fprintf(stderr, "Option %s: string set option with"
1056 " default will always be true\n", o->name);
1058 if (!o->cb && (!o->off1 && !o->roff1)) {
1059 fprintf(stderr, "Option %s: neither cb nor offset given\n",
1062 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1063 o->type == FIO_OPT_STR_MULTI)
1065 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
1066 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
1067 fprintf(stderr, "Option %s: both cb and offset given\n",
1073 * Sanitize the options structure. For now it just sets min/max for bool
1074 * values and whether both callback and offsets are given.
1076 void options_init(struct fio_option *options)
1078 struct fio_option *o;
1080 dprint(FD_PARSE, "init options\n");
1082 for (o = &options[0]; o->name; o++)