blob: d2122e3fd3c7f17361b0f1d7ced0dab4fcabc510 [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
58static int setup_rate(struct thread_data *td)
59{
60 int nr_reads_per_sec;
61
62 if (!td->rate)
63 return 0;
64
65 if (td->rate < td->ratemin) {
66 fprintf(stderr, "min rate larger than nominal rate\n");
67 return -1;
68 }
69
70 nr_reads_per_sec = (td->rate * 1024) / td->min_bs;
71 td->rate_usec_cycle = 1000000 / nr_reads_per_sec;
72 td->rate_pending_usleep = 0;
73 return 0;
74}
75
76static void setup_log(struct io_log **log)
77{
78 struct io_log *l = malloc(sizeof(*l));
79
80 l->nr_samples = 0;
81 l->max_samples = 1024;
82 l->log = malloc(l->max_samples * sizeof(struct io_sample));
83 *log = l;
84}
85
86void finish_log(struct thread_data *td, struct io_log *log, const char *name)
87{
Jens Axboeaea47d42006-05-26 19:27:29 +020088 char file_name[256];
Jens Axboeebac4652005-12-08 15:25:21 +010089 FILE *f;
90 unsigned int i;
91
Jens Axboeaea47d42006-05-26 19:27:29 +020092 snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
Jens Axboeebac4652005-12-08 15:25:21 +010093 f = fopen(file_name, "w");
94 if (!f) {
95 perror("fopen log");
96 return;
97 }
98
99 for (i = 0; i < log->nr_samples; i++)
100 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir);
101
102 fclose(f);
103 free(log->log);
104 free(log);
105}
106
107static struct thread_data *get_new_job(int global, struct thread_data *parent)
108{
109 struct thread_data *td;
110
111 if (global)
112 return &def_thread;
113 if (thread_number >= max_jobs)
114 return NULL;
115
116 td = &threads[thread_number++];
Jens Axboe4ae3f762006-05-30 13:35:14 +0200117 if (parent)
118 *td = *parent;
119 else
120 memset(td, 0, sizeof(*td));
Jens Axboeebac4652005-12-08 15:25:21 +0100121
122 td->fd = -1;
123 td->thread_number = thread_number;
124
125 td->ddir = parent->ddir;
126 td->ioprio = parent->ioprio;
127 td->sequential = parent->sequential;
128 td->bs = parent->bs;
129 td->min_bs = parent->min_bs;
130 td->max_bs = parent->max_bs;
131 td->odirect = parent->odirect;
132 td->thinktime = parent->thinktime;
133 td->fsync_blocks = parent->fsync_blocks;
134 td->start_delay = parent->start_delay;
135 td->timeout = parent->timeout;
136 td->io_engine = parent->io_engine;
137 td->create_file = parent->create_file;
138 td->overwrite = parent->overwrite;
139 td->invalidate_cache = parent->invalidate_cache;
140 td->file_size = parent->file_size;
141 td->file_offset = parent->file_offset;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100142 td->zone_size = parent->zone_size;
143 td->zone_skip = parent->zone_skip;
Jens Axboeebac4652005-12-08 15:25:21 +0100144 td->rate = parent->rate;
145 td->ratemin = parent->ratemin;
146 td->ratecycle = parent->ratecycle;
147 td->iodepth = parent->iodepth;
148 td->sync_io = parent->sync_io;
149 td->mem_type = parent->mem_type;
150 td->bw_avg_time = parent->bw_avg_time;
151 td->create_serialize = parent->create_serialize;
152 td->create_fsync = parent->create_fsync;
153 td->loops = parent->loops;
154 td->verify = parent->verify;
155 td->stonewall = parent->stonewall;
156 td->numjobs = parent->numjobs;
157 td->use_thread = parent->use_thread;
158 td->do_disk_util = parent->do_disk_util;
159 memcpy(&td->cpumask, &parent->cpumask, sizeof(td->cpumask));
160 strcpy(td->io_engine_name, parent->io_engine_name);
161
162 return td;
163}
164
165static void put_job(struct thread_data *td)
166{
167 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
168 thread_number--;
169}
170
Jens Axboe75154842006-06-01 13:56:09 +0200171static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100172{
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200173 char *ddir_str[] = { "read", "write", "randread", "randwrite",
174 "rw", NULL, "randrw" };
Jens Axboeebac4652005-12-08 15:25:21 +0100175 struct stat sb;
176 int numjobs, ddir;
177
178#ifndef FIO_HAVE_LIBAIO
179 if (td->io_engine == FIO_LIBAIO) {
180 fprintf(stderr, "Linux libaio not available\n");
181 return 1;
182 }
183#endif
184#ifndef FIO_HAVE_POSIXAIO
185 if (td->io_engine == FIO_POSIXAIO) {
186 fprintf(stderr, "posix aio not available\n");
187 return 1;
188 }
189#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100190
191 /*
192 * the def_thread is just for options, it's not a real job
193 */
194 if (td == &def_thread)
195 return 0;
196
197 if (td->io_engine & FIO_SYNCIO)
198 td->iodepth = 1;
199 else {
200 if (!td->iodepth)
201 td->iodepth = 1;
202 }
203
Jens Axboe20dc95c2005-12-09 10:29:35 +0100204 /*
205 * only really works for sequential io for now
206 */
207 if (td->zone_size && !td->sequential)
208 td->zone_size = 0;
209
Jens Axboeebac4652005-12-08 15:25:21 +0100210 td->filetype = FIO_TYPE_FILE;
Jens Axboe0af7b542006-02-17 10:10:12 +0100211 if (!stat(jobname, &sb)) {
212 if (S_ISBLK(sb.st_mode))
213 td->filetype = FIO_TYPE_BD;
214 else if (S_ISCHR(sb.st_mode))
215 td->filetype = FIO_TYPE_CHAR;
216 }
Jens Axboeebac4652005-12-08 15:25:21 +0100217
218 if (td->filetype == FIO_TYPE_FILE) {
Jens Axboeef899b62006-06-06 09:31:00 +0200219 if (td->directory && td->directory[0] != '\0')
Jens Axboeeba09dd2006-05-22 20:20:54 +0200220 sprintf(td->file_name, "%s/%s.%d", td->directory, jobname, td->jobnum);
Jens Axboeebac4652005-12-08 15:25:21 +0100221 else
Jens Axboeeba09dd2006-05-22 20:20:54 +0200222 sprintf(td->file_name, "%s.%d", jobname, td->jobnum);
Jens Axboeebac4652005-12-08 15:25:21 +0100223 } else
Jens Axboeaea47d42006-05-26 19:27:29 +0200224 strncpy(td->file_name, jobname, sizeof(td->file_name) - 1);
Jens Axboeebac4652005-12-08 15:25:21 +0100225
226 sem_init(&td->mutex, 0, 0);
227
228 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
229 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
230 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
231
232 if (td->min_bs == -1U)
233 td->min_bs = td->bs;
234 if (td->max_bs == -1U)
235 td->max_bs = td->bs;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200236 if (td_read(td) && !td_rw(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100237 td->verify = 0;
238
239 if (td->stonewall && td->thread_number > 1)
240 groupid++;
241
242 td->groupid = groupid;
243
244 if (setup_rate(td))
245 goto err;
246
247 if (write_lat_log) {
248 setup_log(&td->slat_log);
249 setup_log(&td->clat_log);
250 }
251 if (write_bw_log)
252 setup_log(&td->bw_log);
253
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200254 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
Jens Axboe75154842006-06-01 13:56:09 +0200255
256 if (!job_add_num)
257 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);
258 else if (job_add_num == 1)
259 printf("...\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100260
261 /*
262 * recurse add identical jobs, clear numjobs and stonewall options
263 * as they don't apply to sub-jobs
264 */
265 numjobs = td->numjobs;
266 while (--numjobs) {
267 struct thread_data *td_new = get_new_job(0, td);
268
269 if (!td_new)
270 goto err;
271
272 td_new->numjobs = 1;
273 td_new->stonewall = 0;
Jens Axboeeba09dd2006-05-22 20:20:54 +0200274 td_new->jobnum = numjobs;
Jens Axboe75154842006-06-01 13:56:09 +0200275 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100276
Jens Axboe75154842006-06-01 13:56:09 +0200277 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100278 goto err;
279 }
280 return 0;
281err:
282 put_job(td);
283 return -1;
284}
285
286int init_random_state(struct thread_data *td)
287{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200288 unsigned long seeds[4];
Jens Axboeebac4652005-12-08 15:25:21 +0100289 int fd, num_maps, blocks;
290
Jens Axboe1ac267b2006-05-31 20:29:00 +0200291 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100292 if (fd == -1) {
293 td_verror(td, errno);
294 return 1;
295 }
296
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200297 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100298 td_verror(td, EIO);
299 close(fd);
300 return 1;
301 }
302
303 close(fd);
304
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200305 srand48_r(seeds[0], &td->bsrange_state);
306 srand48_r(seeds[1], &td->verify_state);
307 srand48_r(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100308
309 if (td->sequential)
310 return 0;
311
312 if (repeatable)
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200313 seeds[3] = DEF_RANDSEED;
Jens Axboeebac4652005-12-08 15:25:21 +0100314
315 blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
316 num_maps = blocks / BLOCKS_PER_MAP;
317 td->file_map = malloc(num_maps * sizeof(long));
318 td->num_maps = num_maps;
319 memset(td->file_map, 0, num_maps * sizeof(long));
320
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200321 srand48_r(seeds[3], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100322 return 0;
323}
324
325static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
326{
327#ifdef FIO_HAVE_CPU_AFFINITY
328 unsigned int i;
329
330 CPU_ZERO(&cpumask);
331
332 for (i = 0; i < sizeof(int) * 8; i++) {
333 if ((1 << i) & cpu)
334 CPU_SET(i, &cpumask);
335 }
336#endif
337}
338
339static unsigned long get_mult(char c)
340{
341 switch (c) {
342 case 'k':
343 case 'K':
344 return 1024;
345 case 'm':
346 case 'M':
347 return 1024 * 1024;
348 case 'g':
349 case 'G':
350 return 1024 * 1024 * 1024;
351 default:
352 return 1;
353 }
354}
355
356/*
357 * convert string after '=' into decimal value, noting any size suffix
358 */
359static int str_cnv(char *p, unsigned long long *val)
360{
361 char *str;
362 int len;
363
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100364 str = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100365 if (!str)
366 return 1;
367
368 str++;
369 len = strlen(str);
370
371 *val = strtoul(str, NULL, 10);
372 if (*val == ULONG_MAX && errno == ERANGE)
373 return 1;
374
Jens Axboe9d0d4242006-05-30 13:45:51 +0200375 *val *= get_mult(str[len - 1]);
Jens Axboeebac4652005-12-08 15:25:21 +0100376 return 0;
377}
378
379static int check_strcnv(char *p, char *name, unsigned long long *val)
380{
Jens Axboe20dc95c2005-12-09 10:29:35 +0100381 if (strncmp(p, name, strlen(name) - 1))
Jens Axboeebac4652005-12-08 15:25:21 +0100382 return 1;
383
384 return str_cnv(p, val);
385}
386
387static void strip_blank_front(char **p)
388{
389 char *s = *p;
390
Jens Axboe4ae3f762006-05-30 13:35:14 +0200391 while (isspace(*s))
Jens Axboeebac4652005-12-08 15:25:21 +0100392 s++;
393}
394
395static void strip_blank_end(char *p)
396{
Jens Axboe4ae3f762006-05-30 13:35:14 +0200397 char *s = p + strlen(p) - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100398
Jens Axboe4ae3f762006-05-30 13:35:14 +0200399 while (isspace(*s) || iscntrl(*s))
400 s--;
Jens Axboeaea47d42006-05-26 19:27:29 +0200401
Jens Axboe4ae3f762006-05-30 13:35:14 +0200402 *(s + 1) = '\0';
Jens Axboeaea47d42006-05-26 19:27:29 +0200403}
404
Jens Axboeebac4652005-12-08 15:25:21 +0100405typedef int (str_cb_fn)(struct thread_data *, char *);
406
407static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
408{
Jens Axboe843a7412006-06-01 21:14:21 -0700409 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100410
Jens Axboe843a7412006-06-01 21:14:21 -0700411 if (strncmp(p, name, strlen(name)))
412 return 1;
413
414 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100415 if (!s)
416 return 1;
417
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100418 s = strchr(s, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100419 if (!s)
420 return 1;
421
422 s++;
423 strip_blank_front(&s);
424 return cb(td, s);
425}
426
427static int check_strstore(char *p, char *name, char *dest)
428{
Jens Axboe843a7412006-06-01 21:14:21 -0700429 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100430
Jens Axboe843a7412006-06-01 21:14:21 -0700431 if (strncmp(p, name, strlen(name)))
432 return 1;
433
434 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100435 if (!s)
436 return 1;
437
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100438 s = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100439 if (!s)
440 return 1;
441
442 s++;
443 strip_blank_front(&s);
444
445 strcpy(dest, s);
Jens Axboeebac4652005-12-08 15:25:21 +0100446 return 0;
447}
448
Jens Axboe01617be2005-12-08 19:50:40 +0100449static int __check_range(char *str, unsigned long *val)
Jens Axboeebac4652005-12-08 15:25:21 +0100450{
Jens Axboe01617be2005-12-08 19:50:40 +0100451 char suffix;
Jens Axboeebac4652005-12-08 15:25:21 +0100452
Jens Axboe01617be2005-12-08 19:50:40 +0100453 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
454 *val *= get_mult(suffix);
Jens Axboeebac4652005-12-08 15:25:21 +0100455 return 0;
456 }
457
Jens Axboe01617be2005-12-08 19:50:40 +0100458 if (sscanf(str, "%lu", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100459 return 0;
460
461 return 1;
Jens Axboe01617be2005-12-08 19:50:40 +0100462}
Jens Axboeebac4652005-12-08 15:25:21 +0100463
Jens Axboe01617be2005-12-08 19:50:40 +0100464static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
465{
466 char option[128];
467 char *str, *p1, *p2;
468
Jens Axboe843a7412006-06-01 21:14:21 -0700469 if (strncmp(p, name, strlen(name)))
470 return 1;
471
Jens Axboe01617be2005-12-08 19:50:40 +0100472 strcpy(option, p);
473 p = option;
474
475 str = strstr(p, name);
476 if (!str)
477 return 1;
478
479 p += strlen(name);
480
481 str = strchr(p, '=');
482 if (!str)
483 return 1;
484
485 /*
486 * 'p' now holds whatever is after the '=' sign
487 */
488 p1 = str + 1;
489
490 /*
491 * terminate p1 at the '-' sign
492 */
493 p = strchr(p1, '-');
494 if (!p)
495 return 1;
496
497 p2 = p + 1;
498 *p = '\0';
499
500 if (!__check_range(p1, s) && !__check_range(p2, e))
501 return 0;
502
503 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100504}
505
506static int check_int(char *p, char *name, unsigned int *val)
507{
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100508 char *str;
Jens Axboeebac4652005-12-08 15:25:21 +0100509
Jens Axboeb6754f92006-05-30 14:29:32 +0200510 if (strncmp(p, name, strlen(name)))
511 return 1;
512
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100513 str = strstr(p, name);
514 if (!str)
515 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100516
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100517 str = strchr(p, '=');
518 if (!str)
519 return 1;
520
521 str++;
522
523 if (sscanf(str, "%u", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100524 return 0;
525
526 return 1;
527}
528
529static int check_strset(char *p, char *name)
530{
531 return strncmp(p, name, strlen(name));
532}
533
534static int is_empty_or_comment(char *line)
535{
536 unsigned int i;
537
538 for (i = 0; i < strlen(line); i++) {
539 if (line[i] == ';')
540 return 1;
541 if (!isspace(line[i]) && !iscntrl(line[i]))
542 return 0;
543 }
544
545 return 1;
546}
547
548static int str_rw_cb(struct thread_data *td, char *mem)
549{
550 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
551 td->ddir = DDIR_READ;
552 td->sequential = 1;
553 return 0;
554 } else if (!strncmp(mem, "randread", 8)) {
555 td->ddir = DDIR_READ;
556 td->sequential = 0;
557 return 0;
558 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
559 td->ddir = DDIR_WRITE;
560 td->sequential = 1;
561 return 0;
562 } else if (!strncmp(mem, "randwrite", 9)) {
563 td->ddir = DDIR_WRITE;
564 td->sequential = 0;
565 return 0;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200566 } else if (!strncmp(mem, "rw", 2)) {
567 td->ddir = 0;
568 td->iomix = 1;
569 td->sequential = 1;
570 return 0;
571 } else if (!strncmp(mem, "randrw", 6)) {
572 td->ddir = 0;
573 td->iomix = 1;
574 td->sequential = 0;
575 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100576 }
577
578 fprintf(stderr, "bad data direction: %s\n", mem);
579 return 1;
580}
581
582static int str_verify_cb(struct thread_data *td, char *mem)
583{
584 if (!strncmp(mem, "0", 1)) {
585 td->verify = VERIFY_NONE;
586 return 0;
587 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
588 td->verify = VERIFY_MD5;
589 return 0;
590 } else if (!strncmp(mem, "crc32", 5)) {
591 td->verify = VERIFY_CRC32;
592 return 0;
593 }
594
595 fprintf(stderr, "bad verify type: %s\n", mem);
596 return 1;
597}
598
599static int str_mem_cb(struct thread_data *td, char *mem)
600{
601 if (!strncmp(mem, "malloc", 6)) {
602 td->mem_type = MEM_MALLOC;
603 return 0;
604 } else if (!strncmp(mem, "shm", 3)) {
605 td->mem_type = MEM_SHM;
606 return 0;
607 } else if (!strncmp(mem, "mmap", 4)) {
608 td->mem_type = MEM_MMAP;
609 return 0;
610 }
611
612 fprintf(stderr, "bad mem type: %s\n", mem);
613 return 1;
614}
615
616static int str_ioengine_cb(struct thread_data *td, char *str)
617{
618 if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
619 !strncmp(str, "libaio", 6)) {
620 strcpy(td->io_engine_name, "libaio");
621 td->io_engine = FIO_LIBAIO;
622 return 0;
623 } else if (!strncmp(str, "posixaio", 8)) {
624 strcpy(td->io_engine_name, "posixaio");
625 td->io_engine = FIO_POSIXAIO;
626 return 0;
627 } else if (!strncmp(str, "sync", 4)) {
628 strcpy(td->io_engine_name, "sync");
629 td->io_engine = FIO_SYNCIO;
630 return 0;
631 } else if (!strncmp(str, "mmap", 4)) {
632 strcpy(td->io_engine_name, "mmap");
633 td->io_engine = FIO_MMAPIO;
634 return 0;
635 } else if (!strncmp(str, "sgio", 4)) {
636 strcpy(td->io_engine_name, "sgio");
637 td->io_engine = FIO_SGIO;
638 return 0;
Jens Axboe8756e4d2006-05-27 20:24:53 +0200639 } else if (!strncmp(str, "splice", 6)) {
640 strcpy(td->io_engine_name, "splice");
641 td->io_engine = FIO_SPLICEIO;
642 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100643 }
644
645 fprintf(stderr, "bad ioengine type: %s\n", str);
646 return 1;
647}
648
Jens Axboeaea47d42006-05-26 19:27:29 +0200649static int str_iolog_cb(struct thread_data *td, char *file)
650{
Jens Axboeef899b62006-06-06 09:31:00 +0200651 td->iolog_file = strdup(file);
Jens Axboeaea47d42006-05-26 19:27:29 +0200652 return 0;
653}
Jens Axboeebac4652005-12-08 15:25:21 +0100654
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200655static int str_prerun_cb(struct thread_data *td, char *file)
656{
657 td->exec_prerun = strdup(file);
658 return 0;
659}
660
661static int str_postrun_cb(struct thread_data *td, char *file)
662{
663 td->exec_postrun = strdup(file);
664 return 0;
665}
666
Jens Axboeda867742006-06-06 10:39:10 -0700667static int str_iosched_cb(struct thread_data *td, char *file)
668{
669 td->ioscheduler = strdup(file);
670 return 0;
671}
672
Jens Axboeebac4652005-12-08 15:25:21 +0100673int parse_jobs_ini(char *file)
674{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200675 unsigned int prioclass, prio, cpu, global, il;
Jens Axboeebac4652005-12-08 15:25:21 +0100676 unsigned long long ull;
677 unsigned long ul1, ul2;
678 struct thread_data *td;
Jens Axboeef899b62006-06-06 09:31:00 +0200679 char *string, *name, *tmpbuf;
Jens Axboeebac4652005-12-08 15:25:21 +0100680 fpos_t off;
681 FILE *f;
682 char *p;
683
684 f = fopen(file, "r");
685 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200686 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100687 return 1;
688 }
689
690 string = malloc(4096);
691 name = malloc(256);
Jens Axboeef899b62006-06-06 09:31:00 +0200692 tmpbuf = malloc(4096);
Jens Axboeebac4652005-12-08 15:25:21 +0100693
694 while ((p = fgets(string, 4096, f)) != NULL) {
695 if (is_empty_or_comment(p))
696 continue;
697 if (sscanf(p, "[%s]", name) != 1)
698 continue;
699
700 global = !strncmp(name, "global", 6);
701
702 name[strlen(name) - 1] = '\0';
703
704 td = get_new_job(global, &def_thread);
705 if (!td)
706 return 1;
707
Jens Axboeebac4652005-12-08 15:25:21 +0100708 fgetpos(f, &off);
709 while ((p = fgets(string, 4096, f)) != NULL) {
710 if (is_empty_or_comment(p))
711 continue;
712 if (strstr(p, "["))
713 break;
Jens Axboeb6754f92006-05-30 14:29:32 +0200714 strip_blank_front(&p);
Jens Axboe4ae3f762006-05-30 13:35:14 +0200715 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200716
Jens Axboeebac4652005-12-08 15:25:21 +0100717 if (!check_int(p, "prio", &prio)) {
718#ifndef FIO_HAVE_IOPRIO
719 fprintf(stderr, "io priorities not available\n");
720 return 1;
721#endif
Jens Axboe8756e4d2006-05-27 20:24:53 +0200722 td->ioprio |= prio;
Jens Axboeebac4652005-12-08 15:25:21 +0100723 fgetpos(f, &off);
724 continue;
725 }
726 if (!check_int(p, "prioclass", &prioclass)) {
727#ifndef FIO_HAVE_IOPRIO
728 fprintf(stderr, "io priorities not available\n");
729 return 1;
730#endif
Jens Axboe8756e4d2006-05-27 20:24:53 +0200731 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
Jens Axboeebac4652005-12-08 15:25:21 +0100732 fgetpos(f, &off);
733 continue;
734 }
735 if (!check_int(p, "direct", &td->odirect)) {
736 fgetpos(f, &off);
737 continue;
738 }
739 if (!check_int(p, "rate", &td->rate)) {
740 fgetpos(f, &off);
741 continue;
742 }
743 if (!check_int(p, "ratemin", &td->ratemin)) {
744 fgetpos(f, &off);
745 continue;
746 }
747 if (!check_int(p, "ratecycle", &td->ratecycle)) {
748 fgetpos(f, &off);
749 continue;
750 }
751 if (!check_int(p, "thinktime", &td->thinktime)) {
752 fgetpos(f, &off);
753 continue;
754 }
755 if (!check_int(p, "cpumask", &cpu)) {
756#ifndef FIO_HAVE_CPU_AFFINITY
757 fprintf(stderr, "cpu affinity not available\n");
758 return 1;
759#endif
760 fill_cpu_mask(td->cpumask, cpu);
761 fgetpos(f, &off);
762 continue;
763 }
764 if (!check_int(p, "fsync", &td->fsync_blocks)) {
765 fgetpos(f, &off);
Jens Axboefc1a4712006-05-30 13:04:05 +0200766 td->end_fsync = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100767 continue;
768 }
769 if (!check_int(p, "startdelay", &td->start_delay)) {
770 fgetpos(f, &off);
771 continue;
772 }
773 if (!check_int(p, "timeout", &td->timeout)) {
774 fgetpos(f, &off);
775 continue;
776 }
777 if (!check_int(p, "invalidate",&td->invalidate_cache)) {
778 fgetpos(f, &off);
779 continue;
780 }
781 if (!check_int(p, "iodepth", &td->iodepth)) {
782 fgetpos(f, &off);
783 continue;
784 }
785 if (!check_int(p, "sync", &td->sync_io)) {
786 fgetpos(f, &off);
787 continue;
788 }
789 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
790 fgetpos(f, &off);
791 continue;
792 }
793 if (!check_int(p, "create_serialize", &td->create_serialize)) {
794 fgetpos(f, &off);
795 continue;
796 }
797 if (!check_int(p, "create_fsync", &td->create_fsync)) {
798 fgetpos(f, &off);
799 continue;
800 }
Jens Axboefc1a4712006-05-30 13:04:05 +0200801 if (!check_int(p, "end_fsync", &td->end_fsync)) {
802 fgetpos(f, &off);
803 continue;
804 }
Jens Axboeebac4652005-12-08 15:25:21 +0100805 if (!check_int(p, "loops", &td->loops)) {
806 fgetpos(f, &off);
807 continue;
808 }
809 if (!check_int(p, "numjobs", &td->numjobs)) {
810 fgetpos(f, &off);
811 continue;
812 }
813 if (!check_int(p, "overwrite", &td->overwrite)) {
814 fgetpos(f, &off);
815 continue;
816 }
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200817 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
818 fgetpos(f, &off);
819 continue;
820 }
821 if (!check_int(p, "rwmixread", &il)) {
822 if (il > 100)
823 il = 100;
824 td->rwmixread = il;
825 fgetpos(f, &off);
826 continue;
827 }
828 if (!check_int(p, "rwmixwrite", &il)) {
829 if (il > 100)
830 il = 100;
831 td->rwmixread = 100 - il;
832 fgetpos(f, &off);
833 continue;
834 }
Jens Axboeb6f4d882006-06-02 10:32:51 +0200835 if (!check_int(p, "nice", &td->nice)) {
836 fgetpos(f, &off);
837 continue;
838 }
Jens Axboeebac4652005-12-08 15:25:21 +0100839 if (!check_range(p, "bsrange", &ul1, &ul2)) {
840 if (ul1 > ul2) {
841 td->max_bs = ul1;
842 td->min_bs = ul2;
843 } else {
844 td->max_bs = ul2;
845 td->min_bs = ul1;
846 }
847 fgetpos(f, &off);
848 continue;
849 }
850 if (!check_strcnv(p, "bs", &ull)) {
851 td->bs = ull;
852 fgetpos(f, &off);
853 continue;
854 }
855 if (!check_strcnv(p, "size", &td->file_size)) {
856 fgetpos(f, &off);
857 continue;
858 }
859 if (!check_strcnv(p, "offset", &td->file_offset)) {
860 fgetpos(f, &off);
861 continue;
862 }
Jens Axboe20dc95c2005-12-09 10:29:35 +0100863 if (!check_strcnv(p, "zonesize", &td->zone_size)) {
864 fgetpos(f, &off);
865 continue;
866 }
867 if (!check_strcnv(p, "zoneskip", &td->zone_skip)) {
868 fgetpos(f, &off);
869 continue;
870 }
Jens Axboec04f7ec2006-05-31 10:13:16 +0200871 if (!check_strcnv(p, "lockmem", &mlock_size)) {
872 fgetpos(f, &off);
873 continue;
874 }
Jens Axboeef899b62006-06-06 09:31:00 +0200875 if (!check_strstore(p, "directory", tmpbuf)) {
876 td->directory = strdup(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100877 fgetpos(f, &off);
878 continue;
879 }
880 if (!check_str(p, "mem", str_mem_cb, td)) {
881 fgetpos(f, &off);
882 continue;
883 }
884 if (!check_str(p, "verify", str_verify_cb, td)) {
885 fgetpos(f, &off);
886 continue;
887 }
888 if (!check_str(p, "rw", str_rw_cb, td)) {
889 fgetpos(f, &off);
890 continue;
891 }
892 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
893 fgetpos(f, &off);
894 continue;
895 }
896 if (!check_strset(p, "create")) {
897 td->create_file = 1;
898 fgetpos(f, &off);
899 continue;
900 }
901 if (!check_strset(p, "exitall")) {
902 exitall_on_terminate = 1;
903 fgetpos(f, &off);
904 continue;
905 }
906 if (!check_strset(p, "stonewall")) {
907 td->stonewall = 1;
908 fgetpos(f, &off);
909 continue;
910 }
911 if (!check_strset(p, "thread")) {
912 td->use_thread = 1;
913 fgetpos(f, &off);
914 continue;
915 }
Jens Axboeaea47d42006-05-26 19:27:29 +0200916 if (!check_str(p, "iolog", str_iolog_cb, td)) {
Jens Axboe843a7412006-06-01 21:14:21 -0700917 td->read_iolog = 1;
918 td->write_iolog = 0;
919 fgetpos(f, &off);
920 continue;
921 }
922 if (!td->read_iolog &&
923 !check_str(p, "write_iolog", str_iolog_cb, td)) {
Jens Axboe843a7412006-06-01 21:14:21 -0700924 td->write_iolog = 1;
Jens Axboeaea47d42006-05-26 19:27:29 +0200925 fgetpos(f, &off);
926 continue;
927 }
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200928 if (!check_str(p, "exec_prerun", str_prerun_cb, td)) {
929 fgetpos(f, &off);
930 continue;
931 }
932 if (!check_str(p, "exec_postrun", str_postrun_cb, td)) {
933 fgetpos(f, &off);
934 continue;
935 }
Jens Axboeda867742006-06-06 10:39:10 -0700936 if (!check_str(p, "ioscheduler", str_iosched_cb, td)) {
937 fgetpos(f, &off);
938 continue;
939 }
Jens Axboeebac4652005-12-08 15:25:21 +0100940
941 printf("Client%d: bad option %s\n",td->thread_number,p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200942 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100943 }
944 fsetpos(f, &off);
945
Jens Axboe75154842006-06-01 13:56:09 +0200946 if (add_job(td, name, 0))
Jens Axboeebac4652005-12-08 15:25:21 +0100947 return 1;
948 }
949
950 free(string);
951 free(name);
Jens Axboeef899b62006-06-06 09:31:00 +0200952 free(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100953 fclose(f);
954 return 0;
955}
956
957static int fill_def_thread(void)
958{
959 memset(&def_thread, 0, sizeof(def_thread));
960
961 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
962 perror("sched_getaffinity");
963 return 1;
964 }
965
966 /*
967 * fill globals
968 */
969 def_thread.ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200970 def_thread.iomix = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100971 def_thread.bs = DEF_BS;
972 def_thread.min_bs = -1;
973 def_thread.max_bs = -1;
974 def_thread.io_engine = DEF_IO_ENGINE;
975 strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
976 def_thread.odirect = DEF_ODIRECT;
977 def_thread.ratecycle = DEF_RATE_CYCLE;
978 def_thread.sequential = DEF_SEQUENTIAL;
979 def_thread.timeout = DEF_TIMEOUT;
980 def_thread.create_file = DEF_CREATE;
981 def_thread.overwrite = DEF_OVERWRITE;
982 def_thread.invalidate_cache = DEF_INVALIDATE;
983 def_thread.sync_io = DEF_SYNCIO;
984 def_thread.mem_type = MEM_MALLOC;
985 def_thread.bw_avg_time = DEF_BWAVGTIME;
986 def_thread.create_serialize = DEF_CREATE_SER;
987 def_thread.create_fsync = DEF_CREATE_FSYNC;
988 def_thread.loops = DEF_LOOPS;
989 def_thread.verify = DEF_VERIFY;
990 def_thread.stonewall = DEF_STONEWALL;
991 def_thread.numjobs = DEF_NUMJOBS;
992 def_thread.use_thread = DEF_USE_THREAD;
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200993 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
994 def_thread.rwmixread = DEF_RWMIX_READ;
Jens Axboeb6f4d882006-06-02 10:32:51 +0200995 def_thread.nice = DEF_NICE;
Jens Axboeebac4652005-12-08 15:25:21 +0100996#ifdef FIO_HAVE_DISK_UTIL
997 def_thread.do_disk_util = 1;
998#endif
999
1000 return 0;
1001}
1002
Jens Axboe4785f992006-05-26 03:59:10 +02001003static void usage(char *name)
1004{
1005 printf("%s\n", fio_version_string);
1006 printf("\t-s IO is sequential\n");
1007 printf("\t-b Block size in KiB for each IO\n");
1008 printf("\t-t Runtime in seconds\n");
1009 printf("\t-R Exit all threads on failure to meet rate goal\n");
1010 printf("\t-o Use O_DIRECT\n");
1011 printf("\t-l Generate per-job latency logs\n");
1012 printf("\t-w Generate per-job bandwidth logs\n");
1013 printf("\t-f Job file (Required)\n");
1014 printf("\t-v Print version info and exit\n");
1015}
1016
Jens Axboeebac4652005-12-08 15:25:21 +01001017static void parse_cmd_line(int argc, char *argv[])
1018{
1019 int c;
1020
Jens Axboe4785f992006-05-26 03:59:10 +02001021 while ((c = getopt(argc, argv, "s:b:t:r:R:o:f:lwvh")) != EOF) {
Jens Axboeebac4652005-12-08 15:25:21 +01001022 switch (c) {
1023 case 's':
1024 def_thread.sequential = !!atoi(optarg);
1025 break;
1026 case 'b':
1027 def_thread.bs = atoi(optarg);
1028 def_thread.bs <<= 10;
1029 if (!def_thread.bs) {
1030 printf("bad block size\n");
1031 def_thread.bs = DEF_BS;
1032 }
1033 break;
1034 case 't':
1035 def_thread.timeout = atoi(optarg);
1036 break;
1037 case 'r':
1038 repeatable = !!atoi(optarg);
1039 break;
1040 case 'R':
1041 rate_quit = !!atoi(optarg);
1042 break;
1043 case 'o':
1044 def_thread.odirect = !!atoi(optarg);
1045 break;
1046 case 'f':
1047 ini_file = strdup(optarg);
1048 break;
1049 case 'l':
1050 write_lat_log = 1;
1051 break;
1052 case 'w':
1053 write_bw_log = 1;
1054 break;
Jens Axboe4785f992006-05-26 03:59:10 +02001055 case 'h':
1056 usage(argv[0]);
1057 exit(0);
Jens Axboeebac4652005-12-08 15:25:21 +01001058 case 'v':
1059 printf("%s\n", fio_version_string);
1060 exit(0);
1061 }
1062 }
Jens Axboec9fad892006-05-27 17:26:50 +02001063
1064 if (!ini_file && argc > 1 && argv[argc - 1][0] != '-')
1065 ini_file = strdup(argv[argc - 1]);
Jens Axboeebac4652005-12-08 15:25:21 +01001066}
1067
1068static void free_shm(void)
1069{
1070 struct shmid_ds sbuf;
1071
1072 if (threads) {
1073 shmdt(threads);
1074 threads = NULL;
1075 shmctl(shm_id, IPC_RMID, &sbuf);
1076 }
1077}
1078
1079static int setup_thread_area(void)
1080{
1081 /*
1082 * 1024 is too much on some machines, scale max_jobs if
1083 * we get a failure that looks like too large a shm segment
1084 */
1085 do {
1086 int s = max_jobs * sizeof(struct thread_data);
1087
1088 shm_id = shmget(0, s, IPC_CREAT | 0600);
1089 if (shm_id != -1)
1090 break;
1091 if (errno != EINVAL) {
1092 perror("shmget");
1093 break;
1094 }
1095
1096 max_jobs >>= 1;
1097 } while (max_jobs);
1098
1099 if (shm_id == -1)
1100 return 1;
1101
1102 threads = shmat(shm_id, NULL, 0);
1103 if (threads == (void *) -1) {
1104 perror("shmat");
1105 return 1;
1106 }
1107
1108 atexit(free_shm);
1109 return 0;
1110}
1111
1112int parse_options(int argc, char *argv[])
1113{
1114 if (setup_thread_area())
1115 return 1;
1116 if (fill_def_thread())
1117 return 1;
1118
1119 parse_cmd_line(argc, argv);
1120
1121 if (!ini_file) {
1122 printf("Need job file\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001123 usage(argv[0]);
Jens Axboeebac4652005-12-08 15:25:21 +01001124 return 1;
1125 }
1126
Jens Axboec9fad892006-05-27 17:26:50 +02001127 if (parse_jobs_ini(ini_file)) {
1128 usage(argv[0]);
Jens Axboeebac4652005-12-08 15:25:21 +01001129 return 1;
Jens Axboec9fad892006-05-27 17:26:50 +02001130 }
Jens Axboeebac4652005-12-08 15:25:21 +01001131
1132 return 0;
1133}