blob: 3117d60e5d68e530fde45fed328b7b6bb4e39108 [file] [log] [blame]
Jens Axboeebac4652005-12-08 15:25:21 +01001#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <fcntl.h>
5#include <ctype.h>
6#include <string.h>
7#include <errno.h>
8#include <sys/ipc.h>
9#include <sys/shm.h>
10#include <sys/types.h>
11#include <sys/stat.h>
12
13#include "fio.h"
14
Jens Axboe20dc95c2005-12-09 10:29:35 +010015#define DEF_BS (4096)
16#define DEF_TIMEOUT (0)
17#define DEF_RATE_CYCLE (1000)
18#define DEF_ODIRECT (1)
19#define DEF_IO_ENGINE (FIO_SYNCIO)
Jens Axboeebac4652005-12-08 15:25:21 +010020#define DEF_IO_ENGINE_NAME "sync"
Jens Axboe20dc95c2005-12-09 10:29:35 +010021#define DEF_SEQUENTIAL (1)
22#define DEF_RAND_REPEAT (1)
23#define DEF_OVERWRITE (1)
24#define DEF_CREATE (1)
25#define DEF_INVALIDATE (1)
26#define DEF_SYNCIO (0)
27#define DEF_RANDSEED (0xb1899bedUL)
28#define DEF_BWAVGTIME (500)
29#define DEF_CREATE_SER (1)
Jens Axboeebac4652005-12-08 15:25:21 +010030#define DEF_CREATE_FSYNC (1)
Jens Axboe20dc95c2005-12-09 10:29:35 +010031#define DEF_LOOPS (1)
32#define DEF_VERIFY (0)
33#define DEF_STONEWALL (0)
34#define DEF_NUMJOBS (1)
35#define DEF_USE_THREAD (0)
36#define DEF_FILE_SIZE (1024 * 1024 * 1024UL)
37#define DEF_ZONE_SIZE (0)
38#define DEF_ZONE_SKIP (0)
Jens Axboea6ccc7b2006-06-02 10:14:15 +020039#define DEF_RWMIX_CYCLE (500)
40#define DEF_RWMIX_READ (50)
Jens Axboeb6f4d882006-06-02 10:32:51 +020041#define DEF_NICE (0)
Jens Axboeebac4652005-12-08 15:25:21 +010042
Jens Axboe573275e2006-06-05 12:20:20 +020043static char fio_version_string[] = "fio 1.4";
Jens Axboeebac4652005-12-08 15:25:21 +010044
45static int repeatable = DEF_RAND_REPEAT;
46static char *ini_file;
47static int max_jobs = MAX_JOBS;
48
49struct thread_data def_thread;
50struct thread_data *threads = NULL;
51
52int rate_quit = 0;
53int write_lat_log = 0;
54int write_bw_log = 0;
55int exitall_on_terminate = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +020056unsigned long long mlock_size = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010057
Jens Axboeebac4652005-12-08 15:25:21 +010058static struct thread_data *get_new_job(int global, struct thread_data *parent)
59{
60 struct thread_data *td;
61
62 if (global)
63 return &def_thread;
64 if (thread_number >= max_jobs)
65 return NULL;
66
67 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +020068 *td = *parent;
69 td->name[0] = '\0';
Jens Axboeebac4652005-12-08 15:25:21 +010070
71 td->fd = -1;
72 td->thread_number = thread_number;
73
Jens Axboeebac4652005-12-08 15:25:21 +010074 return td;
75}
76
77static void put_job(struct thread_data *td)
78{
79 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
80 thread_number--;
81}
82
Jens Axboe75154842006-06-01 13:56:09 +020083static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +010084{
Jens Axboe3d60d1e2006-05-25 06:31:06 +020085 char *ddir_str[] = { "read", "write", "randread", "randwrite",
86 "rw", NULL, "randrw" };
Jens Axboeebac4652005-12-08 15:25:21 +010087 struct stat sb;
88 int numjobs, ddir;
89
90#ifndef FIO_HAVE_LIBAIO
91 if (td->io_engine == FIO_LIBAIO) {
92 fprintf(stderr, "Linux libaio not available\n");
93 return 1;
94 }
95#endif
96#ifndef FIO_HAVE_POSIXAIO
97 if (td->io_engine == FIO_POSIXAIO) {
98 fprintf(stderr, "posix aio not available\n");
99 return 1;
100 }
101#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100102
103 /*
104 * the def_thread is just for options, it's not a real job
105 */
106 if (td == &def_thread)
107 return 0;
108
109 if (td->io_engine & FIO_SYNCIO)
110 td->iodepth = 1;
111 else {
112 if (!td->iodepth)
113 td->iodepth = 1;
114 }
115
Jens Axboe20dc95c2005-12-09 10:29:35 +0100116 /*
117 * only really works for sequential io for now
118 */
119 if (td->zone_size && !td->sequential)
120 td->zone_size = 0;
121
Jens Axboeebac4652005-12-08 15:25:21 +0100122 td->filetype = FIO_TYPE_FILE;
Jens Axboe0af7b542006-02-17 10:10:12 +0100123 if (!stat(jobname, &sb)) {
124 if (S_ISBLK(sb.st_mode))
125 td->filetype = FIO_TYPE_BD;
126 else if (S_ISCHR(sb.st_mode))
127 td->filetype = FIO_TYPE_CHAR;
128 }
Jens Axboeebac4652005-12-08 15:25:21 +0100129
130 if (td->filetype == FIO_TYPE_FILE) {
Jens Axboee9c047a2006-06-07 21:13:04 +0200131 char tmp[PATH_MAX];
132
Jens Axboeef899b62006-06-06 09:31:00 +0200133 if (td->directory && td->directory[0] != '\0')
Jens Axboee9c047a2006-06-07 21:13:04 +0200134 sprintf(tmp, "%s/%s.%d", td->directory, jobname, td->jobnum);
Jens Axboeebac4652005-12-08 15:25:21 +0100135 else
Jens Axboee9c047a2006-06-07 21:13:04 +0200136 sprintf(tmp, "%s.%d", jobname, td->jobnum);
137 td->file_name = strdup(tmp);
Jens Axboeebac4652005-12-08 15:25:21 +0100138 } else
Jens Axboee9c047a2006-06-07 21:13:04 +0200139 td->file_name = strdup(jobname);
Jens Axboeebac4652005-12-08 15:25:21 +0100140
Jens Axboebbfd6b02006-06-07 19:42:54 +0200141 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100142
143 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
144 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
145 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
146
147 if (td->min_bs == -1U)
148 td->min_bs = td->bs;
149 if (td->max_bs == -1U)
150 td->max_bs = td->bs;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200151 if (td_read(td) && !td_rw(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100152 td->verify = 0;
153
154 if (td->stonewall && td->thread_number > 1)
155 groupid++;
156
157 td->groupid = groupid;
158
159 if (setup_rate(td))
160 goto err;
161
162 if (write_lat_log) {
163 setup_log(&td->slat_log);
164 setup_log(&td->clat_log);
165 }
166 if (write_bw_log)
167 setup_log(&td->bw_log);
168
Jens Axboe01452052006-06-07 10:29:47 +0200169 if (td->name[0] == '\0')
170 snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
171
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200172 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
Jens Axboe75154842006-06-01 13:56:09 +0200173
174 if (!job_add_num)
Jens Axboe01452052006-06-07 10:29:47 +0200175 printf("%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);
Jens Axboe75154842006-06-01 13:56:09 +0200176 else if (job_add_num == 1)
177 printf("...\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100178
179 /*
180 * recurse add identical jobs, clear numjobs and stonewall options
181 * as they don't apply to sub-jobs
182 */
183 numjobs = td->numjobs;
184 while (--numjobs) {
185 struct thread_data *td_new = get_new_job(0, td);
186
187 if (!td_new)
188 goto err;
189
190 td_new->numjobs = 1;
191 td_new->stonewall = 0;
Jens Axboeeba09dd2006-05-22 20:20:54 +0200192 td_new->jobnum = numjobs;
Jens Axboe75154842006-06-01 13:56:09 +0200193 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100194
Jens Axboe75154842006-06-01 13:56:09 +0200195 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100196 goto err;
197 }
198 return 0;
199err:
200 put_job(td);
201 return -1;
202}
203
204int init_random_state(struct thread_data *td)
205{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200206 unsigned long seeds[4];
Jens Axboeebac4652005-12-08 15:25:21 +0100207 int fd, num_maps, blocks;
208
Jens Axboe1ac267b2006-05-31 20:29:00 +0200209 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100210 if (fd == -1) {
211 td_verror(td, errno);
212 return 1;
213 }
214
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200215 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100216 td_verror(td, EIO);
217 close(fd);
218 return 1;
219 }
220
221 close(fd);
222
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200223 os_random_seed(seeds[0], &td->bsrange_state);
224 os_random_seed(seeds[1], &td->verify_state);
225 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100226
227 if (td->sequential)
228 return 0;
229
230 if (repeatable)
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200231 seeds[3] = DEF_RANDSEED;
Jens Axboeebac4652005-12-08 15:25:21 +0100232
233 blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
234 num_maps = blocks / BLOCKS_PER_MAP;
235 td->file_map = malloc(num_maps * sizeof(long));
236 td->num_maps = num_maps;
237 memset(td->file_map, 0, num_maps * sizeof(long));
238
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200239 os_random_seed(seeds[3], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100240 return 0;
241}
242
243static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
244{
245#ifdef FIO_HAVE_CPU_AFFINITY
246 unsigned int i;
247
248 CPU_ZERO(&cpumask);
249
250 for (i = 0; i < sizeof(int) * 8; i++) {
251 if ((1 << i) & cpu)
252 CPU_SET(i, &cpumask);
253 }
254#endif
255}
256
257static unsigned long get_mult(char c)
258{
259 switch (c) {
260 case 'k':
261 case 'K':
262 return 1024;
263 case 'm':
264 case 'M':
265 return 1024 * 1024;
266 case 'g':
267 case 'G':
268 return 1024 * 1024 * 1024;
269 default:
270 return 1;
271 }
272}
273
274/*
275 * convert string after '=' into decimal value, noting any size suffix
276 */
277static int str_cnv(char *p, unsigned long long *val)
278{
279 char *str;
280 int len;
281
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100282 str = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100283 if (!str)
284 return 1;
285
286 str++;
287 len = strlen(str);
288
289 *val = strtoul(str, NULL, 10);
290 if (*val == ULONG_MAX && errno == ERANGE)
291 return 1;
292
Jens Axboe9d0d4242006-05-30 13:45:51 +0200293 *val *= get_mult(str[len - 1]);
Jens Axboeebac4652005-12-08 15:25:21 +0100294 return 0;
295}
296
297static int check_strcnv(char *p, char *name, unsigned long long *val)
298{
Jens Axboe20dc95c2005-12-09 10:29:35 +0100299 if (strncmp(p, name, strlen(name) - 1))
Jens Axboeebac4652005-12-08 15:25:21 +0100300 return 1;
301
302 return str_cnv(p, val);
303}
304
305static void strip_blank_front(char **p)
306{
307 char *s = *p;
308
Jens Axboe4ae3f762006-05-30 13:35:14 +0200309 while (isspace(*s))
Jens Axboeebac4652005-12-08 15:25:21 +0100310 s++;
311}
312
313static void strip_blank_end(char *p)
314{
Jens Axboe4ae3f762006-05-30 13:35:14 +0200315 char *s = p + strlen(p) - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100316
Jens Axboe4ae3f762006-05-30 13:35:14 +0200317 while (isspace(*s) || iscntrl(*s))
318 s--;
Jens Axboeaea47d42006-05-26 19:27:29 +0200319
Jens Axboe4ae3f762006-05-30 13:35:14 +0200320 *(s + 1) = '\0';
Jens Axboeaea47d42006-05-26 19:27:29 +0200321}
322
Jens Axboeebac4652005-12-08 15:25:21 +0100323typedef int (str_cb_fn)(struct thread_data *, char *);
324
325static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
326{
Jens Axboe843a7412006-06-01 21:14:21 -0700327 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100328
Jens Axboe843a7412006-06-01 21:14:21 -0700329 if (strncmp(p, name, strlen(name)))
330 return 1;
331
332 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100333 if (!s)
334 return 1;
335
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100336 s = strchr(s, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100337 if (!s)
338 return 1;
339
340 s++;
341 strip_blank_front(&s);
342 return cb(td, s);
343}
344
345static int check_strstore(char *p, char *name, char *dest)
346{
Jens Axboe843a7412006-06-01 21:14:21 -0700347 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100348
Jens Axboe843a7412006-06-01 21:14:21 -0700349 if (strncmp(p, name, strlen(name)))
350 return 1;
351
352 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100353 if (!s)
354 return 1;
355
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100356 s = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100357 if (!s)
358 return 1;
359
360 s++;
361 strip_blank_front(&s);
362
363 strcpy(dest, s);
Jens Axboeebac4652005-12-08 15:25:21 +0100364 return 0;
365}
366
Jens Axboe01617be2005-12-08 19:50:40 +0100367static int __check_range(char *str, unsigned long *val)
Jens Axboeebac4652005-12-08 15:25:21 +0100368{
Jens Axboe01617be2005-12-08 19:50:40 +0100369 char suffix;
Jens Axboeebac4652005-12-08 15:25:21 +0100370
Jens Axboe01617be2005-12-08 19:50:40 +0100371 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
372 *val *= get_mult(suffix);
Jens Axboeebac4652005-12-08 15:25:21 +0100373 return 0;
374 }
375
Jens Axboe01617be2005-12-08 19:50:40 +0100376 if (sscanf(str, "%lu", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100377 return 0;
378
379 return 1;
Jens Axboe01617be2005-12-08 19:50:40 +0100380}
Jens Axboeebac4652005-12-08 15:25:21 +0100381
Jens Axboe01617be2005-12-08 19:50:40 +0100382static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
383{
384 char option[128];
385 char *str, *p1, *p2;
386
Jens Axboe843a7412006-06-01 21:14:21 -0700387 if (strncmp(p, name, strlen(name)))
388 return 1;
389
Jens Axboe01617be2005-12-08 19:50:40 +0100390 strcpy(option, p);
391 p = option;
392
393 str = strstr(p, name);
394 if (!str)
395 return 1;
396
397 p += strlen(name);
398
399 str = strchr(p, '=');
400 if (!str)
401 return 1;
402
403 /*
404 * 'p' now holds whatever is after the '=' sign
405 */
406 p1 = str + 1;
407
408 /*
409 * terminate p1 at the '-' sign
410 */
411 p = strchr(p1, '-');
412 if (!p)
413 return 1;
414
415 p2 = p + 1;
416 *p = '\0';
417
418 if (!__check_range(p1, s) && !__check_range(p2, e))
419 return 0;
420
421 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100422}
423
424static int check_int(char *p, char *name, unsigned int *val)
425{
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100426 char *str;
Jens Axboeebac4652005-12-08 15:25:21 +0100427
Jens Axboeb6754f92006-05-30 14:29:32 +0200428 if (strncmp(p, name, strlen(name)))
429 return 1;
430
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100431 str = strstr(p, name);
432 if (!str)
433 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100434
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100435 str = strchr(p, '=');
436 if (!str)
437 return 1;
438
439 str++;
440
441 if (sscanf(str, "%u", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100442 return 0;
443
444 return 1;
445}
446
447static int check_strset(char *p, char *name)
448{
449 return strncmp(p, name, strlen(name));
450}
451
452static int is_empty_or_comment(char *line)
453{
454 unsigned int i;
455
456 for (i = 0; i < strlen(line); i++) {
457 if (line[i] == ';')
458 return 1;
459 if (!isspace(line[i]) && !iscntrl(line[i]))
460 return 0;
461 }
462
463 return 1;
464}
465
466static int str_rw_cb(struct thread_data *td, char *mem)
467{
468 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
469 td->ddir = DDIR_READ;
470 td->sequential = 1;
471 return 0;
472 } else if (!strncmp(mem, "randread", 8)) {
473 td->ddir = DDIR_READ;
474 td->sequential = 0;
475 return 0;
476 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
477 td->ddir = DDIR_WRITE;
478 td->sequential = 1;
479 return 0;
480 } else if (!strncmp(mem, "randwrite", 9)) {
481 td->ddir = DDIR_WRITE;
482 td->sequential = 0;
483 return 0;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200484 } else if (!strncmp(mem, "rw", 2)) {
485 td->ddir = 0;
486 td->iomix = 1;
487 td->sequential = 1;
488 return 0;
489 } else if (!strncmp(mem, "randrw", 6)) {
490 td->ddir = 0;
491 td->iomix = 1;
492 td->sequential = 0;
493 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100494 }
495
Jens Axboe07261982006-06-07 10:51:12 +0200496 fprintf(stderr, "fio: data direction: read, write, randread, randwrite, rw, randrw\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100497 return 1;
498}
499
500static int str_verify_cb(struct thread_data *td, char *mem)
501{
502 if (!strncmp(mem, "0", 1)) {
503 td->verify = VERIFY_NONE;
504 return 0;
505 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
506 td->verify = VERIFY_MD5;
507 return 0;
508 } else if (!strncmp(mem, "crc32", 5)) {
509 td->verify = VERIFY_CRC32;
510 return 0;
511 }
512
Jens Axboe07261982006-06-07 10:51:12 +0200513 fprintf(stderr, "fio: verify types: md5, crc32\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100514 return 1;
515}
516
517static int str_mem_cb(struct thread_data *td, char *mem)
518{
519 if (!strncmp(mem, "malloc", 6)) {
520 td->mem_type = MEM_MALLOC;
521 return 0;
522 } else if (!strncmp(mem, "shm", 3)) {
523 td->mem_type = MEM_SHM;
524 return 0;
525 } else if (!strncmp(mem, "mmap", 4)) {
526 td->mem_type = MEM_MMAP;
527 return 0;
528 }
529
Jens Axboe07261982006-06-07 10:51:12 +0200530 fprintf(stderr, "fio: mem type: malloc, shm, mmap\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100531 return 1;
532}
533
534static int str_ioengine_cb(struct thread_data *td, char *str)
535{
536 if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
537 !strncmp(str, "libaio", 6)) {
538 strcpy(td->io_engine_name, "libaio");
539 td->io_engine = FIO_LIBAIO;
540 return 0;
541 } else if (!strncmp(str, "posixaio", 8)) {
542 strcpy(td->io_engine_name, "posixaio");
543 td->io_engine = FIO_POSIXAIO;
544 return 0;
545 } else if (!strncmp(str, "sync", 4)) {
546 strcpy(td->io_engine_name, "sync");
547 td->io_engine = FIO_SYNCIO;
548 return 0;
549 } else if (!strncmp(str, "mmap", 4)) {
550 strcpy(td->io_engine_name, "mmap");
551 td->io_engine = FIO_MMAPIO;
552 return 0;
553 } else if (!strncmp(str, "sgio", 4)) {
554 strcpy(td->io_engine_name, "sgio");
555 td->io_engine = FIO_SGIO;
556 return 0;
Jens Axboe8756e4d2006-05-27 20:24:53 +0200557 } else if (!strncmp(str, "splice", 6)) {
558 strcpy(td->io_engine_name, "splice");
559 td->io_engine = FIO_SPLICEIO;
560 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100561 }
562
Jens Axboe07261982006-06-07 10:51:12 +0200563 fprintf(stderr, "fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100564 return 1;
565}
566
Jens Axboe07261982006-06-07 10:51:12 +0200567/*
568 * This is our [ini] type file parser.
569 */
Jens Axboeebac4652005-12-08 15:25:21 +0100570int parse_jobs_ini(char *file)
571{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200572 unsigned int prioclass, prio, cpu, global, il;
Jens Axboeebac4652005-12-08 15:25:21 +0100573 unsigned long long ull;
574 unsigned long ul1, ul2;
575 struct thread_data *td;
Jens Axboeef899b62006-06-06 09:31:00 +0200576 char *string, *name, *tmpbuf;
Jens Axboeebac4652005-12-08 15:25:21 +0100577 fpos_t off;
578 FILE *f;
579 char *p;
Jens Axboe45410ac2006-06-07 11:10:39 +0200580 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100581
582 f = fopen(file, "r");
583 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200584 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100585 return 1;
586 }
587
588 string = malloc(4096);
589 name = malloc(256);
Jens Axboeef899b62006-06-06 09:31:00 +0200590 tmpbuf = malloc(4096);
Jens Axboeebac4652005-12-08 15:25:21 +0100591
592 while ((p = fgets(string, 4096, f)) != NULL) {
Jens Axboe45410ac2006-06-07 11:10:39 +0200593 if (ret)
594 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100595 if (is_empty_or_comment(p))
596 continue;
597 if (sscanf(p, "[%s]", name) != 1)
598 continue;
599
600 global = !strncmp(name, "global", 6);
601
602 name[strlen(name) - 1] = '\0';
603
604 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200605 if (!td) {
606 ret = 1;
607 break;
608 }
Jens Axboeebac4652005-12-08 15:25:21 +0100609
Jens Axboeebac4652005-12-08 15:25:21 +0100610 fgetpos(f, &off);
611 while ((p = fgets(string, 4096, f)) != NULL) {
612 if (is_empty_or_comment(p))
613 continue;
614 if (strstr(p, "["))
615 break;
Jens Axboeb6754f92006-05-30 14:29:32 +0200616 strip_blank_front(&p);
Jens Axboe4ae3f762006-05-30 13:35:14 +0200617 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200618
Jens Axboeebac4652005-12-08 15:25:21 +0100619 if (!check_int(p, "prio", &prio)) {
620#ifndef FIO_HAVE_IOPRIO
621 fprintf(stderr, "io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200622 ret = 1;
623 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100624#endif
Jens Axboe8756e4d2006-05-27 20:24:53 +0200625 td->ioprio |= prio;
Jens Axboeebac4652005-12-08 15:25:21 +0100626 fgetpos(f, &off);
627 continue;
628 }
629 if (!check_int(p, "prioclass", &prioclass)) {
630#ifndef FIO_HAVE_IOPRIO
631 fprintf(stderr, "io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200632 ret = 1;
633 break;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200634#else
Jens Axboe8756e4d2006-05-27 20:24:53 +0200635 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
Jens Axboeebac4652005-12-08 15:25:21 +0100636 fgetpos(f, &off);
637 continue;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200638#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100639 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200640 if (!check_int(p, "direct", &il)) {
641 td->odirect = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100642 fgetpos(f, &off);
643 continue;
644 }
645 if (!check_int(p, "rate", &td->rate)) {
646 fgetpos(f, &off);
647 continue;
648 }
649 if (!check_int(p, "ratemin", &td->ratemin)) {
650 fgetpos(f, &off);
651 continue;
652 }
653 if (!check_int(p, "ratecycle", &td->ratecycle)) {
654 fgetpos(f, &off);
655 continue;
656 }
657 if (!check_int(p, "thinktime", &td->thinktime)) {
658 fgetpos(f, &off);
659 continue;
660 }
661 if (!check_int(p, "cpumask", &cpu)) {
662#ifndef FIO_HAVE_CPU_AFFINITY
663 fprintf(stderr, "cpu affinity not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200664 ret = 1;
665 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100666#endif
667 fill_cpu_mask(td->cpumask, cpu);
668 fgetpos(f, &off);
669 continue;
670 }
671 if (!check_int(p, "fsync", &td->fsync_blocks)) {
672 fgetpos(f, &off);
Jens Axboefc1a4712006-05-30 13:04:05 +0200673 td->end_fsync = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100674 continue;
675 }
676 if (!check_int(p, "startdelay", &td->start_delay)) {
677 fgetpos(f, &off);
678 continue;
679 }
680 if (!check_int(p, "timeout", &td->timeout)) {
681 fgetpos(f, &off);
682 continue;
683 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200684 if (!check_int(p, "invalidate", &il)) {
685 td->invalidate_cache = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100686 fgetpos(f, &off);
687 continue;
688 }
689 if (!check_int(p, "iodepth", &td->iodepth)) {
690 fgetpos(f, &off);
691 continue;
692 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200693 if (!check_int(p, "sync", &il)) {
694 td->sync_io = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100695 fgetpos(f, &off);
696 continue;
697 }
698 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
699 fgetpos(f, &off);
700 continue;
701 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200702 if (!check_int(p, "create_serialize", &il)) {
703 td->create_serialize = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100704 fgetpos(f, &off);
705 continue;
706 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200707 if (!check_int(p, "create_fsync", &il)) {
708 td->create_fsync = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100709 fgetpos(f, &off);
710 continue;
711 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200712 if (!check_int(p, "end_fsync", &il)) {
713 td->end_fsync = il;
Jens Axboefc1a4712006-05-30 13:04:05 +0200714 fgetpos(f, &off);
715 continue;
716 }
Jens Axboeebac4652005-12-08 15:25:21 +0100717 if (!check_int(p, "loops", &td->loops)) {
718 fgetpos(f, &off);
719 continue;
720 }
721 if (!check_int(p, "numjobs", &td->numjobs)) {
722 fgetpos(f, &off);
723 continue;
724 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200725 if (!check_int(p, "overwrite", &il)) {
726 td->overwrite = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100727 fgetpos(f, &off);
728 continue;
729 }
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200730 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
731 fgetpos(f, &off);
732 continue;
733 }
734 if (!check_int(p, "rwmixread", &il)) {
735 if (il > 100)
736 il = 100;
737 td->rwmixread = il;
738 fgetpos(f, &off);
739 continue;
740 }
741 if (!check_int(p, "rwmixwrite", &il)) {
742 if (il > 100)
743 il = 100;
744 td->rwmixread = 100 - il;
745 fgetpos(f, &off);
746 continue;
747 }
Jens Axboeb6f4d882006-06-02 10:32:51 +0200748 if (!check_int(p, "nice", &td->nice)) {
749 fgetpos(f, &off);
750 continue;
751 }
Jens Axboeebac4652005-12-08 15:25:21 +0100752 if (!check_range(p, "bsrange", &ul1, &ul2)) {
753 if (ul1 > ul2) {
754 td->max_bs = ul1;
755 td->min_bs = ul2;
756 } else {
757 td->max_bs = ul2;
758 td->min_bs = ul1;
759 }
760 fgetpos(f, &off);
761 continue;
762 }
763 if (!check_strcnv(p, "bs", &ull)) {
764 td->bs = ull;
765 fgetpos(f, &off);
766 continue;
767 }
768 if (!check_strcnv(p, "size", &td->file_size)) {
769 fgetpos(f, &off);
770 continue;
771 }
772 if (!check_strcnv(p, "offset", &td->file_offset)) {
773 fgetpos(f, &off);
774 continue;
775 }
Jens Axboe20dc95c2005-12-09 10:29:35 +0100776 if (!check_strcnv(p, "zonesize", &td->zone_size)) {
777 fgetpos(f, &off);
778 continue;
779 }
780 if (!check_strcnv(p, "zoneskip", &td->zone_skip)) {
781 fgetpos(f, &off);
782 continue;
783 }
Jens Axboec04f7ec2006-05-31 10:13:16 +0200784 if (!check_strcnv(p, "lockmem", &mlock_size)) {
785 fgetpos(f, &off);
786 continue;
787 }
Jens Axboeef899b62006-06-06 09:31:00 +0200788 if (!check_strstore(p, "directory", tmpbuf)) {
789 td->directory = strdup(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100790 fgetpos(f, &off);
791 continue;
792 }
Jens Axboe01452052006-06-07 10:29:47 +0200793 if (!check_strstore(p, "name", tmpbuf)) {
794 snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
795 fgetpos(f, &off);
796 continue;
797 }
Jens Axboeebac4652005-12-08 15:25:21 +0100798 if (!check_str(p, "mem", str_mem_cb, td)) {
799 fgetpos(f, &off);
800 continue;
801 }
802 if (!check_str(p, "verify", str_verify_cb, td)) {
803 fgetpos(f, &off);
804 continue;
805 }
806 if (!check_str(p, "rw", str_rw_cb, td)) {
807 fgetpos(f, &off);
808 continue;
809 }
810 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
811 fgetpos(f, &off);
812 continue;
813 }
814 if (!check_strset(p, "create")) {
815 td->create_file = 1;
816 fgetpos(f, &off);
817 continue;
818 }
819 if (!check_strset(p, "exitall")) {
820 exitall_on_terminate = 1;
821 fgetpos(f, &off);
822 continue;
823 }
824 if (!check_strset(p, "stonewall")) {
825 td->stonewall = 1;
826 fgetpos(f, &off);
827 continue;
828 }
829 if (!check_strset(p, "thread")) {
830 td->use_thread = 1;
831 fgetpos(f, &off);
832 continue;
833 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200834 if (!check_strstore(p, "iolog", tmpbuf)) {
Jens Axboe4f693b92006-06-07 22:52:28 +0200835 if (td->write_iolog) {
836 fprintf(stderr, "fio: read iolog overrides given write_iolog\n");
Jens Axboeaf8c0b42006-06-07 22:33:17 +0200837 free(td->iolog_file);
Jens Axboe4f693b92006-06-07 22:52:28 +0200838 td->write_iolog = 0;
839 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200840 td->iolog_file = strdup(tmpbuf);
Jens Axboe843a7412006-06-01 21:14:21 -0700841 td->read_iolog = 1;
Jens Axboe843a7412006-06-01 21:14:21 -0700842 fgetpos(f, &off);
843 continue;
844 }
Jens Axboeaf8c0b42006-06-07 22:33:17 +0200845 if (!check_strstore(p, "write_iolog", tmpbuf)) {
846 if (!td->read_iolog) {
847 td->iolog_file = strdup(tmpbuf);
848 td->write_iolog = 1;
Jens Axboe4f693b92006-06-07 22:52:28 +0200849 } else
850 fprintf(stderr, "fio: read iolog overrides given write_iolog\n");
Jens Axboeaea47d42006-05-26 19:27:29 +0200851 fgetpos(f, &off);
852 continue;
853 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200854 if (!check_strstore(p, "exec_prerun", tmpbuf)) {
855 td->exec_prerun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200856 fgetpos(f, &off);
857 continue;
858 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200859 if (!check_strstore(p, "exec_postrun", tmpbuf)) {
860 td->exec_postrun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200861 fgetpos(f, &off);
862 continue;
863 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200864 if (!check_strstore(p, "ioscheduler", tmpbuf)) {
Jens Axboe22f78b32006-06-07 11:30:07 +0200865#ifndef FIO_HAVE_IOSCHED_SWITCH
866 fprintf(stderr, "io scheduler switching not available\n");
867 ret = 1;
868 break;
869#else
Jens Axboe80b9feb2006-06-07 10:34:49 +0200870 td->ioscheduler = strdup(tmpbuf);
Jens Axboeda867742006-06-06 10:39:10 -0700871 fgetpos(f, &off);
872 continue;
Jens Axboe22f78b32006-06-07 11:30:07 +0200873#endif
Jens Axboeda867742006-06-06 10:39:10 -0700874 }
Jens Axboeebac4652005-12-08 15:25:21 +0100875
Jens Axboe45410ac2006-06-07 11:10:39 +0200876 /*
877 * Don't break here, continue parsing options so we
878 * dump all the bad ones. Makes trial/error fixups
879 * easier on the user.
880 */
Jens Axboeebac4652005-12-08 15:25:21 +0100881 printf("Client%d: bad option %s\n",td->thread_number,p);
Jens Axboe45410ac2006-06-07 11:10:39 +0200882 ret = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100883 }
Jens Axboeebac4652005-12-08 15:25:21 +0100884
Jens Axboe45410ac2006-06-07 11:10:39 +0200885 if (!ret) {
886 fsetpos(f, &off);
887 ret = add_job(td, name, 0);
888 }
889 if (ret)
890 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100891 }
892
893 free(string);
894 free(name);
Jens Axboeef899b62006-06-06 09:31:00 +0200895 free(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100896 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200897 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100898}
899
900static int fill_def_thread(void)
901{
902 memset(&def_thread, 0, sizeof(def_thread));
903
904 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
905 perror("sched_getaffinity");
906 return 1;
907 }
908
909 /*
910 * fill globals
911 */
912 def_thread.ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200913 def_thread.iomix = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100914 def_thread.bs = DEF_BS;
915 def_thread.min_bs = -1;
916 def_thread.max_bs = -1;
917 def_thread.io_engine = DEF_IO_ENGINE;
918 strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
919 def_thread.odirect = DEF_ODIRECT;
920 def_thread.ratecycle = DEF_RATE_CYCLE;
921 def_thread.sequential = DEF_SEQUENTIAL;
922 def_thread.timeout = DEF_TIMEOUT;
923 def_thread.create_file = DEF_CREATE;
924 def_thread.overwrite = DEF_OVERWRITE;
925 def_thread.invalidate_cache = DEF_INVALIDATE;
926 def_thread.sync_io = DEF_SYNCIO;
927 def_thread.mem_type = MEM_MALLOC;
928 def_thread.bw_avg_time = DEF_BWAVGTIME;
929 def_thread.create_serialize = DEF_CREATE_SER;
930 def_thread.create_fsync = DEF_CREATE_FSYNC;
931 def_thread.loops = DEF_LOOPS;
932 def_thread.verify = DEF_VERIFY;
933 def_thread.stonewall = DEF_STONEWALL;
934 def_thread.numjobs = DEF_NUMJOBS;
935 def_thread.use_thread = DEF_USE_THREAD;
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200936 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
937 def_thread.rwmixread = DEF_RWMIX_READ;
Jens Axboeb6f4d882006-06-02 10:32:51 +0200938 def_thread.nice = DEF_NICE;
Jens Axboeebac4652005-12-08 15:25:21 +0100939#ifdef FIO_HAVE_DISK_UTIL
940 def_thread.do_disk_util = 1;
941#endif
942
943 return 0;
944}
945
Jens Axboe4785f992006-05-26 03:59:10 +0200946static void usage(char *name)
947{
948 printf("%s\n", fio_version_string);
949 printf("\t-s IO is sequential\n");
950 printf("\t-b Block size in KiB for each IO\n");
951 printf("\t-t Runtime in seconds\n");
952 printf("\t-R Exit all threads on failure to meet rate goal\n");
953 printf("\t-o Use O_DIRECT\n");
954 printf("\t-l Generate per-job latency logs\n");
955 printf("\t-w Generate per-job bandwidth logs\n");
956 printf("\t-f Job file (Required)\n");
957 printf("\t-v Print version info and exit\n");
958}
959
Jens Axboeebac4652005-12-08 15:25:21 +0100960static void parse_cmd_line(int argc, char *argv[])
961{
962 int c;
963
Jens Axboe4785f992006-05-26 03:59:10 +0200964 while ((c = getopt(argc, argv, "s:b:t:r:R:o:f:lwvh")) != EOF) {
Jens Axboeebac4652005-12-08 15:25:21 +0100965 switch (c) {
966 case 's':
967 def_thread.sequential = !!atoi(optarg);
968 break;
969 case 'b':
970 def_thread.bs = atoi(optarg);
971 def_thread.bs <<= 10;
972 if (!def_thread.bs) {
973 printf("bad block size\n");
974 def_thread.bs = DEF_BS;
975 }
976 break;
977 case 't':
978 def_thread.timeout = atoi(optarg);
979 break;
980 case 'r':
981 repeatable = !!atoi(optarg);
982 break;
983 case 'R':
984 rate_quit = !!atoi(optarg);
985 break;
986 case 'o':
987 def_thread.odirect = !!atoi(optarg);
988 break;
989 case 'f':
990 ini_file = strdup(optarg);
991 break;
992 case 'l':
993 write_lat_log = 1;
994 break;
995 case 'w':
996 write_bw_log = 1;
997 break;
Jens Axboe4785f992006-05-26 03:59:10 +0200998 case 'h':
999 usage(argv[0]);
1000 exit(0);
Jens Axboeebac4652005-12-08 15:25:21 +01001001 case 'v':
1002 printf("%s\n", fio_version_string);
1003 exit(0);
1004 }
1005 }
Jens Axboec9fad892006-05-27 17:26:50 +02001006
1007 if (!ini_file && argc > 1 && argv[argc - 1][0] != '-')
1008 ini_file = strdup(argv[argc - 1]);
Jens Axboeebac4652005-12-08 15:25:21 +01001009}
1010
1011static void free_shm(void)
1012{
1013 struct shmid_ds sbuf;
1014
1015 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001016 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001017 threads = NULL;
1018 shmctl(shm_id, IPC_RMID, &sbuf);
1019 }
1020}
1021
1022static int setup_thread_area(void)
1023{
1024 /*
1025 * 1024 is too much on some machines, scale max_jobs if
1026 * we get a failure that looks like too large a shm segment
1027 */
1028 do {
1029 int s = max_jobs * sizeof(struct thread_data);
1030
1031 shm_id = shmget(0, s, IPC_CREAT | 0600);
1032 if (shm_id != -1)
1033 break;
1034 if (errno != EINVAL) {
1035 perror("shmget");
1036 break;
1037 }
1038
1039 max_jobs >>= 1;
1040 } while (max_jobs);
1041
1042 if (shm_id == -1)
1043 return 1;
1044
1045 threads = shmat(shm_id, NULL, 0);
1046 if (threads == (void *) -1) {
1047 perror("shmat");
1048 return 1;
1049 }
1050
1051 atexit(free_shm);
1052 return 0;
1053}
1054
1055int parse_options(int argc, char *argv[])
1056{
1057 if (setup_thread_area())
1058 return 1;
1059 if (fill_def_thread())
1060 return 1;
1061
1062 parse_cmd_line(argc, argv);
1063
1064 if (!ini_file) {
1065 printf("Need job file\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001066 usage(argv[0]);
Jens Axboeebac4652005-12-08 15:25:21 +01001067 return 1;
1068 }
1069
Jens Axboe07261982006-06-07 10:51:12 +02001070 if (parse_jobs_ini(ini_file))
Jens Axboeebac4652005-12-08 15:25:21 +01001071 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +01001072
1073 return 0;
1074}