Commit | Line | Data |
---|---|---|
2e1df07d JA |
1 | /* |
2 | * fio - the flexible io tester | |
3 | * | |
4 | * Copyright (C) 2005 Jens Axboe <axboe@suse.de> | |
5 | * Copyright (C) 2006-2012 Jens Axboe <axboe@kernel.dk> | |
6 | * | |
7 | * The license below covers all files distributed with fio unless otherwise | |
8 | * noted in the file itself. | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License version 2 as | |
12 | * published by the Free Software Foundation. | |
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 | */ | |
24 | #include <unistd.h> | |
25 | #include <fcntl.h> | |
26 | #include <string.h> | |
27 | #include <limits.h> | |
28 | #include <signal.h> | |
29 | #include <time.h> | |
30 | #include <locale.h> | |
31 | #include <assert.h> | |
32 | #include <time.h> | |
e43606c2 | 33 | #include <inttypes.h> |
2e1df07d JA |
34 | #include <sys/stat.h> |
35 | #include <sys/wait.h> | |
36 | #include <sys/ipc.h> | |
37 | #include <sys/shm.h> | |
38 | #include <sys/mman.h> | |
39 | ||
40 | #include "fio.h" | |
41 | #include "hash.h" | |
42 | #include "smalloc.h" | |
43 | #include "verify.h" | |
44 | #include "trim.h" | |
45 | #include "diskutil.h" | |
46 | #include "cgroup.h" | |
47 | #include "profile.h" | |
48 | #include "lib/rand.h" | |
49 | #include "memalign.h" | |
50 | #include "server.h" | |
51 | ||
52 | static pthread_t disk_util_thread; | |
53 | static struct fio_mutex *startup_mutex; | |
54 | static struct fio_mutex *writeout_mutex; | |
55 | static struct flist_head *cgroup_list; | |
56 | static char *cgroup_mnt; | |
57 | static int exit_value; | |
58 | static volatile int fio_abort; | |
59 | ||
60 | struct io_log *agg_io_log[2]; | |
61 | ||
a3efc919 JA |
62 | int groupid = 0; |
63 | unsigned int thread_number = 0; | |
64 | unsigned int nr_process = 0; | |
65 | unsigned int nr_thread = 0; | |
66 | int shm_id = 0; | |
67 | int temp_stall_ts; | |
68 | unsigned long done_secs = 0; | |
69 | ||
2e1df07d | 70 | #define PAGE_ALIGN(buf) \ |
e43606c2 | 71 | (char *) (((uintptr_t) (buf) + page_mask) & ~page_mask) |
2e1df07d JA |
72 | |
73 | #define JOB_START_TIMEOUT (5 * 1000) | |
74 | ||
75 | static void sig_int(int sig) | |
76 | { | |
77 | if (threads) { | |
78 | if (is_backend) | |
79 | fio_server_got_signal(sig); | |
80 | else { | |
81 | log_info("\nfio: terminating on signal %d\n", sig); | |
82 | fflush(stdout); | |
83 | exit_value = 128; | |
84 | } | |
85 | ||
86 | fio_terminate_threads(TERMINATE_ALL); | |
87 | } | |
88 | } | |
89 | ||
90 | static void set_sig_handlers(void) | |
91 | { | |
92 | struct sigaction act; | |
93 | ||
94 | memset(&act, 0, sizeof(act)); | |
95 | act.sa_handler = sig_int; | |
96 | act.sa_flags = SA_RESTART; | |
97 | sigaction(SIGINT, &act, NULL); | |
98 | ||
99 | memset(&act, 0, sizeof(act)); | |
100 | act.sa_handler = sig_int; | |
101 | act.sa_flags = SA_RESTART; | |
102 | sigaction(SIGTERM, &act, NULL); | |
103 | ||
104 | if (is_backend) { | |
105 | memset(&act, 0, sizeof(act)); | |
106 | act.sa_handler = sig_int; | |
107 | act.sa_flags = SA_RESTART; | |
108 | sigaction(SIGPIPE, &act, NULL); | |
109 | } | |
110 | } | |
111 | ||
112 | /* | |
113 | * Check if we are above the minimum rate given. | |
114 | */ | |
115 | static int __check_min_rate(struct thread_data *td, struct timeval *now, | |
116 | enum fio_ddir ddir) | |
117 | { | |
118 | unsigned long long bytes = 0; | |
119 | unsigned long iops = 0; | |
120 | unsigned long spent; | |
121 | unsigned long rate; | |
122 | unsigned int ratemin = 0; | |
123 | unsigned int rate_iops = 0; | |
124 | unsigned int rate_iops_min = 0; | |
125 | ||
126 | assert(ddir_rw(ddir)); | |
127 | ||
128 | if (!td->o.ratemin[ddir] && !td->o.rate_iops_min[ddir]) | |
129 | return 0; | |
130 | ||
131 | /* | |
132 | * allow a 2 second settle period in the beginning | |
133 | */ | |
134 | if (mtime_since(&td->start, now) < 2000) | |
135 | return 0; | |
136 | ||
137 | iops += td->this_io_blocks[ddir]; | |
138 | bytes += td->this_io_bytes[ddir]; | |
139 | ratemin += td->o.ratemin[ddir]; | |
140 | rate_iops += td->o.rate_iops[ddir]; | |
141 | rate_iops_min += td->o.rate_iops_min[ddir]; | |
142 | ||
143 | /* | |
144 | * if rate blocks is set, sample is running | |
145 | */ | |
146 | if (td->rate_bytes[ddir] || td->rate_blocks[ddir]) { | |
147 | spent = mtime_since(&td->lastrate[ddir], now); | |
148 | if (spent < td->o.ratecycle) | |
149 | return 0; | |
150 | ||
151 | if (td->o.rate[ddir]) { | |
152 | /* | |
153 | * check bandwidth specified rate | |
154 | */ | |
155 | if (bytes < td->rate_bytes[ddir]) { | |
156 | log_err("%s: min rate %u not met\n", td->o.name, | |
157 | ratemin); | |
158 | return 1; | |
159 | } else { | |
160 | rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent; | |
161 | if (rate < ratemin || | |
162 | bytes < td->rate_bytes[ddir]) { | |
163 | log_err("%s: min rate %u not met, got" | |
164 | " %luKB/sec\n", td->o.name, | |
165 | ratemin, rate); | |
166 | return 1; | |
167 | } | |
168 | } | |
169 | } else { | |
170 | /* | |
171 | * checks iops specified rate | |
172 | */ | |
173 | if (iops < rate_iops) { | |
174 | log_err("%s: min iops rate %u not met\n", | |
175 | td->o.name, rate_iops); | |
176 | return 1; | |
177 | } else { | |
178 | rate = ((iops - td->rate_blocks[ddir]) * 1000) / spent; | |
179 | if (rate < rate_iops_min || | |
180 | iops < td->rate_blocks[ddir]) { | |
181 | log_err("%s: min iops rate %u not met," | |
182 | " got %lu\n", td->o.name, | |
183 | rate_iops_min, rate); | |
184 | } | |
185 | } | |
186 | } | |
187 | } | |
188 | ||
189 | td->rate_bytes[ddir] = bytes; | |
190 | td->rate_blocks[ddir] = iops; | |
191 | memcpy(&td->lastrate[ddir], now, sizeof(*now)); | |
192 | return 0; | |
193 | } | |
194 | ||
195 | static int check_min_rate(struct thread_data *td, struct timeval *now, | |
196 | unsigned long *bytes_done) | |
197 | { | |
198 | int ret = 0; | |
199 | ||
200 | if (bytes_done[0]) | |
201 | ret |= __check_min_rate(td, now, 0); | |
202 | if (bytes_done[1]) | |
203 | ret |= __check_min_rate(td, now, 1); | |
204 | ||
205 | return ret; | |
206 | } | |
207 | ||
208 | /* | |
209 | * When job exits, we can cancel the in-flight IO if we are using async | |
210 | * io. Attempt to do so. | |
211 | */ | |
212 | static void cleanup_pending_aio(struct thread_data *td) | |
213 | { | |
214 | struct flist_head *entry, *n; | |
215 | struct io_u *io_u; | |
216 | int r; | |
217 | ||
218 | /* | |
219 | * get immediately available events, if any | |
220 | */ | |
221 | r = io_u_queued_complete(td, 0, NULL); | |
222 | if (r < 0) | |
223 | return; | |
224 | ||
225 | /* | |
226 | * now cancel remaining active events | |
227 | */ | |
228 | if (td->io_ops->cancel) { | |
229 | flist_for_each_safe(entry, n, &td->io_u_busylist) { | |
230 | io_u = flist_entry(entry, struct io_u, list); | |
231 | ||
232 | /* | |
233 | * if the io_u isn't in flight, then that generally | |
234 | * means someone leaked an io_u. complain but fix | |
235 | * it up, so we don't stall here. | |
236 | */ | |
237 | if ((io_u->flags & IO_U_F_FLIGHT) == 0) { | |
238 | log_err("fio: non-busy IO on busy list\n"); | |
239 | put_io_u(td, io_u); | |
240 | } else { | |
241 | r = td->io_ops->cancel(td, io_u); | |
242 | if (!r) | |
243 | put_io_u(td, io_u); | |
244 | } | |
245 | } | |
246 | } | |
247 | ||
248 | if (td->cur_depth) | |
249 | r = io_u_queued_complete(td, td->cur_depth, NULL); | |
250 | } | |
251 | ||
252 | /* | |
253 | * Helper to handle the final sync of a file. Works just like the normal | |
254 | * io path, just does everything sync. | |
255 | */ | |
256 | static int fio_io_sync(struct thread_data *td, struct fio_file *f) | |
257 | { | |
258 | struct io_u *io_u = __get_io_u(td); | |
259 | int ret; | |
260 | ||
261 | if (!io_u) | |
262 | return 1; | |
263 | ||
264 | io_u->ddir = DDIR_SYNC; | |
265 | io_u->file = f; | |
266 | ||
267 | if (td_io_prep(td, io_u)) { | |
268 | put_io_u(td, io_u); | |
269 | return 1; | |
270 | } | |
271 | ||
272 | requeue: | |
273 | ret = td_io_queue(td, io_u); | |
274 | if (ret < 0) { | |
275 | td_verror(td, io_u->error, "td_io_queue"); | |
276 | put_io_u(td, io_u); | |
277 | return 1; | |
278 | } else if (ret == FIO_Q_QUEUED) { | |
279 | if (io_u_queued_complete(td, 1, NULL) < 0) | |
280 | return 1; | |
281 | } else if (ret == FIO_Q_COMPLETED) { | |
282 | if (io_u->error) { | |
283 | td_verror(td, io_u->error, "td_io_queue"); | |
284 | return 1; | |
285 | } | |
286 | ||
287 | if (io_u_sync_complete(td, io_u, NULL) < 0) | |
288 | return 1; | |
289 | } else if (ret == FIO_Q_BUSY) { | |
290 | if (td_io_commit(td)) | |
291 | return 1; | |
292 | goto requeue; | |
293 | } | |
294 | ||
295 | return 0; | |
296 | } | |
a3efc919 | 297 | |
2e1df07d JA |
298 | static inline void __update_tv_cache(struct thread_data *td) |
299 | { | |
300 | fio_gettime(&td->tv_cache, NULL); | |
301 | } | |
302 | ||
303 | static inline void update_tv_cache(struct thread_data *td) | |
304 | { | |
305 | if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask) | |
306 | __update_tv_cache(td); | |
307 | } | |
308 | ||
309 | static inline int runtime_exceeded(struct thread_data *td, struct timeval *t) | |
310 | { | |
311 | if (in_ramp_time(td)) | |
312 | return 0; | |
313 | if (!td->o.timeout) | |
314 | return 0; | |
315 | if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000) | |
316 | return 1; | |
317 | ||
318 | return 0; | |
319 | } | |
320 | ||
321 | static int break_on_this_error(struct thread_data *td, enum fio_ddir ddir, | |
322 | int *retptr) | |
323 | { | |
324 | int ret = *retptr; | |
325 | ||
326 | if (ret < 0 || td->error) { | |
327 | int err; | |
328 | ||
329 | if (ret < 0) | |
330 | err = -ret; | |
331 | else | |
332 | err = td->error; | |
333 | ||
334 | if (!(td->o.continue_on_error & td_error_type(ddir, err))) | |
335 | return 1; | |
336 | ||
337 | if (td_non_fatal_error(err)) { | |
338 | /* | |
339 | * Continue with the I/Os in case of | |
340 | * a non fatal error. | |
341 | */ | |
342 | update_error_count(td, err); | |
343 | td_clear_error(td); | |
344 | *retptr = 0; | |
345 | return 0; | |
346 | } else if (td->o.fill_device && err == ENOSPC) { | |
347 | /* | |
348 | * We expect to hit this error if | |
349 | * fill_device option is set. | |
350 | */ | |
351 | td_clear_error(td); | |
352 | td->terminate = 1; | |
353 | return 1; | |
354 | } else { | |
355 | /* | |
356 | * Stop the I/O in case of a fatal | |
357 | * error. | |
358 | */ | |
359 | update_error_count(td, err); | |
360 | return 1; | |
361 | } | |
362 | } | |
363 | ||
364 | return 0; | |
365 | } | |
366 | ||
2e1df07d JA |
367 | /* |
368 | * The main verify engine. Runs over the writes we previously submitted, | |
369 | * reads the blocks back in, and checks the crc/md5 of the data. | |
370 | */ | |
371 | static void do_verify(struct thread_data *td) | |
372 | { | |
373 | struct fio_file *f; | |
374 | struct io_u *io_u; | |
375 | int ret, min_events; | |
376 | unsigned int i; | |
377 | ||
378 | dprint(FD_VERIFY, "starting loop\n"); | |
379 | ||
380 | /* | |
381 | * sync io first and invalidate cache, to make sure we really | |
382 | * read from disk. | |
383 | */ | |
384 | for_each_file(td, f, i) { | |
385 | if (!fio_file_open(f)) | |
386 | continue; | |
387 | if (fio_io_sync(td, f)) | |
388 | break; | |
389 | if (file_invalidate_cache(td, f)) | |
390 | break; | |
391 | } | |
392 | ||
393 | if (td->error) | |
394 | return; | |
395 | ||
396 | td_set_runstate(td, TD_VERIFYING); | |
397 | ||
398 | io_u = NULL; | |
399 | while (!td->terminate) { | |
400 | int ret2, full; | |
401 | ||
402 | update_tv_cache(td); | |
403 | ||
404 | if (runtime_exceeded(td, &td->tv_cache)) { | |
405 | __update_tv_cache(td); | |
406 | if (runtime_exceeded(td, &td->tv_cache)) { | |
407 | td->terminate = 1; | |
408 | break; | |
409 | } | |
410 | } | |
411 | ||
412 | io_u = __get_io_u(td); | |
413 | if (!io_u) | |
414 | break; | |
415 | ||
416 | if (get_next_verify(td, io_u)) { | |
417 | put_io_u(td, io_u); | |
418 | break; | |
419 | } | |
420 | ||
421 | if (td_io_prep(td, io_u)) { | |
422 | put_io_u(td, io_u); | |
423 | break; | |
424 | } | |
425 | ||
426 | if (td->o.verify_async) | |
427 | io_u->end_io = verify_io_u_async; | |
428 | else | |
429 | io_u->end_io = verify_io_u; | |
430 | ||
431 | ret = td_io_queue(td, io_u); | |
432 | switch (ret) { | |
433 | case FIO_Q_COMPLETED: | |
434 | if (io_u->error) { | |
435 | ret = -io_u->error; | |
436 | clear_io_u(td, io_u); | |
437 | } else if (io_u->resid) { | |
438 | int bytes = io_u->xfer_buflen - io_u->resid; | |
439 | ||
440 | /* | |
441 | * zero read, fail | |
442 | */ | |
443 | if (!bytes) { | |
444 | td_verror(td, EIO, "full resid"); | |
445 | put_io_u(td, io_u); | |
446 | break; | |
447 | } | |
448 | ||
449 | io_u->xfer_buflen = io_u->resid; | |
450 | io_u->xfer_buf += bytes; | |
451 | io_u->offset += bytes; | |
452 | ||
453 | if (ddir_rw(io_u->ddir)) | |
454 | td->ts.short_io_u[io_u->ddir]++; | |
455 | ||
456 | f = io_u->file; | |
457 | if (io_u->offset == f->real_file_size) | |
458 | goto sync_done; | |
459 | ||
460 | requeue_io_u(td, &io_u); | |
461 | } else { | |
462 | sync_done: | |
463 | ret = io_u_sync_complete(td, io_u, NULL); | |
464 | if (ret < 0) | |
465 | break; | |
466 | } | |
467 | continue; | |
468 | case FIO_Q_QUEUED: | |
469 | break; | |
470 | case FIO_Q_BUSY: | |
471 | requeue_io_u(td, &io_u); | |
472 | ret2 = td_io_commit(td); | |
473 | if (ret2 < 0) | |
474 | ret = ret2; | |
475 | break; | |
476 | default: | |
477 | assert(ret < 0); | |
478 | td_verror(td, -ret, "td_io_queue"); | |
479 | break; | |
480 | } | |
481 | ||
482 | if (break_on_this_error(td, io_u->ddir, &ret)) | |
483 | break; | |
484 | ||
485 | /* | |
486 | * if we can queue more, do so. but check if there are | |
487 | * completed io_u's first. Note that we can get BUSY even | |
488 | * without IO queued, if the system is resource starved. | |
489 | */ | |
490 | full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth); | |
491 | if (full || !td->o.iodepth_batch_complete) { | |
492 | min_events = min(td->o.iodepth_batch_complete, | |
493 | td->cur_depth); | |
494 | if (full && !min_events && td->o.iodepth_batch_complete != 0) | |
495 | min_events = 1; | |
496 | ||
497 | do { | |
498 | /* | |
499 | * Reap required number of io units, if any, | |
500 | * and do the verification on them through | |
501 | * the callback handler | |
502 | */ | |
503 | if (io_u_queued_complete(td, min_events, NULL) < 0) { | |
504 | ret = -1; | |
505 | break; | |
506 | } | |
507 | } while (full && (td->cur_depth > td->o.iodepth_low)); | |
508 | } | |
509 | if (ret < 0) | |
510 | break; | |
511 | } | |
512 | ||
513 | if (!td->error) { | |
514 | min_events = td->cur_depth; | |
515 | ||
516 | if (min_events) | |
517 | ret = io_u_queued_complete(td, min_events, NULL); | |
518 | } else | |
519 | cleanup_pending_aio(td); | |
520 | ||
521 | td_set_runstate(td, TD_RUNNING); | |
522 | ||
523 | dprint(FD_VERIFY, "exiting loop\n"); | |
524 | } | |
525 | ||
526 | /* | |
527 | * Main IO worker function. It retrieves io_u's to process and queues | |
528 | * and reaps them, checking for rate and errors along the way. | |
529 | */ | |
530 | static void do_io(struct thread_data *td) | |
531 | { | |
532 | unsigned int i; | |
533 | int ret = 0; | |
534 | ||
535 | if (in_ramp_time(td)) | |
536 | td_set_runstate(td, TD_RAMP); | |
537 | else | |
538 | td_set_runstate(td, TD_RUNNING); | |
539 | ||
540 | while ( (td->o.read_iolog_file && !flist_empty(&td->io_log_list)) || | |
541 | (!flist_empty(&td->trim_list)) || | |
542 | ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) ) { | |
543 | struct timeval comp_time; | |
544 | unsigned long bytes_done[2] = { 0, 0 }; | |
545 | int min_evts = 0; | |
546 | struct io_u *io_u; | |
547 | int ret2, full; | |
548 | enum fio_ddir ddir; | |
549 | ||
550 | if (td->terminate) | |
551 | break; | |
552 | ||
553 | update_tv_cache(td); | |
554 | ||
555 | if (runtime_exceeded(td, &td->tv_cache)) { | |
556 | __update_tv_cache(td); | |
557 | if (runtime_exceeded(td, &td->tv_cache)) { | |
558 | td->terminate = 1; | |
559 | break; | |
560 | } | |
561 | } | |
562 | ||
563 | io_u = get_io_u(td); | |
564 | if (!io_u) | |
565 | break; | |
566 | ||
567 | ddir = io_u->ddir; | |
568 | ||
569 | /* | |
570 | * Add verification end_io handler, if asked to verify | |
571 | * a previously written file. | |
572 | */ | |
573 | if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ && | |
574 | !td_rw(td)) { | |
575 | if (td->o.verify_async) | |
576 | io_u->end_io = verify_io_u_async; | |
577 | else | |
578 | io_u->end_io = verify_io_u; | |
579 | td_set_runstate(td, TD_VERIFYING); | |
580 | } else if (in_ramp_time(td)) | |
581 | td_set_runstate(td, TD_RAMP); | |
582 | else | |
583 | td_set_runstate(td, TD_RUNNING); | |
584 | ||
585 | ret = td_io_queue(td, io_u); | |
586 | switch (ret) { | |
587 | case FIO_Q_COMPLETED: | |
588 | if (io_u->error) { | |
589 | ret = -io_u->error; | |
590 | clear_io_u(td, io_u); | |
591 | } else if (io_u->resid) { | |
592 | int bytes = io_u->xfer_buflen - io_u->resid; | |
593 | struct fio_file *f = io_u->file; | |
594 | ||
595 | /* | |
596 | * zero read, fail | |
597 | */ | |
598 | if (!bytes) { | |
599 | td_verror(td, EIO, "full resid"); | |
600 | put_io_u(td, io_u); | |
601 | break; | |
602 | } | |
603 | ||
604 | io_u->xfer_buflen = io_u->resid; | |
605 | io_u->xfer_buf += bytes; | |
606 | io_u->offset += bytes; | |
607 | ||
608 | if (ddir_rw(io_u->ddir)) | |
609 | td->ts.short_io_u[io_u->ddir]++; | |
610 | ||
611 | if (io_u->offset == f->real_file_size) | |
612 | goto sync_done; | |
613 | ||
614 | requeue_io_u(td, &io_u); | |
615 | } else { | |
616 | sync_done: | |
617 | if (__should_check_rate(td, 0) || | |
618 | __should_check_rate(td, 1)) | |
619 | fio_gettime(&comp_time, NULL); | |
620 | ||
621 | ret = io_u_sync_complete(td, io_u, bytes_done); | |
622 | if (ret < 0) | |
623 | break; | |
624 | } | |
625 | break; | |
626 | case FIO_Q_QUEUED: | |
627 | /* | |
628 | * if the engine doesn't have a commit hook, | |
629 | * the io_u is really queued. if it does have such | |
630 | * a hook, it has to call io_u_queued() itself. | |
631 | */ | |
632 | if (td->io_ops->commit == NULL) | |
633 | io_u_queued(td, io_u); | |
634 | break; | |
635 | case FIO_Q_BUSY: | |
636 | requeue_io_u(td, &io_u); | |
637 | ret2 = td_io_commit(td); | |
638 | if (ret2 < 0) | |
639 | ret = ret2; | |
640 | break; | |
641 | default: | |
642 | assert(ret < 0); | |
643 | put_io_u(td, io_u); | |
644 | break; | |
645 | } | |
646 | ||
647 | if (break_on_this_error(td, ddir, &ret)) | |
648 | break; | |
649 | ||
650 | /* | |
651 | * See if we need to complete some commands. Note that we | |
652 | * can get BUSY even without IO queued, if the system is | |
653 | * resource starved. | |
654 | */ | |
655 | full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth); | |
656 | if (full || !td->o.iodepth_batch_complete) { | |
657 | min_evts = min(td->o.iodepth_batch_complete, | |
658 | td->cur_depth); | |
659 | if (full && !min_evts && td->o.iodepth_batch_complete != 0) | |
660 | min_evts = 1; | |
661 | ||
662 | if (__should_check_rate(td, 0) || | |
663 | __should_check_rate(td, 1)) | |
664 | fio_gettime(&comp_time, NULL); | |
665 | ||
666 | do { | |
667 | ret = io_u_queued_complete(td, min_evts, bytes_done); | |
668 | if (ret < 0) | |
669 | break; | |
670 | ||
671 | } while (full && (td->cur_depth > td->o.iodepth_low)); | |
672 | } | |
673 | ||
674 | if (ret < 0) | |
675 | break; | |
676 | if (!(bytes_done[0] + bytes_done[1])) | |
677 | continue; | |
678 | ||
679 | if (!in_ramp_time(td) && should_check_rate(td, bytes_done)) { | |
680 | if (check_min_rate(td, &comp_time, bytes_done)) { | |
681 | if (exitall_on_terminate) | |
682 | fio_terminate_threads(td->groupid); | |
683 | td_verror(td, EIO, "check_min_rate"); | |
684 | break; | |
685 | } | |
686 | } | |
687 | ||
688 | if (td->o.thinktime) { | |
689 | unsigned long long b; | |
690 | ||
691 | b = td->io_blocks[0] + td->io_blocks[1]; | |
692 | if (!(b % td->o.thinktime_blocks)) { | |
693 | int left; | |
694 | ||
695 | if (td->o.thinktime_spin) | |
696 | usec_spin(td->o.thinktime_spin); | |
697 | ||
698 | left = td->o.thinktime - td->o.thinktime_spin; | |
699 | if (left) | |
700 | usec_sleep(td, left); | |
701 | } | |
702 | } | |
703 | } | |
704 | ||
705 | if (td->trim_entries) | |
706 | log_err("fio: %d trim entries leaked?\n", td->trim_entries); | |
707 | ||
708 | if (td->o.fill_device && td->error == ENOSPC) { | |
709 | td->error = 0; | |
710 | td->terminate = 1; | |
711 | } | |
712 | if (!td->error) { | |
713 | struct fio_file *f; | |
714 | ||
715 | i = td->cur_depth; | |
716 | if (i) { | |
717 | ret = io_u_queued_complete(td, i, NULL); | |
718 | if (td->o.fill_device && td->error == ENOSPC) | |
719 | td->error = 0; | |
720 | } | |
721 | ||
722 | if (should_fsync(td) && td->o.end_fsync) { | |
723 | td_set_runstate(td, TD_FSYNCING); | |
724 | ||
725 | for_each_file(td, f, i) { | |
726 | if (!fio_file_open(f)) | |
727 | continue; | |
728 | fio_io_sync(td, f); | |
729 | } | |
730 | } | |
731 | } else | |
732 | cleanup_pending_aio(td); | |
733 | ||
734 | /* | |
735 | * stop job if we failed doing any IO | |
736 | */ | |
737 | if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0) | |
738 | td->done = 1; | |
739 | } | |
740 | ||
741 | static void cleanup_io_u(struct thread_data *td) | |
742 | { | |
743 | struct flist_head *entry, *n; | |
744 | struct io_u *io_u; | |
745 | ||
746 | flist_for_each_safe(entry, n, &td->io_u_freelist) { | |
747 | io_u = flist_entry(entry, struct io_u, list); | |
748 | ||
749 | flist_del(&io_u->list); | |
750 | fio_memfree(io_u, sizeof(*io_u)); | |
751 | } | |
752 | ||
753 | free_io_mem(td); | |
754 | } | |
755 | ||
756 | static int init_io_u(struct thread_data *td) | |
757 | { | |
758 | struct io_u *io_u; | |
759 | unsigned int max_bs; | |
760 | int cl_align, i, max_units; | |
761 | char *p; | |
762 | ||
763 | max_units = td->o.iodepth; | |
764 | max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]); | |
765 | td->orig_buffer_size = (unsigned long long) max_bs | |
766 | * (unsigned long long) max_units; | |
767 | ||
768 | if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) { | |
769 | unsigned long bs; | |
770 | ||
771 | bs = td->orig_buffer_size + td->o.hugepage_size - 1; | |
772 | td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1); | |
773 | } | |
774 | ||
775 | if (td->orig_buffer_size != (size_t) td->orig_buffer_size) { | |
776 | log_err("fio: IO memory too large. Reduce max_bs or iodepth\n"); | |
777 | return 1; | |
778 | } | |
779 | ||
780 | if (allocate_io_mem(td)) | |
781 | return 1; | |
782 | ||
783 | if (td->o.odirect || td->o.mem_align || | |
784 | (td->io_ops->flags & FIO_RAWIO)) | |
785 | p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align; | |
786 | else | |
787 | p = td->orig_buffer; | |
788 | ||
789 | cl_align = os_cache_line_size(); | |
790 | ||
791 | for (i = 0; i < max_units; i++) { | |
792 | void *ptr; | |
793 | ||
794 | if (td->terminate) | |
795 | return 1; | |
796 | ||
797 | ptr = fio_memalign(cl_align, sizeof(*io_u)); | |
798 | if (!ptr) { | |
799 | log_err("fio: unable to allocate aligned memory\n"); | |
800 | break; | |
801 | } | |
802 | ||
803 | io_u = ptr; | |
804 | memset(io_u, 0, sizeof(*io_u)); | |
805 | INIT_FLIST_HEAD(&io_u->list); | |
806 | dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i); | |
807 | ||
808 | if (!(td->io_ops->flags & FIO_NOIO)) { | |
809 | io_u->buf = p; | |
810 | dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf); | |
811 | ||
812 | if (td_write(td)) | |
813 | io_u_fill_buffer(td, io_u, max_bs); | |
814 | if (td_write(td) && td->o.verify_pattern_bytes) { | |
815 | /* | |
816 | * Fill the buffer with the pattern if we are | |
817 | * going to be doing writes. | |
818 | */ | |
819 | fill_pattern(td, io_u->buf, max_bs, io_u, 0, 0); | |
820 | } | |
821 | } | |
822 | ||
823 | io_u->index = i; | |
824 | io_u->flags = IO_U_F_FREE; | |
825 | flist_add(&io_u->list, &td->io_u_freelist); | |
826 | p += max_bs; | |
827 | } | |
828 | ||
829 | return 0; | |
830 | } | |
831 | ||
832 | static int switch_ioscheduler(struct thread_data *td) | |
833 | { | |
834 | char tmp[256], tmp2[128]; | |
835 | FILE *f; | |
836 | int ret; | |
837 | ||
838 | if (td->io_ops->flags & FIO_DISKLESSIO) | |
839 | return 0; | |
840 | ||
841 | sprintf(tmp, "%s/queue/scheduler", td->sysfs_root); | |
842 | ||
843 | f = fopen(tmp, "r+"); | |
844 | if (!f) { | |
845 | if (errno == ENOENT) { | |
846 | log_err("fio: os or kernel doesn't support IO scheduler" | |
847 | " switching\n"); | |
848 | return 0; | |
849 | } | |
850 | td_verror(td, errno, "fopen iosched"); | |
851 | return 1; | |
852 | } | |
853 | ||
854 | /* | |
855 | * Set io scheduler. | |
856 | */ | |
857 | ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f); | |
858 | if (ferror(f) || ret != 1) { | |
859 | td_verror(td, errno, "fwrite"); | |
860 | fclose(f); | |
861 | return 1; | |
862 | } | |
863 | ||
864 | rewind(f); | |
865 | ||
866 | /* | |
867 | * Read back and check that the selected scheduler is now the default. | |
868 | */ | |
869 | ret = fread(tmp, 1, sizeof(tmp), f); | |
870 | if (ferror(f) || ret < 0) { | |
871 | td_verror(td, errno, "fread"); | |
872 | fclose(f); | |
873 | return 1; | |
874 | } | |
875 | ||
876 | sprintf(tmp2, "[%s]", td->o.ioscheduler); | |
877 | if (!strstr(tmp, tmp2)) { | |
878 | log_err("fio: io scheduler %s not found\n", td->o.ioscheduler); | |
879 | td_verror(td, EINVAL, "iosched_switch"); | |
880 | fclose(f); | |
881 | return 1; | |
882 | } | |
883 | ||
884 | fclose(f); | |
885 | return 0; | |
886 | } | |
887 | ||
888 | static int keep_running(struct thread_data *td) | |
889 | { | |
890 | unsigned long long io_done; | |
891 | ||
892 | if (td->done) | |
893 | return 0; | |
894 | if (td->o.time_based) | |
895 | return 1; | |
896 | if (td->o.loops) { | |
897 | td->o.loops--; | |
898 | return 1; | |
899 | } | |
900 | ||
901 | io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE] | |
902 | + td->io_skip_bytes; | |
903 | if (io_done < td->o.size) | |
904 | return 1; | |
905 | ||
906 | return 0; | |
907 | } | |
908 | ||
909 | static int exec_string(const char *string) | |
910 | { | |
911 | int ret, newlen = strlen(string) + 1 + 8; | |
912 | char *str; | |
913 | ||
914 | str = malloc(newlen); | |
915 | sprintf(str, "sh -c %s", string); | |
916 | ||
917 | ret = system(str); | |
918 | if (ret == -1) | |
919 | log_err("fio: exec of cmd <%s> failed\n", str); | |
920 | ||
921 | free(str); | |
922 | return ret; | |
923 | } | |
924 | ||
925 | /* | |
926 | * Entry point for the thread based jobs. The process based jobs end up | |
927 | * here as well, after a little setup. | |
928 | */ | |
929 | static void *thread_main(void *data) | |
930 | { | |
931 | unsigned long long elapsed; | |
932 | struct thread_data *td = data; | |
933 | pthread_condattr_t attr; | |
934 | int clear_state; | |
935 | ||
936 | if (!td->o.use_thread) { | |
937 | setsid(); | |
938 | td->pid = getpid(); | |
939 | } else | |
940 | td->pid = gettid(); | |
941 | ||
942 | dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid); | |
943 | ||
944 | INIT_FLIST_HEAD(&td->io_u_freelist); | |
945 | INIT_FLIST_HEAD(&td->io_u_busylist); | |
946 | INIT_FLIST_HEAD(&td->io_u_requeues); | |
947 | INIT_FLIST_HEAD(&td->io_log_list); | |
948 | INIT_FLIST_HEAD(&td->io_hist_list); | |
949 | INIT_FLIST_HEAD(&td->verify_list); | |
950 | INIT_FLIST_HEAD(&td->trim_list); | |
951 | pthread_mutex_init(&td->io_u_lock, NULL); | |
952 | td->io_hist_tree = RB_ROOT; | |
953 | ||
954 | pthread_condattr_init(&attr); | |
955 | pthread_cond_init(&td->verify_cond, &attr); | |
956 | pthread_cond_init(&td->free_cond, &attr); | |
957 | ||
958 | td_set_runstate(td, TD_INITIALIZED); | |
959 | dprint(FD_MUTEX, "up startup_mutex\n"); | |
960 | fio_mutex_up(startup_mutex); | |
961 | dprint(FD_MUTEX, "wait on td->mutex\n"); | |
962 | fio_mutex_down(td->mutex); | |
963 | dprint(FD_MUTEX, "done waiting on td->mutex\n"); | |
964 | ||
965 | /* | |
966 | * the ->mutex mutex is now no longer used, close it to avoid | |
967 | * eating a file descriptor | |
968 | */ | |
969 | fio_mutex_remove(td->mutex); | |
970 | ||
971 | /* | |
972 | * A new gid requires privilege, so we need to do this before setting | |
973 | * the uid. | |
974 | */ | |
975 | if (td->o.gid != -1U && setgid(td->o.gid)) { | |
976 | td_verror(td, errno, "setgid"); | |
977 | goto err; | |
978 | } | |
979 | if (td->o.uid != -1U && setuid(td->o.uid)) { | |
980 | td_verror(td, errno, "setuid"); | |
981 | goto err; | |
982 | } | |
983 | ||
984 | /* | |
985 | * If we have a gettimeofday() thread, make sure we exclude that | |
986 | * thread from this job | |
987 | */ | |
988 | if (td->o.gtod_cpu) | |
989 | fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu); | |
990 | ||
991 | /* | |
992 | * Set affinity first, in case it has an impact on the memory | |
993 | * allocations. | |
994 | */ | |
995 | if (td->o.cpumask_set && fio_setaffinity(td->pid, td->o.cpumask) == -1) { | |
996 | td_verror(td, errno, "cpu_set_affinity"); | |
997 | goto err; | |
998 | } | |
999 | ||
1000 | /* | |
1001 | * May alter parameters that init_io_u() will use, so we need to | |
1002 | * do this first. | |
1003 | */ | |
1004 | if (init_iolog(td)) | |
1005 | goto err; | |
1006 | ||
1007 | if (init_io_u(td)) | |
1008 | goto err; | |
1009 | ||
1010 | if (td->o.verify_async && verify_async_init(td)) | |
1011 | goto err; | |
1012 | ||
1013 | if (td->ioprio_set) { | |
1014 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) { | |
1015 | td_verror(td, errno, "ioprio_set"); | |
1016 | goto err; | |
1017 | } | |
1018 | } | |
1019 | ||
1020 | if (td->o.cgroup_weight && cgroup_setup(td, cgroup_list, &cgroup_mnt)) | |
1021 | goto err; | |
1022 | ||
649c10c5 BC |
1023 | errno = 0; |
1024 | if (nice(td->o.nice) == -1 && errno != 0) { | |
2e1df07d JA |
1025 | td_verror(td, errno, "nice"); |
1026 | goto err; | |
1027 | } | |
1028 | ||
1029 | if (td->o.ioscheduler && switch_ioscheduler(td)) | |
1030 | goto err; | |
1031 | ||
1032 | if (!td->o.create_serialize && setup_files(td)) | |
1033 | goto err; | |
1034 | ||
1035 | if (td_io_init(td)) | |
1036 | goto err; | |
1037 | ||
1038 | if (init_random_map(td)) | |
1039 | goto err; | |
1040 | ||
1041 | if (td->o.exec_prerun) { | |
1042 | if (exec_string(td->o.exec_prerun)) | |
1043 | goto err; | |
1044 | } | |
1045 | ||
1046 | if (td->o.pre_read) { | |
1047 | if (pre_read_files(td) < 0) | |
1048 | goto err; | |
1049 | } | |
1050 | ||
1051 | fio_gettime(&td->epoch, NULL); | |
1052 | getrusage(RUSAGE_SELF, &td->ru_start); | |
1053 | ||
1054 | clear_state = 0; | |
1055 | while (keep_running(td)) { | |
1056 | fio_gettime(&td->start, NULL); | |
1057 | memcpy(&td->bw_sample_time, &td->start, sizeof(td->start)); | |
1058 | memcpy(&td->iops_sample_time, &td->start, sizeof(td->start)); | |
1059 | memcpy(&td->tv_cache, &td->start, sizeof(td->start)); | |
1060 | ||
1061 | if (td->o.ratemin[0] || td->o.ratemin[1]) { | |
1062 | memcpy(&td->lastrate[0], &td->bw_sample_time, | |
1063 | sizeof(td->bw_sample_time)); | |
1064 | memcpy(&td->lastrate[1], &td->bw_sample_time, | |
1065 | sizeof(td->bw_sample_time)); | |
1066 | } | |
1067 | ||
1068 | if (clear_state) | |
1069 | clear_io_state(td); | |
1070 | ||
1071 | prune_io_piece_log(td); | |
1072 | ||
1073 | do_io(td); | |
1074 | ||
1075 | clear_state = 1; | |
1076 | ||
1077 | if (td_read(td) && td->io_bytes[DDIR_READ]) { | |
1078 | elapsed = utime_since_now(&td->start); | |
1079 | td->ts.runtime[DDIR_READ] += elapsed; | |
1080 | } | |
1081 | if (td_write(td) && td->io_bytes[DDIR_WRITE]) { | |
1082 | elapsed = utime_since_now(&td->start); | |
1083 | td->ts.runtime[DDIR_WRITE] += elapsed; | |
1084 | } | |
1085 | ||
1086 | if (td->error || td->terminate) | |
1087 | break; | |
1088 | ||
1089 | if (!td->o.do_verify || | |
1090 | td->o.verify == VERIFY_NONE || | |
1091 | (td->io_ops->flags & FIO_UNIDIR)) | |
1092 | continue; | |
1093 | ||
1094 | clear_io_state(td); | |
1095 | ||
1096 | fio_gettime(&td->start, NULL); | |
1097 | ||
1098 | do_verify(td); | |
1099 | ||
1100 | td->ts.runtime[DDIR_READ] += utime_since_now(&td->start); | |
1101 | ||
1102 | if (td->error || td->terminate) | |
1103 | break; | |
1104 | } | |
1105 | ||
1106 | update_rusage_stat(td); | |
1107 | td->ts.runtime[0] = (td->ts.runtime[0] + 999) / 1000; | |
1108 | td->ts.runtime[1] = (td->ts.runtime[1] + 999) / 1000; | |
1109 | td->ts.total_run_time = mtime_since_now(&td->epoch); | |
1110 | td->ts.io_bytes[0] = td->io_bytes[0]; | |
1111 | td->ts.io_bytes[1] = td->io_bytes[1]; | |
1112 | ||
1113 | fio_mutex_down(writeout_mutex); | |
1114 | if (td->bw_log) { | |
1115 | if (td->o.bw_log_file) { | |
1116 | finish_log_named(td, td->bw_log, | |
1117 | td->o.bw_log_file, "bw"); | |
1118 | } else | |
1119 | finish_log(td, td->bw_log, "bw"); | |
1120 | } | |
1121 | if (td->lat_log) { | |
1122 | if (td->o.lat_log_file) { | |
1123 | finish_log_named(td, td->lat_log, | |
1124 | td->o.lat_log_file, "lat"); | |
1125 | } else | |
1126 | finish_log(td, td->lat_log, "lat"); | |
1127 | } | |
1128 | if (td->slat_log) { | |
1129 | if (td->o.lat_log_file) { | |
1130 | finish_log_named(td, td->slat_log, | |
1131 | td->o.lat_log_file, "slat"); | |
1132 | } else | |
1133 | finish_log(td, td->slat_log, "slat"); | |
1134 | } | |
1135 | if (td->clat_log) { | |
1136 | if (td->o.lat_log_file) { | |
1137 | finish_log_named(td, td->clat_log, | |
1138 | td->o.lat_log_file, "clat"); | |
1139 | } else | |
1140 | finish_log(td, td->clat_log, "clat"); | |
1141 | } | |
1142 | if (td->iops_log) { | |
1143 | if (td->o.iops_log_file) { | |
1144 | finish_log_named(td, td->iops_log, | |
1145 | td->o.iops_log_file, "iops"); | |
1146 | } else | |
1147 | finish_log(td, td->iops_log, "iops"); | |
1148 | } | |
1149 | ||
1150 | fio_mutex_up(writeout_mutex); | |
1151 | if (td->o.exec_postrun) | |
1152 | exec_string(td->o.exec_postrun); | |
1153 | ||
1154 | if (exitall_on_terminate) | |
1155 | fio_terminate_threads(td->groupid); | |
1156 | ||
1157 | err: | |
1158 | if (td->error) | |
1159 | log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error, | |
1160 | td->verror); | |
1161 | ||
1162 | if (td->o.verify_async) | |
1163 | verify_async_exit(td); | |
1164 | ||
1165 | close_and_free_files(td); | |
1166 | close_ioengine(td); | |
1167 | cleanup_io_u(td); | |
1168 | cgroup_shutdown(td, &cgroup_mnt); | |
1169 | ||
1170 | if (td->o.cpumask_set) { | |
1171 | int ret = fio_cpuset_exit(&td->o.cpumask); | |
1172 | ||
1173 | td_verror(td, ret, "fio_cpuset_exit"); | |
1174 | } | |
1175 | ||
1176 | /* | |
1177 | * do this very late, it will log file closing as well | |
1178 | */ | |
1179 | if (td->o.write_iolog_file) | |
1180 | write_iolog_close(td); | |
1181 | ||
1182 | td_set_runstate(td, TD_EXITED); | |
e43606c2 | 1183 | return (void *) (uintptr_t) td->error; |
2e1df07d JA |
1184 | } |
1185 | ||
1186 | ||
1187 | /* | |
1188 | * We cannot pass the td data into a forked process, so attach the td and | |
1189 | * pass it to the thread worker. | |
1190 | */ | |
1191 | static int fork_main(int shmid, int offset) | |
1192 | { | |
1193 | struct thread_data *td; | |
1194 | void *data, *ret; | |
1195 | ||
1196 | #ifndef __hpux | |
1197 | data = shmat(shmid, NULL, 0); | |
1198 | if (data == (void *) -1) { | |
1199 | int __err = errno; | |
1200 | ||
1201 | perror("shmat"); | |
1202 | return __err; | |
1203 | } | |
1204 | #else | |
1205 | /* | |
1206 | * HP-UX inherits shm mappings? | |
1207 | */ | |
1208 | data = threads; | |
1209 | #endif | |
1210 | ||
1211 | td = data + offset * sizeof(struct thread_data); | |
1212 | ret = thread_main(td); | |
1213 | shmdt(data); | |
e43606c2 | 1214 | return (int) (uintptr_t) ret; |
2e1df07d JA |
1215 | } |
1216 | ||
1217 | /* | |
1218 | * Run over the job map and reap the threads that have exited, if any. | |
1219 | */ | |
1220 | static void reap_threads(unsigned int *nr_running, unsigned int *t_rate, | |
1221 | unsigned int *m_rate) | |
1222 | { | |
1223 | struct thread_data *td; | |
1224 | unsigned int cputhreads, realthreads, pending; | |
1225 | int i, status, ret; | |
1226 | ||
1227 | /* | |
1228 | * reap exited threads (TD_EXITED -> TD_REAPED) | |
1229 | */ | |
1230 | realthreads = pending = cputhreads = 0; | |
1231 | for_each_td(td, i) { | |
1232 | int flags = 0; | |
1233 | ||
1234 | /* | |
1235 | * ->io_ops is NULL for a thread that has closed its | |
1236 | * io engine | |
1237 | */ | |
1238 | if (td->io_ops && !strcmp(td->io_ops->name, "cpuio")) | |
1239 | cputhreads++; | |
1240 | else | |
1241 | realthreads++; | |
1242 | ||
1243 | if (!td->pid) { | |
1244 | pending++; | |
1245 | continue; | |
1246 | } | |
1247 | if (td->runstate == TD_REAPED) | |
1248 | continue; | |
1249 | if (td->o.use_thread) { | |
1250 | if (td->runstate == TD_EXITED) { | |
1251 | td_set_runstate(td, TD_REAPED); | |
1252 | goto reaped; | |
1253 | } | |
1254 | continue; | |
1255 | } | |
1256 | ||
1257 | flags = WNOHANG; | |
1258 | if (td->runstate == TD_EXITED) | |
1259 | flags = 0; | |
1260 | ||
1261 | /* | |
1262 | * check if someone quit or got killed in an unusual way | |
1263 | */ | |
1264 | ret = waitpid(td->pid, &status, flags); | |
1265 | if (ret < 0) { | |
1266 | if (errno == ECHILD) { | |
1267 | log_err("fio: pid=%d disappeared %d\n", | |
1268 | (int) td->pid, td->runstate); | |
1269 | td_set_runstate(td, TD_REAPED); | |
1270 | goto reaped; | |
1271 | } | |
1272 | perror("waitpid"); | |
1273 | } else if (ret == td->pid) { | |
1274 | if (WIFSIGNALED(status)) { | |
1275 | int sig = WTERMSIG(status); | |
1276 | ||
1277 | if (sig != SIGTERM) | |
1278 | log_err("fio: pid=%d, got signal=%d\n", | |
1279 | (int) td->pid, sig); | |
1280 | td_set_runstate(td, TD_REAPED); | |
1281 | goto reaped; | |
1282 | } | |
1283 | if (WIFEXITED(status)) { | |
1284 | if (WEXITSTATUS(status) && !td->error) | |
1285 | td->error = WEXITSTATUS(status); | |
1286 | ||
1287 | td_set_runstate(td, TD_REAPED); | |
1288 | goto reaped; | |
1289 | } | |
1290 | } | |
1291 | ||
1292 | /* | |
1293 | * thread is not dead, continue | |
1294 | */ | |
1295 | pending++; | |
1296 | continue; | |
1297 | reaped: | |
1298 | (*nr_running)--; | |
1299 | (*m_rate) -= (td->o.ratemin[0] + td->o.ratemin[1]); | |
1300 | (*t_rate) -= (td->o.rate[0] + td->o.rate[1]); | |
1301 | if (!td->pid) | |
1302 | pending--; | |
1303 | ||
1304 | if (td->error) | |
1305 | exit_value++; | |
1306 | ||
1307 | done_secs += mtime_since_now(&td->epoch) / 1000; | |
1308 | } | |
1309 | ||
1310 | if (*nr_running == cputhreads && !pending && realthreads) | |
1311 | fio_terminate_threads(TERMINATE_ALL); | |
1312 | } | |
1313 | ||
2e1df07d JA |
1314 | /* |
1315 | * Main function for kicking off and reaping jobs, as needed. | |
1316 | */ | |
1317 | static void run_threads(void) | |
1318 | { | |
1319 | struct thread_data *td; | |
1320 | unsigned long spent; | |
1321 | unsigned int i, todo, nr_running, m_rate, t_rate, nr_started; | |
1322 | ||
1323 | if (fio_pin_memory()) | |
1324 | return; | |
1325 | ||
1326 | if (fio_gtod_offload && fio_start_gtod_thread()) | |
1327 | return; | |
1328 | ||
1329 | set_sig_handlers(); | |
1330 | ||
1331 | if (!terse_output) { | |
1332 | log_info("Starting "); | |
1333 | if (nr_thread) | |
1334 | log_info("%d thread%s", nr_thread, | |
1335 | nr_thread > 1 ? "s" : ""); | |
1336 | if (nr_process) { | |
1337 | if (nr_thread) | |
1338 | log_info(" and "); | |
1339 | log_info("%d process%s", nr_process, | |
1340 | nr_process > 1 ? "es" : ""); | |
1341 | } | |
1342 | log_info("\n"); | |
1343 | fflush(stdout); | |
1344 | } | |
1345 | ||
1346 | todo = thread_number; | |
1347 | nr_running = 0; | |
1348 | nr_started = 0; | |
1349 | m_rate = t_rate = 0; | |
1350 | ||
1351 | for_each_td(td, i) { | |
1352 | print_status_init(td->thread_number - 1); | |
1353 | ||
1354 | if (!td->o.create_serialize) | |
1355 | continue; | |
1356 | ||
1357 | /* | |
1358 | * do file setup here so it happens sequentially, | |
1359 | * we don't want X number of threads getting their | |
1360 | * client data interspersed on disk | |
1361 | */ | |
1362 | if (setup_files(td)) { | |
1363 | exit_value++; | |
1364 | if (td->error) | |
1365 | log_err("fio: pid=%d, err=%d/%s\n", | |
1366 | (int) td->pid, td->error, td->verror); | |
1367 | td_set_runstate(td, TD_REAPED); | |
1368 | todo--; | |
1369 | } else { | |
1370 | struct fio_file *f; | |
1371 | unsigned int j; | |
1372 | ||
1373 | /* | |
1374 | * for sharing to work, each job must always open | |
1375 | * its own files. so close them, if we opened them | |
1376 | * for creation | |
1377 | */ | |
1378 | for_each_file(td, f, j) { | |
1379 | if (fio_file_open(f)) | |
1380 | td_io_close_file(td, f); | |
1381 | } | |
1382 | } | |
1383 | } | |
1384 | ||
1385 | set_genesis_time(); | |
1386 | ||
1387 | while (todo) { | |
1388 | struct thread_data *map[REAL_MAX_JOBS]; | |
1389 | struct timeval this_start; | |
1390 | int this_jobs = 0, left; | |
1391 | ||
1392 | /* | |
1393 | * create threads (TD_NOT_CREATED -> TD_CREATED) | |
1394 | */ | |
1395 | for_each_td(td, i) { | |
1396 | if (td->runstate != TD_NOT_CREATED) | |
1397 | continue; | |
1398 | ||
1399 | /* | |
1400 | * never got a chance to start, killed by other | |
1401 | * thread for some reason | |
1402 | */ | |
1403 | if (td->terminate) { | |
1404 | todo--; | |
1405 | continue; | |
1406 | } | |
1407 | ||
1408 | if (td->o.start_delay) { | |
1409 | spent = mtime_since_genesis(); | |
1410 | ||
1411 | if (td->o.start_delay * 1000 > spent) | |
1412 | continue; | |
1413 | } | |
1414 | ||
1415 | if (td->o.stonewall && (nr_started || nr_running)) { | |
1416 | dprint(FD_PROCESS, "%s: stonewall wait\n", | |
1417 | td->o.name); | |
1418 | break; | |
1419 | } | |
1420 | ||
1421 | init_disk_util(td); | |
1422 | ||
1423 | /* | |
1424 | * Set state to created. Thread will transition | |
1425 | * to TD_INITIALIZED when it's done setting up. | |
1426 | */ | |
1427 | td_set_runstate(td, TD_CREATED); | |
1428 | map[this_jobs++] = td; | |
1429 | nr_started++; | |
1430 | ||
1431 | if (td->o.use_thread) { | |
1432 | int ret; | |
1433 | ||
1434 | dprint(FD_PROCESS, "will pthread_create\n"); | |
1435 | ret = pthread_create(&td->thread, NULL, | |
1436 | thread_main, td); | |
1437 | if (ret) { | |
1438 | log_err("pthread_create: %s\n", | |
1439 | strerror(ret)); | |
1440 | nr_started--; | |
1441 | break; | |
1442 | } | |
1443 | ret = pthread_detach(td->thread); | |
1444 | if (ret) | |
1445 | log_err("pthread_detach: %s", | |
1446 | strerror(ret)); | |
1447 | } else { | |
1448 | pid_t pid; | |
1449 | dprint(FD_PROCESS, "will fork\n"); | |
1450 | pid = fork(); | |
1451 | if (!pid) { | |
1452 | int ret = fork_main(shm_id, i); | |
1453 | ||
1454 | _exit(ret); | |
1455 | } else if (i == fio_debug_jobno) | |
1456 | *fio_debug_jobp = pid; | |
1457 | } | |
1458 | dprint(FD_MUTEX, "wait on startup_mutex\n"); | |
1459 | if (fio_mutex_down_timeout(startup_mutex, 10)) { | |
1460 | log_err("fio: job startup hung? exiting.\n"); | |
1461 | fio_terminate_threads(TERMINATE_ALL); | |
1462 | fio_abort = 1; | |
1463 | nr_started--; | |
1464 | break; | |
1465 | } | |
1466 | dprint(FD_MUTEX, "done waiting on startup_mutex\n"); | |
1467 | } | |
1468 | ||
1469 | /* | |
1470 | * Wait for the started threads to transition to | |
1471 | * TD_INITIALIZED. | |
1472 | */ | |
1473 | fio_gettime(&this_start, NULL); | |
1474 | left = this_jobs; | |
1475 | while (left && !fio_abort) { | |
1476 | if (mtime_since_now(&this_start) > JOB_START_TIMEOUT) | |
1477 | break; | |
1478 | ||
1479 | usleep(100000); | |
1480 | ||
1481 | for (i = 0; i < this_jobs; i++) { | |
1482 | td = map[i]; | |
1483 | if (!td) | |
1484 | continue; | |
1485 | if (td->runstate == TD_INITIALIZED) { | |
1486 | map[i] = NULL; | |
1487 | left--; | |
1488 | } else if (td->runstate >= TD_EXITED) { | |
1489 | map[i] = NULL; | |
1490 | left--; | |
1491 | todo--; | |
1492 | nr_running++; /* work-around... */ | |
1493 | } | |
1494 | } | |
1495 | } | |
1496 | ||
1497 | if (left) { | |
4e87c37a JA |
1498 | log_err("fio: %d job%s failed to start\n", left, |
1499 | left > 1 ? "s" : ""); | |
2e1df07d JA |
1500 | for (i = 0; i < this_jobs; i++) { |
1501 | td = map[i]; | |
1502 | if (!td) | |
1503 | continue; | |
1504 | kill(td->pid, SIGTERM); | |
1505 | } | |
1506 | break; | |
1507 | } | |
1508 | ||
1509 | /* | |
1510 | * start created threads (TD_INITIALIZED -> TD_RUNNING). | |
1511 | */ | |
1512 | for_each_td(td, i) { | |
1513 | if (td->runstate != TD_INITIALIZED) | |
1514 | continue; | |
1515 | ||
1516 | if (in_ramp_time(td)) | |
1517 | td_set_runstate(td, TD_RAMP); | |
1518 | else | |
1519 | td_set_runstate(td, TD_RUNNING); | |
1520 | nr_running++; | |
1521 | nr_started--; | |
1522 | m_rate += td->o.ratemin[0] + td->o.ratemin[1]; | |
1523 | t_rate += td->o.rate[0] + td->o.rate[1]; | |
1524 | todo--; | |
1525 | fio_mutex_up(td->mutex); | |
1526 | } | |
1527 | ||
1528 | reap_threads(&nr_running, &t_rate, &m_rate); | |
1529 | ||
1530 | if (todo) { | |
1531 | if (is_backend) | |
1532 | fio_server_idle_loop(); | |
1533 | else | |
1534 | usleep(100000); | |
1535 | } | |
1536 | } | |
1537 | ||
1538 | while (nr_running) { | |
1539 | reap_threads(&nr_running, &t_rate, &m_rate); | |
1540 | ||
1541 | if (is_backend) | |
1542 | fio_server_idle_loop(); | |
1543 | else | |
1544 | usleep(10000); | |
1545 | } | |
1546 | ||
1547 | update_io_ticks(); | |
1548 | fio_unpin_memory(); | |
1549 | } | |
1550 | ||
1551 | static void *disk_thread_main(void *data) | |
1552 | { | |
1553 | fio_mutex_up(startup_mutex); | |
1554 | ||
1555 | while (threads) { | |
1556 | usleep(DISK_UTIL_MSEC * 1000); | |
1557 | if (!threads) | |
1558 | break; | |
1559 | update_io_ticks(); | |
1560 | ||
1561 | if (!is_backend) | |
1562 | print_thread_status(); | |
1563 | } | |
1564 | ||
1565 | return NULL; | |
1566 | } | |
1567 | ||
1568 | static int create_disk_util_thread(void) | |
1569 | { | |
1570 | int ret; | |
1571 | ||
1572 | ret = pthread_create(&disk_util_thread, NULL, disk_thread_main, NULL); | |
1573 | if (ret) { | |
1574 | log_err("Can't create disk util thread: %s\n", strerror(ret)); | |
1575 | return 1; | |
1576 | } | |
1577 | ||
1578 | ret = pthread_detach(disk_util_thread); | |
1579 | if (ret) { | |
1580 | log_err("Can't detatch disk util thread: %s\n", strerror(ret)); | |
1581 | return 1; | |
1582 | } | |
1583 | ||
1584 | dprint(FD_MUTEX, "wait on startup_mutex\n"); | |
1585 | fio_mutex_down(startup_mutex); | |
1586 | dprint(FD_MUTEX, "done waiting on startup_mutex\n"); | |
1587 | return 0; | |
1588 | } | |
1589 | ||
2e1df07d JA |
1590 | int fio_backend(void) |
1591 | { | |
1592 | struct thread_data *td; | |
1593 | int i; | |
1594 | ||
1595 | if (exec_profile) { | |
1596 | if (load_profile(exec_profile)) | |
1597 | return 1; | |
1598 | free(exec_profile); | |
1599 | exec_profile = NULL; | |
1600 | } | |
1601 | if (!thread_number) | |
1602 | return 0; | |
1603 | ||
1604 | if (write_bw_log) { | |
1605 | setup_log(&agg_io_log[DDIR_READ], 0); | |
1606 | setup_log(&agg_io_log[DDIR_WRITE], 0); | |
1607 | } | |
1608 | ||
1609 | startup_mutex = fio_mutex_init(0); | |
1610 | if (startup_mutex == NULL) | |
1611 | return 1; | |
1612 | writeout_mutex = fio_mutex_init(1); | |
1613 | if (writeout_mutex == NULL) | |
1614 | return 1; | |
1615 | ||
1616 | set_genesis_time(); | |
1617 | create_disk_util_thread(); | |
1618 | ||
1619 | cgroup_list = smalloc(sizeof(*cgroup_list)); | |
1620 | INIT_FLIST_HEAD(cgroup_list); | |
1621 | ||
1622 | run_threads(); | |
1623 | ||
1624 | if (!fio_abort) { | |
1625 | show_run_stats(); | |
1626 | if (write_bw_log) { | |
1627 | __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log"); | |
1628 | __finish_log(agg_io_log[DDIR_WRITE], | |
1629 | "agg-write_bw.log"); | |
1630 | } | |
1631 | } | |
1632 | ||
1633 | for_each_td(td, i) | |
1634 | fio_options_free(td); | |
1635 | ||
1636 | cgroup_kill(cgroup_list); | |
1637 | sfree(cgroup_list); | |
1638 | sfree(cgroup_mnt); | |
1639 | ||
1640 | fio_mutex_remove(startup_mutex); | |
1641 | fio_mutex_remove(writeout_mutex); | |
1642 | return exit_value; | |
1643 | } |