blob: 967f0b50589d1fe431599fba7f6a6c1e35645012 [file] [log] [blame]
Jens Axboe906c8d72006-06-13 09:37:56 +02001/*
2 * This file contains the ini and command liner parser. It will create
3 * and initialize the specified jobs.
4 */
Jens Axboeebac4652005-12-08 15:25:21 +01005#include <stdio.h>
6#include <stdlib.h>
7#include <unistd.h>
8#include <fcntl.h>
9#include <ctype.h>
10#include <string.h>
11#include <errno.h>
12#include <sys/ipc.h>
13#include <sys/shm.h>
14#include <sys/types.h>
15#include <sys/stat.h>
16
17#include "fio.h"
18
Jens Axboe906c8d72006-06-13 09:37:56 +020019/*
20 * The default options
21 */
Jens Axboe20dc95c2005-12-09 10:29:35 +010022#define DEF_BS (4096)
23#define DEF_TIMEOUT (0)
24#define DEF_RATE_CYCLE (1000)
25#define DEF_ODIRECT (1)
26#define DEF_IO_ENGINE (FIO_SYNCIO)
Jens Axboeebac4652005-12-08 15:25:21 +010027#define DEF_IO_ENGINE_NAME "sync"
Jens Axboe20dc95c2005-12-09 10:29:35 +010028#define DEF_SEQUENTIAL (1)
29#define DEF_RAND_REPEAT (1)
30#define DEF_OVERWRITE (1)
31#define DEF_CREATE (1)
32#define DEF_INVALIDATE (1)
33#define DEF_SYNCIO (0)
34#define DEF_RANDSEED (0xb1899bedUL)
35#define DEF_BWAVGTIME (500)
36#define DEF_CREATE_SER (1)
Jens Axboeebac4652005-12-08 15:25:21 +010037#define DEF_CREATE_FSYNC (1)
Jens Axboe20dc95c2005-12-09 10:29:35 +010038#define DEF_LOOPS (1)
39#define DEF_VERIFY (0)
40#define DEF_STONEWALL (0)
41#define DEF_NUMJOBS (1)
42#define DEF_USE_THREAD (0)
43#define DEF_FILE_SIZE (1024 * 1024 * 1024UL)
44#define DEF_ZONE_SIZE (0)
45#define DEF_ZONE_SKIP (0)
Jens Axboea6ccc7b2006-06-02 10:14:15 +020046#define DEF_RWMIX_CYCLE (500)
47#define DEF_RWMIX_READ (50)
Jens Axboeb6f4d882006-06-02 10:32:51 +020048#define DEF_NICE (0)
Jens Axboe53cdc682006-10-18 11:50:58 +020049#define DEF_NR_FILES (1)
Jens Axboeebac4652005-12-08 15:25:21 +010050
Jens Axboe972cfd22006-06-09 11:08:56 +020051static int def_timeout = DEF_TIMEOUT;
Jens Axboe972cfd22006-06-09 11:08:56 +020052
Jens Axboef4866ec2006-06-13 09:38:36 +020053static char fio_version_string[] = "fio 1.5";
Jens Axboeebac4652005-12-08 15:25:21 +010054
Jens Axboe972cfd22006-06-09 11:08:56 +020055static char **ini_file;
Jens Axboeebac4652005-12-08 15:25:21 +010056static int max_jobs = MAX_JOBS;
57
58struct thread_data def_thread;
59struct thread_data *threads = NULL;
60
61int rate_quit = 0;
62int write_lat_log = 0;
63int write_bw_log = 0;
64int exitall_on_terminate = 0;
Jens Axboec6ae0a52006-06-12 11:04:44 +020065int terse_output = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +020066unsigned long long mlock_size = 0;
Jens Axboeeb8bbf42006-06-08 21:40:11 +020067FILE *f_out = NULL;
68FILE *f_err = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +010069
Jens Axboe906c8d72006-06-13 09:37:56 +020070/*
71 * Return a free job structure.
72 */
Jens Axboeebac4652005-12-08 15:25:21 +010073static struct thread_data *get_new_job(int global, struct thread_data *parent)
74{
75 struct thread_data *td;
76
77 if (global)
78 return &def_thread;
79 if (thread_number >= max_jobs)
80 return NULL;
81
82 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +020083 *td = *parent;
84 td->name[0] = '\0';
Jens Axboeebac4652005-12-08 15:25:21 +010085
Jens Axboeebac4652005-12-08 15:25:21 +010086 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +010087 return td;
88}
89
90static void put_job(struct thread_data *td)
91{
92 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
93 thread_number--;
94}
95
Jens Axboe906c8d72006-06-13 09:37:56 +020096/*
97 * Adds a job to the list of things todo. Sanitizes the various options
98 * to make sure we don't have conflicts, and initializes various
99 * members of td.
100 */
Jens Axboe75154842006-06-01 13:56:09 +0200101static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100102{
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200103 char *ddir_str[] = { "read", "write", "randread", "randwrite",
104 "rw", NULL, "randrw" };
Jens Axboeebac4652005-12-08 15:25:21 +0100105 struct stat sb;
Jens Axboe53cdc682006-10-18 11:50:58 +0200106 int numjobs, ddir, i;
107 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100108
109#ifndef FIO_HAVE_LIBAIO
110 if (td->io_engine == FIO_LIBAIO) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200111 log_err("Linux libaio not available\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100112 return 1;
113 }
114#endif
115#ifndef FIO_HAVE_POSIXAIO
116 if (td->io_engine == FIO_POSIXAIO) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200117 log_err("posix aio not available\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100118 return 1;
119 }
120#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100121
122 /*
123 * the def_thread is just for options, it's not a real job
124 */
125 if (td == &def_thread)
126 return 0;
127
Jens Axboedf641192006-10-12 07:55:41 +0200128 /*
129 * Set default io engine, if none set
130 */
131 if (!td->io_ops) {
132 td->io_ops = load_ioengine(td, DEF_IO_ENGINE_NAME);
133 if (!td->io_ops) {
134 log_err("default engine %s not there?\n", DEF_IO_ENGINE_NAME);
135 return 1;
136 }
137 }
138
Jens Axboe2866c822006-10-09 15:57:48 +0200139 if (td->io_ops->flags & FIO_SYNCIO)
Jens Axboeebac4652005-12-08 15:25:21 +0100140 td->iodepth = 1;
141 else {
142 if (!td->iodepth)
143 td->iodepth = 1;
144 }
145
Jens Axboe20dc95c2005-12-09 10:29:35 +0100146 /*
Jens Axboe53cdc682006-10-18 11:50:58 +0200147 * only really works for sequential io for now, and with 1 file
Jens Axboe20dc95c2005-12-09 10:29:35 +0100148 */
Jens Axboe53cdc682006-10-18 11:50:58 +0200149 if (td->zone_size && !td->sequential && td->nr_files == 1)
Jens Axboe20dc95c2005-12-09 10:29:35 +0100150 td->zone_size = 0;
151
Jens Axboe9cc935a2006-06-13 09:11:18 +0200152 /*
153 * Reads can do overwrites, we always need to pre-create the file
154 */
155 if (td_read(td) || td_rw(td))
156 td->overwrite = 1;
157
Jens Axboeebac4652005-12-08 15:25:21 +0100158 td->filetype = FIO_TYPE_FILE;
Jens Axboe0af7b542006-02-17 10:10:12 +0100159 if (!stat(jobname, &sb)) {
160 if (S_ISBLK(sb.st_mode))
161 td->filetype = FIO_TYPE_BD;
162 else if (S_ISCHR(sb.st_mode))
163 td->filetype = FIO_TYPE_CHAR;
164 }
Jens Axboeebac4652005-12-08 15:25:21 +0100165
166 if (td->filetype == FIO_TYPE_FILE) {
Jens Axboee9c047a2006-06-07 21:13:04 +0200167 char tmp[PATH_MAX];
Jens Axboe53cdc682006-10-18 11:50:58 +0200168 int len = 0;
169 int i;
Jens Axboee9c047a2006-06-07 21:13:04 +0200170
Jens Axboeef899b62006-06-06 09:31:00 +0200171 if (td->directory && td->directory[0] != '\0')
Jens Axboe53cdc682006-10-18 11:50:58 +0200172 sprintf(tmp, "%s/", td->directory);
Jens Axboeebac4652005-12-08 15:25:21 +0100173
Jens Axboe53cdc682006-10-18 11:50:58 +0200174 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
175
176 for_each_file(td, f, i) {
177 memset(f, 0, sizeof(*f));
178 f->fd = -1;
179
180 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
181 f->file_name = strdup(tmp);
182 }
183 } else {
184 td->nr_files = 1;
185 td->files = malloc(sizeof(struct fio_file));
186 f = &td->files[0];
187
188 memset(f, 0, sizeof(*f));
189 f->fd = -1;
190 f->file_name = strdup(jobname);
191 }
192
193 for_each_file(td, f, i) {
194 f->file_size = td->total_file_size / td->nr_files;
195 f->file_offset = td->start_offset;
196 }
197
Jens Axboebbfd6b02006-06-07 19:42:54 +0200198 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100199
200 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
201 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
202 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
203
204 if (td->min_bs == -1U)
205 td->min_bs = td->bs;
206 if (td->max_bs == -1U)
207 td->max_bs = td->bs;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200208 if (td_read(td) && !td_rw(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100209 td->verify = 0;
210
211 if (td->stonewall && td->thread_number > 1)
212 groupid++;
213
214 td->groupid = groupid;
215
216 if (setup_rate(td))
217 goto err;
218
219 if (write_lat_log) {
220 setup_log(&td->slat_log);
221 setup_log(&td->clat_log);
222 }
223 if (write_bw_log)
224 setup_log(&td->bw_log);
225
Jens Axboe01452052006-06-07 10:29:47 +0200226 if (td->name[0] == '\0')
227 snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
228
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200229 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
Jens Axboe75154842006-06-01 13:56:09 +0200230
Jens Axboec6ae0a52006-06-12 11:04:44 +0200231 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200232 if (!job_add_num) {
Jens Axboe2866c822006-10-09 15:57:48 +0200233 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200234 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
235 else
Jens Axboe2866c822006-10-09 15:57:48 +0200236 fprintf(f_out, "%s: (g=%d): rw=%s, odir=%d, bs=%d-%d, rate=%d, ioengine=%s, iodepth=%d\n", td->name, td->groupid, ddir_str[ddir], td->odirect, td->min_bs, td->max_bs, td->rate, td->io_ops->name, td->iodepth);
Jens Axboeb990b5c2006-09-14 09:48:22 +0200237 } else if (job_add_num == 1)
Jens Axboec6ae0a52006-06-12 11:04:44 +0200238 fprintf(f_out, "...\n");
239 }
Jens Axboeebac4652005-12-08 15:25:21 +0100240
241 /*
242 * recurse add identical jobs, clear numjobs and stonewall options
243 * as they don't apply to sub-jobs
244 */
245 numjobs = td->numjobs;
246 while (--numjobs) {
247 struct thread_data *td_new = get_new_job(0, td);
248
249 if (!td_new)
250 goto err;
251
252 td_new->numjobs = 1;
253 td_new->stonewall = 0;
Jens Axboe75154842006-06-01 13:56:09 +0200254 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100255
Jens Axboe75154842006-06-01 13:56:09 +0200256 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100257 goto err;
258 }
259 return 0;
260err:
261 put_job(td);
262 return -1;
263}
264
Jens Axboe906c8d72006-06-13 09:37:56 +0200265/*
266 * Initialize the various random states we need (random io, block size ranges,
267 * read/write mix, etc).
268 */
Jens Axboeebac4652005-12-08 15:25:21 +0100269int init_random_state(struct thread_data *td)
270{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200271 unsigned long seeds[4];
Jens Axboe53cdc682006-10-18 11:50:58 +0200272 int fd, num_maps, blocks, i;
Jens Axboeebac4652005-12-08 15:25:21 +0100273
Jens Axboe1ac267b2006-05-31 20:29:00 +0200274 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100275 if (fd == -1) {
276 td_verror(td, errno);
277 return 1;
278 }
279
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200280 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100281 td_verror(td, EIO);
282 close(fd);
283 return 1;
284 }
285
286 close(fd);
287
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200288 os_random_seed(seeds[0], &td->bsrange_state);
289 os_random_seed(seeds[1], &td->verify_state);
290 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100291
292 if (td->sequential)
293 return 0;
294
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200295 if (td->rand_repeatable)
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200296 seeds[3] = DEF_RANDSEED;
Jens Axboeebac4652005-12-08 15:25:21 +0100297
Jens Axboe53cdc682006-10-18 11:50:58 +0200298 for (i = 0; i < td->nr_files; i++) {
299 struct fio_file *f = &td->files[i];
300
301 blocks = (f->file_size + td->min_bs - 1) / td->min_bs;
302 num_maps = blocks / BLOCKS_PER_MAP;
303 f->file_map = malloc(num_maps * sizeof(long));
304 f->num_maps = num_maps;
305 memset(f->file_map, 0, num_maps * sizeof(long));
306 }
Jens Axboeebac4652005-12-08 15:25:21 +0100307
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200308 os_random_seed(seeds[3], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100309 return 0;
310}
311
312static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
313{
314#ifdef FIO_HAVE_CPU_AFFINITY
315 unsigned int i;
316
317 CPU_ZERO(&cpumask);
318
319 for (i = 0; i < sizeof(int) * 8; i++) {
320 if ((1 << i) & cpu)
321 CPU_SET(i, &cpumask);
322 }
323#endif
324}
325
Jens Axboe906c8d72006-06-13 09:37:56 +0200326static unsigned long get_mult_time(char c)
327{
328 switch (c) {
329 case 'm':
330 case 'M':
331 return 60;
332 case 'h':
333 case 'H':
334 return 60 * 60;
335 case 'd':
336 case 'D':
337 return 24 * 60 * 60;
338 default:
339 return 1;
340 }
341}
342
343static unsigned long get_mult_bytes(char c)
Jens Axboeebac4652005-12-08 15:25:21 +0100344{
345 switch (c) {
346 case 'k':
347 case 'K':
348 return 1024;
349 case 'm':
350 case 'M':
351 return 1024 * 1024;
352 case 'g':
353 case 'G':
354 return 1024 * 1024 * 1024;
355 default:
356 return 1;
357 }
358}
359
360/*
361 * convert string after '=' into decimal value, noting any size suffix
362 */
Jens Axboe906c8d72006-06-13 09:37:56 +0200363static int str_to_decimal(char *p, unsigned long long *val, int kilo)
Jens Axboeebac4652005-12-08 15:25:21 +0100364{
365 char *str;
366 int len;
367
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100368 str = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100369 if (!str)
370 return 1;
371
372 str++;
373 len = strlen(str);
374
375 *val = strtoul(str, NULL, 10);
376 if (*val == ULONG_MAX && errno == ERANGE)
377 return 1;
378
Jens Axboe906c8d72006-06-13 09:37:56 +0200379 if (kilo)
380 *val *= get_mult_bytes(str[len - 1]);
381 else
382 *val *= get_mult_time(str[len - 1]);
Jens Axboeebac4652005-12-08 15:25:21 +0100383 return 0;
384}
385
Jens Axboe906c8d72006-06-13 09:37:56 +0200386static int check_str_bytes(char *p, char *name, unsigned long long *val)
Jens Axboeebac4652005-12-08 15:25:21 +0100387{
Jens Axboe20dc95c2005-12-09 10:29:35 +0100388 if (strncmp(p, name, strlen(name) - 1))
Jens Axboeebac4652005-12-08 15:25:21 +0100389 return 1;
390
Jens Axboe906c8d72006-06-13 09:37:56 +0200391 return str_to_decimal(p, val, 1);
392}
393
394static int check_str_time(char *p, char *name, unsigned long long *val)
395{
396 if (strncmp(p, name, strlen(name) - 1))
397 return 1;
398
399 return str_to_decimal(p, val, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100400}
401
402static void strip_blank_front(char **p)
403{
404 char *s = *p;
405
Jens Axboe4ae3f762006-05-30 13:35:14 +0200406 while (isspace(*s))
Jens Axboeebac4652005-12-08 15:25:21 +0100407 s++;
408}
409
410static void strip_blank_end(char *p)
411{
Jens Axboe4ae3f762006-05-30 13:35:14 +0200412 char *s = p + strlen(p) - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100413
Jens Axboe4ae3f762006-05-30 13:35:14 +0200414 while (isspace(*s) || iscntrl(*s))
415 s--;
Jens Axboeaea47d42006-05-26 19:27:29 +0200416
Jens Axboe4ae3f762006-05-30 13:35:14 +0200417 *(s + 1) = '\0';
Jens Axboeaea47d42006-05-26 19:27:29 +0200418}
419
Jens Axboeebac4652005-12-08 15:25:21 +0100420typedef int (str_cb_fn)(struct thread_data *, char *);
421
422static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
423{
Jens Axboe843a7412006-06-01 21:14:21 -0700424 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100425
Jens Axboe843a7412006-06-01 21:14:21 -0700426 if (strncmp(p, name, strlen(name)))
427 return 1;
428
429 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100430 if (!s)
431 return 1;
432
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100433 s = strchr(s, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100434 if (!s)
435 return 1;
436
437 s++;
438 strip_blank_front(&s);
439 return cb(td, s);
440}
441
442static int check_strstore(char *p, char *name, char *dest)
443{
Jens Axboe843a7412006-06-01 21:14:21 -0700444 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100445
Jens Axboe843a7412006-06-01 21:14:21 -0700446 if (strncmp(p, name, strlen(name)))
447 return 1;
448
449 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100450 if (!s)
451 return 1;
452
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100453 s = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100454 if (!s)
455 return 1;
456
457 s++;
458 strip_blank_front(&s);
459
460 strcpy(dest, s);
Jens Axboeebac4652005-12-08 15:25:21 +0100461 return 0;
462}
463
Jens Axboe906c8d72006-06-13 09:37:56 +0200464static int __check_range_bytes(char *str, unsigned long *val)
Jens Axboeebac4652005-12-08 15:25:21 +0100465{
Jens Axboe01617be2005-12-08 19:50:40 +0100466 char suffix;
Jens Axboeebac4652005-12-08 15:25:21 +0100467
Jens Axboe01617be2005-12-08 19:50:40 +0100468 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
Jens Axboe906c8d72006-06-13 09:37:56 +0200469 *val *= get_mult_bytes(suffix);
Jens Axboeebac4652005-12-08 15:25:21 +0100470 return 0;
471 }
472
Jens Axboe01617be2005-12-08 19:50:40 +0100473 if (sscanf(str, "%lu", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100474 return 0;
475
476 return 1;
Jens Axboe01617be2005-12-08 19:50:40 +0100477}
Jens Axboeebac4652005-12-08 15:25:21 +0100478
Jens Axboe906c8d72006-06-13 09:37:56 +0200479static int check_range_bytes(char *p, char *name, unsigned long *s,
480 unsigned long *e)
Jens Axboe01617be2005-12-08 19:50:40 +0100481{
482 char option[128];
483 char *str, *p1, *p2;
484
Jens Axboe843a7412006-06-01 21:14:21 -0700485 if (strncmp(p, name, strlen(name)))
486 return 1;
487
Jens Axboe01617be2005-12-08 19:50:40 +0100488 strcpy(option, p);
489 p = option;
490
491 str = strstr(p, name);
492 if (!str)
493 return 1;
494
495 p += strlen(name);
496
497 str = strchr(p, '=');
498 if (!str)
499 return 1;
500
501 /*
502 * 'p' now holds whatever is after the '=' sign
503 */
504 p1 = str + 1;
505
506 /*
507 * terminate p1 at the '-' sign
508 */
509 p = strchr(p1, '-');
510 if (!p)
511 return 1;
512
513 p2 = p + 1;
514 *p = '\0';
515
Jens Axboe906c8d72006-06-13 09:37:56 +0200516 if (!__check_range_bytes(p1, s) && !__check_range_bytes(p2, e))
Jens Axboe01617be2005-12-08 19:50:40 +0100517 return 0;
518
519 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100520}
521
522static int check_int(char *p, char *name, unsigned int *val)
523{
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100524 char *str;
Jens Axboeebac4652005-12-08 15:25:21 +0100525
Jens Axboeb6754f92006-05-30 14:29:32 +0200526 if (strncmp(p, name, strlen(name)))
527 return 1;
528
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100529 str = strstr(p, name);
530 if (!str)
531 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100532
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100533 str = strchr(p, '=');
534 if (!str)
535 return 1;
536
537 str++;
538
539 if (sscanf(str, "%u", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100540 return 0;
541
542 return 1;
543}
544
545static int check_strset(char *p, char *name)
546{
547 return strncmp(p, name, strlen(name));
548}
549
550static int is_empty_or_comment(char *line)
551{
552 unsigned int i;
553
554 for (i = 0; i < strlen(line); i++) {
555 if (line[i] == ';')
556 return 1;
557 if (!isspace(line[i]) && !iscntrl(line[i]))
558 return 0;
559 }
560
561 return 1;
562}
563
564static int str_rw_cb(struct thread_data *td, char *mem)
565{
566 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
567 td->ddir = DDIR_READ;
568 td->sequential = 1;
569 return 0;
570 } else if (!strncmp(mem, "randread", 8)) {
571 td->ddir = DDIR_READ;
572 td->sequential = 0;
573 return 0;
574 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
575 td->ddir = DDIR_WRITE;
576 td->sequential = 1;
577 return 0;
578 } else if (!strncmp(mem, "randwrite", 9)) {
579 td->ddir = DDIR_WRITE;
580 td->sequential = 0;
581 return 0;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200582 } else if (!strncmp(mem, "rw", 2)) {
583 td->ddir = 0;
584 td->iomix = 1;
585 td->sequential = 1;
586 return 0;
587 } else if (!strncmp(mem, "randrw", 6)) {
588 td->ddir = 0;
589 td->iomix = 1;
590 td->sequential = 0;
591 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100592 }
593
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200594 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100595 return 1;
596}
597
598static int str_verify_cb(struct thread_data *td, char *mem)
599{
600 if (!strncmp(mem, "0", 1)) {
601 td->verify = VERIFY_NONE;
602 return 0;
603 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
604 td->verify = VERIFY_MD5;
605 return 0;
606 } else if (!strncmp(mem, "crc32", 5)) {
607 td->verify = VERIFY_CRC32;
608 return 0;
609 }
610
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200611 log_err("fio: verify types: md5, crc32\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100612 return 1;
613}
614
615static int str_mem_cb(struct thread_data *td, char *mem)
616{
617 if (!strncmp(mem, "malloc", 6)) {
618 td->mem_type = MEM_MALLOC;
619 return 0;
620 } else if (!strncmp(mem, "shm", 3)) {
621 td->mem_type = MEM_SHM;
622 return 0;
623 } else if (!strncmp(mem, "mmap", 4)) {
624 td->mem_type = MEM_MMAP;
625 return 0;
626 }
627
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200628 log_err("fio: mem type: malloc, shm, mmap\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100629 return 1;
630}
631
632static int str_ioengine_cb(struct thread_data *td, char *str)
633{
Jens Axboe2866c822006-10-09 15:57:48 +0200634 td->io_ops = load_ioengine(td, str);
635 if (td->io_ops)
Jens Axboeebac4652005-12-08 15:25:21 +0100636 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100637
Jens Axboeb990b5c2006-09-14 09:48:22 +0200638 log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice, cpu\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100639 return 1;
640}
641
Jens Axboe07261982006-06-07 10:51:12 +0200642/*
643 * This is our [ini] type file parser.
644 */
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200645int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100646{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200647 unsigned int prioclass, prio, cpu, global, il;
Jens Axboeebac4652005-12-08 15:25:21 +0100648 unsigned long long ull;
649 unsigned long ul1, ul2;
650 struct thread_data *td;
Jens Axboeef899b62006-06-06 09:31:00 +0200651 char *string, *name, *tmpbuf;
Jens Axboeebac4652005-12-08 15:25:21 +0100652 fpos_t off;
653 FILE *f;
654 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200655 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +0100656
657 f = fopen(file, "r");
658 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200659 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100660 return 1;
661 }
662
663 string = malloc(4096);
664 name = malloc(256);
Jens Axboeef899b62006-06-06 09:31:00 +0200665 tmpbuf = malloc(4096);
Jens Axboeebac4652005-12-08 15:25:21 +0100666
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200667 stonewall = stonewall_flag;
Jens Axboeebac4652005-12-08 15:25:21 +0100668 while ((p = fgets(string, 4096, f)) != NULL) {
Jens Axboe45410ac2006-06-07 11:10:39 +0200669 if (ret)
670 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100671 if (is_empty_or_comment(p))
672 continue;
673 if (sscanf(p, "[%s]", name) != 1)
674 continue;
675
676 global = !strncmp(name, "global", 6);
677
678 name[strlen(name) - 1] = '\0';
679
680 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200681 if (!td) {
682 ret = 1;
683 break;
684 }
Jens Axboeebac4652005-12-08 15:25:21 +0100685
Jens Axboe972cfd22006-06-09 11:08:56 +0200686 /*
687 * Seperate multiple job files by a stonewall
688 */
Jens Axboef9481912006-06-09 11:37:28 +0200689 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +0200690 td->stonewall = stonewall;
691 stonewall = 0;
692 }
693
Jens Axboeebac4652005-12-08 15:25:21 +0100694 fgetpos(f, &off);
695 while ((p = fgets(string, 4096, f)) != NULL) {
696 if (is_empty_or_comment(p))
697 continue;
698 if (strstr(p, "["))
699 break;
Jens Axboeb6754f92006-05-30 14:29:32 +0200700 strip_blank_front(&p);
Jens Axboe4ae3f762006-05-30 13:35:14 +0200701 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200702
Jens Axboeebac4652005-12-08 15:25:21 +0100703 if (!check_int(p, "prio", &prio)) {
704#ifndef FIO_HAVE_IOPRIO
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200705 log_err("io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200706 ret = 1;
707 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100708#endif
Jens Axboe8756e4d2006-05-27 20:24:53 +0200709 td->ioprio |= prio;
Jens Axboeebac4652005-12-08 15:25:21 +0100710 fgetpos(f, &off);
711 continue;
712 }
713 if (!check_int(p, "prioclass", &prioclass)) {
714#ifndef FIO_HAVE_IOPRIO
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200715 log_err("io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200716 ret = 1;
717 break;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200718#else
Jens Axboe8756e4d2006-05-27 20:24:53 +0200719 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
Jens Axboeebac4652005-12-08 15:25:21 +0100720 fgetpos(f, &off);
721 continue;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200722#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100723 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200724 if (!check_int(p, "direct", &il)) {
725 td->odirect = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100726 fgetpos(f, &off);
727 continue;
728 }
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200729 if (!check_int(p, "rand_repeatable", &il)) {
730 td->rand_repeatable = il;
731 fgetpos(f, &off);
732 continue;
733 }
Jens Axboeebac4652005-12-08 15:25:21 +0100734 if (!check_int(p, "rate", &td->rate)) {
735 fgetpos(f, &off);
736 continue;
737 }
738 if (!check_int(p, "ratemin", &td->ratemin)) {
739 fgetpos(f, &off);
740 continue;
741 }
742 if (!check_int(p, "ratecycle", &td->ratecycle)) {
743 fgetpos(f, &off);
744 continue;
745 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200746 if (!check_int(p, "cpuload", &td->cpuload)) {
747 fgetpos(f, &off);
748 continue;
749 }
750 if (!check_int(p, "cpuchunks", &td->cpucycle)) {
751 fgetpos(f, &off);
752 continue;
753 }
Jens Axboeebac4652005-12-08 15:25:21 +0100754 if (!check_int(p, "thinktime", &td->thinktime)) {
755 fgetpos(f, &off);
756 continue;
757 }
758 if (!check_int(p, "cpumask", &cpu)) {
759#ifndef FIO_HAVE_CPU_AFFINITY
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200760 log_err("cpu affinity not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200761 ret = 1;
762 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100763#endif
764 fill_cpu_mask(td->cpumask, cpu);
765 fgetpos(f, &off);
766 continue;
767 }
768 if (!check_int(p, "fsync", &td->fsync_blocks)) {
769 fgetpos(f, &off);
Jens Axboefc1a4712006-05-30 13:04:05 +0200770 td->end_fsync = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100771 continue;
772 }
773 if (!check_int(p, "startdelay", &td->start_delay)) {
774 fgetpos(f, &off);
775 continue;
776 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200777 if (!check_str_time(p, "timeout", &ull)) {
Jens Axboeaf959a72006-07-13 14:36:02 -0700778 td->timeout = ull;
Jens Axboeebac4652005-12-08 15:25:21 +0100779 fgetpos(f, &off);
780 continue;
781 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200782 if (!check_int(p, "invalidate", &il)) {
783 td->invalidate_cache = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100784 fgetpos(f, &off);
785 continue;
786 }
787 if (!check_int(p, "iodepth", &td->iodepth)) {
788 fgetpos(f, &off);
789 continue;
790 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200791 if (!check_int(p, "sync", &il)) {
792 td->sync_io = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100793 fgetpos(f, &off);
794 continue;
795 }
796 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
797 fgetpos(f, &off);
798 continue;
799 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200800 if (!check_int(p, "create_serialize", &il)) {
801 td->create_serialize = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100802 fgetpos(f, &off);
803 continue;
804 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200805 if (!check_int(p, "create_fsync", &il)) {
806 td->create_fsync = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100807 fgetpos(f, &off);
808 continue;
809 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200810 if (!check_int(p, "end_fsync", &il)) {
811 td->end_fsync = il;
Jens Axboefc1a4712006-05-30 13:04:05 +0200812 fgetpos(f, &off);
813 continue;
814 }
Jens Axboeebac4652005-12-08 15:25:21 +0100815 if (!check_int(p, "loops", &td->loops)) {
816 fgetpos(f, &off);
817 continue;
818 }
819 if (!check_int(p, "numjobs", &td->numjobs)) {
820 fgetpos(f, &off);
821 continue;
822 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200823 if (!check_int(p, "overwrite", &il)) {
824 td->overwrite = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100825 fgetpos(f, &off);
826 continue;
827 }
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200828 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
829 fgetpos(f, &off);
830 continue;
831 }
832 if (!check_int(p, "rwmixread", &il)) {
833 if (il > 100)
834 il = 100;
835 td->rwmixread = il;
836 fgetpos(f, &off);
837 continue;
838 }
839 if (!check_int(p, "rwmixwrite", &il)) {
840 if (il > 100)
841 il = 100;
842 td->rwmixread = 100 - il;
843 fgetpos(f, &off);
844 continue;
845 }
Jens Axboeb6f4d882006-06-02 10:32:51 +0200846 if (!check_int(p, "nice", &td->nice)) {
847 fgetpos(f, &off);
848 continue;
849 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200850 if (!check_int(p, "nrfiles", &td->nr_files)) {
851 fgetpos(f, &off);
852 continue;
853 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200854 if (!check_range_bytes(p, "bsrange", &ul1, &ul2)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100855 if (ul1 > ul2) {
856 td->max_bs = ul1;
857 td->min_bs = ul2;
858 } else {
859 td->max_bs = ul2;
860 td->min_bs = ul1;
861 }
862 fgetpos(f, &off);
863 continue;
864 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200865 if (!check_str_bytes(p, "bs", &ull)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100866 td->bs = ull;
867 fgetpos(f, &off);
868 continue;
869 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200870 if (!check_str_bytes(p, "size", &td->total_file_size)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100871 fgetpos(f, &off);
872 continue;
873 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200874 if (!check_str_bytes(p, "offset", &td->start_offset)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100875 fgetpos(f, &off);
876 continue;
877 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200878 if (!check_str_bytes(p, "zonesize", &td->zone_size)) {
Jens Axboe20dc95c2005-12-09 10:29:35 +0100879 fgetpos(f, &off);
880 continue;
881 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200882 if (!check_str_bytes(p, "zoneskip", &td->zone_skip)) {
Jens Axboe20dc95c2005-12-09 10:29:35 +0100883 fgetpos(f, &off);
884 continue;
885 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200886 if (!check_str_bytes(p, "lockmem", &mlock_size)) {
Jens Axboec04f7ec2006-05-31 10:13:16 +0200887 fgetpos(f, &off);
888 continue;
889 }
Jens Axboeef899b62006-06-06 09:31:00 +0200890 if (!check_strstore(p, "directory", tmpbuf)) {
891 td->directory = strdup(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100892 fgetpos(f, &off);
893 continue;
894 }
Jens Axboe01452052006-06-07 10:29:47 +0200895 if (!check_strstore(p, "name", tmpbuf)) {
896 snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
897 fgetpos(f, &off);
898 continue;
899 }
Jens Axboeebac4652005-12-08 15:25:21 +0100900 if (!check_str(p, "mem", str_mem_cb, td)) {
901 fgetpos(f, &off);
902 continue;
903 }
904 if (!check_str(p, "verify", str_verify_cb, td)) {
905 fgetpos(f, &off);
906 continue;
907 }
908 if (!check_str(p, "rw", str_rw_cb, td)) {
909 fgetpos(f, &off);
910 continue;
911 }
912 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
913 fgetpos(f, &off);
914 continue;
915 }
916 if (!check_strset(p, "create")) {
917 td->create_file = 1;
918 fgetpos(f, &off);
919 continue;
920 }
921 if (!check_strset(p, "exitall")) {
922 exitall_on_terminate = 1;
923 fgetpos(f, &off);
924 continue;
925 }
926 if (!check_strset(p, "stonewall")) {
927 td->stonewall = 1;
928 fgetpos(f, &off);
929 continue;
930 }
931 if (!check_strset(p, "thread")) {
932 td->use_thread = 1;
933 fgetpos(f, &off);
934 continue;
935 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200936 if (!check_strstore(p, "iolog", tmpbuf)) {
Jens Axboe4f693b92006-06-07 22:52:28 +0200937 if (td->write_iolog) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200938 log_err("fio: read iolog overrides given write_iolog\n");
Jens Axboeaf8c0b42006-06-07 22:33:17 +0200939 free(td->iolog_file);
Jens Axboe4f693b92006-06-07 22:52:28 +0200940 td->write_iolog = 0;
941 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200942 td->iolog_file = strdup(tmpbuf);
Jens Axboe843a7412006-06-01 21:14:21 -0700943 td->read_iolog = 1;
Jens Axboe843a7412006-06-01 21:14:21 -0700944 fgetpos(f, &off);
945 continue;
946 }
Jens Axboeaf8c0b42006-06-07 22:33:17 +0200947 if (!check_strstore(p, "write_iolog", tmpbuf)) {
948 if (!td->read_iolog) {
949 td->iolog_file = strdup(tmpbuf);
950 td->write_iolog = 1;
Jens Axboe4f693b92006-06-07 22:52:28 +0200951 } else
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200952 log_err("fio: read iolog overrides given write_iolog\n");
Jens Axboeaea47d42006-05-26 19:27:29 +0200953 fgetpos(f, &off);
954 continue;
955 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200956 if (!check_strstore(p, "exec_prerun", tmpbuf)) {
957 td->exec_prerun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200958 fgetpos(f, &off);
959 continue;
960 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200961 if (!check_strstore(p, "exec_postrun", tmpbuf)) {
962 td->exec_postrun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200963 fgetpos(f, &off);
964 continue;
965 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200966 if (!check_strstore(p, "ioscheduler", tmpbuf)) {
Jens Axboe22f78b32006-06-07 11:30:07 +0200967#ifndef FIO_HAVE_IOSCHED_SWITCH
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200968 log_err("io scheduler switching not available\n");
Jens Axboe22f78b32006-06-07 11:30:07 +0200969 ret = 1;
970 break;
971#else
Jens Axboe80b9feb2006-06-07 10:34:49 +0200972 td->ioscheduler = strdup(tmpbuf);
Jens Axboeda867742006-06-06 10:39:10 -0700973 fgetpos(f, &off);
974 continue;
Jens Axboe22f78b32006-06-07 11:30:07 +0200975#endif
Jens Axboeda867742006-06-06 10:39:10 -0700976 }
Jens Axboeebac4652005-12-08 15:25:21 +0100977
Jens Axboe45410ac2006-06-07 11:10:39 +0200978 /*
979 * Don't break here, continue parsing options so we
980 * dump all the bad ones. Makes trial/error fixups
981 * easier on the user.
982 */
Jens Axboeebac4652005-12-08 15:25:21 +0100983 printf("Client%d: bad option %s\n",td->thread_number,p);
Jens Axboe45410ac2006-06-07 11:10:39 +0200984 ret = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100985 }
Jens Axboeebac4652005-12-08 15:25:21 +0100986
Jens Axboe45410ac2006-06-07 11:10:39 +0200987 if (!ret) {
988 fsetpos(f, &off);
989 ret = add_job(td, name, 0);
990 }
991 if (ret)
992 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100993 }
994
995 free(string);
996 free(name);
Jens Axboeef899b62006-06-06 09:31:00 +0200997 free(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100998 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200999 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001000}
1001
1002static int fill_def_thread(void)
1003{
1004 memset(&def_thread, 0, sizeof(def_thread));
1005
1006 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1007 perror("sched_getaffinity");
1008 return 1;
1009 }
1010
1011 /*
1012 * fill globals
1013 */
1014 def_thread.ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001015 def_thread.iomix = 0;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001016 def_thread.bs = DEF_BS;
Jens Axboeebac4652005-12-08 15:25:21 +01001017 def_thread.min_bs = -1;
1018 def_thread.max_bs = -1;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001019 def_thread.odirect = DEF_ODIRECT;
Jens Axboeebac4652005-12-08 15:25:21 +01001020 def_thread.ratecycle = DEF_RATE_CYCLE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001021 def_thread.sequential = DEF_SEQUENTIAL;
Jens Axboe972cfd22006-06-09 11:08:56 +02001022 def_thread.timeout = def_timeout;
Jens Axboeebac4652005-12-08 15:25:21 +01001023 def_thread.create_file = DEF_CREATE;
1024 def_thread.overwrite = DEF_OVERWRITE;
1025 def_thread.invalidate_cache = DEF_INVALIDATE;
1026 def_thread.sync_io = DEF_SYNCIO;
1027 def_thread.mem_type = MEM_MALLOC;
1028 def_thread.bw_avg_time = DEF_BWAVGTIME;
1029 def_thread.create_serialize = DEF_CREATE_SER;
1030 def_thread.create_fsync = DEF_CREATE_FSYNC;
1031 def_thread.loops = DEF_LOOPS;
1032 def_thread.verify = DEF_VERIFY;
1033 def_thread.stonewall = DEF_STONEWALL;
1034 def_thread.numjobs = DEF_NUMJOBS;
1035 def_thread.use_thread = DEF_USE_THREAD;
Jens Axboea6ccc7b2006-06-02 10:14:15 +02001036 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
1037 def_thread.rwmixread = DEF_RWMIX_READ;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001038 def_thread.nice = DEF_NICE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001039 def_thread.rand_repeatable = DEF_RAND_REPEAT;
Jens Axboe53cdc682006-10-18 11:50:58 +02001040 def_thread.nr_files = DEF_NR_FILES;
Jens Axboeebac4652005-12-08 15:25:21 +01001041#ifdef FIO_HAVE_DISK_UTIL
1042 def_thread.do_disk_util = 1;
1043#endif
1044
1045 return 0;
1046}
1047
Jens Axboe4785f992006-05-26 03:59:10 +02001048static void usage(char *name)
1049{
1050 printf("%s\n", fio_version_string);
Jens Axboe47623c72006-06-12 10:23:28 +02001051 printf("\t-o Write output to file\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001052 printf("\t-t Runtime in seconds\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001053 printf("\t-l Generate per-job latency logs\n");
1054 printf("\t-w Generate per-job bandwidth logs\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001055 printf("\t-m Minimal (terse) output\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001056 printf("\t-v Print version info and exit\n");
1057}
1058
Jens Axboe972cfd22006-06-09 11:08:56 +02001059static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +01001060{
Jens Axboe972cfd22006-06-09 11:08:56 +02001061 int c, idx = 1, ini_idx = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001062
Jens Axboe2e778412006-06-16 10:00:50 +02001063 while ((c = getopt(argc, argv, "t:o:lwvhm")) != EOF) {
Jens Axboeebac4652005-12-08 15:25:21 +01001064 switch (c) {
Jens Axboeebac4652005-12-08 15:25:21 +01001065 case 't':
Jens Axboe972cfd22006-06-09 11:08:56 +02001066 def_timeout = atoi(optarg);
Jens Axboe774a6172006-08-24 08:44:26 +02001067 idx = optind;
Jens Axboeebac4652005-12-08 15:25:21 +01001068 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001069 case 'l':
1070 write_lat_log = 1;
Jens Axboe774a6172006-08-24 08:44:26 +02001071 idx = optind;
Jens Axboeebac4652005-12-08 15:25:21 +01001072 break;
1073 case 'w':
1074 write_bw_log = 1;
Jens Axboe774a6172006-08-24 08:44:26 +02001075 idx = optind;
Jens Axboeebac4652005-12-08 15:25:21 +01001076 break;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001077 case 'o':
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001078 f_out = fopen(optarg, "w+");
1079 if (!f_out) {
1080 perror("fopen output");
1081 exit(1);
1082 }
1083 f_err = f_out;
Jens Axboe774a6172006-08-24 08:44:26 +02001084 idx = optind;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001085 break;
Jens Axboec6ae0a52006-06-12 11:04:44 +02001086 case 'm':
1087 terse_output = 1;
Jens Axboe774a6172006-08-24 08:44:26 +02001088 idx = optind;
Jens Axboec6ae0a52006-06-12 11:04:44 +02001089 break;
Jens Axboe4785f992006-05-26 03:59:10 +02001090 case 'h':
1091 usage(argv[0]);
1092 exit(0);
Jens Axboeebac4652005-12-08 15:25:21 +01001093 case 'v':
1094 printf("%s\n", fio_version_string);
1095 exit(0);
1096 }
1097 }
Jens Axboec9fad892006-05-27 17:26:50 +02001098
Jens Axboe972cfd22006-06-09 11:08:56 +02001099 while (idx < argc) {
1100 ini_idx++;
1101 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1102 ini_file[ini_idx - 1] = strdup(argv[idx]);
1103 idx++;
1104 }
Jens Axboe774a6172006-08-24 08:44:26 +02001105
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001106 if (!f_out) {
1107 f_out = stdout;
1108 f_err = stderr;
1109 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001110
1111 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +01001112}
1113
1114static void free_shm(void)
1115{
1116 struct shmid_ds sbuf;
1117
1118 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001119 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001120 threads = NULL;
1121 shmctl(shm_id, IPC_RMID, &sbuf);
1122 }
1123}
1124
Jens Axboe906c8d72006-06-13 09:37:56 +02001125/*
1126 * The thread area is shared between the main process and the job
1127 * threads/processes. So setup a shared memory segment that will hold
1128 * all the job info.
1129 */
Jens Axboeebac4652005-12-08 15:25:21 +01001130static int setup_thread_area(void)
1131{
1132 /*
1133 * 1024 is too much on some machines, scale max_jobs if
1134 * we get a failure that looks like too large a shm segment
1135 */
1136 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001137 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001138
Jens Axboe906c8d72006-06-13 09:37:56 +02001139 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001140 if (shm_id != -1)
1141 break;
1142 if (errno != EINVAL) {
1143 perror("shmget");
1144 break;
1145 }
1146
1147 max_jobs >>= 1;
1148 } while (max_jobs);
1149
1150 if (shm_id == -1)
1151 return 1;
1152
1153 threads = shmat(shm_id, NULL, 0);
1154 if (threads == (void *) -1) {
1155 perror("shmat");
1156 return 1;
1157 }
1158
1159 atexit(free_shm);
1160 return 0;
1161}
1162
1163int parse_options(int argc, char *argv[])
1164{
Jens Axboe972cfd22006-06-09 11:08:56 +02001165 int job_files, i;
1166
Jens Axboeebac4652005-12-08 15:25:21 +01001167 if (setup_thread_area())
1168 return 1;
1169 if (fill_def_thread())
1170 return 1;
1171
Jens Axboe972cfd22006-06-09 11:08:56 +02001172 job_files = parse_cmd_line(argc, argv);
1173 if (!job_files) {
1174 log_err("Need job file(s)\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001175 usage(argv[0]);
Jens Axboeebac4652005-12-08 15:25:21 +01001176 return 1;
1177 }
1178
Jens Axboe972cfd22006-06-09 11:08:56 +02001179 for (i = 0; i < job_files; i++) {
1180 if (fill_def_thread())
1181 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001182 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001183 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001184 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001185 }
Jens Axboeebac4652005-12-08 15:25:21 +01001186
Jens Axboe88c6ed82006-06-09 11:28:10 +02001187 free(ini_file);
Jens Axboeebac4652005-12-08 15:25:21 +01001188 return 0;
1189}