blob: 20dd625dfe3bb1a77b1209916f7b7dc754a19a37 [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 Axboe53cdc682006-10-18 11:50:58 +020049#define DEF_NR_FILES (1)
Jens Axboeebac4652005-12-08 15:25:21 +010050
Jens Axboe972cfd22006-06-09 11:08:56 +020051static int def_timeout = DEF_TIMEOUT;
Jens Axboe972cfd22006-06-09 11:08:56 +020052
Jens Axboef4866ec2006-06-13 09:38:36 +020053static char fio_version_string[] = "fio 1.5";
Jens Axboeebac4652005-12-08 15:25:21 +010054
Jens Axboe972cfd22006-06-09 11:08:56 +020055static char **ini_file;
Jens Axboeebac4652005-12-08 15:25:21 +010056static int max_jobs = MAX_JOBS;
57
58struct thread_data def_thread;
59struct thread_data *threads = NULL;
60
61int rate_quit = 0;
62int write_lat_log = 0;
63int write_bw_log = 0;
64int exitall_on_terminate = 0;
Jens Axboec6ae0a52006-06-12 11:04:44 +020065int terse_output = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +020066unsigned long long mlock_size = 0;
Jens Axboeeb8bbf42006-06-08 21:40:11 +020067FILE *f_out = NULL;
68FILE *f_err = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +010069
Jens Axboe906c8d72006-06-13 09:37:56 +020070/*
71 * Return a free job structure.
72 */
Jens Axboeebac4652005-12-08 15:25:21 +010073static struct thread_data *get_new_job(int global, struct thread_data *parent)
74{
75 struct thread_data *td;
76
77 if (global)
78 return &def_thread;
79 if (thread_number >= max_jobs)
80 return NULL;
81
82 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +020083 *td = *parent;
84 td->name[0] = '\0';
Jens Axboeebac4652005-12-08 15:25:21 +010085
Jens Axboeebac4652005-12-08 15:25:21 +010086 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;
Jens Axboe53cdc682006-10-18 11:50:58 +0200106 int numjobs, ddir, i;
107 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100108
109#ifndef FIO_HAVE_LIBAIO
110 if (td->io_engine == FIO_LIBAIO) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200111 log_err("Linux libaio not available\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100112 return 1;
113 }
114#endif
115#ifndef FIO_HAVE_POSIXAIO
116 if (td->io_engine == FIO_POSIXAIO) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200117 log_err("posix aio not available\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100118 return 1;
119 }
120#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100121
122 /*
123 * the def_thread is just for options, it's not a real job
124 */
125 if (td == &def_thread)
126 return 0;
127
Jens Axboedf641192006-10-12 07:55:41 +0200128 /*
129 * Set default io engine, if none set
130 */
131 if (!td->io_ops) {
132 td->io_ops = load_ioengine(td, DEF_IO_ENGINE_NAME);
133 if (!td->io_ops) {
134 log_err("default engine %s not there?\n", DEF_IO_ENGINE_NAME);
135 return 1;
136 }
137 }
138
Jens Axboe2866c822006-10-09 15:57:48 +0200139 if (td->io_ops->flags & FIO_SYNCIO)
Jens Axboeebac4652005-12-08 15:25:21 +0100140 td->iodepth = 1;
141 else {
142 if (!td->iodepth)
143 td->iodepth = 1;
144 }
145
Jens Axboe20dc95c2005-12-09 10:29:35 +0100146 /*
Jens Axboe53cdc682006-10-18 11:50:58 +0200147 * only really works for sequential io for now, and with 1 file
Jens Axboe20dc95c2005-12-09 10:29:35 +0100148 */
Jens Axboe53cdc682006-10-18 11:50:58 +0200149 if (td->zone_size && !td->sequential && td->nr_files == 1)
Jens Axboe20dc95c2005-12-09 10:29:35 +0100150 td->zone_size = 0;
151
Jens Axboe9cc935a2006-06-13 09:11:18 +0200152 /*
153 * Reads can do overwrites, we always need to pre-create the file
154 */
155 if (td_read(td) || td_rw(td))
156 td->overwrite = 1;
157
Jens Axboeebac4652005-12-08 15:25:21 +0100158 td->filetype = FIO_TYPE_FILE;
Jens Axboe0af7b542006-02-17 10:10:12 +0100159 if (!stat(jobname, &sb)) {
160 if (S_ISBLK(sb.st_mode))
161 td->filetype = FIO_TYPE_BD;
162 else if (S_ISCHR(sb.st_mode))
163 td->filetype = FIO_TYPE_CHAR;
164 }
Jens Axboeebac4652005-12-08 15:25:21 +0100165
Jens Axboeb2a15192006-10-18 14:10:42 +0200166 if (td->odirect)
167 td->io_ops->flags |= FIO_RAWIO;
168
Jens Axboeebac4652005-12-08 15:25:21 +0100169 if (td->filetype == FIO_TYPE_FILE) {
Jens Axboee9c047a2006-06-07 21:13:04 +0200170 char tmp[PATH_MAX];
Jens Axboe53cdc682006-10-18 11:50:58 +0200171 int len = 0;
172 int i;
Jens Axboee9c047a2006-06-07 21:13:04 +0200173
Jens Axboeef899b62006-06-06 09:31:00 +0200174 if (td->directory && td->directory[0] != '\0')
Jens Axboe53cdc682006-10-18 11:50:58 +0200175 sprintf(tmp, "%s/", td->directory);
Jens Axboeebac4652005-12-08 15:25:21 +0100176
Jens Axboe53cdc682006-10-18 11:50:58 +0200177 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
178
179 for_each_file(td, f, i) {
180 memset(f, 0, sizeof(*f));
181 f->fd = -1;
Jens Axboeb2a15192006-10-18 14:10:42 +0200182 f->fileno = i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200183
184 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
185 f->file_name = strdup(tmp);
186 }
187 } else {
188 td->nr_files = 1;
189 td->files = malloc(sizeof(struct fio_file));
190 f = &td->files[0];
191
192 memset(f, 0, sizeof(*f));
193 f->fd = -1;
194 f->file_name = strdup(jobname);
195 }
196
197 for_each_file(td, f, i) {
198 f->file_size = td->total_file_size / td->nr_files;
199 f->file_offset = td->start_offset;
200 }
201
Jens Axboebbfd6b02006-06-07 19:42:54 +0200202 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100203
204 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
205 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
206 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
207
208 if (td->min_bs == -1U)
209 td->min_bs = td->bs;
210 if (td->max_bs == -1U)
211 td->max_bs = td->bs;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200212 if (td_read(td) && !td_rw(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100213 td->verify = 0;
214
215 if (td->stonewall && td->thread_number > 1)
216 groupid++;
217
218 td->groupid = groupid;
219
220 if (setup_rate(td))
221 goto err;
222
223 if (write_lat_log) {
224 setup_log(&td->slat_log);
225 setup_log(&td->clat_log);
226 }
227 if (write_bw_log)
228 setup_log(&td->bw_log);
229
Jens Axboe01452052006-06-07 10:29:47 +0200230 if (td->name[0] == '\0')
231 snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
232
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200233 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
Jens Axboe75154842006-06-01 13:56:09 +0200234
Jens Axboec6ae0a52006-06-12 11:04:44 +0200235 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200236 if (!job_add_num) {
Jens Axboe2866c822006-10-09 15:57:48 +0200237 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200238 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
239 else
Jens Axboe2866c822006-10-09 15:57:48 +0200240 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_ops->name, td->iodepth);
Jens Axboeb990b5c2006-09-14 09:48:22 +0200241 } else if (job_add_num == 1)
Jens Axboec6ae0a52006-06-12 11:04:44 +0200242 fprintf(f_out, "...\n");
243 }
Jens Axboeebac4652005-12-08 15:25:21 +0100244
245 /*
246 * recurse add identical jobs, clear numjobs and stonewall options
247 * as they don't apply to sub-jobs
248 */
249 numjobs = td->numjobs;
250 while (--numjobs) {
251 struct thread_data *td_new = get_new_job(0, td);
252
253 if (!td_new)
254 goto err;
255
256 td_new->numjobs = 1;
257 td_new->stonewall = 0;
Jens Axboe75154842006-06-01 13:56:09 +0200258 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100259
Jens Axboe75154842006-06-01 13:56:09 +0200260 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100261 goto err;
262 }
263 return 0;
264err:
265 put_job(td);
266 return -1;
267}
268
Jens Axboe906c8d72006-06-13 09:37:56 +0200269/*
270 * Initialize the various random states we need (random io, block size ranges,
271 * read/write mix, etc).
272 */
Jens Axboeebac4652005-12-08 15:25:21 +0100273int init_random_state(struct thread_data *td)
274{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200275 unsigned long seeds[4];
Jens Axboe53cdc682006-10-18 11:50:58 +0200276 int fd, num_maps, blocks, i;
Jens Axboeebac4652005-12-08 15:25:21 +0100277
Jens Axboe1ac267b2006-05-31 20:29:00 +0200278 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100279 if (fd == -1) {
280 td_verror(td, errno);
281 return 1;
282 }
283
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200284 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100285 td_verror(td, EIO);
286 close(fd);
287 return 1;
288 }
289
290 close(fd);
291
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200292 os_random_seed(seeds[0], &td->bsrange_state);
293 os_random_seed(seeds[1], &td->verify_state);
294 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100295
296 if (td->sequential)
297 return 0;
298
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200299 if (td->rand_repeatable)
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200300 seeds[3] = DEF_RANDSEED;
Jens Axboeebac4652005-12-08 15:25:21 +0100301
Jens Axboe53cdc682006-10-18 11:50:58 +0200302 for (i = 0; i < td->nr_files; i++) {
303 struct fio_file *f = &td->files[i];
304
305 blocks = (f->file_size + td->min_bs - 1) / td->min_bs;
306 num_maps = blocks / BLOCKS_PER_MAP;
307 f->file_map = malloc(num_maps * sizeof(long));
308 f->num_maps = num_maps;
309 memset(f->file_map, 0, num_maps * sizeof(long));
310 }
Jens Axboeebac4652005-12-08 15:25:21 +0100311
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200312 os_random_seed(seeds[3], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100313 return 0;
314}
315
316static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
317{
318#ifdef FIO_HAVE_CPU_AFFINITY
319 unsigned int i;
320
321 CPU_ZERO(&cpumask);
322
323 for (i = 0; i < sizeof(int) * 8; i++) {
324 if ((1 << i) & cpu)
325 CPU_SET(i, &cpumask);
326 }
327#endif
328}
329
Jens Axboe906c8d72006-06-13 09:37:56 +0200330static unsigned long get_mult_time(char c)
331{
332 switch (c) {
333 case 'm':
334 case 'M':
335 return 60;
336 case 'h':
337 case 'H':
338 return 60 * 60;
339 case 'd':
340 case 'D':
341 return 24 * 60 * 60;
342 default:
343 return 1;
344 }
345}
346
347static unsigned long get_mult_bytes(char c)
Jens Axboeebac4652005-12-08 15:25:21 +0100348{
349 switch (c) {
350 case 'k':
351 case 'K':
352 return 1024;
353 case 'm':
354 case 'M':
355 return 1024 * 1024;
356 case 'g':
357 case 'G':
358 return 1024 * 1024 * 1024;
359 default:
360 return 1;
361 }
362}
363
364/*
365 * convert string after '=' into decimal value, noting any size suffix
366 */
Jens Axboe906c8d72006-06-13 09:37:56 +0200367static int str_to_decimal(char *p, unsigned long long *val, int kilo)
Jens Axboeebac4652005-12-08 15:25:21 +0100368{
369 char *str;
370 int len;
371
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100372 str = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100373 if (!str)
374 return 1;
375
376 str++;
377 len = strlen(str);
378
379 *val = strtoul(str, NULL, 10);
380 if (*val == ULONG_MAX && errno == ERANGE)
381 return 1;
382
Jens Axboe906c8d72006-06-13 09:37:56 +0200383 if (kilo)
384 *val *= get_mult_bytes(str[len - 1]);
385 else
386 *val *= get_mult_time(str[len - 1]);
Jens Axboeebac4652005-12-08 15:25:21 +0100387 return 0;
388}
389
Jens Axboe906c8d72006-06-13 09:37:56 +0200390static int check_str_bytes(char *p, char *name, unsigned long long *val)
Jens Axboeebac4652005-12-08 15:25:21 +0100391{
Jens Axboe20dc95c2005-12-09 10:29:35 +0100392 if (strncmp(p, name, strlen(name) - 1))
Jens Axboeebac4652005-12-08 15:25:21 +0100393 return 1;
394
Jens Axboe906c8d72006-06-13 09:37:56 +0200395 return str_to_decimal(p, val, 1);
396}
397
398static int check_str_time(char *p, char *name, unsigned long long *val)
399{
400 if (strncmp(p, name, strlen(name) - 1))
401 return 1;
402
403 return str_to_decimal(p, val, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100404}
405
406static void strip_blank_front(char **p)
407{
408 char *s = *p;
409
Jens Axboe4ae3f762006-05-30 13:35:14 +0200410 while (isspace(*s))
Jens Axboeebac4652005-12-08 15:25:21 +0100411 s++;
412}
413
414static void strip_blank_end(char *p)
415{
Jens Axboe4ae3f762006-05-30 13:35:14 +0200416 char *s = p + strlen(p) - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100417
Jens Axboe4ae3f762006-05-30 13:35:14 +0200418 while (isspace(*s) || iscntrl(*s))
419 s--;
Jens Axboeaea47d42006-05-26 19:27:29 +0200420
Jens Axboe4ae3f762006-05-30 13:35:14 +0200421 *(s + 1) = '\0';
Jens Axboeaea47d42006-05-26 19:27:29 +0200422}
423
Jens Axboeebac4652005-12-08 15:25:21 +0100424typedef int (str_cb_fn)(struct thread_data *, char *);
425
426static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
427{
Jens Axboe843a7412006-06-01 21:14:21 -0700428 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100429
Jens Axboe843a7412006-06-01 21:14:21 -0700430 if (strncmp(p, name, strlen(name)))
431 return 1;
432
433 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100434 if (!s)
435 return 1;
436
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100437 s = strchr(s, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100438 if (!s)
439 return 1;
440
441 s++;
442 strip_blank_front(&s);
443 return cb(td, s);
444}
445
446static int check_strstore(char *p, char *name, char *dest)
447{
Jens Axboe843a7412006-06-01 21:14:21 -0700448 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100449
Jens Axboe843a7412006-06-01 21:14:21 -0700450 if (strncmp(p, name, strlen(name)))
451 return 1;
452
453 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100454 if (!s)
455 return 1;
456
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100457 s = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100458 if (!s)
459 return 1;
460
461 s++;
462 strip_blank_front(&s);
463
464 strcpy(dest, s);
Jens Axboeebac4652005-12-08 15:25:21 +0100465 return 0;
466}
467
Jens Axboe906c8d72006-06-13 09:37:56 +0200468static int __check_range_bytes(char *str, unsigned long *val)
Jens Axboeebac4652005-12-08 15:25:21 +0100469{
Jens Axboe01617be2005-12-08 19:50:40 +0100470 char suffix;
Jens Axboeebac4652005-12-08 15:25:21 +0100471
Jens Axboe01617be2005-12-08 19:50:40 +0100472 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
Jens Axboe906c8d72006-06-13 09:37:56 +0200473 *val *= get_mult_bytes(suffix);
Jens Axboeebac4652005-12-08 15:25:21 +0100474 return 0;
475 }
476
Jens Axboe01617be2005-12-08 19:50:40 +0100477 if (sscanf(str, "%lu", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100478 return 0;
479
480 return 1;
Jens Axboe01617be2005-12-08 19:50:40 +0100481}
Jens Axboeebac4652005-12-08 15:25:21 +0100482
Jens Axboe906c8d72006-06-13 09:37:56 +0200483static int check_range_bytes(char *p, char *name, unsigned long *s,
484 unsigned long *e)
Jens Axboe01617be2005-12-08 19:50:40 +0100485{
486 char option[128];
487 char *str, *p1, *p2;
488
Jens Axboe843a7412006-06-01 21:14:21 -0700489 if (strncmp(p, name, strlen(name)))
490 return 1;
491
Jens Axboe01617be2005-12-08 19:50:40 +0100492 strcpy(option, p);
493 p = option;
494
495 str = strstr(p, name);
496 if (!str)
497 return 1;
498
499 p += strlen(name);
500
501 str = strchr(p, '=');
502 if (!str)
503 return 1;
504
505 /*
506 * 'p' now holds whatever is after the '=' sign
507 */
508 p1 = str + 1;
509
510 /*
511 * terminate p1 at the '-' sign
512 */
513 p = strchr(p1, '-');
514 if (!p)
515 return 1;
516
517 p2 = p + 1;
518 *p = '\0';
519
Jens Axboe906c8d72006-06-13 09:37:56 +0200520 if (!__check_range_bytes(p1, s) && !__check_range_bytes(p2, e))
Jens Axboe01617be2005-12-08 19:50:40 +0100521 return 0;
522
523 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100524}
525
526static int check_int(char *p, char *name, unsigned int *val)
527{
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100528 char *str;
Jens Axboeebac4652005-12-08 15:25:21 +0100529
Jens Axboeb6754f92006-05-30 14:29:32 +0200530 if (strncmp(p, name, strlen(name)))
531 return 1;
532
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100533 str = strstr(p, name);
534 if (!str)
535 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100536
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100537 str = strchr(p, '=');
538 if (!str)
539 return 1;
540
541 str++;
542
543 if (sscanf(str, "%u", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100544 return 0;
545
546 return 1;
547}
548
549static int check_strset(char *p, char *name)
550{
551 return strncmp(p, name, strlen(name));
552}
553
554static int is_empty_or_comment(char *line)
555{
556 unsigned int i;
557
558 for (i = 0; i < strlen(line); i++) {
559 if (line[i] == ';')
560 return 1;
561 if (!isspace(line[i]) && !iscntrl(line[i]))
562 return 0;
563 }
564
565 return 1;
566}
567
568static int str_rw_cb(struct thread_data *td, char *mem)
569{
570 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
571 td->ddir = DDIR_READ;
572 td->sequential = 1;
573 return 0;
574 } else if (!strncmp(mem, "randread", 8)) {
575 td->ddir = DDIR_READ;
576 td->sequential = 0;
577 return 0;
578 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
579 td->ddir = DDIR_WRITE;
580 td->sequential = 1;
581 return 0;
582 } else if (!strncmp(mem, "randwrite", 9)) {
583 td->ddir = DDIR_WRITE;
584 td->sequential = 0;
585 return 0;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200586 } else if (!strncmp(mem, "rw", 2)) {
587 td->ddir = 0;
588 td->iomix = 1;
589 td->sequential = 1;
590 return 0;
591 } else if (!strncmp(mem, "randrw", 6)) {
592 td->ddir = 0;
593 td->iomix = 1;
594 td->sequential = 0;
595 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100596 }
597
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200598 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100599 return 1;
600}
601
602static int str_verify_cb(struct thread_data *td, char *mem)
603{
604 if (!strncmp(mem, "0", 1)) {
605 td->verify = VERIFY_NONE;
606 return 0;
607 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
608 td->verify = VERIFY_MD5;
609 return 0;
610 } else if (!strncmp(mem, "crc32", 5)) {
611 td->verify = VERIFY_CRC32;
612 return 0;
613 }
614
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200615 log_err("fio: verify types: md5, crc32\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100616 return 1;
617}
618
619static int str_mem_cb(struct thread_data *td, char *mem)
620{
621 if (!strncmp(mem, "malloc", 6)) {
622 td->mem_type = MEM_MALLOC;
623 return 0;
624 } else if (!strncmp(mem, "shm", 3)) {
625 td->mem_type = MEM_SHM;
626 return 0;
627 } else if (!strncmp(mem, "mmap", 4)) {
628 td->mem_type = MEM_MMAP;
629 return 0;
630 }
631
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200632 log_err("fio: mem type: malloc, shm, mmap\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100633 return 1;
634}
635
636static int str_ioengine_cb(struct thread_data *td, char *str)
637{
Jens Axboe2866c822006-10-09 15:57:48 +0200638 td->io_ops = load_ioengine(td, str);
639 if (td->io_ops)
Jens Axboeebac4652005-12-08 15:25:21 +0100640 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100641
Jens Axboeb990b5c2006-09-14 09:48:22 +0200642 log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice, cpu\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100643 return 1;
644}
645
Jens Axboe07261982006-06-07 10:51:12 +0200646/*
647 * This is our [ini] type file parser.
648 */
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200649int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100650{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200651 unsigned int prioclass, prio, cpu, global, il;
Jens Axboeebac4652005-12-08 15:25:21 +0100652 unsigned long long ull;
653 unsigned long ul1, ul2;
654 struct thread_data *td;
Jens Axboeef899b62006-06-06 09:31:00 +0200655 char *string, *name, *tmpbuf;
Jens Axboeebac4652005-12-08 15:25:21 +0100656 fpos_t off;
657 FILE *f;
658 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200659 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +0100660
661 f = fopen(file, "r");
662 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200663 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100664 return 1;
665 }
666
667 string = malloc(4096);
668 name = malloc(256);
Jens Axboeef899b62006-06-06 09:31:00 +0200669 tmpbuf = malloc(4096);
Jens Axboeebac4652005-12-08 15:25:21 +0100670
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200671 stonewall = stonewall_flag;
Jens Axboeebac4652005-12-08 15:25:21 +0100672 while ((p = fgets(string, 4096, f)) != NULL) {
Jens Axboe45410ac2006-06-07 11:10:39 +0200673 if (ret)
674 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100675 if (is_empty_or_comment(p))
676 continue;
677 if (sscanf(p, "[%s]", name) != 1)
678 continue;
679
680 global = !strncmp(name, "global", 6);
681
682 name[strlen(name) - 1] = '\0';
683
684 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200685 if (!td) {
686 ret = 1;
687 break;
688 }
Jens Axboeebac4652005-12-08 15:25:21 +0100689
Jens Axboe972cfd22006-06-09 11:08:56 +0200690 /*
691 * Seperate multiple job files by a stonewall
692 */
Jens Axboef9481912006-06-09 11:37:28 +0200693 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +0200694 td->stonewall = stonewall;
695 stonewall = 0;
696 }
697
Jens Axboeebac4652005-12-08 15:25:21 +0100698 fgetpos(f, &off);
699 while ((p = fgets(string, 4096, f)) != NULL) {
700 if (is_empty_or_comment(p))
701 continue;
702 if (strstr(p, "["))
703 break;
Jens Axboeb6754f92006-05-30 14:29:32 +0200704 strip_blank_front(&p);
Jens Axboe4ae3f762006-05-30 13:35:14 +0200705 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200706
Jens Axboeebac4652005-12-08 15:25:21 +0100707 if (!check_int(p, "prio", &prio)) {
708#ifndef FIO_HAVE_IOPRIO
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200709 log_err("io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200710 ret = 1;
711 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100712#endif
Jens Axboe8756e4d2006-05-27 20:24:53 +0200713 td->ioprio |= prio;
Jens Axboeebac4652005-12-08 15:25:21 +0100714 fgetpos(f, &off);
715 continue;
716 }
717 if (!check_int(p, "prioclass", &prioclass)) {
718#ifndef FIO_HAVE_IOPRIO
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200719 log_err("io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200720 ret = 1;
721 break;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200722#else
Jens Axboe8756e4d2006-05-27 20:24:53 +0200723 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
Jens Axboeebac4652005-12-08 15:25:21 +0100724 fgetpos(f, &off);
725 continue;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200726#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100727 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200728 if (!check_int(p, "direct", &il)) {
729 td->odirect = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100730 fgetpos(f, &off);
731 continue;
732 }
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200733 if (!check_int(p, "rand_repeatable", &il)) {
734 td->rand_repeatable = il;
735 fgetpos(f, &off);
736 continue;
737 }
Jens Axboeebac4652005-12-08 15:25:21 +0100738 if (!check_int(p, "rate", &td->rate)) {
739 fgetpos(f, &off);
740 continue;
741 }
742 if (!check_int(p, "ratemin", &td->ratemin)) {
743 fgetpos(f, &off);
744 continue;
745 }
746 if (!check_int(p, "ratecycle", &td->ratecycle)) {
747 fgetpos(f, &off);
748 continue;
749 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200750 if (!check_int(p, "cpuload", &td->cpuload)) {
751 fgetpos(f, &off);
752 continue;
753 }
754 if (!check_int(p, "cpuchunks", &td->cpucycle)) {
755 fgetpos(f, &off);
756 continue;
757 }
Jens Axboeebac4652005-12-08 15:25:21 +0100758 if (!check_int(p, "thinktime", &td->thinktime)) {
759 fgetpos(f, &off);
760 continue;
761 }
762 if (!check_int(p, "cpumask", &cpu)) {
763#ifndef FIO_HAVE_CPU_AFFINITY
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200764 log_err("cpu affinity not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200765 ret = 1;
766 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100767#endif
768 fill_cpu_mask(td->cpumask, cpu);
769 fgetpos(f, &off);
770 continue;
771 }
772 if (!check_int(p, "fsync", &td->fsync_blocks)) {
773 fgetpos(f, &off);
Jens Axboefc1a4712006-05-30 13:04:05 +0200774 td->end_fsync = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100775 continue;
776 }
777 if (!check_int(p, "startdelay", &td->start_delay)) {
778 fgetpos(f, &off);
779 continue;
780 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200781 if (!check_str_time(p, "timeout", &ull)) {
Jens Axboeaf959a72006-07-13 14:36:02 -0700782 td->timeout = ull;
Jens Axboeebac4652005-12-08 15:25:21 +0100783 fgetpos(f, &off);
784 continue;
785 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200786 if (!check_int(p, "invalidate", &il)) {
787 td->invalidate_cache = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100788 fgetpos(f, &off);
789 continue;
790 }
791 if (!check_int(p, "iodepth", &td->iodepth)) {
792 fgetpos(f, &off);
793 continue;
794 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200795 if (!check_int(p, "sync", &il)) {
796 td->sync_io = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100797 fgetpos(f, &off);
798 continue;
799 }
800 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
801 fgetpos(f, &off);
802 continue;
803 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200804 if (!check_int(p, "create_serialize", &il)) {
805 td->create_serialize = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100806 fgetpos(f, &off);
807 continue;
808 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200809 if (!check_int(p, "create_fsync", &il)) {
810 td->create_fsync = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100811 fgetpos(f, &off);
812 continue;
813 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200814 if (!check_int(p, "end_fsync", &il)) {
815 td->end_fsync = il;
Jens Axboefc1a4712006-05-30 13:04:05 +0200816 fgetpos(f, &off);
817 continue;
818 }
Jens Axboeebac4652005-12-08 15:25:21 +0100819 if (!check_int(p, "loops", &td->loops)) {
820 fgetpos(f, &off);
821 continue;
822 }
823 if (!check_int(p, "numjobs", &td->numjobs)) {
824 fgetpos(f, &off);
825 continue;
826 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200827 if (!check_int(p, "overwrite", &il)) {
828 td->overwrite = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100829 fgetpos(f, &off);
830 continue;
831 }
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200832 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
833 fgetpos(f, &off);
834 continue;
835 }
836 if (!check_int(p, "rwmixread", &il)) {
837 if (il > 100)
838 il = 100;
839 td->rwmixread = il;
840 fgetpos(f, &off);
841 continue;
842 }
843 if (!check_int(p, "rwmixwrite", &il)) {
844 if (il > 100)
845 il = 100;
846 td->rwmixread = 100 - il;
847 fgetpos(f, &off);
848 continue;
849 }
Jens Axboeb6f4d882006-06-02 10:32:51 +0200850 if (!check_int(p, "nice", &td->nice)) {
851 fgetpos(f, &off);
852 continue;
853 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200854 if (!check_int(p, "nrfiles", &td->nr_files)) {
855 fgetpos(f, &off);
856 continue;
857 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200858 if (!check_range_bytes(p, "bsrange", &ul1, &ul2)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100859 if (ul1 > ul2) {
860 td->max_bs = ul1;
861 td->min_bs = ul2;
862 } else {
863 td->max_bs = ul2;
864 td->min_bs = ul1;
865 }
866 fgetpos(f, &off);
867 continue;
868 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200869 if (!check_str_bytes(p, "bs", &ull)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100870 td->bs = ull;
871 fgetpos(f, &off);
872 continue;
873 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200874 if (!check_str_bytes(p, "size", &td->total_file_size)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100875 fgetpos(f, &off);
876 continue;
877 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200878 if (!check_str_bytes(p, "offset", &td->start_offset)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100879 fgetpos(f, &off);
880 continue;
881 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200882 if (!check_str_bytes(p, "zonesize", &td->zone_size)) {
Jens Axboe20dc95c2005-12-09 10:29:35 +0100883 fgetpos(f, &off);
884 continue;
885 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200886 if (!check_str_bytes(p, "zoneskip", &td->zone_skip)) {
Jens Axboe20dc95c2005-12-09 10:29:35 +0100887 fgetpos(f, &off);
888 continue;
889 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200890 if (!check_str_bytes(p, "lockmem", &mlock_size)) {
Jens Axboec04f7ec2006-05-31 10:13:16 +0200891 fgetpos(f, &off);
892 continue;
893 }
Jens Axboeef899b62006-06-06 09:31:00 +0200894 if (!check_strstore(p, "directory", tmpbuf)) {
895 td->directory = strdup(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100896 fgetpos(f, &off);
897 continue;
898 }
Jens Axboe01452052006-06-07 10:29:47 +0200899 if (!check_strstore(p, "name", tmpbuf)) {
900 snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
901 fgetpos(f, &off);
902 continue;
903 }
Jens Axboeebac4652005-12-08 15:25:21 +0100904 if (!check_str(p, "mem", str_mem_cb, td)) {
905 fgetpos(f, &off);
906 continue;
907 }
908 if (!check_str(p, "verify", str_verify_cb, td)) {
909 fgetpos(f, &off);
910 continue;
911 }
912 if (!check_str(p, "rw", str_rw_cb, td)) {
913 fgetpos(f, &off);
914 continue;
915 }
916 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
917 fgetpos(f, &off);
918 continue;
919 }
920 if (!check_strset(p, "create")) {
921 td->create_file = 1;
922 fgetpos(f, &off);
923 continue;
924 }
925 if (!check_strset(p, "exitall")) {
926 exitall_on_terminate = 1;
927 fgetpos(f, &off);
928 continue;
929 }
930 if (!check_strset(p, "stonewall")) {
931 td->stonewall = 1;
932 fgetpos(f, &off);
933 continue;
934 }
935 if (!check_strset(p, "thread")) {
936 td->use_thread = 1;
937 fgetpos(f, &off);
938 continue;
939 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200940 if (!check_strstore(p, "iolog", tmpbuf)) {
Jens Axboe4f693b92006-06-07 22:52:28 +0200941 if (td->write_iolog) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200942 log_err("fio: read iolog overrides given write_iolog\n");
Jens Axboeaf8c0b42006-06-07 22:33:17 +0200943 free(td->iolog_file);
Jens Axboe4f693b92006-06-07 22:52:28 +0200944 td->write_iolog = 0;
945 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200946 td->iolog_file = strdup(tmpbuf);
Jens Axboe843a7412006-06-01 21:14:21 -0700947 td->read_iolog = 1;
Jens Axboe843a7412006-06-01 21:14:21 -0700948 fgetpos(f, &off);
949 continue;
950 }
Jens Axboeaf8c0b42006-06-07 22:33:17 +0200951 if (!check_strstore(p, "write_iolog", tmpbuf)) {
952 if (!td->read_iolog) {
953 td->iolog_file = strdup(tmpbuf);
954 td->write_iolog = 1;
Jens Axboe4f693b92006-06-07 22:52:28 +0200955 } else
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200956 log_err("fio: read iolog overrides given write_iolog\n");
Jens Axboeaea47d42006-05-26 19:27:29 +0200957 fgetpos(f, &off);
958 continue;
959 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200960 if (!check_strstore(p, "exec_prerun", tmpbuf)) {
961 td->exec_prerun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200962 fgetpos(f, &off);
963 continue;
964 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200965 if (!check_strstore(p, "exec_postrun", tmpbuf)) {
966 td->exec_postrun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200967 fgetpos(f, &off);
968 continue;
969 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200970 if (!check_strstore(p, "ioscheduler", tmpbuf)) {
Jens Axboe22f78b32006-06-07 11:30:07 +0200971#ifndef FIO_HAVE_IOSCHED_SWITCH
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200972 log_err("io scheduler switching not available\n");
Jens Axboe22f78b32006-06-07 11:30:07 +0200973 ret = 1;
974 break;
975#else
Jens Axboe80b9feb2006-06-07 10:34:49 +0200976 td->ioscheduler = strdup(tmpbuf);
Jens Axboeda867742006-06-06 10:39:10 -0700977 fgetpos(f, &off);
978 continue;
Jens Axboe22f78b32006-06-07 11:30:07 +0200979#endif
Jens Axboeda867742006-06-06 10:39:10 -0700980 }
Jens Axboeebac4652005-12-08 15:25:21 +0100981
Jens Axboe45410ac2006-06-07 11:10:39 +0200982 /*
983 * Don't break here, continue parsing options so we
984 * dump all the bad ones. Makes trial/error fixups
985 * easier on the user.
986 */
Jens Axboeebac4652005-12-08 15:25:21 +0100987 printf("Client%d: bad option %s\n",td->thread_number,p);
Jens Axboe45410ac2006-06-07 11:10:39 +0200988 ret = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100989 }
Jens Axboeebac4652005-12-08 15:25:21 +0100990
Jens Axboe45410ac2006-06-07 11:10:39 +0200991 if (!ret) {
992 fsetpos(f, &off);
993 ret = add_job(td, name, 0);
994 }
995 if (ret)
996 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100997 }
998
999 free(string);
1000 free(name);
Jens Axboeef899b62006-06-06 09:31:00 +02001001 free(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +01001002 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +02001003 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001004}
1005
1006static int fill_def_thread(void)
1007{
1008 memset(&def_thread, 0, sizeof(def_thread));
1009
1010 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1011 perror("sched_getaffinity");
1012 return 1;
1013 }
1014
1015 /*
1016 * fill globals
1017 */
1018 def_thread.ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001019 def_thread.iomix = 0;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001020 def_thread.bs = DEF_BS;
Jens Axboeebac4652005-12-08 15:25:21 +01001021 def_thread.min_bs = -1;
1022 def_thread.max_bs = -1;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001023 def_thread.odirect = DEF_ODIRECT;
Jens Axboeebac4652005-12-08 15:25:21 +01001024 def_thread.ratecycle = DEF_RATE_CYCLE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001025 def_thread.sequential = DEF_SEQUENTIAL;
Jens Axboe972cfd22006-06-09 11:08:56 +02001026 def_thread.timeout = def_timeout;
Jens Axboeebac4652005-12-08 15:25:21 +01001027 def_thread.create_file = DEF_CREATE;
1028 def_thread.overwrite = DEF_OVERWRITE;
1029 def_thread.invalidate_cache = DEF_INVALIDATE;
1030 def_thread.sync_io = DEF_SYNCIO;
1031 def_thread.mem_type = MEM_MALLOC;
1032 def_thread.bw_avg_time = DEF_BWAVGTIME;
1033 def_thread.create_serialize = DEF_CREATE_SER;
1034 def_thread.create_fsync = DEF_CREATE_FSYNC;
1035 def_thread.loops = DEF_LOOPS;
1036 def_thread.verify = DEF_VERIFY;
1037 def_thread.stonewall = DEF_STONEWALL;
1038 def_thread.numjobs = DEF_NUMJOBS;
1039 def_thread.use_thread = DEF_USE_THREAD;
Jens Axboea6ccc7b2006-06-02 10:14:15 +02001040 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
1041 def_thread.rwmixread = DEF_RWMIX_READ;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001042 def_thread.nice = DEF_NICE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001043 def_thread.rand_repeatable = DEF_RAND_REPEAT;
Jens Axboe53cdc682006-10-18 11:50:58 +02001044 def_thread.nr_files = DEF_NR_FILES;
Jens Axboeebac4652005-12-08 15:25:21 +01001045#ifdef FIO_HAVE_DISK_UTIL
1046 def_thread.do_disk_util = 1;
1047#endif
1048
1049 return 0;
1050}
1051
Jens Axboe4785f992006-05-26 03:59:10 +02001052static void usage(char *name)
1053{
1054 printf("%s\n", fio_version_string);
Jens Axboe47623c72006-06-12 10:23:28 +02001055 printf("\t-o Write output to file\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001056 printf("\t-t Runtime in seconds\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001057 printf("\t-l Generate per-job latency logs\n");
1058 printf("\t-w Generate per-job bandwidth logs\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001059 printf("\t-m Minimal (terse) output\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001060 printf("\t-v Print version info and exit\n");
1061}
1062
Jens Axboe972cfd22006-06-09 11:08:56 +02001063static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +01001064{
Jens Axboe972cfd22006-06-09 11:08:56 +02001065 int c, idx = 1, ini_idx = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001066
Jens Axboe2e778412006-06-16 10:00:50 +02001067 while ((c = getopt(argc, argv, "t:o:lwvhm")) != EOF) {
Jens Axboeebac4652005-12-08 15:25:21 +01001068 switch (c) {
Jens Axboeebac4652005-12-08 15:25:21 +01001069 case 't':
Jens Axboe972cfd22006-06-09 11:08:56 +02001070 def_timeout = atoi(optarg);
Jens Axboe774a6172006-08-24 08:44:26 +02001071 idx = optind;
Jens Axboeebac4652005-12-08 15:25:21 +01001072 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001073 case 'l':
1074 write_lat_log = 1;
Jens Axboe774a6172006-08-24 08:44:26 +02001075 idx = optind;
Jens Axboeebac4652005-12-08 15:25:21 +01001076 break;
1077 case 'w':
1078 write_bw_log = 1;
Jens Axboe774a6172006-08-24 08:44:26 +02001079 idx = optind;
Jens Axboeebac4652005-12-08 15:25:21 +01001080 break;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001081 case 'o':
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001082 f_out = fopen(optarg, "w+");
1083 if (!f_out) {
1084 perror("fopen output");
1085 exit(1);
1086 }
1087 f_err = f_out;
Jens Axboe774a6172006-08-24 08:44:26 +02001088 idx = optind;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001089 break;
Jens Axboec6ae0a52006-06-12 11:04:44 +02001090 case 'm':
1091 terse_output = 1;
Jens Axboe774a6172006-08-24 08:44:26 +02001092 idx = optind;
Jens Axboec6ae0a52006-06-12 11:04:44 +02001093 break;
Jens Axboe4785f992006-05-26 03:59:10 +02001094 case 'h':
1095 usage(argv[0]);
1096 exit(0);
Jens Axboeebac4652005-12-08 15:25:21 +01001097 case 'v':
1098 printf("%s\n", fio_version_string);
1099 exit(0);
1100 }
1101 }
Jens Axboec9fad892006-05-27 17:26:50 +02001102
Jens Axboe972cfd22006-06-09 11:08:56 +02001103 while (idx < argc) {
1104 ini_idx++;
1105 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1106 ini_file[ini_idx - 1] = strdup(argv[idx]);
1107 idx++;
1108 }
Jens Axboe774a6172006-08-24 08:44:26 +02001109
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001110 if (!f_out) {
1111 f_out = stdout;
1112 f_err = stderr;
1113 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001114
1115 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +01001116}
1117
1118static void free_shm(void)
1119{
1120 struct shmid_ds sbuf;
1121
1122 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001123 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001124 threads = NULL;
1125 shmctl(shm_id, IPC_RMID, &sbuf);
1126 }
1127}
1128
Jens Axboe906c8d72006-06-13 09:37:56 +02001129/*
1130 * The thread area is shared between the main process and the job
1131 * threads/processes. So setup a shared memory segment that will hold
1132 * all the job info.
1133 */
Jens Axboeebac4652005-12-08 15:25:21 +01001134static int setup_thread_area(void)
1135{
1136 /*
1137 * 1024 is too much on some machines, scale max_jobs if
1138 * we get a failure that looks like too large a shm segment
1139 */
1140 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001141 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001142
Jens Axboe906c8d72006-06-13 09:37:56 +02001143 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001144 if (shm_id != -1)
1145 break;
1146 if (errno != EINVAL) {
1147 perror("shmget");
1148 break;
1149 }
1150
1151 max_jobs >>= 1;
1152 } while (max_jobs);
1153
1154 if (shm_id == -1)
1155 return 1;
1156
1157 threads = shmat(shm_id, NULL, 0);
1158 if (threads == (void *) -1) {
1159 perror("shmat");
1160 return 1;
1161 }
1162
1163 atexit(free_shm);
1164 return 0;
1165}
1166
1167int parse_options(int argc, char *argv[])
1168{
Jens Axboe972cfd22006-06-09 11:08:56 +02001169 int job_files, i;
1170
Jens Axboeebac4652005-12-08 15:25:21 +01001171 if (setup_thread_area())
1172 return 1;
1173 if (fill_def_thread())
1174 return 1;
1175
Jens Axboe972cfd22006-06-09 11:08:56 +02001176 job_files = parse_cmd_line(argc, argv);
1177 if (!job_files) {
1178 log_err("Need job file(s)\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001179 usage(argv[0]);
Jens Axboeebac4652005-12-08 15:25:21 +01001180 return 1;
1181 }
1182
Jens Axboe972cfd22006-06-09 11:08:56 +02001183 for (i = 0; i < job_files; i++) {
1184 if (fill_def_thread())
1185 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001186 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001187 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001188 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001189 }
Jens Axboeebac4652005-12-08 15:25:21 +01001190
Jens Axboe88c6ed82006-06-09 11:28:10 +02001191 free(ini_file);
Jens Axboeebac4652005-12-08 15:25:21 +01001192 return 0;
1193}