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