Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1 | /* |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 2 | * This file contains job initialization and setup functions. |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 3 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 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> |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 11 | #include <getopt.h> |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 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" |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 18 | #include "parse.h" |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 19 | |
Jens Axboe | 214e1ec | 2007-03-15 10:48:13 +0100 | [diff] [blame] | 20 | static char fio_version_string[] = "fio 1.14a"; |
| 21 | |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 22 | #define FIO_RANDSEED (0xb1899bedUL) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 23 | |
Jens Axboe | 214e1ec | 2007-03-15 10:48:13 +0100 | [diff] [blame] | 24 | static char **ini_file; |
| 25 | static int max_jobs = MAX_JOBS; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 26 | |
Jens Axboe | 214e1ec | 2007-03-15 10:48:13 +0100 | [diff] [blame] | 27 | struct thread_data def_thread; |
| 28 | struct thread_data *threads = NULL; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 29 | |
Jens Axboe | 214e1ec | 2007-03-15 10:48:13 +0100 | [diff] [blame] | 30 | int exitall_on_terminate = 0; |
| 31 | int terse_output = 0; |
| 32 | unsigned long long mlock_size = 0; |
| 33 | FILE *f_out = NULL; |
| 34 | FILE *f_err = NULL; |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 35 | |
Jens Axboe | 214e1ec | 2007-03-15 10:48:13 +0100 | [diff] [blame] | 36 | int write_bw_log = 0; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 37 | |
Jens Axboe | 214e1ec | 2007-03-15 10:48:13 +0100 | [diff] [blame] | 38 | static int def_timeout = 0; |
| 39 | static int write_lat_log = 0; |
| 40 | |
| 41 | static int prev_group_jobs; |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * Command line options. These will contain the above, plus a few |
| 45 | * extra that only pertain to fio itself and not jobs. |
| 46 | */ |
Jens Axboe | 214e1ec | 2007-03-15 10:48:13 +0100 | [diff] [blame] | 47 | static struct option long_options[FIO_NR_OPTIONS] = { |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 48 | { |
| 49 | .name = "output", |
| 50 | .has_arg = required_argument, |
| 51 | .val = 'o', |
| 52 | }, |
| 53 | { |
| 54 | .name = "timeout", |
| 55 | .has_arg = required_argument, |
| 56 | .val = 't', |
| 57 | }, |
| 58 | { |
| 59 | .name = "latency-log", |
| 60 | .has_arg = required_argument, |
| 61 | .val = 'l', |
| 62 | }, |
| 63 | { |
| 64 | .name = "bandwidth-log", |
| 65 | .has_arg = required_argument, |
| 66 | .val = 'b', |
| 67 | }, |
| 68 | { |
| 69 | .name = "minimal", |
| 70 | .has_arg = optional_argument, |
| 71 | .val = 'm', |
| 72 | }, |
| 73 | { |
| 74 | .name = "version", |
| 75 | .has_arg = no_argument, |
| 76 | .val = 'v', |
| 77 | }, |
| 78 | { |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 79 | .name = "help", |
| 80 | .has_arg = no_argument, |
| 81 | .val = 'h', |
| 82 | }, |
| 83 | { |
| 84 | .name = "cmdhelp", |
Jens Axboe | 320beef | 2007-03-01 10:44:12 +0100 | [diff] [blame] | 85 | .has_arg = optional_argument, |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 86 | .val = 'c', |
| 87 | }, |
| 88 | { |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 89 | .name = NULL, |
| 90 | }, |
| 91 | }; |
| 92 | |
Joel Becker | 9728ce3 | 2007-03-01 08:24:39 +0100 | [diff] [blame] | 93 | FILE *get_f_out() |
| 94 | { |
| 95 | return f_out; |
| 96 | } |
| 97 | |
| 98 | FILE *get_f_err() |
| 99 | { |
| 100 | return f_err; |
| 101 | } |
| 102 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 103 | /* |
| 104 | * Return a free job structure. |
| 105 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 106 | static struct thread_data *get_new_job(int global, struct thread_data *parent) |
| 107 | { |
| 108 | struct thread_data *td; |
| 109 | |
| 110 | if (global) |
| 111 | return &def_thread; |
| 112 | if (thread_number >= max_jobs) |
| 113 | return NULL; |
| 114 | |
| 115 | td = &threads[thread_number++]; |
Jens Axboe | ddaeaa5 | 2006-06-08 11:00:58 +0200 | [diff] [blame] | 116 | *td = *parent; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 117 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 118 | td->thread_number = thread_number; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 119 | return td; |
| 120 | } |
| 121 | |
| 122 | static void put_job(struct thread_data *td) |
| 123 | { |
Jens Axboe | 549577a | 2006-10-30 12:23:41 +0100 | [diff] [blame] | 124 | if (td == &def_thread) |
| 125 | return; |
| 126 | |
Jens Axboe | 16edf25 | 2007-02-10 14:59:22 +0100 | [diff] [blame] | 127 | if (td->error) |
Jens Axboe | 6d86144 | 2007-03-15 09:22:23 +0100 | [diff] [blame] | 128 | log_info("fio: %s\n", td->verror); |
Jens Axboe | 16edf25 | 2007-02-10 14:59:22 +0100 | [diff] [blame] | 129 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 130 | memset(&threads[td->thread_number - 1], 0, sizeof(*td)); |
| 131 | thread_number--; |
| 132 | } |
| 133 | |
Jens Axboe | 127f686 | 2007-03-15 12:09:57 +0100 | [diff] [blame^] | 134 | static int setup_rate(struct thread_data *td) |
| 135 | { |
| 136 | unsigned long nr_reads_per_msec; |
| 137 | unsigned long long rate; |
| 138 | unsigned int bs; |
| 139 | |
| 140 | if (!td->rate && !td->rate_iops) |
| 141 | return 0; |
| 142 | |
| 143 | if (td_rw(td)) |
| 144 | bs = td->rw_min_bs; |
| 145 | else if (td_read(td)) |
| 146 | bs = td->min_bs[DDIR_READ]; |
| 147 | else |
| 148 | bs = td->min_bs[DDIR_WRITE]; |
| 149 | |
| 150 | if (td->rate) { |
| 151 | rate = td->rate; |
| 152 | nr_reads_per_msec = (rate * 1024 * 1000LL) / bs; |
| 153 | } else |
| 154 | nr_reads_per_msec = td->rate_iops * 1000UL; |
| 155 | |
| 156 | if (!nr_reads_per_msec) { |
| 157 | log_err("rate lower than supported\n"); |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | td->rate_usec_cycle = 1000000000ULL / nr_reads_per_msec; |
| 162 | td->rate_pending_usleep = 0; |
| 163 | return 0; |
| 164 | } |
| 165 | |
Jens Axboe | dad915e | 2006-10-27 11:10:18 +0200 | [diff] [blame] | 166 | /* |
| 167 | * Lazy way of fixing up options that depend on each other. We could also |
| 168 | * define option callback handlers, but this is easier. |
| 169 | */ |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 170 | static int fixup_options(struct thread_data *td) |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 171 | { |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 172 | if (!td->rwmixread && td->rwmixwrite) |
| 173 | td->rwmixread = 100 - td->rwmixwrite; |
Jens Axboe | dad915e | 2006-10-27 11:10:18 +0200 | [diff] [blame] | 174 | |
Jens Axboe | 076efc7 | 2006-10-27 11:24:25 +0200 | [diff] [blame] | 175 | if (td->write_iolog_file && td->read_iolog_file) { |
| 176 | log_err("fio: read iolog overrides write_iolog\n"); |
| 177 | free(td->write_iolog_file); |
| 178 | td->write_iolog_file = NULL; |
| 179 | } |
Jens Axboe | 16b462a | 2006-10-30 12:35:18 +0100 | [diff] [blame] | 180 | |
| 181 | if (td->io_ops->flags & FIO_SYNCIO) |
| 182 | td->iodepth = 1; |
| 183 | else { |
| 184 | if (!td->iodepth) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 185 | td->iodepth = td->open_files; |
Jens Axboe | 16b462a | 2006-10-30 12:35:18 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* |
| 189 | * only really works for sequential io for now, and with 1 file |
| 190 | */ |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 191 | if (td->zone_size && td_random(td) && td->open_files == 1) |
Jens Axboe | 16b462a | 2006-10-30 12:35:18 +0100 | [diff] [blame] | 192 | td->zone_size = 0; |
| 193 | |
| 194 | /* |
| 195 | * Reads can do overwrites, we always need to pre-create the file |
| 196 | */ |
| 197 | if (td_read(td) || td_rw(td)) |
| 198 | td->overwrite = 1; |
| 199 | |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 200 | if (!td->min_bs[DDIR_READ]) |
| 201 | td->min_bs[DDIR_READ]= td->bs[DDIR_READ]; |
| 202 | if (!td->max_bs[DDIR_READ]) |
| 203 | td->max_bs[DDIR_READ] = td->bs[DDIR_READ]; |
| 204 | if (!td->min_bs[DDIR_WRITE]) |
Jens Axboe | 75e6f36 | 2006-11-03 08:17:09 +0100 | [diff] [blame] | 205 | td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE]; |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 206 | if (!td->max_bs[DDIR_WRITE]) |
Jens Axboe | 75e6f36 | 2006-11-03 08:17:09 +0100 | [diff] [blame] | 207 | td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE]; |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 208 | |
| 209 | td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]); |
| 210 | |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 211 | if (!td->file_size_high) |
| 212 | td->file_size_high = td->file_size_low; |
| 213 | |
Jens Axboe | 16b462a | 2006-10-30 12:35:18 +0100 | [diff] [blame] | 214 | if (td_read(td) && !td_rw(td)) |
| 215 | td->verify = 0; |
Jens Axboe | bb8895e | 2006-10-30 15:14:48 +0100 | [diff] [blame] | 216 | |
| 217 | if (td->norandommap && td->verify != VERIFY_NONE) { |
| 218 | log_err("fio: norandommap given, verify disabled\n"); |
| 219 | td->verify = VERIFY_NONE; |
| 220 | } |
Jens Axboe | 690adba | 2006-10-30 15:25:09 +0100 | [diff] [blame] | 221 | if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO)) |
| 222 | log_err("fio: bs_unaligned may not work with raw io\n"); |
Jens Axboe | e0a2233 | 2006-12-20 12:54:25 +0100 | [diff] [blame] | 223 | |
| 224 | /* |
Jens Axboe | 48097d5 | 2007-02-17 06:30:44 +0100 | [diff] [blame] | 225 | * thinktime_spin must be less than thinktime |
| 226 | */ |
| 227 | if (td->thinktime_spin > td->thinktime) |
| 228 | td->thinktime_spin = td->thinktime; |
Jens Axboe | e916b39 | 2007-02-20 14:37:26 +0100 | [diff] [blame] | 229 | |
| 230 | /* |
| 231 | * The low water mark cannot be bigger than the iodepth |
| 232 | */ |
Jens Axboe | 9467b77 | 2007-02-27 19:56:43 +0100 | [diff] [blame] | 233 | if (td->iodepth_low > td->iodepth || !td->iodepth_low) { |
| 234 | /* |
| 235 | * syslet work around - if the workload is sequential, |
| 236 | * we want to let the queue drain all the way down to |
| 237 | * avoid seeking between async threads |
| 238 | */ |
| 239 | if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td)) |
| 240 | td->iodepth_low = 1; |
| 241 | else |
| 242 | td->iodepth_low = td->iodepth; |
| 243 | } |
Jens Axboe | cb5ab51 | 2007-02-26 12:57:09 +0100 | [diff] [blame] | 244 | |
| 245 | /* |
| 246 | * If batch number isn't set, default to the same as iodepth |
| 247 | */ |
| 248 | if (td->iodepth_batch > td->iodepth || !td->iodepth_batch) |
| 249 | td->iodepth_batch = td->iodepth; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 250 | |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 251 | if (td->nr_files > td->files_index) |
Jens Axboe | 9f9214f | 2007-03-13 14:02:16 +0100 | [diff] [blame] | 252 | td->nr_files = td->files_index; |
| 253 | |
| 254 | if (td->open_files > td->nr_files || !td->open_files) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 255 | td->open_files = td->nr_files; |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 256 | |
| 257 | if ((td->rate && td->rate_iops) || (td->ratemin && td->rate_iops_min)) { |
| 258 | log_err("fio: rate and rate_iops are mutually exclusive\n"); |
| 259 | return 1; |
| 260 | } |
| 261 | if ((td->rate < td->ratemin) || (td->rate_iops < td->rate_iops_min)) { |
| 262 | log_err("fio: minimum rate exceeds rate\n"); |
| 263 | return 1; |
| 264 | } |
| 265 | |
| 266 | return 0; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 267 | } |
| 268 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 269 | /* |
Jens Axboe | f8977ee | 2006-11-06 14:03:03 +0100 | [diff] [blame] | 270 | * This function leaks the buffer |
| 271 | */ |
| 272 | static char *to_kmg(unsigned int val) |
| 273 | { |
| 274 | char *buf = malloc(32); |
Jens Axboe | f3502ba | 2007-02-14 01:27:09 +0100 | [diff] [blame] | 275 | char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 }; |
Jens Axboe | f8977ee | 2006-11-06 14:03:03 +0100 | [diff] [blame] | 276 | char *p = post; |
| 277 | |
Jens Axboe | 245142f | 2006-11-08 12:49:21 +0100 | [diff] [blame] | 278 | do { |
Jens Axboe | f8977ee | 2006-11-06 14:03:03 +0100 | [diff] [blame] | 279 | if (val & 1023) |
| 280 | break; |
| 281 | |
| 282 | val >>= 10; |
| 283 | p++; |
Jens Axboe | 245142f | 2006-11-08 12:49:21 +0100 | [diff] [blame] | 284 | } while (*p); |
Jens Axboe | f8977ee | 2006-11-06 14:03:03 +0100 | [diff] [blame] | 285 | |
| 286 | snprintf(buf, 31, "%u%c", val, *p); |
| 287 | return buf; |
| 288 | } |
| 289 | |
Jens Axboe | 09629a9 | 2007-03-09 09:00:06 +0100 | [diff] [blame] | 290 | /* External engines are specified by "external:name.o") */ |
| 291 | static const char *get_engine_name(const char *str) |
| 292 | { |
| 293 | char *p = strstr(str, ":"); |
| 294 | |
| 295 | if (!p) |
| 296 | return str; |
| 297 | |
| 298 | p++; |
| 299 | strip_blank_front(&p); |
| 300 | strip_blank_end(p); |
| 301 | return p; |
| 302 | } |
| 303 | |
Jens Axboe | e132cba | 2007-03-14 14:23:54 +0100 | [diff] [blame] | 304 | static int exists_and_not_file(const char *filename) |
| 305 | { |
| 306 | struct stat sb; |
| 307 | |
| 308 | if (lstat(filename, &sb) == -1) |
| 309 | return 0; |
| 310 | |
| 311 | if (S_ISREG(sb.st_mode)) |
| 312 | return 0; |
| 313 | |
| 314 | return 1; |
| 315 | } |
| 316 | |
Jens Axboe | f8977ee | 2006-11-06 14:03:03 +0100 | [diff] [blame] | 317 | /* |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 318 | * Initialize the various random states we need (random io, block size ranges, |
| 319 | * read/write mix, etc). |
| 320 | */ |
| 321 | static int init_random_state(struct thread_data *td) |
| 322 | { |
| 323 | unsigned long seeds[6]; |
| 324 | int fd, num_maps, blocks; |
| 325 | struct fio_file *f; |
| 326 | unsigned int i; |
| 327 | |
| 328 | fd = open("/dev/urandom", O_RDONLY); |
| 329 | if (fd == -1) { |
| 330 | td_verror(td, errno, "open"); |
| 331 | return 1; |
| 332 | } |
| 333 | |
| 334 | if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) { |
| 335 | td_verror(td, EIO, "read"); |
| 336 | close(fd); |
| 337 | return 1; |
| 338 | } |
| 339 | |
| 340 | close(fd); |
| 341 | |
| 342 | os_random_seed(seeds[0], &td->bsrange_state); |
| 343 | os_random_seed(seeds[1], &td->verify_state); |
| 344 | os_random_seed(seeds[2], &td->rwmix_state); |
| 345 | |
| 346 | if (td->file_service_type == FIO_FSERVICE_RANDOM) |
| 347 | os_random_seed(seeds[3], &td->next_file_state); |
| 348 | |
| 349 | os_random_seed(seeds[5], &td->file_size_state); |
| 350 | |
| 351 | if (!td_random(td)) |
| 352 | return 0; |
| 353 | |
| 354 | if (td->rand_repeatable) |
| 355 | seeds[4] = FIO_RANDSEED * td->thread_number; |
| 356 | |
| 357 | if (!td->norandommap) { |
| 358 | for_each_file(td, f, i) { |
| 359 | blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs; |
| 360 | num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP; |
| 361 | f->file_map = malloc(num_maps * sizeof(long)); |
| 362 | if (!f->file_map) { |
| 363 | log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n"); |
| 364 | return 1; |
| 365 | } |
| 366 | f->num_maps = num_maps; |
| 367 | memset(f->file_map, 0, num_maps * sizeof(long)); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | os_random_seed(seeds[4], &td->random_state); |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | |
| 376 | /* |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 377 | * Adds a job to the list of things todo. Sanitizes the various options |
| 378 | * to make sure we don't have conflicts, and initializes various |
| 379 | * members of td. |
| 380 | */ |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 381 | static int add_job(struct thread_data *td, const char *jobname, int job_add_num) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 382 | { |
Jens Axboe | 413dd45 | 2007-02-23 09:26:09 +0100 | [diff] [blame] | 383 | const char *ddir_str[] = { NULL, "read", "write", "rw", NULL, |
| 384 | "randread", "randwrite", "randrw" }; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 385 | unsigned int i; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 386 | struct fio_file *f; |
Jens Axboe | 09629a9 | 2007-03-09 09:00:06 +0100 | [diff] [blame] | 387 | const char *engine; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 388 | char fname[PATH_MAX]; |
Jens Axboe | e132cba | 2007-03-14 14:23:54 +0100 | [diff] [blame] | 389 | int numjobs, file_alloced; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 390 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 391 | /* |
| 392 | * the def_thread is just for options, it's not a real job |
| 393 | */ |
| 394 | if (td == &def_thread) |
| 395 | return 0; |
| 396 | |
Jens Axboe | 09629a9 | 2007-03-09 09:00:06 +0100 | [diff] [blame] | 397 | engine = get_engine_name(td->ioengine); |
| 398 | td->io_ops = load_ioengine(td, engine); |
| 399 | if (!td->io_ops) { |
| 400 | log_err("fio: failed to load engine %s\n", engine); |
Jens Axboe | 205927a | 2007-03-15 11:06:32 +0100 | [diff] [blame] | 401 | goto err; |
Jens Axboe | 09629a9 | 2007-03-09 09:00:06 +0100 | [diff] [blame] | 402 | } |
Jens Axboe | df64119 | 2006-10-12 07:55:41 +0200 | [diff] [blame] | 403 | |
Jens Axboe | 9cedf16 | 2007-03-12 11:29:30 +0100 | [diff] [blame] | 404 | if (td->use_thread) |
| 405 | nr_thread++; |
| 406 | else |
| 407 | nr_process++; |
| 408 | |
Jens Axboe | 690adba | 2006-10-30 15:25:09 +0100 | [diff] [blame] | 409 | if (td->odirect) |
| 410 | td->io_ops->flags |= FIO_RAWIO; |
| 411 | |
Jens Axboe | e132cba | 2007-03-14 14:23:54 +0100 | [diff] [blame] | 412 | file_alloced = 0; |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 413 | if (!td->filename && !td->files_index) { |
Jens Axboe | e132cba | 2007-03-14 14:23:54 +0100 | [diff] [blame] | 414 | file_alloced = 1; |
Jens Axboe | 80be24f | 2007-03-12 11:01:25 +0100 | [diff] [blame] | 415 | |
Jens Axboe | e132cba | 2007-03-14 14:23:54 +0100 | [diff] [blame] | 416 | if (td->nr_files == 1 && exists_and_not_file(jobname)) |
| 417 | add_file(td, jobname); |
Jens Axboe | 7b05a21 | 2007-03-13 11:17:07 +0100 | [diff] [blame] | 418 | else { |
| 419 | for (i = 0; i < td->nr_files; i++) { |
Jens Axboe | e132cba | 2007-03-14 14:23:54 +0100 | [diff] [blame] | 420 | sprintf(fname, "%s.%d.%d", jobname, td->thread_number, i); |
Jens Axboe | 7b05a21 | 2007-03-13 11:17:07 +0100 | [diff] [blame] | 421 | add_file(td, fname); |
| 422 | } |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 423 | } |
Jens Axboe | 0af7b54 | 2006-02-17 10:10:12 +0100 | [diff] [blame] | 424 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 425 | |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 426 | if (fixup_options(td)) |
| 427 | goto err; |
Jens Axboe | e0a2233 | 2006-12-20 12:54:25 +0100 | [diff] [blame] | 428 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 429 | for_each_file(td, f, i) { |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 430 | if (td->directory && f->filetype == FIO_TYPE_FILE) { |
| 431 | sprintf(fname, "%s/%s", td->directory, f->file_name); |
| 432 | f->file_name = strdup(fname); |
| 433 | } |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 434 | } |
| 435 | |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 436 | td->mutex = fio_sem_init(0); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 437 | |
Jens Axboe | 756867b | 2007-03-06 15:19:24 +0100 | [diff] [blame] | 438 | td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX; |
| 439 | td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX; |
| 440 | td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 441 | |
Jens Axboe | 3c5df6f | 2007-03-12 15:09:24 +0100 | [diff] [blame] | 442 | if ((td->stonewall || td->numjobs > 1) && prev_group_jobs) { |
| 443 | prev_group_jobs = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 444 | groupid++; |
Jens Axboe | 3c5df6f | 2007-03-12 15:09:24 +0100 | [diff] [blame] | 445 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 446 | |
| 447 | td->groupid = groupid; |
Jens Axboe | 3c5df6f | 2007-03-12 15:09:24 +0100 | [diff] [blame] | 448 | prev_group_jobs++; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 449 | |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 450 | if (init_random_state(td)) |
| 451 | goto err; |
| 452 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 453 | if (setup_rate(td)) |
| 454 | goto err; |
| 455 | |
Jens Axboe | ec94ec5 | 2006-10-20 10:59:19 +0200 | [diff] [blame] | 456 | if (td->write_lat_log) { |
Jens Axboe | 756867b | 2007-03-06 15:19:24 +0100 | [diff] [blame] | 457 | setup_log(&td->ts.slat_log); |
| 458 | setup_log(&td->ts.clat_log); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 459 | } |
Jens Axboe | ec94ec5 | 2006-10-20 10:59:19 +0200 | [diff] [blame] | 460 | if (td->write_bw_log) |
Jens Axboe | 756867b | 2007-03-06 15:19:24 +0100 | [diff] [blame] | 461 | setup_log(&td->ts.bw_log); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 462 | |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 463 | if (!td->name) |
| 464 | td->name = strdup(jobname); |
Jens Axboe | 0145205 | 2006-06-07 10:29:47 +0200 | [diff] [blame] | 465 | |
Jens Axboe | c6ae0a5 | 2006-06-12 11:04:44 +0200 | [diff] [blame] | 466 | if (!terse_output) { |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 467 | if (!job_add_num) { |
Jens Axboe | ba0fbe1 | 2007-03-09 14:34:23 +0100 | [diff] [blame] | 468 | if (!strcmp(td->io_ops->name, "cpuio")) |
Jens Axboe | 6d86144 | 2007-03-15 09:22:23 +0100 | [diff] [blame] | 469 | log_info("%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle); |
Jens Axboe | f8977ee | 2006-11-06 14:03:03 +0100 | [diff] [blame] | 470 | else { |
| 471 | char *c1, *c2, *c3, *c4; |
| 472 | |
| 473 | c1 = to_kmg(td->min_bs[DDIR_READ]); |
| 474 | c2 = to_kmg(td->max_bs[DDIR_READ]); |
| 475 | c3 = to_kmg(td->min_bs[DDIR_WRITE]); |
| 476 | c4 = to_kmg(td->max_bs[DDIR_WRITE]); |
| 477 | |
Jens Axboe | 6d86144 | 2007-03-15 09:22:23 +0100 | [diff] [blame] | 478 | log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s, ioengine=%s, iodepth=%u\n", td->name, td->groupid, ddir_str[td->td_ddir], c1, c2, c3, c4, td->io_ops->name, td->iodepth); |
Jens Axboe | f8977ee | 2006-11-06 14:03:03 +0100 | [diff] [blame] | 479 | |
| 480 | free(c1); |
| 481 | free(c2); |
| 482 | free(c3); |
| 483 | free(c4); |
| 484 | } |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 485 | } else if (job_add_num == 1) |
Jens Axboe | 6d86144 | 2007-03-15 09:22:23 +0100 | [diff] [blame] | 486 | log_info("...\n"); |
Jens Axboe | c6ae0a5 | 2006-06-12 11:04:44 +0200 | [diff] [blame] | 487 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 488 | |
| 489 | /* |
| 490 | * recurse add identical jobs, clear numjobs and stonewall options |
| 491 | * as they don't apply to sub-jobs |
| 492 | */ |
| 493 | numjobs = td->numjobs; |
| 494 | while (--numjobs) { |
| 495 | struct thread_data *td_new = get_new_job(0, td); |
| 496 | |
| 497 | if (!td_new) |
| 498 | goto err; |
| 499 | |
| 500 | td_new->numjobs = 1; |
| 501 | td_new->stonewall = 0; |
Jens Axboe | e132cba | 2007-03-14 14:23:54 +0100 | [diff] [blame] | 502 | |
| 503 | if (file_alloced) { |
| 504 | td_new->filename = NULL; |
| 505 | td_new->files_index = 0; |
| 506 | td_new->files = NULL; |
| 507 | } |
| 508 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 509 | job_add_num = numjobs - 1; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 510 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 511 | if (add_job(td_new, jobname, job_add_num)) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 512 | goto err; |
| 513 | } |
Jens Axboe | 3c5df6f | 2007-03-12 15:09:24 +0100 | [diff] [blame] | 514 | |
| 515 | if (td->numjobs > 1) { |
| 516 | groupid++; |
| 517 | prev_group_jobs = 0; |
| 518 | } |
| 519 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 520 | return 0; |
| 521 | err: |
| 522 | put_job(td); |
| 523 | return -1; |
| 524 | } |
| 525 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 526 | static int is_empty_or_comment(char *line) |
| 527 | { |
| 528 | unsigned int i; |
| 529 | |
| 530 | for (i = 0; i < strlen(line); i++) { |
| 531 | if (line[i] == ';') |
| 532 | return 1; |
Ingo Molnar | 5cc2da3 | 2007-02-20 10:19:44 +0100 | [diff] [blame] | 533 | if (line[i] == '#') |
| 534 | return 1; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 535 | if (!isspace(line[i]) && !iscntrl(line[i])) |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | return 1; |
| 540 | } |
| 541 | |
Jens Axboe | 313cb20 | 2006-12-21 09:50:00 +0100 | [diff] [blame] | 542 | /* |
Jens Axboe | 0726198 | 2006-06-07 10:51:12 +0200 | [diff] [blame] | 543 | * This is our [ini] type file parser. |
| 544 | */ |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 545 | static int parse_jobs_ini(char *file, int stonewall_flag) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 546 | { |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 547 | unsigned int global; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 548 | struct thread_data *td; |
Jens Axboe | fee3bb4 | 2006-10-30 13:32:08 +0100 | [diff] [blame] | 549 | char *string, *name; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 550 | fpos_t off; |
| 551 | FILE *f; |
| 552 | char *p; |
Jens Axboe | 0c7e37a | 2006-06-13 14:53:38 +0200 | [diff] [blame] | 553 | int ret = 0, stonewall; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 554 | |
| 555 | f = fopen(file, "r"); |
| 556 | if (!f) { |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 557 | perror("fopen job file"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 558 | return 1; |
| 559 | } |
| 560 | |
| 561 | string = malloc(4096); |
| 562 | name = malloc(256); |
Jens Axboe | fee3bb4 | 2006-10-30 13:32:08 +0100 | [diff] [blame] | 563 | memset(name, 0, 256); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 564 | |
Jens Axboe | 0c7e37a | 2006-06-13 14:53:38 +0200 | [diff] [blame] | 565 | stonewall = stonewall_flag; |
Jens Axboe | 7c124ac | 2006-10-30 13:36:52 +0100 | [diff] [blame] | 566 | do { |
| 567 | p = fgets(string, 4095, f); |
| 568 | if (!p) |
Jens Axboe | 45410ac | 2006-06-07 11:10:39 +0200 | [diff] [blame] | 569 | break; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 570 | if (is_empty_or_comment(p)) |
| 571 | continue; |
Jens Axboe | fee3bb4 | 2006-10-30 13:32:08 +0100 | [diff] [blame] | 572 | if (sscanf(p, "[%255s]", name) != 1) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 573 | continue; |
| 574 | |
| 575 | global = !strncmp(name, "global", 6); |
| 576 | |
| 577 | name[strlen(name) - 1] = '\0'; |
| 578 | |
| 579 | td = get_new_job(global, &def_thread); |
Jens Axboe | 45410ac | 2006-06-07 11:10:39 +0200 | [diff] [blame] | 580 | if (!td) { |
| 581 | ret = 1; |
| 582 | break; |
| 583 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 584 | |
Jens Axboe | 972cfd2 | 2006-06-09 11:08:56 +0200 | [diff] [blame] | 585 | /* |
| 586 | * Seperate multiple job files by a stonewall |
| 587 | */ |
Jens Axboe | f948191 | 2006-06-09 11:37:28 +0200 | [diff] [blame] | 588 | if (!global && stonewall) { |
Jens Axboe | 972cfd2 | 2006-06-09 11:08:56 +0200 | [diff] [blame] | 589 | td->stonewall = stonewall; |
| 590 | stonewall = 0; |
| 591 | } |
| 592 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 593 | fgetpos(f, &off); |
| 594 | while ((p = fgets(string, 4096, f)) != NULL) { |
| 595 | if (is_empty_or_comment(p)) |
| 596 | continue; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 597 | |
Jens Axboe | b6754f9 | 2006-05-30 14:29:32 +0200 | [diff] [blame] | 598 | strip_blank_front(&p); |
Jens Axboe | 7c124ac | 2006-10-30 13:36:52 +0100 | [diff] [blame] | 599 | |
| 600 | if (p[0] == '[') |
| 601 | break; |
| 602 | |
Jens Axboe | 4ae3f76 | 2006-05-30 13:35:14 +0200 | [diff] [blame] | 603 | strip_blank_end(p); |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 604 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 605 | fgetpos(f, &off); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 606 | |
Jens Axboe | 45410ac | 2006-06-07 11:10:39 +0200 | [diff] [blame] | 607 | /* |
| 608 | * Don't break here, continue parsing options so we |
| 609 | * dump all the bad ones. Makes trial/error fixups |
| 610 | * easier on the user. |
| 611 | */ |
Jens Axboe | 214e1ec | 2007-03-15 10:48:13 +0100 | [diff] [blame] | 612 | ret |= fio_option_parse(td, p); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 613 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 614 | |
Jens Axboe | 45410ac | 2006-06-07 11:10:39 +0200 | [diff] [blame] | 615 | if (!ret) { |
| 616 | fsetpos(f, &off); |
| 617 | ret = add_job(td, name, 0); |
Jens Axboe | b1508cf | 2006-11-02 09:12:40 +0100 | [diff] [blame] | 618 | } else { |
| 619 | log_err("fio: job %s dropped\n", name); |
| 620 | put_job(td); |
Jens Axboe | 45410ac | 2006-06-07 11:10:39 +0200 | [diff] [blame] | 621 | } |
Jens Axboe | 7c124ac | 2006-10-30 13:36:52 +0100 | [diff] [blame] | 622 | } while (!ret); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 623 | |
| 624 | free(string); |
| 625 | free(name); |
| 626 | fclose(f); |
Jens Axboe | 45410ac | 2006-06-07 11:10:39 +0200 | [diff] [blame] | 627 | return ret; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | static int fill_def_thread(void) |
| 631 | { |
| 632 | memset(&def_thread, 0, sizeof(def_thread)); |
| 633 | |
| 634 | if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) { |
| 635 | perror("sched_getaffinity"); |
| 636 | return 1; |
| 637 | } |
| 638 | |
| 639 | /* |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 640 | * fill default options |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 641 | */ |
Jens Axboe | 214e1ec | 2007-03-15 10:48:13 +0100 | [diff] [blame] | 642 | fio_fill_default_options(&def_thread); |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 643 | |
Jens Axboe | 972cfd2 | 2006-06-09 11:08:56 +0200 | [diff] [blame] | 644 | def_thread.timeout = def_timeout; |
Jens Axboe | ec94ec5 | 2006-10-20 10:59:19 +0200 | [diff] [blame] | 645 | def_thread.write_bw_log = write_bw_log; |
| 646 | def_thread.write_lat_log = write_lat_log; |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 647 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 648 | #ifdef FIO_HAVE_DISK_UTIL |
| 649 | def_thread.do_disk_util = 1; |
| 650 | #endif |
| 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 655 | static void free_shm(void) |
| 656 | { |
| 657 | struct shmid_ds sbuf; |
| 658 | |
| 659 | if (threads) { |
Jens Axboe | 2c0ecd2 | 2006-06-08 13:25:41 +0200 | [diff] [blame] | 660 | shmdt((void *) threads); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 661 | threads = NULL; |
| 662 | shmctl(shm_id, IPC_RMID, &sbuf); |
| 663 | } |
| 664 | } |
| 665 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 666 | /* |
| 667 | * The thread area is shared between the main process and the job |
| 668 | * threads/processes. So setup a shared memory segment that will hold |
| 669 | * all the job info. |
| 670 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 671 | static int setup_thread_area(void) |
| 672 | { |
| 673 | /* |
| 674 | * 1024 is too much on some machines, scale max_jobs if |
| 675 | * we get a failure that looks like too large a shm segment |
| 676 | */ |
| 677 | do { |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 678 | size_t size = max_jobs * sizeof(struct thread_data); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 679 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 680 | shm_id = shmget(0, size, IPC_CREAT | 0600); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 681 | if (shm_id != -1) |
| 682 | break; |
| 683 | if (errno != EINVAL) { |
| 684 | perror("shmget"); |
| 685 | break; |
| 686 | } |
| 687 | |
| 688 | max_jobs >>= 1; |
| 689 | } while (max_jobs); |
| 690 | |
| 691 | if (shm_id == -1) |
| 692 | return 1; |
| 693 | |
| 694 | threads = shmat(shm_id, NULL, 0); |
| 695 | if (threads == (void *) -1) { |
| 696 | perror("shmat"); |
| 697 | return 1; |
| 698 | } |
| 699 | |
| 700 | atexit(free_shm); |
| 701 | return 0; |
| 702 | } |
| 703 | |
Jens Axboe | 214e1ec | 2007-03-15 10:48:13 +0100 | [diff] [blame] | 704 | static void usage(void) |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 705 | { |
Jens Axboe | 214e1ec | 2007-03-15 10:48:13 +0100 | [diff] [blame] | 706 | printf("%s\n", fio_version_string); |
| 707 | printf("\t--output\tWrite output to file\n"); |
| 708 | printf("\t--timeout\tRuntime in seconds\n"); |
| 709 | printf("\t--latency-log\tGenerate per-job latency logs\n"); |
| 710 | printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n"); |
| 711 | printf("\t--minimal\tMinimal (terse) output\n"); |
| 712 | printf("\t--version\tPrint version info and exit\n"); |
| 713 | printf("\t--help\t\tPrint this page\n"); |
| 714 | printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n"); |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 715 | } |
| 716 | |
Jens Axboe | 214e1ec | 2007-03-15 10:48:13 +0100 | [diff] [blame] | 717 | static int parse_cmd_line(int argc, char *argv[]) |
| 718 | { |
| 719 | struct thread_data *td = NULL; |
| 720 | int c, ini_idx = 0, lidx, ret, dont_add_job = 0; |
| 721 | |
| 722 | while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) { |
| 723 | switch (c) { |
| 724 | case 't': |
| 725 | def_timeout = atoi(optarg); |
| 726 | break; |
| 727 | case 'l': |
| 728 | write_lat_log = 1; |
| 729 | break; |
| 730 | case 'w': |
| 731 | write_bw_log = 1; |
| 732 | break; |
| 733 | case 'o': |
| 734 | f_out = fopen(optarg, "w+"); |
| 735 | if (!f_out) { |
| 736 | perror("fopen output"); |
| 737 | exit(1); |
| 738 | } |
| 739 | f_err = f_out; |
| 740 | break; |
| 741 | case 'm': |
| 742 | terse_output = 1; |
| 743 | break; |
| 744 | case 'h': |
| 745 | usage(); |
| 746 | exit(0); |
| 747 | case 'c': |
| 748 | exit(fio_show_option_help(optarg)); |
| 749 | case 'v': |
| 750 | printf("%s\n", fio_version_string); |
| 751 | exit(0); |
| 752 | case FIO_GETOPT_JOB: { |
| 753 | const char *opt = long_options[lidx].name; |
| 754 | char *val = optarg; |
| 755 | |
| 756 | if (!strncmp(opt, "name", 4) && td) { |
| 757 | ret = add_job(td, td->name ?: "fio", 0); |
| 758 | if (ret) { |
| 759 | put_job(td); |
| 760 | return 0; |
| 761 | } |
| 762 | td = NULL; |
| 763 | } |
| 764 | if (!td) { |
| 765 | int global = !strncmp(val, "global", 6); |
| 766 | |
| 767 | td = get_new_job(global, &def_thread); |
| 768 | if (!td) |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | ret = fio_cmd_option_parse(td, opt, val); |
| 773 | if (ret) |
| 774 | dont_add_job = 1; |
| 775 | break; |
| 776 | } |
| 777 | default: |
| 778 | break; |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | if (td) { |
| 783 | if (dont_add_job) |
| 784 | put_job(td); |
| 785 | else { |
| 786 | ret = add_job(td, td->name ?: "fio", 0); |
| 787 | if (ret) |
| 788 | put_job(td); |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | while (optind < argc) { |
| 793 | ini_idx++; |
| 794 | ini_file = realloc(ini_file, ini_idx * sizeof(char *)); |
| 795 | ini_file[ini_idx - 1] = strdup(argv[optind]); |
| 796 | optind++; |
| 797 | } |
| 798 | |
| 799 | return ini_idx; |
| 800 | } |
| 801 | |
| 802 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 803 | int parse_options(int argc, char *argv[]) |
| 804 | { |
Jens Axboe | 972cfd2 | 2006-06-09 11:08:56 +0200 | [diff] [blame] | 805 | int job_files, i; |
| 806 | |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 807 | f_out = stdout; |
| 808 | f_err = stderr; |
| 809 | |
Jens Axboe | 214e1ec | 2007-03-15 10:48:13 +0100 | [diff] [blame] | 810 | fio_options_dup_and_init(long_options); |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 811 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 812 | if (setup_thread_area()) |
| 813 | return 1; |
| 814 | if (fill_def_thread()) |
| 815 | return 1; |
| 816 | |
Jens Axboe | 972cfd2 | 2006-06-09 11:08:56 +0200 | [diff] [blame] | 817 | job_files = parse_cmd_line(argc, argv); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 818 | |
Jens Axboe | 972cfd2 | 2006-06-09 11:08:56 +0200 | [diff] [blame] | 819 | for (i = 0; i < job_files; i++) { |
| 820 | if (fill_def_thread()) |
| 821 | return 1; |
Jens Axboe | 0c7e37a | 2006-06-13 14:53:38 +0200 | [diff] [blame] | 822 | if (parse_jobs_ini(ini_file[i], i)) |
Jens Axboe | 972cfd2 | 2006-06-09 11:08:56 +0200 | [diff] [blame] | 823 | return 1; |
Jens Axboe | 88c6ed8 | 2006-06-09 11:28:10 +0200 | [diff] [blame] | 824 | free(ini_file[i]); |
Jens Axboe | 972cfd2 | 2006-06-09 11:08:56 +0200 | [diff] [blame] | 825 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 826 | |
Jens Axboe | 88c6ed8 | 2006-06-09 11:28:10 +0200 | [diff] [blame] | 827 | free(ini_file); |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 828 | |
| 829 | if (!thread_number) { |
| 830 | log_err("No jobs defined(s)\n"); |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 831 | return 1; |
| 832 | } |
| 833 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 834 | return 0; |
| 835 | } |