blob: 50c476012d374014b843eff27560f629ae6c9f97 [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 Axboe12b8a832007-04-02 11:11:34 +020020static char fio_version_string[] = "fio 1.15.1";
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 Axboee1f36502006-10-27 10:54:08 +020026
Jens Axboe214e1ec2007-03-15 10:48:13 +010027struct thread_data def_thread;
28struct thread_data *threads = NULL;
Jens Axboee1f36502006-10-27 10:54:08 +020029
Jens Axboe214e1ec2007-03-15 10:48:13 +010030int exitall_on_terminate = 0;
31int terse_output = 0;
32unsigned long long mlock_size = 0;
33FILE *f_out = NULL;
34FILE *f_err = NULL;
Jens Axboeee738492007-01-10 11:23:16 +010035
Jens Axboe214e1ec2007-03-15 10:48:13 +010036int write_bw_log = 0;
Jens Axboee1f36502006-10-27 10:54:08 +020037
Jens Axboe214e1ec2007-03-15 10:48:13 +010038static int def_timeout = 0;
39static int write_lat_log = 0;
40
41static int prev_group_jobs;
Jens Axboeb4692822006-10-27 13:43:22 +020042
43/*
44 * Command line options. These will contain the above, plus a few
45 * extra that only pertain to fio itself and not jobs.
46 */
Jens Axboe214e1ec2007-03-15 10:48:13 +010047static struct option long_options[FIO_NR_OPTIONS] = {
Jens Axboeb4692822006-10-27 13:43:22 +020048 {
49 .name = "output",
50 .has_arg = required_argument,
51 .val = 'o',
52 },
53 {
54 .name = "timeout",
55 .has_arg = required_argument,
56 .val = 't',
57 },
58 {
59 .name = "latency-log",
60 .has_arg = required_argument,
61 .val = 'l',
62 },
63 {
64 .name = "bandwidth-log",
65 .has_arg = required_argument,
66 .val = 'b',
67 },
68 {
69 .name = "minimal",
70 .has_arg = optional_argument,
71 .val = 'm',
72 },
73 {
74 .name = "version",
75 .has_arg = no_argument,
76 .val = 'v',
77 },
78 {
Jens Axboefd28ca42007-01-09 21:20:13 +010079 .name = "help",
80 .has_arg = no_argument,
81 .val = 'h',
82 },
83 {
84 .name = "cmdhelp",
Jens Axboe320beef2007-03-01 10:44:12 +010085 .has_arg = optional_argument,
Jens Axboefd28ca42007-01-09 21:20:13 +010086 .val = 'c',
87 },
88 {
Jens Axboeb4692822006-10-27 13:43:22 +020089 .name = NULL,
90 },
91};
92
Joel Becker9728ce32007-03-01 08:24:39 +010093FILE *get_f_out()
94{
95 return f_out;
96}
97
98FILE *get_f_err()
99{
100 return f_err;
101}
102
Jens Axboe906c8d72006-06-13 09:37:56 +0200103/*
104 * Return a free job structure.
105 */
Jens Axboeebac4652005-12-08 15:25:21 +0100106static struct thread_data *get_new_job(int global, struct thread_data *parent)
107{
108 struct thread_data *td;
109
110 if (global)
111 return &def_thread;
112 if (thread_number >= max_jobs)
113 return NULL;
114
115 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200116 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100117
Jens Axboecade3ef2007-03-23 15:29:45 +0100118 dup_files(td, parent);
Jens Axboed23bb322007-03-23 15:57:56 +0100119 options_mem_dupe(td);
Jens Axboecade3ef2007-03-23 15:29:45 +0100120
Jens Axboeebac4652005-12-08 15:25:21 +0100121 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100122 return td;
123}
124
125static void put_job(struct thread_data *td)
126{
Jens Axboe549577a2006-10-30 12:23:41 +0100127 if (td == &def_thread)
128 return;
129
Jens Axboe16edf252007-02-10 14:59:22 +0100130 if (td->error)
Jens Axboe6d861442007-03-15 09:22:23 +0100131 log_info("fio: %s\n", td->verror);
Jens Axboe16edf252007-02-10 14:59:22 +0100132
Jens Axboeebac4652005-12-08 15:25:21 +0100133 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
134 thread_number--;
135}
136
Jens Axboe127f6862007-03-15 12:09:57 +0100137static int setup_rate(struct thread_data *td)
138{
139 unsigned long nr_reads_per_msec;
140 unsigned long long rate;
141 unsigned int bs;
142
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100143 if (!td->o.rate && !td->o.rate_iops)
Jens Axboe127f6862007-03-15 12:09:57 +0100144 return 0;
145
146 if (td_rw(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100147 bs = td->o.rw_min_bs;
Jens Axboe127f6862007-03-15 12:09:57 +0100148 else if (td_read(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100149 bs = td->o.min_bs[DDIR_READ];
Jens Axboe127f6862007-03-15 12:09:57 +0100150 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100151 bs = td->o.min_bs[DDIR_WRITE];
Jens Axboe127f6862007-03-15 12:09:57 +0100152
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100153 if (td->o.rate) {
154 rate = td->o.rate;
Jens Axboe127f6862007-03-15 12:09:57 +0100155 nr_reads_per_msec = (rate * 1024 * 1000LL) / bs;
156 } else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100157 nr_reads_per_msec = td->o.rate_iops * 1000UL;
Jens Axboe127f6862007-03-15 12:09:57 +0100158
159 if (!nr_reads_per_msec) {
160 log_err("rate lower than supported\n");
161 return -1;
162 }
163
164 td->rate_usec_cycle = 1000000000ULL / nr_reads_per_msec;
165 td->rate_pending_usleep = 0;
166 return 0;
167}
168
Jens Axboedad915e2006-10-27 11:10:18 +0200169/*
170 * Lazy way of fixing up options that depend on each other. We could also
171 * define option callback handlers, but this is easier.
172 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100173static int fixup_options(struct thread_data *td)
Jens Axboee1f36502006-10-27 10:54:08 +0200174{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100175 struct thread_options *o = &td->o;
Jens Axboedad915e2006-10-27 11:10:18 +0200176
Jens Axboee47f7992007-03-21 14:05:39 +0100177 if (o->rwmix[DDIR_READ] + o->rwmix[DDIR_WRITE] > 100)
178 o->rwmix[DDIR_WRITE] = 100 - o->rwmix[DDIR_READ];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100179
180 if (o->write_iolog_file && o->read_iolog_file) {
Jens Axboe076efc72006-10-27 11:24:25 +0200181 log_err("fio: read iolog overrides write_iolog\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100182 free(o->write_iolog_file);
183 o->write_iolog_file = NULL;
Jens Axboe076efc72006-10-27 11:24:25 +0200184 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100185
186 if (td->io_ops->flags & FIO_SYNCIO)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100187 o->iodepth = 1;
Jens Axboe16b462a2006-10-30 12:35:18 +0100188 else {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100189 if (!o->iodepth)
190 o->iodepth = o->open_files;
Jens Axboe16b462a2006-10-30 12:35:18 +0100191 }
192
193 /*
194 * only really works for sequential io for now, and with 1 file
195 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100196 if (o->zone_size && td_random(td) && o->open_files == 1)
197 o->zone_size = 0;
Jens Axboe16b462a2006-10-30 12:35:18 +0100198
199 /*
200 * Reads can do overwrites, we always need to pre-create the file
201 */
202 if (td_read(td) || td_rw(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100203 o->overwrite = 1;
Jens Axboe16b462a2006-10-30 12:35:18 +0100204
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100205 if (!o->min_bs[DDIR_READ])
206 o->min_bs[DDIR_READ]= o->bs[DDIR_READ];
207 if (!o->max_bs[DDIR_READ])
208 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
209 if (!o->min_bs[DDIR_WRITE])
210 o->min_bs[DDIR_WRITE]= o->bs[DDIR_WRITE];
211 if (!o->max_bs[DDIR_WRITE])
212 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100213
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100214 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
Jens Axboea00735e2006-11-03 08:58:08 +0100215
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100216 if (!o->file_size_high)
217 o->file_size_high = o->file_size_low;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100218
Jens Axboe16b462a2006-10-30 12:35:18 +0100219 if (td_read(td) && !td_rw(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100220 o->verify = 0;
Jens Axboebb8895e2006-10-30 15:14:48 +0100221
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100222 if (o->norandommap && o->verify != VERIFY_NONE) {
Jens Axboebb8895e2006-10-30 15:14:48 +0100223 log_err("fio: norandommap given, verify disabled\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100224 o->verify = VERIFY_NONE;
Jens Axboebb8895e2006-10-30 15:14:48 +0100225 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100226 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
Jens Axboe690adba2006-10-30 15:25:09 +0100227 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100228
229 /*
Jens Axboe48097d52007-02-17 06:30:44 +0100230 * thinktime_spin must be less than thinktime
231 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100232 if (o->thinktime_spin > o->thinktime)
233 o->thinktime_spin = o->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100234
235 /*
236 * The low water mark cannot be bigger than the iodepth
237 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100238 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
Jens Axboe9467b772007-02-27 19:56:43 +0100239 /*
240 * syslet work around - if the workload is sequential,
241 * we want to let the queue drain all the way down to
242 * avoid seeking between async threads
243 */
244 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100245 o->iodepth_low = 1;
Jens Axboe9467b772007-02-27 19:56:43 +0100246 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100247 o->iodepth_low = o->iodepth;
Jens Axboe9467b772007-02-27 19:56:43 +0100248 }
Jens Axboecb5ab512007-02-26 12:57:09 +0100249
250 /*
251 * If batch number isn't set, default to the same as iodepth
252 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100253 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
254 o->iodepth_batch = o->iodepth;
Jens Axboeb5af8292007-03-08 12:43:13 +0100255
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100256 if (o->nr_files > td->files_index)
257 o->nr_files = td->files_index;
Jens Axboe9f9214f2007-03-13 14:02:16 +0100258
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100259 if (o->open_files > o->nr_files || !o->open_files)
260 o->open_files = o->nr_files;
Jens Axboe4e991c22007-03-15 11:41:11 +0100261
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100262 if ((o->rate && o->rate_iops) || (o->ratemin && o->rate_iops_min)) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100263 log_err("fio: rate and rate_iops are mutually exclusive\n");
264 return 1;
265 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100266 if ((o->rate < o->ratemin) || (o->rate_iops < o->rate_iops_min)) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100267 log_err("fio: minimum rate exceeds rate\n");
268 return 1;
269 }
270
271 return 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200272}
273
Jens Axboe906c8d72006-06-13 09:37:56 +0200274/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100275 * This function leaks the buffer
276 */
277static char *to_kmg(unsigned int val)
278{
279 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100280 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100281 char *p = post;
282
Jens Axboe245142f2006-11-08 12:49:21 +0100283 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100284 if (val & 1023)
285 break;
286
287 val >>= 10;
288 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100289 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100290
291 snprintf(buf, 31, "%u%c", val, *p);
292 return buf;
293}
294
Jens Axboe09629a92007-03-09 09:00:06 +0100295/* External engines are specified by "external:name.o") */
296static const char *get_engine_name(const char *str)
297{
298 char *p = strstr(str, ":");
299
300 if (!p)
301 return str;
302
303 p++;
304 strip_blank_front(&p);
305 strip_blank_end(p);
306 return p;
307}
308
Jens Axboee132cba2007-03-14 14:23:54 +0100309static int exists_and_not_file(const char *filename)
310{
311 struct stat sb;
312
313 if (lstat(filename, &sb) == -1)
314 return 0;
315
316 if (S_ISREG(sb.st_mode))
317 return 0;
318
319 return 1;
320}
321
Jens Axboef8977ee2006-11-06 14:03:03 +0100322/*
Jens Axboe9c60ce62007-03-15 09:14:47 +0100323 * Initialize the various random states we need (random io, block size ranges,
324 * read/write mix, etc).
325 */
326static int init_random_state(struct thread_data *td)
327{
328 unsigned long seeds[6];
Jens Axboe68727072007-03-15 20:49:25 +0100329 int fd;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100330
331 fd = open("/dev/urandom", O_RDONLY);
332 if (fd == -1) {
333 td_verror(td, errno, "open");
334 return 1;
335 }
336
337 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
338 td_verror(td, EIO, "read");
339 close(fd);
340 return 1;
341 }
342
343 close(fd);
344
345 os_random_seed(seeds[0], &td->bsrange_state);
346 os_random_seed(seeds[1], &td->verify_state);
347 os_random_seed(seeds[2], &td->rwmix_state);
348
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100349 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100350 os_random_seed(seeds[3], &td->next_file_state);
351
352 os_random_seed(seeds[5], &td->file_size_state);
353
354 if (!td_random(td))
355 return 0;
356
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100357 if (td->o.rand_repeatable)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100358 seeds[4] = FIO_RANDSEED * td->thread_number;
359
Jens Axboe9c60ce62007-03-15 09:14:47 +0100360 os_random_seed(seeds[4], &td->random_state);
361 return 0;
362}
363
Jens Axboe9c60ce62007-03-15 09:14:47 +0100364/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200365 * Adds a job to the list of things todo. Sanitizes the various options
366 * to make sure we don't have conflicts, and initializes various
367 * members of td.
368 */
Jens Axboe75154842006-06-01 13:56:09 +0200369static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100370{
Jens Axboe413dd452007-02-23 09:26:09 +0100371 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
372 "randread", "randwrite", "randrw" };
Jens Axboeaf52b342007-03-13 10:07:47 +0100373 unsigned int i;
Jens Axboe09629a92007-03-09 09:00:06 +0100374 const char *engine;
Jens Axboeaf52b342007-03-13 10:07:47 +0100375 char fname[PATH_MAX];
Jens Axboee132cba2007-03-14 14:23:54 +0100376 int numjobs, file_alloced;
Jens Axboeebac4652005-12-08 15:25:21 +0100377
Jens Axboeebac4652005-12-08 15:25:21 +0100378 /*
379 * the def_thread is just for options, it's not a real job
380 */
381 if (td == &def_thread)
382 return 0;
383
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100384 engine = get_engine_name(td->o.ioengine);
Jens Axboe09629a92007-03-09 09:00:06 +0100385 td->io_ops = load_ioengine(td, engine);
386 if (!td->io_ops) {
387 log_err("fio: failed to load engine %s\n", engine);
Jens Axboe205927a2007-03-15 11:06:32 +0100388 goto err;
Jens Axboe09629a92007-03-09 09:00:06 +0100389 }
Jens Axboedf641192006-10-12 07:55:41 +0200390
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100391 if (td->o.use_thread)
Jens Axboe9cedf162007-03-12 11:29:30 +0100392 nr_thread++;
393 else
394 nr_process++;
395
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100396 if (td->o.odirect)
Jens Axboe690adba2006-10-30 15:25:09 +0100397 td->io_ops->flags |= FIO_RAWIO;
398
Jens Axboee132cba2007-03-14 14:23:54 +0100399 file_alloced = 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100400 if (!td->o.filename && !td->files_index) {
Jens Axboee132cba2007-03-14 14:23:54 +0100401 file_alloced = 1;
Jens Axboe80be24f2007-03-12 11:01:25 +0100402
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100403 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
Jens Axboee132cba2007-03-14 14:23:54 +0100404 add_file(td, jobname);
Jens Axboe7b05a212007-03-13 11:17:07 +0100405 else {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100406 for (i = 0; i < td->o.nr_files; i++) {
Jens Axboee132cba2007-03-14 14:23:54 +0100407 sprintf(fname, "%s.%d.%d", jobname, td->thread_number, i);
Jens Axboe7b05a212007-03-13 11:17:07 +0100408 add_file(td, fname);
409 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100410 }
Jens Axboe0af7b542006-02-17 10:10:12 +0100411 }
Jens Axboeebac4652005-12-08 15:25:21 +0100412
Jens Axboe4e991c22007-03-15 11:41:11 +0100413 if (fixup_options(td))
414 goto err;
Jens Axboee0a22332006-12-20 12:54:25 +0100415
Jens Axboe07739b52007-03-08 20:25:46 +0100416 td->mutex = fio_sem_init(0);
Jens Axboeebac4652005-12-08 15:25:21 +0100417
Jens Axboe756867b2007-03-06 15:19:24 +0100418 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
419 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
420 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
Jens Axboe1e3d53a2007-03-22 19:01:48 +0100421 td->ddir_nr = td->o.ddir_nr;
Jens Axboeebac4652005-12-08 15:25:21 +0100422
Jens Axboeb3d62a72007-03-20 14:23:26 +0100423 if ((td->o.stonewall || td->o.numjobs > 1 || td->o.new_group)
424 && prev_group_jobs) {
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100425 prev_group_jobs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100426 groupid++;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100427 }
Jens Axboeebac4652005-12-08 15:25:21 +0100428
429 td->groupid = groupid;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100430 prev_group_jobs++;
Jens Axboeebac4652005-12-08 15:25:21 +0100431
Jens Axboe9c60ce62007-03-15 09:14:47 +0100432 if (init_random_state(td))
433 goto err;
434
Jens Axboeebac4652005-12-08 15:25:21 +0100435 if (setup_rate(td))
436 goto err;
437
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100438 if (td->o.write_lat_log) {
Jens Axboe756867b2007-03-06 15:19:24 +0100439 setup_log(&td->ts.slat_log);
440 setup_log(&td->ts.clat_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100441 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100442 if (td->o.write_bw_log)
Jens Axboe756867b2007-03-06 15:19:24 +0100443 setup_log(&td->ts.bw_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100444
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100445 if (!td->o.name)
446 td->o.name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200447
Jens Axboec6ae0a52006-06-12 11:04:44 +0200448 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200449 if (!job_add_num) {
Jens Axboeba0fbe12007-03-09 14:34:23 +0100450 if (!strcmp(td->io_ops->name, "cpuio"))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100451 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 +0100452 else {
453 char *c1, *c2, *c3, *c4;
454
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100455 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
456 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
457 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
458 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
Jens Axboef8977ee2006-11-06 14:03:03 +0100459
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100460 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 +0100461
462 free(c1);
463 free(c2);
464 free(c3);
465 free(c4);
466 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200467 } else if (job_add_num == 1)
Jens Axboe6d861442007-03-15 09:22:23 +0100468 log_info("...\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +0200469 }
Jens Axboeebac4652005-12-08 15:25:21 +0100470
471 /*
472 * recurse add identical jobs, clear numjobs and stonewall options
473 * as they don't apply to sub-jobs
474 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100475 numjobs = td->o.numjobs;
Jens Axboeebac4652005-12-08 15:25:21 +0100476 while (--numjobs) {
477 struct thread_data *td_new = get_new_job(0, td);
478
479 if (!td_new)
480 goto err;
481
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100482 td_new->o.numjobs = 1;
483 td_new->o.stonewall = 0;
Jens Axboe92c1d412007-03-28 10:04:08 +0200484 td_new->o.new_group = 0;
Jens Axboee132cba2007-03-14 14:23:54 +0100485
486 if (file_alloced) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100487 td_new->o.filename = NULL;
Jens Axboee132cba2007-03-14 14:23:54 +0100488 td_new->files_index = 0;
489 td_new->files = NULL;
490 }
491
Jens Axboe75154842006-06-01 13:56:09 +0200492 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100493
Jens Axboe75154842006-06-01 13:56:09 +0200494 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100495 goto err;
496 }
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100497
Jens Axboeebac4652005-12-08 15:25:21 +0100498 return 0;
499err:
500 put_job(td);
501 return -1;
502}
503
Jens Axboeebac4652005-12-08 15:25:21 +0100504static int is_empty_or_comment(char *line)
505{
506 unsigned int i;
507
508 for (i = 0; i < strlen(line); i++) {
509 if (line[i] == ';')
510 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +0100511 if (line[i] == '#')
512 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100513 if (!isspace(line[i]) && !iscntrl(line[i]))
514 return 0;
515 }
516
517 return 1;
518}
519
Jens Axboe313cb202006-12-21 09:50:00 +0100520/*
Jens Axboe07261982006-06-07 10:51:12 +0200521 * This is our [ini] type file parser.
522 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100523static int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100524{
Jens Axboee1f36502006-10-27 10:54:08 +0200525 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +0100526 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +0100527 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +0100528 fpos_t off;
529 FILE *f;
530 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200531 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +0100532
533 f = fopen(file, "r");
534 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200535 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100536 return 1;
537 }
538
539 string = malloc(4096);
540 name = malloc(256);
Jens Axboefee3bb42006-10-30 13:32:08 +0100541 memset(name, 0, 256);
Jens Axboeebac4652005-12-08 15:25:21 +0100542
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200543 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100544 do {
545 p = fgets(string, 4095, f);
546 if (!p)
Jens Axboe45410ac2006-06-07 11:10:39 +0200547 break;
gurudas paicdc7f192007-03-29 10:10:56 +0200548
549 strip_blank_front(&p);
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800550 strip_blank_end(p);
gurudas paicdc7f192007-03-29 10:10:56 +0200551
Jens Axboeebac4652005-12-08 15:25:21 +0100552 if (is_empty_or_comment(p))
553 continue;
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800554 if (sscanf(p, "[%255s]", name) != 1) {
555 log_err("fio: option <%s> outside of job section\n", p);
Jens Axboeebac4652005-12-08 15:25:21 +0100556 continue;
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800557 }
Jens Axboeebac4652005-12-08 15:25:21 +0100558
559 global = !strncmp(name, "global", 6);
560
561 name[strlen(name) - 1] = '\0';
562
563 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200564 if (!td) {
565 ret = 1;
566 break;
567 }
Jens Axboeebac4652005-12-08 15:25:21 +0100568
Jens Axboe972cfd22006-06-09 11:08:56 +0200569 /*
570 * Seperate multiple job files by a stonewall
571 */
Jens Axboef9481912006-06-09 11:37:28 +0200572 if (!global && stonewall) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100573 td->o.stonewall = stonewall;
Jens Axboe972cfd22006-06-09 11:08:56 +0200574 stonewall = 0;
575 }
576
Jens Axboeebac4652005-12-08 15:25:21 +0100577 fgetpos(f, &off);
578 while ((p = fgets(string, 4096, f)) != NULL) {
579 if (is_empty_or_comment(p))
580 continue;
Jens Axboee1f36502006-10-27 10:54:08 +0200581
Jens Axboeb6754f92006-05-30 14:29:32 +0200582 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +0100583
584 if (p[0] == '[')
585 break;
586
Jens Axboe4ae3f762006-05-30 13:35:14 +0200587 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200588
Jens Axboee1f36502006-10-27 10:54:08 +0200589 fgetpos(f, &off);
Jens Axboeebac4652005-12-08 15:25:21 +0100590
Jens Axboe45410ac2006-06-07 11:10:39 +0200591 /*
592 * Don't break here, continue parsing options so we
593 * dump all the bad ones. Makes trial/error fixups
594 * easier on the user.
595 */
Jens Axboe214e1ec2007-03-15 10:48:13 +0100596 ret |= fio_option_parse(td, p);
Jens Axboeebac4652005-12-08 15:25:21 +0100597 }
Jens Axboeebac4652005-12-08 15:25:21 +0100598
Jens Axboe45410ac2006-06-07 11:10:39 +0200599 if (!ret) {
600 fsetpos(f, &off);
601 ret = add_job(td, name, 0);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100602 } else {
603 log_err("fio: job %s dropped\n", name);
604 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +0200605 }
Jens Axboe7c124ac2006-10-30 13:36:52 +0100606 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +0100607
608 free(string);
609 free(name);
610 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200611 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100612}
613
614static int fill_def_thread(void)
615{
616 memset(&def_thread, 0, sizeof(def_thread));
617
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100618 if (fio_getaffinity(getpid(), &def_thread.o.cpumask) == -1) {
Jens Axboeebac4652005-12-08 15:25:21 +0100619 perror("sched_getaffinity");
620 return 1;
621 }
622
623 /*
Jens Axboeee738492007-01-10 11:23:16 +0100624 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +0100625 */
Jens Axboe214e1ec2007-03-15 10:48:13 +0100626 fio_fill_default_options(&def_thread);
Jens Axboeee738492007-01-10 11:23:16 +0100627
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100628 def_thread.o.timeout = def_timeout;
629 def_thread.o.write_bw_log = write_bw_log;
630 def_thread.o.write_lat_log = write_lat_log;
Jens Axboeee738492007-01-10 11:23:16 +0100631
Jens Axboeebac4652005-12-08 15:25:21 +0100632#ifdef FIO_HAVE_DISK_UTIL
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100633 def_thread.o.do_disk_util = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100634#endif
635
636 return 0;
637}
638
Jens Axboeebac4652005-12-08 15:25:21 +0100639static void free_shm(void)
640{
641 struct shmid_ds sbuf;
642
643 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +0200644 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +0100645 threads = NULL;
646 shmctl(shm_id, IPC_RMID, &sbuf);
647 }
648}
649
Jens Axboe906c8d72006-06-13 09:37:56 +0200650/*
651 * The thread area is shared between the main process and the job
652 * threads/processes. So setup a shared memory segment that will hold
653 * all the job info.
654 */
Jens Axboeebac4652005-12-08 15:25:21 +0100655static int setup_thread_area(void)
656{
657 /*
658 * 1024 is too much on some machines, scale max_jobs if
659 * we get a failure that looks like too large a shm segment
660 */
661 do {
Jens Axboe906c8d72006-06-13 09:37:56 +0200662 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +0100663
Jens Axboe906c8d72006-06-13 09:37:56 +0200664 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +0100665 if (shm_id != -1)
666 break;
667 if (errno != EINVAL) {
668 perror("shmget");
669 break;
670 }
671
672 max_jobs >>= 1;
673 } while (max_jobs);
674
675 if (shm_id == -1)
676 return 1;
677
678 threads = shmat(shm_id, NULL, 0);
679 if (threads == (void *) -1) {
680 perror("shmat");
681 return 1;
682 }
683
684 atexit(free_shm);
685 return 0;
686}
687
Jens Axboe214e1ec2007-03-15 10:48:13 +0100688static void usage(void)
Jens Axboeb4692822006-10-27 13:43:22 +0200689{
Jens Axboe214e1ec2007-03-15 10:48:13 +0100690 printf("%s\n", fio_version_string);
691 printf("\t--output\tWrite output to file\n");
692 printf("\t--timeout\tRuntime in seconds\n");
693 printf("\t--latency-log\tGenerate per-job latency logs\n");
694 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
695 printf("\t--minimal\tMinimal (terse) output\n");
696 printf("\t--version\tPrint version info and exit\n");
697 printf("\t--help\t\tPrint this page\n");
698 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
Jens Axboeb4692822006-10-27 13:43:22 +0200699}
700
Jens Axboe214e1ec2007-03-15 10:48:13 +0100701static int parse_cmd_line(int argc, char *argv[])
702{
703 struct thread_data *td = NULL;
704 int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
705
706 while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
707 switch (c) {
708 case 't':
709 def_timeout = atoi(optarg);
710 break;
711 case 'l':
712 write_lat_log = 1;
713 break;
714 case 'w':
715 write_bw_log = 1;
716 break;
717 case 'o':
718 f_out = fopen(optarg, "w+");
719 if (!f_out) {
720 perror("fopen output");
721 exit(1);
722 }
723 f_err = f_out;
724 break;
725 case 'm':
726 terse_output = 1;
727 break;
728 case 'h':
729 usage();
730 exit(0);
731 case 'c':
732 exit(fio_show_option_help(optarg));
733 case 'v':
734 printf("%s\n", fio_version_string);
735 exit(0);
736 case FIO_GETOPT_JOB: {
737 const char *opt = long_options[lidx].name;
738 char *val = optarg;
739
740 if (!strncmp(opt, "name", 4) && td) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100741 ret = add_job(td, td->o.name ?: "fio", 0);
Jens Axboe214e1ec2007-03-15 10:48:13 +0100742 if (ret) {
743 put_job(td);
744 return 0;
745 }
746 td = NULL;
747 }
748 if (!td) {
749 int global = !strncmp(val, "global", 6);
750
751 td = get_new_job(global, &def_thread);
752 if (!td)
753 return 0;
754 }
755
756 ret = fio_cmd_option_parse(td, opt, val);
757 if (ret)
758 dont_add_job = 1;
759 break;
760 }
761 default:
762 break;
763 }
764 }
765
766 if (td) {
767 if (dont_add_job)
768 put_job(td);
769 else {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100770 ret = add_job(td, td->o.name ?: "fio", 0);
Jens Axboe214e1ec2007-03-15 10:48:13 +0100771 if (ret)
772 put_job(td);
773 }
774 }
775
776 while (optind < argc) {
777 ini_idx++;
778 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
779 ini_file[ini_idx - 1] = strdup(argv[optind]);
780 optind++;
781 }
782
783 return ini_idx;
784}
785
786
Jens Axboeebac4652005-12-08 15:25:21 +0100787int parse_options(int argc, char *argv[])
788{
Jens Axboe972cfd22006-06-09 11:08:56 +0200789 int job_files, i;
790
Jens Axboeb4692822006-10-27 13:43:22 +0200791 f_out = stdout;
792 f_err = stderr;
793
Jens Axboe214e1ec2007-03-15 10:48:13 +0100794 fio_options_dup_and_init(long_options);
Jens Axboeb4692822006-10-27 13:43:22 +0200795
Jens Axboeebac4652005-12-08 15:25:21 +0100796 if (setup_thread_area())
797 return 1;
798 if (fill_def_thread())
799 return 1;
800
Jens Axboe972cfd22006-06-09 11:08:56 +0200801 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +0100802
Jens Axboe972cfd22006-06-09 11:08:56 +0200803 for (i = 0; i < job_files; i++) {
804 if (fill_def_thread())
805 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200806 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +0200807 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +0200808 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +0200809 }
Jens Axboeebac4652005-12-08 15:25:21 +0100810
Jens Axboe88c6ed82006-06-09 11:28:10 +0200811 free(ini_file);
Jens Axboed23bb322007-03-23 15:57:56 +0100812 options_mem_free(&def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +0200813
814 if (!thread_number) {
815 log_err("No jobs defined(s)\n");
Jens Axboeb4692822006-10-27 13:43:22 +0200816 return 1;
817 }
818
Jens Axboeebac4652005-12-08 15:25:21 +0100819 return 0;
820}