blob: 3c27ea55aa09107a9a62595aa658c86ce11871c5 [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)
32#define DEF_OVERWRITE (1)
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 Axboef6cbb262006-10-18 14:16:42 +020051#define DEF_UNLINK (0)
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]),
145 },
146 {
147 .name = "read_bs",
Jens Axboe75e6f362006-11-03 08:17:09 +0100148 .type = FIO_OPT_STR_VAL_INT,
Jens Axboea00735e2006-11-03 08:58:08 +0100149 .off1 = td_var_offset(bs[DDIR_READ]),
150 },
151 {
152 .name = "write_bs",
Jens Axboe75e6f362006-11-03 08:17:09 +0100153 .type = FIO_OPT_STR_VAL_INT,
Jens Axboea00735e2006-11-03 08:58:08 +0100154 .off1 = td_var_offset(bs[DDIR_WRITE]),
Jens Axboee1f36502006-10-27 10:54:08 +0200155 },
156 {
157 .name = "offset",
158 .type = FIO_OPT_STR_VAL,
159 .off1 = td_var_offset(start_offset),
160 },
161 {
162 .name = "zonesize",
163 .type = FIO_OPT_STR_VAL,
164 .off1 = td_var_offset(zone_size),
165 },
166 {
167 .name = "zoneskip",
168 .type = FIO_OPT_STR_VAL,
169 .off1 = td_var_offset(zone_skip),
170 },
171 {
172 .name = "lockmem",
173 .type = FIO_OPT_STR_VAL,
174 .cb = str_lockmem_cb,
175 },
176 {
177 .name = "bsrange",
178 .type = FIO_OPT_RANGE,
Jens Axboea00735e2006-11-03 08:58:08 +0100179 .off1 = td_var_offset(min_bs[DDIR_READ]),
180 .off2 = td_var_offset(max_bs[DDIR_READ]),
181 },
182 {
183 .name = "read_bsrange",
184 .type = FIO_OPT_RANGE,
185 .off1 = td_var_offset(min_bs[DDIR_READ]),
186 .off2 = td_var_offset(max_bs[DDIR_READ]),
187 },
188 {
189 .name = "write_bsrange",
190 .type = FIO_OPT_RANGE,
191 .off1 = td_var_offset(min_bs[DDIR_WRITE]),
192 .off2 = td_var_offset(max_bs[DDIR_WRITE]),
Jens Axboee1f36502006-10-27 10:54:08 +0200193 },
194 {
195 .name = "nrfiles",
196 .type = FIO_OPT_INT,
197 .off1 = td_var_offset(nr_files),
198 },
199 {
200 .name = "iodepth",
201 .type = FIO_OPT_INT,
202 .off1 = td_var_offset(iodepth),
203 },
204 {
205 .name = "fsync",
206 .type = FIO_OPT_INT,
207 .off1 = td_var_offset(fsync_blocks),
208 },
209 {
210 .name = "rwmixcycle",
211 .type = FIO_OPT_INT,
212 .off1 = td_var_offset(rwmixcycle),
213 },
214 {
215 .name = "rwmixread",
216 .type = FIO_OPT_INT,
217 .off1 = td_var_offset(rwmixread),
218 .max_val= 100,
219 },
220 {
221 .name = "rwmixwrite",
222 .type = FIO_OPT_INT,
223 .off1 = td_var_offset(rwmixwrite),
224 .max_val= 100,
225 },
226 {
227 .name = "nice",
228 .type = FIO_OPT_INT,
229 .off1 = td_var_offset(nice),
230 },
231#ifdef FIO_HAVE_IOPRIO
232 {
233 .name = "prio",
234 .type = FIO_OPT_INT,
235 .cb = str_prio_cb,
236 },
237 {
238 .name = "prioclass",
239 .type = FIO_OPT_INT,
240 .cb = str_prioclass_cb,
241 },
242#endif
243 {
244 .name = "thinktime",
245 .type = FIO_OPT_INT,
246 .off1 = td_var_offset(thinktime)
247 },
248 {
249 .name = "rate",
250 .type = FIO_OPT_INT,
251 .off1 = td_var_offset(rate)
252 },
253 {
254 .name = "ratemin",
255 .type = FIO_OPT_INT,
256 .off1 = td_var_offset(ratemin)
257 },
258 {
259 .name = "ratecycle",
260 .type = FIO_OPT_INT,
261 .off1 = td_var_offset(ratecycle)
262 },
263 {
264 .name = "startdelay",
265 .type = FIO_OPT_INT,
266 .off1 = td_var_offset(start_delay)
267 },
268 {
269 .name = "timeout",
270 .type = FIO_OPT_STR_VAL_TIME,
271 .off1 = td_var_offset(timeout)
272 },
273 {
274 .name = "invalidate",
275 .type = FIO_OPT_INT,
276 .off1 = td_var_offset(invalidate_cache)
277 },
278 {
279 .name = "sync",
280 .type = FIO_OPT_INT,
281 .off1 = td_var_offset(sync_io)
282 },
283 {
284 .name = "bwavgtime",
285 .type = FIO_OPT_INT,
286 .off1 = td_var_offset(bw_avg_time)
287 },
288 {
289 .name = "create_serialize",
290 .type = FIO_OPT_INT,
291 .off1 = td_var_offset(create_serialize)
292 },
293 {
294 .name = "create_fsync",
295 .type = FIO_OPT_INT,
296 .off1 = td_var_offset(create_fsync)
297 },
298 {
299 .name = "loops",
300 .type = FIO_OPT_INT,
301 .off1 = td_var_offset(loops)
302 },
303 {
304 .name = "numjobs",
305 .type = FIO_OPT_INT,
306 .off1 = td_var_offset(numjobs)
307 },
308 {
309 .name = "cpuload",
310 .type = FIO_OPT_INT,
311 .off1 = td_var_offset(cpuload)
312 },
313 {
314 .name = "cpuchunks",
315 .type = FIO_OPT_INT,
316 .off1 = td_var_offset(cpucycle)
317 },
318 {
319 .name = "direct",
320 .type = FIO_OPT_INT,
321 .off1 = td_var_offset(odirect)
322 },
323 {
324 .name = "overwrite",
325 .type = FIO_OPT_INT,
326 .off1 = td_var_offset(overwrite)
327 },
328#ifdef FIO_HAVE_CPU_AFFINITY
329 {
330 .name = "cpumask",
331 .type = FIO_OPT_INT,
332 .cb = str_cpumask_cb,
333 },
334#endif
335 {
336 .name = "end_fsync",
337 .type = FIO_OPT_INT,
338 .off1 = td_var_offset(end_fsync)
339 },
340 {
341 .name = "unlink",
342 .type = FIO_OPT_STR_SET,
343 .off1 = td_var_offset(unlink),
344 },
345 {
346 .name = "exitall",
347 .type = FIO_OPT_STR_SET,
348 .cb = str_exitall_cb,
349 },
350 {
351 .name = "stonewall",
352 .type = FIO_OPT_STR_SET,
353 .off1 = td_var_offset(stonewall),
354 },
355 {
356 .name = "thread",
357 .type = FIO_OPT_STR_SET,
358 .off1 = td_var_offset(thread),
359 },
360 {
361 .name = "write_bw_log",
362 .type = FIO_OPT_STR_SET,
363 .off1 = td_var_offset(write_bw_log),
364 },
365 {
366 .name = "write_lat_log",
367 .type = FIO_OPT_STR_SET,
368 .off1 = td_var_offset(write_lat_log),
369 },
370 {
Jens Axboebb8895e2006-10-30 15:14:48 +0100371 .name = "norandommap",
372 .type = FIO_OPT_STR_SET,
373 .off1 = td_var_offset(norandommap),
374 },
375 {
Jens Axboe690adba2006-10-30 15:25:09 +0100376 .name = "bs_unaligned",
377 .type = FIO_OPT_STR_SET,
378 .off1 = td_var_offset(bs_unaligned),
379 },
380 {
Jens Axboee1f36502006-10-27 10:54:08 +0200381 .name = NULL,
382 },
383};
384
Jens Axboeb4692822006-10-27 13:43:22 +0200385#define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
386#define FIO_CMD_OPTS (16)
387#define FIO_GETOPT_JOB (0x89988998)
388
389/*
390 * Command line options. These will contain the above, plus a few
391 * extra that only pertain to fio itself and not jobs.
392 */
393static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
394 {
395 .name = "output",
396 .has_arg = required_argument,
397 .val = 'o',
398 },
399 {
400 .name = "timeout",
401 .has_arg = required_argument,
402 .val = 't',
403 },
404 {
405 .name = "latency-log",
406 .has_arg = required_argument,
407 .val = 'l',
408 },
409 {
410 .name = "bandwidth-log",
411 .has_arg = required_argument,
412 .val = 'b',
413 },
414 {
415 .name = "minimal",
416 .has_arg = optional_argument,
417 .val = 'm',
418 },
419 {
420 .name = "version",
421 .has_arg = no_argument,
422 .val = 'v',
423 },
424 {
425 .name = NULL,
426 },
427};
428
Jens Axboe972cfd22006-06-09 11:08:56 +0200429static int def_timeout = DEF_TIMEOUT;
Jens Axboe972cfd22006-06-09 11:08:56 +0200430
Jens Axboeb4692822006-10-27 13:43:22 +0200431static char fio_version_string[] = "fio 1.7";
Jens Axboeebac4652005-12-08 15:25:21 +0100432
Jens Axboe972cfd22006-06-09 11:08:56 +0200433static char **ini_file;
Jens Axboeebac4652005-12-08 15:25:21 +0100434static int max_jobs = MAX_JOBS;
435
436struct thread_data def_thread;
437struct thread_data *threads = NULL;
438
439int rate_quit = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100440int exitall_on_terminate = 0;
Jens Axboec6ae0a52006-06-12 11:04:44 +0200441int terse_output = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +0200442unsigned long long mlock_size = 0;
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200443FILE *f_out = NULL;
444FILE *f_err = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +0100445
Jens Axboeec94ec52006-10-20 10:59:19 +0200446static int write_lat_log = DEF_WRITE_LAT_LOG;
447static int write_bw_log = DEF_WRITE_BW_LOG;
448
Jens Axboe906c8d72006-06-13 09:37:56 +0200449/*
450 * Return a free job structure.
451 */
Jens Axboeebac4652005-12-08 15:25:21 +0100452static struct thread_data *get_new_job(int global, struct thread_data *parent)
453{
454 struct thread_data *td;
455
456 if (global)
457 return &def_thread;
458 if (thread_number >= max_jobs)
459 return NULL;
460
461 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200462 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100463
Jens Axboeebac4652005-12-08 15:25:21 +0100464 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100465 return td;
466}
467
468static void put_job(struct thread_data *td)
469{
Jens Axboe549577a2006-10-30 12:23:41 +0100470 if (td == &def_thread)
471 return;
472
Jens Axboeebac4652005-12-08 15:25:21 +0100473 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
474 thread_number--;
475}
476
Jens Axboedad915e2006-10-27 11:10:18 +0200477/*
478 * Lazy way of fixing up options that depend on each other. We could also
479 * define option callback handlers, but this is easier.
480 */
Jens Axboee1f36502006-10-27 10:54:08 +0200481static void fixup_options(struct thread_data *td)
482{
Jens Axboee1f36502006-10-27 10:54:08 +0200483 if (!td->rwmixread && td->rwmixwrite)
484 td->rwmixread = 100 - td->rwmixwrite;
Jens Axboedad915e2006-10-27 11:10:18 +0200485
Jens Axboe076efc72006-10-27 11:24:25 +0200486 if (td->write_iolog_file && td->read_iolog_file) {
487 log_err("fio: read iolog overrides write_iolog\n");
488 free(td->write_iolog_file);
489 td->write_iolog_file = NULL;
490 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100491
492 if (td->io_ops->flags & FIO_SYNCIO)
493 td->iodepth = 1;
494 else {
495 if (!td->iodepth)
496 td->iodepth = td->nr_files;
497 }
498
499 /*
500 * only really works for sequential io for now, and with 1 file
501 */
502 if (td->zone_size && !td->sequential && td->nr_files == 1)
503 td->zone_size = 0;
504
505 /*
506 * Reads can do overwrites, we always need to pre-create the file
507 */
508 if (td_read(td) || td_rw(td))
509 td->overwrite = 1;
510
Jens Axboea00735e2006-11-03 08:58:08 +0100511 if (td->bs[DDIR_READ] != DEF_BS)
512 td->bs[DDIR_WRITE] = td->bs[DDIR_READ];
513 if (!td->min_bs[DDIR_READ])
514 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
515 if (!td->max_bs[DDIR_READ])
516 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
517 if (!td->min_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100518 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100519 if (!td->max_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100520 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100521
522 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
523
Jens Axboe16b462a2006-10-30 12:35:18 +0100524 if (td_read(td) && !td_rw(td))
525 td->verify = 0;
Jens Axboebb8895e2006-10-30 15:14:48 +0100526
527 if (td->norandommap && td->verify != VERIFY_NONE) {
528 log_err("fio: norandommap given, verify disabled\n");
529 td->verify = VERIFY_NONE;
530 }
Jens Axboe690adba2006-10-30 15:25:09 +0100531 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
532 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee1f36502006-10-27 10:54:08 +0200533}
534
Jens Axboe906c8d72006-06-13 09:37:56 +0200535/*
536 * Adds a job to the list of things todo. Sanitizes the various options
537 * to make sure we don't have conflicts, and initializes various
538 * members of td.
539 */
Jens Axboe75154842006-06-01 13:56:09 +0200540static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100541{
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200542 char *ddir_str[] = { "read", "write", "randread", "randwrite",
543 "rw", NULL, "randrw" };
Jens Axboeebac4652005-12-08 15:25:21 +0100544 struct stat sb;
Jens Axboe53cdc682006-10-18 11:50:58 +0200545 int numjobs, ddir, i;
546 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100547
Jens Axboeebac4652005-12-08 15:25:21 +0100548 /*
549 * the def_thread is just for options, it's not a real job
550 */
551 if (td == &def_thread)
552 return 0;
553
Jens Axboedf641192006-10-12 07:55:41 +0200554 /*
555 * Set default io engine, if none set
556 */
557 if (!td->io_ops) {
558 td->io_ops = load_ioengine(td, DEF_IO_ENGINE_NAME);
559 if (!td->io_ops) {
560 log_err("default engine %s not there?\n", DEF_IO_ENGINE_NAME);
561 return 1;
562 }
563 }
564
Jens Axboe690adba2006-10-30 15:25:09 +0100565 if (td->odirect)
566 td->io_ops->flags |= FIO_RAWIO;
567
Jens Axboe16b462a2006-10-30 12:35:18 +0100568 fixup_options(td);
Jens Axboe9cc935a2006-06-13 09:11:18 +0200569
Jens Axboeebac4652005-12-08 15:25:21 +0100570 td->filetype = FIO_TYPE_FILE;
Jens Axboe0af7b542006-02-17 10:10:12 +0100571 if (!stat(jobname, &sb)) {
572 if (S_ISBLK(sb.st_mode))
573 td->filetype = FIO_TYPE_BD;
574 else if (S_ISCHR(sb.st_mode))
575 td->filetype = FIO_TYPE_CHAR;
576 }
Jens Axboeebac4652005-12-08 15:25:21 +0100577
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200578 if (td->filename)
579 td->nr_uniq_files = 1;
580 else
581 td->nr_uniq_files = td->nr_files;
582
583 if (td->filetype == FIO_TYPE_FILE || td->filename) {
Jens Axboee9c047a2006-06-07 21:13:04 +0200584 char tmp[PATH_MAX];
Jens Axboe53cdc682006-10-18 11:50:58 +0200585 int len = 0;
586 int i;
Jens Axboee9c047a2006-06-07 21:13:04 +0200587
Jens Axboeef899b62006-06-06 09:31:00 +0200588 if (td->directory && td->directory[0] != '\0')
Jens Axboe53cdc682006-10-18 11:50:58 +0200589 sprintf(tmp, "%s/", td->directory);
Jens Axboeebac4652005-12-08 15:25:21 +0100590
Jens Axboe53cdc682006-10-18 11:50:58 +0200591 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
592
593 for_each_file(td, f, i) {
594 memset(f, 0, sizeof(*f));
595 f->fd = -1;
596
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200597 if (td->filename)
598 sprintf(tmp + len, "%s", td->filename);
599 else
600 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
Jens Axboe53cdc682006-10-18 11:50:58 +0200601 f->file_name = strdup(tmp);
602 }
603 } else {
604 td->nr_files = 1;
605 td->files = malloc(sizeof(struct fio_file));
606 f = &td->files[0];
607
608 memset(f, 0, sizeof(*f));
609 f->fd = -1;
610 f->file_name = strdup(jobname);
611 }
612
613 for_each_file(td, f, i) {
614 f->file_size = td->total_file_size / td->nr_files;
615 f->file_offset = td->start_offset;
616 }
617
Jens Axboebbfd6b02006-06-07 19:42:54 +0200618 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100619
620 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
621 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
622 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
623
Jens Axboeebac4652005-12-08 15:25:21 +0100624 if (td->stonewall && td->thread_number > 1)
625 groupid++;
626
627 td->groupid = groupid;
628
629 if (setup_rate(td))
630 goto err;
631
Jens Axboeec94ec52006-10-20 10:59:19 +0200632 if (td->write_lat_log) {
Jens Axboeebac4652005-12-08 15:25:21 +0100633 setup_log(&td->slat_log);
634 setup_log(&td->clat_log);
635 }
Jens Axboeec94ec52006-10-20 10:59:19 +0200636 if (td->write_bw_log)
Jens Axboeebac4652005-12-08 15:25:21 +0100637 setup_log(&td->bw_log);
638
Jens Axboeb4692822006-10-27 13:43:22 +0200639 if (!td->name)
640 td->name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200641
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200642 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
Jens Axboe75154842006-06-01 13:56:09 +0200643
Jens Axboec6ae0a52006-06-12 11:04:44 +0200644 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200645 if (!job_add_num) {
Jens Axboe2866c822006-10-09 15:57:48 +0200646 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200647 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
648 else
Jens Axboea00735e2006-11-03 08:58:08 +0100649 fprintf(f_out, "%s: (g=%d): rw=%s, odir=%d, bs=%d-%d/%d-%d, rate=%d, ioengine=%s, iodepth=%d\n", td->name, td->groupid, ddir_str[ddir], td->odirect, td->min_bs[DDIR_READ], td->max_bs[DDIR_READ], td->min_bs[DDIR_WRITE], td->max_bs[DDIR_WRITE], td->rate, td->io_ops->name, td->iodepth);
Jens Axboeb990b5c2006-09-14 09:48:22 +0200650 } else if (job_add_num == 1)
Jens Axboec6ae0a52006-06-12 11:04:44 +0200651 fprintf(f_out, "...\n");
652 }
Jens Axboeebac4652005-12-08 15:25:21 +0100653
654 /*
655 * recurse add identical jobs, clear numjobs and stonewall options
656 * as they don't apply to sub-jobs
657 */
658 numjobs = td->numjobs;
659 while (--numjobs) {
660 struct thread_data *td_new = get_new_job(0, td);
661
662 if (!td_new)
663 goto err;
664
665 td_new->numjobs = 1;
666 td_new->stonewall = 0;
Jens Axboe75154842006-06-01 13:56:09 +0200667 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100668
Jens Axboe75154842006-06-01 13:56:09 +0200669 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100670 goto err;
671 }
672 return 0;
673err:
674 put_job(td);
675 return -1;
676}
677
Jens Axboe906c8d72006-06-13 09:37:56 +0200678/*
679 * Initialize the various random states we need (random io, block size ranges,
680 * read/write mix, etc).
681 */
Jens Axboeebac4652005-12-08 15:25:21 +0100682int init_random_state(struct thread_data *td)
683{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200684 unsigned long seeds[4];
Jens Axboe53cdc682006-10-18 11:50:58 +0200685 int fd, num_maps, blocks, i;
Jens Axboe0ab8db82006-10-18 17:16:23 +0200686 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100687
Jens Axboef48b4672006-10-27 11:30:07 +0200688 if (td->io_ops->flags & FIO_CPUIO)
689 return 0;
690
Jens Axboe1ac267b2006-05-31 20:29:00 +0200691 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100692 if (fd == -1) {
693 td_verror(td, errno);
694 return 1;
695 }
696
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200697 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100698 td_verror(td, EIO);
699 close(fd);
700 return 1;
701 }
702
703 close(fd);
704
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200705 os_random_seed(seeds[0], &td->bsrange_state);
706 os_random_seed(seeds[1], &td->verify_state);
707 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100708
709 if (td->sequential)
710 return 0;
711
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200712 if (td->rand_repeatable)
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200713 seeds[3] = DEF_RANDSEED;
Jens Axboeebac4652005-12-08 15:25:21 +0100714
Jens Axboebb8895e2006-10-30 15:14:48 +0100715 if (!td->norandommap) {
716 for_each_file(td, f, i) {
Jens Axboea00735e2006-11-03 08:58:08 +0100717 blocks = (f->file_size + td->rw_min_bs - 1) / td->rw_min_bs;
Jens Axboec7c280e2006-11-02 20:10:33 +0100718 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
Jens Axboebb8895e2006-10-30 15:14:48 +0100719 f->file_map = malloc(num_maps * sizeof(long));
720 f->num_maps = num_maps;
721 memset(f->file_map, 0, num_maps * sizeof(long));
722 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200723 }
Jens Axboeebac4652005-12-08 15:25:21 +0100724
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200725 os_random_seed(seeds[3], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100726 return 0;
727}
728
729static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
730{
731#ifdef FIO_HAVE_CPU_AFFINITY
732 unsigned int i;
733
734 CPU_ZERO(&cpumask);
735
736 for (i = 0; i < sizeof(int) * 8; i++) {
737 if ((1 << i) & cpu)
738 CPU_SET(i, &cpumask);
739 }
740#endif
741}
742
Jens Axboeebac4652005-12-08 15:25:21 +0100743static int is_empty_or_comment(char *line)
744{
745 unsigned int i;
746
747 for (i = 0; i < strlen(line); i++) {
748 if (line[i] == ';')
749 return 1;
750 if (!isspace(line[i]) && !iscntrl(line[i]))
751 return 0;
752 }
753
754 return 1;
755}
756
Jens Axboeb4692822006-10-27 13:43:22 +0200757static int str_rw_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100758{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200759 struct thread_data *td = data;
760
Jens Axboeebac4652005-12-08 15:25:21 +0100761 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
762 td->ddir = DDIR_READ;
763 td->sequential = 1;
764 return 0;
765 } else if (!strncmp(mem, "randread", 8)) {
766 td->ddir = DDIR_READ;
767 td->sequential = 0;
768 return 0;
769 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
770 td->ddir = DDIR_WRITE;
771 td->sequential = 1;
772 return 0;
773 } else if (!strncmp(mem, "randwrite", 9)) {
774 td->ddir = DDIR_WRITE;
775 td->sequential = 0;
776 return 0;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200777 } else if (!strncmp(mem, "rw", 2)) {
778 td->ddir = 0;
779 td->iomix = 1;
780 td->sequential = 1;
781 return 0;
782 } else if (!strncmp(mem, "randrw", 6)) {
783 td->ddir = 0;
784 td->iomix = 1;
785 td->sequential = 0;
786 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100787 }
788
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200789 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100790 return 1;
791}
792
Jens Axboeb4692822006-10-27 13:43:22 +0200793static int str_verify_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100794{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200795 struct thread_data *td = data;
796
Jens Axboeebac4652005-12-08 15:25:21 +0100797 if (!strncmp(mem, "0", 1)) {
798 td->verify = VERIFY_NONE;
799 return 0;
800 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
801 td->verify = VERIFY_MD5;
802 return 0;
803 } else if (!strncmp(mem, "crc32", 5)) {
804 td->verify = VERIFY_CRC32;
805 return 0;
806 }
807
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200808 log_err("fio: verify types: md5, crc32\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100809 return 1;
810}
811
Jens Axboeb4692822006-10-27 13:43:22 +0200812static int str_mem_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100813{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200814 struct thread_data *td = data;
815
Jens Axboeebac4652005-12-08 15:25:21 +0100816 if (!strncmp(mem, "malloc", 6)) {
817 td->mem_type = MEM_MALLOC;
818 return 0;
819 } else if (!strncmp(mem, "shm", 3)) {
820 td->mem_type = MEM_SHM;
821 return 0;
822 } else if (!strncmp(mem, "mmap", 4)) {
823 td->mem_type = MEM_MMAP;
824 return 0;
825 }
826
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200827 log_err("fio: mem type: malloc, shm, mmap\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100828 return 1;
829}
830
Jens Axboeb4692822006-10-27 13:43:22 +0200831static int str_ioengine_cb(void *data, const char *str)
Jens Axboeebac4652005-12-08 15:25:21 +0100832{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200833 struct thread_data *td = data;
834
Jens Axboe2866c822006-10-09 15:57:48 +0200835 td->io_ops = load_ioengine(td, str);
836 if (td->io_ops)
Jens Axboeebac4652005-12-08 15:25:21 +0100837 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100838
Jens Axboeb990b5c2006-09-14 09:48:22 +0200839 log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice, cpu\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100840 return 1;
841}
842
Jens Axboee1f36502006-10-27 10:54:08 +0200843static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
844{
845 mlock_size = *val;
846 return 0;
847}
848
Jens Axboe34cfcda2006-11-03 14:00:45 +0100849#ifdef FIO_HAVE_IOPRIO
Jens Axboee1f36502006-10-27 10:54:08 +0200850static int str_prioclass_cb(void *data, unsigned int *val)
851{
852 struct thread_data *td = data;
853
854 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
855 return 0;
856}
857
858static int str_prio_cb(void *data, unsigned int *val)
859{
860 struct thread_data *td = data;
861
862 td->ioprio |= *val;
863 return 0;
864}
Jens Axboe34cfcda2006-11-03 14:00:45 +0100865#endif
Jens Axboee1f36502006-10-27 10:54:08 +0200866
867static int str_exitall_cb(void)
868{
869 exitall_on_terminate = 1;
870 return 0;
871}
872
873static int str_cpumask_cb(void *data, unsigned int *val)
874{
875 struct thread_data *td = data;
876
877 fill_cpu_mask(td->cpumask, *val);
878 return 0;
879}
880
Jens Axboe07261982006-06-07 10:51:12 +0200881/*
882 * This is our [ini] type file parser.
883 */
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200884int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100885{
Jens Axboee1f36502006-10-27 10:54:08 +0200886 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +0100887 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +0100888 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +0100889 fpos_t off;
890 FILE *f;
891 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200892 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +0100893
894 f = fopen(file, "r");
895 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200896 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100897 return 1;
898 }
899
900 string = malloc(4096);
901 name = malloc(256);
Jens Axboefee3bb42006-10-30 13:32:08 +0100902 memset(name, 0, 256);
Jens Axboeebac4652005-12-08 15:25:21 +0100903
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200904 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100905 do {
906 p = fgets(string, 4095, f);
907 if (!p)
Jens Axboe45410ac2006-06-07 11:10:39 +0200908 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100909 if (is_empty_or_comment(p))
910 continue;
Jens Axboefee3bb42006-10-30 13:32:08 +0100911 if (sscanf(p, "[%255s]", name) != 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100912 continue;
913
914 global = !strncmp(name, "global", 6);
915
916 name[strlen(name) - 1] = '\0';
917
918 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200919 if (!td) {
920 ret = 1;
921 break;
922 }
Jens Axboeebac4652005-12-08 15:25:21 +0100923
Jens Axboe972cfd22006-06-09 11:08:56 +0200924 /*
925 * Seperate multiple job files by a stonewall
926 */
Jens Axboef9481912006-06-09 11:37:28 +0200927 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +0200928 td->stonewall = stonewall;
929 stonewall = 0;
930 }
931
Jens Axboeebac4652005-12-08 15:25:21 +0100932 fgetpos(f, &off);
933 while ((p = fgets(string, 4096, f)) != NULL) {
934 if (is_empty_or_comment(p))
935 continue;
Jens Axboee1f36502006-10-27 10:54:08 +0200936
Jens Axboeb6754f92006-05-30 14:29:32 +0200937 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +0100938
939 if (p[0] == '[')
940 break;
941
Jens Axboe4ae3f762006-05-30 13:35:14 +0200942 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200943
Jens Axboee1f36502006-10-27 10:54:08 +0200944 fgetpos(f, &off);
Jens Axboeebac4652005-12-08 15:25:21 +0100945
Jens Axboe45410ac2006-06-07 11:10:39 +0200946 /*
947 * Don't break here, continue parsing options so we
948 * dump all the bad ones. Makes trial/error fixups
949 * easier on the user.
950 */
Jens Axboe7c124ac2006-10-30 13:36:52 +0100951 ret |= parse_option(p, options, td);
Jens Axboeebac4652005-12-08 15:25:21 +0100952 }
Jens Axboeebac4652005-12-08 15:25:21 +0100953
Jens Axboe45410ac2006-06-07 11:10:39 +0200954 if (!ret) {
955 fsetpos(f, &off);
956 ret = add_job(td, name, 0);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100957 } else {
958 log_err("fio: job %s dropped\n", name);
959 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +0200960 }
Jens Axboe7c124ac2006-10-30 13:36:52 +0100961 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +0100962
963 free(string);
964 free(name);
965 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200966 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100967}
968
969static int fill_def_thread(void)
970{
971 memset(&def_thread, 0, sizeof(def_thread));
972
973 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
974 perror("sched_getaffinity");
975 return 1;
976 }
977
978 /*
979 * fill globals
980 */
981 def_thread.ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200982 def_thread.iomix = 0;
Jens Axboea00735e2006-11-03 08:58:08 +0100983 def_thread.bs[DDIR_READ] = DEF_BS;
984 def_thread.bs[DDIR_WRITE] = DEF_BS;
985 def_thread.min_bs[DDIR_READ] = def_thread.min_bs[DDIR_WRITE] = 0;
986 def_thread.max_bs[DDIR_READ] = def_thread.max_bs[DDIR_WRITE] = 0;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200987 def_thread.odirect = DEF_ODIRECT;
Jens Axboeebac4652005-12-08 15:25:21 +0100988 def_thread.ratecycle = DEF_RATE_CYCLE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200989 def_thread.sequential = DEF_SEQUENTIAL;
Jens Axboe972cfd22006-06-09 11:08:56 +0200990 def_thread.timeout = def_timeout;
Jens Axboeebac4652005-12-08 15:25:21 +0100991 def_thread.overwrite = DEF_OVERWRITE;
992 def_thread.invalidate_cache = DEF_INVALIDATE;
993 def_thread.sync_io = DEF_SYNCIO;
994 def_thread.mem_type = MEM_MALLOC;
995 def_thread.bw_avg_time = DEF_BWAVGTIME;
996 def_thread.create_serialize = DEF_CREATE_SER;
997 def_thread.create_fsync = DEF_CREATE_FSYNC;
998 def_thread.loops = DEF_LOOPS;
999 def_thread.verify = DEF_VERIFY;
1000 def_thread.stonewall = DEF_STONEWALL;
1001 def_thread.numjobs = DEF_NUMJOBS;
1002 def_thread.use_thread = DEF_USE_THREAD;
Jens Axboea6ccc7b2006-06-02 10:14:15 +02001003 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
1004 def_thread.rwmixread = DEF_RWMIX_READ;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001005 def_thread.nice = DEF_NICE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001006 def_thread.rand_repeatable = DEF_RAND_REPEAT;
Jens Axboe53cdc682006-10-18 11:50:58 +02001007 def_thread.nr_files = DEF_NR_FILES;
Jens Axboef6cbb262006-10-18 14:16:42 +02001008 def_thread.unlink = DEF_UNLINK;
Jens Axboeec94ec52006-10-20 10:59:19 +02001009 def_thread.write_bw_log = write_bw_log;
1010 def_thread.write_lat_log = write_lat_log;
Jens Axboebb8895e2006-10-30 15:14:48 +01001011 def_thread.norandommap = DEF_NO_RAND_MAP;
Jens Axboeebac4652005-12-08 15:25:21 +01001012#ifdef FIO_HAVE_DISK_UTIL
1013 def_thread.do_disk_util = 1;
1014#endif
1015
1016 return 0;
1017}
1018
Jens Axboe0ab8db82006-10-18 17:16:23 +02001019static void usage(void)
Jens Axboe4785f992006-05-26 03:59:10 +02001020{
1021 printf("%s\n", fio_version_string);
Jens Axboeb4692822006-10-27 13:43:22 +02001022 printf("\t--output\tWrite output to file\n");
1023 printf("\t--timeout\tRuntime in seconds\n");
1024 printf("\t--latency-log\tGenerate per-job latency logs\n");
1025 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1026 printf("\t--minimal\tMinimal (terse) output\n");
1027 printf("\t--version\tPrint version info and exit\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001028}
1029
Jens Axboe972cfd22006-06-09 11:08:56 +02001030static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +01001031{
Jens Axboeb4692822006-10-27 13:43:22 +02001032 struct thread_data *td = NULL;
Jens Axboec2b1e752006-10-30 09:03:13 +01001033 int c, ini_idx = 0, lidx, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001034
Jens Axboeb4692822006-10-27 13:43:22 +02001035 while ((c = getopt_long(argc, argv, "", long_options, &lidx)) != -1) {
Jens Axboeebac4652005-12-08 15:25:21 +01001036 switch (c) {
Jens Axboeb4692822006-10-27 13:43:22 +02001037 case 't':
1038 def_timeout = atoi(optarg);
1039 break;
1040 case 'l':
1041 write_lat_log = 1;
1042 break;
1043 case 'w':
1044 write_bw_log = 1;
1045 break;
1046 case 'o':
1047 f_out = fopen(optarg, "w+");
1048 if (!f_out) {
1049 perror("fopen output");
1050 exit(1);
1051 }
1052 f_err = f_out;
1053 break;
1054 case 'm':
1055 terse_output = 1;
1056 break;
1057 case 'h':
1058 usage();
1059 exit(0);
1060 case 'v':
1061 printf("%s\n", fio_version_string);
1062 exit(0);
1063 case FIO_GETOPT_JOB: {
1064 const char *opt = long_options[lidx].name;
1065 char *val = optarg;
1066
Jens Axboec2b1e752006-10-30 09:03:13 +01001067 if (!strncmp(opt, "name", 4) && td) {
1068 ret = add_job(td, td->name ?: "fio", 0);
1069 if (ret) {
1070 put_job(td);
1071 return 0;
1072 }
1073 td = NULL;
1074 }
Jens Axboeb4692822006-10-27 13:43:22 +02001075 if (!td) {
Jens Axboe38d0adb2006-10-30 12:21:25 +01001076 int global = !strncmp(val, "global", 6);
Jens Axboec2b1e752006-10-30 09:03:13 +01001077
1078 td = get_new_job(global, &def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001079 if (!td)
1080 return 0;
1081 }
Jens Axboe38d0adb2006-10-30 12:21:25 +01001082
Jens Axboeb1508cf2006-11-02 09:12:40 +01001083 ret = parse_cmd_option(opt, val, options, td);
1084 if (ret) {
1085 log_err("fio: job dropped\n");
1086 put_job(td);
1087 td = NULL;
1088 }
Jens Axboeb4692822006-10-27 13:43:22 +02001089 break;
1090 }
1091 default:
1092 printf("optarg <<%s>>\n", argv[optind]);
1093 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001094 }
1095 }
Jens Axboec9fad892006-05-27 17:26:50 +02001096
Jens Axboeb4692822006-10-27 13:43:22 +02001097 if (td) {
Jens Axboec2b1e752006-10-30 09:03:13 +01001098 ret = add_job(td, td->name ?: "fio", 0);
Jens Axboeb4692822006-10-27 13:43:22 +02001099 if (ret)
1100 put_job(td);
Jens Axboe972cfd22006-06-09 11:08:56 +02001101 }
Jens Axboe774a6172006-08-24 08:44:26 +02001102
Jens Axboeb4692822006-10-27 13:43:22 +02001103 while (optind < argc) {
1104 ini_idx++;
1105 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1106 ini_file[ini_idx - 1] = strdup(argv[optind]);
1107 optind++;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001108 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001109
1110 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +01001111}
1112
1113static void free_shm(void)
1114{
1115 struct shmid_ds sbuf;
1116
1117 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001118 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001119 threads = NULL;
1120 shmctl(shm_id, IPC_RMID, &sbuf);
1121 }
1122}
1123
Jens Axboe906c8d72006-06-13 09:37:56 +02001124/*
1125 * The thread area is shared between the main process and the job
1126 * threads/processes. So setup a shared memory segment that will hold
1127 * all the job info.
1128 */
Jens Axboeebac4652005-12-08 15:25:21 +01001129static int setup_thread_area(void)
1130{
1131 /*
1132 * 1024 is too much on some machines, scale max_jobs if
1133 * we get a failure that looks like too large a shm segment
1134 */
1135 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001136 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001137
Jens Axboe906c8d72006-06-13 09:37:56 +02001138 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001139 if (shm_id != -1)
1140 break;
1141 if (errno != EINVAL) {
1142 perror("shmget");
1143 break;
1144 }
1145
1146 max_jobs >>= 1;
1147 } while (max_jobs);
1148
1149 if (shm_id == -1)
1150 return 1;
1151
1152 threads = shmat(shm_id, NULL, 0);
1153 if (threads == (void *) -1) {
1154 perror("shmat");
1155 return 1;
1156 }
1157
1158 atexit(free_shm);
1159 return 0;
1160}
1161
Jens Axboeb4692822006-10-27 13:43:22 +02001162/*
1163 * Copy the fio options into the long options map, so we mirror
1164 * job and cmd line options.
1165 */
1166static void dupe_job_options(void)
1167{
1168 struct fio_option *o;
1169 unsigned int i;
1170
1171 i = 0;
1172 while (long_options[i].name)
1173 i++;
1174
1175 o = &options[0];
1176 while (o->name) {
1177 long_options[i].name = o->name;
1178 long_options[i].val = FIO_GETOPT_JOB;
1179 if (o->type == FIO_OPT_STR_SET)
1180 long_options[i].has_arg = no_argument;
1181 else
1182 long_options[i].has_arg = required_argument;
1183
1184 i++;
1185 o++;
1186 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1187 }
1188}
1189
Jens Axboeebac4652005-12-08 15:25:21 +01001190int parse_options(int argc, char *argv[])
1191{
Jens Axboe972cfd22006-06-09 11:08:56 +02001192 int job_files, i;
1193
Jens Axboeb4692822006-10-27 13:43:22 +02001194 f_out = stdout;
1195 f_err = stderr;
1196
1197 dupe_job_options();
1198
Jens Axboeebac4652005-12-08 15:25:21 +01001199 if (setup_thread_area())
1200 return 1;
1201 if (fill_def_thread())
1202 return 1;
1203
Jens Axboe972cfd22006-06-09 11:08:56 +02001204 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001205
Jens Axboe972cfd22006-06-09 11:08:56 +02001206 for (i = 0; i < job_files; i++) {
1207 if (fill_def_thread())
1208 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001209 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001210 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001211 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001212 }
Jens Axboeebac4652005-12-08 15:25:21 +01001213
Jens Axboe88c6ed82006-06-09 11:28:10 +02001214 free(ini_file);
Jens Axboeb4692822006-10-27 13:43:22 +02001215
1216 if (!thread_number) {
1217 log_err("No jobs defined(s)\n");
Jens Axboeb4692822006-10-27 13:43:22 +02001218 return 1;
1219 }
1220
Jens Axboeebac4652005-12-08 15:25:21 +01001221 return 0;
1222}