blob: b70ccaf8b7a68d13bffc0967490963ac775a7bf2 [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 Axboee1f36502006-10-27 10:54:08 +0200138static void fixup_options(struct thread_data *td)
139{
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 Axboee1f36502006-10-27 10:54:08 +0200224}
225
Jens Axboe906c8d72006-06-13 09:37:56 +0200226/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100227 * This function leaks the buffer
228 */
229static char *to_kmg(unsigned int val)
230{
231 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100232 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100233 char *p = post;
234
Jens Axboe245142f2006-11-08 12:49:21 +0100235 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100236 if (val & 1023)
237 break;
238
239 val >>= 10;
240 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100241 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100242
243 snprintf(buf, 31, "%u%c", val, *p);
244 return buf;
245}
246
Jens Axboe09629a92007-03-09 09:00:06 +0100247/* External engines are specified by "external:name.o") */
248static const char *get_engine_name(const char *str)
249{
250 char *p = strstr(str, ":");
251
252 if (!p)
253 return str;
254
255 p++;
256 strip_blank_front(&p);
257 strip_blank_end(p);
258 return p;
259}
260
Jens Axboee132cba2007-03-14 14:23:54 +0100261static int exists_and_not_file(const char *filename)
262{
263 struct stat sb;
264
265 if (lstat(filename, &sb) == -1)
266 return 0;
267
268 if (S_ISREG(sb.st_mode))
269 return 0;
270
271 return 1;
272}
273
Jens Axboef8977ee2006-11-06 14:03:03 +0100274/*
Jens Axboe9c60ce62007-03-15 09:14:47 +0100275 * Initialize the various random states we need (random io, block size ranges,
276 * read/write mix, etc).
277 */
278static int init_random_state(struct thread_data *td)
279{
280 unsigned long seeds[6];
281 int fd, num_maps, blocks;
282 struct fio_file *f;
283 unsigned int i;
284
285 fd = open("/dev/urandom", O_RDONLY);
286 if (fd == -1) {
287 td_verror(td, errno, "open");
288 return 1;
289 }
290
291 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
292 td_verror(td, EIO, "read");
293 close(fd);
294 return 1;
295 }
296
297 close(fd);
298
299 os_random_seed(seeds[0], &td->bsrange_state);
300 os_random_seed(seeds[1], &td->verify_state);
301 os_random_seed(seeds[2], &td->rwmix_state);
302
303 if (td->file_service_type == FIO_FSERVICE_RANDOM)
304 os_random_seed(seeds[3], &td->next_file_state);
305
306 os_random_seed(seeds[5], &td->file_size_state);
307
308 if (!td_random(td))
309 return 0;
310
311 if (td->rand_repeatable)
312 seeds[4] = FIO_RANDSEED * td->thread_number;
313
314 if (!td->norandommap) {
315 for_each_file(td, f, i) {
316 blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs;
317 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
318 f->file_map = malloc(num_maps * sizeof(long));
319 if (!f->file_map) {
320 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
321 return 1;
322 }
323 f->num_maps = num_maps;
324 memset(f->file_map, 0, num_maps * sizeof(long));
325 }
326 }
327
328 os_random_seed(seeds[4], &td->random_state);
329 return 0;
330}
331
332
333/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200334 * Adds a job to the list of things todo. Sanitizes the various options
335 * to make sure we don't have conflicts, and initializes various
336 * members of td.
337 */
Jens Axboe75154842006-06-01 13:56:09 +0200338static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100339{
Jens Axboe413dd452007-02-23 09:26:09 +0100340 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
341 "randread", "randwrite", "randrw" };
Jens Axboeaf52b342007-03-13 10:07:47 +0100342 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200343 struct fio_file *f;
Jens Axboe09629a92007-03-09 09:00:06 +0100344 const char *engine;
Jens Axboeaf52b342007-03-13 10:07:47 +0100345 char fname[PATH_MAX];
Jens Axboee132cba2007-03-14 14:23:54 +0100346 int numjobs, file_alloced;
Jens Axboeebac4652005-12-08 15:25:21 +0100347
Jens Axboeebac4652005-12-08 15:25:21 +0100348 /*
349 * the def_thread is just for options, it's not a real job
350 */
351 if (td == &def_thread)
352 return 0;
353
Jens Axboe09629a92007-03-09 09:00:06 +0100354 engine = get_engine_name(td->ioengine);
355 td->io_ops = load_ioengine(td, engine);
356 if (!td->io_ops) {
357 log_err("fio: failed to load engine %s\n", engine);
358 return 1;
359 }
Jens Axboedf641192006-10-12 07:55:41 +0200360
Jens Axboe9cedf162007-03-12 11:29:30 +0100361 if (td->use_thread)
362 nr_thread++;
363 else
364 nr_process++;
365
Jens Axboe690adba2006-10-30 15:25:09 +0100366 if (td->odirect)
367 td->io_ops->flags |= FIO_RAWIO;
368
Jens Axboee132cba2007-03-14 14:23:54 +0100369 file_alloced = 0;
Jens Axboebbf6b542007-03-13 15:28:55 +0100370 if (!td->filename && !td->files_index) {
Jens Axboee132cba2007-03-14 14:23:54 +0100371 file_alloced = 1;
Jens Axboe80be24f2007-03-12 11:01:25 +0100372
Jens Axboee132cba2007-03-14 14:23:54 +0100373 if (td->nr_files == 1 && exists_and_not_file(jobname))
374 add_file(td, jobname);
Jens Axboe7b05a212007-03-13 11:17:07 +0100375 else {
376 for (i = 0; i < td->nr_files; i++) {
Jens Axboee132cba2007-03-14 14:23:54 +0100377 sprintf(fname, "%s.%d.%d", jobname, td->thread_number, i);
Jens Axboe7b05a212007-03-13 11:17:07 +0100378 add_file(td, fname);
379 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100380 }
Jens Axboe0af7b542006-02-17 10:10:12 +0100381 }
Jens Axboeebac4652005-12-08 15:25:21 +0100382
Jens Axboee0a22332006-12-20 12:54:25 +0100383 fixup_options(td);
384
Jens Axboe53cdc682006-10-18 11:50:58 +0200385 for_each_file(td, f, i) {
Jens Axboeaf52b342007-03-13 10:07:47 +0100386 if (td->directory && f->filetype == FIO_TYPE_FILE) {
387 sprintf(fname, "%s/%s", td->directory, f->file_name);
388 f->file_name = strdup(fname);
389 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200390 }
391
Jens Axboe07739b52007-03-08 20:25:46 +0100392 td->mutex = fio_sem_init(0);
Jens Axboeebac4652005-12-08 15:25:21 +0100393
Jens Axboe756867b2007-03-06 15:19:24 +0100394 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
395 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
396 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
Jens Axboeebac4652005-12-08 15:25:21 +0100397
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100398 if ((td->stonewall || td->numjobs > 1) && prev_group_jobs) {
399 prev_group_jobs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100400 groupid++;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100401 }
Jens Axboeebac4652005-12-08 15:25:21 +0100402
403 td->groupid = groupid;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100404 prev_group_jobs++;
Jens Axboeebac4652005-12-08 15:25:21 +0100405
Jens Axboe9c60ce62007-03-15 09:14:47 +0100406 if (init_random_state(td))
407 goto err;
408
Jens Axboeebac4652005-12-08 15:25:21 +0100409 if (setup_rate(td))
410 goto err;
411
Jens Axboeec94ec52006-10-20 10:59:19 +0200412 if (td->write_lat_log) {
Jens Axboe756867b2007-03-06 15:19:24 +0100413 setup_log(&td->ts.slat_log);
414 setup_log(&td->ts.clat_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100415 }
Jens Axboeec94ec52006-10-20 10:59:19 +0200416 if (td->write_bw_log)
Jens Axboe756867b2007-03-06 15:19:24 +0100417 setup_log(&td->ts.bw_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100418
Jens Axboeb4692822006-10-27 13:43:22 +0200419 if (!td->name)
420 td->name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200421
Jens Axboec6ae0a52006-06-12 11:04:44 +0200422 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200423 if (!job_add_num) {
Jens Axboeba0fbe12007-03-09 14:34:23 +0100424 if (!strcmp(td->io_ops->name, "cpuio"))
Jens Axboe6d861442007-03-15 09:22:23 +0100425 log_info("%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
Jens Axboef8977ee2006-11-06 14:03:03 +0100426 else {
427 char *c1, *c2, *c3, *c4;
428
429 c1 = to_kmg(td->min_bs[DDIR_READ]);
430 c2 = to_kmg(td->max_bs[DDIR_READ]);
431 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
432 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
433
Jens Axboe6d861442007-03-15 09:22:23 +0100434 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 +0100435
436 free(c1);
437 free(c2);
438 free(c3);
439 free(c4);
440 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200441 } else if (job_add_num == 1)
Jens Axboe6d861442007-03-15 09:22:23 +0100442 log_info("...\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +0200443 }
Jens Axboeebac4652005-12-08 15:25:21 +0100444
445 /*
446 * recurse add identical jobs, clear numjobs and stonewall options
447 * as they don't apply to sub-jobs
448 */
449 numjobs = td->numjobs;
450 while (--numjobs) {
451 struct thread_data *td_new = get_new_job(0, td);
452
453 if (!td_new)
454 goto err;
455
456 td_new->numjobs = 1;
457 td_new->stonewall = 0;
Jens Axboee132cba2007-03-14 14:23:54 +0100458
459 if (file_alloced) {
460 td_new->filename = NULL;
461 td_new->files_index = 0;
462 td_new->files = NULL;
463 }
464
Jens Axboe75154842006-06-01 13:56:09 +0200465 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100466
Jens Axboe75154842006-06-01 13:56:09 +0200467 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100468 goto err;
469 }
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100470
471 if (td->numjobs > 1) {
472 groupid++;
473 prev_group_jobs = 0;
474 }
475
Jens Axboeebac4652005-12-08 15:25:21 +0100476 return 0;
477err:
478 put_job(td);
479 return -1;
480}
481
Jens Axboeebac4652005-12-08 15:25:21 +0100482static int is_empty_or_comment(char *line)
483{
484 unsigned int i;
485
486 for (i = 0; i < strlen(line); i++) {
487 if (line[i] == ';')
488 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +0100489 if (line[i] == '#')
490 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100491 if (!isspace(line[i]) && !iscntrl(line[i]))
492 return 0;
493 }
494
495 return 1;
496}
497
Jens Axboe313cb202006-12-21 09:50:00 +0100498/*
Jens Axboe07261982006-06-07 10:51:12 +0200499 * This is our [ini] type file parser.
500 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100501static int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100502{
Jens Axboee1f36502006-10-27 10:54:08 +0200503 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +0100504 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +0100505 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +0100506 fpos_t off;
507 FILE *f;
508 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200509 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +0100510
511 f = fopen(file, "r");
512 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200513 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100514 return 1;
515 }
516
517 string = malloc(4096);
518 name = malloc(256);
Jens Axboefee3bb42006-10-30 13:32:08 +0100519 memset(name, 0, 256);
Jens Axboeebac4652005-12-08 15:25:21 +0100520
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200521 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100522 do {
523 p = fgets(string, 4095, f);
524 if (!p)
Jens Axboe45410ac2006-06-07 11:10:39 +0200525 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100526 if (is_empty_or_comment(p))
527 continue;
Jens Axboefee3bb42006-10-30 13:32:08 +0100528 if (sscanf(p, "[%255s]", name) != 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100529 continue;
530
531 global = !strncmp(name, "global", 6);
532
533 name[strlen(name) - 1] = '\0';
534
535 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200536 if (!td) {
537 ret = 1;
538 break;
539 }
Jens Axboeebac4652005-12-08 15:25:21 +0100540
Jens Axboe972cfd22006-06-09 11:08:56 +0200541 /*
542 * Seperate multiple job files by a stonewall
543 */
Jens Axboef9481912006-06-09 11:37:28 +0200544 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +0200545 td->stonewall = stonewall;
546 stonewall = 0;
547 }
548
Jens Axboeebac4652005-12-08 15:25:21 +0100549 fgetpos(f, &off);
550 while ((p = fgets(string, 4096, f)) != NULL) {
551 if (is_empty_or_comment(p))
552 continue;
Jens Axboee1f36502006-10-27 10:54:08 +0200553
Jens Axboeb6754f92006-05-30 14:29:32 +0200554 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +0100555
556 if (p[0] == '[')
557 break;
558
Jens Axboe4ae3f762006-05-30 13:35:14 +0200559 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200560
Jens Axboee1f36502006-10-27 10:54:08 +0200561 fgetpos(f, &off);
Jens Axboeebac4652005-12-08 15:25:21 +0100562
Jens Axboe45410ac2006-06-07 11:10:39 +0200563 /*
564 * Don't break here, continue parsing options so we
565 * dump all the bad ones. Makes trial/error fixups
566 * easier on the user.
567 */
Jens Axboe214e1ec2007-03-15 10:48:13 +0100568 ret |= fio_option_parse(td, p);
Jens Axboeebac4652005-12-08 15:25:21 +0100569 }
Jens Axboeebac4652005-12-08 15:25:21 +0100570
Jens Axboe45410ac2006-06-07 11:10:39 +0200571 if (!ret) {
572 fsetpos(f, &off);
573 ret = add_job(td, name, 0);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100574 } else {
575 log_err("fio: job %s dropped\n", name);
576 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +0200577 }
Jens Axboe7c124ac2006-10-30 13:36:52 +0100578 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +0100579
580 free(string);
581 free(name);
582 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200583 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100584}
585
586static int fill_def_thread(void)
587{
588 memset(&def_thread, 0, sizeof(def_thread));
589
590 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
591 perror("sched_getaffinity");
592 return 1;
593 }
594
595 /*
Jens Axboeee738492007-01-10 11:23:16 +0100596 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +0100597 */
Jens Axboe214e1ec2007-03-15 10:48:13 +0100598 fio_fill_default_options(&def_thread);
Jens Axboeee738492007-01-10 11:23:16 +0100599
Jens Axboe972cfd22006-06-09 11:08:56 +0200600 def_thread.timeout = def_timeout;
Jens Axboeec94ec52006-10-20 10:59:19 +0200601 def_thread.write_bw_log = write_bw_log;
602 def_thread.write_lat_log = write_lat_log;
Jens Axboeee738492007-01-10 11:23:16 +0100603
Jens Axboeebac4652005-12-08 15:25:21 +0100604#ifdef FIO_HAVE_DISK_UTIL
605 def_thread.do_disk_util = 1;
606#endif
607
608 return 0;
609}
610
Jens Axboeebac4652005-12-08 15:25:21 +0100611static void free_shm(void)
612{
613 struct shmid_ds sbuf;
614
615 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +0200616 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +0100617 threads = NULL;
618 shmctl(shm_id, IPC_RMID, &sbuf);
619 }
620}
621
Jens Axboe906c8d72006-06-13 09:37:56 +0200622/*
623 * The thread area is shared between the main process and the job
624 * threads/processes. So setup a shared memory segment that will hold
625 * all the job info.
626 */
Jens Axboeebac4652005-12-08 15:25:21 +0100627static int setup_thread_area(void)
628{
629 /*
630 * 1024 is too much on some machines, scale max_jobs if
631 * we get a failure that looks like too large a shm segment
632 */
633 do {
Jens Axboe906c8d72006-06-13 09:37:56 +0200634 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +0100635
Jens Axboe906c8d72006-06-13 09:37:56 +0200636 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +0100637 if (shm_id != -1)
638 break;
639 if (errno != EINVAL) {
640 perror("shmget");
641 break;
642 }
643
644 max_jobs >>= 1;
645 } while (max_jobs);
646
647 if (shm_id == -1)
648 return 1;
649
650 threads = shmat(shm_id, NULL, 0);
651 if (threads == (void *) -1) {
652 perror("shmat");
653 return 1;
654 }
655
656 atexit(free_shm);
657 return 0;
658}
659
Jens Axboe214e1ec2007-03-15 10:48:13 +0100660static void usage(void)
Jens Axboeb4692822006-10-27 13:43:22 +0200661{
Jens Axboe214e1ec2007-03-15 10:48:13 +0100662 printf("%s\n", fio_version_string);
663 printf("\t--output\tWrite output to file\n");
664 printf("\t--timeout\tRuntime in seconds\n");
665 printf("\t--latency-log\tGenerate per-job latency logs\n");
666 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
667 printf("\t--minimal\tMinimal (terse) output\n");
668 printf("\t--version\tPrint version info and exit\n");
669 printf("\t--help\t\tPrint this page\n");
670 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
Jens Axboeb4692822006-10-27 13:43:22 +0200671}
672
Jens Axboe214e1ec2007-03-15 10:48:13 +0100673static int parse_cmd_line(int argc, char *argv[])
674{
675 struct thread_data *td = NULL;
676 int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
677
678 while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
679 switch (c) {
680 case 't':
681 def_timeout = atoi(optarg);
682 break;
683 case 'l':
684 write_lat_log = 1;
685 break;
686 case 'w':
687 write_bw_log = 1;
688 break;
689 case 'o':
690 f_out = fopen(optarg, "w+");
691 if (!f_out) {
692 perror("fopen output");
693 exit(1);
694 }
695 f_err = f_out;
696 break;
697 case 'm':
698 terse_output = 1;
699 break;
700 case 'h':
701 usage();
702 exit(0);
703 case 'c':
704 exit(fio_show_option_help(optarg));
705 case 'v':
706 printf("%s\n", fio_version_string);
707 exit(0);
708 case FIO_GETOPT_JOB: {
709 const char *opt = long_options[lidx].name;
710 char *val = optarg;
711
712 if (!strncmp(opt, "name", 4) && td) {
713 ret = add_job(td, td->name ?: "fio", 0);
714 if (ret) {
715 put_job(td);
716 return 0;
717 }
718 td = NULL;
719 }
720 if (!td) {
721 int global = !strncmp(val, "global", 6);
722
723 td = get_new_job(global, &def_thread);
724 if (!td)
725 return 0;
726 }
727
728 ret = fio_cmd_option_parse(td, opt, val);
729 if (ret)
730 dont_add_job = 1;
731 break;
732 }
733 default:
734 break;
735 }
736 }
737
738 if (td) {
739 if (dont_add_job)
740 put_job(td);
741 else {
742 ret = add_job(td, td->name ?: "fio", 0);
743 if (ret)
744 put_job(td);
745 }
746 }
747
748 while (optind < argc) {
749 ini_idx++;
750 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
751 ini_file[ini_idx - 1] = strdup(argv[optind]);
752 optind++;
753 }
754
755 return ini_idx;
756}
757
758
Jens Axboeebac4652005-12-08 15:25:21 +0100759int parse_options(int argc, char *argv[])
760{
Jens Axboe972cfd22006-06-09 11:08:56 +0200761 int job_files, i;
762
Jens Axboeb4692822006-10-27 13:43:22 +0200763 f_out = stdout;
764 f_err = stderr;
765
Jens Axboe214e1ec2007-03-15 10:48:13 +0100766 fio_options_dup_and_init(long_options);
Jens Axboeb4692822006-10-27 13:43:22 +0200767
Jens Axboeebac4652005-12-08 15:25:21 +0100768 if (setup_thread_area())
769 return 1;
770 if (fill_def_thread())
771 return 1;
772
Jens Axboe972cfd22006-06-09 11:08:56 +0200773 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +0100774
Jens Axboe972cfd22006-06-09 11:08:56 +0200775 for (i = 0; i < job_files; i++) {
776 if (fill_def_thread())
777 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200778 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +0200779 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +0200780 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +0200781 }
Jens Axboeebac4652005-12-08 15:25:21 +0100782
Jens Axboe88c6ed82006-06-09 11:28:10 +0200783 free(ini_file);
Jens Axboeb4692822006-10-27 13:43:22 +0200784
785 if (!thread_number) {
786 log_err("No jobs defined(s)\n");
Jens Axboeb4692822006-10-27 13:43:22 +0200787 return 1;
788 }
789
Jens Axboeebac4652005-12-08 15:25:21 +0100790 return 0;
791}