blob: 6ad3c64c4221124d779ae982ac3b96ee12ced5bc [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,
142 .off1 = td_var_offset(bs),
143 },
144 {
145 .name = "offset",
146 .type = FIO_OPT_STR_VAL,
147 .off1 = td_var_offset(start_offset),
148 },
149 {
150 .name = "zonesize",
151 .type = FIO_OPT_STR_VAL,
152 .off1 = td_var_offset(zone_size),
153 },
154 {
155 .name = "zoneskip",
156 .type = FIO_OPT_STR_VAL,
157 .off1 = td_var_offset(zone_skip),
158 },
159 {
160 .name = "lockmem",
161 .type = FIO_OPT_STR_VAL,
162 .cb = str_lockmem_cb,
163 },
164 {
165 .name = "bsrange",
166 .type = FIO_OPT_RANGE,
167 .off1 = td_var_offset(min_bs),
168 .off2 = td_var_offset(max_bs),
169 },
170 {
171 .name = "nrfiles",
172 .type = FIO_OPT_INT,
173 .off1 = td_var_offset(nr_files),
174 },
175 {
176 .name = "iodepth",
177 .type = FIO_OPT_INT,
178 .off1 = td_var_offset(iodepth),
179 },
180 {
181 .name = "fsync",
182 .type = FIO_OPT_INT,
183 .off1 = td_var_offset(fsync_blocks),
184 },
185 {
186 .name = "rwmixcycle",
187 .type = FIO_OPT_INT,
188 .off1 = td_var_offset(rwmixcycle),
189 },
190 {
191 .name = "rwmixread",
192 .type = FIO_OPT_INT,
193 .off1 = td_var_offset(rwmixread),
194 .max_val= 100,
195 },
196 {
197 .name = "rwmixwrite",
198 .type = FIO_OPT_INT,
199 .off1 = td_var_offset(rwmixwrite),
200 .max_val= 100,
201 },
202 {
203 .name = "nice",
204 .type = FIO_OPT_INT,
205 .off1 = td_var_offset(nice),
206 },
207#ifdef FIO_HAVE_IOPRIO
208 {
209 .name = "prio",
210 .type = FIO_OPT_INT,
211 .cb = str_prio_cb,
212 },
213 {
214 .name = "prioclass",
215 .type = FIO_OPT_INT,
216 .cb = str_prioclass_cb,
217 },
218#endif
219 {
220 .name = "thinktime",
221 .type = FIO_OPT_INT,
222 .off1 = td_var_offset(thinktime)
223 },
224 {
225 .name = "rate",
226 .type = FIO_OPT_INT,
227 .off1 = td_var_offset(rate)
228 },
229 {
230 .name = "ratemin",
231 .type = FIO_OPT_INT,
232 .off1 = td_var_offset(ratemin)
233 },
234 {
235 .name = "ratecycle",
236 .type = FIO_OPT_INT,
237 .off1 = td_var_offset(ratecycle)
238 },
239 {
240 .name = "startdelay",
241 .type = FIO_OPT_INT,
242 .off1 = td_var_offset(start_delay)
243 },
244 {
245 .name = "timeout",
246 .type = FIO_OPT_STR_VAL_TIME,
247 .off1 = td_var_offset(timeout)
248 },
249 {
250 .name = "invalidate",
251 .type = FIO_OPT_INT,
252 .off1 = td_var_offset(invalidate_cache)
253 },
254 {
255 .name = "sync",
256 .type = FIO_OPT_INT,
257 .off1 = td_var_offset(sync_io)
258 },
259 {
260 .name = "bwavgtime",
261 .type = FIO_OPT_INT,
262 .off1 = td_var_offset(bw_avg_time)
263 },
264 {
265 .name = "create_serialize",
266 .type = FIO_OPT_INT,
267 .off1 = td_var_offset(create_serialize)
268 },
269 {
270 .name = "create_fsync",
271 .type = FIO_OPT_INT,
272 .off1 = td_var_offset(create_fsync)
273 },
274 {
275 .name = "loops",
276 .type = FIO_OPT_INT,
277 .off1 = td_var_offset(loops)
278 },
279 {
280 .name = "numjobs",
281 .type = FIO_OPT_INT,
282 .off1 = td_var_offset(numjobs)
283 },
284 {
285 .name = "cpuload",
286 .type = FIO_OPT_INT,
287 .off1 = td_var_offset(cpuload)
288 },
289 {
290 .name = "cpuchunks",
291 .type = FIO_OPT_INT,
292 .off1 = td_var_offset(cpucycle)
293 },
294 {
295 .name = "direct",
296 .type = FIO_OPT_INT,
297 .off1 = td_var_offset(odirect)
298 },
299 {
300 .name = "overwrite",
301 .type = FIO_OPT_INT,
302 .off1 = td_var_offset(overwrite)
303 },
304#ifdef FIO_HAVE_CPU_AFFINITY
305 {
306 .name = "cpumask",
307 .type = FIO_OPT_INT,
308 .cb = str_cpumask_cb,
309 },
310#endif
311 {
312 .name = "end_fsync",
313 .type = FIO_OPT_INT,
314 .off1 = td_var_offset(end_fsync)
315 },
316 {
317 .name = "unlink",
318 .type = FIO_OPT_STR_SET,
319 .off1 = td_var_offset(unlink),
320 },
321 {
322 .name = "exitall",
323 .type = FIO_OPT_STR_SET,
324 .cb = str_exitall_cb,
325 },
326 {
327 .name = "stonewall",
328 .type = FIO_OPT_STR_SET,
329 .off1 = td_var_offset(stonewall),
330 },
331 {
332 .name = "thread",
333 .type = FIO_OPT_STR_SET,
334 .off1 = td_var_offset(thread),
335 },
336 {
337 .name = "write_bw_log",
338 .type = FIO_OPT_STR_SET,
339 .off1 = td_var_offset(write_bw_log),
340 },
341 {
342 .name = "write_lat_log",
343 .type = FIO_OPT_STR_SET,
344 .off1 = td_var_offset(write_lat_log),
345 },
346 {
Jens Axboebb8895e2006-10-30 15:14:48 +0100347 .name = "norandommap",
348 .type = FIO_OPT_STR_SET,
349 .off1 = td_var_offset(norandommap),
350 },
351 {
Jens Axboe690adba2006-10-30 15:25:09 +0100352 .name = "bs_unaligned",
353 .type = FIO_OPT_STR_SET,
354 .off1 = td_var_offset(bs_unaligned),
355 },
356 {
Jens Axboee1f36502006-10-27 10:54:08 +0200357 .name = NULL,
358 },
359};
360
Jens Axboeb4692822006-10-27 13:43:22 +0200361#define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
362#define FIO_CMD_OPTS (16)
363#define FIO_GETOPT_JOB (0x89988998)
364
365/*
366 * Command line options. These will contain the above, plus a few
367 * extra that only pertain to fio itself and not jobs.
368 */
369static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
370 {
371 .name = "output",
372 .has_arg = required_argument,
373 .val = 'o',
374 },
375 {
376 .name = "timeout",
377 .has_arg = required_argument,
378 .val = 't',
379 },
380 {
381 .name = "latency-log",
382 .has_arg = required_argument,
383 .val = 'l',
384 },
385 {
386 .name = "bandwidth-log",
387 .has_arg = required_argument,
388 .val = 'b',
389 },
390 {
391 .name = "minimal",
392 .has_arg = optional_argument,
393 .val = 'm',
394 },
395 {
396 .name = "version",
397 .has_arg = no_argument,
398 .val = 'v',
399 },
400 {
401 .name = NULL,
402 },
403};
404
Jens Axboe972cfd22006-06-09 11:08:56 +0200405static int def_timeout = DEF_TIMEOUT;
Jens Axboe972cfd22006-06-09 11:08:56 +0200406
Jens Axboeb4692822006-10-27 13:43:22 +0200407static char fio_version_string[] = "fio 1.7";
Jens Axboeebac4652005-12-08 15:25:21 +0100408
Jens Axboe972cfd22006-06-09 11:08:56 +0200409static char **ini_file;
Jens Axboeebac4652005-12-08 15:25:21 +0100410static int max_jobs = MAX_JOBS;
411
412struct thread_data def_thread;
413struct thread_data *threads = NULL;
414
415int rate_quit = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100416int exitall_on_terminate = 0;
Jens Axboec6ae0a52006-06-12 11:04:44 +0200417int terse_output = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +0200418unsigned long long mlock_size = 0;
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200419FILE *f_out = NULL;
420FILE *f_err = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +0100421
Jens Axboeec94ec52006-10-20 10:59:19 +0200422static int write_lat_log = DEF_WRITE_LAT_LOG;
423static int write_bw_log = DEF_WRITE_BW_LOG;
424
Jens Axboe906c8d72006-06-13 09:37:56 +0200425/*
426 * Return a free job structure.
427 */
Jens Axboeebac4652005-12-08 15:25:21 +0100428static struct thread_data *get_new_job(int global, struct thread_data *parent)
429{
430 struct thread_data *td;
431
432 if (global)
433 return &def_thread;
434 if (thread_number >= max_jobs)
435 return NULL;
436
437 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200438 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100439
Jens Axboeebac4652005-12-08 15:25:21 +0100440 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100441 return td;
442}
443
444static void put_job(struct thread_data *td)
445{
Jens Axboe549577a2006-10-30 12:23:41 +0100446 if (td == &def_thread)
447 return;
448
Jens Axboeebac4652005-12-08 15:25:21 +0100449 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
450 thread_number--;
451}
452
Jens Axboedad915e2006-10-27 11:10:18 +0200453/*
454 * Lazy way of fixing up options that depend on each other. We could also
455 * define option callback handlers, but this is easier.
456 */
Jens Axboee1f36502006-10-27 10:54:08 +0200457static void fixup_options(struct thread_data *td)
458{
Jens Axboee1f36502006-10-27 10:54:08 +0200459 if (!td->rwmixread && td->rwmixwrite)
460 td->rwmixread = 100 - td->rwmixwrite;
Jens Axboedad915e2006-10-27 11:10:18 +0200461
Jens Axboe076efc72006-10-27 11:24:25 +0200462 if (td->write_iolog_file && td->read_iolog_file) {
463 log_err("fio: read iolog overrides write_iolog\n");
464 free(td->write_iolog_file);
465 td->write_iolog_file = NULL;
466 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100467
468 if (td->io_ops->flags & FIO_SYNCIO)
469 td->iodepth = 1;
470 else {
471 if (!td->iodepth)
472 td->iodepth = td->nr_files;
473 }
474
475 /*
476 * only really works for sequential io for now, and with 1 file
477 */
478 if (td->zone_size && !td->sequential && td->nr_files == 1)
479 td->zone_size = 0;
480
481 /*
482 * Reads can do overwrites, we always need to pre-create the file
483 */
484 if (td_read(td) || td_rw(td))
485 td->overwrite = 1;
486
487 if (!td->min_bs)
488 td->min_bs = td->bs;
489 if (!td->max_bs)
490 td->max_bs = td->bs;
491 if (td_read(td) && !td_rw(td))
492 td->verify = 0;
Jens Axboebb8895e2006-10-30 15:14:48 +0100493
494 if (td->norandommap && td->verify != VERIFY_NONE) {
495 log_err("fio: norandommap given, verify disabled\n");
496 td->verify = VERIFY_NONE;
497 }
Jens Axboe690adba2006-10-30 15:25:09 +0100498 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
499 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee1f36502006-10-27 10:54:08 +0200500}
501
Jens Axboe906c8d72006-06-13 09:37:56 +0200502/*
503 * Adds a job to the list of things todo. Sanitizes the various options
504 * to make sure we don't have conflicts, and initializes various
505 * members of td.
506 */
Jens Axboe75154842006-06-01 13:56:09 +0200507static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100508{
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200509 char *ddir_str[] = { "read", "write", "randread", "randwrite",
510 "rw", NULL, "randrw" };
Jens Axboeebac4652005-12-08 15:25:21 +0100511 struct stat sb;
Jens Axboe53cdc682006-10-18 11:50:58 +0200512 int numjobs, ddir, i;
513 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100514
515#ifndef FIO_HAVE_LIBAIO
516 if (td->io_engine == FIO_LIBAIO) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200517 log_err("Linux libaio not available\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100518 return 1;
519 }
520#endif
521#ifndef FIO_HAVE_POSIXAIO
522 if (td->io_engine == FIO_POSIXAIO) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200523 log_err("posix aio not available\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100524 return 1;
525 }
526#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100527
528 /*
529 * the def_thread is just for options, it's not a real job
530 */
531 if (td == &def_thread)
532 return 0;
533
Jens Axboedf641192006-10-12 07:55:41 +0200534 /*
535 * Set default io engine, if none set
536 */
537 if (!td->io_ops) {
538 td->io_ops = load_ioengine(td, DEF_IO_ENGINE_NAME);
539 if (!td->io_ops) {
540 log_err("default engine %s not there?\n", DEF_IO_ENGINE_NAME);
541 return 1;
542 }
543 }
544
Jens Axboe690adba2006-10-30 15:25:09 +0100545 if (td->odirect)
546 td->io_ops->flags |= FIO_RAWIO;
547
Jens Axboe16b462a2006-10-30 12:35:18 +0100548 fixup_options(td);
Jens Axboe9cc935a2006-06-13 09:11:18 +0200549
Jens Axboeebac4652005-12-08 15:25:21 +0100550 td->filetype = FIO_TYPE_FILE;
Jens Axboe0af7b542006-02-17 10:10:12 +0100551 if (!stat(jobname, &sb)) {
552 if (S_ISBLK(sb.st_mode))
553 td->filetype = FIO_TYPE_BD;
554 else if (S_ISCHR(sb.st_mode))
555 td->filetype = FIO_TYPE_CHAR;
556 }
Jens Axboeebac4652005-12-08 15:25:21 +0100557
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200558 if (td->filename)
559 td->nr_uniq_files = 1;
560 else
561 td->nr_uniq_files = td->nr_files;
562
563 if (td->filetype == FIO_TYPE_FILE || td->filename) {
Jens Axboee9c047a2006-06-07 21:13:04 +0200564 char tmp[PATH_MAX];
Jens Axboe53cdc682006-10-18 11:50:58 +0200565 int len = 0;
566 int i;
Jens Axboee9c047a2006-06-07 21:13:04 +0200567
Jens Axboeef899b62006-06-06 09:31:00 +0200568 if (td->directory && td->directory[0] != '\0')
Jens Axboe53cdc682006-10-18 11:50:58 +0200569 sprintf(tmp, "%s/", td->directory);
Jens Axboeebac4652005-12-08 15:25:21 +0100570
Jens Axboe53cdc682006-10-18 11:50:58 +0200571 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
572
573 for_each_file(td, f, i) {
574 memset(f, 0, sizeof(*f));
575 f->fd = -1;
576
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200577 if (td->filename)
578 sprintf(tmp + len, "%s", td->filename);
579 else
580 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
Jens Axboe53cdc682006-10-18 11:50:58 +0200581 f->file_name = strdup(tmp);
582 }
583 } else {
584 td->nr_files = 1;
585 td->files = malloc(sizeof(struct fio_file));
586 f = &td->files[0];
587
588 memset(f, 0, sizeof(*f));
589 f->fd = -1;
590 f->file_name = strdup(jobname);
591 }
592
593 for_each_file(td, f, i) {
594 f->file_size = td->total_file_size / td->nr_files;
595 f->file_offset = td->start_offset;
596 }
597
Jens Axboebbfd6b02006-06-07 19:42:54 +0200598 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100599
600 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
601 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
602 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
603
Jens Axboeebac4652005-12-08 15:25:21 +0100604 if (td->stonewall && td->thread_number > 1)
605 groupid++;
606
607 td->groupid = groupid;
608
609 if (setup_rate(td))
610 goto err;
611
Jens Axboeec94ec52006-10-20 10:59:19 +0200612 if (td->write_lat_log) {
Jens Axboeebac4652005-12-08 15:25:21 +0100613 setup_log(&td->slat_log);
614 setup_log(&td->clat_log);
615 }
Jens Axboeec94ec52006-10-20 10:59:19 +0200616 if (td->write_bw_log)
Jens Axboeebac4652005-12-08 15:25:21 +0100617 setup_log(&td->bw_log);
618
Jens Axboeb4692822006-10-27 13:43:22 +0200619 if (!td->name)
620 td->name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200621
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200622 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
Jens Axboe75154842006-06-01 13:56:09 +0200623
Jens Axboec6ae0a52006-06-12 11:04:44 +0200624 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200625 if (!job_add_num) {
Jens Axboe2866c822006-10-09 15:57:48 +0200626 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200627 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
628 else
Jens Axboe2866c822006-10-09 15:57:48 +0200629 fprintf(f_out, "%s: (g=%d): rw=%s, odir=%d, bs=%d-%d, rate=%d, ioengine=%s, iodepth=%d\n", td->name, td->groupid, ddir_str[ddir], td->odirect, td->min_bs, td->max_bs, td->rate, td->io_ops->name, td->iodepth);
Jens Axboeb990b5c2006-09-14 09:48:22 +0200630 } else if (job_add_num == 1)
Jens Axboec6ae0a52006-06-12 11:04:44 +0200631 fprintf(f_out, "...\n");
632 }
Jens Axboeebac4652005-12-08 15:25:21 +0100633
634 /*
635 * recurse add identical jobs, clear numjobs and stonewall options
636 * as they don't apply to sub-jobs
637 */
638 numjobs = td->numjobs;
639 while (--numjobs) {
640 struct thread_data *td_new = get_new_job(0, td);
641
642 if (!td_new)
643 goto err;
644
645 td_new->numjobs = 1;
646 td_new->stonewall = 0;
Jens Axboe75154842006-06-01 13:56:09 +0200647 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100648
Jens Axboe75154842006-06-01 13:56:09 +0200649 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100650 goto err;
651 }
652 return 0;
653err:
654 put_job(td);
655 return -1;
656}
657
Jens Axboe906c8d72006-06-13 09:37:56 +0200658/*
659 * Initialize the various random states we need (random io, block size ranges,
660 * read/write mix, etc).
661 */
Jens Axboeebac4652005-12-08 15:25:21 +0100662int init_random_state(struct thread_data *td)
663{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200664 unsigned long seeds[4];
Jens Axboe53cdc682006-10-18 11:50:58 +0200665 int fd, num_maps, blocks, i;
Jens Axboe0ab8db82006-10-18 17:16:23 +0200666 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100667
Jens Axboef48b4672006-10-27 11:30:07 +0200668 if (td->io_ops->flags & FIO_CPUIO)
669 return 0;
670
Jens Axboe1ac267b2006-05-31 20:29:00 +0200671 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100672 if (fd == -1) {
673 td_verror(td, errno);
674 return 1;
675 }
676
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200677 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100678 td_verror(td, EIO);
679 close(fd);
680 return 1;
681 }
682
683 close(fd);
684
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200685 os_random_seed(seeds[0], &td->bsrange_state);
686 os_random_seed(seeds[1], &td->verify_state);
687 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100688
689 if (td->sequential)
690 return 0;
691
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200692 if (td->rand_repeatable)
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200693 seeds[3] = DEF_RANDSEED;
Jens Axboeebac4652005-12-08 15:25:21 +0100694
Jens Axboebb8895e2006-10-30 15:14:48 +0100695 if (!td->norandommap) {
696 for_each_file(td, f, i) {
697 blocks = (f->file_size + td->min_bs - 1) / td->min_bs;
698 num_maps = blocks / BLOCKS_PER_MAP;
699 f->file_map = malloc(num_maps * sizeof(long));
700 f->num_maps = num_maps;
701 memset(f->file_map, 0, num_maps * sizeof(long));
702 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200703 }
Jens Axboeebac4652005-12-08 15:25:21 +0100704
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200705 os_random_seed(seeds[3], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100706 return 0;
707}
708
709static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
710{
711#ifdef FIO_HAVE_CPU_AFFINITY
712 unsigned int i;
713
714 CPU_ZERO(&cpumask);
715
716 for (i = 0; i < sizeof(int) * 8; i++) {
717 if ((1 << i) & cpu)
718 CPU_SET(i, &cpumask);
719 }
720#endif
721}
722
Jens Axboeebac4652005-12-08 15:25:21 +0100723static int is_empty_or_comment(char *line)
724{
725 unsigned int i;
726
727 for (i = 0; i < strlen(line); i++) {
728 if (line[i] == ';')
729 return 1;
730 if (!isspace(line[i]) && !iscntrl(line[i]))
731 return 0;
732 }
733
734 return 1;
735}
736
Jens Axboeb4692822006-10-27 13:43:22 +0200737static int str_rw_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100738{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200739 struct thread_data *td = data;
740
Jens Axboeebac4652005-12-08 15:25:21 +0100741 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
742 td->ddir = DDIR_READ;
743 td->sequential = 1;
744 return 0;
745 } else if (!strncmp(mem, "randread", 8)) {
746 td->ddir = DDIR_READ;
747 td->sequential = 0;
748 return 0;
749 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
750 td->ddir = DDIR_WRITE;
751 td->sequential = 1;
752 return 0;
753 } else if (!strncmp(mem, "randwrite", 9)) {
754 td->ddir = DDIR_WRITE;
755 td->sequential = 0;
756 return 0;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200757 } else if (!strncmp(mem, "rw", 2)) {
758 td->ddir = 0;
759 td->iomix = 1;
760 td->sequential = 1;
761 return 0;
762 } else if (!strncmp(mem, "randrw", 6)) {
763 td->ddir = 0;
764 td->iomix = 1;
765 td->sequential = 0;
766 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100767 }
768
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200769 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100770 return 1;
771}
772
Jens Axboeb4692822006-10-27 13:43:22 +0200773static int str_verify_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100774{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200775 struct thread_data *td = data;
776
Jens Axboeebac4652005-12-08 15:25:21 +0100777 if (!strncmp(mem, "0", 1)) {
778 td->verify = VERIFY_NONE;
779 return 0;
780 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
781 td->verify = VERIFY_MD5;
782 return 0;
783 } else if (!strncmp(mem, "crc32", 5)) {
784 td->verify = VERIFY_CRC32;
785 return 0;
786 }
787
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200788 log_err("fio: verify types: md5, crc32\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100789 return 1;
790}
791
Jens Axboeb4692822006-10-27 13:43:22 +0200792static int str_mem_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100793{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200794 struct thread_data *td = data;
795
Jens Axboeebac4652005-12-08 15:25:21 +0100796 if (!strncmp(mem, "malloc", 6)) {
797 td->mem_type = MEM_MALLOC;
798 return 0;
799 } else if (!strncmp(mem, "shm", 3)) {
800 td->mem_type = MEM_SHM;
801 return 0;
802 } else if (!strncmp(mem, "mmap", 4)) {
803 td->mem_type = MEM_MMAP;
804 return 0;
805 }
806
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200807 log_err("fio: mem type: malloc, shm, mmap\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100808 return 1;
809}
810
Jens Axboeb4692822006-10-27 13:43:22 +0200811static int str_ioengine_cb(void *data, const char *str)
Jens Axboeebac4652005-12-08 15:25:21 +0100812{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200813 struct thread_data *td = data;
814
Jens Axboe2866c822006-10-09 15:57:48 +0200815 td->io_ops = load_ioengine(td, str);
816 if (td->io_ops)
Jens Axboeebac4652005-12-08 15:25:21 +0100817 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100818
Jens Axboeb990b5c2006-09-14 09:48:22 +0200819 log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice, cpu\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100820 return 1;
821}
822
Jens Axboee1f36502006-10-27 10:54:08 +0200823static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
824{
825 mlock_size = *val;
826 return 0;
827}
828
829static int str_prioclass_cb(void *data, unsigned int *val)
830{
831 struct thread_data *td = data;
832
833 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
834 return 0;
835}
836
837static int str_prio_cb(void *data, unsigned int *val)
838{
839 struct thread_data *td = data;
840
841 td->ioprio |= *val;
842 return 0;
843}
844
845static int str_exitall_cb(void)
846{
847 exitall_on_terminate = 1;
848 return 0;
849}
850
851static int str_cpumask_cb(void *data, unsigned int *val)
852{
853 struct thread_data *td = data;
854
855 fill_cpu_mask(td->cpumask, *val);
856 return 0;
857}
858
Jens Axboe07261982006-06-07 10:51:12 +0200859/*
860 * This is our [ini] type file parser.
861 */
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200862int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100863{
Jens Axboee1f36502006-10-27 10:54:08 +0200864 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +0100865 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +0100866 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +0100867 fpos_t off;
868 FILE *f;
869 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200870 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +0100871
872 f = fopen(file, "r");
873 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200874 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100875 return 1;
876 }
877
878 string = malloc(4096);
879 name = malloc(256);
Jens Axboefee3bb42006-10-30 13:32:08 +0100880 memset(name, 0, 256);
Jens Axboeebac4652005-12-08 15:25:21 +0100881
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200882 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100883 do {
884 p = fgets(string, 4095, f);
885 if (!p)
Jens Axboe45410ac2006-06-07 11:10:39 +0200886 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100887 if (is_empty_or_comment(p))
888 continue;
Jens Axboefee3bb42006-10-30 13:32:08 +0100889 if (sscanf(p, "[%255s]", name) != 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100890 continue;
891
892 global = !strncmp(name, "global", 6);
893
894 name[strlen(name) - 1] = '\0';
895
896 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200897 if (!td) {
898 ret = 1;
899 break;
900 }
Jens Axboeebac4652005-12-08 15:25:21 +0100901
Jens Axboe972cfd22006-06-09 11:08:56 +0200902 /*
903 * Seperate multiple job files by a stonewall
904 */
Jens Axboef9481912006-06-09 11:37:28 +0200905 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +0200906 td->stonewall = stonewall;
907 stonewall = 0;
908 }
909
Jens Axboeebac4652005-12-08 15:25:21 +0100910 fgetpos(f, &off);
911 while ((p = fgets(string, 4096, f)) != NULL) {
912 if (is_empty_or_comment(p))
913 continue;
Jens Axboee1f36502006-10-27 10:54:08 +0200914
Jens Axboeb6754f92006-05-30 14:29:32 +0200915 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +0100916
917 if (p[0] == '[')
918 break;
919
Jens Axboe4ae3f762006-05-30 13:35:14 +0200920 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200921
Jens Axboee1f36502006-10-27 10:54:08 +0200922 fgetpos(f, &off);
Jens Axboeebac4652005-12-08 15:25:21 +0100923
Jens Axboe45410ac2006-06-07 11:10:39 +0200924 /*
925 * Don't break here, continue parsing options so we
926 * dump all the bad ones. Makes trial/error fixups
927 * easier on the user.
928 */
Jens Axboe7c124ac2006-10-30 13:36:52 +0100929 ret |= parse_option(p, options, td);
Jens Axboeebac4652005-12-08 15:25:21 +0100930 }
Jens Axboeebac4652005-12-08 15:25:21 +0100931
Jens Axboe45410ac2006-06-07 11:10:39 +0200932 if (!ret) {
933 fsetpos(f, &off);
934 ret = add_job(td, name, 0);
935 }
Jens Axboe7c124ac2006-10-30 13:36:52 +0100936 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +0100937
938 free(string);
939 free(name);
940 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200941 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100942}
943
944static int fill_def_thread(void)
945{
946 memset(&def_thread, 0, sizeof(def_thread));
947
948 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
949 perror("sched_getaffinity");
950 return 1;
951 }
952
953 /*
954 * fill globals
955 */
956 def_thread.ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200957 def_thread.iomix = 0;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200958 def_thread.bs = DEF_BS;
Jens Axboe16b462a2006-10-30 12:35:18 +0100959 def_thread.min_bs = 0;
960 def_thread.max_bs = 0;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200961 def_thread.odirect = DEF_ODIRECT;
Jens Axboeebac4652005-12-08 15:25:21 +0100962 def_thread.ratecycle = DEF_RATE_CYCLE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200963 def_thread.sequential = DEF_SEQUENTIAL;
Jens Axboe972cfd22006-06-09 11:08:56 +0200964 def_thread.timeout = def_timeout;
Jens Axboeebac4652005-12-08 15:25:21 +0100965 def_thread.overwrite = DEF_OVERWRITE;
966 def_thread.invalidate_cache = DEF_INVALIDATE;
967 def_thread.sync_io = DEF_SYNCIO;
968 def_thread.mem_type = MEM_MALLOC;
969 def_thread.bw_avg_time = DEF_BWAVGTIME;
970 def_thread.create_serialize = DEF_CREATE_SER;
971 def_thread.create_fsync = DEF_CREATE_FSYNC;
972 def_thread.loops = DEF_LOOPS;
973 def_thread.verify = DEF_VERIFY;
974 def_thread.stonewall = DEF_STONEWALL;
975 def_thread.numjobs = DEF_NUMJOBS;
976 def_thread.use_thread = DEF_USE_THREAD;
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200977 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
978 def_thread.rwmixread = DEF_RWMIX_READ;
Jens Axboeb6f4d882006-06-02 10:32:51 +0200979 def_thread.nice = DEF_NICE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200980 def_thread.rand_repeatable = DEF_RAND_REPEAT;
Jens Axboe53cdc682006-10-18 11:50:58 +0200981 def_thread.nr_files = DEF_NR_FILES;
Jens Axboef6cbb262006-10-18 14:16:42 +0200982 def_thread.unlink = DEF_UNLINK;
Jens Axboeec94ec52006-10-20 10:59:19 +0200983 def_thread.write_bw_log = write_bw_log;
984 def_thread.write_lat_log = write_lat_log;
Jens Axboebb8895e2006-10-30 15:14:48 +0100985 def_thread.norandommap = DEF_NO_RAND_MAP;
Jens Axboeebac4652005-12-08 15:25:21 +0100986#ifdef FIO_HAVE_DISK_UTIL
987 def_thread.do_disk_util = 1;
988#endif
989
990 return 0;
991}
992
Jens Axboe0ab8db82006-10-18 17:16:23 +0200993static void usage(void)
Jens Axboe4785f992006-05-26 03:59:10 +0200994{
995 printf("%s\n", fio_version_string);
Jens Axboeb4692822006-10-27 13:43:22 +0200996 printf("\t--output\tWrite output to file\n");
997 printf("\t--timeout\tRuntime in seconds\n");
998 printf("\t--latency-log\tGenerate per-job latency logs\n");
999 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1000 printf("\t--minimal\tMinimal (terse) output\n");
1001 printf("\t--version\tPrint version info and exit\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001002}
1003
Jens Axboe972cfd22006-06-09 11:08:56 +02001004static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +01001005{
Jens Axboeb4692822006-10-27 13:43:22 +02001006 struct thread_data *td = NULL;
Jens Axboec2b1e752006-10-30 09:03:13 +01001007 int c, ini_idx = 0, lidx, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001008
Jens Axboeb4692822006-10-27 13:43:22 +02001009 while ((c = getopt_long(argc, argv, "", long_options, &lidx)) != -1) {
Jens Axboeebac4652005-12-08 15:25:21 +01001010 switch (c) {
Jens Axboeb4692822006-10-27 13:43:22 +02001011 case 't':
1012 def_timeout = atoi(optarg);
1013 break;
1014 case 'l':
1015 write_lat_log = 1;
1016 break;
1017 case 'w':
1018 write_bw_log = 1;
1019 break;
1020 case 'o':
1021 f_out = fopen(optarg, "w+");
1022 if (!f_out) {
1023 perror("fopen output");
1024 exit(1);
1025 }
1026 f_err = f_out;
1027 break;
1028 case 'm':
1029 terse_output = 1;
1030 break;
1031 case 'h':
1032 usage();
1033 exit(0);
1034 case 'v':
1035 printf("%s\n", fio_version_string);
1036 exit(0);
1037 case FIO_GETOPT_JOB: {
1038 const char *opt = long_options[lidx].name;
1039 char *val = optarg;
1040
Jens Axboec2b1e752006-10-30 09:03:13 +01001041 if (!strncmp(opt, "name", 4) && td) {
1042 ret = add_job(td, td->name ?: "fio", 0);
1043 if (ret) {
1044 put_job(td);
1045 return 0;
1046 }
1047 td = NULL;
1048 }
Jens Axboeb4692822006-10-27 13:43:22 +02001049 if (!td) {
Jens Axboe38d0adb2006-10-30 12:21:25 +01001050 int global = !strncmp(val, "global", 6);
Jens Axboec2b1e752006-10-30 09:03:13 +01001051
1052 td = get_new_job(global, &def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001053 if (!td)
1054 return 0;
1055 }
Jens Axboe38d0adb2006-10-30 12:21:25 +01001056
1057 parse_cmd_option(opt, val, options, td);
Jens Axboeb4692822006-10-27 13:43:22 +02001058 break;
1059 }
1060 default:
1061 printf("optarg <<%s>>\n", argv[optind]);
1062 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001063 }
1064 }
Jens Axboec9fad892006-05-27 17:26:50 +02001065
Jens Axboeb4692822006-10-27 13:43:22 +02001066 if (td) {
Jens Axboec2b1e752006-10-30 09:03:13 +01001067 ret = add_job(td, td->name ?: "fio", 0);
Jens Axboeb4692822006-10-27 13:43:22 +02001068 if (ret)
1069 put_job(td);
Jens Axboe972cfd22006-06-09 11:08:56 +02001070 }
Jens Axboe774a6172006-08-24 08:44:26 +02001071
Jens Axboeb4692822006-10-27 13:43:22 +02001072 while (optind < argc) {
1073 ini_idx++;
1074 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1075 ini_file[ini_idx - 1] = strdup(argv[optind]);
1076 optind++;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001077 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001078
1079 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +01001080}
1081
1082static void free_shm(void)
1083{
1084 struct shmid_ds sbuf;
1085
1086 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001087 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001088 threads = NULL;
1089 shmctl(shm_id, IPC_RMID, &sbuf);
1090 }
1091}
1092
Jens Axboe906c8d72006-06-13 09:37:56 +02001093/*
1094 * The thread area is shared between the main process and the job
1095 * threads/processes. So setup a shared memory segment that will hold
1096 * all the job info.
1097 */
Jens Axboeebac4652005-12-08 15:25:21 +01001098static int setup_thread_area(void)
1099{
1100 /*
1101 * 1024 is too much on some machines, scale max_jobs if
1102 * we get a failure that looks like too large a shm segment
1103 */
1104 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001105 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001106
Jens Axboe906c8d72006-06-13 09:37:56 +02001107 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001108 if (shm_id != -1)
1109 break;
1110 if (errno != EINVAL) {
1111 perror("shmget");
1112 break;
1113 }
1114
1115 max_jobs >>= 1;
1116 } while (max_jobs);
1117
1118 if (shm_id == -1)
1119 return 1;
1120
1121 threads = shmat(shm_id, NULL, 0);
1122 if (threads == (void *) -1) {
1123 perror("shmat");
1124 return 1;
1125 }
1126
1127 atexit(free_shm);
1128 return 0;
1129}
1130
Jens Axboeb4692822006-10-27 13:43:22 +02001131/*
1132 * Copy the fio options into the long options map, so we mirror
1133 * job and cmd line options.
1134 */
1135static void dupe_job_options(void)
1136{
1137 struct fio_option *o;
1138 unsigned int i;
1139
1140 i = 0;
1141 while (long_options[i].name)
1142 i++;
1143
1144 o = &options[0];
1145 while (o->name) {
1146 long_options[i].name = o->name;
1147 long_options[i].val = FIO_GETOPT_JOB;
1148 if (o->type == FIO_OPT_STR_SET)
1149 long_options[i].has_arg = no_argument;
1150 else
1151 long_options[i].has_arg = required_argument;
1152
1153 i++;
1154 o++;
1155 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1156 }
1157}
1158
Jens Axboeebac4652005-12-08 15:25:21 +01001159int parse_options(int argc, char *argv[])
1160{
Jens Axboe972cfd22006-06-09 11:08:56 +02001161 int job_files, i;
1162
Jens Axboeb4692822006-10-27 13:43:22 +02001163 f_out = stdout;
1164 f_err = stderr;
1165
1166 dupe_job_options();
1167
Jens Axboeebac4652005-12-08 15:25:21 +01001168 if (setup_thread_area())
1169 return 1;
1170 if (fill_def_thread())
1171 return 1;
1172
Jens Axboe972cfd22006-06-09 11:08:56 +02001173 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001174
Jens Axboe972cfd22006-06-09 11:08:56 +02001175 for (i = 0; i < job_files; i++) {
1176 if (fill_def_thread())
1177 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001178 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001179 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001180 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001181 }
Jens Axboeebac4652005-12-08 15:25:21 +01001182
Jens Axboe88c6ed82006-06-09 11:28:10 +02001183 free(ini_file);
Jens Axboeb4692822006-10-27 13:43:22 +02001184
1185 if (!thread_number) {
1186 log_err("No jobs defined(s)\n");
1187 usage();
1188 return 1;
1189 }
1190
Jens Axboeebac4652005-12-08 15:25:21 +01001191 return 0;
1192}