blob: 3eaf9b7f383bab8451d4c75b9d193772870cbe84 [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 Axboeebac4652005-12-08 15:25:21 +010049
Jens Axboe972cfd22006-06-09 11:08:56 +020050static int def_timeout = DEF_TIMEOUT;
Jens Axboe972cfd22006-06-09 11:08:56 +020051
Jens Axboef4866ec2006-06-13 09:38:36 +020052static char fio_version_string[] = "fio 1.5";
Jens Axboeebac4652005-12-08 15:25:21 +010053
Jens Axboe972cfd22006-06-09 11:08:56 +020054static char **ini_file;
Jens Axboeebac4652005-12-08 15:25:21 +010055static int max_jobs = MAX_JOBS;
56
57struct thread_data def_thread;
58struct thread_data *threads = NULL;
59
60int rate_quit = 0;
61int write_lat_log = 0;
62int write_bw_log = 0;
63int exitall_on_terminate = 0;
Jens Axboec6ae0a52006-06-12 11:04:44 +020064int terse_output = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +020065unsigned long long mlock_size = 0;
Jens Axboeeb8bbf42006-06-08 21:40:11 +020066FILE *f_out = NULL;
67FILE *f_err = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +010068
Jens Axboe906c8d72006-06-13 09:37:56 +020069/*
70 * Return a free job structure.
71 */
Jens Axboeebac4652005-12-08 15:25:21 +010072static struct thread_data *get_new_job(int global, struct thread_data *parent)
73{
74 struct thread_data *td;
75
76 if (global)
77 return &def_thread;
78 if (thread_number >= max_jobs)
79 return NULL;
80
81 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +020082 *td = *parent;
83 td->name[0] = '\0';
Jens Axboeebac4652005-12-08 15:25:21 +010084
85 td->fd = -1;
86 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;
106 int numjobs, ddir;
107
108#ifndef FIO_HAVE_LIBAIO
109 if (td->io_engine == FIO_LIBAIO) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200110 log_err("Linux libaio not available\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100111 return 1;
112 }
113#endif
114#ifndef FIO_HAVE_POSIXAIO
115 if (td->io_engine == FIO_POSIXAIO) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200116 log_err("posix aio not available\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100117 return 1;
118 }
119#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100120
121 /*
122 * the def_thread is just for options, it's not a real job
123 */
124 if (td == &def_thread)
125 return 0;
126
Jens Axboe2866c822006-10-09 15:57:48 +0200127 if (td->io_ops->flags & FIO_SYNCIO)
Jens Axboeebac4652005-12-08 15:25:21 +0100128 td->iodepth = 1;
129 else {
130 if (!td->iodepth)
131 td->iodepth = 1;
132 }
133
Jens Axboe20dc95c2005-12-09 10:29:35 +0100134 /*
135 * only really works for sequential io for now
136 */
137 if (td->zone_size && !td->sequential)
138 td->zone_size = 0;
139
Jens Axboe9cc935a2006-06-13 09:11:18 +0200140 /*
141 * Reads can do overwrites, we always need to pre-create the file
142 */
143 if (td_read(td) || td_rw(td))
144 td->overwrite = 1;
145
Jens Axboeebac4652005-12-08 15:25:21 +0100146 td->filetype = FIO_TYPE_FILE;
Jens Axboe0af7b542006-02-17 10:10:12 +0100147 if (!stat(jobname, &sb)) {
148 if (S_ISBLK(sb.st_mode))
149 td->filetype = FIO_TYPE_BD;
150 else if (S_ISCHR(sb.st_mode))
151 td->filetype = FIO_TYPE_CHAR;
152 }
Jens Axboeebac4652005-12-08 15:25:21 +0100153
154 if (td->filetype == FIO_TYPE_FILE) {
Jens Axboee9c047a2006-06-07 21:13:04 +0200155 char tmp[PATH_MAX];
156
Jens Axboeef899b62006-06-06 09:31:00 +0200157 if (td->directory && td->directory[0] != '\0')
Jens Axboef9481912006-06-09 11:37:28 +0200158 sprintf(tmp, "%s/%s.%d", td->directory, jobname, td->thread_number);
Jens Axboeebac4652005-12-08 15:25:21 +0100159 else
Jens Axboef9481912006-06-09 11:37:28 +0200160 sprintf(tmp, "%s.%d", jobname, td->thread_number);
Jens Axboee9c047a2006-06-07 21:13:04 +0200161 td->file_name = strdup(tmp);
Jens Axboeebac4652005-12-08 15:25:21 +0100162 } else
Jens Axboee9c047a2006-06-07 21:13:04 +0200163 td->file_name = strdup(jobname);
Jens Axboeebac4652005-12-08 15:25:21 +0100164
Jens Axboebbfd6b02006-06-07 19:42:54 +0200165 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100166
167 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
168 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
169 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
170
171 if (td->min_bs == -1U)
172 td->min_bs = td->bs;
173 if (td->max_bs == -1U)
174 td->max_bs = td->bs;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200175 if (td_read(td) && !td_rw(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100176 td->verify = 0;
177
178 if (td->stonewall && td->thread_number > 1)
179 groupid++;
180
181 td->groupid = groupid;
182
183 if (setup_rate(td))
184 goto err;
185
186 if (write_lat_log) {
187 setup_log(&td->slat_log);
188 setup_log(&td->clat_log);
189 }
190 if (write_bw_log)
191 setup_log(&td->bw_log);
192
Jens Axboe01452052006-06-07 10:29:47 +0200193 if (td->name[0] == '\0')
194 snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
195
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200196 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
Jens Axboe75154842006-06-01 13:56:09 +0200197
Jens Axboec6ae0a52006-06-12 11:04:44 +0200198 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200199 if (!job_add_num) {
Jens Axboe2866c822006-10-09 15:57:48 +0200200 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200201 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
202 else
Jens Axboe2866c822006-10-09 15:57:48 +0200203 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 +0200204 } else if (job_add_num == 1)
Jens Axboec6ae0a52006-06-12 11:04:44 +0200205 fprintf(f_out, "...\n");
206 }
Jens Axboeebac4652005-12-08 15:25:21 +0100207
208 /*
209 * recurse add identical jobs, clear numjobs and stonewall options
210 * as they don't apply to sub-jobs
211 */
212 numjobs = td->numjobs;
213 while (--numjobs) {
214 struct thread_data *td_new = get_new_job(0, td);
215
216 if (!td_new)
217 goto err;
218
219 td_new->numjobs = 1;
220 td_new->stonewall = 0;
Jens Axboe75154842006-06-01 13:56:09 +0200221 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100222
Jens Axboe75154842006-06-01 13:56:09 +0200223 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100224 goto err;
225 }
226 return 0;
227err:
228 put_job(td);
229 return -1;
230}
231
Jens Axboe906c8d72006-06-13 09:37:56 +0200232/*
233 * Initialize the various random states we need (random io, block size ranges,
234 * read/write mix, etc).
235 */
Jens Axboeebac4652005-12-08 15:25:21 +0100236int init_random_state(struct thread_data *td)
237{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200238 unsigned long seeds[4];
Jens Axboeebac4652005-12-08 15:25:21 +0100239 int fd, num_maps, blocks;
240
Jens Axboe1ac267b2006-05-31 20:29:00 +0200241 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100242 if (fd == -1) {
243 td_verror(td, errno);
244 return 1;
245 }
246
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200247 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100248 td_verror(td, EIO);
249 close(fd);
250 return 1;
251 }
252
253 close(fd);
254
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200255 os_random_seed(seeds[0], &td->bsrange_state);
256 os_random_seed(seeds[1], &td->verify_state);
257 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100258
259 if (td->sequential)
260 return 0;
261
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200262 if (td->rand_repeatable)
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200263 seeds[3] = DEF_RANDSEED;
Jens Axboeebac4652005-12-08 15:25:21 +0100264
265 blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
266 num_maps = blocks / BLOCKS_PER_MAP;
267 td->file_map = malloc(num_maps * sizeof(long));
268 td->num_maps = num_maps;
269 memset(td->file_map, 0, num_maps * sizeof(long));
270
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200271 os_random_seed(seeds[3], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100272 return 0;
273}
274
275static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
276{
277#ifdef FIO_HAVE_CPU_AFFINITY
278 unsigned int i;
279
280 CPU_ZERO(&cpumask);
281
282 for (i = 0; i < sizeof(int) * 8; i++) {
283 if ((1 << i) & cpu)
284 CPU_SET(i, &cpumask);
285 }
286#endif
287}
288
Jens Axboe906c8d72006-06-13 09:37:56 +0200289static unsigned long get_mult_time(char c)
290{
291 switch (c) {
292 case 'm':
293 case 'M':
294 return 60;
295 case 'h':
296 case 'H':
297 return 60 * 60;
298 case 'd':
299 case 'D':
300 return 24 * 60 * 60;
301 default:
302 return 1;
303 }
304}
305
306static unsigned long get_mult_bytes(char c)
Jens Axboeebac4652005-12-08 15:25:21 +0100307{
308 switch (c) {
309 case 'k':
310 case 'K':
311 return 1024;
312 case 'm':
313 case 'M':
314 return 1024 * 1024;
315 case 'g':
316 case 'G':
317 return 1024 * 1024 * 1024;
318 default:
319 return 1;
320 }
321}
322
323/*
324 * convert string after '=' into decimal value, noting any size suffix
325 */
Jens Axboe906c8d72006-06-13 09:37:56 +0200326static int str_to_decimal(char *p, unsigned long long *val, int kilo)
Jens Axboeebac4652005-12-08 15:25:21 +0100327{
328 char *str;
329 int len;
330
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100331 str = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100332 if (!str)
333 return 1;
334
335 str++;
336 len = strlen(str);
337
338 *val = strtoul(str, NULL, 10);
339 if (*val == ULONG_MAX && errno == ERANGE)
340 return 1;
341
Jens Axboe906c8d72006-06-13 09:37:56 +0200342 if (kilo)
343 *val *= get_mult_bytes(str[len - 1]);
344 else
345 *val *= get_mult_time(str[len - 1]);
Jens Axboeebac4652005-12-08 15:25:21 +0100346 return 0;
347}
348
Jens Axboe906c8d72006-06-13 09:37:56 +0200349static int check_str_bytes(char *p, char *name, unsigned long long *val)
Jens Axboeebac4652005-12-08 15:25:21 +0100350{
Jens Axboe20dc95c2005-12-09 10:29:35 +0100351 if (strncmp(p, name, strlen(name) - 1))
Jens Axboeebac4652005-12-08 15:25:21 +0100352 return 1;
353
Jens Axboe906c8d72006-06-13 09:37:56 +0200354 return str_to_decimal(p, val, 1);
355}
356
357static int check_str_time(char *p, char *name, unsigned long long *val)
358{
359 if (strncmp(p, name, strlen(name) - 1))
360 return 1;
361
362 return str_to_decimal(p, val, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100363}
364
365static void strip_blank_front(char **p)
366{
367 char *s = *p;
368
Jens Axboe4ae3f762006-05-30 13:35:14 +0200369 while (isspace(*s))
Jens Axboeebac4652005-12-08 15:25:21 +0100370 s++;
371}
372
373static void strip_blank_end(char *p)
374{
Jens Axboe4ae3f762006-05-30 13:35:14 +0200375 char *s = p + strlen(p) - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100376
Jens Axboe4ae3f762006-05-30 13:35:14 +0200377 while (isspace(*s) || iscntrl(*s))
378 s--;
Jens Axboeaea47d42006-05-26 19:27:29 +0200379
Jens Axboe4ae3f762006-05-30 13:35:14 +0200380 *(s + 1) = '\0';
Jens Axboeaea47d42006-05-26 19:27:29 +0200381}
382
Jens Axboeebac4652005-12-08 15:25:21 +0100383typedef int (str_cb_fn)(struct thread_data *, char *);
384
385static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
386{
Jens Axboe843a7412006-06-01 21:14:21 -0700387 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100388
Jens Axboe843a7412006-06-01 21:14:21 -0700389 if (strncmp(p, name, strlen(name)))
390 return 1;
391
392 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100393 if (!s)
394 return 1;
395
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100396 s = strchr(s, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100397 if (!s)
398 return 1;
399
400 s++;
401 strip_blank_front(&s);
402 return cb(td, s);
403}
404
405static int check_strstore(char *p, char *name, char *dest)
406{
Jens Axboe843a7412006-06-01 21:14:21 -0700407 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100408
Jens Axboe843a7412006-06-01 21:14:21 -0700409 if (strncmp(p, name, strlen(name)))
410 return 1;
411
412 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100413 if (!s)
414 return 1;
415
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100416 s = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100417 if (!s)
418 return 1;
419
420 s++;
421 strip_blank_front(&s);
422
423 strcpy(dest, s);
Jens Axboeebac4652005-12-08 15:25:21 +0100424 return 0;
425}
426
Jens Axboe906c8d72006-06-13 09:37:56 +0200427static int __check_range_bytes(char *str, unsigned long *val)
Jens Axboeebac4652005-12-08 15:25:21 +0100428{
Jens Axboe01617be2005-12-08 19:50:40 +0100429 char suffix;
Jens Axboeebac4652005-12-08 15:25:21 +0100430
Jens Axboe01617be2005-12-08 19:50:40 +0100431 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
Jens Axboe906c8d72006-06-13 09:37:56 +0200432 *val *= get_mult_bytes(suffix);
Jens Axboeebac4652005-12-08 15:25:21 +0100433 return 0;
434 }
435
Jens Axboe01617be2005-12-08 19:50:40 +0100436 if (sscanf(str, "%lu", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100437 return 0;
438
439 return 1;
Jens Axboe01617be2005-12-08 19:50:40 +0100440}
Jens Axboeebac4652005-12-08 15:25:21 +0100441
Jens Axboe906c8d72006-06-13 09:37:56 +0200442static int check_range_bytes(char *p, char *name, unsigned long *s,
443 unsigned long *e)
Jens Axboe01617be2005-12-08 19:50:40 +0100444{
445 char option[128];
446 char *str, *p1, *p2;
447
Jens Axboe843a7412006-06-01 21:14:21 -0700448 if (strncmp(p, name, strlen(name)))
449 return 1;
450
Jens Axboe01617be2005-12-08 19:50:40 +0100451 strcpy(option, p);
452 p = option;
453
454 str = strstr(p, name);
455 if (!str)
456 return 1;
457
458 p += strlen(name);
459
460 str = strchr(p, '=');
461 if (!str)
462 return 1;
463
464 /*
465 * 'p' now holds whatever is after the '=' sign
466 */
467 p1 = str + 1;
468
469 /*
470 * terminate p1 at the '-' sign
471 */
472 p = strchr(p1, '-');
473 if (!p)
474 return 1;
475
476 p2 = p + 1;
477 *p = '\0';
478
Jens Axboe906c8d72006-06-13 09:37:56 +0200479 if (!__check_range_bytes(p1, s) && !__check_range_bytes(p2, e))
Jens Axboe01617be2005-12-08 19:50:40 +0100480 return 0;
481
482 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100483}
484
485static int check_int(char *p, char *name, unsigned int *val)
486{
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100487 char *str;
Jens Axboeebac4652005-12-08 15:25:21 +0100488
Jens Axboeb6754f92006-05-30 14:29:32 +0200489 if (strncmp(p, name, strlen(name)))
490 return 1;
491
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100492 str = strstr(p, name);
493 if (!str)
494 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100495
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100496 str = strchr(p, '=');
497 if (!str)
498 return 1;
499
500 str++;
501
502 if (sscanf(str, "%u", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100503 return 0;
504
505 return 1;
506}
507
508static int check_strset(char *p, char *name)
509{
510 return strncmp(p, name, strlen(name));
511}
512
513static int is_empty_or_comment(char *line)
514{
515 unsigned int i;
516
517 for (i = 0; i < strlen(line); i++) {
518 if (line[i] == ';')
519 return 1;
520 if (!isspace(line[i]) && !iscntrl(line[i]))
521 return 0;
522 }
523
524 return 1;
525}
526
527static int str_rw_cb(struct thread_data *td, char *mem)
528{
529 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
530 td->ddir = DDIR_READ;
531 td->sequential = 1;
532 return 0;
533 } else if (!strncmp(mem, "randread", 8)) {
534 td->ddir = DDIR_READ;
535 td->sequential = 0;
536 return 0;
537 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
538 td->ddir = DDIR_WRITE;
539 td->sequential = 1;
540 return 0;
541 } else if (!strncmp(mem, "randwrite", 9)) {
542 td->ddir = DDIR_WRITE;
543 td->sequential = 0;
544 return 0;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200545 } else if (!strncmp(mem, "rw", 2)) {
546 td->ddir = 0;
547 td->iomix = 1;
548 td->sequential = 1;
549 return 0;
550 } else if (!strncmp(mem, "randrw", 6)) {
551 td->ddir = 0;
552 td->iomix = 1;
553 td->sequential = 0;
554 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100555 }
556
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200557 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100558 return 1;
559}
560
561static int str_verify_cb(struct thread_data *td, char *mem)
562{
563 if (!strncmp(mem, "0", 1)) {
564 td->verify = VERIFY_NONE;
565 return 0;
566 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
567 td->verify = VERIFY_MD5;
568 return 0;
569 } else if (!strncmp(mem, "crc32", 5)) {
570 td->verify = VERIFY_CRC32;
571 return 0;
572 }
573
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200574 log_err("fio: verify types: md5, crc32\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100575 return 1;
576}
577
578static int str_mem_cb(struct thread_data *td, char *mem)
579{
580 if (!strncmp(mem, "malloc", 6)) {
581 td->mem_type = MEM_MALLOC;
582 return 0;
583 } else if (!strncmp(mem, "shm", 3)) {
584 td->mem_type = MEM_SHM;
585 return 0;
586 } else if (!strncmp(mem, "mmap", 4)) {
587 td->mem_type = MEM_MMAP;
588 return 0;
589 }
590
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200591 log_err("fio: mem type: malloc, shm, mmap\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100592 return 1;
593}
594
595static int str_ioengine_cb(struct thread_data *td, char *str)
596{
Jens Axboe2866c822006-10-09 15:57:48 +0200597 if (!str)
598 str = DEF_IO_ENGINE_NAME;
599
600 td->io_ops = load_ioengine(td, str);
601 if (td->io_ops)
Jens Axboeebac4652005-12-08 15:25:21 +0100602 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100603
Jens Axboeb990b5c2006-09-14 09:48:22 +0200604 log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice, cpu\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100605 return 1;
606}
607
Jens Axboe07261982006-06-07 10:51:12 +0200608/*
609 * This is our [ini] type file parser.
610 */
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200611int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100612{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200613 unsigned int prioclass, prio, cpu, global, il;
Jens Axboeebac4652005-12-08 15:25:21 +0100614 unsigned long long ull;
615 unsigned long ul1, ul2;
616 struct thread_data *td;
Jens Axboeef899b62006-06-06 09:31:00 +0200617 char *string, *name, *tmpbuf;
Jens Axboeebac4652005-12-08 15:25:21 +0100618 fpos_t off;
619 FILE *f;
620 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200621 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +0100622
623 f = fopen(file, "r");
624 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200625 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100626 return 1;
627 }
628
629 string = malloc(4096);
630 name = malloc(256);
Jens Axboeef899b62006-06-06 09:31:00 +0200631 tmpbuf = malloc(4096);
Jens Axboeebac4652005-12-08 15:25:21 +0100632
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200633 stonewall = stonewall_flag;
Jens Axboeebac4652005-12-08 15:25:21 +0100634 while ((p = fgets(string, 4096, f)) != NULL) {
Jens Axboe45410ac2006-06-07 11:10:39 +0200635 if (ret)
636 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100637 if (is_empty_or_comment(p))
638 continue;
639 if (sscanf(p, "[%s]", name) != 1)
640 continue;
641
642 global = !strncmp(name, "global", 6);
643
644 name[strlen(name) - 1] = '\0';
645
646 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200647 if (!td) {
648 ret = 1;
649 break;
650 }
Jens Axboeebac4652005-12-08 15:25:21 +0100651
Jens Axboe972cfd22006-06-09 11:08:56 +0200652 /*
653 * Seperate multiple job files by a stonewall
654 */
Jens Axboef9481912006-06-09 11:37:28 +0200655 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +0200656 td->stonewall = stonewall;
657 stonewall = 0;
658 }
659
Jens Axboeebac4652005-12-08 15:25:21 +0100660 fgetpos(f, &off);
661 while ((p = fgets(string, 4096, f)) != NULL) {
662 if (is_empty_or_comment(p))
663 continue;
664 if (strstr(p, "["))
665 break;
Jens Axboeb6754f92006-05-30 14:29:32 +0200666 strip_blank_front(&p);
Jens Axboe4ae3f762006-05-30 13:35:14 +0200667 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200668
Jens Axboeebac4652005-12-08 15:25:21 +0100669 if (!check_int(p, "prio", &prio)) {
670#ifndef FIO_HAVE_IOPRIO
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200671 log_err("io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200672 ret = 1;
673 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100674#endif
Jens Axboe8756e4d2006-05-27 20:24:53 +0200675 td->ioprio |= prio;
Jens Axboeebac4652005-12-08 15:25:21 +0100676 fgetpos(f, &off);
677 continue;
678 }
679 if (!check_int(p, "prioclass", &prioclass)) {
680#ifndef FIO_HAVE_IOPRIO
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200681 log_err("io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200682 ret = 1;
683 break;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200684#else
Jens Axboe8756e4d2006-05-27 20:24:53 +0200685 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
Jens Axboeebac4652005-12-08 15:25:21 +0100686 fgetpos(f, &off);
687 continue;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200688#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100689 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200690 if (!check_int(p, "direct", &il)) {
691 td->odirect = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100692 fgetpos(f, &off);
693 continue;
694 }
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200695 if (!check_int(p, "rand_repeatable", &il)) {
696 td->rand_repeatable = il;
697 fgetpos(f, &off);
698 continue;
699 }
Jens Axboeebac4652005-12-08 15:25:21 +0100700 if (!check_int(p, "rate", &td->rate)) {
701 fgetpos(f, &off);
702 continue;
703 }
704 if (!check_int(p, "ratemin", &td->ratemin)) {
705 fgetpos(f, &off);
706 continue;
707 }
708 if (!check_int(p, "ratecycle", &td->ratecycle)) {
709 fgetpos(f, &off);
710 continue;
711 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200712 if (!check_int(p, "cpuload", &td->cpuload)) {
713 fgetpos(f, &off);
714 continue;
715 }
716 if (!check_int(p, "cpuchunks", &td->cpucycle)) {
717 fgetpos(f, &off);
718 continue;
719 }
Jens Axboeebac4652005-12-08 15:25:21 +0100720 if (!check_int(p, "thinktime", &td->thinktime)) {
721 fgetpos(f, &off);
722 continue;
723 }
724 if (!check_int(p, "cpumask", &cpu)) {
725#ifndef FIO_HAVE_CPU_AFFINITY
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200726 log_err("cpu affinity not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200727 ret = 1;
728 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100729#endif
730 fill_cpu_mask(td->cpumask, cpu);
731 fgetpos(f, &off);
732 continue;
733 }
734 if (!check_int(p, "fsync", &td->fsync_blocks)) {
735 fgetpos(f, &off);
Jens Axboefc1a4712006-05-30 13:04:05 +0200736 td->end_fsync = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100737 continue;
738 }
739 if (!check_int(p, "startdelay", &td->start_delay)) {
740 fgetpos(f, &off);
741 continue;
742 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200743 if (!check_str_time(p, "timeout", &ull)) {
Jens Axboeaf959a72006-07-13 14:36:02 -0700744 td->timeout = ull;
Jens Axboeebac4652005-12-08 15:25:21 +0100745 fgetpos(f, &off);
746 continue;
747 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200748 if (!check_int(p, "invalidate", &il)) {
749 td->invalidate_cache = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100750 fgetpos(f, &off);
751 continue;
752 }
753 if (!check_int(p, "iodepth", &td->iodepth)) {
754 fgetpos(f, &off);
755 continue;
756 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200757 if (!check_int(p, "sync", &il)) {
758 td->sync_io = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100759 fgetpos(f, &off);
760 continue;
761 }
762 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
763 fgetpos(f, &off);
764 continue;
765 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200766 if (!check_int(p, "create_serialize", &il)) {
767 td->create_serialize = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100768 fgetpos(f, &off);
769 continue;
770 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200771 if (!check_int(p, "create_fsync", &il)) {
772 td->create_fsync = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100773 fgetpos(f, &off);
774 continue;
775 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200776 if (!check_int(p, "end_fsync", &il)) {
777 td->end_fsync = il;
Jens Axboefc1a4712006-05-30 13:04:05 +0200778 fgetpos(f, &off);
779 continue;
780 }
Jens Axboeebac4652005-12-08 15:25:21 +0100781 if (!check_int(p, "loops", &td->loops)) {
782 fgetpos(f, &off);
783 continue;
784 }
785 if (!check_int(p, "numjobs", &td->numjobs)) {
786 fgetpos(f, &off);
787 continue;
788 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200789 if (!check_int(p, "overwrite", &il)) {
790 td->overwrite = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100791 fgetpos(f, &off);
792 continue;
793 }
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200794 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
795 fgetpos(f, &off);
796 continue;
797 }
798 if (!check_int(p, "rwmixread", &il)) {
799 if (il > 100)
800 il = 100;
801 td->rwmixread = il;
802 fgetpos(f, &off);
803 continue;
804 }
805 if (!check_int(p, "rwmixwrite", &il)) {
806 if (il > 100)
807 il = 100;
808 td->rwmixread = 100 - il;
809 fgetpos(f, &off);
810 continue;
811 }
Jens Axboeb6f4d882006-06-02 10:32:51 +0200812 if (!check_int(p, "nice", &td->nice)) {
813 fgetpos(f, &off);
814 continue;
815 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200816 if (!check_range_bytes(p, "bsrange", &ul1, &ul2)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100817 if (ul1 > ul2) {
818 td->max_bs = ul1;
819 td->min_bs = ul2;
820 } else {
821 td->max_bs = ul2;
822 td->min_bs = ul1;
823 }
824 fgetpos(f, &off);
825 continue;
826 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200827 if (!check_str_bytes(p, "bs", &ull)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100828 td->bs = ull;
829 fgetpos(f, &off);
830 continue;
831 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200832 if (!check_str_bytes(p, "size", &td->file_size)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100833 fgetpos(f, &off);
834 continue;
835 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200836 if (!check_str_bytes(p, "offset", &td->file_offset)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100837 fgetpos(f, &off);
838 continue;
839 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200840 if (!check_str_bytes(p, "zonesize", &td->zone_size)) {
Jens Axboe20dc95c2005-12-09 10:29:35 +0100841 fgetpos(f, &off);
842 continue;
843 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200844 if (!check_str_bytes(p, "zoneskip", &td->zone_skip)) {
Jens Axboe20dc95c2005-12-09 10:29:35 +0100845 fgetpos(f, &off);
846 continue;
847 }
Jens Axboe906c8d72006-06-13 09:37:56 +0200848 if (!check_str_bytes(p, "lockmem", &mlock_size)) {
Jens Axboec04f7ec2006-05-31 10:13:16 +0200849 fgetpos(f, &off);
850 continue;
851 }
Jens Axboeef899b62006-06-06 09:31:00 +0200852 if (!check_strstore(p, "directory", tmpbuf)) {
853 td->directory = strdup(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100854 fgetpos(f, &off);
855 continue;
856 }
Jens Axboe01452052006-06-07 10:29:47 +0200857 if (!check_strstore(p, "name", tmpbuf)) {
858 snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
859 fgetpos(f, &off);
860 continue;
861 }
Jens Axboeebac4652005-12-08 15:25:21 +0100862 if (!check_str(p, "mem", str_mem_cb, td)) {
863 fgetpos(f, &off);
864 continue;
865 }
866 if (!check_str(p, "verify", str_verify_cb, td)) {
867 fgetpos(f, &off);
868 continue;
869 }
870 if (!check_str(p, "rw", str_rw_cb, td)) {
871 fgetpos(f, &off);
872 continue;
873 }
874 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
875 fgetpos(f, &off);
876 continue;
877 }
878 if (!check_strset(p, "create")) {
879 td->create_file = 1;
880 fgetpos(f, &off);
881 continue;
882 }
883 if (!check_strset(p, "exitall")) {
884 exitall_on_terminate = 1;
885 fgetpos(f, &off);
886 continue;
887 }
888 if (!check_strset(p, "stonewall")) {
889 td->stonewall = 1;
890 fgetpos(f, &off);
891 continue;
892 }
893 if (!check_strset(p, "thread")) {
894 td->use_thread = 1;
895 fgetpos(f, &off);
896 continue;
897 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200898 if (!check_strstore(p, "iolog", tmpbuf)) {
Jens Axboe4f693b92006-06-07 22:52:28 +0200899 if (td->write_iolog) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200900 log_err("fio: read iolog overrides given write_iolog\n");
Jens Axboeaf8c0b42006-06-07 22:33:17 +0200901 free(td->iolog_file);
Jens Axboe4f693b92006-06-07 22:52:28 +0200902 td->write_iolog = 0;
903 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200904 td->iolog_file = strdup(tmpbuf);
Jens Axboe843a7412006-06-01 21:14:21 -0700905 td->read_iolog = 1;
Jens Axboe843a7412006-06-01 21:14:21 -0700906 fgetpos(f, &off);
907 continue;
908 }
Jens Axboeaf8c0b42006-06-07 22:33:17 +0200909 if (!check_strstore(p, "write_iolog", tmpbuf)) {
910 if (!td->read_iolog) {
911 td->iolog_file = strdup(tmpbuf);
912 td->write_iolog = 1;
Jens Axboe4f693b92006-06-07 22:52:28 +0200913 } else
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200914 log_err("fio: read iolog overrides given write_iolog\n");
Jens Axboeaea47d42006-05-26 19:27:29 +0200915 fgetpos(f, &off);
916 continue;
917 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200918 if (!check_strstore(p, "exec_prerun", tmpbuf)) {
919 td->exec_prerun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200920 fgetpos(f, &off);
921 continue;
922 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200923 if (!check_strstore(p, "exec_postrun", tmpbuf)) {
924 td->exec_postrun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200925 fgetpos(f, &off);
926 continue;
927 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200928 if (!check_strstore(p, "ioscheduler", tmpbuf)) {
Jens Axboe22f78b32006-06-07 11:30:07 +0200929#ifndef FIO_HAVE_IOSCHED_SWITCH
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200930 log_err("io scheduler switching not available\n");
Jens Axboe22f78b32006-06-07 11:30:07 +0200931 ret = 1;
932 break;
933#else
Jens Axboe80b9feb2006-06-07 10:34:49 +0200934 td->ioscheduler = strdup(tmpbuf);
Jens Axboeda867742006-06-06 10:39:10 -0700935 fgetpos(f, &off);
936 continue;
Jens Axboe22f78b32006-06-07 11:30:07 +0200937#endif
Jens Axboeda867742006-06-06 10:39:10 -0700938 }
Jens Axboeebac4652005-12-08 15:25:21 +0100939
Jens Axboe45410ac2006-06-07 11:10:39 +0200940 /*
941 * Don't break here, continue parsing options so we
942 * dump all the bad ones. Makes trial/error fixups
943 * easier on the user.
944 */
Jens Axboeebac4652005-12-08 15:25:21 +0100945 printf("Client%d: bad option %s\n",td->thread_number,p);
Jens Axboe45410ac2006-06-07 11:10:39 +0200946 ret = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100947 }
Jens Axboeebac4652005-12-08 15:25:21 +0100948
Jens Axboe45410ac2006-06-07 11:10:39 +0200949 if (!ret) {
950 fsetpos(f, &off);
951 ret = add_job(td, name, 0);
952 }
953 if (ret)
954 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100955 }
956
957 free(string);
958 free(name);
Jens Axboeef899b62006-06-06 09:31:00 +0200959 free(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100960 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200961 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100962}
963
964static int fill_def_thread(void)
965{
966 memset(&def_thread, 0, sizeof(def_thread));
967
968 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
969 perror("sched_getaffinity");
970 return 1;
971 }
972
973 /*
974 * fill globals
975 */
976 def_thread.ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200977 def_thread.iomix = 0;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200978 def_thread.bs = DEF_BS;
Jens Axboeebac4652005-12-08 15:25:21 +0100979 def_thread.min_bs = -1;
980 def_thread.max_bs = -1;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200981 def_thread.odirect = DEF_ODIRECT;
Jens Axboeebac4652005-12-08 15:25:21 +0100982 def_thread.ratecycle = DEF_RATE_CYCLE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200983 def_thread.sequential = DEF_SEQUENTIAL;
Jens Axboe972cfd22006-06-09 11:08:56 +0200984 def_thread.timeout = def_timeout;
Jens Axboeebac4652005-12-08 15:25:21 +0100985 def_thread.create_file = DEF_CREATE;
986 def_thread.overwrite = DEF_OVERWRITE;
987 def_thread.invalidate_cache = DEF_INVALIDATE;
988 def_thread.sync_io = DEF_SYNCIO;
989 def_thread.mem_type = MEM_MALLOC;
990 def_thread.bw_avg_time = DEF_BWAVGTIME;
991 def_thread.create_serialize = DEF_CREATE_SER;
992 def_thread.create_fsync = DEF_CREATE_FSYNC;
993 def_thread.loops = DEF_LOOPS;
994 def_thread.verify = DEF_VERIFY;
995 def_thread.stonewall = DEF_STONEWALL;
996 def_thread.numjobs = DEF_NUMJOBS;
997 def_thread.use_thread = DEF_USE_THREAD;
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200998 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
999 def_thread.rwmixread = DEF_RWMIX_READ;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001000 def_thread.nice = DEF_NICE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001001 def_thread.rand_repeatable = DEF_RAND_REPEAT;
Jens Axboeebac4652005-12-08 15:25:21 +01001002#ifdef FIO_HAVE_DISK_UTIL
1003 def_thread.do_disk_util = 1;
1004#endif
1005
1006 return 0;
1007}
1008
Jens Axboe4785f992006-05-26 03:59:10 +02001009static void usage(char *name)
1010{
1011 printf("%s\n", fio_version_string);
Jens Axboe47623c72006-06-12 10:23:28 +02001012 printf("\t-o Write output to file\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001013 printf("\t-t Runtime in seconds\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001014 printf("\t-l Generate per-job latency logs\n");
1015 printf("\t-w Generate per-job bandwidth logs\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001016 printf("\t-m Minimal (terse) output\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001017 printf("\t-v Print version info and exit\n");
1018}
1019
Jens Axboe972cfd22006-06-09 11:08:56 +02001020static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +01001021{
Jens Axboe972cfd22006-06-09 11:08:56 +02001022 int c, idx = 1, ini_idx = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001023
Jens Axboe2e778412006-06-16 10:00:50 +02001024 while ((c = getopt(argc, argv, "t:o:lwvhm")) != EOF) {
Jens Axboeebac4652005-12-08 15:25:21 +01001025 switch (c) {
Jens Axboeebac4652005-12-08 15:25:21 +01001026 case 't':
Jens Axboe972cfd22006-06-09 11:08:56 +02001027 def_timeout = atoi(optarg);
Jens Axboe774a6172006-08-24 08:44:26 +02001028 idx = optind;
Jens Axboeebac4652005-12-08 15:25:21 +01001029 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001030 case 'l':
1031 write_lat_log = 1;
Jens Axboe774a6172006-08-24 08:44:26 +02001032 idx = optind;
Jens Axboeebac4652005-12-08 15:25:21 +01001033 break;
1034 case 'w':
1035 write_bw_log = 1;
Jens Axboe774a6172006-08-24 08:44:26 +02001036 idx = optind;
Jens Axboeebac4652005-12-08 15:25:21 +01001037 break;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001038 case 'o':
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001039 f_out = fopen(optarg, "w+");
1040 if (!f_out) {
1041 perror("fopen output");
1042 exit(1);
1043 }
1044 f_err = f_out;
Jens Axboe774a6172006-08-24 08:44:26 +02001045 idx = optind;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001046 break;
Jens Axboec6ae0a52006-06-12 11:04:44 +02001047 case 'm':
1048 terse_output = 1;
Jens Axboe774a6172006-08-24 08:44:26 +02001049 idx = optind;
Jens Axboec6ae0a52006-06-12 11:04:44 +02001050 break;
Jens Axboe4785f992006-05-26 03:59:10 +02001051 case 'h':
1052 usage(argv[0]);
1053 exit(0);
Jens Axboeebac4652005-12-08 15:25:21 +01001054 case 'v':
1055 printf("%s\n", fio_version_string);
1056 exit(0);
1057 }
1058 }
Jens Axboec9fad892006-05-27 17:26:50 +02001059
Jens Axboe972cfd22006-06-09 11:08:56 +02001060 while (idx < argc) {
1061 ini_idx++;
1062 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1063 ini_file[ini_idx - 1] = strdup(argv[idx]);
1064 idx++;
1065 }
Jens Axboe774a6172006-08-24 08:44:26 +02001066
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001067 if (!f_out) {
1068 f_out = stdout;
1069 f_err = stderr;
1070 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001071
1072 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +01001073}
1074
1075static void free_shm(void)
1076{
1077 struct shmid_ds sbuf;
1078
1079 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001080 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001081 threads = NULL;
1082 shmctl(shm_id, IPC_RMID, &sbuf);
1083 }
1084}
1085
Jens Axboe906c8d72006-06-13 09:37:56 +02001086/*
1087 * The thread area is shared between the main process and the job
1088 * threads/processes. So setup a shared memory segment that will hold
1089 * all the job info.
1090 */
Jens Axboeebac4652005-12-08 15:25:21 +01001091static int setup_thread_area(void)
1092{
1093 /*
1094 * 1024 is too much on some machines, scale max_jobs if
1095 * we get a failure that looks like too large a shm segment
1096 */
1097 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001098 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001099
Jens Axboe906c8d72006-06-13 09:37:56 +02001100 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001101 if (shm_id != -1)
1102 break;
1103 if (errno != EINVAL) {
1104 perror("shmget");
1105 break;
1106 }
1107
1108 max_jobs >>= 1;
1109 } while (max_jobs);
1110
1111 if (shm_id == -1)
1112 return 1;
1113
1114 threads = shmat(shm_id, NULL, 0);
1115 if (threads == (void *) -1) {
1116 perror("shmat");
1117 return 1;
1118 }
1119
1120 atexit(free_shm);
1121 return 0;
1122}
1123
1124int parse_options(int argc, char *argv[])
1125{
Jens Axboe972cfd22006-06-09 11:08:56 +02001126 int job_files, i;
1127
Jens Axboeebac4652005-12-08 15:25:21 +01001128 if (setup_thread_area())
1129 return 1;
1130 if (fill_def_thread())
1131 return 1;
1132
Jens Axboe972cfd22006-06-09 11:08:56 +02001133 job_files = parse_cmd_line(argc, argv);
1134 if (!job_files) {
1135 log_err("Need job file(s)\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001136 usage(argv[0]);
Jens Axboeebac4652005-12-08 15:25:21 +01001137 return 1;
1138 }
1139
Jens Axboe972cfd22006-06-09 11:08:56 +02001140 for (i = 0; i < job_files; i++) {
1141 if (fill_def_thread())
1142 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001143 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001144 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001145 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001146 }
Jens Axboeebac4652005-12-08 15:25:21 +01001147
Jens Axboe88c6ed82006-06-09 11:28:10 +02001148 free(ini_file);
Jens Axboeebac4652005-12-08 15:25:21 +01001149 return 0;
1150}