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