blob: a50362674e21902b30cd1519146e29c59a6ba48b [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>
Jens Axboeebac4652005-12-08 15:25:21 +010012#include <sys/ipc.h>
13#include <sys/shm.h>
14#include <sys/types.h>
15#include <sys/stat.h>
16
17#include "fio.h"
Jens Axboecb2c86f2006-10-26 13:48:34 +020018#include "parse.h"
Jens Axboeebac4652005-12-08 15:25:21 +010019
Jens Axboe64d04e42007-10-30 15:45:12 +010020static char fio_version_string[] = "fio 1.17.2";
Jens Axboe214e1ec2007-03-15 10:48:13 +010021
Jens Axboeee738492007-01-10 11:23:16 +010022#define FIO_RANDSEED (0xb1899bedUL)
Jens Axboeebac4652005-12-08 15:25:21 +010023
Jens Axboe214e1ec2007-03-15 10:48:13 +010024static char **ini_file;
25static int max_jobs = MAX_JOBS;
Jens Axboecca73aa2007-04-04 11:09:19 +020026static int dump_cmdline;
Jens Axboee1f36502006-10-27 10:54:08 +020027
Jens Axboe214e1ec2007-03-15 10:48:13 +010028struct thread_data def_thread;
29struct thread_data *threads = NULL;
Jens Axboee1f36502006-10-27 10:54:08 +020030
Jens Axboe214e1ec2007-03-15 10:48:13 +010031int exitall_on_terminate = 0;
32int terse_output = 0;
Aaron Carrolle592a062007-09-14 09:49:41 +020033int eta_print;
Jens Axboe214e1ec2007-03-15 10:48:13 +010034unsigned long long mlock_size = 0;
35FILE *f_out = NULL;
36FILE *f_err = NULL;
Jens Axboeee738492007-01-10 11:23:16 +010037
Jens Axboe214e1ec2007-03-15 10:48:13 +010038int write_bw_log = 0;
Jens Axboe4241ea82007-09-12 08:18:36 +020039int read_only = 0;
Jens Axboee1f36502006-10-27 10:54:08 +020040
Jens Axboe214e1ec2007-03-15 10:48:13 +010041static int def_timeout = 0;
42static int write_lat_log = 0;
43
44static int prev_group_jobs;
Jens Axboeb4692822006-10-27 13:43:22 +020045
46/*
47 * Command line options. These will contain the above, plus a few
48 * extra that only pertain to fio itself and not jobs.
49 */
Jens Axboe214e1ec2007-03-15 10:48:13 +010050static struct option long_options[FIO_NR_OPTIONS] = {
Jens Axboeb4692822006-10-27 13:43:22 +020051 {
52 .name = "output",
53 .has_arg = required_argument,
54 .val = 'o',
55 },
56 {
57 .name = "timeout",
58 .has_arg = required_argument,
59 .val = 't',
60 },
61 {
62 .name = "latency-log",
63 .has_arg = required_argument,
64 .val = 'l',
65 },
66 {
67 .name = "bandwidth-log",
68 .has_arg = required_argument,
69 .val = 'b',
70 },
71 {
72 .name = "minimal",
73 .has_arg = optional_argument,
74 .val = 'm',
75 },
76 {
77 .name = "version",
78 .has_arg = no_argument,
79 .val = 'v',
80 },
81 {
Jens Axboefd28ca42007-01-09 21:20:13 +010082 .name = "help",
83 .has_arg = no_argument,
84 .val = 'h',
85 },
86 {
87 .name = "cmdhelp",
Jens Axboe320beef2007-03-01 10:44:12 +010088 .has_arg = optional_argument,
Jens Axboefd28ca42007-01-09 21:20:13 +010089 .val = 'c',
90 },
91 {
Jens Axboecca73aa2007-04-04 11:09:19 +020092 .name = "showcmd",
93 .has_arg = no_argument,
Jens Axboe724e4432007-09-11 20:02:05 +020094 .val = 's',
95 },
96 {
97 .name = "readonly",
98 .has_arg = no_argument,
99 .val = 'r',
Jens Axboecca73aa2007-04-04 11:09:19 +0200100 },
101 {
Aaron Carrolle592a062007-09-14 09:49:41 +0200102 .name = "eta",
103 .has_arg = required_argument,
104 .val = 'e',
105 },
106 {
Jens Axboeb4692822006-10-27 13:43:22 +0200107 .name = NULL,
108 },
109};
110
Joel Becker9728ce32007-03-01 08:24:39 +0100111FILE *get_f_out()
112{
113 return f_out;
114}
115
116FILE *get_f_err()
117{
118 return f_err;
119}
120
Jens Axboe906c8d72006-06-13 09:37:56 +0200121/*
122 * Return a free job structure.
123 */
Jens Axboeebac4652005-12-08 15:25:21 +0100124static struct thread_data *get_new_job(int global, struct thread_data *parent)
125{
126 struct thread_data *td;
127
128 if (global)
129 return &def_thread;
130 if (thread_number >= max_jobs)
131 return NULL;
132
133 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200134 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100135
Jens Axboecade3ef2007-03-23 15:29:45 +0100136 dup_files(td, parent);
Jens Axboed23bb322007-03-23 15:57:56 +0100137 options_mem_dupe(td);
Jens Axboecade3ef2007-03-23 15:29:45 +0100138
Jens Axboeebac4652005-12-08 15:25:21 +0100139 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100140 return td;
141}
142
143static void put_job(struct thread_data *td)
144{
Jens Axboe549577a2006-10-30 12:23:41 +0100145 if (td == &def_thread)
146 return;
147
Jens Axboe16edf252007-02-10 14:59:22 +0100148 if (td->error)
Jens Axboe6d861442007-03-15 09:22:23 +0100149 log_info("fio: %s\n", td->verror);
Jens Axboe16edf252007-02-10 14:59:22 +0100150
Jens Axboeebac4652005-12-08 15:25:21 +0100151 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
152 thread_number--;
153}
154
Jens Axboe127f6862007-03-15 12:09:57 +0100155static int setup_rate(struct thread_data *td)
156{
157 unsigned long nr_reads_per_msec;
158 unsigned long long rate;
159 unsigned int bs;
160
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100161 if (!td->o.rate && !td->o.rate_iops)
Jens Axboe127f6862007-03-15 12:09:57 +0100162 return 0;
163
164 if (td_rw(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100165 bs = td->o.rw_min_bs;
Jens Axboe127f6862007-03-15 12:09:57 +0100166 else if (td_read(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100167 bs = td->o.min_bs[DDIR_READ];
Jens Axboe127f6862007-03-15 12:09:57 +0100168 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100169 bs = td->o.min_bs[DDIR_WRITE];
Jens Axboe127f6862007-03-15 12:09:57 +0100170
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100171 if (td->o.rate) {
172 rate = td->o.rate;
Jens Axboe127f6862007-03-15 12:09:57 +0100173 nr_reads_per_msec = (rate * 1024 * 1000LL) / bs;
174 } else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100175 nr_reads_per_msec = td->o.rate_iops * 1000UL;
Jens Axboe127f6862007-03-15 12:09:57 +0100176
177 if (!nr_reads_per_msec) {
178 log_err("rate lower than supported\n");
179 return -1;
180 }
181
182 td->rate_usec_cycle = 1000000000ULL / nr_reads_per_msec;
183 td->rate_pending_usleep = 0;
184 return 0;
185}
186
Jens Axboedad915e2006-10-27 11:10:18 +0200187/*
188 * Lazy way of fixing up options that depend on each other. We could also
189 * define option callback handlers, but this is easier.
190 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100191static int fixup_options(struct thread_data *td)
Jens Axboee1f36502006-10-27 10:54:08 +0200192{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100193 struct thread_options *o = &td->o;
Jens Axboedad915e2006-10-27 11:10:18 +0200194
Jens Axboe724e4432007-09-11 20:02:05 +0200195 if (read_only && td_write(td)) {
196 log_err("fio: job <%s> has write bit set, but fio is in read-only mode\n", td->o.name);
197 return 1;
198 }
199
Jens Axboee47f7992007-03-21 14:05:39 +0100200 if (o->rwmix[DDIR_READ] + o->rwmix[DDIR_WRITE] > 100)
201 o->rwmix[DDIR_WRITE] = 100 - o->rwmix[DDIR_READ];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100202
203 if (o->write_iolog_file && o->read_iolog_file) {
Jens Axboe076efc72006-10-27 11:24:25 +0200204 log_err("fio: read iolog overrides write_iolog\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100205 free(o->write_iolog_file);
206 o->write_iolog_file = NULL;
Jens Axboe076efc72006-10-27 11:24:25 +0200207 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100208
209 if (td->io_ops->flags & FIO_SYNCIO)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100210 o->iodepth = 1;
Jens Axboe16b462a2006-10-30 12:35:18 +0100211 else {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100212 if (!o->iodepth)
213 o->iodepth = o->open_files;
Jens Axboe16b462a2006-10-30 12:35:18 +0100214 }
215
216 /*
217 * only really works for sequential io for now, and with 1 file
218 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100219 if (o->zone_size && td_random(td) && o->open_files == 1)
220 o->zone_size = 0;
Jens Axboe16b462a2006-10-30 12:35:18 +0100221
222 /*
223 * Reads can do overwrites, we always need to pre-create the file
224 */
225 if (td_read(td) || td_rw(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100226 o->overwrite = 1;
Jens Axboe16b462a2006-10-30 12:35:18 +0100227
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100228 if (!o->min_bs[DDIR_READ])
229 o->min_bs[DDIR_READ]= o->bs[DDIR_READ];
230 if (!o->max_bs[DDIR_READ])
231 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
232 if (!o->min_bs[DDIR_WRITE])
233 o->min_bs[DDIR_WRITE]= o->bs[DDIR_WRITE];
234 if (!o->max_bs[DDIR_WRITE])
235 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100236
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100237 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
Jens Axboea00735e2006-11-03 08:58:08 +0100238
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100239 if (!o->file_size_high)
240 o->file_size_high = o->file_size_low;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100241
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100242 if (o->norandommap && o->verify != VERIFY_NONE) {
Jens Axboebb8895e2006-10-30 15:14:48 +0100243 log_err("fio: norandommap given, verify disabled\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100244 o->verify = VERIFY_NONE;
Jens Axboebb8895e2006-10-30 15:14:48 +0100245 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100246 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
Jens Axboe690adba2006-10-30 15:25:09 +0100247 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100248
249 /*
Jens Axboe48097d52007-02-17 06:30:44 +0100250 * thinktime_spin must be less than thinktime
251 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100252 if (o->thinktime_spin > o->thinktime)
253 o->thinktime_spin = o->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100254
255 /*
256 * The low water mark cannot be bigger than the iodepth
257 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100258 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
Jens Axboe9467b772007-02-27 19:56:43 +0100259 /*
260 * syslet work around - if the workload is sequential,
261 * we want to let the queue drain all the way down to
262 * avoid seeking between async threads
263 */
264 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100265 o->iodepth_low = 1;
Jens Axboe9467b772007-02-27 19:56:43 +0100266 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100267 o->iodepth_low = o->iodepth;
Jens Axboe9467b772007-02-27 19:56:43 +0100268 }
Jens Axboecb5ab512007-02-26 12:57:09 +0100269
270 /*
271 * If batch number isn't set, default to the same as iodepth
272 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100273 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
274 o->iodepth_batch = o->iodepth;
Jens Axboeb5af8292007-03-08 12:43:13 +0100275
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100276 if (o->nr_files > td->files_index)
277 o->nr_files = td->files_index;
Jens Axboe9f9214f2007-03-13 14:02:16 +0100278
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100279 if (o->open_files > o->nr_files || !o->open_files)
280 o->open_files = o->nr_files;
Jens Axboe4e991c22007-03-15 11:41:11 +0100281
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100282 if ((o->rate && o->rate_iops) || (o->ratemin && o->rate_iops_min)) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100283 log_err("fio: rate and rate_iops are mutually exclusive\n");
284 return 1;
285 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100286 if ((o->rate < o->ratemin) || (o->rate_iops < o->rate_iops_min)) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100287 log_err("fio: minimum rate exceeds rate\n");
288 return 1;
289 }
290
Jens Axboecf4464c2007-04-17 20:14:42 +0200291 if (!o->timeout && o->time_based) {
292 log_err("fio: time_based requires a runtime/timeout setting\n");
293 o->time_based = 0;
294 }
295
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100296 if (o->fill_device && !o->size)
297 o->size = ULONG_LONG_MAX;
298
Jens Axboe4e991c22007-03-15 11:41:11 +0100299 return 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200300}
301
Jens Axboe906c8d72006-06-13 09:37:56 +0200302/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100303 * This function leaks the buffer
304 */
305static char *to_kmg(unsigned int val)
306{
307 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100308 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100309 char *p = post;
310
Jens Axboe245142f2006-11-08 12:49:21 +0100311 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100312 if (val & 1023)
313 break;
314
315 val >>= 10;
316 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100317 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100318
319 snprintf(buf, 31, "%u%c", val, *p);
320 return buf;
321}
322
Jens Axboe09629a92007-03-09 09:00:06 +0100323/* External engines are specified by "external:name.o") */
324static const char *get_engine_name(const char *str)
325{
326 char *p = strstr(str, ":");
327
328 if (!p)
329 return str;
330
331 p++;
332 strip_blank_front(&p);
333 strip_blank_end(p);
334 return p;
335}
336
Jens Axboee132cba2007-03-14 14:23:54 +0100337static int exists_and_not_file(const char *filename)
338{
339 struct stat sb;
340
341 if (lstat(filename, &sb) == -1)
342 return 0;
343
344 if (S_ISREG(sb.st_mode))
345 return 0;
346
347 return 1;
348}
349
Jens Axboef8977ee2006-11-06 14:03:03 +0100350/*
Jens Axboe9c60ce62007-03-15 09:14:47 +0100351 * Initialize the various random states we need (random io, block size ranges,
352 * read/write mix, etc).
353 */
354static int init_random_state(struct thread_data *td)
355{
356 unsigned long seeds[6];
Jens Axboe68727072007-03-15 20:49:25 +0100357 int fd;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100358
359 fd = open("/dev/urandom", O_RDONLY);
360 if (fd == -1) {
361 td_verror(td, errno, "open");
362 return 1;
363 }
364
365 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
366 td_verror(td, EIO, "read");
367 close(fd);
368 return 1;
369 }
370
371 close(fd);
372
373 os_random_seed(seeds[0], &td->bsrange_state);
374 os_random_seed(seeds[1], &td->verify_state);
375 os_random_seed(seeds[2], &td->rwmix_state);
376
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100377 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100378 os_random_seed(seeds[3], &td->next_file_state);
379
380 os_random_seed(seeds[5], &td->file_size_state);
381
382 if (!td_random(td))
383 return 0;
384
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100385 if (td->o.rand_repeatable)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100386 seeds[4] = FIO_RANDSEED * td->thread_number;
387
Jens Axboe9c60ce62007-03-15 09:14:47 +0100388 os_random_seed(seeds[4], &td->random_state);
389 return 0;
390}
391
Jens Axboe9c60ce62007-03-15 09:14:47 +0100392/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200393 * Adds a job to the list of things todo. Sanitizes the various options
394 * to make sure we don't have conflicts, and initializes various
395 * members of td.
396 */
Jens Axboe75154842006-06-01 13:56:09 +0200397static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100398{
Jens Axboe413dd452007-02-23 09:26:09 +0100399 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
400 "randread", "randwrite", "randrw" };
Jens Axboeaf52b342007-03-13 10:07:47 +0100401 unsigned int i;
Jens Axboe09629a92007-03-09 09:00:06 +0100402 const char *engine;
Jens Axboeaf52b342007-03-13 10:07:47 +0100403 char fname[PATH_MAX];
Jens Axboee132cba2007-03-14 14:23:54 +0100404 int numjobs, file_alloced;
Jens Axboeebac4652005-12-08 15:25:21 +0100405
Jens Axboeebac4652005-12-08 15:25:21 +0100406 /*
407 * the def_thread is just for options, it's not a real job
408 */
409 if (td == &def_thread)
410 return 0;
411
Jens Axboecca73aa2007-04-04 11:09:19 +0200412 /*
413 * if we are just dumping the output command line, don't add the job
414 */
415 if (dump_cmdline) {
416 put_job(td);
417 return 0;
418 }
419
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100420 engine = get_engine_name(td->o.ioengine);
Jens Axboe09629a92007-03-09 09:00:06 +0100421 td->io_ops = load_ioengine(td, engine);
422 if (!td->io_ops) {
423 log_err("fio: failed to load engine %s\n", engine);
Jens Axboe205927a2007-03-15 11:06:32 +0100424 goto err;
Jens Axboe09629a92007-03-09 09:00:06 +0100425 }
Jens Axboedf641192006-10-12 07:55:41 +0200426
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100427 if (td->o.use_thread)
Jens Axboe9cedf162007-03-12 11:29:30 +0100428 nr_thread++;
429 else
430 nr_process++;
431
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100432 if (td->o.odirect)
Jens Axboe690adba2006-10-30 15:25:09 +0100433 td->io_ops->flags |= FIO_RAWIO;
434
Jens Axboee132cba2007-03-14 14:23:54 +0100435 file_alloced = 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100436 if (!td->o.filename && !td->files_index) {
Jens Axboee132cba2007-03-14 14:23:54 +0100437 file_alloced = 1;
Jens Axboe80be24f2007-03-12 11:01:25 +0100438
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100439 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
Jens Axboee132cba2007-03-14 14:23:54 +0100440 add_file(td, jobname);
Jens Axboe7b05a212007-03-13 11:17:07 +0100441 else {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100442 for (i = 0; i < td->o.nr_files; i++) {
Jens Axboee132cba2007-03-14 14:23:54 +0100443 sprintf(fname, "%s.%d.%d", jobname, td->thread_number, i);
Jens Axboe7b05a212007-03-13 11:17:07 +0100444 add_file(td, fname);
445 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100446 }
Jens Axboe0af7b542006-02-17 10:10:12 +0100447 }
Jens Axboeebac4652005-12-08 15:25:21 +0100448
Jens Axboe4e991c22007-03-15 11:41:11 +0100449 if (fixup_options(td))
450 goto err;
Jens Axboee0a22332006-12-20 12:54:25 +0100451
Jens Axboe07eb79d2007-04-12 13:02:38 +0200452 if (td->io_ops->flags & FIO_DISKLESSIO) {
453 struct fio_file *f;
454
455 for_each_file(td, f, i)
456 f->real_file_size = -1ULL;
457 }
458
Jens Axboe07739b52007-03-08 20:25:46 +0100459 td->mutex = fio_sem_init(0);
Jens Axboeebac4652005-12-08 15:25:21 +0100460
Jens Axboe756867b2007-03-06 15:19:24 +0100461 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
462 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
463 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
Jens Axboe1e3d53a2007-03-22 19:01:48 +0100464 td->ddir_nr = td->o.ddir_nr;
Jens Axboeebac4652005-12-08 15:25:21 +0100465
Jens Axboeb3d62a72007-03-20 14:23:26 +0100466 if ((td->o.stonewall || td->o.numjobs > 1 || td->o.new_group)
467 && prev_group_jobs) {
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100468 prev_group_jobs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100469 groupid++;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100470 }
Jens Axboeebac4652005-12-08 15:25:21 +0100471
472 td->groupid = groupid;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100473 prev_group_jobs++;
Jens Axboeebac4652005-12-08 15:25:21 +0100474
Jens Axboe9c60ce62007-03-15 09:14:47 +0100475 if (init_random_state(td))
476 goto err;
477
Jens Axboeebac4652005-12-08 15:25:21 +0100478 if (setup_rate(td))
479 goto err;
480
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100481 if (td->o.write_lat_log) {
Jens Axboe756867b2007-03-06 15:19:24 +0100482 setup_log(&td->ts.slat_log);
483 setup_log(&td->ts.clat_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100484 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100485 if (td->o.write_bw_log)
Jens Axboe756867b2007-03-06 15:19:24 +0100486 setup_log(&td->ts.bw_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100487
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100488 if (!td->o.name)
489 td->o.name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200490
Jens Axboec6ae0a52006-06-12 11:04:44 +0200491 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200492 if (!job_add_num) {
Jens Axboeba0fbe12007-03-09 14:34:23 +0100493 if (!strcmp(td->io_ops->name, "cpuio"))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100494 log_info("%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->o.name, td->o.cpuload, td->o.cpucycle);
Jens Axboef8977ee2006-11-06 14:03:03 +0100495 else {
496 char *c1, *c2, *c3, *c4;
497
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100498 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
499 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
500 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
501 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
Jens Axboef8977ee2006-11-06 14:03:03 +0100502
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100503 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s, ioengine=%s, iodepth=%u\n", td->o.name, td->groupid, ddir_str[td->o.td_ddir], c1, c2, c3, c4, td->io_ops->name, td->o.iodepth);
Jens Axboef8977ee2006-11-06 14:03:03 +0100504
505 free(c1);
506 free(c2);
507 free(c3);
508 free(c4);
509 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200510 } else if (job_add_num == 1)
Jens Axboe6d861442007-03-15 09:22:23 +0100511 log_info("...\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +0200512 }
Jens Axboeebac4652005-12-08 15:25:21 +0100513
514 /*
515 * recurse add identical jobs, clear numjobs and stonewall options
516 * as they don't apply to sub-jobs
517 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100518 numjobs = td->o.numjobs;
Jens Axboeebac4652005-12-08 15:25:21 +0100519 while (--numjobs) {
520 struct thread_data *td_new = get_new_job(0, td);
521
522 if (!td_new)
523 goto err;
524
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100525 td_new->o.numjobs = 1;
526 td_new->o.stonewall = 0;
Jens Axboe92c1d412007-03-28 10:04:08 +0200527 td_new->o.new_group = 0;
Jens Axboee132cba2007-03-14 14:23:54 +0100528
529 if (file_alloced) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100530 td_new->o.filename = NULL;
Jens Axboee132cba2007-03-14 14:23:54 +0100531 td_new->files_index = 0;
532 td_new->files = NULL;
533 }
534
Jens Axboe75154842006-06-01 13:56:09 +0200535 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100536
Jens Axboe75154842006-06-01 13:56:09 +0200537 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100538 goto err;
539 }
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100540
Jens Axboeebac4652005-12-08 15:25:21 +0100541 return 0;
542err:
543 put_job(td);
544 return -1;
545}
546
Jens Axboeebac4652005-12-08 15:25:21 +0100547static int is_empty_or_comment(char *line)
548{
549 unsigned int i;
550
551 for (i = 0; i < strlen(line); i++) {
552 if (line[i] == ';')
553 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +0100554 if (line[i] == '#')
555 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100556 if (!isspace(line[i]) && !iscntrl(line[i]))
557 return 0;
558 }
559
560 return 1;
561}
562
Jens Axboe313cb202006-12-21 09:50:00 +0100563/*
Jens Axboe07261982006-06-07 10:51:12 +0200564 * This is our [ini] type file parser.
565 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100566static int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100567{
Jens Axboee1f36502006-10-27 10:54:08 +0200568 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +0100569 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +0100570 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +0100571 FILE *f;
572 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200573 int ret = 0, stonewall;
Jens Axboe097b2992007-04-19 09:41:45 +0200574 int first_sect = 1;
Jens Axboeccf8f122007-09-27 10:48:55 +0200575 int skip_fgets = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100576
Aaron Carroll5a729cb2007-09-27 09:03:55 +0200577 if (!strcmp(file, "-"))
578 f = stdin;
579 else
580 f = fopen(file, "r");
581
Jens Axboeebac4652005-12-08 15:25:21 +0100582 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200583 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100584 return 1;
585 }
586
587 string = malloc(4096);
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200588
589 /*
590 * it's really 256 + small bit, 280 should suffice
591 */
592 name = malloc(280);
593 memset(name, 0, 280);
Jens Axboeebac4652005-12-08 15:25:21 +0100594
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200595 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100596 do {
Jens Axboeccf8f122007-09-27 10:48:55 +0200597 /*
598 * if skip_fgets is set, we already have loaded a line we
599 * haven't handled.
600 */
601 if (!skip_fgets) {
602 p = fgets(string, 4095, f);
603 if (!p)
604 break;
605 }
gurudas paicdc7f192007-03-29 10:10:56 +0200606
Jens Axboeccf8f122007-09-27 10:48:55 +0200607 skip_fgets = 0;
gurudas paicdc7f192007-03-29 10:10:56 +0200608 strip_blank_front(&p);
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800609 strip_blank_end(p);
gurudas paicdc7f192007-03-29 10:10:56 +0200610
Jens Axboeebac4652005-12-08 15:25:21 +0100611 if (is_empty_or_comment(p))
612 continue;
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800613 if (sscanf(p, "[%255s]", name) != 1) {
Jens Axboe088b4202007-07-19 14:53:01 +0200614 log_err("fio: option <%s> outside of [] job section\n", p);
615 break;
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800616 }
Jens Axboeebac4652005-12-08 15:25:21 +0100617
618 global = !strncmp(name, "global", 6);
619
620 name[strlen(name) - 1] = '\0';
621
Jens Axboecca73aa2007-04-04 11:09:19 +0200622 if (dump_cmdline) {
Jens Axboe097b2992007-04-19 09:41:45 +0200623 if (first_sect)
624 log_info("fio ");
Jens Axboecca73aa2007-04-04 11:09:19 +0200625 if (!global)
626 log_info("--name=%s ", name);
Jens Axboe097b2992007-04-19 09:41:45 +0200627 first_sect = 0;
Jens Axboecca73aa2007-04-04 11:09:19 +0200628 }
629
Jens Axboeebac4652005-12-08 15:25:21 +0100630 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200631 if (!td) {
632 ret = 1;
633 break;
634 }
Jens Axboeebac4652005-12-08 15:25:21 +0100635
Jens Axboe972cfd22006-06-09 11:08:56 +0200636 /*
637 * Seperate multiple job files by a stonewall
638 */
Jens Axboef9481912006-06-09 11:37:28 +0200639 if (!global && stonewall) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100640 td->o.stonewall = stonewall;
Jens Axboe972cfd22006-06-09 11:08:56 +0200641 stonewall = 0;
642 }
643
Jens Axboeebac4652005-12-08 15:25:21 +0100644 while ((p = fgets(string, 4096, f)) != NULL) {
645 if (is_empty_or_comment(p))
646 continue;
Jens Axboee1f36502006-10-27 10:54:08 +0200647
Jens Axboeb6754f92006-05-30 14:29:32 +0200648 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +0100649
Jens Axboeccf8f122007-09-27 10:48:55 +0200650 /*
651 * new section, break out and make sure we don't
652 * fgets() a new line at the top.
653 */
654 if (p[0] == '[') {
655 skip_fgets = 1;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100656 break;
Jens Axboeccf8f122007-09-27 10:48:55 +0200657 }
Jens Axboe7c124ac2006-10-30 13:36:52 +0100658
Jens Axboe4ae3f762006-05-30 13:35:14 +0200659 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200660
Jens Axboe45410ac2006-06-07 11:10:39 +0200661 /*
662 * Don't break here, continue parsing options so we
663 * dump all the bad ones. Makes trial/error fixups
664 * easier on the user.
665 */
Jens Axboe214e1ec2007-03-15 10:48:13 +0100666 ret |= fio_option_parse(td, p);
Jens Axboecca73aa2007-04-04 11:09:19 +0200667 if (!ret && dump_cmdline)
668 log_info("--%s ", p);
Jens Axboeebac4652005-12-08 15:25:21 +0100669 }
Jens Axboeebac4652005-12-08 15:25:21 +0100670
Jens Axboeccf8f122007-09-27 10:48:55 +0200671 if (!ret)
Jens Axboe45410ac2006-06-07 11:10:39 +0200672 ret = add_job(td, name, 0);
Jens Axboeccf8f122007-09-27 10:48:55 +0200673 else {
Jens Axboeb1508cf2006-11-02 09:12:40 +0100674 log_err("fio: job %s dropped\n", name);
675 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +0200676 }
Jens Axboe7c124ac2006-10-30 13:36:52 +0100677 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +0100678
Jens Axboecca73aa2007-04-04 11:09:19 +0200679 if (dump_cmdline)
680 log_info("\n");
681
Jens Axboeebac4652005-12-08 15:25:21 +0100682 free(string);
683 free(name);
Aaron Carroll5a729cb2007-09-27 09:03:55 +0200684 if (f != stdin)
685 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200686 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100687}
688
689static int fill_def_thread(void)
690{
691 memset(&def_thread, 0, sizeof(def_thread));
692
Jens Axboe375b2692007-05-22 09:13:02 +0200693 fio_getaffinity(getpid(), &def_thread.o.cpumask);
Jens Axboeebac4652005-12-08 15:25:21 +0100694
695 /*
Jens Axboeee738492007-01-10 11:23:16 +0100696 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +0100697 */
Jens Axboe214e1ec2007-03-15 10:48:13 +0100698 fio_fill_default_options(&def_thread);
Jens Axboeee738492007-01-10 11:23:16 +0100699
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100700 def_thread.o.timeout = def_timeout;
701 def_thread.o.write_bw_log = write_bw_log;
702 def_thread.o.write_lat_log = write_lat_log;
Jens Axboeee738492007-01-10 11:23:16 +0100703
Jens Axboeebac4652005-12-08 15:25:21 +0100704 return 0;
705}
706
Jens Axboeebac4652005-12-08 15:25:21 +0100707static void free_shm(void)
708{
709 struct shmid_ds sbuf;
710
711 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +0200712 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +0100713 threads = NULL;
714 shmctl(shm_id, IPC_RMID, &sbuf);
715 }
716}
717
Jens Axboe906c8d72006-06-13 09:37:56 +0200718/*
719 * The thread area is shared between the main process and the job
720 * threads/processes. So setup a shared memory segment that will hold
721 * all the job info.
722 */
Jens Axboeebac4652005-12-08 15:25:21 +0100723static int setup_thread_area(void)
724{
725 /*
726 * 1024 is too much on some machines, scale max_jobs if
727 * we get a failure that looks like too large a shm segment
728 */
729 do {
Jens Axboe906c8d72006-06-13 09:37:56 +0200730 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +0100731
Jens Axboe906c8d72006-06-13 09:37:56 +0200732 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +0100733 if (shm_id != -1)
734 break;
735 if (errno != EINVAL) {
736 perror("shmget");
737 break;
738 }
739
740 max_jobs >>= 1;
741 } while (max_jobs);
742
743 if (shm_id == -1)
744 return 1;
745
746 threads = shmat(shm_id, NULL, 0);
747 if (threads == (void *) -1) {
748 perror("shmat");
749 return 1;
750 }
751
Jens Axboe1f809d12007-10-25 18:34:02 +0200752 memset(threads, 0, max_jobs * sizeof(struct thread_data));
Jens Axboeebac4652005-12-08 15:25:21 +0100753 atexit(free_shm);
754 return 0;
755}
756
Jens Axboe45378b32007-12-19 13:54:38 +0100757static void usage(const char *name)
Jens Axboeb4692822006-10-27 13:43:22 +0200758{
Jens Axboe214e1ec2007-03-15 10:48:13 +0100759 printf("%s\n", fio_version_string);
Jens Axboe45378b32007-12-19 13:54:38 +0100760 printf("%s [options] [job options] <job file(s)>\n", name);
Jens Axboe214e1ec2007-03-15 10:48:13 +0100761 printf("\t--output\tWrite output to file\n");
762 printf("\t--timeout\tRuntime in seconds\n");
763 printf("\t--latency-log\tGenerate per-job latency logs\n");
764 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
765 printf("\t--minimal\tMinimal (terse) output\n");
766 printf("\t--version\tPrint version info and exit\n");
767 printf("\t--help\t\tPrint this page\n");
768 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
Jens Axboecca73aa2007-04-04 11:09:19 +0200769 printf("\t--showcmd\tTurn a job file into command line options\n");
Aaron Carrolle592a062007-09-14 09:49:41 +0200770 printf("\t--eta=when\tWhen ETA estimate should be printed\n");
771 printf("\t \tMay be \"always\", \"never\" or \"auto\"\n");
Jens Axboeb4692822006-10-27 13:43:22 +0200772}
773
Jens Axboe214e1ec2007-03-15 10:48:13 +0100774static int parse_cmd_line(int argc, char *argv[])
775{
776 struct thread_data *td = NULL;
777 int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
778
779 while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
780 switch (c) {
781 case 't':
782 def_timeout = atoi(optarg);
783 break;
784 case 'l':
785 write_lat_log = 1;
786 break;
787 case 'w':
788 write_bw_log = 1;
789 break;
790 case 'o':
791 f_out = fopen(optarg, "w+");
792 if (!f_out) {
793 perror("fopen output");
794 exit(1);
795 }
796 f_err = f_out;
797 break;
798 case 'm':
799 terse_output = 1;
800 break;
801 case 'h':
Jens Axboe45378b32007-12-19 13:54:38 +0100802 usage(argv[0]);
Jens Axboe214e1ec2007-03-15 10:48:13 +0100803 exit(0);
804 case 'c':
805 exit(fio_show_option_help(optarg));
Jens Axboecca73aa2007-04-04 11:09:19 +0200806 case 's':
807 dump_cmdline = 1;
808 break;
Jens Axboe724e4432007-09-11 20:02:05 +0200809 case 'r':
810 read_only = 1;
811 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +0100812 case 'v':
813 printf("%s\n", fio_version_string);
814 exit(0);
Aaron Carrolle592a062007-09-14 09:49:41 +0200815 case 'e':
816 if (!strcmp("always", optarg))
817 eta_print = FIO_ETA_ALWAYS;
818 else if (!strcmp("never", optarg))
819 eta_print = FIO_ETA_NEVER;
820 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +0100821 case FIO_GETOPT_JOB: {
822 const char *opt = long_options[lidx].name;
823 char *val = optarg;
824
825 if (!strncmp(opt, "name", 4) && td) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100826 ret = add_job(td, td->o.name ?: "fio", 0);
Jens Axboe214e1ec2007-03-15 10:48:13 +0100827 if (ret) {
828 put_job(td);
829 return 0;
830 }
831 td = NULL;
832 }
833 if (!td) {
Jens Axboe3106f222007-04-19 09:53:28 +0200834 int global = 0;
835
836 if (strncmp(opt, "name", 4) ||
837 !strncmp(val, "global", 6))
838 global = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +0100839
840 td = get_new_job(global, &def_thread);
841 if (!td)
842 return 0;
843 }
844
845 ret = fio_cmd_option_parse(td, opt, val);
846 if (ret)
847 dont_add_job = 1;
848 break;
849 }
850 default:
851 break;
852 }
853 }
854
855 if (td) {
856 if (dont_add_job)
857 put_job(td);
858 else {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100859 ret = add_job(td, td->o.name ?: "fio", 0);
Jens Axboe214e1ec2007-03-15 10:48:13 +0100860 if (ret)
861 put_job(td);
862 }
863 }
864
865 while (optind < argc) {
866 ini_idx++;
867 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
868 ini_file[ini_idx - 1] = strdup(argv[optind]);
869 optind++;
870 }
871
872 return ini_idx;
873}
874
875
Jens Axboeebac4652005-12-08 15:25:21 +0100876int parse_options(int argc, char *argv[])
877{
Jens Axboe972cfd22006-06-09 11:08:56 +0200878 int job_files, i;
879
Jens Axboeb4692822006-10-27 13:43:22 +0200880 f_out = stdout;
881 f_err = stderr;
882
Jens Axboe214e1ec2007-03-15 10:48:13 +0100883 fio_options_dup_and_init(long_options);
Jens Axboeb4692822006-10-27 13:43:22 +0200884
Jens Axboeebac4652005-12-08 15:25:21 +0100885 if (setup_thread_area())
886 return 1;
887 if (fill_def_thread())
888 return 1;
889
Jens Axboe972cfd22006-06-09 11:08:56 +0200890 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +0100891
Jens Axboe972cfd22006-06-09 11:08:56 +0200892 for (i = 0; i < job_files; i++) {
893 if (fill_def_thread())
894 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200895 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +0200896 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +0200897 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +0200898 }
Jens Axboeebac4652005-12-08 15:25:21 +0100899
Jens Axboe88c6ed82006-06-09 11:28:10 +0200900 free(ini_file);
Jens Axboed23bb322007-03-23 15:57:56 +0100901 options_mem_free(&def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +0200902
903 if (!thread_number) {
Jens Axboecca73aa2007-04-04 11:09:19 +0200904 if (dump_cmdline)
905 return 0;
906
Jens Axboeb4692822006-10-27 13:43:22 +0200907 log_err("No jobs defined(s)\n");
Jens Axboe45378b32007-12-19 13:54:38 +0100908 usage(argv[0]);
Jens Axboeb4692822006-10-27 13:43:22 +0200909 return 1;
910 }
911
Jens Axboeebac4652005-12-08 15:25:21 +0100912 return 0;
913}