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> | |
2e1df07d | 37 | #include <sys/mman.h> |
ff6bb260 | 38 | #include <math.h> |
2e1df07d JA |
39 | |
40 | #include "fio.h" | |
a5e0ee11 O |
41 | #ifndef FIO_NO_HAVE_SHM_H |
42 | #include <sys/shm.h> | |
43 | #endif | |
2e1df07d JA |
44 | #include "hash.h" |
45 | #include "smalloc.h" | |
46 | #include "verify.h" | |
47 | #include "trim.h" | |
48 | #include "diskutil.h" | |
49 | #include "cgroup.h" | |
50 | #include "profile.h" | |
51 | #include "lib/rand.h" | |
f7690c4a | 52 | #include "lib/memalign.h" |
2e1df07d | 53 | #include "server.h" |
44404c5a | 54 | #include "lib/getrusage.h" |
f2a2ce0e | 55 | #include "idletime.h" |
002fe734 | 56 | #include "err.h" |
a9da8ab2 | 57 | #include "workqueue.h" |
e81ecca3 | 58 | #include "lib/mountcheck.h" |
40511301 | 59 | #include "rate-submit.h" |
2e1df07d | 60 | |
5ddc6707 JA |
61 | static pthread_t helper_thread; |
62 | static pthread_mutex_t helper_lock; | |
63 | pthread_cond_t helper_cond; | |
64 | int helper_do_stat = 0; | |
8aab824f | 65 | |
2e1df07d | 66 | static struct fio_mutex *startup_mutex; |
2e1df07d JA |
67 | static struct flist_head *cgroup_list; |
68 | static char *cgroup_mnt; | |
69 | static int exit_value; | |
70 | static volatile int fio_abort; | |
3a5f6bde JA |
71 | static unsigned int nr_process = 0; |
72 | static unsigned int nr_thread = 0; | |
2e1df07d | 73 | |
6eaf09d6 | 74 | struct io_log *agg_io_log[DDIR_RWDIR_CNT]; |
2e1df07d | 75 | |
a3efc919 JA |
76 | int groupid = 0; |
77 | unsigned int thread_number = 0; | |
108fea77 | 78 | unsigned int stat_number = 0; |
a3efc919 JA |
79 | int shm_id = 0; |
80 | int temp_stall_ts; | |
81 | unsigned long done_secs = 0; | |
5ddc6707 | 82 | volatile int helper_exit = 0; |
a3efc919 | 83 | |
2e1df07d | 84 | #define PAGE_ALIGN(buf) \ |
e43606c2 | 85 | (char *) (((uintptr_t) (buf) + page_mask) & ~page_mask) |
2e1df07d JA |
86 | |
87 | #define JOB_START_TIMEOUT (5 * 1000) | |
88 | ||
89 | static void sig_int(int sig) | |
90 | { | |
91 | if (threads) { | |
92 | if (is_backend) | |
93 | fio_server_got_signal(sig); | |
94 | else { | |
95 | log_info("\nfio: terminating on signal %d\n", sig); | |
e411c301 | 96 | log_info_flush(); |
2e1df07d JA |
97 | exit_value = 128; |
98 | } | |
99 | ||
100 | fio_terminate_threads(TERMINATE_ALL); | |
101 | } | |
102 | } | |
103 | ||
d2235e56 | 104 | void sig_show_status(int sig) |
4c6d91e8 JA |
105 | { |
106 | show_running_run_stats(); | |
107 | } | |
108 | ||
2e1df07d JA |
109 | static void set_sig_handlers(void) |
110 | { | |
111 | struct sigaction act; | |
112 | ||
113 | memset(&act, 0, sizeof(act)); | |
114 | act.sa_handler = sig_int; | |
115 | act.sa_flags = SA_RESTART; | |
116 | sigaction(SIGINT, &act, NULL); | |
117 | ||
118 | memset(&act, 0, sizeof(act)); | |
119 | act.sa_handler = sig_int; | |
120 | act.sa_flags = SA_RESTART; | |
121 | sigaction(SIGTERM, &act, NULL); | |
122 | ||
2f694507 BC |
123 | /* Windows uses SIGBREAK as a quit signal from other applications */ |
124 | #ifdef WIN32 | |
125 | memset(&act, 0, sizeof(act)); | |
126 | act.sa_handler = sig_int; | |
127 | act.sa_flags = SA_RESTART; | |
128 | sigaction(SIGBREAK, &act, NULL); | |
129 | #endif | |
130 | ||
4c6d91e8 JA |
131 | memset(&act, 0, sizeof(act)); |
132 | act.sa_handler = sig_show_status; | |
133 | act.sa_flags = SA_RESTART; | |
134 | sigaction(SIGUSR1, &act, NULL); | |
135 | ||
2e1df07d JA |
136 | if (is_backend) { |
137 | memset(&act, 0, sizeof(act)); | |
138 | act.sa_handler = sig_int; | |
139 | act.sa_flags = SA_RESTART; | |
140 | sigaction(SIGPIPE, &act, NULL); | |
141 | } | |
142 | } | |
143 | ||
144 | /* | |
145 | * Check if we are above the minimum rate given. | |
146 | */ | |
e39c0676 JA |
147 | static bool __check_min_rate(struct thread_data *td, struct timeval *now, |
148 | enum fio_ddir ddir) | |
2e1df07d JA |
149 | { |
150 | unsigned long long bytes = 0; | |
151 | unsigned long iops = 0; | |
152 | unsigned long spent; | |
153 | unsigned long rate; | |
154 | unsigned int ratemin = 0; | |
155 | unsigned int rate_iops = 0; | |
156 | unsigned int rate_iops_min = 0; | |
157 | ||
158 | assert(ddir_rw(ddir)); | |
159 | ||
160 | if (!td->o.ratemin[ddir] && !td->o.rate_iops_min[ddir]) | |
e39c0676 | 161 | return false; |
2e1df07d JA |
162 | |
163 | /* | |
164 | * allow a 2 second settle period in the beginning | |
165 | */ | |
166 | if (mtime_since(&td->start, now) < 2000) | |
e39c0676 | 167 | return false; |
2e1df07d JA |
168 | |
169 | iops += td->this_io_blocks[ddir]; | |
170 | bytes += td->this_io_bytes[ddir]; | |
171 | ratemin += td->o.ratemin[ddir]; | |
172 | rate_iops += td->o.rate_iops[ddir]; | |
173 | rate_iops_min += td->o.rate_iops_min[ddir]; | |
174 | ||
175 | /* | |
176 | * if rate blocks is set, sample is running | |
177 | */ | |
178 | if (td->rate_bytes[ddir] || td->rate_blocks[ddir]) { | |
179 | spent = mtime_since(&td->lastrate[ddir], now); | |
180 | if (spent < td->o.ratecycle) | |
e39c0676 | 181 | return false; |
2e1df07d | 182 | |
20e37e4a | 183 | if (td->o.rate[ddir] || td->o.ratemin[ddir]) { |
2e1df07d JA |
184 | /* |
185 | * check bandwidth specified rate | |
186 | */ | |
187 | if (bytes < td->rate_bytes[ddir]) { | |
188 | log_err("%s: min rate %u not met\n", td->o.name, | |
189 | ratemin); | |
e39c0676 | 190 | return true; |
2e1df07d | 191 | } else { |
49cba9b3 JA |
192 | if (spent) |
193 | rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent; | |
194 | else | |
195 | rate = 0; | |
196 | ||
2e1df07d JA |
197 | if (rate < ratemin || |
198 | bytes < td->rate_bytes[ddir]) { | |
199 | log_err("%s: min rate %u not met, got" | |
200 | " %luKB/sec\n", td->o.name, | |
201 | ratemin, rate); | |
e39c0676 | 202 | return true; |
2e1df07d JA |
203 | } |
204 | } | |
205 | } else { | |
206 | /* | |
207 | * checks iops specified rate | |
208 | */ | |
209 | if (iops < rate_iops) { | |
210 | log_err("%s: min iops rate %u not met\n", | |
211 | td->o.name, rate_iops); | |
e39c0676 | 212 | return true; |
2e1df07d | 213 | } else { |
4c707a3b JA |
214 | if (spent) |
215 | rate = ((iops - td->rate_blocks[ddir]) * 1000) / spent; | |
216 | else | |
217 | rate = 0; | |
218 | ||
2e1df07d JA |
219 | if (rate < rate_iops_min || |
220 | iops < td->rate_blocks[ddir]) { | |
221 | log_err("%s: min iops rate %u not met," | |
222 | " got %lu\n", td->o.name, | |
223 | rate_iops_min, rate); | |
e39c0676 | 224 | return true; |
2e1df07d JA |
225 | } |
226 | } | |
227 | } | |
228 | } | |
229 | ||
230 | td->rate_bytes[ddir] = bytes; | |
231 | td->rate_blocks[ddir] = iops; | |
232 | memcpy(&td->lastrate[ddir], now, sizeof(*now)); | |
e39c0676 | 233 | return false; |
2e1df07d JA |
234 | } |
235 | ||
e39c0676 | 236 | static bool check_min_rate(struct thread_data *td, struct timeval *now) |
2e1df07d | 237 | { |
e39c0676 | 238 | bool ret = false; |
2e1df07d | 239 | |
55312f9f | 240 | if (td->bytes_done[DDIR_READ]) |
6eaf09d6 | 241 | ret |= __check_min_rate(td, now, DDIR_READ); |
55312f9f | 242 | if (td->bytes_done[DDIR_WRITE]) |
6eaf09d6 | 243 | ret |= __check_min_rate(td, now, DDIR_WRITE); |
55312f9f | 244 | if (td->bytes_done[DDIR_TRIM]) |
6eaf09d6 | 245 | ret |= __check_min_rate(td, now, DDIR_TRIM); |
2e1df07d JA |
246 | |
247 | return ret; | |
248 | } | |
249 | ||
250 | /* | |
251 | * When job exits, we can cancel the in-flight IO if we are using async | |
252 | * io. Attempt to do so. | |
253 | */ | |
254 | static void cleanup_pending_aio(struct thread_data *td) | |
255 | { | |
2e1df07d JA |
256 | int r; |
257 | ||
258 | /* | |
259 | * get immediately available events, if any | |
260 | */ | |
55312f9f | 261 | r = io_u_queued_complete(td, 0); |
2e1df07d JA |
262 | if (r < 0) |
263 | return; | |
264 | ||
265 | /* | |
266 | * now cancel remaining active events | |
267 | */ | |
268 | if (td->io_ops->cancel) { | |
2ae0b204 JA |
269 | struct io_u *io_u; |
270 | int i; | |
2e1df07d | 271 | |
2ae0b204 JA |
272 | io_u_qiter(&td->io_u_all, io_u, i) { |
273 | if (io_u->flags & IO_U_F_FLIGHT) { | |
2e1df07d JA |
274 | r = td->io_ops->cancel(td, io_u); |
275 | if (!r) | |
276 | put_io_u(td, io_u); | |
277 | } | |
278 | } | |
279 | } | |
280 | ||
281 | if (td->cur_depth) | |
55312f9f | 282 | r = io_u_queued_complete(td, td->cur_depth); |
2e1df07d JA |
283 | } |
284 | ||
285 | /* | |
286 | * Helper to handle the final sync of a file. Works just like the normal | |
287 | * io path, just does everything sync. | |
288 | */ | |
e39c0676 | 289 | static bool fio_io_sync(struct thread_data *td, struct fio_file *f) |
2e1df07d JA |
290 | { |
291 | struct io_u *io_u = __get_io_u(td); | |
292 | int ret; | |
293 | ||
294 | if (!io_u) | |
e39c0676 | 295 | return true; |
2e1df07d JA |
296 | |
297 | io_u->ddir = DDIR_SYNC; | |
298 | io_u->file = f; | |
299 | ||
300 | if (td_io_prep(td, io_u)) { | |
301 | put_io_u(td, io_u); | |
e39c0676 | 302 | return true; |
2e1df07d JA |
303 | } |
304 | ||
305 | requeue: | |
306 | ret = td_io_queue(td, io_u); | |
307 | if (ret < 0) { | |
308 | td_verror(td, io_u->error, "td_io_queue"); | |
309 | put_io_u(td, io_u); | |
e39c0676 | 310 | return true; |
2e1df07d | 311 | } else if (ret == FIO_Q_QUEUED) { |
55312f9f | 312 | if (io_u_queued_complete(td, 1) < 0) |
e39c0676 | 313 | return true; |
2e1df07d JA |
314 | } else if (ret == FIO_Q_COMPLETED) { |
315 | if (io_u->error) { | |
316 | td_verror(td, io_u->error, "td_io_queue"); | |
e39c0676 | 317 | return true; |
2e1df07d JA |
318 | } |
319 | ||
55312f9f | 320 | if (io_u_sync_complete(td, io_u) < 0) |
e39c0676 | 321 | return true; |
2e1df07d JA |
322 | } else if (ret == FIO_Q_BUSY) { |
323 | if (td_io_commit(td)) | |
e39c0676 | 324 | return true; |
2e1df07d JA |
325 | goto requeue; |
326 | } | |
327 | ||
e39c0676 | 328 | return false; |
2e1df07d | 329 | } |
a3efc919 | 330 | |
61ee0f86 JA |
331 | static int fio_file_fsync(struct thread_data *td, struct fio_file *f) |
332 | { | |
333 | int ret; | |
334 | ||
335 | if (fio_file_open(f)) | |
336 | return fio_io_sync(td, f); | |
337 | ||
338 | if (td_io_open_file(td, f)) | |
339 | return 1; | |
340 | ||
341 | ret = fio_io_sync(td, f); | |
342 | td_io_close_file(td, f); | |
343 | return ret; | |
344 | } | |
345 | ||
2e1df07d JA |
346 | static inline void __update_tv_cache(struct thread_data *td) |
347 | { | |
348 | fio_gettime(&td->tv_cache, NULL); | |
349 | } | |
350 | ||
351 | static inline void update_tv_cache(struct thread_data *td) | |
352 | { | |
353 | if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask) | |
354 | __update_tv_cache(td); | |
355 | } | |
356 | ||
e39c0676 | 357 | static inline bool runtime_exceeded(struct thread_data *td, struct timeval *t) |
2e1df07d JA |
358 | { |
359 | if (in_ramp_time(td)) | |
e39c0676 | 360 | return false; |
2e1df07d | 361 | if (!td->o.timeout) |
e39c0676 | 362 | return false; |
0de5b26f | 363 | if (utime_since(&td->epoch, t) >= td->o.timeout) |
e39c0676 | 364 | return true; |
2e1df07d | 365 | |
e39c0676 | 366 | return false; |
2e1df07d JA |
367 | } |
368 | ||
95603b74 BF |
369 | /* |
370 | * We need to update the runtime consistently in ms, but keep a running | |
371 | * tally of the current elapsed time in microseconds for sub millisecond | |
372 | * updates. | |
373 | */ | |
374 | static inline void update_runtime(struct thread_data *td, | |
375 | unsigned long long *elapsed_us, | |
376 | const enum fio_ddir ddir) | |
377 | { | |
d5c8ea29 JA |
378 | if (ddir == DDIR_WRITE && td_write(td) && td->o.verify_only) |
379 | return; | |
380 | ||
95603b74 BF |
381 | td->ts.runtime[ddir] -= (elapsed_us[ddir] + 999) / 1000; |
382 | elapsed_us[ddir] += utime_since_now(&td->start); | |
383 | td->ts.runtime[ddir] += (elapsed_us[ddir] + 999) / 1000; | |
384 | } | |
385 | ||
e39c0676 JA |
386 | static bool break_on_this_error(struct thread_data *td, enum fio_ddir ddir, |
387 | int *retptr) | |
2e1df07d JA |
388 | { |
389 | int ret = *retptr; | |
390 | ||
391 | if (ret < 0 || td->error) { | |
8b28bd41 DM |
392 | int err = td->error; |
393 | enum error_type_bit eb; | |
2e1df07d JA |
394 | |
395 | if (ret < 0) | |
396 | err = -ret; | |
2e1df07d | 397 | |
8b28bd41 DM |
398 | eb = td_error_type(ddir, err); |
399 | if (!(td->o.continue_on_error & (1 << eb))) | |
e39c0676 | 400 | return true; |
2e1df07d | 401 | |
8b28bd41 | 402 | if (td_non_fatal_error(td, eb, err)) { |
2e1df07d JA |
403 | /* |
404 | * Continue with the I/Os in case of | |
405 | * a non fatal error. | |
406 | */ | |
407 | update_error_count(td, err); | |
408 | td_clear_error(td); | |
409 | *retptr = 0; | |
e39c0676 | 410 | return false; |
2e1df07d JA |
411 | } else if (td->o.fill_device && err == ENOSPC) { |
412 | /* | |
413 | * We expect to hit this error if | |
414 | * fill_device option is set. | |
415 | */ | |
416 | td_clear_error(td); | |
ebea2133 | 417 | fio_mark_td_terminate(td); |
e39c0676 | 418 | return true; |
2e1df07d JA |
419 | } else { |
420 | /* | |
421 | * Stop the I/O in case of a fatal | |
422 | * error. | |
423 | */ | |
424 | update_error_count(td, err); | |
e39c0676 | 425 | return true; |
2e1df07d JA |
426 | } |
427 | } | |
428 | ||
e39c0676 | 429 | return false; |
2e1df07d JA |
430 | } |
431 | ||
c97f1ad6 JA |
432 | static void check_update_rusage(struct thread_data *td) |
433 | { | |
434 | if (td->update_rusage) { | |
435 | td->update_rusage = 0; | |
436 | update_rusage_stat(td); | |
437 | fio_mutex_up(td->rusage_sem); | |
438 | } | |
439 | } | |
440 | ||
55312f9f | 441 | static int wait_for_completions(struct thread_data *td, struct timeval *time) |
69fea81e JA |
442 | { |
443 | const int full = queue_full(td); | |
444 | int min_evts = 0; | |
445 | int ret; | |
446 | ||
447 | /* | |
448 | * if the queue is full, we MUST reap at least 1 event | |
449 | */ | |
82407585 | 450 | min_evts = min(td->o.iodepth_batch_complete_min, td->cur_depth); |
4b157ac6 | 451 | if ((full && !min_evts) || !td->o.iodepth_batch_complete_min) |
69fea81e JA |
452 | min_evts = 1; |
453 | ||
66b6c5ef | 454 | if (time && (__should_check_rate(td, DDIR_READ) || |
69fea81e JA |
455 | __should_check_rate(td, DDIR_WRITE) || |
456 | __should_check_rate(td, DDIR_TRIM))) | |
457 | fio_gettime(time, NULL); | |
458 | ||
459 | do { | |
55312f9f | 460 | ret = io_u_queued_complete(td, min_evts); |
69fea81e JA |
461 | if (ret < 0) |
462 | break; | |
463 | } while (full && (td->cur_depth > td->o.iodepth_low)); | |
464 | ||
465 | return ret; | |
466 | } | |
467 | ||
e9d512d8 JA |
468 | int io_queue_event(struct thread_data *td, struct io_u *io_u, int *ret, |
469 | enum fio_ddir ddir, uint64_t *bytes_issued, int from_verify, | |
470 | struct timeval *comp_time) | |
471 | { | |
472 | int ret2; | |
473 | ||
474 | switch (*ret) { | |
475 | case FIO_Q_COMPLETED: | |
476 | if (io_u->error) { | |
477 | *ret = -io_u->error; | |
478 | clear_io_u(td, io_u); | |
479 | } else if (io_u->resid) { | |
480 | int bytes = io_u->xfer_buflen - io_u->resid; | |
481 | struct fio_file *f = io_u->file; | |
482 | ||
483 | if (bytes_issued) | |
484 | *bytes_issued += bytes; | |
485 | ||
486 | if (!from_verify) | |
487 | trim_io_piece(td, io_u); | |
488 | ||
489 | /* | |
490 | * zero read, fail | |
491 | */ | |
492 | if (!bytes) { | |
493 | if (!from_verify) | |
494 | unlog_io_piece(td, io_u); | |
495 | td_verror(td, EIO, "full resid"); | |
496 | put_io_u(td, io_u); | |
497 | break; | |
498 | } | |
499 | ||
500 | io_u->xfer_buflen = io_u->resid; | |
501 | io_u->xfer_buf += bytes; | |
502 | io_u->offset += bytes; | |
503 | ||
504 | if (ddir_rw(io_u->ddir)) | |
505 | td->ts.short_io_u[io_u->ddir]++; | |
506 | ||
507 | f = io_u->file; | |
508 | if (io_u->offset == f->real_file_size) | |
509 | goto sync_done; | |
510 | ||
511 | requeue_io_u(td, &io_u); | |
512 | } else { | |
513 | sync_done: | |
514 | if (comp_time && (__should_check_rate(td, DDIR_READ) || | |
515 | __should_check_rate(td, DDIR_WRITE) || | |
516 | __should_check_rate(td, DDIR_TRIM))) | |
517 | fio_gettime(comp_time, NULL); | |
518 | ||
519 | *ret = io_u_sync_complete(td, io_u); | |
520 | if (*ret < 0) | |
521 | break; | |
522 | } | |
a0e3e5a6 EW |
523 | |
524 | /* | |
525 | * when doing I/O (not when verifying), | |
526 | * check for any errors that are to be ignored | |
527 | */ | |
528 | if (!from_verify) | |
529 | break; | |
530 | ||
e9d512d8 JA |
531 | return 0; |
532 | case FIO_Q_QUEUED: | |
533 | /* | |
534 | * if the engine doesn't have a commit hook, | |
535 | * the io_u is really queued. if it does have such | |
536 | * a hook, it has to call io_u_queued() itself. | |
537 | */ | |
538 | if (td->io_ops->commit == NULL) | |
539 | io_u_queued(td, io_u); | |
540 | if (bytes_issued) | |
541 | *bytes_issued += io_u->xfer_buflen; | |
542 | break; | |
543 | case FIO_Q_BUSY: | |
544 | if (!from_verify) | |
545 | unlog_io_piece(td, io_u); | |
546 | requeue_io_u(td, &io_u); | |
547 | ret2 = td_io_commit(td); | |
548 | if (ret2 < 0) | |
549 | *ret = ret2; | |
550 | break; | |
551 | default: | |
b0775325 | 552 | assert(*ret < 0); |
e9d512d8 JA |
553 | td_verror(td, -(*ret), "td_io_queue"); |
554 | break; | |
555 | } | |
556 | ||
557 | if (break_on_this_error(td, ddir, ret)) | |
558 | return 1; | |
559 | ||
560 | return 0; | |
561 | } | |
562 | ||
e39c0676 | 563 | static inline bool io_in_polling(struct thread_data *td) |
82407585 RP |
564 | { |
565 | return !td->o.iodepth_batch_complete_min && | |
566 | !td->o.iodepth_batch_complete_max; | |
567 | } | |
568 | ||
2e1df07d JA |
569 | /* |
570 | * The main verify engine. Runs over the writes we previously submitted, | |
571 | * reads the blocks back in, and checks the crc/md5 of the data. | |
572 | */ | |
100f49f1 | 573 | static void do_verify(struct thread_data *td, uint64_t verify_bytes) |
2e1df07d JA |
574 | { |
575 | struct fio_file *f; | |
576 | struct io_u *io_u; | |
577 | int ret, min_events; | |
578 | unsigned int i; | |
579 | ||
580 | dprint(FD_VERIFY, "starting loop\n"); | |
581 | ||
582 | /* | |
583 | * sync io first and invalidate cache, to make sure we really | |
584 | * read from disk. | |
585 | */ | |
586 | for_each_file(td, f, i) { | |
587 | if (!fio_file_open(f)) | |
588 | continue; | |
589 | if (fio_io_sync(td, f)) | |
590 | break; | |
591 | if (file_invalidate_cache(td, f)) | |
592 | break; | |
593 | } | |
594 | ||
c97f1ad6 JA |
595 | check_update_rusage(td); |
596 | ||
2e1df07d JA |
597 | if (td->error) |
598 | return; | |
599 | ||
600 | td_set_runstate(td, TD_VERIFYING); | |
601 | ||
602 | io_u = NULL; | |
603 | while (!td->terminate) { | |
fbccf46c | 604 | enum fio_ddir ddir; |
e9d512d8 | 605 | int full; |
2e1df07d JA |
606 | |
607 | update_tv_cache(td); | |
c97f1ad6 | 608 | check_update_rusage(td); |
2e1df07d JA |
609 | |
610 | if (runtime_exceeded(td, &td->tv_cache)) { | |
611 | __update_tv_cache(td); | |
612 | if (runtime_exceeded(td, &td->tv_cache)) { | |
ebea2133 | 613 | fio_mark_td_terminate(td); |
2e1df07d JA |
614 | break; |
615 | } | |
616 | } | |
617 | ||
9e684a49 DE |
618 | if (flow_threshold_exceeded(td)) |
619 | continue; | |
620 | ||
44cbc6da JA |
621 | if (!td->o.experimental_verify) { |
622 | io_u = __get_io_u(td); | |
623 | if (!io_u) | |
624 | break; | |
2e1df07d | 625 | |
44cbc6da JA |
626 | if (get_next_verify(td, io_u)) { |
627 | put_io_u(td, io_u); | |
628 | break; | |
629 | } | |
2e1df07d | 630 | |
44cbc6da JA |
631 | if (td_io_prep(td, io_u)) { |
632 | put_io_u(td, io_u); | |
633 | break; | |
634 | } | |
635 | } else { | |
55312f9f | 636 | if (ddir_rw_sum(td->bytes_done) + td->o.rw_min_bs > verify_bytes) |
100f49f1 JA |
637 | break; |
638 | ||
bcd5abfa | 639 | while ((io_u = get_io_u(td)) != NULL) { |
002fe734 JA |
640 | if (IS_ERR(io_u)) { |
641 | io_u = NULL; | |
642 | ret = FIO_Q_BUSY; | |
643 | goto reap; | |
644 | } | |
645 | ||
bcd5abfa JA |
646 | /* |
647 | * We are only interested in the places where | |
648 | * we wrote or trimmed IOs. Turn those into | |
649 | * reads for verification purposes. | |
650 | */ | |
651 | if (io_u->ddir == DDIR_READ) { | |
652 | /* | |
653 | * Pretend we issued it for rwmix | |
654 | * accounting | |
655 | */ | |
656 | td->io_issues[DDIR_READ]++; | |
657 | put_io_u(td, io_u); | |
658 | continue; | |
659 | } else if (io_u->ddir == DDIR_TRIM) { | |
660 | io_u->ddir = DDIR_READ; | |
a9da8ab2 | 661 | io_u_set(io_u, IO_U_F_TRIMMED); |
bcd5abfa JA |
662 | break; |
663 | } else if (io_u->ddir == DDIR_WRITE) { | |
664 | io_u->ddir = DDIR_READ; | |
665 | break; | |
666 | } else { | |
667 | put_io_u(td, io_u); | |
668 | continue; | |
669 | } | |
670 | } | |
44cbc6da | 671 | |
bcd5abfa | 672 | if (!io_u) |
44cbc6da | 673 | break; |
2e1df07d JA |
674 | } |
675 | ||
ca09be4b JA |
676 | if (verify_state_should_stop(td, io_u)) { |
677 | put_io_u(td, io_u); | |
678 | break; | |
679 | } | |
680 | ||
2e1df07d JA |
681 | if (td->o.verify_async) |
682 | io_u->end_io = verify_io_u_async; | |
683 | else | |
684 | io_u->end_io = verify_io_u; | |
685 | ||
fbccf46c | 686 | ddir = io_u->ddir; |
10adc4a7 GG |
687 | if (!td->o.disable_slat) |
688 | fio_gettime(&io_u->start_time, NULL); | |
fbccf46c | 689 | |
2e1df07d | 690 | ret = td_io_queue(td, io_u); |
2e1df07d | 691 | |
e9d512d8 | 692 | if (io_queue_event(td, io_u, &ret, ddir, NULL, 1, NULL)) |
2e1df07d JA |
693 | break; |
694 | ||
695 | /* | |
696 | * if we can queue more, do so. but check if there are | |
697 | * completed io_u's first. Note that we can get BUSY even | |
698 | * without IO queued, if the system is resource starved. | |
699 | */ | |
002fe734 | 700 | reap: |
2e1df07d | 701 | full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth); |
82407585 | 702 | if (full || io_in_polling(td)) |
55312f9f | 703 | ret = wait_for_completions(td, NULL); |
2e1df07d | 704 | |
2e1df07d JA |
705 | if (ret < 0) |
706 | break; | |
707 | } | |
708 | ||
c97f1ad6 JA |
709 | check_update_rusage(td); |
710 | ||
2e1df07d JA |
711 | if (!td->error) { |
712 | min_events = td->cur_depth; | |
713 | ||
714 | if (min_events) | |
55312f9f | 715 | ret = io_u_queued_complete(td, min_events); |
2e1df07d JA |
716 | } else |
717 | cleanup_pending_aio(td); | |
718 | ||
719 | td_set_runstate(td, TD_RUNNING); | |
720 | ||
721 | dprint(FD_VERIFY, "exiting loop\n"); | |
722 | } | |
723 | ||
e39c0676 | 724 | static bool exceeds_number_ios(struct thread_data *td) |
3939fe85 JA |
725 | { |
726 | unsigned long long number_ios; | |
727 | ||
728 | if (!td->o.number_ios) | |
e39c0676 | 729 | return false; |
3939fe85 | 730 | |
cf8a46a7 | 731 | number_ios = ddir_rw_sum(td->io_blocks); |
3939fe85 JA |
732 | number_ios += td->io_u_queued + td->io_u_in_flight; |
733 | ||
cf8a46a7 | 734 | return number_ios >= (td->o.number_ios * td->loops); |
3939fe85 JA |
735 | } |
736 | ||
e39c0676 | 737 | static bool io_issue_bytes_exceeded(struct thread_data *td) |
f7078f7b | 738 | { |
77731b29 | 739 | unsigned long long bytes, limit; |
f7078f7b JA |
740 | |
741 | if (td_rw(td)) | |
74d6277f | 742 | bytes = td->io_issue_bytes[DDIR_READ] + td->io_issue_bytes[DDIR_WRITE]; |
f7078f7b | 743 | else if (td_write(td)) |
74d6277f | 744 | bytes = td->io_issue_bytes[DDIR_WRITE]; |
6eaf09d6 | 745 | else if (td_read(td)) |
74d6277f | 746 | bytes = td->io_issue_bytes[DDIR_READ]; |
f7078f7b | 747 | else |
74d6277f | 748 | bytes = td->io_issue_bytes[DDIR_TRIM]; |
f7078f7b | 749 | |
77731b29 JA |
750 | if (td->o.io_limit) |
751 | limit = td->o.io_limit; | |
752 | else | |
753 | limit = td->o.size; | |
754 | ||
cf8a46a7 | 755 | limit *= td->loops; |
77731b29 | 756 | return bytes >= limit || exceeds_number_ios(td); |
f7078f7b JA |
757 | } |
758 | ||
e39c0676 | 759 | static bool io_complete_bytes_exceeded(struct thread_data *td) |
e28dd2cf JE |
760 | { |
761 | unsigned long long bytes, limit; | |
762 | ||
763 | if (td_rw(td)) | |
764 | bytes = td->this_io_bytes[DDIR_READ] + td->this_io_bytes[DDIR_WRITE]; | |
765 | else if (td_write(td)) | |
766 | bytes = td->this_io_bytes[DDIR_WRITE]; | |
767 | else if (td_read(td)) | |
768 | bytes = td->this_io_bytes[DDIR_READ]; | |
769 | else | |
770 | bytes = td->this_io_bytes[DDIR_TRIM]; | |
771 | ||
772 | if (td->o.io_limit) | |
773 | limit = td->o.io_limit; | |
774 | else | |
775 | limit = td->o.size; | |
776 | ||
cf8a46a7 | 777 | limit *= td->loops; |
e28dd2cf JE |
778 | return bytes >= limit || exceeds_number_ios(td); |
779 | } | |
780 | ||
50a8ce86 D |
781 | /* |
782 | * used to calculate the next io time for rate control | |
783 | * | |
784 | */ | |
785 | static long long usec_for_io(struct thread_data *td, enum fio_ddir ddir) | |
786 | { | |
ff6bb260 | 787 | uint64_t secs, remainder, bps, bytes, iops; |
50a8ce86 D |
788 | |
789 | assert(!(td->flags & TD_F_CHILD)); | |
790 | bytes = td->rate_io_issue_bytes[ddir]; | |
791 | bps = td->rate_bps[ddir]; | |
ff6bb260 | 792 | |
6de65959 | 793 | if (td->o.rate_process == RATE_PROCESS_POISSON) { |
a77d139b | 794 | uint64_t val; |
ff6bb260 | 795 | iops = bps / td->o.bs[ddir]; |
a77d139b JA |
796 | val = (int64_t) (1000000 / iops) * |
797 | -logf(__rand_0_1(&td->poisson_state)); | |
798 | if (val) { | |
799 | dprint(FD_RATE, "poisson rate iops=%llu\n", | |
800 | (unsigned long long) 1000000 / val); | |
801 | } | |
802 | td->last_usec += val; | |
ff6bb260 SL |
803 | return td->last_usec; |
804 | } else if (bps) { | |
50a8ce86 D |
805 | secs = bytes / bps; |
806 | remainder = bytes % bps; | |
807 | return remainder * 1000000 / bps + secs * 1000000; | |
e7b24047 JA |
808 | } |
809 | ||
810 | return 0; | |
50a8ce86 D |
811 | } |
812 | ||
2e1df07d JA |
813 | /* |
814 | * Main IO worker function. It retrieves io_u's to process and queues | |
815 | * and reaps them, checking for rate and errors along the way. | |
100f49f1 JA |
816 | * |
817 | * Returns number of bytes written and trimmed. | |
2e1df07d | 818 | */ |
95b03be0 | 819 | static void do_io(struct thread_data *td, uint64_t *bytes_done) |
2e1df07d JA |
820 | { |
821 | unsigned int i; | |
822 | int ret = 0; | |
c2703bf3 | 823 | uint64_t total_bytes, bytes_issued = 0; |
70c68076 | 824 | |
95b03be0 JA |
825 | for (i = 0; i < DDIR_RWDIR_CNT; i++) |
826 | bytes_done[i] = td->bytes_done[i]; | |
2e1df07d JA |
827 | |
828 | if (in_ramp_time(td)) | |
829 | td_set_runstate(td, TD_RAMP); | |
830 | else | |
831 | td_set_runstate(td, TD_RUNNING); | |
832 | ||
3e260a46 JA |
833 | lat_target_init(td); |
834 | ||
1e564979 JE |
835 | total_bytes = td->o.size; |
836 | /* | |
837 | * Allow random overwrite workloads to write up to io_limit | |
838 | * before starting verification phase as 'size' doesn't apply. | |
839 | */ | |
840 | if (td_write(td) && td_random(td) && td->o.norandommap) | |
841 | total_bytes = max(total_bytes, (uint64_t) td->o.io_limit); | |
78a6469c JA |
842 | /* |
843 | * If verify_backlog is enabled, we'll run the verify in this | |
844 | * handler as well. For that case, we may need up to twice the | |
845 | * amount of bytes. | |
846 | */ | |
78a6469c JA |
847 | if (td->o.verify != VERIFY_NONE && |
848 | (td_write(td) && td->o.verify_backlog)) | |
c2703bf3 JA |
849 | total_bytes += td->o.size; |
850 | ||
82a90686 | 851 | /* In trimwrite mode, each byte is trimmed and then written, so |
0e4dd95c | 852 | * allow total_bytes to be twice as big */ |
82a90686 | 853 | if (td_trimwrite(td)) |
0e4dd95c DE |
854 | total_bytes += td->total_io_size; |
855 | ||
f7078f7b | 856 | while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) || |
e28dd2cf | 857 | (!flist_empty(&td->trim_list)) || !io_issue_bytes_exceeded(td) || |
c04e4661 | 858 | td->o.time_based) { |
2e1df07d | 859 | struct timeval comp_time; |
2e1df07d | 860 | struct io_u *io_u; |
e9d512d8 | 861 | int full; |
2e1df07d JA |
862 | enum fio_ddir ddir; |
863 | ||
c97f1ad6 JA |
864 | check_update_rusage(td); |
865 | ||
7d7803fa | 866 | if (td->terminate || td->done) |
2e1df07d JA |
867 | break; |
868 | ||
869 | update_tv_cache(td); | |
870 | ||
871 | if (runtime_exceeded(td, &td->tv_cache)) { | |
872 | __update_tv_cache(td); | |
873 | if (runtime_exceeded(td, &td->tv_cache)) { | |
ebea2133 | 874 | fio_mark_td_terminate(td); |
2e1df07d JA |
875 | break; |
876 | } | |
877 | } | |
878 | ||
9e684a49 DE |
879 | if (flow_threshold_exceeded(td)) |
880 | continue; | |
881 | ||
c82ea3d4 | 882 | if (!td->o.time_based && bytes_issued >= total_bytes) |
20876c53 JC |
883 | break; |
884 | ||
2e1df07d | 885 | io_u = get_io_u(td); |
002fe734 JA |
886 | if (IS_ERR_OR_NULL(io_u)) { |
887 | int err = PTR_ERR(io_u); | |
888 | ||
889 | io_u = NULL; | |
890 | if (err == -EBUSY) { | |
891 | ret = FIO_Q_BUSY; | |
892 | goto reap; | |
893 | } | |
3e260a46 JA |
894 | if (td->o.latency_target) |
895 | goto reap; | |
2e1df07d | 896 | break; |
3e260a46 | 897 | } |
2e1df07d JA |
898 | |
899 | ddir = io_u->ddir; | |
900 | ||
901 | /* | |
82af2a7c JA |
902 | * Add verification end_io handler if: |
903 | * - Asked to verify (!td_rw(td)) | |
904 | * - Or the io_u is from our verify list (mixed write/ver) | |
2e1df07d JA |
905 | */ |
906 | if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ && | |
82af2a7c | 907 | ((io_u->flags & IO_U_F_VER_LIST) || !td_rw(td))) { |
c4b6117b PV |
908 | |
909 | if (!td->o.verify_pattern_bytes) { | |
d6b72507 | 910 | io_u->rand_seed = __rand(&td->verify_state); |
c4b6117b | 911 | if (sizeof(int) != sizeof(long *)) |
d6b72507 | 912 | io_u->rand_seed *= __rand(&td->verify_state); |
c4b6117b PV |
913 | } |
914 | ||
ca09be4b JA |
915 | if (verify_state_should_stop(td, io_u)) { |
916 | put_io_u(td, io_u); | |
917 | break; | |
918 | } | |
919 | ||
2e1df07d JA |
920 | if (td->o.verify_async) |
921 | io_u->end_io = verify_io_u_async; | |
922 | else | |
923 | io_u->end_io = verify_io_u; | |
924 | td_set_runstate(td, TD_VERIFYING); | |
925 | } else if (in_ramp_time(td)) | |
926 | td_set_runstate(td, TD_RAMP); | |
927 | else | |
928 | td_set_runstate(td, TD_RUNNING); | |
929 | ||
9a50c5c5 | 930 | /* |
f9401285 JA |
931 | * Always log IO before it's issued, so we know the specific |
932 | * order of it. The logged unit will track when the IO has | |
933 | * completed. | |
9a50c5c5 | 934 | */ |
c4b6117b PV |
935 | if (td_write(td) && io_u->ddir == DDIR_WRITE && |
936 | td->o.do_verify && | |
937 | td->o.verify != VERIFY_NONE && | |
f9401285 | 938 | !td->o.experimental_verify) |
c4b6117b PV |
939 | log_io_piece(td, io_u); |
940 | ||
a9da8ab2 | 941 | if (td->o.io_submit_mode == IO_MODE_OFFLOAD) { |
0c5df5f9 JA |
942 | const unsigned long blen = io_u->xfer_buflen; |
943 | const enum fio_ddir ddir = acct_ddir(io_u); | |
944 | ||
a9da8ab2 JA |
945 | if (td->error) |
946 | break; | |
0c5df5f9 | 947 | |
26de50cf JA |
948 | workqueue_enqueue(&td->io_wq, &io_u->work); |
949 | ret = FIO_Q_QUEUED; | |
50a8ce86 | 950 | |
26de50cf | 951 | if (ddir_rw(ddir)) { |
0c5df5f9 JA |
952 | td->io_issues[ddir]++; |
953 | td->io_issue_bytes[ddir] += blen; | |
954 | td->rate_io_issue_bytes[ddir] += blen; | |
955 | } | |
956 | ||
50a8ce86 D |
957 | if (should_check_rate(td)) |
958 | td->rate_next_io_time[ddir] = usec_for_io(td, ddir); | |
959 | ||
a9da8ab2 JA |
960 | } else { |
961 | ret = td_io_queue(td, io_u); | |
2e1df07d | 962 | |
50a8ce86 D |
963 | if (should_check_rate(td)) |
964 | td->rate_next_io_time[ddir] = usec_for_io(td, ddir); | |
965 | ||
fd727d9d | 966 | if (io_queue_event(td, io_u, &ret, ddir, &bytes_issued, 0, &comp_time)) |
a9da8ab2 | 967 | break; |
2e1df07d | 968 | |
a9da8ab2 JA |
969 | /* |
970 | * See if we need to complete some commands. Note that | |
971 | * we can get BUSY even without IO queued, if the | |
972 | * system is resource starved. | |
973 | */ | |
3e260a46 | 974 | reap: |
a9da8ab2 JA |
975 | full = queue_full(td) || |
976 | (ret == FIO_Q_BUSY && td->cur_depth); | |
82407585 | 977 | if (full || io_in_polling(td)) |
a9da8ab2 JA |
978 | ret = wait_for_completions(td, &comp_time); |
979 | } | |
2e1df07d JA |
980 | if (ret < 0) |
981 | break; | |
55312f9f JA |
982 | if (!ddir_rw_sum(td->bytes_done) && |
983 | !(td->io_ops->flags & FIO_NOIO)) | |
2e1df07d JA |
984 | continue; |
985 | ||
55312f9f JA |
986 | if (!in_ramp_time(td) && should_check_rate(td)) { |
987 | if (check_min_rate(td, &comp_time)) { | |
f9cafb12 | 988 | if (exitall_on_terminate || td->o.exitall_error) |
2e1df07d JA |
989 | fio_terminate_threads(td->groupid); |
990 | td_verror(td, EIO, "check_min_rate"); | |
991 | break; | |
992 | } | |
993 | } | |
3e260a46 JA |
994 | if (!in_ramp_time(td) && td->o.latency_target) |
995 | lat_target_check(td); | |
e155cb64 | 996 | |
2e1df07d JA |
997 | if (td->o.thinktime) { |
998 | unsigned long long b; | |
999 | ||
342f4be4 | 1000 | b = ddir_rw_sum(td->io_blocks); |
2e1df07d JA |
1001 | if (!(b % td->o.thinktime_blocks)) { |
1002 | int left; | |
1003 | ||
002e7183 JA |
1004 | io_u_quiesce(td); |
1005 | ||
2e1df07d JA |
1006 | if (td->o.thinktime_spin) |
1007 | usec_spin(td->o.thinktime_spin); | |
1008 | ||
1009 | left = td->o.thinktime - td->o.thinktime_spin; | |
1010 | if (left) | |
1011 | usec_sleep(td, left); | |
1012 | } | |
1013 | } | |
1014 | } | |
1015 | ||
c97f1ad6 JA |
1016 | check_update_rusage(td); |
1017 | ||
2e1df07d | 1018 | if (td->trim_entries) |
4e0a8fa2 | 1019 | log_err("fio: %lu trim entries leaked?\n", td->trim_entries); |
2e1df07d JA |
1020 | |
1021 | if (td->o.fill_device && td->error == ENOSPC) { | |
1022 | td->error = 0; | |
ebea2133 | 1023 | fio_mark_td_terminate(td); |
2e1df07d JA |
1024 | } |
1025 | if (!td->error) { | |
1026 | struct fio_file *f; | |
1027 | ||
a9da8ab2 JA |
1028 | if (td->o.io_submit_mode == IO_MODE_OFFLOAD) { |
1029 | workqueue_flush(&td->io_wq); | |
1030 | i = 0; | |
1031 | } else | |
1032 | i = td->cur_depth; | |
1033 | ||
2e1df07d | 1034 | if (i) { |
55312f9f | 1035 | ret = io_u_queued_complete(td, i); |
2e1df07d JA |
1036 | if (td->o.fill_device && td->error == ENOSPC) |
1037 | td->error = 0; | |
1038 | } | |
1039 | ||
1040 | if (should_fsync(td) && td->o.end_fsync) { | |
1041 | td_set_runstate(td, TD_FSYNCING); | |
1042 | ||
1043 | for_each_file(td, f, i) { | |
61ee0f86 | 1044 | if (!fio_file_fsync(td, f)) |
2e1df07d | 1045 | continue; |
61ee0f86 JA |
1046 | |
1047 | log_err("fio: end_fsync failed for file %s\n", | |
1048 | f->file_name); | |
2e1df07d JA |
1049 | } |
1050 | } | |
1051 | } else | |
1052 | cleanup_pending_aio(td); | |
1053 | ||
1054 | /* | |
1055 | * stop job if we failed doing any IO | |
1056 | */ | |
342f4be4 | 1057 | if (!ddir_rw_sum(td->this_io_bytes)) |
2e1df07d | 1058 | td->done = 1; |
100f49f1 | 1059 | |
95b03be0 JA |
1060 | for (i = 0; i < DDIR_RWDIR_CNT; i++) |
1061 | bytes_done[i] = td->bytes_done[i] - bytes_done[i]; | |
2e1df07d JA |
1062 | } |
1063 | ||
1064 | static void cleanup_io_u(struct thread_data *td) | |
1065 | { | |
2e1df07d JA |
1066 | struct io_u *io_u; |
1067 | ||
2ae0b204 | 1068 | while ((io_u = io_u_qpop(&td->io_u_freelist)) != NULL) { |
c73ed246 JA |
1069 | |
1070 | if (td->io_ops->io_u_free) | |
1071 | td->io_ops->io_u_free(td, io_u); | |
1072 | ||
2e1df07d JA |
1073 | fio_memfree(io_u, sizeof(*io_u)); |
1074 | } | |
1075 | ||
1076 | free_io_mem(td); | |
2ae0b204 JA |
1077 | |
1078 | io_u_rexit(&td->io_u_requeues); | |
1079 | io_u_qexit(&td->io_u_freelist); | |
1080 | io_u_qexit(&td->io_u_all); | |
ca09be4b JA |
1081 | |
1082 | if (td->last_write_comp) | |
1083 | sfree(td->last_write_comp); | |
2e1df07d JA |
1084 | } |
1085 | ||
1086 | static int init_io_u(struct thread_data *td) | |
1087 | { | |
1088 | struct io_u *io_u; | |
9c42684e | 1089 | unsigned int max_bs, min_write; |
2e1df07d | 1090 | int cl_align, i, max_units; |
2ae0b204 | 1091 | int data_xfer = 1, err; |
2e1df07d JA |
1092 | char *p; |
1093 | ||
1094 | max_units = td->o.iodepth; | |
74f4b020 | 1095 | max_bs = td_max_bs(td); |
9c42684e | 1096 | min_write = td->o.min_bs[DDIR_WRITE]; |
2e1df07d JA |
1097 | td->orig_buffer_size = (unsigned long long) max_bs |
1098 | * (unsigned long long) max_units; | |
1099 | ||
88045e04 | 1100 | if ((td->io_ops->flags & FIO_NOIO) || !(td_read(td) || td_write(td))) |
59d8d0f5 JA |
1101 | data_xfer = 0; |
1102 | ||
2ae0b204 JA |
1103 | err = 0; |
1104 | err += io_u_rinit(&td->io_u_requeues, td->o.iodepth); | |
1105 | err += io_u_qinit(&td->io_u_freelist, td->o.iodepth); | |
1106 | err += io_u_qinit(&td->io_u_all, td->o.iodepth); | |
1107 | ||
1108 | if (err) { | |
1109 | log_err("fio: failed setting up IO queues\n"); | |
1110 | return 1; | |
1111 | } | |
1112 | ||
fd8a09b8 | 1113 | /* |
1114 | * if we may later need to do address alignment, then add any | |
1115 | * possible adjustment here so that we don't cause a buffer | |
1116 | * overflow later. this adjustment may be too much if we get | |
1117 | * lucky and the allocator gives us an aligned address. | |
1118 | */ | |
d01612f3 CM |
1119 | if (td->o.odirect || td->o.mem_align || td->o.oatomic || |
1120 | (td->io_ops->flags & FIO_RAWIO)) | |
fd8a09b8 | 1121 | td->orig_buffer_size += page_mask + td->o.mem_align; |
1122 | ||
2e1df07d JA |
1123 | if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) { |
1124 | unsigned long bs; | |
1125 | ||
1126 | bs = td->orig_buffer_size + td->o.hugepage_size - 1; | |
1127 | td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1); | |
1128 | } | |
1129 | ||
1130 | if (td->orig_buffer_size != (size_t) td->orig_buffer_size) { | |
1131 | log_err("fio: IO memory too large. Reduce max_bs or iodepth\n"); | |
1132 | return 1; | |
1133 | } | |
1134 | ||
59d8d0f5 | 1135 | if (data_xfer && allocate_io_mem(td)) |
2e1df07d JA |
1136 | return 1; |
1137 | ||
d01612f3 | 1138 | if (td->o.odirect || td->o.mem_align || td->o.oatomic || |
2e1df07d JA |
1139 | (td->io_ops->flags & FIO_RAWIO)) |
1140 | p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align; | |
1141 | else | |
1142 | p = td->orig_buffer; | |
1143 | ||
1144 | cl_align = os_cache_line_size(); | |
1145 | ||
1146 | for (i = 0; i < max_units; i++) { | |
1147 | void *ptr; | |
1148 | ||
1149 | if (td->terminate) | |
1150 | return 1; | |
1151 | ||
1152 | ptr = fio_memalign(cl_align, sizeof(*io_u)); | |
1153 | if (!ptr) { | |
1154 | log_err("fio: unable to allocate aligned memory\n"); | |
1155 | break; | |
1156 | } | |
1157 | ||
1158 | io_u = ptr; | |
1159 | memset(io_u, 0, sizeof(*io_u)); | |
2ae0b204 | 1160 | INIT_FLIST_HEAD(&io_u->verify_list); |
2e1df07d JA |
1161 | dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i); |
1162 | ||
59d8d0f5 | 1163 | if (data_xfer) { |
2e1df07d JA |
1164 | io_u->buf = p; |
1165 | dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf); | |
1166 | ||
1167 | if (td_write(td)) | |
9c42684e | 1168 | io_u_fill_buffer(td, io_u, min_write, max_bs); |
2e1df07d JA |
1169 | if (td_write(td) && td->o.verify_pattern_bytes) { |
1170 | /* | |
1171 | * Fill the buffer with the pattern if we are | |
1172 | * going to be doing writes. | |
1173 | */ | |
ce35b1ec | 1174 | fill_verify_pattern(td, io_u->buf, max_bs, io_u, 0, 0); |
2e1df07d JA |
1175 | } |
1176 | } | |
1177 | ||
1178 | io_u->index = i; | |
1179 | io_u->flags = IO_U_F_FREE; | |
2ae0b204 JA |
1180 | io_u_qpush(&td->io_u_freelist, io_u); |
1181 | ||
1182 | /* | |
1183 | * io_u never leaves this stack, used for iteration of all | |
1184 | * io_u buffers. | |
1185 | */ | |
1186 | io_u_qpush(&td->io_u_all, io_u); | |
c73ed246 JA |
1187 | |
1188 | if (td->io_ops->io_u_init) { | |
1189 | int ret = td->io_ops->io_u_init(td, io_u); | |
1190 | ||
1191 | if (ret) { | |
1192 | log_err("fio: failed to init engine data: %d\n", ret); | |
1193 | return 1; | |
1194 | } | |
1195 | } | |
1196 | ||
2e1df07d JA |
1197 | p += max_bs; |
1198 | } | |
1199 | ||
ca09be4b JA |
1200 | if (td->o.verify != VERIFY_NONE) { |
1201 | td->last_write_comp = scalloc(max_units, sizeof(uint64_t)); | |
1202 | if (!td->last_write_comp) { | |
1203 | log_err("fio: failed to alloc write comp data\n"); | |
1204 | return 1; | |
1205 | } | |
1206 | } | |
1207 | ||
2e1df07d JA |
1208 | return 0; |
1209 | } | |
1210 | ||
1211 | static int switch_ioscheduler(struct thread_data *td) | |
1212 | { | |
1213 | char tmp[256], tmp2[128]; | |
1214 | FILE *f; | |
1215 | int ret; | |
1216 | ||
1217 | if (td->io_ops->flags & FIO_DISKLESSIO) | |
1218 | return 0; | |
1219 | ||
1220 | sprintf(tmp, "%s/queue/scheduler", td->sysfs_root); | |
1221 | ||
1222 | f = fopen(tmp, "r+"); | |
1223 | if (!f) { | |
1224 | if (errno == ENOENT) { | |
1225 | log_err("fio: os or kernel doesn't support IO scheduler" | |
1226 | " switching\n"); | |
1227 | return 0; | |
1228 | } | |
1229 | td_verror(td, errno, "fopen iosched"); | |
1230 | return 1; | |
1231 | } | |
1232 | ||
1233 | /* | |
1234 | * Set io scheduler. | |
1235 | */ | |
1236 | ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f); | |
1237 | if (ferror(f) || ret != 1) { | |
1238 | td_verror(td, errno, "fwrite"); | |
1239 | fclose(f); | |
1240 | return 1; | |
1241 | } | |
1242 | ||
1243 | rewind(f); | |
1244 | ||
1245 | /* | |
1246 | * Read back and check that the selected scheduler is now the default. | |
1247 | */ | |
b44b9e45 | 1248 | memset(tmp, 0, sizeof(tmp)); |
49c6f33d | 1249 | ret = fread(tmp, sizeof(tmp), 1, f); |
2e1df07d JA |
1250 | if (ferror(f) || ret < 0) { |
1251 | td_verror(td, errno, "fread"); | |
1252 | fclose(f); | |
1253 | return 1; | |
1254 | } | |
b44b9e45 TK |
1255 | /* |
1256 | * either a list of io schedulers or "none\n" is expected. | |
1257 | */ | |
1258 | tmp[strlen(tmp) - 1] = '\0'; | |
49c6f33d | 1259 | |
2e1df07d JA |
1260 | |
1261 | sprintf(tmp2, "[%s]", td->o.ioscheduler); | |
1262 | if (!strstr(tmp, tmp2)) { | |
1263 | log_err("fio: io scheduler %s not found\n", td->o.ioscheduler); | |
1264 | td_verror(td, EINVAL, "iosched_switch"); | |
1265 | fclose(f); | |
1266 | return 1; | |
1267 | } | |
1268 | ||
1269 | fclose(f); | |
1270 | return 0; | |
1271 | } | |
1272 | ||
e39c0676 | 1273 | static bool keep_running(struct thread_data *td) |
2e1df07d | 1274 | { |
77731b29 JA |
1275 | unsigned long long limit; |
1276 | ||
2e1df07d | 1277 | if (td->done) |
e39c0676 | 1278 | return false; |
2e1df07d | 1279 | if (td->o.time_based) |
e39c0676 | 1280 | return true; |
2e1df07d JA |
1281 | if (td->o.loops) { |
1282 | td->o.loops--; | |
e39c0676 | 1283 | return true; |
2e1df07d | 1284 | } |
3939fe85 | 1285 | if (exceeds_number_ios(td)) |
e39c0676 | 1286 | return false; |
26251d8d | 1287 | |
77731b29 JA |
1288 | if (td->o.io_limit) |
1289 | limit = td->o.io_limit; | |
1290 | else | |
1291 | limit = td->o.size; | |
1292 | ||
1293 | if (limit != -1ULL && ddir_rw_sum(td->io_bytes) < limit) { | |
5bd5f71a JA |
1294 | uint64_t diff; |
1295 | ||
1296 | /* | |
1297 | * If the difference is less than the minimum IO size, we | |
1298 | * are done. | |
1299 | */ | |
77731b29 | 1300 | diff = limit - ddir_rw_sum(td->io_bytes); |
74f4b020 | 1301 | if (diff < td_max_bs(td)) |
e39c0676 | 1302 | return false; |
5bd5f71a | 1303 | |
543e2e9d | 1304 | if (fio_files_done(td) && !td->o.io_limit) |
e39c0676 | 1305 | return false; |
002fe734 | 1306 | |
e39c0676 | 1307 | return true; |
5bd5f71a | 1308 | } |
2e1df07d | 1309 | |
e39c0676 | 1310 | return false; |
2e1df07d JA |
1311 | } |
1312 | ||
ce486495 | 1313 | static int exec_string(struct thread_options *o, const char *string, const char *mode) |
2e1df07d | 1314 | { |
6c9ce469 | 1315 | size_t newlen = strlen(string) + strlen(o->name) + strlen(mode) + 9 + 1; |
f491a907 | 1316 | int ret; |
2e1df07d JA |
1317 | char *str; |
1318 | ||
1319 | str = malloc(newlen); | |
ce486495 | 1320 | sprintf(str, "%s &> %s.%s.txt", string, o->name, mode); |
2e1df07d | 1321 | |
ce486495 | 1322 | log_info("%s : Saving output of %s in %s.%s.txt\n",o->name, mode, o->name, mode); |
2e1df07d JA |
1323 | ret = system(str); |
1324 | if (ret == -1) | |
1325 | log_err("fio: exec of cmd <%s> failed\n", str); | |
1326 | ||
1327 | free(str); | |
1328 | return ret; | |
1329 | } | |
1330 | ||
62167762 JC |
1331 | /* |
1332 | * Dry run to compute correct state of numberio for verification. | |
1333 | */ | |
1334 | static uint64_t do_dry_run(struct thread_data *td) | |
1335 | { | |
62167762 JC |
1336 | td_set_runstate(td, TD_RUNNING); |
1337 | ||
1338 | while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) || | |
e28dd2cf | 1339 | (!flist_empty(&td->trim_list)) || !io_complete_bytes_exceeded(td)) { |
62167762 JC |
1340 | struct io_u *io_u; |
1341 | int ret; | |
1342 | ||
1343 | if (td->terminate || td->done) | |
1344 | break; | |
1345 | ||
1346 | io_u = get_io_u(td); | |
1347 | if (!io_u) | |
1348 | break; | |
1349 | ||
a9da8ab2 | 1350 | io_u_set(io_u, IO_U_F_FLIGHT); |
62167762 JC |
1351 | io_u->error = 0; |
1352 | io_u->resid = 0; | |
1353 | if (ddir_rw(acct_ddir(io_u))) | |
1354 | td->io_issues[acct_ddir(io_u)]++; | |
1355 | if (ddir_rw(io_u->ddir)) { | |
1356 | io_u_mark_depth(td, 1); | |
1357 | td->ts.total_io_u[io_u->ddir]++; | |
1358 | } | |
1359 | ||
2e63e96b PV |
1360 | if (td_write(td) && io_u->ddir == DDIR_WRITE && |
1361 | td->o.do_verify && | |
1362 | td->o.verify != VERIFY_NONE && | |
1363 | !td->o.experimental_verify) | |
1364 | log_io_piece(td, io_u); | |
1365 | ||
55312f9f | 1366 | ret = io_u_sync_complete(td, io_u); |
62167762 JC |
1367 | (void) ret; |
1368 | } | |
1369 | ||
55312f9f | 1370 | return td->bytes_done[DDIR_WRITE] + td->bytes_done[DDIR_TRIM]; |
62167762 JC |
1371 | } |
1372 | ||
24660963 JA |
1373 | struct fork_data { |
1374 | struct thread_data *td; | |
1375 | struct sk_out *sk_out; | |
1376 | }; | |
1377 | ||
2e1df07d JA |
1378 | /* |
1379 | * Entry point for the thread based jobs. The process based jobs end up | |
1380 | * here as well, after a little setup. | |
1381 | */ | |
1382 | static void *thread_main(void *data) | |
1383 | { | |
24660963 | 1384 | struct fork_data *fd = data; |
95603b74 | 1385 | unsigned long long elapsed_us[DDIR_RWDIR_CNT] = { 0, }; |
24660963 | 1386 | struct thread_data *td = fd->td; |
4896473e | 1387 | struct thread_options *o = &td->o; |
24660963 | 1388 | struct sk_out *sk_out = fd->sk_out; |
2e1df07d JA |
1389 | pthread_condattr_t attr; |
1390 | int clear_state; | |
28727df7 | 1391 | int ret; |
2e1df07d | 1392 | |
24660963 JA |
1393 | sk_out_assign(sk_out); |
1394 | free(fd); | |
1395 | ||
4896473e | 1396 | if (!o->use_thread) { |
2e1df07d JA |
1397 | setsid(); |
1398 | td->pid = getpid(); | |
1399 | } else | |
1400 | td->pid = gettid(); | |
1401 | ||
4896473e | 1402 | fio_local_clock_init(o->use_thread); |
5d879392 | 1403 | |
2e1df07d JA |
1404 | dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid); |
1405 | ||
122c7725 JA |
1406 | if (is_backend) |
1407 | fio_server_send_start(td); | |
1408 | ||
2e1df07d JA |
1409 | INIT_FLIST_HEAD(&td->io_log_list); |
1410 | INIT_FLIST_HEAD(&td->io_hist_list); | |
1411 | INIT_FLIST_HEAD(&td->verify_list); | |
1412 | INIT_FLIST_HEAD(&td->trim_list); | |
1ae83d45 | 1413 | INIT_FLIST_HEAD(&td->next_rand_list); |
2e1df07d JA |
1414 | pthread_mutex_init(&td->io_u_lock, NULL); |
1415 | td->io_hist_tree = RB_ROOT; | |
1416 | ||
1417 | pthread_condattr_init(&attr); | |
1418 | pthread_cond_init(&td->verify_cond, &attr); | |
1419 | pthread_cond_init(&td->free_cond, &attr); | |
1420 | ||
1421 | td_set_runstate(td, TD_INITIALIZED); | |
1422 | dprint(FD_MUTEX, "up startup_mutex\n"); | |
1423 | fio_mutex_up(startup_mutex); | |
1424 | dprint(FD_MUTEX, "wait on td->mutex\n"); | |
1425 | fio_mutex_down(td->mutex); | |
1426 | dprint(FD_MUTEX, "done waiting on td->mutex\n"); | |
1427 | ||
2e1df07d JA |
1428 | /* |
1429 | * A new gid requires privilege, so we need to do this before setting | |
1430 | * the uid. | |
1431 | */ | |
4896473e | 1432 | if (o->gid != -1U && setgid(o->gid)) { |
2e1df07d JA |
1433 | td_verror(td, errno, "setgid"); |
1434 | goto err; | |
1435 | } | |
4896473e | 1436 | if (o->uid != -1U && setuid(o->uid)) { |
2e1df07d JA |
1437 | td_verror(td, errno, "setuid"); |
1438 | goto err; | |
1439 | } | |
1440 | ||
1441 | /* | |
1442 | * If we have a gettimeofday() thread, make sure we exclude that | |
1443 | * thread from this job | |
1444 | */ | |
4896473e JA |
1445 | if (o->gtod_cpu) |
1446 | fio_cpu_clear(&o->cpumask, o->gtod_cpu); | |
2e1df07d JA |
1447 | |
1448 | /* | |
1449 | * Set affinity first, in case it has an impact on the memory | |
1450 | * allocations. | |
1451 | */ | |
b2a9e649 | 1452 | if (fio_option_is_set(o, cpumask)) { |
c2acfbac | 1453 | if (o->cpus_allowed_policy == FIO_CPUS_SPLIT) { |
30cb4c65 | 1454 | ret = fio_cpus_split(&o->cpumask, td->thread_number - 1); |
c2acfbac JA |
1455 | if (!ret) { |
1456 | log_err("fio: no CPUs set\n"); | |
1457 | log_err("fio: Try increasing number of available CPUs\n"); | |
1458 | td_verror(td, EINVAL, "cpus_split"); | |
1459 | goto err; | |
1460 | } | |
1461 | } | |
28727df7 JA |
1462 | ret = fio_setaffinity(td->pid, o->cpumask); |
1463 | if (ret == -1) { | |
4896473e JA |
1464 | td_verror(td, errno, "cpu_set_affinity"); |
1465 | goto err; | |
1466 | } | |
2e1df07d JA |
1467 | } |
1468 | ||
67bf9823 | 1469 | #ifdef CONFIG_LIBNUMA |
d0b937ed | 1470 | /* numa node setup */ |
b2a9e649 JA |
1471 | if (fio_option_is_set(o, numa_cpunodes) || |
1472 | fio_option_is_set(o, numa_memnodes)) { | |
43522848 | 1473 | struct bitmask *mask; |
d0b937ed YR |
1474 | |
1475 | if (numa_available() < 0) { | |
1476 | td_verror(td, errno, "Does not support NUMA API\n"); | |
1477 | goto err; | |
1478 | } | |
1479 | ||
b2a9e649 | 1480 | if (fio_option_is_set(o, numa_cpunodes)) { |
43522848 DG |
1481 | mask = numa_parse_nodestring(o->numa_cpunodes); |
1482 | ret = numa_run_on_node_mask(mask); | |
1483 | numa_free_nodemask(mask); | |
d0b937ed YR |
1484 | if (ret == -1) { |
1485 | td_verror(td, errno, \ | |
1486 | "numa_run_on_node_mask failed\n"); | |
1487 | goto err; | |
1488 | } | |
1489 | } | |
1490 | ||
b2a9e649 | 1491 | if (fio_option_is_set(o, numa_memnodes)) { |
43522848 DG |
1492 | mask = NULL; |
1493 | if (o->numa_memnodes) | |
1494 | mask = numa_parse_nodestring(o->numa_memnodes); | |
1495 | ||
4896473e | 1496 | switch (o->numa_mem_mode) { |
d0b937ed | 1497 | case MPOL_INTERLEAVE: |
43522848 | 1498 | numa_set_interleave_mask(mask); |
d0b937ed YR |
1499 | break; |
1500 | case MPOL_BIND: | |
43522848 | 1501 | numa_set_membind(mask); |
d0b937ed YR |
1502 | break; |
1503 | case MPOL_LOCAL: | |
1504 | numa_set_localalloc(); | |
1505 | break; | |
1506 | case MPOL_PREFERRED: | |
4896473e | 1507 | numa_set_preferred(o->numa_mem_prefer_node); |
d0b937ed YR |
1508 | break; |
1509 | case MPOL_DEFAULT: | |
1510 | default: | |
1511 | break; | |
1512 | } | |
1513 | ||
43522848 DG |
1514 | if (mask) |
1515 | numa_free_nodemask(mask); | |
1516 | ||
d0b937ed YR |
1517 | } |
1518 | } | |
1519 | #endif | |
1520 | ||
9a3f1100 JA |
1521 | if (fio_pin_memory(td)) |
1522 | goto err; | |
1523 | ||
2e1df07d JA |
1524 | /* |
1525 | * May alter parameters that init_io_u() will use, so we need to | |
1526 | * do this first. | |
1527 | */ | |
1528 | if (init_iolog(td)) | |
1529 | goto err; | |
1530 | ||
1531 | if (init_io_u(td)) | |
1532 | goto err; | |
1533 | ||
4896473e | 1534 | if (o->verify_async && verify_async_init(td)) |
2e1df07d JA |
1535 | goto err; |
1536 | ||
27d74836 JA |
1537 | if (fio_option_is_set(o, ioprio) || |
1538 | fio_option_is_set(o, ioprio_class)) { | |
28727df7 JA |
1539 | ret = ioprio_set(IOPRIO_WHO_PROCESS, 0, o->ioprio_class, o->ioprio); |
1540 | if (ret == -1) { | |
2e1df07d JA |
1541 | td_verror(td, errno, "ioprio_set"); |
1542 | goto err; | |
1543 | } | |
1544 | } | |
1545 | ||
4896473e | 1546 | if (o->cgroup && cgroup_setup(td, cgroup_list, &cgroup_mnt)) |
2e1df07d JA |
1547 | goto err; |
1548 | ||
649c10c5 | 1549 | errno = 0; |
4896473e | 1550 | if (nice(o->nice) == -1 && errno != 0) { |
2e1df07d JA |
1551 | td_verror(td, errno, "nice"); |
1552 | goto err; | |
1553 | } | |
1554 | ||
4896473e | 1555 | if (o->ioscheduler && switch_ioscheduler(td)) |
2e1df07d JA |
1556 | goto err; |
1557 | ||
4896473e | 1558 | if (!o->create_serialize && setup_files(td)) |
2e1df07d JA |
1559 | goto err; |
1560 | ||
1561 | if (td_io_init(td)) | |
1562 | goto err; | |
1563 | ||
1564 | if (init_random_map(td)) | |
1565 | goto err; | |
1566 | ||
ce486495 | 1567 | if (o->exec_prerun && exec_string(o, o->exec_prerun, (const char *)"prerun")) |
4896473e | 1568 | goto err; |
2e1df07d | 1569 | |
4896473e | 1570 | if (o->pre_read) { |
2e1df07d JA |
1571 | if (pre_read_files(td) < 0) |
1572 | goto err; | |
1573 | } | |
1574 | ||
24660963 | 1575 | if (iolog_compress_init(td, sk_out)) |
155f2f02 | 1576 | goto err; |
aee2ab67 | 1577 | |
dc5bfbb2 JA |
1578 | fio_verify_init(td); |
1579 | ||
24660963 | 1580 | if (rate_submit_init(td, sk_out)) |
a9da8ab2 JA |
1581 | goto err; |
1582 | ||
2e1df07d | 1583 | fio_gettime(&td->epoch, NULL); |
44404c5a | 1584 | fio_getrusage(&td->ru_start); |
ac28d905 JA |
1585 | memcpy(&td->bw_sample_time, &td->epoch, sizeof(td->epoch)); |
1586 | memcpy(&td->iops_sample_time, &td->epoch, sizeof(td->epoch)); | |
1587 | ||
1588 | if (o->ratemin[DDIR_READ] || o->ratemin[DDIR_WRITE] || | |
1589 | o->ratemin[DDIR_TRIM]) { | |
1590 | memcpy(&td->lastrate[DDIR_READ], &td->bw_sample_time, | |
1591 | sizeof(td->bw_sample_time)); | |
1592 | memcpy(&td->lastrate[DDIR_WRITE], &td->bw_sample_time, | |
1593 | sizeof(td->bw_sample_time)); | |
1594 | memcpy(&td->lastrate[DDIR_TRIM], &td->bw_sample_time, | |
1595 | sizeof(td->bw_sample_time)); | |
1596 | } | |
1597 | ||
2e1df07d JA |
1598 | clear_state = 0; |
1599 | while (keep_running(td)) { | |
100f49f1 JA |
1600 | uint64_t verify_bytes; |
1601 | ||
2e1df07d | 1602 | fio_gettime(&td->start, NULL); |
2e1df07d JA |
1603 | memcpy(&td->tv_cache, &td->start, sizeof(td->start)); |
1604 | ||
2e1df07d | 1605 | if (clear_state) |
ac28d905 | 1606 | clear_io_state(td, 0); |
2e1df07d JA |
1607 | |
1608 | prune_io_piece_log(td); | |
1609 | ||
62167762 JC |
1610 | if (td->o.verify_only && (td_write(td) || td_rw(td))) |
1611 | verify_bytes = do_dry_run(td); | |
095196b1 | 1612 | else { |
95b03be0 JA |
1613 | uint64_t bytes_done[DDIR_RWDIR_CNT]; |
1614 | ||
1615 | do_io(td, bytes_done); | |
1616 | ||
1617 | if (!ddir_rw_sum(bytes_done)) { | |
095196b1 | 1618 | fio_mark_td_terminate(td); |
95b03be0 JA |
1619 | verify_bytes = 0; |
1620 | } else { | |
1621 | verify_bytes = bytes_done[DDIR_WRITE] + | |
1622 | bytes_done[DDIR_TRIM]; | |
1623 | } | |
095196b1 | 1624 | } |
2e1df07d JA |
1625 | |
1626 | clear_state = 1; | |
1627 | ||
40c666c8 JA |
1628 | /* |
1629 | * Make sure we've successfully updated the rusage stats | |
1630 | * before waiting on the stat mutex. Otherwise we could have | |
1631 | * the stat thread holding stat mutex and waiting for | |
1632 | * the rusage_sem, which would never get upped because | |
1633 | * this thread is waiting for the stat mutex. | |
1634 | */ | |
1635 | check_update_rusage(td); | |
1636 | ||
e5437a07 | 1637 | fio_mutex_down(stat_mutex); |
95603b74 BF |
1638 | if (td_read(td) && td->io_bytes[DDIR_READ]) |
1639 | update_runtime(td, elapsed_us, DDIR_READ); | |
1640 | if (td_write(td) && td->io_bytes[DDIR_WRITE]) | |
1641 | update_runtime(td, elapsed_us, DDIR_WRITE); | |
1642 | if (td_trim(td) && td->io_bytes[DDIR_TRIM]) | |
1643 | update_runtime(td, elapsed_us, DDIR_TRIM); | |
e5437a07 VT |
1644 | fio_gettime(&td->start, NULL); |
1645 | fio_mutex_up(stat_mutex); | |
2e1df07d JA |
1646 | |
1647 | if (td->error || td->terminate) | |
1648 | break; | |
1649 | ||
4896473e JA |
1650 | if (!o->do_verify || |
1651 | o->verify == VERIFY_NONE || | |
2e1df07d JA |
1652 | (td->io_ops->flags & FIO_UNIDIR)) |
1653 | continue; | |
1654 | ||
ac28d905 | 1655 | clear_io_state(td, 0); |
2e1df07d JA |
1656 | |
1657 | fio_gettime(&td->start, NULL); | |
1658 | ||
100f49f1 | 1659 | do_verify(td, verify_bytes); |
2e1df07d | 1660 | |
40c666c8 JA |
1661 | /* |
1662 | * See comment further up for why this is done here. | |
1663 | */ | |
1664 | check_update_rusage(td); | |
1665 | ||
e5437a07 | 1666 | fio_mutex_down(stat_mutex); |
95603b74 | 1667 | update_runtime(td, elapsed_us, DDIR_READ); |
e5437a07 VT |
1668 | fio_gettime(&td->start, NULL); |
1669 | fio_mutex_up(stat_mutex); | |
2e1df07d JA |
1670 | |
1671 | if (td->error || td->terminate) | |
1672 | break; | |
1673 | } | |
1674 | ||
1675 | update_rusage_stat(td); | |
2e1df07d | 1676 | td->ts.total_run_time = mtime_since_now(&td->epoch); |
6eaf09d6 SL |
1677 | td->ts.io_bytes[DDIR_READ] = td->io_bytes[DDIR_READ]; |
1678 | td->ts.io_bytes[DDIR_WRITE] = td->io_bytes[DDIR_WRITE]; | |
1679 | td->ts.io_bytes[DDIR_TRIM] = td->io_bytes[DDIR_TRIM]; | |
2e1df07d | 1680 | |
ca09be4b | 1681 | if (td->o.verify_state_save && !(td->flags & TD_F_VSTATE_SAVED) && |
d264264a JA |
1682 | (td->o.verify != VERIFY_NONE && td_write(td))) |
1683 | verify_save_state(td->thread_number); | |
ca09be4b | 1684 | |
9a3f1100 JA |
1685 | fio_unpin_memory(td); |
1686 | ||
905e3d4f | 1687 | fio_writeout_logs(td); |
2e1df07d | 1688 | |
155f2f02 | 1689 | iolog_compress_exit(td); |
103b174e | 1690 | rate_submit_exit(td); |
aee2ab67 | 1691 | |
4896473e | 1692 | if (o->exec_postrun) |
ce486495 | 1693 | exec_string(o, o->exec_postrun, (const char *)"postrun"); |
2e1df07d | 1694 | |
f9cafb12 | 1695 | if (exitall_on_terminate || (o->exitall_error && td->error)) |
2e1df07d JA |
1696 | fio_terminate_threads(td->groupid); |
1697 | ||
1698 | err: | |
1699 | if (td->error) | |
1700 | log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error, | |
1701 | td->verror); | |
1702 | ||
4896473e | 1703 | if (o->verify_async) |
2e1df07d JA |
1704 | verify_async_exit(td); |
1705 | ||
1706 | close_and_free_files(td); | |
2e1df07d | 1707 | cleanup_io_u(td); |
32dbca2c | 1708 | close_ioengine(td); |
2e1df07d | 1709 | cgroup_shutdown(td, &cgroup_mnt); |
ca09be4b | 1710 | verify_free_state(td); |
2e1df07d | 1711 | |
b2a9e649 | 1712 | if (fio_option_is_set(o, cpumask)) { |
8a1db9a1 JA |
1713 | ret = fio_cpuset_exit(&o->cpumask); |
1714 | if (ret) | |
1715 | td_verror(td, ret, "fio_cpuset_exit"); | |
2e1df07d JA |
1716 | } |
1717 | ||
1718 | /* | |
1719 | * do this very late, it will log file closing as well | |
1720 | */ | |
4896473e | 1721 | if (o->write_iolog_file) |
2e1df07d JA |
1722 | write_iolog_close(td); |
1723 | ||
ea66e04f JA |
1724 | fio_mutex_remove(td->mutex); |
1725 | td->mutex = NULL; | |
1726 | ||
2e1df07d | 1727 | td_set_runstate(td, TD_EXITED); |
fda2cfac JA |
1728 | |
1729 | /* | |
1730 | * Do this last after setting our runstate to exited, so we | |
1731 | * know that the stat thread is signaled. | |
1732 | */ | |
1733 | check_update_rusage(td); | |
1734 | ||
24660963 | 1735 | sk_out_drop(); |
e43606c2 | 1736 | return (void *) (uintptr_t) td->error; |
2e1df07d JA |
1737 | } |
1738 | ||
1739 | ||
1740 | /* | |
1741 | * We cannot pass the td data into a forked process, so attach the td and | |
1742 | * pass it to the thread worker. | |
1743 | */ | |
24660963 | 1744 | static int fork_main(struct sk_out *sk_out, int shmid, int offset) |
2e1df07d | 1745 | { |
24660963 | 1746 | struct fork_data *fd; |
2e1df07d JA |
1747 | void *data, *ret; |
1748 | ||
c8931876 | 1749 | #if !defined(__hpux) && !defined(CONFIG_NO_SHM) |
2e1df07d JA |
1750 | data = shmat(shmid, NULL, 0); |
1751 | if (data == (void *) -1) { | |
1752 | int __err = errno; | |
1753 | ||
1754 | perror("shmat"); | |
1755 | return __err; | |
1756 | } | |
1757 | #else | |
1758 | /* | |
1759 | * HP-UX inherits shm mappings? | |
1760 | */ | |
1761 | data = threads; | |
1762 | #endif | |
1763 | ||
24660963 JA |
1764 | fd = calloc(1, sizeof(*fd)); |
1765 | fd->td = data + offset * sizeof(struct thread_data); | |
1766 | fd->sk_out = sk_out; | |
1767 | ret = thread_main(fd); | |
2e1df07d | 1768 | shmdt(data); |
e43606c2 | 1769 | return (int) (uintptr_t) ret; |
2e1df07d JA |
1770 | } |
1771 | ||
cba5460c JA |
1772 | static void dump_td_info(struct thread_data *td) |
1773 | { | |
1774 | log_err("fio: job '%s' hasn't exited in %lu seconds, it appears to " | |
1775 | "be stuck. Doing forceful exit of this job.\n", td->o.name, | |
1776 | (unsigned long) time_since_now(&td->terminate_time)); | |
1777 | } | |
1778 | ||
2e1df07d JA |
1779 | /* |
1780 | * Run over the job map and reap the threads that have exited, if any. | |
1781 | */ | |
1782 | static void reap_threads(unsigned int *nr_running, unsigned int *t_rate, | |
1783 | unsigned int *m_rate) | |
1784 | { | |
1785 | struct thread_data *td; | |
1786 | unsigned int cputhreads, realthreads, pending; | |
1787 | int i, status, ret; | |
1788 | ||
1789 | /* | |
1790 | * reap exited threads (TD_EXITED -> TD_REAPED) | |
1791 | */ | |
1792 | realthreads = pending = cputhreads = 0; | |
1793 | for_each_td(td, i) { | |
1794 | int flags = 0; | |
1795 | ||
1796 | /* | |
1797 | * ->io_ops is NULL for a thread that has closed its | |
1798 | * io engine | |
1799 | */ | |
1800 | if (td->io_ops && !strcmp(td->io_ops->name, "cpuio")) | |
1801 | cputhreads++; | |
1802 | else | |
1803 | realthreads++; | |
1804 | ||
1805 | if (!td->pid) { | |
1806 | pending++; | |
1807 | continue; | |
1808 | } | |
1809 | if (td->runstate == TD_REAPED) | |
1810 | continue; | |
1811 | if (td->o.use_thread) { | |
1812 | if (td->runstate == TD_EXITED) { | |
1813 | td_set_runstate(td, TD_REAPED); | |
1814 | goto reaped; | |
1815 | } | |
1816 | continue; | |
1817 | } | |
1818 | ||
1819 | flags = WNOHANG; | |
1820 | if (td->runstate == TD_EXITED) | |
1821 | flags = 0; | |
1822 | ||
1823 | /* | |
1824 | * check if someone quit or got killed in an unusual way | |
1825 | */ | |
1826 | ret = waitpid(td->pid, &status, flags); | |
1827 | if (ret < 0) { | |
1828 | if (errno == ECHILD) { | |
1829 | log_err("fio: pid=%d disappeared %d\n", | |
1830 | (int) td->pid, td->runstate); | |
a5e371a6 | 1831 | td->sig = ECHILD; |
2e1df07d JA |
1832 | td_set_runstate(td, TD_REAPED); |
1833 | goto reaped; | |
1834 | } | |
1835 | perror("waitpid"); | |
1836 | } else if (ret == td->pid) { | |
1837 | if (WIFSIGNALED(status)) { | |
1838 | int sig = WTERMSIG(status); | |
1839 | ||
36d80bc7 | 1840 | if (sig != SIGTERM && sig != SIGUSR2) |
2e1df07d JA |
1841 | log_err("fio: pid=%d, got signal=%d\n", |
1842 | (int) td->pid, sig); | |
a5e371a6 | 1843 | td->sig = sig; |
2e1df07d JA |
1844 | td_set_runstate(td, TD_REAPED); |
1845 | goto reaped; | |
1846 | } | |
1847 | if (WIFEXITED(status)) { | |
1848 | if (WEXITSTATUS(status) && !td->error) | |
1849 | td->error = WEXITSTATUS(status); | |
1850 | ||
1851 | td_set_runstate(td, TD_REAPED); | |
1852 | goto reaped; | |
1853 | } | |
1854 | } | |
1855 | ||
cba5460c JA |
1856 | /* |
1857 | * If the job is stuck, do a forceful timeout of it and | |
1858 | * move on. | |
1859 | */ | |
1860 | if (td->terminate && | |
1861 | time_since_now(&td->terminate_time) >= FIO_REAP_TIMEOUT) { | |
1862 | dump_td_info(td); | |
1863 | td_set_runstate(td, TD_REAPED); | |
1864 | goto reaped; | |
1865 | } | |
1866 | ||
2e1df07d JA |
1867 | /* |
1868 | * thread is not dead, continue | |
1869 | */ | |
1870 | pending++; | |
1871 | continue; | |
1872 | reaped: | |
1873 | (*nr_running)--; | |
342f4be4 JA |
1874 | (*m_rate) -= ddir_rw_sum(td->o.ratemin); |
1875 | (*t_rate) -= ddir_rw_sum(td->o.rate); | |
2e1df07d JA |
1876 | if (!td->pid) |
1877 | pending--; | |
1878 | ||
1879 | if (td->error) | |
1880 | exit_value++; | |
1881 | ||
1882 | done_secs += mtime_since_now(&td->epoch) / 1000; | |
4a88752a | 1883 | profile_td_exit(td); |
2e1df07d JA |
1884 | } |
1885 | ||
1886 | if (*nr_running == cputhreads && !pending && realthreads) | |
1887 | fio_terminate_threads(TERMINATE_ALL); | |
1888 | } | |
1889 | ||
e39c0676 | 1890 | static bool __check_trigger_file(void) |
ca09be4b JA |
1891 | { |
1892 | struct stat sb; | |
1893 | ||
1894 | if (!trigger_file) | |
e39c0676 | 1895 | return false; |
ca09be4b JA |
1896 | |
1897 | if (stat(trigger_file, &sb)) | |
e39c0676 | 1898 | return false; |
ca09be4b JA |
1899 | |
1900 | if (unlink(trigger_file) < 0) | |
1901 | log_err("fio: failed to unlink %s: %s\n", trigger_file, | |
1902 | strerror(errno)); | |
1903 | ||
e39c0676 | 1904 | return true; |
ca09be4b JA |
1905 | } |
1906 | ||
e39c0676 | 1907 | static bool trigger_timedout(void) |
ca09be4b JA |
1908 | { |
1909 | if (trigger_timeout) | |
1910 | return time_since_genesis() >= trigger_timeout; | |
1911 | ||
e39c0676 | 1912 | return false; |
ca09be4b JA |
1913 | } |
1914 | ||
1915 | void exec_trigger(const char *cmd) | |
1916 | { | |
1917 | int ret; | |
1918 | ||
1919 | if (!cmd) | |
1920 | return; | |
1921 | ||
1922 | ret = system(cmd); | |
1923 | if (ret == -1) | |
1924 | log_err("fio: failed executing %s trigger\n", cmd); | |
1925 | } | |
1926 | ||
1927 | void check_trigger_file(void) | |
1928 | { | |
1929 | if (__check_trigger_file() || trigger_timedout()) { | |
796fb3ce JA |
1930 | if (nr_clients) |
1931 | fio_clients_send_trigger(trigger_remote_cmd); | |
1932 | else { | |
d264264a | 1933 | verify_save_state(IO_LIST_ALL); |
ca09be4b JA |
1934 | fio_terminate_threads(TERMINATE_ALL); |
1935 | exec_trigger(trigger_cmd); | |
1936 | } | |
1937 | } | |
1938 | } | |
1939 | ||
1940 | static int fio_verify_load_state(struct thread_data *td) | |
1941 | { | |
1942 | int ret; | |
1943 | ||
1944 | if (!td->o.verify_state) | |
1945 | return 0; | |
1946 | ||
1947 | if (is_backend) { | |
1948 | void *data; | |
c3546b53 | 1949 | int ver; |
ca09be4b JA |
1950 | |
1951 | ret = fio_server_get_verify_state(td->o.name, | |
c3546b53 | 1952 | td->thread_number - 1, &data, &ver); |
ca09be4b | 1953 | if (!ret) |
c3546b53 | 1954 | verify_convert_assign_state(td, data, ver); |
ca09be4b JA |
1955 | } else |
1956 | ret = verify_load_state(td, "local"); | |
1957 | ||
1958 | return ret; | |
1959 | } | |
1960 | ||
06464907 JA |
1961 | static void do_usleep(unsigned int usecs) |
1962 | { | |
1963 | check_for_running_stats(); | |
ca09be4b | 1964 | check_trigger_file(); |
06464907 JA |
1965 | usleep(usecs); |
1966 | } | |
1967 | ||
e39c0676 | 1968 | static bool check_mount_writes(struct thread_data *td) |
e81ecca3 JA |
1969 | { |
1970 | struct fio_file *f; | |
1971 | unsigned int i; | |
1972 | ||
1973 | if (!td_write(td) || td->o.allow_mounted_write) | |
e39c0676 | 1974 | return false; |
e81ecca3 JA |
1975 | |
1976 | for_each_file(td, f, i) { | |
1977 | if (f->filetype != FIO_TYPE_BD) | |
1978 | continue; | |
1979 | if (device_is_mounted(f->file_name)) | |
1980 | goto mounted; | |
1981 | } | |
1982 | ||
e39c0676 | 1983 | return false; |
e81ecca3 JA |
1984 | mounted: |
1985 | log_err("fio: %s appears mounted, and 'allow_mounted_write' isn't set. Aborting.", f->file_name); | |
e39c0676 | 1986 | return true; |
e81ecca3 JA |
1987 | } |
1988 | ||
9cc8cb91 AK |
1989 | static bool waitee_running(struct thread_data *me) |
1990 | { | |
1991 | const char *waitee = me->o.wait_for; | |
1992 | const char *self = me->o.name; | |
1993 | struct thread_data *td; | |
1994 | int i; | |
1995 | ||
1996 | if (!waitee) | |
1997 | return false; | |
1998 | ||
1999 | for_each_td(td, i) { | |
2000 | if (!strcmp(td->o.name, self) || strcmp(td->o.name, waitee)) | |
2001 | continue; | |
2002 | ||
2003 | if (td->runstate < TD_EXITED) { | |
2004 | dprint(FD_PROCESS, "%s fenced by %s(%s)\n", | |
2005 | self, td->o.name, | |
2006 | runstate_to_name(td->runstate)); | |
2007 | return true; | |
2008 | } | |
2009 | } | |
2010 | ||
2011 | dprint(FD_PROCESS, "%s: %s completed, can run\n", self, waitee); | |
2012 | return false; | |
2013 | } | |
2014 | ||
2e1df07d JA |
2015 | /* |
2016 | * Main function for kicking off and reaping jobs, as needed. | |
2017 | */ | |
24660963 | 2018 | static void run_threads(struct sk_out *sk_out) |
2e1df07d JA |
2019 | { |
2020 | struct thread_data *td; | |
2e1df07d | 2021 | unsigned int i, todo, nr_running, m_rate, t_rate, nr_started; |
0de5b26f | 2022 | uint64_t spent; |
2e1df07d | 2023 | |
2e1df07d JA |
2024 | if (fio_gtod_offload && fio_start_gtod_thread()) |
2025 | return; | |
334185e9 | 2026 | |
f2a2ce0e | 2027 | fio_idle_prof_init(); |
2e1df07d JA |
2028 | |
2029 | set_sig_handlers(); | |
2030 | ||
3a5f6bde JA |
2031 | nr_thread = nr_process = 0; |
2032 | for_each_td(td, i) { | |
e81ecca3 JA |
2033 | if (check_mount_writes(td)) |
2034 | return; | |
3a5f6bde JA |
2035 | if (td->o.use_thread) |
2036 | nr_thread++; | |
2037 | else | |
2038 | nr_process++; | |
2039 | } | |
2040 | ||
01cfefcc | 2041 | if (output_format & FIO_OUTPUT_NORMAL) { |
2e1df07d JA |
2042 | log_info("Starting "); |
2043 | if (nr_thread) | |
2044 | log_info("%d thread%s", nr_thread, | |
2045 | nr_thread > 1 ? "s" : ""); | |
2046 | if (nr_process) { | |
2047 | if (nr_thread) | |
2048 | log_info(" and "); | |
2049 | log_info("%d process%s", nr_process, | |
2050 | nr_process > 1 ? "es" : ""); | |
2051 | } | |
2052 | log_info("\n"); | |
e411c301 | 2053 | log_info_flush(); |
2e1df07d JA |
2054 | } |
2055 | ||
2056 | todo = thread_number; | |
2057 | nr_running = 0; | |
2058 | nr_started = 0; | |
2059 | m_rate = t_rate = 0; | |
2060 | ||
2061 | for_each_td(td, i) { | |
2062 | print_status_init(td->thread_number - 1); | |
2063 | ||
2064 | if (!td->o.create_serialize) | |
2065 | continue; | |
2066 | ||
ca09be4b JA |
2067 | if (fio_verify_load_state(td)) |
2068 | goto reap; | |
2069 | ||
2e1df07d JA |
2070 | /* |
2071 | * do file setup here so it happens sequentially, | |
2072 | * we don't want X number of threads getting their | |
2073 | * client data interspersed on disk | |
2074 | */ | |
2075 | if (setup_files(td)) { | |
ca09be4b | 2076 | reap: |
2e1df07d JA |
2077 | exit_value++; |
2078 | if (td->error) | |
2079 | log_err("fio: pid=%d, err=%d/%s\n", | |
2080 | (int) td->pid, td->error, td->verror); | |
2081 | td_set_runstate(td, TD_REAPED); | |
2082 | todo--; | |
2083 | } else { | |
2084 | struct fio_file *f; | |
2085 | unsigned int j; | |
2086 | ||
2087 | /* | |
2088 | * for sharing to work, each job must always open | |
2089 | * its own files. so close them, if we opened them | |
2090 | * for creation | |
2091 | */ | |
2092 | for_each_file(td, f, j) { | |
2093 | if (fio_file_open(f)) | |
2094 | td_io_close_file(td, f); | |
2095 | } | |
2096 | } | |
2097 | } | |
2098 | ||
f2a2ce0e HL |
2099 | /* start idle threads before io threads start to run */ |
2100 | fio_idle_prof_start(); | |
2101 | ||
2e1df07d JA |
2102 | set_genesis_time(); |
2103 | ||
2104 | while (todo) { | |
2105 | struct thread_data *map[REAL_MAX_JOBS]; | |
2106 | struct timeval this_start; | |
2107 | int this_jobs = 0, left; | |
2108 | ||
2109 | /* | |
2110 | * create threads (TD_NOT_CREATED -> TD_CREATED) | |
2111 | */ | |
2112 | for_each_td(td, i) { | |
2113 | if (td->runstate != TD_NOT_CREATED) | |
2114 | continue; | |
2115 | ||
2116 | /* | |
2117 | * never got a chance to start, killed by other | |
2118 | * thread for some reason | |
2119 | */ | |
2120 | if (td->terminate) { | |
2121 | todo--; | |
2122 | continue; | |
2123 | } | |
2124 | ||
2125 | if (td->o.start_delay) { | |
0de5b26f | 2126 | spent = utime_since_genesis(); |
2e1df07d | 2127 | |
74454ce4 | 2128 | if (td->o.start_delay > spent) |
2e1df07d JA |
2129 | continue; |
2130 | } | |
2131 | ||
2132 | if (td->o.stonewall && (nr_started || nr_running)) { | |
2133 | dprint(FD_PROCESS, "%s: stonewall wait\n", | |
2134 | td->o.name); | |
2135 | break; | |
2136 | } | |
2137 | ||
9cc8cb91 AK |
2138 | if (waitee_running(td)) { |
2139 | dprint(FD_PROCESS, "%s: waiting for %s\n", | |
2140 | td->o.name, td->o.wait_for); | |
5c74fc76 | 2141 | continue; |
9cc8cb91 AK |
2142 | } |
2143 | ||
2e1df07d JA |
2144 | init_disk_util(td); |
2145 | ||
c97f1ad6 JA |
2146 | td->rusage_sem = fio_mutex_init(FIO_MUTEX_LOCKED); |
2147 | td->update_rusage = 0; | |
2148 | ||
2e1df07d JA |
2149 | /* |
2150 | * Set state to created. Thread will transition | |
2151 | * to TD_INITIALIZED when it's done setting up. | |
2152 | */ | |
2153 | td_set_runstate(td, TD_CREATED); | |
2154 | map[this_jobs++] = td; | |
2155 | nr_started++; | |
2156 | ||
2157 | if (td->o.use_thread) { | |
24660963 | 2158 | struct fork_data *fd; |
2e1df07d JA |
2159 | int ret; |
2160 | ||
24660963 JA |
2161 | fd = calloc(1, sizeof(*fd)); |
2162 | fd->td = td; | |
2163 | fd->sk_out = sk_out; | |
2164 | ||
2e1df07d JA |
2165 | dprint(FD_PROCESS, "will pthread_create\n"); |
2166 | ret = pthread_create(&td->thread, NULL, | |
24660963 | 2167 | thread_main, fd); |
2e1df07d JA |
2168 | if (ret) { |
2169 | log_err("pthread_create: %s\n", | |
2170 | strerror(ret)); | |
24660963 | 2171 | free(fd); |
2e1df07d JA |
2172 | nr_started--; |
2173 | break; | |
2174 | } | |
2175 | ret = pthread_detach(td->thread); | |
2176 | if (ret) | |
2177 | log_err("pthread_detach: %s", | |
2178 | strerror(ret)); | |
2179 | } else { | |
2180 | pid_t pid; | |
2181 | dprint(FD_PROCESS, "will fork\n"); | |
2182 | pid = fork(); | |
2183 | if (!pid) { | |
24660963 | 2184 | int ret = fork_main(sk_out, shm_id, i); |
2e1df07d JA |
2185 | |
2186 | _exit(ret); | |
2187 | } else if (i == fio_debug_jobno) | |
2188 | *fio_debug_jobp = pid; | |
2189 | } | |
2190 | dprint(FD_MUTEX, "wait on startup_mutex\n"); | |
09400a60 | 2191 | if (fio_mutex_down_timeout(startup_mutex, 10000)) { |
2e1df07d JA |
2192 | log_err("fio: job startup hung? exiting.\n"); |
2193 | fio_terminate_threads(TERMINATE_ALL); | |
2194 | fio_abort = 1; | |
2195 | nr_started--; | |
2196 | break; | |
2197 | } | |
2198 | dprint(FD_MUTEX, "done waiting on startup_mutex\n"); | |
2199 | } | |
2200 | ||
2201 | /* | |
2202 | * Wait for the started threads to transition to | |
2203 | * TD_INITIALIZED. | |
2204 | */ | |
2205 | fio_gettime(&this_start, NULL); | |
2206 | left = this_jobs; | |
2207 | while (left && !fio_abort) { | |
2208 | if (mtime_since_now(&this_start) > JOB_START_TIMEOUT) | |
2209 | break; | |
2210 | ||
06464907 | 2211 | do_usleep(100000); |
2e1df07d JA |
2212 | |
2213 | for (i = 0; i < this_jobs; i++) { | |
2214 | td = map[i]; | |
2215 | if (!td) | |
2216 | continue; | |
2217 | if (td->runstate == TD_INITIALIZED) { | |
2218 | map[i] = NULL; | |
2219 | left--; | |
2220 | } else if (td->runstate >= TD_EXITED) { | |
2221 | map[i] = NULL; | |
2222 | left--; | |
2223 | todo--; | |
2224 | nr_running++; /* work-around... */ | |
2225 | } | |
2226 | } | |
2227 | } | |
2228 | ||
2229 | if (left) { | |
4e87c37a JA |
2230 | log_err("fio: %d job%s failed to start\n", left, |
2231 | left > 1 ? "s" : ""); | |
2e1df07d JA |
2232 | for (i = 0; i < this_jobs; i++) { |
2233 | td = map[i]; | |
2234 | if (!td) | |
2235 | continue; | |
2236 | kill(td->pid, SIGTERM); | |
2237 | } | |
2238 | break; | |
2239 | } | |
2240 | ||
2241 | /* | |
2242 | * start created threads (TD_INITIALIZED -> TD_RUNNING). | |
2243 | */ | |
2244 | for_each_td(td, i) { | |
2245 | if (td->runstate != TD_INITIALIZED) | |
2246 | continue; | |
2247 | ||
2248 | if (in_ramp_time(td)) | |
2249 | td_set_runstate(td, TD_RAMP); | |
2250 | else | |
2251 | td_set_runstate(td, TD_RUNNING); | |
2252 | nr_running++; | |
2253 | nr_started--; | |
342f4be4 JA |
2254 | m_rate += ddir_rw_sum(td->o.ratemin); |
2255 | t_rate += ddir_rw_sum(td->o.rate); | |
2e1df07d JA |
2256 | todo--; |
2257 | fio_mutex_up(td->mutex); | |
2258 | } | |
2259 | ||
2260 | reap_threads(&nr_running, &t_rate, &m_rate); | |
2261 | ||
122c7725 | 2262 | if (todo) |
06464907 | 2263 | do_usleep(100000); |
2e1df07d JA |
2264 | } |
2265 | ||
2266 | while (nr_running) { | |
2267 | reap_threads(&nr_running, &t_rate, &m_rate); | |
06464907 | 2268 | do_usleep(10000); |
2e1df07d JA |
2269 | } |
2270 | ||
f2a2ce0e HL |
2271 | fio_idle_prof_stop(); |
2272 | ||
2e1df07d | 2273 | update_io_ticks(); |
2e1df07d JA |
2274 | } |
2275 | ||
5ddc6707 | 2276 | static void wait_for_helper_thread_exit(void) |
9ec7779f | 2277 | { |
8aab824f JA |
2278 | void *ret; |
2279 | ||
98b4b0a2 | 2280 | helper_exit = 1; |
5ddc6707 JA |
2281 | pthread_cond_signal(&helper_cond); |
2282 | pthread_join(helper_thread, &ret); | |
9ec7779f JA |
2283 | } |
2284 | ||
27357187 JA |
2285 | static void free_disk_util(void) |
2286 | { | |
27357187 | 2287 | disk_util_prune_entries(); |
8aab824f | 2288 | |
5ddc6707 | 2289 | pthread_cond_destroy(&helper_cond); |
27357187 JA |
2290 | } |
2291 | ||
5ddc6707 | 2292 | static void *helper_thread_main(void *data) |
2e1df07d | 2293 | { |
24660963 | 2294 | struct sk_out *sk_out = data; |
9ec7779f JA |
2295 | int ret = 0; |
2296 | ||
24660963 | 2297 | sk_out_assign(sk_out); |
2a2804c9 | 2298 | |
2e1df07d JA |
2299 | fio_mutex_up(startup_mutex); |
2300 | ||
8aab824f JA |
2301 | while (!ret) { |
2302 | uint64_t sec = DISK_UTIL_MSEC / 1000; | |
2303 | uint64_t nsec = (DISK_UTIL_MSEC % 1000) * 1000000; | |
2304 | struct timespec ts; | |
2305 | struct timeval tv; | |
2306 | ||
2307 | gettimeofday(&tv, NULL); | |
2308 | ts.tv_sec = tv.tv_sec + sec; | |
2309 | ts.tv_nsec = (tv.tv_usec * 1000) + nsec; | |
f893b76d | 2310 | |
823952af | 2311 | if (ts.tv_nsec >= 1000000000ULL) { |
8aab824f JA |
2312 | ts.tv_nsec -= 1000000000ULL; |
2313 | ts.tv_sec++; | |
2314 | } | |
2315 | ||
5ddc6707 | 2316 | pthread_cond_timedwait(&helper_cond, &helper_lock, &ts); |
8aab824f | 2317 | |
9ec7779f | 2318 | ret = update_io_ticks(); |
2e1df07d | 2319 | |
5ddc6707 JA |
2320 | if (helper_do_stat) { |
2321 | helper_do_stat = 0; | |
2322 | __show_running_run_stats(); | |
2323 | } | |
2324 | ||
2e1df07d JA |
2325 | if (!is_backend) |
2326 | print_thread_status(); | |
2327 | } | |
2328 | ||
24660963 | 2329 | sk_out_drop(); |
2e1df07d JA |
2330 | return NULL; |
2331 | } | |
2332 | ||
24660963 | 2333 | static int create_helper_thread(struct sk_out *sk_out) |
2e1df07d JA |
2334 | { |
2335 | int ret; | |
2336 | ||
9ec7779f JA |
2337 | setup_disk_util(); |
2338 | ||
5ddc6707 JA |
2339 | pthread_cond_init(&helper_cond, NULL); |
2340 | pthread_mutex_init(&helper_lock, NULL); | |
8aab824f | 2341 | |
24660963 | 2342 | ret = pthread_create(&helper_thread, NULL, helper_thread_main, sk_out); |
2e1df07d | 2343 | if (ret) { |
5ddc6707 | 2344 | log_err("Can't create helper thread: %s\n", strerror(ret)); |
2e1df07d JA |
2345 | return 1; |
2346 | } | |
2347 | ||
2e1df07d JA |
2348 | dprint(FD_MUTEX, "wait on startup_mutex\n"); |
2349 | fio_mutex_down(startup_mutex); | |
2350 | dprint(FD_MUTEX, "done waiting on startup_mutex\n"); | |
2351 | return 0; | |
2352 | } | |
2353 | ||
24660963 | 2354 | int fio_backend(struct sk_out *sk_out) |
2e1df07d JA |
2355 | { |
2356 | struct thread_data *td; | |
2357 | int i; | |
2358 | ||
2359 | if (exec_profile) { | |
2360 | if (load_profile(exec_profile)) | |
2361 | return 1; | |
2362 | free(exec_profile); | |
2363 | exec_profile = NULL; | |
2364 | } | |
2365 | if (!thread_number) | |
2366 | return 0; | |
2367 | ||
2368 | if (write_bw_log) { | |
aee2ab67 JA |
2369 | struct log_params p = { |
2370 | .log_type = IO_LOG_TYPE_BW, | |
2371 | }; | |
2372 | ||
2373 | setup_log(&agg_io_log[DDIR_READ], &p, "agg-read_bw.log"); | |
2374 | setup_log(&agg_io_log[DDIR_WRITE], &p, "agg-write_bw.log"); | |
2375 | setup_log(&agg_io_log[DDIR_TRIM], &p, "agg-trim_bw.log"); | |
2e1df07d JA |
2376 | } |
2377 | ||
521da527 | 2378 | startup_mutex = fio_mutex_init(FIO_MUTEX_LOCKED); |
2e1df07d JA |
2379 | if (startup_mutex == NULL) |
2380 | return 1; | |
2e1df07d JA |
2381 | |
2382 | set_genesis_time(); | |
cef9175e | 2383 | stat_init(); |
24660963 | 2384 | create_helper_thread(sk_out); |
2e1df07d JA |
2385 | |
2386 | cgroup_list = smalloc(sizeof(*cgroup_list)); | |
2387 | INIT_FLIST_HEAD(cgroup_list); | |
2388 | ||
24660963 | 2389 | run_threads(sk_out); |
2e1df07d | 2390 | |
5ddc6707 | 2391 | wait_for_helper_thread_exit(); |
8aab824f | 2392 | |
2e1df07d | 2393 | if (!fio_abort) { |
83f7b64e | 2394 | __show_run_stats(); |
2e1df07d | 2395 | if (write_bw_log) { |
cb7e0ace JA |
2396 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
2397 | struct io_log *log = agg_io_log[i]; | |
2398 | ||
3a5db920 | 2399 | flush_log(log, 0); |
518dac09 | 2400 | free_log(log); |
cb7e0ace | 2401 | } |
2e1df07d JA |
2402 | } |
2403 | } | |
2404 | ||
fda2cfac | 2405 | for_each_td(td, i) { |
2e1df07d | 2406 | fio_options_free(td); |
8049adc1 JA |
2407 | if (td->rusage_sem) { |
2408 | fio_mutex_remove(td->rusage_sem); | |
2409 | td->rusage_sem = NULL; | |
2410 | } | |
fda2cfac | 2411 | } |
2e1df07d | 2412 | |
a462baef | 2413 | free_disk_util(); |
2e1df07d JA |
2414 | cgroup_kill(cgroup_list); |
2415 | sfree(cgroup_list); | |
2416 | sfree(cgroup_mnt); | |
2417 | ||
2418 | fio_mutex_remove(startup_mutex); | |
cef9175e | 2419 | stat_exit(); |
2e1df07d JA |
2420 | return exit_value; |
2421 | } |