blob: 06b10a9b2cc723d5cebb49f253e7a1922701e32e [file] [log] [blame]
Jens Axboe906c8d72006-06-13 09:37:56 +02001/*
Jens Axboecb2c86f2006-10-26 13:48:34 +02002 * This file contains job initialization and setup functions.
Jens Axboe906c8d72006-06-13 09:37:56 +02003 */
Jens Axboeebac4652005-12-08 15:25:21 +01004#include <stdio.h>
5#include <stdlib.h>
6#include <unistd.h>
7#include <fcntl.h>
8#include <ctype.h>
9#include <string.h>
10#include <errno.h>
Jens Axboeb4692822006-10-27 13:43:22 +020011#include <getopt.h>
12#include <assert.h>
Jens Axboeebac4652005-12-08 15:25:21 +010013#include <sys/ipc.h>
14#include <sys/shm.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17
18#include "fio.h"
Jens Axboecb2c86f2006-10-26 13:48:34 +020019#include "parse.h"
Jens Axboeebac4652005-12-08 15:25:21 +010020
Jens Axboe906c8d72006-06-13 09:37:56 +020021/*
22 * The default options
23 */
Jens Axboe20dc95c2005-12-09 10:29:35 +010024#define DEF_BS (4096)
25#define DEF_TIMEOUT (0)
26#define DEF_RATE_CYCLE (1000)
27#define DEF_ODIRECT (1)
28#define DEF_IO_ENGINE (FIO_SYNCIO)
Jens Axboeebac4652005-12-08 15:25:21 +010029#define DEF_IO_ENGINE_NAME "sync"
Jens Axboe20dc95c2005-12-09 10:29:35 +010030#define DEF_SEQUENTIAL (1)
31#define DEF_RAND_REPEAT (1)
Jens Axboe178d11f2006-12-15 13:11:24 +010032#define DEF_OVERWRITE (0)
Jens Axboe20dc95c2005-12-09 10:29:35 +010033#define DEF_INVALIDATE (1)
34#define DEF_SYNCIO (0)
35#define DEF_RANDSEED (0xb1899bedUL)
36#define DEF_BWAVGTIME (500)
37#define DEF_CREATE_SER (1)
Jens Axboeebac4652005-12-08 15:25:21 +010038#define DEF_CREATE_FSYNC (1)
Jens Axboe20dc95c2005-12-09 10:29:35 +010039#define DEF_LOOPS (1)
40#define DEF_VERIFY (0)
41#define DEF_STONEWALL (0)
42#define DEF_NUMJOBS (1)
43#define DEF_USE_THREAD (0)
44#define DEF_FILE_SIZE (1024 * 1024 * 1024UL)
45#define DEF_ZONE_SIZE (0)
46#define DEF_ZONE_SKIP (0)
Jens Axboea6ccc7b2006-06-02 10:14:15 +020047#define DEF_RWMIX_CYCLE (500)
48#define DEF_RWMIX_READ (50)
Jens Axboeb6f4d882006-06-02 10:32:51 +020049#define DEF_NICE (0)
Jens Axboe53cdc682006-10-18 11:50:58 +020050#define DEF_NR_FILES (1)
Jens Axboe178d11f2006-12-15 13:11:24 +010051#define DEF_UNLINK (1)
Jens Axboeec94ec52006-10-20 10:59:19 +020052#define DEF_WRITE_BW_LOG (0)
53#define DEF_WRITE_LAT_LOG (0)
Jens Axboebb8895e2006-10-30 15:14:48 +010054#define DEF_NO_RAND_MAP (0)
Jens Axboe56bb17f2006-12-20 20:27:36 +010055#define DEF_HUGEPAGE_SIZE FIO_HUGE_PAGE
Jens Axboe9c1f7432007-01-03 20:43:19 +010056#define DEF_THINKTIME_BLOCKS (1)
Jens Axboeebac4652005-12-08 15:25:21 +010057
Jens Axboee1f36502006-10-27 10:54:08 +020058#define td_var_offset(var) ((size_t) &((struct thread_data *)0)->var)
59
Jens Axboeb4692822006-10-27 13:43:22 +020060static int str_rw_cb(void *, const char *);
61static int str_ioengine_cb(void *, const char *);
62static int str_mem_cb(void *, const char *);
63static int str_verify_cb(void *, const char *);
Jens Axboee1f36502006-10-27 10:54:08 +020064static int str_lockmem_cb(void *, unsigned long *);
Jens Axboe34cfcda2006-11-03 14:00:45 +010065#ifdef FIO_HAVE_IOPRIO
Jens Axboee1f36502006-10-27 10:54:08 +020066static int str_prio_cb(void *, unsigned int *);
67static int str_prioclass_cb(void *, unsigned int *);
Jens Axboe34cfcda2006-11-03 14:00:45 +010068#endif
Jens Axboee1f36502006-10-27 10:54:08 +020069static int str_exitall_cb(void);
70static int str_cpumask_cb(void *, unsigned int *);
71
72/*
73 * Map of job/command line options
74 */
75static struct fio_option options[] = {
76 {
77 .name = "name",
78 .type = FIO_OPT_STR_STORE,
79 .off1 = td_var_offset(name),
80 },
81 {
82 .name = "directory",
83 .type = FIO_OPT_STR_STORE,
84 .off1 = td_var_offset(directory),
85 },
86 {
87 .name = "filename",
88 .type = FIO_OPT_STR_STORE,
89 .off1 = td_var_offset(filename),
90 },
91 {
92 .name = "rw",
93 .type = FIO_OPT_STR,
94 .cb = str_rw_cb,
95 },
96 {
97 .name = "ioengine",
98 .type = FIO_OPT_STR,
99 .cb = str_ioengine_cb,
100 },
101 {
102 .name = "mem",
103 .type = FIO_OPT_STR,
104 .cb = str_mem_cb,
105 },
106 {
107 .name = "verify",
108 .type = FIO_OPT_STR,
109 .cb = str_verify_cb,
110 },
111 {
112 .name = "write_iolog",
Jens Axboe076efc72006-10-27 11:24:25 +0200113 .type = FIO_OPT_STR_STORE,
114 .off1 = td_var_offset(write_iolog_file),
Jens Axboee1f36502006-10-27 10:54:08 +0200115 },
116 {
Jens Axboe076efc72006-10-27 11:24:25 +0200117 .name = "read_iolog",
Jens Axboee1f36502006-10-27 10:54:08 +0200118 .type = FIO_OPT_STR_STORE,
Jens Axboe076efc72006-10-27 11:24:25 +0200119 .off1 = td_var_offset(read_iolog_file),
Jens Axboee1f36502006-10-27 10:54:08 +0200120 },
121 {
122 .name = "exec_prerun",
123 .type = FIO_OPT_STR_STORE,
124 .off1 = td_var_offset(exec_prerun),
125 },
126 {
127 .name = "exec_postrun",
128 .type = FIO_OPT_STR_STORE,
129 .off1 = td_var_offset(exec_postrun),
130 },
131#ifdef FIO_HAVE_IOSCHED_SWITCH
132 {
133 .name = "ioscheduler",
134 .type = FIO_OPT_STR_STORE,
135 .off1 = td_var_offset(ioscheduler),
136 },
137#endif
138 {
139 .name = "size",
140 .type = FIO_OPT_STR_VAL,
141 .off1 = td_var_offset(total_file_size),
142 },
143 {
144 .name = "bs",
Jens Axboe75e6f362006-11-03 08:17:09 +0100145 .type = FIO_OPT_STR_VAL_INT,
Jens Axboea00735e2006-11-03 08:58:08 +0100146 .off1 = td_var_offset(bs[DDIR_READ]),
Jens Axboef90eff52006-11-06 11:08:21 +0100147 .off2 = td_var_offset(bs[DDIR_WRITE]),
Jens Axboee1f36502006-10-27 10:54:08 +0200148 },
149 {
150 .name = "offset",
151 .type = FIO_OPT_STR_VAL,
152 .off1 = td_var_offset(start_offset),
153 },
154 {
155 .name = "zonesize",
156 .type = FIO_OPT_STR_VAL,
157 .off1 = td_var_offset(zone_size),
158 },
159 {
160 .name = "zoneskip",
161 .type = FIO_OPT_STR_VAL,
162 .off1 = td_var_offset(zone_skip),
163 },
164 {
165 .name = "lockmem",
166 .type = FIO_OPT_STR_VAL,
167 .cb = str_lockmem_cb,
168 },
169 {
170 .name = "bsrange",
171 .type = FIO_OPT_RANGE,
Jens Axboea00735e2006-11-03 08:58:08 +0100172 .off1 = td_var_offset(min_bs[DDIR_READ]),
173 .off2 = td_var_offset(max_bs[DDIR_READ]),
Jens Axboef90eff52006-11-06 11:08:21 +0100174 .off3 = td_var_offset(min_bs[DDIR_WRITE]),
175 .off4 = td_var_offset(max_bs[DDIR_WRITE]),
Jens Axboee1f36502006-10-27 10:54:08 +0200176 },
177 {
178 .name = "nrfiles",
179 .type = FIO_OPT_INT,
180 .off1 = td_var_offset(nr_files),
181 },
182 {
183 .name = "iodepth",
184 .type = FIO_OPT_INT,
185 .off1 = td_var_offset(iodepth),
186 },
187 {
188 .name = "fsync",
189 .type = FIO_OPT_INT,
190 .off1 = td_var_offset(fsync_blocks),
191 },
192 {
193 .name = "rwmixcycle",
194 .type = FIO_OPT_INT,
195 .off1 = td_var_offset(rwmixcycle),
196 },
197 {
198 .name = "rwmixread",
199 .type = FIO_OPT_INT,
200 .off1 = td_var_offset(rwmixread),
201 .max_val= 100,
202 },
203 {
204 .name = "rwmixwrite",
205 .type = FIO_OPT_INT,
206 .off1 = td_var_offset(rwmixwrite),
207 .max_val= 100,
208 },
209 {
210 .name = "nice",
211 .type = FIO_OPT_INT,
212 .off1 = td_var_offset(nice),
213 },
214#ifdef FIO_HAVE_IOPRIO
215 {
216 .name = "prio",
217 .type = FIO_OPT_INT,
218 .cb = str_prio_cb,
219 },
220 {
221 .name = "prioclass",
222 .type = FIO_OPT_INT,
223 .cb = str_prioclass_cb,
224 },
225#endif
226 {
227 .name = "thinktime",
228 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100229 .off1 = td_var_offset(thinktime),
Jens Axboee1f36502006-10-27 10:54:08 +0200230 },
231 {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100232 .name = "thinktime_blocks",
233 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100234 .off1 = td_var_offset(thinktime_blocks),
Jens Axboe9c1f7432007-01-03 20:43:19 +0100235 },
236 {
Jens Axboee1f36502006-10-27 10:54:08 +0200237 .name = "rate",
238 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100239 .off1 = td_var_offset(rate),
Jens Axboee1f36502006-10-27 10:54:08 +0200240 },
241 {
242 .name = "ratemin",
243 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100244 .off1 = td_var_offset(ratemin),
Jens Axboee1f36502006-10-27 10:54:08 +0200245 },
246 {
247 .name = "ratecycle",
248 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100249 .off1 = td_var_offset(ratecycle),
Jens Axboee1f36502006-10-27 10:54:08 +0200250 },
251 {
252 .name = "startdelay",
253 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100254 .off1 = td_var_offset(start_delay),
Jens Axboee1f36502006-10-27 10:54:08 +0200255 },
256 {
257 .name = "timeout",
258 .type = FIO_OPT_STR_VAL_TIME,
Jens Axboe13049232007-01-03 20:45:09 +0100259 .off1 = td_var_offset(timeout),
Jens Axboee1f36502006-10-27 10:54:08 +0200260 },
261 {
262 .name = "invalidate",
263 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100264 .off1 = td_var_offset(invalidate_cache),
Jens Axboee1f36502006-10-27 10:54:08 +0200265 },
266 {
267 .name = "sync",
268 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100269 .off1 = td_var_offset(sync_io),
Jens Axboee1f36502006-10-27 10:54:08 +0200270 },
271 {
272 .name = "bwavgtime",
273 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100274 .off1 = td_var_offset(bw_avg_time),
Jens Axboee1f36502006-10-27 10:54:08 +0200275 },
276 {
277 .name = "create_serialize",
278 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100279 .off1 = td_var_offset(create_serialize),
Jens Axboee1f36502006-10-27 10:54:08 +0200280 },
281 {
282 .name = "create_fsync",
283 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100284 .off1 = td_var_offset(create_fsync),
Jens Axboee1f36502006-10-27 10:54:08 +0200285 },
286 {
287 .name = "loops",
288 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100289 .off1 = td_var_offset(loops),
Jens Axboee1f36502006-10-27 10:54:08 +0200290 },
291 {
292 .name = "numjobs",
293 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100294 .off1 = td_var_offset(numjobs),
Jens Axboee1f36502006-10-27 10:54:08 +0200295 },
296 {
297 .name = "cpuload",
298 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100299 .off1 = td_var_offset(cpuload),
Jens Axboee1f36502006-10-27 10:54:08 +0200300 },
301 {
302 .name = "cpuchunks",
303 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100304 .off1 = td_var_offset(cpucycle),
Jens Axboee1f36502006-10-27 10:54:08 +0200305 },
306 {
307 .name = "direct",
308 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100309 .off1 = td_var_offset(odirect),
Jens Axboee1f36502006-10-27 10:54:08 +0200310 },
311 {
312 .name = "overwrite",
313 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100314 .off1 = td_var_offset(overwrite),
Jens Axboee1f36502006-10-27 10:54:08 +0200315 },
316#ifdef FIO_HAVE_CPU_AFFINITY
317 {
318 .name = "cpumask",
319 .type = FIO_OPT_INT,
320 .cb = str_cpumask_cb,
321 },
322#endif
323 {
324 .name = "end_fsync",
325 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100326 .off1 = td_var_offset(end_fsync),
Jens Axboee1f36502006-10-27 10:54:08 +0200327 },
328 {
329 .name = "unlink",
Jens Axboe8aeebd52007-01-08 10:47:43 +0100330 .type = FIO_OPT_INT,
Jens Axboee1f36502006-10-27 10:54:08 +0200331 .off1 = td_var_offset(unlink),
332 },
333 {
334 .name = "exitall",
335 .type = FIO_OPT_STR_SET,
336 .cb = str_exitall_cb,
337 },
338 {
339 .name = "stonewall",
340 .type = FIO_OPT_STR_SET,
341 .off1 = td_var_offset(stonewall),
342 },
343 {
344 .name = "thread",
345 .type = FIO_OPT_STR_SET,
346 .off1 = td_var_offset(thread),
347 },
348 {
349 .name = "write_bw_log",
350 .type = FIO_OPT_STR_SET,
351 .off1 = td_var_offset(write_bw_log),
352 },
353 {
354 .name = "write_lat_log",
355 .type = FIO_OPT_STR_SET,
356 .off1 = td_var_offset(write_lat_log),
357 },
358 {
Jens Axboebb8895e2006-10-30 15:14:48 +0100359 .name = "norandommap",
360 .type = FIO_OPT_STR_SET,
361 .off1 = td_var_offset(norandommap),
362 },
363 {
Jens Axboe690adba2006-10-30 15:25:09 +0100364 .name = "bs_unaligned",
365 .type = FIO_OPT_STR_SET,
366 .off1 = td_var_offset(bs_unaligned),
367 },
368 {
Jens Axboe56bb17f2006-12-20 20:27:36 +0100369 .name = "hugepage-size",
370 .type = FIO_OPT_STR_VAL,
371 .off1 = td_var_offset(hugepage_size),
372 },
373 {
Jens Axboee1f36502006-10-27 10:54:08 +0200374 .name = NULL,
375 },
376};
377
Jens Axboeb4692822006-10-27 13:43:22 +0200378#define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
379#define FIO_CMD_OPTS (16)
380#define FIO_GETOPT_JOB (0x89988998)
381
382/*
383 * Command line options. These will contain the above, plus a few
384 * extra that only pertain to fio itself and not jobs.
385 */
386static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
387 {
388 .name = "output",
389 .has_arg = required_argument,
390 .val = 'o',
391 },
392 {
393 .name = "timeout",
394 .has_arg = required_argument,
395 .val = 't',
396 },
397 {
398 .name = "latency-log",
399 .has_arg = required_argument,
400 .val = 'l',
401 },
402 {
403 .name = "bandwidth-log",
404 .has_arg = required_argument,
405 .val = 'b',
406 },
407 {
408 .name = "minimal",
409 .has_arg = optional_argument,
410 .val = 'm',
411 },
412 {
413 .name = "version",
414 .has_arg = no_argument,
415 .val = 'v',
416 },
417 {
418 .name = NULL,
419 },
420};
421
Jens Axboe972cfd22006-06-09 11:08:56 +0200422static int def_timeout = DEF_TIMEOUT;
Jens Axboe972cfd22006-06-09 11:08:56 +0200423
Jens Axboe5cd033b2007-01-09 08:19:01 +0100424static char fio_version_string[] = "fio 1.11";
Jens Axboeebac4652005-12-08 15:25:21 +0100425
Jens Axboe972cfd22006-06-09 11:08:56 +0200426static char **ini_file;
Jens Axboeebac4652005-12-08 15:25:21 +0100427static int max_jobs = MAX_JOBS;
428
429struct thread_data def_thread;
430struct thread_data *threads = NULL;
431
Jens Axboeebac4652005-12-08 15:25:21 +0100432int exitall_on_terminate = 0;
Jens Axboec6ae0a52006-06-12 11:04:44 +0200433int terse_output = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +0200434unsigned long long mlock_size = 0;
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200435FILE *f_out = NULL;
436FILE *f_err = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +0100437
Jens Axboeec94ec52006-10-20 10:59:19 +0200438static int write_lat_log = DEF_WRITE_LAT_LOG;
439static int write_bw_log = DEF_WRITE_BW_LOG;
440
Jens Axboe906c8d72006-06-13 09:37:56 +0200441/*
442 * Return a free job structure.
443 */
Jens Axboeebac4652005-12-08 15:25:21 +0100444static struct thread_data *get_new_job(int global, struct thread_data *parent)
445{
446 struct thread_data *td;
447
448 if (global)
449 return &def_thread;
450 if (thread_number >= max_jobs)
451 return NULL;
452
453 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200454 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100455
Jens Axboeebac4652005-12-08 15:25:21 +0100456 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100457 return td;
458}
459
460static void put_job(struct thread_data *td)
461{
Jens Axboe549577a2006-10-30 12:23:41 +0100462 if (td == &def_thread)
463 return;
464
Jens Axboeebac4652005-12-08 15:25:21 +0100465 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
466 thread_number--;
467}
468
Jens Axboedad915e2006-10-27 11:10:18 +0200469/*
470 * Lazy way of fixing up options that depend on each other. We could also
471 * define option callback handlers, but this is easier.
472 */
Jens Axboee1f36502006-10-27 10:54:08 +0200473static void fixup_options(struct thread_data *td)
474{
Jens Axboee1f36502006-10-27 10:54:08 +0200475 if (!td->rwmixread && td->rwmixwrite)
476 td->rwmixread = 100 - td->rwmixwrite;
Jens Axboedad915e2006-10-27 11:10:18 +0200477
Jens Axboe076efc72006-10-27 11:24:25 +0200478 if (td->write_iolog_file && td->read_iolog_file) {
479 log_err("fio: read iolog overrides write_iolog\n");
480 free(td->write_iolog_file);
481 td->write_iolog_file = NULL;
482 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100483
484 if (td->io_ops->flags & FIO_SYNCIO)
485 td->iodepth = 1;
486 else {
487 if (!td->iodepth)
488 td->iodepth = td->nr_files;
489 }
490
491 /*
492 * only really works for sequential io for now, and with 1 file
493 */
494 if (td->zone_size && !td->sequential && td->nr_files == 1)
495 td->zone_size = 0;
496
497 /*
498 * Reads can do overwrites, we always need to pre-create the file
499 */
500 if (td_read(td) || td_rw(td))
501 td->overwrite = 1;
502
Jens Axboea00735e2006-11-03 08:58:08 +0100503 if (!td->min_bs[DDIR_READ])
504 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
505 if (!td->max_bs[DDIR_READ])
506 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
507 if (!td->min_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100508 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100509 if (!td->max_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100510 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100511
512 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
513
Jens Axboe16b462a2006-10-30 12:35:18 +0100514 if (td_read(td) && !td_rw(td))
515 td->verify = 0;
Jens Axboebb8895e2006-10-30 15:14:48 +0100516
517 if (td->norandommap && td->verify != VERIFY_NONE) {
518 log_err("fio: norandommap given, verify disabled\n");
519 td->verify = VERIFY_NONE;
520 }
Jens Axboe690adba2006-10-30 15:25:09 +0100521 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
522 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100523
524 /*
525 * O_DIRECT and char doesn't mix, clear that flag if necessary.
526 */
527 if (td->filetype == FIO_TYPE_CHAR && td->odirect)
528 td->odirect = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200529}
530
Jens Axboe906c8d72006-06-13 09:37:56 +0200531/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100532 * This function leaks the buffer
533 */
534static char *to_kmg(unsigned int val)
535{
536 char *buf = malloc(32);
Jens Axboe245142f2006-11-08 12:49:21 +0100537 char post[] = { 0, 'K', 'M', 'G', 'P', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100538 char *p = post;
539
Jens Axboe245142f2006-11-08 12:49:21 +0100540 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100541 if (val & 1023)
542 break;
543
544 val >>= 10;
545 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100546 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100547
548 snprintf(buf, 31, "%u%c", val, *p);
549 return buf;
550}
551
552/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200553 * Adds a job to the list of things todo. Sanitizes the various options
554 * to make sure we don't have conflicts, and initializes various
555 * members of td.
556 */
Jens Axboe75154842006-06-01 13:56:09 +0200557static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100558{
Jens Axboe3c9b60c2006-11-07 08:36:14 +0100559 const char *ddir_str[] = { "read", "write", "randread", "randwrite",
560 "rw", NULL, "randrw" };
Jens Axboeebac4652005-12-08 15:25:21 +0100561 struct stat sb;
Jens Axboe53cdc682006-10-18 11:50:58 +0200562 int numjobs, ddir, i;
563 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100564
Jens Axboeebac4652005-12-08 15:25:21 +0100565 /*
566 * the def_thread is just for options, it's not a real job
567 */
568 if (td == &def_thread)
569 return 0;
570
Jens Axboedf641192006-10-12 07:55:41 +0200571 /*
572 * Set default io engine, if none set
573 */
574 if (!td->io_ops) {
575 td->io_ops = load_ioengine(td, DEF_IO_ENGINE_NAME);
576 if (!td->io_ops) {
577 log_err("default engine %s not there?\n", DEF_IO_ENGINE_NAME);
578 return 1;
579 }
580 }
581
Jens Axboe690adba2006-10-30 15:25:09 +0100582 if (td->odirect)
583 td->io_ops->flags |= FIO_RAWIO;
584
Jens Axboeebac4652005-12-08 15:25:21 +0100585 td->filetype = FIO_TYPE_FILE;
Jens Axboe0af7b542006-02-17 10:10:12 +0100586 if (!stat(jobname, &sb)) {
587 if (S_ISBLK(sb.st_mode))
588 td->filetype = FIO_TYPE_BD;
589 else if (S_ISCHR(sb.st_mode))
590 td->filetype = FIO_TYPE_CHAR;
591 }
Jens Axboeebac4652005-12-08 15:25:21 +0100592
Jens Axboee0a22332006-12-20 12:54:25 +0100593 fixup_options(td);
594
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200595 if (td->filename)
596 td->nr_uniq_files = 1;
597 else
598 td->nr_uniq_files = td->nr_files;
599
600 if (td->filetype == FIO_TYPE_FILE || td->filename) {
Jens Axboee9c047a2006-06-07 21:13:04 +0200601 char tmp[PATH_MAX];
Jens Axboe53cdc682006-10-18 11:50:58 +0200602 int len = 0;
Jens Axboee9c047a2006-06-07 21:13:04 +0200603
Jens Axboeef899b62006-06-06 09:31:00 +0200604 if (td->directory && td->directory[0] != '\0')
Jens Axboe8aeebd52007-01-08 10:47:43 +0100605 len = sprintf(tmp, "%s/", td->directory);
Jens Axboeebac4652005-12-08 15:25:21 +0100606
Jens Axboe53cdc682006-10-18 11:50:58 +0200607 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
608
609 for_each_file(td, f, i) {
610 memset(f, 0, sizeof(*f));
611 f->fd = -1;
612
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200613 if (td->filename)
614 sprintf(tmp + len, "%s", td->filename);
615 else
616 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
Jens Axboe53cdc682006-10-18 11:50:58 +0200617 f->file_name = strdup(tmp);
618 }
619 } else {
620 td->nr_files = 1;
621 td->files = malloc(sizeof(struct fio_file));
622 f = &td->files[0];
623
624 memset(f, 0, sizeof(*f));
625 f->fd = -1;
626 f->file_name = strdup(jobname);
627 }
628
629 for_each_file(td, f, i) {
630 f->file_size = td->total_file_size / td->nr_files;
631 f->file_offset = td->start_offset;
632 }
633
Jens Axboebbfd6b02006-06-07 19:42:54 +0200634 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100635
636 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
637 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
638 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
639
Jens Axboeebac4652005-12-08 15:25:21 +0100640 if (td->stonewall && td->thread_number > 1)
641 groupid++;
642
643 td->groupid = groupid;
644
645 if (setup_rate(td))
646 goto err;
647
Jens Axboeec94ec52006-10-20 10:59:19 +0200648 if (td->write_lat_log) {
Jens Axboeebac4652005-12-08 15:25:21 +0100649 setup_log(&td->slat_log);
650 setup_log(&td->clat_log);
651 }
Jens Axboeec94ec52006-10-20 10:59:19 +0200652 if (td->write_bw_log)
Jens Axboeebac4652005-12-08 15:25:21 +0100653 setup_log(&td->bw_log);
654
Jens Axboeb4692822006-10-27 13:43:22 +0200655 if (!td->name)
656 td->name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200657
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200658 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
Jens Axboe75154842006-06-01 13:56:09 +0200659
Jens Axboec6ae0a52006-06-12 11:04:44 +0200660 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200661 if (!job_add_num) {
Jens Axboe2866c822006-10-09 15:57:48 +0200662 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200663 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
Jens Axboef8977ee2006-11-06 14:03:03 +0100664 else {
665 char *c1, *c2, *c3, *c4;
666
667 c1 = to_kmg(td->min_bs[DDIR_READ]);
668 c2 = to_kmg(td->max_bs[DDIR_READ]);
669 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
670 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
671
Jens Axboe1e97cce2006-12-05 11:44:16 +0100672 fprintf(f_out, "%s: (g=%d): rw=%s, odir=%u, bs=%s-%s/%s-%s, rate=%u, ioengine=%s, iodepth=%u\n", td->name, td->groupid, ddir_str[ddir], td->odirect, c1, c2, c3, c4, td->rate, td->io_ops->name, td->iodepth);
Jens Axboef8977ee2006-11-06 14:03:03 +0100673
674 free(c1);
675 free(c2);
676 free(c3);
677 free(c4);
678 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200679 } else if (job_add_num == 1)
Jens Axboec6ae0a52006-06-12 11:04:44 +0200680 fprintf(f_out, "...\n");
681 }
Jens Axboeebac4652005-12-08 15:25:21 +0100682
683 /*
684 * recurse add identical jobs, clear numjobs and stonewall options
685 * as they don't apply to sub-jobs
686 */
687 numjobs = td->numjobs;
688 while (--numjobs) {
689 struct thread_data *td_new = get_new_job(0, td);
690
691 if (!td_new)
692 goto err;
693
694 td_new->numjobs = 1;
695 td_new->stonewall = 0;
Jens Axboe75154842006-06-01 13:56:09 +0200696 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100697
Jens Axboe75154842006-06-01 13:56:09 +0200698 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100699 goto err;
700 }
701 return 0;
702err:
703 put_job(td);
704 return -1;
705}
706
Jens Axboe906c8d72006-06-13 09:37:56 +0200707/*
708 * Initialize the various random states we need (random io, block size ranges,
709 * read/write mix, etc).
710 */
Jens Axboeebac4652005-12-08 15:25:21 +0100711int init_random_state(struct thread_data *td)
712{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200713 unsigned long seeds[4];
Jens Axboe53cdc682006-10-18 11:50:58 +0200714 int fd, num_maps, blocks, i;
Jens Axboe0ab8db82006-10-18 17:16:23 +0200715 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100716
Jens Axboef48b4672006-10-27 11:30:07 +0200717 if (td->io_ops->flags & FIO_CPUIO)
718 return 0;
719
Jens Axboe1ac267b2006-05-31 20:29:00 +0200720 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100721 if (fd == -1) {
722 td_verror(td, errno);
723 return 1;
724 }
725
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200726 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100727 td_verror(td, EIO);
728 close(fd);
729 return 1;
730 }
731
732 close(fd);
733
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200734 os_random_seed(seeds[0], &td->bsrange_state);
735 os_random_seed(seeds[1], &td->verify_state);
736 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100737
738 if (td->sequential)
739 return 0;
740
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200741 if (td->rand_repeatable)
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200742 seeds[3] = DEF_RANDSEED;
Jens Axboeebac4652005-12-08 15:25:21 +0100743
Jens Axboebb8895e2006-10-30 15:14:48 +0100744 if (!td->norandommap) {
745 for_each_file(td, f, i) {
Jens Axboea00735e2006-11-03 08:58:08 +0100746 blocks = (f->file_size + td->rw_min_bs - 1) / td->rw_min_bs;
Jens Axboec7c280e2006-11-02 20:10:33 +0100747 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
Jens Axboebb8895e2006-10-30 15:14:48 +0100748 f->file_map = malloc(num_maps * sizeof(long));
749 f->num_maps = num_maps;
750 memset(f->file_map, 0, num_maps * sizeof(long));
751 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200752 }
Jens Axboeebac4652005-12-08 15:25:21 +0100753
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200754 os_random_seed(seeds[3], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100755 return 0;
756}
757
758static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
759{
760#ifdef FIO_HAVE_CPU_AFFINITY
761 unsigned int i;
762
763 CPU_ZERO(&cpumask);
764
765 for (i = 0; i < sizeof(int) * 8; i++) {
766 if ((1 << i) & cpu)
767 CPU_SET(i, &cpumask);
768 }
769#endif
770}
771
Jens Axboeebac4652005-12-08 15:25:21 +0100772static int is_empty_or_comment(char *line)
773{
774 unsigned int i;
775
776 for (i = 0; i < strlen(line); i++) {
777 if (line[i] == ';')
778 return 1;
779 if (!isspace(line[i]) && !iscntrl(line[i]))
780 return 0;
781 }
782
783 return 1;
784}
785
Jens Axboeb4692822006-10-27 13:43:22 +0200786static int str_rw_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100787{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200788 struct thread_data *td = data;
789
Jens Axboeebac4652005-12-08 15:25:21 +0100790 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
791 td->ddir = DDIR_READ;
792 td->sequential = 1;
793 return 0;
794 } else if (!strncmp(mem, "randread", 8)) {
795 td->ddir = DDIR_READ;
796 td->sequential = 0;
797 return 0;
798 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
799 td->ddir = DDIR_WRITE;
800 td->sequential = 1;
801 return 0;
802 } else if (!strncmp(mem, "randwrite", 9)) {
803 td->ddir = DDIR_WRITE;
804 td->sequential = 0;
805 return 0;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200806 } else if (!strncmp(mem, "rw", 2)) {
Jens Axboe1e97cce2006-12-05 11:44:16 +0100807 td->ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200808 td->iomix = 1;
809 td->sequential = 1;
810 return 0;
811 } else if (!strncmp(mem, "randrw", 6)) {
Jens Axboe1e97cce2006-12-05 11:44:16 +0100812 td->ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200813 td->iomix = 1;
814 td->sequential = 0;
815 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100816 }
817
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200818 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100819 return 1;
820}
821
Jens Axboeb4692822006-10-27 13:43:22 +0200822static int str_verify_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100823{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200824 struct thread_data *td = data;
825
Jens Axboeebac4652005-12-08 15:25:21 +0100826 if (!strncmp(mem, "0", 1)) {
827 td->verify = VERIFY_NONE;
828 return 0;
829 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
830 td->verify = VERIFY_MD5;
831 return 0;
832 } else if (!strncmp(mem, "crc32", 5)) {
833 td->verify = VERIFY_CRC32;
834 return 0;
835 }
836
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200837 log_err("fio: verify types: md5, crc32\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100838 return 1;
839}
840
Jens Axboe313cb202006-12-21 09:50:00 +0100841/*
842 * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
843 */
844static char *get_mmap_file(const char *str)
845{
846 char *p = strstr(str, ":");
847
848 if (!p)
849 return NULL;
850
851 p++;
852 strip_blank_front(&p);
853 strip_blank_end(p);
854 return strdup(p);
855}
856
Jens Axboeb4692822006-10-27 13:43:22 +0200857static int str_mem_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100858{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200859 struct thread_data *td = data;
860
Jens Axboeebac4652005-12-08 15:25:21 +0100861 if (!strncmp(mem, "malloc", 6)) {
862 td->mem_type = MEM_MALLOC;
863 return 0;
Jens Axboed0bdaf42006-12-20 14:40:44 +0100864 } else if (!strncmp(mem, "mmaphuge", 8)) {
865#ifdef FIO_HAVE_HUGETLB
Jens Axboed0bdaf42006-12-20 14:40:44 +0100866 /*
867 * mmaphuge must be appended with the actual file
868 */
Jens Axboe313cb202006-12-21 09:50:00 +0100869 td->mmapfile = get_mmap_file(mem);
870 if (!td->mmapfile) {
Jens Axboed0bdaf42006-12-20 14:40:44 +0100871 log_err("fio: mmaphuge:/path/to/file\n");
872 return 1;
873 }
874
Jens Axboed0bdaf42006-12-20 14:40:44 +0100875 td->mem_type = MEM_MMAPHUGE;
876 return 0;
877#else
878 log_err("fio: mmaphuge not available\n");
879 return 1;
880#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100881 } else if (!strncmp(mem, "mmap", 4)) {
Jens Axboe313cb202006-12-21 09:50:00 +0100882 /*
883 * Check if the user wants file backed memory. It's ok
884 * if there's no file given, we'll just use anon mamp then.
885 */
886 td->mmapfile = get_mmap_file(mem);
Jens Axboeebac4652005-12-08 15:25:21 +0100887 td->mem_type = MEM_MMAP;
888 return 0;
Jens Axboe74b025b2006-12-19 15:18:14 +0100889 } else if (!strncmp(mem, "shmhuge", 7)) {
890#ifdef FIO_HAVE_HUGETLB
891 td->mem_type = MEM_SHMHUGE;
892 return 0;
893#else
894 log_err("fio: shmhuge not available\n");
895 return 1;
896#endif
Jens Axboe0268b8b2006-12-20 12:48:23 +0100897 } else if (!strncmp(mem, "shm", 3)) {
898 td->mem_type = MEM_SHM;
899 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100900 }
901
Jens Axboed0bdaf42006-12-20 14:40:44 +0100902 log_err("fio: mem type: malloc, shm, shmhuge, mmap, mmaphuge\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100903 return 1;
904}
905
Jens Axboeb4692822006-10-27 13:43:22 +0200906static int str_ioengine_cb(void *data, const char *str)
Jens Axboeebac4652005-12-08 15:25:21 +0100907{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200908 struct thread_data *td = data;
909
Jens Axboe2866c822006-10-09 15:57:48 +0200910 td->io_ops = load_ioengine(td, str);
911 if (td->io_ops)
Jens Axboeebac4652005-12-08 15:25:21 +0100912 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100913
Jens Axboe08aae9a2006-11-24 12:43:49 +0100914 log_err("fio: ioengine= libaio, posixaio, sync, mmap, sgio, splice, cpu, null\n");
Jens Axboe5f350952006-11-07 15:20:59 +0100915 log_err("fio: or specify path to dynamic ioengine module\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100916 return 1;
917}
918
Jens Axboee1f36502006-10-27 10:54:08 +0200919static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
920{
921 mlock_size = *val;
922 return 0;
923}
924
Jens Axboe34cfcda2006-11-03 14:00:45 +0100925#ifdef FIO_HAVE_IOPRIO
Jens Axboee1f36502006-10-27 10:54:08 +0200926static int str_prioclass_cb(void *data, unsigned int *val)
927{
928 struct thread_data *td = data;
929
930 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
931 return 0;
932}
933
934static int str_prio_cb(void *data, unsigned int *val)
935{
936 struct thread_data *td = data;
937
938 td->ioprio |= *val;
939 return 0;
940}
Jens Axboe34cfcda2006-11-03 14:00:45 +0100941#endif
Jens Axboee1f36502006-10-27 10:54:08 +0200942
943static int str_exitall_cb(void)
944{
945 exitall_on_terminate = 1;
946 return 0;
947}
948
949static int str_cpumask_cb(void *data, unsigned int *val)
950{
951 struct thread_data *td = data;
952
953 fill_cpu_mask(td->cpumask, *val);
954 return 0;
955}
956
Jens Axboe07261982006-06-07 10:51:12 +0200957/*
958 * This is our [ini] type file parser.
959 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100960static int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100961{
Jens Axboee1f36502006-10-27 10:54:08 +0200962 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +0100963 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +0100964 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +0100965 fpos_t off;
966 FILE *f;
967 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200968 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +0100969
970 f = fopen(file, "r");
971 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200972 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100973 return 1;
974 }
975
976 string = malloc(4096);
977 name = malloc(256);
Jens Axboefee3bb42006-10-30 13:32:08 +0100978 memset(name, 0, 256);
Jens Axboeebac4652005-12-08 15:25:21 +0100979
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200980 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100981 do {
982 p = fgets(string, 4095, f);
983 if (!p)
Jens Axboe45410ac2006-06-07 11:10:39 +0200984 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100985 if (is_empty_or_comment(p))
986 continue;
Jens Axboefee3bb42006-10-30 13:32:08 +0100987 if (sscanf(p, "[%255s]", name) != 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100988 continue;
989
990 global = !strncmp(name, "global", 6);
991
992 name[strlen(name) - 1] = '\0';
993
994 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200995 if (!td) {
996 ret = 1;
997 break;
998 }
Jens Axboeebac4652005-12-08 15:25:21 +0100999
Jens Axboe972cfd22006-06-09 11:08:56 +02001000 /*
1001 * Seperate multiple job files by a stonewall
1002 */
Jens Axboef9481912006-06-09 11:37:28 +02001003 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +02001004 td->stonewall = stonewall;
1005 stonewall = 0;
1006 }
1007
Jens Axboeebac4652005-12-08 15:25:21 +01001008 fgetpos(f, &off);
1009 while ((p = fgets(string, 4096, f)) != NULL) {
1010 if (is_empty_or_comment(p))
1011 continue;
Jens Axboee1f36502006-10-27 10:54:08 +02001012
Jens Axboeb6754f92006-05-30 14:29:32 +02001013 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +01001014
1015 if (p[0] == '[')
1016 break;
1017
Jens Axboe4ae3f762006-05-30 13:35:14 +02001018 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +02001019
Jens Axboee1f36502006-10-27 10:54:08 +02001020 fgetpos(f, &off);
Jens Axboeebac4652005-12-08 15:25:21 +01001021
Jens Axboe45410ac2006-06-07 11:10:39 +02001022 /*
1023 * Don't break here, continue parsing options so we
1024 * dump all the bad ones. Makes trial/error fixups
1025 * easier on the user.
1026 */
Jens Axboe7c124ac2006-10-30 13:36:52 +01001027 ret |= parse_option(p, options, td);
Jens Axboeebac4652005-12-08 15:25:21 +01001028 }
Jens Axboeebac4652005-12-08 15:25:21 +01001029
Jens Axboe45410ac2006-06-07 11:10:39 +02001030 if (!ret) {
1031 fsetpos(f, &off);
1032 ret = add_job(td, name, 0);
Jens Axboeb1508cf2006-11-02 09:12:40 +01001033 } else {
1034 log_err("fio: job %s dropped\n", name);
1035 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +02001036 }
Jens Axboe7c124ac2006-10-30 13:36:52 +01001037 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001038
1039 free(string);
1040 free(name);
1041 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +02001042 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001043}
1044
1045static int fill_def_thread(void)
1046{
1047 memset(&def_thread, 0, sizeof(def_thread));
1048
1049 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1050 perror("sched_getaffinity");
1051 return 1;
1052 }
1053
1054 /*
1055 * fill globals
1056 */
1057 def_thread.ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001058 def_thread.iomix = 0;
Jens Axboea00735e2006-11-03 08:58:08 +01001059 def_thread.bs[DDIR_READ] = DEF_BS;
1060 def_thread.bs[DDIR_WRITE] = DEF_BS;
1061 def_thread.min_bs[DDIR_READ] = def_thread.min_bs[DDIR_WRITE] = 0;
1062 def_thread.max_bs[DDIR_READ] = def_thread.max_bs[DDIR_WRITE] = 0;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001063 def_thread.odirect = DEF_ODIRECT;
Jens Axboeebac4652005-12-08 15:25:21 +01001064 def_thread.ratecycle = DEF_RATE_CYCLE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001065 def_thread.sequential = DEF_SEQUENTIAL;
Jens Axboe972cfd22006-06-09 11:08:56 +02001066 def_thread.timeout = def_timeout;
Jens Axboeebac4652005-12-08 15:25:21 +01001067 def_thread.overwrite = DEF_OVERWRITE;
1068 def_thread.invalidate_cache = DEF_INVALIDATE;
1069 def_thread.sync_io = DEF_SYNCIO;
1070 def_thread.mem_type = MEM_MALLOC;
1071 def_thread.bw_avg_time = DEF_BWAVGTIME;
1072 def_thread.create_serialize = DEF_CREATE_SER;
1073 def_thread.create_fsync = DEF_CREATE_FSYNC;
1074 def_thread.loops = DEF_LOOPS;
1075 def_thread.verify = DEF_VERIFY;
1076 def_thread.stonewall = DEF_STONEWALL;
1077 def_thread.numjobs = DEF_NUMJOBS;
1078 def_thread.use_thread = DEF_USE_THREAD;
Jens Axboea6ccc7b2006-06-02 10:14:15 +02001079 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
1080 def_thread.rwmixread = DEF_RWMIX_READ;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001081 def_thread.nice = DEF_NICE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001082 def_thread.rand_repeatable = DEF_RAND_REPEAT;
Jens Axboe53cdc682006-10-18 11:50:58 +02001083 def_thread.nr_files = DEF_NR_FILES;
Jens Axboef6cbb262006-10-18 14:16:42 +02001084 def_thread.unlink = DEF_UNLINK;
Jens Axboeec94ec52006-10-20 10:59:19 +02001085 def_thread.write_bw_log = write_bw_log;
1086 def_thread.write_lat_log = write_lat_log;
Jens Axboebb8895e2006-10-30 15:14:48 +01001087 def_thread.norandommap = DEF_NO_RAND_MAP;
Jens Axboe56bb17f2006-12-20 20:27:36 +01001088 def_thread.hugepage_size = DEF_HUGEPAGE_SIZE;
Jens Axboe9c1f7432007-01-03 20:43:19 +01001089 def_thread.thinktime_blocks = DEF_THINKTIME_BLOCKS;
Jens Axboeebac4652005-12-08 15:25:21 +01001090#ifdef FIO_HAVE_DISK_UTIL
1091 def_thread.do_disk_util = 1;
1092#endif
1093
1094 return 0;
1095}
1096
Jens Axboe0ab8db82006-10-18 17:16:23 +02001097static void usage(void)
Jens Axboe4785f992006-05-26 03:59:10 +02001098{
1099 printf("%s\n", fio_version_string);
Jens Axboeb4692822006-10-27 13:43:22 +02001100 printf("\t--output\tWrite output to file\n");
1101 printf("\t--timeout\tRuntime in seconds\n");
1102 printf("\t--latency-log\tGenerate per-job latency logs\n");
1103 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1104 printf("\t--minimal\tMinimal (terse) output\n");
1105 printf("\t--version\tPrint version info and exit\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001106}
1107
Jens Axboe972cfd22006-06-09 11:08:56 +02001108static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +01001109{
Jens Axboeb4692822006-10-27 13:43:22 +02001110 struct thread_data *td = NULL;
Jens Axboec2b1e752006-10-30 09:03:13 +01001111 int c, ini_idx = 0, lidx, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001112
Jens Axboeb4692822006-10-27 13:43:22 +02001113 while ((c = getopt_long(argc, argv, "", long_options, &lidx)) != -1) {
Jens Axboeebac4652005-12-08 15:25:21 +01001114 switch (c) {
Jens Axboeb4692822006-10-27 13:43:22 +02001115 case 't':
1116 def_timeout = atoi(optarg);
1117 break;
1118 case 'l':
1119 write_lat_log = 1;
1120 break;
1121 case 'w':
1122 write_bw_log = 1;
1123 break;
1124 case 'o':
1125 f_out = fopen(optarg, "w+");
1126 if (!f_out) {
1127 perror("fopen output");
1128 exit(1);
1129 }
1130 f_err = f_out;
1131 break;
1132 case 'm':
1133 terse_output = 1;
1134 break;
1135 case 'h':
1136 usage();
1137 exit(0);
1138 case 'v':
1139 printf("%s\n", fio_version_string);
1140 exit(0);
1141 case FIO_GETOPT_JOB: {
1142 const char *opt = long_options[lidx].name;
1143 char *val = optarg;
1144
Jens Axboec2b1e752006-10-30 09:03:13 +01001145 if (!strncmp(opt, "name", 4) && td) {
1146 ret = add_job(td, td->name ?: "fio", 0);
1147 if (ret) {
1148 put_job(td);
1149 return 0;
1150 }
1151 td = NULL;
1152 }
Jens Axboeb4692822006-10-27 13:43:22 +02001153 if (!td) {
Jens Axboe38d0adb2006-10-30 12:21:25 +01001154 int global = !strncmp(val, "global", 6);
Jens Axboec2b1e752006-10-30 09:03:13 +01001155
1156 td = get_new_job(global, &def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001157 if (!td)
1158 return 0;
1159 }
Jens Axboe38d0adb2006-10-30 12:21:25 +01001160
Jens Axboeb1508cf2006-11-02 09:12:40 +01001161 ret = parse_cmd_option(opt, val, options, td);
1162 if (ret) {
1163 log_err("fio: job dropped\n");
1164 put_job(td);
1165 td = NULL;
1166 }
Jens Axboeb4692822006-10-27 13:43:22 +02001167 break;
1168 }
1169 default:
Jens Axboeb4692822006-10-27 13:43:22 +02001170 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001171 }
1172 }
Jens Axboec9fad892006-05-27 17:26:50 +02001173
Jens Axboeb4692822006-10-27 13:43:22 +02001174 if (td) {
Jens Axboec2b1e752006-10-30 09:03:13 +01001175 ret = add_job(td, td->name ?: "fio", 0);
Jens Axboeb4692822006-10-27 13:43:22 +02001176 if (ret)
1177 put_job(td);
Jens Axboe972cfd22006-06-09 11:08:56 +02001178 }
Jens Axboe774a6172006-08-24 08:44:26 +02001179
Jens Axboeb4692822006-10-27 13:43:22 +02001180 while (optind < argc) {
1181 ini_idx++;
1182 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1183 ini_file[ini_idx - 1] = strdup(argv[optind]);
1184 optind++;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001185 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001186
1187 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +01001188}
1189
1190static void free_shm(void)
1191{
1192 struct shmid_ds sbuf;
1193
1194 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001195 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001196 threads = NULL;
1197 shmctl(shm_id, IPC_RMID, &sbuf);
1198 }
1199}
1200
Jens Axboe906c8d72006-06-13 09:37:56 +02001201/*
1202 * The thread area is shared between the main process and the job
1203 * threads/processes. So setup a shared memory segment that will hold
1204 * all the job info.
1205 */
Jens Axboeebac4652005-12-08 15:25:21 +01001206static int setup_thread_area(void)
1207{
1208 /*
1209 * 1024 is too much on some machines, scale max_jobs if
1210 * we get a failure that looks like too large a shm segment
1211 */
1212 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001213 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001214
Jens Axboe906c8d72006-06-13 09:37:56 +02001215 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001216 if (shm_id != -1)
1217 break;
1218 if (errno != EINVAL) {
1219 perror("shmget");
1220 break;
1221 }
1222
1223 max_jobs >>= 1;
1224 } while (max_jobs);
1225
1226 if (shm_id == -1)
1227 return 1;
1228
1229 threads = shmat(shm_id, NULL, 0);
1230 if (threads == (void *) -1) {
1231 perror("shmat");
1232 return 1;
1233 }
1234
1235 atexit(free_shm);
1236 return 0;
1237}
1238
Jens Axboeb4692822006-10-27 13:43:22 +02001239/*
1240 * Copy the fio options into the long options map, so we mirror
1241 * job and cmd line options.
1242 */
1243static void dupe_job_options(void)
1244{
1245 struct fio_option *o;
1246 unsigned int i;
1247
1248 i = 0;
1249 while (long_options[i].name)
1250 i++;
1251
1252 o = &options[0];
1253 while (o->name) {
1254 long_options[i].name = o->name;
1255 long_options[i].val = FIO_GETOPT_JOB;
1256 if (o->type == FIO_OPT_STR_SET)
1257 long_options[i].has_arg = no_argument;
1258 else
1259 long_options[i].has_arg = required_argument;
1260
1261 i++;
1262 o++;
1263 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1264 }
1265}
1266
Jens Axboeebac4652005-12-08 15:25:21 +01001267int parse_options(int argc, char *argv[])
1268{
Jens Axboe972cfd22006-06-09 11:08:56 +02001269 int job_files, i;
1270
Jens Axboeb4692822006-10-27 13:43:22 +02001271 f_out = stdout;
1272 f_err = stderr;
1273
1274 dupe_job_options();
1275
Jens Axboeebac4652005-12-08 15:25:21 +01001276 if (setup_thread_area())
1277 return 1;
1278 if (fill_def_thread())
1279 return 1;
1280
Jens Axboe972cfd22006-06-09 11:08:56 +02001281 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001282
Jens Axboe972cfd22006-06-09 11:08:56 +02001283 for (i = 0; i < job_files; i++) {
1284 if (fill_def_thread())
1285 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001286 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001287 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001288 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001289 }
Jens Axboeebac4652005-12-08 15:25:21 +01001290
Jens Axboe88c6ed82006-06-09 11:28:10 +02001291 free(ini_file);
Jens Axboeb4692822006-10-27 13:43:22 +02001292
1293 if (!thread_number) {
1294 log_err("No jobs defined(s)\n");
Jens Axboeb4692822006-10-27 13:43:22 +02001295 return 1;
1296 }
1297
Jens Axboeebac4652005-12-08 15:25:21 +01001298 return 0;
1299}