blob: b40437afa84fab9aaaeac099e37088246ce2bcd5 [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 Axboeeb8bbf42006-06-08 21:40:11 +020057FILE *f_out = NULL;
58FILE *f_err = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +010059
Jens Axboeebac4652005-12-08 15:25:21 +010060static struct thread_data *get_new_job(int global, struct thread_data *parent)
61{
62 struct thread_data *td;
63
64 if (global)
65 return &def_thread;
66 if (thread_number >= max_jobs)
67 return NULL;
68
69 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +020070 *td = *parent;
71 td->name[0] = '\0';
Jens Axboeebac4652005-12-08 15:25:21 +010072
73 td->fd = -1;
74 td->thread_number = thread_number;
75
Jens Axboeebac4652005-12-08 15:25:21 +010076 return td;
77}
78
79static void put_job(struct thread_data *td)
80{
81 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
82 thread_number--;
83}
84
Jens Axboe75154842006-06-01 13:56:09 +020085static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +010086{
Jens Axboe3d60d1e2006-05-25 06:31:06 +020087 char *ddir_str[] = { "read", "write", "randread", "randwrite",
88 "rw", NULL, "randrw" };
Jens Axboeebac4652005-12-08 15:25:21 +010089 struct stat sb;
90 int numjobs, ddir;
91
92#ifndef FIO_HAVE_LIBAIO
93 if (td->io_engine == FIO_LIBAIO) {
Jens Axboeeb8bbf42006-06-08 21:40:11 +020094 fprintf(f_err, "Linux libaio not available\n");
Jens Axboeebac4652005-12-08 15:25:21 +010095 return 1;
96 }
97#endif
98#ifndef FIO_HAVE_POSIXAIO
99 if (td->io_engine == FIO_POSIXAIO) {
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200100 fprintf(f_err, "posix aio not available\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100101 return 1;
102 }
103#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100104
105 /*
106 * the def_thread is just for options, it's not a real job
107 */
108 if (td == &def_thread)
109 return 0;
110
111 if (td->io_engine & FIO_SYNCIO)
112 td->iodepth = 1;
113 else {
114 if (!td->iodepth)
115 td->iodepth = 1;
116 }
117
Jens Axboe20dc95c2005-12-09 10:29:35 +0100118 /*
119 * only really works for sequential io for now
120 */
121 if (td->zone_size && !td->sequential)
122 td->zone_size = 0;
123
Jens Axboeebac4652005-12-08 15:25:21 +0100124 td->filetype = FIO_TYPE_FILE;
Jens Axboe0af7b542006-02-17 10:10:12 +0100125 if (!stat(jobname, &sb)) {
126 if (S_ISBLK(sb.st_mode))
127 td->filetype = FIO_TYPE_BD;
128 else if (S_ISCHR(sb.st_mode))
129 td->filetype = FIO_TYPE_CHAR;
130 }
Jens Axboeebac4652005-12-08 15:25:21 +0100131
132 if (td->filetype == FIO_TYPE_FILE) {
Jens Axboee9c047a2006-06-07 21:13:04 +0200133 char tmp[PATH_MAX];
134
Jens Axboeef899b62006-06-06 09:31:00 +0200135 if (td->directory && td->directory[0] != '\0')
Jens Axboee9c047a2006-06-07 21:13:04 +0200136 sprintf(tmp, "%s/%s.%d", td->directory, jobname, td->jobnum);
Jens Axboeebac4652005-12-08 15:25:21 +0100137 else
Jens Axboee9c047a2006-06-07 21:13:04 +0200138 sprintf(tmp, "%s.%d", jobname, td->jobnum);
139 td->file_name = strdup(tmp);
Jens Axboeebac4652005-12-08 15:25:21 +0100140 } else
Jens Axboee9c047a2006-06-07 21:13:04 +0200141 td->file_name = strdup(jobname);
Jens Axboeebac4652005-12-08 15:25:21 +0100142
Jens Axboebbfd6b02006-06-07 19:42:54 +0200143 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100144
145 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
146 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
147 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
148
149 if (td->min_bs == -1U)
150 td->min_bs = td->bs;
151 if (td->max_bs == -1U)
152 td->max_bs = td->bs;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200153 if (td_read(td) && !td_rw(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100154 td->verify = 0;
155
156 if (td->stonewall && td->thread_number > 1)
157 groupid++;
158
159 td->groupid = groupid;
160
161 if (setup_rate(td))
162 goto err;
163
164 if (write_lat_log) {
165 setup_log(&td->slat_log);
166 setup_log(&td->clat_log);
167 }
168 if (write_bw_log)
169 setup_log(&td->bw_log);
170
Jens Axboe01452052006-06-07 10:29:47 +0200171 if (td->name[0] == '\0')
172 snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
173
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200174 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
Jens Axboe75154842006-06-01 13:56:09 +0200175
176 if (!job_add_num)
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200177 fprintf(f_out, "%s: (g=%d): rw=%s, odir=%d, bs=%d-%d, rate=%d, ioengine=%s, iodepth=%d\n", td->name, td->groupid, ddir_str[ddir], td->odirect, td->min_bs, td->max_bs, td->rate, td->io_engine_name, td->iodepth);
Jens Axboe75154842006-06-01 13:56:09 +0200178 else if (job_add_num == 1)
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200179 fprintf(f_out, "...\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100180
181 /*
182 * recurse add identical jobs, clear numjobs and stonewall options
183 * as they don't apply to sub-jobs
184 */
185 numjobs = td->numjobs;
186 while (--numjobs) {
187 struct thread_data *td_new = get_new_job(0, td);
188
189 if (!td_new)
190 goto err;
191
192 td_new->numjobs = 1;
193 td_new->stonewall = 0;
Jens Axboeeba09dd2006-05-22 20:20:54 +0200194 td_new->jobnum = numjobs;
Jens Axboe75154842006-06-01 13:56:09 +0200195 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100196
Jens Axboe75154842006-06-01 13:56:09 +0200197 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100198 goto err;
199 }
200 return 0;
201err:
202 put_job(td);
203 return -1;
204}
205
206int init_random_state(struct thread_data *td)
207{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200208 unsigned long seeds[4];
Jens Axboeebac4652005-12-08 15:25:21 +0100209 int fd, num_maps, blocks;
210
Jens Axboe1ac267b2006-05-31 20:29:00 +0200211 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100212 if (fd == -1) {
213 td_verror(td, errno);
214 return 1;
215 }
216
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200217 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100218 td_verror(td, EIO);
219 close(fd);
220 return 1;
221 }
222
223 close(fd);
224
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200225 os_random_seed(seeds[0], &td->bsrange_state);
226 os_random_seed(seeds[1], &td->verify_state);
227 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100228
229 if (td->sequential)
230 return 0;
231
232 if (repeatable)
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200233 seeds[3] = DEF_RANDSEED;
Jens Axboeebac4652005-12-08 15:25:21 +0100234
235 blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
236 num_maps = blocks / BLOCKS_PER_MAP;
237 td->file_map = malloc(num_maps * sizeof(long));
238 td->num_maps = num_maps;
239 memset(td->file_map, 0, num_maps * sizeof(long));
240
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200241 os_random_seed(seeds[3], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100242 return 0;
243}
244
245static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
246{
247#ifdef FIO_HAVE_CPU_AFFINITY
248 unsigned int i;
249
250 CPU_ZERO(&cpumask);
251
252 for (i = 0; i < sizeof(int) * 8; i++) {
253 if ((1 << i) & cpu)
254 CPU_SET(i, &cpumask);
255 }
256#endif
257}
258
259static unsigned long get_mult(char c)
260{
261 switch (c) {
262 case 'k':
263 case 'K':
264 return 1024;
265 case 'm':
266 case 'M':
267 return 1024 * 1024;
268 case 'g':
269 case 'G':
270 return 1024 * 1024 * 1024;
271 default:
272 return 1;
273 }
274}
275
276/*
277 * convert string after '=' into decimal value, noting any size suffix
278 */
279static int str_cnv(char *p, unsigned long long *val)
280{
281 char *str;
282 int len;
283
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100284 str = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100285 if (!str)
286 return 1;
287
288 str++;
289 len = strlen(str);
290
291 *val = strtoul(str, NULL, 10);
292 if (*val == ULONG_MAX && errno == ERANGE)
293 return 1;
294
Jens Axboe9d0d4242006-05-30 13:45:51 +0200295 *val *= get_mult(str[len - 1]);
Jens Axboeebac4652005-12-08 15:25:21 +0100296 return 0;
297}
298
299static int check_strcnv(char *p, char *name, unsigned long long *val)
300{
Jens Axboe20dc95c2005-12-09 10:29:35 +0100301 if (strncmp(p, name, strlen(name) - 1))
Jens Axboeebac4652005-12-08 15:25:21 +0100302 return 1;
303
304 return str_cnv(p, val);
305}
306
307static void strip_blank_front(char **p)
308{
309 char *s = *p;
310
Jens Axboe4ae3f762006-05-30 13:35:14 +0200311 while (isspace(*s))
Jens Axboeebac4652005-12-08 15:25:21 +0100312 s++;
313}
314
315static void strip_blank_end(char *p)
316{
Jens Axboe4ae3f762006-05-30 13:35:14 +0200317 char *s = p + strlen(p) - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100318
Jens Axboe4ae3f762006-05-30 13:35:14 +0200319 while (isspace(*s) || iscntrl(*s))
320 s--;
Jens Axboeaea47d42006-05-26 19:27:29 +0200321
Jens Axboe4ae3f762006-05-30 13:35:14 +0200322 *(s + 1) = '\0';
Jens Axboeaea47d42006-05-26 19:27:29 +0200323}
324
Jens Axboeebac4652005-12-08 15:25:21 +0100325typedef int (str_cb_fn)(struct thread_data *, char *);
326
327static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
328{
Jens Axboe843a7412006-06-01 21:14:21 -0700329 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100330
Jens Axboe843a7412006-06-01 21:14:21 -0700331 if (strncmp(p, name, strlen(name)))
332 return 1;
333
334 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100335 if (!s)
336 return 1;
337
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100338 s = strchr(s, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100339 if (!s)
340 return 1;
341
342 s++;
343 strip_blank_front(&s);
344 return cb(td, s);
345}
346
347static int check_strstore(char *p, char *name, char *dest)
348{
Jens Axboe843a7412006-06-01 21:14:21 -0700349 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100350
Jens Axboe843a7412006-06-01 21:14:21 -0700351 if (strncmp(p, name, strlen(name)))
352 return 1;
353
354 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100355 if (!s)
356 return 1;
357
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100358 s = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100359 if (!s)
360 return 1;
361
362 s++;
363 strip_blank_front(&s);
364
365 strcpy(dest, s);
Jens Axboeebac4652005-12-08 15:25:21 +0100366 return 0;
367}
368
Jens Axboe01617be2005-12-08 19:50:40 +0100369static int __check_range(char *str, unsigned long *val)
Jens Axboeebac4652005-12-08 15:25:21 +0100370{
Jens Axboe01617be2005-12-08 19:50:40 +0100371 char suffix;
Jens Axboeebac4652005-12-08 15:25:21 +0100372
Jens Axboe01617be2005-12-08 19:50:40 +0100373 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
374 *val *= get_mult(suffix);
Jens Axboeebac4652005-12-08 15:25:21 +0100375 return 0;
376 }
377
Jens Axboe01617be2005-12-08 19:50:40 +0100378 if (sscanf(str, "%lu", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100379 return 0;
380
381 return 1;
Jens Axboe01617be2005-12-08 19:50:40 +0100382}
Jens Axboeebac4652005-12-08 15:25:21 +0100383
Jens Axboe01617be2005-12-08 19:50:40 +0100384static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
385{
386 char option[128];
387 char *str, *p1, *p2;
388
Jens Axboe843a7412006-06-01 21:14:21 -0700389 if (strncmp(p, name, strlen(name)))
390 return 1;
391
Jens Axboe01617be2005-12-08 19:50:40 +0100392 strcpy(option, p);
393 p = option;
394
395 str = strstr(p, name);
396 if (!str)
397 return 1;
398
399 p += strlen(name);
400
401 str = strchr(p, '=');
402 if (!str)
403 return 1;
404
405 /*
406 * 'p' now holds whatever is after the '=' sign
407 */
408 p1 = str + 1;
409
410 /*
411 * terminate p1 at the '-' sign
412 */
413 p = strchr(p1, '-');
414 if (!p)
415 return 1;
416
417 p2 = p + 1;
418 *p = '\0';
419
420 if (!__check_range(p1, s) && !__check_range(p2, e))
421 return 0;
422
423 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100424}
425
426static int check_int(char *p, char *name, unsigned int *val)
427{
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100428 char *str;
Jens Axboeebac4652005-12-08 15:25:21 +0100429
Jens Axboeb6754f92006-05-30 14:29:32 +0200430 if (strncmp(p, name, strlen(name)))
431 return 1;
432
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100433 str = strstr(p, name);
434 if (!str)
435 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100436
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100437 str = strchr(p, '=');
438 if (!str)
439 return 1;
440
441 str++;
442
443 if (sscanf(str, "%u", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100444 return 0;
445
446 return 1;
447}
448
449static int check_strset(char *p, char *name)
450{
451 return strncmp(p, name, strlen(name));
452}
453
454static int is_empty_or_comment(char *line)
455{
456 unsigned int i;
457
458 for (i = 0; i < strlen(line); i++) {
459 if (line[i] == ';')
460 return 1;
461 if (!isspace(line[i]) && !iscntrl(line[i]))
462 return 0;
463 }
464
465 return 1;
466}
467
468static int str_rw_cb(struct thread_data *td, char *mem)
469{
470 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
471 td->ddir = DDIR_READ;
472 td->sequential = 1;
473 return 0;
474 } else if (!strncmp(mem, "randread", 8)) {
475 td->ddir = DDIR_READ;
476 td->sequential = 0;
477 return 0;
478 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
479 td->ddir = DDIR_WRITE;
480 td->sequential = 1;
481 return 0;
482 } else if (!strncmp(mem, "randwrite", 9)) {
483 td->ddir = DDIR_WRITE;
484 td->sequential = 0;
485 return 0;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200486 } else if (!strncmp(mem, "rw", 2)) {
487 td->ddir = 0;
488 td->iomix = 1;
489 td->sequential = 1;
490 return 0;
491 } else if (!strncmp(mem, "randrw", 6)) {
492 td->ddir = 0;
493 td->iomix = 1;
494 td->sequential = 0;
495 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100496 }
497
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200498 fprintf(f_err, "fio: data direction: read, write, randread, randwrite, rw, randrw\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100499 return 1;
500}
501
502static int str_verify_cb(struct thread_data *td, char *mem)
503{
504 if (!strncmp(mem, "0", 1)) {
505 td->verify = VERIFY_NONE;
506 return 0;
507 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
508 td->verify = VERIFY_MD5;
509 return 0;
510 } else if (!strncmp(mem, "crc32", 5)) {
511 td->verify = VERIFY_CRC32;
512 return 0;
513 }
514
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200515 fprintf(f_err, "fio: verify types: md5, crc32\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100516 return 1;
517}
518
519static int str_mem_cb(struct thread_data *td, char *mem)
520{
521 if (!strncmp(mem, "malloc", 6)) {
522 td->mem_type = MEM_MALLOC;
523 return 0;
524 } else if (!strncmp(mem, "shm", 3)) {
525 td->mem_type = MEM_SHM;
526 return 0;
527 } else if (!strncmp(mem, "mmap", 4)) {
528 td->mem_type = MEM_MMAP;
529 return 0;
530 }
531
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200532 fprintf(f_err, "fio: mem type: malloc, shm, mmap\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100533 return 1;
534}
535
536static int str_ioengine_cb(struct thread_data *td, char *str)
537{
538 if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
539 !strncmp(str, "libaio", 6)) {
540 strcpy(td->io_engine_name, "libaio");
541 td->io_engine = FIO_LIBAIO;
542 return 0;
543 } else if (!strncmp(str, "posixaio", 8)) {
544 strcpy(td->io_engine_name, "posixaio");
545 td->io_engine = FIO_POSIXAIO;
546 return 0;
547 } else if (!strncmp(str, "sync", 4)) {
548 strcpy(td->io_engine_name, "sync");
549 td->io_engine = FIO_SYNCIO;
550 return 0;
551 } else if (!strncmp(str, "mmap", 4)) {
552 strcpy(td->io_engine_name, "mmap");
553 td->io_engine = FIO_MMAPIO;
554 return 0;
555 } else if (!strncmp(str, "sgio", 4)) {
556 strcpy(td->io_engine_name, "sgio");
557 td->io_engine = FIO_SGIO;
558 return 0;
Jens Axboe8756e4d2006-05-27 20:24:53 +0200559 } else if (!strncmp(str, "splice", 6)) {
560 strcpy(td->io_engine_name, "splice");
561 td->io_engine = FIO_SPLICEIO;
562 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100563 }
564
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200565 fprintf(f_err, "fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100566 return 1;
567}
568
Jens Axboe07261982006-06-07 10:51:12 +0200569/*
570 * This is our [ini] type file parser.
571 */
Jens Axboeebac4652005-12-08 15:25:21 +0100572int parse_jobs_ini(char *file)
573{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200574 unsigned int prioclass, prio, cpu, global, il;
Jens Axboeebac4652005-12-08 15:25:21 +0100575 unsigned long long ull;
576 unsigned long ul1, ul2;
577 struct thread_data *td;
Jens Axboeef899b62006-06-06 09:31:00 +0200578 char *string, *name, *tmpbuf;
Jens Axboeebac4652005-12-08 15:25:21 +0100579 fpos_t off;
580 FILE *f;
581 char *p;
Jens Axboe45410ac2006-06-07 11:10:39 +0200582 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100583
584 f = fopen(file, "r");
585 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200586 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100587 return 1;
588 }
589
590 string = malloc(4096);
591 name = malloc(256);
Jens Axboeef899b62006-06-06 09:31:00 +0200592 tmpbuf = malloc(4096);
Jens Axboeebac4652005-12-08 15:25:21 +0100593
594 while ((p = fgets(string, 4096, f)) != NULL) {
Jens Axboe45410ac2006-06-07 11:10:39 +0200595 if (ret)
596 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100597 if (is_empty_or_comment(p))
598 continue;
599 if (sscanf(p, "[%s]", name) != 1)
600 continue;
601
602 global = !strncmp(name, "global", 6);
603
604 name[strlen(name) - 1] = '\0';
605
606 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200607 if (!td) {
608 ret = 1;
609 break;
610 }
Jens Axboeebac4652005-12-08 15:25:21 +0100611
Jens Axboeebac4652005-12-08 15:25:21 +0100612 fgetpos(f, &off);
613 while ((p = fgets(string, 4096, f)) != NULL) {
614 if (is_empty_or_comment(p))
615 continue;
616 if (strstr(p, "["))
617 break;
Jens Axboeb6754f92006-05-30 14:29:32 +0200618 strip_blank_front(&p);
Jens Axboe4ae3f762006-05-30 13:35:14 +0200619 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200620
Jens Axboeebac4652005-12-08 15:25:21 +0100621 if (!check_int(p, "prio", &prio)) {
622#ifndef FIO_HAVE_IOPRIO
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200623 fprintf(f_err, "io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200624 ret = 1;
625 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100626#endif
Jens Axboe8756e4d2006-05-27 20:24:53 +0200627 td->ioprio |= prio;
Jens Axboeebac4652005-12-08 15:25:21 +0100628 fgetpos(f, &off);
629 continue;
630 }
631 if (!check_int(p, "prioclass", &prioclass)) {
632#ifndef FIO_HAVE_IOPRIO
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200633 fprintf(f_err, "io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200634 ret = 1;
635 break;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200636#else
Jens Axboe8756e4d2006-05-27 20:24:53 +0200637 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
Jens Axboeebac4652005-12-08 15:25:21 +0100638 fgetpos(f, &off);
639 continue;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200640#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100641 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200642 if (!check_int(p, "direct", &il)) {
643 td->odirect = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100644 fgetpos(f, &off);
645 continue;
646 }
647 if (!check_int(p, "rate", &td->rate)) {
648 fgetpos(f, &off);
649 continue;
650 }
651 if (!check_int(p, "ratemin", &td->ratemin)) {
652 fgetpos(f, &off);
653 continue;
654 }
655 if (!check_int(p, "ratecycle", &td->ratecycle)) {
656 fgetpos(f, &off);
657 continue;
658 }
659 if (!check_int(p, "thinktime", &td->thinktime)) {
660 fgetpos(f, &off);
661 continue;
662 }
663 if (!check_int(p, "cpumask", &cpu)) {
664#ifndef FIO_HAVE_CPU_AFFINITY
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200665 fprintf(f_err, "cpu affinity not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200666 ret = 1;
667 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100668#endif
669 fill_cpu_mask(td->cpumask, cpu);
670 fgetpos(f, &off);
671 continue;
672 }
673 if (!check_int(p, "fsync", &td->fsync_blocks)) {
674 fgetpos(f, &off);
Jens Axboefc1a4712006-05-30 13:04:05 +0200675 td->end_fsync = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100676 continue;
677 }
678 if (!check_int(p, "startdelay", &td->start_delay)) {
679 fgetpos(f, &off);
680 continue;
681 }
682 if (!check_int(p, "timeout", &td->timeout)) {
683 fgetpos(f, &off);
684 continue;
685 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200686 if (!check_int(p, "invalidate", &il)) {
687 td->invalidate_cache = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100688 fgetpos(f, &off);
689 continue;
690 }
691 if (!check_int(p, "iodepth", &td->iodepth)) {
692 fgetpos(f, &off);
693 continue;
694 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200695 if (!check_int(p, "sync", &il)) {
696 td->sync_io = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100697 fgetpos(f, &off);
698 continue;
699 }
700 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
701 fgetpos(f, &off);
702 continue;
703 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200704 if (!check_int(p, "create_serialize", &il)) {
705 td->create_serialize = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100706 fgetpos(f, &off);
707 continue;
708 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200709 if (!check_int(p, "create_fsync", &il)) {
710 td->create_fsync = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100711 fgetpos(f, &off);
712 continue;
713 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200714 if (!check_int(p, "end_fsync", &il)) {
715 td->end_fsync = il;
Jens Axboefc1a4712006-05-30 13:04:05 +0200716 fgetpos(f, &off);
717 continue;
718 }
Jens Axboeebac4652005-12-08 15:25:21 +0100719 if (!check_int(p, "loops", &td->loops)) {
720 fgetpos(f, &off);
721 continue;
722 }
723 if (!check_int(p, "numjobs", &td->numjobs)) {
724 fgetpos(f, &off);
725 continue;
726 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200727 if (!check_int(p, "overwrite", &il)) {
728 td->overwrite = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100729 fgetpos(f, &off);
730 continue;
731 }
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200732 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
733 fgetpos(f, &off);
734 continue;
735 }
736 if (!check_int(p, "rwmixread", &il)) {
737 if (il > 100)
738 il = 100;
739 td->rwmixread = il;
740 fgetpos(f, &off);
741 continue;
742 }
743 if (!check_int(p, "rwmixwrite", &il)) {
744 if (il > 100)
745 il = 100;
746 td->rwmixread = 100 - il;
747 fgetpos(f, &off);
748 continue;
749 }
Jens Axboeb6f4d882006-06-02 10:32:51 +0200750 if (!check_int(p, "nice", &td->nice)) {
751 fgetpos(f, &off);
752 continue;
753 }
Jens Axboeebac4652005-12-08 15:25:21 +0100754 if (!check_range(p, "bsrange", &ul1, &ul2)) {
755 if (ul1 > ul2) {
756 td->max_bs = ul1;
757 td->min_bs = ul2;
758 } else {
759 td->max_bs = ul2;
760 td->min_bs = ul1;
761 }
762 fgetpos(f, &off);
763 continue;
764 }
765 if (!check_strcnv(p, "bs", &ull)) {
766 td->bs = ull;
767 fgetpos(f, &off);
768 continue;
769 }
770 if (!check_strcnv(p, "size", &td->file_size)) {
771 fgetpos(f, &off);
772 continue;
773 }
774 if (!check_strcnv(p, "offset", &td->file_offset)) {
775 fgetpos(f, &off);
776 continue;
777 }
Jens Axboe20dc95c2005-12-09 10:29:35 +0100778 if (!check_strcnv(p, "zonesize", &td->zone_size)) {
779 fgetpos(f, &off);
780 continue;
781 }
782 if (!check_strcnv(p, "zoneskip", &td->zone_skip)) {
783 fgetpos(f, &off);
784 continue;
785 }
Jens Axboec04f7ec2006-05-31 10:13:16 +0200786 if (!check_strcnv(p, "lockmem", &mlock_size)) {
787 fgetpos(f, &off);
788 continue;
789 }
Jens Axboeef899b62006-06-06 09:31:00 +0200790 if (!check_strstore(p, "directory", tmpbuf)) {
791 td->directory = strdup(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100792 fgetpos(f, &off);
793 continue;
794 }
Jens Axboe01452052006-06-07 10:29:47 +0200795 if (!check_strstore(p, "name", tmpbuf)) {
796 snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
797 fgetpos(f, &off);
798 continue;
799 }
Jens Axboeebac4652005-12-08 15:25:21 +0100800 if (!check_str(p, "mem", str_mem_cb, td)) {
801 fgetpos(f, &off);
802 continue;
803 }
804 if (!check_str(p, "verify", str_verify_cb, td)) {
805 fgetpos(f, &off);
806 continue;
807 }
808 if (!check_str(p, "rw", str_rw_cb, td)) {
809 fgetpos(f, &off);
810 continue;
811 }
812 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
813 fgetpos(f, &off);
814 continue;
815 }
816 if (!check_strset(p, "create")) {
817 td->create_file = 1;
818 fgetpos(f, &off);
819 continue;
820 }
821 if (!check_strset(p, "exitall")) {
822 exitall_on_terminate = 1;
823 fgetpos(f, &off);
824 continue;
825 }
826 if (!check_strset(p, "stonewall")) {
827 td->stonewall = 1;
828 fgetpos(f, &off);
829 continue;
830 }
831 if (!check_strset(p, "thread")) {
832 td->use_thread = 1;
833 fgetpos(f, &off);
834 continue;
835 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200836 if (!check_strstore(p, "iolog", tmpbuf)) {
Jens Axboe4f693b92006-06-07 22:52:28 +0200837 if (td->write_iolog) {
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200838 fprintf(f_err, "fio: read iolog overrides given write_iolog\n");
Jens Axboeaf8c0b42006-06-07 22:33:17 +0200839 free(td->iolog_file);
Jens Axboe4f693b92006-06-07 22:52:28 +0200840 td->write_iolog = 0;
841 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200842 td->iolog_file = strdup(tmpbuf);
Jens Axboe843a7412006-06-01 21:14:21 -0700843 td->read_iolog = 1;
Jens Axboe843a7412006-06-01 21:14:21 -0700844 fgetpos(f, &off);
845 continue;
846 }
Jens Axboeaf8c0b42006-06-07 22:33:17 +0200847 if (!check_strstore(p, "write_iolog", tmpbuf)) {
848 if (!td->read_iolog) {
849 td->iolog_file = strdup(tmpbuf);
850 td->write_iolog = 1;
Jens Axboe4f693b92006-06-07 22:52:28 +0200851 } else
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200852 fprintf(f_err, "fio: read iolog overrides given write_iolog\n");
Jens Axboeaea47d42006-05-26 19:27:29 +0200853 fgetpos(f, &off);
854 continue;
855 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200856 if (!check_strstore(p, "exec_prerun", tmpbuf)) {
857 td->exec_prerun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200858 fgetpos(f, &off);
859 continue;
860 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200861 if (!check_strstore(p, "exec_postrun", tmpbuf)) {
862 td->exec_postrun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200863 fgetpos(f, &off);
864 continue;
865 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200866 if (!check_strstore(p, "ioscheduler", tmpbuf)) {
Jens Axboe22f78b32006-06-07 11:30:07 +0200867#ifndef FIO_HAVE_IOSCHED_SWITCH
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200868 fprintf(f_err, "io scheduler switching not available\n");
Jens Axboe22f78b32006-06-07 11:30:07 +0200869 ret = 1;
870 break;
871#else
Jens Axboe80b9feb2006-06-07 10:34:49 +0200872 td->ioscheduler = strdup(tmpbuf);
Jens Axboeda867742006-06-06 10:39:10 -0700873 fgetpos(f, &off);
874 continue;
Jens Axboe22f78b32006-06-07 11:30:07 +0200875#endif
Jens Axboeda867742006-06-06 10:39:10 -0700876 }
Jens Axboeebac4652005-12-08 15:25:21 +0100877
Jens Axboe45410ac2006-06-07 11:10:39 +0200878 /*
879 * Don't break here, continue parsing options so we
880 * dump all the bad ones. Makes trial/error fixups
881 * easier on the user.
882 */
Jens Axboeebac4652005-12-08 15:25:21 +0100883 printf("Client%d: bad option %s\n",td->thread_number,p);
Jens Axboe45410ac2006-06-07 11:10:39 +0200884 ret = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100885 }
Jens Axboeebac4652005-12-08 15:25:21 +0100886
Jens Axboe45410ac2006-06-07 11:10:39 +0200887 if (!ret) {
888 fsetpos(f, &off);
889 ret = add_job(td, name, 0);
890 }
891 if (ret)
892 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100893 }
894
895 free(string);
896 free(name);
Jens Axboeef899b62006-06-06 09:31:00 +0200897 free(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100898 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200899 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100900}
901
902static int fill_def_thread(void)
903{
904 memset(&def_thread, 0, sizeof(def_thread));
905
906 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
907 perror("sched_getaffinity");
908 return 1;
909 }
910
911 /*
912 * fill globals
913 */
914 def_thread.ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200915 def_thread.iomix = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100916 def_thread.bs = DEF_BS;
917 def_thread.min_bs = -1;
918 def_thread.max_bs = -1;
919 def_thread.io_engine = DEF_IO_ENGINE;
920 strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
921 def_thread.odirect = DEF_ODIRECT;
922 def_thread.ratecycle = DEF_RATE_CYCLE;
923 def_thread.sequential = DEF_SEQUENTIAL;
924 def_thread.timeout = DEF_TIMEOUT;
925 def_thread.create_file = DEF_CREATE;
926 def_thread.overwrite = DEF_OVERWRITE;
927 def_thread.invalidate_cache = DEF_INVALIDATE;
928 def_thread.sync_io = DEF_SYNCIO;
929 def_thread.mem_type = MEM_MALLOC;
930 def_thread.bw_avg_time = DEF_BWAVGTIME;
931 def_thread.create_serialize = DEF_CREATE_SER;
932 def_thread.create_fsync = DEF_CREATE_FSYNC;
933 def_thread.loops = DEF_LOOPS;
934 def_thread.verify = DEF_VERIFY;
935 def_thread.stonewall = DEF_STONEWALL;
936 def_thread.numjobs = DEF_NUMJOBS;
937 def_thread.use_thread = DEF_USE_THREAD;
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200938 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
939 def_thread.rwmixread = DEF_RWMIX_READ;
Jens Axboeb6f4d882006-06-02 10:32:51 +0200940 def_thread.nice = DEF_NICE;
Jens Axboeebac4652005-12-08 15:25:21 +0100941#ifdef FIO_HAVE_DISK_UTIL
942 def_thread.do_disk_util = 1;
943#endif
944
945 return 0;
946}
947
Jens Axboe4785f992006-05-26 03:59:10 +0200948static void usage(char *name)
949{
950 printf("%s\n", fio_version_string);
951 printf("\t-s IO is sequential\n");
952 printf("\t-b Block size in KiB for each IO\n");
953 printf("\t-t Runtime in seconds\n");
954 printf("\t-R Exit all threads on failure to meet rate goal\n");
955 printf("\t-o Use O_DIRECT\n");
956 printf("\t-l Generate per-job latency logs\n");
957 printf("\t-w Generate per-job bandwidth logs\n");
958 printf("\t-f Job file (Required)\n");
959 printf("\t-v Print version info and exit\n");
960}
961
Jens Axboeebac4652005-12-08 15:25:21 +0100962static void parse_cmd_line(int argc, char *argv[])
963{
964 int c;
965
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200966 while ((c = getopt(argc, argv, "s:b:t:r:R:o:f:lwvhO:")) != EOF) {
Jens Axboeebac4652005-12-08 15:25:21 +0100967 switch (c) {
968 case 's':
969 def_thread.sequential = !!atoi(optarg);
970 break;
971 case 'b':
972 def_thread.bs = atoi(optarg);
973 def_thread.bs <<= 10;
974 if (!def_thread.bs) {
975 printf("bad block size\n");
976 def_thread.bs = DEF_BS;
977 }
978 break;
979 case 't':
980 def_thread.timeout = atoi(optarg);
981 break;
982 case 'r':
983 repeatable = !!atoi(optarg);
984 break;
985 case 'R':
986 rate_quit = !!atoi(optarg);
987 break;
988 case 'o':
989 def_thread.odirect = !!atoi(optarg);
990 break;
991 case 'f':
992 ini_file = strdup(optarg);
993 break;
994 case 'l':
995 write_lat_log = 1;
996 break;
997 case 'w':
998 write_bw_log = 1;
999 break;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001000 case 'O':
1001 f_out = fopen(optarg, "w+");
1002 if (!f_out) {
1003 perror("fopen output");
1004 exit(1);
1005 }
1006 f_err = f_out;
1007 break;
Jens Axboe4785f992006-05-26 03:59:10 +02001008 case 'h':
1009 usage(argv[0]);
1010 exit(0);
Jens Axboeebac4652005-12-08 15:25:21 +01001011 case 'v':
1012 printf("%s\n", fio_version_string);
1013 exit(0);
1014 }
1015 }
Jens Axboec9fad892006-05-27 17:26:50 +02001016
1017 if (!ini_file && argc > 1 && argv[argc - 1][0] != '-')
1018 ini_file = strdup(argv[argc - 1]);
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001019 if (!f_out) {
1020 f_out = stdout;
1021 f_err = stderr;
1022 }
Jens Axboeebac4652005-12-08 15:25:21 +01001023}
1024
1025static void free_shm(void)
1026{
1027 struct shmid_ds sbuf;
1028
1029 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001030 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001031 threads = NULL;
1032 shmctl(shm_id, IPC_RMID, &sbuf);
1033 }
1034}
1035
1036static int setup_thread_area(void)
1037{
1038 /*
1039 * 1024 is too much on some machines, scale max_jobs if
1040 * we get a failure that looks like too large a shm segment
1041 */
1042 do {
1043 int s = max_jobs * sizeof(struct thread_data);
1044
1045 shm_id = shmget(0, s, IPC_CREAT | 0600);
1046 if (shm_id != -1)
1047 break;
1048 if (errno != EINVAL) {
1049 perror("shmget");
1050 break;
1051 }
1052
1053 max_jobs >>= 1;
1054 } while (max_jobs);
1055
1056 if (shm_id == -1)
1057 return 1;
1058
1059 threads = shmat(shm_id, NULL, 0);
1060 if (threads == (void *) -1) {
1061 perror("shmat");
1062 return 1;
1063 }
1064
1065 atexit(free_shm);
1066 return 0;
1067}
1068
1069int parse_options(int argc, char *argv[])
1070{
1071 if (setup_thread_area())
1072 return 1;
1073 if (fill_def_thread())
1074 return 1;
1075
1076 parse_cmd_line(argc, argv);
1077
1078 if (!ini_file) {
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001079 fprintf(f_err, "Need job file\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001080 usage(argv[0]);
Jens Axboeebac4652005-12-08 15:25:21 +01001081 return 1;
1082 }
1083
Jens Axboe07261982006-06-07 10:51:12 +02001084 if (parse_jobs_ini(ini_file))
Jens Axboeebac4652005-12-08 15:25:21 +01001085 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +01001086
1087 return 0;
1088}