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