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