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 | 6 | * |
8e9fe637 JA |
7 | * The license below covers all files distributed with fio unless otherwise |
8 | * noted in the file itself. | |
9 | * | |
ebac4655 | 10 | * This program is free software; you can redistribute it and/or modify |
8e9fe637 JA |
11 | * it under the terms of the GNU General Public License version 2 as |
12 | * published by the Free Software Foundation. | |
ebac4655 JA |
13 | * |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
22 | * | |
23 | */ | |
ebac4655 JA |
24 | #include <unistd.h> |
25 | #include <fcntl.h> | |
26 | #include <string.h> | |
ebac4655 JA |
27 | #include <signal.h> |
28 | #include <time.h> | |
ebac4655 | 29 | #include <assert.h> |
ebac4655 JA |
30 | #include <sys/stat.h> |
31 | #include <sys/wait.h> | |
32 | #include <sys/ipc.h> | |
33 | #include <sys/shm.h> | |
34 | #include <sys/ioctl.h> | |
35 | #include <sys/mman.h> | |
36 | ||
37 | #include "fio.h" | |
38 | #include "os.h" | |
39 | ||
40 | #define MASK (4095) | |
41 | ||
42 | #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK)) | |
43 | ||
44 | int groupid = 0; | |
45 | int thread_number = 0; | |
ebac4655 | 46 | int shm_id = 0; |
53cdc686 | 47 | int temp_stall_ts; |
ebac4655 | 48 | |
bbfd6b00 | 49 | static volatile int startup_sem; |
ebac4655 JA |
50 | |
51 | #define TERMINATE_ALL (-1) | |
75154845 | 52 | #define JOB_START_TIMEOUT (5 * 1000) |
ebac4655 JA |
53 | |
54 | static void terminate_threads(int group_id) | |
55 | { | |
34572e28 | 56 | struct thread_data *td; |
ebac4655 JA |
57 | int i; |
58 | ||
34572e28 | 59 | for_each_td(td, i) { |
ebac4655 JA |
60 | if (group_id == TERMINATE_ALL || groupid == td->groupid) { |
61 | td->terminate = 1; | |
62 | td->start_delay = 0; | |
63 | } | |
64 | } | |
65 | } | |
66 | ||
67 | static void sig_handler(int sig) | |
68 | { | |
69 | switch (sig) { | |
70 | case SIGALRM: | |
71 | update_io_ticks(); | |
72 | disk_util_timer_arm(); | |
73 | print_thread_status(); | |
74 | break; | |
75 | default: | |
76 | printf("\nfio: terminating on signal\n"); | |
77 | fflush(stdout); | |
78 | terminate_threads(TERMINATE_ALL); | |
79 | break; | |
80 | } | |
81 | } | |
82 | ||
906c8d75 JA |
83 | /* |
84 | * Check if we are above the minimum rate given. | |
85 | */ | |
ebac4655 JA |
86 | static int check_min_rate(struct thread_data *td, struct timeval *now) |
87 | { | |
88 | unsigned long spent; | |
89 | unsigned long rate; | |
90 | int ddir = td->ddir; | |
91 | ||
92 | /* | |
93 | * allow a 2 second settle period in the beginning | |
94 | */ | |
95 | if (mtime_since(&td->start, now) < 2000) | |
96 | return 0; | |
97 | ||
98 | /* | |
99 | * if rate blocks is set, sample is running | |
100 | */ | |
101 | if (td->rate_bytes) { | |
102 | spent = mtime_since(&td->lastrate, now); | |
103 | if (spent < td->ratecycle) | |
104 | return 0; | |
105 | ||
106 | rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent; | |
107 | if (rate < td->ratemin) { | |
1e97cce9 | 108 | fprintf(f_out, "%s: min rate %u not met, got %luKiB/sec\n", td->name, td->ratemin, rate); |
ebac4655 JA |
109 | return 1; |
110 | } | |
111 | } | |
112 | ||
113 | td->rate_bytes = td->this_io_bytes[ddir]; | |
114 | memcpy(&td->lastrate, now, sizeof(*now)); | |
115 | return 0; | |
116 | } | |
117 | ||
118 | static inline int runtime_exceeded(struct thread_data *td, struct timeval *t) | |
119 | { | |
120 | if (!td->timeout) | |
121 | return 0; | |
122 | if (mtime_since(&td->epoch, t) >= td->timeout * 1000) | |
123 | return 1; | |
124 | ||
125 | return 0; | |
126 | } | |
127 | ||
ebac4655 JA |
128 | static inline void td_set_runstate(struct thread_data *td, int runstate) |
129 | { | |
ebac4655 JA |
130 | td->runstate = runstate; |
131 | } | |
132 | ||
53cdc686 JA |
133 | static struct fio_file *get_next_file(struct thread_data *td) |
134 | { | |
0ab8db89 | 135 | unsigned int old_next_file = td->next_file; |
b2a15192 JA |
136 | struct fio_file *f; |
137 | ||
138 | do { | |
139 | f = &td->files[td->next_file]; | |
140 | ||
141 | td->next_file++; | |
142 | if (td->next_file >= td->nr_files) | |
143 | td->next_file = 0; | |
144 | ||
145 | if (f->fd != -1) | |
146 | break; | |
53cdc686 | 147 | |
b2a15192 JA |
148 | f = NULL; |
149 | } while (td->next_file != old_next_file); | |
53cdc686 JA |
150 | |
151 | return f; | |
152 | } | |
153 | ||
906c8d75 JA |
154 | /* |
155 | * When job exits, we can cancel the in-flight IO if we are using async | |
156 | * io. Attempt to do so. | |
157 | */ | |
ebac4655 JA |
158 | static void cleanup_pending_aio(struct thread_data *td) |
159 | { | |
160 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; | |
161 | struct list_head *entry, *n; | |
162 | struct io_completion_data icd; | |
163 | struct io_u *io_u; | |
164 | int r; | |
165 | ||
166 | /* | |
167 | * get immediately available events, if any | |
168 | */ | |
45bee283 | 169 | r = td_io_getevents(td, 0, td->cur_depth, &ts); |
ebac4655 JA |
170 | if (r > 0) { |
171 | icd.nr = r; | |
172 | ios_completed(td, &icd); | |
173 | } | |
174 | ||
175 | /* | |
176 | * now cancel remaining active events | |
177 | */ | |
2866c82d | 178 | if (td->io_ops->cancel) { |
ebac4655 JA |
179 | list_for_each_safe(entry, n, &td->io_u_busylist) { |
180 | io_u = list_entry(entry, struct io_u, list); | |
181 | ||
2866c82d | 182 | r = td->io_ops->cancel(td, io_u); |
ebac4655 JA |
183 | if (!r) |
184 | put_io_u(td, io_u); | |
185 | } | |
186 | } | |
187 | ||
188 | if (td->cur_depth) { | |
45bee283 | 189 | r = td_io_getevents(td, td->cur_depth, td->cur_depth, NULL); |
ebac4655 JA |
190 | if (r > 0) { |
191 | icd.nr = r; | |
192 | ios_completed(td, &icd); | |
193 | } | |
194 | } | |
195 | } | |
196 | ||
858a3d47 JA |
197 | /* |
198 | * Helper to handle the final sync of a file. Works just like the normal | |
199 | * io path, just does everything sync. | |
200 | */ | |
201 | static int fio_io_sync(struct thread_data *td, struct fio_file *f) | |
202 | { | |
203 | struct io_u *io_u = __get_io_u(td); | |
204 | struct io_completion_data icd; | |
205 | int ret; | |
206 | ||
207 | if (!io_u) | |
208 | return 1; | |
209 | ||
210 | io_u->ddir = DDIR_SYNC; | |
211 | io_u->file = f; | |
212 | ||
213 | if (td_io_prep(td, io_u)) { | |
214 | put_io_u(td, io_u); | |
215 | return 1; | |
216 | } | |
217 | ||
218 | ret = td_io_queue(td, io_u); | |
219 | if (ret) { | |
353a7e0e | 220 | td_verror(td, io_u->error); |
858a3d47 | 221 | put_io_u(td, io_u); |
858a3d47 JA |
222 | return 1; |
223 | } | |
224 | ||
225 | ret = td_io_getevents(td, 1, td->cur_depth, NULL); | |
226 | if (ret < 0) { | |
353a7e0e | 227 | td_verror(td, ret); |
858a3d47 JA |
228 | return 1; |
229 | } | |
230 | ||
231 | icd.nr = ret; | |
232 | ios_completed(td, &icd); | |
233 | if (icd.error) { | |
234 | td_verror(td, icd.error); | |
235 | return 1; | |
236 | } | |
237 | ||
238 | return 0; | |
239 | } | |
240 | ||
906c8d75 JA |
241 | /* |
242 | * The main verify engine. Runs over the writes we previusly submitted, | |
243 | * reads the blocks back in, and checks the crc/md5 of the data. | |
244 | */ | |
1e97cce9 | 245 | static void do_verify(struct thread_data *td) |
ebac4655 | 246 | { |
ebac4655 JA |
247 | struct io_u *io_u, *v_io_u = NULL; |
248 | struct io_completion_data icd; | |
53cdc686 | 249 | struct fio_file *f; |
e5b401d4 JA |
250 | int ret, i; |
251 | ||
252 | /* | |
253 | * sync io first and invalidate cache, to make sure we really | |
254 | * read from disk. | |
255 | */ | |
256 | for_each_file(td, f, i) { | |
858a3d47 | 257 | fio_io_sync(td, f); |
e5b401d4 JA |
258 | file_invalidate_cache(td, f); |
259 | } | |
ebac4655 JA |
260 | |
261 | td_set_runstate(td, TD_VERIFYING); | |
262 | ||
263 | do { | |
264 | if (td->terminate) | |
265 | break; | |
266 | ||
ebac4655 JA |
267 | io_u = __get_io_u(td); |
268 | if (!io_u) | |
269 | break; | |
270 | ||
02bcaa8c JA |
271 | if (runtime_exceeded(td, &io_u->start_time)) { |
272 | put_io_u(td, io_u); | |
273 | break; | |
274 | } | |
275 | ||
aea47d44 | 276 | if (get_next_verify(td, io_u)) { |
ebac4655 JA |
277 | put_io_u(td, io_u); |
278 | break; | |
279 | } | |
280 | ||
53cdc686 JA |
281 | f = get_next_file(td); |
282 | if (!f) | |
283 | break; | |
284 | ||
285 | io_u->file = f; | |
286 | ||
aea47d44 | 287 | if (td_io_prep(td, io_u)) { |
ebac4655 JA |
288 | put_io_u(td, io_u); |
289 | break; | |
290 | } | |
291 | ||
45bee283 | 292 | ret = td_io_queue(td, io_u); |
ebac4655 | 293 | if (ret) { |
353a7e0e | 294 | td_verror(td, io_u->error); |
ebac4655 | 295 | put_io_u(td, io_u); |
ebac4655 JA |
296 | break; |
297 | } | |
298 | ||
299 | /* | |
300 | * we have one pending to verify, do that while | |
301 | * we are doing io on the next one | |
302 | */ | |
303 | if (do_io_u_verify(td, &v_io_u)) | |
304 | break; | |
305 | ||
45bee283 | 306 | ret = td_io_getevents(td, 1, 1, NULL); |
ebac4655 JA |
307 | if (ret != 1) { |
308 | if (ret < 0) | |
309 | td_verror(td, ret); | |
310 | break; | |
311 | } | |
312 | ||
2866c82d | 313 | v_io_u = td->io_ops->event(td, 0); |
ebac4655 JA |
314 | icd.nr = 1; |
315 | icd.error = 0; | |
02bcaa8c | 316 | fio_gettime(&icd.time, NULL); |
ebac4655 JA |
317 | io_completed(td, v_io_u, &icd); |
318 | ||
319 | if (icd.error) { | |
320 | td_verror(td, icd.error); | |
321 | put_io_u(td, v_io_u); | |
322 | v_io_u = NULL; | |
323 | break; | |
324 | } | |
325 | ||
ebac4655 JA |
326 | /* |
327 | * if we can't submit more io, we need to verify now | |
328 | */ | |
329 | if (queue_full(td) && do_io_u_verify(td, &v_io_u)) | |
330 | break; | |
331 | ||
332 | } while (1); | |
333 | ||
334 | do_io_u_verify(td, &v_io_u); | |
335 | ||
336 | if (td->cur_depth) | |
337 | cleanup_pending_aio(td); | |
338 | ||
339 | td_set_runstate(td, TD_RUNNING); | |
340 | } | |
341 | ||
b990b5c0 JA |
342 | /* |
343 | * Not really an io thread, all it does is burn CPU cycles in the specified | |
344 | * manner. | |
345 | */ | |
346 | static void do_cpuio(struct thread_data *td) | |
347 | { | |
348 | struct timeval e; | |
349 | int split = 100 / td->cpuload; | |
350 | int i = 0; | |
351 | ||
352 | while (!td->terminate) { | |
02bcaa8c | 353 | fio_gettime(&e, NULL); |
b990b5c0 JA |
354 | |
355 | if (runtime_exceeded(td, &e)) | |
356 | break; | |
357 | ||
358 | if (!(i % split)) | |
359 | __usec_sleep(10000); | |
360 | else | |
361 | usec_sleep(td, 10000); | |
362 | ||
363 | i++; | |
364 | } | |
365 | } | |
366 | ||
32cd46a0 | 367 | /* |
906c8d75 | 368 | * Main IO worker function. It retrieves io_u's to process and queues |
32cd46a0 JA |
369 | * and reaps them, checking for rate and errors along the way. |
370 | */ | |
ebac4655 JA |
371 | static void do_io(struct thread_data *td) |
372 | { | |
373 | struct io_completion_data icd; | |
02bcaa8c | 374 | struct timeval s; |
ebac4655 | 375 | unsigned long usec; |
53cdc686 | 376 | struct fio_file *f; |
84585003 | 377 | int i, ret = 0; |
ebac4655 | 378 | |
5853e5a8 JA |
379 | td_set_runstate(td, TD_RUNNING); |
380 | ||
ebac4655 | 381 | while (td->this_io_bytes[td->ddir] < td->io_size) { |
ebac4655 | 382 | struct timespec *timeout; |
84585003 | 383 | int min_evts = 0; |
ebac4655 JA |
384 | struct io_u *io_u; |
385 | ||
386 | if (td->terminate) | |
387 | break; | |
388 | ||
53cdc686 JA |
389 | f = get_next_file(td); |
390 | if (!f) | |
391 | break; | |
392 | ||
393 | io_u = get_io_u(td, f); | |
ebac4655 JA |
394 | if (!io_u) |
395 | break; | |
396 | ||
397 | memcpy(&s, &io_u->start_time, sizeof(s)); | |
398 | ||
45bee283 | 399 | ret = td_io_queue(td, io_u); |
ebac4655 | 400 | if (ret) { |
353a7e0e | 401 | td_verror(td, io_u->error); |
ebac4655 | 402 | put_io_u(td, io_u); |
ebac4655 JA |
403 | break; |
404 | } | |
405 | ||
406 | add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time)); | |
407 | ||
408 | if (td->cur_depth < td->iodepth) { | |
8d7ad8d9 JA |
409 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; |
410 | ||
ebac4655 JA |
411 | timeout = &ts; |
412 | min_evts = 0; | |
413 | } else { | |
414 | timeout = NULL; | |
415 | min_evts = 1; | |
416 | } | |
417 | ||
45bee283 | 418 | ret = td_io_getevents(td, min_evts, td->cur_depth, timeout); |
ebac4655 | 419 | if (ret < 0) { |
87dc1ab1 | 420 | td_verror(td, ret); |
ebac4655 JA |
421 | break; |
422 | } else if (!ret) | |
423 | continue; | |
424 | ||
425 | icd.nr = ret; | |
426 | ios_completed(td, &icd); | |
427 | if (icd.error) { | |
428 | td_verror(td, icd.error); | |
429 | break; | |
430 | } | |
431 | ||
432 | /* | |
433 | * the rate is batched for now, it should work for batches | |
434 | * of completions except the very first one which may look | |
435 | * a little bursty | |
436 | */ | |
02bcaa8c | 437 | usec = utime_since(&s, &icd.time); |
ebac4655 | 438 | |
a00735e6 | 439 | rate_throttle(td, usec, icd.bytes_done[td->ddir], td->ddir); |
ebac4655 | 440 | |
02bcaa8c | 441 | if (check_min_rate(td, &icd.time)) { |
98aa62d8 | 442 | if (exitall_on_terminate) |
2f9ade3c | 443 | terminate_threads(td->groupid); |
ebac4655 JA |
444 | td_verror(td, ENOMEM); |
445 | break; | |
446 | } | |
447 | ||
02bcaa8c | 448 | if (runtime_exceeded(td, &icd.time)) |
ebac4655 JA |
449 | break; |
450 | ||
9c1f7434 JA |
451 | if (td->thinktime) { |
452 | unsigned long long b; | |
453 | ||
454 | b = td->io_blocks[0] + td->io_blocks[1]; | |
25309a33 | 455 | if (!(b % td->thinktime_blocks)) |
9c1f7434 JA |
456 | usec_sleep(td, td->thinktime); |
457 | } | |
ebac4655 JA |
458 | } |
459 | ||
4d2413c6 | 460 | if (!td->error) { |
84585003 JA |
461 | if (td->cur_depth) |
462 | cleanup_pending_aio(td); | |
ebac4655 | 463 | |
84585003 JA |
464 | if (should_fsync(td) && td->end_fsync) { |
465 | td_set_runstate(td, TD_FSYNCING); | |
466 | for_each_file(td, f, i) | |
858a3d47 | 467 | fio_io_sync(td, f); |
84585003 | 468 | } |
5853e5a8 | 469 | } |
ebac4655 JA |
470 | } |
471 | ||
ebac4655 JA |
472 | static void cleanup_io_u(struct thread_data *td) |
473 | { | |
474 | struct list_head *entry, *n; | |
475 | struct io_u *io_u; | |
476 | ||
477 | list_for_each_safe(entry, n, &td->io_u_freelist) { | |
478 | io_u = list_entry(entry, struct io_u, list); | |
479 | ||
480 | list_del(&io_u->list); | |
481 | free(io_u); | |
482 | } | |
483 | ||
2f9ade3c | 484 | free_io_mem(td); |
ebac4655 JA |
485 | } |
486 | ||
6b9cea23 JA |
487 | /* |
488 | * "randomly" fill the buffer contents | |
489 | */ | |
66eeb296 | 490 | static void fill_rand_buf(struct io_u *io_u, int max_bs) |
6b9cea23 | 491 | { |
66eeb296 | 492 | int *ptr = io_u->buf; |
6b9cea23 JA |
493 | |
494 | while ((void *) ptr - io_u->buf < max_bs) { | |
495 | *ptr = rand() * 0x9e370001; | |
496 | ptr++; | |
497 | } | |
498 | } | |
499 | ||
ebac4655 JA |
500 | static int init_io_u(struct thread_data *td) |
501 | { | |
502 | struct io_u *io_u; | |
a00735e6 | 503 | unsigned int max_bs; |
ebac4655 JA |
504 | int i, max_units; |
505 | char *p; | |
506 | ||
2866c82d | 507 | if (td->io_ops->flags & FIO_CPUIO) |
b990b5c0 JA |
508 | return 0; |
509 | ||
2866c82d | 510 | if (td->io_ops->flags & FIO_SYNCIO) |
ebac4655 JA |
511 | max_units = 1; |
512 | else | |
513 | max_units = td->iodepth; | |
514 | ||
a00735e6 | 515 | max_bs = max(td->max_bs[DDIR_READ], td->max_bs[DDIR_WRITE]); |
74b025b0 JA |
516 | td->orig_buffer_size = max_bs * max_units; |
517 | ||
d0bdaf49 | 518 | if (td->mem_type == MEM_SHMHUGE || td->mem_type == MEM_MMAPHUGE) |
56bb17f2 | 519 | td->orig_buffer_size = (td->orig_buffer_size + td->hugepage_size - 1) & ~(td->hugepage_size - 1); |
74b025b0 JA |
520 | else |
521 | td->orig_buffer_size += MASK; | |
ebac4655 | 522 | |
2f9ade3c JA |
523 | if (allocate_io_mem(td)) |
524 | return 1; | |
ebac4655 | 525 | |
ebac4655 JA |
526 | p = ALIGN(td->orig_buffer); |
527 | for (i = 0; i < max_units; i++) { | |
528 | io_u = malloc(sizeof(*io_u)); | |
529 | memset(io_u, 0, sizeof(*io_u)); | |
530 | INIT_LIST_HEAD(&io_u->list); | |
531 | ||
a00735e6 | 532 | io_u->buf = p + max_bs * i; |
6b9cea23 | 533 | if (td_write(td) || td_rw(td)) |
a00735e6 | 534 | fill_rand_buf(io_u, max_bs); |
6b9cea23 | 535 | |
b1ff3403 | 536 | io_u->index = i; |
ebac4655 JA |
537 | list_add(&io_u->list, &td->io_u_freelist); |
538 | } | |
539 | ||
540 | return 0; | |
541 | } | |
542 | ||
da86774e JA |
543 | static int switch_ioscheduler(struct thread_data *td) |
544 | { | |
545 | char tmp[256], tmp2[128]; | |
546 | FILE *f; | |
547 | int ret; | |
548 | ||
f48b467c JA |
549 | if (td->io_ops->flags & FIO_CPUIO) |
550 | return 0; | |
551 | ||
da86774e JA |
552 | sprintf(tmp, "%s/queue/scheduler", td->sysfs_root); |
553 | ||
554 | f = fopen(tmp, "r+"); | |
555 | if (!f) { | |
556 | td_verror(td, errno); | |
557 | return 1; | |
558 | } | |
559 | ||
560 | /* | |
561 | * Set io scheduler. | |
562 | */ | |
563 | ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f); | |
564 | if (ferror(f) || ret != 1) { | |
565 | td_verror(td, errno); | |
566 | fclose(f); | |
567 | return 1; | |
568 | } | |
569 | ||
570 | rewind(f); | |
571 | ||
572 | /* | |
573 | * Read back and check that the selected scheduler is now the default. | |
574 | */ | |
575 | ret = fread(tmp, 1, sizeof(tmp), f); | |
576 | if (ferror(f) || ret < 0) { | |
577 | td_verror(td, errno); | |
578 | fclose(f); | |
579 | return 1; | |
580 | } | |
581 | ||
582 | sprintf(tmp2, "[%s]", td->ioscheduler); | |
583 | if (!strstr(tmp, tmp2)) { | |
3b70d7e5 | 584 | log_err("fio: io scheduler %s not found\n", td->ioscheduler); |
da86774e JA |
585 | td_verror(td, EINVAL); |
586 | fclose(f); | |
587 | return 1; | |
588 | } | |
589 | ||
590 | fclose(f); | |
591 | return 0; | |
592 | } | |
593 | ||
ebac4655 JA |
594 | static void clear_io_state(struct thread_data *td) |
595 | { | |
53cdc686 JA |
596 | struct fio_file *f; |
597 | int i; | |
ebac4655 | 598 | |
ebac4655 JA |
599 | td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0; |
600 | td->this_io_bytes[0] = td->this_io_bytes[1] = 0; | |
20dc95c4 | 601 | td->zone_bytes = 0; |
ebac4655 | 602 | |
53cdc686 JA |
603 | for_each_file(td, f, i) { |
604 | f->last_pos = 0; | |
605 | if (td->io_ops->flags & FIO_SYNCIO) | |
606 | lseek(f->fd, SEEK_SET, 0); | |
607 | ||
608 | if (f->file_map) | |
609 | memset(f->file_map, 0, f->num_maps * sizeof(long)); | |
610 | } | |
ebac4655 JA |
611 | } |
612 | ||
906c8d75 JA |
613 | /* |
614 | * Entry point for the thread based jobs. The process based jobs end up | |
615 | * here as well, after a little setup. | |
616 | */ | |
ebac4655 JA |
617 | static void *thread_main(void *data) |
618 | { | |
69008999 | 619 | unsigned long long runtime[2]; |
ebac4655 | 620 | struct thread_data *td = data; |
ebac4655 JA |
621 | |
622 | if (!td->use_thread) | |
623 | setsid(); | |
624 | ||
625 | td->pid = getpid(); | |
626 | ||
aea47d44 JA |
627 | INIT_LIST_HEAD(&td->io_u_freelist); |
628 | INIT_LIST_HEAD(&td->io_u_busylist); | |
629 | INIT_LIST_HEAD(&td->io_hist_list); | |
630 | INIT_LIST_HEAD(&td->io_log_list); | |
631 | ||
ebac4655 JA |
632 | if (init_io_u(td)) |
633 | goto err; | |
634 | ||
635 | if (fio_setaffinity(td) == -1) { | |
636 | td_verror(td, errno); | |
637 | goto err; | |
638 | } | |
639 | ||
45bee283 | 640 | if (td_io_init(td)) |
ebac4655 JA |
641 | goto err; |
642 | ||
aea47d44 JA |
643 | if (init_iolog(td)) |
644 | goto err; | |
645 | ||
ebac4655 JA |
646 | if (td->ioprio) { |
647 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) { | |
648 | td_verror(td, errno); | |
649 | goto err; | |
650 | } | |
651 | } | |
652 | ||
1056eaad | 653 | if (nice(td->nice) == -1) { |
b6f4d880 JA |
654 | td_verror(td, errno); |
655 | goto err; | |
656 | } | |
657 | ||
75154845 JA |
658 | if (init_random_state(td)) |
659 | goto err; | |
660 | ||
56f9498d JA |
661 | if (td->ioscheduler && switch_ioscheduler(td)) |
662 | goto err; | |
da86774e | 663 | |
75154845 | 664 | td_set_runstate(td, TD_INITIALIZED); |
bbfd6b00 JA |
665 | fio_sem_up(&startup_sem); |
666 | fio_sem_down(&td->mutex); | |
ebac4655 | 667 | |
53cdc686 | 668 | if (!td->create_serialize && setup_files(td)) |
ebac4655 | 669 | goto err; |
21972cde JA |
670 | if (open_files(td)) |
671 | goto err; | |
ebac4655 | 672 | |
56f9498d | 673 | if (td->exec_prerun) |
4e0ba8af JA |
674 | system(td->exec_prerun); |
675 | ||
69008999 | 676 | fio_gettime(&td->epoch, NULL); |
36dff966 | 677 | getrusage(RUSAGE_SELF, &td->ru_start); |
69008999 JA |
678 | |
679 | runtime[0] = runtime[1] = 0; | |
ebac4655 | 680 | while (td->loops--) { |
02bcaa8c | 681 | fio_gettime(&td->start, NULL); |
ebac4655 JA |
682 | memcpy(&td->stat_sample_time, &td->start, sizeof(td->start)); |
683 | ||
684 | if (td->ratemin) | |
685 | memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate)); | |
686 | ||
687 | clear_io_state(td); | |
688 | prune_io_piece_log(td); | |
689 | ||
2866c82d | 690 | if (td->io_ops->flags & FIO_CPUIO) |
b990b5c0 JA |
691 | do_cpuio(td); |
692 | else | |
693 | do_io(td); | |
ebac4655 | 694 | |
69008999 | 695 | runtime[td->ddir] += utime_since_now(&td->start); |
aea47d44 | 696 | if (td_rw(td) && td->io_bytes[td->ddir ^ 1]) |
69008999 | 697 | runtime[td->ddir ^ 1] = runtime[td->ddir]; |
3d60d1ed | 698 | |
ebac4655 JA |
699 | if (td->error || td->terminate) |
700 | break; | |
701 | ||
702 | if (td->verify == VERIFY_NONE) | |
703 | continue; | |
704 | ||
705 | clear_io_state(td); | |
02bcaa8c | 706 | fio_gettime(&td->start, NULL); |
ebac4655 JA |
707 | |
708 | do_verify(td); | |
709 | ||
69008999 | 710 | runtime[DDIR_READ] += utime_since_now(&td->start); |
ebac4655 JA |
711 | |
712 | if (td->error || td->terminate) | |
713 | break; | |
714 | } | |
715 | ||
36dff966 | 716 | update_rusage_stat(td); |
69008999 JA |
717 | fio_gettime(&td->end_time, NULL); |
718 | td->runtime[0] = runtime[0] / 1000; | |
719 | td->runtime[1] = runtime[1] / 1000; | |
720 | ||
ebac4655 JA |
721 | if (td->bw_log) |
722 | finish_log(td, td->bw_log, "bw"); | |
723 | if (td->slat_log) | |
724 | finish_log(td, td->slat_log, "slat"); | |
725 | if (td->clat_log) | |
726 | finish_log(td, td->clat_log, "clat"); | |
076efc7c | 727 | if (td->write_iolog_file) |
843a7413 | 728 | write_iolog_close(td); |
56f9498d | 729 | if (td->exec_postrun) |
4e0ba8af | 730 | system(td->exec_postrun); |
ebac4655 JA |
731 | |
732 | if (exitall_on_terminate) | |
733 | terminate_threads(td->groupid); | |
734 | ||
735 | err: | |
53cdc686 | 736 | close_files(td); |
2866c82d | 737 | close_ioengine(td); |
ebac4655 | 738 | cleanup_io_u(td); |
ebac4655 JA |
739 | td_set_runstate(td, TD_EXITED); |
740 | return NULL; | |
741 | ||
742 | } | |
743 | ||
906c8d75 JA |
744 | /* |
745 | * We cannot pass the td data into a forked process, so attach the td and | |
746 | * pass it to the thread worker. | |
747 | */ | |
ebac4655 JA |
748 | static void *fork_main(int shmid, int offset) |
749 | { | |
750 | struct thread_data *td; | |
751 | void *data; | |
752 | ||
753 | data = shmat(shmid, NULL, 0); | |
754 | if (data == (void *) -1) { | |
755 | perror("shmat"); | |
756 | return NULL; | |
757 | } | |
758 | ||
759 | td = data + offset * sizeof(struct thread_data); | |
760 | thread_main(td); | |
761 | shmdt(data); | |
762 | return NULL; | |
763 | } | |
764 | ||
906c8d75 JA |
765 | /* |
766 | * Run over the job map and reap the threads that have exited, if any. | |
767 | */ | |
ebac4655 JA |
768 | static void reap_threads(int *nr_running, int *t_rate, int *m_rate) |
769 | { | |
34572e28 | 770 | struct thread_data *td; |
4d2413c6 | 771 | int i, cputhreads, pending; |
ebac4655 JA |
772 | |
773 | /* | |
774 | * reap exited threads (TD_EXITED -> TD_REAPED) | |
775 | */ | |
4d2413c6 | 776 | pending = cputhreads = 0; |
34572e28 | 777 | for_each_td(td, i) { |
84585003 JA |
778 | /* |
779 | * ->io_ops is NULL for a thread that has closed its | |
780 | * io engine | |
781 | */ | |
782 | if (td->io_ops && td->io_ops->flags & FIO_CPUIO) | |
b990b5c0 JA |
783 | cputhreads++; |
784 | ||
4d2413c6 JA |
785 | if (td->runstate != TD_EXITED) { |
786 | if (td->runstate < TD_RUNNING) | |
787 | pending++; | |
788 | ||
ebac4655 | 789 | continue; |
4d2413c6 | 790 | } |
ebac4655 JA |
791 | |
792 | td_set_runstate(td, TD_REAPED); | |
793 | ||
794 | if (td->use_thread) { | |
795 | long ret; | |
796 | ||
797 | if (pthread_join(td->thread, (void *) &ret)) | |
798 | perror("thread_join"); | |
799 | } else | |
800 | waitpid(td->pid, NULL, 0); | |
801 | ||
802 | (*nr_running)--; | |
803 | (*m_rate) -= td->ratemin; | |
804 | (*t_rate) -= td->rate; | |
805 | } | |
b990b5c0 | 806 | |
4d2413c6 | 807 | if (*nr_running == cputhreads && !pending) |
b990b5c0 | 808 | terminate_threads(TERMINATE_ALL); |
ebac4655 JA |
809 | } |
810 | ||
906c8d75 JA |
811 | /* |
812 | * Main function for kicking off and reaping jobs, as needed. | |
813 | */ | |
ebac4655 JA |
814 | static void run_threads(void) |
815 | { | |
ebac4655 JA |
816 | struct thread_data *td; |
817 | unsigned long spent; | |
818 | int i, todo, nr_running, m_rate, t_rate, nr_started; | |
fcb6ade2 | 819 | |
2f9ade3c JA |
820 | if (fio_pin_memory()) |
821 | return; | |
ebac4655 | 822 | |
c6ae0a5b JA |
823 | if (!terse_output) { |
824 | printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : ""); | |
825 | fflush(stdout); | |
826 | } | |
c04f7ec3 | 827 | |
4efa970e JA |
828 | signal(SIGINT, sig_handler); |
829 | signal(SIGALRM, sig_handler); | |
830 | ||
ebac4655 JA |
831 | todo = thread_number; |
832 | nr_running = 0; | |
833 | nr_started = 0; | |
834 | m_rate = t_rate = 0; | |
835 | ||
34572e28 | 836 | for_each_td(td, i) { |
263e529f | 837 | print_status_init(td->thread_number - 1); |
ebac4655 JA |
838 | |
839 | init_disk_util(td); | |
840 | ||
841 | if (!td->create_serialize) | |
842 | continue; | |
843 | ||
844 | /* | |
845 | * do file setup here so it happens sequentially, | |
846 | * we don't want X number of threads getting their | |
847 | * client data interspersed on disk | |
848 | */ | |
53cdc686 | 849 | if (setup_files(td)) { |
ebac4655 JA |
850 | td_set_runstate(td, TD_REAPED); |
851 | todo--; | |
852 | } | |
853 | } | |
854 | ||
ebac4655 | 855 | while (todo) { |
75154845 JA |
856 | struct thread_data *map[MAX_JOBS]; |
857 | struct timeval this_start; | |
858 | int this_jobs = 0, left; | |
859 | ||
ebac4655 JA |
860 | /* |
861 | * create threads (TD_NOT_CREATED -> TD_CREATED) | |
862 | */ | |
34572e28 | 863 | for_each_td(td, i) { |
ebac4655 JA |
864 | if (td->runstate != TD_NOT_CREATED) |
865 | continue; | |
866 | ||
867 | /* | |
868 | * never got a chance to start, killed by other | |
869 | * thread for some reason | |
870 | */ | |
871 | if (td->terminate) { | |
872 | todo--; | |
873 | continue; | |
874 | } | |
875 | ||
876 | if (td->start_delay) { | |
263e529f | 877 | spent = mtime_since_genesis(); |
ebac4655 JA |
878 | |
879 | if (td->start_delay * 1000 > spent) | |
880 | continue; | |
881 | } | |
882 | ||
883 | if (td->stonewall && (nr_started || nr_running)) | |
884 | break; | |
885 | ||
75154845 JA |
886 | /* |
887 | * Set state to created. Thread will transition | |
888 | * to TD_INITIALIZED when it's done setting up. | |
889 | */ | |
ebac4655 | 890 | td_set_runstate(td, TD_CREATED); |
75154845 | 891 | map[this_jobs++] = td; |
bbfd6b00 | 892 | fio_sem_init(&startup_sem, 1); |
ebac4655 JA |
893 | nr_started++; |
894 | ||
895 | if (td->use_thread) { | |
896 | if (pthread_create(&td->thread, NULL, thread_main, td)) { | |
897 | perror("thread_create"); | |
898 | nr_started--; | |
899 | } | |
900 | } else { | |
901 | if (fork()) | |
bbfd6b00 | 902 | fio_sem_down(&startup_sem); |
ebac4655 JA |
903 | else { |
904 | fork_main(shm_id, i); | |
905 | exit(0); | |
906 | } | |
907 | } | |
908 | } | |
909 | ||
910 | /* | |
75154845 JA |
911 | * Wait for the started threads to transition to |
912 | * TD_INITIALIZED. | |
ebac4655 | 913 | */ |
02bcaa8c | 914 | fio_gettime(&this_start, NULL); |
75154845 JA |
915 | left = this_jobs; |
916 | while (left) { | |
917 | if (mtime_since_now(&this_start) > JOB_START_TIMEOUT) | |
918 | break; | |
919 | ||
920 | usleep(100000); | |
921 | ||
922 | for (i = 0; i < this_jobs; i++) { | |
923 | td = map[i]; | |
924 | if (!td) | |
925 | continue; | |
b6f4d880 | 926 | if (td->runstate == TD_INITIALIZED) { |
75154845 JA |
927 | map[i] = NULL; |
928 | left--; | |
b6f4d880 JA |
929 | } else if (td->runstate >= TD_EXITED) { |
930 | map[i] = NULL; | |
931 | left--; | |
932 | todo--; | |
933 | nr_running++; /* work-around... */ | |
75154845 JA |
934 | } |
935 | } | |
936 | } | |
937 | ||
938 | if (left) { | |
3b70d7e5 | 939 | log_err("fio: %d jobs failed to start\n", left); |
75154845 JA |
940 | for (i = 0; i < this_jobs; i++) { |
941 | td = map[i]; | |
942 | if (!td) | |
943 | continue; | |
944 | kill(td->pid, SIGTERM); | |
945 | } | |
946 | break; | |
947 | } | |
948 | ||
949 | /* | |
b6f4d880 | 950 | * start created threads (TD_INITIALIZED -> TD_RUNNING). |
75154845 | 951 | */ |
34572e28 | 952 | for_each_td(td, i) { |
75154845 | 953 | if (td->runstate != TD_INITIALIZED) |
ebac4655 JA |
954 | continue; |
955 | ||
956 | td_set_runstate(td, TD_RUNNING); | |
957 | nr_running++; | |
958 | nr_started--; | |
959 | m_rate += td->ratemin; | |
960 | t_rate += td->rate; | |
75154845 | 961 | todo--; |
bbfd6b00 | 962 | fio_sem_up(&td->mutex); |
ebac4655 JA |
963 | } |
964 | ||
965 | reap_threads(&nr_running, &t_rate, &m_rate); | |
966 | ||
967 | if (todo) | |
968 | usleep(100000); | |
969 | } | |
970 | ||
971 | while (nr_running) { | |
972 | reap_threads(&nr_running, &t_rate, &m_rate); | |
973 | usleep(10000); | |
974 | } | |
975 | ||
976 | update_io_ticks(); | |
2f9ade3c | 977 | fio_unpin_memory(); |
ebac4655 JA |
978 | } |
979 | ||
ebac4655 JA |
980 | int main(int argc, char *argv[]) |
981 | { | |
982 | if (parse_options(argc, argv)) | |
983 | return 1; | |
984 | ||
985 | if (!thread_number) { | |
3b70d7e5 | 986 | log_err("Nothing to do\n"); |
ebac4655 JA |
987 | return 1; |
988 | } | |
989 | ||
990 | disk_util_timer_arm(); | |
991 | ||
992 | run_threads(); | |
993 | show_run_stats(); | |
994 | ||
995 | return 0; | |
996 | } |