blob: 2df509b24630b05c421c5152709701dfa41bcac9 [file] [log] [blame]
Jens Axboe906c8d72006-06-13 09:37:56 +02001/*
2 * This file contains the ini and command liner parser. It will create
3 * and initialize the specified jobs.
4 */
Jens Axboeebac4652005-12-08 15:25:21 +01005#include <stdio.h>
6#include <stdlib.h>
7#include <unistd.h>
8#include <fcntl.h>
9#include <ctype.h>
10#include <string.h>
11#include <errno.h>
12#include <sys/ipc.h>
13#include <sys/shm.h>
14#include <sys/types.h>
15#include <sys/stat.h>
16
17#include "fio.h"
18
Jens Axboe906c8d72006-06-13 09:37:56 +020019/*
20 * The default options
21 */
Jens Axboe20dc95c2005-12-09 10:29:35 +010022#define DEF_BS (4096)
23#define DEF_TIMEOUT (0)
24#define DEF_RATE_CYCLE (1000)
25#define DEF_ODIRECT (1)
26#define DEF_IO_ENGINE (FIO_SYNCIO)
Jens Axboeebac4652005-12-08 15:25:21 +010027#define DEF_IO_ENGINE_NAME "sync"
Jens Axboe20dc95c2005-12-09 10:29:35 +010028#define DEF_SEQUENTIAL (1)
29#define DEF_RAND_REPEAT (1)
30#define DEF_OVERWRITE (1)
31#define DEF_CREATE (1)
32#define DEF_INVALIDATE (1)
33#define DEF_SYNCIO (0)
34#define DEF_RANDSEED (0xb1899bedUL)
35#define DEF_BWAVGTIME (500)
36#define DEF_CREATE_SER (1)
Jens Axboeebac4652005-12-08 15:25:21 +010037#define DEF_CREATE_FSYNC (1)
Jens Axboe20dc95c2005-12-09 10:29:35 +010038#define DEF_LOOPS (1)
39#define DEF_VERIFY (0)
40#define DEF_STONEWALL (0)
41#define DEF_NUMJOBS (1)
42#define DEF_USE_THREAD (0)
43#define DEF_FILE_SIZE (1024 * 1024 * 1024UL)
44#define DEF_ZONE_SIZE (0)
45#define DEF_ZONE_SKIP (0)
Jens Axboea6ccc7b2006-06-02 10:14:15 +020046#define DEF_RWMIX_CYCLE (500)
47#define DEF_RWMIX_READ (50)
Jens Axboeb6f4d882006-06-02 10:32:51 +020048#define DEF_NICE (0)
Jens Axboeebac4652005-12-08 15:25:21 +010049
Jens Axboe972cfd22006-06-09 11:08:56 +020050static int def_timeout = DEF_TIMEOUT;
Jens Axboe972cfd22006-06-09 11:08:56 +020051
Jens Axboef4866ec2006-06-13 09:38:36 +020052static char fio_version_string[] = "fio 1.5";
Jens Axboeebac4652005-12-08 15:25:21 +010053
Jens Axboe972cfd22006-06-09 11:08:56 +020054static char **ini_file;
Jens Axboeebac4652005-12-08 15:25:21 +010055static int max_jobs = MAX_JOBS;
56
57struct thread_data def_thread;
58struct thread_data *threads = NULL;
59
60int rate_quit = 0;
61int write_lat_log = 0;
62int write_bw_log = 0;
63int exitall_on_terminate = 0;
Jens Axboec6ae0a52006-06-12 11:04:44 +020064int terse_output = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +020065unsigned long long mlock_size = 0;
Jens Axboeeb8bbf42006-06-08 21:40:11 +020066FILE *f_out = NULL;
67FILE *f_err = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +010068
Jens Axboe906c8d72006-06-13 09:37:56 +020069/*
70 * Return a free job structure.
71 */
Jens Axboeebac4652005-12-08 15:25:21 +010072static struct thread_data *get_new_job(int global, struct thread_data *parent)
73{
74 struct thread_data *td;
75
76 if (global)
77 return &def_thread;
78 if (thread_number >= max_jobs)
79 return NULL;
80
81 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +020082 *td = *parent;
83 td->name[0] = '\0';
Jens Axboeebac4652005-12-08 15:25:21 +010084
85 td->fd = -1;
86 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +010087 return td;
88}
89
90static void put_job(struct thread_data *td)
91{
92 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
93 thread_number--;
94}
95
Jens Axboe906c8d72006-06-13 09:37:56 +020096/*
97 * Adds a job to the list of things todo. Sanitizes the various options
98 * to make sure we don't have conflicts, and initializes various
99 * members of td.
100 */
Jens Axboe75154842006-06-01 13:56:09 +0200101static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100102{
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200103 char *ddir_str[] = { "read", "write", "randread", "randwrite",
104 "rw", NULL, "randrw" };
Jens Axboeebac4652005-12-08 15:25:21 +0100105 struct stat sb;
106 int numjobs, ddir;
107
108#ifndef FIO_HAVE_LIBAIO
109 if (td->io_engine == FIO_LIBAIO) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200110 log_err("Linux libaio not available\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100111 return 1;
112 }
113#endif
114#ifndef FIO_HAVE_POSIXAIO
115 if (td->io_engine == FIO_POSIXAIO) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200116 log_err("posix aio not available\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100117 return 1;
118 }
119#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100120
121 /*
122 * the def_thread is just for options, it's not a real job
123 */
124 if (td == &def_thread)
125 return 0;
126
127 if (td->io_engine & FIO_SYNCIO)
128 td->iodepth = 1;
129 else {
130 if (!td->iodepth)
131 td->iodepth = 1;
132 }
133
Jens Axboe20dc95c2005-12-09 10:29:35 +0100134 /*
135 * only really works for sequential io for now
136 */
137 if (td->zone_size && !td->sequential)
138 td->zone_size = 0;
139
Jens Axboe9cc935a2006-06-13 09:11:18 +0200140 /*
141 * Reads can do overwrites, we always need to pre-create the file
142 */
143 if (td_read(td) || td_rw(td))
144 td->overwrite = 1;
145
Jens Axboeebac4652005-12-08 15:25:21 +0100146 td->filetype = FIO_TYPE_FILE;
Jens Axboe0af7b542006-02-17 10:10:12 +0100147 if (!stat(jobname, &sb)) {
148 if (S_ISBLK(sb.st_mode))
149 td->filetype = FIO_TYPE_BD;
150 else if (S_ISCHR(sb.st_mode))
151 td->filetype = FIO_TYPE_CHAR;
152 }
Jens Axboeebac4652005-12-08 15:25:21 +0100153
154 if (td->filetype == FIO_TYPE_FILE) {
Jens Axboee9c047a2006-06-07 21:13:04 +0200155 char tmp[PATH_MAX];
156
Jens Axboeef899b62006-06-06 09:31:00 +0200157 if (td->directory && td->directory[0] != '\0')
Jens Axboef9481912006-06-09 11:37:28 +0200158 sprintf(tmp, "%s/%s.%d", td->directory, jobname, td->thread_number);
Jens Axboeebac4652005-12-08 15:25:21 +0100159 else
Jens Axboef9481912006-06-09 11:37:28 +0200160 sprintf(tmp, "%s.%d", jobname, td->thread_number);
Jens Axboee9c047a2006-06-07 21:13:04 +0200161 td->file_name = strdup(tmp);
Jens Axboeebac4652005-12-08 15:25:21 +0100162 } else
Jens Axboee9c047a2006-06-07 21:13:04 +0200163 td->file_name = strdup(jobname);
Jens Axboeebac4652005-12-08 15:25:21 +0100164
Jens Axboebbfd6b02006-06-07 19:42:54 +0200165 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100166
167 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
168 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
169 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
170
171 if (td->min_bs == -1U)
172 td->min_bs = td->bs;
173 if (td->max_bs == -1U)
174 td->max_bs = td->bs;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200175 if (td_read(td) && !td_rw(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100176 td->verify = 0;
177
178 if (td->stonewall && td->thread_number > 1)
179 groupid++;
180
181 td->groupid = groupid;
182
183 if (setup_rate(td))
184 goto err;
185
186 if (write_lat_log) {
187 setup_log(&td->slat_log);
188 setup_log(&td->clat_log);
189 }
190 if (write_bw_log)
191 setup_log(&td->bw_log);
192
Jens Axboe01452052006-06-07 10:29:47 +0200193 if (td->name[0] == '\0')
194 snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
195
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200196 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
Jens Axboe75154842006-06-01 13:56:09 +0200197
Jens Axboec6ae0a52006-06-12 11:04:44 +0200198 if (!terse_output) {
199 if (!job_add_num)
200 fprintf(f_out, "%s: (g=%d): rw=%s, odir=%d, bs=%d-%d, rate=%d, ioengine=%s, iodepth=%d\n", td->name, td->groupid, ddir_str[ddir], td->odirect, td->min_bs, td->max_bs, td->rate, td->io_engine_name, td->iodepth);
201 else if (job_add_num == 1)
202 fprintf(f_out, "...\n");
203 }
Jens Axboeebac4652005-12-08 15:25:21 +0100204
205 /*
206 * recurse add identical jobs, clear numjobs and stonewall options
207 * as they don't apply to sub-jobs
208 */
209 numjobs = td->numjobs;
210 while (--numjobs) {
211 struct thread_data *td_new = get_new_job(0, td);
212
213 if (!td_new)
214 goto err;
215
216 td_new->numjobs = 1;
217 td_new->stonewall = 0;
Jens Axboe75154842006-06-01 13:56:09 +0200218 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100219
Jens Axboe75154842006-06-01 13:56:09 +0200220 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100221 goto err;
222 }
223 return 0;
224err:
225 put_job(td);
226 return -1;
227}
228
Jens Axboe906c8d72006-06-13 09:37:56 +0200229/*
230 * Initialize the various random states we need (random io, block size ranges,
231 * read/write mix, etc).
232 */
Jens Axboeebac4652005-12-08 15:25:21 +0100233int init_random_state(struct thread_data *td)
234{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200235 unsigned long seeds[4];
Jens Axboeebac4652005-12-08 15:25:21 +0100236 int fd, num_maps, blocks;
237
Jens Axboe1ac267b2006-05-31 20:29:00 +0200238 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100239 if (fd == -1) {
240 td_verror(td, errno);
241 return 1;
242 }
243
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200244 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100245 td_verror(td, EIO);
246 close(fd);
247 return 1;
248 }
249
250 close(fd);
251
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200252 os_random_seed(seeds[0], &td->bsrange_state);
253 os_random_seed(seeds[1], &td->verify_state);
254 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100255
256 if (td->sequential)
257 return 0;
258
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200259 if (td->rand_repeatable)
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200260 seeds[3] = DEF_RANDSEED;
Jens Axboeebac4652005-12-08 15:25:21 +0100261
262 blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
263 num_maps = blocks / BLOCKS_PER_MAP;
264 td->file_map = malloc(num_maps * sizeof(long));
265 td->num_maps = num_maps;
266 memset(td->file_map, 0, num_maps * sizeof(long));
267
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200268 os_random_seed(seeds[3], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100269 return 0;
270}
271
272static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
273{
274#ifdef FIO_HAVE_CPU_AFFINITY
275 unsigned int i;
276
277 CPU_ZERO(&cpumask);
278
279 for (i = 0; i < sizeof(int) * 8; i++) {
280 if ((1 << i) & cpu)
281 CPU_SET(i, &cpumask);
282 }
283#endif
284}
285
Jens Axboe906c8d72006-06-13 09:37:56 +0200286static unsigned long get_mult_time(char c)
287{
288 switch (c) {
289 case 'm':
290 case 'M':
291 return 60;
292 case 'h':
293 case 'H':
294 return 60 * 60;
295 case 'd':
296 case 'D':
297 return 24 * 60 * 60;
298 default:
299 return 1;
300 }
301}
302
303static unsigned long get_mult_bytes(char c)
Jens Axboeebac4652005-12-08 15:25:21 +0100304{
305 switch (c) {
306 case 'k':
307 case 'K':
308 return 1024;
309 case 'm':
310 case 'M':
311 return 1024 * 1024;
312 case 'g':
313 case 'G':
314 return 1024 * 1024 * 1024;
315 default:
316 return 1;
317 }
318}
319
320/*
321 * convert string after '=' into decimal value, noting any size suffix
322 */
Jens Axboe906c8d72006-06-13 09:37:56 +0200323static int str_to_decimal(char *p, unsigned long long *val, int kilo)
Jens Axboeebac4652005-12-08 15:25:21 +0100324{
325 char *str;
326 int len;
327
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100328 str = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100329 if (!str)
330 return 1;
331
332 str++;
333 len = strlen(str);
334
335 *val = strtoul(str, NULL, 10);
336 if (*val == ULONG_MAX && errno == ERANGE)
337 return 1;
338
Jens Axboe906c8d72006-06-13 09:37:56 +0200339 if (kilo)
340 *val *= get_mult_bytes(str[len - 1]);
341 else
342 *val *= get_mult_time(str[len - 1]);
Jens Axboeebac4652005-12-08 15:25:21 +0100343 return 0;
344}
345
Jens Axboe906c8d72006-06-13 09:37:56 +0200346static int check_str_bytes(char *p, char *name, unsigned long long *val)
Jens Axboeebac4652005-12-08 15:25:21 +0100347{
Jens Axboe20dc95c2005-12-09 10:29:35 +0100348 if (strncmp(p, name, strlen(name) - 1))
Jens Axboeebac4652005-12-08 15:25:21 +0100349 return 1;
350
Jens Axboe906c8d72006-06-13 09:37:56 +0200351 return str_to_decimal(p, val, 1);
352}
353
354static int check_str_time(char *p, char *name, unsigned long long *val)
355{
356 if (strncmp(p, name, strlen(name) - 1))
357 return 1;
358
359 return str_to_decimal(p, val, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100360}
361
362static void strip_blank_front(char **p)
363{
364 char *s = *p;
365
Jens Axboe4ae3f762006-05-30 13:35:14 +0200366 while (isspace(*s))
Jens Axboeebac4652005-12-08 15:25:21 +0100367 s++;
368}
369
370static void strip_blank_end(char *p)
371{
Jens Axboe4ae3f762006-05-30 13:35:14 +0200372 char *s = p + strlen(p) - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100373
Jens Axboe4ae3f762006-05-30 13:35:14 +0200374 while (isspace(*s) || iscntrl(*s))
375 s--;
Jens Axboeaea47d42006-05-26 19:27:29 +0200376
Jens Axboe4ae3f762006-05-30 13:35:14 +0200377 *(s + 1) = '\0';
Jens Axboeaea47d42006-05-26 19:27:29 +0200378}
379
Jens Axboeebac4652005-12-08 15:25:21 +0100380typedef int (str_cb_fn)(struct thread_data *, char *);
381
382static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
383{
Jens Axboe843a7412006-06-01 21:14:21 -0700384 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100385
Jens Axboe843a7412006-06-01 21:14:21 -0700386 if (strncmp(p, name, strlen(name)))
387 return 1;
388
389 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100390 if (!s)
391 return 1;
392
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100393 s = strchr(s, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100394 if (!s)
395 return 1;
396
397 s++;
398 strip_blank_front(&s);
399 return cb(td, s);
400}
401
402static int check_strstore(char *p, char *name, char *dest)
403{
Jens Axboe843a7412006-06-01 21:14:21 -0700404 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100405
Jens Axboe843a7412006-06-01 21:14:21 -0700406 if (strncmp(p, name, strlen(name)))
407 return 1;
408
409 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100410 if (!s)
411 return 1;
412
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100413 s = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100414 if (!s)
415 return 1;
416
417 s++;
418 strip_blank_front(&s);
419
420 strcpy(dest, s);
Jens Axboeebac4652005-12-08 15:25:21 +0100421 return 0;
422}
423
Jens Axboe906c8d72006-06-13 09:37:56 +0200424static int __check_range_bytes(char *str, unsigned long *val)
Jens Axboeebac4652005-12-08 15:25:21 +0100425{
Jens Axboe01617be2005-12-08 19:50:40 +0100426 char suffix;
Jens Axboeebac4652005-12-08 15:25:21 +0100427
Jens Axboe01617be2005-12-08 19:50:40 +0100428 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
Jens Axboe906c8d72006-06-13 09:37:56 +0200429 *val *= get_mult_bytes(suffix);
Jens Axboeebac4652005-12-08 15:25:21 +0100430 return 0;
431 }
432
Jens Axboe01617be2005-12-08 19:50:40 +0100433 if (sscanf(str, "%lu", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100434 return 0;
435
436 return 1;
Jens Axboe01617be2005-12-08 19:50:40 +0100437}
Jens Axboeebac4652005-12-08 15:25:21 +0100438
Jens Axboe906c8d72006-06-13 09:37:56 +0200439static int check_range_bytes(char *p, char *name, unsigned long *s,
440 unsigned long *e)
Jens Axboe01617be2005-12-08 19:50:40 +0100441{
442 char option[128];
443 char *str, *p1, *p2;
444
Jens Axboe843a7412006-06-01 21:14:21 -0700445 if (strncmp(p, name, strlen(name)))
446 return 1;
447
Jens Axboe01617be2005-12-08 19:50:40 +0100448 strcpy(option, p);
449 p = option;
450
451 str = strstr(p, name);
452 if (!str)
453 return 1;
454
455 p += strlen(name);
456
457 str = strchr(p, '=');
458 if (!str)
459 return 1;
460
461 /*
462 * 'p' now holds whatever is after the '=' sign
463 */
464 p1 = str + 1;
465
466 /*
467 * terminate p1 at the '-' sign
468 */
469 p = strchr(p1, '-');
470 if (!p)
471 return 1;
472
473 p2 = p + 1;
474 *p = '\0';
475
Jens Axboe906c8d72006-06-13 09:37:56 +0200476 if (!__check_range_bytes(p1, s) && !__check_range_bytes(p2, e))
Jens Axboe01617be2005-12-08 19:50:40 +0100477 return 0;
478
479 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100480}
481
482static int check_int(char *p, char *name, unsigned int *val)
483{
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100484 char *str;
Jens Axboeebac4652005-12-08 15:25:21 +0100485
Jens Axboeb6754f92006-05-30 14:29:32 +0200486 if (strncmp(p, name, strlen(name)))
487 return 1;
488
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100489 str = strstr(p, name);
490 if (!str)
491 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100492
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100493 str = strchr(p, '=');
494 if (!str)
495 return 1;
496
497 str++;
498
499 if (sscanf(str, "%u", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100500 return 0;
501
502 return 1;
503}
504
505static int check_strset(char *p, char *name)
506{
507 return strncmp(p, name, strlen(name));
508}
509
510static int is_empty_or_comment(char *line)
511{
512 unsigned int i;
513
514 for (i = 0; i < strlen(line); i++) {
515 if (line[i] == ';')
516 return 1;
517 if (!isspace(line[i]) && !iscntrl(line[i]))
518 return 0;
519 }
520
521 return 1;
522}
523
524static int str_rw_cb(struct thread_data *td, char *mem)
525{
526 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
527 td->ddir = DDIR_READ;
528 td->sequential = 1;
529 return 0;
530 } else if (!strncmp(mem, "randread", 8)) {
531 td->ddir = DDIR_READ;
532 td->sequential = 0;
533 return 0;
534 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
535 td->ddir = DDIR_WRITE;
536 td->sequential = 1;
537 return 0;
538 } else if (!strncmp(mem, "randwrite", 9)) {
539 td->ddir = DDIR_WRITE;
540 td->sequential = 0;
541 return 0;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200542 } else if (!strncmp(mem, "rw", 2)) {
543 td->ddir = 0;
544 td->iomix = 1;
545 td->sequential = 1;
546 return 0;
547 } else if (!strncmp(mem, "randrw", 6)) {
548 td->ddir = 0;
549 td->iomix = 1;
550 td->sequential = 0;
551 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100552 }
553
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200554 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100555 return 1;
556}
557
558static int str_verify_cb(struct thread_data *td, char *mem)
559{
560 if (!strncmp(mem, "0", 1)) {
561 td->verify = VERIFY_NONE;
562 return 0;
563 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
564 td->verify = VERIFY_MD5;
565 return 0;
566 } else if (!strncmp(mem, "crc32", 5)) {
567 td->verify = VERIFY_CRC32;
568 return 0;
569 }
570
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200571 log_err("fio: verify types: md5, crc32\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100572 return 1;
573}
574
575static int str_mem_cb(struct thread_data *td, char *mem)
576{
577 if (!strncmp(mem, "malloc", 6)) {
578 td->mem_type = MEM_MALLOC;
579 return 0;
580 } else if (!strncmp(mem, "shm", 3)) {
581 td->mem_type = MEM_SHM;
582 return 0;
583 } else if (!strncmp(mem, "mmap", 4)) {
584 td->mem_type = MEM_MMAP;
585 return 0;
586 }
587
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200588 log_err("fio: mem type: malloc, shm, mmap\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100589 return 1;
590}
591
592static int str_ioengine_cb(struct thread_data *td, char *str)
593{
594 if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
595 !strncmp(str, "libaio", 6)) {
596 strcpy(td->io_engine_name, "libaio");
597 td->io_engine = FIO_LIBAIO;
598 return 0;
599 } else if (!strncmp(str, "posixaio", 8)) {
600 strcpy(td->io_engine_name, "posixaio");
601 td->io_engine = FIO_POSIXAIO;
602 return 0;
603 } else if (!strncmp(str, "sync", 4)) {
604 strcpy(td->io_engine_name, "sync");
605 td->io_engine = FIO_SYNCIO;
606 return 0;
607 } else if (!strncmp(str, "mmap", 4)) {
608 strcpy(td->io_engine_name, "mmap");
609 td->io_engine = FIO_MMAPIO;
610 return 0;
611 } else if (!strncmp(str, "sgio", 4)) {
612 strcpy(td->io_engine_name, "sgio");
613 td->io_engine = FIO_SGIO;
614 return 0;
Jens Axboe8756e4d2006-05-27 20:24:53 +0200615 } else if (!strncmp(str, "splice", 6)) {
616 strcpy(td->io_engine_name, "splice");
617 td->io_engine = FIO_SPLICEIO;
618 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100619 }
620
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200621 log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100622 return 1;
623}
624
Jens Axboe07261982006-06-07 10:51:12 +0200625/*
626 * This is our [ini] type file parser.
627 */
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200628int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100629{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200630 unsigned int prioclass, prio, cpu, global, il;
Jens Axboeebac4652005-12-08 15:25:21 +0100631 unsigned long long ull;
632 unsigned long ul1, ul2;
633 struct thread_data *td;
Jens Axboeef899b62006-06-06 09:31:00 +0200634 char *string, *name, *tmpbuf;
Jens Axboeebac4652005-12-08 15:25:21 +0100635 fpos_t off;
636 FILE *f;
637 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200638 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +0100639
640 f = fopen(file, "r");
641 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200642 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100643 return 1;
644 }
645
646 string = malloc(4096);
647 name = malloc(256);
Jens Axboeef899b62006-06-06 09:31:00 +0200648 tmpbuf = malloc(4096);
Jens Axboeebac4652005-12-08 15:25:21 +0100649
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200650 stonewall = stonewall_flag;
Jens Axboeebac4652005-12-08 15:25:21 +0100651 while ((p = fgets(string, 4096, f)) != NULL) {
Jens Axboe45410ac2006-06-07 11:10:39 +0200652 if (ret)
653 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100654 if (is_empty_or_comment(p))
655 continue;
656 if (sscanf(p, "[%s]", name) != 1)
657 continue;
658
659 global = !strncmp(name, "global", 6);
660
661 name[strlen(name) - 1] = '\0';
662
663 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200664 if (!td) {
665 ret = 1;
666 break;
667 }
Jens Axboeebac4652005-12-08 15:25:21 +0100668
Jens Axboe972cfd22006-06-09 11:08:56 +0200669 /*
670 * Seperate multiple job files by a stonewall
671 */
Jens Axboef9481912006-06-09 11:37:28 +0200672 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +0200673 td->stonewall = stonewall;
674 stonewall = 0;
675 }
676
Jens Axboeebac4652005-12-08 15:25:21 +0100677 fgetpos(f, &off);
678 while ((p = fgets(string, 4096, f)) != NULL) {
679 if (is_empty_or_comment(p))
680 continue;
681 if (strstr(p, "["))
682 break;
Jens Axboeb6754f92006-05-30 14:29:32 +0200683 strip_blank_front(&p);
Jens Axboe4ae3f762006-05-30 13:35:14 +0200684 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200685
Jens Axboeebac4652005-12-08 15:25:21 +0100686 if (!check_int(p, "prio", &prio)) {
687#ifndef FIO_HAVE_IOPRIO
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200688 log_err("io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200689 ret = 1;
690 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100691#endif
Jens Axboe8756e4d2006-05-27 20:24:53 +0200692 td->ioprio |= prio;
Jens Axboeebac4652005-12-08 15:25:21 +0100693 fgetpos(f, &off);
694 continue;
695 }
696 if (!check_int(p, "prioclass", &prioclass)) {
697#ifndef FIO_HAVE_IOPRIO
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200698 log_err("io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200699 ret = 1;
700 break;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200701#else
Jens Axboe8756e4d2006-05-27 20:24:53 +0200702 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
Jens Axboeebac4652005-12-08 15:25:21 +0100703 fgetpos(f, &off);
704 continue;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200705#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100706 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200707 if (!check_int(p, "direct", &il)) {
708 td->odirect = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100709 fgetpos(f, &off);
710 continue;
711 }
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200712 if (!check_int(p, "rand_repeatable", &il)) {
713 td->rand_repeatable = il;
714 fgetpos(f, &off);
715 continue;
716 }
Jens Axboeebac4652005-12-08 15:25:21 +0100717 if (!check_int(p, "rate", &td->rate)) {
718 fgetpos(f, &off);
719 continue;
720 }
721 if (!check_int(p, "ratemin", &td->ratemin)) {
722 fgetpos(f, &off);
723 continue;
724 }
725 if (!check_int(p, "ratecycle", &td->ratecycle)) {
726 fgetpos(f, &off);
727 continue;
728 }
729 if (!check_int(p, "thinktime", &td->thinktime)) {
730 fgetpos(f, &off);
731 continue;
732 }
733 if (!check_int(p, "cpumask", &cpu)) {
734#ifndef FIO_HAVE_CPU_AFFINITY
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200735 log_err("cpu affinity not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200736 ret = 1;
737 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100738#endif
739 fill_cpu_mask(td->cpumask, cpu);
740 fgetpos(f, &off);
741 continue;
742 }
743 if (!check_int(p, "fsync", &td->fsync_blocks)) {
744 fgetpos(f, &off);
Jens Axboefc1a4712006-05-30 13:04:05 +0200745 td->end_fsync = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100746 continue;
747 }
748 if (!check_int(p, "startdelay", &td->start_delay)) {
749 fgetpos(f, &off);
750 continue;
751 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200752 if (!check_str_time(p, "timeout", &ull)) {
753 td->timeout = ul1;
Jens Axboeebac4652005-12-08 15:25:21 +0100754 fgetpos(f, &off);
755 continue;
756 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200757 if (!check_int(p, "invalidate", &il)) {
758 td->invalidate_cache = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100759 fgetpos(f, &off);
760 continue;
761 }
762 if (!check_int(p, "iodepth", &td->iodepth)) {
763 fgetpos(f, &off);
764 continue;
765 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200766 if (!check_int(p, "sync", &il)) {
767 td->sync_io = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100768 fgetpos(f, &off);
769 continue;
770 }
771 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
772 fgetpos(f, &off);
773 continue;
774 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200775 if (!check_int(p, "create_serialize", &il)) {
776 td->create_serialize = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100777 fgetpos(f, &off);
778 continue;
779 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200780 if (!check_int(p, "create_fsync", &il)) {
781 td->create_fsync = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100782 fgetpos(f, &off);
783 continue;
784 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200785 if (!check_int(p, "end_fsync", &il)) {
786 td->end_fsync = il;
Jens Axboefc1a4712006-05-30 13:04:05 +0200787 fgetpos(f, &off);
788 continue;
789 }
Jens Axboeebac4652005-12-08 15:25:21 +0100790 if (!check_int(p, "loops", &td->loops)) {
791 fgetpos(f, &off);
792 continue;
793 }
794 if (!check_int(p, "numjobs", &td->numjobs)) {
795 fgetpos(f, &off);
796 continue;
797 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200798 if (!check_int(p, "overwrite", &il)) {
799 td->overwrite = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100800 fgetpos(f, &off);
801 continue;
802 }
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200803 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
804 fgetpos(f, &off);
805 continue;
806 }
807 if (!check_int(p, "rwmixread", &il)) {
808 if (il > 100)
809 il = 100;
810 td->rwmixread = il;
811 fgetpos(f, &off);
812 continue;
813 }
814 if (!check_int(p, "rwmixwrite", &il)) {
815 if (il > 100)
816 il = 100;
817 td->rwmixread = 100 - il;
818 fgetpos(f, &off);
819 continue;
820 }
Jens Axboeb6f4d882006-06-02 10:32:51 +0200821 if (!check_int(p, "nice", &td->nice)) {
822 fgetpos(f, &off);
823 continue;
824 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200825 if (!check_range_bytes(p, "bsrange", &ul1, &ul2)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100826 if (ul1 > ul2) {
827 td->max_bs = ul1;
828 td->min_bs = ul2;
829 } else {
830 td->max_bs = ul2;
831 td->min_bs = ul1;
832 }
833 fgetpos(f, &off);
834 continue;
835 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200836 if (!check_str_bytes(p, "bs", &ull)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100837 td->bs = ull;
838 fgetpos(f, &off);
839 continue;
840 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200841 if (!check_str_bytes(p, "size", &td->file_size)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100842 fgetpos(f, &off);
843 continue;
844 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200845 if (!check_str_bytes(p, "offset", &td->file_offset)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100846 fgetpos(f, &off);
847 continue;
848 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200849 if (!check_str_bytes(p, "zonesize", &td->zone_size)) {
Jens Axboe20dc95c2005-12-09 10:29:35 +0100850 fgetpos(f, &off);
851 continue;
852 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200853 if (!check_str_bytes(p, "zoneskip", &td->zone_skip)) {
Jens Axboe20dc95c2005-12-09 10:29:35 +0100854 fgetpos(f, &off);
855 continue;
856 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200857 if (!check_str_bytes(p, "lockmem", &mlock_size)) {
Jens Axboec04f7ec2006-05-31 10:13:16 +0200858 fgetpos(f, &off);
859 continue;
860 }
Jens Axboeef899b62006-06-06 09:31:00 +0200861 if (!check_strstore(p, "directory", tmpbuf)) {
862 td->directory = strdup(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100863 fgetpos(f, &off);
864 continue;
865 }
Jens Axboe01452052006-06-07 10:29:47 +0200866 if (!check_strstore(p, "name", tmpbuf)) {
867 snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
868 fgetpos(f, &off);
869 continue;
870 }
Jens Axboeebac4652005-12-08 15:25:21 +0100871 if (!check_str(p, "mem", str_mem_cb, td)) {
872 fgetpos(f, &off);
873 continue;
874 }
875 if (!check_str(p, "verify", str_verify_cb, td)) {
876 fgetpos(f, &off);
877 continue;
878 }
879 if (!check_str(p, "rw", str_rw_cb, td)) {
880 fgetpos(f, &off);
881 continue;
882 }
883 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
884 fgetpos(f, &off);
885 continue;
886 }
887 if (!check_strset(p, "create")) {
888 td->create_file = 1;
889 fgetpos(f, &off);
890 continue;
891 }
892 if (!check_strset(p, "exitall")) {
893 exitall_on_terminate = 1;
894 fgetpos(f, &off);
895 continue;
896 }
897 if (!check_strset(p, "stonewall")) {
898 td->stonewall = 1;
899 fgetpos(f, &off);
900 continue;
901 }
902 if (!check_strset(p, "thread")) {
903 td->use_thread = 1;
904 fgetpos(f, &off);
905 continue;
906 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200907 if (!check_strstore(p, "iolog", tmpbuf)) {
Jens Axboe4f693b92006-06-07 22:52:28 +0200908 if (td->write_iolog) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200909 log_err("fio: read iolog overrides given write_iolog\n");
Jens Axboeaf8c0b42006-06-07 22:33:17 +0200910 free(td->iolog_file);
Jens Axboe4f693b92006-06-07 22:52:28 +0200911 td->write_iolog = 0;
912 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200913 td->iolog_file = strdup(tmpbuf);
Jens Axboe843a7412006-06-01 21:14:21 -0700914 td->read_iolog = 1;
Jens Axboe843a7412006-06-01 21:14:21 -0700915 fgetpos(f, &off);
916 continue;
917 }
Jens Axboeaf8c0b42006-06-07 22:33:17 +0200918 if (!check_strstore(p, "write_iolog", tmpbuf)) {
919 if (!td->read_iolog) {
920 td->iolog_file = strdup(tmpbuf);
921 td->write_iolog = 1;
Jens Axboe4f693b92006-06-07 22:52:28 +0200922 } else
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200923 log_err("fio: read iolog overrides given write_iolog\n");
Jens Axboeaea47d42006-05-26 19:27:29 +0200924 fgetpos(f, &off);
925 continue;
926 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200927 if (!check_strstore(p, "exec_prerun", tmpbuf)) {
928 td->exec_prerun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200929 fgetpos(f, &off);
930 continue;
931 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200932 if (!check_strstore(p, "exec_postrun", tmpbuf)) {
933 td->exec_postrun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200934 fgetpos(f, &off);
935 continue;
936 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200937 if (!check_strstore(p, "ioscheduler", tmpbuf)) {
Jens Axboe22f78b32006-06-07 11:30:07 +0200938#ifndef FIO_HAVE_IOSCHED_SWITCH
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200939 log_err("io scheduler switching not available\n");
Jens Axboe22f78b32006-06-07 11:30:07 +0200940 ret = 1;
941 break;
942#else
Jens Axboe80b9feb2006-06-07 10:34:49 +0200943 td->ioscheduler = strdup(tmpbuf);
Jens Axboeda867742006-06-06 10:39:10 -0700944 fgetpos(f, &off);
945 continue;
Jens Axboe22f78b32006-06-07 11:30:07 +0200946#endif
Jens Axboeda867742006-06-06 10:39:10 -0700947 }
Jens Axboeebac4652005-12-08 15:25:21 +0100948
Jens Axboe45410ac2006-06-07 11:10:39 +0200949 /*
950 * Don't break here, continue parsing options so we
951 * dump all the bad ones. Makes trial/error fixups
952 * easier on the user.
953 */
Jens Axboeebac4652005-12-08 15:25:21 +0100954 printf("Client%d: bad option %s\n",td->thread_number,p);
Jens Axboe45410ac2006-06-07 11:10:39 +0200955 ret = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100956 }
Jens Axboeebac4652005-12-08 15:25:21 +0100957
Jens Axboe45410ac2006-06-07 11:10:39 +0200958 if (!ret) {
959 fsetpos(f, &off);
960 ret = add_job(td, name, 0);
961 }
962 if (ret)
963 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100964 }
965
966 free(string);
967 free(name);
Jens Axboeef899b62006-06-06 09:31:00 +0200968 free(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100969 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200970 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100971}
972
973static int fill_def_thread(void)
974{
975 memset(&def_thread, 0, sizeof(def_thread));
976
977 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
978 perror("sched_getaffinity");
979 return 1;
980 }
981
982 /*
983 * fill globals
984 */
985 def_thread.ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200986 def_thread.iomix = 0;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200987 def_thread.bs = DEF_BS;
Jens Axboeebac4652005-12-08 15:25:21 +0100988 def_thread.min_bs = -1;
989 def_thread.max_bs = -1;
990 def_thread.io_engine = DEF_IO_ENGINE;
991 strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200992 def_thread.odirect = DEF_ODIRECT;
Jens Axboeebac4652005-12-08 15:25:21 +0100993 def_thread.ratecycle = DEF_RATE_CYCLE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200994 def_thread.sequential = DEF_SEQUENTIAL;
Jens Axboe972cfd22006-06-09 11:08:56 +0200995 def_thread.timeout = def_timeout;
Jens Axboeebac4652005-12-08 15:25:21 +0100996 def_thread.create_file = DEF_CREATE;
997 def_thread.overwrite = DEF_OVERWRITE;
998 def_thread.invalidate_cache = DEF_INVALIDATE;
999 def_thread.sync_io = DEF_SYNCIO;
1000 def_thread.mem_type = MEM_MALLOC;
1001 def_thread.bw_avg_time = DEF_BWAVGTIME;
1002 def_thread.create_serialize = DEF_CREATE_SER;
1003 def_thread.create_fsync = DEF_CREATE_FSYNC;
1004 def_thread.loops = DEF_LOOPS;
1005 def_thread.verify = DEF_VERIFY;
1006 def_thread.stonewall = DEF_STONEWALL;
1007 def_thread.numjobs = DEF_NUMJOBS;
1008 def_thread.use_thread = DEF_USE_THREAD;
Jens Axboea6ccc7b2006-06-02 10:14:15 +02001009 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
1010 def_thread.rwmixread = DEF_RWMIX_READ;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001011 def_thread.nice = DEF_NICE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001012 def_thread.rand_repeatable = DEF_RAND_REPEAT;
Jens Axboeebac4652005-12-08 15:25:21 +01001013#ifdef FIO_HAVE_DISK_UTIL
1014 def_thread.do_disk_util = 1;
1015#endif
1016
1017 return 0;
1018}
1019
Jens Axboe4785f992006-05-26 03:59:10 +02001020static void usage(char *name)
1021{
1022 printf("%s\n", fio_version_string);
Jens Axboe47623c72006-06-12 10:23:28 +02001023 printf("\t-o Write output to file\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001024 printf("\t-t Runtime in seconds\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001025 printf("\t-l Generate per-job latency logs\n");
1026 printf("\t-w Generate per-job bandwidth logs\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001027 printf("\t-m Minimal (terse) output\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001028 printf("\t-f Job file (Required)\n");
1029 printf("\t-v Print version info and exit\n");
1030}
1031
Jens Axboe972cfd22006-06-09 11:08:56 +02001032static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +01001033{
Jens Axboe972cfd22006-06-09 11:08:56 +02001034 int c, idx = 1, ini_idx = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001035
Jens Axboec6ae0a52006-06-12 11:04:44 +02001036 while ((c = getopt(argc, argv, "t:o:f:lwvhm")) != EOF) {
Jens Axboeebac4652005-12-08 15:25:21 +01001037 switch (c) {
Jens Axboeebac4652005-12-08 15:25:21 +01001038 case 't':
Jens Axboe972cfd22006-06-09 11:08:56 +02001039 def_timeout = atoi(optarg);
1040 idx++;
Jens Axboeebac4652005-12-08 15:25:21 +01001041 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001042 case 'f':
Jens Axboe972cfd22006-06-09 11:08:56 +02001043 ini_idx++;
1044 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1045 ini_file[ini_idx - 1] = strdup(optarg);
1046 idx++;
Jens Axboeebac4652005-12-08 15:25:21 +01001047 break;
1048 case 'l':
1049 write_lat_log = 1;
Jens Axboe972cfd22006-06-09 11:08:56 +02001050 idx++;
Jens Axboeebac4652005-12-08 15:25:21 +01001051 break;
1052 case 'w':
1053 write_bw_log = 1;
Jens Axboe972cfd22006-06-09 11:08:56 +02001054 idx++;
Jens Axboeebac4652005-12-08 15:25:21 +01001055 break;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001056 case 'o':
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001057 f_out = fopen(optarg, "w+");
1058 if (!f_out) {
1059 perror("fopen output");
1060 exit(1);
1061 }
1062 f_err = f_out;
Jens Axboe972cfd22006-06-09 11:08:56 +02001063 idx++;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001064 break;
Jens Axboec6ae0a52006-06-12 11:04:44 +02001065 case 'm':
1066 terse_output = 1;
1067 idx++;
1068 break;
Jens Axboe4785f992006-05-26 03:59:10 +02001069 case 'h':
1070 usage(argv[0]);
1071 exit(0);
Jens Axboeebac4652005-12-08 15:25:21 +01001072 case 'v':
1073 printf("%s\n", fio_version_string);
1074 exit(0);
1075 }
1076 }
Jens Axboec9fad892006-05-27 17:26:50 +02001077
Jens Axboe972cfd22006-06-09 11:08:56 +02001078 while (idx < argc) {
1079 ini_idx++;
1080 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1081 ini_file[ini_idx - 1] = strdup(argv[idx]);
1082 idx++;
1083 }
1084
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001085 if (!f_out) {
1086 f_out = stdout;
1087 f_err = stderr;
1088 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001089
1090 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +01001091}
1092
1093static void free_shm(void)
1094{
1095 struct shmid_ds sbuf;
1096
1097 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001098 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001099 threads = NULL;
1100 shmctl(shm_id, IPC_RMID, &sbuf);
1101 }
1102}
1103
Jens Axboe906c8d72006-06-13 09:37:56 +02001104/*
1105 * The thread area is shared between the main process and the job
1106 * threads/processes. So setup a shared memory segment that will hold
1107 * all the job info.
1108 */
Jens Axboeebac4652005-12-08 15:25:21 +01001109static int setup_thread_area(void)
1110{
1111 /*
1112 * 1024 is too much on some machines, scale max_jobs if
1113 * we get a failure that looks like too large a shm segment
1114 */
1115 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001116 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001117
Jens Axboe906c8d72006-06-13 09:37:56 +02001118 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001119 if (shm_id != -1)
1120 break;
1121 if (errno != EINVAL) {
1122 perror("shmget");
1123 break;
1124 }
1125
1126 max_jobs >>= 1;
1127 } while (max_jobs);
1128
1129 if (shm_id == -1)
1130 return 1;
1131
1132 threads = shmat(shm_id, NULL, 0);
1133 if (threads == (void *) -1) {
1134 perror("shmat");
1135 return 1;
1136 }
1137
1138 atexit(free_shm);
1139 return 0;
1140}
1141
1142int parse_options(int argc, char *argv[])
1143{
Jens Axboe972cfd22006-06-09 11:08:56 +02001144 int job_files, i;
1145
Jens Axboeebac4652005-12-08 15:25:21 +01001146 if (setup_thread_area())
1147 return 1;
1148 if (fill_def_thread())
1149 return 1;
1150
Jens Axboe972cfd22006-06-09 11:08:56 +02001151 job_files = parse_cmd_line(argc, argv);
1152 if (!job_files) {
1153 log_err("Need job file(s)\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001154 usage(argv[0]);
Jens Axboeebac4652005-12-08 15:25:21 +01001155 return 1;
1156 }
1157
Jens Axboe972cfd22006-06-09 11:08:56 +02001158 for (i = 0; i < job_files; i++) {
1159 if (fill_def_thread())
1160 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001161 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001162 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001163 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001164 }
Jens Axboeebac4652005-12-08 15:25:21 +01001165
Jens Axboe88c6ed82006-06-09 11:28:10 +02001166 free(ini_file);
Jens Axboeebac4652005-12-08 15:25:21 +01001167 return 0;
1168}