Commit | Line | Data |
---|---|---|
ebac4655 JA |
1 | /* |
2 | * fio - the flexible io tester | |
3 | * | |
4 | * Copyright (C) 2005 Jens Axboe <axboe@suse.de> | |
aae22ca7 | 5 | * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk> |
ebac4655 JA |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | */ | |
ebac4655 JA |
22 | #include <unistd.h> |
23 | #include <fcntl.h> | |
24 | #include <string.h> | |
ebac4655 JA |
25 | #include <signal.h> |
26 | #include <time.h> | |
ebac4655 | 27 | #include <assert.h> |
ebac4655 JA |
28 | #include <sys/stat.h> |
29 | #include <sys/wait.h> | |
30 | #include <sys/ipc.h> | |
31 | #include <sys/shm.h> | |
32 | #include <sys/ioctl.h> | |
33 | #include <sys/mman.h> | |
34 | ||
35 | #include "fio.h" | |
36 | #include "os.h" | |
37 | ||
38 | #define MASK (4095) | |
39 | ||
40 | #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK)) | |
41 | ||
42 | int groupid = 0; | |
43 | int thread_number = 0; | |
ebac4655 | 44 | int shm_id = 0; |
53cdc686 | 45 | int temp_stall_ts; |
c1d5725e | 46 | char *fio_inst_prefix = _INST_PREFIX; |
ebac4655 | 47 | |
c04f7ec3 JA |
48 | extern unsigned long long mlock_size; |
49 | ||
3d60d1ed | 50 | #define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync)) |
ebac4655 | 51 | |
bbfd6b00 | 52 | static volatile int startup_sem; |
ebac4655 JA |
53 | |
54 | #define TERMINATE_ALL (-1) | |
75154845 | 55 | #define JOB_START_TIMEOUT (5 * 1000) |
ebac4655 JA |
56 | |
57 | static void terminate_threads(int group_id) | |
58 | { | |
59 | int i; | |
60 | ||
61 | for (i = 0; i < thread_number; i++) { | |
62 | struct thread_data *td = &threads[i]; | |
63 | ||
64 | if (group_id == TERMINATE_ALL || groupid == td->groupid) { | |
65 | td->terminate = 1; | |
66 | td->start_delay = 0; | |
67 | } | |
68 | } | |
69 | } | |
70 | ||
71 | static void sig_handler(int sig) | |
72 | { | |
73 | switch (sig) { | |
74 | case SIGALRM: | |
75 | update_io_ticks(); | |
76 | disk_util_timer_arm(); | |
77 | print_thread_status(); | |
78 | break; | |
79 | default: | |
80 | printf("\nfio: terminating on signal\n"); | |
81 | fflush(stdout); | |
82 | terminate_threads(TERMINATE_ALL); | |
83 | break; | |
84 | } | |
85 | } | |
86 | ||
906c8d75 JA |
87 | /* |
88 | * The ->file_map[] contains a map of blocks we have or have not done io | |
89 | * to yet. Used to make sure we cover the entire range in a fair fashion. | |
90 | */ | |
53cdc686 JA |
91 | static int random_map_free(struct thread_data *td, struct fio_file *f, |
92 | unsigned long long block) | |
ebac4655 | 93 | { |
53cdc686 JA |
94 | unsigned int idx = RAND_MAP_IDX(td, f, block); |
95 | unsigned int bit = RAND_MAP_BIT(td, f, block); | |
ebac4655 | 96 | |
53cdc686 | 97 | return (f->file_map[idx] & (1UL << bit)) == 0; |
ebac4655 JA |
98 | } |
99 | ||
906c8d75 JA |
100 | /* |
101 | * Return the next free block in the map. | |
102 | */ | |
53cdc686 JA |
103 | static int get_next_free_block(struct thread_data *td, struct fio_file *f, |
104 | unsigned long long *b) | |
ebac4655 JA |
105 | { |
106 | int i; | |
107 | ||
108 | *b = 0; | |
109 | i = 0; | |
53cdc686 JA |
110 | while ((*b) * td->min_bs < f->file_size) { |
111 | if (f->file_map[i] != -1UL) { | |
112 | *b += ffz(f->file_map[i]); | |
ebac4655 JA |
113 | return 0; |
114 | } | |
115 | ||
116 | *b += BLOCKS_PER_MAP; | |
117 | i++; | |
118 | } | |
119 | ||
120 | return 1; | |
121 | } | |
122 | ||
906c8d75 JA |
123 | /* |
124 | * Mark a given offset as used in the map. | |
125 | */ | |
53cdc686 JA |
126 | static void mark_random_map(struct thread_data *td, struct fio_file *f, |
127 | struct io_u *io_u) | |
ebac4655 | 128 | { |
200bc855 | 129 | unsigned long long block = io_u->offset / (unsigned long long) td->min_bs; |
ebac4655 JA |
130 | unsigned int blocks = 0; |
131 | ||
132 | while (blocks < (io_u->buflen / td->min_bs)) { | |
133 | unsigned int idx, bit; | |
134 | ||
53cdc686 | 135 | if (!random_map_free(td, f, block)) |
ebac4655 JA |
136 | break; |
137 | ||
53cdc686 JA |
138 | idx = RAND_MAP_IDX(td, f, block); |
139 | bit = RAND_MAP_BIT(td, f, block); | |
ebac4655 | 140 | |
53cdc686 | 141 | assert(idx < f->num_maps); |
ebac4655 | 142 | |
53cdc686 | 143 | f->file_map[idx] |= (1UL << bit); |
ebac4655 JA |
144 | block++; |
145 | blocks++; | |
146 | } | |
147 | ||
148 | if ((blocks * td->min_bs) < io_u->buflen) | |
149 | io_u->buflen = blocks * td->min_bs; | |
150 | } | |
151 | ||
906c8d75 JA |
152 | /* |
153 | * For random io, generate a random new block and see if it's used. Repeat | |
154 | * until we find a free one. For sequential io, just return the end of | |
155 | * the last io issued. | |
156 | */ | |
53cdc686 JA |
157 | static int get_next_offset(struct thread_data *td, struct fio_file *f, |
158 | unsigned long long *offset) | |
20dc95c4 JA |
159 | { |
160 | unsigned long long b, rb; | |
161 | long r; | |
162 | ||
163 | if (!td->sequential) { | |
085227ad | 164 | unsigned long long max_blocks = td->io_size / td->min_bs; |
20dc95c4 JA |
165 | int loops = 50; |
166 | ||
167 | do { | |
6dfd46b9 | 168 | r = os_random_long(&td->random_state); |
085227ad | 169 | b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0)); |
53cdc686 | 170 | rb = b + (f->file_offset / td->min_bs); |
20dc95c4 | 171 | loops--; |
53cdc686 | 172 | } while (!random_map_free(td, f, rb) && loops); |
20dc95c4 JA |
173 | |
174 | if (!loops) { | |
53cdc686 | 175 | if (get_next_free_block(td, f, &b)) |
20dc95c4 JA |
176 | return 1; |
177 | } | |
178 | } else | |
53cdc686 | 179 | b = f->last_pos / td->min_bs; |
20dc95c4 | 180 | |
53cdc686 JA |
181 | *offset = (b * td->min_bs) + f->file_offset; |
182 | if (*offset > f->file_size) | |
20dc95c4 JA |
183 | return 1; |
184 | ||
185 | return 0; | |
186 | } | |
187 | ||
188 | static unsigned int get_next_buflen(struct thread_data *td) | |
189 | { | |
190 | unsigned int buflen; | |
191 | long r; | |
192 | ||
193 | if (td->min_bs == td->max_bs) | |
194 | buflen = td->min_bs; | |
195 | else { | |
6dfd46b9 | 196 | r = os_random_long(&td->bsrange_state); |
20dc95c4 JA |
197 | buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0)); |
198 | buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1); | |
199 | } | |
200 | ||
b2a15192 JA |
201 | if (buflen > td->io_size - td->this_io_bytes[td->ddir]) { |
202 | /* | |
203 | * if using direct/raw io, we may not be able to | |
204 | * shrink the size. so just fail it. | |
205 | */ | |
206 | if (td->io_ops->flags & FIO_RAWIO) | |
207 | return 0; | |
208 | ||
20dc95c4 | 209 | buflen = td->io_size - td->this_io_bytes[td->ddir]; |
b2a15192 | 210 | } |
20dc95c4 JA |
211 | |
212 | return buflen; | |
213 | } | |
214 | ||
906c8d75 JA |
215 | /* |
216 | * Check if we are above the minimum rate given. | |
217 | */ | |
ebac4655 JA |
218 | static int check_min_rate(struct thread_data *td, struct timeval *now) |
219 | { | |
220 | unsigned long spent; | |
221 | unsigned long rate; | |
222 | int ddir = td->ddir; | |
223 | ||
224 | /* | |
225 | * allow a 2 second settle period in the beginning | |
226 | */ | |
227 | if (mtime_since(&td->start, now) < 2000) | |
228 | return 0; | |
229 | ||
230 | /* | |
231 | * if rate blocks is set, sample is running | |
232 | */ | |
233 | if (td->rate_bytes) { | |
234 | spent = mtime_since(&td->lastrate, now); | |
235 | if (spent < td->ratecycle) | |
236 | return 0; | |
237 | ||
238 | rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent; | |
239 | if (rate < td->ratemin) { | |
eb8bbf48 | 240 | fprintf(f_out, "%s: min rate %d not met, got %ldKiB/sec\n", td->name, td->ratemin, rate); |
ebac4655 JA |
241 | if (rate_quit) |
242 | terminate_threads(td->groupid); | |
243 | return 1; | |
244 | } | |
245 | } | |
246 | ||
247 | td->rate_bytes = td->this_io_bytes[ddir]; | |
248 | memcpy(&td->lastrate, now, sizeof(*now)); | |
249 | return 0; | |
250 | } | |
251 | ||
252 | static inline int runtime_exceeded(struct thread_data *td, struct timeval *t) | |
253 | { | |
254 | if (!td->timeout) | |
255 | return 0; | |
256 | if (mtime_since(&td->epoch, t) >= td->timeout * 1000) | |
257 | return 1; | |
258 | ||
259 | return 0; | |
260 | } | |
261 | ||
262 | static void fill_random_bytes(struct thread_data *td, | |
263 | unsigned char *p, unsigned int len) | |
264 | { | |
265 | unsigned int todo; | |
266 | double r; | |
267 | ||
268 | while (len) { | |
6dfd46b9 | 269 | r = os_random_double(&td->verify_state); |
ebac4655 JA |
270 | |
271 | /* | |
272 | * lrand48_r seems to be broken and only fill the bottom | |
273 | * 32-bits, even on 64-bit archs with 64-bit longs | |
274 | */ | |
275 | todo = sizeof(r); | |
276 | if (todo > len) | |
277 | todo = len; | |
278 | ||
279 | memcpy(p, &r, todo); | |
280 | ||
281 | len -= todo; | |
282 | p += todo; | |
283 | } | |
284 | } | |
285 | ||
286 | static void hexdump(void *buffer, int len) | |
287 | { | |
288 | unsigned char *p = buffer; | |
289 | int i; | |
290 | ||
291 | for (i = 0; i < len; i++) | |
eb8bbf48 JA |
292 | fprintf(f_out, "%02x", p[i]); |
293 | fprintf(f_out, "\n"); | |
ebac4655 JA |
294 | } |
295 | ||
296 | static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u) | |
297 | { | |
298 | unsigned char *p = (unsigned char *) io_u->buf; | |
299 | unsigned long c; | |
ebac4655 JA |
300 | |
301 | p += sizeof(*hdr); | |
302 | c = crc32(p, hdr->len - sizeof(*hdr)); | |
ebac4655 | 303 | |
22f78b32 | 304 | if (c != hdr->crc32) { |
3b70d7e5 JA |
305 | log_err("crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen); |
306 | log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c); | |
22f78b32 | 307 | return 1; |
ebac4655 JA |
308 | } |
309 | ||
22f78b32 | 310 | return 0; |
ebac4655 JA |
311 | } |
312 | ||
313 | static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u) | |
314 | { | |
315 | unsigned char *p = (unsigned char *) io_u->buf; | |
316 | struct md5_ctx md5_ctx; | |
ebac4655 JA |
317 | |
318 | memset(&md5_ctx, 0, sizeof(md5_ctx)); | |
319 | p += sizeof(*hdr); | |
320 | md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr)); | |
321 | ||
22f78b32 | 322 | if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) { |
3b70d7e5 | 323 | log_err("md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen); |
ebac4655 JA |
324 | hexdump(hdr->md5_digest, sizeof(hdr->md5_digest)); |
325 | hexdump(md5_ctx.hash, sizeof(md5_ctx.hash)); | |
22f78b32 | 326 | return 1; |
ebac4655 JA |
327 | } |
328 | ||
22f78b32 | 329 | return 0; |
ebac4655 JA |
330 | } |
331 | ||
332 | static int verify_io_u(struct io_u *io_u) | |
333 | { | |
334 | struct verify_header *hdr = (struct verify_header *) io_u->buf; | |
335 | int ret; | |
336 | ||
337 | if (hdr->fio_magic != FIO_HDR_MAGIC) | |
338 | return 1; | |
339 | ||
340 | if (hdr->verify_type == VERIFY_MD5) | |
341 | ret = verify_io_u_md5(hdr, io_u); | |
342 | else if (hdr->verify_type == VERIFY_CRC32) | |
343 | ret = verify_io_u_crc32(hdr, io_u); | |
344 | else { | |
3b70d7e5 | 345 | log_err("Bad verify type %d\n", hdr->verify_type); |
ebac4655 JA |
346 | ret = 1; |
347 | } | |
348 | ||
349 | return ret; | |
350 | } | |
351 | ||
352 | static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len) | |
353 | { | |
354 | hdr->crc32 = crc32(p, len); | |
355 | } | |
356 | ||
357 | static void fill_md5(struct verify_header *hdr, void *p, unsigned int len) | |
358 | { | |
359 | struct md5_ctx md5_ctx; | |
360 | ||
361 | memset(&md5_ctx, 0, sizeof(md5_ctx)); | |
362 | md5_update(&md5_ctx, p, len); | |
363 | memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash)); | |
364 | } | |
365 | ||
906c8d75 JA |
366 | /* |
367 | * Return the data direction for the next io_u. If the job is a | |
368 | * mixed read/write workload, check the rwmix cycle and switch if | |
369 | * necessary. | |
370 | */ | |
a6ccc7be | 371 | static int get_rw_ddir(struct thread_data *td) |
3d60d1ed | 372 | { |
a6ccc7be JA |
373 | if (td_rw(td)) { |
374 | struct timeval now; | |
375 | unsigned long elapsed; | |
3d60d1ed | 376 | |
a6ccc7be JA |
377 | gettimeofday(&now, NULL); |
378 | elapsed = mtime_since_now(&td->rwmix_switch); | |
3d60d1ed | 379 | |
a6ccc7be JA |
380 | /* |
381 | * Check if it's time to seed a new data direction. | |
382 | */ | |
383 | if (elapsed >= td->rwmixcycle) { | |
c1ee2ca4 | 384 | int v; |
a6ccc7be | 385 | long r; |
3d60d1ed | 386 | |
c1ee2ca4 JA |
387 | r = os_random_long(&td->rwmix_state); |
388 | v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0))); | |
a6ccc7be JA |
389 | if (v < td->rwmixread) |
390 | td->rwmix_ddir = DDIR_READ; | |
391 | else | |
392 | td->rwmix_ddir = DDIR_WRITE; | |
393 | memcpy(&td->rwmix_switch, &now, sizeof(now)); | |
394 | } | |
395 | return td->rwmix_ddir; | |
396 | } else if (td_read(td)) | |
3d60d1ed JA |
397 | return DDIR_READ; |
398 | else | |
399 | return DDIR_WRITE; | |
400 | } | |
401 | ||
ebac4655 JA |
402 | /* |
403 | * fill body of io_u->buf with random data and add a header with the | |
22f78b32 | 404 | * crc32 or md5 sum of that data. |
ebac4655 JA |
405 | */ |
406 | static void populate_io_u(struct thread_data *td, struct io_u *io_u) | |
407 | { | |
408 | unsigned char *p = (unsigned char *) io_u->buf; | |
409 | struct verify_header hdr; | |
410 | ||
411 | hdr.fio_magic = FIO_HDR_MAGIC; | |
412 | hdr.len = io_u->buflen; | |
413 | p += sizeof(hdr); | |
414 | fill_random_bytes(td, p, io_u->buflen - sizeof(hdr)); | |
415 | ||
416 | if (td->verify == VERIFY_MD5) { | |
417 | fill_md5(&hdr, p, io_u->buflen - sizeof(hdr)); | |
418 | hdr.verify_type = VERIFY_MD5; | |
419 | } else { | |
420 | fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr)); | |
421 | hdr.verify_type = VERIFY_CRC32; | |
422 | } | |
423 | ||
424 | memcpy(io_u->buf, &hdr, sizeof(hdr)); | |
425 | } | |
426 | ||
aea47d44 | 427 | static int td_io_prep(struct thread_data *td, struct io_u *io_u) |
20dc95c4 | 428 | { |
2866c82d | 429 | if (td->io_ops->prep && td->io_ops->prep(td, io_u)) |
20dc95c4 JA |
430 | return 1; |
431 | ||
432 | return 0; | |
433 | } | |
434 | ||
b1ff3403 | 435 | void put_io_u(struct thread_data *td, struct io_u *io_u) |
ebac4655 | 436 | { |
53cdc686 | 437 | io_u->file = NULL; |
ebac4655 JA |
438 | list_del(&io_u->list); |
439 | list_add(&io_u->list, &td->io_u_freelist); | |
440 | td->cur_depth--; | |
441 | } | |
442 | ||
53cdc686 JA |
443 | static int fill_io_u(struct thread_data *td, struct fio_file *f, |
444 | struct io_u *io_u) | |
843a7413 JA |
445 | { |
446 | /* | |
447 | * If using an iolog, grab next piece if any available. | |
448 | */ | |
449 | if (td->read_iolog) | |
450 | return read_iolog_get(td, io_u); | |
451 | ||
aea47d44 JA |
452 | /* |
453 | * No log, let the seq/rand engine retrieve the next position. | |
454 | */ | |
53cdc686 | 455 | if (!get_next_offset(td, f, &io_u->offset)) { |
aea47d44 JA |
456 | io_u->buflen = get_next_buflen(td); |
457 | ||
458 | if (io_u->buflen) { | |
459 | io_u->ddir = get_rw_ddir(td); | |
843a7413 JA |
460 | |
461 | /* | |
462 | * If using a write iolog, store this entry. | |
463 | */ | |
464 | if (td->write_iolog) | |
465 | write_iolog_put(td, io_u); | |
466 | ||
53cdc686 | 467 | io_u->file = f; |
aea47d44 JA |
468 | return 0; |
469 | } | |
470 | } | |
471 | ||
472 | return 1; | |
473 | } | |
474 | ||
22f78b32 | 475 | #define queue_full(td) list_empty(&(td)->io_u_freelist) |
ebac4655 | 476 | |
b1ff3403 | 477 | struct io_u *__get_io_u(struct thread_data *td) |
ebac4655 | 478 | { |
22f78b32 | 479 | struct io_u *io_u = NULL; |
ebac4655 | 480 | |
22f78b32 JA |
481 | if (!queue_full(td)) { |
482 | io_u = list_entry(td->io_u_freelist.next, struct io_u, list); | |
483 | ||
484 | io_u->error = 0; | |
485 | io_u->resid = 0; | |
486 | list_del(&io_u->list); | |
487 | list_add(&io_u->list, &td->io_u_busylist); | |
488 | td->cur_depth++; | |
489 | } | |
ebac4655 | 490 | |
ebac4655 JA |
491 | return io_u; |
492 | } | |
493 | ||
906c8d75 JA |
494 | /* |
495 | * Return an io_u to be processed. Gets a buflen and offset, sets direction, | |
496 | * etc. The returned io_u is fully ready to be prepped and submitted. | |
497 | */ | |
53cdc686 | 498 | static struct io_u *get_io_u(struct thread_data *td, struct fio_file *f) |
ebac4655 JA |
499 | { |
500 | struct io_u *io_u; | |
501 | ||
502 | io_u = __get_io_u(td); | |
503 | if (!io_u) | |
504 | return NULL; | |
505 | ||
20dc95c4 JA |
506 | if (td->zone_bytes >= td->zone_size) { |
507 | td->zone_bytes = 0; | |
53cdc686 | 508 | f->last_pos += td->zone_skip; |
20dc95c4 JA |
509 | } |
510 | ||
53cdc686 | 511 | if (fill_io_u(td, f, io_u)) { |
ebac4655 JA |
512 | put_io_u(td, io_u); |
513 | return NULL; | |
514 | } | |
515 | ||
b2a15192 JA |
516 | if (io_u->buflen + io_u->offset > f->file_size) { |
517 | if (td->io_ops->flags & FIO_RAWIO) { | |
518 | put_io_u(td, io_u); | |
519 | return NULL; | |
520 | } | |
521 | ||
53cdc686 | 522 | io_u->buflen = f->file_size - io_u->offset; |
b2a15192 | 523 | } |
ebac4655 JA |
524 | |
525 | if (!io_u->buflen) { | |
526 | put_io_u(td, io_u); | |
527 | return NULL; | |
528 | } | |
529 | ||
843a7413 | 530 | if (!td->read_iolog && !td->sequential) |
53cdc686 | 531 | mark_random_map(td, f, io_u); |
ebac4655 | 532 | |
53cdc686 | 533 | f->last_pos += io_u->buflen; |
ebac4655 JA |
534 | |
535 | if (td->verify != VERIFY_NONE) | |
536 | populate_io_u(td, io_u); | |
537 | ||
aea47d44 | 538 | if (td_io_prep(td, io_u)) { |
ebac4655 JA |
539 | put_io_u(td, io_u); |
540 | return NULL; | |
541 | } | |
542 | ||
543 | gettimeofday(&io_u->start_time, NULL); | |
544 | return io_u; | |
545 | } | |
546 | ||
547 | static inline void td_set_runstate(struct thread_data *td, int runstate) | |
548 | { | |
ebac4655 JA |
549 | td->runstate = runstate; |
550 | } | |
551 | ||
aea47d44 | 552 | static int get_next_verify(struct thread_data *td, struct io_u *io_u) |
ebac4655 JA |
553 | { |
554 | struct io_piece *ipo; | |
555 | ||
22f78b32 JA |
556 | if (!list_empty(&td->io_hist_list)) { |
557 | ipo = list_entry(td->io_hist_list.next, struct io_piece, list); | |
ebac4655 | 558 | |
22f78b32 | 559 | list_del(&ipo->list); |
ebac4655 | 560 | |
22f78b32 JA |
561 | io_u->offset = ipo->offset; |
562 | io_u->buflen = ipo->len; | |
563 | io_u->ddir = DDIR_READ; | |
564 | free(ipo); | |
565 | return 0; | |
566 | } | |
567 | ||
568 | return 1; | |
ebac4655 JA |
569 | } |
570 | ||
53cdc686 JA |
571 | static struct fio_file *get_next_file(struct thread_data *td) |
572 | { | |
b2a15192 JA |
573 | int old_next_file = td->next_file; |
574 | struct fio_file *f; | |
575 | ||
576 | do { | |
577 | f = &td->files[td->next_file]; | |
578 | ||
579 | td->next_file++; | |
580 | if (td->next_file >= td->nr_files) | |
581 | td->next_file = 0; | |
582 | ||
583 | if (f->fd != -1) | |
584 | break; | |
53cdc686 | 585 | |
b2a15192 JA |
586 | f = NULL; |
587 | } while (td->next_file != old_next_file); | |
53cdc686 JA |
588 | |
589 | return f; | |
590 | } | |
591 | ||
592 | static int td_io_sync(struct thread_data *td, struct fio_file *f) | |
ebac4655 | 593 | { |
2866c82d | 594 | if (td->io_ops->sync) |
53cdc686 | 595 | return td->io_ops->sync(td, f); |
ebac4655 JA |
596 | |
597 | return 0; | |
598 | } | |
599 | ||
45bee283 | 600 | static int td_io_getevents(struct thread_data *td, int min, int max, |
ebac4655 JA |
601 | struct timespec *t) |
602 | { | |
2866c82d | 603 | return td->io_ops->getevents(td, min, max, t); |
ebac4655 JA |
604 | } |
605 | ||
45bee283 | 606 | static int td_io_queue(struct thread_data *td, struct io_u *io_u) |
ebac4655 JA |
607 | { |
608 | gettimeofday(&io_u->issue_time, NULL); | |
609 | ||
2866c82d | 610 | return td->io_ops->queue(td, io_u); |
ebac4655 JA |
611 | } |
612 | ||
613 | #define iocb_time(iocb) ((unsigned long) (iocb)->data) | |
614 | ||
615 | static void io_completed(struct thread_data *td, struct io_u *io_u, | |
616 | struct io_completion_data *icd) | |
617 | { | |
618 | struct timeval e; | |
619 | unsigned long msec; | |
620 | ||
621 | gettimeofday(&e, NULL); | |
622 | ||
623 | if (!io_u->error) { | |
20dc95c4 | 624 | unsigned int bytes = io_u->buflen - io_u->resid; |
3d60d1ed | 625 | const int idx = io_u->ddir; |
ebac4655 JA |
626 | |
627 | td->io_blocks[idx]++; | |
20dc95c4 JA |
628 | td->io_bytes[idx] += bytes; |
629 | td->zone_bytes += bytes; | |
630 | td->this_io_bytes[idx] += bytes; | |
ebac4655 JA |
631 | |
632 | msec = mtime_since(&io_u->issue_time, &e); | |
633 | ||
3d60d1ed JA |
634 | add_clat_sample(td, idx, msec); |
635 | add_bw_sample(td, idx); | |
ebac4655 | 636 | |
3d60d1ed | 637 | if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE) |
ebac4655 JA |
638 | log_io_piece(td, io_u); |
639 | ||
20dc95c4 | 640 | icd->bytes_done[idx] += bytes; |
ebac4655 JA |
641 | } else |
642 | icd->error = io_u->error; | |
643 | } | |
644 | ||
645 | static void ios_completed(struct thread_data *td,struct io_completion_data *icd) | |
646 | { | |
647 | struct io_u *io_u; | |
648 | int i; | |
649 | ||
650 | icd->error = 0; | |
651 | icd->bytes_done[0] = icd->bytes_done[1] = 0; | |
652 | ||
653 | for (i = 0; i < icd->nr; i++) { | |
2866c82d | 654 | io_u = td->io_ops->event(td, i); |
ebac4655 JA |
655 | |
656 | io_completed(td, io_u, icd); | |
657 | put_io_u(td, io_u); | |
658 | } | |
659 | } | |
660 | ||
906c8d75 JA |
661 | /* |
662 | * When job exits, we can cancel the in-flight IO if we are using async | |
663 | * io. Attempt to do so. | |
664 | */ | |
ebac4655 JA |
665 | static void cleanup_pending_aio(struct thread_data *td) |
666 | { | |
667 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; | |
668 | struct list_head *entry, *n; | |
669 | struct io_completion_data icd; | |
670 | struct io_u *io_u; | |
671 | int r; | |
672 | ||
673 | /* | |
674 | * get immediately available events, if any | |
675 | */ | |
45bee283 | 676 | r = td_io_getevents(td, 0, td->cur_depth, &ts); |
ebac4655 JA |
677 | if (r > 0) { |
678 | icd.nr = r; | |
679 | ios_completed(td, &icd); | |
680 | } | |
681 | ||
682 | /* | |
683 | * now cancel remaining active events | |
684 | */ | |
2866c82d | 685 | if (td->io_ops->cancel) { |
ebac4655 JA |
686 | list_for_each_safe(entry, n, &td->io_u_busylist) { |
687 | io_u = list_entry(entry, struct io_u, list); | |
688 | ||
2866c82d | 689 | r = td->io_ops->cancel(td, io_u); |
ebac4655 JA |
690 | if (!r) |
691 | put_io_u(td, io_u); | |
692 | } | |
693 | } | |
694 | ||
695 | if (td->cur_depth) { | |
45bee283 | 696 | r = td_io_getevents(td, td->cur_depth, td->cur_depth, NULL); |
ebac4655 JA |
697 | if (r > 0) { |
698 | icd.nr = r; | |
699 | ios_completed(td, &icd); | |
700 | } | |
701 | } | |
702 | } | |
703 | ||
704 | static int do_io_u_verify(struct thread_data *td, struct io_u **io_u) | |
705 | { | |
706 | struct io_u *v_io_u = *io_u; | |
707 | int ret = 0; | |
708 | ||
709 | if (v_io_u) { | |
710 | ret = verify_io_u(v_io_u); | |
711 | put_io_u(td, v_io_u); | |
712 | *io_u = NULL; | |
713 | } | |
714 | ||
715 | return ret; | |
716 | } | |
717 | ||
906c8d75 JA |
718 | /* |
719 | * The main verify engine. Runs over the writes we previusly submitted, | |
720 | * reads the blocks back in, and checks the crc/md5 of the data. | |
721 | */ | |
ebac4655 JA |
722 | static void do_verify(struct thread_data *td) |
723 | { | |
724 | struct timeval t; | |
725 | struct io_u *io_u, *v_io_u = NULL; | |
726 | struct io_completion_data icd; | |
53cdc686 | 727 | struct fio_file *f; |
ebac4655 JA |
728 | int ret; |
729 | ||
730 | td_set_runstate(td, TD_VERIFYING); | |
731 | ||
732 | do { | |
733 | if (td->terminate) | |
734 | break; | |
735 | ||
736 | gettimeofday(&t, NULL); | |
737 | if (runtime_exceeded(td, &t)) | |
738 | break; | |
739 | ||
740 | io_u = __get_io_u(td); | |
741 | if (!io_u) | |
742 | break; | |
743 | ||
aea47d44 | 744 | if (get_next_verify(td, io_u)) { |
ebac4655 JA |
745 | put_io_u(td, io_u); |
746 | break; | |
747 | } | |
748 | ||
53cdc686 JA |
749 | f = get_next_file(td); |
750 | if (!f) | |
751 | break; | |
752 | ||
753 | io_u->file = f; | |
754 | ||
aea47d44 | 755 | if (td_io_prep(td, io_u)) { |
ebac4655 JA |
756 | put_io_u(td, io_u); |
757 | break; | |
758 | } | |
759 | ||
45bee283 | 760 | ret = td_io_queue(td, io_u); |
ebac4655 JA |
761 | if (ret) { |
762 | put_io_u(td, io_u); | |
763 | td_verror(td, ret); | |
764 | break; | |
765 | } | |
766 | ||
767 | /* | |
768 | * we have one pending to verify, do that while | |
769 | * we are doing io on the next one | |
770 | */ | |
771 | if (do_io_u_verify(td, &v_io_u)) | |
772 | break; | |
773 | ||
45bee283 | 774 | ret = td_io_getevents(td, 1, 1, NULL); |
ebac4655 JA |
775 | if (ret != 1) { |
776 | if (ret < 0) | |
777 | td_verror(td, ret); | |
778 | break; | |
779 | } | |
780 | ||
2866c82d | 781 | v_io_u = td->io_ops->event(td, 0); |
ebac4655 JA |
782 | icd.nr = 1; |
783 | icd.error = 0; | |
784 | io_completed(td, v_io_u, &icd); | |
785 | ||
786 | if (icd.error) { | |
787 | td_verror(td, icd.error); | |
788 | put_io_u(td, v_io_u); | |
789 | v_io_u = NULL; | |
790 | break; | |
791 | } | |
792 | ||
ebac4655 JA |
793 | /* |
794 | * if we can't submit more io, we need to verify now | |
795 | */ | |
796 | if (queue_full(td) && do_io_u_verify(td, &v_io_u)) | |
797 | break; | |
798 | ||
799 | } while (1); | |
800 | ||
801 | do_io_u_verify(td, &v_io_u); | |
802 | ||
803 | if (td->cur_depth) | |
804 | cleanup_pending_aio(td); | |
805 | ||
806 | td_set_runstate(td, TD_RUNNING); | |
807 | } | |
808 | ||
b990b5c0 JA |
809 | /* |
810 | * Not really an io thread, all it does is burn CPU cycles in the specified | |
811 | * manner. | |
812 | */ | |
813 | static void do_cpuio(struct thread_data *td) | |
814 | { | |
815 | struct timeval e; | |
816 | int split = 100 / td->cpuload; | |
817 | int i = 0; | |
818 | ||
819 | while (!td->terminate) { | |
820 | gettimeofday(&e, NULL); | |
821 | ||
822 | if (runtime_exceeded(td, &e)) | |
823 | break; | |
824 | ||
825 | if (!(i % split)) | |
826 | __usec_sleep(10000); | |
827 | else | |
828 | usec_sleep(td, 10000); | |
829 | ||
830 | i++; | |
831 | } | |
832 | } | |
833 | ||
32cd46a0 | 834 | /* |
906c8d75 | 835 | * Main IO worker function. It retrieves io_u's to process and queues |
32cd46a0 JA |
836 | * and reaps them, checking for rate and errors along the way. |
837 | */ | |
ebac4655 JA |
838 | static void do_io(struct thread_data *td) |
839 | { | |
840 | struct io_completion_data icd; | |
841 | struct timeval s, e; | |
842 | unsigned long usec; | |
53cdc686 JA |
843 | struct fio_file *f; |
844 | int i; | |
ebac4655 | 845 | |
5853e5a8 JA |
846 | td_set_runstate(td, TD_RUNNING); |
847 | ||
ebac4655 JA |
848 | while (td->this_io_bytes[td->ddir] < td->io_size) { |
849 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; | |
850 | struct timespec *timeout; | |
851 | int ret, min_evts = 0; | |
852 | struct io_u *io_u; | |
853 | ||
854 | if (td->terminate) | |
855 | break; | |
856 | ||
53cdc686 JA |
857 | f = get_next_file(td); |
858 | if (!f) | |
859 | break; | |
860 | ||
861 | io_u = get_io_u(td, f); | |
ebac4655 JA |
862 | if (!io_u) |
863 | break; | |
864 | ||
865 | memcpy(&s, &io_u->start_time, sizeof(s)); | |
866 | ||
45bee283 | 867 | ret = td_io_queue(td, io_u); |
ebac4655 JA |
868 | if (ret) { |
869 | put_io_u(td, io_u); | |
870 | td_verror(td, ret); | |
871 | break; | |
872 | } | |
873 | ||
874 | add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time)); | |
875 | ||
876 | if (td->cur_depth < td->iodepth) { | |
877 | timeout = &ts; | |
878 | min_evts = 0; | |
879 | } else { | |
880 | timeout = NULL; | |
881 | min_evts = 1; | |
882 | } | |
883 | ||
45bee283 | 884 | ret = td_io_getevents(td, min_evts, td->cur_depth, timeout); |
ebac4655 JA |
885 | if (ret < 0) { |
886 | td_verror(td, ret); | |
887 | break; | |
888 | } else if (!ret) | |
889 | continue; | |
890 | ||
891 | icd.nr = ret; | |
892 | ios_completed(td, &icd); | |
893 | if (icd.error) { | |
894 | td_verror(td, icd.error); | |
895 | break; | |
896 | } | |
897 | ||
898 | /* | |
899 | * the rate is batched for now, it should work for batches | |
900 | * of completions except the very first one which may look | |
901 | * a little bursty | |
902 | */ | |
903 | gettimeofday(&e, NULL); | |
904 | usec = utime_since(&s, &e); | |
905 | ||
906 | rate_throttle(td, usec, icd.bytes_done[td->ddir]); | |
907 | ||
908 | if (check_min_rate(td, &e)) { | |
909 | td_verror(td, ENOMEM); | |
910 | break; | |
911 | } | |
912 | ||
913 | if (runtime_exceeded(td, &e)) | |
914 | break; | |
915 | ||
916 | if (td->thinktime) | |
917 | usec_sleep(td, td->thinktime); | |
918 | ||
919 | if (should_fsync(td) && td->fsync_blocks && | |
920 | (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0) | |
53cdc686 | 921 | td_io_sync(td, f); |
ebac4655 JA |
922 | } |
923 | ||
924 | if (td->cur_depth) | |
925 | cleanup_pending_aio(td); | |
926 | ||
5853e5a8 JA |
927 | if (should_fsync(td) && td->end_fsync) { |
928 | td_set_runstate(td, TD_FSYNCING); | |
53cdc686 JA |
929 | for_each_file(td, f, i) |
930 | td_io_sync(td, f); | |
5853e5a8 | 931 | } |
ebac4655 JA |
932 | } |
933 | ||
45bee283 | 934 | static int td_io_init(struct thread_data *td) |
ebac4655 | 935 | { |
2866c82d JA |
936 | if (td->io_ops->init) |
937 | return td->io_ops->init(td); | |
938 | ||
939 | return 0; | |
ebac4655 JA |
940 | } |
941 | ||
942 | static void cleanup_io_u(struct thread_data *td) | |
943 | { | |
944 | struct list_head *entry, *n; | |
945 | struct io_u *io_u; | |
946 | ||
947 | list_for_each_safe(entry, n, &td->io_u_freelist) { | |
948 | io_u = list_entry(entry, struct io_u, list); | |
949 | ||
950 | list_del(&io_u->list); | |
951 | free(io_u); | |
952 | } | |
953 | ||
954 | if (td->mem_type == MEM_MALLOC) | |
955 | free(td->orig_buffer); | |
956 | else if (td->mem_type == MEM_SHM) { | |
957 | struct shmid_ds sbuf; | |
958 | ||
959 | shmdt(td->orig_buffer); | |
960 | shmctl(td->shm_id, IPC_RMID, &sbuf); | |
961 | } else if (td->mem_type == MEM_MMAP) | |
962 | munmap(td->orig_buffer, td->orig_buffer_size); | |
963 | else | |
3b70d7e5 | 964 | log_err("Bad memory type %d\n", td->mem_type); |
ebac4655 JA |
965 | |
966 | td->orig_buffer = NULL; | |
967 | } | |
968 | ||
969 | static int init_io_u(struct thread_data *td) | |
970 | { | |
971 | struct io_u *io_u; | |
972 | int i, max_units; | |
973 | char *p; | |
974 | ||
2866c82d | 975 | if (td->io_ops->flags & FIO_CPUIO) |
b990b5c0 JA |
976 | return 0; |
977 | ||
2866c82d | 978 | if (td->io_ops->flags & FIO_SYNCIO) |
ebac4655 JA |
979 | max_units = 1; |
980 | else | |
981 | max_units = td->iodepth; | |
982 | ||
983 | td->orig_buffer_size = td->max_bs * max_units + MASK; | |
984 | ||
985 | if (td->mem_type == MEM_MALLOC) | |
986 | td->orig_buffer = malloc(td->orig_buffer_size); | |
987 | else if (td->mem_type == MEM_SHM) { | |
988 | td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600); | |
989 | if (td->shm_id < 0) { | |
990 | td_verror(td, errno); | |
991 | perror("shmget"); | |
992 | return 1; | |
993 | } | |
994 | ||
995 | td->orig_buffer = shmat(td->shm_id, NULL, 0); | |
996 | if (td->orig_buffer == (void *) -1) { | |
997 | td_verror(td, errno); | |
998 | perror("shmat"); | |
999 | td->orig_buffer = NULL; | |
1000 | return 1; | |
1001 | } | |
1002 | } else if (td->mem_type == MEM_MMAP) { | |
1003 | td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0); | |
1004 | if (td->orig_buffer == MAP_FAILED) { | |
1005 | td_verror(td, errno); | |
1006 | perror("mmap"); | |
1007 | td->orig_buffer = NULL; | |
1008 | return 1; | |
1009 | } | |
1010 | } | |
1011 | ||
ebac4655 JA |
1012 | p = ALIGN(td->orig_buffer); |
1013 | for (i = 0; i < max_units; i++) { | |
1014 | io_u = malloc(sizeof(*io_u)); | |
1015 | memset(io_u, 0, sizeof(*io_u)); | |
1016 | INIT_LIST_HEAD(&io_u->list); | |
1017 | ||
1018 | io_u->buf = p + td->max_bs * i; | |
b1ff3403 | 1019 | io_u->index = i; |
ebac4655 JA |
1020 | list_add(&io_u->list, &td->io_u_freelist); |
1021 | } | |
1022 | ||
1023 | return 0; | |
1024 | } | |
1025 | ||
da86774e JA |
1026 | static int switch_ioscheduler(struct thread_data *td) |
1027 | { | |
1028 | char tmp[256], tmp2[128]; | |
1029 | FILE *f; | |
1030 | int ret; | |
1031 | ||
1032 | sprintf(tmp, "%s/queue/scheduler", td->sysfs_root); | |
1033 | ||
1034 | f = fopen(tmp, "r+"); | |
1035 | if (!f) { | |
1036 | td_verror(td, errno); | |
1037 | return 1; | |
1038 | } | |
1039 | ||
1040 | /* | |
1041 | * Set io scheduler. | |
1042 | */ | |
1043 | ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f); | |
1044 | if (ferror(f) || ret != 1) { | |
1045 | td_verror(td, errno); | |
1046 | fclose(f); | |
1047 | return 1; | |
1048 | } | |
1049 | ||
1050 | rewind(f); | |
1051 | ||
1052 | /* | |
1053 | * Read back and check that the selected scheduler is now the default. | |
1054 | */ | |
1055 | ret = fread(tmp, 1, sizeof(tmp), f); | |
1056 | if (ferror(f) || ret < 0) { | |
1057 | td_verror(td, errno); | |
1058 | fclose(f); | |
1059 | return 1; | |
1060 | } | |
1061 | ||
1062 | sprintf(tmp2, "[%s]", td->ioscheduler); | |
1063 | if (!strstr(tmp, tmp2)) { | |
3b70d7e5 | 1064 | log_err("fio: io scheduler %s not found\n", td->ioscheduler); |
da86774e JA |
1065 | td_verror(td, EINVAL); |
1066 | fclose(f); | |
1067 | return 1; | |
1068 | } | |
1069 | ||
1070 | fclose(f); | |
1071 | return 0; | |
1072 | } | |
1073 | ||
ebac4655 JA |
1074 | static void clear_io_state(struct thread_data *td) |
1075 | { | |
53cdc686 JA |
1076 | struct fio_file *f; |
1077 | int i; | |
ebac4655 | 1078 | |
ebac4655 JA |
1079 | td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0; |
1080 | td->this_io_bytes[0] = td->this_io_bytes[1] = 0; | |
20dc95c4 | 1081 | td->zone_bytes = 0; |
ebac4655 | 1082 | |
53cdc686 JA |
1083 | for_each_file(td, f, i) { |
1084 | f->last_pos = 0; | |
1085 | if (td->io_ops->flags & FIO_SYNCIO) | |
1086 | lseek(f->fd, SEEK_SET, 0); | |
1087 | ||
1088 | if (f->file_map) | |
1089 | memset(f->file_map, 0, f->num_maps * sizeof(long)); | |
1090 | } | |
ebac4655 JA |
1091 | } |
1092 | ||
906c8d75 JA |
1093 | /* |
1094 | * Entry point for the thread based jobs. The process based jobs end up | |
1095 | * here as well, after a little setup. | |
1096 | */ | |
ebac4655 JA |
1097 | static void *thread_main(void *data) |
1098 | { | |
1099 | struct thread_data *td = data; | |
ebac4655 JA |
1100 | |
1101 | if (!td->use_thread) | |
1102 | setsid(); | |
1103 | ||
1104 | td->pid = getpid(); | |
1105 | ||
aea47d44 JA |
1106 | INIT_LIST_HEAD(&td->io_u_freelist); |
1107 | INIT_LIST_HEAD(&td->io_u_busylist); | |
1108 | INIT_LIST_HEAD(&td->io_hist_list); | |
1109 | INIT_LIST_HEAD(&td->io_log_list); | |
1110 | ||
ebac4655 JA |
1111 | if (init_io_u(td)) |
1112 | goto err; | |
1113 | ||
1114 | if (fio_setaffinity(td) == -1) { | |
1115 | td_verror(td, errno); | |
1116 | goto err; | |
1117 | } | |
1118 | ||
45bee283 | 1119 | if (td_io_init(td)) |
ebac4655 JA |
1120 | goto err; |
1121 | ||
aea47d44 JA |
1122 | if (init_iolog(td)) |
1123 | goto err; | |
1124 | ||
ebac4655 JA |
1125 | if (td->ioprio) { |
1126 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) { | |
1127 | td_verror(td, errno); | |
1128 | goto err; | |
1129 | } | |
1130 | } | |
1131 | ||
1056eaad | 1132 | if (nice(td->nice) == -1) { |
b6f4d880 JA |
1133 | td_verror(td, errno); |
1134 | goto err; | |
1135 | } | |
1136 | ||
75154845 JA |
1137 | if (init_random_state(td)) |
1138 | goto err; | |
1139 | ||
da86774e JA |
1140 | if (td->ioscheduler && switch_ioscheduler(td)) |
1141 | goto err; | |
1142 | ||
75154845 | 1143 | td_set_runstate(td, TD_INITIALIZED); |
bbfd6b00 JA |
1144 | fio_sem_up(&startup_sem); |
1145 | fio_sem_down(&td->mutex); | |
ebac4655 | 1146 | |
53cdc686 | 1147 | if (!td->create_serialize && setup_files(td)) |
ebac4655 JA |
1148 | goto err; |
1149 | ||
ebac4655 JA |
1150 | gettimeofday(&td->epoch, NULL); |
1151 | ||
4e0ba8af JA |
1152 | if (td->exec_prerun) |
1153 | system(td->exec_prerun); | |
1154 | ||
ebac4655 JA |
1155 | while (td->loops--) { |
1156 | getrusage(RUSAGE_SELF, &td->ru_start); | |
1157 | gettimeofday(&td->start, NULL); | |
1158 | memcpy(&td->stat_sample_time, &td->start, sizeof(td->start)); | |
1159 | ||
1160 | if (td->ratemin) | |
1161 | memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate)); | |
1162 | ||
1163 | clear_io_state(td); | |
1164 | prune_io_piece_log(td); | |
1165 | ||
2866c82d | 1166 | if (td->io_ops->flags & FIO_CPUIO) |
b990b5c0 JA |
1167 | do_cpuio(td); |
1168 | else | |
1169 | do_io(td); | |
ebac4655 JA |
1170 | |
1171 | td->runtime[td->ddir] += mtime_since_now(&td->start); | |
aea47d44 | 1172 | if (td_rw(td) && td->io_bytes[td->ddir ^ 1]) |
3d60d1ed JA |
1173 | td->runtime[td->ddir ^ 1] = td->runtime[td->ddir]; |
1174 | ||
ebac4655 JA |
1175 | update_rusage_stat(td); |
1176 | ||
1177 | if (td->error || td->terminate) | |
1178 | break; | |
1179 | ||
1180 | if (td->verify == VERIFY_NONE) | |
1181 | continue; | |
1182 | ||
1183 | clear_io_state(td); | |
1184 | gettimeofday(&td->start, NULL); | |
1185 | ||
1186 | do_verify(td); | |
1187 | ||
1188 | td->runtime[DDIR_READ] += mtime_since_now(&td->start); | |
1189 | ||
1190 | if (td->error || td->terminate) | |
1191 | break; | |
1192 | } | |
1193 | ||
ebac4655 JA |
1194 | if (td->bw_log) |
1195 | finish_log(td, td->bw_log, "bw"); | |
1196 | if (td->slat_log) | |
1197 | finish_log(td, td->slat_log, "slat"); | |
1198 | if (td->clat_log) | |
1199 | finish_log(td, td->clat_log, "clat"); | |
843a7413 JA |
1200 | if (td->write_iolog) |
1201 | write_iolog_close(td); | |
4e0ba8af JA |
1202 | if (td->exec_postrun) |
1203 | system(td->exec_postrun); | |
ebac4655 JA |
1204 | |
1205 | if (exitall_on_terminate) | |
1206 | terminate_threads(td->groupid); | |
1207 | ||
1208 | err: | |
53cdc686 | 1209 | close_files(td); |
2866c82d | 1210 | close_ioengine(td); |
ebac4655 | 1211 | cleanup_io_u(td); |
ebac4655 JA |
1212 | td_set_runstate(td, TD_EXITED); |
1213 | return NULL; | |
1214 | ||
1215 | } | |
1216 | ||
906c8d75 JA |
1217 | /* |
1218 | * We cannot pass the td data into a forked process, so attach the td and | |
1219 | * pass it to the thread worker. | |
1220 | */ | |
ebac4655 JA |
1221 | static void *fork_main(int shmid, int offset) |
1222 | { | |
1223 | struct thread_data *td; | |
1224 | void *data; | |
1225 | ||
1226 | data = shmat(shmid, NULL, 0); | |
1227 | if (data == (void *) -1) { | |
1228 | perror("shmat"); | |
1229 | return NULL; | |
1230 | } | |
1231 | ||
1232 | td = data + offset * sizeof(struct thread_data); | |
1233 | thread_main(td); | |
1234 | shmdt(data); | |
1235 | return NULL; | |
1236 | } | |
1237 | ||
906c8d75 JA |
1238 | /* |
1239 | * Run over the job map and reap the threads that have exited, if any. | |
1240 | */ | |
ebac4655 JA |
1241 | static void reap_threads(int *nr_running, int *t_rate, int *m_rate) |
1242 | { | |
b990b5c0 | 1243 | int i, cputhreads; |
ebac4655 JA |
1244 | |
1245 | /* | |
1246 | * reap exited threads (TD_EXITED -> TD_REAPED) | |
1247 | */ | |
b990b5c0 | 1248 | for (i = 0, cputhreads = 0; i < thread_number; i++) { |
ebac4655 JA |
1249 | struct thread_data *td = &threads[i]; |
1250 | ||
2866c82d | 1251 | if (td->io_ops->flags & FIO_CPUIO) |
b990b5c0 JA |
1252 | cputhreads++; |
1253 | ||
ebac4655 JA |
1254 | if (td->runstate != TD_EXITED) |
1255 | continue; | |
1256 | ||
1257 | td_set_runstate(td, TD_REAPED); | |
1258 | ||
1259 | if (td->use_thread) { | |
1260 | long ret; | |
1261 | ||
1262 | if (pthread_join(td->thread, (void *) &ret)) | |
1263 | perror("thread_join"); | |
1264 | } else | |
1265 | waitpid(td->pid, NULL, 0); | |
1266 | ||
1267 | (*nr_running)--; | |
1268 | (*m_rate) -= td->ratemin; | |
1269 | (*t_rate) -= td->rate; | |
1270 | } | |
b990b5c0 JA |
1271 | |
1272 | if (*nr_running == cputhreads) | |
1273 | terminate_threads(TERMINATE_ALL); | |
ebac4655 JA |
1274 | } |
1275 | ||
fcb6ade2 JA |
1276 | static void fio_unpin_memory(void *pinned) |
1277 | { | |
1278 | if (pinned) { | |
1279 | if (munlock(pinned, mlock_size) < 0) | |
1280 | perror("munlock"); | |
1281 | munmap(pinned, mlock_size); | |
1282 | } | |
1283 | } | |
1284 | ||
1285 | static void *fio_pin_memory(void) | |
1286 | { | |
32cd46a0 | 1287 | unsigned long long phys_mem; |
fcb6ade2 JA |
1288 | void *ptr; |
1289 | ||
1290 | if (!mlock_size) | |
1291 | return NULL; | |
1292 | ||
1293 | /* | |
1294 | * Don't allow mlock of more than real_mem-128MB | |
1295 | */ | |
32cd46a0 JA |
1296 | phys_mem = os_phys_mem(); |
1297 | if (phys_mem) { | |
1298 | if ((mlock_size + 128 * 1024 * 1024) > phys_mem) { | |
1299 | mlock_size = phys_mem - 128 * 1024 * 1024; | |
eb8bbf48 | 1300 | fprintf(f_out, "fio: limiting mlocked memory to %lluMiB\n", mlock_size >> 20); |
fcb6ade2 JA |
1301 | } |
1302 | } | |
1303 | ||
1304 | ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0); | |
1305 | if (!ptr) { | |
1306 | perror("malloc locked mem"); | |
1307 | return NULL; | |
1308 | } | |
1309 | if (mlock(ptr, mlock_size) < 0) { | |
1310 | munmap(ptr, mlock_size); | |
1311 | perror("mlock"); | |
1312 | return NULL; | |
1313 | } | |
1314 | ||
1315 | return ptr; | |
1316 | } | |
1317 | ||
906c8d75 JA |
1318 | /* |
1319 | * Main function for kicking off and reaping jobs, as needed. | |
1320 | */ | |
ebac4655 JA |
1321 | static void run_threads(void) |
1322 | { | |
ebac4655 JA |
1323 | struct thread_data *td; |
1324 | unsigned long spent; | |
1325 | int i, todo, nr_running, m_rate, t_rate, nr_started; | |
fcb6ade2 JA |
1326 | void *mlocked_mem; |
1327 | ||
1328 | mlocked_mem = fio_pin_memory(); | |
ebac4655 | 1329 | |
c6ae0a5b JA |
1330 | if (!terse_output) { |
1331 | printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : ""); | |
1332 | fflush(stdout); | |
1333 | } | |
c04f7ec3 | 1334 | |
4efa970e JA |
1335 | signal(SIGINT, sig_handler); |
1336 | signal(SIGALRM, sig_handler); | |
1337 | ||
ebac4655 JA |
1338 | todo = thread_number; |
1339 | nr_running = 0; | |
1340 | nr_started = 0; | |
1341 | m_rate = t_rate = 0; | |
1342 | ||
1343 | for (i = 0; i < thread_number; i++) { | |
1344 | td = &threads[i]; | |
1345 | ||
263e529f | 1346 | print_status_init(td->thread_number - 1); |
ebac4655 JA |
1347 | |
1348 | init_disk_util(td); | |
1349 | ||
1350 | if (!td->create_serialize) | |
1351 | continue; | |
1352 | ||
1353 | /* | |
1354 | * do file setup here so it happens sequentially, | |
1355 | * we don't want X number of threads getting their | |
1356 | * client data interspersed on disk | |
1357 | */ | |
53cdc686 | 1358 | if (setup_files(td)) { |
ebac4655 JA |
1359 | td_set_runstate(td, TD_REAPED); |
1360 | todo--; | |
1361 | } | |
1362 | } | |
1363 | ||
263e529f | 1364 | time_init(); |
ebac4655 JA |
1365 | |
1366 | while (todo) { | |
75154845 JA |
1367 | struct thread_data *map[MAX_JOBS]; |
1368 | struct timeval this_start; | |
1369 | int this_jobs = 0, left; | |
1370 | ||
ebac4655 JA |
1371 | /* |
1372 | * create threads (TD_NOT_CREATED -> TD_CREATED) | |
1373 | */ | |
1374 | for (i = 0; i < thread_number; i++) { | |
1375 | td = &threads[i]; | |
1376 | ||
1377 | if (td->runstate != TD_NOT_CREATED) | |
1378 | continue; | |
1379 | ||
1380 | /* | |
1381 | * never got a chance to start, killed by other | |
1382 | * thread for some reason | |
1383 | */ | |
1384 | if (td->terminate) { | |
1385 | todo--; | |
1386 | continue; | |
1387 | } | |
1388 | ||
1389 | if (td->start_delay) { | |
263e529f | 1390 | spent = mtime_since_genesis(); |
ebac4655 JA |
1391 | |
1392 | if (td->start_delay * 1000 > spent) | |
1393 | continue; | |
1394 | } | |
1395 | ||
1396 | if (td->stonewall && (nr_started || nr_running)) | |
1397 | break; | |
1398 | ||
75154845 JA |
1399 | /* |
1400 | * Set state to created. Thread will transition | |
1401 | * to TD_INITIALIZED when it's done setting up. | |
1402 | */ | |
ebac4655 | 1403 | td_set_runstate(td, TD_CREATED); |
75154845 | 1404 | map[this_jobs++] = td; |
bbfd6b00 | 1405 | fio_sem_init(&startup_sem, 1); |
ebac4655 JA |
1406 | nr_started++; |
1407 | ||
1408 | if (td->use_thread) { | |
1409 | if (pthread_create(&td->thread, NULL, thread_main, td)) { | |
1410 | perror("thread_create"); | |
1411 | nr_started--; | |
1412 | } | |
1413 | } else { | |
1414 | if (fork()) | |
bbfd6b00 | 1415 | fio_sem_down(&startup_sem); |
ebac4655 JA |
1416 | else { |
1417 | fork_main(shm_id, i); | |
1418 | exit(0); | |
1419 | } | |
1420 | } | |
1421 | } | |
1422 | ||
1423 | /* | |
75154845 JA |
1424 | * Wait for the started threads to transition to |
1425 | * TD_INITIALIZED. | |
ebac4655 | 1426 | */ |
75154845 JA |
1427 | gettimeofday(&this_start, NULL); |
1428 | left = this_jobs; | |
1429 | while (left) { | |
1430 | if (mtime_since_now(&this_start) > JOB_START_TIMEOUT) | |
1431 | break; | |
1432 | ||
1433 | usleep(100000); | |
1434 | ||
1435 | for (i = 0; i < this_jobs; i++) { | |
1436 | td = map[i]; | |
1437 | if (!td) | |
1438 | continue; | |
b6f4d880 | 1439 | if (td->runstate == TD_INITIALIZED) { |
75154845 JA |
1440 | map[i] = NULL; |
1441 | left--; | |
b6f4d880 JA |
1442 | } else if (td->runstate >= TD_EXITED) { |
1443 | map[i] = NULL; | |
1444 | left--; | |
1445 | todo--; | |
1446 | nr_running++; /* work-around... */ | |
75154845 JA |
1447 | } |
1448 | } | |
1449 | } | |
1450 | ||
1451 | if (left) { | |
3b70d7e5 | 1452 | log_err("fio: %d jobs failed to start\n", left); |
75154845 JA |
1453 | for (i = 0; i < this_jobs; i++) { |
1454 | td = map[i]; | |
1455 | if (!td) | |
1456 | continue; | |
1457 | kill(td->pid, SIGTERM); | |
1458 | } | |
1459 | break; | |
1460 | } | |
1461 | ||
1462 | /* | |
b6f4d880 | 1463 | * start created threads (TD_INITIALIZED -> TD_RUNNING). |
75154845 | 1464 | */ |
ebac4655 JA |
1465 | for (i = 0; i < thread_number; i++) { |
1466 | td = &threads[i]; | |
1467 | ||
75154845 | 1468 | if (td->runstate != TD_INITIALIZED) |
ebac4655 JA |
1469 | continue; |
1470 | ||
1471 | td_set_runstate(td, TD_RUNNING); | |
1472 | nr_running++; | |
1473 | nr_started--; | |
1474 | m_rate += td->ratemin; | |
1475 | t_rate += td->rate; | |
75154845 | 1476 | todo--; |
bbfd6b00 | 1477 | fio_sem_up(&td->mutex); |
ebac4655 JA |
1478 | } |
1479 | ||
1480 | reap_threads(&nr_running, &t_rate, &m_rate); | |
1481 | ||
1482 | if (todo) | |
1483 | usleep(100000); | |
1484 | } | |
1485 | ||
1486 | while (nr_running) { | |
1487 | reap_threads(&nr_running, &t_rate, &m_rate); | |
1488 | usleep(10000); | |
1489 | } | |
1490 | ||
1491 | update_io_ticks(); | |
fcb6ade2 | 1492 | fio_unpin_memory(mlocked_mem); |
ebac4655 JA |
1493 | } |
1494 | ||
ebac4655 JA |
1495 | int main(int argc, char *argv[]) |
1496 | { | |
1497 | if (parse_options(argc, argv)) | |
1498 | return 1; | |
1499 | ||
1500 | if (!thread_number) { | |
3b70d7e5 | 1501 | log_err("Nothing to do\n"); |
ebac4655 JA |
1502 | return 1; |
1503 | } | |
1504 | ||
1505 | disk_util_timer_arm(); | |
1506 | ||
1507 | run_threads(); | |
1508 | show_run_stats(); | |
1509 | ||
1510 | return 0; | |
1511 | } |