blob: c2d1cc811f6740a58bf0692a5c9f740e1d085aae [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
Stephen M. Cameron88b635b2014-09-29 12:10:49 -060021#ifdef CONFIG_ARITHMETIC
22#include "y.tab.h"
23#endif
24
Jens Axboe9af4a242012-03-16 10:13:49 +010025static struct fio_option *__fio_options;
Jens Axboe3b8b7132008-06-10 19:46:23 +020026
Jens Axboef0857372007-03-19 13:15:23 +010027static int vp_cmp(const void *p1, const void *p2)
28{
29 const struct value_pair *vp1 = p1;
30 const struct value_pair *vp2 = p2;
31
32 return strlen(vp2->ival) - strlen(vp1->ival);
33}
34
Jens Axboece952ab2011-08-31 14:29:22 -060035static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
Jens Axboef0857372007-03-19 13:15:23 +010036{
37 const struct value_pair *vp;
38 int entries;
39
40 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
41
42 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
43 vp = &o->posval[entries];
44 if (!vp->ival || vp->ival[0] == '\0')
45 break;
46
47 memcpy(&vpmap[entries], vp, sizeof(*vp));
48 }
49
50 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
51}
52
Jens Axboe06b0be62011-10-04 10:31:10 +020053static void show_option_range(struct fio_option *o,
54 int (*logger)(const char *format, ...))
Jens Axboeb1ec1da2007-02-23 10:29:16 +010055{
Jens Axboe3c037bc2013-04-10 11:21:33 +020056 if (o->type == FIO_OPT_FLOAT_LIST) {
Bruce Cranab9461e2013-02-02 14:31:24 +000057 if (o->minfp == DBL_MIN && o->maxfp == DBL_MAX)
Yu-ju Hong83349192011-08-13 00:53:44 +020058 return;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010059
Jens Axboe06b0be62011-10-04 10:31:10 +020060 logger("%20s: min=%f", "range", o->minfp);
Bruce Cranab9461e2013-02-02 14:31:24 +000061 if (o->maxfp != DBL_MAX)
Jens Axboe06b0be62011-10-04 10:31:10 +020062 logger(", max=%f", o->maxfp);
63 logger("\n");
Jens Axboe3c037bc2013-04-10 11:21:33 +020064 } else if (!o->posval[0].ival) {
Yu-ju Hong83349192011-08-13 00:53:44 +020065 if (!o->minval && !o->maxval)
66 return;
67
Jens Axboe06b0be62011-10-04 10:31:10 +020068 logger("%20s: min=%d", "range", o->minval);
Yu-ju Hong83349192011-08-13 00:53:44 +020069 if (o->maxval)
Jens Axboe06b0be62011-10-04 10:31:10 +020070 logger(", max=%d", o->maxval);
71 logger("\n");
Yu-ju Hong83349192011-08-13 00:53:44 +020072 }
Jens Axboeb1ec1da2007-02-23 10:29:16 +010073}
74
75static void show_option_values(struct fio_option *o)
76{
Jens Axboea3073f42010-03-05 10:06:15 +010077 int i;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010078
Jens Axboea3073f42010-03-05 10:06:15 +010079 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboe78372132007-03-14 13:24:07 +010080 const struct value_pair *vp = &o->posval[i];
81
82 if (!vp->ival)
Jens Axboea3073f42010-03-05 10:06:15 +010083 continue;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010084
Jens Axboe06b0be62011-10-04 10:31:10 +020085 log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
Jens Axboe78372132007-03-14 13:24:07 +010086 if (vp->help)
Jens Axboe06b0be62011-10-04 10:31:10 +020087 log_info(" %s", vp->help);
88 log_info("\n");
Jens Axboea3073f42010-03-05 10:06:15 +010089 }
Jens Axboeb1ec1da2007-02-23 10:29:16 +010090
91 if (i)
Jens Axboe06b0be62011-10-04 10:31:10 +020092 log_info("\n");
Jens Axboeb1ec1da2007-02-23 10:29:16 +010093}
94
Jens Axboe06b0be62011-10-04 10:31:10 +020095static void show_option_help(struct fio_option *o, int is_err)
Jens Axboed447a8c2009-07-17 22:26:15 +020096{
97 const char *typehelp[] = {
Jens Axboe07b32322010-03-05 09:48:44 +010098 "invalid",
Jens Axboed447a8c2009-07-17 22:26:15 +020099 "string (opt=bla)",
Jens Axboeae3fb6f2010-09-14 13:10:35 +0200100 "string (opt=bla)",
Jens Axboed447a8c2009-07-17 22:26:15 +0200101 "string with possible k/m/g postfix (opt=4k)",
102 "string with time postfix (opt=10s)",
103 "string (opt=bla)",
104 "string with dual range (opt=1k-4k,4k-8k)",
105 "integer value (opt=100)",
106 "boolean value (opt=1)",
Yu-ju Hong83349192011-08-13 00:53:44 +0200107 "list of floating point values separated by ':' (opt=5.9:7.8)",
Jens Axboed447a8c2009-07-17 22:26:15 +0200108 "no argument (opt)",
Jens Axboe07b32322010-03-05 09:48:44 +0100109 "deprecated",
Jens Axboed447a8c2009-07-17 22:26:15 +0200110 };
Jens Axboe06b0be62011-10-04 10:31:10 +0200111 int (*logger)(const char *format, ...);
112
113 if (is_err)
114 logger = log_err;
115 else
116 logger = log_info;
Jens Axboed447a8c2009-07-17 22:26:15 +0200117
118 if (o->alias)
Jens Axboe06b0be62011-10-04 10:31:10 +0200119 logger("%20s: %s\n", "alias", o->alias);
Jens Axboed447a8c2009-07-17 22:26:15 +0200120
Jens Axboe06b0be62011-10-04 10:31:10 +0200121 logger("%20s: %s\n", "type", typehelp[o->type]);
122 logger("%20s: %s\n", "default", o->def ? o->def : "no default");
Jens Axboe07b32322010-03-05 09:48:44 +0100123 if (o->prof_name)
Jens Axboe06b0be62011-10-04 10:31:10 +0200124 logger("%20s: only for profile '%s'\n", "valid", o->prof_name);
125 show_option_range(o, logger);
Jens Axboed447a8c2009-07-17 22:26:15 +0200126 show_option_values(o);
127}
128
Jens Axboe0de5b262014-02-21 15:26:01 -0800129static unsigned long long get_mult_time(const char *str, int len,
130 int is_seconds)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200131{
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100132 const char *p = str;
133 char *c;
Jens Axboee4668262014-02-21 11:50:09 -0800134 unsigned long long mult = 1;
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100135
136 /*
137 * Go forward until we hit a non-digit, or +/- sign
138 */
139 while ((p - str) <= len) {
140 if (!isdigit((int) *p) && (*p != '+') && (*p != '-'))
141 break;
142 p++;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200143 }
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100144
Jens Axboe0de5b262014-02-21 15:26:01 -0800145 if (!isalpha((int) *p)) {
146 if (is_seconds)
147 return 1000000UL;
148 else
149 return 1;
150 }
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100151
152 c = strdup(p);
153 for (int i = 0; i < strlen(c); i++)
154 c[i] = tolower(c[i]);
155
Jens Axboee4668262014-02-21 11:50:09 -0800156 if (!strncmp("us", c, 2) || !strncmp("usec", c, 4))
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100157 mult = 1;
Jens Axboee4668262014-02-21 11:50:09 -0800158 else if (!strncmp("ms", c, 2) || !strncmp("msec", c, 4))
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100159 mult = 1000;
Jens Axboee4668262014-02-21 11:50:09 -0800160 else if (!strcmp("s", c))
161 mult = 1000000;
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100162 else if (!strcmp("m", c))
Jens Axboee4668262014-02-21 11:50:09 -0800163 mult = 60 * 1000000UL;
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100164 else if (!strcmp("h", c))
Jens Axboee4668262014-02-21 11:50:09 -0800165 mult = 60 * 60 * 1000000UL;
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100166 else if (!strcmp("d", c))
Jens Axboee4668262014-02-21 11:50:09 -0800167 mult = 24 * 60 * 60 * 1000000UL;
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100168
169 free(c);
170 return mult;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200171}
172
Jens Axboea03fb652013-03-29 12:20:51 -0600173static int is_separator(char c)
174{
175 switch (c) {
176 case ':':
177 case '-':
178 case ',':
179 case '/':
180 return 1;
181 default:
182 return 0;
183 }
184}
185
Jens Axboe7bb59102011-07-12 19:47:03 +0200186static unsigned long long __get_mult_bytes(const char *p, void *data,
187 int *percent)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200188{
Jens Axboed6978a32009-07-18 21:04:09 +0200189 unsigned int kb_base = fio_get_kb_base(data);
Jens Axboea639f0b2009-07-18 08:25:35 +0200190 unsigned long long ret = 1;
Jens Axboe57fc29f2010-06-23 22:24:07 +0200191 unsigned int i, pow = 0, mult = kb_base;
192 char *c;
Jens Axboea639f0b2009-07-18 08:25:35 +0200193
Jens Axboe57fc29f2010-06-23 22:24:07 +0200194 if (!p)
195 return 1;
Jens Axboea639f0b2009-07-18 08:25:35 +0200196
Jens Axboe57fc29f2010-06-23 22:24:07 +0200197 c = strdup(p);
198
Jens Axboea03fb652013-03-29 12:20:51 -0600199 for (i = 0; i < strlen(c); i++) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200200 c[i] = tolower(c[i]);
Jens Axboea03fb652013-03-29 12:20:51 -0600201 if (is_separator(c[i])) {
202 c[i] = '\0';
203 break;
204 }
205 }
Jens Axboe57fc29f2010-06-23 22:24:07 +0200206
Jens Axboea04f1582012-03-07 10:53:29 +0100207 if (!strncmp("pib", c, 3)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200208 pow = 5;
209 mult = 1000;
Jens Axboea04f1582012-03-07 10:53:29 +0100210 } else if (!strncmp("tib", c, 3)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200211 pow = 4;
212 mult = 1000;
Jens Axboea04f1582012-03-07 10:53:29 +0100213 } else if (!strncmp("gib", c, 3)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200214 pow = 3;
215 mult = 1000;
Jens Axboea04f1582012-03-07 10:53:29 +0100216 } else if (!strncmp("mib", c, 3)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200217 pow = 2;
218 mult = 1000;
Jens Axboea04f1582012-03-07 10:53:29 +0100219 } else if (!strncmp("kib", c, 3)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200220 pow = 1;
221 mult = 1000;
Jens Axboea04f1582012-03-07 10:53:29 +0100222 } else if (!strncmp("p", c, 1) || !strncmp("pb", c, 2))
Jens Axboe57fc29f2010-06-23 22:24:07 +0200223 pow = 5;
Jens Axboea04f1582012-03-07 10:53:29 +0100224 else if (!strncmp("t", c, 1) || !strncmp("tb", c, 2))
Jens Axboe57fc29f2010-06-23 22:24:07 +0200225 pow = 4;
Jens Axboea04f1582012-03-07 10:53:29 +0100226 else if (!strncmp("g", c, 1) || !strncmp("gb", c, 2))
Jens Axboe57fc29f2010-06-23 22:24:07 +0200227 pow = 3;
Jens Axboea04f1582012-03-07 10:53:29 +0100228 else if (!strncmp("m", c, 1) || !strncmp("mb", c, 2))
Jens Axboe57fc29f2010-06-23 22:24:07 +0200229 pow = 2;
Jens Axboea04f1582012-03-07 10:53:29 +0100230 else if (!strncmp("k", c, 1) || !strncmp("kb", c, 2))
Jens Axboe57fc29f2010-06-23 22:24:07 +0200231 pow = 1;
Jens Axboea04f1582012-03-07 10:53:29 +0100232 else if (!strncmp("%", c, 1)) {
Jens Axboe7bb59102011-07-12 19:47:03 +0200233 *percent = 1;
Jens Axboee721c572012-02-09 20:55:29 +0100234 free(c);
Jens Axboe7bb59102011-07-12 19:47:03 +0200235 return ret;
236 }
Jens Axboe57fc29f2010-06-23 22:24:07 +0200237
238 while (pow--)
239 ret *= (unsigned long long) mult;
240
241 free(c);
Jens Axboea639f0b2009-07-18 08:25:35 +0200242 return ret;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200243}
244
Jens Axboe7bb59102011-07-12 19:47:03 +0200245static unsigned long long get_mult_bytes(const char *str, int len, void *data,
Jens Axboe6925dd32011-09-07 22:10:11 +0200246 int *percent)
Jens Axboe57fc29f2010-06-23 22:24:07 +0200247{
Jens Axboe55ed9632011-03-27 20:55:09 +0200248 const char *p = str;
Jens Axboeba4ddd62011-09-07 22:11:00 +0200249 int digit_seen = 0;
Jens Axboe57fc29f2010-06-23 22:24:07 +0200250
Jens Axboe1d1c1872010-07-29 10:50:05 +0200251 if (len < 2)
Jens Axboe7bb59102011-07-12 19:47:03 +0200252 return __get_mult_bytes(str, data, percent);
Jens Axboe1d1c1872010-07-29 10:50:05 +0200253
Steven Langd0c814e2011-10-28 08:37:13 +0200254 /*
255 * Go forward until we hit a non-digit, or +/- sign
256 */
Jens Axboe55ed9632011-03-27 20:55:09 +0200257 while ((p - str) <= len) {
Jens Axboeba4ddd62011-09-07 22:11:00 +0200258 if (!isdigit((int) *p) &&
259 (((*p != '+') && (*p != '-')) || digit_seen))
Jens Axboe55ed9632011-03-27 20:55:09 +0200260 break;
Jens Axboe3c703d12011-09-21 09:27:24 +0200261 digit_seen |= isdigit((int) *p);
Jens Axboe55ed9632011-03-27 20:55:09 +0200262 p++;
263 }
264
Jens Axboe7bb59102011-07-12 19:47:03 +0200265 if (!isalpha((int) *p) && (*p != '%'))
Jens Axboe57fc29f2010-06-23 22:24:07 +0200266 p = NULL;
267
Jens Axboe7bb59102011-07-12 19:47:03 +0200268 return __get_mult_bytes(p, data, percent);
Jens Axboe57fc29f2010-06-23 22:24:07 +0200269}
270
Stephen M. Cameron88b635b2014-09-29 12:10:49 -0600271extern int evaluate_arithmetic_expression(const char *buffer, long long *ival,
272 double *dval);
273
Jens Axboecb2c86f2006-10-26 13:48:34 +0200274/*
Yu-ju Hong83349192011-08-13 00:53:44 +0200275 * Convert string into a floating number. Return 1 for success and 0 otherwise.
276 */
Jens Axboee25839d2012-11-06 10:49:42 +0100277int str_to_float(const char *str, double *val)
Yu-ju Hong83349192011-08-13 00:53:44 +0200278{
Stephen M. Cameron88b635b2014-09-29 12:10:49 -0600279#ifdef CONFIG_ARITHMETIC
280 int rc;
281 long long ival;
282 double dval;
283
284 if (str[0] == '(') {
285 rc = evaluate_arithmetic_expression(str, &ival, &dval);
286 if (!rc) {
287 *val = dval;
288 return 1;
289 }
290 }
291#endif
292 return 1 == sscanf(str, "%lf", val);
Yu-ju Hong83349192011-08-13 00:53:44 +0200293}
294
295/*
Jens Axboee1f36502006-10-27 10:54:08 +0200296 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200297 */
Jens Axboe0de5b262014-02-21 15:26:01 -0800298int str_to_decimal(const char *str, long long *val, int kilo, void *data,
299 int is_seconds)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200300{
Jens Axboeb347f9d2009-03-09 14:15:21 +0100301 int len, base;
Stephen M. Cameron88b635b2014-09-29 12:10:49 -0600302 int rc = 1;
303#ifdef CONFIG_ARITHMETIC
304 long long ival;
305 double dval;
306#endif
Jens Axboecb2c86f2006-10-26 13:48:34 +0200307
Jens Axboecb2c86f2006-10-26 13:48:34 +0200308 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100309 if (!len)
310 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200311
Stephen M. Cameron88b635b2014-09-29 12:10:49 -0600312#ifdef CONFIG_ARITHMETIC
313 if (str[0] == '(')
314 rc = evaluate_arithmetic_expression(str, &ival, &dval);
315 if (str[0] == '(' && !rc)
316 *val = ival;
317#endif
Jens Axboeb347f9d2009-03-09 14:15:21 +0100318
Stephen M. Cameron88b635b2014-09-29 12:10:49 -0600319 if (rc == 1) {
320 if (strstr(str, "0x") || strstr(str, "0X"))
321 base = 16;
322 else
323 base = 10;
324
325 *val = strtoll(str, NULL, base);
326 if (*val == LONG_MAX && errno == ERANGE)
327 return 1;
328 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200329
Jens Axboe7bb59102011-07-12 19:47:03 +0200330 if (kilo) {
331 unsigned long long mult;
332 int perc = 0;
333
Jens Axboe6925dd32011-09-07 22:10:11 +0200334 mult = get_mult_bytes(str, len, data, &perc);
Jens Axboe7bb59102011-07-12 19:47:03 +0200335 if (perc)
336 *val = -1ULL - *val;
337 else
338 *val *= mult;
339 } else
Jens Axboe0de5b262014-02-21 15:26:01 -0800340 *val *= get_mult_time(str, len, is_seconds);
Jens Axboe787f7e92006-11-06 13:26:29 +0100341
Jens Axboecb2c86f2006-10-26 13:48:34 +0200342 return 0;
343}
344
Jens Axboe9af4a242012-03-16 10:13:49 +0100345int check_str_bytes(const char *p, long long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200346{
Jens Axboe0de5b262014-02-21 15:26:01 -0800347 return str_to_decimal(p, val, 1, data, 0);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200348}
349
Jens Axboe0de5b262014-02-21 15:26:01 -0800350int check_str_time(const char *p, long long *val, int is_seconds)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200351{
Jens Axboe0de5b262014-02-21 15:26:01 -0800352 return str_to_decimal(p, val, 0, NULL, is_seconds);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200353}
354
355void strip_blank_front(char **p)
356{
357 char *s = *p;
358
Jens Axboe4c8e9642011-10-15 14:37:38 +0200359 if (!strlen(s))
360 return;
Jens Axboe76cd9372011-07-08 21:14:57 +0200361 while (isspace((int) *s))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200362 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200363
364 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200365}
366
367void strip_blank_end(char *p)
368{
Jens Axboe853ee7f2009-03-06 20:29:29 +0100369 char *start = p, *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200370
Jens Axboe4c8e9642011-10-15 14:37:38 +0200371 if (!strlen(p))
372 return;
373
Jens Axboe523bfad2007-04-04 11:04:06 +0200374 s = strchr(p, ';');
375 if (s)
376 *s = '\0';
377 s = strchr(p, '#');
378 if (s)
379 *s = '\0';
380 if (s)
381 p = s;
382
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200383 s = p + strlen(p);
Jens Axboe76cd9372011-07-08 21:14:57 +0200384 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200385 s--;
386
387 *(s + 1) = '\0';
388}
389
Jens Axboed6978a32009-07-18 21:04:09 +0200390static int check_range_bytes(const char *str, long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200391{
Jens Axboe57fc29f2010-06-23 22:24:07 +0200392 long long __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200393
Jens Axboe0de5b262014-02-21 15:26:01 -0800394 if (!str_to_decimal(str, &__val, 1, data, 0)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200395 *val = __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200396 return 0;
397 }
398
Jens Axboecb2c86f2006-10-26 13:48:34 +0200399 return 1;
400}
401
Jens Axboe63f29372007-01-10 13:20:09 +0100402static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200403{
Jens Axboe787f7e92006-11-06 13:26:29 +0100404 if (!strlen(p))
405 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200406 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200407 if (sscanf(p, "%x", val) == 1)
408 return 0;
409 } else {
410 if (sscanf(p, "%u", val) == 1)
411 return 0;
412 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200413
414 return 1;
415}
416
Jens Axboe8adb4522014-09-23 10:38:54 -0600417static size_t opt_len(const char *str)
Jens Axboe808def72010-07-14 16:27:02 -0600418{
419 char *postfix;
420
421 postfix = strchr(str, ':');
422 if (!postfix)
423 return strlen(str);
424
425 return (int)(postfix - str);
426}
427
Jens Axboe119cd932012-12-06 17:34:57 +0100428static int str_match_len(const struct value_pair *vp, const char *str)
429{
430 return max(strlen(vp->ival), opt_len(str));
431}
432
Jens Axboef0fdbca2014-02-11 15:44:50 -0700433#define val_store(ptr, val, off, or, data, o) \
434 do { \
435 ptr = td_var((data), (o), (off)); \
436 if ((or)) \
437 *ptr |= (val); \
438 else \
439 *ptr = (val); \
Jens Axboe17abbe82006-11-06 11:23:10 +0100440 } while (0)
441
Jens Axboef90eff52006-11-06 11:08:21 +0100442static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Yu-ju Hong83349192011-08-13 00:53:44 +0200443 int first, int more, int curr)
Jens Axboee1f36502006-10-27 10:54:08 +0200444{
Erwan Velu2fdbefd2013-07-22 23:48:48 +0200445 int il=0, *ilp;
Vincent Kang Fufd112d32013-02-02 09:28:55 +0100446 fio_fp64_t *flp;
Jens Axboe63f29372007-01-10 13:20:09 +0100447 long long ull, *ullp;
448 long ul1, ul2;
Yu-ju Hong83349192011-08-13 00:53:44 +0200449 double uf;
Jens Axboebfe1d592012-11-29 21:33:03 +0100450 char **cp = NULL;
Jens Axboee42b01e2011-05-05 12:05:10 -0600451 int ret = 0, is_time = 0;
Jens Axboec44b1ff2011-08-30 20:43:53 -0600452 const struct value_pair *vp;
453 struct value_pair posval[PARSE_MAX_VP];
454 int i, all_skipped = 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200455
Jens Axboea3d741f2008-02-27 18:32:33 +0100456 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
457 o->type, ptr);
458
Jens Axboee3cedca2008-11-19 19:57:52 +0100459 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200460 log_err("Option %s requires an argument\n", o->name);
Jens Axboe08e26e32006-11-21 13:15:10 +0100461 return 1;
462 }
463
Jens Axboee1f36502006-10-27 10:54:08 +0200464 switch (o->type) {
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100465 case FIO_OPT_STR:
466 case FIO_OPT_STR_MULTI: {
Jens Axboee1f36502006-10-27 10:54:08 +0200467 fio_opt_str_fn *fn = o->cb;
468
Jens Axboef0857372007-03-19 13:15:23 +0100469 posval_sort(o, posval);
470
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100471 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100472 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100473 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100474 if (!vp->ival || vp->ival[0] == '\0')
Jens Axboea3073f42010-03-05 10:06:15 +0100475 continue;
Jens Axboeae2ddba2010-03-10 12:49:03 +0100476 all_skipped = 0;
Jens Axboe119cd932012-12-06 17:34:57 +0100477 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100478 ret = 0;
Jens Axboe7b504ed2014-02-11 14:19:38 -0700479 if (o->off1)
Daniel Gollubebadc0c2014-02-12 08:24:57 -0700480 val_store(ilp, vp->oval, o->off1, vp->orval, data, o);
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100481 continue;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100482 }
483 }
484
Jens Axboeae2ddba2010-03-10 12:49:03 +0100485 if (ret && !all_skipped)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100486 show_option_values(o);
487 else if (fn)
488 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200489 break;
490 }
Jens Axboee42b01e2011-05-05 12:05:10 -0600491 case FIO_OPT_STR_VAL_TIME:
492 is_time = 1;
Jens Axboef7fa2652009-03-09 14:20:20 +0100493 case FIO_OPT_INT:
Jens Axboee42b01e2011-05-05 12:05:10 -0600494 case FIO_OPT_STR_VAL: {
495 fio_opt_str_val_fn *fn = o->cb;
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200496 char tmp[128], *p;
497
498 strncpy(tmp, ptr, sizeof(tmp) - 1);
499 p = strchr(tmp, ',');
500 if (p)
501 *p = '\0';
Jens Axboee1f36502006-10-27 10:54:08 +0200502
Jens Axboee42b01e2011-05-05 12:05:10 -0600503 if (is_time)
Jens Axboe0de5b262014-02-21 15:26:01 -0800504 ret = check_str_time(tmp, &ull, o->is_seconds);
Jens Axboee42b01e2011-05-05 12:05:10 -0600505 else
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200506 ret = check_str_bytes(tmp, &ull, data);
Jens Axboee42b01e2011-05-05 12:05:10 -0600507
Jens Axboeae3fcb52013-04-25 10:06:32 -0600508 dprint(FD_PARSE, " ret=%d, out=%llu\n", ret, ull);
509
Jens Axboee1f36502006-10-27 10:54:08 +0200510 if (ret)
511 break;
512
Jens Axboedb8e0162007-01-10 13:41:26 +0100513 if (o->maxval && ull > o->maxval) {
Jens Axboed4fc2f02012-09-24 14:32:26 +0200514 log_err("max value out of range: %llu"
515 " (%u max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100516 return 1;
517 }
518 if (o->minval && ull < o->minval) {
Jens Axboed4fc2f02012-09-24 14:32:26 +0200519 log_err("min value out of range: %llu"
520 " (%u min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100521 return 1;
522 }
Jens Axboed926d532013-04-09 21:02:15 +0200523 if (o->posval[0].ival) {
524 posval_sort(o, posval);
525
526 ret = 1;
527 for (i = 0; i < PARSE_MAX_VP; i++) {
528 vp = &posval[i];
529 if (!vp->ival || vp->ival[0] == '\0')
530 continue;
531 if (vp->oval == ull) {
532 ret = 0;
533 break;
534 }
535 }
536 if (ret) {
Jens Axboe128e4ca2013-04-10 11:06:34 +0200537 log_err("fio: value %llu not allowed:\n", ull);
538 show_option_values(o);
Jens Axboed926d532013-04-09 21:02:15 +0200539 return 1;
540 }
541 }
Jens Axboee1f36502006-10-27 10:54:08 +0200542
543 if (fn)
544 ret = fn(data, &ull);
545 else {
Jens Axboee01b22b2009-06-09 15:43:25 +0200546 if (o->type == FIO_OPT_INT) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700547 if (first)
548 val_store(ilp, ull, o->off1, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200549 if (curr == 1) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700550 if (o->off2)
551 val_store(ilp, ull, o->off2, 0, data, o);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100552 }
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200553 if (curr == 2) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700554 if (o->off3)
555 val_store(ilp, ull, o->off3, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200556 }
557 if (!more) {
558 if (curr < 1) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700559 if (o->off2)
560 val_store(ilp, ull, o->off2, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200561 }
562 if (curr < 2) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700563 if (o->off3)
564 val_store(ilp, ull, o->off3, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200565 }
566 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100567 } else {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700568 if (first)
569 val_store(ullp, ull, o->off1, 0, data, o);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100570 if (!more) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700571 if (o->off2)
572 val_store(ullp, ull, o->off2, 0, data, o);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100573 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100574 }
Jens Axboee1f36502006-10-27 10:54:08 +0200575 }
576 break;
577 }
Yu-ju Hong83349192011-08-13 00:53:44 +0200578 case FIO_OPT_FLOAT_LIST: {
Jens Axboeeef02442013-02-06 08:51:57 +0100579 char *cp2;
580
Vincent Kang Fu435d1952013-02-06 08:43:40 +0100581 if (first) {
582 /*
583 ** Initialize precision to 0 and zero out list
584 ** in case specified list is shorter than default
585 */
Jens Axboe3e260a42013-12-09 12:38:53 -0700586 if (o->off2) {
587 ul2 = 0;
Jens Axboef0fdbca2014-02-11 15:44:50 -0700588 ilp = td_var(data, o, o->off2);
Jens Axboe3e260a42013-12-09 12:38:53 -0700589 *ilp = ul2;
590 }
Vincent Kang Fu435d1952013-02-06 08:43:40 +0100591
Jens Axboef0fdbca2014-02-11 15:44:50 -0700592 flp = td_var(data, o, o->off1);
Vincent Kang Fu435d1952013-02-06 08:43:40 +0100593 for(i = 0; i < o->maxlen; i++)
594 flp[i].u.f = 0.0;
595 }
Yu-ju Hong83349192011-08-13 00:53:44 +0200596 if (curr >= o->maxlen) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200597 log_err("the list exceeding max length %d\n",
Yu-ju Hong83349192011-08-13 00:53:44 +0200598 o->maxlen);
599 return 1;
600 }
Jens Axboe3c3ed072012-03-27 09:12:39 +0200601 if (!str_to_float(ptr, &uf)) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200602 log_err("not a floating point value: %s\n", ptr);
Yu-ju Hong83349192011-08-13 00:53:44 +0200603 return 1;
604 }
Jens Axboe26650692013-02-02 09:56:23 +0100605 if (uf > o->maxfp) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200606 log_err("value out of range: %f"
Yu-ju Hong83349192011-08-13 00:53:44 +0200607 " (range max: %f)\n", uf, o->maxfp);
608 return 1;
609 }
Jens Axboe26650692013-02-02 09:56:23 +0100610 if (uf < o->minfp) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200611 log_err("value out of range: %f"
Yu-ju Hong83349192011-08-13 00:53:44 +0200612 " (range min: %f)\n", uf, o->minfp);
613 return 1;
614 }
615
Jens Axboef0fdbca2014-02-11 15:44:50 -0700616 flp = td_var(data, o, o->off1);
Vincent Kang Fufd112d32013-02-02 09:28:55 +0100617 flp[curr].u.f = uf;
Yu-ju Hong83349192011-08-13 00:53:44 +0200618
Jens Axboeae3fcb52013-04-25 10:06:32 -0600619 dprint(FD_PARSE, " out=%f\n", uf);
620
Vincent Kang Fu435d1952013-02-06 08:43:40 +0100621 /*
622 ** Calculate precision for output by counting
623 ** number of digits after period. Find first
624 ** period in entire remaining list each time
625 */
626 cp2 = strchr(ptr, '.');
627 if (cp2 != NULL) {
Jens Axboeeef02442013-02-06 08:51:57 +0100628 int len = 0;
Vincent Kang Fu435d1952013-02-06 08:43:40 +0100629
630 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
631 len++;
632
Jens Axboe3e260a42013-12-09 12:38:53 -0700633 if (o->off2) {
Jens Axboef0fdbca2014-02-11 15:44:50 -0700634 ilp = td_var(data, o, o->off2);
Jens Axboe3e260a42013-12-09 12:38:53 -0700635 if (len > *ilp)
636 *ilp = len;
637 }
Vincent Kang Fu435d1952013-02-06 08:43:40 +0100638 }
639
Yu-ju Hong83349192011-08-13 00:53:44 +0200640 break;
641 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100642 case FIO_OPT_STR_STORE: {
643 fio_opt_str_fn *fn = o->cb;
644
Jens Axboef75c69a2014-04-03 08:18:07 -0600645 if (!strlen(ptr))
646 return 1;
647
Jens Axboe7b504ed2014-02-11 14:19:38 -0700648 if (o->off1) {
Jens Axboef0fdbca2014-02-11 15:44:50 -0700649 cp = td_var(data, o, o->off1);
Steven Lang184b4092011-11-16 10:33:51 +0100650 *cp = strdup(ptr);
Jens Axboe1c964ce2011-08-31 16:45:03 -0600651 }
Jens Axboe02c6aad2010-03-04 13:49:11 +0100652
Steven Lang184b4092011-11-16 10:33:51 +0100653 if (fn)
654 ret = fn(data, ptr);
655 else if (o->posval[0].ival) {
656 posval_sort(o, posval);
Jens Axboec44b1ff2011-08-30 20:43:53 -0600657
Steven Lang184b4092011-11-16 10:33:51 +0100658 ret = 1;
659 for (i = 0; i < PARSE_MAX_VP; i++) {
660 vp = &posval[i];
Jens Axboeab508172014-04-14 13:16:10 -0600661 if (!vp->ival || vp->ival[0] == '\0' || !cp)
Steven Lang184b4092011-11-16 10:33:51 +0100662 continue;
663 all_skipped = 0;
Jens Axboe119cd932012-12-06 17:34:57 +0100664 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
Steven Lang184b4092011-11-16 10:33:51 +0100665 char *rest;
666
667 ret = 0;
668 if (vp->cb)
669 fn = vp->cb;
670 rest = strstr(*cp ?: ptr, ":");
671 if (rest) {
672 if (*cp)
673 *rest = '\0';
674 ptr = rest + 1;
675 } else
676 ptr = NULL;
677 break;
678 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100679 }
680 }
Jens Axboec44b1ff2011-08-30 20:43:53 -0600681
Steven Lang184b4092011-11-16 10:33:51 +0100682 if (!all_skipped) {
683 if (ret && !*cp)
684 show_option_values(o);
685 else if (ret && *cp)
686 ret = 0;
687 else if (fn && ptr)
688 ret = fn(data, ptr);
689 }
Jens Axboec44b1ff2011-08-30 20:43:53 -0600690
Jens Axboee1f36502006-10-27 10:54:08 +0200691 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100692 }
Jens Axboee1f36502006-10-27 10:54:08 +0200693 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200694 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200695 char *p1, *p2;
696
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100697 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200698
Dave Engberg31d23f42011-07-23 21:07:13 +0200699 /* Handle bsrange with separate read,write values: */
700 p1 = strchr(tmp, ',');
701 if (p1)
702 *p1 = '\0';
703
Jens Axboeb765a372006-10-27 15:00:16 +0200704 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200705 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100706 p1 = strchr(tmp, ':');
707 if (!p1) {
708 ret = 1;
709 break;
710 }
Jens Axboee1f36502006-10-27 10:54:08 +0200711 }
712
713 p2 = p1 + 1;
714 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200715 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200716
717 ret = 1;
Jens Axboed6978a32009-07-18 21:04:09 +0200718 if (!check_range_bytes(p1, &ul1, data) &&
719 !check_range_bytes(p2, &ul2, data)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200720 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200721 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100722 unsigned long foo = ul1;
723
724 ul1 = ul2;
725 ul2 = foo;
726 }
727
728 if (first) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700729 val_store(ilp, ul1, o->off1, 0, data, o);
730 val_store(ilp, ul2, o->off2, 0, data, o);
Jens Axboee1f36502006-10-27 10:54:08 +0200731 }
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200732 if (curr == 1) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700733 if (o->off3 && o->off4) {
734 val_store(ilp, ul1, o->off3, 0, data, o);
735 val_store(ilp, ul2, o->off4, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200736 }
737 }
738 if (curr == 2) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700739 if (o->off5 && o->off6) {
740 val_store(ilp, ul1, o->off5, 0, data, o);
741 val_store(ilp, ul2, o->off6, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200742 }
743 }
744 if (!more) {
745 if (curr < 1) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700746 if (o->off3 && o->off4) {
747 val_store(ilp, ul1, o->off3, 0, data, o);
748 val_store(ilp, ul2, o->off4, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200749 }
750 }
751 if (curr < 2) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700752 if (o->off5 && o->off6) {
753 val_store(ilp, ul1, o->off5, 0, data, o);
754 val_store(ilp, ul2, o->off6, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200755 }
756 }
Jens Axboe17abbe82006-11-06 11:23:10 +0100757 }
758 }
759
Jens Axboee1f36502006-10-27 10:54:08 +0200760 break;
761 }
Jens Axboe74ba1802011-07-15 09:09:15 +0200762 case FIO_OPT_BOOL:
763 case FIO_OPT_STR_SET: {
Jens Axboee1f36502006-10-27 10:54:08 +0200764 fio_opt_int_fn *fn = o->cb;
765
Jens Axboe74ba1802011-07-15 09:09:15 +0200766 if (ptr)
767 ret = check_int(ptr, &il);
768 else if (o->type == FIO_OPT_BOOL)
769 ret = 1;
770 else
771 il = 1;
772
Jens Axboeae3fcb52013-04-25 10:06:32 -0600773 dprint(FD_PARSE, " ret=%d, out=%d\n", ret, il);
774
Jens Axboee1f36502006-10-27 10:54:08 +0200775 if (ret)
776 break;
777
Jens Axboedb8e0162007-01-10 13:41:26 +0100778 if (o->maxval && il > (int) o->maxval) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200779 log_err("max value out of range: %d (%d max)\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100780 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100781 return 1;
782 }
783 if (o->minval && il < o->minval) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200784 log_err("min value out of range: %d (%d min)\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100785 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100786 return 1;
787 }
Jens Axboee1f36502006-10-27 10:54:08 +0200788
Jens Axboe76a43db2007-01-11 13:24:44 +0100789 if (o->neg)
790 il = !il;
791
Jens Axboee1f36502006-10-27 10:54:08 +0200792 if (fn)
793 ret = fn(data, &il);
794 else {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700795 if (first)
796 val_store(ilp, il, o->off1, 0, data, o);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100797 if (!more) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700798 if (o->off2)
799 val_store(ilp, il, o->off2, 0, data, o);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100800 }
Jens Axboee1f36502006-10-27 10:54:08 +0200801 }
802 break;
803 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200804 case FIO_OPT_DEPRECATED:
Jens Axboe06b0be62011-10-04 10:31:10 +0200805 log_info("Option %s is deprecated\n", o->name);
Jens Axboed9472272013-07-25 10:20:45 -0600806 ret = 1;
Jens Axboe15ca1502008-04-07 09:26:02 +0200807 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200808 default:
Jens Axboe06b0be62011-10-04 10:31:10 +0200809 log_err("Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200810 ret = 1;
811 }
812
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200813 if (ret)
814 return ret;
815
Jens Axboed447a8c2009-07-17 22:26:15 +0200816 if (o->verify) {
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200817 ret = o->verify(o, data);
Jens Axboed447a8c2009-07-17 22:26:15 +0200818 if (ret) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200819 log_err("Correct format for offending option\n");
820 log_err("%20s: %s\n", o->name, o->help);
Jens Axboe06b0be62011-10-04 10:31:10 +0200821 show_option_help(o, 1);
Jens Axboed447a8c2009-07-17 22:26:15 +0200822 }
823 }
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200824
Jens Axboee1f36502006-10-27 10:54:08 +0200825 return ret;
826}
827
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200828static int handle_option(struct fio_option *o, const char *__ptr, void *data)
Jens Axboef90eff52006-11-06 11:08:21 +0100829{
Jens Axboeb62bdf22010-03-10 12:36:17 +0100830 char *o_ptr, *ptr, *ptr2;
831 int ret, done;
Jens Axboef90eff52006-11-06 11:08:21 +0100832
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200833 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
834
Jens Axboeb62bdf22010-03-10 12:36:17 +0100835 o_ptr = ptr = NULL;
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200836 if (__ptr)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100837 o_ptr = ptr = strdup(__ptr);
Jens Axboea3d741f2008-02-27 18:32:33 +0100838
Jens Axboef90eff52006-11-06 11:08:21 +0100839 /*
Jens Axboeb62bdf22010-03-10 12:36:17 +0100840 * See if we have another set of parameters, hidden after a comma.
841 * Do this before parsing this round, to check if we should
Jens Axboe787f7e92006-11-06 13:26:29 +0100842 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100843 */
Jens Axboeb62bdf22010-03-10 12:36:17 +0100844 done = 0;
845 ret = 1;
846 do {
847 int __ret;
848
849 ptr2 = NULL;
850 if (ptr &&
851 (o->type != FIO_OPT_STR_STORE) &&
Yu-ju Hong83349192011-08-13 00:53:44 +0200852 (o->type != FIO_OPT_STR) &&
853 (o->type != FIO_OPT_FLOAT_LIST)) {
Jens Axboeb62bdf22010-03-10 12:36:17 +0100854 ptr2 = strchr(ptr, ',');
855 if (ptr2 && *(ptr2 + 1) == '\0')
856 *ptr2 = '\0';
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200857 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
Jens Axboeb62bdf22010-03-10 12:36:17 +0100858 if (!ptr2)
859 ptr2 = strchr(ptr, ':');
860 if (!ptr2)
861 ptr2 = strchr(ptr, '-');
862 }
Yu-ju Hong83349192011-08-13 00:53:44 +0200863 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
864 ptr2 = strchr(ptr, ':');
Jens Axboeb62bdf22010-03-10 12:36:17 +0100865 }
866
867 /*
868 * Don't return early if parsing the first option fails - if
869 * we are doing multiple arguments, we can allow the first one
870 * being empty.
871 */
Yu-ju Hong83349192011-08-13 00:53:44 +0200872 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
Jens Axboeb62bdf22010-03-10 12:36:17 +0100873 if (ret)
874 ret = __ret;
875
Jens Axboe0c9baf92007-01-11 15:59:26 +0100876 if (!ptr2)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100877 break;
Jens Axboe787f7e92006-11-06 13:26:29 +0100878
Jens Axboeb62bdf22010-03-10 12:36:17 +0100879 ptr = ptr2 + 1;
880 done++;
881 } while (1);
Jens Axboe787f7e92006-11-06 13:26:29 +0100882
Jens Axboeb62bdf22010-03-10 12:36:17 +0100883 if (o_ptr)
884 free(o_ptr);
885 return ret;
Jens Axboef90eff52006-11-06 11:08:21 +0100886}
887
Steven Langde890a12011-11-09 14:03:34 +0100888static struct fio_option *get_option(char *opt,
Jens Axboe07b32322010-03-05 09:48:44 +0100889 struct fio_option *options, char **post)
Jens Axboe3b8b7132008-06-10 19:46:23 +0200890{
891 struct fio_option *o;
892 char *ret;
893
894 ret = strchr(opt, '=');
895 if (ret) {
896 *post = ret;
897 *ret = '\0';
Steven Langde890a12011-11-09 14:03:34 +0100898 ret = opt;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200899 (*post)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100900 strip_blank_end(ret);
Jens Axboe07b32322010-03-05 09:48:44 +0100901 o = find_option(options, ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200902 } else {
Jens Axboe07b32322010-03-05 09:48:44 +0100903 o = find_option(options, opt);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200904 *post = NULL;
905 }
906
907 return o;
908}
909
910static int opt_cmp(const void *p1, const void *p2)
911{
Steven Langde890a12011-11-09 14:03:34 +0100912 struct fio_option *o;
913 char *s, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100914 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200915
Jens Axboe8cdabc12008-11-19 12:31:43 +0100916 prio1 = prio2 = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200917
Steven Langde890a12011-11-09 14:03:34 +0100918 if (*(char **)p1) {
919 s = strdup(*((char **) p1));
Jens Axboe9af4a242012-03-16 10:13:49 +0100920 o = get_option(s, __fio_options, &foo);
Steven Langde890a12011-11-09 14:03:34 +0100921 if (o)
922 prio1 = o->prio;
923 free(s);
924 }
925 if (*(char **)p2) {
926 s = strdup(*((char **) p2));
Jens Axboe9af4a242012-03-16 10:13:49 +0100927 o = get_option(s, __fio_options, &foo);
Steven Langde890a12011-11-09 14:03:34 +0100928 if (o)
929 prio2 = o->prio;
930 free(s);
931 }
932
Jens Axboe8cdabc12008-11-19 12:31:43 +0100933 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200934}
935
936void sort_options(char **opts, struct fio_option *options, int num_opts)
937{
Jens Axboe9af4a242012-03-16 10:13:49 +0100938 __fio_options = options;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200939 qsort(opts, num_opts, sizeof(char *), opt_cmp);
Jens Axboe9af4a242012-03-16 10:13:49 +0100940 __fio_options = NULL;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200941}
942
Jens Axboeb4692822006-10-27 13:43:22 +0200943int parse_cmd_option(const char *opt, const char *val,
Jens Axboe07b32322010-03-05 09:48:44 +0100944 struct fio_option *options, void *data)
Jens Axboeb4692822006-10-27 13:43:22 +0200945{
946 struct fio_option *o;
947
Jens Axboe07b32322010-03-05 09:48:44 +0100948 o = find_option(options, opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200949 if (!o) {
Jens Axboe06b0be62011-10-04 10:31:10 +0200950 log_err("Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200951 return 1;
952 }
953
Jens Axboeb1508cf2006-11-02 09:12:40 +0100954 if (!handle_option(o, val, data))
955 return 0;
956
Jens Axboe06b0be62011-10-04 10:31:10 +0200957 log_err("fio: failed parsing %s=%s\n", opt, val);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100958 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200959}
960
Steven Langde890a12011-11-09 14:03:34 +0100961int parse_option(char *opt, const char *input,
Jens Axboe292cc472013-11-26 20:39:35 -0700962 struct fio_option *options, struct fio_option **o, void *data,
963 int dump_cmdline)
Jens Axboee1f36502006-10-27 10:54:08 +0200964{
Steven Langd0c814e2011-10-28 08:37:13 +0200965 char *post;
Jens Axboee1f36502006-10-27 10:54:08 +0200966
Steven Langd0c814e2011-10-28 08:37:13 +0200967 if (!opt) {
968 log_err("fio: failed parsing %s\n", input);
Steven Langde890a12011-11-09 14:03:34 +0100969 *o = NULL;
Jens Axboe38789b52010-03-03 09:23:20 +0100970 return 1;
Steven Langd0c814e2011-10-28 08:37:13 +0200971 }
Jens Axboee1f36502006-10-27 10:54:08 +0200972
Steven Langde890a12011-11-09 14:03:34 +0100973 *o = get_option(opt, options, &post);
974 if (!*o) {
975 if (post) {
976 int len = strlen(opt);
977 if (opt + len + 1 != post)
978 memmove(opt + len + 1, post, strlen(post));
979 opt[len] = '=';
980 }
Jens Axboee1f36502006-10-27 10:54:08 +0200981 return 1;
982 }
983
Jens Axboe292cc472013-11-26 20:39:35 -0700984 if (handle_option(*o, post, data)) {
985 log_err("fio: failed parsing %s\n", input);
986 return 1;
987 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100988
Jens Axboe292cc472013-11-26 20:39:35 -0700989 if (dump_cmdline) {
990 const char *delim;
991
992 if (!strcmp("description", (*o)->name))
993 delim = "\"";
994 else
995 delim = "";
996
997 log_info("--%s%s", (*o)->name, post ? "" : " ");
998 if (post)
999 log_info("=%s%s%s ", delim, post, delim);
1000 }
1001
1002 return 0;
Jens Axboee1f36502006-10-27 10:54:08 +02001003}
Jens Axboefd28ca42007-01-09 21:20:13 +01001004
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001005/*
1006 * Option match, levenshtein distance. Handy for not quite remembering what
1007 * the option name is.
1008 */
1009static int string_distance(const char *s1, const char *s2)
1010{
1011 unsigned int s1_len = strlen(s1);
1012 unsigned int s2_len = strlen(s2);
1013 unsigned int *p, *q, *r;
1014 unsigned int i, j;
1015
1016 p = malloc(sizeof(unsigned int) * (s2_len + 1));
1017 q = malloc(sizeof(unsigned int) * (s2_len + 1));
1018
1019 p[0] = 0;
1020 for (i = 1; i <= s2_len; i++)
1021 p[i] = p[i - 1] + 1;
1022
1023 for (i = 1; i <= s1_len; i++) {
1024 q[0] = p[0] + 1;
1025 for (j = 1; j <= s2_len; j++) {
1026 unsigned int sub = p[j - 1];
1027
1028 if (s1[i - 1] != s2[j - 1])
1029 sub++;
1030
1031 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
1032 }
1033 r = p;
1034 p = q;
1035 q = r;
1036 }
1037
1038 i = p[s2_len];
1039 free(p);
1040 free(q);
1041 return i;
1042}
1043
Jens Axboeafdf9352007-07-31 16:14:34 +02001044static struct fio_option *find_child(struct fio_option *options,
1045 struct fio_option *o)
1046{
1047 struct fio_option *__o;
1048
Jens Axboefdf28742007-07-31 23:06:09 +02001049 for (__o = options + 1; __o->name; __o++)
1050 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +02001051 return __o;
1052
1053 return NULL;
1054}
1055
Jens Axboe323d9112008-03-01 15:12:14 +01001056static void __print_option(struct fio_option *o, struct fio_option *org,
1057 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +02001058{
1059 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +01001060 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +02001061
1062 if (!o)
1063 return;
Jens Axboeef9aff52007-07-31 22:56:53 +02001064 if (!org)
1065 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001066
Jens Axboeafdf9352007-07-31 16:14:34 +02001067 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +01001068 depth = level;
1069 while (depth--)
1070 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +02001071
1072 sprintf(p, "%s", o->name);
1073
Jens Axboe28d7c362011-10-05 20:30:24 +02001074 log_info("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +01001075}
1076
1077static void print_option(struct fio_option *o)
1078{
1079 struct fio_option *parent;
1080 struct fio_option *__o;
1081 unsigned int printed;
1082 unsigned int level;
1083
1084 __print_option(o, NULL, 0);
1085 parent = o;
1086 level = 0;
1087 do {
1088 level++;
1089 printed = 0;
1090
1091 while ((__o = find_child(o, parent)) != NULL) {
1092 __print_option(__o, o, level);
1093 o = __o;
1094 printed++;
1095 }
1096
1097 parent = o;
1098 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +02001099}
1100
Jens Axboe07b32322010-03-05 09:48:44 +01001101int show_cmd_help(struct fio_option *options, const char *name)
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001102{
1103 struct fio_option *o, *closest;
Jens Axboed091d092010-04-01 19:56:23 +02001104 unsigned int best_dist = -1U;
Jens Axboe29fc6af2007-01-09 21:22:02 +01001105 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +01001106 int show_all = 0;
1107
1108 if (!name || !strcmp(name, "all"))
1109 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +01001110
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001111 closest = NULL;
1112 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +01001113 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +01001114 int match = 0;
1115
Jens Axboe15ca1502008-04-07 09:26:02 +02001116 if (o->type == FIO_OPT_DEPRECATED)
1117 continue;
Jens Axboe07b32322010-03-05 09:48:44 +01001118 if (!exec_profile && o->prof_name)
1119 continue;
Jens Axboe4ac23d22013-05-23 21:14:03 +02001120 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1121 continue;
Jens Axboe15ca1502008-04-07 09:26:02 +02001122
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001123 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +01001124 if (!strcmp(name, o->name) ||
1125 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001126 match = 1;
1127 else {
1128 unsigned int dist;
1129
1130 dist = string_distance(name, o->name);
1131 if (dist < best_dist) {
1132 best_dist = dist;
1133 closest = o;
1134 }
1135 }
1136 }
Jens Axboefd28ca42007-01-09 21:20:13 +01001137
1138 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +01001139 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +01001140 if (match)
Jens Axboe28d7c362011-10-05 20:30:24 +02001141 log_info("%20s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +01001142 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +02001143 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +01001144 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +01001145 continue;
Jens Axboec167ded2007-03-14 13:02:53 +01001146 }
Jens Axboefd28ca42007-01-09 21:20:13 +01001147 }
1148
Jens Axboe70df2f12007-01-11 14:04:47 +01001149 if (!match)
1150 continue;
1151
Jens Axboe06b0be62011-10-04 10:31:10 +02001152 show_option_help(o, 0);
Jens Axboefd28ca42007-01-09 21:20:13 +01001153 }
1154
Jens Axboe29fc6af2007-01-09 21:22:02 +01001155 if (found)
1156 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +01001157
Jens Axboe28d7c362011-10-05 20:30:24 +02001158 log_err("No such command: %s", name);
Jens Axboed091d092010-04-01 19:56:23 +02001159
1160 /*
1161 * Only print an appropriately close option, one where the edit
1162 * distance isn't too big. Otherwise we get crazy matches.
1163 */
1164 if (closest && best_dist < 3) {
Jens Axboe28d7c362011-10-05 20:30:24 +02001165 log_info(" - showing closest match\n");
1166 log_info("%20s: %s\n", closest->name, closest->help);
Jens Axboe06b0be62011-10-04 10:31:10 +02001167 show_option_help(closest, 0);
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001168 } else
Jens Axboe28d7c362011-10-05 20:30:24 +02001169 log_info("\n");
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001170
Jens Axboe29fc6af2007-01-09 21:22:02 +01001171 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +01001172}
Jens Axboeee738492007-01-10 11:23:16 +01001173
Jens Axboe13335dd2007-01-10 13:14:02 +01001174/*
1175 * Handle parsing of default parameters.
1176 */
Jens Axboeee738492007-01-10 11:23:16 +01001177void fill_default_options(void *data, struct fio_option *options)
1178{
Jens Axboe4945ba12007-01-11 14:36:56 +01001179 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +01001180
Jens Axboea3d741f2008-02-27 18:32:33 +01001181 dprint(FD_PARSE, "filling default options\n");
1182
Jens Axboe4945ba12007-01-11 14:36:56 +01001183 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +01001184 if (o->def)
1185 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +01001186}
Jens Axboe13335dd2007-01-10 13:14:02 +01001187
Jens Axboe9f988e22010-03-04 10:42:38 +01001188void option_init(struct fio_option *o)
1189{
1190 if (o->type == FIO_OPT_DEPRECATED)
1191 return;
1192 if (o->type == FIO_OPT_BOOL) {
1193 o->minval = 0;
1194 o->maxval = 1;
1195 }
Jens Axboed4fc2f02012-09-24 14:32:26 +02001196 if (o->type == FIO_OPT_INT) {
1197 if (!o->maxval)
1198 o->maxval = UINT_MAX;
1199 }
Yu-ju Hong83349192011-08-13 00:53:44 +02001200 if (o->type == FIO_OPT_FLOAT_LIST) {
Bruce Cranab9461e2013-02-02 14:31:24 +00001201 o->minfp = DBL_MIN;
1202 o->maxfp = DBL_MAX;
Yu-ju Hong83349192011-08-13 00:53:44 +02001203 }
Jens Axboeef7035a2014-06-18 15:30:09 -07001204 if (o->type == FIO_OPT_STR_SET && o->def && !o->no_warn_def) {
Jens Axboe06b0be62011-10-04 10:31:10 +02001205 log_err("Option %s: string set option with"
Jens Axboe9f988e22010-03-04 10:42:38 +01001206 " default will always be true\n", o->name);
1207 }
Jens Axboe7b504ed2014-02-11 14:19:38 -07001208 if (!o->cb && !o->off1)
Jens Axboe06b0be62011-10-04 10:31:10 +02001209 log_err("Option %s: neither cb nor offset given\n", o->name);
Jens Axboe22754742013-04-10 13:05:23 +02001210 if (!o->category) {
Jens Axboecca5b5b2013-04-10 19:35:25 +02001211 log_info("Option %s: no category defined. Setting to misc\n", o->name);
Jens Axboe22754742013-04-10 13:05:23 +02001212 o->category = FIO_OPT_C_GENERAL;
Jens Axboeffd78212013-04-10 13:07:43 +02001213 o->group = FIO_OPT_G_INVALID;
Jens Axboe22754742013-04-10 13:05:23 +02001214 }
Jens Axboe5f6ddf12010-03-09 20:40:44 +01001215 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1216 o->type == FIO_OPT_STR_MULTI)
Jens Axboe9f988e22010-03-04 10:42:38 +01001217 return;
Jens Axboe7b504ed2014-02-11 14:19:38 -07001218 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
Jens Axboe06b0be62011-10-04 10:31:10 +02001219 log_err("Option %s: both cb and offset given\n", o->name);
Jens Axboe9f988e22010-03-04 10:42:38 +01001220}
1221
Jens Axboe13335dd2007-01-10 13:14:02 +01001222/*
1223 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +01001224 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +01001225 */
1226void options_init(struct fio_option *options)
1227{
Jens Axboe4945ba12007-01-11 14:36:56 +01001228 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +01001229
Jens Axboea3d741f2008-02-27 18:32:33 +01001230 dprint(FD_PARSE, "init options\n");
1231
Jens Axboe90265352012-03-19 20:29:44 +01001232 for (o = &options[0]; o->name; o++) {
Jens Axboe9f988e22010-03-04 10:42:38 +01001233 option_init(o);
Jens Axboe90265352012-03-19 20:29:44 +01001234 if (o->inverse)
1235 o->inv_opt = find_option(options, o->inverse);
1236 }
Jens Axboe13335dd2007-01-10 13:14:02 +01001237}
Jens Axboe7e356b22011-10-14 10:55:16 +02001238
1239void options_free(struct fio_option *options, void *data)
1240{
1241 struct fio_option *o;
1242 char **ptr;
1243
1244 dprint(FD_PARSE, "free options\n");
1245
1246 for (o = &options[0]; o->name; o++) {
Steven Langde890a12011-11-09 14:03:34 +01001247 if (o->type != FIO_OPT_STR_STORE || !o->off1)
Jens Axboe7e356b22011-10-14 10:55:16 +02001248 continue;
1249
Jens Axboef0fdbca2014-02-11 15:44:50 -07001250 ptr = td_var(data, o, o->off1);
Jens Axboe7e356b22011-10-14 10:55:16 +02001251 if (*ptr) {
1252 free(*ptr);
1253 *ptr = NULL;
1254 }
1255 }
1256}