blob: b310f6633236ac4720b4ef8f7375bcfddea5d276 [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 Axboeebac4652005-12-08 15:25:21 +0100628int parse_jobs_ini(char *file)
629{
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 Axboe972cfd22006-06-09 11:08:56 +0200638 int ret = 0, stonewall = 1;
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
650 while ((p = fgets(string, 4096, f)) != NULL) {
Jens Axboe45410ac2006-06-07 11:10:39 +0200651 if (ret)
652 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100653 if (is_empty_or_comment(p))
654 continue;
655 if (sscanf(p, "[%s]", name) != 1)
656 continue;
657
658 global = !strncmp(name, "global", 6);
659
660 name[strlen(name) - 1] = '\0';
661
662 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200663 if (!td) {
664 ret = 1;
665 break;
666 }
Jens Axboeebac4652005-12-08 15:25:21 +0100667
Jens Axboe972cfd22006-06-09 11:08:56 +0200668 /*
669 * Seperate multiple job files by a stonewall
670 */
Jens Axboef9481912006-06-09 11:37:28 +0200671 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +0200672 td->stonewall = stonewall;
673 stonewall = 0;
674 }
675
Jens Axboeebac4652005-12-08 15:25:21 +0100676 fgetpos(f, &off);
677 while ((p = fgets(string, 4096, f)) != NULL) {
678 if (is_empty_or_comment(p))
679 continue;
680 if (strstr(p, "["))
681 break;
Jens Axboeb6754f92006-05-30 14:29:32 +0200682 strip_blank_front(&p);
Jens Axboe4ae3f762006-05-30 13:35:14 +0200683 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200684
Jens Axboeebac4652005-12-08 15:25:21 +0100685 if (!check_int(p, "prio", &prio)) {
686#ifndef FIO_HAVE_IOPRIO
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200687 log_err("io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200688 ret = 1;
689 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100690#endif
Jens Axboe8756e4d2006-05-27 20:24:53 +0200691 td->ioprio |= prio;
Jens Axboeebac4652005-12-08 15:25:21 +0100692 fgetpos(f, &off);
693 continue;
694 }
695 if (!check_int(p, "prioclass", &prioclass)) {
696#ifndef FIO_HAVE_IOPRIO
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200697 log_err("io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200698 ret = 1;
699 break;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200700#else
Jens Axboe8756e4d2006-05-27 20:24:53 +0200701 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
Jens Axboeebac4652005-12-08 15:25:21 +0100702 fgetpos(f, &off);
703 continue;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200704#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100705 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200706 if (!check_int(p, "direct", &il)) {
707 td->odirect = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100708 fgetpos(f, &off);
709 continue;
710 }
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200711 if (!check_int(p, "rand_repeatable", &il)) {
712 td->rand_repeatable = il;
713 fgetpos(f, &off);
714 continue;
715 }
Jens Axboeebac4652005-12-08 15:25:21 +0100716 if (!check_int(p, "rate", &td->rate)) {
717 fgetpos(f, &off);
718 continue;
719 }
720 if (!check_int(p, "ratemin", &td->ratemin)) {
721 fgetpos(f, &off);
722 continue;
723 }
724 if (!check_int(p, "ratecycle", &td->ratecycle)) {
725 fgetpos(f, &off);
726 continue;
727 }
728 if (!check_int(p, "thinktime", &td->thinktime)) {
729 fgetpos(f, &off);
730 continue;
731 }
732 if (!check_int(p, "cpumask", &cpu)) {
733#ifndef FIO_HAVE_CPU_AFFINITY
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200734 log_err("cpu affinity not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200735 ret = 1;
736 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100737#endif
738 fill_cpu_mask(td->cpumask, cpu);
739 fgetpos(f, &off);
740 continue;
741 }
742 if (!check_int(p, "fsync", &td->fsync_blocks)) {
743 fgetpos(f, &off);
Jens Axboefc1a4712006-05-30 13:04:05 +0200744 td->end_fsync = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100745 continue;
746 }
747 if (!check_int(p, "startdelay", &td->start_delay)) {
748 fgetpos(f, &off);
749 continue;
750 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200751 if (!check_str_time(p, "timeout", &ull)) {
752 td->timeout = ul1;
Jens Axboeebac4652005-12-08 15:25:21 +0100753 fgetpos(f, &off);
754 continue;
755 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200756 if (!check_int(p, "invalidate", &il)) {
757 td->invalidate_cache = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100758 fgetpos(f, &off);
759 continue;
760 }
761 if (!check_int(p, "iodepth", &td->iodepth)) {
762 fgetpos(f, &off);
763 continue;
764 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200765 if (!check_int(p, "sync", &il)) {
766 td->sync_io = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100767 fgetpos(f, &off);
768 continue;
769 }
770 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
771 fgetpos(f, &off);
772 continue;
773 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200774 if (!check_int(p, "create_serialize", &il)) {
775 td->create_serialize = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100776 fgetpos(f, &off);
777 continue;
778 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200779 if (!check_int(p, "create_fsync", &il)) {
780 td->create_fsync = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100781 fgetpos(f, &off);
782 continue;
783 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200784 if (!check_int(p, "end_fsync", &il)) {
785 td->end_fsync = il;
Jens Axboefc1a4712006-05-30 13:04:05 +0200786 fgetpos(f, &off);
787 continue;
788 }
Jens Axboeebac4652005-12-08 15:25:21 +0100789 if (!check_int(p, "loops", &td->loops)) {
790 fgetpos(f, &off);
791 continue;
792 }
793 if (!check_int(p, "numjobs", &td->numjobs)) {
794 fgetpos(f, &off);
795 continue;
796 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200797 if (!check_int(p, "overwrite", &il)) {
798 td->overwrite = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100799 fgetpos(f, &off);
800 continue;
801 }
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200802 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
803 fgetpos(f, &off);
804 continue;
805 }
806 if (!check_int(p, "rwmixread", &il)) {
807 if (il > 100)
808 il = 100;
809 td->rwmixread = il;
810 fgetpos(f, &off);
811 continue;
812 }
813 if (!check_int(p, "rwmixwrite", &il)) {
814 if (il > 100)
815 il = 100;
816 td->rwmixread = 100 - il;
817 fgetpos(f, &off);
818 continue;
819 }
Jens Axboeb6f4d882006-06-02 10:32:51 +0200820 if (!check_int(p, "nice", &td->nice)) {
821 fgetpos(f, &off);
822 continue;
823 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200824 if (!check_range_bytes(p, "bsrange", &ul1, &ul2)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100825 if (ul1 > ul2) {
826 td->max_bs = ul1;
827 td->min_bs = ul2;
828 } else {
829 td->max_bs = ul2;
830 td->min_bs = ul1;
831 }
832 fgetpos(f, &off);
833 continue;
834 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200835 if (!check_str_bytes(p, "bs", &ull)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100836 td->bs = ull;
837 fgetpos(f, &off);
838 continue;
839 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200840 if (!check_str_bytes(p, "size", &td->file_size)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100841 fgetpos(f, &off);
842 continue;
843 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200844 if (!check_str_bytes(p, "offset", &td->file_offset)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100845 fgetpos(f, &off);
846 continue;
847 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200848 if (!check_str_bytes(p, "zonesize", &td->zone_size)) {
Jens Axboe20dc95c2005-12-09 10:29:35 +0100849 fgetpos(f, &off);
850 continue;
851 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200852 if (!check_str_bytes(p, "zoneskip", &td->zone_skip)) {
Jens Axboe20dc95c2005-12-09 10:29:35 +0100853 fgetpos(f, &off);
854 continue;
855 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200856 if (!check_str_bytes(p, "lockmem", &mlock_size)) {
Jens Axboec04f7ec2006-05-31 10:13:16 +0200857 fgetpos(f, &off);
858 continue;
859 }
Jens Axboeef899b62006-06-06 09:31:00 +0200860 if (!check_strstore(p, "directory", tmpbuf)) {
861 td->directory = strdup(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100862 fgetpos(f, &off);
863 continue;
864 }
Jens Axboe01452052006-06-07 10:29:47 +0200865 if (!check_strstore(p, "name", tmpbuf)) {
866 snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
867 fgetpos(f, &off);
868 continue;
869 }
Jens Axboeebac4652005-12-08 15:25:21 +0100870 if (!check_str(p, "mem", str_mem_cb, td)) {
871 fgetpos(f, &off);
872 continue;
873 }
874 if (!check_str(p, "verify", str_verify_cb, td)) {
875 fgetpos(f, &off);
876 continue;
877 }
878 if (!check_str(p, "rw", str_rw_cb, td)) {
879 fgetpos(f, &off);
880 continue;
881 }
882 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
883 fgetpos(f, &off);
884 continue;
885 }
886 if (!check_strset(p, "create")) {
887 td->create_file = 1;
888 fgetpos(f, &off);
889 continue;
890 }
891 if (!check_strset(p, "exitall")) {
892 exitall_on_terminate = 1;
893 fgetpos(f, &off);
894 continue;
895 }
896 if (!check_strset(p, "stonewall")) {
897 td->stonewall = 1;
898 fgetpos(f, &off);
899 continue;
900 }
901 if (!check_strset(p, "thread")) {
902 td->use_thread = 1;
903 fgetpos(f, &off);
904 continue;
905 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200906 if (!check_strstore(p, "iolog", tmpbuf)) {
Jens Axboe4f693b92006-06-07 22:52:28 +0200907 if (td->write_iolog) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200908 log_err("fio: read iolog overrides given write_iolog\n");
Jens Axboeaf8c0b42006-06-07 22:33:17 +0200909 free(td->iolog_file);
Jens Axboe4f693b92006-06-07 22:52:28 +0200910 td->write_iolog = 0;
911 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200912 td->iolog_file = strdup(tmpbuf);
Jens Axboe843a7412006-06-01 21:14:21 -0700913 td->read_iolog = 1;
Jens Axboe843a7412006-06-01 21:14:21 -0700914 fgetpos(f, &off);
915 continue;
916 }
Jens Axboeaf8c0b42006-06-07 22:33:17 +0200917 if (!check_strstore(p, "write_iolog", tmpbuf)) {
918 if (!td->read_iolog) {
919 td->iolog_file = strdup(tmpbuf);
920 td->write_iolog = 1;
Jens Axboe4f693b92006-06-07 22:52:28 +0200921 } else
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200922 log_err("fio: read iolog overrides given write_iolog\n");
Jens Axboeaea47d42006-05-26 19:27:29 +0200923 fgetpos(f, &off);
924 continue;
925 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200926 if (!check_strstore(p, "exec_prerun", tmpbuf)) {
927 td->exec_prerun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200928 fgetpos(f, &off);
929 continue;
930 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200931 if (!check_strstore(p, "exec_postrun", tmpbuf)) {
932 td->exec_postrun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200933 fgetpos(f, &off);
934 continue;
935 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200936 if (!check_strstore(p, "ioscheduler", tmpbuf)) {
Jens Axboe22f78b32006-06-07 11:30:07 +0200937#ifndef FIO_HAVE_IOSCHED_SWITCH
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200938 log_err("io scheduler switching not available\n");
Jens Axboe22f78b32006-06-07 11:30:07 +0200939 ret = 1;
940 break;
941#else
Jens Axboe80b9feb2006-06-07 10:34:49 +0200942 td->ioscheduler = strdup(tmpbuf);
Jens Axboeda867742006-06-06 10:39:10 -0700943 fgetpos(f, &off);
944 continue;
Jens Axboe22f78b32006-06-07 11:30:07 +0200945#endif
Jens Axboeda867742006-06-06 10:39:10 -0700946 }
Jens Axboeebac4652005-12-08 15:25:21 +0100947
Jens Axboe45410ac2006-06-07 11:10:39 +0200948 /*
949 * Don't break here, continue parsing options so we
950 * dump all the bad ones. Makes trial/error fixups
951 * easier on the user.
952 */
Jens Axboeebac4652005-12-08 15:25:21 +0100953 printf("Client%d: bad option %s\n",td->thread_number,p);
Jens Axboe45410ac2006-06-07 11:10:39 +0200954 ret = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100955 }
Jens Axboeebac4652005-12-08 15:25:21 +0100956
Jens Axboe45410ac2006-06-07 11:10:39 +0200957 if (!ret) {
958 fsetpos(f, &off);
959 ret = add_job(td, name, 0);
960 }
961 if (ret)
962 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100963 }
964
965 free(string);
966 free(name);
Jens Axboeef899b62006-06-06 09:31:00 +0200967 free(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100968 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200969 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100970}
971
972static int fill_def_thread(void)
973{
974 memset(&def_thread, 0, sizeof(def_thread));
975
976 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
977 perror("sched_getaffinity");
978 return 1;
979 }
980
981 /*
982 * fill globals
983 */
984 def_thread.ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200985 def_thread.iomix = 0;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200986 def_thread.bs = DEF_BS;
Jens Axboeebac4652005-12-08 15:25:21 +0100987 def_thread.min_bs = -1;
988 def_thread.max_bs = -1;
989 def_thread.io_engine = DEF_IO_ENGINE;
990 strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200991 def_thread.odirect = DEF_ODIRECT;
Jens Axboeebac4652005-12-08 15:25:21 +0100992 def_thread.ratecycle = DEF_RATE_CYCLE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200993 def_thread.sequential = DEF_SEQUENTIAL;
Jens Axboe972cfd22006-06-09 11:08:56 +0200994 def_thread.timeout = def_timeout;
Jens Axboeebac4652005-12-08 15:25:21 +0100995 def_thread.create_file = DEF_CREATE;
996 def_thread.overwrite = DEF_OVERWRITE;
997 def_thread.invalidate_cache = DEF_INVALIDATE;
998 def_thread.sync_io = DEF_SYNCIO;
999 def_thread.mem_type = MEM_MALLOC;
1000 def_thread.bw_avg_time = DEF_BWAVGTIME;
1001 def_thread.create_serialize = DEF_CREATE_SER;
1002 def_thread.create_fsync = DEF_CREATE_FSYNC;
1003 def_thread.loops = DEF_LOOPS;
1004 def_thread.verify = DEF_VERIFY;
1005 def_thread.stonewall = DEF_STONEWALL;
1006 def_thread.numjobs = DEF_NUMJOBS;
1007 def_thread.use_thread = DEF_USE_THREAD;
Jens Axboea6ccc7b2006-06-02 10:14:15 +02001008 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
1009 def_thread.rwmixread = DEF_RWMIX_READ;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001010 def_thread.nice = DEF_NICE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001011 def_thread.rand_repeatable = DEF_RAND_REPEAT;
Jens Axboeebac4652005-12-08 15:25:21 +01001012#ifdef FIO_HAVE_DISK_UTIL
1013 def_thread.do_disk_util = 1;
1014#endif
1015
1016 return 0;
1017}
1018
Jens Axboe4785f992006-05-26 03:59:10 +02001019static void usage(char *name)
1020{
1021 printf("%s\n", fio_version_string);
Jens Axboe47623c72006-06-12 10:23:28 +02001022 printf("\t-o Write output to file\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001023 printf("\t-t Runtime in seconds\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001024 printf("\t-l Generate per-job latency logs\n");
1025 printf("\t-w Generate per-job bandwidth logs\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001026 printf("\t-m Minimal (terse) output\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001027 printf("\t-f Job file (Required)\n");
1028 printf("\t-v Print version info and exit\n");
1029}
1030
Jens Axboe972cfd22006-06-09 11:08:56 +02001031static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +01001032{
Jens Axboe972cfd22006-06-09 11:08:56 +02001033 int c, idx = 1, ini_idx = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001034
Jens Axboec6ae0a52006-06-12 11:04:44 +02001035 while ((c = getopt(argc, argv, "t:o:f:lwvhm")) != EOF) {
Jens Axboeebac4652005-12-08 15:25:21 +01001036 switch (c) {
Jens Axboeebac4652005-12-08 15:25:21 +01001037 case 't':
Jens Axboe972cfd22006-06-09 11:08:56 +02001038 def_timeout = atoi(optarg);
1039 idx++;
Jens Axboeebac4652005-12-08 15:25:21 +01001040 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001041 case 'f':
Jens Axboe972cfd22006-06-09 11:08:56 +02001042 ini_idx++;
1043 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1044 ini_file[ini_idx - 1] = strdup(optarg);
1045 idx++;
Jens Axboeebac4652005-12-08 15:25:21 +01001046 break;
1047 case 'l':
1048 write_lat_log = 1;
Jens Axboe972cfd22006-06-09 11:08:56 +02001049 idx++;
Jens Axboeebac4652005-12-08 15:25:21 +01001050 break;
1051 case 'w':
1052 write_bw_log = 1;
Jens Axboe972cfd22006-06-09 11:08:56 +02001053 idx++;
Jens Axboeebac4652005-12-08 15:25:21 +01001054 break;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001055 case 'o':
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001056 f_out = fopen(optarg, "w+");
1057 if (!f_out) {
1058 perror("fopen output");
1059 exit(1);
1060 }
1061 f_err = f_out;
Jens Axboe972cfd22006-06-09 11:08:56 +02001062 idx++;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001063 break;
Jens Axboec6ae0a52006-06-12 11:04:44 +02001064 case 'm':
1065 terse_output = 1;
1066 idx++;
1067 break;
Jens Axboe4785f992006-05-26 03:59:10 +02001068 case 'h':
1069 usage(argv[0]);
1070 exit(0);
Jens Axboeebac4652005-12-08 15:25:21 +01001071 case 'v':
1072 printf("%s\n", fio_version_string);
1073 exit(0);
1074 }
1075 }
Jens Axboec9fad892006-05-27 17:26:50 +02001076
Jens Axboe972cfd22006-06-09 11:08:56 +02001077 while (idx < argc) {
1078 ini_idx++;
1079 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1080 ini_file[ini_idx - 1] = strdup(argv[idx]);
1081 idx++;
1082 }
1083
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001084 if (!f_out) {
1085 f_out = stdout;
1086 f_err = stderr;
1087 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001088
1089 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +01001090}
1091
1092static void free_shm(void)
1093{
1094 struct shmid_ds sbuf;
1095
1096 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001097 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001098 threads = NULL;
1099 shmctl(shm_id, IPC_RMID, &sbuf);
1100 }
1101}
1102
Jens Axboe906c8d72006-06-13 09:37:56 +02001103/*
1104 * The thread area is shared between the main process and the job
1105 * threads/processes. So setup a shared memory segment that will hold
1106 * all the job info.
1107 */
Jens Axboeebac4652005-12-08 15:25:21 +01001108static int setup_thread_area(void)
1109{
1110 /*
1111 * 1024 is too much on some machines, scale max_jobs if
1112 * we get a failure that looks like too large a shm segment
1113 */
1114 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001115 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001116
Jens Axboe906c8d72006-06-13 09:37:56 +02001117 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001118 if (shm_id != -1)
1119 break;
1120 if (errno != EINVAL) {
1121 perror("shmget");
1122 break;
1123 }
1124
1125 max_jobs >>= 1;
1126 } while (max_jobs);
1127
1128 if (shm_id == -1)
1129 return 1;
1130
1131 threads = shmat(shm_id, NULL, 0);
1132 if (threads == (void *) -1) {
1133 perror("shmat");
1134 return 1;
1135 }
1136
1137 atexit(free_shm);
1138 return 0;
1139}
1140
1141int parse_options(int argc, char *argv[])
1142{
Jens Axboe972cfd22006-06-09 11:08:56 +02001143 int job_files, i;
1144
Jens Axboeebac4652005-12-08 15:25:21 +01001145 if (setup_thread_area())
1146 return 1;
1147 if (fill_def_thread())
1148 return 1;
1149
Jens Axboe972cfd22006-06-09 11:08:56 +02001150 job_files = parse_cmd_line(argc, argv);
1151 if (!job_files) {
1152 log_err("Need job file(s)\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001153 usage(argv[0]);
Jens Axboeebac4652005-12-08 15:25:21 +01001154 return 1;
1155 }
1156
Jens Axboe972cfd22006-06-09 11:08:56 +02001157 for (i = 0; i < job_files; i++) {
1158 if (fill_def_thread())
1159 return 1;
1160 if (parse_jobs_ini(ini_file[i]))
1161 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001162 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001163 }
Jens Axboeebac4652005-12-08 15:25:21 +01001164
Jens Axboe88c6ed82006-06-09 11:28:10 +02001165 free(ini_file);
Jens Axboeebac4652005-12-08 15:25:21 +01001166 return 0;
1167}