blob: 40fcf1c35f61a5426648ee19dcfa9f680147bcf1 [file] [log] [blame]
Jens Axboeebac4652005-12-08 15:25:21 +01001#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <fcntl.h>
5#include <ctype.h>
6#include <string.h>
7#include <errno.h>
8#include <sys/ipc.h>
9#include <sys/shm.h>
10#include <sys/types.h>
11#include <sys/stat.h>
12
13#include "fio.h"
14
Jens Axboe20dc95c2005-12-09 10:29:35 +010015#define DEF_BS (4096)
16#define DEF_TIMEOUT (0)
17#define DEF_RATE_CYCLE (1000)
18#define DEF_ODIRECT (1)
19#define DEF_IO_ENGINE (FIO_SYNCIO)
Jens Axboeebac4652005-12-08 15:25:21 +010020#define DEF_IO_ENGINE_NAME "sync"
Jens Axboe20dc95c2005-12-09 10:29:35 +010021#define DEF_SEQUENTIAL (1)
22#define DEF_RAND_REPEAT (1)
23#define DEF_OVERWRITE (1)
24#define DEF_CREATE (1)
25#define DEF_INVALIDATE (1)
26#define DEF_SYNCIO (0)
27#define DEF_RANDSEED (0xb1899bedUL)
28#define DEF_BWAVGTIME (500)
29#define DEF_CREATE_SER (1)
Jens Axboeebac4652005-12-08 15:25:21 +010030#define DEF_CREATE_FSYNC (1)
Jens Axboe20dc95c2005-12-09 10:29:35 +010031#define DEF_LOOPS (1)
32#define DEF_VERIFY (0)
33#define DEF_STONEWALL (0)
34#define DEF_NUMJOBS (1)
35#define DEF_USE_THREAD (0)
36#define DEF_FILE_SIZE (1024 * 1024 * 1024UL)
37#define DEF_ZONE_SIZE (0)
38#define DEF_ZONE_SKIP (0)
Jens Axboea6ccc7b2006-06-02 10:14:15 +020039#define DEF_RWMIX_CYCLE (500)
40#define DEF_RWMIX_READ (50)
Jens Axboeb6f4d882006-06-02 10:32:51 +020041#define DEF_NICE (0)
Jens Axboeebac4652005-12-08 15:25:21 +010042
Jens Axboe972cfd22006-06-09 11:08:56 +020043static int def_timeout = DEF_TIMEOUT;
Jens Axboe972cfd22006-06-09 11:08:56 +020044
Jens Axboe573275e2006-06-05 12:20:20 +020045static char fio_version_string[] = "fio 1.4";
Jens Axboeebac4652005-12-08 15:25:21 +010046
Jens Axboe972cfd22006-06-09 11:08:56 +020047static char **ini_file;
Jens Axboeebac4652005-12-08 15:25:21 +010048static int max_jobs = MAX_JOBS;
49
50struct thread_data def_thread;
51struct thread_data *threads = NULL;
52
53int rate_quit = 0;
54int write_lat_log = 0;
55int write_bw_log = 0;
56int exitall_on_terminate = 0;
Jens Axboec6ae0a52006-06-12 11:04:44 +020057int terse_output = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +020058unsigned long long mlock_size = 0;
Jens Axboeeb8bbf42006-06-08 21:40:11 +020059FILE *f_out = NULL;
60FILE *f_err = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +010061
Jens Axboeebac4652005-12-08 15:25:21 +010062static struct thread_data *get_new_job(int global, struct thread_data *parent)
63{
64 struct thread_data *td;
65
66 if (global)
67 return &def_thread;
68 if (thread_number >= max_jobs)
69 return NULL;
70
71 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +020072 *td = *parent;
73 td->name[0] = '\0';
Jens Axboeebac4652005-12-08 15:25:21 +010074
75 td->fd = -1;
76 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +010077 return td;
78}
79
80static void put_job(struct thread_data *td)
81{
82 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
83 thread_number--;
84}
85
Jens Axboe75154842006-06-01 13:56:09 +020086static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +010087{
Jens Axboe3d60d1e2006-05-25 06:31:06 +020088 char *ddir_str[] = { "read", "write", "randread", "randwrite",
89 "rw", NULL, "randrw" };
Jens Axboeebac4652005-12-08 15:25:21 +010090 struct stat sb;
91 int numjobs, ddir;
92
93#ifndef FIO_HAVE_LIBAIO
94 if (td->io_engine == FIO_LIBAIO) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +020095 log_err("Linux libaio not available\n");
Jens Axboeebac4652005-12-08 15:25:21 +010096 return 1;
97 }
98#endif
99#ifndef FIO_HAVE_POSIXAIO
100 if (td->io_engine == FIO_POSIXAIO) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200101 log_err("posix aio not available\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100102 return 1;
103 }
104#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100105
106 /*
107 * the def_thread is just for options, it's not a real job
108 */
109 if (td == &def_thread)
110 return 0;
111
112 if (td->io_engine & FIO_SYNCIO)
113 td->iodepth = 1;
114 else {
115 if (!td->iodepth)
116 td->iodepth = 1;
117 }
118
Jens Axboe20dc95c2005-12-09 10:29:35 +0100119 /*
120 * only really works for sequential io for now
121 */
122 if (td->zone_size && !td->sequential)
123 td->zone_size = 0;
124
Jens Axboeebac4652005-12-08 15:25:21 +0100125 td->filetype = FIO_TYPE_FILE;
Jens Axboe0af7b542006-02-17 10:10:12 +0100126 if (!stat(jobname, &sb)) {
127 if (S_ISBLK(sb.st_mode))
128 td->filetype = FIO_TYPE_BD;
129 else if (S_ISCHR(sb.st_mode))
130 td->filetype = FIO_TYPE_CHAR;
131 }
Jens Axboeebac4652005-12-08 15:25:21 +0100132
133 if (td->filetype == FIO_TYPE_FILE) {
Jens Axboee9c047a2006-06-07 21:13:04 +0200134 char tmp[PATH_MAX];
135
Jens Axboeef899b62006-06-06 09:31:00 +0200136 if (td->directory && td->directory[0] != '\0')
Jens Axboef9481912006-06-09 11:37:28 +0200137 sprintf(tmp, "%s/%s.%d", td->directory, jobname, td->thread_number);
Jens Axboeebac4652005-12-08 15:25:21 +0100138 else
Jens Axboef9481912006-06-09 11:37:28 +0200139 sprintf(tmp, "%s.%d", jobname, td->thread_number);
Jens Axboee9c047a2006-06-07 21:13:04 +0200140 td->file_name = strdup(tmp);
Jens Axboeebac4652005-12-08 15:25:21 +0100141 } else
Jens Axboee9c047a2006-06-07 21:13:04 +0200142 td->file_name = strdup(jobname);
Jens Axboeebac4652005-12-08 15:25:21 +0100143
Jens Axboebbfd6b02006-06-07 19:42:54 +0200144 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100145
146 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
147 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
148 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
149
150 if (td->min_bs == -1U)
151 td->min_bs = td->bs;
152 if (td->max_bs == -1U)
153 td->max_bs = td->bs;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200154 if (td_read(td) && !td_rw(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100155 td->verify = 0;
156
157 if (td->stonewall && td->thread_number > 1)
158 groupid++;
159
160 td->groupid = groupid;
161
162 if (setup_rate(td))
163 goto err;
164
165 if (write_lat_log) {
166 setup_log(&td->slat_log);
167 setup_log(&td->clat_log);
168 }
169 if (write_bw_log)
170 setup_log(&td->bw_log);
171
Jens Axboe01452052006-06-07 10:29:47 +0200172 if (td->name[0] == '\0')
173 snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
174
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200175 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
Jens Axboe75154842006-06-01 13:56:09 +0200176
Jens Axboec6ae0a52006-06-12 11:04:44 +0200177 if (!terse_output) {
178 if (!job_add_num)
179 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_engine_name, td->iodepth);
180 else if (job_add_num == 1)
181 fprintf(f_out, "...\n");
182 }
Jens Axboeebac4652005-12-08 15:25:21 +0100183
184 /*
185 * recurse add identical jobs, clear numjobs and stonewall options
186 * as they don't apply to sub-jobs
187 */
188 numjobs = td->numjobs;
189 while (--numjobs) {
190 struct thread_data *td_new = get_new_job(0, td);
191
192 if (!td_new)
193 goto err;
194
195 td_new->numjobs = 1;
196 td_new->stonewall = 0;
Jens Axboe75154842006-06-01 13:56:09 +0200197 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100198
Jens Axboe75154842006-06-01 13:56:09 +0200199 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100200 goto err;
201 }
202 return 0;
203err:
204 put_job(td);
205 return -1;
206}
207
208int init_random_state(struct thread_data *td)
209{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200210 unsigned long seeds[4];
Jens Axboeebac4652005-12-08 15:25:21 +0100211 int fd, num_maps, blocks;
212
Jens Axboe1ac267b2006-05-31 20:29:00 +0200213 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100214 if (fd == -1) {
215 td_verror(td, errno);
216 return 1;
217 }
218
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200219 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100220 td_verror(td, EIO);
221 close(fd);
222 return 1;
223 }
224
225 close(fd);
226
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200227 os_random_seed(seeds[0], &td->bsrange_state);
228 os_random_seed(seeds[1], &td->verify_state);
229 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100230
231 if (td->sequential)
232 return 0;
233
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200234 if (td->rand_repeatable)
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200235 seeds[3] = DEF_RANDSEED;
Jens Axboeebac4652005-12-08 15:25:21 +0100236
237 blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
238 num_maps = blocks / BLOCKS_PER_MAP;
239 td->file_map = malloc(num_maps * sizeof(long));
240 td->num_maps = num_maps;
241 memset(td->file_map, 0, num_maps * sizeof(long));
242
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200243 os_random_seed(seeds[3], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100244 return 0;
245}
246
247static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
248{
249#ifdef FIO_HAVE_CPU_AFFINITY
250 unsigned int i;
251
252 CPU_ZERO(&cpumask);
253
254 for (i = 0; i < sizeof(int) * 8; i++) {
255 if ((1 << i) & cpu)
256 CPU_SET(i, &cpumask);
257 }
258#endif
259}
260
261static unsigned long get_mult(char c)
262{
263 switch (c) {
264 case 'k':
265 case 'K':
266 return 1024;
267 case 'm':
268 case 'M':
269 return 1024 * 1024;
270 case 'g':
271 case 'G':
272 return 1024 * 1024 * 1024;
273 default:
274 return 1;
275 }
276}
277
278/*
279 * convert string after '=' into decimal value, noting any size suffix
280 */
281static int str_cnv(char *p, unsigned long long *val)
282{
283 char *str;
284 int len;
285
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100286 str = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100287 if (!str)
288 return 1;
289
290 str++;
291 len = strlen(str);
292
293 *val = strtoul(str, NULL, 10);
294 if (*val == ULONG_MAX && errno == ERANGE)
295 return 1;
296
Jens Axboe9d0d4242006-05-30 13:45:51 +0200297 *val *= get_mult(str[len - 1]);
Jens Axboeebac4652005-12-08 15:25:21 +0100298 return 0;
299}
300
301static int check_strcnv(char *p, char *name, unsigned long long *val)
302{
Jens Axboe20dc95c2005-12-09 10:29:35 +0100303 if (strncmp(p, name, strlen(name) - 1))
Jens Axboeebac4652005-12-08 15:25:21 +0100304 return 1;
305
306 return str_cnv(p, val);
307}
308
309static void strip_blank_front(char **p)
310{
311 char *s = *p;
312
Jens Axboe4ae3f762006-05-30 13:35:14 +0200313 while (isspace(*s))
Jens Axboeebac4652005-12-08 15:25:21 +0100314 s++;
315}
316
317static void strip_blank_end(char *p)
318{
Jens Axboe4ae3f762006-05-30 13:35:14 +0200319 char *s = p + strlen(p) - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100320
Jens Axboe4ae3f762006-05-30 13:35:14 +0200321 while (isspace(*s) || iscntrl(*s))
322 s--;
Jens Axboeaea47d42006-05-26 19:27:29 +0200323
Jens Axboe4ae3f762006-05-30 13:35:14 +0200324 *(s + 1) = '\0';
Jens Axboeaea47d42006-05-26 19:27:29 +0200325}
326
Jens Axboeebac4652005-12-08 15:25:21 +0100327typedef int (str_cb_fn)(struct thread_data *, char *);
328
329static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
330{
Jens Axboe843a7412006-06-01 21:14:21 -0700331 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100332
Jens Axboe843a7412006-06-01 21:14:21 -0700333 if (strncmp(p, name, strlen(name)))
334 return 1;
335
336 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100337 if (!s)
338 return 1;
339
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100340 s = strchr(s, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100341 if (!s)
342 return 1;
343
344 s++;
345 strip_blank_front(&s);
346 return cb(td, s);
347}
348
349static int check_strstore(char *p, char *name, char *dest)
350{
Jens Axboe843a7412006-06-01 21:14:21 -0700351 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100352
Jens Axboe843a7412006-06-01 21:14:21 -0700353 if (strncmp(p, name, strlen(name)))
354 return 1;
355
356 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100357 if (!s)
358 return 1;
359
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100360 s = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100361 if (!s)
362 return 1;
363
364 s++;
365 strip_blank_front(&s);
366
367 strcpy(dest, s);
Jens Axboeebac4652005-12-08 15:25:21 +0100368 return 0;
369}
370
Jens Axboe01617be2005-12-08 19:50:40 +0100371static int __check_range(char *str, unsigned long *val)
Jens Axboeebac4652005-12-08 15:25:21 +0100372{
Jens Axboe01617be2005-12-08 19:50:40 +0100373 char suffix;
Jens Axboeebac4652005-12-08 15:25:21 +0100374
Jens Axboe01617be2005-12-08 19:50:40 +0100375 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
376 *val *= get_mult(suffix);
Jens Axboeebac4652005-12-08 15:25:21 +0100377 return 0;
378 }
379
Jens Axboe01617be2005-12-08 19:50:40 +0100380 if (sscanf(str, "%lu", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100381 return 0;
382
383 return 1;
Jens Axboe01617be2005-12-08 19:50:40 +0100384}
Jens Axboeebac4652005-12-08 15:25:21 +0100385
Jens Axboe01617be2005-12-08 19:50:40 +0100386static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
387{
388 char option[128];
389 char *str, *p1, *p2;
390
Jens Axboe843a7412006-06-01 21:14:21 -0700391 if (strncmp(p, name, strlen(name)))
392 return 1;
393
Jens Axboe01617be2005-12-08 19:50:40 +0100394 strcpy(option, p);
395 p = option;
396
397 str = strstr(p, name);
398 if (!str)
399 return 1;
400
401 p += strlen(name);
402
403 str = strchr(p, '=');
404 if (!str)
405 return 1;
406
407 /*
408 * 'p' now holds whatever is after the '=' sign
409 */
410 p1 = str + 1;
411
412 /*
413 * terminate p1 at the '-' sign
414 */
415 p = strchr(p1, '-');
416 if (!p)
417 return 1;
418
419 p2 = p + 1;
420 *p = '\0';
421
422 if (!__check_range(p1, s) && !__check_range(p2, e))
423 return 0;
424
425 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100426}
427
428static int check_int(char *p, char *name, unsigned int *val)
429{
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100430 char *str;
Jens Axboeebac4652005-12-08 15:25:21 +0100431
Jens Axboeb6754f92006-05-30 14:29:32 +0200432 if (strncmp(p, name, strlen(name)))
433 return 1;
434
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100435 str = strstr(p, name);
436 if (!str)
437 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100438
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100439 str = strchr(p, '=');
440 if (!str)
441 return 1;
442
443 str++;
444
445 if (sscanf(str, "%u", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100446 return 0;
447
448 return 1;
449}
450
451static int check_strset(char *p, char *name)
452{
453 return strncmp(p, name, strlen(name));
454}
455
456static int is_empty_or_comment(char *line)
457{
458 unsigned int i;
459
460 for (i = 0; i < strlen(line); i++) {
461 if (line[i] == ';')
462 return 1;
463 if (!isspace(line[i]) && !iscntrl(line[i]))
464 return 0;
465 }
466
467 return 1;
468}
469
470static int str_rw_cb(struct thread_data *td, char *mem)
471{
472 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
473 td->ddir = DDIR_READ;
474 td->sequential = 1;
475 return 0;
476 } else if (!strncmp(mem, "randread", 8)) {
477 td->ddir = DDIR_READ;
478 td->sequential = 0;
479 return 0;
480 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
481 td->ddir = DDIR_WRITE;
482 td->sequential = 1;
483 return 0;
484 } else if (!strncmp(mem, "randwrite", 9)) {
485 td->ddir = DDIR_WRITE;
486 td->sequential = 0;
487 return 0;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200488 } else if (!strncmp(mem, "rw", 2)) {
489 td->ddir = 0;
490 td->iomix = 1;
491 td->sequential = 1;
492 return 0;
493 } else if (!strncmp(mem, "randrw", 6)) {
494 td->ddir = 0;
495 td->iomix = 1;
496 td->sequential = 0;
497 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100498 }
499
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200500 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100501 return 1;
502}
503
504static int str_verify_cb(struct thread_data *td, char *mem)
505{
506 if (!strncmp(mem, "0", 1)) {
507 td->verify = VERIFY_NONE;
508 return 0;
509 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
510 td->verify = VERIFY_MD5;
511 return 0;
512 } else if (!strncmp(mem, "crc32", 5)) {
513 td->verify = VERIFY_CRC32;
514 return 0;
515 }
516
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200517 log_err("fio: verify types: md5, crc32\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100518 return 1;
519}
520
521static int str_mem_cb(struct thread_data *td, char *mem)
522{
523 if (!strncmp(mem, "malloc", 6)) {
524 td->mem_type = MEM_MALLOC;
525 return 0;
526 } else if (!strncmp(mem, "shm", 3)) {
527 td->mem_type = MEM_SHM;
528 return 0;
529 } else if (!strncmp(mem, "mmap", 4)) {
530 td->mem_type = MEM_MMAP;
531 return 0;
532 }
533
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200534 log_err("fio: mem type: malloc, shm, mmap\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100535 return 1;
536}
537
538static int str_ioengine_cb(struct thread_data *td, char *str)
539{
540 if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
541 !strncmp(str, "libaio", 6)) {
542 strcpy(td->io_engine_name, "libaio");
543 td->io_engine = FIO_LIBAIO;
544 return 0;
545 } else if (!strncmp(str, "posixaio", 8)) {
546 strcpy(td->io_engine_name, "posixaio");
547 td->io_engine = FIO_POSIXAIO;
548 return 0;
549 } else if (!strncmp(str, "sync", 4)) {
550 strcpy(td->io_engine_name, "sync");
551 td->io_engine = FIO_SYNCIO;
552 return 0;
553 } else if (!strncmp(str, "mmap", 4)) {
554 strcpy(td->io_engine_name, "mmap");
555 td->io_engine = FIO_MMAPIO;
556 return 0;
557 } else if (!strncmp(str, "sgio", 4)) {
558 strcpy(td->io_engine_name, "sgio");
559 td->io_engine = FIO_SGIO;
560 return 0;
Jens Axboe8756e4d2006-05-27 20:24:53 +0200561 } else if (!strncmp(str, "splice", 6)) {
562 strcpy(td->io_engine_name, "splice");
563 td->io_engine = FIO_SPLICEIO;
564 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100565 }
566
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200567 log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100568 return 1;
569}
570
Jens Axboe07261982006-06-07 10:51:12 +0200571/*
572 * This is our [ini] type file parser.
573 */
Jens Axboeebac4652005-12-08 15:25:21 +0100574int parse_jobs_ini(char *file)
575{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200576 unsigned int prioclass, prio, cpu, global, il;
Jens Axboeebac4652005-12-08 15:25:21 +0100577 unsigned long long ull;
578 unsigned long ul1, ul2;
579 struct thread_data *td;
Jens Axboeef899b62006-06-06 09:31:00 +0200580 char *string, *name, *tmpbuf;
Jens Axboeebac4652005-12-08 15:25:21 +0100581 fpos_t off;
582 FILE *f;
583 char *p;
Jens Axboe972cfd22006-06-09 11:08:56 +0200584 int ret = 0, stonewall = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100585
586 f = fopen(file, "r");
587 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200588 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100589 return 1;
590 }
591
592 string = malloc(4096);
593 name = malloc(256);
Jens Axboeef899b62006-06-06 09:31:00 +0200594 tmpbuf = malloc(4096);
Jens Axboeebac4652005-12-08 15:25:21 +0100595
596 while ((p = fgets(string, 4096, f)) != NULL) {
Jens Axboe45410ac2006-06-07 11:10:39 +0200597 if (ret)
598 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100599 if (is_empty_or_comment(p))
600 continue;
601 if (sscanf(p, "[%s]", name) != 1)
602 continue;
603
604 global = !strncmp(name, "global", 6);
605
606 name[strlen(name) - 1] = '\0';
607
608 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200609 if (!td) {
610 ret = 1;
611 break;
612 }
Jens Axboeebac4652005-12-08 15:25:21 +0100613
Jens Axboe972cfd22006-06-09 11:08:56 +0200614 /*
615 * Seperate multiple job files by a stonewall
616 */
Jens Axboef9481912006-06-09 11:37:28 +0200617 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +0200618 td->stonewall = stonewall;
619 stonewall = 0;
620 }
621
Jens Axboeebac4652005-12-08 15:25:21 +0100622 fgetpos(f, &off);
623 while ((p = fgets(string, 4096, f)) != NULL) {
624 if (is_empty_or_comment(p))
625 continue;
626 if (strstr(p, "["))
627 break;
Jens Axboeb6754f92006-05-30 14:29:32 +0200628 strip_blank_front(&p);
Jens Axboe4ae3f762006-05-30 13:35:14 +0200629 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200630
Jens Axboeebac4652005-12-08 15:25:21 +0100631 if (!check_int(p, "prio", &prio)) {
632#ifndef FIO_HAVE_IOPRIO
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200633 log_err("io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200634 ret = 1;
635 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100636#endif
Jens Axboe8756e4d2006-05-27 20:24:53 +0200637 td->ioprio |= prio;
Jens Axboeebac4652005-12-08 15:25:21 +0100638 fgetpos(f, &off);
639 continue;
640 }
641 if (!check_int(p, "prioclass", &prioclass)) {
642#ifndef FIO_HAVE_IOPRIO
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200643 log_err("io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200644 ret = 1;
645 break;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200646#else
Jens Axboe8756e4d2006-05-27 20:24:53 +0200647 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
Jens Axboeebac4652005-12-08 15:25:21 +0100648 fgetpos(f, &off);
649 continue;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200650#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100651 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200652 if (!check_int(p, "direct", &il)) {
653 td->odirect = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100654 fgetpos(f, &off);
655 continue;
656 }
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200657 if (!check_int(p, "rand_repeatable", &il)) {
658 td->rand_repeatable = il;
659 fgetpos(f, &off);
660 continue;
661 }
Jens Axboeebac4652005-12-08 15:25:21 +0100662 if (!check_int(p, "rate", &td->rate)) {
663 fgetpos(f, &off);
664 continue;
665 }
666 if (!check_int(p, "ratemin", &td->ratemin)) {
667 fgetpos(f, &off);
668 continue;
669 }
670 if (!check_int(p, "ratecycle", &td->ratecycle)) {
671 fgetpos(f, &off);
672 continue;
673 }
674 if (!check_int(p, "thinktime", &td->thinktime)) {
675 fgetpos(f, &off);
676 continue;
677 }
678 if (!check_int(p, "cpumask", &cpu)) {
679#ifndef FIO_HAVE_CPU_AFFINITY
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200680 log_err("cpu affinity not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200681 ret = 1;
682 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100683#endif
684 fill_cpu_mask(td->cpumask, cpu);
685 fgetpos(f, &off);
686 continue;
687 }
688 if (!check_int(p, "fsync", &td->fsync_blocks)) {
689 fgetpos(f, &off);
Jens Axboefc1a4712006-05-30 13:04:05 +0200690 td->end_fsync = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100691 continue;
692 }
693 if (!check_int(p, "startdelay", &td->start_delay)) {
694 fgetpos(f, &off);
695 continue;
696 }
697 if (!check_int(p, "timeout", &td->timeout)) {
698 fgetpos(f, &off);
699 continue;
700 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200701 if (!check_int(p, "invalidate", &il)) {
702 td->invalidate_cache = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100703 fgetpos(f, &off);
704 continue;
705 }
706 if (!check_int(p, "iodepth", &td->iodepth)) {
707 fgetpos(f, &off);
708 continue;
709 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200710 if (!check_int(p, "sync", &il)) {
711 td->sync_io = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100712 fgetpos(f, &off);
713 continue;
714 }
715 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
716 fgetpos(f, &off);
717 continue;
718 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200719 if (!check_int(p, "create_serialize", &il)) {
720 td->create_serialize = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100721 fgetpos(f, &off);
722 continue;
723 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200724 if (!check_int(p, "create_fsync", &il)) {
725 td->create_fsync = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100726 fgetpos(f, &off);
727 continue;
728 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200729 if (!check_int(p, "end_fsync", &il)) {
730 td->end_fsync = il;
Jens Axboefc1a4712006-05-30 13:04:05 +0200731 fgetpos(f, &off);
732 continue;
733 }
Jens Axboeebac4652005-12-08 15:25:21 +0100734 if (!check_int(p, "loops", &td->loops)) {
735 fgetpos(f, &off);
736 continue;
737 }
738 if (!check_int(p, "numjobs", &td->numjobs)) {
739 fgetpos(f, &off);
740 continue;
741 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200742 if (!check_int(p, "overwrite", &il)) {
743 td->overwrite = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100744 fgetpos(f, &off);
745 continue;
746 }
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200747 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
748 fgetpos(f, &off);
749 continue;
750 }
751 if (!check_int(p, "rwmixread", &il)) {
752 if (il > 100)
753 il = 100;
754 td->rwmixread = il;
755 fgetpos(f, &off);
756 continue;
757 }
758 if (!check_int(p, "rwmixwrite", &il)) {
759 if (il > 100)
760 il = 100;
761 td->rwmixread = 100 - il;
762 fgetpos(f, &off);
763 continue;
764 }
Jens Axboeb6f4d882006-06-02 10:32:51 +0200765 if (!check_int(p, "nice", &td->nice)) {
766 fgetpos(f, &off);
767 continue;
768 }
Jens Axboeebac4652005-12-08 15:25:21 +0100769 if (!check_range(p, "bsrange", &ul1, &ul2)) {
770 if (ul1 > ul2) {
771 td->max_bs = ul1;
772 td->min_bs = ul2;
773 } else {
774 td->max_bs = ul2;
775 td->min_bs = ul1;
776 }
777 fgetpos(f, &off);
778 continue;
779 }
780 if (!check_strcnv(p, "bs", &ull)) {
781 td->bs = ull;
782 fgetpos(f, &off);
783 continue;
784 }
785 if (!check_strcnv(p, "size", &td->file_size)) {
786 fgetpos(f, &off);
787 continue;
788 }
789 if (!check_strcnv(p, "offset", &td->file_offset)) {
790 fgetpos(f, &off);
791 continue;
792 }
Jens Axboe20dc95c2005-12-09 10:29:35 +0100793 if (!check_strcnv(p, "zonesize", &td->zone_size)) {
794 fgetpos(f, &off);
795 continue;
796 }
797 if (!check_strcnv(p, "zoneskip", &td->zone_skip)) {
798 fgetpos(f, &off);
799 continue;
800 }
Jens Axboec04f7ec2006-05-31 10:13:16 +0200801 if (!check_strcnv(p, "lockmem", &mlock_size)) {
802 fgetpos(f, &off);
803 continue;
804 }
Jens Axboeef899b62006-06-06 09:31:00 +0200805 if (!check_strstore(p, "directory", tmpbuf)) {
806 td->directory = strdup(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100807 fgetpos(f, &off);
808 continue;
809 }
Jens Axboe01452052006-06-07 10:29:47 +0200810 if (!check_strstore(p, "name", tmpbuf)) {
811 snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
812 fgetpos(f, &off);
813 continue;
814 }
Jens Axboeebac4652005-12-08 15:25:21 +0100815 if (!check_str(p, "mem", str_mem_cb, td)) {
816 fgetpos(f, &off);
817 continue;
818 }
819 if (!check_str(p, "verify", str_verify_cb, td)) {
820 fgetpos(f, &off);
821 continue;
822 }
823 if (!check_str(p, "rw", str_rw_cb, td)) {
824 fgetpos(f, &off);
825 continue;
826 }
827 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
828 fgetpos(f, &off);
829 continue;
830 }
831 if (!check_strset(p, "create")) {
832 td->create_file = 1;
833 fgetpos(f, &off);
834 continue;
835 }
836 if (!check_strset(p, "exitall")) {
837 exitall_on_terminate = 1;
838 fgetpos(f, &off);
839 continue;
840 }
841 if (!check_strset(p, "stonewall")) {
842 td->stonewall = 1;
843 fgetpos(f, &off);
844 continue;
845 }
846 if (!check_strset(p, "thread")) {
847 td->use_thread = 1;
848 fgetpos(f, &off);
849 continue;
850 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200851 if (!check_strstore(p, "iolog", tmpbuf)) {
Jens Axboe4f693b92006-06-07 22:52:28 +0200852 if (td->write_iolog) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200853 log_err("fio: read iolog overrides given write_iolog\n");
Jens Axboeaf8c0b42006-06-07 22:33:17 +0200854 free(td->iolog_file);
Jens Axboe4f693b92006-06-07 22:52:28 +0200855 td->write_iolog = 0;
856 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200857 td->iolog_file = strdup(tmpbuf);
Jens Axboe843a7412006-06-01 21:14:21 -0700858 td->read_iolog = 1;
Jens Axboe843a7412006-06-01 21:14:21 -0700859 fgetpos(f, &off);
860 continue;
861 }
Jens Axboeaf8c0b42006-06-07 22:33:17 +0200862 if (!check_strstore(p, "write_iolog", tmpbuf)) {
863 if (!td->read_iolog) {
864 td->iolog_file = strdup(tmpbuf);
865 td->write_iolog = 1;
Jens Axboe4f693b92006-06-07 22:52:28 +0200866 } else
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200867 log_err("fio: read iolog overrides given write_iolog\n");
Jens Axboeaea47d42006-05-26 19:27:29 +0200868 fgetpos(f, &off);
869 continue;
870 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200871 if (!check_strstore(p, "exec_prerun", tmpbuf)) {
872 td->exec_prerun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200873 fgetpos(f, &off);
874 continue;
875 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200876 if (!check_strstore(p, "exec_postrun", tmpbuf)) {
877 td->exec_postrun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200878 fgetpos(f, &off);
879 continue;
880 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200881 if (!check_strstore(p, "ioscheduler", tmpbuf)) {
Jens Axboe22f78b32006-06-07 11:30:07 +0200882#ifndef FIO_HAVE_IOSCHED_SWITCH
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200883 log_err("io scheduler switching not available\n");
Jens Axboe22f78b32006-06-07 11:30:07 +0200884 ret = 1;
885 break;
886#else
Jens Axboe80b9feb2006-06-07 10:34:49 +0200887 td->ioscheduler = strdup(tmpbuf);
Jens Axboeda867742006-06-06 10:39:10 -0700888 fgetpos(f, &off);
889 continue;
Jens Axboe22f78b32006-06-07 11:30:07 +0200890#endif
Jens Axboeda867742006-06-06 10:39:10 -0700891 }
Jens Axboeebac4652005-12-08 15:25:21 +0100892
Jens Axboe45410ac2006-06-07 11:10:39 +0200893 /*
894 * Don't break here, continue parsing options so we
895 * dump all the bad ones. Makes trial/error fixups
896 * easier on the user.
897 */
Jens Axboeebac4652005-12-08 15:25:21 +0100898 printf("Client%d: bad option %s\n",td->thread_number,p);
Jens Axboe45410ac2006-06-07 11:10:39 +0200899 ret = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100900 }
Jens Axboeebac4652005-12-08 15:25:21 +0100901
Jens Axboe45410ac2006-06-07 11:10:39 +0200902 if (!ret) {
903 fsetpos(f, &off);
904 ret = add_job(td, name, 0);
905 }
906 if (ret)
907 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100908 }
909
910 free(string);
911 free(name);
Jens Axboeef899b62006-06-06 09:31:00 +0200912 free(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100913 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200914 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100915}
916
917static int fill_def_thread(void)
918{
919 memset(&def_thread, 0, sizeof(def_thread));
920
921 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
922 perror("sched_getaffinity");
923 return 1;
924 }
925
926 /*
927 * fill globals
928 */
929 def_thread.ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200930 def_thread.iomix = 0;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200931 def_thread.bs = DEF_BS;
Jens Axboeebac4652005-12-08 15:25:21 +0100932 def_thread.min_bs = -1;
933 def_thread.max_bs = -1;
934 def_thread.io_engine = DEF_IO_ENGINE;
935 strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200936 def_thread.odirect = DEF_ODIRECT;
Jens Axboeebac4652005-12-08 15:25:21 +0100937 def_thread.ratecycle = DEF_RATE_CYCLE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200938 def_thread.sequential = DEF_SEQUENTIAL;
Jens Axboe972cfd22006-06-09 11:08:56 +0200939 def_thread.timeout = def_timeout;
Jens Axboeebac4652005-12-08 15:25:21 +0100940 def_thread.create_file = DEF_CREATE;
941 def_thread.overwrite = DEF_OVERWRITE;
942 def_thread.invalidate_cache = DEF_INVALIDATE;
943 def_thread.sync_io = DEF_SYNCIO;
944 def_thread.mem_type = MEM_MALLOC;
945 def_thread.bw_avg_time = DEF_BWAVGTIME;
946 def_thread.create_serialize = DEF_CREATE_SER;
947 def_thread.create_fsync = DEF_CREATE_FSYNC;
948 def_thread.loops = DEF_LOOPS;
949 def_thread.verify = DEF_VERIFY;
950 def_thread.stonewall = DEF_STONEWALL;
951 def_thread.numjobs = DEF_NUMJOBS;
952 def_thread.use_thread = DEF_USE_THREAD;
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200953 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
954 def_thread.rwmixread = DEF_RWMIX_READ;
Jens Axboeb6f4d882006-06-02 10:32:51 +0200955 def_thread.nice = DEF_NICE;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200956 def_thread.rand_repeatable = DEF_RAND_REPEAT;
Jens Axboeebac4652005-12-08 15:25:21 +0100957#ifdef FIO_HAVE_DISK_UTIL
958 def_thread.do_disk_util = 1;
959#endif
960
961 return 0;
962}
963
Jens Axboe4785f992006-05-26 03:59:10 +0200964static void usage(char *name)
965{
966 printf("%s\n", fio_version_string);
Jens Axboe47623c72006-06-12 10:23:28 +0200967 printf("\t-o Write output to file\n");
Jens Axboe4785f992006-05-26 03:59:10 +0200968 printf("\t-t Runtime in seconds\n");
Jens Axboe4785f992006-05-26 03:59:10 +0200969 printf("\t-l Generate per-job latency logs\n");
970 printf("\t-w Generate per-job bandwidth logs\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +0200971 printf("\t-m Minimal (terse) output\n");
Jens Axboe4785f992006-05-26 03:59:10 +0200972 printf("\t-f Job file (Required)\n");
973 printf("\t-v Print version info and exit\n");
974}
975
Jens Axboe972cfd22006-06-09 11:08:56 +0200976static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +0100977{
Jens Axboe972cfd22006-06-09 11:08:56 +0200978 int c, idx = 1, ini_idx = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100979
Jens Axboec6ae0a52006-06-12 11:04:44 +0200980 while ((c = getopt(argc, argv, "t:o:f:lwvhm")) != EOF) {
Jens Axboeebac4652005-12-08 15:25:21 +0100981 switch (c) {
Jens Axboeebac4652005-12-08 15:25:21 +0100982 case 't':
Jens Axboe972cfd22006-06-09 11:08:56 +0200983 def_timeout = atoi(optarg);
984 idx++;
Jens Axboeebac4652005-12-08 15:25:21 +0100985 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100986 case 'f':
Jens Axboe972cfd22006-06-09 11:08:56 +0200987 ini_idx++;
988 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
989 ini_file[ini_idx - 1] = strdup(optarg);
990 idx++;
Jens Axboeebac4652005-12-08 15:25:21 +0100991 break;
992 case 'l':
993 write_lat_log = 1;
Jens Axboe972cfd22006-06-09 11:08:56 +0200994 idx++;
Jens Axboeebac4652005-12-08 15:25:21 +0100995 break;
996 case 'w':
997 write_bw_log = 1;
Jens Axboe972cfd22006-06-09 11:08:56 +0200998 idx++;
Jens Axboeebac4652005-12-08 15:25:21 +0100999 break;
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001000 case 'o':
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001001 f_out = fopen(optarg, "w+");
1002 if (!f_out) {
1003 perror("fopen output");
1004 exit(1);
1005 }
1006 f_err = f_out;
Jens Axboe972cfd22006-06-09 11:08:56 +02001007 idx++;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001008 break;
Jens Axboec6ae0a52006-06-12 11:04:44 +02001009 case 'm':
1010 terse_output = 1;
1011 idx++;
1012 break;
Jens Axboe4785f992006-05-26 03:59:10 +02001013 case 'h':
1014 usage(argv[0]);
1015 exit(0);
Jens Axboeebac4652005-12-08 15:25:21 +01001016 case 'v':
1017 printf("%s\n", fio_version_string);
1018 exit(0);
1019 }
1020 }
Jens Axboec9fad892006-05-27 17:26:50 +02001021
Jens Axboe972cfd22006-06-09 11:08:56 +02001022 while (idx < argc) {
1023 ini_idx++;
1024 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1025 ini_file[ini_idx - 1] = strdup(argv[idx]);
1026 idx++;
1027 }
1028
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001029 if (!f_out) {
1030 f_out = stdout;
1031 f_err = stderr;
1032 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001033
1034 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +01001035}
1036
1037static void free_shm(void)
1038{
1039 struct shmid_ds sbuf;
1040
1041 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001042 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001043 threads = NULL;
1044 shmctl(shm_id, IPC_RMID, &sbuf);
1045 }
1046}
1047
1048static int setup_thread_area(void)
1049{
1050 /*
1051 * 1024 is too much on some machines, scale max_jobs if
1052 * we get a failure that looks like too large a shm segment
1053 */
1054 do {
1055 int s = max_jobs * sizeof(struct thread_data);
1056
1057 shm_id = shmget(0, s, IPC_CREAT | 0600);
1058 if (shm_id != -1)
1059 break;
1060 if (errno != EINVAL) {
1061 perror("shmget");
1062 break;
1063 }
1064
1065 max_jobs >>= 1;
1066 } while (max_jobs);
1067
1068 if (shm_id == -1)
1069 return 1;
1070
1071 threads = shmat(shm_id, NULL, 0);
1072 if (threads == (void *) -1) {
1073 perror("shmat");
1074 return 1;
1075 }
1076
1077 atexit(free_shm);
1078 return 0;
1079}
1080
1081int parse_options(int argc, char *argv[])
1082{
Jens Axboe972cfd22006-06-09 11:08:56 +02001083 int job_files, i;
1084
Jens Axboeebac4652005-12-08 15:25:21 +01001085 if (setup_thread_area())
1086 return 1;
1087 if (fill_def_thread())
1088 return 1;
1089
Jens Axboe972cfd22006-06-09 11:08:56 +02001090 job_files = parse_cmd_line(argc, argv);
1091 if (!job_files) {
1092 log_err("Need job file(s)\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001093 usage(argv[0]);
Jens Axboeebac4652005-12-08 15:25:21 +01001094 return 1;
1095 }
1096
Jens Axboe972cfd22006-06-09 11:08:56 +02001097 for (i = 0; i < job_files; i++) {
1098 if (fill_def_thread())
1099 return 1;
1100 if (parse_jobs_ini(ini_file[i]))
1101 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001102 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001103 }
Jens Axboeebac4652005-12-08 15:25:21 +01001104
Jens Axboe88c6ed82006-06-09 11:28:10 +02001105 free(ini_file);
Jens Axboeebac4652005-12-08 15:25:21 +01001106 return 0;
1107}