blob: d51503519ff709875ebb56cb210a671ad118de59 [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 Axboeebac4652005-12-08 15:25:21 +010039
Jens Axboee8e387c2006-05-05 11:11:22 +020040static char fio_version_string[] = "fio 1.3";
Jens Axboeebac4652005-12-08 15:25:21 +010041
42static int repeatable = DEF_RAND_REPEAT;
43static char *ini_file;
44static int max_jobs = MAX_JOBS;
45
46struct thread_data def_thread;
47struct thread_data *threads = NULL;
48
49int rate_quit = 0;
50int write_lat_log = 0;
51int write_bw_log = 0;
52int exitall_on_terminate = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +020053unsigned long long mlock_size = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010054
55static int setup_rate(struct thread_data *td)
56{
57 int nr_reads_per_sec;
58
59 if (!td->rate)
60 return 0;
61
62 if (td->rate < td->ratemin) {
63 fprintf(stderr, "min rate larger than nominal rate\n");
64 return -1;
65 }
66
67 nr_reads_per_sec = (td->rate * 1024) / td->min_bs;
68 td->rate_usec_cycle = 1000000 / nr_reads_per_sec;
69 td->rate_pending_usleep = 0;
70 return 0;
71}
72
73static void setup_log(struct io_log **log)
74{
75 struct io_log *l = malloc(sizeof(*l));
76
77 l->nr_samples = 0;
78 l->max_samples = 1024;
79 l->log = malloc(l->max_samples * sizeof(struct io_sample));
80 *log = l;
81}
82
83void finish_log(struct thread_data *td, struct io_log *log, const char *name)
84{
Jens Axboeaea47d42006-05-26 19:27:29 +020085 char file_name[256];
Jens Axboeebac4652005-12-08 15:25:21 +010086 FILE *f;
87 unsigned int i;
88
Jens Axboeaea47d42006-05-26 19:27:29 +020089 snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
Jens Axboeebac4652005-12-08 15:25:21 +010090 f = fopen(file_name, "w");
91 if (!f) {
92 perror("fopen log");
93 return;
94 }
95
96 for (i = 0; i < log->nr_samples; i++)
97 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir);
98
99 fclose(f);
100 free(log->log);
101 free(log);
102}
103
104static struct thread_data *get_new_job(int global, struct thread_data *parent)
105{
106 struct thread_data *td;
107
108 if (global)
109 return &def_thread;
110 if (thread_number >= max_jobs)
111 return NULL;
112
113 td = &threads[thread_number++];
Jens Axboe4ae3f762006-05-30 13:35:14 +0200114 if (parent)
115 *td = *parent;
116 else
117 memset(td, 0, sizeof(*td));
Jens Axboeebac4652005-12-08 15:25:21 +0100118
119 td->fd = -1;
120 td->thread_number = thread_number;
121
122 td->ddir = parent->ddir;
123 td->ioprio = parent->ioprio;
124 td->sequential = parent->sequential;
125 td->bs = parent->bs;
126 td->min_bs = parent->min_bs;
127 td->max_bs = parent->max_bs;
128 td->odirect = parent->odirect;
129 td->thinktime = parent->thinktime;
130 td->fsync_blocks = parent->fsync_blocks;
131 td->start_delay = parent->start_delay;
132 td->timeout = parent->timeout;
133 td->io_engine = parent->io_engine;
134 td->create_file = parent->create_file;
135 td->overwrite = parent->overwrite;
136 td->invalidate_cache = parent->invalidate_cache;
137 td->file_size = parent->file_size;
138 td->file_offset = parent->file_offset;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100139 td->zone_size = parent->zone_size;
140 td->zone_skip = parent->zone_skip;
Jens Axboeebac4652005-12-08 15:25:21 +0100141 td->rate = parent->rate;
142 td->ratemin = parent->ratemin;
143 td->ratecycle = parent->ratecycle;
144 td->iodepth = parent->iodepth;
145 td->sync_io = parent->sync_io;
146 td->mem_type = parent->mem_type;
147 td->bw_avg_time = parent->bw_avg_time;
148 td->create_serialize = parent->create_serialize;
149 td->create_fsync = parent->create_fsync;
150 td->loops = parent->loops;
151 td->verify = parent->verify;
152 td->stonewall = parent->stonewall;
153 td->numjobs = parent->numjobs;
154 td->use_thread = parent->use_thread;
155 td->do_disk_util = parent->do_disk_util;
156 memcpy(&td->cpumask, &parent->cpumask, sizeof(td->cpumask));
157 strcpy(td->io_engine_name, parent->io_engine_name);
158
159 return td;
160}
161
162static void put_job(struct thread_data *td)
163{
164 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
165 thread_number--;
166}
167
Jens Axboe75154842006-06-01 13:56:09 +0200168static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100169{
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200170 char *ddir_str[] = { "read", "write", "randread", "randwrite",
171 "rw", NULL, "randrw" };
Jens Axboeebac4652005-12-08 15:25:21 +0100172 struct stat sb;
173 int numjobs, ddir;
174
175#ifndef FIO_HAVE_LIBAIO
176 if (td->io_engine == FIO_LIBAIO) {
177 fprintf(stderr, "Linux libaio not available\n");
178 return 1;
179 }
180#endif
181#ifndef FIO_HAVE_POSIXAIO
182 if (td->io_engine == FIO_POSIXAIO) {
183 fprintf(stderr, "posix aio not available\n");
184 return 1;
185 }
186#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100187
188 /*
189 * the def_thread is just for options, it's not a real job
190 */
191 if (td == &def_thread)
192 return 0;
193
194 if (td->io_engine & FIO_SYNCIO)
195 td->iodepth = 1;
196 else {
197 if (!td->iodepth)
198 td->iodepth = 1;
199 }
200
Jens Axboe20dc95c2005-12-09 10:29:35 +0100201 /*
202 * only really works for sequential io for now
203 */
204 if (td->zone_size && !td->sequential)
205 td->zone_size = 0;
206
Jens Axboeebac4652005-12-08 15:25:21 +0100207 td->filetype = FIO_TYPE_FILE;
Jens Axboe0af7b542006-02-17 10:10:12 +0100208 if (!stat(jobname, &sb)) {
209 if (S_ISBLK(sb.st_mode))
210 td->filetype = FIO_TYPE_BD;
211 else if (S_ISCHR(sb.st_mode))
212 td->filetype = FIO_TYPE_CHAR;
213 }
Jens Axboeebac4652005-12-08 15:25:21 +0100214
215 if (td->filetype == FIO_TYPE_FILE) {
216 if (td->directory[0] != '\0')
Jens Axboeeba09dd2006-05-22 20:20:54 +0200217 sprintf(td->file_name, "%s/%s.%d", td->directory, jobname, td->jobnum);
Jens Axboeebac4652005-12-08 15:25:21 +0100218 else
Jens Axboeeba09dd2006-05-22 20:20:54 +0200219 sprintf(td->file_name, "%s.%d", jobname, td->jobnum);
Jens Axboeebac4652005-12-08 15:25:21 +0100220 } else
Jens Axboeaea47d42006-05-26 19:27:29 +0200221 strncpy(td->file_name, jobname, sizeof(td->file_name) - 1);
Jens Axboeebac4652005-12-08 15:25:21 +0100222
223 sem_init(&td->mutex, 0, 0);
224
225 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
226 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
227 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
228
229 if (td->min_bs == -1U)
230 td->min_bs = td->bs;
231 if (td->max_bs == -1U)
232 td->max_bs = td->bs;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200233 if (td_read(td) && !td_rw(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100234 td->verify = 0;
235
236 if (td->stonewall && td->thread_number > 1)
237 groupid++;
238
239 td->groupid = groupid;
240
241 if (setup_rate(td))
242 goto err;
243
244 if (write_lat_log) {
245 setup_log(&td->slat_log);
246 setup_log(&td->clat_log);
247 }
248 if (write_bw_log)
249 setup_log(&td->bw_log);
250
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200251 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
Jens Axboe75154842006-06-01 13:56:09 +0200252
253 if (!job_add_num)
254 printf("Client%d (g=%d): rw=%s, odir=%d, bs=%d-%d, rate=%d, ioengine=%s, iodepth=%d\n", td->thread_number, td->groupid, ddir_str[ddir], td->odirect, td->min_bs, td->max_bs, td->rate, td->io_engine_name, td->iodepth);
255 else if (job_add_num == 1)
256 printf("...\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100257
258 /*
259 * recurse add identical jobs, clear numjobs and stonewall options
260 * as they don't apply to sub-jobs
261 */
262 numjobs = td->numjobs;
263 while (--numjobs) {
264 struct thread_data *td_new = get_new_job(0, td);
265
266 if (!td_new)
267 goto err;
268
269 td_new->numjobs = 1;
270 td_new->stonewall = 0;
Jens Axboeeba09dd2006-05-22 20:20:54 +0200271 td_new->jobnum = numjobs;
Jens Axboe75154842006-06-01 13:56:09 +0200272 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100273
Jens Axboe75154842006-06-01 13:56:09 +0200274 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100275 goto err;
276 }
277 return 0;
278err:
279 put_job(td);
280 return -1;
281}
282
283int init_random_state(struct thread_data *td)
284{
285 unsigned long seed;
286 int fd, num_maps, blocks;
287
Jens Axboe1ac267b2006-05-31 20:29:00 +0200288 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100289 if (fd == -1) {
290 td_verror(td, errno);
291 return 1;
292 }
293
294 if (read(fd, &seed, sizeof(seed)) < (int) sizeof(seed)) {
295 td_verror(td, EIO);
296 close(fd);
297 return 1;
298 }
299
300 close(fd);
301
302 srand48_r(seed, &td->bsrange_state);
303 srand48_r(seed, &td->verify_state);
304
305 if (td->sequential)
306 return 0;
307
308 if (repeatable)
309 seed = DEF_RANDSEED;
310
311 blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
312 num_maps = blocks / BLOCKS_PER_MAP;
313 td->file_map = malloc(num_maps * sizeof(long));
314 td->num_maps = num_maps;
315 memset(td->file_map, 0, num_maps * sizeof(long));
316
317 srand48_r(seed, &td->random_state);
318 return 0;
319}
320
321static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
322{
323#ifdef FIO_HAVE_CPU_AFFINITY
324 unsigned int i;
325
326 CPU_ZERO(&cpumask);
327
328 for (i = 0; i < sizeof(int) * 8; i++) {
329 if ((1 << i) & cpu)
330 CPU_SET(i, &cpumask);
331 }
332#endif
333}
334
335static unsigned long get_mult(char c)
336{
337 switch (c) {
338 case 'k':
339 case 'K':
340 return 1024;
341 case 'm':
342 case 'M':
343 return 1024 * 1024;
344 case 'g':
345 case 'G':
346 return 1024 * 1024 * 1024;
347 default:
348 return 1;
349 }
350}
351
352/*
353 * convert string after '=' into decimal value, noting any size suffix
354 */
355static int str_cnv(char *p, unsigned long long *val)
356{
357 char *str;
358 int len;
359
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100360 str = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100361 if (!str)
362 return 1;
363
364 str++;
365 len = strlen(str);
366
367 *val = strtoul(str, NULL, 10);
368 if (*val == ULONG_MAX && errno == ERANGE)
369 return 1;
370
Jens Axboe9d0d4242006-05-30 13:45:51 +0200371 *val *= get_mult(str[len - 1]);
Jens Axboeebac4652005-12-08 15:25:21 +0100372 return 0;
373}
374
375static int check_strcnv(char *p, char *name, unsigned long long *val)
376{
Jens Axboe20dc95c2005-12-09 10:29:35 +0100377 if (strncmp(p, name, strlen(name) - 1))
Jens Axboeebac4652005-12-08 15:25:21 +0100378 return 1;
379
380 return str_cnv(p, val);
381}
382
383static void strip_blank_front(char **p)
384{
385 char *s = *p;
386
Jens Axboe4ae3f762006-05-30 13:35:14 +0200387 while (isspace(*s))
Jens Axboeebac4652005-12-08 15:25:21 +0100388 s++;
389}
390
391static void strip_blank_end(char *p)
392{
Jens Axboe4ae3f762006-05-30 13:35:14 +0200393 char *s = p + strlen(p) - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100394
Jens Axboe4ae3f762006-05-30 13:35:14 +0200395 while (isspace(*s) || iscntrl(*s))
396 s--;
Jens Axboeaea47d42006-05-26 19:27:29 +0200397
Jens Axboe4ae3f762006-05-30 13:35:14 +0200398 *(s + 1) = '\0';
Jens Axboeaea47d42006-05-26 19:27:29 +0200399}
400
Jens Axboeebac4652005-12-08 15:25:21 +0100401typedef int (str_cb_fn)(struct thread_data *, char *);
402
403static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
404{
Jens Axboe843a7412006-06-01 21:14:21 -0700405 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100406
Jens Axboe843a7412006-06-01 21:14:21 -0700407 if (strncmp(p, name, strlen(name)))
408 return 1;
409
410 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100411 if (!s)
412 return 1;
413
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100414 s = strchr(s, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100415 if (!s)
416 return 1;
417
418 s++;
419 strip_blank_front(&s);
420 return cb(td, s);
421}
422
423static int check_strstore(char *p, char *name, char *dest)
424{
Jens Axboe843a7412006-06-01 21:14:21 -0700425 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100426
Jens Axboe843a7412006-06-01 21:14:21 -0700427 if (strncmp(p, name, strlen(name)))
428 return 1;
429
430 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100431 if (!s)
432 return 1;
433
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100434 s = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100435 if (!s)
436 return 1;
437
438 s++;
439 strip_blank_front(&s);
440
441 strcpy(dest, s);
Jens Axboeebac4652005-12-08 15:25:21 +0100442 return 0;
443}
444
Jens Axboe01617be2005-12-08 19:50:40 +0100445static int __check_range(char *str, unsigned long *val)
Jens Axboeebac4652005-12-08 15:25:21 +0100446{
Jens Axboe01617be2005-12-08 19:50:40 +0100447 char suffix;
Jens Axboeebac4652005-12-08 15:25:21 +0100448
Jens Axboe01617be2005-12-08 19:50:40 +0100449 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
450 *val *= get_mult(suffix);
Jens Axboeebac4652005-12-08 15:25:21 +0100451 return 0;
452 }
453
Jens Axboe01617be2005-12-08 19:50:40 +0100454 if (sscanf(str, "%lu", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100455 return 0;
456
457 return 1;
Jens Axboe01617be2005-12-08 19:50:40 +0100458}
Jens Axboeebac4652005-12-08 15:25:21 +0100459
Jens Axboe01617be2005-12-08 19:50:40 +0100460static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
461{
462 char option[128];
463 char *str, *p1, *p2;
464
Jens Axboe843a7412006-06-01 21:14:21 -0700465 if (strncmp(p, name, strlen(name)))
466 return 1;
467
Jens Axboe01617be2005-12-08 19:50:40 +0100468 strcpy(option, p);
469 p = option;
470
471 str = strstr(p, name);
472 if (!str)
473 return 1;
474
475 p += strlen(name);
476
477 str = strchr(p, '=');
478 if (!str)
479 return 1;
480
481 /*
482 * 'p' now holds whatever is after the '=' sign
483 */
484 p1 = str + 1;
485
486 /*
487 * terminate p1 at the '-' sign
488 */
489 p = strchr(p1, '-');
490 if (!p)
491 return 1;
492
493 p2 = p + 1;
494 *p = '\0';
495
496 if (!__check_range(p1, s) && !__check_range(p2, e))
497 return 0;
498
499 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100500}
501
502static int check_int(char *p, char *name, unsigned int *val)
503{
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100504 char *str;
Jens Axboeebac4652005-12-08 15:25:21 +0100505
Jens Axboeb6754f92006-05-30 14:29:32 +0200506 if (strncmp(p, name, strlen(name)))
507 return 1;
508
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100509 str = strstr(p, name);
510 if (!str)
511 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100512
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100513 str = strchr(p, '=');
514 if (!str)
515 return 1;
516
517 str++;
518
519 if (sscanf(str, "%u", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100520 return 0;
521
522 return 1;
523}
524
525static int check_strset(char *p, char *name)
526{
527 return strncmp(p, name, strlen(name));
528}
529
530static int is_empty_or_comment(char *line)
531{
532 unsigned int i;
533
534 for (i = 0; i < strlen(line); i++) {
535 if (line[i] == ';')
536 return 1;
537 if (!isspace(line[i]) && !iscntrl(line[i]))
538 return 0;
539 }
540
541 return 1;
542}
543
544static int str_rw_cb(struct thread_data *td, char *mem)
545{
546 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
547 td->ddir = DDIR_READ;
548 td->sequential = 1;
549 return 0;
550 } else if (!strncmp(mem, "randread", 8)) {
551 td->ddir = DDIR_READ;
552 td->sequential = 0;
553 return 0;
554 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
555 td->ddir = DDIR_WRITE;
556 td->sequential = 1;
557 return 0;
558 } else if (!strncmp(mem, "randwrite", 9)) {
559 td->ddir = DDIR_WRITE;
560 td->sequential = 0;
561 return 0;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200562 } else if (!strncmp(mem, "rw", 2)) {
563 td->ddir = 0;
564 td->iomix = 1;
565 td->sequential = 1;
566 return 0;
567 } else if (!strncmp(mem, "randrw", 6)) {
568 td->ddir = 0;
569 td->iomix = 1;
570 td->sequential = 0;
571 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100572 }
573
574 fprintf(stderr, "bad data direction: %s\n", mem);
575 return 1;
576}
577
578static int str_verify_cb(struct thread_data *td, char *mem)
579{
580 if (!strncmp(mem, "0", 1)) {
581 td->verify = VERIFY_NONE;
582 return 0;
583 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
584 td->verify = VERIFY_MD5;
585 return 0;
586 } else if (!strncmp(mem, "crc32", 5)) {
587 td->verify = VERIFY_CRC32;
588 return 0;
589 }
590
591 fprintf(stderr, "bad verify type: %s\n", mem);
592 return 1;
593}
594
595static int str_mem_cb(struct thread_data *td, char *mem)
596{
597 if (!strncmp(mem, "malloc", 6)) {
598 td->mem_type = MEM_MALLOC;
599 return 0;
600 } else if (!strncmp(mem, "shm", 3)) {
601 td->mem_type = MEM_SHM;
602 return 0;
603 } else if (!strncmp(mem, "mmap", 4)) {
604 td->mem_type = MEM_MMAP;
605 return 0;
606 }
607
608 fprintf(stderr, "bad mem type: %s\n", mem);
609 return 1;
610}
611
612static int str_ioengine_cb(struct thread_data *td, char *str)
613{
614 if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
615 !strncmp(str, "libaio", 6)) {
616 strcpy(td->io_engine_name, "libaio");
617 td->io_engine = FIO_LIBAIO;
618 return 0;
619 } else if (!strncmp(str, "posixaio", 8)) {
620 strcpy(td->io_engine_name, "posixaio");
621 td->io_engine = FIO_POSIXAIO;
622 return 0;
623 } else if (!strncmp(str, "sync", 4)) {
624 strcpy(td->io_engine_name, "sync");
625 td->io_engine = FIO_SYNCIO;
626 return 0;
627 } else if (!strncmp(str, "mmap", 4)) {
628 strcpy(td->io_engine_name, "mmap");
629 td->io_engine = FIO_MMAPIO;
630 return 0;
631 } else if (!strncmp(str, "sgio", 4)) {
632 strcpy(td->io_engine_name, "sgio");
633 td->io_engine = FIO_SGIO;
634 return 0;
Jens Axboe8756e4d2006-05-27 20:24:53 +0200635 } else if (!strncmp(str, "splice", 6)) {
636 strcpy(td->io_engine_name, "splice");
637 td->io_engine = FIO_SPLICEIO;
638 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100639 }
640
641 fprintf(stderr, "bad ioengine type: %s\n", str);
642 return 1;
643}
644
Jens Axboeaea47d42006-05-26 19:27:29 +0200645static int str_iolog_cb(struct thread_data *td, char *file)
646{
Jens Axboeaea47d42006-05-26 19:27:29 +0200647 strncpy(td->iolog_file, file, sizeof(td->iolog_file) - 1);
Jens Axboeaea47d42006-05-26 19:27:29 +0200648 return 0;
649}
Jens Axboeebac4652005-12-08 15:25:21 +0100650
651int parse_jobs_ini(char *file)
652{
653 unsigned int prioclass, prio, cpu, global;
654 unsigned long long ull;
655 unsigned long ul1, ul2;
656 struct thread_data *td;
657 char *string, *name;
658 fpos_t off;
659 FILE *f;
660 char *p;
661
662 f = fopen(file, "r");
663 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200664 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100665 return 1;
666 }
667
668 string = malloc(4096);
669 name = malloc(256);
670
671 while ((p = fgets(string, 4096, f)) != NULL) {
672 if (is_empty_or_comment(p))
673 continue;
674 if (sscanf(p, "[%s]", name) != 1)
675 continue;
676
677 global = !strncmp(name, "global", 6);
678
679 name[strlen(name) - 1] = '\0';
680
681 td = get_new_job(global, &def_thread);
682 if (!td)
683 return 1;
684
Jens Axboeebac4652005-12-08 15:25:21 +0100685 fgetpos(f, &off);
686 while ((p = fgets(string, 4096, f)) != NULL) {
687 if (is_empty_or_comment(p))
688 continue;
689 if (strstr(p, "["))
690 break;
Jens Axboeb6754f92006-05-30 14:29:32 +0200691 strip_blank_front(&p);
Jens Axboe4ae3f762006-05-30 13:35:14 +0200692 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200693
Jens Axboeebac4652005-12-08 15:25:21 +0100694 if (!check_int(p, "prio", &prio)) {
695#ifndef FIO_HAVE_IOPRIO
696 fprintf(stderr, "io priorities not available\n");
697 return 1;
698#endif
Jens Axboe8756e4d2006-05-27 20:24:53 +0200699 td->ioprio |= prio;
Jens Axboeebac4652005-12-08 15:25:21 +0100700 fgetpos(f, &off);
701 continue;
702 }
703 if (!check_int(p, "prioclass", &prioclass)) {
704#ifndef FIO_HAVE_IOPRIO
705 fprintf(stderr, "io priorities not available\n");
706 return 1;
707#endif
Jens Axboe8756e4d2006-05-27 20:24:53 +0200708 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
Jens Axboeebac4652005-12-08 15:25:21 +0100709 fgetpos(f, &off);
710 continue;
711 }
712 if (!check_int(p, "direct", &td->odirect)) {
713 fgetpos(f, &off);
714 continue;
715 }
716 if (!check_int(p, "rate", &td->rate)) {
717 fgetpos(f, &off);
718 continue;
719 }
720 if (!check_int(p, "ratemin", &td->ratemin)) {
721 fgetpos(f, &off);
722 continue;
723 }
724 if (!check_int(p, "ratecycle", &td->ratecycle)) {
725 fgetpos(f, &off);
726 continue;
727 }
728 if (!check_int(p, "thinktime", &td->thinktime)) {
729 fgetpos(f, &off);
730 continue;
731 }
732 if (!check_int(p, "cpumask", &cpu)) {
733#ifndef FIO_HAVE_CPU_AFFINITY
734 fprintf(stderr, "cpu affinity not available\n");
735 return 1;
736#endif
737 fill_cpu_mask(td->cpumask, cpu);
738 fgetpos(f, &off);
739 continue;
740 }
741 if (!check_int(p, "fsync", &td->fsync_blocks)) {
742 fgetpos(f, &off);
Jens Axboefc1a4712006-05-30 13:04:05 +0200743 td->end_fsync = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100744 continue;
745 }
746 if (!check_int(p, "startdelay", &td->start_delay)) {
747 fgetpos(f, &off);
748 continue;
749 }
750 if (!check_int(p, "timeout", &td->timeout)) {
751 fgetpos(f, &off);
752 continue;
753 }
754 if (!check_int(p, "invalidate",&td->invalidate_cache)) {
755 fgetpos(f, &off);
756 continue;
757 }
758 if (!check_int(p, "iodepth", &td->iodepth)) {
759 fgetpos(f, &off);
760 continue;
761 }
762 if (!check_int(p, "sync", &td->sync_io)) {
763 fgetpos(f, &off);
764 continue;
765 }
766 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
767 fgetpos(f, &off);
768 continue;
769 }
770 if (!check_int(p, "create_serialize", &td->create_serialize)) {
771 fgetpos(f, &off);
772 continue;
773 }
774 if (!check_int(p, "create_fsync", &td->create_fsync)) {
775 fgetpos(f, &off);
776 continue;
777 }
Jens Axboefc1a4712006-05-30 13:04:05 +0200778 if (!check_int(p, "end_fsync", &td->end_fsync)) {
779 fgetpos(f, &off);
780 continue;
781 }
Jens Axboeebac4652005-12-08 15:25:21 +0100782 if (!check_int(p, "loops", &td->loops)) {
783 fgetpos(f, &off);
784 continue;
785 }
786 if (!check_int(p, "numjobs", &td->numjobs)) {
787 fgetpos(f, &off);
788 continue;
789 }
790 if (!check_int(p, "overwrite", &td->overwrite)) {
791 fgetpos(f, &off);
792 continue;
793 }
794 if (!check_range(p, "bsrange", &ul1, &ul2)) {
795 if (ul1 > ul2) {
796 td->max_bs = ul1;
797 td->min_bs = ul2;
798 } else {
799 td->max_bs = ul2;
800 td->min_bs = ul1;
801 }
802 fgetpos(f, &off);
803 continue;
804 }
805 if (!check_strcnv(p, "bs", &ull)) {
806 td->bs = ull;
807 fgetpos(f, &off);
808 continue;
809 }
810 if (!check_strcnv(p, "size", &td->file_size)) {
811 fgetpos(f, &off);
812 continue;
813 }
814 if (!check_strcnv(p, "offset", &td->file_offset)) {
815 fgetpos(f, &off);
816 continue;
817 }
Jens Axboe20dc95c2005-12-09 10:29:35 +0100818 if (!check_strcnv(p, "zonesize", &td->zone_size)) {
819 fgetpos(f, &off);
820 continue;
821 }
822 if (!check_strcnv(p, "zoneskip", &td->zone_skip)) {
823 fgetpos(f, &off);
824 continue;
825 }
Jens Axboec04f7ec2006-05-31 10:13:16 +0200826 if (!check_strcnv(p, "lockmem", &mlock_size)) {
827 fgetpos(f, &off);
828 continue;
829 }
Jens Axboeebac4652005-12-08 15:25:21 +0100830 if (!check_strstore(p, "directory", td->directory)) {
831 fgetpos(f, &off);
832 continue;
833 }
834 if (!check_str(p, "mem", str_mem_cb, td)) {
835 fgetpos(f, &off);
836 continue;
837 }
838 if (!check_str(p, "verify", str_verify_cb, td)) {
839 fgetpos(f, &off);
840 continue;
841 }
842 if (!check_str(p, "rw", str_rw_cb, td)) {
843 fgetpos(f, &off);
844 continue;
845 }
846 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
847 fgetpos(f, &off);
848 continue;
849 }
850 if (!check_strset(p, "create")) {
851 td->create_file = 1;
852 fgetpos(f, &off);
853 continue;
854 }
855 if (!check_strset(p, "exitall")) {
856 exitall_on_terminate = 1;
857 fgetpos(f, &off);
858 continue;
859 }
860 if (!check_strset(p, "stonewall")) {
861 td->stonewall = 1;
862 fgetpos(f, &off);
863 continue;
864 }
865 if (!check_strset(p, "thread")) {
866 td->use_thread = 1;
867 fgetpos(f, &off);
868 continue;
869 }
Jens Axboeaea47d42006-05-26 19:27:29 +0200870 if (!check_str(p, "iolog", str_iolog_cb, td)) {
Jens Axboe843a7412006-06-01 21:14:21 -0700871 printf("got read iolog\n");
872 td->read_iolog = 1;
873 td->write_iolog = 0;
874 fgetpos(f, &off);
875 continue;
876 }
877 if (!td->read_iolog &&
878 !check_str(p, "write_iolog", str_iolog_cb, td)) {
879 printf("got write iolog\n");
880 td->write_iolog = 1;
Jens Axboeaea47d42006-05-26 19:27:29 +0200881 fgetpos(f, &off);
882 continue;
883 }
Jens Axboeebac4652005-12-08 15:25:21 +0100884
885 printf("Client%d: bad option %s\n",td->thread_number,p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200886 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100887 }
888 fsetpos(f, &off);
889
Jens Axboe75154842006-06-01 13:56:09 +0200890 if (add_job(td, name, 0))
Jens Axboeebac4652005-12-08 15:25:21 +0100891 return 1;
892 }
893
894 free(string);
895 free(name);
896 fclose(f);
897 return 0;
898}
899
900static int fill_def_thread(void)
901{
902 memset(&def_thread, 0, sizeof(def_thread));
903
904 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
905 perror("sched_getaffinity");
906 return 1;
907 }
908
909 /*
910 * fill globals
911 */
912 def_thread.ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200913 def_thread.iomix = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100914 def_thread.bs = DEF_BS;
915 def_thread.min_bs = -1;
916 def_thread.max_bs = -1;
917 def_thread.io_engine = DEF_IO_ENGINE;
918 strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
919 def_thread.odirect = DEF_ODIRECT;
920 def_thread.ratecycle = DEF_RATE_CYCLE;
921 def_thread.sequential = DEF_SEQUENTIAL;
922 def_thread.timeout = DEF_TIMEOUT;
923 def_thread.create_file = DEF_CREATE;
924 def_thread.overwrite = DEF_OVERWRITE;
925 def_thread.invalidate_cache = DEF_INVALIDATE;
926 def_thread.sync_io = DEF_SYNCIO;
927 def_thread.mem_type = MEM_MALLOC;
928 def_thread.bw_avg_time = DEF_BWAVGTIME;
929 def_thread.create_serialize = DEF_CREATE_SER;
930 def_thread.create_fsync = DEF_CREATE_FSYNC;
931 def_thread.loops = DEF_LOOPS;
932 def_thread.verify = DEF_VERIFY;
933 def_thread.stonewall = DEF_STONEWALL;
934 def_thread.numjobs = DEF_NUMJOBS;
935 def_thread.use_thread = DEF_USE_THREAD;
936#ifdef FIO_HAVE_DISK_UTIL
937 def_thread.do_disk_util = 1;
938#endif
939
940 return 0;
941}
942
Jens Axboe4785f992006-05-26 03:59:10 +0200943static void usage(char *name)
944{
945 printf("%s\n", fio_version_string);
946 printf("\t-s IO is sequential\n");
947 printf("\t-b Block size in KiB for each IO\n");
948 printf("\t-t Runtime in seconds\n");
949 printf("\t-R Exit all threads on failure to meet rate goal\n");
950 printf("\t-o Use O_DIRECT\n");
951 printf("\t-l Generate per-job latency logs\n");
952 printf("\t-w Generate per-job bandwidth logs\n");
953 printf("\t-f Job file (Required)\n");
954 printf("\t-v Print version info and exit\n");
955}
956
Jens Axboeebac4652005-12-08 15:25:21 +0100957static void parse_cmd_line(int argc, char *argv[])
958{
959 int c;
960
Jens Axboe4785f992006-05-26 03:59:10 +0200961 while ((c = getopt(argc, argv, "s:b:t:r:R:o:f:lwvh")) != EOF) {
Jens Axboeebac4652005-12-08 15:25:21 +0100962 switch (c) {
963 case 's':
964 def_thread.sequential = !!atoi(optarg);
965 break;
966 case 'b':
967 def_thread.bs = atoi(optarg);
968 def_thread.bs <<= 10;
969 if (!def_thread.bs) {
970 printf("bad block size\n");
971 def_thread.bs = DEF_BS;
972 }
973 break;
974 case 't':
975 def_thread.timeout = atoi(optarg);
976 break;
977 case 'r':
978 repeatable = !!atoi(optarg);
979 break;
980 case 'R':
981 rate_quit = !!atoi(optarg);
982 break;
983 case 'o':
984 def_thread.odirect = !!atoi(optarg);
985 break;
986 case 'f':
987 ini_file = strdup(optarg);
988 break;
989 case 'l':
990 write_lat_log = 1;
991 break;
992 case 'w':
993 write_bw_log = 1;
994 break;
Jens Axboe4785f992006-05-26 03:59:10 +0200995 case 'h':
996 usage(argv[0]);
997 exit(0);
Jens Axboeebac4652005-12-08 15:25:21 +0100998 case 'v':
999 printf("%s\n", fio_version_string);
1000 exit(0);
1001 }
1002 }
Jens Axboec9fad892006-05-27 17:26:50 +02001003
1004 if (!ini_file && argc > 1 && argv[argc - 1][0] != '-')
1005 ini_file = strdup(argv[argc - 1]);
Jens Axboeebac4652005-12-08 15:25:21 +01001006}
1007
1008static void free_shm(void)
1009{
1010 struct shmid_ds sbuf;
1011
1012 if (threads) {
1013 shmdt(threads);
1014 threads = NULL;
1015 shmctl(shm_id, IPC_RMID, &sbuf);
1016 }
1017}
1018
1019static int setup_thread_area(void)
1020{
1021 /*
1022 * 1024 is too much on some machines, scale max_jobs if
1023 * we get a failure that looks like too large a shm segment
1024 */
1025 do {
1026 int s = max_jobs * sizeof(struct thread_data);
1027
1028 shm_id = shmget(0, s, IPC_CREAT | 0600);
1029 if (shm_id != -1)
1030 break;
1031 if (errno != EINVAL) {
1032 perror("shmget");
1033 break;
1034 }
1035
1036 max_jobs >>= 1;
1037 } while (max_jobs);
1038
1039 if (shm_id == -1)
1040 return 1;
1041
1042 threads = shmat(shm_id, NULL, 0);
1043 if (threads == (void *) -1) {
1044 perror("shmat");
1045 return 1;
1046 }
1047
1048 atexit(free_shm);
1049 return 0;
1050}
1051
1052int parse_options(int argc, char *argv[])
1053{
1054 if (setup_thread_area())
1055 return 1;
1056 if (fill_def_thread())
1057 return 1;
1058
1059 parse_cmd_line(argc, argv);
1060
1061 if (!ini_file) {
1062 printf("Need job file\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001063 usage(argv[0]);
Jens Axboeebac4652005-12-08 15:25:21 +01001064 return 1;
1065 }
1066
Jens Axboec9fad892006-05-27 17:26:50 +02001067 if (parse_jobs_ini(ini_file)) {
1068 usage(argv[0]);
Jens Axboeebac4652005-12-08 15:25:21 +01001069 return 1;
Jens Axboec9fad892006-05-27 17:26:50 +02001070 }
Jens Axboeebac4652005-12-08 15:25:21 +01001071
1072 return 0;
1073}