blob: 925da6aea27519abe5bcd78c9e45324fe5c07e00 [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 Axboe56bb17f2006-12-20 20:27:36 +010055#define DEF_HUGEPAGE_SIZE FIO_HUGE_PAGE
Jens Axboeebac4652005-12-08 15:25:21 +010056
Jens Axboee1f36502006-10-27 10:54:08 +020057#define td_var_offset(var) ((size_t) &((struct thread_data *)0)->var)
58
Jens Axboeb4692822006-10-27 13:43:22 +020059static int str_rw_cb(void *, const char *);
60static int str_ioengine_cb(void *, const char *);
61static int str_mem_cb(void *, const char *);
62static int str_verify_cb(void *, const char *);
Jens Axboee1f36502006-10-27 10:54:08 +020063static int str_lockmem_cb(void *, unsigned long *);
Jens Axboe34cfcda2006-11-03 14:00:45 +010064#ifdef FIO_HAVE_IOPRIO
Jens Axboee1f36502006-10-27 10:54:08 +020065static int str_prio_cb(void *, unsigned int *);
66static int str_prioclass_cb(void *, unsigned int *);
Jens Axboe34cfcda2006-11-03 14:00:45 +010067#endif
Jens Axboee1f36502006-10-27 10:54:08 +020068static int str_exitall_cb(void);
69static int str_cpumask_cb(void *, unsigned int *);
70
71/*
72 * Map of job/command line options
73 */
74static struct fio_option options[] = {
75 {
76 .name = "name",
77 .type = FIO_OPT_STR_STORE,
78 .off1 = td_var_offset(name),
79 },
80 {
81 .name = "directory",
82 .type = FIO_OPT_STR_STORE,
83 .off1 = td_var_offset(directory),
84 },
85 {
86 .name = "filename",
87 .type = FIO_OPT_STR_STORE,
88 .off1 = td_var_offset(filename),
89 },
90 {
91 .name = "rw",
92 .type = FIO_OPT_STR,
93 .cb = str_rw_cb,
94 },
95 {
96 .name = "ioengine",
97 .type = FIO_OPT_STR,
98 .cb = str_ioengine_cb,
99 },
100 {
101 .name = "mem",
102 .type = FIO_OPT_STR,
103 .cb = str_mem_cb,
104 },
105 {
106 .name = "verify",
107 .type = FIO_OPT_STR,
108 .cb = str_verify_cb,
109 },
110 {
111 .name = "write_iolog",
Jens Axboe076efc72006-10-27 11:24:25 +0200112 .type = FIO_OPT_STR_STORE,
113 .off1 = td_var_offset(write_iolog_file),
Jens Axboee1f36502006-10-27 10:54:08 +0200114 },
115 {
Jens Axboe076efc72006-10-27 11:24:25 +0200116 .name = "read_iolog",
Jens Axboee1f36502006-10-27 10:54:08 +0200117 .type = FIO_OPT_STR_STORE,
Jens Axboe076efc72006-10-27 11:24:25 +0200118 .off1 = td_var_offset(read_iolog_file),
Jens Axboee1f36502006-10-27 10:54:08 +0200119 },
120 {
121 .name = "exec_prerun",
122 .type = FIO_OPT_STR_STORE,
123 .off1 = td_var_offset(exec_prerun),
124 },
125 {
126 .name = "exec_postrun",
127 .type = FIO_OPT_STR_STORE,
128 .off1 = td_var_offset(exec_postrun),
129 },
130#ifdef FIO_HAVE_IOSCHED_SWITCH
131 {
132 .name = "ioscheduler",
133 .type = FIO_OPT_STR_STORE,
134 .off1 = td_var_offset(ioscheduler),
135 },
136#endif
137 {
138 .name = "size",
139 .type = FIO_OPT_STR_VAL,
140 .off1 = td_var_offset(total_file_size),
141 },
142 {
143 .name = "bs",
Jens Axboe75e6f362006-11-03 08:17:09 +0100144 .type = FIO_OPT_STR_VAL_INT,
Jens Axboea00735e2006-11-03 08:58:08 +0100145 .off1 = td_var_offset(bs[DDIR_READ]),
Jens Axboef90eff52006-11-06 11:08:21 +0100146 .off2 = td_var_offset(bs[DDIR_WRITE]),
Jens Axboee1f36502006-10-27 10:54:08 +0200147 },
148 {
149 .name = "offset",
150 .type = FIO_OPT_STR_VAL,
151 .off1 = td_var_offset(start_offset),
152 },
153 {
154 .name = "zonesize",
155 .type = FIO_OPT_STR_VAL,
156 .off1 = td_var_offset(zone_size),
157 },
158 {
159 .name = "zoneskip",
160 .type = FIO_OPT_STR_VAL,
161 .off1 = td_var_offset(zone_skip),
162 },
163 {
164 .name = "lockmem",
165 .type = FIO_OPT_STR_VAL,
166 .cb = str_lockmem_cb,
167 },
168 {
169 .name = "bsrange",
170 .type = FIO_OPT_RANGE,
Jens Axboea00735e2006-11-03 08:58:08 +0100171 .off1 = td_var_offset(min_bs[DDIR_READ]),
172 .off2 = td_var_offset(max_bs[DDIR_READ]),
Jens Axboef90eff52006-11-06 11:08:21 +0100173 .off3 = td_var_offset(min_bs[DDIR_WRITE]),
174 .off4 = td_var_offset(max_bs[DDIR_WRITE]),
Jens Axboee1f36502006-10-27 10:54:08 +0200175 },
176 {
177 .name = "nrfiles",
178 .type = FIO_OPT_INT,
179 .off1 = td_var_offset(nr_files),
180 },
181 {
182 .name = "iodepth",
183 .type = FIO_OPT_INT,
184 .off1 = td_var_offset(iodepth),
185 },
186 {
187 .name = "fsync",
188 .type = FIO_OPT_INT,
189 .off1 = td_var_offset(fsync_blocks),
190 },
191 {
192 .name = "rwmixcycle",
193 .type = FIO_OPT_INT,
194 .off1 = td_var_offset(rwmixcycle),
195 },
196 {
197 .name = "rwmixread",
198 .type = FIO_OPT_INT,
199 .off1 = td_var_offset(rwmixread),
200 .max_val= 100,
201 },
202 {
203 .name = "rwmixwrite",
204 .type = FIO_OPT_INT,
205 .off1 = td_var_offset(rwmixwrite),
206 .max_val= 100,
207 },
208 {
209 .name = "nice",
210 .type = FIO_OPT_INT,
211 .off1 = td_var_offset(nice),
212 },
213#ifdef FIO_HAVE_IOPRIO
214 {
215 .name = "prio",
216 .type = FIO_OPT_INT,
217 .cb = str_prio_cb,
218 },
219 {
220 .name = "prioclass",
221 .type = FIO_OPT_INT,
222 .cb = str_prioclass_cb,
223 },
224#endif
225 {
226 .name = "thinktime",
227 .type = FIO_OPT_INT,
228 .off1 = td_var_offset(thinktime)
229 },
230 {
231 .name = "rate",
232 .type = FIO_OPT_INT,
233 .off1 = td_var_offset(rate)
234 },
235 {
236 .name = "ratemin",
237 .type = FIO_OPT_INT,
238 .off1 = td_var_offset(ratemin)
239 },
240 {
241 .name = "ratecycle",
242 .type = FIO_OPT_INT,
243 .off1 = td_var_offset(ratecycle)
244 },
245 {
246 .name = "startdelay",
247 .type = FIO_OPT_INT,
248 .off1 = td_var_offset(start_delay)
249 },
250 {
251 .name = "timeout",
252 .type = FIO_OPT_STR_VAL_TIME,
253 .off1 = td_var_offset(timeout)
254 },
255 {
256 .name = "invalidate",
257 .type = FIO_OPT_INT,
258 .off1 = td_var_offset(invalidate_cache)
259 },
260 {
261 .name = "sync",
262 .type = FIO_OPT_INT,
263 .off1 = td_var_offset(sync_io)
264 },
265 {
266 .name = "bwavgtime",
267 .type = FIO_OPT_INT,
268 .off1 = td_var_offset(bw_avg_time)
269 },
270 {
271 .name = "create_serialize",
272 .type = FIO_OPT_INT,
273 .off1 = td_var_offset(create_serialize)
274 },
275 {
276 .name = "create_fsync",
277 .type = FIO_OPT_INT,
278 .off1 = td_var_offset(create_fsync)
279 },
280 {
281 .name = "loops",
282 .type = FIO_OPT_INT,
283 .off1 = td_var_offset(loops)
284 },
285 {
286 .name = "numjobs",
287 .type = FIO_OPT_INT,
288 .off1 = td_var_offset(numjobs)
289 },
290 {
291 .name = "cpuload",
292 .type = FIO_OPT_INT,
293 .off1 = td_var_offset(cpuload)
294 },
295 {
296 .name = "cpuchunks",
297 .type = FIO_OPT_INT,
298 .off1 = td_var_offset(cpucycle)
299 },
300 {
301 .name = "direct",
302 .type = FIO_OPT_INT,
303 .off1 = td_var_offset(odirect)
304 },
305 {
306 .name = "overwrite",
307 .type = FIO_OPT_INT,
308 .off1 = td_var_offset(overwrite)
309 },
310#ifdef FIO_HAVE_CPU_AFFINITY
311 {
312 .name = "cpumask",
313 .type = FIO_OPT_INT,
314 .cb = str_cpumask_cb,
315 },
316#endif
317 {
318 .name = "end_fsync",
319 .type = FIO_OPT_INT,
320 .off1 = td_var_offset(end_fsync)
321 },
322 {
323 .name = "unlink",
324 .type = FIO_OPT_STR_SET,
325 .off1 = td_var_offset(unlink),
326 },
327 {
328 .name = "exitall",
329 .type = FIO_OPT_STR_SET,
330 .cb = str_exitall_cb,
331 },
332 {
333 .name = "stonewall",
334 .type = FIO_OPT_STR_SET,
335 .off1 = td_var_offset(stonewall),
336 },
337 {
338 .name = "thread",
339 .type = FIO_OPT_STR_SET,
340 .off1 = td_var_offset(thread),
341 },
342 {
343 .name = "write_bw_log",
344 .type = FIO_OPT_STR_SET,
345 .off1 = td_var_offset(write_bw_log),
346 },
347 {
348 .name = "write_lat_log",
349 .type = FIO_OPT_STR_SET,
350 .off1 = td_var_offset(write_lat_log),
351 },
352 {
Jens Axboebb8895e2006-10-30 15:14:48 +0100353 .name = "norandommap",
354 .type = FIO_OPT_STR_SET,
355 .off1 = td_var_offset(norandommap),
356 },
357 {
Jens Axboe690adba2006-10-30 15:25:09 +0100358 .name = "bs_unaligned",
359 .type = FIO_OPT_STR_SET,
360 .off1 = td_var_offset(bs_unaligned),
361 },
362 {
Jens Axboe56bb17f2006-12-20 20:27:36 +0100363 .name = "hugepage-size",
364 .type = FIO_OPT_STR_VAL,
365 .off1 = td_var_offset(hugepage_size),
366 },
367 {
Jens Axboee1f36502006-10-27 10:54:08 +0200368 .name = NULL,
369 },
370};
371
Jens Axboeb4692822006-10-27 13:43:22 +0200372#define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
373#define FIO_CMD_OPTS (16)
374#define FIO_GETOPT_JOB (0x89988998)
375
376/*
377 * Command line options. These will contain the above, plus a few
378 * extra that only pertain to fio itself and not jobs.
379 */
380static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
381 {
382 .name = "output",
383 .has_arg = required_argument,
384 .val = 'o',
385 },
386 {
387 .name = "timeout",
388 .has_arg = required_argument,
389 .val = 't',
390 },
391 {
392 .name = "latency-log",
393 .has_arg = required_argument,
394 .val = 'l',
395 },
396 {
397 .name = "bandwidth-log",
398 .has_arg = required_argument,
399 .val = 'b',
400 },
401 {
402 .name = "minimal",
403 .has_arg = optional_argument,
404 .val = 'm',
405 },
406 {
407 .name = "version",
408 .has_arg = no_argument,
409 .val = 'v',
410 },
411 {
412 .name = NULL,
413 },
414};
415
Jens Axboe972cfd22006-06-09 11:08:56 +0200416static int def_timeout = DEF_TIMEOUT;
Jens Axboe972cfd22006-06-09 11:08:56 +0200417
Jens Axboebe67e2d2006-11-07 14:33:15 +0100418static char fio_version_string[] = "fio 1.9";
Jens Axboeebac4652005-12-08 15:25:21 +0100419
Jens Axboe972cfd22006-06-09 11:08:56 +0200420static char **ini_file;
Jens Axboeebac4652005-12-08 15:25:21 +0100421static int max_jobs = MAX_JOBS;
422
423struct thread_data def_thread;
424struct thread_data *threads = NULL;
425
Jens Axboeebac4652005-12-08 15:25:21 +0100426int exitall_on_terminate = 0;
Jens Axboec6ae0a52006-06-12 11:04:44 +0200427int terse_output = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +0200428unsigned long long mlock_size = 0;
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200429FILE *f_out = NULL;
430FILE *f_err = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +0100431
Jens Axboeec94ec52006-10-20 10:59:19 +0200432static int write_lat_log = DEF_WRITE_LAT_LOG;
433static int write_bw_log = DEF_WRITE_BW_LOG;
434
Jens Axboe906c8d72006-06-13 09:37:56 +0200435/*
436 * Return a free job structure.
437 */
Jens Axboeebac4652005-12-08 15:25:21 +0100438static struct thread_data *get_new_job(int global, struct thread_data *parent)
439{
440 struct thread_data *td;
441
442 if (global)
443 return &def_thread;
444 if (thread_number >= max_jobs)
445 return NULL;
446
447 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200448 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100449
Jens Axboeebac4652005-12-08 15:25:21 +0100450 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100451 return td;
452}
453
454static void put_job(struct thread_data *td)
455{
Jens Axboe549577a2006-10-30 12:23:41 +0100456 if (td == &def_thread)
457 return;
458
Jens Axboeebac4652005-12-08 15:25:21 +0100459 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
460 thread_number--;
461}
462
Jens Axboedad915e2006-10-27 11:10:18 +0200463/*
464 * Lazy way of fixing up options that depend on each other. We could also
465 * define option callback handlers, but this is easier.
466 */
Jens Axboee1f36502006-10-27 10:54:08 +0200467static void fixup_options(struct thread_data *td)
468{
Jens Axboee1f36502006-10-27 10:54:08 +0200469 if (!td->rwmixread && td->rwmixwrite)
470 td->rwmixread = 100 - td->rwmixwrite;
Jens Axboedad915e2006-10-27 11:10:18 +0200471
Jens Axboe076efc72006-10-27 11:24:25 +0200472 if (td->write_iolog_file && td->read_iolog_file) {
473 log_err("fio: read iolog overrides write_iolog\n");
474 free(td->write_iolog_file);
475 td->write_iolog_file = NULL;
476 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100477
478 if (td->io_ops->flags & FIO_SYNCIO)
479 td->iodepth = 1;
480 else {
481 if (!td->iodepth)
482 td->iodepth = td->nr_files;
483 }
484
485 /*
486 * only really works for sequential io for now, and with 1 file
487 */
488 if (td->zone_size && !td->sequential && td->nr_files == 1)
489 td->zone_size = 0;
490
491 /*
492 * Reads can do overwrites, we always need to pre-create the file
493 */
494 if (td_read(td) || td_rw(td))
495 td->overwrite = 1;
496
Jens Axboea00735e2006-11-03 08:58:08 +0100497 if (!td->min_bs[DDIR_READ])
498 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
499 if (!td->max_bs[DDIR_READ])
500 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
501 if (!td->min_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100502 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100503 if (!td->max_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100504 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100505
506 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
507
Jens Axboe16b462a2006-10-30 12:35:18 +0100508 if (td_read(td) && !td_rw(td))
509 td->verify = 0;
Jens Axboebb8895e2006-10-30 15:14:48 +0100510
511 if (td->norandommap && td->verify != VERIFY_NONE) {
512 log_err("fio: norandommap given, verify disabled\n");
513 td->verify = VERIFY_NONE;
514 }
Jens Axboe690adba2006-10-30 15:25:09 +0100515 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
516 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100517
518 /*
519 * O_DIRECT and char doesn't mix, clear that flag if necessary.
520 */
521 if (td->filetype == FIO_TYPE_CHAR && td->odirect)
522 td->odirect = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200523}
524
Jens Axboe906c8d72006-06-13 09:37:56 +0200525/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100526 * This function leaks the buffer
527 */
528static char *to_kmg(unsigned int val)
529{
530 char *buf = malloc(32);
Jens Axboe245142f2006-11-08 12:49:21 +0100531 char post[] = { 0, 'K', 'M', 'G', 'P', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100532 char *p = post;
533
Jens Axboe245142f2006-11-08 12:49:21 +0100534 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100535 if (val & 1023)
536 break;
537
538 val >>= 10;
539 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100540 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100541
542 snprintf(buf, 31, "%u%c", val, *p);
543 return buf;
544}
545
546/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200547 * Adds a job to the list of things todo. Sanitizes the various options
548 * to make sure we don't have conflicts, and initializes various
549 * members of td.
550 */
Jens Axboe75154842006-06-01 13:56:09 +0200551static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100552{
Jens Axboe3c9b60c2006-11-07 08:36:14 +0100553 const char *ddir_str[] = { "read", "write", "randread", "randwrite",
554 "rw", NULL, "randrw" };
Jens Axboeebac4652005-12-08 15:25:21 +0100555 struct stat sb;
Jens Axboe53cdc682006-10-18 11:50:58 +0200556 int numjobs, ddir, i;
557 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100558
Jens Axboeebac4652005-12-08 15:25:21 +0100559 /*
560 * the def_thread is just for options, it's not a real job
561 */
562 if (td == &def_thread)
563 return 0;
564
Jens Axboedf641192006-10-12 07:55:41 +0200565 /*
566 * Set default io engine, if none set
567 */
568 if (!td->io_ops) {
569 td->io_ops = load_ioengine(td, DEF_IO_ENGINE_NAME);
570 if (!td->io_ops) {
571 log_err("default engine %s not there?\n", DEF_IO_ENGINE_NAME);
572 return 1;
573 }
574 }
575
Jens Axboe690adba2006-10-30 15:25:09 +0100576 if (td->odirect)
577 td->io_ops->flags |= FIO_RAWIO;
578
Jens Axboeebac4652005-12-08 15:25:21 +0100579 td->filetype = FIO_TYPE_FILE;
Jens Axboe0af7b542006-02-17 10:10:12 +0100580 if (!stat(jobname, &sb)) {
581 if (S_ISBLK(sb.st_mode))
582 td->filetype = FIO_TYPE_BD;
583 else if (S_ISCHR(sb.st_mode))
584 td->filetype = FIO_TYPE_CHAR;
585 }
Jens Axboeebac4652005-12-08 15:25:21 +0100586
Jens Axboee0a22332006-12-20 12:54:25 +0100587 fixup_options(td);
588
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200589 if (td->filename)
590 td->nr_uniq_files = 1;
591 else
592 td->nr_uniq_files = td->nr_files;
593
594 if (td->filetype == FIO_TYPE_FILE || td->filename) {
Jens Axboee9c047a2006-06-07 21:13:04 +0200595 char tmp[PATH_MAX];
Jens Axboe53cdc682006-10-18 11:50:58 +0200596 int len = 0;
Jens Axboee9c047a2006-06-07 21:13:04 +0200597
Jens Axboeef899b62006-06-06 09:31:00 +0200598 if (td->directory && td->directory[0] != '\0')
Jens Axboe53cdc682006-10-18 11:50:58 +0200599 sprintf(tmp, "%s/", td->directory);
Jens Axboeebac4652005-12-08 15:25:21 +0100600
Jens Axboe53cdc682006-10-18 11:50:58 +0200601 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
602
603 for_each_file(td, f, i) {
604 memset(f, 0, sizeof(*f));
605 f->fd = -1;
606
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200607 if (td->filename)
608 sprintf(tmp + len, "%s", td->filename);
609 else
610 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
Jens Axboe53cdc682006-10-18 11:50:58 +0200611 f->file_name = strdup(tmp);
612 }
613 } else {
614 td->nr_files = 1;
615 td->files = malloc(sizeof(struct fio_file));
616 f = &td->files[0];
617
618 memset(f, 0, sizeof(*f));
619 f->fd = -1;
620 f->file_name = strdup(jobname);
621 }
622
623 for_each_file(td, f, i) {
624 f->file_size = td->total_file_size / td->nr_files;
625 f->file_offset = td->start_offset;
626 }
627
Jens Axboebbfd6b02006-06-07 19:42:54 +0200628 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100629
630 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
631 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
632 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
633
Jens Axboeebac4652005-12-08 15:25:21 +0100634 if (td->stonewall && td->thread_number > 1)
635 groupid++;
636
637 td->groupid = groupid;
638
639 if (setup_rate(td))
640 goto err;
641
Jens Axboeec94ec52006-10-20 10:59:19 +0200642 if (td->write_lat_log) {
Jens Axboeebac4652005-12-08 15:25:21 +0100643 setup_log(&td->slat_log);
644 setup_log(&td->clat_log);
645 }
Jens Axboeec94ec52006-10-20 10:59:19 +0200646 if (td->write_bw_log)
Jens Axboeebac4652005-12-08 15:25:21 +0100647 setup_log(&td->bw_log);
648
Jens Axboeb4692822006-10-27 13:43:22 +0200649 if (!td->name)
650 td->name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200651
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200652 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
Jens Axboe75154842006-06-01 13:56:09 +0200653
Jens Axboec6ae0a52006-06-12 11:04:44 +0200654 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200655 if (!job_add_num) {
Jens Axboe2866c822006-10-09 15:57:48 +0200656 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200657 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
Jens Axboef8977ee2006-11-06 14:03:03 +0100658 else {
659 char *c1, *c2, *c3, *c4;
660
661 c1 = to_kmg(td->min_bs[DDIR_READ]);
662 c2 = to_kmg(td->max_bs[DDIR_READ]);
663 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
664 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
665
Jens Axboe1e97cce2006-12-05 11:44:16 +0100666 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 +0100667
668 free(c1);
669 free(c2);
670 free(c3);
671 free(c4);
672 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200673 } else if (job_add_num == 1)
Jens Axboec6ae0a52006-06-12 11:04:44 +0200674 fprintf(f_out, "...\n");
675 }
Jens Axboeebac4652005-12-08 15:25:21 +0100676
677 /*
678 * recurse add identical jobs, clear numjobs and stonewall options
679 * as they don't apply to sub-jobs
680 */
681 numjobs = td->numjobs;
682 while (--numjobs) {
683 struct thread_data *td_new = get_new_job(0, td);
684
685 if (!td_new)
686 goto err;
687
688 td_new->numjobs = 1;
689 td_new->stonewall = 0;
Jens Axboe75154842006-06-01 13:56:09 +0200690 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100691
Jens Axboe75154842006-06-01 13:56:09 +0200692 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100693 goto err;
694 }
695 return 0;
696err:
697 put_job(td);
698 return -1;
699}
700
Jens Axboe906c8d72006-06-13 09:37:56 +0200701/*
702 * Initialize the various random states we need (random io, block size ranges,
703 * read/write mix, etc).
704 */
Jens Axboeebac4652005-12-08 15:25:21 +0100705int init_random_state(struct thread_data *td)
706{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200707 unsigned long seeds[4];
Jens Axboe53cdc682006-10-18 11:50:58 +0200708 int fd, num_maps, blocks, i;
Jens Axboe0ab8db82006-10-18 17:16:23 +0200709 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100710
Jens Axboef48b4672006-10-27 11:30:07 +0200711 if (td->io_ops->flags & FIO_CPUIO)
712 return 0;
713
Jens Axboe1ac267b2006-05-31 20:29:00 +0200714 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100715 if (fd == -1) {
716 td_verror(td, errno);
717 return 1;
718 }
719
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200720 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100721 td_verror(td, EIO);
722 close(fd);
723 return 1;
724 }
725
726 close(fd);
727
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200728 os_random_seed(seeds[0], &td->bsrange_state);
729 os_random_seed(seeds[1], &td->verify_state);
730 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100731
732 if (td->sequential)
733 return 0;
734
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200735 if (td->rand_repeatable)
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200736 seeds[3] = DEF_RANDSEED;
Jens Axboeebac4652005-12-08 15:25:21 +0100737
Jens Axboebb8895e2006-10-30 15:14:48 +0100738 if (!td->norandommap) {
739 for_each_file(td, f, i) {
Jens Axboea00735e2006-11-03 08:58:08 +0100740 blocks = (f->file_size + td->rw_min_bs - 1) / td->rw_min_bs;
Jens Axboec7c280e2006-11-02 20:10:33 +0100741 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
Jens Axboebb8895e2006-10-30 15:14:48 +0100742 f->file_map = malloc(num_maps * sizeof(long));
743 f->num_maps = num_maps;
744 memset(f->file_map, 0, num_maps * sizeof(long));
745 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200746 }
Jens Axboeebac4652005-12-08 15:25:21 +0100747
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200748 os_random_seed(seeds[3], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100749 return 0;
750}
751
752static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
753{
754#ifdef FIO_HAVE_CPU_AFFINITY
755 unsigned int i;
756
757 CPU_ZERO(&cpumask);
758
759 for (i = 0; i < sizeof(int) * 8; i++) {
760 if ((1 << i) & cpu)
761 CPU_SET(i, &cpumask);
762 }
763#endif
764}
765
Jens Axboeebac4652005-12-08 15:25:21 +0100766static int is_empty_or_comment(char *line)
767{
768 unsigned int i;
769
770 for (i = 0; i < strlen(line); i++) {
771 if (line[i] == ';')
772 return 1;
773 if (!isspace(line[i]) && !iscntrl(line[i]))
774 return 0;
775 }
776
777 return 1;
778}
779
Jens Axboeb4692822006-10-27 13:43:22 +0200780static int str_rw_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100781{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200782 struct thread_data *td = data;
783
Jens Axboeebac4652005-12-08 15:25:21 +0100784 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
785 td->ddir = DDIR_READ;
786 td->sequential = 1;
787 return 0;
788 } else if (!strncmp(mem, "randread", 8)) {
789 td->ddir = DDIR_READ;
790 td->sequential = 0;
791 return 0;
792 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
793 td->ddir = DDIR_WRITE;
794 td->sequential = 1;
795 return 0;
796 } else if (!strncmp(mem, "randwrite", 9)) {
797 td->ddir = DDIR_WRITE;
798 td->sequential = 0;
799 return 0;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200800 } else if (!strncmp(mem, "rw", 2)) {
Jens Axboe1e97cce2006-12-05 11:44:16 +0100801 td->ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200802 td->iomix = 1;
803 td->sequential = 1;
804 return 0;
805 } else if (!strncmp(mem, "randrw", 6)) {
Jens Axboe1e97cce2006-12-05 11:44:16 +0100806 td->ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200807 td->iomix = 1;
808 td->sequential = 0;
809 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100810 }
811
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200812 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100813 return 1;
814}
815
Jens Axboeb4692822006-10-27 13:43:22 +0200816static int str_verify_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100817{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200818 struct thread_data *td = data;
819
Jens Axboeebac4652005-12-08 15:25:21 +0100820 if (!strncmp(mem, "0", 1)) {
821 td->verify = VERIFY_NONE;
822 return 0;
823 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
824 td->verify = VERIFY_MD5;
825 return 0;
826 } else if (!strncmp(mem, "crc32", 5)) {
827 td->verify = VERIFY_CRC32;
828 return 0;
829 }
830
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200831 log_err("fio: verify types: md5, crc32\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100832 return 1;
833}
834
Jens Axboe313cb202006-12-21 09:50:00 +0100835/*
836 * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
837 */
838static char *get_mmap_file(const char *str)
839{
840 char *p = strstr(str, ":");
841
842 if (!p)
843 return NULL;
844
845 p++;
846 strip_blank_front(&p);
847 strip_blank_end(p);
848 return strdup(p);
849}
850
Jens Axboeb4692822006-10-27 13:43:22 +0200851static int str_mem_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100852{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200853 struct thread_data *td = data;
854
Jens Axboeebac4652005-12-08 15:25:21 +0100855 if (!strncmp(mem, "malloc", 6)) {
856 td->mem_type = MEM_MALLOC;
857 return 0;
Jens Axboed0bdaf42006-12-20 14:40:44 +0100858 } else if (!strncmp(mem, "mmaphuge", 8)) {
859#ifdef FIO_HAVE_HUGETLB
Jens Axboed0bdaf42006-12-20 14:40:44 +0100860 /*
861 * mmaphuge must be appended with the actual file
862 */
Jens Axboe313cb202006-12-21 09:50:00 +0100863 td->mmapfile = get_mmap_file(mem);
864 if (!td->mmapfile) {
Jens Axboed0bdaf42006-12-20 14:40:44 +0100865 log_err("fio: mmaphuge:/path/to/file\n");
866 return 1;
867 }
868
Jens Axboed0bdaf42006-12-20 14:40:44 +0100869 td->mem_type = MEM_MMAPHUGE;
870 return 0;
871#else
872 log_err("fio: mmaphuge not available\n");
873 return 1;
874#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100875 } else if (!strncmp(mem, "mmap", 4)) {
Jens Axboe313cb202006-12-21 09:50:00 +0100876 /*
877 * Check if the user wants file backed memory. It's ok
878 * if there's no file given, we'll just use anon mamp then.
879 */
880 td->mmapfile = get_mmap_file(mem);
Jens Axboeebac4652005-12-08 15:25:21 +0100881 td->mem_type = MEM_MMAP;
882 return 0;
Jens Axboe74b025b2006-12-19 15:18:14 +0100883 } else if (!strncmp(mem, "shmhuge", 7)) {
884#ifdef FIO_HAVE_HUGETLB
885 td->mem_type = MEM_SHMHUGE;
886 return 0;
887#else
888 log_err("fio: shmhuge not available\n");
889 return 1;
890#endif
Jens Axboe0268b8b2006-12-20 12:48:23 +0100891 } else if (!strncmp(mem, "shm", 3)) {
892 td->mem_type = MEM_SHM;
893 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100894 }
895
Jens Axboed0bdaf42006-12-20 14:40:44 +0100896 log_err("fio: mem type: malloc, shm, shmhuge, mmap, mmaphuge\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100897 return 1;
898}
899
Jens Axboeb4692822006-10-27 13:43:22 +0200900static int str_ioengine_cb(void *data, const char *str)
Jens Axboeebac4652005-12-08 15:25:21 +0100901{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200902 struct thread_data *td = data;
903
Jens Axboe2866c822006-10-09 15:57:48 +0200904 td->io_ops = load_ioengine(td, str);
905 if (td->io_ops)
Jens Axboeebac4652005-12-08 15:25:21 +0100906 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100907
Jens Axboe08aae9a2006-11-24 12:43:49 +0100908 log_err("fio: ioengine= libaio, posixaio, sync, mmap, sgio, splice, cpu, null\n");
Jens Axboe5f350952006-11-07 15:20:59 +0100909 log_err("fio: or specify path to dynamic ioengine module\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100910 return 1;
911}
912
Jens Axboee1f36502006-10-27 10:54:08 +0200913static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
914{
915 mlock_size = *val;
916 return 0;
917}
918
Jens Axboe34cfcda2006-11-03 14:00:45 +0100919#ifdef FIO_HAVE_IOPRIO
Jens Axboee1f36502006-10-27 10:54:08 +0200920static int str_prioclass_cb(void *data, unsigned int *val)
921{
922 struct thread_data *td = data;
923
924 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
925 return 0;
926}
927
928static int str_prio_cb(void *data, unsigned int *val)
929{
930 struct thread_data *td = data;
931
932 td->ioprio |= *val;
933 return 0;
934}
Jens Axboe34cfcda2006-11-03 14:00:45 +0100935#endif
Jens Axboee1f36502006-10-27 10:54:08 +0200936
937static int str_exitall_cb(void)
938{
939 exitall_on_terminate = 1;
940 return 0;
941}
942
943static int str_cpumask_cb(void *data, unsigned int *val)
944{
945 struct thread_data *td = data;
946
947 fill_cpu_mask(td->cpumask, *val);
948 return 0;
949}
950
Jens Axboe07261982006-06-07 10:51:12 +0200951/*
952 * This is our [ini] type file parser.
953 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100954static int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100955{
Jens Axboee1f36502006-10-27 10:54:08 +0200956 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +0100957 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +0100958 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +0100959 fpos_t off;
960 FILE *f;
961 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200962 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +0100963
964 f = fopen(file, "r");
965 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200966 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100967 return 1;
968 }
969
970 string = malloc(4096);
971 name = malloc(256);
Jens Axboefee3bb42006-10-30 13:32:08 +0100972 memset(name, 0, 256);
Jens Axboeebac4652005-12-08 15:25:21 +0100973
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200974 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100975 do {
976 p = fgets(string, 4095, f);
977 if (!p)
Jens Axboe45410ac2006-06-07 11:10:39 +0200978 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100979 if (is_empty_or_comment(p))
980 continue;
Jens Axboefee3bb42006-10-30 13:32:08 +0100981 if (sscanf(p, "[%255s]", name) != 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100982 continue;
983
984 global = !strncmp(name, "global", 6);
985
986 name[strlen(name) - 1] = '\0';
987
988 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200989 if (!td) {
990 ret = 1;
991 break;
992 }
Jens Axboeebac4652005-12-08 15:25:21 +0100993
Jens Axboe972cfd22006-06-09 11:08:56 +0200994 /*
995 * Seperate multiple job files by a stonewall
996 */
Jens Axboef9481912006-06-09 11:37:28 +0200997 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +0200998 td->stonewall = stonewall;
999 stonewall = 0;
1000 }
1001
Jens Axboeebac4652005-12-08 15:25:21 +01001002 fgetpos(f, &off);
1003 while ((p = fgets(string, 4096, f)) != NULL) {
1004 if (is_empty_or_comment(p))
1005 continue;
Jens Axboee1f36502006-10-27 10:54:08 +02001006
Jens Axboeb6754f92006-05-30 14:29:32 +02001007 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +01001008
1009 if (p[0] == '[')
1010 break;
1011
Jens Axboe4ae3f762006-05-30 13:35:14 +02001012 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +02001013
Jens Axboee1f36502006-10-27 10:54:08 +02001014 fgetpos(f, &off);
Jens Axboeebac4652005-12-08 15:25:21 +01001015
Jens Axboe45410ac2006-06-07 11:10:39 +02001016 /*
1017 * Don't break here, continue parsing options so we
1018 * dump all the bad ones. Makes trial/error fixups
1019 * easier on the user.
1020 */
Jens Axboe7c124ac2006-10-30 13:36:52 +01001021 ret |= parse_option(p, options, td);
Jens Axboeebac4652005-12-08 15:25:21 +01001022 }
Jens Axboeebac4652005-12-08 15:25:21 +01001023
Jens Axboe45410ac2006-06-07 11:10:39 +02001024 if (!ret) {
1025 fsetpos(f, &off);
1026 ret = add_job(td, name, 0);
Jens Axboeb1508cf2006-11-02 09:12:40 +01001027 } else {
1028 log_err("fio: job %s dropped\n", name);
1029 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +02001030 }
Jens Axboe7c124ac2006-10-30 13:36:52 +01001031 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001032
1033 free(string);
1034 free(name);
1035 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +02001036 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001037}
1038
1039static int fill_def_thread(void)
1040{
1041 memset(&def_thread, 0, sizeof(def_thread));
1042
1043 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1044 perror("sched_getaffinity");
1045 return 1;
1046 }
1047
1048 /*
1049 * fill globals
1050 */
1051 def_thread.ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001052 def_thread.iomix = 0;
Jens Axboea00735e2006-11-03 08:58:08 +01001053 def_thread.bs[DDIR_READ] = DEF_BS;
1054 def_thread.bs[DDIR_WRITE] = DEF_BS;
1055 def_thread.min_bs[DDIR_READ] = def_thread.min_bs[DDIR_WRITE] = 0;
1056 def_thread.max_bs[DDIR_READ] = def_thread.max_bs[DDIR_WRITE] = 0;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001057 def_thread.odirect = DEF_ODIRECT;
Jens Axboeebac4652005-12-08 15:25:21 +01001058 def_thread.ratecycle = DEF_RATE_CYCLE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001059 def_thread.sequential = DEF_SEQUENTIAL;
Jens Axboe972cfd22006-06-09 11:08:56 +02001060 def_thread.timeout = def_timeout;
Jens Axboeebac4652005-12-08 15:25:21 +01001061 def_thread.overwrite = DEF_OVERWRITE;
1062 def_thread.invalidate_cache = DEF_INVALIDATE;
1063 def_thread.sync_io = DEF_SYNCIO;
1064 def_thread.mem_type = MEM_MALLOC;
1065 def_thread.bw_avg_time = DEF_BWAVGTIME;
1066 def_thread.create_serialize = DEF_CREATE_SER;
1067 def_thread.create_fsync = DEF_CREATE_FSYNC;
1068 def_thread.loops = DEF_LOOPS;
1069 def_thread.verify = DEF_VERIFY;
1070 def_thread.stonewall = DEF_STONEWALL;
1071 def_thread.numjobs = DEF_NUMJOBS;
1072 def_thread.use_thread = DEF_USE_THREAD;
Jens Axboea6ccc7b2006-06-02 10:14:15 +02001073 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
1074 def_thread.rwmixread = DEF_RWMIX_READ;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001075 def_thread.nice = DEF_NICE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001076 def_thread.rand_repeatable = DEF_RAND_REPEAT;
Jens Axboe53cdc682006-10-18 11:50:58 +02001077 def_thread.nr_files = DEF_NR_FILES;
Jens Axboef6cbb262006-10-18 14:16:42 +02001078 def_thread.unlink = DEF_UNLINK;
Jens Axboeec94ec52006-10-20 10:59:19 +02001079 def_thread.write_bw_log = write_bw_log;
1080 def_thread.write_lat_log = write_lat_log;
Jens Axboebb8895e2006-10-30 15:14:48 +01001081 def_thread.norandommap = DEF_NO_RAND_MAP;
Jens Axboe56bb17f2006-12-20 20:27:36 +01001082 def_thread.hugepage_size = DEF_HUGEPAGE_SIZE;
Jens Axboeebac4652005-12-08 15:25:21 +01001083#ifdef FIO_HAVE_DISK_UTIL
1084 def_thread.do_disk_util = 1;
1085#endif
1086
1087 return 0;
1088}
1089
Jens Axboe0ab8db82006-10-18 17:16:23 +02001090static void usage(void)
Jens Axboe4785f992006-05-26 03:59:10 +02001091{
1092 printf("%s\n", fio_version_string);
Jens Axboeb4692822006-10-27 13:43:22 +02001093 printf("\t--output\tWrite output to file\n");
1094 printf("\t--timeout\tRuntime in seconds\n");
1095 printf("\t--latency-log\tGenerate per-job latency logs\n");
1096 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1097 printf("\t--minimal\tMinimal (terse) output\n");
1098 printf("\t--version\tPrint version info and exit\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001099}
1100
Jens Axboe972cfd22006-06-09 11:08:56 +02001101static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +01001102{
Jens Axboeb4692822006-10-27 13:43:22 +02001103 struct thread_data *td = NULL;
Jens Axboec2b1e752006-10-30 09:03:13 +01001104 int c, ini_idx = 0, lidx, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001105
Jens Axboeb4692822006-10-27 13:43:22 +02001106 while ((c = getopt_long(argc, argv, "", long_options, &lidx)) != -1) {
Jens Axboeebac4652005-12-08 15:25:21 +01001107 switch (c) {
Jens Axboeb4692822006-10-27 13:43:22 +02001108 case 't':
1109 def_timeout = atoi(optarg);
1110 break;
1111 case 'l':
1112 write_lat_log = 1;
1113 break;
1114 case 'w':
1115 write_bw_log = 1;
1116 break;
1117 case 'o':
1118 f_out = fopen(optarg, "w+");
1119 if (!f_out) {
1120 perror("fopen output");
1121 exit(1);
1122 }
1123 f_err = f_out;
1124 break;
1125 case 'm':
1126 terse_output = 1;
1127 break;
1128 case 'h':
1129 usage();
1130 exit(0);
1131 case 'v':
1132 printf("%s\n", fio_version_string);
1133 exit(0);
1134 case FIO_GETOPT_JOB: {
1135 const char *opt = long_options[lidx].name;
1136 char *val = optarg;
1137
Jens Axboec2b1e752006-10-30 09:03:13 +01001138 if (!strncmp(opt, "name", 4) && td) {
1139 ret = add_job(td, td->name ?: "fio", 0);
1140 if (ret) {
1141 put_job(td);
1142 return 0;
1143 }
1144 td = NULL;
1145 }
Jens Axboeb4692822006-10-27 13:43:22 +02001146 if (!td) {
Jens Axboe38d0adb2006-10-30 12:21:25 +01001147 int global = !strncmp(val, "global", 6);
Jens Axboec2b1e752006-10-30 09:03:13 +01001148
1149 td = get_new_job(global, &def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001150 if (!td)
1151 return 0;
1152 }
Jens Axboe38d0adb2006-10-30 12:21:25 +01001153
Jens Axboeb1508cf2006-11-02 09:12:40 +01001154 ret = parse_cmd_option(opt, val, options, td);
1155 if (ret) {
1156 log_err("fio: job dropped\n");
1157 put_job(td);
1158 td = NULL;
1159 }
Jens Axboeb4692822006-10-27 13:43:22 +02001160 break;
1161 }
1162 default:
1163 printf("optarg <<%s>>\n", argv[optind]);
1164 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001165 }
1166 }
Jens Axboec9fad892006-05-27 17:26:50 +02001167
Jens Axboeb4692822006-10-27 13:43:22 +02001168 if (td) {
Jens Axboec2b1e752006-10-30 09:03:13 +01001169 ret = add_job(td, td->name ?: "fio", 0);
Jens Axboeb4692822006-10-27 13:43:22 +02001170 if (ret)
1171 put_job(td);
Jens Axboe972cfd22006-06-09 11:08:56 +02001172 }
Jens Axboe774a6172006-08-24 08:44:26 +02001173
Jens Axboeb4692822006-10-27 13:43:22 +02001174 while (optind < argc) {
1175 ini_idx++;
1176 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1177 ini_file[ini_idx - 1] = strdup(argv[optind]);
1178 optind++;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001179 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001180
1181 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +01001182}
1183
1184static void free_shm(void)
1185{
1186 struct shmid_ds sbuf;
1187
1188 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001189 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001190 threads = NULL;
1191 shmctl(shm_id, IPC_RMID, &sbuf);
1192 }
1193}
1194
Jens Axboe906c8d72006-06-13 09:37:56 +02001195/*
1196 * The thread area is shared between the main process and the job
1197 * threads/processes. So setup a shared memory segment that will hold
1198 * all the job info.
1199 */
Jens Axboeebac4652005-12-08 15:25:21 +01001200static int setup_thread_area(void)
1201{
1202 /*
1203 * 1024 is too much on some machines, scale max_jobs if
1204 * we get a failure that looks like too large a shm segment
1205 */
1206 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001207 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001208
Jens Axboe906c8d72006-06-13 09:37:56 +02001209 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001210 if (shm_id != -1)
1211 break;
1212 if (errno != EINVAL) {
1213 perror("shmget");
1214 break;
1215 }
1216
1217 max_jobs >>= 1;
1218 } while (max_jobs);
1219
1220 if (shm_id == -1)
1221 return 1;
1222
1223 threads = shmat(shm_id, NULL, 0);
1224 if (threads == (void *) -1) {
1225 perror("shmat");
1226 return 1;
1227 }
1228
1229 atexit(free_shm);
1230 return 0;
1231}
1232
Jens Axboeb4692822006-10-27 13:43:22 +02001233/*
1234 * Copy the fio options into the long options map, so we mirror
1235 * job and cmd line options.
1236 */
1237static void dupe_job_options(void)
1238{
1239 struct fio_option *o;
1240 unsigned int i;
1241
1242 i = 0;
1243 while (long_options[i].name)
1244 i++;
1245
1246 o = &options[0];
1247 while (o->name) {
1248 long_options[i].name = o->name;
1249 long_options[i].val = FIO_GETOPT_JOB;
1250 if (o->type == FIO_OPT_STR_SET)
1251 long_options[i].has_arg = no_argument;
1252 else
1253 long_options[i].has_arg = required_argument;
1254
1255 i++;
1256 o++;
1257 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1258 }
1259}
1260
Jens Axboeebac4652005-12-08 15:25:21 +01001261int parse_options(int argc, char *argv[])
1262{
Jens Axboe972cfd22006-06-09 11:08:56 +02001263 int job_files, i;
1264
Jens Axboeb4692822006-10-27 13:43:22 +02001265 f_out = stdout;
1266 f_err = stderr;
1267
1268 dupe_job_options();
1269
Jens Axboeebac4652005-12-08 15:25:21 +01001270 if (setup_thread_area())
1271 return 1;
1272 if (fill_def_thread())
1273 return 1;
1274
Jens Axboe972cfd22006-06-09 11:08:56 +02001275 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001276
Jens Axboe972cfd22006-06-09 11:08:56 +02001277 for (i = 0; i < job_files; i++) {
1278 if (fill_def_thread())
1279 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001280 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001281 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001282 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001283 }
Jens Axboeebac4652005-12-08 15:25:21 +01001284
Jens Axboe88c6ed82006-06-09 11:28:10 +02001285 free(ini_file);
Jens Axboeb4692822006-10-27 13:43:22 +02001286
1287 if (!thread_number) {
1288 log_err("No jobs defined(s)\n");
Jens Axboeb4692822006-10-27 13:43:22 +02001289 return 1;
1290 }
1291
Jens Axboeebac4652005-12-08 15:25:21 +01001292 return 0;
1293}