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; | |
44 | static char run_str[MAX_JOBS + 1]; | |
45 | int shm_id = 0; | |
5289b847 | 46 | static struct timeval genesis; |
88c6ed80 | 47 | static int temp_stall_ts; |
ebac4655 | 48 | |
ebac4655 JA |
49 | static void print_thread_status(void); |
50 | ||
c04f7ec3 JA |
51 | extern unsigned long long mlock_size; |
52 | ||
ebac4655 | 53 | /* |
79809113 JA |
54 | * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it |
55 | * will never back again. It may cycle between running/verififying/fsyncing. | |
56 | * Once the thread reaches TD_EXITED, it is just waiting for the core to | |
57 | * reap it. | |
ebac4655 JA |
58 | */ |
59 | enum { | |
60 | TD_NOT_CREATED = 0, | |
61 | TD_CREATED, | |
75154845 | 62 | TD_INITIALIZED, |
ebac4655 JA |
63 | TD_RUNNING, |
64 | TD_VERIFYING, | |
5853e5a8 | 65 | TD_FSYNCING, |
ebac4655 JA |
66 | TD_EXITED, |
67 | TD_REAPED, | |
68 | }; | |
69 | ||
3d60d1ed | 70 | #define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync)) |
ebac4655 | 71 | |
bbfd6b00 | 72 | static volatile int startup_sem; |
ebac4655 JA |
73 | |
74 | #define TERMINATE_ALL (-1) | |
75154845 | 75 | #define JOB_START_TIMEOUT (5 * 1000) |
ebac4655 JA |
76 | |
77 | static void terminate_threads(int group_id) | |
78 | { | |
79 | int i; | |
80 | ||
81 | for (i = 0; i < thread_number; i++) { | |
82 | struct thread_data *td = &threads[i]; | |
83 | ||
84 | if (group_id == TERMINATE_ALL || groupid == td->groupid) { | |
85 | td->terminate = 1; | |
86 | td->start_delay = 0; | |
87 | } | |
88 | } | |
89 | } | |
90 | ||
91 | static void sig_handler(int sig) | |
92 | { | |
93 | switch (sig) { | |
94 | case SIGALRM: | |
95 | update_io_ticks(); | |
96 | disk_util_timer_arm(); | |
97 | print_thread_status(); | |
98 | break; | |
99 | default: | |
100 | printf("\nfio: terminating on signal\n"); | |
101 | fflush(stdout); | |
102 | terminate_threads(TERMINATE_ALL); | |
103 | break; | |
104 | } | |
105 | } | |
106 | ||
906c8d75 JA |
107 | /* |
108 | * The ->file_map[] contains a map of blocks we have or have not done io | |
109 | * to yet. Used to make sure we cover the entire range in a fair fashion. | |
110 | */ | |
ebac4655 JA |
111 | static int random_map_free(struct thread_data *td, unsigned long long block) |
112 | { | |
113 | unsigned int idx = RAND_MAP_IDX(td, block); | |
114 | unsigned int bit = RAND_MAP_BIT(td, block); | |
115 | ||
116 | return (td->file_map[idx] & (1UL << bit)) == 0; | |
117 | } | |
118 | ||
906c8d75 JA |
119 | /* |
120 | * Return the next free block in the map. | |
121 | */ | |
ebac4655 JA |
122 | static int get_next_free_block(struct thread_data *td, unsigned long long *b) |
123 | { | |
124 | int i; | |
125 | ||
126 | *b = 0; | |
127 | i = 0; | |
128 | while ((*b) * td->min_bs < td->io_size) { | |
129 | if (td->file_map[i] != -1UL) { | |
130 | *b += ffz(td->file_map[i]); | |
131 | return 0; | |
132 | } | |
133 | ||
134 | *b += BLOCKS_PER_MAP; | |
135 | i++; | |
136 | } | |
137 | ||
138 | return 1; | |
139 | } | |
140 | ||
906c8d75 JA |
141 | /* |
142 | * Mark a given offset as used in the map. | |
143 | */ | |
ebac4655 JA |
144 | static void mark_random_map(struct thread_data *td, struct io_u *io_u) |
145 | { | |
200bc855 | 146 | unsigned long long block = io_u->offset / (unsigned long long) td->min_bs; |
ebac4655 JA |
147 | unsigned int blocks = 0; |
148 | ||
149 | while (blocks < (io_u->buflen / td->min_bs)) { | |
150 | unsigned int idx, bit; | |
151 | ||
152 | if (!random_map_free(td, block)) | |
153 | break; | |
154 | ||
155 | idx = RAND_MAP_IDX(td, block); | |
156 | bit = RAND_MAP_BIT(td, block); | |
157 | ||
158 | assert(idx < td->num_maps); | |
159 | ||
160 | td->file_map[idx] |= (1UL << bit); | |
161 | block++; | |
162 | blocks++; | |
163 | } | |
164 | ||
165 | if ((blocks * td->min_bs) < io_u->buflen) | |
166 | io_u->buflen = blocks * td->min_bs; | |
167 | } | |
168 | ||
906c8d75 JA |
169 | /* |
170 | * For random io, generate a random new block and see if it's used. Repeat | |
171 | * until we find a free one. For sequential io, just return the end of | |
172 | * the last io issued. | |
173 | */ | |
20dc95c4 JA |
174 | static int get_next_offset(struct thread_data *td, unsigned long long *offset) |
175 | { | |
176 | unsigned long long b, rb; | |
177 | long r; | |
178 | ||
179 | if (!td->sequential) { | |
085227ad | 180 | unsigned long long max_blocks = td->io_size / td->min_bs; |
20dc95c4 JA |
181 | int loops = 50; |
182 | ||
183 | do { | |
6dfd46b9 | 184 | r = os_random_long(&td->random_state); |
085227ad | 185 | b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0)); |
20dc95c4 JA |
186 | rb = b + (td->file_offset / td->min_bs); |
187 | loops--; | |
188 | } while (!random_map_free(td, rb) && loops); | |
189 | ||
190 | if (!loops) { | |
191 | if (get_next_free_block(td, &b)) | |
192 | return 1; | |
193 | } | |
194 | } else | |
195 | b = td->last_pos / td->min_bs; | |
196 | ||
197 | *offset = (b * td->min_bs) + td->file_offset; | |
838a3cd3 | 198 | if (*offset > td->real_file_size) |
20dc95c4 JA |
199 | return 1; |
200 | ||
201 | return 0; | |
202 | } | |
203 | ||
204 | static unsigned int get_next_buflen(struct thread_data *td) | |
205 | { | |
206 | unsigned int buflen; | |
207 | long r; | |
208 | ||
209 | if (td->min_bs == td->max_bs) | |
210 | buflen = td->min_bs; | |
211 | else { | |
6dfd46b9 | 212 | r = os_random_long(&td->bsrange_state); |
20dc95c4 JA |
213 | buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0)); |
214 | buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1); | |
215 | } | |
216 | ||
217 | if (buflen > td->io_size - td->this_io_bytes[td->ddir]) | |
218 | buflen = td->io_size - td->this_io_bytes[td->ddir]; | |
219 | ||
220 | return buflen; | |
221 | } | |
222 | ||
906c8d75 JA |
223 | /* |
224 | * Check if we are above the minimum rate given. | |
225 | */ | |
ebac4655 JA |
226 | static int check_min_rate(struct thread_data *td, struct timeval *now) |
227 | { | |
228 | unsigned long spent; | |
229 | unsigned long rate; | |
230 | int ddir = td->ddir; | |
231 | ||
232 | /* | |
233 | * allow a 2 second settle period in the beginning | |
234 | */ | |
235 | if (mtime_since(&td->start, now) < 2000) | |
236 | return 0; | |
237 | ||
238 | /* | |
239 | * if rate blocks is set, sample is running | |
240 | */ | |
241 | if (td->rate_bytes) { | |
242 | spent = mtime_since(&td->lastrate, now); | |
243 | if (spent < td->ratecycle) | |
244 | return 0; | |
245 | ||
246 | rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent; | |
247 | if (rate < td->ratemin) { | |
eb8bbf48 | 248 | fprintf(f_out, "%s: min rate %d not met, got %ldKiB/sec\n", td->name, td->ratemin, rate); |
ebac4655 JA |
249 | if (rate_quit) |
250 | terminate_threads(td->groupid); | |
251 | return 1; | |
252 | } | |
253 | } | |
254 | ||
255 | td->rate_bytes = td->this_io_bytes[ddir]; | |
256 | memcpy(&td->lastrate, now, sizeof(*now)); | |
257 | return 0; | |
258 | } | |
259 | ||
260 | static inline int runtime_exceeded(struct thread_data *td, struct timeval *t) | |
261 | { | |
262 | if (!td->timeout) | |
263 | return 0; | |
264 | if (mtime_since(&td->epoch, t) >= td->timeout * 1000) | |
265 | return 1; | |
266 | ||
267 | return 0; | |
268 | } | |
269 | ||
270 | static void fill_random_bytes(struct thread_data *td, | |
271 | unsigned char *p, unsigned int len) | |
272 | { | |
273 | unsigned int todo; | |
274 | double r; | |
275 | ||
276 | while (len) { | |
6dfd46b9 | 277 | r = os_random_double(&td->verify_state); |
ebac4655 JA |
278 | |
279 | /* | |
280 | * lrand48_r seems to be broken and only fill the bottom | |
281 | * 32-bits, even on 64-bit archs with 64-bit longs | |
282 | */ | |
283 | todo = sizeof(r); | |
284 | if (todo > len) | |
285 | todo = len; | |
286 | ||
287 | memcpy(p, &r, todo); | |
288 | ||
289 | len -= todo; | |
290 | p += todo; | |
291 | } | |
292 | } | |
293 | ||
294 | static void hexdump(void *buffer, int len) | |
295 | { | |
296 | unsigned char *p = buffer; | |
297 | int i; | |
298 | ||
299 | for (i = 0; i < len; i++) | |
eb8bbf48 JA |
300 | fprintf(f_out, "%02x", p[i]); |
301 | fprintf(f_out, "\n"); | |
ebac4655 JA |
302 | } |
303 | ||
304 | static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u) | |
305 | { | |
306 | unsigned char *p = (unsigned char *) io_u->buf; | |
307 | unsigned long c; | |
ebac4655 JA |
308 | |
309 | p += sizeof(*hdr); | |
310 | c = crc32(p, hdr->len - sizeof(*hdr)); | |
ebac4655 | 311 | |
22f78b32 | 312 | if (c != hdr->crc32) { |
3b70d7e5 JA |
313 | log_err("crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen); |
314 | log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c); | |
22f78b32 | 315 | return 1; |
ebac4655 JA |
316 | } |
317 | ||
22f78b32 | 318 | return 0; |
ebac4655 JA |
319 | } |
320 | ||
321 | static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u) | |
322 | { | |
323 | unsigned char *p = (unsigned char *) io_u->buf; | |
324 | struct md5_ctx md5_ctx; | |
ebac4655 JA |
325 | |
326 | memset(&md5_ctx, 0, sizeof(md5_ctx)); | |
327 | p += sizeof(*hdr); | |
328 | md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr)); | |
329 | ||
22f78b32 | 330 | if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) { |
3b70d7e5 | 331 | log_err("md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen); |
ebac4655 JA |
332 | hexdump(hdr->md5_digest, sizeof(hdr->md5_digest)); |
333 | hexdump(md5_ctx.hash, sizeof(md5_ctx.hash)); | |
22f78b32 | 334 | return 1; |
ebac4655 JA |
335 | } |
336 | ||
22f78b32 | 337 | return 0; |
ebac4655 JA |
338 | } |
339 | ||
340 | static int verify_io_u(struct io_u *io_u) | |
341 | { | |
342 | struct verify_header *hdr = (struct verify_header *) io_u->buf; | |
343 | int ret; | |
344 | ||
345 | if (hdr->fio_magic != FIO_HDR_MAGIC) | |
346 | return 1; | |
347 | ||
348 | if (hdr->verify_type == VERIFY_MD5) | |
349 | ret = verify_io_u_md5(hdr, io_u); | |
350 | else if (hdr->verify_type == VERIFY_CRC32) | |
351 | ret = verify_io_u_crc32(hdr, io_u); | |
352 | else { | |
3b70d7e5 | 353 | log_err("Bad verify type %d\n", hdr->verify_type); |
ebac4655 JA |
354 | ret = 1; |
355 | } | |
356 | ||
357 | return ret; | |
358 | } | |
359 | ||
360 | static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len) | |
361 | { | |
362 | hdr->crc32 = crc32(p, len); | |
363 | } | |
364 | ||
365 | static void fill_md5(struct verify_header *hdr, void *p, unsigned int len) | |
366 | { | |
367 | struct md5_ctx md5_ctx; | |
368 | ||
369 | memset(&md5_ctx, 0, sizeof(md5_ctx)); | |
370 | md5_update(&md5_ctx, p, len); | |
371 | memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash)); | |
372 | } | |
373 | ||
906c8d75 JA |
374 | /* |
375 | * Return the data direction for the next io_u. If the job is a | |
376 | * mixed read/write workload, check the rwmix cycle and switch if | |
377 | * necessary. | |
378 | */ | |
a6ccc7be | 379 | static int get_rw_ddir(struct thread_data *td) |
3d60d1ed | 380 | { |
a6ccc7be JA |
381 | if (td_rw(td)) { |
382 | struct timeval now; | |
383 | unsigned long elapsed; | |
3d60d1ed | 384 | |
a6ccc7be JA |
385 | gettimeofday(&now, NULL); |
386 | elapsed = mtime_since_now(&td->rwmix_switch); | |
3d60d1ed | 387 | |
a6ccc7be JA |
388 | /* |
389 | * Check if it's time to seed a new data direction. | |
390 | */ | |
391 | if (elapsed >= td->rwmixcycle) { | |
c1ee2ca4 | 392 | int v; |
a6ccc7be | 393 | long r; |
3d60d1ed | 394 | |
c1ee2ca4 JA |
395 | r = os_random_long(&td->rwmix_state); |
396 | v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0))); | |
a6ccc7be JA |
397 | if (v < td->rwmixread) |
398 | td->rwmix_ddir = DDIR_READ; | |
399 | else | |
400 | td->rwmix_ddir = DDIR_WRITE; | |
401 | memcpy(&td->rwmix_switch, &now, sizeof(now)); | |
402 | } | |
403 | return td->rwmix_ddir; | |
404 | } else if (td_read(td)) | |
3d60d1ed JA |
405 | return DDIR_READ; |
406 | else | |
407 | return DDIR_WRITE; | |
408 | } | |
409 | ||
ebac4655 JA |
410 | /* |
411 | * fill body of io_u->buf with random data and add a header with the | |
22f78b32 | 412 | * crc32 or md5 sum of that data. |
ebac4655 JA |
413 | */ |
414 | static void populate_io_u(struct thread_data *td, struct io_u *io_u) | |
415 | { | |
416 | unsigned char *p = (unsigned char *) io_u->buf; | |
417 | struct verify_header hdr; | |
418 | ||
419 | hdr.fio_magic = FIO_HDR_MAGIC; | |
420 | hdr.len = io_u->buflen; | |
421 | p += sizeof(hdr); | |
422 | fill_random_bytes(td, p, io_u->buflen - sizeof(hdr)); | |
423 | ||
424 | if (td->verify == VERIFY_MD5) { | |
425 | fill_md5(&hdr, p, io_u->buflen - sizeof(hdr)); | |
426 | hdr.verify_type = VERIFY_MD5; | |
427 | } else { | |
428 | fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr)); | |
429 | hdr.verify_type = VERIFY_CRC32; | |
430 | } | |
431 | ||
432 | memcpy(io_u->buf, &hdr, sizeof(hdr)); | |
433 | } | |
434 | ||
aea47d44 | 435 | static int td_io_prep(struct thread_data *td, struct io_u *io_u) |
20dc95c4 | 436 | { |
2866c82d | 437 | if (td->io_ops->prep && td->io_ops->prep(td, io_u)) |
20dc95c4 JA |
438 | return 1; |
439 | ||
440 | return 0; | |
441 | } | |
442 | ||
b1ff3403 | 443 | void put_io_u(struct thread_data *td, struct io_u *io_u) |
ebac4655 JA |
444 | { |
445 | list_del(&io_u->list); | |
446 | list_add(&io_u->list, &td->io_u_freelist); | |
447 | td->cur_depth--; | |
448 | } | |
449 | ||
843a7413 JA |
450 | static int fill_io_u(struct thread_data *td, struct io_u *io_u) |
451 | { | |
452 | /* | |
453 | * If using an iolog, grab next piece if any available. | |
454 | */ | |
455 | if (td->read_iolog) | |
456 | return read_iolog_get(td, io_u); | |
457 | ||
aea47d44 JA |
458 | /* |
459 | * No log, let the seq/rand engine retrieve the next position. | |
460 | */ | |
461 | if (!get_next_offset(td, &io_u->offset)) { | |
462 | io_u->buflen = get_next_buflen(td); | |
463 | ||
464 | if (io_u->buflen) { | |
465 | io_u->ddir = get_rw_ddir(td); | |
843a7413 JA |
466 | |
467 | /* | |
468 | * If using a write iolog, store this entry. | |
469 | */ | |
470 | if (td->write_iolog) | |
471 | write_iolog_put(td, io_u); | |
472 | ||
aea47d44 JA |
473 | return 0; |
474 | } | |
475 | } | |
476 | ||
477 | return 1; | |
478 | } | |
479 | ||
22f78b32 | 480 | #define queue_full(td) list_empty(&(td)->io_u_freelist) |
ebac4655 | 481 | |
b1ff3403 | 482 | struct io_u *__get_io_u(struct thread_data *td) |
ebac4655 | 483 | { |
22f78b32 | 484 | struct io_u *io_u = NULL; |
ebac4655 | 485 | |
22f78b32 JA |
486 | if (!queue_full(td)) { |
487 | io_u = list_entry(td->io_u_freelist.next, struct io_u, list); | |
488 | ||
489 | io_u->error = 0; | |
490 | io_u->resid = 0; | |
491 | list_del(&io_u->list); | |
492 | list_add(&io_u->list, &td->io_u_busylist); | |
493 | td->cur_depth++; | |
494 | } | |
ebac4655 | 495 | |
ebac4655 JA |
496 | return io_u; |
497 | } | |
498 | ||
906c8d75 JA |
499 | /* |
500 | * Return an io_u to be processed. Gets a buflen and offset, sets direction, | |
501 | * etc. The returned io_u is fully ready to be prepped and submitted. | |
502 | */ | |
ebac4655 JA |
503 | static struct io_u *get_io_u(struct thread_data *td) |
504 | { | |
505 | struct io_u *io_u; | |
506 | ||
507 | io_u = __get_io_u(td); | |
508 | if (!io_u) | |
509 | return NULL; | |
510 | ||
20dc95c4 JA |
511 | if (td->zone_bytes >= td->zone_size) { |
512 | td->zone_bytes = 0; | |
513 | td->last_pos += td->zone_skip; | |
514 | } | |
515 | ||
aea47d44 | 516 | if (fill_io_u(td, io_u)) { |
ebac4655 JA |
517 | put_io_u(td, io_u); |
518 | return NULL; | |
519 | } | |
520 | ||
838a3cd3 JA |
521 | if (io_u->buflen + io_u->offset > td->real_file_size) |
522 | io_u->buflen = td->real_file_size - io_u->offset; | |
ebac4655 JA |
523 | |
524 | if (!io_u->buflen) { | |
525 | put_io_u(td, io_u); | |
526 | return NULL; | |
527 | } | |
528 | ||
843a7413 | 529 | if (!td->read_iolog && !td->sequential) |
ebac4655 JA |
530 | mark_random_map(td, io_u); |
531 | ||
20dc95c4 | 532 | td->last_pos += io_u->buflen; |
ebac4655 JA |
533 | |
534 | if (td->verify != VERIFY_NONE) | |
535 | populate_io_u(td, io_u); | |
536 | ||
aea47d44 | 537 | if (td_io_prep(td, io_u)) { |
ebac4655 JA |
538 | put_io_u(td, io_u); |
539 | return NULL; | |
540 | } | |
541 | ||
542 | gettimeofday(&io_u->start_time, NULL); | |
543 | return io_u; | |
544 | } | |
545 | ||
546 | static inline void td_set_runstate(struct thread_data *td, int runstate) | |
547 | { | |
ebac4655 JA |
548 | td->runstate = runstate; |
549 | } | |
550 | ||
aea47d44 | 551 | static int get_next_verify(struct thread_data *td, struct io_u *io_u) |
ebac4655 JA |
552 | { |
553 | struct io_piece *ipo; | |
554 | ||
22f78b32 JA |
555 | if (!list_empty(&td->io_hist_list)) { |
556 | ipo = list_entry(td->io_hist_list.next, struct io_piece, list); | |
ebac4655 | 557 | |
22f78b32 | 558 | list_del(&ipo->list); |
ebac4655 | 559 | |
22f78b32 JA |
560 | io_u->offset = ipo->offset; |
561 | io_u->buflen = ipo->len; | |
562 | io_u->ddir = DDIR_READ; | |
563 | free(ipo); | |
564 | return 0; | |
565 | } | |
566 | ||
567 | return 1; | |
ebac4655 JA |
568 | } |
569 | ||
ebac4655 JA |
570 | static int sync_td(struct thread_data *td) |
571 | { | |
2866c82d JA |
572 | if (td->io_ops->sync) |
573 | return td->io_ops->sync(td); | |
ebac4655 JA |
574 | |
575 | return 0; | |
576 | } | |
577 | ||
578 | static int io_u_getevents(struct thread_data *td, int min, int max, | |
579 | struct timespec *t) | |
580 | { | |
2866c82d | 581 | return td->io_ops->getevents(td, min, max, t); |
ebac4655 JA |
582 | } |
583 | ||
584 | static int io_u_queue(struct thread_data *td, struct io_u *io_u) | |
585 | { | |
586 | gettimeofday(&io_u->issue_time, NULL); | |
587 | ||
2866c82d | 588 | return td->io_ops->queue(td, io_u); |
ebac4655 JA |
589 | } |
590 | ||
591 | #define iocb_time(iocb) ((unsigned long) (iocb)->data) | |
592 | ||
593 | static void io_completed(struct thread_data *td, struct io_u *io_u, | |
594 | struct io_completion_data *icd) | |
595 | { | |
596 | struct timeval e; | |
597 | unsigned long msec; | |
598 | ||
599 | gettimeofday(&e, NULL); | |
600 | ||
601 | if (!io_u->error) { | |
20dc95c4 | 602 | unsigned int bytes = io_u->buflen - io_u->resid; |
3d60d1ed | 603 | const int idx = io_u->ddir; |
ebac4655 JA |
604 | |
605 | td->io_blocks[idx]++; | |
20dc95c4 JA |
606 | td->io_bytes[idx] += bytes; |
607 | td->zone_bytes += bytes; | |
608 | td->this_io_bytes[idx] += bytes; | |
ebac4655 JA |
609 | |
610 | msec = mtime_since(&io_u->issue_time, &e); | |
611 | ||
3d60d1ed JA |
612 | add_clat_sample(td, idx, msec); |
613 | add_bw_sample(td, idx); | |
ebac4655 | 614 | |
3d60d1ed | 615 | if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE) |
ebac4655 JA |
616 | log_io_piece(td, io_u); |
617 | ||
20dc95c4 | 618 | icd->bytes_done[idx] += bytes; |
ebac4655 JA |
619 | } else |
620 | icd->error = io_u->error; | |
621 | } | |
622 | ||
623 | static void ios_completed(struct thread_data *td,struct io_completion_data *icd) | |
624 | { | |
625 | struct io_u *io_u; | |
626 | int i; | |
627 | ||
628 | icd->error = 0; | |
629 | icd->bytes_done[0] = icd->bytes_done[1] = 0; | |
630 | ||
631 | for (i = 0; i < icd->nr; i++) { | |
2866c82d | 632 | io_u = td->io_ops->event(td, i); |
ebac4655 JA |
633 | |
634 | io_completed(td, io_u, icd); | |
635 | put_io_u(td, io_u); | |
636 | } | |
637 | } | |
638 | ||
906c8d75 JA |
639 | /* |
640 | * When job exits, we can cancel the in-flight IO if we are using async | |
641 | * io. Attempt to do so. | |
642 | */ | |
ebac4655 JA |
643 | static void cleanup_pending_aio(struct thread_data *td) |
644 | { | |
645 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; | |
646 | struct list_head *entry, *n; | |
647 | struct io_completion_data icd; | |
648 | struct io_u *io_u; | |
649 | int r; | |
650 | ||
651 | /* | |
652 | * get immediately available events, if any | |
653 | */ | |
654 | r = io_u_getevents(td, 0, td->cur_depth, &ts); | |
655 | if (r > 0) { | |
656 | icd.nr = r; | |
657 | ios_completed(td, &icd); | |
658 | } | |
659 | ||
660 | /* | |
661 | * now cancel remaining active events | |
662 | */ | |
2866c82d | 663 | if (td->io_ops->cancel) { |
ebac4655 JA |
664 | list_for_each_safe(entry, n, &td->io_u_busylist) { |
665 | io_u = list_entry(entry, struct io_u, list); | |
666 | ||
2866c82d | 667 | r = td->io_ops->cancel(td, io_u); |
ebac4655 JA |
668 | if (!r) |
669 | put_io_u(td, io_u); | |
670 | } | |
671 | } | |
672 | ||
673 | if (td->cur_depth) { | |
674 | r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL); | |
675 | if (r > 0) { | |
676 | icd.nr = r; | |
677 | ios_completed(td, &icd); | |
678 | } | |
679 | } | |
680 | } | |
681 | ||
682 | static int do_io_u_verify(struct thread_data *td, struct io_u **io_u) | |
683 | { | |
684 | struct io_u *v_io_u = *io_u; | |
685 | int ret = 0; | |
686 | ||
687 | if (v_io_u) { | |
688 | ret = verify_io_u(v_io_u); | |
689 | put_io_u(td, v_io_u); | |
690 | *io_u = NULL; | |
691 | } | |
692 | ||
693 | return ret; | |
694 | } | |
695 | ||
906c8d75 JA |
696 | /* |
697 | * The main verify engine. Runs over the writes we previusly submitted, | |
698 | * reads the blocks back in, and checks the crc/md5 of the data. | |
699 | */ | |
ebac4655 JA |
700 | static void do_verify(struct thread_data *td) |
701 | { | |
702 | struct timeval t; | |
703 | struct io_u *io_u, *v_io_u = NULL; | |
704 | struct io_completion_data icd; | |
705 | int ret; | |
706 | ||
707 | td_set_runstate(td, TD_VERIFYING); | |
708 | ||
709 | do { | |
710 | if (td->terminate) | |
711 | break; | |
712 | ||
713 | gettimeofday(&t, NULL); | |
714 | if (runtime_exceeded(td, &t)) | |
715 | break; | |
716 | ||
717 | io_u = __get_io_u(td); | |
718 | if (!io_u) | |
719 | break; | |
720 | ||
aea47d44 | 721 | if (get_next_verify(td, io_u)) { |
ebac4655 JA |
722 | put_io_u(td, io_u); |
723 | break; | |
724 | } | |
725 | ||
aea47d44 | 726 | if (td_io_prep(td, io_u)) { |
ebac4655 JA |
727 | put_io_u(td, io_u); |
728 | break; | |
729 | } | |
730 | ||
731 | ret = io_u_queue(td, io_u); | |
732 | if (ret) { | |
733 | put_io_u(td, io_u); | |
734 | td_verror(td, ret); | |
735 | break; | |
736 | } | |
737 | ||
738 | /* | |
739 | * we have one pending to verify, do that while | |
740 | * we are doing io on the next one | |
741 | */ | |
742 | if (do_io_u_verify(td, &v_io_u)) | |
743 | break; | |
744 | ||
745 | ret = io_u_getevents(td, 1, 1, NULL); | |
746 | if (ret != 1) { | |
747 | if (ret < 0) | |
748 | td_verror(td, ret); | |
749 | break; | |
750 | } | |
751 | ||
2866c82d | 752 | v_io_u = td->io_ops->event(td, 0); |
ebac4655 JA |
753 | icd.nr = 1; |
754 | icd.error = 0; | |
755 | io_completed(td, v_io_u, &icd); | |
756 | ||
757 | if (icd.error) { | |
758 | td_verror(td, icd.error); | |
759 | put_io_u(td, v_io_u); | |
760 | v_io_u = NULL; | |
761 | break; | |
762 | } | |
763 | ||
ebac4655 JA |
764 | /* |
765 | * if we can't submit more io, we need to verify now | |
766 | */ | |
767 | if (queue_full(td) && do_io_u_verify(td, &v_io_u)) | |
768 | break; | |
769 | ||
770 | } while (1); | |
771 | ||
772 | do_io_u_verify(td, &v_io_u); | |
773 | ||
774 | if (td->cur_depth) | |
775 | cleanup_pending_aio(td); | |
776 | ||
777 | td_set_runstate(td, TD_RUNNING); | |
778 | } | |
779 | ||
b990b5c0 JA |
780 | /* |
781 | * Not really an io thread, all it does is burn CPU cycles in the specified | |
782 | * manner. | |
783 | */ | |
784 | static void do_cpuio(struct thread_data *td) | |
785 | { | |
786 | struct timeval e; | |
787 | int split = 100 / td->cpuload; | |
788 | int i = 0; | |
789 | ||
790 | while (!td->terminate) { | |
791 | gettimeofday(&e, NULL); | |
792 | ||
793 | if (runtime_exceeded(td, &e)) | |
794 | break; | |
795 | ||
796 | if (!(i % split)) | |
797 | __usec_sleep(10000); | |
798 | else | |
799 | usec_sleep(td, 10000); | |
800 | ||
801 | i++; | |
802 | } | |
803 | } | |
804 | ||
32cd46a0 | 805 | /* |
906c8d75 | 806 | * Main IO worker function. It retrieves io_u's to process and queues |
32cd46a0 JA |
807 | * and reaps them, checking for rate and errors along the way. |
808 | */ | |
ebac4655 JA |
809 | static void do_io(struct thread_data *td) |
810 | { | |
811 | struct io_completion_data icd; | |
812 | struct timeval s, e; | |
813 | unsigned long usec; | |
814 | ||
5853e5a8 JA |
815 | td_set_runstate(td, TD_RUNNING); |
816 | ||
ebac4655 JA |
817 | while (td->this_io_bytes[td->ddir] < td->io_size) { |
818 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; | |
819 | struct timespec *timeout; | |
820 | int ret, min_evts = 0; | |
821 | struct io_u *io_u; | |
822 | ||
823 | if (td->terminate) | |
824 | break; | |
825 | ||
826 | io_u = get_io_u(td); | |
827 | if (!io_u) | |
828 | break; | |
829 | ||
830 | memcpy(&s, &io_u->start_time, sizeof(s)); | |
831 | ||
832 | ret = io_u_queue(td, io_u); | |
833 | if (ret) { | |
834 | put_io_u(td, io_u); | |
835 | td_verror(td, ret); | |
836 | break; | |
837 | } | |
838 | ||
839 | add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time)); | |
840 | ||
841 | if (td->cur_depth < td->iodepth) { | |
842 | timeout = &ts; | |
843 | min_evts = 0; | |
844 | } else { | |
845 | timeout = NULL; | |
846 | min_evts = 1; | |
847 | } | |
848 | ||
849 | ret = io_u_getevents(td, min_evts, td->cur_depth, timeout); | |
850 | if (ret < 0) { | |
851 | td_verror(td, ret); | |
852 | break; | |
853 | } else if (!ret) | |
854 | continue; | |
855 | ||
856 | icd.nr = ret; | |
857 | ios_completed(td, &icd); | |
858 | if (icd.error) { | |
859 | td_verror(td, icd.error); | |
860 | break; | |
861 | } | |
862 | ||
863 | /* | |
864 | * the rate is batched for now, it should work for batches | |
865 | * of completions except the very first one which may look | |
866 | * a little bursty | |
867 | */ | |
868 | gettimeofday(&e, NULL); | |
869 | usec = utime_since(&s, &e); | |
870 | ||
871 | rate_throttle(td, usec, icd.bytes_done[td->ddir]); | |
872 | ||
873 | if (check_min_rate(td, &e)) { | |
874 | td_verror(td, ENOMEM); | |
875 | break; | |
876 | } | |
877 | ||
878 | if (runtime_exceeded(td, &e)) | |
879 | break; | |
880 | ||
881 | if (td->thinktime) | |
882 | usec_sleep(td, td->thinktime); | |
883 | ||
884 | if (should_fsync(td) && td->fsync_blocks && | |
885 | (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0) | |
886 | sync_td(td); | |
887 | } | |
888 | ||
889 | if (td->cur_depth) | |
890 | cleanup_pending_aio(td); | |
891 | ||
5853e5a8 JA |
892 | if (should_fsync(td) && td->end_fsync) { |
893 | td_set_runstate(td, TD_FSYNCING); | |
ebac4655 | 894 | sync_td(td); |
5853e5a8 | 895 | } |
ebac4655 JA |
896 | } |
897 | ||
ebac4655 JA |
898 | static int init_io(struct thread_data *td) |
899 | { | |
2866c82d JA |
900 | if (td->io_ops->init) |
901 | return td->io_ops->init(td); | |
902 | ||
903 | return 0; | |
ebac4655 JA |
904 | } |
905 | ||
906 | static void cleanup_io_u(struct thread_data *td) | |
907 | { | |
908 | struct list_head *entry, *n; | |
909 | struct io_u *io_u; | |
910 | ||
911 | list_for_each_safe(entry, n, &td->io_u_freelist) { | |
912 | io_u = list_entry(entry, struct io_u, list); | |
913 | ||
914 | list_del(&io_u->list); | |
915 | free(io_u); | |
916 | } | |
917 | ||
918 | if (td->mem_type == MEM_MALLOC) | |
919 | free(td->orig_buffer); | |
920 | else if (td->mem_type == MEM_SHM) { | |
921 | struct shmid_ds sbuf; | |
922 | ||
923 | shmdt(td->orig_buffer); | |
924 | shmctl(td->shm_id, IPC_RMID, &sbuf); | |
925 | } else if (td->mem_type == MEM_MMAP) | |
926 | munmap(td->orig_buffer, td->orig_buffer_size); | |
927 | else | |
3b70d7e5 | 928 | log_err("Bad memory type %d\n", td->mem_type); |
ebac4655 JA |
929 | |
930 | td->orig_buffer = NULL; | |
931 | } | |
932 | ||
933 | static int init_io_u(struct thread_data *td) | |
934 | { | |
935 | struct io_u *io_u; | |
936 | int i, max_units; | |
937 | char *p; | |
938 | ||
2866c82d | 939 | if (td->io_ops->flags & FIO_CPUIO) |
b990b5c0 JA |
940 | return 0; |
941 | ||
2866c82d | 942 | if (td->io_ops->flags & FIO_SYNCIO) |
ebac4655 JA |
943 | max_units = 1; |
944 | else | |
945 | max_units = td->iodepth; | |
946 | ||
947 | td->orig_buffer_size = td->max_bs * max_units + MASK; | |
948 | ||
949 | if (td->mem_type == MEM_MALLOC) | |
950 | td->orig_buffer = malloc(td->orig_buffer_size); | |
951 | else if (td->mem_type == MEM_SHM) { | |
952 | td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600); | |
953 | if (td->shm_id < 0) { | |
954 | td_verror(td, errno); | |
955 | perror("shmget"); | |
956 | return 1; | |
957 | } | |
958 | ||
959 | td->orig_buffer = shmat(td->shm_id, NULL, 0); | |
960 | if (td->orig_buffer == (void *) -1) { | |
961 | td_verror(td, errno); | |
962 | perror("shmat"); | |
963 | td->orig_buffer = NULL; | |
964 | return 1; | |
965 | } | |
966 | } else if (td->mem_type == MEM_MMAP) { | |
967 | td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0); | |
968 | if (td->orig_buffer == MAP_FAILED) { | |
969 | td_verror(td, errno); | |
970 | perror("mmap"); | |
971 | td->orig_buffer = NULL; | |
972 | return 1; | |
973 | } | |
974 | } | |
975 | ||
ebac4655 JA |
976 | p = ALIGN(td->orig_buffer); |
977 | for (i = 0; i < max_units; i++) { | |
978 | io_u = malloc(sizeof(*io_u)); | |
979 | memset(io_u, 0, sizeof(*io_u)); | |
980 | INIT_LIST_HEAD(&io_u->list); | |
981 | ||
982 | io_u->buf = p + td->max_bs * i; | |
b1ff3403 | 983 | io_u->index = i; |
ebac4655 JA |
984 | list_add(&io_u->list, &td->io_u_freelist); |
985 | } | |
986 | ||
987 | return 0; | |
988 | } | |
989 | ||
9cc935a1 | 990 | static int create_file(struct thread_data *td, unsigned long long size) |
ebac4655 JA |
991 | { |
992 | unsigned long long left; | |
993 | unsigned int bs; | |
ebac4655 | 994 | char *b; |
9cc935a1 | 995 | int r; |
ebac4655 JA |
996 | |
997 | /* | |
998 | * unless specifically asked for overwrite, let normal io extend it | |
999 | */ | |
9cc935a1 | 1000 | if (!td->overwrite) { |
c20d25b4 | 1001 | td->real_file_size = size; |
ebac4655 | 1002 | return 0; |
c20d25b4 | 1003 | } |
ebac4655 JA |
1004 | |
1005 | if (!size) { | |
3b70d7e5 | 1006 | log_err("Need size for create\n"); |
ebac4655 JA |
1007 | td_verror(td, EINVAL); |
1008 | return 1; | |
1009 | } | |
1010 | ||
88c6ed80 | 1011 | temp_stall_ts = 1; |
9cc935a1 | 1012 | fprintf(f_out, "%s: Laying out IO file (%LuMiB)\n",td->name,size >> 20); |
88c6ed80 | 1013 | |
9cc935a1 | 1014 | td->fd = open(td->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
ebac4655 JA |
1015 | if (td->fd < 0) { |
1016 | td_verror(td, errno); | |
c20d25b4 | 1017 | goto done_noclose; |
ebac4655 JA |
1018 | } |
1019 | ||
9cc935a1 | 1020 | if (ftruncate(td->fd, td->file_size) == -1) { |
ebac4655 | 1021 | td_verror(td, errno); |
c20d25b4 | 1022 | goto done; |
ebac4655 JA |
1023 | } |
1024 | ||
1025 | td->io_size = td->file_size; | |
1026 | b = malloc(td->max_bs); | |
1027 | memset(b, 0, td->max_bs); | |
1028 | ||
1029 | left = size; | |
1030 | while (left && !td->terminate) { | |
1031 | bs = td->max_bs; | |
1032 | if (bs > left) | |
1033 | bs = left; | |
1034 | ||
1035 | r = write(td->fd, b, bs); | |
1036 | ||
1037 | if (r == (int) bs) { | |
1038 | left -= bs; | |
1039 | continue; | |
1040 | } else { | |
1041 | if (r < 0) | |
1042 | td_verror(td, errno); | |
1043 | else | |
1044 | td_verror(td, EIO); | |
1045 | ||
1046 | break; | |
1047 | } | |
1048 | } | |
1049 | ||
1050 | if (td->terminate) | |
1051 | unlink(td->file_name); | |
1052 | else if (td->create_fsync) | |
1053 | fsync(td->fd); | |
1054 | ||
c20d25b4 JA |
1055 | free(b); |
1056 | done: | |
ebac4655 JA |
1057 | close(td->fd); |
1058 | td->fd = -1; | |
c20d25b4 JA |
1059 | done_noclose: |
1060 | temp_stall_ts = 0; | |
ebac4655 JA |
1061 | return 0; |
1062 | } | |
1063 | ||
1064 | static int file_size(struct thread_data *td) | |
1065 | { | |
1066 | struct stat st; | |
1067 | ||
c20d25b4 JA |
1068 | if (td->overwrite) { |
1069 | if (fstat(td->fd, &st) == -1) { | |
1070 | td_verror(td, errno); | |
1071 | return 1; | |
1072 | } | |
ebac4655 | 1073 | |
c20d25b4 | 1074 | td->real_file_size = st.st_size; |
838a3cd3 | 1075 | |
c20d25b4 JA |
1076 | if (!td->file_size || td->file_size > td->real_file_size) |
1077 | td->file_size = td->real_file_size; | |
1078 | } | |
ebac4655 | 1079 | |
e8e387c1 | 1080 | td->file_size -= td->file_offset; |
ebac4655 JA |
1081 | return 0; |
1082 | } | |
1083 | ||
1084 | static int bdev_size(struct thread_data *td) | |
1085 | { | |
9104f874 | 1086 | unsigned long long bytes; |
ebac4655 JA |
1087 | int r; |
1088 | ||
1089 | r = blockdev_size(td->fd, &bytes); | |
1090 | if (r) { | |
1091 | td_verror(td, r); | |
1092 | return 1; | |
1093 | } | |
1094 | ||
838a3cd3 JA |
1095 | td->real_file_size = bytes; |
1096 | ||
ebac4655 JA |
1097 | /* |
1098 | * no extend possibilities, so limit size to device size if too large | |
1099 | */ | |
838a3cd3 JA |
1100 | if (!td->file_size || td->file_size > td->real_file_size) |
1101 | td->file_size = td->real_file_size; | |
ebac4655 | 1102 | |
e8e387c1 | 1103 | td->file_size -= td->file_offset; |
ebac4655 JA |
1104 | return 0; |
1105 | } | |
1106 | ||
1107 | static int get_file_size(struct thread_data *td) | |
1108 | { | |
0af7b542 | 1109 | int ret = 0; |
ebac4655 JA |
1110 | |
1111 | if (td->filetype == FIO_TYPE_FILE) | |
1112 | ret = file_size(td); | |
0af7b542 | 1113 | else if (td->filetype == FIO_TYPE_BD) |
ebac4655 | 1114 | ret = bdev_size(td); |
0af7b542 JA |
1115 | else |
1116 | td->real_file_size = -1; | |
ebac4655 JA |
1117 | |
1118 | if (ret) | |
1119 | return ret; | |
1120 | ||
e8e387c1 | 1121 | if (td->file_offset > td->real_file_size) { |
3b70d7e5 | 1122 | log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, td->file_offset, td->real_file_size); |
ebac4655 JA |
1123 | return 1; |
1124 | } | |
1125 | ||
838a3cd3 | 1126 | td->io_size = td->file_size; |
ebac4655 | 1127 | if (td->io_size == 0) { |
3b70d7e5 | 1128 | log_err("%s: no io blocks\n", td->name); |
ebac4655 JA |
1129 | td_verror(td, EINVAL); |
1130 | return 1; | |
1131 | } | |
1132 | ||
20dc95c4 JA |
1133 | if (!td->zone_size) |
1134 | td->zone_size = td->io_size; | |
1135 | ||
ebac4655 JA |
1136 | td->total_io_size = td->io_size * td->loops; |
1137 | return 0; | |
1138 | } | |
1139 | ||
1140 | static int setup_file_mmap(struct thread_data *td) | |
1141 | { | |
1142 | int flags; | |
1143 | ||
3d60d1ed JA |
1144 | if (td_rw(td)) |
1145 | flags = PROT_READ | PROT_WRITE; | |
1146 | else if (td_write(td)) { | |
ebac4655 JA |
1147 | flags = PROT_WRITE; |
1148 | ||
1149 | if (td->verify != VERIFY_NONE) | |
1150 | flags |= PROT_READ; | |
3d60d1ed JA |
1151 | } else |
1152 | flags = PROT_READ; | |
ebac4655 JA |
1153 | |
1154 | td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset); | |
1155 | if (td->mmap == MAP_FAILED) { | |
1156 | td->mmap = NULL; | |
1157 | td_verror(td, errno); | |
1158 | return 1; | |
1159 | } | |
1160 | ||
1161 | if (td->invalidate_cache) { | |
1162 | if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) { | |
1163 | td_verror(td, errno); | |
1164 | return 1; | |
1165 | } | |
1166 | } | |
1167 | ||
1168 | if (td->sequential) { | |
1169 | if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) { | |
1170 | td_verror(td, errno); | |
1171 | return 1; | |
1172 | } | |
1173 | } else { | |
1174 | if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) { | |
1175 | td_verror(td, errno); | |
1176 | return 1; | |
1177 | } | |
1178 | } | |
1179 | ||
1180 | return 0; | |
1181 | } | |
1182 | ||
1183 | static int setup_file_plain(struct thread_data *td) | |
1184 | { | |
1185 | if (td->invalidate_cache) { | |
1186 | if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) { | |
1187 | td_verror(td, errno); | |
1188 | return 1; | |
1189 | } | |
1190 | } | |
1191 | ||
1192 | if (td->sequential) { | |
1193 | if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) { | |
1194 | td_verror(td, errno); | |
1195 | return 1; | |
1196 | } | |
1197 | } else { | |
1198 | if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) { | |
1199 | td_verror(td, errno); | |
1200 | return 1; | |
1201 | } | |
1202 | } | |
1203 | ||
1204 | return 0; | |
1205 | } | |
1206 | ||
1207 | static int setup_file(struct thread_data *td) | |
1208 | { | |
1209 | struct stat st; | |
1210 | int flags = 0; | |
1211 | ||
2866c82d | 1212 | if (td->io_ops->flags & FIO_CPUIO) |
b990b5c0 JA |
1213 | return 0; |
1214 | ||
ebac4655 JA |
1215 | if (stat(td->file_name, &st) == -1) { |
1216 | if (errno != ENOENT) { | |
1217 | td_verror(td, errno); | |
1218 | return 1; | |
1219 | } | |
1220 | if (!td->create_file) { | |
1221 | td_verror(td, ENOENT); | |
1222 | return 1; | |
1223 | } | |
9cc935a1 JA |
1224 | if (create_file(td, td->file_size)) |
1225 | return 1; | |
1226 | } else if (td->filetype == FIO_TYPE_FILE && | |
1227 | st.st_size < (off_t) td->file_size) { | |
1228 | if (create_file(td, td->file_size)) | |
ebac4655 | 1229 | return 1; |
ebac4655 JA |
1230 | } |
1231 | ||
1232 | if (td->odirect) | |
2c0ecd28 | 1233 | flags |= OS_O_DIRECT; |
ebac4655 | 1234 | |
3d60d1ed | 1235 | if (td_write(td) || td_rw(td)) { |
ebac4655 JA |
1236 | if (td->filetype == FIO_TYPE_FILE) { |
1237 | if (!td->overwrite) | |
1238 | flags |= O_TRUNC; | |
1239 | ||
1240 | flags |= O_CREAT; | |
1241 | } | |
1242 | if (td->sync_io) | |
1243 | flags |= O_SYNC; | |
1244 | ||
1245 | flags |= O_RDWR; | |
1246 | ||
1247 | td->fd = open(td->file_name, flags, 0600); | |
3d60d1ed JA |
1248 | } else { |
1249 | if (td->filetype == FIO_TYPE_CHAR) | |
1250 | flags |= O_RDWR; | |
1251 | else | |
1252 | flags |= O_RDONLY; | |
1253 | ||
1254 | td->fd = open(td->file_name, flags); | |
ebac4655 JA |
1255 | } |
1256 | ||
1257 | if (td->fd == -1) { | |
1258 | td_verror(td, errno); | |
1259 | return 1; | |
1260 | } | |
1261 | ||
1262 | if (get_file_size(td)) | |
1263 | return 1; | |
1264 | ||
2866c82d | 1265 | if (td->io_ops->flags & FIO_MMAPIO) |
ebac4655 | 1266 | return setup_file_mmap(td); |
2866c82d JA |
1267 | else |
1268 | return setup_file_plain(td); | |
ebac4655 JA |
1269 | } |
1270 | ||
da86774e JA |
1271 | static int switch_ioscheduler(struct thread_data *td) |
1272 | { | |
1273 | char tmp[256], tmp2[128]; | |
1274 | FILE *f; | |
1275 | int ret; | |
1276 | ||
1277 | sprintf(tmp, "%s/queue/scheduler", td->sysfs_root); | |
1278 | ||
1279 | f = fopen(tmp, "r+"); | |
1280 | if (!f) { | |
1281 | td_verror(td, errno); | |
1282 | return 1; | |
1283 | } | |
1284 | ||
1285 | /* | |
1286 | * Set io scheduler. | |
1287 | */ | |
1288 | ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f); | |
1289 | if (ferror(f) || ret != 1) { | |
1290 | td_verror(td, errno); | |
1291 | fclose(f); | |
1292 | return 1; | |
1293 | } | |
1294 | ||
1295 | rewind(f); | |
1296 | ||
1297 | /* | |
1298 | * Read back and check that the selected scheduler is now the default. | |
1299 | */ | |
1300 | ret = fread(tmp, 1, sizeof(tmp), f); | |
1301 | if (ferror(f) || ret < 0) { | |
1302 | td_verror(td, errno); | |
1303 | fclose(f); | |
1304 | return 1; | |
1305 | } | |
1306 | ||
1307 | sprintf(tmp2, "[%s]", td->ioscheduler); | |
1308 | if (!strstr(tmp, tmp2)) { | |
3b70d7e5 | 1309 | log_err("fio: io scheduler %s not found\n", td->ioscheduler); |
da86774e JA |
1310 | td_verror(td, EINVAL); |
1311 | fclose(f); | |
1312 | return 1; | |
1313 | } | |
1314 | ||
1315 | fclose(f); | |
1316 | return 0; | |
1317 | } | |
1318 | ||
ebac4655 JA |
1319 | static void clear_io_state(struct thread_data *td) |
1320 | { | |
2866c82d | 1321 | if (td->io_ops->flags & FIO_SYNCIO) |
ebac4655 JA |
1322 | lseek(td->fd, SEEK_SET, 0); |
1323 | ||
20dc95c4 | 1324 | td->last_pos = 0; |
ebac4655 JA |
1325 | td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0; |
1326 | td->this_io_bytes[0] = td->this_io_bytes[1] = 0; | |
20dc95c4 | 1327 | td->zone_bytes = 0; |
ebac4655 JA |
1328 | |
1329 | if (td->file_map) | |
1330 | memset(td->file_map, 0, td->num_maps * sizeof(long)); | |
1331 | } | |
1332 | ||
906c8d75 JA |
1333 | /* |
1334 | * Entry point for the thread based jobs. The process based jobs end up | |
1335 | * here as well, after a little setup. | |
1336 | */ | |
ebac4655 JA |
1337 | static void *thread_main(void *data) |
1338 | { | |
1339 | struct thread_data *td = data; | |
ebac4655 JA |
1340 | |
1341 | if (!td->use_thread) | |
1342 | setsid(); | |
1343 | ||
1344 | td->pid = getpid(); | |
1345 | ||
aea47d44 JA |
1346 | INIT_LIST_HEAD(&td->io_u_freelist); |
1347 | INIT_LIST_HEAD(&td->io_u_busylist); | |
1348 | INIT_LIST_HEAD(&td->io_hist_list); | |
1349 | INIT_LIST_HEAD(&td->io_log_list); | |
1350 | ||
ebac4655 JA |
1351 | if (init_io_u(td)) |
1352 | goto err; | |
1353 | ||
1354 | if (fio_setaffinity(td) == -1) { | |
1355 | td_verror(td, errno); | |
1356 | goto err; | |
1357 | } | |
1358 | ||
1359 | if (init_io(td)) | |
1360 | goto err; | |
1361 | ||
aea47d44 JA |
1362 | if (init_iolog(td)) |
1363 | goto err; | |
1364 | ||
ebac4655 JA |
1365 | if (td->ioprio) { |
1366 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) { | |
1367 | td_verror(td, errno); | |
1368 | goto err; | |
1369 | } | |
1370 | } | |
1371 | ||
1056eaad | 1372 | if (nice(td->nice) == -1) { |
b6f4d880 JA |
1373 | td_verror(td, errno); |
1374 | goto err; | |
1375 | } | |
1376 | ||
75154845 JA |
1377 | if (init_random_state(td)) |
1378 | goto err; | |
1379 | ||
da86774e JA |
1380 | if (td->ioscheduler && switch_ioscheduler(td)) |
1381 | goto err; | |
1382 | ||
75154845 | 1383 | td_set_runstate(td, TD_INITIALIZED); |
bbfd6b00 JA |
1384 | fio_sem_up(&startup_sem); |
1385 | fio_sem_down(&td->mutex); | |
ebac4655 JA |
1386 | |
1387 | if (!td->create_serialize && setup_file(td)) | |
1388 | goto err; | |
1389 | ||
ebac4655 JA |
1390 | gettimeofday(&td->epoch, NULL); |
1391 | ||
4e0ba8af JA |
1392 | if (td->exec_prerun) |
1393 | system(td->exec_prerun); | |
1394 | ||
ebac4655 JA |
1395 | while (td->loops--) { |
1396 | getrusage(RUSAGE_SELF, &td->ru_start); | |
1397 | gettimeofday(&td->start, NULL); | |
1398 | memcpy(&td->stat_sample_time, &td->start, sizeof(td->start)); | |
1399 | ||
1400 | if (td->ratemin) | |
1401 | memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate)); | |
1402 | ||
1403 | clear_io_state(td); | |
1404 | prune_io_piece_log(td); | |
1405 | ||
2866c82d | 1406 | if (td->io_ops->flags & FIO_CPUIO) |
b990b5c0 JA |
1407 | do_cpuio(td); |
1408 | else | |
1409 | do_io(td); | |
ebac4655 JA |
1410 | |
1411 | td->runtime[td->ddir] += mtime_since_now(&td->start); | |
aea47d44 | 1412 | if (td_rw(td) && td->io_bytes[td->ddir ^ 1]) |
3d60d1ed JA |
1413 | td->runtime[td->ddir ^ 1] = td->runtime[td->ddir]; |
1414 | ||
ebac4655 JA |
1415 | update_rusage_stat(td); |
1416 | ||
1417 | if (td->error || td->terminate) | |
1418 | break; | |
1419 | ||
1420 | if (td->verify == VERIFY_NONE) | |
1421 | continue; | |
1422 | ||
1423 | clear_io_state(td); | |
1424 | gettimeofday(&td->start, NULL); | |
1425 | ||
1426 | do_verify(td); | |
1427 | ||
1428 | td->runtime[DDIR_READ] += mtime_since_now(&td->start); | |
1429 | ||
1430 | if (td->error || td->terminate) | |
1431 | break; | |
1432 | } | |
1433 | ||
ebac4655 JA |
1434 | if (td->bw_log) |
1435 | finish_log(td, td->bw_log, "bw"); | |
1436 | if (td->slat_log) | |
1437 | finish_log(td, td->slat_log, "slat"); | |
1438 | if (td->clat_log) | |
1439 | finish_log(td, td->clat_log, "clat"); | |
843a7413 JA |
1440 | if (td->write_iolog) |
1441 | write_iolog_close(td); | |
4e0ba8af JA |
1442 | if (td->exec_postrun) |
1443 | system(td->exec_postrun); | |
ebac4655 JA |
1444 | |
1445 | if (exitall_on_terminate) | |
1446 | terminate_threads(td->groupid); | |
1447 | ||
1448 | err: | |
1449 | if (td->fd != -1) { | |
1450 | close(td->fd); | |
1451 | td->fd = -1; | |
1452 | } | |
1453 | if (td->mmap) | |
1454 | munmap(td->mmap, td->file_size); | |
2866c82d | 1455 | close_ioengine(td); |
ebac4655 | 1456 | cleanup_io_u(td); |
ebac4655 JA |
1457 | td_set_runstate(td, TD_EXITED); |
1458 | return NULL; | |
1459 | ||
1460 | } | |
1461 | ||
906c8d75 JA |
1462 | /* |
1463 | * We cannot pass the td data into a forked process, so attach the td and | |
1464 | * pass it to the thread worker. | |
1465 | */ | |
ebac4655 JA |
1466 | static void *fork_main(int shmid, int offset) |
1467 | { | |
1468 | struct thread_data *td; | |
1469 | void *data; | |
1470 | ||
1471 | data = shmat(shmid, NULL, 0); | |
1472 | if (data == (void *) -1) { | |
1473 | perror("shmat"); | |
1474 | return NULL; | |
1475 | } | |
1476 | ||
1477 | td = data + offset * sizeof(struct thread_data); | |
1478 | thread_main(td); | |
1479 | shmdt(data); | |
1480 | return NULL; | |
1481 | } | |
1482 | ||
906c8d75 JA |
1483 | /* |
1484 | * Sets the status of the 'td' in the printed status map. | |
1485 | */ | |
ebac4655 JA |
1486 | static void check_str_update(struct thread_data *td) |
1487 | { | |
1488 | char c = run_str[td->thread_number - 1]; | |
1489 | ||
ebac4655 JA |
1490 | switch (td->runstate) { |
1491 | case TD_REAPED: | |
1492 | c = '_'; | |
1493 | break; | |
1494 | case TD_EXITED: | |
1495 | c = 'E'; | |
1496 | break; | |
1497 | case TD_RUNNING: | |
3d60d1ed JA |
1498 | if (td_rw(td)) { |
1499 | if (td->sequential) | |
1500 | c = 'M'; | |
1501 | else | |
1502 | c = 'm'; | |
1503 | } else if (td_read(td)) { | |
ebac4655 JA |
1504 | if (td->sequential) |
1505 | c = 'R'; | |
1506 | else | |
1507 | c = 'r'; | |
1508 | } else { | |
1509 | if (td->sequential) | |
1510 | c = 'W'; | |
1511 | else | |
1512 | c = 'w'; | |
1513 | } | |
1514 | break; | |
1515 | case TD_VERIFYING: | |
1516 | c = 'V'; | |
1517 | break; | |
5853e5a8 JA |
1518 | case TD_FSYNCING: |
1519 | c = 'F'; | |
1520 | break; | |
ebac4655 JA |
1521 | case TD_CREATED: |
1522 | c = 'C'; | |
1523 | break; | |
75154845 JA |
1524 | case TD_INITIALIZED: |
1525 | c = 'I'; | |
1526 | break; | |
ebac4655 JA |
1527 | case TD_NOT_CREATED: |
1528 | c = 'P'; | |
1529 | break; | |
1530 | default: | |
3b70d7e5 | 1531 | log_err("state %d\n", td->runstate); |
ebac4655 JA |
1532 | } |
1533 | ||
1534 | run_str[td->thread_number - 1] = c; | |
ebac4655 JA |
1535 | } |
1536 | ||
906c8d75 JA |
1537 | /* |
1538 | * Convert seconds to a printable string. | |
1539 | */ | |
5289b847 JA |
1540 | static void eta_to_str(char *str, int eta_sec) |
1541 | { | |
1542 | unsigned int d, h, m, s; | |
1543 | static int always_d, always_h; | |
1544 | ||
1545 | d = h = m = s = 0; | |
1546 | ||
1547 | s = eta_sec % 60; | |
1548 | eta_sec /= 60; | |
1549 | m = eta_sec % 60; | |
1550 | eta_sec /= 60; | |
1551 | h = eta_sec % 24; | |
1552 | eta_sec /= 24; | |
1553 | d = eta_sec; | |
1554 | ||
1555 | if (d || always_d) { | |
1556 | always_d = 1; | |
1557 | str += sprintf(str, "%02dd:", d); | |
1558 | } | |
1559 | if (h || always_h) { | |
1560 | always_h = 1; | |
1561 | str += sprintf(str, "%02dh:", h); | |
1562 | } | |
1563 | ||
1564 | str += sprintf(str, "%02dm:", m); | |
1565 | str += sprintf(str, "%02ds", s); | |
1566 | } | |
1567 | ||
906c8d75 JA |
1568 | /* |
1569 | * Best effort calculation of the estimated pending runtime of a job. | |
1570 | */ | |
6a0106a0 JA |
1571 | static int thread_eta(struct thread_data *td, unsigned long elapsed) |
1572 | { | |
1573 | unsigned long long bytes_total, bytes_done; | |
1574 | unsigned int eta_sec = 0; | |
1575 | ||
1576 | bytes_total = td->total_io_size; | |
3d60d1ed JA |
1577 | |
1578 | /* | |
1579 | * if writing, bytes_total will be twice the size. If mixing, | |
1580 | * assume a 50/50 split and thus bytes_total will be 50% larger. | |
1581 | */ | |
1582 | if (td->verify) { | |
1583 | if (td_rw(td)) | |
1584 | bytes_total = bytes_total * 3 / 2; | |
1585 | else | |
1586 | bytes_total <<= 1; | |
1587 | } | |
6a0106a0 JA |
1588 | if (td->zone_size && td->zone_skip) |
1589 | bytes_total /= (td->zone_skip / td->zone_size); | |
1590 | ||
1591 | if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) { | |
1592 | double perc; | |
8b611c34 | 1593 | |
6a0106a0 JA |
1594 | bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]; |
1595 | perc = (double) bytes_done / (double) bytes_total; | |
1596 | if (perc > 1.0) | |
1597 | perc = 1.0; | |
1598 | ||
1599 | eta_sec = (elapsed * (1.0 / perc)) - elapsed; | |
1600 | ||
8b611c34 | 1601 | if (td->timeout && eta_sec > (td->timeout - elapsed)) |
6a0106a0 | 1602 | eta_sec = td->timeout - elapsed; |
75154845 JA |
1603 | } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED |
1604 | || td->runstate == TD_INITIALIZED) { | |
6a0106a0 JA |
1605 | int t_eta = 0, r_eta = 0; |
1606 | ||
1607 | /* | |
1608 | * We can only guess - assume it'll run the full timeout | |
1609 | * if given, otherwise assume it'll run at the specified rate. | |
1610 | */ | |
1611 | if (td->timeout) | |
1612 | t_eta = td->timeout + td->start_delay - elapsed; | |
1613 | if (td->rate) { | |
1614 | r_eta = (bytes_total / 1024) / td->rate; | |
1615 | r_eta += td->start_delay - elapsed; | |
1616 | } | |
1617 | ||
1618 | if (r_eta && t_eta) | |
1619 | eta_sec = min(r_eta, t_eta); | |
1620 | else if (r_eta) | |
1621 | eta_sec = r_eta; | |
1622 | else if (t_eta) | |
1623 | eta_sec = t_eta; | |
1624 | else | |
972cfd25 | 1625 | eta_sec = 0; |
6a0106a0 JA |
1626 | } else { |
1627 | /* | |
5853e5a8 | 1628 | * thread is already done or waiting for fsync |
6a0106a0 JA |
1629 | */ |
1630 | eta_sec = 0; | |
1631 | } | |
1632 | ||
1633 | return eta_sec; | |
1634 | } | |
1635 | ||
906c8d75 JA |
1636 | /* |
1637 | * Print status of the jobs we know about. This includes rate estimates, | |
1638 | * ETA, thread state, etc. | |
1639 | */ | |
ebac4655 JA |
1640 | static void print_thread_status(void) |
1641 | { | |
6a0106a0 | 1642 | unsigned long elapsed = time_since_now(&genesis); |
71a751ce | 1643 | int i, nr_running, nr_pending, t_rate, m_rate, *eta_secs, eta_sec; |
5289b847 | 1644 | char eta_str[32]; |
6a0106a0 JA |
1645 | double perc = 0.0; |
1646 | ||
c6ae0a5b | 1647 | if (temp_stall_ts || terse_output) |
88c6ed80 JA |
1648 | return; |
1649 | ||
6a0106a0 JA |
1650 | eta_secs = malloc(thread_number * sizeof(int)); |
1651 | memset(eta_secs, 0, thread_number * sizeof(int)); | |
ebac4655 | 1652 | |
71a751ce | 1653 | nr_pending = nr_running = t_rate = m_rate = 0; |
ebac4655 JA |
1654 | for (i = 0; i < thread_number; i++) { |
1655 | struct thread_data *td = &threads[i]; | |
1656 | ||
5853e5a8 JA |
1657 | if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING|| |
1658 | td->runstate == TD_FSYNCING) { | |
ebac4655 JA |
1659 | nr_running++; |
1660 | t_rate += td->rate; | |
1661 | m_rate += td->ratemin; | |
71a751ce JA |
1662 | } else if (td->runstate < TD_RUNNING) |
1663 | nr_pending++; | |
ebac4655 | 1664 | |
6a0106a0 JA |
1665 | if (elapsed >= 3) |
1666 | eta_secs[i] = thread_eta(td, elapsed); | |
8b611c34 JA |
1667 | else |
1668 | eta_secs[i] = INT_MAX; | |
ebac4655 JA |
1669 | |
1670 | check_str_update(td); | |
1671 | } | |
1672 | ||
6a0106a0 JA |
1673 | if (exitall_on_terminate) |
1674 | eta_sec = INT_MAX; | |
1675 | else | |
1676 | eta_sec = 0; | |
5289b847 | 1677 | |
6a0106a0 JA |
1678 | for (i = 0; i < thread_number; i++) { |
1679 | if (exitall_on_terminate) { | |
1680 | if (eta_secs[i] < eta_sec) | |
1681 | eta_sec = eta_secs[i]; | |
1682 | } else { | |
1683 | if (eta_secs[i] > eta_sec) | |
1684 | eta_sec = eta_secs[i]; | |
5289b847 | 1685 | } |
6a0106a0 | 1686 | } |
5289b847 | 1687 | |
8b611c34 | 1688 | if (eta_sec != INT_MAX && elapsed) { |
6a0106a0 JA |
1689 | perc = (double) elapsed / (double) (elapsed + eta_sec); |
1690 | eta_to_str(eta_str, eta_sec); | |
ebac4655 JA |
1691 | } |
1692 | ||
71a751ce | 1693 | if (!nr_running && !nr_pending) |
f44c1d46 JA |
1694 | return; |
1695 | ||
eb8bbf48 | 1696 | printf("Threads running: %d", nr_running); |
ebac4655 JA |
1697 | if (m_rate || t_rate) |
1698 | printf(", commitrate %d/%dKiB/sec", t_rate, m_rate); | |
30521453 | 1699 | if (eta_sec != INT_MAX && nr_running) { |
6a0106a0 JA |
1700 | perc *= 100.0; |
1701 | printf(": [%s] [%3.2f%% done] [eta %s]", run_str, perc,eta_str); | |
1702 | } | |
5289b847 | 1703 | printf("\r"); |
ebac4655 | 1704 | fflush(stdout); |
6a0106a0 | 1705 | free(eta_secs); |
ebac4655 JA |
1706 | } |
1707 | ||
906c8d75 JA |
1708 | /* |
1709 | * Run over the job map and reap the threads that have exited, if any. | |
1710 | */ | |
ebac4655 JA |
1711 | static void reap_threads(int *nr_running, int *t_rate, int *m_rate) |
1712 | { | |
b990b5c0 | 1713 | int i, cputhreads; |
ebac4655 JA |
1714 | |
1715 | /* | |
1716 | * reap exited threads (TD_EXITED -> TD_REAPED) | |
1717 | */ | |
b990b5c0 | 1718 | for (i = 0, cputhreads = 0; i < thread_number; i++) { |
ebac4655 JA |
1719 | struct thread_data *td = &threads[i]; |
1720 | ||
2866c82d | 1721 | if (td->io_ops->flags & FIO_CPUIO) |
b990b5c0 JA |
1722 | cputhreads++; |
1723 | ||
ebac4655 JA |
1724 | if (td->runstate != TD_EXITED) |
1725 | continue; | |
1726 | ||
1727 | td_set_runstate(td, TD_REAPED); | |
1728 | ||
1729 | if (td->use_thread) { | |
1730 | long ret; | |
1731 | ||
1732 | if (pthread_join(td->thread, (void *) &ret)) | |
1733 | perror("thread_join"); | |
1734 | } else | |
1735 | waitpid(td->pid, NULL, 0); | |
1736 | ||
1737 | (*nr_running)--; | |
1738 | (*m_rate) -= td->ratemin; | |
1739 | (*t_rate) -= td->rate; | |
1740 | } | |
b990b5c0 JA |
1741 | |
1742 | if (*nr_running == cputhreads) | |
1743 | terminate_threads(TERMINATE_ALL); | |
ebac4655 JA |
1744 | } |
1745 | ||
fcb6ade2 JA |
1746 | static void fio_unpin_memory(void *pinned) |
1747 | { | |
1748 | if (pinned) { | |
1749 | if (munlock(pinned, mlock_size) < 0) | |
1750 | perror("munlock"); | |
1751 | munmap(pinned, mlock_size); | |
1752 | } | |
1753 | } | |
1754 | ||
1755 | static void *fio_pin_memory(void) | |
1756 | { | |
32cd46a0 | 1757 | unsigned long long phys_mem; |
fcb6ade2 JA |
1758 | void *ptr; |
1759 | ||
1760 | if (!mlock_size) | |
1761 | return NULL; | |
1762 | ||
1763 | /* | |
1764 | * Don't allow mlock of more than real_mem-128MB | |
1765 | */ | |
32cd46a0 JA |
1766 | phys_mem = os_phys_mem(); |
1767 | if (phys_mem) { | |
1768 | if ((mlock_size + 128 * 1024 * 1024) > phys_mem) { | |
1769 | mlock_size = phys_mem - 128 * 1024 * 1024; | |
eb8bbf48 | 1770 | fprintf(f_out, "fio: limiting mlocked memory to %lluMiB\n", mlock_size >> 20); |
fcb6ade2 JA |
1771 | } |
1772 | } | |
1773 | ||
1774 | ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0); | |
1775 | if (!ptr) { | |
1776 | perror("malloc locked mem"); | |
1777 | return NULL; | |
1778 | } | |
1779 | if (mlock(ptr, mlock_size) < 0) { | |
1780 | munmap(ptr, mlock_size); | |
1781 | perror("mlock"); | |
1782 | return NULL; | |
1783 | } | |
1784 | ||
1785 | return ptr; | |
1786 | } | |
1787 | ||
906c8d75 JA |
1788 | /* |
1789 | * Main function for kicking off and reaping jobs, as needed. | |
1790 | */ | |
ebac4655 JA |
1791 | static void run_threads(void) |
1792 | { | |
ebac4655 JA |
1793 | struct thread_data *td; |
1794 | unsigned long spent; | |
1795 | int i, todo, nr_running, m_rate, t_rate, nr_started; | |
fcb6ade2 JA |
1796 | void *mlocked_mem; |
1797 | ||
1798 | mlocked_mem = fio_pin_memory(); | |
ebac4655 | 1799 | |
c6ae0a5b JA |
1800 | if (!terse_output) { |
1801 | printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : ""); | |
1802 | fflush(stdout); | |
1803 | } | |
c04f7ec3 | 1804 | |
4efa970e JA |
1805 | signal(SIGINT, sig_handler); |
1806 | signal(SIGALRM, sig_handler); | |
1807 | ||
ebac4655 JA |
1808 | todo = thread_number; |
1809 | nr_running = 0; | |
1810 | nr_started = 0; | |
1811 | m_rate = t_rate = 0; | |
1812 | ||
1813 | for (i = 0; i < thread_number; i++) { | |
1814 | td = &threads[i]; | |
1815 | ||
1816 | run_str[td->thread_number - 1] = 'P'; | |
1817 | ||
1818 | init_disk_util(td); | |
1819 | ||
1820 | if (!td->create_serialize) | |
1821 | continue; | |
1822 | ||
1823 | /* | |
1824 | * do file setup here so it happens sequentially, | |
1825 | * we don't want X number of threads getting their | |
1826 | * client data interspersed on disk | |
1827 | */ | |
1828 | if (setup_file(td)) { | |
1829 | td_set_runstate(td, TD_REAPED); | |
1830 | todo--; | |
1831 | } | |
1832 | } | |
1833 | ||
1834 | gettimeofday(&genesis, NULL); | |
1835 | ||
1836 | while (todo) { | |
75154845 JA |
1837 | struct thread_data *map[MAX_JOBS]; |
1838 | struct timeval this_start; | |
1839 | int this_jobs = 0, left; | |
1840 | ||
ebac4655 JA |
1841 | /* |
1842 | * create threads (TD_NOT_CREATED -> TD_CREATED) | |
1843 | */ | |
1844 | for (i = 0; i < thread_number; i++) { | |
1845 | td = &threads[i]; | |
1846 | ||
1847 | if (td->runstate != TD_NOT_CREATED) | |
1848 | continue; | |
1849 | ||
1850 | /* | |
1851 | * never got a chance to start, killed by other | |
1852 | * thread for some reason | |
1853 | */ | |
1854 | if (td->terminate) { | |
1855 | todo--; | |
1856 | continue; | |
1857 | } | |
1858 | ||
1859 | if (td->start_delay) { | |
1860 | spent = mtime_since_now(&genesis); | |
1861 | ||
1862 | if (td->start_delay * 1000 > spent) | |
1863 | continue; | |
1864 | } | |
1865 | ||
1866 | if (td->stonewall && (nr_started || nr_running)) | |
1867 | break; | |
1868 | ||
75154845 JA |
1869 | /* |
1870 | * Set state to created. Thread will transition | |
1871 | * to TD_INITIALIZED when it's done setting up. | |
1872 | */ | |
ebac4655 | 1873 | td_set_runstate(td, TD_CREATED); |
75154845 | 1874 | map[this_jobs++] = td; |
bbfd6b00 | 1875 | fio_sem_init(&startup_sem, 1); |
ebac4655 JA |
1876 | nr_started++; |
1877 | ||
1878 | if (td->use_thread) { | |
1879 | if (pthread_create(&td->thread, NULL, thread_main, td)) { | |
1880 | perror("thread_create"); | |
1881 | nr_started--; | |
1882 | } | |
1883 | } else { | |
1884 | if (fork()) | |
bbfd6b00 | 1885 | fio_sem_down(&startup_sem); |
ebac4655 JA |
1886 | else { |
1887 | fork_main(shm_id, i); | |
1888 | exit(0); | |
1889 | } | |
1890 | } | |
1891 | } | |
1892 | ||
1893 | /* | |
75154845 JA |
1894 | * Wait for the started threads to transition to |
1895 | * TD_INITIALIZED. | |
ebac4655 | 1896 | */ |
75154845 JA |
1897 | gettimeofday(&this_start, NULL); |
1898 | left = this_jobs; | |
1899 | while (left) { | |
1900 | if (mtime_since_now(&this_start) > JOB_START_TIMEOUT) | |
1901 | break; | |
1902 | ||
1903 | usleep(100000); | |
1904 | ||
1905 | for (i = 0; i < this_jobs; i++) { | |
1906 | td = map[i]; | |
1907 | if (!td) | |
1908 | continue; | |
b6f4d880 | 1909 | if (td->runstate == TD_INITIALIZED) { |
75154845 JA |
1910 | map[i] = NULL; |
1911 | left--; | |
b6f4d880 JA |
1912 | } else if (td->runstate >= TD_EXITED) { |
1913 | map[i] = NULL; | |
1914 | left--; | |
1915 | todo--; | |
1916 | nr_running++; /* work-around... */ | |
75154845 JA |
1917 | } |
1918 | } | |
1919 | } | |
1920 | ||
1921 | if (left) { | |
3b70d7e5 | 1922 | log_err("fio: %d jobs failed to start\n", left); |
75154845 JA |
1923 | for (i = 0; i < this_jobs; i++) { |
1924 | td = map[i]; | |
1925 | if (!td) | |
1926 | continue; | |
1927 | kill(td->pid, SIGTERM); | |
1928 | } | |
1929 | break; | |
1930 | } | |
1931 | ||
1932 | /* | |
b6f4d880 | 1933 | * start created threads (TD_INITIALIZED -> TD_RUNNING). |
75154845 | 1934 | */ |
ebac4655 JA |
1935 | for (i = 0; i < thread_number; i++) { |
1936 | td = &threads[i]; | |
1937 | ||
75154845 | 1938 | if (td->runstate != TD_INITIALIZED) |
ebac4655 JA |
1939 | continue; |
1940 | ||
1941 | td_set_runstate(td, TD_RUNNING); | |
1942 | nr_running++; | |
1943 | nr_started--; | |
1944 | m_rate += td->ratemin; | |
1945 | t_rate += td->rate; | |
75154845 | 1946 | todo--; |
bbfd6b00 | 1947 | fio_sem_up(&td->mutex); |
ebac4655 JA |
1948 | } |
1949 | ||
1950 | reap_threads(&nr_running, &t_rate, &m_rate); | |
1951 | ||
1952 | if (todo) | |
1953 | usleep(100000); | |
1954 | } | |
1955 | ||
1956 | while (nr_running) { | |
1957 | reap_threads(&nr_running, &t_rate, &m_rate); | |
1958 | usleep(10000); | |
1959 | } | |
1960 | ||
1961 | update_io_ticks(); | |
fcb6ade2 | 1962 | fio_unpin_memory(mlocked_mem); |
ebac4655 JA |
1963 | } |
1964 | ||
ebac4655 JA |
1965 | int main(int argc, char *argv[]) |
1966 | { | |
1967 | if (parse_options(argc, argv)) | |
1968 | return 1; | |
1969 | ||
1970 | if (!thread_number) { | |
3b70d7e5 | 1971 | log_err("Nothing to do\n"); |
ebac4655 JA |
1972 | return 1; |
1973 | } | |
1974 | ||
1975 | disk_util_timer_arm(); | |
1976 | ||
1977 | run_threads(); | |
1978 | show_run_stats(); | |
1979 | ||
1980 | return 0; | |
1981 | } |