blob: ee9ab7d2ab6772b95b718c7ff94afcb9315bae7f [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_sequential = DEF_SEQUENTIAL;
44static int def_bs = DEF_BS;
45static int def_timeout = DEF_TIMEOUT;
46static int def_repeatable = DEF_RAND_REPEAT;
47static int def_odirect = DEF_ODIRECT;
48
Jens Axboe573275e2006-06-05 12:20:20 +020049static char fio_version_string[] = "fio 1.4";
Jens Axboeebac4652005-12-08 15:25:21 +010050
Jens Axboe972cfd22006-06-09 11:08:56 +020051static char **ini_file;
Jens Axboeebac4652005-12-08 15:25:21 +010052static int max_jobs = MAX_JOBS;
53
54struct thread_data def_thread;
55struct thread_data *threads = NULL;
56
57int rate_quit = 0;
58int write_lat_log = 0;
59int write_bw_log = 0;
60int exitall_on_terminate = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +020061unsigned long long mlock_size = 0;
Jens Axboeeb8bbf42006-06-08 21:40:11 +020062FILE *f_out = NULL;
63FILE *f_err = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +010064
Jens Axboeebac4652005-12-08 15:25:21 +010065static struct thread_data *get_new_job(int global, struct thread_data *parent)
66{
67 struct thread_data *td;
68
69 if (global)
70 return &def_thread;
71 if (thread_number >= max_jobs)
72 return NULL;
73
74 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +020075 *td = *parent;
76 td->name[0] = '\0';
Jens Axboeebac4652005-12-08 15:25:21 +010077
78 td->fd = -1;
79 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +010080 return td;
81}
82
83static void put_job(struct thread_data *td)
84{
85 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
86 thread_number--;
87}
88
Jens Axboe75154842006-06-01 13:56:09 +020089static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +010090{
Jens Axboe3d60d1e2006-05-25 06:31:06 +020091 char *ddir_str[] = { "read", "write", "randread", "randwrite",
92 "rw", NULL, "randrw" };
Jens Axboeebac4652005-12-08 15:25:21 +010093 struct stat sb;
94 int numjobs, ddir;
95
96#ifndef FIO_HAVE_LIBAIO
97 if (td->io_engine == FIO_LIBAIO) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +020098 log_err("Linux libaio not available\n");
Jens Axboeebac4652005-12-08 15:25:21 +010099 return 1;
100 }
101#endif
102#ifndef FIO_HAVE_POSIXAIO
103 if (td->io_engine == FIO_POSIXAIO) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200104 log_err("posix aio not available\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100105 return 1;
106 }
107#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100108
109 /*
110 * the def_thread is just for options, it's not a real job
111 */
112 if (td == &def_thread)
113 return 0;
114
115 if (td->io_engine & FIO_SYNCIO)
116 td->iodepth = 1;
117 else {
118 if (!td->iodepth)
119 td->iodepth = 1;
120 }
121
Jens Axboe20dc95c2005-12-09 10:29:35 +0100122 /*
123 * only really works for sequential io for now
124 */
125 if (td->zone_size && !td->sequential)
126 td->zone_size = 0;
127
Jens Axboeebac4652005-12-08 15:25:21 +0100128 td->filetype = FIO_TYPE_FILE;
Jens Axboe0af7b542006-02-17 10:10:12 +0100129 if (!stat(jobname, &sb)) {
130 if (S_ISBLK(sb.st_mode))
131 td->filetype = FIO_TYPE_BD;
132 else if (S_ISCHR(sb.st_mode))
133 td->filetype = FIO_TYPE_CHAR;
134 }
Jens Axboeebac4652005-12-08 15:25:21 +0100135
136 if (td->filetype == FIO_TYPE_FILE) {
Jens Axboee9c047a2006-06-07 21:13:04 +0200137 char tmp[PATH_MAX];
138
Jens Axboeef899b62006-06-06 09:31:00 +0200139 if (td->directory && td->directory[0] != '\0')
Jens Axboee9c047a2006-06-07 21:13:04 +0200140 sprintf(tmp, "%s/%s.%d", td->directory, jobname, td->jobnum);
Jens Axboeebac4652005-12-08 15:25:21 +0100141 else
Jens Axboee9c047a2006-06-07 21:13:04 +0200142 sprintf(tmp, "%s.%d", jobname, td->jobnum);
143 td->file_name = strdup(tmp);
Jens Axboeebac4652005-12-08 15:25:21 +0100144 } else
Jens Axboee9c047a2006-06-07 21:13:04 +0200145 td->file_name = strdup(jobname);
Jens Axboeebac4652005-12-08 15:25:21 +0100146
Jens Axboebbfd6b02006-06-07 19:42:54 +0200147 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100148
149 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
150 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
151 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
152
153 if (td->min_bs == -1U)
154 td->min_bs = td->bs;
155 if (td->max_bs == -1U)
156 td->max_bs = td->bs;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200157 if (td_read(td) && !td_rw(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100158 td->verify = 0;
159
160 if (td->stonewall && td->thread_number > 1)
161 groupid++;
162
163 td->groupid = groupid;
164
165 if (setup_rate(td))
166 goto err;
167
168 if (write_lat_log) {
169 setup_log(&td->slat_log);
170 setup_log(&td->clat_log);
171 }
172 if (write_bw_log)
173 setup_log(&td->bw_log);
174
Jens Axboe01452052006-06-07 10:29:47 +0200175 if (td->name[0] == '\0')
176 snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
177
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200178 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
Jens Axboe75154842006-06-01 13:56:09 +0200179
180 if (!job_add_num)
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200181 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);
Jens Axboe75154842006-06-01 13:56:09 +0200182 else if (job_add_num == 1)
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200183 fprintf(f_out, "...\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100184
185 /*
186 * recurse add identical jobs, clear numjobs and stonewall options
187 * as they don't apply to sub-jobs
188 */
189 numjobs = td->numjobs;
190 while (--numjobs) {
191 struct thread_data *td_new = get_new_job(0, td);
192
193 if (!td_new)
194 goto err;
195
196 td_new->numjobs = 1;
197 td_new->stonewall = 0;
Jens Axboeeba09dd2006-05-22 20:20:54 +0200198 td_new->jobnum = numjobs;
Jens Axboe75154842006-06-01 13:56:09 +0200199 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100200
Jens Axboe75154842006-06-01 13:56:09 +0200201 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100202 goto err;
203 }
204 return 0;
205err:
206 put_job(td);
207 return -1;
208}
209
210int init_random_state(struct thread_data *td)
211{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200212 unsigned long seeds[4];
Jens Axboeebac4652005-12-08 15:25:21 +0100213 int fd, num_maps, blocks;
214
Jens Axboe1ac267b2006-05-31 20:29:00 +0200215 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100216 if (fd == -1) {
217 td_verror(td, errno);
218 return 1;
219 }
220
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200221 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100222 td_verror(td, EIO);
223 close(fd);
224 return 1;
225 }
226
227 close(fd);
228
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200229 os_random_seed(seeds[0], &td->bsrange_state);
230 os_random_seed(seeds[1], &td->verify_state);
231 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100232
233 if (td->sequential)
234 return 0;
235
Jens Axboe972cfd22006-06-09 11:08:56 +0200236 if (def_repeatable)
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200237 seeds[3] = DEF_RANDSEED;
Jens Axboeebac4652005-12-08 15:25:21 +0100238
239 blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
240 num_maps = blocks / BLOCKS_PER_MAP;
241 td->file_map = malloc(num_maps * sizeof(long));
242 td->num_maps = num_maps;
243 memset(td->file_map, 0, num_maps * sizeof(long));
244
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200245 os_random_seed(seeds[3], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100246 return 0;
247}
248
249static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
250{
251#ifdef FIO_HAVE_CPU_AFFINITY
252 unsigned int i;
253
254 CPU_ZERO(&cpumask);
255
256 for (i = 0; i < sizeof(int) * 8; i++) {
257 if ((1 << i) & cpu)
258 CPU_SET(i, &cpumask);
259 }
260#endif
261}
262
263static unsigned long get_mult(char c)
264{
265 switch (c) {
266 case 'k':
267 case 'K':
268 return 1024;
269 case 'm':
270 case 'M':
271 return 1024 * 1024;
272 case 'g':
273 case 'G':
274 return 1024 * 1024 * 1024;
275 default:
276 return 1;
277 }
278}
279
280/*
281 * convert string after '=' into decimal value, noting any size suffix
282 */
283static int str_cnv(char *p, unsigned long long *val)
284{
285 char *str;
286 int len;
287
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100288 str = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100289 if (!str)
290 return 1;
291
292 str++;
293 len = strlen(str);
294
295 *val = strtoul(str, NULL, 10);
296 if (*val == ULONG_MAX && errno == ERANGE)
297 return 1;
298
Jens Axboe9d0d4242006-05-30 13:45:51 +0200299 *val *= get_mult(str[len - 1]);
Jens Axboeebac4652005-12-08 15:25:21 +0100300 return 0;
301}
302
303static int check_strcnv(char *p, char *name, unsigned long long *val)
304{
Jens Axboe20dc95c2005-12-09 10:29:35 +0100305 if (strncmp(p, name, strlen(name) - 1))
Jens Axboeebac4652005-12-08 15:25:21 +0100306 return 1;
307
308 return str_cnv(p, val);
309}
310
311static void strip_blank_front(char **p)
312{
313 char *s = *p;
314
Jens Axboe4ae3f762006-05-30 13:35:14 +0200315 while (isspace(*s))
Jens Axboeebac4652005-12-08 15:25:21 +0100316 s++;
317}
318
319static void strip_blank_end(char *p)
320{
Jens Axboe4ae3f762006-05-30 13:35:14 +0200321 char *s = p + strlen(p) - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100322
Jens Axboe4ae3f762006-05-30 13:35:14 +0200323 while (isspace(*s) || iscntrl(*s))
324 s--;
Jens Axboeaea47d42006-05-26 19:27:29 +0200325
Jens Axboe4ae3f762006-05-30 13:35:14 +0200326 *(s + 1) = '\0';
Jens Axboeaea47d42006-05-26 19:27:29 +0200327}
328
Jens Axboeebac4652005-12-08 15:25:21 +0100329typedef int (str_cb_fn)(struct thread_data *, char *);
330
331static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
332{
Jens Axboe843a7412006-06-01 21:14:21 -0700333 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100334
Jens Axboe843a7412006-06-01 21:14:21 -0700335 if (strncmp(p, name, strlen(name)))
336 return 1;
337
338 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100339 if (!s)
340 return 1;
341
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100342 s = strchr(s, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100343 if (!s)
344 return 1;
345
346 s++;
347 strip_blank_front(&s);
348 return cb(td, s);
349}
350
351static int check_strstore(char *p, char *name, char *dest)
352{
Jens Axboe843a7412006-06-01 21:14:21 -0700353 char *s;
Jens Axboeebac4652005-12-08 15:25:21 +0100354
Jens Axboe843a7412006-06-01 21:14:21 -0700355 if (strncmp(p, name, strlen(name)))
356 return 1;
357
358 s = strstr(p, name);
Jens Axboeebac4652005-12-08 15:25:21 +0100359 if (!s)
360 return 1;
361
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100362 s = strchr(p, '=');
Jens Axboeebac4652005-12-08 15:25:21 +0100363 if (!s)
364 return 1;
365
366 s++;
367 strip_blank_front(&s);
368
369 strcpy(dest, s);
Jens Axboeebac4652005-12-08 15:25:21 +0100370 return 0;
371}
372
Jens Axboe01617be2005-12-08 19:50:40 +0100373static int __check_range(char *str, unsigned long *val)
Jens Axboeebac4652005-12-08 15:25:21 +0100374{
Jens Axboe01617be2005-12-08 19:50:40 +0100375 char suffix;
Jens Axboeebac4652005-12-08 15:25:21 +0100376
Jens Axboe01617be2005-12-08 19:50:40 +0100377 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
378 *val *= get_mult(suffix);
Jens Axboeebac4652005-12-08 15:25:21 +0100379 return 0;
380 }
381
Jens Axboe01617be2005-12-08 19:50:40 +0100382 if (sscanf(str, "%lu", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100383 return 0;
384
385 return 1;
Jens Axboe01617be2005-12-08 19:50:40 +0100386}
Jens Axboeebac4652005-12-08 15:25:21 +0100387
Jens Axboe01617be2005-12-08 19:50:40 +0100388static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
389{
390 char option[128];
391 char *str, *p1, *p2;
392
Jens Axboe843a7412006-06-01 21:14:21 -0700393 if (strncmp(p, name, strlen(name)))
394 return 1;
395
Jens Axboe01617be2005-12-08 19:50:40 +0100396 strcpy(option, p);
397 p = option;
398
399 str = strstr(p, name);
400 if (!str)
401 return 1;
402
403 p += strlen(name);
404
405 str = strchr(p, '=');
406 if (!str)
407 return 1;
408
409 /*
410 * 'p' now holds whatever is after the '=' sign
411 */
412 p1 = str + 1;
413
414 /*
415 * terminate p1 at the '-' sign
416 */
417 p = strchr(p1, '-');
418 if (!p)
419 return 1;
420
421 p2 = p + 1;
422 *p = '\0';
423
424 if (!__check_range(p1, s) && !__check_range(p2, e))
425 return 0;
426
427 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100428}
429
430static int check_int(char *p, char *name, unsigned int *val)
431{
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100432 char *str;
Jens Axboeebac4652005-12-08 15:25:21 +0100433
Jens Axboeb6754f92006-05-30 14:29:32 +0200434 if (strncmp(p, name, strlen(name)))
435 return 1;
436
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100437 str = strstr(p, name);
438 if (!str)
439 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100440
Jens Axboef5d3e5e2005-12-08 20:05:38 +0100441 str = strchr(p, '=');
442 if (!str)
443 return 1;
444
445 str++;
446
447 if (sscanf(str, "%u", val) == 1)
Jens Axboeebac4652005-12-08 15:25:21 +0100448 return 0;
449
450 return 1;
451}
452
453static int check_strset(char *p, char *name)
454{
455 return strncmp(p, name, strlen(name));
456}
457
458static int is_empty_or_comment(char *line)
459{
460 unsigned int i;
461
462 for (i = 0; i < strlen(line); i++) {
463 if (line[i] == ';')
464 return 1;
465 if (!isspace(line[i]) && !iscntrl(line[i]))
466 return 0;
467 }
468
469 return 1;
470}
471
472static int str_rw_cb(struct thread_data *td, char *mem)
473{
474 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
475 td->ddir = DDIR_READ;
476 td->sequential = 1;
477 return 0;
478 } else if (!strncmp(mem, "randread", 8)) {
479 td->ddir = DDIR_READ;
480 td->sequential = 0;
481 return 0;
482 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
483 td->ddir = DDIR_WRITE;
484 td->sequential = 1;
485 return 0;
486 } else if (!strncmp(mem, "randwrite", 9)) {
487 td->ddir = DDIR_WRITE;
488 td->sequential = 0;
489 return 0;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200490 } else if (!strncmp(mem, "rw", 2)) {
491 td->ddir = 0;
492 td->iomix = 1;
493 td->sequential = 1;
494 return 0;
495 } else if (!strncmp(mem, "randrw", 6)) {
496 td->ddir = 0;
497 td->iomix = 1;
498 td->sequential = 0;
499 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100500 }
501
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200502 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100503 return 1;
504}
505
506static int str_verify_cb(struct thread_data *td, char *mem)
507{
508 if (!strncmp(mem, "0", 1)) {
509 td->verify = VERIFY_NONE;
510 return 0;
511 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
512 td->verify = VERIFY_MD5;
513 return 0;
514 } else if (!strncmp(mem, "crc32", 5)) {
515 td->verify = VERIFY_CRC32;
516 return 0;
517 }
518
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200519 log_err("fio: verify types: md5, crc32\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100520 return 1;
521}
522
523static int str_mem_cb(struct thread_data *td, char *mem)
524{
525 if (!strncmp(mem, "malloc", 6)) {
526 td->mem_type = MEM_MALLOC;
527 return 0;
528 } else if (!strncmp(mem, "shm", 3)) {
529 td->mem_type = MEM_SHM;
530 return 0;
531 } else if (!strncmp(mem, "mmap", 4)) {
532 td->mem_type = MEM_MMAP;
533 return 0;
534 }
535
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200536 log_err("fio: mem type: malloc, shm, mmap\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100537 return 1;
538}
539
540static int str_ioengine_cb(struct thread_data *td, char *str)
541{
542 if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
543 !strncmp(str, "libaio", 6)) {
544 strcpy(td->io_engine_name, "libaio");
545 td->io_engine = FIO_LIBAIO;
546 return 0;
547 } else if (!strncmp(str, "posixaio", 8)) {
548 strcpy(td->io_engine_name, "posixaio");
549 td->io_engine = FIO_POSIXAIO;
550 return 0;
551 } else if (!strncmp(str, "sync", 4)) {
552 strcpy(td->io_engine_name, "sync");
553 td->io_engine = FIO_SYNCIO;
554 return 0;
555 } else if (!strncmp(str, "mmap", 4)) {
556 strcpy(td->io_engine_name, "mmap");
557 td->io_engine = FIO_MMAPIO;
558 return 0;
559 } else if (!strncmp(str, "sgio", 4)) {
560 strcpy(td->io_engine_name, "sgio");
561 td->io_engine = FIO_SGIO;
562 return 0;
Jens Axboe8756e4d2006-05-27 20:24:53 +0200563 } else if (!strncmp(str, "splice", 6)) {
564 strcpy(td->io_engine_name, "splice");
565 td->io_engine = FIO_SPLICEIO;
566 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100567 }
568
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200569 log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100570 return 1;
571}
572
Jens Axboe07261982006-06-07 10:51:12 +0200573/*
574 * This is our [ini] type file parser.
575 */
Jens Axboeebac4652005-12-08 15:25:21 +0100576int parse_jobs_ini(char *file)
577{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200578 unsigned int prioclass, prio, cpu, global, il;
Jens Axboeebac4652005-12-08 15:25:21 +0100579 unsigned long long ull;
580 unsigned long ul1, ul2;
581 struct thread_data *td;
Jens Axboeef899b62006-06-06 09:31:00 +0200582 char *string, *name, *tmpbuf;
Jens Axboeebac4652005-12-08 15:25:21 +0100583 fpos_t off;
584 FILE *f;
585 char *p;
Jens Axboe972cfd22006-06-09 11:08:56 +0200586 int ret = 0, stonewall = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100587
588 f = fopen(file, "r");
589 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200590 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100591 return 1;
592 }
593
594 string = malloc(4096);
595 name = malloc(256);
Jens Axboeef899b62006-06-06 09:31:00 +0200596 tmpbuf = malloc(4096);
Jens Axboeebac4652005-12-08 15:25:21 +0100597
598 while ((p = fgets(string, 4096, f)) != NULL) {
Jens Axboe45410ac2006-06-07 11:10:39 +0200599 if (ret)
600 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100601 if (is_empty_or_comment(p))
602 continue;
603 if (sscanf(p, "[%s]", name) != 1)
604 continue;
605
606 global = !strncmp(name, "global", 6);
607
608 name[strlen(name) - 1] = '\0';
609
610 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200611 if (!td) {
612 ret = 1;
613 break;
614 }
Jens Axboeebac4652005-12-08 15:25:21 +0100615
Jens Axboe972cfd22006-06-09 11:08:56 +0200616 /*
617 * Seperate multiple job files by a stonewall
618 */
619 if (stonewall) {
620 td->stonewall = stonewall;
621 stonewall = 0;
622 }
623
Jens Axboeebac4652005-12-08 15:25:21 +0100624 fgetpos(f, &off);
625 while ((p = fgets(string, 4096, f)) != NULL) {
626 if (is_empty_or_comment(p))
627 continue;
628 if (strstr(p, "["))
629 break;
Jens Axboeb6754f92006-05-30 14:29:32 +0200630 strip_blank_front(&p);
Jens Axboe4ae3f762006-05-30 13:35:14 +0200631 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200632
Jens Axboeebac4652005-12-08 15:25:21 +0100633 if (!check_int(p, "prio", &prio)) {
634#ifndef FIO_HAVE_IOPRIO
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200635 log_err("io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200636 ret = 1;
637 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100638#endif
Jens Axboe8756e4d2006-05-27 20:24:53 +0200639 td->ioprio |= prio;
Jens Axboeebac4652005-12-08 15:25:21 +0100640 fgetpos(f, &off);
641 continue;
642 }
643 if (!check_int(p, "prioclass", &prioclass)) {
644#ifndef FIO_HAVE_IOPRIO
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200645 log_err("io priorities not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200646 ret = 1;
647 break;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200648#else
Jens Axboe8756e4d2006-05-27 20:24:53 +0200649 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
Jens Axboeebac4652005-12-08 15:25:21 +0100650 fgetpos(f, &off);
651 continue;
Jens Axboe5c4e1db2006-06-07 14:17:08 +0200652#endif
Jens Axboeebac4652005-12-08 15:25:21 +0100653 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200654 if (!check_int(p, "direct", &il)) {
655 td->odirect = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100656 fgetpos(f, &off);
657 continue;
658 }
659 if (!check_int(p, "rate", &td->rate)) {
660 fgetpos(f, &off);
661 continue;
662 }
663 if (!check_int(p, "ratemin", &td->ratemin)) {
664 fgetpos(f, &off);
665 continue;
666 }
667 if (!check_int(p, "ratecycle", &td->ratecycle)) {
668 fgetpos(f, &off);
669 continue;
670 }
671 if (!check_int(p, "thinktime", &td->thinktime)) {
672 fgetpos(f, &off);
673 continue;
674 }
675 if (!check_int(p, "cpumask", &cpu)) {
676#ifndef FIO_HAVE_CPU_AFFINITY
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200677 log_err("cpu affinity not available\n");
Jens Axboe45410ac2006-06-07 11:10:39 +0200678 ret = 1;
679 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100680#endif
681 fill_cpu_mask(td->cpumask, cpu);
682 fgetpos(f, &off);
683 continue;
684 }
685 if (!check_int(p, "fsync", &td->fsync_blocks)) {
686 fgetpos(f, &off);
Jens Axboefc1a4712006-05-30 13:04:05 +0200687 td->end_fsync = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100688 continue;
689 }
690 if (!check_int(p, "startdelay", &td->start_delay)) {
691 fgetpos(f, &off);
692 continue;
693 }
694 if (!check_int(p, "timeout", &td->timeout)) {
695 fgetpos(f, &off);
696 continue;
697 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200698 if (!check_int(p, "invalidate", &il)) {
699 td->invalidate_cache = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100700 fgetpos(f, &off);
701 continue;
702 }
703 if (!check_int(p, "iodepth", &td->iodepth)) {
704 fgetpos(f, &off);
705 continue;
706 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200707 if (!check_int(p, "sync", &il)) {
708 td->sync_io = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100709 fgetpos(f, &off);
710 continue;
711 }
712 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
713 fgetpos(f, &off);
714 continue;
715 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200716 if (!check_int(p, "create_serialize", &il)) {
717 td->create_serialize = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100718 fgetpos(f, &off);
719 continue;
720 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200721 if (!check_int(p, "create_fsync", &il)) {
722 td->create_fsync = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100723 fgetpos(f, &off);
724 continue;
725 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200726 if (!check_int(p, "end_fsync", &il)) {
727 td->end_fsync = il;
Jens Axboefc1a4712006-05-30 13:04:05 +0200728 fgetpos(f, &off);
729 continue;
730 }
Jens Axboeebac4652005-12-08 15:25:21 +0100731 if (!check_int(p, "loops", &td->loops)) {
732 fgetpos(f, &off);
733 continue;
734 }
735 if (!check_int(p, "numjobs", &td->numjobs)) {
736 fgetpos(f, &off);
737 continue;
738 }
Jens Axboee9c047a2006-06-07 21:13:04 +0200739 if (!check_int(p, "overwrite", &il)) {
740 td->overwrite = il;
Jens Axboeebac4652005-12-08 15:25:21 +0100741 fgetpos(f, &off);
742 continue;
743 }
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200744 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
745 fgetpos(f, &off);
746 continue;
747 }
748 if (!check_int(p, "rwmixread", &il)) {
749 if (il > 100)
750 il = 100;
751 td->rwmixread = il;
752 fgetpos(f, &off);
753 continue;
754 }
755 if (!check_int(p, "rwmixwrite", &il)) {
756 if (il > 100)
757 il = 100;
758 td->rwmixread = 100 - il;
759 fgetpos(f, &off);
760 continue;
761 }
Jens Axboeb6f4d882006-06-02 10:32:51 +0200762 if (!check_int(p, "nice", &td->nice)) {
763 fgetpos(f, &off);
764 continue;
765 }
Jens Axboeebac4652005-12-08 15:25:21 +0100766 if (!check_range(p, "bsrange", &ul1, &ul2)) {
767 if (ul1 > ul2) {
768 td->max_bs = ul1;
769 td->min_bs = ul2;
770 } else {
771 td->max_bs = ul2;
772 td->min_bs = ul1;
773 }
774 fgetpos(f, &off);
775 continue;
776 }
777 if (!check_strcnv(p, "bs", &ull)) {
778 td->bs = ull;
779 fgetpos(f, &off);
780 continue;
781 }
782 if (!check_strcnv(p, "size", &td->file_size)) {
783 fgetpos(f, &off);
784 continue;
785 }
786 if (!check_strcnv(p, "offset", &td->file_offset)) {
787 fgetpos(f, &off);
788 continue;
789 }
Jens Axboe20dc95c2005-12-09 10:29:35 +0100790 if (!check_strcnv(p, "zonesize", &td->zone_size)) {
791 fgetpos(f, &off);
792 continue;
793 }
794 if (!check_strcnv(p, "zoneskip", &td->zone_skip)) {
795 fgetpos(f, &off);
796 continue;
797 }
Jens Axboec04f7ec2006-05-31 10:13:16 +0200798 if (!check_strcnv(p, "lockmem", &mlock_size)) {
799 fgetpos(f, &off);
800 continue;
801 }
Jens Axboeef899b62006-06-06 09:31:00 +0200802 if (!check_strstore(p, "directory", tmpbuf)) {
803 td->directory = strdup(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100804 fgetpos(f, &off);
805 continue;
806 }
Jens Axboe01452052006-06-07 10:29:47 +0200807 if (!check_strstore(p, "name", tmpbuf)) {
808 snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
809 fgetpos(f, &off);
810 continue;
811 }
Jens Axboeebac4652005-12-08 15:25:21 +0100812 if (!check_str(p, "mem", str_mem_cb, td)) {
813 fgetpos(f, &off);
814 continue;
815 }
816 if (!check_str(p, "verify", str_verify_cb, td)) {
817 fgetpos(f, &off);
818 continue;
819 }
820 if (!check_str(p, "rw", str_rw_cb, td)) {
821 fgetpos(f, &off);
822 continue;
823 }
824 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
825 fgetpos(f, &off);
826 continue;
827 }
828 if (!check_strset(p, "create")) {
829 td->create_file = 1;
830 fgetpos(f, &off);
831 continue;
832 }
833 if (!check_strset(p, "exitall")) {
834 exitall_on_terminate = 1;
835 fgetpos(f, &off);
836 continue;
837 }
838 if (!check_strset(p, "stonewall")) {
839 td->stonewall = 1;
840 fgetpos(f, &off);
841 continue;
842 }
843 if (!check_strset(p, "thread")) {
844 td->use_thread = 1;
845 fgetpos(f, &off);
846 continue;
847 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200848 if (!check_strstore(p, "iolog", tmpbuf)) {
Jens Axboe4f693b92006-06-07 22:52:28 +0200849 if (td->write_iolog) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200850 log_err("fio: read iolog overrides given write_iolog\n");
Jens Axboeaf8c0b42006-06-07 22:33:17 +0200851 free(td->iolog_file);
Jens Axboe4f693b92006-06-07 22:52:28 +0200852 td->write_iolog = 0;
853 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200854 td->iolog_file = strdup(tmpbuf);
Jens Axboe843a7412006-06-01 21:14:21 -0700855 td->read_iolog = 1;
Jens Axboe843a7412006-06-01 21:14:21 -0700856 fgetpos(f, &off);
857 continue;
858 }
Jens Axboeaf8c0b42006-06-07 22:33:17 +0200859 if (!check_strstore(p, "write_iolog", tmpbuf)) {
860 if (!td->read_iolog) {
861 td->iolog_file = strdup(tmpbuf);
862 td->write_iolog = 1;
Jens Axboe4f693b92006-06-07 22:52:28 +0200863 } else
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200864 log_err("fio: read iolog overrides given write_iolog\n");
Jens Axboeaea47d42006-05-26 19:27:29 +0200865 fgetpos(f, &off);
866 continue;
867 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200868 if (!check_strstore(p, "exec_prerun", tmpbuf)) {
869 td->exec_prerun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200870 fgetpos(f, &off);
871 continue;
872 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200873 if (!check_strstore(p, "exec_postrun", tmpbuf)) {
874 td->exec_postrun = strdup(tmpbuf);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200875 fgetpos(f, &off);
876 continue;
877 }
Jens Axboe80b9feb2006-06-07 10:34:49 +0200878 if (!check_strstore(p, "ioscheduler", tmpbuf)) {
Jens Axboe22f78b32006-06-07 11:30:07 +0200879#ifndef FIO_HAVE_IOSCHED_SWITCH
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200880 log_err("io scheduler switching not available\n");
Jens Axboe22f78b32006-06-07 11:30:07 +0200881 ret = 1;
882 break;
883#else
Jens Axboe80b9feb2006-06-07 10:34:49 +0200884 td->ioscheduler = strdup(tmpbuf);
Jens Axboeda867742006-06-06 10:39:10 -0700885 fgetpos(f, &off);
886 continue;
Jens Axboe22f78b32006-06-07 11:30:07 +0200887#endif
Jens Axboeda867742006-06-06 10:39:10 -0700888 }
Jens Axboeebac4652005-12-08 15:25:21 +0100889
Jens Axboe45410ac2006-06-07 11:10:39 +0200890 /*
891 * Don't break here, continue parsing options so we
892 * dump all the bad ones. Makes trial/error fixups
893 * easier on the user.
894 */
Jens Axboeebac4652005-12-08 15:25:21 +0100895 printf("Client%d: bad option %s\n",td->thread_number,p);
Jens Axboe45410ac2006-06-07 11:10:39 +0200896 ret = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100897 }
Jens Axboeebac4652005-12-08 15:25:21 +0100898
Jens Axboe45410ac2006-06-07 11:10:39 +0200899 if (!ret) {
900 fsetpos(f, &off);
901 ret = add_job(td, name, 0);
902 }
903 if (ret)
904 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100905 }
906
907 free(string);
908 free(name);
Jens Axboeef899b62006-06-06 09:31:00 +0200909 free(tmpbuf);
Jens Axboeebac4652005-12-08 15:25:21 +0100910 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200911 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100912}
913
914static int fill_def_thread(void)
915{
916 memset(&def_thread, 0, sizeof(def_thread));
917
918 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
919 perror("sched_getaffinity");
920 return 1;
921 }
922
923 /*
924 * fill globals
925 */
926 def_thread.ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200927 def_thread.iomix = 0;
Jens Axboe972cfd22006-06-09 11:08:56 +0200928 def_thread.bs = def_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100929 def_thread.min_bs = -1;
930 def_thread.max_bs = -1;
931 def_thread.io_engine = DEF_IO_ENGINE;
932 strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
Jens Axboe972cfd22006-06-09 11:08:56 +0200933 def_thread.odirect = def_odirect;
Jens Axboeebac4652005-12-08 15:25:21 +0100934 def_thread.ratecycle = DEF_RATE_CYCLE;
Jens Axboe972cfd22006-06-09 11:08:56 +0200935 def_thread.sequential = def_sequential;
936 def_thread.timeout = def_timeout;
Jens Axboeebac4652005-12-08 15:25:21 +0100937 def_thread.create_file = DEF_CREATE;
938 def_thread.overwrite = DEF_OVERWRITE;
939 def_thread.invalidate_cache = DEF_INVALIDATE;
940 def_thread.sync_io = DEF_SYNCIO;
941 def_thread.mem_type = MEM_MALLOC;
942 def_thread.bw_avg_time = DEF_BWAVGTIME;
943 def_thread.create_serialize = DEF_CREATE_SER;
944 def_thread.create_fsync = DEF_CREATE_FSYNC;
945 def_thread.loops = DEF_LOOPS;
946 def_thread.verify = DEF_VERIFY;
947 def_thread.stonewall = DEF_STONEWALL;
948 def_thread.numjobs = DEF_NUMJOBS;
949 def_thread.use_thread = DEF_USE_THREAD;
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200950 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
951 def_thread.rwmixread = DEF_RWMIX_READ;
Jens Axboeb6f4d882006-06-02 10:32:51 +0200952 def_thread.nice = DEF_NICE;
Jens Axboeebac4652005-12-08 15:25:21 +0100953#ifdef FIO_HAVE_DISK_UTIL
954 def_thread.do_disk_util = 1;
955#endif
956
957 return 0;
958}
959
Jens Axboe4785f992006-05-26 03:59:10 +0200960static void usage(char *name)
961{
962 printf("%s\n", fio_version_string);
963 printf("\t-s IO is sequential\n");
964 printf("\t-b Block size in KiB for each IO\n");
965 printf("\t-t Runtime in seconds\n");
966 printf("\t-R Exit all threads on failure to meet rate goal\n");
967 printf("\t-o Use O_DIRECT\n");
968 printf("\t-l Generate per-job latency logs\n");
969 printf("\t-w Generate per-job bandwidth logs\n");
970 printf("\t-f Job file (Required)\n");
971 printf("\t-v Print version info and exit\n");
972}
973
Jens Axboe972cfd22006-06-09 11:08:56 +0200974static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +0100975{
Jens Axboe972cfd22006-06-09 11:08:56 +0200976 int c, idx = 1, ini_idx = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100977
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200978 while ((c = getopt(argc, argv, "s:b:t:r:R:o:f:lwvhO:")) != EOF) {
Jens Axboeebac4652005-12-08 15:25:21 +0100979 switch (c) {
980 case 's':
Jens Axboe972cfd22006-06-09 11:08:56 +0200981 def_sequential = !!atoi(optarg);
982 idx++;
Jens Axboeebac4652005-12-08 15:25:21 +0100983 break;
984 case 'b':
Jens Axboe972cfd22006-06-09 11:08:56 +0200985 def_bs = atoi(optarg);
986 def_bs <<= 10;
987 if (!def_bs) {
Jens Axboeebac4652005-12-08 15:25:21 +0100988 printf("bad block size\n");
Jens Axboe972cfd22006-06-09 11:08:56 +0200989 def_bs = DEF_BS;
Jens Axboeebac4652005-12-08 15:25:21 +0100990 }
Jens Axboe972cfd22006-06-09 11:08:56 +0200991 idx++;
Jens Axboeebac4652005-12-08 15:25:21 +0100992 break;
993 case 't':
Jens Axboe972cfd22006-06-09 11:08:56 +0200994 def_timeout = atoi(optarg);
995 idx++;
Jens Axboeebac4652005-12-08 15:25:21 +0100996 break;
997 case 'r':
Jens Axboe972cfd22006-06-09 11:08:56 +0200998 def_repeatable = !!atoi(optarg);
999 idx++;
Jens Axboeebac4652005-12-08 15:25:21 +01001000 break;
1001 case 'R':
1002 rate_quit = !!atoi(optarg);
Jens Axboe972cfd22006-06-09 11:08:56 +02001003 idx++;
Jens Axboeebac4652005-12-08 15:25:21 +01001004 break;
1005 case 'o':
Jens Axboe972cfd22006-06-09 11:08:56 +02001006 def_odirect = !!atoi(optarg);
1007 idx++;
Jens Axboeebac4652005-12-08 15:25:21 +01001008 break;
1009 case 'f':
Jens Axboe972cfd22006-06-09 11:08:56 +02001010 ini_idx++;
1011 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1012 ini_file[ini_idx - 1] = strdup(optarg);
1013 idx++;
Jens Axboeebac4652005-12-08 15:25:21 +01001014 break;
1015 case 'l':
1016 write_lat_log = 1;
Jens Axboe972cfd22006-06-09 11:08:56 +02001017 idx++;
Jens Axboeebac4652005-12-08 15:25:21 +01001018 break;
1019 case 'w':
1020 write_bw_log = 1;
Jens Axboe972cfd22006-06-09 11:08:56 +02001021 idx++;
Jens Axboeebac4652005-12-08 15:25:21 +01001022 break;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001023 case 'O':
1024 f_out = fopen(optarg, "w+");
1025 if (!f_out) {
1026 perror("fopen output");
1027 exit(1);
1028 }
1029 f_err = f_out;
Jens Axboe972cfd22006-06-09 11:08:56 +02001030 idx++;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001031 break;
Jens Axboe4785f992006-05-26 03:59:10 +02001032 case 'h':
1033 usage(argv[0]);
1034 exit(0);
Jens Axboeebac4652005-12-08 15:25:21 +01001035 case 'v':
1036 printf("%s\n", fio_version_string);
1037 exit(0);
1038 }
1039 }
Jens Axboec9fad892006-05-27 17:26:50 +02001040
Jens Axboe972cfd22006-06-09 11:08:56 +02001041 while (idx < argc) {
1042 ini_idx++;
1043 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1044 ini_file[ini_idx - 1] = strdup(argv[idx]);
1045 idx++;
1046 }
1047
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001048 if (!f_out) {
1049 f_out = stdout;
1050 f_err = stderr;
1051 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001052
1053 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +01001054}
1055
1056static void free_shm(void)
1057{
1058 struct shmid_ds sbuf;
1059
1060 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001061 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001062 threads = NULL;
1063 shmctl(shm_id, IPC_RMID, &sbuf);
1064 }
1065}
1066
1067static int setup_thread_area(void)
1068{
1069 /*
1070 * 1024 is too much on some machines, scale max_jobs if
1071 * we get a failure that looks like too large a shm segment
1072 */
1073 do {
1074 int s = max_jobs * sizeof(struct thread_data);
1075
1076 shm_id = shmget(0, s, IPC_CREAT | 0600);
1077 if (shm_id != -1)
1078 break;
1079 if (errno != EINVAL) {
1080 perror("shmget");
1081 break;
1082 }
1083
1084 max_jobs >>= 1;
1085 } while (max_jobs);
1086
1087 if (shm_id == -1)
1088 return 1;
1089
1090 threads = shmat(shm_id, NULL, 0);
1091 if (threads == (void *) -1) {
1092 perror("shmat");
1093 return 1;
1094 }
1095
1096 atexit(free_shm);
1097 return 0;
1098}
1099
1100int parse_options(int argc, char *argv[])
1101{
Jens Axboe972cfd22006-06-09 11:08:56 +02001102 int job_files, i;
1103
Jens Axboeebac4652005-12-08 15:25:21 +01001104 if (setup_thread_area())
1105 return 1;
1106 if (fill_def_thread())
1107 return 1;
1108
Jens Axboe972cfd22006-06-09 11:08:56 +02001109 job_files = parse_cmd_line(argc, argv);
1110 if (!job_files) {
1111 log_err("Need job file(s)\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001112 usage(argv[0]);
Jens Axboeebac4652005-12-08 15:25:21 +01001113 return 1;
1114 }
1115
Jens Axboe972cfd22006-06-09 11:08:56 +02001116 for (i = 0; i < job_files; i++) {
1117 if (fill_def_thread())
1118 return 1;
1119 if (parse_jobs_ini(ini_file[i]))
1120 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001121 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001122 }
Jens Axboeebac4652005-12-08 15:25:21 +01001123
Jens Axboe88c6ed82006-06-09 11:28:10 +02001124 free(ini_file);
Jens Axboeebac4652005-12-08 15:25:21 +01001125 return 0;
1126}