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