blob: 1ffd6ff007d1e333a6bcd05704d10f932a86d1c9 [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) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200199 if (!job_add_num) {
200 if (td->io_engine == FIO_CPUIO)
201 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
202 else
203 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);
204 } else if (job_add_num == 1)
Jens Axboec6ae0a52006-06-12 11:04:44 +0200205 fprintf(f_out, "...\n");
206 }
Jens Axboeebac4652005-12-08 15:25:21 +0100207
208 /*
209 * recurse add identical jobs, clear numjobs and stonewall options
210 * as they don't apply to sub-jobs
211 */
212 numjobs = td->numjobs;
213 while (--numjobs) {
214 struct thread_data *td_new = get_new_job(0, td);
215
216 if (!td_new)
217 goto err;
218
219 td_new->numjobs = 1;
220 td_new->stonewall = 0;
Jens Axboe75154842006-06-01 13:56:09 +0200221 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100222
Jens Axboe75154842006-06-01 13:56:09 +0200223 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100224 goto err;
225 }
226 return 0;
227err:
228 put_job(td);
229 return -1;
230}
231
Jens Axboe906c8d72006-06-13 09:37:56 +0200232/*
233 * Initialize the various random states we need (random io, block size ranges,
234 * read/write mix, etc).
235 */
Jens Axboeebac4652005-12-08 15:25:21 +0100236int init_random_state(struct thread_data *td)
237{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200238 unsigned long seeds[4];
Jens Axboeebac4652005-12-08 15:25:21 +0100239 int fd, num_maps, blocks;
240
Jens Axboe1ac267b2006-05-31 20:29:00 +0200241 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100242 if (fd == -1) {
243 td_verror(td, errno);
244 return 1;
245 }
246
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200247 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100248 td_verror(td, EIO);
249 close(fd);
250 return 1;
251 }
252
253 close(fd);
254
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200255 os_random_seed(seeds[0], &td->bsrange_state);
256 os_random_seed(seeds[1], &td->verify_state);
257 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100258
259 if (td->sequential)
260 return 0;
261
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200262 if (td->rand_repeatable)
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200263 seeds[3] = DEF_RANDSEED;
Jens Axboeebac4652005-12-08 15:25:21 +0100264
265 blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
266 num_maps = blocks / BLOCKS_PER_MAP;
267 td->file_map = malloc(num_maps * sizeof(long));
268 td->num_maps = num_maps;
269 memset(td->file_map, 0, num_maps * sizeof(long));
270
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200271 os_random_seed(seeds[3], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100272 return 0;
273}
274
275static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
276{
277#ifdef FIO_HAVE_CPU_AFFINITY
278 unsigned int i;
279
280 CPU_ZERO(&cpumask);
281
282 for (i = 0; i < sizeof(int) * 8; i++) {
283 if ((1 << i) & cpu)
284 CPU_SET(i, &cpumask);
285 }
286#endif
287}
288
Jens Axboe906c8d72006-06-13 09:37:56 +0200289static unsigned long get_mult_time(char c)
290{
291 switch (c) {
292 case 'm':
293 case 'M':
294 return 60;
295 case 'h':
296 case 'H':
297 return 60 * 60;
298 case 'd':
299 case 'D':
300 return 24 * 60 * 60;
301 default:
302 return 1;
303 }
304}
305
306static unsigned long get_mult_bytes(char c)
Jens Axboeebac4652005-12-08 15:25:21 +0100307{
308 switch (c) {
309 case 'k':
310 case 'K':
311 return 1024;
312 case 'm':
313 case 'M':
314 return 1024 * 1024;
315 case 'g':
316 case 'G':
317 return 1024 * 1024 * 1024;
318 default:
319 return 1;
320 }
321}
322
323/*
324 * convert string after '=' into decimal value, noting any size suffix
325 */
Jens Axboe906c8d72006-06-13 09:37:56 +0200326static int str_to_decimal(char *p, unsigned long long *val, int kilo)
Jens Axboeebac4652005-12-08 15:25:21 +0100327{
328 char *str;
329 int len;
330
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100331 str = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100332 if (!str)
333 return 1;
334
335 str++;
336 len = strlen(str);
337
338 *val = strtoul(str, NULL, 10);
339 if (*val == ULONG_MAX && errno == ERANGE)
340 return 1;
341
Jens Axboe906c8d72006-06-13 09:37:56 +0200342 if (kilo)
343 *val *= get_mult_bytes(str[len - 1]);
344 else
345 *val *= get_mult_time(str[len - 1]);
Jens Axboeebac4652005-12-08 15:25:21 +0100346 return 0;
347}
348
Jens Axboe906c8d72006-06-13 09:37:56 +0200349static int check_str_bytes(char *p, char *name, unsigned long long *val)
Jens Axboeebac4652005-12-08 15:25:21 +0100350{
Jens Axboe20dc95c2005-12-09 10:29:35 +0100351 if (strncmp(p, name, strlen(name) - 1))
Jens Axboeebac4652005-12-08 15:25:21 +0100352 return 1;
353
Jens Axboe906c8d72006-06-13 09:37:56 +0200354 return str_to_decimal(p, val, 1);
355}
356
357static int check_str_time(char *p, char *name, unsigned long long *val)
358{
359 if (strncmp(p, name, strlen(name) - 1))
360 return 1;
361
362 return str_to_decimal(p, val, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100363}
364
365static void strip_blank_front(char **p)
366{
367 char *s = *p;
368
Jens Axboe4ae3f762006-05-30 13:35:14 +0200369 while (isspace(*s))
Jens Axboeebac4652005-12-08 15:25:21 +0100370 s++;
371}
372
373static void strip_blank_end(char *p)
374{
Jens Axboe4ae3f762006-05-30 13:35:14 +0200375 char *s = p + strlen(p) - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100376
Jens Axboe4ae3f762006-05-30 13:35:14 +0200377 while (isspace(*s) || iscntrl(*s))
378 s--;
Jens Axboeaea47d42006-05-26 19:27:29 +0200379
Jens Axboe4ae3f762006-05-30 13:35:14 +0200380 *(s + 1) = '\0';
Jens Axboeaea47d42006-05-26 19:27:29 +0200381}
382
Jens Axboeebac4652005-12-08 15:25:21 +0100383typedef int (str_cb_fn)(struct thread_data *, char *);
384
385static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
386{
Jens Axboe843a7412006-06-01 21:14:21 -0700387 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100388
Jens Axboe843a7412006-06-01 21:14:21 -0700389 if (strncmp(p, name, strlen(name)))
390 return 1;
391
392 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100393 if (!s)
394 return 1;
395
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100396 s = strchr(s, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100397 if (!s)
398 return 1;
399
400 s++;
401 strip_blank_front(&s);
402 return cb(td, s);
403}
404
405static int check_strstore(char *p, char *name, char *dest)
406{
Jens Axboe843a7412006-06-01 21:14:21 -0700407 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100408
Jens Axboe843a7412006-06-01 21:14:21 -0700409 if (strncmp(p, name, strlen(name)))
410 return 1;
411
412 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100413 if (!s)
414 return 1;
415
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100416 s = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100417 if (!s)
418 return 1;
419
420 s++;
421 strip_blank_front(&s);
422
423 strcpy(dest, s);
Jens Axboeebac4652005-12-08 15:25:21 +0100424 return 0;
425}
426
Jens Axboe906c8d72006-06-13 09:37:56 +0200427static int __check_range_bytes(char *str, unsigned long *val)
Jens Axboeebac4652005-12-08 15:25:21 +0100428{
Jens Axboe01617be2005-12-08 19:50:40 +0100429 char suffix;
Jens Axboeebac4652005-12-08 15:25:21 +0100430
Jens Axboe01617be2005-12-08 19:50:40 +0100431 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
Jens Axboe906c8d72006-06-13 09:37:56 +0200432 *val *= get_mult_bytes(suffix);
Jens Axboeebac4652005-12-08 15:25:21 +0100433 return 0;
434 }
435
Jens Axboe01617be2005-12-08 19:50:40 +0100436 if (sscanf(str, "%lu", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100437 return 0;
438
439 return 1;
Jens Axboe01617be2005-12-08 19:50:40 +0100440}
Jens Axboeebac4652005-12-08 15:25:21 +0100441
Jens Axboe906c8d72006-06-13 09:37:56 +0200442static int check_range_bytes(char *p, char *name, unsigned long *s,
443 unsigned long *e)
Jens Axboe01617be2005-12-08 19:50:40 +0100444{
445 char option[128];
446 char *str, *p1, *p2;
447
Jens Axboe843a7412006-06-01 21:14:21 -0700448 if (strncmp(p, name, strlen(name)))
449 return 1;
450
Jens Axboe01617be2005-12-08 19:50:40 +0100451 strcpy(option, p);
452 p = option;
453
454 str = strstr(p, name);
455 if (!str)
456 return 1;
457
458 p += strlen(name);
459
460 str = strchr(p, '=');
461 if (!str)
462 return 1;
463
464 /*
465 * 'p' now holds whatever is after the '=' sign
466 */
467 p1 = str + 1;
468
469 /*
470 * terminate p1 at the '-' sign
471 */
472 p = strchr(p1, '-');
473 if (!p)
474 return 1;
475
476 p2 = p + 1;
477 *p = '\0';
478
Jens Axboe906c8d72006-06-13 09:37:56 +0200479 if (!__check_range_bytes(p1, s) && !__check_range_bytes(p2, e))
Jens Axboe01617be2005-12-08 19:50:40 +0100480 return 0;
481
482 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100483}
484
485static int check_int(char *p, char *name, unsigned int *val)
486{
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100487 char *str;
Jens Axboeebac4652005-12-08 15:25:21 +0100488
Jens Axboeb6754f92006-05-30 14:29:32 +0200489 if (strncmp(p, name, strlen(name)))
490 return 1;
491
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100492 str = strstr(p, name);
493 if (!str)
494 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100495
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100496 str = strchr(p, '=');
497 if (!str)
498 return 1;
499
500 str++;
501
502 if (sscanf(str, "%u", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100503 return 0;
504
505 return 1;
506}
507
508static int check_strset(char *p, char *name)
509{
510 return strncmp(p, name, strlen(name));
511}
512
513static int is_empty_or_comment(char *line)
514{
515 unsigned int i;
516
517 for (i = 0; i < strlen(line); i++) {
518 if (line[i] == ';')
519 return 1;
520 if (!isspace(line[i]) && !iscntrl(line[i]))
521 return 0;
522 }
523
524 return 1;
525}
526
527static int str_rw_cb(struct thread_data *td, char *mem)
528{
529 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
530 td->ddir = DDIR_READ;
531 td->sequential = 1;
532 return 0;
533 } else if (!strncmp(mem, "randread", 8)) {
534 td->ddir = DDIR_READ;
535 td->sequential = 0;
536 return 0;
537 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
538 td->ddir = DDIR_WRITE;
539 td->sequential = 1;
540 return 0;
541 } else if (!strncmp(mem, "randwrite", 9)) {
542 td->ddir = DDIR_WRITE;
543 td->sequential = 0;
544 return 0;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200545 } else if (!strncmp(mem, "rw", 2)) {
546 td->ddir = 0;
547 td->iomix = 1;
548 td->sequential = 1;
549 return 0;
550 } else if (!strncmp(mem, "randrw", 6)) {
551 td->ddir = 0;
552 td->iomix = 1;
553 td->sequential = 0;
554 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100555 }
556
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200557 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100558 return 1;
559}
560
561static int str_verify_cb(struct thread_data *td, char *mem)
562{
563 if (!strncmp(mem, "0", 1)) {
564 td->verify = VERIFY_NONE;
565 return 0;
566 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
567 td->verify = VERIFY_MD5;
568 return 0;
569 } else if (!strncmp(mem, "crc32", 5)) {
570 td->verify = VERIFY_CRC32;
571 return 0;
572 }
573
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200574 log_err("fio: verify types: md5, crc32\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100575 return 1;
576}
577
578static int str_mem_cb(struct thread_data *td, char *mem)
579{
580 if (!strncmp(mem, "malloc", 6)) {
581 td->mem_type = MEM_MALLOC;
582 return 0;
583 } else if (!strncmp(mem, "shm", 3)) {
584 td->mem_type = MEM_SHM;
585 return 0;
586 } else if (!strncmp(mem, "mmap", 4)) {
587 td->mem_type = MEM_MMAP;
588 return 0;
589 }
590
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200591 log_err("fio: mem type: malloc, shm, mmap\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100592 return 1;
593}
594
595static int str_ioengine_cb(struct thread_data *td, char *str)
596{
597 if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
598 !strncmp(str, "libaio", 6)) {
599 strcpy(td->io_engine_name, "libaio");
600 td->io_engine = FIO_LIBAIO;
601 return 0;
602 } else if (!strncmp(str, "posixaio", 8)) {
603 strcpy(td->io_engine_name, "posixaio");
604 td->io_engine = FIO_POSIXAIO;
605 return 0;
606 } else if (!strncmp(str, "sync", 4)) {
607 strcpy(td->io_engine_name, "sync");
608 td->io_engine = FIO_SYNCIO;
609 return 0;
610 } else if (!strncmp(str, "mmap", 4)) {
611 strcpy(td->io_engine_name, "mmap");
612 td->io_engine = FIO_MMAPIO;
613 return 0;
614 } else if (!strncmp(str, "sgio", 4)) {
615 strcpy(td->io_engine_name, "sgio");
616 td->io_engine = FIO_SGIO;
617 return 0;
Jens Axboe8756e4d2006-05-27 20:24:53 +0200618 } else if (!strncmp(str, "splice", 6)) {
619 strcpy(td->io_engine_name, "splice");
620 td->io_engine = FIO_SPLICEIO;
621 return 0;
Jens Axboeb990b5c2006-09-14 09:48:22 +0200622 } else if (!strncmp(str, "cpu", 3)) {
623 strcpy(td->io_engine_name, "cpu");
624 td->io_engine = FIO_CPUIO;
625 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100626 }
627
Jens Axboeb990b5c2006-09-14 09:48:22 +0200628 log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice, cpu\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100629 return 1;
630}
631
Jens Axboe07261982006-06-07 10:51:12 +0200632/*
633 * This is our [ini] type file parser.
634 */
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200635int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100636{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200637 unsigned int prioclass, prio, cpu, global, il;
Jens Axboeebac4652005-12-08 15:25:21 +0100638 unsigned long long ull;
639 unsigned long ul1, ul2;
640 struct thread_data *td;
Jens Axboeef899b62006-06-06 09:31:00 +0200641 char *string, *name, *tmpbuf;
Jens Axboeebac4652005-12-08 15:25:21 +0100642 fpos_t off;
643 FILE *f;
644 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200645 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +0100646
647 f = fopen(file, "r");
648 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200649 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100650 return 1;
651 }
652
653 string = malloc(4096);
654 name = malloc(256);
Jens Axboeef899b62006-06-06 09:31:00 +0200655 tmpbuf = malloc(4096);
Jens Axboeebac4652005-12-08 15:25:21 +0100656
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200657 stonewall = stonewall_flag;
Jens Axboeebac4652005-12-08 15:25:21 +0100658 while ((p = fgets(string, 4096, f)) != NULL) {
Jens Axboe45410ac2006-06-07 11:10:39 +0200659 if (ret)
660 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100661 if (is_empty_or_comment(p))
662 continue;
663 if (sscanf(p, "[%s]", name) != 1)
664 continue;
665
666 global = !strncmp(name, "global", 6);
667
668 name[strlen(name) - 1] = '\0';
669
670 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200671 if (!td) {
672 ret = 1;
673 break;
674 }
Jens Axboeebac4652005-12-08 15:25:21 +0100675
Jens Axboe972cfd22006-06-09 11:08:56 +0200676 /*
677 * Seperate multiple job files by a stonewall
678 */
Jens Axboef9481912006-06-09 11:37:28 +0200679 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +0200680 td->stonewall = stonewall;
681 stonewall = 0;
682 }
683
Jens Axboeebac4652005-12-08 15:25:21 +0100684 fgetpos(f, &off);
685 while ((p = fgets(string, 4096, f)) != NULL) {
686 if (is_empty_or_comment(p))
687 continue;
688 if (strstr(p, "["))
689 break;
Jens Axboeb6754f92006-05-30 14:29:32 +0200690 strip_blank_front(&p);
Jens Axboe4ae3f762006-05-30 13:35:14 +0200691 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200692
Jens Axboeebac4652005-12-08 15:25:21 +0100693 if (!check_int(p, "prio", &prio)) {
694#ifndef FIO_HAVE_IOPRIO
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200695 log_err("io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200696 ret = 1;
697 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100698#endif
Jens Axboe8756e4d2006-05-27 20:24:53 +0200699 td->ioprio |= prio;
Jens Axboeebac4652005-12-08 15:25:21 +0100700 fgetpos(f, &off);
701 continue;
702 }
703 if (!check_int(p, "prioclass", &prioclass)) {
704#ifndef FIO_HAVE_IOPRIO
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200705 log_err("io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200706 ret = 1;
707 break;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200708#else
Jens Axboe8756e4d2006-05-27 20:24:53 +0200709 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
Jens Axboeebac4652005-12-08 15:25:21 +0100710 fgetpos(f, &off);
711 continue;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200712#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100713 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200714 if (!check_int(p, "direct", &il)) {
715 td->odirect = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100716 fgetpos(f, &off);
717 continue;
718 }
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200719 if (!check_int(p, "rand_repeatable", &il)) {
720 td->rand_repeatable = il;
721 fgetpos(f, &off);
722 continue;
723 }
Jens Axboeebac4652005-12-08 15:25:21 +0100724 if (!check_int(p, "rate", &td->rate)) {
725 fgetpos(f, &off);
726 continue;
727 }
728 if (!check_int(p, "ratemin", &td->ratemin)) {
729 fgetpos(f, &off);
730 continue;
731 }
732 if (!check_int(p, "ratecycle", &td->ratecycle)) {
733 fgetpos(f, &off);
734 continue;
735 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200736 if (!check_int(p, "cpuload", &td->cpuload)) {
737 fgetpos(f, &off);
738 continue;
739 }
740 if (!check_int(p, "cpuchunks", &td->cpucycle)) {
741 fgetpos(f, &off);
742 continue;
743 }
Jens Axboeebac4652005-12-08 15:25:21 +0100744 if (!check_int(p, "thinktime", &td->thinktime)) {
745 fgetpos(f, &off);
746 continue;
747 }
748 if (!check_int(p, "cpumask", &cpu)) {
749#ifndef FIO_HAVE_CPU_AFFINITY
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200750 log_err("cpu affinity not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200751 ret = 1;
752 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100753#endif
754 fill_cpu_mask(td->cpumask, cpu);
755 fgetpos(f, &off);
756 continue;
757 }
758 if (!check_int(p, "fsync", &td->fsync_blocks)) {
759 fgetpos(f, &off);
Jens Axboefc1a4712006-05-30 13:04:05 +0200760 td->end_fsync = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100761 continue;
762 }
763 if (!check_int(p, "startdelay", &td->start_delay)) {
764 fgetpos(f, &off);
765 continue;
766 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200767 if (!check_str_time(p, "timeout", &ull)) {
Jens Axboeaf959a72006-07-13 14:36:02 -0700768 td->timeout = ull;
Jens Axboeebac4652005-12-08 15:25:21 +0100769 fgetpos(f, &off);
770 continue;
771 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200772 if (!check_int(p, "invalidate", &il)) {
773 td->invalidate_cache = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100774 fgetpos(f, &off);
775 continue;
776 }
777 if (!check_int(p, "iodepth", &td->iodepth)) {
778 fgetpos(f, &off);
779 continue;
780 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200781 if (!check_int(p, "sync", &il)) {
782 td->sync_io = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100783 fgetpos(f, &off);
784 continue;
785 }
786 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
787 fgetpos(f, &off);
788 continue;
789 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200790 if (!check_int(p, "create_serialize", &il)) {
791 td->create_serialize = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100792 fgetpos(f, &off);
793 continue;
794 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200795 if (!check_int(p, "create_fsync", &il)) {
796 td->create_fsync = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100797 fgetpos(f, &off);
798 continue;
799 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200800 if (!check_int(p, "end_fsync", &il)) {
801 td->end_fsync = il;
Jens Axboefc1a4712006-05-30 13:04:05 +0200802 fgetpos(f, &off);
803 continue;
804 }
Jens Axboeebac4652005-12-08 15:25:21 +0100805 if (!check_int(p, "loops", &td->loops)) {
806 fgetpos(f, &off);
807 continue;
808 }
809 if (!check_int(p, "numjobs", &td->numjobs)) {
810 fgetpos(f, &off);
811 continue;
812 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200813 if (!check_int(p, "overwrite", &il)) {
814 td->overwrite = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100815 fgetpos(f, &off);
816 continue;
817 }
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200818 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
819 fgetpos(f, &off);
820 continue;
821 }
822 if (!check_int(p, "rwmixread", &il)) {
823 if (il > 100)
824 il = 100;
825 td->rwmixread = il;
826 fgetpos(f, &off);
827 continue;
828 }
829 if (!check_int(p, "rwmixwrite", &il)) {
830 if (il > 100)
831 il = 100;
832 td->rwmixread = 100 - il;
833 fgetpos(f, &off);
834 continue;
835 }
Jens Axboeb6f4d882006-06-02 10:32:51 +0200836 if (!check_int(p, "nice", &td->nice)) {
837 fgetpos(f, &off);
838 continue;
839 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200840 if (!check_range_bytes(p, "bsrange", &ul1, &ul2)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100841 if (ul1 > ul2) {
842 td->max_bs = ul1;
843 td->min_bs = ul2;
844 } else {
845 td->max_bs = ul2;
846 td->min_bs = ul1;
847 }
848 fgetpos(f, &off);
849 continue;
850 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200851 if (!check_str_bytes(p, "bs", &ull)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100852 td->bs = ull;
853 fgetpos(f, &off);
854 continue;
855 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200856 if (!check_str_bytes(p, "size", &td->file_size)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100857 fgetpos(f, &off);
858 continue;
859 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200860 if (!check_str_bytes(p, "offset", &td->file_offset)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100861 fgetpos(f, &off);
862 continue;
863 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200864 if (!check_str_bytes(p, "zonesize", &td->zone_size)) {
Jens Axboe20dc95c2005-12-09 10:29:35 +0100865 fgetpos(f, &off);
866 continue;
867 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200868 if (!check_str_bytes(p, "zoneskip", &td->zone_skip)) {
Jens Axboe20dc95c2005-12-09 10:29:35 +0100869 fgetpos(f, &off);
870 continue;
871 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200872 if (!check_str_bytes(p, "lockmem", &mlock_size)) {
Jens Axboec04f7ec2006-05-31 10:13:16 +0200873 fgetpos(f, &off);
874 continue;
875 }
Jens Axboeef899b62006-06-06 09:31:00 +0200876 if (!check_strstore(p, "directory", tmpbuf)) {
877 td->directory = strdup(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100878 fgetpos(f, &off);
879 continue;
880 }
Jens Axboe01452052006-06-07 10:29:47 +0200881 if (!check_strstore(p, "name", tmpbuf)) {
882 snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
883 fgetpos(f, &off);
884 continue;
885 }
Jens Axboeebac4652005-12-08 15:25:21 +0100886 if (!check_str(p, "mem", str_mem_cb, td)) {
887 fgetpos(f, &off);
888 continue;
889 }
890 if (!check_str(p, "verify", str_verify_cb, td)) {
891 fgetpos(f, &off);
892 continue;
893 }
894 if (!check_str(p, "rw", str_rw_cb, td)) {
895 fgetpos(f, &off);
896 continue;
897 }
898 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
899 fgetpos(f, &off);
900 continue;
901 }
902 if (!check_strset(p, "create")) {
903 td->create_file = 1;
904 fgetpos(f, &off);
905 continue;
906 }
907 if (!check_strset(p, "exitall")) {
908 exitall_on_terminate = 1;
909 fgetpos(f, &off);
910 continue;
911 }
912 if (!check_strset(p, "stonewall")) {
913 td->stonewall = 1;
914 fgetpos(f, &off);
915 continue;
916 }
917 if (!check_strset(p, "thread")) {
918 td->use_thread = 1;
919 fgetpos(f, &off);
920 continue;
921 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200922 if (!check_strstore(p, "iolog", tmpbuf)) {
Jens Axboe4f693b92006-06-07 22:52:28 +0200923 if (td->write_iolog) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200924 log_err("fio: read iolog overrides given write_iolog\n");
Jens Axboeaf8c0b42006-06-07 22:33:17 +0200925 free(td->iolog_file);
Jens Axboe4f693b92006-06-07 22:52:28 +0200926 td->write_iolog = 0;
927 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200928 td->iolog_file = strdup(tmpbuf);
Jens Axboe843a7412006-06-01 21:14:21 -0700929 td->read_iolog = 1;
Jens Axboe843a7412006-06-01 21:14:21 -0700930 fgetpos(f, &off);
931 continue;
932 }
Jens Axboeaf8c0b42006-06-07 22:33:17 +0200933 if (!check_strstore(p, "write_iolog", tmpbuf)) {
934 if (!td->read_iolog) {
935 td->iolog_file = strdup(tmpbuf);
936 td->write_iolog = 1;
Jens Axboe4f693b92006-06-07 22:52:28 +0200937 } else
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200938 log_err("fio: read iolog overrides given write_iolog\n");
Jens Axboeaea47d42006-05-26 19:27:29 +0200939 fgetpos(f, &off);
940 continue;
941 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200942 if (!check_strstore(p, "exec_prerun", tmpbuf)) {
943 td->exec_prerun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200944 fgetpos(f, &off);
945 continue;
946 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200947 if (!check_strstore(p, "exec_postrun", tmpbuf)) {
948 td->exec_postrun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200949 fgetpos(f, &off);
950 continue;
951 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200952 if (!check_strstore(p, "ioscheduler", tmpbuf)) {
Jens Axboe22f78b32006-06-07 11:30:07 +0200953#ifndef FIO_HAVE_IOSCHED_SWITCH
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200954 log_err("io scheduler switching not available\n");
Jens Axboe22f78b32006-06-07 11:30:07 +0200955 ret = 1;
956 break;
957#else
Jens Axboe80b9feb2006-06-07 10:34:49 +0200958 td->ioscheduler = strdup(tmpbuf);
Jens Axboeda867742006-06-06 10:39:10 -0700959 fgetpos(f, &off);
960 continue;
Jens Axboe22f78b32006-06-07 11:30:07 +0200961#endif
Jens Axboeda867742006-06-06 10:39:10 -0700962 }
Jens Axboeebac4652005-12-08 15:25:21 +0100963
Jens Axboe45410ac2006-06-07 11:10:39 +0200964 /*
965 * Don't break here, continue parsing options so we
966 * dump all the bad ones. Makes trial/error fixups
967 * easier on the user.
968 */
Jens Axboeebac4652005-12-08 15:25:21 +0100969 printf("Client%d: bad option %s\n",td->thread_number,p);
Jens Axboe45410ac2006-06-07 11:10:39 +0200970 ret = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100971 }
Jens Axboeebac4652005-12-08 15:25:21 +0100972
Jens Axboe45410ac2006-06-07 11:10:39 +0200973 if (!ret) {
974 fsetpos(f, &off);
975 ret = add_job(td, name, 0);
976 }
977 if (ret)
978 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100979 }
980
981 free(string);
982 free(name);
Jens Axboeef899b62006-06-06 09:31:00 +0200983 free(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100984 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200985 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100986}
987
988static int fill_def_thread(void)
989{
990 memset(&def_thread, 0, sizeof(def_thread));
991
992 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
993 perror("sched_getaffinity");
994 return 1;
995 }
996
997 /*
998 * fill globals
999 */
1000 def_thread.ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001001 def_thread.iomix = 0;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001002 def_thread.bs = DEF_BS;
Jens Axboeebac4652005-12-08 15:25:21 +01001003 def_thread.min_bs = -1;
1004 def_thread.max_bs = -1;
1005 def_thread.io_engine = DEF_IO_ENGINE;
1006 strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001007 def_thread.odirect = DEF_ODIRECT;
Jens Axboeebac4652005-12-08 15:25:21 +01001008 def_thread.ratecycle = DEF_RATE_CYCLE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001009 def_thread.sequential = DEF_SEQUENTIAL;
Jens Axboe972cfd22006-06-09 11:08:56 +02001010 def_thread.timeout = def_timeout;
Jens Axboeebac4652005-12-08 15:25:21 +01001011 def_thread.create_file = DEF_CREATE;
1012 def_thread.overwrite = DEF_OVERWRITE;
1013 def_thread.invalidate_cache = DEF_INVALIDATE;
1014 def_thread.sync_io = DEF_SYNCIO;
1015 def_thread.mem_type = MEM_MALLOC;
1016 def_thread.bw_avg_time = DEF_BWAVGTIME;
1017 def_thread.create_serialize = DEF_CREATE_SER;
1018 def_thread.create_fsync = DEF_CREATE_FSYNC;
1019 def_thread.loops = DEF_LOOPS;
1020 def_thread.verify = DEF_VERIFY;
1021 def_thread.stonewall = DEF_STONEWALL;
1022 def_thread.numjobs = DEF_NUMJOBS;
1023 def_thread.use_thread = DEF_USE_THREAD;
Jens Axboea6ccc7b2006-06-02 10:14:15 +02001024 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
1025 def_thread.rwmixread = DEF_RWMIX_READ;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001026 def_thread.nice = DEF_NICE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001027 def_thread.rand_repeatable = DEF_RAND_REPEAT;
Jens Axboeebac4652005-12-08 15:25:21 +01001028#ifdef FIO_HAVE_DISK_UTIL
1029 def_thread.do_disk_util = 1;
1030#endif
1031
1032 return 0;
1033}
1034
Jens Axboe4785f992006-05-26 03:59:10 +02001035static void usage(char *name)
1036{
1037 printf("%s\n", fio_version_string);
Jens Axboe47623c72006-06-12 10:23:28 +02001038 printf("\t-o Write output to file\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001039 printf("\t-t Runtime in seconds\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001040 printf("\t-l Generate per-job latency logs\n");
1041 printf("\t-w Generate per-job bandwidth logs\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001042 printf("\t-m Minimal (terse) output\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001043 printf("\t-v Print version info and exit\n");
1044}
1045
Jens Axboe972cfd22006-06-09 11:08:56 +02001046static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +01001047{
Jens Axboe972cfd22006-06-09 11:08:56 +02001048 int c, idx = 1, ini_idx = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001049
Jens Axboe2e778412006-06-16 10:00:50 +02001050 while ((c = getopt(argc, argv, "t:o:lwvhm")) != EOF) {
Jens Axboeebac4652005-12-08 15:25:21 +01001051 switch (c) {
Jens Axboeebac4652005-12-08 15:25:21 +01001052 case 't':
Jens Axboe972cfd22006-06-09 11:08:56 +02001053 def_timeout = atoi(optarg);
Jens Axboe774a6172006-08-24 08:44:26 +02001054 idx = optind;
Jens Axboeebac4652005-12-08 15:25:21 +01001055 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001056 case 'l':
1057 write_lat_log = 1;
Jens Axboe774a6172006-08-24 08:44:26 +02001058 idx = optind;
Jens Axboeebac4652005-12-08 15:25:21 +01001059 break;
1060 case 'w':
1061 write_bw_log = 1;
Jens Axboe774a6172006-08-24 08:44:26 +02001062 idx = optind;
Jens Axboeebac4652005-12-08 15:25:21 +01001063 break;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001064 case 'o':
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001065 f_out = fopen(optarg, "w+");
1066 if (!f_out) {
1067 perror("fopen output");
1068 exit(1);
1069 }
1070 f_err = f_out;
Jens Axboe774a6172006-08-24 08:44:26 +02001071 idx = optind;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001072 break;
Jens Axboec6ae0a52006-06-12 11:04:44 +02001073 case 'm':
1074 terse_output = 1;
Jens Axboe774a6172006-08-24 08:44:26 +02001075 idx = optind;
Jens Axboec6ae0a52006-06-12 11:04:44 +02001076 break;
Jens Axboe4785f992006-05-26 03:59:10 +02001077 case 'h':
1078 usage(argv[0]);
1079 exit(0);
Jens Axboeebac4652005-12-08 15:25:21 +01001080 case 'v':
1081 printf("%s\n", fio_version_string);
1082 exit(0);
1083 }
1084 }
Jens Axboec9fad892006-05-27 17:26:50 +02001085
Jens Axboe972cfd22006-06-09 11:08:56 +02001086 while (idx < argc) {
1087 ini_idx++;
1088 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1089 ini_file[ini_idx - 1] = strdup(argv[idx]);
1090 idx++;
1091 }
Jens Axboe774a6172006-08-24 08:44:26 +02001092
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001093 if (!f_out) {
1094 f_out = stdout;
1095 f_err = stderr;
1096 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001097
1098 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +01001099}
1100
1101static void free_shm(void)
1102{
1103 struct shmid_ds sbuf;
1104
1105 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001106 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001107 threads = NULL;
1108 shmctl(shm_id, IPC_RMID, &sbuf);
1109 }
1110}
1111
Jens Axboe906c8d72006-06-13 09:37:56 +02001112/*
1113 * The thread area is shared between the main process and the job
1114 * threads/processes. So setup a shared memory segment that will hold
1115 * all the job info.
1116 */
Jens Axboeebac4652005-12-08 15:25:21 +01001117static int setup_thread_area(void)
1118{
1119 /*
1120 * 1024 is too much on some machines, scale max_jobs if
1121 * we get a failure that looks like too large a shm segment
1122 */
1123 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001124 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001125
Jens Axboe906c8d72006-06-13 09:37:56 +02001126 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001127 if (shm_id != -1)
1128 break;
1129 if (errno != EINVAL) {
1130 perror("shmget");
1131 break;
1132 }
1133
1134 max_jobs >>= 1;
1135 } while (max_jobs);
1136
1137 if (shm_id == -1)
1138 return 1;
1139
1140 threads = shmat(shm_id, NULL, 0);
1141 if (threads == (void *) -1) {
1142 perror("shmat");
1143 return 1;
1144 }
1145
1146 atexit(free_shm);
1147 return 0;
1148}
1149
1150int parse_options(int argc, char *argv[])
1151{
Jens Axboe972cfd22006-06-09 11:08:56 +02001152 int job_files, i;
1153
Jens Axboeebac4652005-12-08 15:25:21 +01001154 if (setup_thread_area())
1155 return 1;
1156 if (fill_def_thread())
1157 return 1;
1158
Jens Axboe972cfd22006-06-09 11:08:56 +02001159 job_files = parse_cmd_line(argc, argv);
1160 if (!job_files) {
1161 log_err("Need job file(s)\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001162 usage(argv[0]);
Jens Axboeebac4652005-12-08 15:25:21 +01001163 return 1;
1164 }
1165
Jens Axboe972cfd22006-06-09 11:08:56 +02001166 for (i = 0; i < job_files; i++) {
1167 if (fill_def_thread())
1168 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001169 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001170 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001171 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001172 }
Jens Axboeebac4652005-12-08 15:25:21 +01001173
Jens Axboe88c6ed82006-06-09 11:28:10 +02001174 free(ini_file);
Jens Axboeebac4652005-12-08 15:25:21 +01001175 return 0;
1176}