blob: 5b8e10f9f2e936ef55817c2d44b291e7c868e9f4 [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 Axboe3b8b7132008-06-10 19:46:23 +020021static struct fio_option *fio_options;
Jens Axboed6978a32009-07-18 21:04:09 +020022extern unsigned int fio_get_kb_base(void *);
Jens Axboe3b8b7132008-06-10 19:46:23 +020023
Jens Axboef0857372007-03-19 13:15:23 +010024static int vp_cmp(const void *p1, const void *p2)
25{
26 const struct value_pair *vp1 = p1;
27 const struct value_pair *vp2 = p2;
28
29 return strlen(vp2->ival) - strlen(vp1->ival);
30}
31
Jens Axboece952ab2011-08-31 14:29:22 -060032static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
Jens Axboef0857372007-03-19 13:15:23 +010033{
34 const struct value_pair *vp;
35 int entries;
36
37 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
38
39 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
40 vp = &o->posval[entries];
41 if (!vp->ival || vp->ival[0] == '\0')
42 break;
43
44 memcpy(&vpmap[entries], vp, sizeof(*vp));
45 }
46
47 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
48}
49
Jens Axboe06b0be62011-10-04 10:31:10 +020050static void show_option_range(struct fio_option *o,
51 int (*logger)(const char *format, ...))
Jens Axboeb1ec1da2007-02-23 10:29:16 +010052{
Yu-ju Hong83349192011-08-13 00:53:44 +020053 if (o->type == FIO_OPT_FLOAT_LIST){
Bruce Cranab9461e2013-02-02 14:31:24 +000054 if (o->minfp == DBL_MIN && o->maxfp == DBL_MAX)
Yu-ju Hong83349192011-08-13 00:53:44 +020055 return;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010056
Jens Axboe06b0be62011-10-04 10:31:10 +020057 logger("%20s: min=%f", "range", o->minfp);
Bruce Cranab9461e2013-02-02 14:31:24 +000058 if (o->maxfp != DBL_MAX)
Jens Axboe06b0be62011-10-04 10:31:10 +020059 logger(", max=%f", o->maxfp);
60 logger("\n");
Yu-ju Hong83349192011-08-13 00:53:44 +020061 } else {
62 if (!o->minval && !o->maxval)
63 return;
64
Jens Axboe06b0be62011-10-04 10:31:10 +020065 logger("%20s: min=%d", "range", o->minval);
Yu-ju Hong83349192011-08-13 00:53:44 +020066 if (o->maxval)
Jens Axboe06b0be62011-10-04 10:31:10 +020067 logger(", max=%d", o->maxval);
68 logger("\n");
Yu-ju Hong83349192011-08-13 00:53:44 +020069 }
Jens Axboeb1ec1da2007-02-23 10:29:16 +010070}
71
72static void show_option_values(struct fio_option *o)
73{
Jens Axboea3073f42010-03-05 10:06:15 +010074 int i;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010075
Jens Axboea3073f42010-03-05 10:06:15 +010076 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboe78372132007-03-14 13:24:07 +010077 const struct value_pair *vp = &o->posval[i];
78
79 if (!vp->ival)
Jens Axboea3073f42010-03-05 10:06:15 +010080 continue;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010081
Jens Axboe06b0be62011-10-04 10:31:10 +020082 log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
Jens Axboe78372132007-03-14 13:24:07 +010083 if (vp->help)
Jens Axboe06b0be62011-10-04 10:31:10 +020084 log_info(" %s", vp->help);
85 log_info("\n");
Jens Axboea3073f42010-03-05 10:06:15 +010086 }
Jens Axboeb1ec1da2007-02-23 10:29:16 +010087
88 if (i)
Jens Axboe06b0be62011-10-04 10:31:10 +020089 log_info("\n");
Jens Axboeb1ec1da2007-02-23 10:29:16 +010090}
91
Jens Axboe06b0be62011-10-04 10:31:10 +020092static void show_option_help(struct fio_option *o, int is_err)
Jens Axboed447a8c2009-07-17 22:26:15 +020093{
94 const char *typehelp[] = {
Jens Axboe07b32322010-03-05 09:48:44 +010095 "invalid",
Jens Axboed447a8c2009-07-17 22:26:15 +020096 "string (opt=bla)",
Jens Axboeae3fb6f2010-09-14 13:10:35 +020097 "string (opt=bla)",
Jens Axboed447a8c2009-07-17 22:26:15 +020098 "string with possible k/m/g postfix (opt=4k)",
99 "string with time postfix (opt=10s)",
100 "string (opt=bla)",
101 "string with dual range (opt=1k-4k,4k-8k)",
102 "integer value (opt=100)",
103 "boolean value (opt=1)",
Yu-ju Hong83349192011-08-13 00:53:44 +0200104 "list of floating point values separated by ':' (opt=5.9:7.8)",
Jens Axboed447a8c2009-07-17 22:26:15 +0200105 "no argument (opt)",
Jens Axboe07b32322010-03-05 09:48:44 +0100106 "deprecated",
Jens Axboed447a8c2009-07-17 22:26:15 +0200107 };
Jens Axboe06b0be62011-10-04 10:31:10 +0200108 int (*logger)(const char *format, ...);
109
110 if (is_err)
111 logger = log_err;
112 else
113 logger = log_info;
Jens Axboed447a8c2009-07-17 22:26:15 +0200114
115 if (o->alias)
Jens Axboe06b0be62011-10-04 10:31:10 +0200116 logger("%20s: %s\n", "alias", o->alias);
Jens Axboed447a8c2009-07-17 22:26:15 +0200117
Jens Axboe06b0be62011-10-04 10:31:10 +0200118 logger("%20s: %s\n", "type", typehelp[o->type]);
119 logger("%20s: %s\n", "default", o->def ? o->def : "no default");
Jens Axboe07b32322010-03-05 09:48:44 +0100120 if (o->prof_name)
Jens Axboe06b0be62011-10-04 10:31:10 +0200121 logger("%20s: only for profile '%s'\n", "valid", o->prof_name);
122 show_option_range(o, logger);
Jens Axboed447a8c2009-07-17 22:26:15 +0200123 show_option_values(o);
124}
125
Jens Axboecb2c86f2006-10-26 13:48:34 +0200126static unsigned long get_mult_time(char c)
127{
128 switch (c) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100129 case 'm':
130 case 'M':
131 return 60;
132 case 'h':
133 case 'H':
134 return 60 * 60;
135 case 'd':
136 case 'D':
137 return 24 * 60 * 60;
138 default:
139 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200140 }
141}
142
Jens Axboea03fb652013-03-29 12:20:51 -0600143static int is_separator(char c)
144{
145 switch (c) {
146 case ':':
147 case '-':
148 case ',':
149 case '/':
150 return 1;
151 default:
152 return 0;
153 }
154}
155
Jens Axboe7bb59102011-07-12 19:47:03 +0200156static unsigned long long __get_mult_bytes(const char *p, void *data,
157 int *percent)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200158{
Jens Axboed6978a32009-07-18 21:04:09 +0200159 unsigned int kb_base = fio_get_kb_base(data);
Jens Axboea639f0b2009-07-18 08:25:35 +0200160 unsigned long long ret = 1;
Jens Axboe57fc29f2010-06-23 22:24:07 +0200161 unsigned int i, pow = 0, mult = kb_base;
162 char *c;
Jens Axboea639f0b2009-07-18 08:25:35 +0200163
Jens Axboe57fc29f2010-06-23 22:24:07 +0200164 if (!p)
165 return 1;
Jens Axboea639f0b2009-07-18 08:25:35 +0200166
Jens Axboe57fc29f2010-06-23 22:24:07 +0200167 c = strdup(p);
168
Jens Axboea03fb652013-03-29 12:20:51 -0600169 for (i = 0; i < strlen(c); i++) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200170 c[i] = tolower(c[i]);
Jens Axboea03fb652013-03-29 12:20:51 -0600171 if (is_separator(c[i])) {
172 c[i] = '\0';
173 break;
174 }
175 }
Jens Axboe57fc29f2010-06-23 22:24:07 +0200176
177 if (!strcmp("pib", c)) {
178 pow = 5;
179 mult = 1000;
180 } else if (!strcmp("tib", c)) {
181 pow = 4;
182 mult = 1000;
183 } else if (!strcmp("gib", c)) {
184 pow = 3;
185 mult = 1000;
186 } else if (!strcmp("mib", c)) {
187 pow = 2;
188 mult = 1000;
189 } else if (!strcmp("kib", c)) {
190 pow = 1;
191 mult = 1000;
192 } else if (!strcmp("p", c) || !strcmp("pb", c))
193 pow = 5;
194 else if (!strcmp("t", c) || !strcmp("tb", c))
195 pow = 4;
196 else if (!strcmp("g", c) || !strcmp("gb", c))
197 pow = 3;
198 else if (!strcmp("m", c) || !strcmp("mb", c))
199 pow = 2;
200 else if (!strcmp("k", c) || !strcmp("kb", c))
201 pow = 1;
Jens Axboe7bb59102011-07-12 19:47:03 +0200202 else if (!strcmp("%", c)) {
203 *percent = 1;
Jens Axboee721c572012-02-09 20:55:29 +0100204 free(c);
Jens Axboe7bb59102011-07-12 19:47:03 +0200205 return ret;
206 }
Jens Axboe57fc29f2010-06-23 22:24:07 +0200207
208 while (pow--)
209 ret *= (unsigned long long) mult;
210
211 free(c);
Jens Axboea639f0b2009-07-18 08:25:35 +0200212 return ret;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200213}
214
Jens Axboe7bb59102011-07-12 19:47:03 +0200215static unsigned long long get_mult_bytes(const char *str, int len, void *data,
Jens Axboe6925dd32011-09-07 22:10:11 +0200216 int *percent)
Jens Axboe57fc29f2010-06-23 22:24:07 +0200217{
Jens Axboe55ed9632011-03-27 20:55:09 +0200218 const char *p = str;
Jens Axboeba4ddd62011-09-07 22:11:00 +0200219 int digit_seen = 0;
Jens Axboe57fc29f2010-06-23 22:24:07 +0200220
Jens Axboe1d1c1872010-07-29 10:50:05 +0200221 if (len < 2)
Jens Axboe7bb59102011-07-12 19:47:03 +0200222 return __get_mult_bytes(str, data, percent);
Jens Axboe1d1c1872010-07-29 10:50:05 +0200223
Steven Langd0c814e2011-10-28 08:37:13 +0200224 /*
225 * Go forward until we hit a non-digit, or +/- sign
226 */
Jens Axboe55ed9632011-03-27 20:55:09 +0200227 while ((p - str) <= len) {
Jens Axboeba4ddd62011-09-07 22:11:00 +0200228 if (!isdigit((int) *p) &&
229 (((*p != '+') && (*p != '-')) || digit_seen))
Jens Axboe55ed9632011-03-27 20:55:09 +0200230 break;
Jens Axboe3c703d12011-09-21 09:27:24 +0200231 digit_seen |= isdigit((int) *p);
Jens Axboe55ed9632011-03-27 20:55:09 +0200232 p++;
233 }
234
Jens Axboe7bb59102011-07-12 19:47:03 +0200235 if (!isalpha((int) *p) && (*p != '%'))
Jens Axboe57fc29f2010-06-23 22:24:07 +0200236 p = NULL;
237
Jens Axboe7bb59102011-07-12 19:47:03 +0200238 return __get_mult_bytes(p, data, percent);
Jens Axboe57fc29f2010-06-23 22:24:07 +0200239}
240
Jens Axboecb2c86f2006-10-26 13:48:34 +0200241/*
Yu-ju Hong83349192011-08-13 00:53:44 +0200242 * Convert string into a floating number. Return 1 for success and 0 otherwise.
243 */
Jens Axboee25839d2012-11-06 10:49:42 +0100244int str_to_float(const char *str, double *val)
Yu-ju Hong83349192011-08-13 00:53:44 +0200245{
246 return (1 == sscanf(str, "%lf", val));
247}
248
249/*
Jens Axboee1f36502006-10-27 10:54:08 +0200250 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200251 */
Jens Axboe6925dd32011-09-07 22:10:11 +0200252int str_to_decimal(const char *str, long long *val, int kilo, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200253{
Jens Axboeb347f9d2009-03-09 14:15:21 +0100254 int len, base;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200255
Jens Axboecb2c86f2006-10-26 13:48:34 +0200256 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100257 if (!len)
258 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200259
Jens Axboeb347f9d2009-03-09 14:15:21 +0100260 if (strstr(str, "0x") || strstr(str, "0X"))
261 base = 16;
262 else
263 base = 10;
264
265 *val = strtoll(str, NULL, base);
Jens Axboecda866c2007-01-10 16:02:32 +0100266 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200267 return 1;
268
Jens Axboe7bb59102011-07-12 19:47:03 +0200269 if (kilo) {
270 unsigned long long mult;
271 int perc = 0;
272
Jens Axboe6925dd32011-09-07 22:10:11 +0200273 mult = get_mult_bytes(str, len, data, &perc);
Jens Axboe7bb59102011-07-12 19:47:03 +0200274 if (perc)
275 *val = -1ULL - *val;
276 else
277 *val *= mult;
278 } else
Jens Axboecb2c86f2006-10-26 13:48:34 +0200279 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +0100280
Jens Axboecb2c86f2006-10-26 13:48:34 +0200281 return 0;
282}
283
Jens Axboe6925dd32011-09-07 22:10:11 +0200284static int check_str_bytes(const char *p, long long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200285{
Jens Axboe6925dd32011-09-07 22:10:11 +0200286 return str_to_decimal(p, val, 1, data);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200287}
288
Jens Axboe63f29372007-01-10 13:20:09 +0100289static int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200290{
Jens Axboe6925dd32011-09-07 22:10:11 +0200291 return str_to_decimal(p, val, 0, NULL);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200292}
293
294void strip_blank_front(char **p)
295{
296 char *s = *p;
297
Jens Axboe4c8e9642011-10-15 14:37:38 +0200298 if (!strlen(s))
299 return;
Jens Axboe76cd9372011-07-08 21:14:57 +0200300 while (isspace((int) *s))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200301 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200302
303 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200304}
305
306void strip_blank_end(char *p)
307{
Jens Axboe853ee7f2009-03-06 20:29:29 +0100308 char *start = p, *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200309
Jens Axboe4c8e9642011-10-15 14:37:38 +0200310 if (!strlen(p))
311 return;
312
Jens Axboe523bfad2007-04-04 11:04:06 +0200313 s = strchr(p, ';');
314 if (s)
315 *s = '\0';
316 s = strchr(p, '#');
317 if (s)
318 *s = '\0';
319 if (s)
320 p = s;
321
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200322 s = p + strlen(p);
Jens Axboe76cd9372011-07-08 21:14:57 +0200323 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200324 s--;
325
326 *(s + 1) = '\0';
327}
328
Jens Axboed6978a32009-07-18 21:04:09 +0200329static int check_range_bytes(const char *str, long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200330{
Jens Axboe57fc29f2010-06-23 22:24:07 +0200331 long long __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200332
Jens Axboe6925dd32011-09-07 22:10:11 +0200333 if (!str_to_decimal(str, &__val, 1, data)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200334 *val = __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200335 return 0;
336 }
337
Jens Axboecb2c86f2006-10-26 13:48:34 +0200338 return 1;
339}
340
Jens Axboe63f29372007-01-10 13:20:09 +0100341static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200342{
Jens Axboe787f7e92006-11-06 13:26:29 +0100343 if (!strlen(p))
344 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200345 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200346 if (sscanf(p, "%x", val) == 1)
347 return 0;
348 } else {
349 if (sscanf(p, "%u", val) == 1)
350 return 0;
351 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200352
353 return 1;
354}
355
Jens Axboe808def72010-07-14 16:27:02 -0600356static int opt_len(const char *str)
357{
358 char *postfix;
359
360 postfix = strchr(str, ':');
361 if (!postfix)
362 return strlen(str);
363
364 return (int)(postfix - str);
365}
366
Jens Axboe119cd932012-12-06 17:34:57 +0100367static int str_match_len(const struct value_pair *vp, const char *str)
368{
369 return max(strlen(vp->ival), opt_len(str));
370}
371
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100372#define val_store(ptr, val, off, or, data) \
Jens Axboe17abbe82006-11-06 11:23:10 +0100373 do { \
374 ptr = td_var((data), (off)); \
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100375 if ((or)) \
376 *ptr |= (val); \
377 else \
378 *ptr = (val); \
Jens Axboe17abbe82006-11-06 11:23:10 +0100379 } while (0)
380
Jens Axboef90eff52006-11-06 11:08:21 +0100381static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Yu-ju Hong83349192011-08-13 00:53:44 +0200382 int first, int more, int curr)
Jens Axboee1f36502006-10-27 10:54:08 +0200383{
Jens Axboe63f29372007-01-10 13:20:09 +0100384 int il, *ilp;
Vincent Kang Fufd112d32013-02-02 09:28:55 +0100385 fio_fp64_t *flp;
Jens Axboe63f29372007-01-10 13:20:09 +0100386 long long ull, *ullp;
387 long ul1, ul2;
Yu-ju Hong83349192011-08-13 00:53:44 +0200388 double uf;
Jens Axboebfe1d592012-11-29 21:33:03 +0100389 char **cp = NULL;
Jens Axboee42b01e2011-05-05 12:05:10 -0600390 int ret = 0, is_time = 0;
Jens Axboec44b1ff2011-08-30 20:43:53 -0600391 const struct value_pair *vp;
392 struct value_pair posval[PARSE_MAX_VP];
Jens Axboeeef02442013-02-06 08:51:57 +0100393 int i, all_skipped = 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200394
Jens Axboea3d741f2008-02-27 18:32:33 +0100395 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
396 o->type, ptr);
397
Jens Axboee3cedca2008-11-19 19:57:52 +0100398 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200399 log_err("Option %s requires an argument\n", o->name);
Jens Axboe08e26e32006-11-21 13:15:10 +0100400 return 1;
401 }
402
Jens Axboee1f36502006-10-27 10:54:08 +0200403 switch (o->type) {
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100404 case FIO_OPT_STR:
405 case FIO_OPT_STR_MULTI: {
Jens Axboee1f36502006-10-27 10:54:08 +0200406 fio_opt_str_fn *fn = o->cb;
407
Jens Axboef0857372007-03-19 13:15:23 +0100408 posval_sort(o, posval);
409
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100410 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100411 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100412 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100413 if (!vp->ival || vp->ival[0] == '\0')
Jens Axboea3073f42010-03-05 10:06:15 +0100414 continue;
Jens Axboeae2ddba2010-03-10 12:49:03 +0100415 all_skipped = 0;
Jens Axboe119cd932012-12-06 17:34:57 +0100416 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100417 ret = 0;
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100418 if (o->roff1) {
419 if (vp->or)
420 *(unsigned int *) o->roff1 |= vp->oval;
421 else
422 *(unsigned int *) o->roff1 = vp->oval;
423 } else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100424 if (!o->off1)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100425 continue;
426 val_store(ilp, vp->oval, o->off1, vp->or, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100427 }
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100428 continue;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100429 }
430 }
431
Jens Axboeae2ddba2010-03-10 12:49:03 +0100432 if (ret && !all_skipped)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100433 show_option_values(o);
434 else if (fn)
435 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200436 break;
437 }
Jens Axboee42b01e2011-05-05 12:05:10 -0600438 case FIO_OPT_STR_VAL_TIME:
439 is_time = 1;
Jens Axboef7fa2652009-03-09 14:20:20 +0100440 case FIO_OPT_INT:
Jens Axboee42b01e2011-05-05 12:05:10 -0600441 case FIO_OPT_STR_VAL: {
442 fio_opt_str_val_fn *fn = o->cb;
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200443 char tmp[128], *p;
444
445 strncpy(tmp, ptr, sizeof(tmp) - 1);
446 p = strchr(tmp, ',');
447 if (p)
448 *p = '\0';
Jens Axboee1f36502006-10-27 10:54:08 +0200449
Jens Axboee42b01e2011-05-05 12:05:10 -0600450 if (is_time)
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200451 ret = check_str_time(tmp, &ull);
Jens Axboee42b01e2011-05-05 12:05:10 -0600452 else
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200453 ret = check_str_bytes(tmp, &ull, data);
Jens Axboee42b01e2011-05-05 12:05:10 -0600454
Jens Axboee1f36502006-10-27 10:54:08 +0200455 if (ret)
456 break;
457
Jens Axboedb8e0162007-01-10 13:41:26 +0100458 if (o->maxval && ull > o->maxval) {
Jens Axboed4fc2f02012-09-24 14:32:26 +0200459 log_err("max value out of range: %llu"
460 " (%u max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100461 return 1;
462 }
463 if (o->minval && ull < o->minval) {
Jens Axboed4fc2f02012-09-24 14:32:26 +0200464 log_err("min value out of range: %llu"
465 " (%u min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100466 return 1;
467 }
Jens Axboee1f36502006-10-27 10:54:08 +0200468
469 if (fn)
470 ret = fn(data, &ull);
471 else {
Jens Axboee01b22b2009-06-09 15:43:25 +0200472 if (o->type == FIO_OPT_INT) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100473 if (first) {
474 if (o->roff1)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100475 *(unsigned int *) o->roff1 = ull;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100476 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100477 val_store(ilp, ull, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100478 }
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200479 if (curr == 1) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100480 if (o->roff2)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100481 *(unsigned int *) o->roff2 = ull;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100482 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100483 val_store(ilp, ull, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100484 }
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200485 if (curr == 2) {
486 if (o->roff3)
487 *(unsigned int *) o->roff3 = ull;
488 else if (o->off3)
489 val_store(ilp, ull, o->off3, 0, data);
490 }
491 if (!more) {
492 if (curr < 1) {
493 if (o->roff2)
494 *(unsigned int *) o->roff2 = ull;
495 else if (o->off2)
496 val_store(ilp, ull, o->off2, 0, data);
497 }
498 if (curr < 2) {
499 if (o->roff3)
500 *(unsigned int *) o->roff3 = ull;
501 else if (o->off3)
502 val_store(ilp, ull, o->off3, 0, data);
503 }
504 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100505 } else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100506 if (first) {
507 if (o->roff1)
508 *(unsigned long long *) o->roff1 = ull;
509 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100510 val_store(ullp, ull, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100511 }
512 if (!more) {
513 if (o->roff2)
514 *(unsigned long long *) o->roff2 = ull;
515 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100516 val_store(ullp, ull, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100517 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100518 }
Jens Axboee1f36502006-10-27 10:54:08 +0200519 }
520 break;
521 }
Yu-ju Hong83349192011-08-13 00:53:44 +0200522 case FIO_OPT_FLOAT_LIST: {
Jens Axboeeef02442013-02-06 08:51:57 +0100523 char *cp2;
524
Vincent Kang Fu435d1952013-02-06 08:43:40 +0100525 if (first) {
526 /*
527 ** Initialize precision to 0 and zero out list
528 ** in case specified list is shorter than default
529 */
530 ul2 = 0;
531 ilp = td_var(data, o->off2);
532 *ilp = ul2;
533
534 flp = td_var(data, o->off1);
535 for(i = 0; i < o->maxlen; i++)
536 flp[i].u.f = 0.0;
537 }
Yu-ju Hong83349192011-08-13 00:53:44 +0200538 if (curr >= o->maxlen) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200539 log_err("the list exceeding max length %d\n",
Yu-ju Hong83349192011-08-13 00:53:44 +0200540 o->maxlen);
541 return 1;
542 }
Jens Axboe619adf92013-02-06 08:46:55 +0100543 if (!str_to_float(ptr, &uf)) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200544 log_err("not a floating point value: %s\n", ptr);
Yu-ju Hong83349192011-08-13 00:53:44 +0200545 return 1;
546 }
Jens Axboe26650692013-02-02 09:56:23 +0100547 if (uf > o->maxfp) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200548 log_err("value out of range: %f"
Yu-ju Hong83349192011-08-13 00:53:44 +0200549 " (range max: %f)\n", uf, o->maxfp);
550 return 1;
551 }
Jens Axboe26650692013-02-02 09:56:23 +0100552 if (uf < o->minfp) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200553 log_err("value out of range: %f"
Yu-ju Hong83349192011-08-13 00:53:44 +0200554 " (range min: %f)\n", uf, o->minfp);
555 return 1;
556 }
557
558 flp = td_var(data, o->off1);
Vincent Kang Fufd112d32013-02-02 09:28:55 +0100559 flp[curr].u.f = uf;
Yu-ju Hong83349192011-08-13 00:53:44 +0200560
Vincent Kang Fu435d1952013-02-06 08:43:40 +0100561 /*
562 ** Calculate precision for output by counting
563 ** number of digits after period. Find first
564 ** period in entire remaining list each time
565 */
566 cp2 = strchr(ptr, '.');
567 if (cp2 != NULL) {
Jens Axboeeef02442013-02-06 08:51:57 +0100568 int len = 0;
Vincent Kang Fu435d1952013-02-06 08:43:40 +0100569
570 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
571 len++;
572
573 ilp = td_var(data, o->off2);
574 if (len > *ilp)
575 *ilp = len;
576 }
577
Yu-ju Hong83349192011-08-13 00:53:44 +0200578 break;
579 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100580 case FIO_OPT_STR_STORE: {
581 fio_opt_str_fn *fn = o->cb;
582
Steven Lang184b4092011-11-16 10:33:51 +0100583 if (o->roff1 || o->off1) {
584 if (o->roff1)
585 cp = (char **) o->roff1;
586 else if (o->off1)
587 cp = td_var(data, o->off1);
Jens Axboece952ab2011-08-31 14:29:22 -0600588
Steven Lang184b4092011-11-16 10:33:51 +0100589 *cp = strdup(ptr);
Jens Axboe1c964ce2011-08-31 16:45:03 -0600590 }
Jens Axboe02c6aad2010-03-04 13:49:11 +0100591
Steven Lang184b4092011-11-16 10:33:51 +0100592 if (fn)
593 ret = fn(data, ptr);
594 else if (o->posval[0].ival) {
595 posval_sort(o, posval);
Jens Axboec44b1ff2011-08-30 20:43:53 -0600596
Steven Lang184b4092011-11-16 10:33:51 +0100597 ret = 1;
598 for (i = 0; i < PARSE_MAX_VP; i++) {
599 vp = &posval[i];
600 if (!vp->ival || vp->ival[0] == '\0')
601 continue;
602 all_skipped = 0;
Jens Axboe119cd932012-12-06 17:34:57 +0100603 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
Steven Lang184b4092011-11-16 10:33:51 +0100604 char *rest;
605
606 ret = 0;
607 if (vp->cb)
608 fn = vp->cb;
609 rest = strstr(*cp ?: ptr, ":");
610 if (rest) {
611 if (*cp)
612 *rest = '\0';
613 ptr = rest + 1;
614 } else
615 ptr = NULL;
616 break;
617 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100618 }
619 }
Jens Axboec44b1ff2011-08-30 20:43:53 -0600620
Steven Lang184b4092011-11-16 10:33:51 +0100621 if (!all_skipped) {
622 if (ret && !*cp)
623 show_option_values(o);
624 else if (ret && *cp)
625 ret = 0;
626 else if (fn && ptr)
627 ret = fn(data, ptr);
628 }
Jens Axboec44b1ff2011-08-30 20:43:53 -0600629
Jens Axboee1f36502006-10-27 10:54:08 +0200630 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100631 }
Jens Axboee1f36502006-10-27 10:54:08 +0200632 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200633 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200634 char *p1, *p2;
635
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100636 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200637
Dave Engberg31d23f42011-07-23 21:07:13 +0200638 /* Handle bsrange with separate read,write values: */
639 p1 = strchr(tmp, ',');
640 if (p1)
641 *p1 = '\0';
642
Jens Axboeb765a372006-10-27 15:00:16 +0200643 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200644 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100645 p1 = strchr(tmp, ':');
646 if (!p1) {
647 ret = 1;
648 break;
649 }
Jens Axboee1f36502006-10-27 10:54:08 +0200650 }
651
652 p2 = p1 + 1;
653 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200654 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200655
656 ret = 1;
Jens Axboed6978a32009-07-18 21:04:09 +0200657 if (!check_range_bytes(p1, &ul1, data) &&
658 !check_range_bytes(p2, &ul2, data)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200659 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200660 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100661 unsigned long foo = ul1;
662
663 ul1 = ul2;
664 ul2 = foo;
665 }
666
667 if (first) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100668 if (o->roff1)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100669 *(unsigned int *) o->roff1 = ul1;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100670 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100671 val_store(ilp, ul1, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100672 if (o->roff2)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100673 *(unsigned int *) o->roff2 = ul2;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100674 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100675 val_store(ilp, ul2, o->off2, 0, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200676 }
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200677 if (curr == 1) {
678 if (o->roff3 && o->roff4) {
679 *(unsigned int *) o->roff3 = ul1;
680 *(unsigned int *) o->roff4 = ul2;
681 } else if (o->off3 && o->off4) {
682 val_store(ilp, ul1, o->off3, 0, data);
683 val_store(ilp, ul2, o->off4, 0, data);
684 }
685 }
686 if (curr == 2) {
687 if (o->roff5 && o->roff6) {
688 *(unsigned int *) o->roff5 = ul1;
689 *(unsigned int *) o->roff6 = ul2;
690 } else if (o->off5 && o->off6) {
691 val_store(ilp, ul1, o->off5, 0, data);
692 val_store(ilp, ul2, o->off6, 0, data);
693 }
694 }
695 if (!more) {
696 if (curr < 1) {
697 if (o->roff3 && o->roff4) {
698 *(unsigned int *) o->roff3 = ul1;
699 *(unsigned int *) o->roff4 = ul2;
700 } else if (o->off3 && o->off4) {
701 val_store(ilp, ul1, o->off3, 0, data);
702 val_store(ilp, ul2, o->off4, 0, data);
703 }
704 }
705 if (curr < 2) {
706 if (o->roff5 && o->roff6) {
707 *(unsigned int *) o->roff5 = ul1;
708 *(unsigned int *) o->roff6 = ul2;
709 } else if (o->off5 && o->off6) {
710 val_store(ilp, ul1, o->off5, 0, data);
711 val_store(ilp, ul2, o->off6, 0, data);
712 }
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 Axboee1f36502006-10-27 10:54:08 +0200730 if (ret)
731 break;
732
Jens Axboedb8e0162007-01-10 13:41:26 +0100733 if (o->maxval && il > (int) o->maxval) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200734 log_err("max value out of range: %d (%d max)\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100735 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100736 return 1;
737 }
738 if (o->minval && il < o->minval) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200739 log_err("min value out of range: %d (%d min)\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100740 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100741 return 1;
742 }
Jens Axboee1f36502006-10-27 10:54:08 +0200743
Jens Axboe76a43db2007-01-11 13:24:44 +0100744 if (o->neg)
745 il = !il;
746
Jens Axboee1f36502006-10-27 10:54:08 +0200747 if (fn)
748 ret = fn(data, &il);
749 else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100750 if (first) {
751 if (o->roff1)
752 *(unsigned int *)o->roff1 = il;
753 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100754 val_store(ilp, il, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100755 }
756 if (!more) {
757 if (o->roff2)
758 *(unsigned int *) o->roff2 = il;
759 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100760 val_store(ilp, il, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100761 }
Jens Axboee1f36502006-10-27 10:54:08 +0200762 }
763 break;
764 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200765 case FIO_OPT_DEPRECATED:
Jens Axboe06b0be62011-10-04 10:31:10 +0200766 log_info("Option %s is deprecated\n", o->name);
Jens Axboe15ca1502008-04-07 09:26:02 +0200767 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200768 default:
Jens Axboe06b0be62011-10-04 10:31:10 +0200769 log_err("Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200770 ret = 1;
771 }
772
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200773 if (ret)
774 return ret;
775
Jens Axboed447a8c2009-07-17 22:26:15 +0200776 if (o->verify) {
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200777 ret = o->verify(o, data);
Jens Axboed447a8c2009-07-17 22:26:15 +0200778 if (ret) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200779 log_err("Correct format for offending option\n");
780 log_err("%20s: %s\n", o->name, o->help);
Jens Axboe06b0be62011-10-04 10:31:10 +0200781 show_option_help(o, 1);
Jens Axboed447a8c2009-07-17 22:26:15 +0200782 }
783 }
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200784
Jens Axboee1f36502006-10-27 10:54:08 +0200785 return ret;
786}
787
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200788static int handle_option(struct fio_option *o, const char *__ptr, void *data)
Jens Axboef90eff52006-11-06 11:08:21 +0100789{
Jens Axboeb62bdf22010-03-10 12:36:17 +0100790 char *o_ptr, *ptr, *ptr2;
791 int ret, done;
Jens Axboef90eff52006-11-06 11:08:21 +0100792
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200793 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
794
Jens Axboeb62bdf22010-03-10 12:36:17 +0100795 o_ptr = ptr = NULL;
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200796 if (__ptr)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100797 o_ptr = ptr = strdup(__ptr);
Jens Axboea3d741f2008-02-27 18:32:33 +0100798
Jens Axboef90eff52006-11-06 11:08:21 +0100799 /*
Jens Axboeb62bdf22010-03-10 12:36:17 +0100800 * See if we have another set of parameters, hidden after a comma.
801 * Do this before parsing this round, to check if we should
Jens Axboe787f7e92006-11-06 13:26:29 +0100802 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100803 */
Jens Axboeb62bdf22010-03-10 12:36:17 +0100804 done = 0;
805 ret = 1;
806 do {
807 int __ret;
808
809 ptr2 = NULL;
810 if (ptr &&
811 (o->type != FIO_OPT_STR_STORE) &&
Yu-ju Hong83349192011-08-13 00:53:44 +0200812 (o->type != FIO_OPT_STR) &&
813 (o->type != FIO_OPT_FLOAT_LIST)) {
Jens Axboeb62bdf22010-03-10 12:36:17 +0100814 ptr2 = strchr(ptr, ',');
815 if (ptr2 && *(ptr2 + 1) == '\0')
816 *ptr2 = '\0';
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200817 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
Jens Axboeb62bdf22010-03-10 12:36:17 +0100818 if (!ptr2)
819 ptr2 = strchr(ptr, ':');
820 if (!ptr2)
821 ptr2 = strchr(ptr, '-');
822 }
Yu-ju Hong83349192011-08-13 00:53:44 +0200823 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
824 ptr2 = strchr(ptr, ':');
Jens Axboeb62bdf22010-03-10 12:36:17 +0100825 }
826
827 /*
828 * Don't return early if parsing the first option fails - if
829 * we are doing multiple arguments, we can allow the first one
830 * being empty.
831 */
Yu-ju Hong83349192011-08-13 00:53:44 +0200832 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
Jens Axboeb62bdf22010-03-10 12:36:17 +0100833 if (ret)
834 ret = __ret;
835
Jens Axboe0c9baf92007-01-11 15:59:26 +0100836 if (!ptr2)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100837 break;
Jens Axboe787f7e92006-11-06 13:26:29 +0100838
Jens Axboeb62bdf22010-03-10 12:36:17 +0100839 ptr = ptr2 + 1;
840 done++;
841 } while (1);
Jens Axboe787f7e92006-11-06 13:26:29 +0100842
Jens Axboeb62bdf22010-03-10 12:36:17 +0100843 if (o_ptr)
844 free(o_ptr);
845 return ret;
Jens Axboef90eff52006-11-06 11:08:21 +0100846}
847
Steven Langde890a12011-11-09 14:03:34 +0100848static struct fio_option *get_option(char *opt,
Jens Axboe07b32322010-03-05 09:48:44 +0100849 struct fio_option *options, char **post)
Jens Axboe3b8b7132008-06-10 19:46:23 +0200850{
851 struct fio_option *o;
852 char *ret;
853
854 ret = strchr(opt, '=');
855 if (ret) {
856 *post = ret;
857 *ret = '\0';
Steven Langde890a12011-11-09 14:03:34 +0100858 ret = opt;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200859 (*post)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100860 strip_blank_end(ret);
Jens Axboe07b32322010-03-05 09:48:44 +0100861 o = find_option(options, ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200862 } else {
Jens Axboe07b32322010-03-05 09:48:44 +0100863 o = find_option(options, opt);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200864 *post = NULL;
865 }
866
867 return o;
868}
869
870static int opt_cmp(const void *p1, const void *p2)
871{
Steven Langde890a12011-11-09 14:03:34 +0100872 struct fio_option *o;
873 char *s, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100874 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200875
Jens Axboe8cdabc12008-11-19 12:31:43 +0100876 prio1 = prio2 = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200877
Steven Langde890a12011-11-09 14:03:34 +0100878 if (*(char **)p1) {
879 s = strdup(*((char **) p1));
880 o = get_option(s, fio_options, &foo);
881 if (o)
882 prio1 = o->prio;
883 free(s);
884 }
885 if (*(char **)p2) {
886 s = strdup(*((char **) p2));
887 o = get_option(s, fio_options, &foo);
888 if (o)
889 prio2 = o->prio;
890 free(s);
891 }
892
Jens Axboe8cdabc12008-11-19 12:31:43 +0100893 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200894}
895
896void sort_options(char **opts, struct fio_option *options, int num_opts)
897{
898 fio_options = options;
899 qsort(opts, num_opts, sizeof(char *), opt_cmp);
900 fio_options = NULL;
901}
902
Jens Axboeb4692822006-10-27 13:43:22 +0200903int parse_cmd_option(const char *opt, const char *val,
Jens Axboe07b32322010-03-05 09:48:44 +0100904 struct fio_option *options, void *data)
Jens Axboeb4692822006-10-27 13:43:22 +0200905{
906 struct fio_option *o;
907
Jens Axboe07b32322010-03-05 09:48:44 +0100908 o = find_option(options, opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200909 if (!o) {
Jens Axboe06b0be62011-10-04 10:31:10 +0200910 log_err("Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200911 return 1;
912 }
913
Jens Axboeb1508cf2006-11-02 09:12:40 +0100914 if (!handle_option(o, val, data))
915 return 0;
916
Jens Axboe06b0be62011-10-04 10:31:10 +0200917 log_err("fio: failed parsing %s=%s\n", opt, val);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100918 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200919}
920
Steven Langde890a12011-11-09 14:03:34 +0100921int parse_option(char *opt, const char *input,
922 struct fio_option *options, struct fio_option **o, void *data)
Jens Axboee1f36502006-10-27 10:54:08 +0200923{
Steven Langd0c814e2011-10-28 08:37:13 +0200924 char *post;
Jens Axboee1f36502006-10-27 10:54:08 +0200925
Steven Langd0c814e2011-10-28 08:37:13 +0200926 if (!opt) {
927 log_err("fio: failed parsing %s\n", input);
Steven Langde890a12011-11-09 14:03:34 +0100928 *o = NULL;
Jens Axboe38789b52010-03-03 09:23:20 +0100929 return 1;
Steven Langd0c814e2011-10-28 08:37:13 +0200930 }
Jens Axboee1f36502006-10-27 10:54:08 +0200931
Steven Langde890a12011-11-09 14:03:34 +0100932 *o = get_option(opt, options, &post);
933 if (!*o) {
934 if (post) {
935 int len = strlen(opt);
936 if (opt + len + 1 != post)
937 memmove(opt + len + 1, post, strlen(post));
938 opt[len] = '=';
939 }
Jens Axboee1f36502006-10-27 10:54:08 +0200940 return 1;
941 }
942
Steven Langde890a12011-11-09 14:03:34 +0100943 if (!handle_option(*o, post, data)) {
Jens Axboeb1508cf2006-11-02 09:12:40 +0100944 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200945 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100946
Steven Langd0c814e2011-10-28 08:37:13 +0200947 log_err("fio: failed parsing %s\n", input);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100948 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200949}
Jens Axboefd28ca42007-01-09 21:20:13 +0100950
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100951/*
952 * Option match, levenshtein distance. Handy for not quite remembering what
953 * the option name is.
954 */
955static int string_distance(const char *s1, const char *s2)
956{
957 unsigned int s1_len = strlen(s1);
958 unsigned int s2_len = strlen(s2);
959 unsigned int *p, *q, *r;
960 unsigned int i, j;
961
962 p = malloc(sizeof(unsigned int) * (s2_len + 1));
963 q = malloc(sizeof(unsigned int) * (s2_len + 1));
964
965 p[0] = 0;
966 for (i = 1; i <= s2_len; i++)
967 p[i] = p[i - 1] + 1;
968
969 for (i = 1; i <= s1_len; i++) {
970 q[0] = p[0] + 1;
971 for (j = 1; j <= s2_len; j++) {
972 unsigned int sub = p[j - 1];
973
974 if (s1[i - 1] != s2[j - 1])
975 sub++;
976
977 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
978 }
979 r = p;
980 p = q;
981 q = r;
982 }
983
984 i = p[s2_len];
985 free(p);
986 free(q);
987 return i;
988}
989
Jens Axboeafdf9352007-07-31 16:14:34 +0200990static struct fio_option *find_child(struct fio_option *options,
991 struct fio_option *o)
992{
993 struct fio_option *__o;
994
Jens Axboefdf28742007-07-31 23:06:09 +0200995 for (__o = options + 1; __o->name; __o++)
996 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +0200997 return __o;
998
999 return NULL;
1000}
1001
Jens Axboe323d9112008-03-01 15:12:14 +01001002static void __print_option(struct fio_option *o, struct fio_option *org,
1003 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +02001004{
1005 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +01001006 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +02001007
1008 if (!o)
1009 return;
Jens Axboeef9aff52007-07-31 22:56:53 +02001010 if (!org)
1011 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001012
Jens Axboeafdf9352007-07-31 16:14:34 +02001013 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +01001014 depth = level;
1015 while (depth--)
1016 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +02001017
1018 sprintf(p, "%s", o->name);
1019
Jens Axboe28d7c362011-10-05 20:30:24 +02001020 log_info("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +01001021}
1022
1023static void print_option(struct fio_option *o)
1024{
1025 struct fio_option *parent;
1026 struct fio_option *__o;
1027 unsigned int printed;
1028 unsigned int level;
1029
1030 __print_option(o, NULL, 0);
1031 parent = o;
1032 level = 0;
1033 do {
1034 level++;
1035 printed = 0;
1036
1037 while ((__o = find_child(o, parent)) != NULL) {
1038 __print_option(__o, o, level);
1039 o = __o;
1040 printed++;
1041 }
1042
1043 parent = o;
1044 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +02001045}
1046
Jens Axboe07b32322010-03-05 09:48:44 +01001047int show_cmd_help(struct fio_option *options, const char *name)
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001048{
1049 struct fio_option *o, *closest;
Jens Axboed091d092010-04-01 19:56:23 +02001050 unsigned int best_dist = -1U;
Jens Axboe29fc6af2007-01-09 21:22:02 +01001051 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +01001052 int show_all = 0;
1053
1054 if (!name || !strcmp(name, "all"))
1055 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +01001056
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001057 closest = NULL;
1058 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +01001059 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +01001060 int match = 0;
1061
Jens Axboe15ca1502008-04-07 09:26:02 +02001062 if (o->type == FIO_OPT_DEPRECATED)
1063 continue;
Jens Axboe07b32322010-03-05 09:48:44 +01001064 if (!exec_profile && o->prof_name)
1065 continue;
Jens Axboe15ca1502008-04-07 09:26:02 +02001066
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001067 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +01001068 if (!strcmp(name, o->name) ||
1069 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001070 match = 1;
1071 else {
1072 unsigned int dist;
1073
1074 dist = string_distance(name, o->name);
1075 if (dist < best_dist) {
1076 best_dist = dist;
1077 closest = o;
1078 }
1079 }
1080 }
Jens Axboefd28ca42007-01-09 21:20:13 +01001081
1082 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +01001083 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +01001084 if (match)
Jens Axboe28d7c362011-10-05 20:30:24 +02001085 log_info("%20s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +01001086 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +02001087 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +01001088 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +01001089 continue;
Jens Axboec167ded2007-03-14 13:02:53 +01001090 }
Jens Axboefd28ca42007-01-09 21:20:13 +01001091 }
1092
Jens Axboe70df2f12007-01-11 14:04:47 +01001093 if (!match)
1094 continue;
1095
Jens Axboe06b0be62011-10-04 10:31:10 +02001096 show_option_help(o, 0);
Jens Axboefd28ca42007-01-09 21:20:13 +01001097 }
1098
Jens Axboe29fc6af2007-01-09 21:22:02 +01001099 if (found)
1100 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +01001101
Jens Axboe28d7c362011-10-05 20:30:24 +02001102 log_err("No such command: %s", name);
Jens Axboed091d092010-04-01 19:56:23 +02001103
1104 /*
1105 * Only print an appropriately close option, one where the edit
1106 * distance isn't too big. Otherwise we get crazy matches.
1107 */
1108 if (closest && best_dist < 3) {
Jens Axboe28d7c362011-10-05 20:30:24 +02001109 log_info(" - showing closest match\n");
1110 log_info("%20s: %s\n", closest->name, closest->help);
Jens Axboe06b0be62011-10-04 10:31:10 +02001111 show_option_help(closest, 0);
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001112 } else
Jens Axboe28d7c362011-10-05 20:30:24 +02001113 log_info("\n");
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001114
Jens Axboe29fc6af2007-01-09 21:22:02 +01001115 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +01001116}
Jens Axboeee738492007-01-10 11:23:16 +01001117
Jens Axboe13335dd2007-01-10 13:14:02 +01001118/*
1119 * Handle parsing of default parameters.
1120 */
Jens Axboeee738492007-01-10 11:23:16 +01001121void fill_default_options(void *data, struct fio_option *options)
1122{
Jens Axboe4945ba12007-01-11 14:36:56 +01001123 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +01001124
Jens Axboea3d741f2008-02-27 18:32:33 +01001125 dprint(FD_PARSE, "filling default options\n");
1126
Jens Axboe4945ba12007-01-11 14:36:56 +01001127 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +01001128 if (o->def)
1129 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +01001130}
Jens Axboe13335dd2007-01-10 13:14:02 +01001131
Jens Axboe9f988e22010-03-04 10:42:38 +01001132void option_init(struct fio_option *o)
1133{
1134 if (o->type == FIO_OPT_DEPRECATED)
1135 return;
1136 if (o->type == FIO_OPT_BOOL) {
1137 o->minval = 0;
1138 o->maxval = 1;
1139 }
Jens Axboed4fc2f02012-09-24 14:32:26 +02001140 if (o->type == FIO_OPT_INT) {
1141 if (!o->maxval)
1142 o->maxval = UINT_MAX;
1143 }
Yu-ju Hong83349192011-08-13 00:53:44 +02001144 if (o->type == FIO_OPT_FLOAT_LIST) {
Bruce Cranab9461e2013-02-02 14:31:24 +00001145 o->minfp = DBL_MIN;
1146 o->maxfp = DBL_MAX;
Yu-ju Hong83349192011-08-13 00:53:44 +02001147 }
Jens Axboe9f988e22010-03-04 10:42:38 +01001148 if (o->type == FIO_OPT_STR_SET && o->def) {
Jens Axboe06b0be62011-10-04 10:31:10 +02001149 log_err("Option %s: string set option with"
Jens Axboe9f988e22010-03-04 10:42:38 +01001150 " default will always be true\n", o->name);
1151 }
Jens Axboe06b0be62011-10-04 10:31:10 +02001152 if (!o->cb && (!o->off1 && !o->roff1))
1153 log_err("Option %s: neither cb nor offset given\n", o->name);
Jens Axboe5f6ddf12010-03-09 20:40:44 +01001154 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1155 o->type == FIO_OPT_STR_MULTI)
Jens Axboe9f988e22010-03-04 10:42:38 +01001156 return;
Jens Axboee2de69d2010-03-04 14:05:48 +01001157 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
1158 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
Jens Axboe06b0be62011-10-04 10:31:10 +02001159 log_err("Option %s: both cb and offset given\n", o->name);
Jens Axboe9f988e22010-03-04 10:42:38 +01001160 }
1161}
1162
Jens Axboe13335dd2007-01-10 13:14:02 +01001163/*
1164 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +01001165 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +01001166 */
1167void options_init(struct fio_option *options)
1168{
Jens Axboe4945ba12007-01-11 14:36:56 +01001169 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +01001170
Jens Axboea3d741f2008-02-27 18:32:33 +01001171 dprint(FD_PARSE, "init options\n");
1172
Jens Axboe9f988e22010-03-04 10:42:38 +01001173 for (o = &options[0]; o->name; o++)
1174 option_init(o);
Jens Axboe13335dd2007-01-10 13:14:02 +01001175}
Jens Axboe7e356b22011-10-14 10:55:16 +02001176
1177void options_free(struct fio_option *options, void *data)
1178{
1179 struct fio_option *o;
1180 char **ptr;
1181
1182 dprint(FD_PARSE, "free options\n");
1183
1184 for (o = &options[0]; o->name; o++) {
Steven Langde890a12011-11-09 14:03:34 +01001185 if (o->type != FIO_OPT_STR_STORE || !o->off1)
Jens Axboe7e356b22011-10-14 10:55:16 +02001186 continue;
1187
1188 ptr = td_var(data, o->off1);
1189 if (*ptr) {
1190 free(*ptr);
1191 *ptr = NULL;
1192 }
1193 }
1194}