blob: 1fc694d373a7ce60d5e74431ecc282cfb7e93b69 [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 Axboe4ae3f762006-05-30 13:35:14 +020068 if (parent)
69 *td = *parent;
70 else
71 memset(td, 0, sizeof(*td));
Jens Axboeebac4652005-12-08 15:25:21 +010072
73 td->fd = -1;
74 td->thread_number = thread_number;
75
76 td->ddir = parent->ddir;
77 td->ioprio = parent->ioprio;
78 td->sequential = parent->sequential;
79 td->bs = parent->bs;
80 td->min_bs = parent->min_bs;
81 td->max_bs = parent->max_bs;
82 td->odirect = parent->odirect;
83 td->thinktime = parent->thinktime;
84 td->fsync_blocks = parent->fsync_blocks;
85 td->start_delay = parent->start_delay;
86 td->timeout = parent->timeout;
87 td->io_engine = parent->io_engine;
88 td->create_file = parent->create_file;
89 td->overwrite = parent->overwrite;
90 td->invalidate_cache = parent->invalidate_cache;
91 td->file_size = parent->file_size;
92 td->file_offset = parent->file_offset;
Jens Axboe20dc95c2005-12-09 10:29:35 +010093 td->zone_size = parent->zone_size;
94 td->zone_skip = parent->zone_skip;
Jens Axboeebac4652005-12-08 15:25:21 +010095 td->rate = parent->rate;
96 td->ratemin = parent->ratemin;
97 td->ratecycle = parent->ratecycle;
98 td->iodepth = parent->iodepth;
99 td->sync_io = parent->sync_io;
100 td->mem_type = parent->mem_type;
101 td->bw_avg_time = parent->bw_avg_time;
102 td->create_serialize = parent->create_serialize;
103 td->create_fsync = parent->create_fsync;
104 td->loops = parent->loops;
105 td->verify = parent->verify;
106 td->stonewall = parent->stonewall;
107 td->numjobs = parent->numjobs;
108 td->use_thread = parent->use_thread;
109 td->do_disk_util = parent->do_disk_util;
110 memcpy(&td->cpumask, &parent->cpumask, sizeof(td->cpumask));
111 strcpy(td->io_engine_name, parent->io_engine_name);
112
113 return td;
114}
115
116static void put_job(struct thread_data *td)
117{
118 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
119 thread_number--;
120}
121
Jens Axboe75154842006-06-01 13:56:09 +0200122static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100123{
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200124 char *ddir_str[] = { "read", "write", "randread", "randwrite",
125 "rw", NULL, "randrw" };
Jens Axboeebac4652005-12-08 15:25:21 +0100126 struct stat sb;
127 int numjobs, ddir;
128
129#ifndef FIO_HAVE_LIBAIO
130 if (td->io_engine == FIO_LIBAIO) {
131 fprintf(stderr, "Linux libaio not available\n");
132 return 1;
133 }
134#endif
135#ifndef FIO_HAVE_POSIXAIO
136 if (td->io_engine == FIO_POSIXAIO) {
137 fprintf(stderr, "posix aio not available\n");
138 return 1;
139 }
140#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100141
142 /*
143 * the def_thread is just for options, it's not a real job
144 */
145 if (td == &def_thread)
146 return 0;
147
148 if (td->io_engine & FIO_SYNCIO)
149 td->iodepth = 1;
150 else {
151 if (!td->iodepth)
152 td->iodepth = 1;
153 }
154
Jens Axboe20dc95c2005-12-09 10:29:35 +0100155 /*
156 * only really works for sequential io for now
157 */
158 if (td->zone_size && !td->sequential)
159 td->zone_size = 0;
160
Jens Axboeebac4652005-12-08 15:25:21 +0100161 td->filetype = FIO_TYPE_FILE;
Jens Axboe0af7b542006-02-17 10:10:12 +0100162 if (!stat(jobname, &sb)) {
163 if (S_ISBLK(sb.st_mode))
164 td->filetype = FIO_TYPE_BD;
165 else if (S_ISCHR(sb.st_mode))
166 td->filetype = FIO_TYPE_CHAR;
167 }
Jens Axboeebac4652005-12-08 15:25:21 +0100168
169 if (td->filetype == FIO_TYPE_FILE) {
Jens Axboee9c047a2006-06-07 21:13:04 +0200170 char tmp[PATH_MAX];
171
Jens Axboeef899b62006-06-06 09:31:00 +0200172 if (td->directory && td->directory[0] != '\0')
Jens Axboee9c047a2006-06-07 21:13:04 +0200173 sprintf(tmp, "%s/%s.%d", td->directory, jobname, td->jobnum);
Jens Axboeebac4652005-12-08 15:25:21 +0100174 else
Jens Axboee9c047a2006-06-07 21:13:04 +0200175 sprintf(tmp, "%s.%d", jobname, td->jobnum);
176 td->file_name = strdup(tmp);
Jens Axboeebac4652005-12-08 15:25:21 +0100177 } else
Jens Axboee9c047a2006-06-07 21:13:04 +0200178 td->file_name = strdup(jobname);
Jens Axboeebac4652005-12-08 15:25:21 +0100179
Jens Axboebbfd6b02006-06-07 19:42:54 +0200180 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100181
182 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
183 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
184 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
185
186 if (td->min_bs == -1U)
187 td->min_bs = td->bs;
188 if (td->max_bs == -1U)
189 td->max_bs = td->bs;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200190 if (td_read(td) && !td_rw(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100191 td->verify = 0;
192
193 if (td->stonewall && td->thread_number > 1)
194 groupid++;
195
196 td->groupid = groupid;
197
198 if (setup_rate(td))
199 goto err;
200
201 if (write_lat_log) {
202 setup_log(&td->slat_log);
203 setup_log(&td->clat_log);
204 }
205 if (write_bw_log)
206 setup_log(&td->bw_log);
207
Jens Axboe01452052006-06-07 10:29:47 +0200208 if (td->name[0] == '\0')
209 snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
210
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200211 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
Jens Axboe75154842006-06-01 13:56:09 +0200212
213 if (!job_add_num)
Jens Axboe01452052006-06-07 10:29:47 +0200214 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 +0200215 else if (job_add_num == 1)
216 printf("...\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100217
218 /*
219 * recurse add identical jobs, clear numjobs and stonewall options
220 * as they don't apply to sub-jobs
221 */
222 numjobs = td->numjobs;
223 while (--numjobs) {
224 struct thread_data *td_new = get_new_job(0, td);
225
226 if (!td_new)
227 goto err;
228
229 td_new->numjobs = 1;
230 td_new->stonewall = 0;
Jens Axboeeba09dd2006-05-22 20:20:54 +0200231 td_new->jobnum = numjobs;
Jens Axboe75154842006-06-01 13:56:09 +0200232 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100233
Jens Axboe75154842006-06-01 13:56:09 +0200234 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100235 goto err;
236 }
237 return 0;
238err:
239 put_job(td);
240 return -1;
241}
242
243int init_random_state(struct thread_data *td)
244{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200245 unsigned long seeds[4];
Jens Axboeebac4652005-12-08 15:25:21 +0100246 int fd, num_maps, blocks;
247
Jens Axboe1ac267b2006-05-31 20:29:00 +0200248 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100249 if (fd == -1) {
250 td_verror(td, errno);
251 return 1;
252 }
253
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200254 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100255 td_verror(td, EIO);
256 close(fd);
257 return 1;
258 }
259
260 close(fd);
261
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200262 os_random_seed(seeds[0], &td->bsrange_state);
263 os_random_seed(seeds[1], &td->verify_state);
264 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100265
266 if (td->sequential)
267 return 0;
268
269 if (repeatable)
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200270 seeds[3] = DEF_RANDSEED;
Jens Axboeebac4652005-12-08 15:25:21 +0100271
272 blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
273 num_maps = blocks / BLOCKS_PER_MAP;
274 td->file_map = malloc(num_maps * sizeof(long));
275 td->num_maps = num_maps;
276 memset(td->file_map, 0, num_maps * sizeof(long));
277
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200278 os_random_seed(seeds[3], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100279 return 0;
280}
281
282static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
283{
284#ifdef FIO_HAVE_CPU_AFFINITY
285 unsigned int i;
286
287 CPU_ZERO(&cpumask);
288
289 for (i = 0; i < sizeof(int) * 8; i++) {
290 if ((1 << i) & cpu)
291 CPU_SET(i, &cpumask);
292 }
293#endif
294}
295
296static unsigned long get_mult(char c)
297{
298 switch (c) {
299 case 'k':
300 case 'K':
301 return 1024;
302 case 'm':
303 case 'M':
304 return 1024 * 1024;
305 case 'g':
306 case 'G':
307 return 1024 * 1024 * 1024;
308 default:
309 return 1;
310 }
311}
312
313/*
314 * convert string after '=' into decimal value, noting any size suffix
315 */
316static int str_cnv(char *p, unsigned long long *val)
317{
318 char *str;
319 int len;
320
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100321 str = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100322 if (!str)
323 return 1;
324
325 str++;
326 len = strlen(str);
327
328 *val = strtoul(str, NULL, 10);
329 if (*val == ULONG_MAX && errno == ERANGE)
330 return 1;
331
Jens Axboe9d0d4242006-05-30 13:45:51 +0200332 *val *= get_mult(str[len - 1]);
Jens Axboeebac4652005-12-08 15:25:21 +0100333 return 0;
334}
335
336static int check_strcnv(char *p, char *name, unsigned long long *val)
337{
Jens Axboe20dc95c2005-12-09 10:29:35 +0100338 if (strncmp(p, name, strlen(name) - 1))
Jens Axboeebac4652005-12-08 15:25:21 +0100339 return 1;
340
341 return str_cnv(p, val);
342}
343
344static void strip_blank_front(char **p)
345{
346 char *s = *p;
347
Jens Axboe4ae3f762006-05-30 13:35:14 +0200348 while (isspace(*s))
Jens Axboeebac4652005-12-08 15:25:21 +0100349 s++;
350}
351
352static void strip_blank_end(char *p)
353{
Jens Axboe4ae3f762006-05-30 13:35:14 +0200354 char *s = p + strlen(p) - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100355
Jens Axboe4ae3f762006-05-30 13:35:14 +0200356 while (isspace(*s) || iscntrl(*s))
357 s--;
Jens Axboeaea47d42006-05-26 19:27:29 +0200358
Jens Axboe4ae3f762006-05-30 13:35:14 +0200359 *(s + 1) = '\0';
Jens Axboeaea47d42006-05-26 19:27:29 +0200360}
361
Jens Axboeebac4652005-12-08 15:25:21 +0100362typedef int (str_cb_fn)(struct thread_data *, char *);
363
364static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
365{
Jens Axboe843a7412006-06-01 21:14:21 -0700366 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100367
Jens Axboe843a7412006-06-01 21:14:21 -0700368 if (strncmp(p, name, strlen(name)))
369 return 1;
370
371 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100372 if (!s)
373 return 1;
374
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100375 s = strchr(s, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100376 if (!s)
377 return 1;
378
379 s++;
380 strip_blank_front(&s);
381 return cb(td, s);
382}
383
384static int check_strstore(char *p, char *name, char *dest)
385{
Jens Axboe843a7412006-06-01 21:14:21 -0700386 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100387
Jens Axboe843a7412006-06-01 21:14:21 -0700388 if (strncmp(p, name, strlen(name)))
389 return 1;
390
391 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100392 if (!s)
393 return 1;
394
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100395 s = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100396 if (!s)
397 return 1;
398
399 s++;
400 strip_blank_front(&s);
401
402 strcpy(dest, s);
Jens Axboeebac4652005-12-08 15:25:21 +0100403 return 0;
404}
405
Jens Axboe01617be2005-12-08 19:50:40 +0100406static int __check_range(char *str, unsigned long *val)
Jens Axboeebac4652005-12-08 15:25:21 +0100407{
Jens Axboe01617be2005-12-08 19:50:40 +0100408 char suffix;
Jens Axboeebac4652005-12-08 15:25:21 +0100409
Jens Axboe01617be2005-12-08 19:50:40 +0100410 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
411 *val *= get_mult(suffix);
Jens Axboeebac4652005-12-08 15:25:21 +0100412 return 0;
413 }
414
Jens Axboe01617be2005-12-08 19:50:40 +0100415 if (sscanf(str, "%lu", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100416 return 0;
417
418 return 1;
Jens Axboe01617be2005-12-08 19:50:40 +0100419}
Jens Axboeebac4652005-12-08 15:25:21 +0100420
Jens Axboe01617be2005-12-08 19:50:40 +0100421static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
422{
423 char option[128];
424 char *str, *p1, *p2;
425
Jens Axboe843a7412006-06-01 21:14:21 -0700426 if (strncmp(p, name, strlen(name)))
427 return 1;
428
Jens Axboe01617be2005-12-08 19:50:40 +0100429 strcpy(option, p);
430 p = option;
431
432 str = strstr(p, name);
433 if (!str)
434 return 1;
435
436 p += strlen(name);
437
438 str = strchr(p, '=');
439 if (!str)
440 return 1;
441
442 /*
443 * 'p' now holds whatever is after the '=' sign
444 */
445 p1 = str + 1;
446
447 /*
448 * terminate p1 at the '-' sign
449 */
450 p = strchr(p1, '-');
451 if (!p)
452 return 1;
453
454 p2 = p + 1;
455 *p = '\0';
456
457 if (!__check_range(p1, s) && !__check_range(p2, e))
458 return 0;
459
460 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100461}
462
463static int check_int(char *p, char *name, unsigned int *val)
464{
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100465 char *str;
Jens Axboeebac4652005-12-08 15:25:21 +0100466
Jens Axboeb6754f92006-05-30 14:29:32 +0200467 if (strncmp(p, name, strlen(name)))
468 return 1;
469
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100470 str = strstr(p, name);
471 if (!str)
472 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100473
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100474 str = strchr(p, '=');
475 if (!str)
476 return 1;
477
478 str++;
479
480 if (sscanf(str, "%u", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100481 return 0;
482
483 return 1;
484}
485
486static int check_strset(char *p, char *name)
487{
488 return strncmp(p, name, strlen(name));
489}
490
491static int is_empty_or_comment(char *line)
492{
493 unsigned int i;
494
495 for (i = 0; i < strlen(line); i++) {
496 if (line[i] == ';')
497 return 1;
498 if (!isspace(line[i]) && !iscntrl(line[i]))
499 return 0;
500 }
501
502 return 1;
503}
504
505static int str_rw_cb(struct thread_data *td, char *mem)
506{
507 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
508 td->ddir = DDIR_READ;
509 td->sequential = 1;
510 return 0;
511 } else if (!strncmp(mem, "randread", 8)) {
512 td->ddir = DDIR_READ;
513 td->sequential = 0;
514 return 0;
515 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
516 td->ddir = DDIR_WRITE;
517 td->sequential = 1;
518 return 0;
519 } else if (!strncmp(mem, "randwrite", 9)) {
520 td->ddir = DDIR_WRITE;
521 td->sequential = 0;
522 return 0;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200523 } else if (!strncmp(mem, "rw", 2)) {
524 td->ddir = 0;
525 td->iomix = 1;
526 td->sequential = 1;
527 return 0;
528 } else if (!strncmp(mem, "randrw", 6)) {
529 td->ddir = 0;
530 td->iomix = 1;
531 td->sequential = 0;
532 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100533 }
534
Jens Axboe07261982006-06-07 10:51:12 +0200535 fprintf(stderr, "fio: data direction: read, write, randread, randwrite, rw, randrw\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100536 return 1;
537}
538
539static int str_verify_cb(struct thread_data *td, char *mem)
540{
541 if (!strncmp(mem, "0", 1)) {
542 td->verify = VERIFY_NONE;
543 return 0;
544 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
545 td->verify = VERIFY_MD5;
546 return 0;
547 } else if (!strncmp(mem, "crc32", 5)) {
548 td->verify = VERIFY_CRC32;
549 return 0;
550 }
551
Jens Axboe07261982006-06-07 10:51:12 +0200552 fprintf(stderr, "fio: verify types: md5, crc32\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100553 return 1;
554}
555
556static int str_mem_cb(struct thread_data *td, char *mem)
557{
558 if (!strncmp(mem, "malloc", 6)) {
559 td->mem_type = MEM_MALLOC;
560 return 0;
561 } else if (!strncmp(mem, "shm", 3)) {
562 td->mem_type = MEM_SHM;
563 return 0;
564 } else if (!strncmp(mem, "mmap", 4)) {
565 td->mem_type = MEM_MMAP;
566 return 0;
567 }
568
Jens Axboe07261982006-06-07 10:51:12 +0200569 fprintf(stderr, "fio: mem type: malloc, shm, mmap\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100570 return 1;
571}
572
573static int str_ioengine_cb(struct thread_data *td, char *str)
574{
575 if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
576 !strncmp(str, "libaio", 6)) {
577 strcpy(td->io_engine_name, "libaio");
578 td->io_engine = FIO_LIBAIO;
579 return 0;
580 } else if (!strncmp(str, "posixaio", 8)) {
581 strcpy(td->io_engine_name, "posixaio");
582 td->io_engine = FIO_POSIXAIO;
583 return 0;
584 } else if (!strncmp(str, "sync", 4)) {
585 strcpy(td->io_engine_name, "sync");
586 td->io_engine = FIO_SYNCIO;
587 return 0;
588 } else if (!strncmp(str, "mmap", 4)) {
589 strcpy(td->io_engine_name, "mmap");
590 td->io_engine = FIO_MMAPIO;
591 return 0;
592 } else if (!strncmp(str, "sgio", 4)) {
593 strcpy(td->io_engine_name, "sgio");
594 td->io_engine = FIO_SGIO;
595 return 0;
Jens Axboe8756e4d2006-05-27 20:24:53 +0200596 } else if (!strncmp(str, "splice", 6)) {
597 strcpy(td->io_engine_name, "splice");
598 td->io_engine = FIO_SPLICEIO;
599 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100600 }
601
Jens Axboe07261982006-06-07 10:51:12 +0200602 fprintf(stderr, "fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100603 return 1;
604}
605
Jens Axboe07261982006-06-07 10:51:12 +0200606/*
607 * This is our [ini] type file parser.
608 */
Jens Axboeebac4652005-12-08 15:25:21 +0100609int parse_jobs_ini(char *file)
610{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200611 unsigned int prioclass, prio, cpu, global, il;
Jens Axboeebac4652005-12-08 15:25:21 +0100612 unsigned long long ull;
613 unsigned long ul1, ul2;
614 struct thread_data *td;
Jens Axboeef899b62006-06-06 09:31:00 +0200615 char *string, *name, *tmpbuf;
Jens Axboeebac4652005-12-08 15:25:21 +0100616 fpos_t off;
617 FILE *f;
618 char *p;
Jens Axboe45410ac2006-06-07 11:10:39 +0200619 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100620
621 f = fopen(file, "r");
622 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200623 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100624 return 1;
625 }
626
627 string = malloc(4096);
628 name = malloc(256);
Jens Axboeef899b62006-06-06 09:31:00 +0200629 tmpbuf = malloc(4096);
Jens Axboeebac4652005-12-08 15:25:21 +0100630
631 while ((p = fgets(string, 4096, f)) != NULL) {
Jens Axboe45410ac2006-06-07 11:10:39 +0200632 if (ret)
633 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100634 if (is_empty_or_comment(p))
635 continue;
636 if (sscanf(p, "[%s]", name) != 1)
637 continue;
638
639 global = !strncmp(name, "global", 6);
640
641 name[strlen(name) - 1] = '\0';
642
643 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200644 if (!td) {
645 ret = 1;
646 break;
647 }
Jens Axboeebac4652005-12-08 15:25:21 +0100648
Jens Axboeebac4652005-12-08 15:25:21 +0100649 fgetpos(f, &off);
650 while ((p = fgets(string, 4096, f)) != NULL) {
651 if (is_empty_or_comment(p))
652 continue;
653 if (strstr(p, "["))
654 break;
Jens Axboeb6754f92006-05-30 14:29:32 +0200655 strip_blank_front(&p);
Jens Axboe4ae3f762006-05-30 13:35:14 +0200656 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200657
Jens Axboeebac4652005-12-08 15:25:21 +0100658 if (!check_int(p, "prio", &prio)) {
659#ifndef FIO_HAVE_IOPRIO
660 fprintf(stderr, "io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200661 ret = 1;
662 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100663#endif
Jens Axboe8756e4d2006-05-27 20:24:53 +0200664 td->ioprio |= prio;
Jens Axboeebac4652005-12-08 15:25:21 +0100665 fgetpos(f, &off);
666 continue;
667 }
668 if (!check_int(p, "prioclass", &prioclass)) {
669#ifndef FIO_HAVE_IOPRIO
670 fprintf(stderr, "io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200671 ret = 1;
672 break;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200673#else
Jens Axboe8756e4d2006-05-27 20:24:53 +0200674 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
Jens Axboeebac4652005-12-08 15:25:21 +0100675 fgetpos(f, &off);
676 continue;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200677#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100678 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200679 if (!check_int(p, "direct", &il)) {
680 td->odirect = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100681 fgetpos(f, &off);
682 continue;
683 }
684 if (!check_int(p, "rate", &td->rate)) {
685 fgetpos(f, &off);
686 continue;
687 }
688 if (!check_int(p, "ratemin", &td->ratemin)) {
689 fgetpos(f, &off);
690 continue;
691 }
692 if (!check_int(p, "ratecycle", &td->ratecycle)) {
693 fgetpos(f, &off);
694 continue;
695 }
696 if (!check_int(p, "thinktime", &td->thinktime)) {
697 fgetpos(f, &off);
698 continue;
699 }
700 if (!check_int(p, "cpumask", &cpu)) {
701#ifndef FIO_HAVE_CPU_AFFINITY
702 fprintf(stderr, "cpu affinity not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200703 ret = 1;
704 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100705#endif
706 fill_cpu_mask(td->cpumask, cpu);
707 fgetpos(f, &off);
708 continue;
709 }
710 if (!check_int(p, "fsync", &td->fsync_blocks)) {
711 fgetpos(f, &off);
Jens Axboefc1a4712006-05-30 13:04:05 +0200712 td->end_fsync = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100713 continue;
714 }
715 if (!check_int(p, "startdelay", &td->start_delay)) {
716 fgetpos(f, &off);
717 continue;
718 }
719 if (!check_int(p, "timeout", &td->timeout)) {
720 fgetpos(f, &off);
721 continue;
722 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200723 if (!check_int(p, "invalidate", &il)) {
724 td->invalidate_cache = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100725 fgetpos(f, &off);
726 continue;
727 }
728 if (!check_int(p, "iodepth", &td->iodepth)) {
729 fgetpos(f, &off);
730 continue;
731 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200732 if (!check_int(p, "sync", &il)) {
733 td->sync_io = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100734 fgetpos(f, &off);
735 continue;
736 }
737 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
738 fgetpos(f, &off);
739 continue;
740 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200741 if (!check_int(p, "create_serialize", &il)) {
742 td->create_serialize = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100743 fgetpos(f, &off);
744 continue;
745 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200746 if (!check_int(p, "create_fsync", &il)) {
747 td->create_fsync = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100748 fgetpos(f, &off);
749 continue;
750 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200751 if (!check_int(p, "end_fsync", &il)) {
752 td->end_fsync = il;
Jens Axboefc1a4712006-05-30 13:04:05 +0200753 fgetpos(f, &off);
754 continue;
755 }
Jens Axboeebac4652005-12-08 15:25:21 +0100756 if (!check_int(p, "loops", &td->loops)) {
757 fgetpos(f, &off);
758 continue;
759 }
760 if (!check_int(p, "numjobs", &td->numjobs)) {
761 fgetpos(f, &off);
762 continue;
763 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200764 if (!check_int(p, "overwrite", &il)) {
765 td->overwrite = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100766 fgetpos(f, &off);
767 continue;
768 }
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200769 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
770 fgetpos(f, &off);
771 continue;
772 }
773 if (!check_int(p, "rwmixread", &il)) {
774 if (il > 100)
775 il = 100;
776 td->rwmixread = il;
777 fgetpos(f, &off);
778 continue;
779 }
780 if (!check_int(p, "rwmixwrite", &il)) {
781 if (il > 100)
782 il = 100;
783 td->rwmixread = 100 - il;
784 fgetpos(f, &off);
785 continue;
786 }
Jens Axboeb6f4d882006-06-02 10:32:51 +0200787 if (!check_int(p, "nice", &td->nice)) {
788 fgetpos(f, &off);
789 continue;
790 }
Jens Axboeebac4652005-12-08 15:25:21 +0100791 if (!check_range(p, "bsrange", &ul1, &ul2)) {
792 if (ul1 > ul2) {
793 td->max_bs = ul1;
794 td->min_bs = ul2;
795 } else {
796 td->max_bs = ul2;
797 td->min_bs = ul1;
798 }
799 fgetpos(f, &off);
800 continue;
801 }
802 if (!check_strcnv(p, "bs", &ull)) {
803 td->bs = ull;
804 fgetpos(f, &off);
805 continue;
806 }
807 if (!check_strcnv(p, "size", &td->file_size)) {
808 fgetpos(f, &off);
809 continue;
810 }
811 if (!check_strcnv(p, "offset", &td->file_offset)) {
812 fgetpos(f, &off);
813 continue;
814 }
Jens Axboe20dc95c2005-12-09 10:29:35 +0100815 if (!check_strcnv(p, "zonesize", &td->zone_size)) {
816 fgetpos(f, &off);
817 continue;
818 }
819 if (!check_strcnv(p, "zoneskip", &td->zone_skip)) {
820 fgetpos(f, &off);
821 continue;
822 }
Jens Axboec04f7ec2006-05-31 10:13:16 +0200823 if (!check_strcnv(p, "lockmem", &mlock_size)) {
824 fgetpos(f, &off);
825 continue;
826 }
Jens Axboeef899b62006-06-06 09:31:00 +0200827 if (!check_strstore(p, "directory", tmpbuf)) {
828 td->directory = strdup(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100829 fgetpos(f, &off);
830 continue;
831 }
Jens Axboe01452052006-06-07 10:29:47 +0200832 if (!check_strstore(p, "name", tmpbuf)) {
833 snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
834 fgetpos(f, &off);
835 continue;
836 }
Jens Axboeebac4652005-12-08 15:25:21 +0100837 if (!check_str(p, "mem", str_mem_cb, td)) {
838 fgetpos(f, &off);
839 continue;
840 }
841 if (!check_str(p, "verify", str_verify_cb, td)) {
842 fgetpos(f, &off);
843 continue;
844 }
845 if (!check_str(p, "rw", str_rw_cb, td)) {
846 fgetpos(f, &off);
847 continue;
848 }
849 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
850 fgetpos(f, &off);
851 continue;
852 }
853 if (!check_strset(p, "create")) {
854 td->create_file = 1;
855 fgetpos(f, &off);
856 continue;
857 }
858 if (!check_strset(p, "exitall")) {
859 exitall_on_terminate = 1;
860 fgetpos(f, &off);
861 continue;
862 }
863 if (!check_strset(p, "stonewall")) {
864 td->stonewall = 1;
865 fgetpos(f, &off);
866 continue;
867 }
868 if (!check_strset(p, "thread")) {
869 td->use_thread = 1;
870 fgetpos(f, &off);
871 continue;
872 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200873 if (!check_strstore(p, "iolog", tmpbuf)) {
874 td->iolog_file = strdup(tmpbuf);
Jens Axboe843a7412006-06-01 21:14:21 -0700875 td->read_iolog = 1;
876 td->write_iolog = 0;
877 fgetpos(f, &off);
878 continue;
879 }
880 if (!td->read_iolog &&
Jens Axboe80b9feb2006-06-07 10:34:49 +0200881 !check_strstore(p, "write_iolog", tmpbuf)) {
882 td->iolog_file = strdup(tmpbuf);
Jens Axboe843a7412006-06-01 21:14:21 -0700883 td->write_iolog = 1;
Jens Axboeaea47d42006-05-26 19:27:29 +0200884 fgetpos(f, &off);
885 continue;
886 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200887 if (!check_strstore(p, "exec_prerun", tmpbuf)) {
888 td->exec_prerun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200889 fgetpos(f, &off);
890 continue;
891 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200892 if (!check_strstore(p, "exec_postrun", tmpbuf)) {
893 td->exec_postrun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200894 fgetpos(f, &off);
895 continue;
896 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200897 if (!check_strstore(p, "ioscheduler", tmpbuf)) {
Jens Axboe22f78b32006-06-07 11:30:07 +0200898#ifndef FIO_HAVE_IOSCHED_SWITCH
899 fprintf(stderr, "io scheduler switching not available\n");
900 ret = 1;
901 break;
902#else
Jens Axboe80b9feb2006-06-07 10:34:49 +0200903 td->ioscheduler = strdup(tmpbuf);
Jens Axboeda867742006-06-06 10:39:10 -0700904 fgetpos(f, &off);
905 continue;
Jens Axboe22f78b32006-06-07 11:30:07 +0200906#endif
Jens Axboeda867742006-06-06 10:39:10 -0700907 }
Jens Axboeebac4652005-12-08 15:25:21 +0100908
Jens Axboe45410ac2006-06-07 11:10:39 +0200909 /*
910 * Don't break here, continue parsing options so we
911 * dump all the bad ones. Makes trial/error fixups
912 * easier on the user.
913 */
Jens Axboeebac4652005-12-08 15:25:21 +0100914 printf("Client%d: bad option %s\n",td->thread_number,p);
Jens Axboe45410ac2006-06-07 11:10:39 +0200915 ret = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100916 }
Jens Axboeebac4652005-12-08 15:25:21 +0100917
Jens Axboe45410ac2006-06-07 11:10:39 +0200918 if (!ret) {
919 fsetpos(f, &off);
920 ret = add_job(td, name, 0);
921 }
922 if (ret)
923 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100924 }
925
926 free(string);
927 free(name);
Jens Axboeef899b62006-06-06 09:31:00 +0200928 free(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100929 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200930 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100931}
932
933static int fill_def_thread(void)
934{
935 memset(&def_thread, 0, sizeof(def_thread));
936
937 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
938 perror("sched_getaffinity");
939 return 1;
940 }
941
942 /*
943 * fill globals
944 */
945 def_thread.ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200946 def_thread.iomix = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100947 def_thread.bs = DEF_BS;
948 def_thread.min_bs = -1;
949 def_thread.max_bs = -1;
950 def_thread.io_engine = DEF_IO_ENGINE;
951 strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
952 def_thread.odirect = DEF_ODIRECT;
953 def_thread.ratecycle = DEF_RATE_CYCLE;
954 def_thread.sequential = DEF_SEQUENTIAL;
955 def_thread.timeout = DEF_TIMEOUT;
956 def_thread.create_file = DEF_CREATE;
957 def_thread.overwrite = DEF_OVERWRITE;
958 def_thread.invalidate_cache = DEF_INVALIDATE;
959 def_thread.sync_io = DEF_SYNCIO;
960 def_thread.mem_type = MEM_MALLOC;
961 def_thread.bw_avg_time = DEF_BWAVGTIME;
962 def_thread.create_serialize = DEF_CREATE_SER;
963 def_thread.create_fsync = DEF_CREATE_FSYNC;
964 def_thread.loops = DEF_LOOPS;
965 def_thread.verify = DEF_VERIFY;
966 def_thread.stonewall = DEF_STONEWALL;
967 def_thread.numjobs = DEF_NUMJOBS;
968 def_thread.use_thread = DEF_USE_THREAD;
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200969 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
970 def_thread.rwmixread = DEF_RWMIX_READ;
Jens Axboeb6f4d882006-06-02 10:32:51 +0200971 def_thread.nice = DEF_NICE;
Jens Axboeebac4652005-12-08 15:25:21 +0100972#ifdef FIO_HAVE_DISK_UTIL
973 def_thread.do_disk_util = 1;
974#endif
975
976 return 0;
977}
978
Jens Axboe4785f992006-05-26 03:59:10 +0200979static void usage(char *name)
980{
981 printf("%s\n", fio_version_string);
982 printf("\t-s IO is sequential\n");
983 printf("\t-b Block size in KiB for each IO\n");
984 printf("\t-t Runtime in seconds\n");
985 printf("\t-R Exit all threads on failure to meet rate goal\n");
986 printf("\t-o Use O_DIRECT\n");
987 printf("\t-l Generate per-job latency logs\n");
988 printf("\t-w Generate per-job bandwidth logs\n");
989 printf("\t-f Job file (Required)\n");
990 printf("\t-v Print version info and exit\n");
991}
992
Jens Axboeebac4652005-12-08 15:25:21 +0100993static void parse_cmd_line(int argc, char *argv[])
994{
995 int c;
996
Jens Axboe4785f992006-05-26 03:59:10 +0200997 while ((c = getopt(argc, argv, "s:b:t:r:R:o:f:lwvh")) != EOF) {
Jens Axboeebac4652005-12-08 15:25:21 +0100998 switch (c) {
999 case 's':
1000 def_thread.sequential = !!atoi(optarg);
1001 break;
1002 case 'b':
1003 def_thread.bs = atoi(optarg);
1004 def_thread.bs <<= 10;
1005 if (!def_thread.bs) {
1006 printf("bad block size\n");
1007 def_thread.bs = DEF_BS;
1008 }
1009 break;
1010 case 't':
1011 def_thread.timeout = atoi(optarg);
1012 break;
1013 case 'r':
1014 repeatable = !!atoi(optarg);
1015 break;
1016 case 'R':
1017 rate_quit = !!atoi(optarg);
1018 break;
1019 case 'o':
1020 def_thread.odirect = !!atoi(optarg);
1021 break;
1022 case 'f':
1023 ini_file = strdup(optarg);
1024 break;
1025 case 'l':
1026 write_lat_log = 1;
1027 break;
1028 case 'w':
1029 write_bw_log = 1;
1030 break;
Jens Axboe4785f992006-05-26 03:59:10 +02001031 case 'h':
1032 usage(argv[0]);
1033 exit(0);
Jens Axboeebac4652005-12-08 15:25:21 +01001034 case 'v':
1035 printf("%s\n", fio_version_string);
1036 exit(0);
1037 }
1038 }
Jens Axboec9fad892006-05-27 17:26:50 +02001039
1040 if (!ini_file && argc > 1 && argv[argc - 1][0] != '-')
1041 ini_file = strdup(argv[argc - 1]);
Jens Axboeebac4652005-12-08 15:25:21 +01001042}
1043
1044static void free_shm(void)
1045{
1046 struct shmid_ds sbuf;
1047
1048 if (threads) {
1049 shmdt(threads);
1050 threads = NULL;
1051 shmctl(shm_id, IPC_RMID, &sbuf);
1052 }
1053}
1054
1055static int setup_thread_area(void)
1056{
1057 /*
1058 * 1024 is too much on some machines, scale max_jobs if
1059 * we get a failure that looks like too large a shm segment
1060 */
1061 do {
1062 int s = max_jobs * sizeof(struct thread_data);
1063
1064 shm_id = shmget(0, s, IPC_CREAT | 0600);
1065 if (shm_id != -1)
1066 break;
1067 if (errno != EINVAL) {
1068 perror("shmget");
1069 break;
1070 }
1071
1072 max_jobs >>= 1;
1073 } while (max_jobs);
1074
1075 if (shm_id == -1)
1076 return 1;
1077
1078 threads = shmat(shm_id, NULL, 0);
1079 if (threads == (void *) -1) {
1080 perror("shmat");
1081 return 1;
1082 }
1083
1084 atexit(free_shm);
1085 return 0;
1086}
1087
1088int parse_options(int argc, char *argv[])
1089{
1090 if (setup_thread_area())
1091 return 1;
1092 if (fill_def_thread())
1093 return 1;
1094
1095 parse_cmd_line(argc, argv);
1096
1097 if (!ini_file) {
1098 printf("Need job file\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001099 usage(argv[0]);
Jens Axboeebac4652005-12-08 15:25:21 +01001100 return 1;
1101 }
1102
Jens Axboe07261982006-06-07 10:51:12 +02001103 if (parse_jobs_ini(ini_file))
Jens Axboeebac4652005-12-08 15:25:21 +01001104 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +01001105
1106 return 0;
1107}