blob: 15e96aacbebdea4e21ab874a6df0511aed1c7b19 [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 Axboe214e1ec2007-03-15 10:48:13 +010020static char fio_version_string[] = "fio 1.14a";
21
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 Axboeebac4652005-12-08 15:25:21 +0100118 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100119 return td;
120}
121
122static void put_job(struct thread_data *td)
123{
Jens Axboe549577a2006-10-30 12:23:41 +0100124 if (td == &def_thread)
125 return;
126
Jens Axboe16edf252007-02-10 14:59:22 +0100127 if (td->error)
Jens Axboe6d861442007-03-15 09:22:23 +0100128 log_info("fio: %s\n", td->verror);
Jens Axboe16edf252007-02-10 14:59:22 +0100129
Jens Axboeebac4652005-12-08 15:25:21 +0100130 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
131 thread_number--;
132}
133
Jens Axboedad915e2006-10-27 11:10:18 +0200134/*
135 * Lazy way of fixing up options that depend on each other. We could also
136 * define option callback handlers, but this is easier.
137 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100138static int fixup_options(struct thread_data *td)
Jens Axboee1f36502006-10-27 10:54:08 +0200139{
Jens Axboee1f36502006-10-27 10:54:08 +0200140 if (!td->rwmixread && td->rwmixwrite)
141 td->rwmixread = 100 - td->rwmixwrite;
Jens Axboedad915e2006-10-27 11:10:18 +0200142
Jens Axboe076efc72006-10-27 11:24:25 +0200143 if (td->write_iolog_file && td->read_iolog_file) {
144 log_err("fio: read iolog overrides write_iolog\n");
145 free(td->write_iolog_file);
146 td->write_iolog_file = NULL;
147 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100148
149 if (td->io_ops->flags & FIO_SYNCIO)
150 td->iodepth = 1;
151 else {
152 if (!td->iodepth)
Jens Axboeb5af8292007-03-08 12:43:13 +0100153 td->iodepth = td->open_files;
Jens Axboe16b462a2006-10-30 12:35:18 +0100154 }
155
156 /*
157 * only really works for sequential io for now, and with 1 file
158 */
Jens Axboeb5af8292007-03-08 12:43:13 +0100159 if (td->zone_size && td_random(td) && td->open_files == 1)
Jens Axboe16b462a2006-10-30 12:35:18 +0100160 td->zone_size = 0;
161
162 /*
163 * Reads can do overwrites, we always need to pre-create the file
164 */
165 if (td_read(td) || td_rw(td))
166 td->overwrite = 1;
167
Jens Axboea00735e2006-11-03 08:58:08 +0100168 if (!td->min_bs[DDIR_READ])
169 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
170 if (!td->max_bs[DDIR_READ])
171 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
172 if (!td->min_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100173 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100174 if (!td->max_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100175 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100176
177 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
178
Jens Axboe9c60ce62007-03-15 09:14:47 +0100179 if (!td->file_size_high)
180 td->file_size_high = td->file_size_low;
181
Jens Axboe16b462a2006-10-30 12:35:18 +0100182 if (td_read(td) && !td_rw(td))
183 td->verify = 0;
Jens Axboebb8895e2006-10-30 15:14:48 +0100184
185 if (td->norandommap && td->verify != VERIFY_NONE) {
186 log_err("fio: norandommap given, verify disabled\n");
187 td->verify = VERIFY_NONE;
188 }
Jens Axboe690adba2006-10-30 15:25:09 +0100189 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
190 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100191
192 /*
Jens Axboe48097d52007-02-17 06:30:44 +0100193 * thinktime_spin must be less than thinktime
194 */
195 if (td->thinktime_spin > td->thinktime)
196 td->thinktime_spin = td->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100197
198 /*
199 * The low water mark cannot be bigger than the iodepth
200 */
Jens Axboe9467b772007-02-27 19:56:43 +0100201 if (td->iodepth_low > td->iodepth || !td->iodepth_low) {
202 /*
203 * syslet work around - if the workload is sequential,
204 * we want to let the queue drain all the way down to
205 * avoid seeking between async threads
206 */
207 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
208 td->iodepth_low = 1;
209 else
210 td->iodepth_low = td->iodepth;
211 }
Jens Axboecb5ab512007-02-26 12:57:09 +0100212
213 /*
214 * If batch number isn't set, default to the same as iodepth
215 */
216 if (td->iodepth_batch > td->iodepth || !td->iodepth_batch)
217 td->iodepth_batch = td->iodepth;
Jens Axboeb5af8292007-03-08 12:43:13 +0100218
Jens Axboebbf6b542007-03-13 15:28:55 +0100219 if (td->nr_files > td->files_index)
Jens Axboe9f9214f2007-03-13 14:02:16 +0100220 td->nr_files = td->files_index;
221
222 if (td->open_files > td->nr_files || !td->open_files)
Jens Axboeb5af8292007-03-08 12:43:13 +0100223 td->open_files = td->nr_files;
Jens Axboe4e991c22007-03-15 11:41:11 +0100224
225 if ((td->rate && td->rate_iops) || (td->ratemin && td->rate_iops_min)) {
226 log_err("fio: rate and rate_iops are mutually exclusive\n");
227 return 1;
228 }
229 if ((td->rate < td->ratemin) || (td->rate_iops < td->rate_iops_min)) {
230 log_err("fio: minimum rate exceeds rate\n");
231 return 1;
232 }
233
234 return 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200235}
236
Jens Axboe906c8d72006-06-13 09:37:56 +0200237/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100238 * This function leaks the buffer
239 */
240static char *to_kmg(unsigned int val)
241{
242 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100243 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100244 char *p = post;
245
Jens Axboe245142f2006-11-08 12:49:21 +0100246 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100247 if (val & 1023)
248 break;
249
250 val >>= 10;
251 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100252 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100253
254 snprintf(buf, 31, "%u%c", val, *p);
255 return buf;
256}
257
Jens Axboe09629a92007-03-09 09:00:06 +0100258/* External engines are specified by "external:name.o") */
259static const char *get_engine_name(const char *str)
260{
261 char *p = strstr(str, ":");
262
263 if (!p)
264 return str;
265
266 p++;
267 strip_blank_front(&p);
268 strip_blank_end(p);
269 return p;
270}
271
Jens Axboee132cba2007-03-14 14:23:54 +0100272static int exists_and_not_file(const char *filename)
273{
274 struct stat sb;
275
276 if (lstat(filename, &sb) == -1)
277 return 0;
278
279 if (S_ISREG(sb.st_mode))
280 return 0;
281
282 return 1;
283}
284
Jens Axboef8977ee2006-11-06 14:03:03 +0100285/*
Jens Axboe9c60ce62007-03-15 09:14:47 +0100286 * Initialize the various random states we need (random io, block size ranges,
287 * read/write mix, etc).
288 */
289static int init_random_state(struct thread_data *td)
290{
291 unsigned long seeds[6];
292 int fd, num_maps, blocks;
293 struct fio_file *f;
294 unsigned int i;
295
296 fd = open("/dev/urandom", O_RDONLY);
297 if (fd == -1) {
298 td_verror(td, errno, "open");
299 return 1;
300 }
301
302 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
303 td_verror(td, EIO, "read");
304 close(fd);
305 return 1;
306 }
307
308 close(fd);
309
310 os_random_seed(seeds[0], &td->bsrange_state);
311 os_random_seed(seeds[1], &td->verify_state);
312 os_random_seed(seeds[2], &td->rwmix_state);
313
314 if (td->file_service_type == FIO_FSERVICE_RANDOM)
315 os_random_seed(seeds[3], &td->next_file_state);
316
317 os_random_seed(seeds[5], &td->file_size_state);
318
319 if (!td_random(td))
320 return 0;
321
322 if (td->rand_repeatable)
323 seeds[4] = FIO_RANDSEED * td->thread_number;
324
325 if (!td->norandommap) {
326 for_each_file(td, f, i) {
327 blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs;
328 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
329 f->file_map = malloc(num_maps * sizeof(long));
330 if (!f->file_map) {
331 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
332 return 1;
333 }
334 f->num_maps = num_maps;
335 memset(f->file_map, 0, num_maps * sizeof(long));
336 }
337 }
338
339 os_random_seed(seeds[4], &td->random_state);
340 return 0;
341}
342
343
344/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200345 * Adds a job to the list of things todo. Sanitizes the various options
346 * to make sure we don't have conflicts, and initializes various
347 * members of td.
348 */
Jens Axboe75154842006-06-01 13:56:09 +0200349static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100350{
Jens Axboe413dd452007-02-23 09:26:09 +0100351 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
352 "randread", "randwrite", "randrw" };
Jens Axboeaf52b342007-03-13 10:07:47 +0100353 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200354 struct fio_file *f;
Jens Axboe09629a92007-03-09 09:00:06 +0100355 const char *engine;
Jens Axboeaf52b342007-03-13 10:07:47 +0100356 char fname[PATH_MAX];
Jens Axboee132cba2007-03-14 14:23:54 +0100357 int numjobs, file_alloced;
Jens Axboeebac4652005-12-08 15:25:21 +0100358
Jens Axboeebac4652005-12-08 15:25:21 +0100359 /*
360 * the def_thread is just for options, it's not a real job
361 */
362 if (td == &def_thread)
363 return 0;
364
Jens Axboe09629a92007-03-09 09:00:06 +0100365 engine = get_engine_name(td->ioengine);
366 td->io_ops = load_ioengine(td, engine);
367 if (!td->io_ops) {
368 log_err("fio: failed to load engine %s\n", engine);
Jens Axboe205927a2007-03-15 11:06:32 +0100369 goto err;
Jens Axboe09629a92007-03-09 09:00:06 +0100370 }
Jens Axboedf641192006-10-12 07:55:41 +0200371
Jens Axboe9cedf162007-03-12 11:29:30 +0100372 if (td->use_thread)
373 nr_thread++;
374 else
375 nr_process++;
376
Jens Axboe690adba2006-10-30 15:25:09 +0100377 if (td->odirect)
378 td->io_ops->flags |= FIO_RAWIO;
379
Jens Axboee132cba2007-03-14 14:23:54 +0100380 file_alloced = 0;
Jens Axboebbf6b542007-03-13 15:28:55 +0100381 if (!td->filename && !td->files_index) {
Jens Axboee132cba2007-03-14 14:23:54 +0100382 file_alloced = 1;
Jens Axboe80be24f2007-03-12 11:01:25 +0100383
Jens Axboee132cba2007-03-14 14:23:54 +0100384 if (td->nr_files == 1 && exists_and_not_file(jobname))
385 add_file(td, jobname);
Jens Axboe7b05a212007-03-13 11:17:07 +0100386 else {
387 for (i = 0; i < td->nr_files; i++) {
Jens Axboee132cba2007-03-14 14:23:54 +0100388 sprintf(fname, "%s.%d.%d", jobname, td->thread_number, i);
Jens Axboe7b05a212007-03-13 11:17:07 +0100389 add_file(td, fname);
390 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100391 }
Jens Axboe0af7b542006-02-17 10:10:12 +0100392 }
Jens Axboeebac4652005-12-08 15:25:21 +0100393
Jens Axboe4e991c22007-03-15 11:41:11 +0100394 if (fixup_options(td))
395 goto err;
Jens Axboee0a22332006-12-20 12:54:25 +0100396
Jens Axboe53cdc682006-10-18 11:50:58 +0200397 for_each_file(td, f, i) {
Jens Axboeaf52b342007-03-13 10:07:47 +0100398 if (td->directory && f->filetype == FIO_TYPE_FILE) {
399 sprintf(fname, "%s/%s", td->directory, f->file_name);
400 f->file_name = strdup(fname);
401 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200402 }
403
Jens Axboe07739b52007-03-08 20:25:46 +0100404 td->mutex = fio_sem_init(0);
Jens Axboeebac4652005-12-08 15:25:21 +0100405
Jens Axboe756867b2007-03-06 15:19:24 +0100406 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
407 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
408 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
Jens Axboeebac4652005-12-08 15:25:21 +0100409
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100410 if ((td->stonewall || td->numjobs > 1) && prev_group_jobs) {
411 prev_group_jobs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100412 groupid++;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100413 }
Jens Axboeebac4652005-12-08 15:25:21 +0100414
415 td->groupid = groupid;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100416 prev_group_jobs++;
Jens Axboeebac4652005-12-08 15:25:21 +0100417
Jens Axboe9c60ce62007-03-15 09:14:47 +0100418 if (init_random_state(td))
419 goto err;
420
Jens Axboeebac4652005-12-08 15:25:21 +0100421 if (setup_rate(td))
422 goto err;
423
Jens Axboeec94ec52006-10-20 10:59:19 +0200424 if (td->write_lat_log) {
Jens Axboe756867b2007-03-06 15:19:24 +0100425 setup_log(&td->ts.slat_log);
426 setup_log(&td->ts.clat_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100427 }
Jens Axboeec94ec52006-10-20 10:59:19 +0200428 if (td->write_bw_log)
Jens Axboe756867b2007-03-06 15:19:24 +0100429 setup_log(&td->ts.bw_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100430
Jens Axboeb4692822006-10-27 13:43:22 +0200431 if (!td->name)
432 td->name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200433
Jens Axboec6ae0a52006-06-12 11:04:44 +0200434 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200435 if (!job_add_num) {
Jens Axboeba0fbe12007-03-09 14:34:23 +0100436 if (!strcmp(td->io_ops->name, "cpuio"))
Jens Axboe6d861442007-03-15 09:22:23 +0100437 log_info("%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
Jens Axboef8977ee2006-11-06 14:03:03 +0100438 else {
439 char *c1, *c2, *c3, *c4;
440
441 c1 = to_kmg(td->min_bs[DDIR_READ]);
442 c2 = to_kmg(td->max_bs[DDIR_READ]);
443 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
444 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
445
Jens Axboe6d861442007-03-15 09:22:23 +0100446 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s, ioengine=%s, iodepth=%u\n", td->name, td->groupid, ddir_str[td->td_ddir], c1, c2, c3, c4, td->io_ops->name, td->iodepth);
Jens Axboef8977ee2006-11-06 14:03:03 +0100447
448 free(c1);
449 free(c2);
450 free(c3);
451 free(c4);
452 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200453 } else if (job_add_num == 1)
Jens Axboe6d861442007-03-15 09:22:23 +0100454 log_info("...\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +0200455 }
Jens Axboeebac4652005-12-08 15:25:21 +0100456
457 /*
458 * recurse add identical jobs, clear numjobs and stonewall options
459 * as they don't apply to sub-jobs
460 */
461 numjobs = td->numjobs;
462 while (--numjobs) {
463 struct thread_data *td_new = get_new_job(0, td);
464
465 if (!td_new)
466 goto err;
467
468 td_new->numjobs = 1;
469 td_new->stonewall = 0;
Jens Axboee132cba2007-03-14 14:23:54 +0100470
471 if (file_alloced) {
472 td_new->filename = NULL;
473 td_new->files_index = 0;
474 td_new->files = NULL;
475 }
476
Jens Axboe75154842006-06-01 13:56:09 +0200477 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100478
Jens Axboe75154842006-06-01 13:56:09 +0200479 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100480 goto err;
481 }
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100482
483 if (td->numjobs > 1) {
484 groupid++;
485 prev_group_jobs = 0;
486 }
487
Jens Axboeebac4652005-12-08 15:25:21 +0100488 return 0;
489err:
490 put_job(td);
491 return -1;
492}
493
Jens Axboeebac4652005-12-08 15:25:21 +0100494static int is_empty_or_comment(char *line)
495{
496 unsigned int i;
497
498 for (i = 0; i < strlen(line); i++) {
499 if (line[i] == ';')
500 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +0100501 if (line[i] == '#')
502 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100503 if (!isspace(line[i]) && !iscntrl(line[i]))
504 return 0;
505 }
506
507 return 1;
508}
509
Jens Axboe313cb202006-12-21 09:50:00 +0100510/*
Jens Axboe07261982006-06-07 10:51:12 +0200511 * This is our [ini] type file parser.
512 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100513static int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100514{
Jens Axboee1f36502006-10-27 10:54:08 +0200515 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +0100516 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +0100517 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +0100518 fpos_t off;
519 FILE *f;
520 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200521 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +0100522
523 f = fopen(file, "r");
524 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200525 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100526 return 1;
527 }
528
529 string = malloc(4096);
530 name = malloc(256);
Jens Axboefee3bb42006-10-30 13:32:08 +0100531 memset(name, 0, 256);
Jens Axboeebac4652005-12-08 15:25:21 +0100532
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200533 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100534 do {
535 p = fgets(string, 4095, f);
536 if (!p)
Jens Axboe45410ac2006-06-07 11:10:39 +0200537 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100538 if (is_empty_or_comment(p))
539 continue;
Jens Axboefee3bb42006-10-30 13:32:08 +0100540 if (sscanf(p, "[%255s]", name) != 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100541 continue;
542
543 global = !strncmp(name, "global", 6);
544
545 name[strlen(name) - 1] = '\0';
546
547 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200548 if (!td) {
549 ret = 1;
550 break;
551 }
Jens Axboeebac4652005-12-08 15:25:21 +0100552
Jens Axboe972cfd22006-06-09 11:08:56 +0200553 /*
554 * Seperate multiple job files by a stonewall
555 */
Jens Axboef9481912006-06-09 11:37:28 +0200556 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +0200557 td->stonewall = stonewall;
558 stonewall = 0;
559 }
560
Jens Axboeebac4652005-12-08 15:25:21 +0100561 fgetpos(f, &off);
562 while ((p = fgets(string, 4096, f)) != NULL) {
563 if (is_empty_or_comment(p))
564 continue;
Jens Axboee1f36502006-10-27 10:54:08 +0200565
Jens Axboeb6754f92006-05-30 14:29:32 +0200566 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +0100567
568 if (p[0] == '[')
569 break;
570
Jens Axboe4ae3f762006-05-30 13:35:14 +0200571 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200572
Jens Axboee1f36502006-10-27 10:54:08 +0200573 fgetpos(f, &off);
Jens Axboeebac4652005-12-08 15:25:21 +0100574
Jens Axboe45410ac2006-06-07 11:10:39 +0200575 /*
576 * Don't break here, continue parsing options so we
577 * dump all the bad ones. Makes trial/error fixups
578 * easier on the user.
579 */
Jens Axboe214e1ec2007-03-15 10:48:13 +0100580 ret |= fio_option_parse(td, p);
Jens Axboeebac4652005-12-08 15:25:21 +0100581 }
Jens Axboeebac4652005-12-08 15:25:21 +0100582
Jens Axboe45410ac2006-06-07 11:10:39 +0200583 if (!ret) {
584 fsetpos(f, &off);
585 ret = add_job(td, name, 0);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100586 } else {
587 log_err("fio: job %s dropped\n", name);
588 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +0200589 }
Jens Axboe7c124ac2006-10-30 13:36:52 +0100590 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +0100591
592 free(string);
593 free(name);
594 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200595 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100596}
597
598static int fill_def_thread(void)
599{
600 memset(&def_thread, 0, sizeof(def_thread));
601
602 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
603 perror("sched_getaffinity");
604 return 1;
605 }
606
607 /*
Jens Axboeee738492007-01-10 11:23:16 +0100608 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +0100609 */
Jens Axboe214e1ec2007-03-15 10:48:13 +0100610 fio_fill_default_options(&def_thread);
Jens Axboeee738492007-01-10 11:23:16 +0100611
Jens Axboe972cfd22006-06-09 11:08:56 +0200612 def_thread.timeout = def_timeout;
Jens Axboeec94ec52006-10-20 10:59:19 +0200613 def_thread.write_bw_log = write_bw_log;
614 def_thread.write_lat_log = write_lat_log;
Jens Axboeee738492007-01-10 11:23:16 +0100615
Jens Axboeebac4652005-12-08 15:25:21 +0100616#ifdef FIO_HAVE_DISK_UTIL
617 def_thread.do_disk_util = 1;
618#endif
619
620 return 0;
621}
622
Jens Axboeebac4652005-12-08 15:25:21 +0100623static void free_shm(void)
624{
625 struct shmid_ds sbuf;
626
627 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +0200628 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +0100629 threads = NULL;
630 shmctl(shm_id, IPC_RMID, &sbuf);
631 }
632}
633
Jens Axboe906c8d72006-06-13 09:37:56 +0200634/*
635 * The thread area is shared between the main process and the job
636 * threads/processes. So setup a shared memory segment that will hold
637 * all the job info.
638 */
Jens Axboeebac4652005-12-08 15:25:21 +0100639static int setup_thread_area(void)
640{
641 /*
642 * 1024 is too much on some machines, scale max_jobs if
643 * we get a failure that looks like too large a shm segment
644 */
645 do {
Jens Axboe906c8d72006-06-13 09:37:56 +0200646 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +0100647
Jens Axboe906c8d72006-06-13 09:37:56 +0200648 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +0100649 if (shm_id != -1)
650 break;
651 if (errno != EINVAL) {
652 perror("shmget");
653 break;
654 }
655
656 max_jobs >>= 1;
657 } while (max_jobs);
658
659 if (shm_id == -1)
660 return 1;
661
662 threads = shmat(shm_id, NULL, 0);
663 if (threads == (void *) -1) {
664 perror("shmat");
665 return 1;
666 }
667
668 atexit(free_shm);
669 return 0;
670}
671
Jens Axboe214e1ec2007-03-15 10:48:13 +0100672static void usage(void)
Jens Axboeb4692822006-10-27 13:43:22 +0200673{
Jens Axboe214e1ec2007-03-15 10:48:13 +0100674 printf("%s\n", fio_version_string);
675 printf("\t--output\tWrite output to file\n");
676 printf("\t--timeout\tRuntime in seconds\n");
677 printf("\t--latency-log\tGenerate per-job latency logs\n");
678 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
679 printf("\t--minimal\tMinimal (terse) output\n");
680 printf("\t--version\tPrint version info and exit\n");
681 printf("\t--help\t\tPrint this page\n");
682 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
Jens Axboeb4692822006-10-27 13:43:22 +0200683}
684
Jens Axboe214e1ec2007-03-15 10:48:13 +0100685static int parse_cmd_line(int argc, char *argv[])
686{
687 struct thread_data *td = NULL;
688 int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
689
690 while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
691 switch (c) {
692 case 't':
693 def_timeout = atoi(optarg);
694 break;
695 case 'l':
696 write_lat_log = 1;
697 break;
698 case 'w':
699 write_bw_log = 1;
700 break;
701 case 'o':
702 f_out = fopen(optarg, "w+");
703 if (!f_out) {
704 perror("fopen output");
705 exit(1);
706 }
707 f_err = f_out;
708 break;
709 case 'm':
710 terse_output = 1;
711 break;
712 case 'h':
713 usage();
714 exit(0);
715 case 'c':
716 exit(fio_show_option_help(optarg));
717 case 'v':
718 printf("%s\n", fio_version_string);
719 exit(0);
720 case FIO_GETOPT_JOB: {
721 const char *opt = long_options[lidx].name;
722 char *val = optarg;
723
724 if (!strncmp(opt, "name", 4) && td) {
725 ret = add_job(td, td->name ?: "fio", 0);
726 if (ret) {
727 put_job(td);
728 return 0;
729 }
730 td = NULL;
731 }
732 if (!td) {
733 int global = !strncmp(val, "global", 6);
734
735 td = get_new_job(global, &def_thread);
736 if (!td)
737 return 0;
738 }
739
740 ret = fio_cmd_option_parse(td, opt, val);
741 if (ret)
742 dont_add_job = 1;
743 break;
744 }
745 default:
746 break;
747 }
748 }
749
750 if (td) {
751 if (dont_add_job)
752 put_job(td);
753 else {
754 ret = add_job(td, td->name ?: "fio", 0);
755 if (ret)
756 put_job(td);
757 }
758 }
759
760 while (optind < argc) {
761 ini_idx++;
762 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
763 ini_file[ini_idx - 1] = strdup(argv[optind]);
764 optind++;
765 }
766
767 return ini_idx;
768}
769
770
Jens Axboeebac4652005-12-08 15:25:21 +0100771int parse_options(int argc, char *argv[])
772{
Jens Axboe972cfd22006-06-09 11:08:56 +0200773 int job_files, i;
774
Jens Axboeb4692822006-10-27 13:43:22 +0200775 f_out = stdout;
776 f_err = stderr;
777
Jens Axboe214e1ec2007-03-15 10:48:13 +0100778 fio_options_dup_and_init(long_options);
Jens Axboeb4692822006-10-27 13:43:22 +0200779
Jens Axboeebac4652005-12-08 15:25:21 +0100780 if (setup_thread_area())
781 return 1;
782 if (fill_def_thread())
783 return 1;
784
Jens Axboe972cfd22006-06-09 11:08:56 +0200785 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +0100786
Jens Axboe972cfd22006-06-09 11:08:56 +0200787 for (i = 0; i < job_files; i++) {
788 if (fill_def_thread())
789 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200790 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +0200791 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +0200792 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +0200793 }
Jens Axboeebac4652005-12-08 15:25:21 +0100794
Jens Axboe88c6ed82006-06-09 11:28:10 +0200795 free(ini_file);
Jens Axboeb4692822006-10-27 13:43:22 +0200796
797 if (!thread_number) {
798 log_err("No jobs defined(s)\n");
Jens Axboeb4692822006-10-27 13:43:22 +0200799 return 1;
800 }
801
Jens Axboeebac4652005-12-08 15:25:21 +0100802 return 0;
803}