Commit | Line | Data |
---|---|---|
906c8d75 | 1 | /* |
cb2c86fd | 2 | * This file contains job initialization and setup functions. |
906c8d75 | 3 | */ |
ebac4655 JA |
4 | #include <stdio.h> |
5 | #include <stdlib.h> | |
6 | #include <unistd.h> | |
7 | #include <fcntl.h> | |
8 | #include <ctype.h> | |
9 | #include <string.h> | |
10 | #include <errno.h> | |
11 | #include <sys/ipc.h> | |
12 | #include <sys/shm.h> | |
13 | #include <sys/types.h> | |
14 | #include <sys/stat.h> | |
15 | ||
16 | #include "fio.h" | |
cb2c86fd | 17 | #include "parse.h" |
ebac4655 | 18 | |
906c8d75 JA |
19 | /* |
20 | * The default options | |
21 | */ | |
20dc95c4 JA |
22 | #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) | |
ebac4655 | 27 | #define DEF_IO_ENGINE_NAME "sync" |
20dc95c4 JA |
28 | #define DEF_SEQUENTIAL (1) |
29 | #define DEF_RAND_REPEAT (1) | |
30 | #define DEF_OVERWRITE (1) | |
20dc95c4 JA |
31 | #define DEF_INVALIDATE (1) |
32 | #define DEF_SYNCIO (0) | |
33 | #define DEF_RANDSEED (0xb1899bedUL) | |
34 | #define DEF_BWAVGTIME (500) | |
35 | #define DEF_CREATE_SER (1) | |
ebac4655 | 36 | #define DEF_CREATE_FSYNC (1) |
20dc95c4 JA |
37 | #define DEF_LOOPS (1) |
38 | #define DEF_VERIFY (0) | |
39 | #define DEF_STONEWALL (0) | |
40 | #define DEF_NUMJOBS (1) | |
41 | #define DEF_USE_THREAD (0) | |
42 | #define DEF_FILE_SIZE (1024 * 1024 * 1024UL) | |
43 | #define DEF_ZONE_SIZE (0) | |
44 | #define DEF_ZONE_SKIP (0) | |
a6ccc7be JA |
45 | #define DEF_RWMIX_CYCLE (500) |
46 | #define DEF_RWMIX_READ (50) | |
b6f4d880 | 47 | #define DEF_NICE (0) |
53cdc686 | 48 | #define DEF_NR_FILES (1) |
f6cbb269 | 49 | #define DEF_UNLINK (0) |
ec94ec56 JA |
50 | #define DEF_WRITE_BW_LOG (0) |
51 | #define DEF_WRITE_LAT_LOG (0) | |
ebac4655 | 52 | |
972cfd25 | 53 | static int def_timeout = DEF_TIMEOUT; |
972cfd25 | 54 | |
f4866ec5 | 55 | static char fio_version_string[] = "fio 1.5"; |
ebac4655 | 56 | |
972cfd25 | 57 | static char **ini_file; |
ebac4655 JA |
58 | static int max_jobs = MAX_JOBS; |
59 | ||
60 | struct thread_data def_thread; | |
61 | struct thread_data *threads = NULL; | |
62 | ||
63 | int rate_quit = 0; | |
ebac4655 | 64 | int exitall_on_terminate = 0; |
c6ae0a5b | 65 | int terse_output = 0; |
c04f7ec3 | 66 | unsigned long long mlock_size = 0; |
eb8bbf48 JA |
67 | FILE *f_out = NULL; |
68 | FILE *f_err = NULL; | |
ebac4655 | 69 | |
ec94ec56 JA |
70 | static int write_lat_log = DEF_WRITE_LAT_LOG; |
71 | static int write_bw_log = DEF_WRITE_BW_LOG; | |
72 | ||
906c8d75 JA |
73 | /* |
74 | * Return a free job structure. | |
75 | */ | |
ebac4655 JA |
76 | static struct thread_data *get_new_job(int global, struct thread_data *parent) |
77 | { | |
78 | struct thread_data *td; | |
79 | ||
80 | if (global) | |
81 | return &def_thread; | |
82 | if (thread_number >= max_jobs) | |
83 | return NULL; | |
84 | ||
85 | td = &threads[thread_number++]; | |
ddaeaa5a JA |
86 | *td = *parent; |
87 | td->name[0] = '\0'; | |
ebac4655 | 88 | |
ebac4655 | 89 | td->thread_number = thread_number; |
ebac4655 JA |
90 | return td; |
91 | } | |
92 | ||
93 | static void put_job(struct thread_data *td) | |
94 | { | |
95 | memset(&threads[td->thread_number - 1], 0, sizeof(*td)); | |
96 | thread_number--; | |
97 | } | |
98 | ||
906c8d75 JA |
99 | /* |
100 | * Adds a job to the list of things todo. Sanitizes the various options | |
101 | * to make sure we don't have conflicts, and initializes various | |
102 | * members of td. | |
103 | */ | |
75154845 | 104 | static int add_job(struct thread_data *td, const char *jobname, int job_add_num) |
ebac4655 | 105 | { |
3d60d1ed JA |
106 | char *ddir_str[] = { "read", "write", "randread", "randwrite", |
107 | "rw", NULL, "randrw" }; | |
ebac4655 | 108 | struct stat sb; |
53cdc686 JA |
109 | int numjobs, ddir, i; |
110 | struct fio_file *f; | |
ebac4655 JA |
111 | |
112 | #ifndef FIO_HAVE_LIBAIO | |
113 | if (td->io_engine == FIO_LIBAIO) { | |
3b70d7e5 | 114 | log_err("Linux libaio not available\n"); |
ebac4655 JA |
115 | return 1; |
116 | } | |
117 | #endif | |
118 | #ifndef FIO_HAVE_POSIXAIO | |
119 | if (td->io_engine == FIO_POSIXAIO) { | |
3b70d7e5 | 120 | log_err("posix aio not available\n"); |
ebac4655 JA |
121 | return 1; |
122 | } | |
123 | #endif | |
ebac4655 JA |
124 | |
125 | /* | |
126 | * the def_thread is just for options, it's not a real job | |
127 | */ | |
128 | if (td == &def_thread) | |
129 | return 0; | |
130 | ||
df64119d JA |
131 | /* |
132 | * Set default io engine, if none set | |
133 | */ | |
134 | if (!td->io_ops) { | |
135 | td->io_ops = load_ioengine(td, DEF_IO_ENGINE_NAME); | |
136 | if (!td->io_ops) { | |
137 | log_err("default engine %s not there?\n", DEF_IO_ENGINE_NAME); | |
138 | return 1; | |
139 | } | |
140 | } | |
141 | ||
2866c82d | 142 | if (td->io_ops->flags & FIO_SYNCIO) |
ebac4655 JA |
143 | td->iodepth = 1; |
144 | else { | |
145 | if (!td->iodepth) | |
841ddd13 | 146 | td->iodepth = td->nr_files; |
ebac4655 JA |
147 | } |
148 | ||
20dc95c4 | 149 | /* |
53cdc686 | 150 | * only really works for sequential io for now, and with 1 file |
20dc95c4 | 151 | */ |
53cdc686 | 152 | if (td->zone_size && !td->sequential && td->nr_files == 1) |
20dc95c4 JA |
153 | td->zone_size = 0; |
154 | ||
9cc935a1 JA |
155 | /* |
156 | * Reads can do overwrites, we always need to pre-create the file | |
157 | */ | |
158 | if (td_read(td) || td_rw(td)) | |
159 | td->overwrite = 1; | |
160 | ||
ebac4655 | 161 | td->filetype = FIO_TYPE_FILE; |
0af7b542 JA |
162 | if (!stat(jobname, &sb)) { |
163 | if (S_ISBLK(sb.st_mode)) | |
164 | td->filetype = FIO_TYPE_BD; | |
165 | else if (S_ISCHR(sb.st_mode)) | |
166 | td->filetype = FIO_TYPE_CHAR; | |
167 | } | |
ebac4655 | 168 | |
b2a15192 JA |
169 | if (td->odirect) |
170 | td->io_ops->flags |= FIO_RAWIO; | |
171 | ||
13f8e2d2 JA |
172 | if (td->filename) |
173 | td->nr_uniq_files = 1; | |
174 | else | |
175 | td->nr_uniq_files = td->nr_files; | |
176 | ||
177 | if (td->filetype == FIO_TYPE_FILE || td->filename) { | |
e9c047a0 | 178 | char tmp[PATH_MAX]; |
53cdc686 JA |
179 | int len = 0; |
180 | int i; | |
e9c047a0 | 181 | |
ef899b63 | 182 | if (td->directory && td->directory[0] != '\0') |
53cdc686 | 183 | sprintf(tmp, "%s/", td->directory); |
ebac4655 | 184 | |
53cdc686 JA |
185 | td->files = malloc(sizeof(struct fio_file) * td->nr_files); |
186 | ||
187 | for_each_file(td, f, i) { | |
188 | memset(f, 0, sizeof(*f)); | |
189 | f->fd = -1; | |
190 | ||
13f8e2d2 JA |
191 | if (td->filename) |
192 | sprintf(tmp + len, "%s", td->filename); | |
193 | else | |
194 | sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i); | |
53cdc686 JA |
195 | f->file_name = strdup(tmp); |
196 | } | |
197 | } else { | |
198 | td->nr_files = 1; | |
199 | td->files = malloc(sizeof(struct fio_file)); | |
200 | f = &td->files[0]; | |
201 | ||
202 | memset(f, 0, sizeof(*f)); | |
203 | f->fd = -1; | |
204 | f->file_name = strdup(jobname); | |
205 | } | |
206 | ||
207 | for_each_file(td, f, i) { | |
208 | f->file_size = td->total_file_size / td->nr_files; | |
209 | f->file_offset = td->start_offset; | |
210 | } | |
211 | ||
bbfd6b00 | 212 | fio_sem_init(&td->mutex, 0); |
ebac4655 JA |
213 | |
214 | td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX; | |
215 | td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX; | |
216 | td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX; | |
217 | ||
218 | if (td->min_bs == -1U) | |
219 | td->min_bs = td->bs; | |
220 | if (td->max_bs == -1U) | |
221 | td->max_bs = td->bs; | |
3d60d1ed | 222 | if (td_read(td) && !td_rw(td)) |
ebac4655 JA |
223 | td->verify = 0; |
224 | ||
225 | if (td->stonewall && td->thread_number > 1) | |
226 | groupid++; | |
227 | ||
228 | td->groupid = groupid; | |
229 | ||
230 | if (setup_rate(td)) | |
231 | goto err; | |
232 | ||
ec94ec56 | 233 | if (td->write_lat_log) { |
ebac4655 JA |
234 | setup_log(&td->slat_log); |
235 | setup_log(&td->clat_log); | |
236 | } | |
ec94ec56 | 237 | if (td->write_bw_log) |
ebac4655 JA |
238 | setup_log(&td->bw_log); |
239 | ||
01452055 JA |
240 | if (td->name[0] == '\0') |
241 | snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number); | |
242 | ||
3d60d1ed | 243 | ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2); |
75154845 | 244 | |
c6ae0a5b | 245 | if (!terse_output) { |
b990b5c0 | 246 | if (!job_add_num) { |
2866c82d | 247 | if (td->io_ops->flags & FIO_CPUIO) |
b990b5c0 JA |
248 | fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle); |
249 | else | |
2866c82d | 250 | 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); |
b990b5c0 | 251 | } else if (job_add_num == 1) |
c6ae0a5b JA |
252 | fprintf(f_out, "...\n"); |
253 | } | |
ebac4655 JA |
254 | |
255 | /* | |
256 | * recurse add identical jobs, clear numjobs and stonewall options | |
257 | * as they don't apply to sub-jobs | |
258 | */ | |
259 | numjobs = td->numjobs; | |
260 | while (--numjobs) { | |
261 | struct thread_data *td_new = get_new_job(0, td); | |
262 | ||
263 | if (!td_new) | |
264 | goto err; | |
265 | ||
266 | td_new->numjobs = 1; | |
267 | td_new->stonewall = 0; | |
75154845 | 268 | job_add_num = numjobs - 1; |
ebac4655 | 269 | |
75154845 | 270 | if (add_job(td_new, jobname, job_add_num)) |
ebac4655 JA |
271 | goto err; |
272 | } | |
273 | return 0; | |
274 | err: | |
275 | put_job(td); | |
276 | return -1; | |
277 | } | |
278 | ||
906c8d75 JA |
279 | /* |
280 | * Initialize the various random states we need (random io, block size ranges, | |
281 | * read/write mix, etc). | |
282 | */ | |
ebac4655 JA |
283 | int init_random_state(struct thread_data *td) |
284 | { | |
a6ccc7be | 285 | unsigned long seeds[4]; |
53cdc686 | 286 | int fd, num_maps, blocks, i; |
0ab8db89 | 287 | struct fio_file *f; |
ebac4655 | 288 | |
1ac267bb | 289 | fd = open("/dev/urandom", O_RDONLY); |
ebac4655 JA |
290 | if (fd == -1) { |
291 | td_verror(td, errno); | |
292 | return 1; | |
293 | } | |
294 | ||
a6ccc7be | 295 | if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) { |
ebac4655 JA |
296 | td_verror(td, EIO); |
297 | close(fd); | |
298 | return 1; | |
299 | } | |
300 | ||
301 | close(fd); | |
302 | ||
6dfd46b9 JA |
303 | os_random_seed(seeds[0], &td->bsrange_state); |
304 | os_random_seed(seeds[1], &td->verify_state); | |
305 | os_random_seed(seeds[2], &td->rwmix_state); | |
ebac4655 JA |
306 | |
307 | if (td->sequential) | |
308 | return 0; | |
309 | ||
9ebc27e1 | 310 | if (td->rand_repeatable) |
a6ccc7be | 311 | seeds[3] = DEF_RANDSEED; |
ebac4655 | 312 | |
0ab8db89 | 313 | for_each_file(td, f, i) { |
53cdc686 JA |
314 | blocks = (f->file_size + td->min_bs - 1) / td->min_bs; |
315 | num_maps = blocks / BLOCKS_PER_MAP; | |
316 | f->file_map = malloc(num_maps * sizeof(long)); | |
317 | f->num_maps = num_maps; | |
318 | memset(f->file_map, 0, num_maps * sizeof(long)); | |
319 | } | |
ebac4655 | 320 | |
6dfd46b9 | 321 | os_random_seed(seeds[3], &td->random_state); |
ebac4655 JA |
322 | return 0; |
323 | } | |
324 | ||
325 | static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu) | |
326 | { | |
327 | #ifdef FIO_HAVE_CPU_AFFINITY | |
328 | unsigned int i; | |
329 | ||
330 | CPU_ZERO(&cpumask); | |
331 | ||
332 | for (i = 0; i < sizeof(int) * 8; i++) { | |
333 | if ((1 << i) & cpu) | |
334 | CPU_SET(i, &cpumask); | |
335 | } | |
336 | #endif | |
337 | } | |
338 | ||
ebac4655 JA |
339 | static int is_empty_or_comment(char *line) |
340 | { | |
341 | unsigned int i; | |
342 | ||
343 | for (i = 0; i < strlen(line); i++) { | |
344 | if (line[i] == ';') | |
345 | return 1; | |
346 | if (!isspace(line[i]) && !iscntrl(line[i])) | |
347 | return 0; | |
348 | } | |
349 | ||
350 | return 1; | |
351 | } | |
352 | ||
cb2c86fd | 353 | static int str_rw_cb(void *data, char *mem) |
ebac4655 | 354 | { |
cb2c86fd JA |
355 | struct thread_data *td = data; |
356 | ||
ebac4655 JA |
357 | if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) { |
358 | td->ddir = DDIR_READ; | |
359 | td->sequential = 1; | |
360 | return 0; | |
361 | } else if (!strncmp(mem, "randread", 8)) { | |
362 | td->ddir = DDIR_READ; | |
363 | td->sequential = 0; | |
364 | return 0; | |
365 | } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) { | |
366 | td->ddir = DDIR_WRITE; | |
367 | td->sequential = 1; | |
368 | return 0; | |
369 | } else if (!strncmp(mem, "randwrite", 9)) { | |
370 | td->ddir = DDIR_WRITE; | |
371 | td->sequential = 0; | |
372 | return 0; | |
3d60d1ed JA |
373 | } else if (!strncmp(mem, "rw", 2)) { |
374 | td->ddir = 0; | |
375 | td->iomix = 1; | |
376 | td->sequential = 1; | |
377 | return 0; | |
378 | } else if (!strncmp(mem, "randrw", 6)) { | |
379 | td->ddir = 0; | |
380 | td->iomix = 1; | |
381 | td->sequential = 0; | |
382 | return 0; | |
ebac4655 JA |
383 | } |
384 | ||
3b70d7e5 | 385 | log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n"); |
ebac4655 JA |
386 | return 1; |
387 | } | |
388 | ||
cb2c86fd | 389 | static int str_verify_cb(void *data, char *mem) |
ebac4655 | 390 | { |
cb2c86fd JA |
391 | struct thread_data *td = data; |
392 | ||
ebac4655 JA |
393 | if (!strncmp(mem, "0", 1)) { |
394 | td->verify = VERIFY_NONE; | |
395 | return 0; | |
396 | } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) { | |
397 | td->verify = VERIFY_MD5; | |
398 | return 0; | |
399 | } else if (!strncmp(mem, "crc32", 5)) { | |
400 | td->verify = VERIFY_CRC32; | |
401 | return 0; | |
402 | } | |
403 | ||
3b70d7e5 | 404 | log_err("fio: verify types: md5, crc32\n"); |
ebac4655 JA |
405 | return 1; |
406 | } | |
407 | ||
cb2c86fd | 408 | static int str_mem_cb(void *data, char *mem) |
ebac4655 | 409 | { |
cb2c86fd JA |
410 | struct thread_data *td = data; |
411 | ||
ebac4655 JA |
412 | if (!strncmp(mem, "malloc", 6)) { |
413 | td->mem_type = MEM_MALLOC; | |
414 | return 0; | |
415 | } else if (!strncmp(mem, "shm", 3)) { | |
416 | td->mem_type = MEM_SHM; | |
417 | return 0; | |
418 | } else if (!strncmp(mem, "mmap", 4)) { | |
419 | td->mem_type = MEM_MMAP; | |
420 | return 0; | |
421 | } | |
422 | ||
3b70d7e5 | 423 | log_err("fio: mem type: malloc, shm, mmap\n"); |
ebac4655 JA |
424 | return 1; |
425 | } | |
426 | ||
cb2c86fd | 427 | static int str_ioengine_cb(void *data, char *str) |
ebac4655 | 428 | { |
cb2c86fd JA |
429 | struct thread_data *td = data; |
430 | ||
2866c82d JA |
431 | td->io_ops = load_ioengine(td, str); |
432 | if (td->io_ops) | |
b990b5c0 | 433 | return 0; |
ebac4655 | 434 | |
b990b5c0 | 435 | log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice, cpu\n"); |
ebac4655 JA |
436 | return 1; |
437 | } | |
438 | ||
07261983 JA |
439 | /* |
440 | * This is our [ini] type file parser. | |
441 | */ | |
0c7e37a0 | 442 | int parse_jobs_ini(char *file, int stonewall_flag) |
ebac4655 | 443 | { |
a6ccc7be | 444 | unsigned int prioclass, prio, cpu, global, il; |
ebac4655 JA |
445 | unsigned long long ull; |
446 | unsigned long ul1, ul2; | |
447 | struct thread_data *td; | |
ef899b63 | 448 | char *string, *name, *tmpbuf; |
ebac4655 JA |
449 | fpos_t off; |
450 | FILE *f; | |
451 | char *p; | |
0c7e37a0 | 452 | int ret = 0, stonewall; |
ebac4655 JA |
453 | |
454 | f = fopen(file, "r"); | |
455 | if (!f) { | |
aea47d44 | 456 | perror("fopen job file"); |
ebac4655 JA |
457 | return 1; |
458 | } | |
459 | ||
460 | string = malloc(4096); | |
461 | name = malloc(256); | |
ef899b63 | 462 | tmpbuf = malloc(4096); |
ebac4655 | 463 | |
0c7e37a0 | 464 | stonewall = stonewall_flag; |
ebac4655 | 465 | while ((p = fgets(string, 4096, f)) != NULL) { |
45410acb JA |
466 | if (ret) |
467 | break; | |
ebac4655 JA |
468 | if (is_empty_or_comment(p)) |
469 | continue; | |
470 | if (sscanf(p, "[%s]", name) != 1) | |
471 | continue; | |
472 | ||
473 | global = !strncmp(name, "global", 6); | |
474 | ||
475 | name[strlen(name) - 1] = '\0'; | |
476 | ||
477 | td = get_new_job(global, &def_thread); | |
45410acb JA |
478 | if (!td) { |
479 | ret = 1; | |
480 | break; | |
481 | } | |
ebac4655 | 482 | |
972cfd25 JA |
483 | /* |
484 | * Seperate multiple job files by a stonewall | |
485 | */ | |
f9481919 | 486 | if (!global && stonewall) { |
972cfd25 JA |
487 | td->stonewall = stonewall; |
488 | stonewall = 0; | |
489 | } | |
490 | ||
ebac4655 JA |
491 | fgetpos(f, &off); |
492 | while ((p = fgets(string, 4096, f)) != NULL) { | |
493 | if (is_empty_or_comment(p)) | |
494 | continue; | |
495 | if (strstr(p, "[")) | |
496 | break; | |
b6754f9d | 497 | strip_blank_front(&p); |
4ae3f763 | 498 | strip_blank_end(p); |
aea47d44 | 499 | |
ebac4655 JA |
500 | if (!check_int(p, "prio", &prio)) { |
501 | #ifndef FIO_HAVE_IOPRIO | |
3b70d7e5 | 502 | log_err("io priorities not available\n"); |
45410acb JA |
503 | ret = 1; |
504 | break; | |
ebac4655 | 505 | #endif |
8756e4d4 | 506 | td->ioprio |= prio; |
ebac4655 JA |
507 | fgetpos(f, &off); |
508 | continue; | |
509 | } | |
510 | if (!check_int(p, "prioclass", &prioclass)) { | |
511 | #ifndef FIO_HAVE_IOPRIO | |
3b70d7e5 | 512 | log_err("io priorities not available\n"); |
45410acb JA |
513 | ret = 1; |
514 | break; | |
5c4e1dbc | 515 | #else |
8756e4d4 | 516 | td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT; |
ebac4655 JA |
517 | fgetpos(f, &off); |
518 | continue; | |
5c4e1dbc | 519 | #endif |
ebac4655 | 520 | } |
e9c047a0 JA |
521 | if (!check_int(p, "direct", &il)) { |
522 | td->odirect = il; | |
ebac4655 JA |
523 | fgetpos(f, &off); |
524 | continue; | |
525 | } | |
9ebc27e1 JA |
526 | if (!check_int(p, "rand_repeatable", &il)) { |
527 | td->rand_repeatable = il; | |
528 | fgetpos(f, &off); | |
529 | continue; | |
530 | } | |
ebac4655 JA |
531 | if (!check_int(p, "rate", &td->rate)) { |
532 | fgetpos(f, &off); | |
533 | continue; | |
534 | } | |
535 | if (!check_int(p, "ratemin", &td->ratemin)) { | |
536 | fgetpos(f, &off); | |
537 | continue; | |
538 | } | |
539 | if (!check_int(p, "ratecycle", &td->ratecycle)) { | |
540 | fgetpos(f, &off); | |
541 | continue; | |
542 | } | |
b990b5c0 JA |
543 | if (!check_int(p, "cpuload", &td->cpuload)) { |
544 | fgetpos(f, &off); | |
545 | continue; | |
546 | } | |
547 | if (!check_int(p, "cpuchunks", &td->cpucycle)) { | |
548 | fgetpos(f, &off); | |
549 | continue; | |
550 | } | |
ebac4655 JA |
551 | if (!check_int(p, "thinktime", &td->thinktime)) { |
552 | fgetpos(f, &off); | |
553 | continue; | |
554 | } | |
555 | if (!check_int(p, "cpumask", &cpu)) { | |
556 | #ifndef FIO_HAVE_CPU_AFFINITY | |
3b70d7e5 | 557 | log_err("cpu affinity not available\n"); |
45410acb JA |
558 | ret = 1; |
559 | break; | |
ebac4655 JA |
560 | #endif |
561 | fill_cpu_mask(td->cpumask, cpu); | |
562 | fgetpos(f, &off); | |
563 | continue; | |
564 | } | |
565 | if (!check_int(p, "fsync", &td->fsync_blocks)) { | |
566 | fgetpos(f, &off); | |
fc1a4713 | 567 | td->end_fsync = 1; |
ebac4655 JA |
568 | continue; |
569 | } | |
570 | if (!check_int(p, "startdelay", &td->start_delay)) { | |
571 | fgetpos(f, &off); | |
572 | continue; | |
573 | } | |
906c8d75 | 574 | if (!check_str_time(p, "timeout", &ull)) { |
af959a78 | 575 | td->timeout = ull; |
ebac4655 JA |
576 | fgetpos(f, &off); |
577 | continue; | |
578 | } | |
e9c047a0 JA |
579 | if (!check_int(p, "invalidate", &il)) { |
580 | td->invalidate_cache = il; | |
ebac4655 JA |
581 | fgetpos(f, &off); |
582 | continue; | |
583 | } | |
584 | if (!check_int(p, "iodepth", &td->iodepth)) { | |
585 | fgetpos(f, &off); | |
586 | continue; | |
587 | } | |
e9c047a0 JA |
588 | if (!check_int(p, "sync", &il)) { |
589 | td->sync_io = il; | |
ebac4655 JA |
590 | fgetpos(f, &off); |
591 | continue; | |
592 | } | |
593 | if (!check_int(p, "bwavgtime", &td->bw_avg_time)) { | |
594 | fgetpos(f, &off); | |
595 | continue; | |
596 | } | |
e9c047a0 JA |
597 | if (!check_int(p, "create_serialize", &il)) { |
598 | td->create_serialize = il; | |
ebac4655 JA |
599 | fgetpos(f, &off); |
600 | continue; | |
601 | } | |
e9c047a0 JA |
602 | if (!check_int(p, "create_fsync", &il)) { |
603 | td->create_fsync = il; | |
ebac4655 JA |
604 | fgetpos(f, &off); |
605 | continue; | |
606 | } | |
e9c047a0 JA |
607 | if (!check_int(p, "end_fsync", &il)) { |
608 | td->end_fsync = il; | |
fc1a4713 JA |
609 | fgetpos(f, &off); |
610 | continue; | |
611 | } | |
ebac4655 JA |
612 | if (!check_int(p, "loops", &td->loops)) { |
613 | fgetpos(f, &off); | |
614 | continue; | |
615 | } | |
616 | if (!check_int(p, "numjobs", &td->numjobs)) { | |
617 | fgetpos(f, &off); | |
618 | continue; | |
619 | } | |
e9c047a0 JA |
620 | if (!check_int(p, "overwrite", &il)) { |
621 | td->overwrite = il; | |
ebac4655 JA |
622 | fgetpos(f, &off); |
623 | continue; | |
624 | } | |
a6ccc7be JA |
625 | if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) { |
626 | fgetpos(f, &off); | |
627 | continue; | |
628 | } | |
629 | if (!check_int(p, "rwmixread", &il)) { | |
630 | if (il > 100) | |
631 | il = 100; | |
632 | td->rwmixread = il; | |
633 | fgetpos(f, &off); | |
634 | continue; | |
635 | } | |
636 | if (!check_int(p, "rwmixwrite", &il)) { | |
637 | if (il > 100) | |
638 | il = 100; | |
639 | td->rwmixread = 100 - il; | |
640 | fgetpos(f, &off); | |
641 | continue; | |
642 | } | |
b6f4d880 JA |
643 | if (!check_int(p, "nice", &td->nice)) { |
644 | fgetpos(f, &off); | |
645 | continue; | |
646 | } | |
53cdc686 JA |
647 | if (!check_int(p, "nrfiles", &td->nr_files)) { |
648 | fgetpos(f, &off); | |
649 | continue; | |
650 | } | |
906c8d75 | 651 | if (!check_range_bytes(p, "bsrange", &ul1, &ul2)) { |
ebac4655 JA |
652 | if (ul1 > ul2) { |
653 | td->max_bs = ul1; | |
654 | td->min_bs = ul2; | |
655 | } else { | |
656 | td->max_bs = ul2; | |
657 | td->min_bs = ul1; | |
658 | } | |
659 | fgetpos(f, &off); | |
660 | continue; | |
661 | } | |
906c8d75 | 662 | if (!check_str_bytes(p, "bs", &ull)) { |
ebac4655 JA |
663 | td->bs = ull; |
664 | fgetpos(f, &off); | |
665 | continue; | |
666 | } | |
53cdc686 | 667 | if (!check_str_bytes(p, "size", &td->total_file_size)) { |
ebac4655 JA |
668 | fgetpos(f, &off); |
669 | continue; | |
670 | } | |
53cdc686 | 671 | if (!check_str_bytes(p, "offset", &td->start_offset)) { |
ebac4655 | 672 | fgetpos(f, &off); |
20dc95c4 JA |
673 | continue; |
674 | } | |
906c8d75 | 675 | if (!check_str_bytes(p, "zonesize", &td->zone_size)) { |
20dc95c4 JA |
676 | fgetpos(f, &off); |
677 | continue; | |
678 | } | |
906c8d75 | 679 | if (!check_str_bytes(p, "zoneskip", &td->zone_skip)) { |
20dc95c4 | 680 | fgetpos(f, &off); |
ebac4655 JA |
681 | continue; |
682 | } | |
906c8d75 | 683 | if (!check_str_bytes(p, "lockmem", &mlock_size)) { |
c04f7ec3 JA |
684 | fgetpos(f, &off); |
685 | continue; | |
686 | } | |
ef899b63 JA |
687 | if (!check_strstore(p, "directory", tmpbuf)) { |
688 | td->directory = strdup(tmpbuf); | |
ebac4655 JA |
689 | fgetpos(f, &off); |
690 | continue; | |
691 | } | |
13f8e2d2 JA |
692 | if (!check_strstore(p, "filename", tmpbuf)) { |
693 | td->filename = strdup(tmpbuf); | |
694 | fgetpos(f, &off); | |
695 | continue; | |
696 | } | |
01452055 JA |
697 | if (!check_strstore(p, "name", tmpbuf)) { |
698 | snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number); | |
699 | fgetpos(f, &off); | |
700 | continue; | |
701 | } | |
ebac4655 JA |
702 | if (!check_str(p, "mem", str_mem_cb, td)) { |
703 | fgetpos(f, &off); | |
704 | continue; | |
705 | } | |
706 | if (!check_str(p, "verify", str_verify_cb, td)) { | |
707 | fgetpos(f, &off); | |
708 | continue; | |
709 | } | |
710 | if (!check_str(p, "rw", str_rw_cb, td)) { | |
711 | fgetpos(f, &off); | |
712 | continue; | |
713 | } | |
714 | if (!check_str(p, "ioengine", str_ioengine_cb, td)) { | |
715 | fgetpos(f, &off); | |
716 | continue; | |
717 | } | |
ebac4655 JA |
718 | if (!check_strset(p, "exitall")) { |
719 | exitall_on_terminate = 1; | |
720 | fgetpos(f, &off); | |
721 | continue; | |
722 | } | |
723 | if (!check_strset(p, "stonewall")) { | |
724 | td->stonewall = 1; | |
725 | fgetpos(f, &off); | |
726 | continue; | |
727 | } | |
728 | if (!check_strset(p, "thread")) { | |
729 | td->use_thread = 1; | |
730 | fgetpos(f, &off); | |
731 | continue; | |
732 | } | |
f6cbb269 JA |
733 | if (!check_strset(p, "unlink")) { |
734 | td->unlink = 1; | |
735 | fgetpos(f, &off); | |
736 | continue; | |
737 | } | |
ec94ec56 JA |
738 | if (!check_strset(p, "write_bw_log")) { |
739 | td->write_bw_log = 1; | |
740 | fgetpos(f, &off); | |
741 | continue; | |
742 | } | |
743 | if (!check_strset(p, "write_lat_log")) { | |
744 | td->write_lat_log = 1; | |
745 | fgetpos(f, &off); | |
746 | continue; | |
747 | } | |
80b9feb0 | 748 | if (!check_strstore(p, "iolog", tmpbuf)) { |
4f693b9d | 749 | if (td->write_iolog) { |
3b70d7e5 | 750 | log_err("fio: read iolog overrides given write_iolog\n"); |
af8c0b4f | 751 | free(td->iolog_file); |
4f693b9d JA |
752 | td->write_iolog = 0; |
753 | } | |
80b9feb0 | 754 | td->iolog_file = strdup(tmpbuf); |
843a7413 | 755 | td->read_iolog = 1; |
843a7413 JA |
756 | fgetpos(f, &off); |
757 | continue; | |
758 | } | |
af8c0b4f JA |
759 | if (!check_strstore(p, "write_iolog", tmpbuf)) { |
760 | if (!td->read_iolog) { | |
761 | td->iolog_file = strdup(tmpbuf); | |
762 | td->write_iolog = 1; | |
4f693b9d | 763 | } else |
3b70d7e5 | 764 | log_err("fio: read iolog overrides given write_iolog\n"); |
aea47d44 JA |
765 | fgetpos(f, &off); |
766 | continue; | |
767 | } | |
80b9feb0 JA |
768 | if (!check_strstore(p, "exec_prerun", tmpbuf)) { |
769 | td->exec_prerun = strdup(tmpbuf); | |
4e0ba8af JA |
770 | fgetpos(f, &off); |
771 | continue; | |
772 | } | |
80b9feb0 JA |
773 | if (!check_strstore(p, "exec_postrun", tmpbuf)) { |
774 | td->exec_postrun = strdup(tmpbuf); | |
4e0ba8af JA |
775 | fgetpos(f, &off); |
776 | continue; | |
777 | } | |
80b9feb0 | 778 | if (!check_strstore(p, "ioscheduler", tmpbuf)) { |
22f78b32 | 779 | #ifndef FIO_HAVE_IOSCHED_SWITCH |
3b70d7e5 | 780 | log_err("io scheduler switching not available\n"); |
22f78b32 JA |
781 | ret = 1; |
782 | break; | |
783 | #else | |
80b9feb0 | 784 | td->ioscheduler = strdup(tmpbuf); |
da86774e JA |
785 | fgetpos(f, &off); |
786 | continue; | |
22f78b32 | 787 | #endif |
da86774e | 788 | } |
ebac4655 | 789 | |
45410acb JA |
790 | /* |
791 | * Don't break here, continue parsing options so we | |
792 | * dump all the bad ones. Makes trial/error fixups | |
793 | * easier on the user. | |
794 | */ | |
ebac4655 | 795 | printf("Client%d: bad option %s\n",td->thread_number,p); |
45410acb | 796 | ret = 1; |
ebac4655 | 797 | } |
ebac4655 | 798 | |
45410acb JA |
799 | if (!ret) { |
800 | fsetpos(f, &off); | |
801 | ret = add_job(td, name, 0); | |
802 | } | |
803 | if (ret) | |
804 | break; | |
ebac4655 JA |
805 | } |
806 | ||
807 | free(string); | |
808 | free(name); | |
ef899b63 | 809 | free(tmpbuf); |
ebac4655 | 810 | fclose(f); |
45410acb | 811 | return ret; |
ebac4655 JA |
812 | } |
813 | ||
814 | static int fill_def_thread(void) | |
815 | { | |
816 | memset(&def_thread, 0, sizeof(def_thread)); | |
817 | ||
818 | if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) { | |
819 | perror("sched_getaffinity"); | |
820 | return 1; | |
821 | } | |
822 | ||
823 | /* | |
824 | * fill globals | |
825 | */ | |
826 | def_thread.ddir = DDIR_READ; | |
3d60d1ed | 827 | def_thread.iomix = 0; |
9ebc27e1 | 828 | def_thread.bs = DEF_BS; |
ebac4655 JA |
829 | def_thread.min_bs = -1; |
830 | def_thread.max_bs = -1; | |
9ebc27e1 | 831 | def_thread.odirect = DEF_ODIRECT; |
ebac4655 | 832 | def_thread.ratecycle = DEF_RATE_CYCLE; |
9ebc27e1 | 833 | def_thread.sequential = DEF_SEQUENTIAL; |
972cfd25 | 834 | def_thread.timeout = def_timeout; |
ebac4655 JA |
835 | def_thread.overwrite = DEF_OVERWRITE; |
836 | def_thread.invalidate_cache = DEF_INVALIDATE; | |
837 | def_thread.sync_io = DEF_SYNCIO; | |
838 | def_thread.mem_type = MEM_MALLOC; | |
839 | def_thread.bw_avg_time = DEF_BWAVGTIME; | |
840 | def_thread.create_serialize = DEF_CREATE_SER; | |
841 | def_thread.create_fsync = DEF_CREATE_FSYNC; | |
842 | def_thread.loops = DEF_LOOPS; | |
843 | def_thread.verify = DEF_VERIFY; | |
844 | def_thread.stonewall = DEF_STONEWALL; | |
845 | def_thread.numjobs = DEF_NUMJOBS; | |
846 | def_thread.use_thread = DEF_USE_THREAD; | |
a6ccc7be JA |
847 | def_thread.rwmixcycle = DEF_RWMIX_CYCLE; |
848 | def_thread.rwmixread = DEF_RWMIX_READ; | |
b6f4d880 | 849 | def_thread.nice = DEF_NICE; |
9ebc27e1 | 850 | def_thread.rand_repeatable = DEF_RAND_REPEAT; |
53cdc686 | 851 | def_thread.nr_files = DEF_NR_FILES; |
f6cbb269 | 852 | def_thread.unlink = DEF_UNLINK; |
ec94ec56 JA |
853 | def_thread.write_bw_log = write_bw_log; |
854 | def_thread.write_lat_log = write_lat_log; | |
ebac4655 JA |
855 | #ifdef FIO_HAVE_DISK_UTIL |
856 | def_thread.do_disk_util = 1; | |
857 | #endif | |
858 | ||
859 | return 0; | |
860 | } | |
861 | ||
0ab8db89 | 862 | static void usage(void) |
4785f995 JA |
863 | { |
864 | printf("%s\n", fio_version_string); | |
47623c72 | 865 | printf("\t-o Write output to file\n"); |
4785f995 | 866 | printf("\t-t Runtime in seconds\n"); |
4785f995 JA |
867 | printf("\t-l Generate per-job latency logs\n"); |
868 | printf("\t-w Generate per-job bandwidth logs\n"); | |
c6ae0a5b | 869 | printf("\t-m Minimal (terse) output\n"); |
4785f995 JA |
870 | printf("\t-v Print version info and exit\n"); |
871 | } | |
872 | ||
972cfd25 | 873 | static int parse_cmd_line(int argc, char *argv[]) |
ebac4655 | 874 | { |
972cfd25 | 875 | int c, idx = 1, ini_idx = 0; |
ebac4655 | 876 | |
2e77841b | 877 | while ((c = getopt(argc, argv, "t:o:lwvhm")) != EOF) { |
ebac4655 | 878 | switch (c) { |
ebac4655 | 879 | case 't': |
972cfd25 | 880 | def_timeout = atoi(optarg); |
774a6177 | 881 | idx = optind; |
ebac4655 | 882 | break; |
ebac4655 JA |
883 | case 'l': |
884 | write_lat_log = 1; | |
774a6177 | 885 | idx = optind; |
ebac4655 JA |
886 | break; |
887 | case 'w': | |
888 | write_bw_log = 1; | |
774a6177 | 889 | idx = optind; |
ebac4655 | 890 | break; |
9ebc27e1 | 891 | case 'o': |
eb8bbf48 JA |
892 | f_out = fopen(optarg, "w+"); |
893 | if (!f_out) { | |
894 | perror("fopen output"); | |
895 | exit(1); | |
896 | } | |
897 | f_err = f_out; | |
774a6177 | 898 | idx = optind; |
eb8bbf48 | 899 | break; |
c6ae0a5b JA |
900 | case 'm': |
901 | terse_output = 1; | |
774a6177 | 902 | idx = optind; |
c6ae0a5b | 903 | break; |
4785f995 | 904 | case 'h': |
0ab8db89 | 905 | usage(); |
4785f995 | 906 | exit(0); |
ebac4655 JA |
907 | case 'v': |
908 | printf("%s\n", fio_version_string); | |
909 | exit(0); | |
910 | } | |
911 | } | |
c9fad893 | 912 | |
972cfd25 JA |
913 | while (idx < argc) { |
914 | ini_idx++; | |
915 | ini_file = realloc(ini_file, ini_idx * sizeof(char *)); | |
916 | ini_file[ini_idx - 1] = strdup(argv[idx]); | |
917 | idx++; | |
918 | } | |
774a6177 | 919 | |
eb8bbf48 JA |
920 | if (!f_out) { |
921 | f_out = stdout; | |
922 | f_err = stderr; | |
923 | } | |
972cfd25 JA |
924 | |
925 | return ini_idx; | |
ebac4655 JA |
926 | } |
927 | ||
928 | static void free_shm(void) | |
929 | { | |
930 | struct shmid_ds sbuf; | |
931 | ||
932 | if (threads) { | |
2c0ecd28 | 933 | shmdt((void *) threads); |
ebac4655 JA |
934 | threads = NULL; |
935 | shmctl(shm_id, IPC_RMID, &sbuf); | |
936 | } | |
937 | } | |
938 | ||
906c8d75 JA |
939 | /* |
940 | * The thread area is shared between the main process and the job | |
941 | * threads/processes. So setup a shared memory segment that will hold | |
942 | * all the job info. | |
943 | */ | |
ebac4655 JA |
944 | static int setup_thread_area(void) |
945 | { | |
946 | /* | |
947 | * 1024 is too much on some machines, scale max_jobs if | |
948 | * we get a failure that looks like too large a shm segment | |
949 | */ | |
950 | do { | |
906c8d75 | 951 | size_t size = max_jobs * sizeof(struct thread_data); |
ebac4655 | 952 | |
906c8d75 | 953 | shm_id = shmget(0, size, IPC_CREAT | 0600); |
ebac4655 JA |
954 | if (shm_id != -1) |
955 | break; | |
956 | if (errno != EINVAL) { | |
957 | perror("shmget"); | |
958 | break; | |
959 | } | |
960 | ||
961 | max_jobs >>= 1; | |
962 | } while (max_jobs); | |
963 | ||
964 | if (shm_id == -1) | |
965 | return 1; | |
966 | ||
967 | threads = shmat(shm_id, NULL, 0); | |
968 | if (threads == (void *) -1) { | |
969 | perror("shmat"); | |
970 | return 1; | |
971 | } | |
972 | ||
973 | atexit(free_shm); | |
974 | return 0; | |
975 | } | |
976 | ||
977 | int parse_options(int argc, char *argv[]) | |
978 | { | |
972cfd25 JA |
979 | int job_files, i; |
980 | ||
ebac4655 JA |
981 | if (setup_thread_area()) |
982 | return 1; | |
983 | if (fill_def_thread()) | |
984 | return 1; | |
985 | ||
972cfd25 JA |
986 | job_files = parse_cmd_line(argc, argv); |
987 | if (!job_files) { | |
988 | log_err("Need job file(s)\n"); | |
0ab8db89 | 989 | usage(); |
ebac4655 JA |
990 | return 1; |
991 | } | |
992 | ||
972cfd25 JA |
993 | for (i = 0; i < job_files; i++) { |
994 | if (fill_def_thread()) | |
995 | return 1; | |
0c7e37a0 | 996 | if (parse_jobs_ini(ini_file[i], i)) |
972cfd25 | 997 | return 1; |
88c6ed80 | 998 | free(ini_file[i]); |
972cfd25 | 999 | } |
ebac4655 | 1000 | |
88c6ed80 | 1001 | free(ini_file); |
ebac4655 JA |
1002 | return 0; |
1003 | } |