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