blob: edaec144f3aaaa5895339df4f2a703bdff7d9c48 [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>
Jens Axboeb4692822006-10-27 13:43:22 +020011#include <getopt.h>
12#include <assert.h>
Jens Axboeebac4652005-12-08 15:25:21 +010013#include <sys/ipc.h>
14#include <sys/shm.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17
18#include "fio.h"
Jens Axboecb2c86f2006-10-26 13:48:34 +020019#include "parse.h"
Jens Axboeebac4652005-12-08 15:25:21 +010020
Jens Axboe906c8d72006-06-13 09:37:56 +020021/*
22 * The default options
23 */
Jens Axboe20dc95c2005-12-09 10:29:35 +010024#define DEF_BS (4096)
25#define DEF_TIMEOUT (0)
26#define DEF_RATE_CYCLE (1000)
27#define DEF_ODIRECT (1)
28#define DEF_IO_ENGINE (FIO_SYNCIO)
Jens Axboeebac4652005-12-08 15:25:21 +010029#define DEF_IO_ENGINE_NAME "sync"
Jens Axboe20dc95c2005-12-09 10:29:35 +010030#define DEF_SEQUENTIAL (1)
31#define DEF_RAND_REPEAT (1)
Jens Axboe178d11f2006-12-15 13:11:24 +010032#define DEF_OVERWRITE (0)
Jens Axboe20dc95c2005-12-09 10:29:35 +010033#define DEF_INVALIDATE (1)
34#define DEF_SYNCIO (0)
35#define DEF_RANDSEED (0xb1899bedUL)
36#define DEF_BWAVGTIME (500)
37#define DEF_CREATE_SER (1)
Jens Axboeebac4652005-12-08 15:25:21 +010038#define DEF_CREATE_FSYNC (1)
Jens Axboe20dc95c2005-12-09 10:29:35 +010039#define DEF_LOOPS (1)
40#define DEF_VERIFY (0)
41#define DEF_STONEWALL (0)
42#define DEF_NUMJOBS (1)
43#define DEF_USE_THREAD (0)
44#define DEF_FILE_SIZE (1024 * 1024 * 1024UL)
45#define DEF_ZONE_SIZE (0)
46#define DEF_ZONE_SKIP (0)
Jens Axboea6ccc7b2006-06-02 10:14:15 +020047#define DEF_RWMIX_CYCLE (500)
48#define DEF_RWMIX_READ (50)
Jens Axboeb6f4d882006-06-02 10:32:51 +020049#define DEF_NICE (0)
Jens Axboe53cdc682006-10-18 11:50:58 +020050#define DEF_NR_FILES (1)
Jens Axboe178d11f2006-12-15 13:11:24 +010051#define DEF_UNLINK (1)
Jens Axboeec94ec52006-10-20 10:59:19 +020052#define DEF_WRITE_BW_LOG (0)
53#define DEF_WRITE_LAT_LOG (0)
Jens Axboebb8895e2006-10-30 15:14:48 +010054#define DEF_NO_RAND_MAP (0)
Jens Axboeebac4652005-12-08 15:25:21 +010055
Jens Axboee1f36502006-10-27 10:54:08 +020056#define td_var_offset(var) ((size_t) &((struct thread_data *)0)->var)
57
Jens Axboeb4692822006-10-27 13:43:22 +020058static int str_rw_cb(void *, const char *);
59static int str_ioengine_cb(void *, const char *);
60static int str_mem_cb(void *, const char *);
61static int str_verify_cb(void *, const char *);
Jens Axboee1f36502006-10-27 10:54:08 +020062static int str_lockmem_cb(void *, unsigned long *);
Jens Axboe34cfcda2006-11-03 14:00:45 +010063#ifdef FIO_HAVE_IOPRIO
Jens Axboee1f36502006-10-27 10:54:08 +020064static int str_prio_cb(void *, unsigned int *);
65static int str_prioclass_cb(void *, unsigned int *);
Jens Axboe34cfcda2006-11-03 14:00:45 +010066#endif
Jens Axboee1f36502006-10-27 10:54:08 +020067static int str_exitall_cb(void);
68static int str_cpumask_cb(void *, unsigned int *);
69
70/*
71 * Map of job/command line options
72 */
73static struct fio_option options[] = {
74 {
75 .name = "name",
76 .type = FIO_OPT_STR_STORE,
77 .off1 = td_var_offset(name),
78 },
79 {
80 .name = "directory",
81 .type = FIO_OPT_STR_STORE,
82 .off1 = td_var_offset(directory),
83 },
84 {
85 .name = "filename",
86 .type = FIO_OPT_STR_STORE,
87 .off1 = td_var_offset(filename),
88 },
89 {
90 .name = "rw",
91 .type = FIO_OPT_STR,
92 .cb = str_rw_cb,
93 },
94 {
95 .name = "ioengine",
96 .type = FIO_OPT_STR,
97 .cb = str_ioengine_cb,
98 },
99 {
100 .name = "mem",
101 .type = FIO_OPT_STR,
102 .cb = str_mem_cb,
103 },
104 {
105 .name = "verify",
106 .type = FIO_OPT_STR,
107 .cb = str_verify_cb,
108 },
109 {
110 .name = "write_iolog",
Jens Axboe076efc72006-10-27 11:24:25 +0200111 .type = FIO_OPT_STR_STORE,
112 .off1 = td_var_offset(write_iolog_file),
Jens Axboee1f36502006-10-27 10:54:08 +0200113 },
114 {
Jens Axboe076efc72006-10-27 11:24:25 +0200115 .name = "read_iolog",
Jens Axboee1f36502006-10-27 10:54:08 +0200116 .type = FIO_OPT_STR_STORE,
Jens Axboe076efc72006-10-27 11:24:25 +0200117 .off1 = td_var_offset(read_iolog_file),
Jens Axboee1f36502006-10-27 10:54:08 +0200118 },
119 {
120 .name = "exec_prerun",
121 .type = FIO_OPT_STR_STORE,
122 .off1 = td_var_offset(exec_prerun),
123 },
124 {
125 .name = "exec_postrun",
126 .type = FIO_OPT_STR_STORE,
127 .off1 = td_var_offset(exec_postrun),
128 },
129#ifdef FIO_HAVE_IOSCHED_SWITCH
130 {
131 .name = "ioscheduler",
132 .type = FIO_OPT_STR_STORE,
133 .off1 = td_var_offset(ioscheduler),
134 },
135#endif
136 {
137 .name = "size",
138 .type = FIO_OPT_STR_VAL,
139 .off1 = td_var_offset(total_file_size),
140 },
141 {
142 .name = "bs",
Jens Axboe75e6f362006-11-03 08:17:09 +0100143 .type = FIO_OPT_STR_VAL_INT,
Jens Axboea00735e2006-11-03 08:58:08 +0100144 .off1 = td_var_offset(bs[DDIR_READ]),
Jens Axboef90eff52006-11-06 11:08:21 +0100145 .off2 = td_var_offset(bs[DDIR_WRITE]),
Jens Axboee1f36502006-10-27 10:54:08 +0200146 },
147 {
148 .name = "offset",
149 .type = FIO_OPT_STR_VAL,
150 .off1 = td_var_offset(start_offset),
151 },
152 {
153 .name = "zonesize",
154 .type = FIO_OPT_STR_VAL,
155 .off1 = td_var_offset(zone_size),
156 },
157 {
158 .name = "zoneskip",
159 .type = FIO_OPT_STR_VAL,
160 .off1 = td_var_offset(zone_skip),
161 },
162 {
163 .name = "lockmem",
164 .type = FIO_OPT_STR_VAL,
165 .cb = str_lockmem_cb,
166 },
167 {
168 .name = "bsrange",
169 .type = FIO_OPT_RANGE,
Jens Axboea00735e2006-11-03 08:58:08 +0100170 .off1 = td_var_offset(min_bs[DDIR_READ]),
171 .off2 = td_var_offset(max_bs[DDIR_READ]),
Jens Axboef90eff52006-11-06 11:08:21 +0100172 .off3 = td_var_offset(min_bs[DDIR_WRITE]),
173 .off4 = td_var_offset(max_bs[DDIR_WRITE]),
Jens Axboee1f36502006-10-27 10:54:08 +0200174 },
175 {
176 .name = "nrfiles",
177 .type = FIO_OPT_INT,
178 .off1 = td_var_offset(nr_files),
179 },
180 {
181 .name = "iodepth",
182 .type = FIO_OPT_INT,
183 .off1 = td_var_offset(iodepth),
184 },
185 {
186 .name = "fsync",
187 .type = FIO_OPT_INT,
188 .off1 = td_var_offset(fsync_blocks),
189 },
190 {
191 .name = "rwmixcycle",
192 .type = FIO_OPT_INT,
193 .off1 = td_var_offset(rwmixcycle),
194 },
195 {
196 .name = "rwmixread",
197 .type = FIO_OPT_INT,
198 .off1 = td_var_offset(rwmixread),
199 .max_val= 100,
200 },
201 {
202 .name = "rwmixwrite",
203 .type = FIO_OPT_INT,
204 .off1 = td_var_offset(rwmixwrite),
205 .max_val= 100,
206 },
207 {
208 .name = "nice",
209 .type = FIO_OPT_INT,
210 .off1 = td_var_offset(nice),
211 },
212#ifdef FIO_HAVE_IOPRIO
213 {
214 .name = "prio",
215 .type = FIO_OPT_INT,
216 .cb = str_prio_cb,
217 },
218 {
219 .name = "prioclass",
220 .type = FIO_OPT_INT,
221 .cb = str_prioclass_cb,
222 },
223#endif
224 {
225 .name = "thinktime",
226 .type = FIO_OPT_INT,
227 .off1 = td_var_offset(thinktime)
228 },
229 {
230 .name = "rate",
231 .type = FIO_OPT_INT,
232 .off1 = td_var_offset(rate)
233 },
234 {
235 .name = "ratemin",
236 .type = FIO_OPT_INT,
237 .off1 = td_var_offset(ratemin)
238 },
239 {
240 .name = "ratecycle",
241 .type = FIO_OPT_INT,
242 .off1 = td_var_offset(ratecycle)
243 },
244 {
245 .name = "startdelay",
246 .type = FIO_OPT_INT,
247 .off1 = td_var_offset(start_delay)
248 },
249 {
250 .name = "timeout",
251 .type = FIO_OPT_STR_VAL_TIME,
252 .off1 = td_var_offset(timeout)
253 },
254 {
255 .name = "invalidate",
256 .type = FIO_OPT_INT,
257 .off1 = td_var_offset(invalidate_cache)
258 },
259 {
260 .name = "sync",
261 .type = FIO_OPT_INT,
262 .off1 = td_var_offset(sync_io)
263 },
264 {
265 .name = "bwavgtime",
266 .type = FIO_OPT_INT,
267 .off1 = td_var_offset(bw_avg_time)
268 },
269 {
270 .name = "create_serialize",
271 .type = FIO_OPT_INT,
272 .off1 = td_var_offset(create_serialize)
273 },
274 {
275 .name = "create_fsync",
276 .type = FIO_OPT_INT,
277 .off1 = td_var_offset(create_fsync)
278 },
279 {
280 .name = "loops",
281 .type = FIO_OPT_INT,
282 .off1 = td_var_offset(loops)
283 },
284 {
285 .name = "numjobs",
286 .type = FIO_OPT_INT,
287 .off1 = td_var_offset(numjobs)
288 },
289 {
290 .name = "cpuload",
291 .type = FIO_OPT_INT,
292 .off1 = td_var_offset(cpuload)
293 },
294 {
295 .name = "cpuchunks",
296 .type = FIO_OPT_INT,
297 .off1 = td_var_offset(cpucycle)
298 },
299 {
300 .name = "direct",
301 .type = FIO_OPT_INT,
302 .off1 = td_var_offset(odirect)
303 },
304 {
305 .name = "overwrite",
306 .type = FIO_OPT_INT,
307 .off1 = td_var_offset(overwrite)
308 },
309#ifdef FIO_HAVE_CPU_AFFINITY
310 {
311 .name = "cpumask",
312 .type = FIO_OPT_INT,
313 .cb = str_cpumask_cb,
314 },
315#endif
316 {
317 .name = "end_fsync",
318 .type = FIO_OPT_INT,
319 .off1 = td_var_offset(end_fsync)
320 },
321 {
322 .name = "unlink",
323 .type = FIO_OPT_STR_SET,
324 .off1 = td_var_offset(unlink),
325 },
326 {
327 .name = "exitall",
328 .type = FIO_OPT_STR_SET,
329 .cb = str_exitall_cb,
330 },
331 {
332 .name = "stonewall",
333 .type = FIO_OPT_STR_SET,
334 .off1 = td_var_offset(stonewall),
335 },
336 {
337 .name = "thread",
338 .type = FIO_OPT_STR_SET,
339 .off1 = td_var_offset(thread),
340 },
341 {
342 .name = "write_bw_log",
343 .type = FIO_OPT_STR_SET,
344 .off1 = td_var_offset(write_bw_log),
345 },
346 {
347 .name = "write_lat_log",
348 .type = FIO_OPT_STR_SET,
349 .off1 = td_var_offset(write_lat_log),
350 },
351 {
Jens Axboebb8895e2006-10-30 15:14:48 +0100352 .name = "norandommap",
353 .type = FIO_OPT_STR_SET,
354 .off1 = td_var_offset(norandommap),
355 },
356 {
Jens Axboe690adba2006-10-30 15:25:09 +0100357 .name = "bs_unaligned",
358 .type = FIO_OPT_STR_SET,
359 .off1 = td_var_offset(bs_unaligned),
360 },
361 {
Jens Axboee1f36502006-10-27 10:54:08 +0200362 .name = NULL,
363 },
364};
365
Jens Axboeb4692822006-10-27 13:43:22 +0200366#define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
367#define FIO_CMD_OPTS (16)
368#define FIO_GETOPT_JOB (0x89988998)
369
370/*
371 * Command line options. These will contain the above, plus a few
372 * extra that only pertain to fio itself and not jobs.
373 */
374static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
375 {
376 .name = "output",
377 .has_arg = required_argument,
378 .val = 'o',
379 },
380 {
381 .name = "timeout",
382 .has_arg = required_argument,
383 .val = 't',
384 },
385 {
386 .name = "latency-log",
387 .has_arg = required_argument,
388 .val = 'l',
389 },
390 {
391 .name = "bandwidth-log",
392 .has_arg = required_argument,
393 .val = 'b',
394 },
395 {
396 .name = "minimal",
397 .has_arg = optional_argument,
398 .val = 'm',
399 },
400 {
401 .name = "version",
402 .has_arg = no_argument,
403 .val = 'v',
404 },
405 {
406 .name = NULL,
407 },
408};
409
Jens Axboe972cfd22006-06-09 11:08:56 +0200410static int def_timeout = DEF_TIMEOUT;
Jens Axboe972cfd22006-06-09 11:08:56 +0200411
Jens Axboebe67e2d2006-11-07 14:33:15 +0100412static char fio_version_string[] = "fio 1.9";
Jens Axboeebac4652005-12-08 15:25:21 +0100413
Jens Axboe972cfd22006-06-09 11:08:56 +0200414static char **ini_file;
Jens Axboeebac4652005-12-08 15:25:21 +0100415static int max_jobs = MAX_JOBS;
416
417struct thread_data def_thread;
418struct thread_data *threads = NULL;
419
Jens Axboeebac4652005-12-08 15:25:21 +0100420int exitall_on_terminate = 0;
Jens Axboec6ae0a52006-06-12 11:04:44 +0200421int terse_output = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +0200422unsigned long long mlock_size = 0;
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200423FILE *f_out = NULL;
424FILE *f_err = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +0100425
Jens Axboeec94ec52006-10-20 10:59:19 +0200426static int write_lat_log = DEF_WRITE_LAT_LOG;
427static int write_bw_log = DEF_WRITE_BW_LOG;
428
Jens Axboe906c8d72006-06-13 09:37:56 +0200429/*
430 * Return a free job structure.
431 */
Jens Axboeebac4652005-12-08 15:25:21 +0100432static struct thread_data *get_new_job(int global, struct thread_data *parent)
433{
434 struct thread_data *td;
435
436 if (global)
437 return &def_thread;
438 if (thread_number >= max_jobs)
439 return NULL;
440
441 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200442 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100443
Jens Axboeebac4652005-12-08 15:25:21 +0100444 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100445 return td;
446}
447
448static void put_job(struct thread_data *td)
449{
Jens Axboe549577a2006-10-30 12:23:41 +0100450 if (td == &def_thread)
451 return;
452
Jens Axboeebac4652005-12-08 15:25:21 +0100453 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
454 thread_number--;
455}
456
Jens Axboedad915e2006-10-27 11:10:18 +0200457/*
458 * Lazy way of fixing up options that depend on each other. We could also
459 * define option callback handlers, but this is easier.
460 */
Jens Axboee1f36502006-10-27 10:54:08 +0200461static void fixup_options(struct thread_data *td)
462{
Jens Axboee1f36502006-10-27 10:54:08 +0200463 if (!td->rwmixread && td->rwmixwrite)
464 td->rwmixread = 100 - td->rwmixwrite;
Jens Axboedad915e2006-10-27 11:10:18 +0200465
Jens Axboe076efc72006-10-27 11:24:25 +0200466 if (td->write_iolog_file && td->read_iolog_file) {
467 log_err("fio: read iolog overrides write_iolog\n");
468 free(td->write_iolog_file);
469 td->write_iolog_file = NULL;
470 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100471
472 if (td->io_ops->flags & FIO_SYNCIO)
473 td->iodepth = 1;
474 else {
475 if (!td->iodepth)
476 td->iodepth = td->nr_files;
477 }
478
479 /*
480 * only really works for sequential io for now, and with 1 file
481 */
482 if (td->zone_size && !td->sequential && td->nr_files == 1)
483 td->zone_size = 0;
484
485 /*
486 * Reads can do overwrites, we always need to pre-create the file
487 */
488 if (td_read(td) || td_rw(td))
489 td->overwrite = 1;
490
Jens Axboea00735e2006-11-03 08:58:08 +0100491 if (!td->min_bs[DDIR_READ])
492 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
493 if (!td->max_bs[DDIR_READ])
494 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
495 if (!td->min_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100496 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100497 if (!td->max_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100498 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100499
500 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
501
Jens Axboe16b462a2006-10-30 12:35:18 +0100502 if (td_read(td) && !td_rw(td))
503 td->verify = 0;
Jens Axboebb8895e2006-10-30 15:14:48 +0100504
505 if (td->norandommap && td->verify != VERIFY_NONE) {
506 log_err("fio: norandommap given, verify disabled\n");
507 td->verify = VERIFY_NONE;
508 }
Jens Axboe690adba2006-10-30 15:25:09 +0100509 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
510 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100511
512 /*
513 * O_DIRECT and char doesn't mix, clear that flag if necessary.
514 */
515 if (td->filetype == FIO_TYPE_CHAR && td->odirect)
516 td->odirect = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200517}
518
Jens Axboe906c8d72006-06-13 09:37:56 +0200519/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100520 * This function leaks the buffer
521 */
522static char *to_kmg(unsigned int val)
523{
524 char *buf = malloc(32);
Jens Axboe245142f2006-11-08 12:49:21 +0100525 char post[] = { 0, 'K', 'M', 'G', 'P', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100526 char *p = post;
527
Jens Axboe245142f2006-11-08 12:49:21 +0100528 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100529 if (val & 1023)
530 break;
531
532 val >>= 10;
533 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100534 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100535
536 snprintf(buf, 31, "%u%c", val, *p);
537 return buf;
538}
539
540/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200541 * Adds a job to the list of things todo. Sanitizes the various options
542 * to make sure we don't have conflicts, and initializes various
543 * members of td.
544 */
Jens Axboe75154842006-06-01 13:56:09 +0200545static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100546{
Jens Axboe3c9b60c2006-11-07 08:36:14 +0100547 const char *ddir_str[] = { "read", "write", "randread", "randwrite",
548 "rw", NULL, "randrw" };
Jens Axboeebac4652005-12-08 15:25:21 +0100549 struct stat sb;
Jens Axboe53cdc682006-10-18 11:50:58 +0200550 int numjobs, ddir, i;
551 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100552
Jens Axboeebac4652005-12-08 15:25:21 +0100553 /*
554 * the def_thread is just for options, it's not a real job
555 */
556 if (td == &def_thread)
557 return 0;
558
Jens Axboedf641192006-10-12 07:55:41 +0200559 /*
560 * Set default io engine, if none set
561 */
562 if (!td->io_ops) {
563 td->io_ops = load_ioengine(td, DEF_IO_ENGINE_NAME);
564 if (!td->io_ops) {
565 log_err("default engine %s not there?\n", DEF_IO_ENGINE_NAME);
566 return 1;
567 }
568 }
569
Jens Axboe690adba2006-10-30 15:25:09 +0100570 if (td->odirect)
571 td->io_ops->flags |= FIO_RAWIO;
572
Jens Axboeebac4652005-12-08 15:25:21 +0100573 td->filetype = FIO_TYPE_FILE;
Jens Axboe0af7b542006-02-17 10:10:12 +0100574 if (!stat(jobname, &sb)) {
575 if (S_ISBLK(sb.st_mode))
576 td->filetype = FIO_TYPE_BD;
577 else if (S_ISCHR(sb.st_mode))
578 td->filetype = FIO_TYPE_CHAR;
579 }
Jens Axboeebac4652005-12-08 15:25:21 +0100580
Jens Axboee0a22332006-12-20 12:54:25 +0100581 fixup_options(td);
582
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200583 if (td->filename)
584 td->nr_uniq_files = 1;
585 else
586 td->nr_uniq_files = td->nr_files;
587
588 if (td->filetype == FIO_TYPE_FILE || td->filename) {
Jens Axboee9c047a2006-06-07 21:13:04 +0200589 char tmp[PATH_MAX];
Jens Axboe53cdc682006-10-18 11:50:58 +0200590 int len = 0;
Jens Axboee9c047a2006-06-07 21:13:04 +0200591
Jens Axboeef899b62006-06-06 09:31:00 +0200592 if (td->directory && td->directory[0] != '\0')
Jens Axboe53cdc682006-10-18 11:50:58 +0200593 sprintf(tmp, "%s/", td->directory);
Jens Axboeebac4652005-12-08 15:25:21 +0100594
Jens Axboe53cdc682006-10-18 11:50:58 +0200595 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
596
597 for_each_file(td, f, i) {
598 memset(f, 0, sizeof(*f));
599 f->fd = -1;
600
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200601 if (td->filename)
602 sprintf(tmp + len, "%s", td->filename);
603 else
604 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
Jens Axboe53cdc682006-10-18 11:50:58 +0200605 f->file_name = strdup(tmp);
606 }
607 } else {
608 td->nr_files = 1;
609 td->files = malloc(sizeof(struct fio_file));
610 f = &td->files[0];
611
612 memset(f, 0, sizeof(*f));
613 f->fd = -1;
614 f->file_name = strdup(jobname);
615 }
616
617 for_each_file(td, f, i) {
618 f->file_size = td->total_file_size / td->nr_files;
619 f->file_offset = td->start_offset;
620 }
621
Jens Axboebbfd6b02006-06-07 19:42:54 +0200622 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100623
624 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
625 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
626 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
627
Jens Axboeebac4652005-12-08 15:25:21 +0100628 if (td->stonewall && td->thread_number > 1)
629 groupid++;
630
631 td->groupid = groupid;
632
633 if (setup_rate(td))
634 goto err;
635
Jens Axboeec94ec52006-10-20 10:59:19 +0200636 if (td->write_lat_log) {
Jens Axboeebac4652005-12-08 15:25:21 +0100637 setup_log(&td->slat_log);
638 setup_log(&td->clat_log);
639 }
Jens Axboeec94ec52006-10-20 10:59:19 +0200640 if (td->write_bw_log)
Jens Axboeebac4652005-12-08 15:25:21 +0100641 setup_log(&td->bw_log);
642
Jens Axboeb4692822006-10-27 13:43:22 +0200643 if (!td->name)
644 td->name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200645
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200646 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
Jens Axboe75154842006-06-01 13:56:09 +0200647
Jens Axboec6ae0a52006-06-12 11:04:44 +0200648 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200649 if (!job_add_num) {
Jens Axboe2866c822006-10-09 15:57:48 +0200650 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200651 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
Jens Axboef8977ee2006-11-06 14:03:03 +0100652 else {
653 char *c1, *c2, *c3, *c4;
654
655 c1 = to_kmg(td->min_bs[DDIR_READ]);
656 c2 = to_kmg(td->max_bs[DDIR_READ]);
657 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
658 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
659
Jens Axboe1e97cce2006-12-05 11:44:16 +0100660 fprintf(f_out, "%s: (g=%d): rw=%s, odir=%u, bs=%s-%s/%s-%s, rate=%u, ioengine=%s, iodepth=%u\n", td->name, td->groupid, ddir_str[ddir], td->odirect, c1, c2, c3, c4, td->rate, td->io_ops->name, td->iodepth);
Jens Axboef8977ee2006-11-06 14:03:03 +0100661
662 free(c1);
663 free(c2);
664 free(c3);
665 free(c4);
666 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200667 } else if (job_add_num == 1)
Jens Axboec6ae0a52006-06-12 11:04:44 +0200668 fprintf(f_out, "...\n");
669 }
Jens Axboeebac4652005-12-08 15:25:21 +0100670
671 /*
672 * recurse add identical jobs, clear numjobs and stonewall options
673 * as they don't apply to sub-jobs
674 */
675 numjobs = td->numjobs;
676 while (--numjobs) {
677 struct thread_data *td_new = get_new_job(0, td);
678
679 if (!td_new)
680 goto err;
681
682 td_new->numjobs = 1;
683 td_new->stonewall = 0;
Jens Axboe75154842006-06-01 13:56:09 +0200684 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100685
Jens Axboe75154842006-06-01 13:56:09 +0200686 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100687 goto err;
688 }
689 return 0;
690err:
691 put_job(td);
692 return -1;
693}
694
Jens Axboe906c8d72006-06-13 09:37:56 +0200695/*
696 * Initialize the various random states we need (random io, block size ranges,
697 * read/write mix, etc).
698 */
Jens Axboeebac4652005-12-08 15:25:21 +0100699int init_random_state(struct thread_data *td)
700{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200701 unsigned long seeds[4];
Jens Axboe53cdc682006-10-18 11:50:58 +0200702 int fd, num_maps, blocks, i;
Jens Axboe0ab8db82006-10-18 17:16:23 +0200703 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100704
Jens Axboef48b4672006-10-27 11:30:07 +0200705 if (td->io_ops->flags & FIO_CPUIO)
706 return 0;
707
Jens Axboe1ac267b2006-05-31 20:29:00 +0200708 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100709 if (fd == -1) {
710 td_verror(td, errno);
711 return 1;
712 }
713
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200714 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100715 td_verror(td, EIO);
716 close(fd);
717 return 1;
718 }
719
720 close(fd);
721
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200722 os_random_seed(seeds[0], &td->bsrange_state);
723 os_random_seed(seeds[1], &td->verify_state);
724 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100725
726 if (td->sequential)
727 return 0;
728
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200729 if (td->rand_repeatable)
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200730 seeds[3] = DEF_RANDSEED;
Jens Axboeebac4652005-12-08 15:25:21 +0100731
Jens Axboebb8895e2006-10-30 15:14:48 +0100732 if (!td->norandommap) {
733 for_each_file(td, f, i) {
Jens Axboea00735e2006-11-03 08:58:08 +0100734 blocks = (f->file_size + td->rw_min_bs - 1) / td->rw_min_bs;
Jens Axboec7c280e2006-11-02 20:10:33 +0100735 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
Jens Axboebb8895e2006-10-30 15:14:48 +0100736 f->file_map = malloc(num_maps * sizeof(long));
737 f->num_maps = num_maps;
738 memset(f->file_map, 0, num_maps * sizeof(long));
739 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200740 }
Jens Axboeebac4652005-12-08 15:25:21 +0100741
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200742 os_random_seed(seeds[3], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100743 return 0;
744}
745
746static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
747{
748#ifdef FIO_HAVE_CPU_AFFINITY
749 unsigned int i;
750
751 CPU_ZERO(&cpumask);
752
753 for (i = 0; i < sizeof(int) * 8; i++) {
754 if ((1 << i) & cpu)
755 CPU_SET(i, &cpumask);
756 }
757#endif
758}
759
Jens Axboeebac4652005-12-08 15:25:21 +0100760static int is_empty_or_comment(char *line)
761{
762 unsigned int i;
763
764 for (i = 0; i < strlen(line); i++) {
765 if (line[i] == ';')
766 return 1;
767 if (!isspace(line[i]) && !iscntrl(line[i]))
768 return 0;
769 }
770
771 return 1;
772}
773
Jens Axboeb4692822006-10-27 13:43:22 +0200774static int str_rw_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100775{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200776 struct thread_data *td = data;
777
Jens Axboeebac4652005-12-08 15:25:21 +0100778 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
779 td->ddir = DDIR_READ;
780 td->sequential = 1;
781 return 0;
782 } else if (!strncmp(mem, "randread", 8)) {
783 td->ddir = DDIR_READ;
784 td->sequential = 0;
785 return 0;
786 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
787 td->ddir = DDIR_WRITE;
788 td->sequential = 1;
789 return 0;
790 } else if (!strncmp(mem, "randwrite", 9)) {
791 td->ddir = DDIR_WRITE;
792 td->sequential = 0;
793 return 0;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200794 } else if (!strncmp(mem, "rw", 2)) {
Jens Axboe1e97cce2006-12-05 11:44:16 +0100795 td->ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200796 td->iomix = 1;
797 td->sequential = 1;
798 return 0;
799 } else if (!strncmp(mem, "randrw", 6)) {
Jens Axboe1e97cce2006-12-05 11:44:16 +0100800 td->ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200801 td->iomix = 1;
802 td->sequential = 0;
803 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100804 }
805
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200806 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100807 return 1;
808}
809
Jens Axboeb4692822006-10-27 13:43:22 +0200810static int str_verify_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100811{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200812 struct thread_data *td = data;
813
Jens Axboeebac4652005-12-08 15:25:21 +0100814 if (!strncmp(mem, "0", 1)) {
815 td->verify = VERIFY_NONE;
816 return 0;
817 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
818 td->verify = VERIFY_MD5;
819 return 0;
820 } else if (!strncmp(mem, "crc32", 5)) {
821 td->verify = VERIFY_CRC32;
822 return 0;
823 }
824
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200825 log_err("fio: verify types: md5, crc32\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100826 return 1;
827}
828
Jens Axboeb4692822006-10-27 13:43:22 +0200829static int str_mem_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100830{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200831 struct thread_data *td = data;
832
Jens Axboeebac4652005-12-08 15:25:21 +0100833 if (!strncmp(mem, "malloc", 6)) {
834 td->mem_type = MEM_MALLOC;
835 return 0;
Jens Axboed0bdaf42006-12-20 14:40:44 +0100836 } else if (!strncmp(mem, "mmaphuge", 8)) {
837#ifdef FIO_HAVE_HUGETLB
838 char *hugefile;
839
840 /*
841 * mmaphuge must be appended with the actual file
842 */
843 hugefile = strstr(mem, ":");
844 if (!hugefile) {
845 log_err("fio: mmaphuge:/path/to/file\n");
846 return 1;
847 }
848
849 hugefile++;
850 strip_blank_front(&hugefile);
851 strip_blank_end(hugefile);
852 td->hugefile = strdup(hugefile);
853 td->mem_type = MEM_MMAPHUGE;
854 return 0;
855#else
856 log_err("fio: mmaphuge not available\n");
857 return 1;
858#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100859 } else if (!strncmp(mem, "mmap", 4)) {
860 td->mem_type = MEM_MMAP;
861 return 0;
Jens Axboe74b025b2006-12-19 15:18:14 +0100862 } else if (!strncmp(mem, "shmhuge", 7)) {
863#ifdef FIO_HAVE_HUGETLB
864 td->mem_type = MEM_SHMHUGE;
865 return 0;
866#else
867 log_err("fio: shmhuge not available\n");
868 return 1;
869#endif
Jens Axboe0268b8b2006-12-20 12:48:23 +0100870 } else if (!strncmp(mem, "shm", 3)) {
871 td->mem_type = MEM_SHM;
872 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100873 }
874
Jens Axboed0bdaf42006-12-20 14:40:44 +0100875 log_err("fio: mem type: malloc, shm, shmhuge, mmap, mmaphuge\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100876 return 1;
877}
878
Jens Axboeb4692822006-10-27 13:43:22 +0200879static int str_ioengine_cb(void *data, const char *str)
Jens Axboeebac4652005-12-08 15:25:21 +0100880{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200881 struct thread_data *td = data;
882
Jens Axboe2866c822006-10-09 15:57:48 +0200883 td->io_ops = load_ioengine(td, str);
884 if (td->io_ops)
Jens Axboeebac4652005-12-08 15:25:21 +0100885 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100886
Jens Axboe08aae9a2006-11-24 12:43:49 +0100887 log_err("fio: ioengine= libaio, posixaio, sync, mmap, sgio, splice, cpu, null\n");
Jens Axboe5f350952006-11-07 15:20:59 +0100888 log_err("fio: or specify path to dynamic ioengine module\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100889 return 1;
890}
891
Jens Axboee1f36502006-10-27 10:54:08 +0200892static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
893{
894 mlock_size = *val;
895 return 0;
896}
897
Jens Axboe34cfcda2006-11-03 14:00:45 +0100898#ifdef FIO_HAVE_IOPRIO
Jens Axboee1f36502006-10-27 10:54:08 +0200899static int str_prioclass_cb(void *data, unsigned int *val)
900{
901 struct thread_data *td = data;
902
903 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
904 return 0;
905}
906
907static int str_prio_cb(void *data, unsigned int *val)
908{
909 struct thread_data *td = data;
910
911 td->ioprio |= *val;
912 return 0;
913}
Jens Axboe34cfcda2006-11-03 14:00:45 +0100914#endif
Jens Axboee1f36502006-10-27 10:54:08 +0200915
916static int str_exitall_cb(void)
917{
918 exitall_on_terminate = 1;
919 return 0;
920}
921
922static int str_cpumask_cb(void *data, unsigned int *val)
923{
924 struct thread_data *td = data;
925
926 fill_cpu_mask(td->cpumask, *val);
927 return 0;
928}
929
Jens Axboe07261982006-06-07 10:51:12 +0200930/*
931 * This is our [ini] type file parser.
932 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100933static int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100934{
Jens Axboee1f36502006-10-27 10:54:08 +0200935 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +0100936 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +0100937 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +0100938 fpos_t off;
939 FILE *f;
940 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200941 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +0100942
943 f = fopen(file, "r");
944 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200945 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100946 return 1;
947 }
948
949 string = malloc(4096);
950 name = malloc(256);
Jens Axboefee3bb42006-10-30 13:32:08 +0100951 memset(name, 0, 256);
Jens Axboeebac4652005-12-08 15:25:21 +0100952
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200953 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100954 do {
955 p = fgets(string, 4095, f);
956 if (!p)
Jens Axboe45410ac2006-06-07 11:10:39 +0200957 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100958 if (is_empty_or_comment(p))
959 continue;
Jens Axboefee3bb42006-10-30 13:32:08 +0100960 if (sscanf(p, "[%255s]", name) != 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100961 continue;
962
963 global = !strncmp(name, "global", 6);
964
965 name[strlen(name) - 1] = '\0';
966
967 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200968 if (!td) {
969 ret = 1;
970 break;
971 }
Jens Axboeebac4652005-12-08 15:25:21 +0100972
Jens Axboe972cfd22006-06-09 11:08:56 +0200973 /*
974 * Seperate multiple job files by a stonewall
975 */
Jens Axboef9481912006-06-09 11:37:28 +0200976 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +0200977 td->stonewall = stonewall;
978 stonewall = 0;
979 }
980
Jens Axboeebac4652005-12-08 15:25:21 +0100981 fgetpos(f, &off);
982 while ((p = fgets(string, 4096, f)) != NULL) {
983 if (is_empty_or_comment(p))
984 continue;
Jens Axboee1f36502006-10-27 10:54:08 +0200985
Jens Axboeb6754f92006-05-30 14:29:32 +0200986 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +0100987
988 if (p[0] == '[')
989 break;
990
Jens Axboe4ae3f762006-05-30 13:35:14 +0200991 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200992
Jens Axboee1f36502006-10-27 10:54:08 +0200993 fgetpos(f, &off);
Jens Axboeebac4652005-12-08 15:25:21 +0100994
Jens Axboe45410ac2006-06-07 11:10:39 +0200995 /*
996 * Don't break here, continue parsing options so we
997 * dump all the bad ones. Makes trial/error fixups
998 * easier on the user.
999 */
Jens Axboe7c124ac2006-10-30 13:36:52 +01001000 ret |= parse_option(p, options, td);
Jens Axboeebac4652005-12-08 15:25:21 +01001001 }
Jens Axboeebac4652005-12-08 15:25:21 +01001002
Jens Axboe45410ac2006-06-07 11:10:39 +02001003 if (!ret) {
1004 fsetpos(f, &off);
1005 ret = add_job(td, name, 0);
Jens Axboeb1508cf2006-11-02 09:12:40 +01001006 } else {
1007 log_err("fio: job %s dropped\n", name);
1008 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +02001009 }
Jens Axboe7c124ac2006-10-30 13:36:52 +01001010 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001011
1012 free(string);
1013 free(name);
1014 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +02001015 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001016}
1017
1018static int fill_def_thread(void)
1019{
1020 memset(&def_thread, 0, sizeof(def_thread));
1021
1022 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1023 perror("sched_getaffinity");
1024 return 1;
1025 }
1026
1027 /*
1028 * fill globals
1029 */
1030 def_thread.ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001031 def_thread.iomix = 0;
Jens Axboea00735e2006-11-03 08:58:08 +01001032 def_thread.bs[DDIR_READ] = DEF_BS;
1033 def_thread.bs[DDIR_WRITE] = DEF_BS;
1034 def_thread.min_bs[DDIR_READ] = def_thread.min_bs[DDIR_WRITE] = 0;
1035 def_thread.max_bs[DDIR_READ] = def_thread.max_bs[DDIR_WRITE] = 0;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001036 def_thread.odirect = DEF_ODIRECT;
Jens Axboeebac4652005-12-08 15:25:21 +01001037 def_thread.ratecycle = DEF_RATE_CYCLE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001038 def_thread.sequential = DEF_SEQUENTIAL;
Jens Axboe972cfd22006-06-09 11:08:56 +02001039 def_thread.timeout = def_timeout;
Jens Axboeebac4652005-12-08 15:25:21 +01001040 def_thread.overwrite = DEF_OVERWRITE;
1041 def_thread.invalidate_cache = DEF_INVALIDATE;
1042 def_thread.sync_io = DEF_SYNCIO;
1043 def_thread.mem_type = MEM_MALLOC;
1044 def_thread.bw_avg_time = DEF_BWAVGTIME;
1045 def_thread.create_serialize = DEF_CREATE_SER;
1046 def_thread.create_fsync = DEF_CREATE_FSYNC;
1047 def_thread.loops = DEF_LOOPS;
1048 def_thread.verify = DEF_VERIFY;
1049 def_thread.stonewall = DEF_STONEWALL;
1050 def_thread.numjobs = DEF_NUMJOBS;
1051 def_thread.use_thread = DEF_USE_THREAD;
Jens Axboea6ccc7b2006-06-02 10:14:15 +02001052 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
1053 def_thread.rwmixread = DEF_RWMIX_READ;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001054 def_thread.nice = DEF_NICE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001055 def_thread.rand_repeatable = DEF_RAND_REPEAT;
Jens Axboe53cdc682006-10-18 11:50:58 +02001056 def_thread.nr_files = DEF_NR_FILES;
Jens Axboef6cbb262006-10-18 14:16:42 +02001057 def_thread.unlink = DEF_UNLINK;
Jens Axboeec94ec52006-10-20 10:59:19 +02001058 def_thread.write_bw_log = write_bw_log;
1059 def_thread.write_lat_log = write_lat_log;
Jens Axboebb8895e2006-10-30 15:14:48 +01001060 def_thread.norandommap = DEF_NO_RAND_MAP;
Jens Axboeebac4652005-12-08 15:25:21 +01001061#ifdef FIO_HAVE_DISK_UTIL
1062 def_thread.do_disk_util = 1;
1063#endif
1064
1065 return 0;
1066}
1067
Jens Axboe0ab8db82006-10-18 17:16:23 +02001068static void usage(void)
Jens Axboe4785f992006-05-26 03:59:10 +02001069{
1070 printf("%s\n", fio_version_string);
Jens Axboeb4692822006-10-27 13:43:22 +02001071 printf("\t--output\tWrite output to file\n");
1072 printf("\t--timeout\tRuntime in seconds\n");
1073 printf("\t--latency-log\tGenerate per-job latency logs\n");
1074 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1075 printf("\t--minimal\tMinimal (terse) output\n");
1076 printf("\t--version\tPrint version info and exit\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001077}
1078
Jens Axboe972cfd22006-06-09 11:08:56 +02001079static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +01001080{
Jens Axboeb4692822006-10-27 13:43:22 +02001081 struct thread_data *td = NULL;
Jens Axboec2b1e752006-10-30 09:03:13 +01001082 int c, ini_idx = 0, lidx, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001083
Jens Axboeb4692822006-10-27 13:43:22 +02001084 while ((c = getopt_long(argc, argv, "", long_options, &lidx)) != -1) {
Jens Axboeebac4652005-12-08 15:25:21 +01001085 switch (c) {
Jens Axboeb4692822006-10-27 13:43:22 +02001086 case 't':
1087 def_timeout = atoi(optarg);
1088 break;
1089 case 'l':
1090 write_lat_log = 1;
1091 break;
1092 case 'w':
1093 write_bw_log = 1;
1094 break;
1095 case 'o':
1096 f_out = fopen(optarg, "w+");
1097 if (!f_out) {
1098 perror("fopen output");
1099 exit(1);
1100 }
1101 f_err = f_out;
1102 break;
1103 case 'm':
1104 terse_output = 1;
1105 break;
1106 case 'h':
1107 usage();
1108 exit(0);
1109 case 'v':
1110 printf("%s\n", fio_version_string);
1111 exit(0);
1112 case FIO_GETOPT_JOB: {
1113 const char *opt = long_options[lidx].name;
1114 char *val = optarg;
1115
Jens Axboec2b1e752006-10-30 09:03:13 +01001116 if (!strncmp(opt, "name", 4) && td) {
1117 ret = add_job(td, td->name ?: "fio", 0);
1118 if (ret) {
1119 put_job(td);
1120 return 0;
1121 }
1122 td = NULL;
1123 }
Jens Axboeb4692822006-10-27 13:43:22 +02001124 if (!td) {
Jens Axboe38d0adb2006-10-30 12:21:25 +01001125 int global = !strncmp(val, "global", 6);
Jens Axboec2b1e752006-10-30 09:03:13 +01001126
1127 td = get_new_job(global, &def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001128 if (!td)
1129 return 0;
1130 }
Jens Axboe38d0adb2006-10-30 12:21:25 +01001131
Jens Axboeb1508cf2006-11-02 09:12:40 +01001132 ret = parse_cmd_option(opt, val, options, td);
1133 if (ret) {
1134 log_err("fio: job dropped\n");
1135 put_job(td);
1136 td = NULL;
1137 }
Jens Axboeb4692822006-10-27 13:43:22 +02001138 break;
1139 }
1140 default:
1141 printf("optarg <<%s>>\n", argv[optind]);
1142 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001143 }
1144 }
Jens Axboec9fad892006-05-27 17:26:50 +02001145
Jens Axboeb4692822006-10-27 13:43:22 +02001146 if (td) {
Jens Axboec2b1e752006-10-30 09:03:13 +01001147 ret = add_job(td, td->name ?: "fio", 0);
Jens Axboeb4692822006-10-27 13:43:22 +02001148 if (ret)
1149 put_job(td);
Jens Axboe972cfd22006-06-09 11:08:56 +02001150 }
Jens Axboe774a6172006-08-24 08:44:26 +02001151
Jens Axboeb4692822006-10-27 13:43:22 +02001152 while (optind < argc) {
1153 ini_idx++;
1154 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1155 ini_file[ini_idx - 1] = strdup(argv[optind]);
1156 optind++;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001157 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001158
1159 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +01001160}
1161
1162static void free_shm(void)
1163{
1164 struct shmid_ds sbuf;
1165
1166 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001167 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001168 threads = NULL;
1169 shmctl(shm_id, IPC_RMID, &sbuf);
1170 }
1171}
1172
Jens Axboe906c8d72006-06-13 09:37:56 +02001173/*
1174 * The thread area is shared between the main process and the job
1175 * threads/processes. So setup a shared memory segment that will hold
1176 * all the job info.
1177 */
Jens Axboeebac4652005-12-08 15:25:21 +01001178static int setup_thread_area(void)
1179{
1180 /*
1181 * 1024 is too much on some machines, scale max_jobs if
1182 * we get a failure that looks like too large a shm segment
1183 */
1184 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001185 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001186
Jens Axboe906c8d72006-06-13 09:37:56 +02001187 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001188 if (shm_id != -1)
1189 break;
1190 if (errno != EINVAL) {
1191 perror("shmget");
1192 break;
1193 }
1194
1195 max_jobs >>= 1;
1196 } while (max_jobs);
1197
1198 if (shm_id == -1)
1199 return 1;
1200
1201 threads = shmat(shm_id, NULL, 0);
1202 if (threads == (void *) -1) {
1203 perror("shmat");
1204 return 1;
1205 }
1206
1207 atexit(free_shm);
1208 return 0;
1209}
1210
Jens Axboeb4692822006-10-27 13:43:22 +02001211/*
1212 * Copy the fio options into the long options map, so we mirror
1213 * job and cmd line options.
1214 */
1215static void dupe_job_options(void)
1216{
1217 struct fio_option *o;
1218 unsigned int i;
1219
1220 i = 0;
1221 while (long_options[i].name)
1222 i++;
1223
1224 o = &options[0];
1225 while (o->name) {
1226 long_options[i].name = o->name;
1227 long_options[i].val = FIO_GETOPT_JOB;
1228 if (o->type == FIO_OPT_STR_SET)
1229 long_options[i].has_arg = no_argument;
1230 else
1231 long_options[i].has_arg = required_argument;
1232
1233 i++;
1234 o++;
1235 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1236 }
1237}
1238
Jens Axboeebac4652005-12-08 15:25:21 +01001239int parse_options(int argc, char *argv[])
1240{
Jens Axboe972cfd22006-06-09 11:08:56 +02001241 int job_files, i;
1242
Jens Axboeb4692822006-10-27 13:43:22 +02001243 f_out = stdout;
1244 f_err = stderr;
1245
1246 dupe_job_options();
1247
Jens Axboeebac4652005-12-08 15:25:21 +01001248 if (setup_thread_area())
1249 return 1;
1250 if (fill_def_thread())
1251 return 1;
1252
Jens Axboe972cfd22006-06-09 11:08:56 +02001253 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001254
Jens Axboe972cfd22006-06-09 11:08:56 +02001255 for (i = 0; i < job_files; i++) {
1256 if (fill_def_thread())
1257 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001258 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001259 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001260 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001261 }
Jens Axboeebac4652005-12-08 15:25:21 +01001262
Jens Axboe88c6ed82006-06-09 11:28:10 +02001263 free(ini_file);
Jens Axboeb4692822006-10-27 13:43:22 +02001264
1265 if (!thread_number) {
1266 log_err("No jobs defined(s)\n");
Jens Axboeb4692822006-10-27 13:43:22 +02001267 return 1;
1268 }
1269
Jens Axboeebac4652005-12-08 15:25:21 +01001270 return 0;
1271}