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> | |
b4692828 | 11 | #include <getopt.h> |
ebac4655 JA |
12 | #include <sys/ipc.h> |
13 | #include <sys/shm.h> | |
14 | #include <sys/types.h> | |
15 | #include <sys/stat.h> | |
16 | ||
17 | #include "fio.h" | |
cb2c86fd | 18 | #include "parse.h" |
2e5cdb11 | 19 | #include "smalloc.h" |
380065aa | 20 | #include "filehash.h" |
ebac4655 | 21 | |
8ca7ead9 | 22 | static char fio_version_string[] = "fio 1.20-rc1"; |
214e1eca | 23 | |
ee738499 | 24 | #define FIO_RANDSEED (0xb1899bedUL) |
ebac4655 | 25 | |
214e1eca JA |
26 | static char **ini_file; |
27 | static int max_jobs = MAX_JOBS; | |
cca73aa7 | 28 | static int dump_cmdline; |
e1f36503 | 29 | |
214e1eca JA |
30 | struct thread_data def_thread; |
31 | struct thread_data *threads = NULL; | |
e1f36503 | 32 | |
214e1eca JA |
33 | int exitall_on_terminate = 0; |
34 | int terse_output = 0; | |
e592a06b | 35 | int eta_print; |
214e1eca JA |
36 | unsigned long long mlock_size = 0; |
37 | FILE *f_out = NULL; | |
38 | FILE *f_err = NULL; | |
01f06b63 | 39 | char *job_section = NULL; |
ee738499 | 40 | |
214e1eca | 41 | int write_bw_log = 0; |
4241ea8f | 42 | int read_only = 0; |
214e1eca JA |
43 | |
44 | static int def_timeout = 0; | |
45 | static int write_lat_log = 0; | |
e1f36503 | 46 | |
214e1eca | 47 | static int prev_group_jobs; |
b4692828 | 48 | |
ee56ad50 JA |
49 | unsigned long fio_debug = 0; |
50 | ||
b4692828 JA |
51 | /* |
52 | * Command line options. These will contain the above, plus a few | |
53 | * extra that only pertain to fio itself and not jobs. | |
54 | */ | |
214e1eca | 55 | static struct option long_options[FIO_NR_OPTIONS] = { |
b4692828 JA |
56 | { |
57 | .name = "output", | |
58 | .has_arg = required_argument, | |
59 | .val = 'o', | |
60 | }, | |
61 | { | |
62 | .name = "timeout", | |
63 | .has_arg = required_argument, | |
64 | .val = 't', | |
65 | }, | |
66 | { | |
67 | .name = "latency-log", | |
68 | .has_arg = required_argument, | |
69 | .val = 'l', | |
70 | }, | |
71 | { | |
72 | .name = "bandwidth-log", | |
73 | .has_arg = required_argument, | |
74 | .val = 'b', | |
75 | }, | |
76 | { | |
77 | .name = "minimal", | |
78 | .has_arg = optional_argument, | |
79 | .val = 'm', | |
80 | }, | |
81 | { | |
82 | .name = "version", | |
83 | .has_arg = no_argument, | |
84 | .val = 'v', | |
85 | }, | |
fd28ca49 JA |
86 | { |
87 | .name = "help", | |
88 | .has_arg = no_argument, | |
89 | .val = 'h', | |
90 | }, | |
91 | { | |
92 | .name = "cmdhelp", | |
320beefe | 93 | .has_arg = optional_argument, |
fd28ca49 JA |
94 | .val = 'c', |
95 | }, | |
cca73aa7 JA |
96 | { |
97 | .name = "showcmd", | |
98 | .has_arg = no_argument, | |
724e4435 JA |
99 | .val = 's', |
100 | }, | |
101 | { | |
102 | .name = "readonly", | |
103 | .has_arg = no_argument, | |
104 | .val = 'r', | |
cca73aa7 | 105 | }, |
e592a06b AC |
106 | { |
107 | .name = "eta", | |
108 | .has_arg = required_argument, | |
109 | .val = 'e', | |
110 | }, | |
ee56ad50 JA |
111 | { |
112 | .name = "debug", | |
113 | .has_arg = required_argument, | |
114 | .val = 'd', | |
115 | }, | |
01f06b63 JA |
116 | { |
117 | .name = "section", | |
118 | .has_arg = required_argument, | |
119 | .val = 'x', | |
120 | }, | |
b4692828 JA |
121 | { |
122 | .name = NULL, | |
123 | }, | |
124 | }; | |
125 | ||
9728ce37 JB |
126 | FILE *get_f_out() |
127 | { | |
128 | return f_out; | |
129 | } | |
130 | ||
131 | FILE *get_f_err() | |
132 | { | |
133 | return f_err; | |
134 | } | |
135 | ||
906c8d75 JA |
136 | /* |
137 | * Return a free job structure. | |
138 | */ | |
ebac4655 JA |
139 | static struct thread_data *get_new_job(int global, struct thread_data *parent) |
140 | { | |
141 | struct thread_data *td; | |
142 | ||
143 | if (global) | |
144 | return &def_thread; | |
145 | if (thread_number >= max_jobs) | |
146 | return NULL; | |
147 | ||
148 | td = &threads[thread_number++]; | |
ddaeaa5a | 149 | *td = *parent; |
ebac4655 | 150 | |
cade3ef4 | 151 | dup_files(td, parent); |
d23bb327 | 152 | options_mem_dupe(td); |
cade3ef4 | 153 | |
ebac4655 | 154 | td->thread_number = thread_number; |
ebac4655 JA |
155 | return td; |
156 | } | |
157 | ||
158 | static void put_job(struct thread_data *td) | |
159 | { | |
549577a7 JA |
160 | if (td == &def_thread) |
161 | return; | |
162 | ||
16edf25d | 163 | if (td->error) |
6d86144d | 164 | log_info("fio: %s\n", td->verror); |
16edf25d | 165 | |
ebac4655 JA |
166 | memset(&threads[td->thread_number - 1], 0, sizeof(*td)); |
167 | thread_number--; | |
168 | } | |
169 | ||
127f6865 JA |
170 | static int setup_rate(struct thread_data *td) |
171 | { | |
172 | unsigned long nr_reads_per_msec; | |
173 | unsigned long long rate; | |
174 | unsigned int bs; | |
175 | ||
2dc1bbeb | 176 | if (!td->o.rate && !td->o.rate_iops) |
127f6865 JA |
177 | return 0; |
178 | ||
179 | if (td_rw(td)) | |
2dc1bbeb | 180 | bs = td->o.rw_min_bs; |
127f6865 | 181 | else if (td_read(td)) |
2dc1bbeb | 182 | bs = td->o.min_bs[DDIR_READ]; |
127f6865 | 183 | else |
2dc1bbeb | 184 | bs = td->o.min_bs[DDIR_WRITE]; |
127f6865 | 185 | |
2dc1bbeb JA |
186 | if (td->o.rate) { |
187 | rate = td->o.rate; | |
127f6865 JA |
188 | nr_reads_per_msec = (rate * 1024 * 1000LL) / bs; |
189 | } else | |
2dc1bbeb | 190 | nr_reads_per_msec = td->o.rate_iops * 1000UL; |
127f6865 JA |
191 | |
192 | if (!nr_reads_per_msec) { | |
193 | log_err("rate lower than supported\n"); | |
194 | return -1; | |
195 | } | |
196 | ||
197 | td->rate_usec_cycle = 1000000000ULL / nr_reads_per_msec; | |
198 | td->rate_pending_usleep = 0; | |
199 | return 0; | |
200 | } | |
201 | ||
dad915e3 JA |
202 | /* |
203 | * Lazy way of fixing up options that depend on each other. We could also | |
204 | * define option callback handlers, but this is easier. | |
205 | */ | |
4e991c23 | 206 | static int fixup_options(struct thread_data *td) |
e1f36503 | 207 | { |
2dc1bbeb | 208 | struct thread_options *o = &td->o; |
dad915e3 | 209 | |
724e4435 JA |
210 | if (read_only && td_write(td)) { |
211 | log_err("fio: job <%s> has write bit set, but fio is in read-only mode\n", td->o.name); | |
212 | return 1; | |
213 | } | |
214 | ||
e47f799f JA |
215 | if (o->rwmix[DDIR_READ] + o->rwmix[DDIR_WRITE] > 100) |
216 | o->rwmix[DDIR_WRITE] = 100 - o->rwmix[DDIR_READ]; | |
2dc1bbeb JA |
217 | |
218 | if (o->write_iolog_file && o->read_iolog_file) { | |
076efc7c | 219 | log_err("fio: read iolog overrides write_iolog\n"); |
2dc1bbeb JA |
220 | free(o->write_iolog_file); |
221 | o->write_iolog_file = NULL; | |
076efc7c | 222 | } |
16b462ae | 223 | |
16b462ae JA |
224 | /* |
225 | * only really works for sequential io for now, and with 1 file | |
226 | */ | |
2dc1bbeb JA |
227 | if (o->zone_size && td_random(td) && o->open_files == 1) |
228 | o->zone_size = 0; | |
16b462ae JA |
229 | |
230 | /* | |
231 | * Reads can do overwrites, we always need to pre-create the file | |
232 | */ | |
233 | if (td_read(td) || td_rw(td)) | |
2dc1bbeb | 234 | o->overwrite = 1; |
16b462ae | 235 | |
2dc1bbeb JA |
236 | if (!o->min_bs[DDIR_READ]) |
237 | o->min_bs[DDIR_READ]= o->bs[DDIR_READ]; | |
238 | if (!o->max_bs[DDIR_READ]) | |
239 | o->max_bs[DDIR_READ] = o->bs[DDIR_READ]; | |
240 | if (!o->min_bs[DDIR_WRITE]) | |
241 | o->min_bs[DDIR_WRITE]= o->bs[DDIR_WRITE]; | |
242 | if (!o->max_bs[DDIR_WRITE]) | |
243 | o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE]; | |
a00735e6 | 244 | |
2dc1bbeb | 245 | o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]); |
a00735e6 | 246 | |
2dc1bbeb JA |
247 | if (!o->file_size_high) |
248 | o->file_size_high = o->file_size_low; | |
9c60ce64 | 249 | |
2dc1bbeb | 250 | if (o->norandommap && o->verify != VERIFY_NONE) { |
bb8895e0 | 251 | log_err("fio: norandommap given, verify disabled\n"); |
2dc1bbeb | 252 | o->verify = VERIFY_NONE; |
bb8895e0 | 253 | } |
2dc1bbeb | 254 | if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO)) |
690adba3 | 255 | log_err("fio: bs_unaligned may not work with raw io\n"); |
e0a22335 | 256 | |
48097d5c JA |
257 | /* |
258 | * thinktime_spin must be less than thinktime | |
259 | */ | |
2dc1bbeb JA |
260 | if (o->thinktime_spin > o->thinktime) |
261 | o->thinktime_spin = o->thinktime; | |
e916b390 JA |
262 | |
263 | /* | |
264 | * The low water mark cannot be bigger than the iodepth | |
265 | */ | |
2dc1bbeb | 266 | if (o->iodepth_low > o->iodepth || !o->iodepth_low) { |
9467b77c JA |
267 | /* |
268 | * syslet work around - if the workload is sequential, | |
269 | * we want to let the queue drain all the way down to | |
270 | * avoid seeking between async threads | |
271 | */ | |
272 | if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td)) | |
2dc1bbeb | 273 | o->iodepth_low = 1; |
9467b77c | 274 | else |
2dc1bbeb | 275 | o->iodepth_low = o->iodepth; |
9467b77c | 276 | } |
cb5ab512 JA |
277 | |
278 | /* | |
279 | * If batch number isn't set, default to the same as iodepth | |
280 | */ | |
2dc1bbeb JA |
281 | if (o->iodepth_batch > o->iodepth || !o->iodepth_batch) |
282 | o->iodepth_batch = o->iodepth; | |
b5af8293 | 283 | |
2dc1bbeb JA |
284 | if (o->nr_files > td->files_index) |
285 | o->nr_files = td->files_index; | |
9f9214f2 | 286 | |
2dc1bbeb JA |
287 | if (o->open_files > o->nr_files || !o->open_files) |
288 | o->open_files = o->nr_files; | |
4e991c23 | 289 | |
2dc1bbeb | 290 | if ((o->rate && o->rate_iops) || (o->ratemin && o->rate_iops_min)) { |
4e991c23 JA |
291 | log_err("fio: rate and rate_iops are mutually exclusive\n"); |
292 | return 1; | |
293 | } | |
2dc1bbeb | 294 | if ((o->rate < o->ratemin) || (o->rate_iops < o->rate_iops_min)) { |
4e991c23 JA |
295 | log_err("fio: minimum rate exceeds rate\n"); |
296 | return 1; | |
297 | } | |
298 | ||
cf4464ca JA |
299 | if (!o->timeout && o->time_based) { |
300 | log_err("fio: time_based requires a runtime/timeout setting\n"); | |
301 | o->time_based = 0; | |
302 | } | |
303 | ||
aa31f1f1 SL |
304 | if (o->fill_device && !o->size) |
305 | o->size = ULONG_LONG_MAX; | |
79713149 JA |
306 | |
307 | if (td_rw(td) && td->o.verify != VERIFY_NONE) | |
308 | log_info("fio: mixed read/write workload with verify. May not " | |
309 | "work as expected, unless you pre-populated the file\n"); | |
aa31f1f1 | 310 | |
4e991c23 | 311 | return 0; |
e1f36503 JA |
312 | } |
313 | ||
f8977ee6 JA |
314 | /* |
315 | * This function leaks the buffer | |
316 | */ | |
317 | static char *to_kmg(unsigned int val) | |
318 | { | |
319 | char *buf = malloc(32); | |
f3502ba2 | 320 | char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 }; |
f8977ee6 JA |
321 | char *p = post; |
322 | ||
245142ff | 323 | do { |
f8977ee6 JA |
324 | if (val & 1023) |
325 | break; | |
326 | ||
327 | val >>= 10; | |
328 | p++; | |
245142ff | 329 | } while (*p); |
f8977ee6 JA |
330 | |
331 | snprintf(buf, 31, "%u%c", val, *p); | |
332 | return buf; | |
333 | } | |
334 | ||
09629a90 JA |
335 | /* External engines are specified by "external:name.o") */ |
336 | static const char *get_engine_name(const char *str) | |
337 | { | |
338 | char *p = strstr(str, ":"); | |
339 | ||
340 | if (!p) | |
341 | return str; | |
342 | ||
343 | p++; | |
344 | strip_blank_front(&p); | |
345 | strip_blank_end(p); | |
346 | return p; | |
347 | } | |
348 | ||
e132cbae JA |
349 | static int exists_and_not_file(const char *filename) |
350 | { | |
351 | struct stat sb; | |
352 | ||
353 | if (lstat(filename, &sb) == -1) | |
354 | return 0; | |
355 | ||
356 | if (S_ISREG(sb.st_mode)) | |
357 | return 0; | |
358 | ||
359 | return 1; | |
360 | } | |
361 | ||
9c60ce64 JA |
362 | /* |
363 | * Initialize the various random states we need (random io, block size ranges, | |
364 | * read/write mix, etc). | |
365 | */ | |
366 | static int init_random_state(struct thread_data *td) | |
367 | { | |
368 | unsigned long seeds[6]; | |
68727076 | 369 | int fd; |
9c60ce64 JA |
370 | |
371 | fd = open("/dev/urandom", O_RDONLY); | |
372 | if (fd == -1) { | |
373 | td_verror(td, errno, "open"); | |
374 | return 1; | |
375 | } | |
376 | ||
377 | if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) { | |
378 | td_verror(td, EIO, "read"); | |
379 | close(fd); | |
380 | return 1; | |
381 | } | |
382 | ||
383 | close(fd); | |
384 | ||
385 | os_random_seed(seeds[0], &td->bsrange_state); | |
386 | os_random_seed(seeds[1], &td->verify_state); | |
387 | os_random_seed(seeds[2], &td->rwmix_state); | |
388 | ||
2dc1bbeb | 389 | if (td->o.file_service_type == FIO_FSERVICE_RANDOM) |
9c60ce64 JA |
390 | os_random_seed(seeds[3], &td->next_file_state); |
391 | ||
392 | os_random_seed(seeds[5], &td->file_size_state); | |
393 | ||
394 | if (!td_random(td)) | |
395 | return 0; | |
396 | ||
2dc1bbeb | 397 | if (td->o.rand_repeatable) |
9c60ce64 JA |
398 | seeds[4] = FIO_RANDSEED * td->thread_number; |
399 | ||
9c60ce64 JA |
400 | os_random_seed(seeds[4], &td->random_state); |
401 | return 0; | |
402 | } | |
403 | ||
906c8d75 JA |
404 | /* |
405 | * Adds a job to the list of things todo. Sanitizes the various options | |
406 | * to make sure we don't have conflicts, and initializes various | |
407 | * members of td. | |
408 | */ | |
75154845 | 409 | static int add_job(struct thread_data *td, const char *jobname, int job_add_num) |
ebac4655 | 410 | { |
413dd459 JA |
411 | const char *ddir_str[] = { NULL, "read", "write", "rw", NULL, |
412 | "randread", "randwrite", "randrw" }; | |
af52b345 | 413 | unsigned int i; |
09629a90 | 414 | const char *engine; |
af52b345 | 415 | char fname[PATH_MAX]; |
e132cbae | 416 | int numjobs, file_alloced; |
ebac4655 | 417 | |
ebac4655 JA |
418 | /* |
419 | * the def_thread is just for options, it's not a real job | |
420 | */ | |
421 | if (td == &def_thread) | |
422 | return 0; | |
423 | ||
cca73aa7 JA |
424 | /* |
425 | * if we are just dumping the output command line, don't add the job | |
426 | */ | |
427 | if (dump_cmdline) { | |
428 | put_job(td); | |
429 | return 0; | |
430 | } | |
431 | ||
2dc1bbeb | 432 | engine = get_engine_name(td->o.ioengine); |
09629a90 JA |
433 | td->io_ops = load_ioengine(td, engine); |
434 | if (!td->io_ops) { | |
435 | log_err("fio: failed to load engine %s\n", engine); | |
205927a3 | 436 | goto err; |
09629a90 | 437 | } |
df64119d | 438 | |
2dc1bbeb | 439 | if (td->o.use_thread) |
9cedf167 JA |
440 | nr_thread++; |
441 | else | |
442 | nr_process++; | |
443 | ||
2dc1bbeb | 444 | if (td->o.odirect) |
690adba3 JA |
445 | td->io_ops->flags |= FIO_RAWIO; |
446 | ||
e132cbae | 447 | file_alloced = 0; |
2dc1bbeb | 448 | if (!td->o.filename && !td->files_index) { |
e132cbae | 449 | file_alloced = 1; |
80be24f4 | 450 | |
2dc1bbeb | 451 | if (td->o.nr_files == 1 && exists_and_not_file(jobname)) |
e132cbae | 452 | add_file(td, jobname); |
7b05a215 | 453 | else { |
2dc1bbeb | 454 | for (i = 0; i < td->o.nr_files; i++) { |
e132cbae | 455 | sprintf(fname, "%s.%d.%d", jobname, td->thread_number, i); |
7b05a215 JA |
456 | add_file(td, fname); |
457 | } | |
af52b345 | 458 | } |
0af7b542 | 459 | } |
ebac4655 | 460 | |
4e991c23 JA |
461 | if (fixup_options(td)) |
462 | goto err; | |
e0a22335 | 463 | |
07eb79df JA |
464 | if (td->io_ops->flags & FIO_DISKLESSIO) { |
465 | struct fio_file *f; | |
466 | ||
467 | for_each_file(td, f, i) | |
468 | f->real_file_size = -1ULL; | |
469 | } | |
470 | ||
cdd18ad8 | 471 | td->mutex = fio_mutex_init(0); |
ebac4655 | 472 | |
756867bd JA |
473 | td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX; |
474 | td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX; | |
475 | td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX; | |
1e3d53ac | 476 | td->ddir_nr = td->o.ddir_nr; |
ebac4655 | 477 | |
b3d62a75 JA |
478 | if ((td->o.stonewall || td->o.numjobs > 1 || td->o.new_group) |
479 | && prev_group_jobs) { | |
3c5df6fa | 480 | prev_group_jobs = 0; |
ebac4655 | 481 | groupid++; |
3c5df6fa | 482 | } |
ebac4655 JA |
483 | |
484 | td->groupid = groupid; | |
3c5df6fa | 485 | prev_group_jobs++; |
ebac4655 | 486 | |
9c60ce64 JA |
487 | if (init_random_state(td)) |
488 | goto err; | |
489 | ||
ebac4655 JA |
490 | if (setup_rate(td)) |
491 | goto err; | |
492 | ||
2dc1bbeb | 493 | if (td->o.write_lat_log) { |
756867bd JA |
494 | setup_log(&td->ts.slat_log); |
495 | setup_log(&td->ts.clat_log); | |
ebac4655 | 496 | } |
2dc1bbeb | 497 | if (td->o.write_bw_log) |
756867bd | 498 | setup_log(&td->ts.bw_log); |
ebac4655 | 499 | |
2dc1bbeb JA |
500 | if (!td->o.name) |
501 | td->o.name = strdup(jobname); | |
01452055 | 502 | |
c6ae0a5b | 503 | if (!terse_output) { |
b990b5c0 | 504 | if (!job_add_num) { |
ba0fbe10 | 505 | if (!strcmp(td->io_ops->name, "cpuio")) |
2dc1bbeb | 506 | log_info("%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->o.name, td->o.cpuload, td->o.cpucycle); |
f8977ee6 JA |
507 | else { |
508 | char *c1, *c2, *c3, *c4; | |
509 | ||
2dc1bbeb JA |
510 | c1 = to_kmg(td->o.min_bs[DDIR_READ]); |
511 | c2 = to_kmg(td->o.max_bs[DDIR_READ]); | |
512 | c3 = to_kmg(td->o.min_bs[DDIR_WRITE]); | |
513 | c4 = to_kmg(td->o.max_bs[DDIR_WRITE]); | |
f8977ee6 | 514 | |
2dc1bbeb | 515 | log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s, ioengine=%s, iodepth=%u\n", td->o.name, td->groupid, ddir_str[td->o.td_ddir], c1, c2, c3, c4, td->io_ops->name, td->o.iodepth); |
f8977ee6 JA |
516 | |
517 | free(c1); | |
518 | free(c2); | |
519 | free(c3); | |
520 | free(c4); | |
521 | } | |
b990b5c0 | 522 | } else if (job_add_num == 1) |
6d86144d | 523 | log_info("...\n"); |
c6ae0a5b | 524 | } |
ebac4655 JA |
525 | |
526 | /* | |
527 | * recurse add identical jobs, clear numjobs and stonewall options | |
528 | * as they don't apply to sub-jobs | |
529 | */ | |
2dc1bbeb | 530 | numjobs = td->o.numjobs; |
ebac4655 JA |
531 | while (--numjobs) { |
532 | struct thread_data *td_new = get_new_job(0, td); | |
533 | ||
534 | if (!td_new) | |
535 | goto err; | |
536 | ||
2dc1bbeb JA |
537 | td_new->o.numjobs = 1; |
538 | td_new->o.stonewall = 0; | |
92c1d41f | 539 | td_new->o.new_group = 0; |
e132cbae JA |
540 | |
541 | if (file_alloced) { | |
2dc1bbeb | 542 | td_new->o.filename = NULL; |
e132cbae JA |
543 | td_new->files_index = 0; |
544 | td_new->files = NULL; | |
545 | } | |
546 | ||
75154845 | 547 | job_add_num = numjobs - 1; |
ebac4655 | 548 | |
75154845 | 549 | if (add_job(td_new, jobname, job_add_num)) |
ebac4655 JA |
550 | goto err; |
551 | } | |
3c5df6fa | 552 | |
ebac4655 JA |
553 | return 0; |
554 | err: | |
555 | put_job(td); | |
556 | return -1; | |
557 | } | |
558 | ||
01f06b63 JA |
559 | static int skip_this_section(const char *name) |
560 | { | |
561 | if (!job_section) | |
562 | return 0; | |
563 | if (!strncmp(name, "global", 6)) | |
564 | return 0; | |
565 | ||
566 | return strncmp(job_section, name, strlen(job_section)); | |
567 | } | |
568 | ||
ebac4655 JA |
569 | static int is_empty_or_comment(char *line) |
570 | { | |
571 | unsigned int i; | |
572 | ||
573 | for (i = 0; i < strlen(line); i++) { | |
574 | if (line[i] == ';') | |
575 | return 1; | |
5cc2da30 IM |
576 | if (line[i] == '#') |
577 | return 1; | |
ebac4655 JA |
578 | if (!isspace(line[i]) && !iscntrl(line[i])) |
579 | return 0; | |
580 | } | |
581 | ||
582 | return 1; | |
583 | } | |
584 | ||
07261983 JA |
585 | /* |
586 | * This is our [ini] type file parser. | |
587 | */ | |
1e97cce9 | 588 | static int parse_jobs_ini(char *file, int stonewall_flag) |
ebac4655 | 589 | { |
e1f36503 | 590 | unsigned int global; |
ebac4655 | 591 | struct thread_data *td; |
fee3bb48 | 592 | char *string, *name; |
ebac4655 JA |
593 | FILE *f; |
594 | char *p; | |
0c7e37a0 | 595 | int ret = 0, stonewall; |
097b2991 | 596 | int first_sect = 1; |
ccf8f127 | 597 | int skip_fgets = 0; |
01f06b63 | 598 | int inside_skip = 0; |
ebac4655 | 599 | |
5a729cbe AC |
600 | if (!strcmp(file, "-")) |
601 | f = stdin; | |
602 | else | |
603 | f = fopen(file, "r"); | |
604 | ||
ebac4655 | 605 | if (!f) { |
aea47d44 | 606 | perror("fopen job file"); |
ebac4655 JA |
607 | return 1; |
608 | } | |
609 | ||
610 | string = malloc(4096); | |
7f7e6e59 JA |
611 | |
612 | /* | |
613 | * it's really 256 + small bit, 280 should suffice | |
614 | */ | |
615 | name = malloc(280); | |
616 | memset(name, 0, 280); | |
ebac4655 | 617 | |
0c7e37a0 | 618 | stonewall = stonewall_flag; |
7c124ac1 | 619 | do { |
ccf8f127 JA |
620 | /* |
621 | * if skip_fgets is set, we already have loaded a line we | |
622 | * haven't handled. | |
623 | */ | |
624 | if (!skip_fgets) { | |
625 | p = fgets(string, 4095, f); | |
626 | if (!p) | |
627 | break; | |
628 | } | |
cdc7f193 | 629 | |
ccf8f127 | 630 | skip_fgets = 0; |
cdc7f193 | 631 | strip_blank_front(&p); |
6c7c7da1 | 632 | strip_blank_end(p); |
cdc7f193 | 633 | |
ebac4655 JA |
634 | if (is_empty_or_comment(p)) |
635 | continue; | |
6c7c7da1 | 636 | if (sscanf(p, "[%255s]", name) != 1) { |
01f06b63 JA |
637 | if (inside_skip) |
638 | continue; | |
088b4207 JA |
639 | log_err("fio: option <%s> outside of [] job section\n", p); |
640 | break; | |
6c7c7da1 | 641 | } |
ebac4655 | 642 | |
01f06b63 JA |
643 | if (skip_this_section(name)) { |
644 | inside_skip = 1; | |
645 | continue; | |
646 | } else | |
647 | inside_skip = 0; | |
648 | ||
ebac4655 JA |
649 | global = !strncmp(name, "global", 6); |
650 | ||
651 | name[strlen(name) - 1] = '\0'; | |
652 | ||
cca73aa7 | 653 | if (dump_cmdline) { |
097b2991 JA |
654 | if (first_sect) |
655 | log_info("fio "); | |
cca73aa7 JA |
656 | if (!global) |
657 | log_info("--name=%s ", name); | |
097b2991 | 658 | first_sect = 0; |
cca73aa7 JA |
659 | } |
660 | ||
ebac4655 | 661 | td = get_new_job(global, &def_thread); |
45410acb JA |
662 | if (!td) { |
663 | ret = 1; | |
664 | break; | |
665 | } | |
ebac4655 | 666 | |
972cfd25 JA |
667 | /* |
668 | * Seperate multiple job files by a stonewall | |
669 | */ | |
f9481919 | 670 | if (!global && stonewall) { |
2dc1bbeb | 671 | td->o.stonewall = stonewall; |
972cfd25 JA |
672 | stonewall = 0; |
673 | } | |
674 | ||
ebac4655 JA |
675 | while ((p = fgets(string, 4096, f)) != NULL) { |
676 | if (is_empty_or_comment(p)) | |
677 | continue; | |
e1f36503 | 678 | |
b6754f9d | 679 | strip_blank_front(&p); |
7c124ac1 | 680 | |
ccf8f127 JA |
681 | /* |
682 | * new section, break out and make sure we don't | |
683 | * fgets() a new line at the top. | |
684 | */ | |
685 | if (p[0] == '[') { | |
686 | skip_fgets = 1; | |
7c124ac1 | 687 | break; |
ccf8f127 | 688 | } |
7c124ac1 | 689 | |
4ae3f763 | 690 | strip_blank_end(p); |
aea47d44 | 691 | |
45410acb JA |
692 | /* |
693 | * Don't break here, continue parsing options so we | |
694 | * dump all the bad ones. Makes trial/error fixups | |
695 | * easier on the user. | |
696 | */ | |
214e1eca | 697 | ret |= fio_option_parse(td, p); |
cca73aa7 JA |
698 | if (!ret && dump_cmdline) |
699 | log_info("--%s ", p); | |
ebac4655 | 700 | } |
ebac4655 | 701 | |
ccf8f127 | 702 | if (!ret) |
45410acb | 703 | ret = add_job(td, name, 0); |
ccf8f127 | 704 | else { |
b1508cf9 JA |
705 | log_err("fio: job %s dropped\n", name); |
706 | put_job(td); | |
45410acb | 707 | } |
7c124ac1 | 708 | } while (!ret); |
ebac4655 | 709 | |
cca73aa7 JA |
710 | if (dump_cmdline) |
711 | log_info("\n"); | |
712 | ||
ebac4655 JA |
713 | free(string); |
714 | free(name); | |
5a729cbe AC |
715 | if (f != stdin) |
716 | fclose(f); | |
45410acb | 717 | return ret; |
ebac4655 JA |
718 | } |
719 | ||
720 | static int fill_def_thread(void) | |
721 | { | |
722 | memset(&def_thread, 0, sizeof(def_thread)); | |
723 | ||
375b2695 | 724 | fio_getaffinity(getpid(), &def_thread.o.cpumask); |
ebac4655 JA |
725 | |
726 | /* | |
ee738499 | 727 | * fill default options |
ebac4655 | 728 | */ |
214e1eca | 729 | fio_fill_default_options(&def_thread); |
ee738499 | 730 | |
2dc1bbeb JA |
731 | def_thread.o.timeout = def_timeout; |
732 | def_thread.o.write_bw_log = write_bw_log; | |
733 | def_thread.o.write_lat_log = write_lat_log; | |
ee738499 | 734 | |
ebac4655 JA |
735 | return 0; |
736 | } | |
737 | ||
214e1eca JA |
738 | static void free_shm(void) |
739 | { | |
740 | struct shmid_ds sbuf; | |
741 | ||
742 | if (threads) { | |
743 | shmdt((void *) threads); | |
744 | threads = NULL; | |
745 | shmctl(shm_id, IPC_RMID, &sbuf); | |
746 | } | |
2e5cdb11 JA |
747 | |
748 | scleanup(); | |
214e1eca JA |
749 | } |
750 | ||
751 | /* | |
752 | * The thread area is shared between the main process and the job | |
753 | * threads/processes. So setup a shared memory segment that will hold | |
380065aa JA |
754 | * all the job info. We use the end of the region for keeping track of |
755 | * open files across jobs, for file sharing. | |
214e1eca JA |
756 | */ |
757 | static int setup_thread_area(void) | |
758 | { | |
380065aa JA |
759 | void *hash; |
760 | ||
214e1eca JA |
761 | /* |
762 | * 1024 is too much on some machines, scale max_jobs if | |
763 | * we get a failure that looks like too large a shm segment | |
764 | */ | |
765 | do { | |
766 | size_t size = max_jobs * sizeof(struct thread_data); | |
767 | ||
380065aa JA |
768 | size += file_hash_size; |
769 | ||
214e1eca JA |
770 | shm_id = shmget(0, size, IPC_CREAT | 0600); |
771 | if (shm_id != -1) | |
772 | break; | |
773 | if (errno != EINVAL) { | |
774 | perror("shmget"); | |
775 | break; | |
776 | } | |
777 | ||
778 | max_jobs >>= 1; | |
779 | } while (max_jobs); | |
780 | ||
781 | if (shm_id == -1) | |
782 | return 1; | |
783 | ||
784 | threads = shmat(shm_id, NULL, 0); | |
785 | if (threads == (void *) -1) { | |
786 | perror("shmat"); | |
787 | return 1; | |
788 | } | |
789 | ||
1f809d15 | 790 | memset(threads, 0, max_jobs * sizeof(struct thread_data)); |
380065aa JA |
791 | hash = (void *) threads + max_jobs * sizeof(struct thread_data); |
792 | file_hash_init(hash); | |
214e1eca JA |
793 | atexit(free_shm); |
794 | return 0; | |
795 | } | |
796 | ||
45378b35 | 797 | static void usage(const char *name) |
4785f995 JA |
798 | { |
799 | printf("%s\n", fio_version_string); | |
45378b35 | 800 | printf("%s [options] [job options] <job file(s)>\n", name); |
ee56ad50 | 801 | printf("\t--debug=options\tEnable debug logging\n"); |
b4692828 JA |
802 | printf("\t--output\tWrite output to file\n"); |
803 | printf("\t--timeout\tRuntime in seconds\n"); | |
804 | printf("\t--latency-log\tGenerate per-job latency logs\n"); | |
805 | printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n"); | |
806 | printf("\t--minimal\tMinimal (terse) output\n"); | |
807 | printf("\t--version\tPrint version info and exit\n"); | |
fd28ca49 JA |
808 | printf("\t--help\t\tPrint this page\n"); |
809 | printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n"); | |
cca73aa7 | 810 | printf("\t--showcmd\tTurn a job file into command line options\n"); |
e592a06b AC |
811 | printf("\t--eta=when\tWhen ETA estimate should be printed\n"); |
812 | printf("\t \tMay be \"always\", \"never\" or \"auto\"\n"); | |
062c6022 | 813 | printf("\t--readonly\tTurn on safety read-only checks, preventing writes\n"); |
01f06b63 | 814 | printf("\t--section=name\tOnly run specified section in job file\n"); |
4785f995 JA |
815 | } |
816 | ||
79e48f72 | 817 | #ifdef FIO_INC_DEBUG |
ee56ad50 | 818 | struct debug_level debug_levels[] = { |
bd6f78b2 JA |
819 | { .name = "process", .shift = FD_PROCESS, }, |
820 | { .name = "file", .shift = FD_FILE, }, | |
821 | { .name = "io", .shift = FD_IO, }, | |
822 | { .name = "mem", .shift = FD_MEM, }, | |
823 | { .name = "blktrace", .shift = FD_BLKTRACE }, | |
824 | { .name = "verify", .shift = FD_VERIFY }, | |
84422acd | 825 | { .name = "random", .shift = FD_RANDOM }, |
a3d741fa | 826 | { .name = "parse", .shift = FD_PARSE }, |
ee56ad50 JA |
827 | { }, |
828 | }; | |
829 | ||
c09823ab | 830 | static int set_debug(const char *string) |
ee56ad50 JA |
831 | { |
832 | struct debug_level *dl; | |
833 | char *p = (char *) string; | |
834 | char *opt; | |
835 | int i; | |
836 | ||
837 | if (!strcmp(string, "?") || !strcmp(string, "help")) { | |
838 | int i; | |
839 | ||
840 | log_info("fio: dumping debug options:"); | |
841 | for (i = 0; debug_levels[i].name; i++) { | |
842 | dl = &debug_levels[i]; | |
843 | log_info("%s,", dl->name); | |
844 | } | |
bd6f78b2 | 845 | log_info("all\n"); |
c09823ab | 846 | return 1; |
bd6f78b2 JA |
847 | } else if (!strcmp(string, "all")) { |
848 | fio_debug = ~0UL; | |
c09823ab | 849 | return 0; |
ee56ad50 JA |
850 | } |
851 | ||
852 | while ((opt = strsep(&p, ",")) != NULL) { | |
853 | int found = 0; | |
854 | ||
855 | for (i = 0; debug_levels[i].name; i++) { | |
856 | dl = &debug_levels[i]; | |
857 | if (!strncmp(opt, dl->name, strlen(opt))) { | |
858 | log_info("fio: set debug option %s\n", opt); | |
859 | found = 1; | |
bd6f78b2 | 860 | fio_debug |= (1UL << dl->shift); |
ee56ad50 JA |
861 | break; |
862 | } | |
863 | } | |
864 | ||
865 | if (!found) | |
866 | log_err("fio: debug mask %s not found\n", opt); | |
867 | } | |
c09823ab | 868 | return 0; |
ee56ad50 | 869 | } |
79e48f72 JA |
870 | #else |
871 | static void set_debug(const char *string) | |
872 | { | |
873 | log_err("fio: debug tracing not included in build\n"); | |
c09823ab | 874 | return 1; |
79e48f72 JA |
875 | } |
876 | #endif | |
ee56ad50 | 877 | |
972cfd25 | 878 | static int parse_cmd_line(int argc, char *argv[]) |
ebac4655 | 879 | { |
b4692828 | 880 | struct thread_data *td = NULL; |
c09823ab | 881 | int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0; |
ebac4655 | 882 | |
0be06ea2 | 883 | while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) { |
ebac4655 | 884 | switch (c) { |
b4692828 JA |
885 | case 't': |
886 | def_timeout = atoi(optarg); | |
887 | break; | |
888 | case 'l': | |
889 | write_lat_log = 1; | |
890 | break; | |
891 | case 'w': | |
892 | write_bw_log = 1; | |
893 | break; | |
894 | case 'o': | |
895 | f_out = fopen(optarg, "w+"); | |
896 | if (!f_out) { | |
897 | perror("fopen output"); | |
898 | exit(1); | |
899 | } | |
900 | f_err = f_out; | |
901 | break; | |
902 | case 'm': | |
903 | terse_output = 1; | |
904 | break; | |
905 | case 'h': | |
45378b35 | 906 | usage(argv[0]); |
b4692828 | 907 | exit(0); |
fd28ca49 | 908 | case 'c': |
214e1eca | 909 | exit(fio_show_option_help(optarg)); |
cca73aa7 JA |
910 | case 's': |
911 | dump_cmdline = 1; | |
912 | break; | |
724e4435 JA |
913 | case 'r': |
914 | read_only = 1; | |
915 | break; | |
b4692828 JA |
916 | case 'v': |
917 | printf("%s\n", fio_version_string); | |
918 | exit(0); | |
e592a06b AC |
919 | case 'e': |
920 | if (!strcmp("always", optarg)) | |
921 | eta_print = FIO_ETA_ALWAYS; | |
922 | else if (!strcmp("never", optarg)) | |
923 | eta_print = FIO_ETA_NEVER; | |
924 | break; | |
ee56ad50 | 925 | case 'd': |
c09823ab JA |
926 | if (set_debug(optarg)) |
927 | do_exit++; | |
ee56ad50 | 928 | break; |
01f06b63 JA |
929 | case 'x': |
930 | if (!strcmp(optarg, "global")) { | |
931 | log_err("fio: can't use global as only section\n"); | |
c09823ab JA |
932 | do_exit++; |
933 | exit_val = 1; | |
01f06b63 JA |
934 | break; |
935 | } | |
936 | if (job_section) | |
937 | free(job_section); | |
938 | job_section = strdup(optarg); | |
939 | break; | |
b4692828 JA |
940 | case FIO_GETOPT_JOB: { |
941 | const char *opt = long_options[lidx].name; | |
942 | char *val = optarg; | |
943 | ||
c2b1e753 | 944 | if (!strncmp(opt, "name", 4) && td) { |
2dc1bbeb | 945 | ret = add_job(td, td->o.name ?: "fio", 0); |
c2b1e753 JA |
946 | if (ret) { |
947 | put_job(td); | |
948 | return 0; | |
949 | } | |
950 | td = NULL; | |
951 | } | |
b4692828 | 952 | if (!td) { |
01f06b63 | 953 | int is_section = !strncmp(opt, "name", 4); |
3106f220 JA |
954 | int global = 0; |
955 | ||
01f06b63 | 956 | if (!is_section || !strncmp(val, "global", 6)) |
3106f220 | 957 | global = 1; |
c2b1e753 | 958 | |
01f06b63 JA |
959 | if (is_section && skip_this_section(val)) |
960 | continue; | |
961 | ||
c2b1e753 | 962 | td = get_new_job(global, &def_thread); |
b4692828 JA |
963 | if (!td) |
964 | return 0; | |
965 | } | |
38d0adb0 | 966 | |
214e1eca | 967 | ret = fio_cmd_option_parse(td, opt, val); |
b4692828 JA |
968 | break; |
969 | } | |
970 | default: | |
c09823ab JA |
971 | do_exit++; |
972 | exit_val = 1; | |
b4692828 | 973 | break; |
ebac4655 JA |
974 | } |
975 | } | |
c9fad893 | 976 | |
c09823ab JA |
977 | if (do_exit) |
978 | exit(exit_val); | |
536582bf | 979 | |
b4692828 | 980 | if (td) { |
7d6a8904 | 981 | if (!ret) |
2dc1bbeb | 982 | ret = add_job(td, td->o.name ?: "fio", 0); |
7d6a8904 JA |
983 | if (ret) |
984 | put_job(td); | |
972cfd25 | 985 | } |
774a6177 | 986 | |
b4692828 JA |
987 | while (optind < argc) { |
988 | ini_idx++; | |
989 | ini_file = realloc(ini_file, ini_idx * sizeof(char *)); | |
990 | ini_file[ini_idx - 1] = strdup(argv[optind]); | |
991 | optind++; | |
eb8bbf48 | 992 | } |
972cfd25 JA |
993 | |
994 | return ini_idx; | |
ebac4655 JA |
995 | } |
996 | ||
ebac4655 JA |
997 | int parse_options(int argc, char *argv[]) |
998 | { | |
972cfd25 JA |
999 | int job_files, i; |
1000 | ||
b4692828 JA |
1001 | f_out = stdout; |
1002 | f_err = stderr; | |
1003 | ||
214e1eca | 1004 | fio_options_dup_and_init(long_options); |
b4692828 | 1005 | |
ebac4655 JA |
1006 | if (setup_thread_area()) |
1007 | return 1; | |
1008 | if (fill_def_thread()) | |
1009 | return 1; | |
1010 | ||
972cfd25 | 1011 | job_files = parse_cmd_line(argc, argv); |
ebac4655 | 1012 | |
972cfd25 JA |
1013 | for (i = 0; i < job_files; i++) { |
1014 | if (fill_def_thread()) | |
1015 | return 1; | |
0c7e37a0 | 1016 | if (parse_jobs_ini(ini_file[i], i)) |
972cfd25 | 1017 | return 1; |
88c6ed80 | 1018 | free(ini_file[i]); |
972cfd25 | 1019 | } |
ebac4655 | 1020 | |
88c6ed80 | 1021 | free(ini_file); |
d23bb327 | 1022 | options_mem_free(&def_thread); |
b4692828 JA |
1023 | |
1024 | if (!thread_number) { | |
cca73aa7 JA |
1025 | if (dump_cmdline) |
1026 | return 0; | |
1027 | ||
b4692828 | 1028 | log_err("No jobs defined(s)\n"); |
45378b35 | 1029 | usage(argv[0]); |
b4692828 JA |
1030 | return 1; |
1031 | } | |
1032 | ||
ebac4655 JA |
1033 | return 0; |
1034 | } |