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