blob: f27fdf1f03f5bfeb9a85aa690670d1cd2e2d2fda [file] [log] [blame]
Jens Axboe906c8d72006-06-13 09:37:56 +02001/*
Jens Axboecb2c86f2006-10-26 13:48:34 +02002 * This file contains job initialization and setup functions.
Jens Axboe906c8d72006-06-13 09:37:56 +02003 */
Jens Axboeebac4652005-12-08 15:25:21 +01004#include <stdio.h>
5#include <stdlib.h>
6#include <unistd.h>
7#include <fcntl.h>
8#include <ctype.h>
9#include <string.h>
10#include <errno.h>
11#include <sys/ipc.h>
12#include <sys/shm.h>
13#include <sys/types.h>
14#include <sys/stat.h>
15
16#include "fio.h"
Jens Axboecb2c86f2006-10-26 13:48:34 +020017#include "parse.h"
Jens Axboeebac4652005-12-08 15:25:21 +010018
Jens Axboe906c8d72006-06-13 09:37:56 +020019/*
20 * The default options
21 */
Jens Axboe20dc95c2005-12-09 10:29:35 +010022#define DEF_BS (4096)
23#define DEF_TIMEOUT (0)
24#define DEF_RATE_CYCLE (1000)
25#define DEF_ODIRECT (1)
26#define DEF_IO_ENGINE (FIO_SYNCIO)
Jens Axboeebac4652005-12-08 15:25:21 +010027#define DEF_IO_ENGINE_NAME "sync"
Jens Axboe20dc95c2005-12-09 10:29:35 +010028#define DEF_SEQUENTIAL (1)
29#define DEF_RAND_REPEAT (1)
30#define DEF_OVERWRITE (1)
Jens Axboe20dc95c2005-12-09 10:29:35 +010031#define DEF_INVALIDATE (1)
32#define DEF_SYNCIO (0)
33#define DEF_RANDSEED (0xb1899bedUL)
34#define DEF_BWAVGTIME (500)
35#define DEF_CREATE_SER (1)
Jens Axboeebac4652005-12-08 15:25:21 +010036#define DEF_CREATE_FSYNC (1)
Jens Axboe20dc95c2005-12-09 10:29:35 +010037#define DEF_LOOPS (1)
38#define DEF_VERIFY (0)
39#define DEF_STONEWALL (0)
40#define DEF_NUMJOBS (1)
41#define DEF_USE_THREAD (0)
42#define DEF_FILE_SIZE (1024 * 1024 * 1024UL)
43#define DEF_ZONE_SIZE (0)
44#define DEF_ZONE_SKIP (0)
Jens Axboea6ccc7b2006-06-02 10:14:15 +020045#define DEF_RWMIX_CYCLE (500)
46#define DEF_RWMIX_READ (50)
Jens Axboeb6f4d882006-06-02 10:32:51 +020047#define DEF_NICE (0)
Jens Axboe53cdc682006-10-18 11:50:58 +020048#define DEF_NR_FILES (1)
Jens Axboef6cbb262006-10-18 14:16:42 +020049#define DEF_UNLINK (0)
Jens Axboeec94ec52006-10-20 10:59:19 +020050#define DEF_WRITE_BW_LOG (0)
51#define DEF_WRITE_LAT_LOG (0)
Jens Axboeebac4652005-12-08 15:25:21 +010052
Jens Axboee1f36502006-10-27 10:54:08 +020053#define td_var_offset(var) ((size_t) &((struct thread_data *)0)->var)
54
55static int str_rw_cb(void *, char *);
56static int str_ioengine_cb(void *, char *);
57static int str_mem_cb(void *, char *);
58static int str_verify_cb(void *, char *);
59static int str_lockmem_cb(void *, unsigned long *);
60static int str_prio_cb(void *, unsigned int *);
61static int str_prioclass_cb(void *, unsigned int *);
62static int str_exitall_cb(void);
63static int str_cpumask_cb(void *, unsigned int *);
64
65/*
66 * Map of job/command line options
67 */
68static struct fio_option options[] = {
69 {
70 .name = "name",
71 .type = FIO_OPT_STR_STORE,
72 .off1 = td_var_offset(name),
73 },
74 {
75 .name = "directory",
76 .type = FIO_OPT_STR_STORE,
77 .off1 = td_var_offset(directory),
78 },
79 {
80 .name = "filename",
81 .type = FIO_OPT_STR_STORE,
82 .off1 = td_var_offset(filename),
83 },
84 {
85 .name = "rw",
86 .type = FIO_OPT_STR,
87 .cb = str_rw_cb,
88 },
89 {
90 .name = "ioengine",
91 .type = FIO_OPT_STR,
92 .cb = str_ioengine_cb,
93 },
94 {
95 .name = "mem",
96 .type = FIO_OPT_STR,
97 .cb = str_mem_cb,
98 },
99 {
100 .name = "verify",
101 .type = FIO_OPT_STR,
102 .cb = str_verify_cb,
103 },
104 {
105 .name = "write_iolog",
Jens Axboe076efc72006-10-27 11:24:25 +0200106 .type = FIO_OPT_STR_STORE,
107 .off1 = td_var_offset(write_iolog_file),
Jens Axboee1f36502006-10-27 10:54:08 +0200108 },
109 {
Jens Axboe076efc72006-10-27 11:24:25 +0200110 .name = "read_iolog",
Jens Axboee1f36502006-10-27 10:54:08 +0200111 .type = FIO_OPT_STR_STORE,
Jens Axboe076efc72006-10-27 11:24:25 +0200112 .off1 = td_var_offset(read_iolog_file),
Jens Axboee1f36502006-10-27 10:54:08 +0200113 },
114 {
115 .name = "exec_prerun",
116 .type = FIO_OPT_STR_STORE,
117 .off1 = td_var_offset(exec_prerun),
118 },
119 {
120 .name = "exec_postrun",
121 .type = FIO_OPT_STR_STORE,
122 .off1 = td_var_offset(exec_postrun),
123 },
124#ifdef FIO_HAVE_IOSCHED_SWITCH
125 {
126 .name = "ioscheduler",
127 .type = FIO_OPT_STR_STORE,
128 .off1 = td_var_offset(ioscheduler),
129 },
130#endif
131 {
132 .name = "size",
133 .type = FIO_OPT_STR_VAL,
134 .off1 = td_var_offset(total_file_size),
135 },
136 {
137 .name = "bs",
138 .type = FIO_OPT_STR_VAL,
139 .off1 = td_var_offset(bs),
140 },
141 {
142 .name = "offset",
143 .type = FIO_OPT_STR_VAL,
144 .off1 = td_var_offset(start_offset),
145 },
146 {
147 .name = "zonesize",
148 .type = FIO_OPT_STR_VAL,
149 .off1 = td_var_offset(zone_size),
150 },
151 {
152 .name = "zoneskip",
153 .type = FIO_OPT_STR_VAL,
154 .off1 = td_var_offset(zone_skip),
155 },
156 {
157 .name = "lockmem",
158 .type = FIO_OPT_STR_VAL,
159 .cb = str_lockmem_cb,
160 },
161 {
162 .name = "bsrange",
163 .type = FIO_OPT_RANGE,
164 .off1 = td_var_offset(min_bs),
165 .off2 = td_var_offset(max_bs),
166 },
167 {
168 .name = "nrfiles",
169 .type = FIO_OPT_INT,
170 .off1 = td_var_offset(nr_files),
171 },
172 {
173 .name = "iodepth",
174 .type = FIO_OPT_INT,
175 .off1 = td_var_offset(iodepth),
176 },
177 {
178 .name = "fsync",
179 .type = FIO_OPT_INT,
180 .off1 = td_var_offset(fsync_blocks),
181 },
182 {
183 .name = "rwmixcycle",
184 .type = FIO_OPT_INT,
185 .off1 = td_var_offset(rwmixcycle),
186 },
187 {
188 .name = "rwmixread",
189 .type = FIO_OPT_INT,
190 .off1 = td_var_offset(rwmixread),
191 .max_val= 100,
192 },
193 {
194 .name = "rwmixwrite",
195 .type = FIO_OPT_INT,
196 .off1 = td_var_offset(rwmixwrite),
197 .max_val= 100,
198 },
199 {
200 .name = "nice",
201 .type = FIO_OPT_INT,
202 .off1 = td_var_offset(nice),
203 },
204#ifdef FIO_HAVE_IOPRIO
205 {
206 .name = "prio",
207 .type = FIO_OPT_INT,
208 .cb = str_prio_cb,
209 },
210 {
211 .name = "prioclass",
212 .type = FIO_OPT_INT,
213 .cb = str_prioclass_cb,
214 },
215#endif
216 {
217 .name = "thinktime",
218 .type = FIO_OPT_INT,
219 .off1 = td_var_offset(thinktime)
220 },
221 {
222 .name = "rate",
223 .type = FIO_OPT_INT,
224 .off1 = td_var_offset(rate)
225 },
226 {
227 .name = "ratemin",
228 .type = FIO_OPT_INT,
229 .off1 = td_var_offset(ratemin)
230 },
231 {
232 .name = "ratecycle",
233 .type = FIO_OPT_INT,
234 .off1 = td_var_offset(ratecycle)
235 },
236 {
237 .name = "startdelay",
238 .type = FIO_OPT_INT,
239 .off1 = td_var_offset(start_delay)
240 },
241 {
242 .name = "timeout",
243 .type = FIO_OPT_STR_VAL_TIME,
244 .off1 = td_var_offset(timeout)
245 },
246 {
247 .name = "invalidate",
248 .type = FIO_OPT_INT,
249 .off1 = td_var_offset(invalidate_cache)
250 },
251 {
252 .name = "sync",
253 .type = FIO_OPT_INT,
254 .off1 = td_var_offset(sync_io)
255 },
256 {
257 .name = "bwavgtime",
258 .type = FIO_OPT_INT,
259 .off1 = td_var_offset(bw_avg_time)
260 },
261 {
262 .name = "create_serialize",
263 .type = FIO_OPT_INT,
264 .off1 = td_var_offset(create_serialize)
265 },
266 {
267 .name = "create_fsync",
268 .type = FIO_OPT_INT,
269 .off1 = td_var_offset(create_fsync)
270 },
271 {
272 .name = "loops",
273 .type = FIO_OPT_INT,
274 .off1 = td_var_offset(loops)
275 },
276 {
277 .name = "numjobs",
278 .type = FIO_OPT_INT,
279 .off1 = td_var_offset(numjobs)
280 },
281 {
282 .name = "cpuload",
283 .type = FIO_OPT_INT,
284 .off1 = td_var_offset(cpuload)
285 },
286 {
287 .name = "cpuchunks",
288 .type = FIO_OPT_INT,
289 .off1 = td_var_offset(cpucycle)
290 },
291 {
292 .name = "direct",
293 .type = FIO_OPT_INT,
294 .off1 = td_var_offset(odirect)
295 },
296 {
297 .name = "overwrite",
298 .type = FIO_OPT_INT,
299 .off1 = td_var_offset(overwrite)
300 },
301#ifdef FIO_HAVE_CPU_AFFINITY
302 {
303 .name = "cpumask",
304 .type = FIO_OPT_INT,
305 .cb = str_cpumask_cb,
306 },
307#endif
308 {
309 .name = "end_fsync",
310 .type = FIO_OPT_INT,
311 .off1 = td_var_offset(end_fsync)
312 },
313 {
314 .name = "unlink",
315 .type = FIO_OPT_STR_SET,
316 .off1 = td_var_offset(unlink),
317 },
318 {
319 .name = "exitall",
320 .type = FIO_OPT_STR_SET,
321 .cb = str_exitall_cb,
322 },
323 {
324 .name = "stonewall",
325 .type = FIO_OPT_STR_SET,
326 .off1 = td_var_offset(stonewall),
327 },
328 {
329 .name = "thread",
330 .type = FIO_OPT_STR_SET,
331 .off1 = td_var_offset(thread),
332 },
333 {
334 .name = "write_bw_log",
335 .type = FIO_OPT_STR_SET,
336 .off1 = td_var_offset(write_bw_log),
337 },
338 {
339 .name = "write_lat_log",
340 .type = FIO_OPT_STR_SET,
341 .off1 = td_var_offset(write_lat_log),
342 },
343 {
344 .name = NULL,
345 },
346};
347
Jens Axboe972cfd22006-06-09 11:08:56 +0200348static int def_timeout = DEF_TIMEOUT;
Jens Axboe972cfd22006-06-09 11:08:56 +0200349
Jens Axboef4866ec2006-06-13 09:38:36 +0200350static char fio_version_string[] = "fio 1.5";
Jens Axboeebac4652005-12-08 15:25:21 +0100351
Jens Axboe972cfd22006-06-09 11:08:56 +0200352static char **ini_file;
Jens Axboeebac4652005-12-08 15:25:21 +0100353static int max_jobs = MAX_JOBS;
354
355struct thread_data def_thread;
356struct thread_data *threads = NULL;
357
358int rate_quit = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100359int exitall_on_terminate = 0;
Jens Axboec6ae0a52006-06-12 11:04:44 +0200360int terse_output = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +0200361unsigned long long mlock_size = 0;
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200362FILE *f_out = NULL;
363FILE *f_err = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +0100364
Jens Axboeec94ec52006-10-20 10:59:19 +0200365static int write_lat_log = DEF_WRITE_LAT_LOG;
366static int write_bw_log = DEF_WRITE_BW_LOG;
367
Jens Axboe906c8d72006-06-13 09:37:56 +0200368/*
369 * Return a free job structure.
370 */
Jens Axboeebac4652005-12-08 15:25:21 +0100371static struct thread_data *get_new_job(int global, struct thread_data *parent)
372{
373 struct thread_data *td;
374
375 if (global)
376 return &def_thread;
377 if (thread_number >= max_jobs)
378 return NULL;
379
380 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200381 *td = *parent;
382 td->name[0] = '\0';
Jens Axboeebac4652005-12-08 15:25:21 +0100383
Jens Axboeebac4652005-12-08 15:25:21 +0100384 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100385 return td;
386}
387
388static void put_job(struct thread_data *td)
389{
390 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
391 thread_number--;
392}
393
Jens Axboedad915e2006-10-27 11:10:18 +0200394/*
395 * Lazy way of fixing up options that depend on each other. We could also
396 * define option callback handlers, but this is easier.
397 */
Jens Axboee1f36502006-10-27 10:54:08 +0200398static void fixup_options(struct thread_data *td)
399{
400 if (!td->min_bs)
401 td->min_bs = td->bs;
402 if (!td->max_bs)
403 td->max_bs = td->bs;
404
405 if (!td->rwmixread && td->rwmixwrite)
406 td->rwmixread = 100 - td->rwmixwrite;
Jens Axboedad915e2006-10-27 11:10:18 +0200407
Jens Axboe076efc72006-10-27 11:24:25 +0200408 if (td->write_iolog_file && td->read_iolog_file) {
409 log_err("fio: read iolog overrides write_iolog\n");
410 free(td->write_iolog_file);
411 td->write_iolog_file = NULL;
412 }
Jens Axboee1f36502006-10-27 10:54:08 +0200413}
414
Jens Axboe906c8d72006-06-13 09:37:56 +0200415/*
416 * Adds a job to the list of things todo. Sanitizes the various options
417 * to make sure we don't have conflicts, and initializes various
418 * members of td.
419 */
Jens Axboe75154842006-06-01 13:56:09 +0200420static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100421{
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200422 char *ddir_str[] = { "read", "write", "randread", "randwrite",
423 "rw", NULL, "randrw" };
Jens Axboeebac4652005-12-08 15:25:21 +0100424 struct stat sb;
Jens Axboe53cdc682006-10-18 11:50:58 +0200425 int numjobs, ddir, i;
426 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100427
428#ifndef FIO_HAVE_LIBAIO
429 if (td->io_engine == FIO_LIBAIO) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200430 log_err("Linux libaio not available\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100431 return 1;
432 }
433#endif
434#ifndef FIO_HAVE_POSIXAIO
435 if (td->io_engine == FIO_POSIXAIO) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200436 log_err("posix aio not available\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100437 return 1;
438 }
439#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100440
Jens Axboee1f36502006-10-27 10:54:08 +0200441 fixup_options(td);
442
Jens Axboeebac4652005-12-08 15:25:21 +0100443 /*
444 * the def_thread is just for options, it's not a real job
445 */
446 if (td == &def_thread)
447 return 0;
448
Jens Axboedf641192006-10-12 07:55:41 +0200449 /*
450 * Set default io engine, if none set
451 */
452 if (!td->io_ops) {
453 td->io_ops = load_ioengine(td, DEF_IO_ENGINE_NAME);
454 if (!td->io_ops) {
455 log_err("default engine %s not there?\n", DEF_IO_ENGINE_NAME);
456 return 1;
457 }
458 }
459
Jens Axboe2866c822006-10-09 15:57:48 +0200460 if (td->io_ops->flags & FIO_SYNCIO)
Jens Axboeebac4652005-12-08 15:25:21 +0100461 td->iodepth = 1;
462 else {
463 if (!td->iodepth)
Jens Axboe841ddd12006-10-20 09:07:46 +0200464 td->iodepth = td->nr_files;
Jens Axboeebac4652005-12-08 15:25:21 +0100465 }
466
Jens Axboe20dc95c2005-12-09 10:29:35 +0100467 /*
Jens Axboe53cdc682006-10-18 11:50:58 +0200468 * only really works for sequential io for now, and with 1 file
Jens Axboe20dc95c2005-12-09 10:29:35 +0100469 */
Jens Axboe53cdc682006-10-18 11:50:58 +0200470 if (td->zone_size && !td->sequential && td->nr_files == 1)
Jens Axboe20dc95c2005-12-09 10:29:35 +0100471 td->zone_size = 0;
472
Jens Axboe9cc935a2006-06-13 09:11:18 +0200473 /*
474 * Reads can do overwrites, we always need to pre-create the file
475 */
476 if (td_read(td) || td_rw(td))
477 td->overwrite = 1;
478
Jens Axboeebac4652005-12-08 15:25:21 +0100479 td->filetype = FIO_TYPE_FILE;
Jens Axboe0af7b542006-02-17 10:10:12 +0100480 if (!stat(jobname, &sb)) {
481 if (S_ISBLK(sb.st_mode))
482 td->filetype = FIO_TYPE_BD;
483 else if (S_ISCHR(sb.st_mode))
484 td->filetype = FIO_TYPE_CHAR;
485 }
Jens Axboeebac4652005-12-08 15:25:21 +0100486
Jens Axboeb2a15192006-10-18 14:10:42 +0200487 if (td->odirect)
488 td->io_ops->flags |= FIO_RAWIO;
489
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200490 if (td->filename)
491 td->nr_uniq_files = 1;
492 else
493 td->nr_uniq_files = td->nr_files;
494
495 if (td->filetype == FIO_TYPE_FILE || td->filename) {
Jens Axboee9c047a2006-06-07 21:13:04 +0200496 char tmp[PATH_MAX];
Jens Axboe53cdc682006-10-18 11:50:58 +0200497 int len = 0;
498 int i;
Jens Axboee9c047a2006-06-07 21:13:04 +0200499
Jens Axboeef899b62006-06-06 09:31:00 +0200500 if (td->directory && td->directory[0] != '\0')
Jens Axboe53cdc682006-10-18 11:50:58 +0200501 sprintf(tmp, "%s/", td->directory);
Jens Axboeebac4652005-12-08 15:25:21 +0100502
Jens Axboe53cdc682006-10-18 11:50:58 +0200503 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
504
505 for_each_file(td, f, i) {
506 memset(f, 0, sizeof(*f));
507 f->fd = -1;
508
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200509 if (td->filename)
510 sprintf(tmp + len, "%s", td->filename);
511 else
512 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
Jens Axboe53cdc682006-10-18 11:50:58 +0200513 f->file_name = strdup(tmp);
514 }
515 } else {
516 td->nr_files = 1;
517 td->files = malloc(sizeof(struct fio_file));
518 f = &td->files[0];
519
520 memset(f, 0, sizeof(*f));
521 f->fd = -1;
522 f->file_name = strdup(jobname);
523 }
524
525 for_each_file(td, f, i) {
526 f->file_size = td->total_file_size / td->nr_files;
527 f->file_offset = td->start_offset;
528 }
529
Jens Axboebbfd6b02006-06-07 19:42:54 +0200530 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100531
532 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
533 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
534 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
535
536 if (td->min_bs == -1U)
537 td->min_bs = td->bs;
538 if (td->max_bs == -1U)
539 td->max_bs = td->bs;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200540 if (td_read(td) && !td_rw(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100541 td->verify = 0;
542
543 if (td->stonewall && td->thread_number > 1)
544 groupid++;
545
546 td->groupid = groupid;
547
548 if (setup_rate(td))
549 goto err;
550
Jens Axboeec94ec52006-10-20 10:59:19 +0200551 if (td->write_lat_log) {
Jens Axboeebac4652005-12-08 15:25:21 +0100552 setup_log(&td->slat_log);
553 setup_log(&td->clat_log);
554 }
Jens Axboeec94ec52006-10-20 10:59:19 +0200555 if (td->write_bw_log)
Jens Axboeebac4652005-12-08 15:25:21 +0100556 setup_log(&td->bw_log);
557
Jens Axboe01452052006-06-07 10:29:47 +0200558 if (td->name[0] == '\0')
559 snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
560
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200561 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
Jens Axboe75154842006-06-01 13:56:09 +0200562
Jens Axboec6ae0a52006-06-12 11:04:44 +0200563 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200564 if (!job_add_num) {
Jens Axboe2866c822006-10-09 15:57:48 +0200565 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200566 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
567 else
Jens Axboe2866c822006-10-09 15:57:48 +0200568 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_ops->name, td->iodepth);
Jens Axboeb990b5c2006-09-14 09:48:22 +0200569 } else if (job_add_num == 1)
Jens Axboec6ae0a52006-06-12 11:04:44 +0200570 fprintf(f_out, "...\n");
571 }
Jens Axboeebac4652005-12-08 15:25:21 +0100572
573 /*
574 * recurse add identical jobs, clear numjobs and stonewall options
575 * as they don't apply to sub-jobs
576 */
577 numjobs = td->numjobs;
578 while (--numjobs) {
579 struct thread_data *td_new = get_new_job(0, td);
580
581 if (!td_new)
582 goto err;
583
584 td_new->numjobs = 1;
585 td_new->stonewall = 0;
Jens Axboe75154842006-06-01 13:56:09 +0200586 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100587
Jens Axboe75154842006-06-01 13:56:09 +0200588 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100589 goto err;
590 }
591 return 0;
592err:
593 put_job(td);
594 return -1;
595}
596
Jens Axboe906c8d72006-06-13 09:37:56 +0200597/*
598 * Initialize the various random states we need (random io, block size ranges,
599 * read/write mix, etc).
600 */
Jens Axboeebac4652005-12-08 15:25:21 +0100601int init_random_state(struct thread_data *td)
602{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200603 unsigned long seeds[4];
Jens Axboe53cdc682006-10-18 11:50:58 +0200604 int fd, num_maps, blocks, i;
Jens Axboe0ab8db82006-10-18 17:16:23 +0200605 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100606
Jens Axboe1ac267b2006-05-31 20:29:00 +0200607 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100608 if (fd == -1) {
609 td_verror(td, errno);
610 return 1;
611 }
612
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200613 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100614 td_verror(td, EIO);
615 close(fd);
616 return 1;
617 }
618
619 close(fd);
620
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200621 os_random_seed(seeds[0], &td->bsrange_state);
622 os_random_seed(seeds[1], &td->verify_state);
623 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100624
625 if (td->sequential)
626 return 0;
627
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200628 if (td->rand_repeatable)
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200629 seeds[3] = DEF_RANDSEED;
Jens Axboeebac4652005-12-08 15:25:21 +0100630
Jens Axboe0ab8db82006-10-18 17:16:23 +0200631 for_each_file(td, f, i) {
Jens Axboe53cdc682006-10-18 11:50:58 +0200632 blocks = (f->file_size + td->min_bs - 1) / td->min_bs;
633 num_maps = blocks / BLOCKS_PER_MAP;
634 f->file_map = malloc(num_maps * sizeof(long));
635 f->num_maps = num_maps;
636 memset(f->file_map, 0, num_maps * sizeof(long));
637 }
Jens Axboeebac4652005-12-08 15:25:21 +0100638
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200639 os_random_seed(seeds[3], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100640 return 0;
641}
642
643static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
644{
645#ifdef FIO_HAVE_CPU_AFFINITY
646 unsigned int i;
647
648 CPU_ZERO(&cpumask);
649
650 for (i = 0; i < sizeof(int) * 8; i++) {
651 if ((1 << i) & cpu)
652 CPU_SET(i, &cpumask);
653 }
654#endif
655}
656
Jens Axboeebac4652005-12-08 15:25:21 +0100657static int is_empty_or_comment(char *line)
658{
659 unsigned int i;
660
661 for (i = 0; i < strlen(line); i++) {
662 if (line[i] == ';')
663 return 1;
664 if (!isspace(line[i]) && !iscntrl(line[i]))
665 return 0;
666 }
667
668 return 1;
669}
670
Jens Axboecb2c86f2006-10-26 13:48:34 +0200671static int str_rw_cb(void *data, char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100672{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200673 struct thread_data *td = data;
674
Jens Axboeebac4652005-12-08 15:25:21 +0100675 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
676 td->ddir = DDIR_READ;
677 td->sequential = 1;
678 return 0;
679 } else if (!strncmp(mem, "randread", 8)) {
680 td->ddir = DDIR_READ;
681 td->sequential = 0;
682 return 0;
683 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
684 td->ddir = DDIR_WRITE;
685 td->sequential = 1;
686 return 0;
687 } else if (!strncmp(mem, "randwrite", 9)) {
688 td->ddir = DDIR_WRITE;
689 td->sequential = 0;
690 return 0;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200691 } else if (!strncmp(mem, "rw", 2)) {
692 td->ddir = 0;
693 td->iomix = 1;
694 td->sequential = 1;
695 return 0;
696 } else if (!strncmp(mem, "randrw", 6)) {
697 td->ddir = 0;
698 td->iomix = 1;
699 td->sequential = 0;
700 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100701 }
702
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200703 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100704 return 1;
705}
706
Jens Axboecb2c86f2006-10-26 13:48:34 +0200707static int str_verify_cb(void *data, char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100708{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200709 struct thread_data *td = data;
710
Jens Axboeebac4652005-12-08 15:25:21 +0100711 if (!strncmp(mem, "0", 1)) {
712 td->verify = VERIFY_NONE;
713 return 0;
714 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
715 td->verify = VERIFY_MD5;
716 return 0;
717 } else if (!strncmp(mem, "crc32", 5)) {
718 td->verify = VERIFY_CRC32;
719 return 0;
720 }
721
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200722 log_err("fio: verify types: md5, crc32\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100723 return 1;
724}
725
Jens Axboecb2c86f2006-10-26 13:48:34 +0200726static int str_mem_cb(void *data, char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100727{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200728 struct thread_data *td = data;
729
Jens Axboeebac4652005-12-08 15:25:21 +0100730 if (!strncmp(mem, "malloc", 6)) {
731 td->mem_type = MEM_MALLOC;
732 return 0;
733 } else if (!strncmp(mem, "shm", 3)) {
734 td->mem_type = MEM_SHM;
735 return 0;
736 } else if (!strncmp(mem, "mmap", 4)) {
737 td->mem_type = MEM_MMAP;
738 return 0;
739 }
740
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200741 log_err("fio: mem type: malloc, shm, mmap\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100742 return 1;
743}
744
Jens Axboecb2c86f2006-10-26 13:48:34 +0200745static int str_ioengine_cb(void *data, char *str)
Jens Axboeebac4652005-12-08 15:25:21 +0100746{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200747 struct thread_data *td = data;
748
Jens Axboe2866c822006-10-09 15:57:48 +0200749 td->io_ops = load_ioengine(td, str);
750 if (td->io_ops)
Jens Axboeebac4652005-12-08 15:25:21 +0100751 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100752
Jens Axboeb990b5c2006-09-14 09:48:22 +0200753 log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice, cpu\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100754 return 1;
755}
756
Jens Axboee1f36502006-10-27 10:54:08 +0200757static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
758{
759 mlock_size = *val;
760 return 0;
761}
762
763static int str_prioclass_cb(void *data, unsigned int *val)
764{
765 struct thread_data *td = data;
766
767 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
768 return 0;
769}
770
771static int str_prio_cb(void *data, unsigned int *val)
772{
773 struct thread_data *td = data;
774
775 td->ioprio |= *val;
776 return 0;
777}
778
779static int str_exitall_cb(void)
780{
781 exitall_on_terminate = 1;
782 return 0;
783}
784
785static int str_cpumask_cb(void *data, unsigned int *val)
786{
787 struct thread_data *td = data;
788
789 fill_cpu_mask(td->cpumask, *val);
790 return 0;
791}
792
Jens Axboe07261982006-06-07 10:51:12 +0200793/*
794 * This is our [ini] type file parser.
795 */
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200796int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100797{
Jens Axboee1f36502006-10-27 10:54:08 +0200798 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +0100799 struct thread_data *td;
Jens Axboeef899b62006-06-06 09:31:00 +0200800 char *string, *name, *tmpbuf;
Jens Axboeebac4652005-12-08 15:25:21 +0100801 fpos_t off;
802 FILE *f;
803 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200804 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +0100805
806 f = fopen(file, "r");
807 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200808 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100809 return 1;
810 }
811
812 string = malloc(4096);
813 name = malloc(256);
Jens Axboeef899b62006-06-06 09:31:00 +0200814 tmpbuf = malloc(4096);
Jens Axboeebac4652005-12-08 15:25:21 +0100815
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200816 stonewall = stonewall_flag;
Jens Axboeebac4652005-12-08 15:25:21 +0100817 while ((p = fgets(string, 4096, f)) != NULL) {
Jens Axboe45410ac2006-06-07 11:10:39 +0200818 if (ret)
819 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100820 if (is_empty_or_comment(p))
821 continue;
822 if (sscanf(p, "[%s]", name) != 1)
823 continue;
824
825 global = !strncmp(name, "global", 6);
826
827 name[strlen(name) - 1] = '\0';
828
829 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200830 if (!td) {
831 ret = 1;
832 break;
833 }
Jens Axboeebac4652005-12-08 15:25:21 +0100834
Jens Axboe972cfd22006-06-09 11:08:56 +0200835 /*
836 * Seperate multiple job files by a stonewall
837 */
Jens Axboef9481912006-06-09 11:37:28 +0200838 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +0200839 td->stonewall = stonewall;
840 stonewall = 0;
841 }
842
Jens Axboeebac4652005-12-08 15:25:21 +0100843 fgetpos(f, &off);
844 while ((p = fgets(string, 4096, f)) != NULL) {
845 if (is_empty_or_comment(p))
846 continue;
847 if (strstr(p, "["))
848 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200849
Jens Axboeb6754f92006-05-30 14:29:32 +0200850 strip_blank_front(&p);
Jens Axboe4ae3f762006-05-30 13:35:14 +0200851 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200852
Jens Axboee1f36502006-10-27 10:54:08 +0200853 fgetpos(f, &off);
Jens Axboeebac4652005-12-08 15:25:21 +0100854
Jens Axboe45410ac2006-06-07 11:10:39 +0200855 /*
856 * Don't break here, continue parsing options so we
857 * dump all the bad ones. Makes trial/error fixups
858 * easier on the user.
859 */
Jens Axboee1f36502006-10-27 10:54:08 +0200860 ret = parse_option(p, options, td);
Jens Axboeebac4652005-12-08 15:25:21 +0100861 }
Jens Axboeebac4652005-12-08 15:25:21 +0100862
Jens Axboe45410ac2006-06-07 11:10:39 +0200863 if (!ret) {
864 fsetpos(f, &off);
865 ret = add_job(td, name, 0);
866 }
867 if (ret)
868 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100869 }
870
871 free(string);
872 free(name);
Jens Axboeef899b62006-06-06 09:31:00 +0200873 free(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100874 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200875 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100876}
877
878static int fill_def_thread(void)
879{
880 memset(&def_thread, 0, sizeof(def_thread));
881
882 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
883 perror("sched_getaffinity");
884 return 1;
885 }
886
887 /*
888 * fill globals
889 */
890 def_thread.ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200891 def_thread.iomix = 0;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200892 def_thread.bs = DEF_BS;
Jens Axboeebac4652005-12-08 15:25:21 +0100893 def_thread.min_bs = -1;
894 def_thread.max_bs = -1;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200895 def_thread.odirect = DEF_ODIRECT;
Jens Axboeebac4652005-12-08 15:25:21 +0100896 def_thread.ratecycle = DEF_RATE_CYCLE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200897 def_thread.sequential = DEF_SEQUENTIAL;
Jens Axboe972cfd22006-06-09 11:08:56 +0200898 def_thread.timeout = def_timeout;
Jens Axboeebac4652005-12-08 15:25:21 +0100899 def_thread.overwrite = DEF_OVERWRITE;
900 def_thread.invalidate_cache = DEF_INVALIDATE;
901 def_thread.sync_io = DEF_SYNCIO;
902 def_thread.mem_type = MEM_MALLOC;
903 def_thread.bw_avg_time = DEF_BWAVGTIME;
904 def_thread.create_serialize = DEF_CREATE_SER;
905 def_thread.create_fsync = DEF_CREATE_FSYNC;
906 def_thread.loops = DEF_LOOPS;
907 def_thread.verify = DEF_VERIFY;
908 def_thread.stonewall = DEF_STONEWALL;
909 def_thread.numjobs = DEF_NUMJOBS;
910 def_thread.use_thread = DEF_USE_THREAD;
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200911 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
912 def_thread.rwmixread = DEF_RWMIX_READ;
Jens Axboeb6f4d882006-06-02 10:32:51 +0200913 def_thread.nice = DEF_NICE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200914 def_thread.rand_repeatable = DEF_RAND_REPEAT;
Jens Axboe53cdc682006-10-18 11:50:58 +0200915 def_thread.nr_files = DEF_NR_FILES;
Jens Axboef6cbb262006-10-18 14:16:42 +0200916 def_thread.unlink = DEF_UNLINK;
Jens Axboeec94ec52006-10-20 10:59:19 +0200917 def_thread.write_bw_log = write_bw_log;
918 def_thread.write_lat_log = write_lat_log;
Jens Axboeebac4652005-12-08 15:25:21 +0100919#ifdef FIO_HAVE_DISK_UTIL
920 def_thread.do_disk_util = 1;
921#endif
922
923 return 0;
924}
925
Jens Axboe0ab8db82006-10-18 17:16:23 +0200926static void usage(void)
Jens Axboe4785f992006-05-26 03:59:10 +0200927{
928 printf("%s\n", fio_version_string);
Jens Axboe47623c72006-06-12 10:23:28 +0200929 printf("\t-o Write output to file\n");
Jens Axboe4785f992006-05-26 03:59:10 +0200930 printf("\t-t Runtime in seconds\n");
Jens Axboe4785f992006-05-26 03:59:10 +0200931 printf("\t-l Generate per-job latency logs\n");
932 printf("\t-w Generate per-job bandwidth logs\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +0200933 printf("\t-m Minimal (terse) output\n");
Jens Axboe4785f992006-05-26 03:59:10 +0200934 printf("\t-v Print version info and exit\n");
935}
936
Jens Axboe972cfd22006-06-09 11:08:56 +0200937static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +0100938{
Jens Axboe972cfd22006-06-09 11:08:56 +0200939 int c, idx = 1, ini_idx = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100940
Jens Axboe2e778412006-06-16 10:00:50 +0200941 while ((c = getopt(argc, argv, "t:o:lwvhm")) != EOF) {
Jens Axboeebac4652005-12-08 15:25:21 +0100942 switch (c) {
Jens Axboeebac4652005-12-08 15:25:21 +0100943 case 't':
Jens Axboe972cfd22006-06-09 11:08:56 +0200944 def_timeout = atoi(optarg);
Jens Axboe774a6172006-08-24 08:44:26 +0200945 idx = optind;
Jens Axboeebac4652005-12-08 15:25:21 +0100946 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100947 case 'l':
948 write_lat_log = 1;
Jens Axboe774a6172006-08-24 08:44:26 +0200949 idx = optind;
Jens Axboeebac4652005-12-08 15:25:21 +0100950 break;
951 case 'w':
952 write_bw_log = 1;
Jens Axboe774a6172006-08-24 08:44:26 +0200953 idx = optind;
Jens Axboeebac4652005-12-08 15:25:21 +0100954 break;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200955 case 'o':
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200956 f_out = fopen(optarg, "w+");
957 if (!f_out) {
958 perror("fopen output");
959 exit(1);
960 }
961 f_err = f_out;
Jens Axboe774a6172006-08-24 08:44:26 +0200962 idx = optind;
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200963 break;
Jens Axboec6ae0a52006-06-12 11:04:44 +0200964 case 'm':
965 terse_output = 1;
Jens Axboe774a6172006-08-24 08:44:26 +0200966 idx = optind;
Jens Axboec6ae0a52006-06-12 11:04:44 +0200967 break;
Jens Axboe4785f992006-05-26 03:59:10 +0200968 case 'h':
Jens Axboe0ab8db82006-10-18 17:16:23 +0200969 usage();
Jens Axboe4785f992006-05-26 03:59:10 +0200970 exit(0);
Jens Axboeebac4652005-12-08 15:25:21 +0100971 case 'v':
972 printf("%s\n", fio_version_string);
973 exit(0);
974 }
975 }
Jens Axboec9fad892006-05-27 17:26:50 +0200976
Jens Axboe972cfd22006-06-09 11:08:56 +0200977 while (idx < argc) {
978 ini_idx++;
979 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
980 ini_file[ini_idx - 1] = strdup(argv[idx]);
981 idx++;
982 }
Jens Axboe774a6172006-08-24 08:44:26 +0200983
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200984 if (!f_out) {
985 f_out = stdout;
986 f_err = stderr;
987 }
Jens Axboe972cfd22006-06-09 11:08:56 +0200988
989 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +0100990}
991
992static void free_shm(void)
993{
994 struct shmid_ds sbuf;
995
996 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +0200997 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +0100998 threads = NULL;
999 shmctl(shm_id, IPC_RMID, &sbuf);
1000 }
1001}
1002
Jens Axboe906c8d72006-06-13 09:37:56 +02001003/*
1004 * The thread area is shared between the main process and the job
1005 * threads/processes. So setup a shared memory segment that will hold
1006 * all the job info.
1007 */
Jens Axboeebac4652005-12-08 15:25:21 +01001008static int setup_thread_area(void)
1009{
1010 /*
1011 * 1024 is too much on some machines, scale max_jobs if
1012 * we get a failure that looks like too large a shm segment
1013 */
1014 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001015 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001016
Jens Axboe906c8d72006-06-13 09:37:56 +02001017 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001018 if (shm_id != -1)
1019 break;
1020 if (errno != EINVAL) {
1021 perror("shmget");
1022 break;
1023 }
1024
1025 max_jobs >>= 1;
1026 } while (max_jobs);
1027
1028 if (shm_id == -1)
1029 return 1;
1030
1031 threads = shmat(shm_id, NULL, 0);
1032 if (threads == (void *) -1) {
1033 perror("shmat");
1034 return 1;
1035 }
1036
1037 atexit(free_shm);
1038 return 0;
1039}
1040
1041int parse_options(int argc, char *argv[])
1042{
Jens Axboe972cfd22006-06-09 11:08:56 +02001043 int job_files, i;
1044
Jens Axboeebac4652005-12-08 15:25:21 +01001045 if (setup_thread_area())
1046 return 1;
1047 if (fill_def_thread())
1048 return 1;
1049
Jens Axboe972cfd22006-06-09 11:08:56 +02001050 job_files = parse_cmd_line(argc, argv);
1051 if (!job_files) {
1052 log_err("Need job file(s)\n");
Jens Axboe0ab8db82006-10-18 17:16:23 +02001053 usage();
Jens Axboeebac4652005-12-08 15:25:21 +01001054 return 1;
1055 }
1056
Jens Axboe972cfd22006-06-09 11:08:56 +02001057 for (i = 0; i < job_files; i++) {
1058 if (fill_def_thread())
1059 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001060 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001061 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001062 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001063 }
Jens Axboeebac4652005-12-08 15:25:21 +01001064
Jens Axboe88c6ed82006-06-09 11:28:10 +02001065 free(ini_file);
Jens Axboeebac4652005-12-08 15:25:21 +01001066 return 0;
1067}