blob: 8c6b2e1e336c7a572902f346336969157da3231b [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 Axboeef899b62006-06-06 09:31:00 +0200170 if (td->directory && td->directory[0] != '\0')
Jens Axboeeba09dd2006-05-22 20:20:54 +0200171 sprintf(td->file_name, "%s/%s.%d", td->directory, jobname, td->jobnum);
Jens Axboeebac4652005-12-08 15:25:21 +0100172 else
Jens Axboeeba09dd2006-05-22 20:20:54 +0200173 sprintf(td->file_name, "%s.%d", jobname, td->jobnum);
Jens Axboeebac4652005-12-08 15:25:21 +0100174 } else
Jens Axboeaea47d42006-05-26 19:27:29 +0200175 strncpy(td->file_name, jobname, sizeof(td->file_name) - 1);
Jens Axboeebac4652005-12-08 15:25:21 +0100176
Jens Axboebbfd6b02006-06-07 19:42:54 +0200177 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100178
179 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
180 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
181 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
182
183 if (td->min_bs == -1U)
184 td->min_bs = td->bs;
185 if (td->max_bs == -1U)
186 td->max_bs = td->bs;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200187 if (td_read(td) && !td_rw(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100188 td->verify = 0;
189
190 if (td->stonewall && td->thread_number > 1)
191 groupid++;
192
193 td->groupid = groupid;
194
195 if (setup_rate(td))
196 goto err;
197
198 if (write_lat_log) {
199 setup_log(&td->slat_log);
200 setup_log(&td->clat_log);
201 }
202 if (write_bw_log)
203 setup_log(&td->bw_log);
204
Jens Axboe01452052006-06-07 10:29:47 +0200205 if (td->name[0] == '\0')
206 snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
207
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200208 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
Jens Axboe75154842006-06-01 13:56:09 +0200209
210 if (!job_add_num)
Jens Axboe01452052006-06-07 10:29:47 +0200211 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 +0200212 else if (job_add_num == 1)
213 printf("...\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100214
215 /*
216 * recurse add identical jobs, clear numjobs and stonewall options
217 * as they don't apply to sub-jobs
218 */
219 numjobs = td->numjobs;
220 while (--numjobs) {
221 struct thread_data *td_new = get_new_job(0, td);
222
223 if (!td_new)
224 goto err;
225
226 td_new->numjobs = 1;
227 td_new->stonewall = 0;
Jens Axboeeba09dd2006-05-22 20:20:54 +0200228 td_new->jobnum = numjobs;
Jens Axboe75154842006-06-01 13:56:09 +0200229 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100230
Jens Axboe75154842006-06-01 13:56:09 +0200231 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100232 goto err;
233 }
234 return 0;
235err:
236 put_job(td);
237 return -1;
238}
239
240int init_random_state(struct thread_data *td)
241{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200242 unsigned long seeds[4];
Jens Axboeebac4652005-12-08 15:25:21 +0100243 int fd, num_maps, blocks;
244
Jens Axboe1ac267b2006-05-31 20:29:00 +0200245 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100246 if (fd == -1) {
247 td_verror(td, errno);
248 return 1;
249 }
250
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200251 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100252 td_verror(td, EIO);
253 close(fd);
254 return 1;
255 }
256
257 close(fd);
258
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200259 os_random_seed(seeds[0], &td->bsrange_state);
260 os_random_seed(seeds[1], &td->verify_state);
261 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100262
263 if (td->sequential)
264 return 0;
265
266 if (repeatable)
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200267 seeds[3] = DEF_RANDSEED;
Jens Axboeebac4652005-12-08 15:25:21 +0100268
269 blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
270 num_maps = blocks / BLOCKS_PER_MAP;
271 td->file_map = malloc(num_maps * sizeof(long));
272 td->num_maps = num_maps;
273 memset(td->file_map, 0, num_maps * sizeof(long));
274
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200275 os_random_seed(seeds[3], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100276 return 0;
277}
278
279static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
280{
281#ifdef FIO_HAVE_CPU_AFFINITY
282 unsigned int i;
283
284 CPU_ZERO(&cpumask);
285
286 for (i = 0; i < sizeof(int) * 8; i++) {
287 if ((1 << i) & cpu)
288 CPU_SET(i, &cpumask);
289 }
290#endif
291}
292
293static unsigned long get_mult(char c)
294{
295 switch (c) {
296 case 'k':
297 case 'K':
298 return 1024;
299 case 'm':
300 case 'M':
301 return 1024 * 1024;
302 case 'g':
303 case 'G':
304 return 1024 * 1024 * 1024;
305 default:
306 return 1;
307 }
308}
309
310/*
311 * convert string after '=' into decimal value, noting any size suffix
312 */
313static int str_cnv(char *p, unsigned long long *val)
314{
315 char *str;
316 int len;
317
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100318 str = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100319 if (!str)
320 return 1;
321
322 str++;
323 len = strlen(str);
324
325 *val = strtoul(str, NULL, 10);
326 if (*val == ULONG_MAX && errno == ERANGE)
327 return 1;
328
Jens Axboe9d0d4242006-05-30 13:45:51 +0200329 *val *= get_mult(str[len - 1]);
Jens Axboeebac4652005-12-08 15:25:21 +0100330 return 0;
331}
332
333static int check_strcnv(char *p, char *name, unsigned long long *val)
334{
Jens Axboe20dc95c2005-12-09 10:29:35 +0100335 if (strncmp(p, name, strlen(name) - 1))
Jens Axboeebac4652005-12-08 15:25:21 +0100336 return 1;
337
338 return str_cnv(p, val);
339}
340
341static void strip_blank_front(char **p)
342{
343 char *s = *p;
344
Jens Axboe4ae3f762006-05-30 13:35:14 +0200345 while (isspace(*s))
Jens Axboeebac4652005-12-08 15:25:21 +0100346 s++;
347}
348
349static void strip_blank_end(char *p)
350{
Jens Axboe4ae3f762006-05-30 13:35:14 +0200351 char *s = p + strlen(p) - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100352
Jens Axboe4ae3f762006-05-30 13:35:14 +0200353 while (isspace(*s) || iscntrl(*s))
354 s--;
Jens Axboeaea47d42006-05-26 19:27:29 +0200355
Jens Axboe4ae3f762006-05-30 13:35:14 +0200356 *(s + 1) = '\0';
Jens Axboeaea47d42006-05-26 19:27:29 +0200357}
358
Jens Axboeebac4652005-12-08 15:25:21 +0100359typedef int (str_cb_fn)(struct thread_data *, char *);
360
361static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
362{
Jens Axboe843a7412006-06-01 21:14:21 -0700363 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100364
Jens Axboe843a7412006-06-01 21:14:21 -0700365 if (strncmp(p, name, strlen(name)))
366 return 1;
367
368 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100369 if (!s)
370 return 1;
371
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100372 s = strchr(s, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100373 if (!s)
374 return 1;
375
376 s++;
377 strip_blank_front(&s);
378 return cb(td, s);
379}
380
381static int check_strstore(char *p, char *name, char *dest)
382{
Jens Axboe843a7412006-06-01 21:14:21 -0700383 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100384
Jens Axboe843a7412006-06-01 21:14:21 -0700385 if (strncmp(p, name, strlen(name)))
386 return 1;
387
388 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100389 if (!s)
390 return 1;
391
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100392 s = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100393 if (!s)
394 return 1;
395
396 s++;
397 strip_blank_front(&s);
398
399 strcpy(dest, s);
Jens Axboeebac4652005-12-08 15:25:21 +0100400 return 0;
401}
402
Jens Axboe01617be2005-12-08 19:50:40 +0100403static int __check_range(char *str, unsigned long *val)
Jens Axboeebac4652005-12-08 15:25:21 +0100404{
Jens Axboe01617be2005-12-08 19:50:40 +0100405 char suffix;
Jens Axboeebac4652005-12-08 15:25:21 +0100406
Jens Axboe01617be2005-12-08 19:50:40 +0100407 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
408 *val *= get_mult(suffix);
Jens Axboeebac4652005-12-08 15:25:21 +0100409 return 0;
410 }
411
Jens Axboe01617be2005-12-08 19:50:40 +0100412 if (sscanf(str, "%lu", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100413 return 0;
414
415 return 1;
Jens Axboe01617be2005-12-08 19:50:40 +0100416}
Jens Axboeebac4652005-12-08 15:25:21 +0100417
Jens Axboe01617be2005-12-08 19:50:40 +0100418static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
419{
420 char option[128];
421 char *str, *p1, *p2;
422
Jens Axboe843a7412006-06-01 21:14:21 -0700423 if (strncmp(p, name, strlen(name)))
424 return 1;
425
Jens Axboe01617be2005-12-08 19:50:40 +0100426 strcpy(option, p);
427 p = option;
428
429 str = strstr(p, name);
430 if (!str)
431 return 1;
432
433 p += strlen(name);
434
435 str = strchr(p, '=');
436 if (!str)
437 return 1;
438
439 /*
440 * 'p' now holds whatever is after the '=' sign
441 */
442 p1 = str + 1;
443
444 /*
445 * terminate p1 at the '-' sign
446 */
447 p = strchr(p1, '-');
448 if (!p)
449 return 1;
450
451 p2 = p + 1;
452 *p = '\0';
453
454 if (!__check_range(p1, s) && !__check_range(p2, e))
455 return 0;
456
457 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100458}
459
460static int check_int(char *p, char *name, unsigned int *val)
461{
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100462 char *str;
Jens Axboeebac4652005-12-08 15:25:21 +0100463
Jens Axboeb6754f92006-05-30 14:29:32 +0200464 if (strncmp(p, name, strlen(name)))
465 return 1;
466
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100467 str = strstr(p, name);
468 if (!str)
469 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100470
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100471 str = strchr(p, '=');
472 if (!str)
473 return 1;
474
475 str++;
476
477 if (sscanf(str, "%u", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100478 return 0;
479
480 return 1;
481}
482
483static int check_strset(char *p, char *name)
484{
485 return strncmp(p, name, strlen(name));
486}
487
488static int is_empty_or_comment(char *line)
489{
490 unsigned int i;
491
492 for (i = 0; i < strlen(line); i++) {
493 if (line[i] == ';')
494 return 1;
495 if (!isspace(line[i]) && !iscntrl(line[i]))
496 return 0;
497 }
498
499 return 1;
500}
501
502static int str_rw_cb(struct thread_data *td, char *mem)
503{
504 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
505 td->ddir = DDIR_READ;
506 td->sequential = 1;
507 return 0;
508 } else if (!strncmp(mem, "randread", 8)) {
509 td->ddir = DDIR_READ;
510 td->sequential = 0;
511 return 0;
512 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
513 td->ddir = DDIR_WRITE;
514 td->sequential = 1;
515 return 0;
516 } else if (!strncmp(mem, "randwrite", 9)) {
517 td->ddir = DDIR_WRITE;
518 td->sequential = 0;
519 return 0;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200520 } else if (!strncmp(mem, "rw", 2)) {
521 td->ddir = 0;
522 td->iomix = 1;
523 td->sequential = 1;
524 return 0;
525 } else if (!strncmp(mem, "randrw", 6)) {
526 td->ddir = 0;
527 td->iomix = 1;
528 td->sequential = 0;
529 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100530 }
531
Jens Axboe07261982006-06-07 10:51:12 +0200532 fprintf(stderr, "fio: data direction: read, write, randread, randwrite, rw, randrw\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100533 return 1;
534}
535
536static int str_verify_cb(struct thread_data *td, char *mem)
537{
538 if (!strncmp(mem, "0", 1)) {
539 td->verify = VERIFY_NONE;
540 return 0;
541 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
542 td->verify = VERIFY_MD5;
543 return 0;
544 } else if (!strncmp(mem, "crc32", 5)) {
545 td->verify = VERIFY_CRC32;
546 return 0;
547 }
548
Jens Axboe07261982006-06-07 10:51:12 +0200549 fprintf(stderr, "fio: verify types: md5, crc32\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100550 return 1;
551}
552
553static int str_mem_cb(struct thread_data *td, char *mem)
554{
555 if (!strncmp(mem, "malloc", 6)) {
556 td->mem_type = MEM_MALLOC;
557 return 0;
558 } else if (!strncmp(mem, "shm", 3)) {
559 td->mem_type = MEM_SHM;
560 return 0;
561 } else if (!strncmp(mem, "mmap", 4)) {
562 td->mem_type = MEM_MMAP;
563 return 0;
564 }
565
Jens Axboe07261982006-06-07 10:51:12 +0200566 fprintf(stderr, "fio: mem type: malloc, shm, mmap\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100567 return 1;
568}
569
570static int str_ioengine_cb(struct thread_data *td, char *str)
571{
572 if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
573 !strncmp(str, "libaio", 6)) {
574 strcpy(td->io_engine_name, "libaio");
575 td->io_engine = FIO_LIBAIO;
576 return 0;
577 } else if (!strncmp(str, "posixaio", 8)) {
578 strcpy(td->io_engine_name, "posixaio");
579 td->io_engine = FIO_POSIXAIO;
580 return 0;
581 } else if (!strncmp(str, "sync", 4)) {
582 strcpy(td->io_engine_name, "sync");
583 td->io_engine = FIO_SYNCIO;
584 return 0;
585 } else if (!strncmp(str, "mmap", 4)) {
586 strcpy(td->io_engine_name, "mmap");
587 td->io_engine = FIO_MMAPIO;
588 return 0;
589 } else if (!strncmp(str, "sgio", 4)) {
590 strcpy(td->io_engine_name, "sgio");
591 td->io_engine = FIO_SGIO;
592 return 0;
Jens Axboe8756e4d2006-05-27 20:24:53 +0200593 } else if (!strncmp(str, "splice", 6)) {
594 strcpy(td->io_engine_name, "splice");
595 td->io_engine = FIO_SPLICEIO;
596 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100597 }
598
Jens Axboe07261982006-06-07 10:51:12 +0200599 fprintf(stderr, "fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100600 return 1;
601}
602
Jens Axboe07261982006-06-07 10:51:12 +0200603/*
604 * This is our [ini] type file parser.
605 */
Jens Axboeebac4652005-12-08 15:25:21 +0100606int parse_jobs_ini(char *file)
607{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200608 unsigned int prioclass, prio, cpu, global, il;
Jens Axboeebac4652005-12-08 15:25:21 +0100609 unsigned long long ull;
610 unsigned long ul1, ul2;
611 struct thread_data *td;
Jens Axboeef899b62006-06-06 09:31:00 +0200612 char *string, *name, *tmpbuf;
Jens Axboeebac4652005-12-08 15:25:21 +0100613 fpos_t off;
614 FILE *f;
615 char *p;
Jens Axboe45410ac2006-06-07 11:10:39 +0200616 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100617
618 f = fopen(file, "r");
619 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200620 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100621 return 1;
622 }
623
624 string = malloc(4096);
625 name = malloc(256);
Jens Axboeef899b62006-06-06 09:31:00 +0200626 tmpbuf = malloc(4096);
Jens Axboeebac4652005-12-08 15:25:21 +0100627
628 while ((p = fgets(string, 4096, f)) != NULL) {
Jens Axboe45410ac2006-06-07 11:10:39 +0200629 if (ret)
630 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100631 if (is_empty_or_comment(p))
632 continue;
633 if (sscanf(p, "[%s]", name) != 1)
634 continue;
635
636 global = !strncmp(name, "global", 6);
637
638 name[strlen(name) - 1] = '\0';
639
640 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200641 if (!td) {
642 ret = 1;
643 break;
644 }
Jens Axboeebac4652005-12-08 15:25:21 +0100645
Jens Axboeebac4652005-12-08 15:25:21 +0100646 fgetpos(f, &off);
647 while ((p = fgets(string, 4096, f)) != NULL) {
648 if (is_empty_or_comment(p))
649 continue;
650 if (strstr(p, "["))
651 break;
Jens Axboeb6754f92006-05-30 14:29:32 +0200652 strip_blank_front(&p);
Jens Axboe4ae3f762006-05-30 13:35:14 +0200653 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200654
Jens Axboeebac4652005-12-08 15:25:21 +0100655 if (!check_int(p, "prio", &prio)) {
656#ifndef FIO_HAVE_IOPRIO
657 fprintf(stderr, "io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200658 ret = 1;
659 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100660#endif
Jens Axboe8756e4d2006-05-27 20:24:53 +0200661 td->ioprio |= prio;
Jens Axboeebac4652005-12-08 15:25:21 +0100662 fgetpos(f, &off);
663 continue;
664 }
665 if (!check_int(p, "prioclass", &prioclass)) {
666#ifndef FIO_HAVE_IOPRIO
667 fprintf(stderr, "io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200668 ret = 1;
669 break;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200670#else
Jens Axboe8756e4d2006-05-27 20:24:53 +0200671 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
Jens Axboeebac4652005-12-08 15:25:21 +0100672 fgetpos(f, &off);
673 continue;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200674#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100675 }
676 if (!check_int(p, "direct", &td->odirect)) {
677 fgetpos(f, &off);
678 continue;
679 }
680 if (!check_int(p, "rate", &td->rate)) {
681 fgetpos(f, &off);
682 continue;
683 }
684 if (!check_int(p, "ratemin", &td->ratemin)) {
685 fgetpos(f, &off);
686 continue;
687 }
688 if (!check_int(p, "ratecycle", &td->ratecycle)) {
689 fgetpos(f, &off);
690 continue;
691 }
692 if (!check_int(p, "thinktime", &td->thinktime)) {
693 fgetpos(f, &off);
694 continue;
695 }
696 if (!check_int(p, "cpumask", &cpu)) {
697#ifndef FIO_HAVE_CPU_AFFINITY
698 fprintf(stderr, "cpu affinity not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200699 ret = 1;
700 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100701#endif
702 fill_cpu_mask(td->cpumask, cpu);
703 fgetpos(f, &off);
704 continue;
705 }
706 if (!check_int(p, "fsync", &td->fsync_blocks)) {
707 fgetpos(f, &off);
Jens Axboefc1a4712006-05-30 13:04:05 +0200708 td->end_fsync = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100709 continue;
710 }
711 if (!check_int(p, "startdelay", &td->start_delay)) {
712 fgetpos(f, &off);
713 continue;
714 }
715 if (!check_int(p, "timeout", &td->timeout)) {
716 fgetpos(f, &off);
717 continue;
718 }
719 if (!check_int(p, "invalidate",&td->invalidate_cache)) {
720 fgetpos(f, &off);
721 continue;
722 }
723 if (!check_int(p, "iodepth", &td->iodepth)) {
724 fgetpos(f, &off);
725 continue;
726 }
727 if (!check_int(p, "sync", &td->sync_io)) {
728 fgetpos(f, &off);
729 continue;
730 }
731 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
732 fgetpos(f, &off);
733 continue;
734 }
735 if (!check_int(p, "create_serialize", &td->create_serialize)) {
736 fgetpos(f, &off);
737 continue;
738 }
739 if (!check_int(p, "create_fsync", &td->create_fsync)) {
740 fgetpos(f, &off);
741 continue;
742 }
Jens Axboefc1a4712006-05-30 13:04:05 +0200743 if (!check_int(p, "end_fsync", &td->end_fsync)) {
744 fgetpos(f, &off);
745 continue;
746 }
Jens Axboeebac4652005-12-08 15:25:21 +0100747 if (!check_int(p, "loops", &td->loops)) {
748 fgetpos(f, &off);
749 continue;
750 }
751 if (!check_int(p, "numjobs", &td->numjobs)) {
752 fgetpos(f, &off);
753 continue;
754 }
755 if (!check_int(p, "overwrite", &td->overwrite)) {
756 fgetpos(f, &off);
757 continue;
758 }
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200759 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
760 fgetpos(f, &off);
761 continue;
762 }
763 if (!check_int(p, "rwmixread", &il)) {
764 if (il > 100)
765 il = 100;
766 td->rwmixread = il;
767 fgetpos(f, &off);
768 continue;
769 }
770 if (!check_int(p, "rwmixwrite", &il)) {
771 if (il > 100)
772 il = 100;
773 td->rwmixread = 100 - il;
774 fgetpos(f, &off);
775 continue;
776 }
Jens Axboeb6f4d882006-06-02 10:32:51 +0200777 if (!check_int(p, "nice", &td->nice)) {
778 fgetpos(f, &off);
779 continue;
780 }
Jens Axboeebac4652005-12-08 15:25:21 +0100781 if (!check_range(p, "bsrange", &ul1, &ul2)) {
782 if (ul1 > ul2) {
783 td->max_bs = ul1;
784 td->min_bs = ul2;
785 } else {
786 td->max_bs = ul2;
787 td->min_bs = ul1;
788 }
789 fgetpos(f, &off);
790 continue;
791 }
792 if (!check_strcnv(p, "bs", &ull)) {
793 td->bs = ull;
794 fgetpos(f, &off);
795 continue;
796 }
797 if (!check_strcnv(p, "size", &td->file_size)) {
798 fgetpos(f, &off);
799 continue;
800 }
801 if (!check_strcnv(p, "offset", &td->file_offset)) {
802 fgetpos(f, &off);
803 continue;
804 }
Jens Axboe20dc95c2005-12-09 10:29:35 +0100805 if (!check_strcnv(p, "zonesize", &td->zone_size)) {
806 fgetpos(f, &off);
807 continue;
808 }
809 if (!check_strcnv(p, "zoneskip", &td->zone_skip)) {
810 fgetpos(f, &off);
811 continue;
812 }
Jens Axboec04f7ec2006-05-31 10:13:16 +0200813 if (!check_strcnv(p, "lockmem", &mlock_size)) {
814 fgetpos(f, &off);
815 continue;
816 }
Jens Axboeef899b62006-06-06 09:31:00 +0200817 if (!check_strstore(p, "directory", tmpbuf)) {
818 td->directory = strdup(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100819 fgetpos(f, &off);
820 continue;
821 }
Jens Axboe01452052006-06-07 10:29:47 +0200822 if (!check_strstore(p, "name", tmpbuf)) {
823 snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
824 fgetpos(f, &off);
825 continue;
826 }
Jens Axboeebac4652005-12-08 15:25:21 +0100827 if (!check_str(p, "mem", str_mem_cb, td)) {
828 fgetpos(f, &off);
829 continue;
830 }
831 if (!check_str(p, "verify", str_verify_cb, td)) {
832 fgetpos(f, &off);
833 continue;
834 }
835 if (!check_str(p, "rw", str_rw_cb, td)) {
836 fgetpos(f, &off);
837 continue;
838 }
839 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
840 fgetpos(f, &off);
841 continue;
842 }
843 if (!check_strset(p, "create")) {
844 td->create_file = 1;
845 fgetpos(f, &off);
846 continue;
847 }
848 if (!check_strset(p, "exitall")) {
849 exitall_on_terminate = 1;
850 fgetpos(f, &off);
851 continue;
852 }
853 if (!check_strset(p, "stonewall")) {
854 td->stonewall = 1;
855 fgetpos(f, &off);
856 continue;
857 }
858 if (!check_strset(p, "thread")) {
859 td->use_thread = 1;
860 fgetpos(f, &off);
861 continue;
862 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200863 if (!check_strstore(p, "iolog", tmpbuf)) {
864 td->iolog_file = strdup(tmpbuf);
Jens Axboe843a7412006-06-01 21:14:21 -0700865 td->read_iolog = 1;
866 td->write_iolog = 0;
867 fgetpos(f, &off);
868 continue;
869 }
870 if (!td->read_iolog &&
Jens Axboe80b9feb2006-06-07 10:34:49 +0200871 !check_strstore(p, "write_iolog", tmpbuf)) {
872 td->iolog_file = strdup(tmpbuf);
Jens Axboe843a7412006-06-01 21:14:21 -0700873 td->write_iolog = 1;
Jens Axboeaea47d42006-05-26 19:27:29 +0200874 fgetpos(f, &off);
875 continue;
876 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200877 if (!check_strstore(p, "exec_prerun", tmpbuf)) {
878 td->exec_prerun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200879 fgetpos(f, &off);
880 continue;
881 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200882 if (!check_strstore(p, "exec_postrun", tmpbuf)) {
883 td->exec_postrun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200884 fgetpos(f, &off);
885 continue;
886 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200887 if (!check_strstore(p, "ioscheduler", tmpbuf)) {
Jens Axboe22f78b32006-06-07 11:30:07 +0200888#ifndef FIO_HAVE_IOSCHED_SWITCH
889 fprintf(stderr, "io scheduler switching not available\n");
890 ret = 1;
891 break;
892#else
Jens Axboe80b9feb2006-06-07 10:34:49 +0200893 td->ioscheduler = strdup(tmpbuf);
Jens Axboeda867742006-06-06 10:39:10 -0700894 fgetpos(f, &off);
895 continue;
Jens Axboe22f78b32006-06-07 11:30:07 +0200896#endif
Jens Axboeda867742006-06-06 10:39:10 -0700897 }
Jens Axboeebac4652005-12-08 15:25:21 +0100898
Jens Axboe45410ac2006-06-07 11:10:39 +0200899 /*
900 * Don't break here, continue parsing options so we
901 * dump all the bad ones. Makes trial/error fixups
902 * easier on the user.
903 */
Jens Axboeebac4652005-12-08 15:25:21 +0100904 printf("Client%d: bad option %s\n",td->thread_number,p);
Jens Axboe45410ac2006-06-07 11:10:39 +0200905 ret = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100906 }
Jens Axboeebac4652005-12-08 15:25:21 +0100907
Jens Axboe45410ac2006-06-07 11:10:39 +0200908 if (!ret) {
909 fsetpos(f, &off);
910 ret = add_job(td, name, 0);
911 }
912 if (ret)
913 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100914 }
915
916 free(string);
917 free(name);
Jens Axboeef899b62006-06-06 09:31:00 +0200918 free(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100919 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200920 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100921}
922
923static int fill_def_thread(void)
924{
925 memset(&def_thread, 0, sizeof(def_thread));
926
927 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
928 perror("sched_getaffinity");
929 return 1;
930 }
931
932 /*
933 * fill globals
934 */
935 def_thread.ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200936 def_thread.iomix = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100937 def_thread.bs = DEF_BS;
938 def_thread.min_bs = -1;
939 def_thread.max_bs = -1;
940 def_thread.io_engine = DEF_IO_ENGINE;
941 strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
942 def_thread.odirect = DEF_ODIRECT;
943 def_thread.ratecycle = DEF_RATE_CYCLE;
944 def_thread.sequential = DEF_SEQUENTIAL;
945 def_thread.timeout = DEF_TIMEOUT;
946 def_thread.create_file = DEF_CREATE;
947 def_thread.overwrite = DEF_OVERWRITE;
948 def_thread.invalidate_cache = DEF_INVALIDATE;
949 def_thread.sync_io = DEF_SYNCIO;
950 def_thread.mem_type = MEM_MALLOC;
951 def_thread.bw_avg_time = DEF_BWAVGTIME;
952 def_thread.create_serialize = DEF_CREATE_SER;
953 def_thread.create_fsync = DEF_CREATE_FSYNC;
954 def_thread.loops = DEF_LOOPS;
955 def_thread.verify = DEF_VERIFY;
956 def_thread.stonewall = DEF_STONEWALL;
957 def_thread.numjobs = DEF_NUMJOBS;
958 def_thread.use_thread = DEF_USE_THREAD;
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200959 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
960 def_thread.rwmixread = DEF_RWMIX_READ;
Jens Axboeb6f4d882006-06-02 10:32:51 +0200961 def_thread.nice = DEF_NICE;
Jens Axboeebac4652005-12-08 15:25:21 +0100962#ifdef FIO_HAVE_DISK_UTIL
963 def_thread.do_disk_util = 1;
964#endif
965
966 return 0;
967}
968
Jens Axboe4785f992006-05-26 03:59:10 +0200969static void usage(char *name)
970{
971 printf("%s\n", fio_version_string);
972 printf("\t-s IO is sequential\n");
973 printf("\t-b Block size in KiB for each IO\n");
974 printf("\t-t Runtime in seconds\n");
975 printf("\t-R Exit all threads on failure to meet rate goal\n");
976 printf("\t-o Use O_DIRECT\n");
977 printf("\t-l Generate per-job latency logs\n");
978 printf("\t-w Generate per-job bandwidth logs\n");
979 printf("\t-f Job file (Required)\n");
980 printf("\t-v Print version info and exit\n");
981}
982
Jens Axboeebac4652005-12-08 15:25:21 +0100983static void parse_cmd_line(int argc, char *argv[])
984{
985 int c;
986
Jens Axboe4785f992006-05-26 03:59:10 +0200987 while ((c = getopt(argc, argv, "s:b:t:r:R:o:f:lwvh")) != EOF) {
Jens Axboeebac4652005-12-08 15:25:21 +0100988 switch (c) {
989 case 's':
990 def_thread.sequential = !!atoi(optarg);
991 break;
992 case 'b':
993 def_thread.bs = atoi(optarg);
994 def_thread.bs <<= 10;
995 if (!def_thread.bs) {
996 printf("bad block size\n");
997 def_thread.bs = DEF_BS;
998 }
999 break;
1000 case 't':
1001 def_thread.timeout = atoi(optarg);
1002 break;
1003 case 'r':
1004 repeatable = !!atoi(optarg);
1005 break;
1006 case 'R':
1007 rate_quit = !!atoi(optarg);
1008 break;
1009 case 'o':
1010 def_thread.odirect = !!atoi(optarg);
1011 break;
1012 case 'f':
1013 ini_file = strdup(optarg);
1014 break;
1015 case 'l':
1016 write_lat_log = 1;
1017 break;
1018 case 'w':
1019 write_bw_log = 1;
1020 break;
Jens Axboe4785f992006-05-26 03:59:10 +02001021 case 'h':
1022 usage(argv[0]);
1023 exit(0);
Jens Axboeebac4652005-12-08 15:25:21 +01001024 case 'v':
1025 printf("%s\n", fio_version_string);
1026 exit(0);
1027 }
1028 }
Jens Axboec9fad892006-05-27 17:26:50 +02001029
1030 if (!ini_file && argc > 1 && argv[argc - 1][0] != '-')
1031 ini_file = strdup(argv[argc - 1]);
Jens Axboeebac4652005-12-08 15:25:21 +01001032}
1033
1034static void free_shm(void)
1035{
1036 struct shmid_ds sbuf;
1037
1038 if (threads) {
1039 shmdt(threads);
1040 threads = NULL;
1041 shmctl(shm_id, IPC_RMID, &sbuf);
1042 }
1043}
1044
1045static int setup_thread_area(void)
1046{
1047 /*
1048 * 1024 is too much on some machines, scale max_jobs if
1049 * we get a failure that looks like too large a shm segment
1050 */
1051 do {
1052 int s = max_jobs * sizeof(struct thread_data);
1053
1054 shm_id = shmget(0, s, IPC_CREAT | 0600);
1055 if (shm_id != -1)
1056 break;
1057 if (errno != EINVAL) {
1058 perror("shmget");
1059 break;
1060 }
1061
1062 max_jobs >>= 1;
1063 } while (max_jobs);
1064
1065 if (shm_id == -1)
1066 return 1;
1067
1068 threads = shmat(shm_id, NULL, 0);
1069 if (threads == (void *) -1) {
1070 perror("shmat");
1071 return 1;
1072 }
1073
1074 atexit(free_shm);
1075 return 0;
1076}
1077
1078int parse_options(int argc, char *argv[])
1079{
1080 if (setup_thread_area())
1081 return 1;
1082 if (fill_def_thread())
1083 return 1;
1084
1085 parse_cmd_line(argc, argv);
1086
1087 if (!ini_file) {
1088 printf("Need job file\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001089 usage(argv[0]);
Jens Axboeebac4652005-12-08 15:25:21 +01001090 return 1;
1091 }
1092
Jens Axboe07261982006-06-07 10:51:12 +02001093 if (parse_jobs_ini(ini_file))
Jens Axboeebac4652005-12-08 15:25:21 +01001094 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +01001095
1096 return 0;
1097}