blob: 616c51f27ff738e7d2d2bd3eddbf7c924d693c3b [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 *);
63static int str_prio_cb(void *, unsigned int *);
64static int str_prioclass_cb(void *, unsigned int *);
65static int str_exitall_cb(void);
66static int str_cpumask_cb(void *, unsigned int *);
67
68/*
69 * Map of job/command line options
70 */
71static struct fio_option options[] = {
72 {
73 .name = "name",
74 .type = FIO_OPT_STR_STORE,
75 .off1 = td_var_offset(name),
76 },
77 {
78 .name = "directory",
79 .type = FIO_OPT_STR_STORE,
80 .off1 = td_var_offset(directory),
81 },
82 {
83 .name = "filename",
84 .type = FIO_OPT_STR_STORE,
85 .off1 = td_var_offset(filename),
86 },
87 {
88 .name = "rw",
89 .type = FIO_OPT_STR,
90 .cb = str_rw_cb,
91 },
92 {
93 .name = "ioengine",
94 .type = FIO_OPT_STR,
95 .cb = str_ioengine_cb,
96 },
97 {
98 .name = "mem",
99 .type = FIO_OPT_STR,
100 .cb = str_mem_cb,
101 },
102 {
103 .name = "verify",
104 .type = FIO_OPT_STR,
105 .cb = str_verify_cb,
106 },
107 {
108 .name = "write_iolog",
Jens Axboe076efc72006-10-27 11:24:25 +0200109 .type = FIO_OPT_STR_STORE,
110 .off1 = td_var_offset(write_iolog_file),
Jens Axboee1f36502006-10-27 10:54:08 +0200111 },
112 {
Jens Axboe076efc72006-10-27 11:24:25 +0200113 .name = "read_iolog",
Jens Axboee1f36502006-10-27 10:54:08 +0200114 .type = FIO_OPT_STR_STORE,
Jens Axboe076efc72006-10-27 11:24:25 +0200115 .off1 = td_var_offset(read_iolog_file),
Jens Axboee1f36502006-10-27 10:54:08 +0200116 },
117 {
118 .name = "exec_prerun",
119 .type = FIO_OPT_STR_STORE,
120 .off1 = td_var_offset(exec_prerun),
121 },
122 {
123 .name = "exec_postrun",
124 .type = FIO_OPT_STR_STORE,
125 .off1 = td_var_offset(exec_postrun),
126 },
127#ifdef FIO_HAVE_IOSCHED_SWITCH
128 {
129 .name = "ioscheduler",
130 .type = FIO_OPT_STR_STORE,
131 .off1 = td_var_offset(ioscheduler),
132 },
133#endif
134 {
135 .name = "size",
136 .type = FIO_OPT_STR_VAL,
137 .off1 = td_var_offset(total_file_size),
138 },
139 {
140 .name = "bs",
141 .type = FIO_OPT_STR_VAL,
Jens Axboea00735e2006-11-03 08:58:08 +0100142 .off1 = td_var_offset(bs[DDIR_READ]),
143 },
144 {
145 .name = "read_bs",
146 .type = FIO_OPT_STR_VAL,
147 .off1 = td_var_offset(bs[DDIR_READ]),
148 },
149 {
150 .name = "write_bs",
151 .type = FIO_OPT_STR_VAL,
152 .off1 = td_var_offset(bs[DDIR_WRITE]),
Jens Axboee1f36502006-10-27 10:54:08 +0200153 },
154 {
155 .name = "offset",
156 .type = FIO_OPT_STR_VAL,
157 .off1 = td_var_offset(start_offset),
158 },
159 {
160 .name = "zonesize",
161 .type = FIO_OPT_STR_VAL,
162 .off1 = td_var_offset(zone_size),
163 },
164 {
165 .name = "zoneskip",
166 .type = FIO_OPT_STR_VAL,
167 .off1 = td_var_offset(zone_skip),
168 },
169 {
170 .name = "lockmem",
171 .type = FIO_OPT_STR_VAL,
172 .cb = str_lockmem_cb,
173 },
174 {
175 .name = "bsrange",
176 .type = FIO_OPT_RANGE,
Jens Axboea00735e2006-11-03 08:58:08 +0100177 .off1 = td_var_offset(min_bs[DDIR_READ]),
178 .off2 = td_var_offset(max_bs[DDIR_READ]),
179 },
180 {
181 .name = "read_bsrange",
182 .type = FIO_OPT_RANGE,
183 .off1 = td_var_offset(min_bs[DDIR_READ]),
184 .off2 = td_var_offset(max_bs[DDIR_READ]),
185 },
186 {
187 .name = "write_bsrange",
188 .type = FIO_OPT_RANGE,
189 .off1 = td_var_offset(min_bs[DDIR_WRITE]),
190 .off2 = td_var_offset(max_bs[DDIR_WRITE]),
Jens Axboee1f36502006-10-27 10:54:08 +0200191 },
192 {
193 .name = "nrfiles",
194 .type = FIO_OPT_INT,
195 .off1 = td_var_offset(nr_files),
196 },
197 {
198 .name = "iodepth",
199 .type = FIO_OPT_INT,
200 .off1 = td_var_offset(iodepth),
201 },
202 {
203 .name = "fsync",
204 .type = FIO_OPT_INT,
205 .off1 = td_var_offset(fsync_blocks),
206 },
207 {
208 .name = "rwmixcycle",
209 .type = FIO_OPT_INT,
210 .off1 = td_var_offset(rwmixcycle),
211 },
212 {
213 .name = "rwmixread",
214 .type = FIO_OPT_INT,
215 .off1 = td_var_offset(rwmixread),
216 .max_val= 100,
217 },
218 {
219 .name = "rwmixwrite",
220 .type = FIO_OPT_INT,
221 .off1 = td_var_offset(rwmixwrite),
222 .max_val= 100,
223 },
224 {
225 .name = "nice",
226 .type = FIO_OPT_INT,
227 .off1 = td_var_offset(nice),
228 },
229#ifdef FIO_HAVE_IOPRIO
230 {
231 .name = "prio",
232 .type = FIO_OPT_INT,
233 .cb = str_prio_cb,
234 },
235 {
236 .name = "prioclass",
237 .type = FIO_OPT_INT,
238 .cb = str_prioclass_cb,
239 },
240#endif
241 {
242 .name = "thinktime",
243 .type = FIO_OPT_INT,
244 .off1 = td_var_offset(thinktime)
245 },
246 {
247 .name = "rate",
248 .type = FIO_OPT_INT,
249 .off1 = td_var_offset(rate)
250 },
251 {
252 .name = "ratemin",
253 .type = FIO_OPT_INT,
254 .off1 = td_var_offset(ratemin)
255 },
256 {
257 .name = "ratecycle",
258 .type = FIO_OPT_INT,
259 .off1 = td_var_offset(ratecycle)
260 },
261 {
262 .name = "startdelay",
263 .type = FIO_OPT_INT,
264 .off1 = td_var_offset(start_delay)
265 },
266 {
267 .name = "timeout",
268 .type = FIO_OPT_STR_VAL_TIME,
269 .off1 = td_var_offset(timeout)
270 },
271 {
272 .name = "invalidate",
273 .type = FIO_OPT_INT,
274 .off1 = td_var_offset(invalidate_cache)
275 },
276 {
277 .name = "sync",
278 .type = FIO_OPT_INT,
279 .off1 = td_var_offset(sync_io)
280 },
281 {
282 .name = "bwavgtime",
283 .type = FIO_OPT_INT,
284 .off1 = td_var_offset(bw_avg_time)
285 },
286 {
287 .name = "create_serialize",
288 .type = FIO_OPT_INT,
289 .off1 = td_var_offset(create_serialize)
290 },
291 {
292 .name = "create_fsync",
293 .type = FIO_OPT_INT,
294 .off1 = td_var_offset(create_fsync)
295 },
296 {
297 .name = "loops",
298 .type = FIO_OPT_INT,
299 .off1 = td_var_offset(loops)
300 },
301 {
302 .name = "numjobs",
303 .type = FIO_OPT_INT,
304 .off1 = td_var_offset(numjobs)
305 },
306 {
307 .name = "cpuload",
308 .type = FIO_OPT_INT,
309 .off1 = td_var_offset(cpuload)
310 },
311 {
312 .name = "cpuchunks",
313 .type = FIO_OPT_INT,
314 .off1 = td_var_offset(cpucycle)
315 },
316 {
317 .name = "direct",
318 .type = FIO_OPT_INT,
319 .off1 = td_var_offset(odirect)
320 },
321 {
322 .name = "overwrite",
323 .type = FIO_OPT_INT,
324 .off1 = td_var_offset(overwrite)
325 },
326#ifdef FIO_HAVE_CPU_AFFINITY
327 {
328 .name = "cpumask",
329 .type = FIO_OPT_INT,
330 .cb = str_cpumask_cb,
331 },
332#endif
333 {
334 .name = "end_fsync",
335 .type = FIO_OPT_INT,
336 .off1 = td_var_offset(end_fsync)
337 },
338 {
339 .name = "unlink",
340 .type = FIO_OPT_STR_SET,
341 .off1 = td_var_offset(unlink),
342 },
343 {
344 .name = "exitall",
345 .type = FIO_OPT_STR_SET,
346 .cb = str_exitall_cb,
347 },
348 {
349 .name = "stonewall",
350 .type = FIO_OPT_STR_SET,
351 .off1 = td_var_offset(stonewall),
352 },
353 {
354 .name = "thread",
355 .type = FIO_OPT_STR_SET,
356 .off1 = td_var_offset(thread),
357 },
358 {
359 .name = "write_bw_log",
360 .type = FIO_OPT_STR_SET,
361 .off1 = td_var_offset(write_bw_log),
362 },
363 {
364 .name = "write_lat_log",
365 .type = FIO_OPT_STR_SET,
366 .off1 = td_var_offset(write_lat_log),
367 },
368 {
Jens Axboebb8895e2006-10-30 15:14:48 +0100369 .name = "norandommap",
370 .type = FIO_OPT_STR_SET,
371 .off1 = td_var_offset(norandommap),
372 },
373 {
Jens Axboe690adba2006-10-30 15:25:09 +0100374 .name = "bs_unaligned",
375 .type = FIO_OPT_STR_SET,
376 .off1 = td_var_offset(bs_unaligned),
377 },
378 {
Jens Axboee1f36502006-10-27 10:54:08 +0200379 .name = NULL,
380 },
381};
382
Jens Axboeb4692822006-10-27 13:43:22 +0200383#define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
384#define FIO_CMD_OPTS (16)
385#define FIO_GETOPT_JOB (0x89988998)
386
387/*
388 * Command line options. These will contain the above, plus a few
389 * extra that only pertain to fio itself and not jobs.
390 */
391static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
392 {
393 .name = "output",
394 .has_arg = required_argument,
395 .val = 'o',
396 },
397 {
398 .name = "timeout",
399 .has_arg = required_argument,
400 .val = 't',
401 },
402 {
403 .name = "latency-log",
404 .has_arg = required_argument,
405 .val = 'l',
406 },
407 {
408 .name = "bandwidth-log",
409 .has_arg = required_argument,
410 .val = 'b',
411 },
412 {
413 .name = "minimal",
414 .has_arg = optional_argument,
415 .val = 'm',
416 },
417 {
418 .name = "version",
419 .has_arg = no_argument,
420 .val = 'v',
421 },
422 {
423 .name = NULL,
424 },
425};
426
Jens Axboe972cfd22006-06-09 11:08:56 +0200427static int def_timeout = DEF_TIMEOUT;
Jens Axboe972cfd22006-06-09 11:08:56 +0200428
Jens Axboeb4692822006-10-27 13:43:22 +0200429static char fio_version_string[] = "fio 1.7";
Jens Axboeebac4652005-12-08 15:25:21 +0100430
Jens Axboe972cfd22006-06-09 11:08:56 +0200431static char **ini_file;
Jens Axboeebac4652005-12-08 15:25:21 +0100432static int max_jobs = MAX_JOBS;
433
434struct thread_data def_thread;
435struct thread_data *threads = NULL;
436
437int rate_quit = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100438int exitall_on_terminate = 0;
Jens Axboec6ae0a52006-06-12 11:04:44 +0200439int terse_output = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +0200440unsigned long long mlock_size = 0;
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200441FILE *f_out = NULL;
442FILE *f_err = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +0100443
Jens Axboeec94ec52006-10-20 10:59:19 +0200444static int write_lat_log = DEF_WRITE_LAT_LOG;
445static int write_bw_log = DEF_WRITE_BW_LOG;
446
Jens Axboe906c8d72006-06-13 09:37:56 +0200447/*
448 * Return a free job structure.
449 */
Jens Axboeebac4652005-12-08 15:25:21 +0100450static struct thread_data *get_new_job(int global, struct thread_data *parent)
451{
452 struct thread_data *td;
453
454 if (global)
455 return &def_thread;
456 if (thread_number >= max_jobs)
457 return NULL;
458
459 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200460 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100461
Jens Axboeebac4652005-12-08 15:25:21 +0100462 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100463 return td;
464}
465
466static void put_job(struct thread_data *td)
467{
Jens Axboe549577a2006-10-30 12:23:41 +0100468 if (td == &def_thread)
469 return;
470
Jens Axboeebac4652005-12-08 15:25:21 +0100471 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
472 thread_number--;
473}
474
Jens Axboedad915e2006-10-27 11:10:18 +0200475/*
476 * Lazy way of fixing up options that depend on each other. We could also
477 * define option callback handlers, but this is easier.
478 */
Jens Axboee1f36502006-10-27 10:54:08 +0200479static void fixup_options(struct thread_data *td)
480{
Jens Axboee1f36502006-10-27 10:54:08 +0200481 if (!td->rwmixread && td->rwmixwrite)
482 td->rwmixread = 100 - td->rwmixwrite;
Jens Axboedad915e2006-10-27 11:10:18 +0200483
Jens Axboe076efc72006-10-27 11:24:25 +0200484 if (td->write_iolog_file && td->read_iolog_file) {
485 log_err("fio: read iolog overrides write_iolog\n");
486 free(td->write_iolog_file);
487 td->write_iolog_file = NULL;
488 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100489
490 if (td->io_ops->flags & FIO_SYNCIO)
491 td->iodepth = 1;
492 else {
493 if (!td->iodepth)
494 td->iodepth = td->nr_files;
495 }
496
497 /*
498 * only really works for sequential io for now, and with 1 file
499 */
500 if (td->zone_size && !td->sequential && td->nr_files == 1)
501 td->zone_size = 0;
502
503 /*
504 * Reads can do overwrites, we always need to pre-create the file
505 */
506 if (td_read(td) || td_rw(td))
507 td->overwrite = 1;
508
Jens Axboea00735e2006-11-03 08:58:08 +0100509 if (td->bs[DDIR_READ] != DEF_BS)
510 td->bs[DDIR_WRITE] = td->bs[DDIR_READ];
511 if (!td->min_bs[DDIR_READ])
512 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
513 if (!td->max_bs[DDIR_READ])
514 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
515 if (!td->min_bs[DDIR_WRITE])
516 td->min_bs[DDIR_WRITE]= td->bs[DDIR_READ];
517 if (!td->max_bs[DDIR_WRITE])
518 td->max_bs[DDIR_WRITE] = td->bs[DDIR_READ];
519
520 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
521
Jens Axboe16b462a2006-10-30 12:35:18 +0100522 if (td_read(td) && !td_rw(td))
523 td->verify = 0;
Jens Axboebb8895e2006-10-30 15:14:48 +0100524
525 if (td->norandommap && td->verify != VERIFY_NONE) {
526 log_err("fio: norandommap given, verify disabled\n");
527 td->verify = VERIFY_NONE;
528 }
Jens Axboe690adba2006-10-30 15:25:09 +0100529 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
530 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee1f36502006-10-27 10:54:08 +0200531}
532
Jens Axboe906c8d72006-06-13 09:37:56 +0200533/*
534 * Adds a job to the list of things todo. Sanitizes the various options
535 * to make sure we don't have conflicts, and initializes various
536 * members of td.
537 */
Jens Axboe75154842006-06-01 13:56:09 +0200538static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100539{
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200540 char *ddir_str[] = { "read", "write", "randread", "randwrite",
541 "rw", NULL, "randrw" };
Jens Axboeebac4652005-12-08 15:25:21 +0100542 struct stat sb;
Jens Axboe53cdc682006-10-18 11:50:58 +0200543 int numjobs, ddir, i;
544 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100545
546#ifndef FIO_HAVE_LIBAIO
547 if (td->io_engine == FIO_LIBAIO) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200548 log_err("Linux libaio not available\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100549 return 1;
550 }
551#endif
552#ifndef FIO_HAVE_POSIXAIO
553 if (td->io_engine == FIO_POSIXAIO) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200554 log_err("posix aio not available\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100555 return 1;
556 }
557#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100558
559 /*
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 Axboe16b462a2006-10-30 12:35:18 +0100579 fixup_options(td);
Jens Axboe9cc935a2006-06-13 09:11:18 +0200580
Jens Axboeebac4652005-12-08 15:25:21 +0100581 td->filetype = FIO_TYPE_FILE;
Jens Axboe0af7b542006-02-17 10:10:12 +0100582 if (!stat(jobname, &sb)) {
583 if (S_ISBLK(sb.st_mode))
584 td->filetype = FIO_TYPE_BD;
585 else if (S_ISCHR(sb.st_mode))
586 td->filetype = FIO_TYPE_CHAR;
587 }
Jens Axboeebac4652005-12-08 15:25:21 +0100588
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;
597 int i;
Jens Axboee9c047a2006-06-07 21:13:04 +0200598
Jens Axboeef899b62006-06-06 09:31:00 +0200599 if (td->directory && td->directory[0] != '\0')
Jens Axboe53cdc682006-10-18 11:50:58 +0200600 sprintf(tmp, "%s/", td->directory);
Jens Axboeebac4652005-12-08 15:25:21 +0100601
Jens Axboe53cdc682006-10-18 11:50:58 +0200602 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
603
604 for_each_file(td, f, i) {
605 memset(f, 0, sizeof(*f));
606 f->fd = -1;
607
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200608 if (td->filename)
609 sprintf(tmp + len, "%s", td->filename);
610 else
611 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
Jens Axboe53cdc682006-10-18 11:50:58 +0200612 f->file_name = strdup(tmp);
613 }
614 } else {
615 td->nr_files = 1;
616 td->files = malloc(sizeof(struct fio_file));
617 f = &td->files[0];
618
619 memset(f, 0, sizeof(*f));
620 f->fd = -1;
621 f->file_name = strdup(jobname);
622 }
623
624 for_each_file(td, f, i) {
625 f->file_size = td->total_file_size / td->nr_files;
626 f->file_offset = td->start_offset;
627 }
628
Jens Axboebbfd6b02006-06-07 19:42:54 +0200629 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100630
631 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
632 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
633 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
634
Jens Axboeebac4652005-12-08 15:25:21 +0100635 if (td->stonewall && td->thread_number > 1)
636 groupid++;
637
638 td->groupid = groupid;
639
640 if (setup_rate(td))
641 goto err;
642
Jens Axboeec94ec52006-10-20 10:59:19 +0200643 if (td->write_lat_log) {
Jens Axboeebac4652005-12-08 15:25:21 +0100644 setup_log(&td->slat_log);
645 setup_log(&td->clat_log);
646 }
Jens Axboeec94ec52006-10-20 10:59:19 +0200647 if (td->write_bw_log)
Jens Axboeebac4652005-12-08 15:25:21 +0100648 setup_log(&td->bw_log);
649
Jens Axboeb4692822006-10-27 13:43:22 +0200650 if (!td->name)
651 td->name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200652
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200653 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
Jens Axboe75154842006-06-01 13:56:09 +0200654
Jens Axboec6ae0a52006-06-12 11:04:44 +0200655 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200656 if (!job_add_num) {
Jens Axboe2866c822006-10-09 15:57:48 +0200657 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200658 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
659 else
Jens Axboea00735e2006-11-03 08:58:08 +0100660 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 +0200661 } else if (job_add_num == 1)
Jens Axboec6ae0a52006-06-12 11:04:44 +0200662 fprintf(f_out, "...\n");
663 }
Jens Axboeebac4652005-12-08 15:25:21 +0100664
665 /*
666 * recurse add identical jobs, clear numjobs and stonewall options
667 * as they don't apply to sub-jobs
668 */
669 numjobs = td->numjobs;
670 while (--numjobs) {
671 struct thread_data *td_new = get_new_job(0, td);
672
673 if (!td_new)
674 goto err;
675
676 td_new->numjobs = 1;
677 td_new->stonewall = 0;
Jens Axboe75154842006-06-01 13:56:09 +0200678 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100679
Jens Axboe75154842006-06-01 13:56:09 +0200680 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100681 goto err;
682 }
683 return 0;
684err:
685 put_job(td);
686 return -1;
687}
688
Jens Axboe906c8d72006-06-13 09:37:56 +0200689/*
690 * Initialize the various random states we need (random io, block size ranges,
691 * read/write mix, etc).
692 */
Jens Axboeebac4652005-12-08 15:25:21 +0100693int init_random_state(struct thread_data *td)
694{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200695 unsigned long seeds[4];
Jens Axboe53cdc682006-10-18 11:50:58 +0200696 int fd, num_maps, blocks, i;
Jens Axboe0ab8db82006-10-18 17:16:23 +0200697 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100698
Jens Axboef48b4672006-10-27 11:30:07 +0200699 if (td->io_ops->flags & FIO_CPUIO)
700 return 0;
701
Jens Axboe1ac267b2006-05-31 20:29:00 +0200702 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100703 if (fd == -1) {
704 td_verror(td, errno);
705 return 1;
706 }
707
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200708 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100709 td_verror(td, EIO);
710 close(fd);
711 return 1;
712 }
713
714 close(fd);
715
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200716 os_random_seed(seeds[0], &td->bsrange_state);
717 os_random_seed(seeds[1], &td->verify_state);
718 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100719
720 if (td->sequential)
721 return 0;
722
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200723 if (td->rand_repeatable)
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200724 seeds[3] = DEF_RANDSEED;
Jens Axboeebac4652005-12-08 15:25:21 +0100725
Jens Axboebb8895e2006-10-30 15:14:48 +0100726 if (!td->norandommap) {
727 for_each_file(td, f, i) {
Jens Axboea00735e2006-11-03 08:58:08 +0100728 blocks = (f->file_size + td->rw_min_bs - 1) / td->rw_min_bs;
Jens Axboec7c280e2006-11-02 20:10:33 +0100729 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
Jens Axboebb8895e2006-10-30 15:14:48 +0100730 f->file_map = malloc(num_maps * sizeof(long));
731 f->num_maps = num_maps;
732 memset(f->file_map, 0, num_maps * sizeof(long));
733 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200734 }
Jens Axboeebac4652005-12-08 15:25:21 +0100735
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200736 os_random_seed(seeds[3], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100737 return 0;
738}
739
740static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
741{
742#ifdef FIO_HAVE_CPU_AFFINITY
743 unsigned int i;
744
745 CPU_ZERO(&cpumask);
746
747 for (i = 0; i < sizeof(int) * 8; i++) {
748 if ((1 << i) & cpu)
749 CPU_SET(i, &cpumask);
750 }
751#endif
752}
753
Jens Axboeebac4652005-12-08 15:25:21 +0100754static int is_empty_or_comment(char *line)
755{
756 unsigned int i;
757
758 for (i = 0; i < strlen(line); i++) {
759 if (line[i] == ';')
760 return 1;
761 if (!isspace(line[i]) && !iscntrl(line[i]))
762 return 0;
763 }
764
765 return 1;
766}
767
Jens Axboeb4692822006-10-27 13:43:22 +0200768static int str_rw_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100769{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200770 struct thread_data *td = data;
771
Jens Axboeebac4652005-12-08 15:25:21 +0100772 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
773 td->ddir = DDIR_READ;
774 td->sequential = 1;
775 return 0;
776 } else if (!strncmp(mem, "randread", 8)) {
777 td->ddir = DDIR_READ;
778 td->sequential = 0;
779 return 0;
780 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
781 td->ddir = DDIR_WRITE;
782 td->sequential = 1;
783 return 0;
784 } else if (!strncmp(mem, "randwrite", 9)) {
785 td->ddir = DDIR_WRITE;
786 td->sequential = 0;
787 return 0;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200788 } else if (!strncmp(mem, "rw", 2)) {
789 td->ddir = 0;
790 td->iomix = 1;
791 td->sequential = 1;
792 return 0;
793 } else if (!strncmp(mem, "randrw", 6)) {
794 td->ddir = 0;
795 td->iomix = 1;
796 td->sequential = 0;
797 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100798 }
799
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200800 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100801 return 1;
802}
803
Jens Axboeb4692822006-10-27 13:43:22 +0200804static int str_verify_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100805{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200806 struct thread_data *td = data;
807
Jens Axboeebac4652005-12-08 15:25:21 +0100808 if (!strncmp(mem, "0", 1)) {
809 td->verify = VERIFY_NONE;
810 return 0;
811 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
812 td->verify = VERIFY_MD5;
813 return 0;
814 } else if (!strncmp(mem, "crc32", 5)) {
815 td->verify = VERIFY_CRC32;
816 return 0;
817 }
818
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200819 log_err("fio: verify types: md5, crc32\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100820 return 1;
821}
822
Jens Axboeb4692822006-10-27 13:43:22 +0200823static int str_mem_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100824{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200825 struct thread_data *td = data;
826
Jens Axboeebac4652005-12-08 15:25:21 +0100827 if (!strncmp(mem, "malloc", 6)) {
828 td->mem_type = MEM_MALLOC;
829 return 0;
830 } else if (!strncmp(mem, "shm", 3)) {
831 td->mem_type = MEM_SHM;
832 return 0;
833 } else if (!strncmp(mem, "mmap", 4)) {
834 td->mem_type = MEM_MMAP;
835 return 0;
836 }
837
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200838 log_err("fio: mem type: malloc, shm, mmap\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100839 return 1;
840}
841
Jens Axboeb4692822006-10-27 13:43:22 +0200842static int str_ioengine_cb(void *data, const char *str)
Jens Axboeebac4652005-12-08 15:25:21 +0100843{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200844 struct thread_data *td = data;
845
Jens Axboe2866c822006-10-09 15:57:48 +0200846 td->io_ops = load_ioengine(td, str);
847 if (td->io_ops)
Jens Axboeebac4652005-12-08 15:25:21 +0100848 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100849
Jens Axboeb990b5c2006-09-14 09:48:22 +0200850 log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice, cpu\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100851 return 1;
852}
853
Jens Axboee1f36502006-10-27 10:54:08 +0200854static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
855{
856 mlock_size = *val;
857 return 0;
858}
859
860static int str_prioclass_cb(void *data, unsigned int *val)
861{
862 struct thread_data *td = data;
863
864 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
865 return 0;
866}
867
868static int str_prio_cb(void *data, unsigned int *val)
869{
870 struct thread_data *td = data;
871
872 td->ioprio |= *val;
873 return 0;
874}
875
876static int str_exitall_cb(void)
877{
878 exitall_on_terminate = 1;
879 return 0;
880}
881
882static int str_cpumask_cb(void *data, unsigned int *val)
883{
884 struct thread_data *td = data;
885
886 fill_cpu_mask(td->cpumask, *val);
887 return 0;
888}
889
Jens Axboe07261982006-06-07 10:51:12 +0200890/*
891 * This is our [ini] type file parser.
892 */
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200893int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100894{
Jens Axboee1f36502006-10-27 10:54:08 +0200895 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +0100896 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +0100897 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +0100898 fpos_t off;
899 FILE *f;
900 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200901 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +0100902
903 f = fopen(file, "r");
904 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200905 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100906 return 1;
907 }
908
909 string = malloc(4096);
910 name = malloc(256);
Jens Axboefee3bb42006-10-30 13:32:08 +0100911 memset(name, 0, 256);
Jens Axboeebac4652005-12-08 15:25:21 +0100912
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200913 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100914 do {
915 p = fgets(string, 4095, f);
916 if (!p)
Jens Axboe45410ac2006-06-07 11:10:39 +0200917 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100918 if (is_empty_or_comment(p))
919 continue;
Jens Axboefee3bb42006-10-30 13:32:08 +0100920 if (sscanf(p, "[%255s]", name) != 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100921 continue;
922
923 global = !strncmp(name, "global", 6);
924
925 name[strlen(name) - 1] = '\0';
926
927 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200928 if (!td) {
929 ret = 1;
930 break;
931 }
Jens Axboeebac4652005-12-08 15:25:21 +0100932
Jens Axboe972cfd22006-06-09 11:08:56 +0200933 /*
934 * Seperate multiple job files by a stonewall
935 */
Jens Axboef9481912006-06-09 11:37:28 +0200936 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +0200937 td->stonewall = stonewall;
938 stonewall = 0;
939 }
940
Jens Axboeebac4652005-12-08 15:25:21 +0100941 fgetpos(f, &off);
942 while ((p = fgets(string, 4096, f)) != NULL) {
943 if (is_empty_or_comment(p))
944 continue;
Jens Axboee1f36502006-10-27 10:54:08 +0200945
Jens Axboeb6754f92006-05-30 14:29:32 +0200946 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +0100947
948 if (p[0] == '[')
949 break;
950
Jens Axboe4ae3f762006-05-30 13:35:14 +0200951 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200952
Jens Axboee1f36502006-10-27 10:54:08 +0200953 fgetpos(f, &off);
Jens Axboeebac4652005-12-08 15:25:21 +0100954
Jens Axboe45410ac2006-06-07 11:10:39 +0200955 /*
956 * Don't break here, continue parsing options so we
957 * dump all the bad ones. Makes trial/error fixups
958 * easier on the user.
959 */
Jens Axboe7c124ac2006-10-30 13:36:52 +0100960 ret |= parse_option(p, options, td);
Jens Axboeebac4652005-12-08 15:25:21 +0100961 }
Jens Axboeebac4652005-12-08 15:25:21 +0100962
Jens Axboe45410ac2006-06-07 11:10:39 +0200963 if (!ret) {
964 fsetpos(f, &off);
965 ret = add_job(td, name, 0);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100966 } else {
967 log_err("fio: job %s dropped\n", name);
968 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +0200969 }
Jens Axboe7c124ac2006-10-30 13:36:52 +0100970 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +0100971
972 free(string);
973 free(name);
974 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200975 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100976}
977
978static int fill_def_thread(void)
979{
980 memset(&def_thread, 0, sizeof(def_thread));
981
982 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
983 perror("sched_getaffinity");
984 return 1;
985 }
986
987 /*
988 * fill globals
989 */
990 def_thread.ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200991 def_thread.iomix = 0;
Jens Axboea00735e2006-11-03 08:58:08 +0100992 def_thread.bs[DDIR_READ] = DEF_BS;
993 def_thread.bs[DDIR_WRITE] = DEF_BS;
994 def_thread.min_bs[DDIR_READ] = def_thread.min_bs[DDIR_WRITE] = 0;
995 def_thread.max_bs[DDIR_READ] = def_thread.max_bs[DDIR_WRITE] = 0;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200996 def_thread.odirect = DEF_ODIRECT;
Jens Axboeebac4652005-12-08 15:25:21 +0100997 def_thread.ratecycle = DEF_RATE_CYCLE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200998 def_thread.sequential = DEF_SEQUENTIAL;
Jens Axboe972cfd22006-06-09 11:08:56 +0200999 def_thread.timeout = def_timeout;
Jens Axboeebac4652005-12-08 15:25:21 +01001000 def_thread.overwrite = DEF_OVERWRITE;
1001 def_thread.invalidate_cache = DEF_INVALIDATE;
1002 def_thread.sync_io = DEF_SYNCIO;
1003 def_thread.mem_type = MEM_MALLOC;
1004 def_thread.bw_avg_time = DEF_BWAVGTIME;
1005 def_thread.create_serialize = DEF_CREATE_SER;
1006 def_thread.create_fsync = DEF_CREATE_FSYNC;
1007 def_thread.loops = DEF_LOOPS;
1008 def_thread.verify = DEF_VERIFY;
1009 def_thread.stonewall = DEF_STONEWALL;
1010 def_thread.numjobs = DEF_NUMJOBS;
1011 def_thread.use_thread = DEF_USE_THREAD;
Jens Axboea6ccc7b2006-06-02 10:14:15 +02001012 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
1013 def_thread.rwmixread = DEF_RWMIX_READ;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001014 def_thread.nice = DEF_NICE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001015 def_thread.rand_repeatable = DEF_RAND_REPEAT;
Jens Axboe53cdc682006-10-18 11:50:58 +02001016 def_thread.nr_files = DEF_NR_FILES;
Jens Axboef6cbb262006-10-18 14:16:42 +02001017 def_thread.unlink = DEF_UNLINK;
Jens Axboeec94ec52006-10-20 10:59:19 +02001018 def_thread.write_bw_log = write_bw_log;
1019 def_thread.write_lat_log = write_lat_log;
Jens Axboebb8895e2006-10-30 15:14:48 +01001020 def_thread.norandommap = DEF_NO_RAND_MAP;
Jens Axboeebac4652005-12-08 15:25:21 +01001021#ifdef FIO_HAVE_DISK_UTIL
1022 def_thread.do_disk_util = 1;
1023#endif
1024
1025 return 0;
1026}
1027
Jens Axboe0ab8db82006-10-18 17:16:23 +02001028static void usage(void)
Jens Axboe4785f992006-05-26 03:59:10 +02001029{
1030 printf("%s\n", fio_version_string);
Jens Axboeb4692822006-10-27 13:43:22 +02001031 printf("\t--output\tWrite output to file\n");
1032 printf("\t--timeout\tRuntime in seconds\n");
1033 printf("\t--latency-log\tGenerate per-job latency logs\n");
1034 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1035 printf("\t--minimal\tMinimal (terse) output\n");
1036 printf("\t--version\tPrint version info and exit\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001037}
1038
Jens Axboe972cfd22006-06-09 11:08:56 +02001039static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +01001040{
Jens Axboeb4692822006-10-27 13:43:22 +02001041 struct thread_data *td = NULL;
Jens Axboec2b1e752006-10-30 09:03:13 +01001042 int c, ini_idx = 0, lidx, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001043
Jens Axboeb4692822006-10-27 13:43:22 +02001044 while ((c = getopt_long(argc, argv, "", long_options, &lidx)) != -1) {
Jens Axboeebac4652005-12-08 15:25:21 +01001045 switch (c) {
Jens Axboeb4692822006-10-27 13:43:22 +02001046 case 't':
1047 def_timeout = atoi(optarg);
1048 break;
1049 case 'l':
1050 write_lat_log = 1;
1051 break;
1052 case 'w':
1053 write_bw_log = 1;
1054 break;
1055 case 'o':
1056 f_out = fopen(optarg, "w+");
1057 if (!f_out) {
1058 perror("fopen output");
1059 exit(1);
1060 }
1061 f_err = f_out;
1062 break;
1063 case 'm':
1064 terse_output = 1;
1065 break;
1066 case 'h':
1067 usage();
1068 exit(0);
1069 case 'v':
1070 printf("%s\n", fio_version_string);
1071 exit(0);
1072 case FIO_GETOPT_JOB: {
1073 const char *opt = long_options[lidx].name;
1074 char *val = optarg;
1075
Jens Axboec2b1e752006-10-30 09:03:13 +01001076 if (!strncmp(opt, "name", 4) && td) {
1077 ret = add_job(td, td->name ?: "fio", 0);
1078 if (ret) {
1079 put_job(td);
1080 return 0;
1081 }
1082 td = NULL;
1083 }
Jens Axboeb4692822006-10-27 13:43:22 +02001084 if (!td) {
Jens Axboe38d0adb2006-10-30 12:21:25 +01001085 int global = !strncmp(val, "global", 6);
Jens Axboec2b1e752006-10-30 09:03:13 +01001086
1087 td = get_new_job(global, &def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001088 if (!td)
1089 return 0;
1090 }
Jens Axboe38d0adb2006-10-30 12:21:25 +01001091
Jens Axboeb1508cf2006-11-02 09:12:40 +01001092 ret = parse_cmd_option(opt, val, options, td);
1093 if (ret) {
1094 log_err("fio: job dropped\n");
1095 put_job(td);
1096 td = NULL;
1097 }
Jens Axboeb4692822006-10-27 13:43:22 +02001098 break;
1099 }
1100 default:
1101 printf("optarg <<%s>>\n", argv[optind]);
1102 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001103 }
1104 }
Jens Axboec9fad892006-05-27 17:26:50 +02001105
Jens Axboeb4692822006-10-27 13:43:22 +02001106 if (td) {
Jens Axboec2b1e752006-10-30 09:03:13 +01001107 ret = add_job(td, td->name ?: "fio", 0);
Jens Axboeb4692822006-10-27 13:43:22 +02001108 if (ret)
1109 put_job(td);
Jens Axboe972cfd22006-06-09 11:08:56 +02001110 }
Jens Axboe774a6172006-08-24 08:44:26 +02001111
Jens Axboeb4692822006-10-27 13:43:22 +02001112 while (optind < argc) {
1113 ini_idx++;
1114 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1115 ini_file[ini_idx - 1] = strdup(argv[optind]);
1116 optind++;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001117 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001118
1119 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +01001120}
1121
1122static void free_shm(void)
1123{
1124 struct shmid_ds sbuf;
1125
1126 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001127 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001128 threads = NULL;
1129 shmctl(shm_id, IPC_RMID, &sbuf);
1130 }
1131}
1132
Jens Axboe906c8d72006-06-13 09:37:56 +02001133/*
1134 * The thread area is shared between the main process and the job
1135 * threads/processes. So setup a shared memory segment that will hold
1136 * all the job info.
1137 */
Jens Axboeebac4652005-12-08 15:25:21 +01001138static int setup_thread_area(void)
1139{
1140 /*
1141 * 1024 is too much on some machines, scale max_jobs if
1142 * we get a failure that looks like too large a shm segment
1143 */
1144 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001145 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001146
Jens Axboe906c8d72006-06-13 09:37:56 +02001147 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001148 if (shm_id != -1)
1149 break;
1150 if (errno != EINVAL) {
1151 perror("shmget");
1152 break;
1153 }
1154
1155 max_jobs >>= 1;
1156 } while (max_jobs);
1157
1158 if (shm_id == -1)
1159 return 1;
1160
1161 threads = shmat(shm_id, NULL, 0);
1162 if (threads == (void *) -1) {
1163 perror("shmat");
1164 return 1;
1165 }
1166
1167 atexit(free_shm);
1168 return 0;
1169}
1170
Jens Axboeb4692822006-10-27 13:43:22 +02001171/*
1172 * Copy the fio options into the long options map, so we mirror
1173 * job and cmd line options.
1174 */
1175static void dupe_job_options(void)
1176{
1177 struct fio_option *o;
1178 unsigned int i;
1179
1180 i = 0;
1181 while (long_options[i].name)
1182 i++;
1183
1184 o = &options[0];
1185 while (o->name) {
1186 long_options[i].name = o->name;
1187 long_options[i].val = FIO_GETOPT_JOB;
1188 if (o->type == FIO_OPT_STR_SET)
1189 long_options[i].has_arg = no_argument;
1190 else
1191 long_options[i].has_arg = required_argument;
1192
1193 i++;
1194 o++;
1195 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1196 }
1197}
1198
Jens Axboeebac4652005-12-08 15:25:21 +01001199int parse_options(int argc, char *argv[])
1200{
Jens Axboe972cfd22006-06-09 11:08:56 +02001201 int job_files, i;
1202
Jens Axboeb4692822006-10-27 13:43:22 +02001203 f_out = stdout;
1204 f_err = stderr;
1205
1206 dupe_job_options();
1207
Jens Axboeebac4652005-12-08 15:25:21 +01001208 if (setup_thread_area())
1209 return 1;
1210 if (fill_def_thread())
1211 return 1;
1212
Jens Axboe972cfd22006-06-09 11:08:56 +02001213 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001214
Jens Axboe972cfd22006-06-09 11:08:56 +02001215 for (i = 0; i < job_files; i++) {
1216 if (fill_def_thread())
1217 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001218 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001219 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001220 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001221 }
Jens Axboeebac4652005-12-08 15:25:21 +01001222
Jens Axboe88c6ed82006-06-09 11:28:10 +02001223 free(ini_file);
Jens Axboeb4692822006-10-27 13:43:22 +02001224
1225 if (!thread_number) {
1226 log_err("No jobs defined(s)\n");
Jens Axboeb4692822006-10-27 13:43:22 +02001227 return 1;
1228 }
1229
Jens Axboeebac4652005-12-08 15:25:21 +01001230 return 0;
1231}