blob: 5c23d91ebab6c36ea3f6790bac998900015aff20 [file] [log] [blame]
Jens Axboecb2c86f2006-10-26 13:48:34 +02001/*
2 * This file contains the ini and command liner parser main.
3 */
4#include <stdio.h>
5#include <stdlib.h>
6#include <unistd.h>
7#include <ctype.h>
8#include <string.h>
9#include <errno.h>
10#include <limits.h>
Aaron Carroll88b5a392008-10-07 11:25:20 +020011#include <stdlib.h>
Yu-ju Hong83349192011-08-13 00:53:44 +020012#include <math.h>
Bruce Cranab9461e2013-02-02 14:31:24 +000013#include <float.h>
Jens Axboecb2c86f2006-10-26 13:48:34 +020014
15#include "parse.h"
Jens Axboea3d741f2008-02-27 18:32:33 +010016#include "debug.h"
Jens Axboe9f988e22010-03-04 10:42:38 +010017#include "options.h"
Jens Axboee25839d2012-11-06 10:49:42 +010018#include "minmax.h"
Vincent Kang Fufd112d32013-02-02 09:28:55 +010019#include "lib/ieee754.h"
Jens Axboecb2c86f2006-10-26 13:48:34 +020020
Jens Axboe9af4a242012-03-16 10:13:49 +010021static struct fio_option *__fio_options;
Jens Axboe3b8b7132008-06-10 19:46:23 +020022
Jens Axboef0857372007-03-19 13:15:23 +010023static int vp_cmp(const void *p1, const void *p2)
24{
25 const struct value_pair *vp1 = p1;
26 const struct value_pair *vp2 = p2;
27
28 return strlen(vp2->ival) - strlen(vp1->ival);
29}
30
Jens Axboece952ab2011-08-31 14:29:22 -060031static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
Jens Axboef0857372007-03-19 13:15:23 +010032{
33 const struct value_pair *vp;
34 int entries;
35
36 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
37
38 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
39 vp = &o->posval[entries];
40 if (!vp->ival || vp->ival[0] == '\0')
41 break;
42
43 memcpy(&vpmap[entries], vp, sizeof(*vp));
44 }
45
46 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
47}
48
Jens Axboe06b0be62011-10-04 10:31:10 +020049static void show_option_range(struct fio_option *o,
50 int (*logger)(const char *format, ...))
Jens Axboeb1ec1da2007-02-23 10:29:16 +010051{
Jens Axboe3c037bc2013-04-10 11:21:33 +020052 if (o->type == FIO_OPT_FLOAT_LIST) {
Bruce Cranab9461e2013-02-02 14:31:24 +000053 if (o->minfp == DBL_MIN && o->maxfp == DBL_MAX)
Yu-ju Hong83349192011-08-13 00:53:44 +020054 return;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010055
Jens Axboe06b0be62011-10-04 10:31:10 +020056 logger("%20s: min=%f", "range", o->minfp);
Bruce Cranab9461e2013-02-02 14:31:24 +000057 if (o->maxfp != DBL_MAX)
Jens Axboe06b0be62011-10-04 10:31:10 +020058 logger(", max=%f", o->maxfp);
59 logger("\n");
Jens Axboe3c037bc2013-04-10 11:21:33 +020060 } else if (!o->posval[0].ival) {
Yu-ju Hong83349192011-08-13 00:53:44 +020061 if (!o->minval && !o->maxval)
62 return;
63
Jens Axboe06b0be62011-10-04 10:31:10 +020064 logger("%20s: min=%d", "range", o->minval);
Yu-ju Hong83349192011-08-13 00:53:44 +020065 if (o->maxval)
Jens Axboe06b0be62011-10-04 10:31:10 +020066 logger(", max=%d", o->maxval);
67 logger("\n");
Yu-ju Hong83349192011-08-13 00:53:44 +020068 }
Jens Axboeb1ec1da2007-02-23 10:29:16 +010069}
70
71static void show_option_values(struct fio_option *o)
72{
Jens Axboea3073f42010-03-05 10:06:15 +010073 int i;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010074
Jens Axboea3073f42010-03-05 10:06:15 +010075 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboe78372132007-03-14 13:24:07 +010076 const struct value_pair *vp = &o->posval[i];
77
78 if (!vp->ival)
Jens Axboea3073f42010-03-05 10:06:15 +010079 continue;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010080
Jens Axboe06b0be62011-10-04 10:31:10 +020081 log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
Jens Axboe78372132007-03-14 13:24:07 +010082 if (vp->help)
Jens Axboe06b0be62011-10-04 10:31:10 +020083 log_info(" %s", vp->help);
84 log_info("\n");
Jens Axboea3073f42010-03-05 10:06:15 +010085 }
Jens Axboeb1ec1da2007-02-23 10:29:16 +010086
87 if (i)
Jens Axboe06b0be62011-10-04 10:31:10 +020088 log_info("\n");
Jens Axboeb1ec1da2007-02-23 10:29:16 +010089}
90
Jens Axboe06b0be62011-10-04 10:31:10 +020091static void show_option_help(struct fio_option *o, int is_err)
Jens Axboed447a8c2009-07-17 22:26:15 +020092{
93 const char *typehelp[] = {
Jens Axboe07b32322010-03-05 09:48:44 +010094 "invalid",
Jens Axboed447a8c2009-07-17 22:26:15 +020095 "string (opt=bla)",
Jens Axboeae3fb6f2010-09-14 13:10:35 +020096 "string (opt=bla)",
Jens Axboed447a8c2009-07-17 22:26:15 +020097 "string with possible k/m/g postfix (opt=4k)",
98 "string with time postfix (opt=10s)",
99 "string (opt=bla)",
100 "string with dual range (opt=1k-4k,4k-8k)",
101 "integer value (opt=100)",
102 "boolean value (opt=1)",
Yu-ju Hong83349192011-08-13 00:53:44 +0200103 "list of floating point values separated by ':' (opt=5.9:7.8)",
Jens Axboed447a8c2009-07-17 22:26:15 +0200104 "no argument (opt)",
Jens Axboe07b32322010-03-05 09:48:44 +0100105 "deprecated",
Jens Axboed447a8c2009-07-17 22:26:15 +0200106 };
Jens Axboe06b0be62011-10-04 10:31:10 +0200107 int (*logger)(const char *format, ...);
108
109 if (is_err)
110 logger = log_err;
111 else
112 logger = log_info;
Jens Axboed447a8c2009-07-17 22:26:15 +0200113
114 if (o->alias)
Jens Axboe06b0be62011-10-04 10:31:10 +0200115 logger("%20s: %s\n", "alias", o->alias);
Jens Axboed447a8c2009-07-17 22:26:15 +0200116
Jens Axboe06b0be62011-10-04 10:31:10 +0200117 logger("%20s: %s\n", "type", typehelp[o->type]);
118 logger("%20s: %s\n", "default", o->def ? o->def : "no default");
Jens Axboe07b32322010-03-05 09:48:44 +0100119 if (o->prof_name)
Jens Axboe06b0be62011-10-04 10:31:10 +0200120 logger("%20s: only for profile '%s'\n", "valid", o->prof_name);
121 show_option_range(o, logger);
Jens Axboed447a8c2009-07-17 22:26:15 +0200122 show_option_values(o);
123}
124
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100125static unsigned long long get_mult_time(const char *str, int len)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200126{
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100127 const char *p = str;
128 char *c;
Jens Axboee4668262014-02-21 11:50:09 -0800129 unsigned long long mult = 1;
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100130
131 /*
132 * Go forward until we hit a non-digit, or +/- sign
133 */
134 while ((p - str) <= len) {
135 if (!isdigit((int) *p) && (*p != '+') && (*p != '-'))
136 break;
137 p++;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200138 }
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100139
140 if (!isalpha((int) *p))
Jens Axboee4668262014-02-21 11:50:09 -0800141 return mult;
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100142
143 c = strdup(p);
144 for (int i = 0; i < strlen(c); i++)
145 c[i] = tolower(c[i]);
146
Jens Axboee4668262014-02-21 11:50:09 -0800147 if (!strncmp("us", c, 2) || !strncmp("usec", c, 4))
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100148 mult = 1;
Jens Axboee4668262014-02-21 11:50:09 -0800149 else if (!strncmp("ms", c, 2) || !strncmp("msec", c, 4))
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100150 mult = 1000;
Jens Axboee4668262014-02-21 11:50:09 -0800151 else if (!strcmp("s", c))
152 mult = 1000000;
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100153 else if (!strcmp("m", c))
Jens Axboee4668262014-02-21 11:50:09 -0800154 mult = 60 * 1000000UL;
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100155 else if (!strcmp("h", c))
Jens Axboee4668262014-02-21 11:50:09 -0800156 mult = 60 * 60 * 1000000UL;
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100157 else if (!strcmp("d", c))
Jens Axboee4668262014-02-21 11:50:09 -0800158 mult = 24 * 60 * 60 * 1000000UL;
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100159
160 free(c);
161 return mult;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200162}
163
Jens Axboea03fb652013-03-29 12:20:51 -0600164static int is_separator(char c)
165{
166 switch (c) {
167 case ':':
168 case '-':
169 case ',':
170 case '/':
171 return 1;
172 default:
173 return 0;
174 }
175}
176
Jens Axboe7bb59102011-07-12 19:47:03 +0200177static unsigned long long __get_mult_bytes(const char *p, void *data,
178 int *percent)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200179{
Jens Axboed6978a32009-07-18 21:04:09 +0200180 unsigned int kb_base = fio_get_kb_base(data);
Jens Axboea639f0b2009-07-18 08:25:35 +0200181 unsigned long long ret = 1;
Jens Axboe57fc29f2010-06-23 22:24:07 +0200182 unsigned int i, pow = 0, mult = kb_base;
183 char *c;
Jens Axboea639f0b2009-07-18 08:25:35 +0200184
Jens Axboe57fc29f2010-06-23 22:24:07 +0200185 if (!p)
186 return 1;
Jens Axboea639f0b2009-07-18 08:25:35 +0200187
Jens Axboe57fc29f2010-06-23 22:24:07 +0200188 c = strdup(p);
189
Jens Axboea03fb652013-03-29 12:20:51 -0600190 for (i = 0; i < strlen(c); i++) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200191 c[i] = tolower(c[i]);
Jens Axboea03fb652013-03-29 12:20:51 -0600192 if (is_separator(c[i])) {
193 c[i] = '\0';
194 break;
195 }
196 }
Jens Axboe57fc29f2010-06-23 22:24:07 +0200197
Jens Axboea04f1582012-03-07 10:53:29 +0100198 if (!strncmp("pib", c, 3)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200199 pow = 5;
200 mult = 1000;
Jens Axboea04f1582012-03-07 10:53:29 +0100201 } else if (!strncmp("tib", c, 3)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200202 pow = 4;
203 mult = 1000;
Jens Axboea04f1582012-03-07 10:53:29 +0100204 } else if (!strncmp("gib", c, 3)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200205 pow = 3;
206 mult = 1000;
Jens Axboea04f1582012-03-07 10:53:29 +0100207 } else if (!strncmp("mib", c, 3)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200208 pow = 2;
209 mult = 1000;
Jens Axboea04f1582012-03-07 10:53:29 +0100210 } else if (!strncmp("kib", c, 3)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200211 pow = 1;
212 mult = 1000;
Jens Axboea04f1582012-03-07 10:53:29 +0100213 } else if (!strncmp("p", c, 1) || !strncmp("pb", c, 2))
Jens Axboe57fc29f2010-06-23 22:24:07 +0200214 pow = 5;
Jens Axboea04f1582012-03-07 10:53:29 +0100215 else if (!strncmp("t", c, 1) || !strncmp("tb", c, 2))
Jens Axboe57fc29f2010-06-23 22:24:07 +0200216 pow = 4;
Jens Axboea04f1582012-03-07 10:53:29 +0100217 else if (!strncmp("g", c, 1) || !strncmp("gb", c, 2))
Jens Axboe57fc29f2010-06-23 22:24:07 +0200218 pow = 3;
Jens Axboea04f1582012-03-07 10:53:29 +0100219 else if (!strncmp("m", c, 1) || !strncmp("mb", c, 2))
Jens Axboe57fc29f2010-06-23 22:24:07 +0200220 pow = 2;
Jens Axboea04f1582012-03-07 10:53:29 +0100221 else if (!strncmp("k", c, 1) || !strncmp("kb", c, 2))
Jens Axboe57fc29f2010-06-23 22:24:07 +0200222 pow = 1;
Jens Axboea04f1582012-03-07 10:53:29 +0100223 else if (!strncmp("%", c, 1)) {
Jens Axboe7bb59102011-07-12 19:47:03 +0200224 *percent = 1;
Jens Axboee721c572012-02-09 20:55:29 +0100225 free(c);
Jens Axboe7bb59102011-07-12 19:47:03 +0200226 return ret;
227 }
Jens Axboe57fc29f2010-06-23 22:24:07 +0200228
229 while (pow--)
230 ret *= (unsigned long long) mult;
231
232 free(c);
Jens Axboea639f0b2009-07-18 08:25:35 +0200233 return ret;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200234}
235
Jens Axboe7bb59102011-07-12 19:47:03 +0200236static unsigned long long get_mult_bytes(const char *str, int len, void *data,
Jens Axboe6925dd32011-09-07 22:10:11 +0200237 int *percent)
Jens Axboe57fc29f2010-06-23 22:24:07 +0200238{
Jens Axboe55ed9632011-03-27 20:55:09 +0200239 const char *p = str;
Jens Axboeba4ddd62011-09-07 22:11:00 +0200240 int digit_seen = 0;
Jens Axboe57fc29f2010-06-23 22:24:07 +0200241
Jens Axboe1d1c1872010-07-29 10:50:05 +0200242 if (len < 2)
Jens Axboe7bb59102011-07-12 19:47:03 +0200243 return __get_mult_bytes(str, data, percent);
Jens Axboe1d1c1872010-07-29 10:50:05 +0200244
Steven Langd0c814e2011-10-28 08:37:13 +0200245 /*
246 * Go forward until we hit a non-digit, or +/- sign
247 */
Jens Axboe55ed9632011-03-27 20:55:09 +0200248 while ((p - str) <= len) {
Jens Axboeba4ddd62011-09-07 22:11:00 +0200249 if (!isdigit((int) *p) &&
250 (((*p != '+') && (*p != '-')) || digit_seen))
Jens Axboe55ed9632011-03-27 20:55:09 +0200251 break;
Jens Axboe3c703d12011-09-21 09:27:24 +0200252 digit_seen |= isdigit((int) *p);
Jens Axboe55ed9632011-03-27 20:55:09 +0200253 p++;
254 }
255
Jens Axboe7bb59102011-07-12 19:47:03 +0200256 if (!isalpha((int) *p) && (*p != '%'))
Jens Axboe57fc29f2010-06-23 22:24:07 +0200257 p = NULL;
258
Jens Axboe7bb59102011-07-12 19:47:03 +0200259 return __get_mult_bytes(p, data, percent);
Jens Axboe57fc29f2010-06-23 22:24:07 +0200260}
261
Jens Axboecb2c86f2006-10-26 13:48:34 +0200262/*
Yu-ju Hong83349192011-08-13 00:53:44 +0200263 * Convert string into a floating number. Return 1 for success and 0 otherwise.
264 */
Jens Axboee25839d2012-11-06 10:49:42 +0100265int str_to_float(const char *str, double *val)
Yu-ju Hong83349192011-08-13 00:53:44 +0200266{
267 return (1 == sscanf(str, "%lf", val));
268}
269
270/*
Jens Axboee1f36502006-10-27 10:54:08 +0200271 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200272 */
Jens Axboe6925dd32011-09-07 22:10:11 +0200273int str_to_decimal(const char *str, long long *val, int kilo, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200274{
Jens Axboeb347f9d2009-03-09 14:15:21 +0100275 int len, base;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200276
Jens Axboecb2c86f2006-10-26 13:48:34 +0200277 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100278 if (!len)
279 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200280
Jens Axboeb347f9d2009-03-09 14:15:21 +0100281 if (strstr(str, "0x") || strstr(str, "0X"))
282 base = 16;
283 else
284 base = 10;
285
286 *val = strtoll(str, NULL, base);
Jens Axboecda866c2007-01-10 16:02:32 +0100287 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200288 return 1;
289
Jens Axboe7bb59102011-07-12 19:47:03 +0200290 if (kilo) {
291 unsigned long long mult;
292 int perc = 0;
293
Jens Axboe6925dd32011-09-07 22:10:11 +0200294 mult = get_mult_bytes(str, len, data, &perc);
Jens Axboe7bb59102011-07-12 19:47:03 +0200295 if (perc)
296 *val = -1ULL - *val;
297 else
298 *val *= mult;
299 } else
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100300 *val *= get_mult_time(str, len);
Jens Axboe787f7e92006-11-06 13:26:29 +0100301
Jens Axboecb2c86f2006-10-26 13:48:34 +0200302 return 0;
303}
304
Jens Axboe9af4a242012-03-16 10:13:49 +0100305int check_str_bytes(const char *p, long long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200306{
Jens Axboe6925dd32011-09-07 22:10:11 +0200307 return str_to_decimal(p, val, 1, data);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200308}
309
Jens Axboe06464902013-04-24 20:38:54 -0600310int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200311{
Jens Axboe6925dd32011-09-07 22:10:11 +0200312 return str_to_decimal(p, val, 0, NULL);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200313}
314
315void strip_blank_front(char **p)
316{
317 char *s = *p;
318
Jens Axboe4c8e9642011-10-15 14:37:38 +0200319 if (!strlen(s))
320 return;
Jens Axboe76cd9372011-07-08 21:14:57 +0200321 while (isspace((int) *s))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200322 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200323
324 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200325}
326
327void strip_blank_end(char *p)
328{
Jens Axboe853ee7f2009-03-06 20:29:29 +0100329 char *start = p, *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200330
Jens Axboe4c8e9642011-10-15 14:37:38 +0200331 if (!strlen(p))
332 return;
333
Jens Axboe523bfad2007-04-04 11:04:06 +0200334 s = strchr(p, ';');
335 if (s)
336 *s = '\0';
337 s = strchr(p, '#');
338 if (s)
339 *s = '\0';
340 if (s)
341 p = s;
342
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200343 s = p + strlen(p);
Jens Axboe76cd9372011-07-08 21:14:57 +0200344 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200345 s--;
346
347 *(s + 1) = '\0';
348}
349
Jens Axboed6978a32009-07-18 21:04:09 +0200350static int check_range_bytes(const char *str, long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200351{
Jens Axboe57fc29f2010-06-23 22:24:07 +0200352 long long __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200353
Jens Axboe6925dd32011-09-07 22:10:11 +0200354 if (!str_to_decimal(str, &__val, 1, data)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200355 *val = __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200356 return 0;
357 }
358
Jens Axboecb2c86f2006-10-26 13:48:34 +0200359 return 1;
360}
361
Jens Axboe63f29372007-01-10 13:20:09 +0100362static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200363{
Jens Axboe787f7e92006-11-06 13:26:29 +0100364 if (!strlen(p))
365 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200366 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200367 if (sscanf(p, "%x", val) == 1)
368 return 0;
369 } else {
370 if (sscanf(p, "%u", val) == 1)
371 return 0;
372 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200373
374 return 1;
375}
376
Jens Axboe808def72010-07-14 16:27:02 -0600377static int opt_len(const char *str)
378{
379 char *postfix;
380
381 postfix = strchr(str, ':');
382 if (!postfix)
383 return strlen(str);
384
385 return (int)(postfix - str);
386}
387
Jens Axboe119cd932012-12-06 17:34:57 +0100388static int str_match_len(const struct value_pair *vp, const char *str)
389{
390 return max(strlen(vp->ival), opt_len(str));
391}
392
Jens Axboef0fdbca2014-02-11 15:44:50 -0700393#define val_store(ptr, val, off, or, data, o) \
394 do { \
395 ptr = td_var((data), (o), (off)); \
396 if ((or)) \
397 *ptr |= (val); \
398 else \
399 *ptr = (val); \
Jens Axboe17abbe82006-11-06 11:23:10 +0100400 } while (0)
401
Jens Axboef90eff52006-11-06 11:08:21 +0100402static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Yu-ju Hong83349192011-08-13 00:53:44 +0200403 int first, int more, int curr)
Jens Axboee1f36502006-10-27 10:54:08 +0200404{
Erwan Velu2fdbefd2013-07-22 23:48:48 +0200405 int il=0, *ilp;
Vincent Kang Fufd112d32013-02-02 09:28:55 +0100406 fio_fp64_t *flp;
Jens Axboe63f29372007-01-10 13:20:09 +0100407 long long ull, *ullp;
408 long ul1, ul2;
Yu-ju Hong83349192011-08-13 00:53:44 +0200409 double uf;
Jens Axboebfe1d592012-11-29 21:33:03 +0100410 char **cp = NULL;
Jens Axboee42b01e2011-05-05 12:05:10 -0600411 int ret = 0, is_time = 0;
Jens Axboec44b1ff2011-08-30 20:43:53 -0600412 const struct value_pair *vp;
413 struct value_pair posval[PARSE_MAX_VP];
414 int i, all_skipped = 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200415
Jens Axboea3d741f2008-02-27 18:32:33 +0100416 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
417 o->type, ptr);
418
Jens Axboee3cedca2008-11-19 19:57:52 +0100419 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200420 log_err("Option %s requires an argument\n", o->name);
Jens Axboe08e26e32006-11-21 13:15:10 +0100421 return 1;
422 }
423
Jens Axboee1f36502006-10-27 10:54:08 +0200424 switch (o->type) {
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100425 case FIO_OPT_STR:
426 case FIO_OPT_STR_MULTI: {
Jens Axboee1f36502006-10-27 10:54:08 +0200427 fio_opt_str_fn *fn = o->cb;
428
Jens Axboef0857372007-03-19 13:15:23 +0100429 posval_sort(o, posval);
430
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100431 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100432 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100433 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100434 if (!vp->ival || vp->ival[0] == '\0')
Jens Axboea3073f42010-03-05 10:06:15 +0100435 continue;
Jens Axboeae2ddba2010-03-10 12:49:03 +0100436 all_skipped = 0;
Jens Axboe119cd932012-12-06 17:34:57 +0100437 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100438 ret = 0;
Jens Axboe7b504ed2014-02-11 14:19:38 -0700439 if (o->off1)
Daniel Gollubebadc0c2014-02-12 08:24:57 -0700440 val_store(ilp, vp->oval, o->off1, vp->orval, data, o);
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100441 continue;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100442 }
443 }
444
Jens Axboeae2ddba2010-03-10 12:49:03 +0100445 if (ret && !all_skipped)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100446 show_option_values(o);
447 else if (fn)
448 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200449 break;
450 }
Jens Axboee42b01e2011-05-05 12:05:10 -0600451 case FIO_OPT_STR_VAL_TIME:
452 is_time = 1;
Jens Axboef7fa2652009-03-09 14:20:20 +0100453 case FIO_OPT_INT:
Jens Axboee42b01e2011-05-05 12:05:10 -0600454 case FIO_OPT_STR_VAL: {
455 fio_opt_str_val_fn *fn = o->cb;
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200456 char tmp[128], *p;
457
458 strncpy(tmp, ptr, sizeof(tmp) - 1);
459 p = strchr(tmp, ',');
460 if (p)
461 *p = '\0';
Jens Axboee1f36502006-10-27 10:54:08 +0200462
Jens Axboee42b01e2011-05-05 12:05:10 -0600463 if (is_time)
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200464 ret = check_str_time(tmp, &ull);
Jens Axboee42b01e2011-05-05 12:05:10 -0600465 else
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200466 ret = check_str_bytes(tmp, &ull, data);
Jens Axboee42b01e2011-05-05 12:05:10 -0600467
Jens Axboeae3fcb52013-04-25 10:06:32 -0600468 dprint(FD_PARSE, " ret=%d, out=%llu\n", ret, ull);
469
Jens Axboee1f36502006-10-27 10:54:08 +0200470 if (ret)
471 break;
472
Jens Axboedb8e0162007-01-10 13:41:26 +0100473 if (o->maxval && ull > o->maxval) {
Jens Axboed4fc2f02012-09-24 14:32:26 +0200474 log_err("max value out of range: %llu"
475 " (%u max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100476 return 1;
477 }
478 if (o->minval && ull < o->minval) {
Jens Axboed4fc2f02012-09-24 14:32:26 +0200479 log_err("min value out of range: %llu"
480 " (%u min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100481 return 1;
482 }
Jens Axboed926d532013-04-09 21:02:15 +0200483 if (o->posval[0].ival) {
484 posval_sort(o, posval);
485
486 ret = 1;
487 for (i = 0; i < PARSE_MAX_VP; i++) {
488 vp = &posval[i];
489 if (!vp->ival || vp->ival[0] == '\0')
490 continue;
491 if (vp->oval == ull) {
492 ret = 0;
493 break;
494 }
495 }
496 if (ret) {
Jens Axboe128e4ca2013-04-10 11:06:34 +0200497 log_err("fio: value %llu not allowed:\n", ull);
498 show_option_values(o);
Jens Axboed926d532013-04-09 21:02:15 +0200499 return 1;
500 }
501 }
Jens Axboee1f36502006-10-27 10:54:08 +0200502
503 if (fn)
504 ret = fn(data, &ull);
505 else {
Jens Axboee01b22b2009-06-09 15:43:25 +0200506 if (o->type == FIO_OPT_INT) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700507 if (first)
508 val_store(ilp, ull, o->off1, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200509 if (curr == 1) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700510 if (o->off2)
511 val_store(ilp, ull, o->off2, 0, data, o);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100512 }
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200513 if (curr == 2) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700514 if (o->off3)
515 val_store(ilp, ull, o->off3, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200516 }
517 if (!more) {
518 if (curr < 1) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700519 if (o->off2)
520 val_store(ilp, ull, o->off2, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200521 }
522 if (curr < 2) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700523 if (o->off3)
524 val_store(ilp, ull, o->off3, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200525 }
526 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100527 } else {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700528 if (first)
529 val_store(ullp, ull, o->off1, 0, data, o);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100530 if (!more) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700531 if (o->off2)
532 val_store(ullp, ull, o->off2, 0, data, o);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100533 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100534 }
Jens Axboee1f36502006-10-27 10:54:08 +0200535 }
536 break;
537 }
Yu-ju Hong83349192011-08-13 00:53:44 +0200538 case FIO_OPT_FLOAT_LIST: {
Jens Axboeeef02442013-02-06 08:51:57 +0100539 char *cp2;
540
Vincent Kang Fu435d1952013-02-06 08:43:40 +0100541 if (first) {
542 /*
543 ** Initialize precision to 0 and zero out list
544 ** in case specified list is shorter than default
545 */
Jens Axboe3e260a42013-12-09 12:38:53 -0700546 if (o->off2) {
547 ul2 = 0;
Jens Axboef0fdbca2014-02-11 15:44:50 -0700548 ilp = td_var(data, o, o->off2);
Jens Axboe3e260a42013-12-09 12:38:53 -0700549 *ilp = ul2;
550 }
Vincent Kang Fu435d1952013-02-06 08:43:40 +0100551
Jens Axboef0fdbca2014-02-11 15:44:50 -0700552 flp = td_var(data, o, o->off1);
Vincent Kang Fu435d1952013-02-06 08:43:40 +0100553 for(i = 0; i < o->maxlen; i++)
554 flp[i].u.f = 0.0;
555 }
Yu-ju Hong83349192011-08-13 00:53:44 +0200556 if (curr >= o->maxlen) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200557 log_err("the list exceeding max length %d\n",
Yu-ju Hong83349192011-08-13 00:53:44 +0200558 o->maxlen);
559 return 1;
560 }
Jens Axboe3c3ed072012-03-27 09:12:39 +0200561 if (!str_to_float(ptr, &uf)) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200562 log_err("not a floating point value: %s\n", ptr);
Yu-ju Hong83349192011-08-13 00:53:44 +0200563 return 1;
564 }
Jens Axboe26650692013-02-02 09:56:23 +0100565 if (uf > o->maxfp) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200566 log_err("value out of range: %f"
Yu-ju Hong83349192011-08-13 00:53:44 +0200567 " (range max: %f)\n", uf, o->maxfp);
568 return 1;
569 }
Jens Axboe26650692013-02-02 09:56:23 +0100570 if (uf < o->minfp) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200571 log_err("value out of range: %f"
Yu-ju Hong83349192011-08-13 00:53:44 +0200572 " (range min: %f)\n", uf, o->minfp);
573 return 1;
574 }
575
Jens Axboef0fdbca2014-02-11 15:44:50 -0700576 flp = td_var(data, o, o->off1);
Vincent Kang Fufd112d32013-02-02 09:28:55 +0100577 flp[curr].u.f = uf;
Yu-ju Hong83349192011-08-13 00:53:44 +0200578
Jens Axboeae3fcb52013-04-25 10:06:32 -0600579 dprint(FD_PARSE, " out=%f\n", uf);
580
Vincent Kang Fu435d1952013-02-06 08:43:40 +0100581 /*
582 ** Calculate precision for output by counting
583 ** number of digits after period. Find first
584 ** period in entire remaining list each time
585 */
586 cp2 = strchr(ptr, '.');
587 if (cp2 != NULL) {
Jens Axboeeef02442013-02-06 08:51:57 +0100588 int len = 0;
Vincent Kang Fu435d1952013-02-06 08:43:40 +0100589
590 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
591 len++;
592
Jens Axboe3e260a42013-12-09 12:38:53 -0700593 if (o->off2) {
Jens Axboef0fdbca2014-02-11 15:44:50 -0700594 ilp = td_var(data, o, o->off2);
Jens Axboe3e260a42013-12-09 12:38:53 -0700595 if (len > *ilp)
596 *ilp = len;
597 }
Vincent Kang Fu435d1952013-02-06 08:43:40 +0100598 }
599
Yu-ju Hong83349192011-08-13 00:53:44 +0200600 break;
601 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100602 case FIO_OPT_STR_STORE: {
603 fio_opt_str_fn *fn = o->cb;
604
Jens Axboe7b504ed2014-02-11 14:19:38 -0700605 if (o->off1) {
Jens Axboef0fdbca2014-02-11 15:44:50 -0700606 cp = td_var(data, o, o->off1);
Steven Lang184b4092011-11-16 10:33:51 +0100607 *cp = strdup(ptr);
Jens Axboe1c964ce2011-08-31 16:45:03 -0600608 }
Jens Axboe02c6aad2010-03-04 13:49:11 +0100609
Steven Lang184b4092011-11-16 10:33:51 +0100610 if (fn)
611 ret = fn(data, ptr);
612 else if (o->posval[0].ival) {
613 posval_sort(o, posval);
Jens Axboec44b1ff2011-08-30 20:43:53 -0600614
Steven Lang184b4092011-11-16 10:33:51 +0100615 ret = 1;
616 for (i = 0; i < PARSE_MAX_VP; i++) {
617 vp = &posval[i];
618 if (!vp->ival || vp->ival[0] == '\0')
619 continue;
620 all_skipped = 0;
Jens Axboe119cd932012-12-06 17:34:57 +0100621 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
Steven Lang184b4092011-11-16 10:33:51 +0100622 char *rest;
623
624 ret = 0;
625 if (vp->cb)
626 fn = vp->cb;
627 rest = strstr(*cp ?: ptr, ":");
628 if (rest) {
629 if (*cp)
630 *rest = '\0';
631 ptr = rest + 1;
632 } else
633 ptr = NULL;
634 break;
635 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100636 }
637 }
Jens Axboec44b1ff2011-08-30 20:43:53 -0600638
Steven Lang184b4092011-11-16 10:33:51 +0100639 if (!all_skipped) {
640 if (ret && !*cp)
641 show_option_values(o);
642 else if (ret && *cp)
643 ret = 0;
644 else if (fn && ptr)
645 ret = fn(data, ptr);
646 }
Jens Axboec44b1ff2011-08-30 20:43:53 -0600647
Jens Axboee1f36502006-10-27 10:54:08 +0200648 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100649 }
Jens Axboee1f36502006-10-27 10:54:08 +0200650 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200651 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200652 char *p1, *p2;
653
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100654 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200655
Dave Engberg31d23f42011-07-23 21:07:13 +0200656 /* Handle bsrange with separate read,write values: */
657 p1 = strchr(tmp, ',');
658 if (p1)
659 *p1 = '\0';
660
Jens Axboeb765a372006-10-27 15:00:16 +0200661 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200662 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100663 p1 = strchr(tmp, ':');
664 if (!p1) {
665 ret = 1;
666 break;
667 }
Jens Axboee1f36502006-10-27 10:54:08 +0200668 }
669
670 p2 = p1 + 1;
671 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200672 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200673
674 ret = 1;
Jens Axboed6978a32009-07-18 21:04:09 +0200675 if (!check_range_bytes(p1, &ul1, data) &&
676 !check_range_bytes(p2, &ul2, data)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200677 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200678 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100679 unsigned long foo = ul1;
680
681 ul1 = ul2;
682 ul2 = foo;
683 }
684
685 if (first) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700686 val_store(ilp, ul1, o->off1, 0, data, o);
687 val_store(ilp, ul2, o->off2, 0, data, o);
Jens Axboee1f36502006-10-27 10:54:08 +0200688 }
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200689 if (curr == 1) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700690 if (o->off3 && o->off4) {
691 val_store(ilp, ul1, o->off3, 0, data, o);
692 val_store(ilp, ul2, o->off4, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200693 }
694 }
695 if (curr == 2) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700696 if (o->off5 && o->off6) {
697 val_store(ilp, ul1, o->off5, 0, data, o);
698 val_store(ilp, ul2, o->off6, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200699 }
700 }
701 if (!more) {
702 if (curr < 1) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700703 if (o->off3 && o->off4) {
704 val_store(ilp, ul1, o->off3, 0, data, o);
705 val_store(ilp, ul2, o->off4, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200706 }
707 }
708 if (curr < 2) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700709 if (o->off5 && o->off6) {
710 val_store(ilp, ul1, o->off5, 0, data, o);
711 val_store(ilp, ul2, o->off6, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200712 }
713 }
Jens Axboe17abbe82006-11-06 11:23:10 +0100714 }
715 }
716
Jens Axboee1f36502006-10-27 10:54:08 +0200717 break;
718 }
Jens Axboe74ba1802011-07-15 09:09:15 +0200719 case FIO_OPT_BOOL:
720 case FIO_OPT_STR_SET: {
Jens Axboee1f36502006-10-27 10:54:08 +0200721 fio_opt_int_fn *fn = o->cb;
722
Jens Axboe74ba1802011-07-15 09:09:15 +0200723 if (ptr)
724 ret = check_int(ptr, &il);
725 else if (o->type == FIO_OPT_BOOL)
726 ret = 1;
727 else
728 il = 1;
729
Jens Axboeae3fcb52013-04-25 10:06:32 -0600730 dprint(FD_PARSE, " ret=%d, out=%d\n", ret, il);
731
Jens Axboee1f36502006-10-27 10:54:08 +0200732 if (ret)
733 break;
734
Jens Axboedb8e0162007-01-10 13:41:26 +0100735 if (o->maxval && il > (int) o->maxval) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200736 log_err("max value out of range: %d (%d max)\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100737 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100738 return 1;
739 }
740 if (o->minval && il < o->minval) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200741 log_err("min value out of range: %d (%d min)\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100742 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100743 return 1;
744 }
Jens Axboee1f36502006-10-27 10:54:08 +0200745
Jens Axboe76a43db2007-01-11 13:24:44 +0100746 if (o->neg)
747 il = !il;
748
Jens Axboee1f36502006-10-27 10:54:08 +0200749 if (fn)
750 ret = fn(data, &il);
751 else {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700752 if (first)
753 val_store(ilp, il, o->off1, 0, data, o);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100754 if (!more) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700755 if (o->off2)
756 val_store(ilp, il, o->off2, 0, data, o);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100757 }
Jens Axboee1f36502006-10-27 10:54:08 +0200758 }
759 break;
760 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200761 case FIO_OPT_DEPRECATED:
Jens Axboe06b0be62011-10-04 10:31:10 +0200762 log_info("Option %s is deprecated\n", o->name);
Jens Axboed9472272013-07-25 10:20:45 -0600763 ret = 1;
Jens Axboe15ca1502008-04-07 09:26:02 +0200764 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200765 default:
Jens Axboe06b0be62011-10-04 10:31:10 +0200766 log_err("Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200767 ret = 1;
768 }
769
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200770 if (ret)
771 return ret;
772
Jens Axboed447a8c2009-07-17 22:26:15 +0200773 if (o->verify) {
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200774 ret = o->verify(o, data);
Jens Axboed447a8c2009-07-17 22:26:15 +0200775 if (ret) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200776 log_err("Correct format for offending option\n");
777 log_err("%20s: %s\n", o->name, o->help);
Jens Axboe06b0be62011-10-04 10:31:10 +0200778 show_option_help(o, 1);
Jens Axboed447a8c2009-07-17 22:26:15 +0200779 }
780 }
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200781
Jens Axboee1f36502006-10-27 10:54:08 +0200782 return ret;
783}
784
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200785static int handle_option(struct fio_option *o, const char *__ptr, void *data)
Jens Axboef90eff52006-11-06 11:08:21 +0100786{
Jens Axboeb62bdf22010-03-10 12:36:17 +0100787 char *o_ptr, *ptr, *ptr2;
788 int ret, done;
Jens Axboef90eff52006-11-06 11:08:21 +0100789
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200790 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
791
Jens Axboeb62bdf22010-03-10 12:36:17 +0100792 o_ptr = ptr = NULL;
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200793 if (__ptr)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100794 o_ptr = ptr = strdup(__ptr);
Jens Axboea3d741f2008-02-27 18:32:33 +0100795
Jens Axboef90eff52006-11-06 11:08:21 +0100796 /*
Jens Axboeb62bdf22010-03-10 12:36:17 +0100797 * See if we have another set of parameters, hidden after a comma.
798 * Do this before parsing this round, to check if we should
Jens Axboe787f7e92006-11-06 13:26:29 +0100799 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100800 */
Jens Axboeb62bdf22010-03-10 12:36:17 +0100801 done = 0;
802 ret = 1;
803 do {
804 int __ret;
805
806 ptr2 = NULL;
807 if (ptr &&
808 (o->type != FIO_OPT_STR_STORE) &&
Yu-ju Hong83349192011-08-13 00:53:44 +0200809 (o->type != FIO_OPT_STR) &&
810 (o->type != FIO_OPT_FLOAT_LIST)) {
Jens Axboeb62bdf22010-03-10 12:36:17 +0100811 ptr2 = strchr(ptr, ',');
812 if (ptr2 && *(ptr2 + 1) == '\0')
813 *ptr2 = '\0';
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200814 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
Jens Axboeb62bdf22010-03-10 12:36:17 +0100815 if (!ptr2)
816 ptr2 = strchr(ptr, ':');
817 if (!ptr2)
818 ptr2 = strchr(ptr, '-');
819 }
Yu-ju Hong83349192011-08-13 00:53:44 +0200820 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
821 ptr2 = strchr(ptr, ':');
Jens Axboeb62bdf22010-03-10 12:36:17 +0100822 }
823
824 /*
825 * Don't return early if parsing the first option fails - if
826 * we are doing multiple arguments, we can allow the first one
827 * being empty.
828 */
Yu-ju Hong83349192011-08-13 00:53:44 +0200829 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
Jens Axboeb62bdf22010-03-10 12:36:17 +0100830 if (ret)
831 ret = __ret;
832
Jens Axboe0c9baf92007-01-11 15:59:26 +0100833 if (!ptr2)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100834 break;
Jens Axboe787f7e92006-11-06 13:26:29 +0100835
Jens Axboeb62bdf22010-03-10 12:36:17 +0100836 ptr = ptr2 + 1;
837 done++;
838 } while (1);
Jens Axboe787f7e92006-11-06 13:26:29 +0100839
Jens Axboeb62bdf22010-03-10 12:36:17 +0100840 if (o_ptr)
841 free(o_ptr);
842 return ret;
Jens Axboef90eff52006-11-06 11:08:21 +0100843}
844
Steven Langde890a12011-11-09 14:03:34 +0100845static struct fio_option *get_option(char *opt,
Jens Axboe07b32322010-03-05 09:48:44 +0100846 struct fio_option *options, char **post)
Jens Axboe3b8b7132008-06-10 19:46:23 +0200847{
848 struct fio_option *o;
849 char *ret;
850
851 ret = strchr(opt, '=');
852 if (ret) {
853 *post = ret;
854 *ret = '\0';
Steven Langde890a12011-11-09 14:03:34 +0100855 ret = opt;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200856 (*post)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100857 strip_blank_end(ret);
Jens Axboe07b32322010-03-05 09:48:44 +0100858 o = find_option(options, ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200859 } else {
Jens Axboe07b32322010-03-05 09:48:44 +0100860 o = find_option(options, opt);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200861 *post = NULL;
862 }
863
864 return o;
865}
866
867static int opt_cmp(const void *p1, const void *p2)
868{
Steven Langde890a12011-11-09 14:03:34 +0100869 struct fio_option *o;
870 char *s, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100871 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200872
Jens Axboe8cdabc12008-11-19 12:31:43 +0100873 prio1 = prio2 = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200874
Steven Langde890a12011-11-09 14:03:34 +0100875 if (*(char **)p1) {
876 s = strdup(*((char **) p1));
Jens Axboe9af4a242012-03-16 10:13:49 +0100877 o = get_option(s, __fio_options, &foo);
Steven Langde890a12011-11-09 14:03:34 +0100878 if (o)
879 prio1 = o->prio;
880 free(s);
881 }
882 if (*(char **)p2) {
883 s = strdup(*((char **) p2));
Jens Axboe9af4a242012-03-16 10:13:49 +0100884 o = get_option(s, __fio_options, &foo);
Steven Langde890a12011-11-09 14:03:34 +0100885 if (o)
886 prio2 = o->prio;
887 free(s);
888 }
889
Jens Axboe8cdabc12008-11-19 12:31:43 +0100890 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200891}
892
893void sort_options(char **opts, struct fio_option *options, int num_opts)
894{
Jens Axboe9af4a242012-03-16 10:13:49 +0100895 __fio_options = options;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200896 qsort(opts, num_opts, sizeof(char *), opt_cmp);
Jens Axboe9af4a242012-03-16 10:13:49 +0100897 __fio_options = NULL;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200898}
899
Jens Axboeb4692822006-10-27 13:43:22 +0200900int parse_cmd_option(const char *opt, const char *val,
Jens Axboe07b32322010-03-05 09:48:44 +0100901 struct fio_option *options, void *data)
Jens Axboeb4692822006-10-27 13:43:22 +0200902{
903 struct fio_option *o;
904
Jens Axboe07b32322010-03-05 09:48:44 +0100905 o = find_option(options, opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200906 if (!o) {
Jens Axboe06b0be62011-10-04 10:31:10 +0200907 log_err("Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200908 return 1;
909 }
910
Jens Axboeb1508cf2006-11-02 09:12:40 +0100911 if (!handle_option(o, val, data))
912 return 0;
913
Jens Axboe06b0be62011-10-04 10:31:10 +0200914 log_err("fio: failed parsing %s=%s\n", opt, val);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100915 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200916}
917
Steven Langde890a12011-11-09 14:03:34 +0100918int parse_option(char *opt, const char *input,
Jens Axboe292cc472013-11-26 20:39:35 -0700919 struct fio_option *options, struct fio_option **o, void *data,
920 int dump_cmdline)
Jens Axboee1f36502006-10-27 10:54:08 +0200921{
Steven Langd0c814e2011-10-28 08:37:13 +0200922 char *post;
Jens Axboee1f36502006-10-27 10:54:08 +0200923
Steven Langd0c814e2011-10-28 08:37:13 +0200924 if (!opt) {
925 log_err("fio: failed parsing %s\n", input);
Steven Langde890a12011-11-09 14:03:34 +0100926 *o = NULL;
Jens Axboe38789b52010-03-03 09:23:20 +0100927 return 1;
Steven Langd0c814e2011-10-28 08:37:13 +0200928 }
Jens Axboee1f36502006-10-27 10:54:08 +0200929
Steven Langde890a12011-11-09 14:03:34 +0100930 *o = get_option(opt, options, &post);
931 if (!*o) {
932 if (post) {
933 int len = strlen(opt);
934 if (opt + len + 1 != post)
935 memmove(opt + len + 1, post, strlen(post));
936 opt[len] = '=';
937 }
Jens Axboee1f36502006-10-27 10:54:08 +0200938 return 1;
939 }
940
Jens Axboe292cc472013-11-26 20:39:35 -0700941 if (handle_option(*o, post, data)) {
942 log_err("fio: failed parsing %s\n", input);
943 return 1;
944 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100945
Jens Axboe292cc472013-11-26 20:39:35 -0700946 if (dump_cmdline) {
947 const char *delim;
948
949 if (!strcmp("description", (*o)->name))
950 delim = "\"";
951 else
952 delim = "";
953
954 log_info("--%s%s", (*o)->name, post ? "" : " ");
955 if (post)
956 log_info("=%s%s%s ", delim, post, delim);
957 }
958
959 return 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200960}
Jens Axboefd28ca42007-01-09 21:20:13 +0100961
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100962/*
963 * Option match, levenshtein distance. Handy for not quite remembering what
964 * the option name is.
965 */
966static int string_distance(const char *s1, const char *s2)
967{
968 unsigned int s1_len = strlen(s1);
969 unsigned int s2_len = strlen(s2);
970 unsigned int *p, *q, *r;
971 unsigned int i, j;
972
973 p = malloc(sizeof(unsigned int) * (s2_len + 1));
974 q = malloc(sizeof(unsigned int) * (s2_len + 1));
975
976 p[0] = 0;
977 for (i = 1; i <= s2_len; i++)
978 p[i] = p[i - 1] + 1;
979
980 for (i = 1; i <= s1_len; i++) {
981 q[0] = p[0] + 1;
982 for (j = 1; j <= s2_len; j++) {
983 unsigned int sub = p[j - 1];
984
985 if (s1[i - 1] != s2[j - 1])
986 sub++;
987
988 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
989 }
990 r = p;
991 p = q;
992 q = r;
993 }
994
995 i = p[s2_len];
996 free(p);
997 free(q);
998 return i;
999}
1000
Jens Axboeafdf9352007-07-31 16:14:34 +02001001static struct fio_option *find_child(struct fio_option *options,
1002 struct fio_option *o)
1003{
1004 struct fio_option *__o;
1005
Jens Axboefdf28742007-07-31 23:06:09 +02001006 for (__o = options + 1; __o->name; __o++)
1007 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +02001008 return __o;
1009
1010 return NULL;
1011}
1012
Jens Axboe323d9112008-03-01 15:12:14 +01001013static void __print_option(struct fio_option *o, struct fio_option *org,
1014 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +02001015{
1016 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +01001017 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +02001018
1019 if (!o)
1020 return;
Jens Axboeef9aff52007-07-31 22:56:53 +02001021 if (!org)
1022 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001023
Jens Axboeafdf9352007-07-31 16:14:34 +02001024 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +01001025 depth = level;
1026 while (depth--)
1027 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +02001028
1029 sprintf(p, "%s", o->name);
1030
Jens Axboe28d7c362011-10-05 20:30:24 +02001031 log_info("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +01001032}
1033
1034static void print_option(struct fio_option *o)
1035{
1036 struct fio_option *parent;
1037 struct fio_option *__o;
1038 unsigned int printed;
1039 unsigned int level;
1040
1041 __print_option(o, NULL, 0);
1042 parent = o;
1043 level = 0;
1044 do {
1045 level++;
1046 printed = 0;
1047
1048 while ((__o = find_child(o, parent)) != NULL) {
1049 __print_option(__o, o, level);
1050 o = __o;
1051 printed++;
1052 }
1053
1054 parent = o;
1055 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +02001056}
1057
Jens Axboe07b32322010-03-05 09:48:44 +01001058int show_cmd_help(struct fio_option *options, const char *name)
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001059{
1060 struct fio_option *o, *closest;
Jens Axboed091d092010-04-01 19:56:23 +02001061 unsigned int best_dist = -1U;
Jens Axboe29fc6af2007-01-09 21:22:02 +01001062 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +01001063 int show_all = 0;
1064
1065 if (!name || !strcmp(name, "all"))
1066 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +01001067
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001068 closest = NULL;
1069 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +01001070 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +01001071 int match = 0;
1072
Jens Axboe15ca1502008-04-07 09:26:02 +02001073 if (o->type == FIO_OPT_DEPRECATED)
1074 continue;
Jens Axboe07b32322010-03-05 09:48:44 +01001075 if (!exec_profile && o->prof_name)
1076 continue;
Jens Axboe4ac23d22013-05-23 21:14:03 +02001077 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1078 continue;
Jens Axboe15ca1502008-04-07 09:26:02 +02001079
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001080 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +01001081 if (!strcmp(name, o->name) ||
1082 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001083 match = 1;
1084 else {
1085 unsigned int dist;
1086
1087 dist = string_distance(name, o->name);
1088 if (dist < best_dist) {
1089 best_dist = dist;
1090 closest = o;
1091 }
1092 }
1093 }
Jens Axboefd28ca42007-01-09 21:20:13 +01001094
1095 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +01001096 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +01001097 if (match)
Jens Axboe28d7c362011-10-05 20:30:24 +02001098 log_info("%20s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +01001099 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +02001100 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +01001101 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +01001102 continue;
Jens Axboec167ded2007-03-14 13:02:53 +01001103 }
Jens Axboefd28ca42007-01-09 21:20:13 +01001104 }
1105
Jens Axboe70df2f12007-01-11 14:04:47 +01001106 if (!match)
1107 continue;
1108
Jens Axboe06b0be62011-10-04 10:31:10 +02001109 show_option_help(o, 0);
Jens Axboefd28ca42007-01-09 21:20:13 +01001110 }
1111
Jens Axboe29fc6af2007-01-09 21:22:02 +01001112 if (found)
1113 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +01001114
Jens Axboe28d7c362011-10-05 20:30:24 +02001115 log_err("No such command: %s", name);
Jens Axboed091d092010-04-01 19:56:23 +02001116
1117 /*
1118 * Only print an appropriately close option, one where the edit
1119 * distance isn't too big. Otherwise we get crazy matches.
1120 */
1121 if (closest && best_dist < 3) {
Jens Axboe28d7c362011-10-05 20:30:24 +02001122 log_info(" - showing closest match\n");
1123 log_info("%20s: %s\n", closest->name, closest->help);
Jens Axboe06b0be62011-10-04 10:31:10 +02001124 show_option_help(closest, 0);
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001125 } else
Jens Axboe28d7c362011-10-05 20:30:24 +02001126 log_info("\n");
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001127
Jens Axboe29fc6af2007-01-09 21:22:02 +01001128 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +01001129}
Jens Axboeee738492007-01-10 11:23:16 +01001130
Jens Axboe13335dd2007-01-10 13:14:02 +01001131/*
1132 * Handle parsing of default parameters.
1133 */
Jens Axboeee738492007-01-10 11:23:16 +01001134void fill_default_options(void *data, struct fio_option *options)
1135{
Jens Axboe4945ba12007-01-11 14:36:56 +01001136 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +01001137
Jens Axboea3d741f2008-02-27 18:32:33 +01001138 dprint(FD_PARSE, "filling default options\n");
1139
Jens Axboe4945ba12007-01-11 14:36:56 +01001140 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +01001141 if (o->def)
1142 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +01001143}
Jens Axboe13335dd2007-01-10 13:14:02 +01001144
Jens Axboe9f988e22010-03-04 10:42:38 +01001145void option_init(struct fio_option *o)
1146{
1147 if (o->type == FIO_OPT_DEPRECATED)
1148 return;
1149 if (o->type == FIO_OPT_BOOL) {
1150 o->minval = 0;
1151 o->maxval = 1;
1152 }
Jens Axboed4fc2f02012-09-24 14:32:26 +02001153 if (o->type == FIO_OPT_INT) {
1154 if (!o->maxval)
1155 o->maxval = UINT_MAX;
1156 }
Yu-ju Hong83349192011-08-13 00:53:44 +02001157 if (o->type == FIO_OPT_FLOAT_LIST) {
Bruce Cranab9461e2013-02-02 14:31:24 +00001158 o->minfp = DBL_MIN;
1159 o->maxfp = DBL_MAX;
Yu-ju Hong83349192011-08-13 00:53:44 +02001160 }
Jens Axboe9f988e22010-03-04 10:42:38 +01001161 if (o->type == FIO_OPT_STR_SET && o->def) {
Jens Axboe06b0be62011-10-04 10:31:10 +02001162 log_err("Option %s: string set option with"
Jens Axboe9f988e22010-03-04 10:42:38 +01001163 " default will always be true\n", o->name);
1164 }
Jens Axboe7b504ed2014-02-11 14:19:38 -07001165 if (!o->cb && !o->off1)
Jens Axboe06b0be62011-10-04 10:31:10 +02001166 log_err("Option %s: neither cb nor offset given\n", o->name);
Jens Axboe22754742013-04-10 13:05:23 +02001167 if (!o->category) {
Jens Axboecca5b5b2013-04-10 19:35:25 +02001168 log_info("Option %s: no category defined. Setting to misc\n", o->name);
Jens Axboe22754742013-04-10 13:05:23 +02001169 o->category = FIO_OPT_C_GENERAL;
Jens Axboeffd78212013-04-10 13:07:43 +02001170 o->group = FIO_OPT_G_INVALID;
Jens Axboe22754742013-04-10 13:05:23 +02001171 }
Jens Axboe5f6ddf12010-03-09 20:40:44 +01001172 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1173 o->type == FIO_OPT_STR_MULTI)
Jens Axboe9f988e22010-03-04 10:42:38 +01001174 return;
Jens Axboe7b504ed2014-02-11 14:19:38 -07001175 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
Jens Axboe06b0be62011-10-04 10:31:10 +02001176 log_err("Option %s: both cb and offset given\n", o->name);
Jens Axboe9f988e22010-03-04 10:42:38 +01001177}
1178
Jens Axboe13335dd2007-01-10 13:14:02 +01001179/*
1180 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +01001181 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +01001182 */
1183void options_init(struct fio_option *options)
1184{
Jens Axboe4945ba12007-01-11 14:36:56 +01001185 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +01001186
Jens Axboea3d741f2008-02-27 18:32:33 +01001187 dprint(FD_PARSE, "init options\n");
1188
Jens Axboe90265352012-03-19 20:29:44 +01001189 for (o = &options[0]; o->name; o++) {
Jens Axboe9f988e22010-03-04 10:42:38 +01001190 option_init(o);
Jens Axboe90265352012-03-19 20:29:44 +01001191 if (o->inverse)
1192 o->inv_opt = find_option(options, o->inverse);
1193 }
Jens Axboe13335dd2007-01-10 13:14:02 +01001194}
Jens Axboe7e356b22011-10-14 10:55:16 +02001195
1196void options_free(struct fio_option *options, void *data)
1197{
1198 struct fio_option *o;
1199 char **ptr;
1200
1201 dprint(FD_PARSE, "free options\n");
1202
1203 for (o = &options[0]; o->name; o++) {
Steven Langde890a12011-11-09 14:03:34 +01001204 if (o->type != FIO_OPT_STR_STORE || !o->off1)
Jens Axboe7e356b22011-10-14 10:55:16 +02001205 continue;
1206
Jens Axboef0fdbca2014-02-11 15:44:50 -07001207 ptr = td_var(data, o, o->off1);
Jens Axboe7e356b22011-10-14 10:55:16 +02001208 if (*ptr) {
1209 free(*ptr);
1210 *ptr = NULL;
1211 }
1212 }
1213}