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