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> | |
ebac4655 | 29 | #include <assert.h> |
ebac4655 JA |
30 | #include <sys/stat.h> |
31 | #include <sys/wait.h> | |
32 | #include <sys/ipc.h> | |
33 | #include <sys/shm.h> | |
34 | #include <sys/ioctl.h> | |
35 | #include <sys/mman.h> | |
36 | ||
37 | #include "fio.h" | |
38 | #include "os.h" | |
39 | ||
40 | #define MASK (4095) | |
41 | ||
42 | #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK)) | |
43 | ||
44 | int groupid = 0; | |
45 | int thread_number = 0; | |
ebac4655 | 46 | int shm_id = 0; |
53cdc686 | 47 | int temp_stall_ts; |
c1d5725e | 48 | char *fio_inst_prefix = _INST_PREFIX; |
ebac4655 | 49 | |
bbfd6b00 | 50 | static volatile int startup_sem; |
ebac4655 JA |
51 | |
52 | #define TERMINATE_ALL (-1) | |
75154845 | 53 | #define JOB_START_TIMEOUT (5 * 1000) |
ebac4655 JA |
54 | |
55 | static void terminate_threads(int group_id) | |
56 | { | |
34572e28 | 57 | struct thread_data *td; |
ebac4655 JA |
58 | int i; |
59 | ||
34572e28 | 60 | for_each_td(td, i) { |
ebac4655 JA |
61 | if (group_id == TERMINATE_ALL || groupid == td->groupid) { |
62 | td->terminate = 1; | |
63 | td->start_delay = 0; | |
64 | } | |
65 | } | |
66 | } | |
67 | ||
68 | static void sig_handler(int sig) | |
69 | { | |
70 | switch (sig) { | |
71 | case SIGALRM: | |
72 | update_io_ticks(); | |
73 | disk_util_timer_arm(); | |
74 | print_thread_status(); | |
75 | break; | |
76 | default: | |
77 | printf("\nfio: terminating on signal\n"); | |
78 | fflush(stdout); | |
79 | terminate_threads(TERMINATE_ALL); | |
80 | break; | |
81 | } | |
82 | } | |
83 | ||
906c8d75 JA |
84 | /* |
85 | * Check if we are above the minimum rate given. | |
86 | */ | |
ebac4655 JA |
87 | static int check_min_rate(struct thread_data *td, struct timeval *now) |
88 | { | |
89 | unsigned long spent; | |
90 | unsigned long rate; | |
91 | int ddir = td->ddir; | |
92 | ||
93 | /* | |
94 | * allow a 2 second settle period in the beginning | |
95 | */ | |
96 | if (mtime_since(&td->start, now) < 2000) | |
97 | return 0; | |
98 | ||
99 | /* | |
100 | * if rate blocks is set, sample is running | |
101 | */ | |
102 | if (td->rate_bytes) { | |
103 | spent = mtime_since(&td->lastrate, now); | |
104 | if (spent < td->ratecycle) | |
105 | return 0; | |
106 | ||
107 | rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent; | |
108 | if (rate < td->ratemin) { | |
eb8bbf48 | 109 | fprintf(f_out, "%s: min rate %d not met, got %ldKiB/sec\n", td->name, td->ratemin, rate); |
ebac4655 JA |
110 | return 1; |
111 | } | |
112 | } | |
113 | ||
114 | td->rate_bytes = td->this_io_bytes[ddir]; | |
115 | memcpy(&td->lastrate, now, sizeof(*now)); | |
116 | return 0; | |
117 | } | |
118 | ||
119 | static inline int runtime_exceeded(struct thread_data *td, struct timeval *t) | |
120 | { | |
121 | if (!td->timeout) | |
122 | return 0; | |
123 | if (mtime_since(&td->epoch, t) >= td->timeout * 1000) | |
124 | return 1; | |
125 | ||
126 | return 0; | |
127 | } | |
128 | ||
ebac4655 JA |
129 | static inline void td_set_runstate(struct thread_data *td, int runstate) |
130 | { | |
ebac4655 JA |
131 | td->runstate = runstate; |
132 | } | |
133 | ||
53cdc686 JA |
134 | static struct fio_file *get_next_file(struct thread_data *td) |
135 | { | |
0ab8db89 | 136 | unsigned int old_next_file = td->next_file; |
b2a15192 JA |
137 | struct fio_file *f; |
138 | ||
139 | do { | |
140 | f = &td->files[td->next_file]; | |
141 | ||
142 | td->next_file++; | |
143 | if (td->next_file >= td->nr_files) | |
144 | td->next_file = 0; | |
145 | ||
146 | if (f->fd != -1) | |
147 | break; | |
53cdc686 | 148 | |
b2a15192 JA |
149 | f = NULL; |
150 | } while (td->next_file != old_next_file); | |
53cdc686 JA |
151 | |
152 | return f; | |
153 | } | |
154 | ||
906c8d75 JA |
155 | /* |
156 | * When job exits, we can cancel the in-flight IO if we are using async | |
157 | * io. Attempt to do so. | |
158 | */ | |
ebac4655 JA |
159 | static void cleanup_pending_aio(struct thread_data *td) |
160 | { | |
161 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; | |
162 | struct list_head *entry, *n; | |
163 | struct io_completion_data icd; | |
164 | struct io_u *io_u; | |
165 | int r; | |
166 | ||
167 | /* | |
168 | * get immediately available events, if any | |
169 | */ | |
45bee283 | 170 | r = td_io_getevents(td, 0, td->cur_depth, &ts); |
ebac4655 JA |
171 | if (r > 0) { |
172 | icd.nr = r; | |
173 | ios_completed(td, &icd); | |
174 | } | |
175 | ||
176 | /* | |
177 | * now cancel remaining active events | |
178 | */ | |
2866c82d | 179 | if (td->io_ops->cancel) { |
ebac4655 JA |
180 | list_for_each_safe(entry, n, &td->io_u_busylist) { |
181 | io_u = list_entry(entry, struct io_u, list); | |
182 | ||
2866c82d | 183 | r = td->io_ops->cancel(td, io_u); |
ebac4655 JA |
184 | if (!r) |
185 | put_io_u(td, io_u); | |
186 | } | |
187 | } | |
188 | ||
189 | if (td->cur_depth) { | |
45bee283 | 190 | r = td_io_getevents(td, td->cur_depth, td->cur_depth, NULL); |
ebac4655 JA |
191 | if (r > 0) { |
192 | icd.nr = r; | |
193 | ios_completed(td, &icd); | |
194 | } | |
195 | } | |
196 | } | |
197 | ||
858a3d47 JA |
198 | /* |
199 | * Helper to handle the final sync of a file. Works just like the normal | |
200 | * io path, just does everything sync. | |
201 | */ | |
202 | static int fio_io_sync(struct thread_data *td, struct fio_file *f) | |
203 | { | |
204 | struct io_u *io_u = __get_io_u(td); | |
205 | struct io_completion_data icd; | |
206 | int ret; | |
207 | ||
208 | if (!io_u) | |
209 | return 1; | |
210 | ||
211 | io_u->ddir = DDIR_SYNC; | |
212 | io_u->file = f; | |
213 | ||
214 | if (td_io_prep(td, io_u)) { | |
215 | put_io_u(td, io_u); | |
216 | return 1; | |
217 | } | |
218 | ||
219 | ret = td_io_queue(td, io_u); | |
220 | if (ret) { | |
353a7e0e | 221 | td_verror(td, io_u->error); |
858a3d47 | 222 | put_io_u(td, io_u); |
858a3d47 JA |
223 | return 1; |
224 | } | |
225 | ||
226 | ret = td_io_getevents(td, 1, td->cur_depth, NULL); | |
227 | if (ret < 0) { | |
353a7e0e | 228 | td_verror(td, ret); |
858a3d47 JA |
229 | return 1; |
230 | } | |
231 | ||
232 | icd.nr = ret; | |
233 | ios_completed(td, &icd); | |
234 | if (icd.error) { | |
235 | td_verror(td, icd.error); | |
236 | return 1; | |
237 | } | |
238 | ||
239 | return 0; | |
240 | } | |
241 | ||
906c8d75 JA |
242 | /* |
243 | * The main verify engine. Runs over the writes we previusly submitted, | |
244 | * reads the blocks back in, and checks the crc/md5 of the data. | |
245 | */ | |
a9619d44 | 246 | void do_verify(struct thread_data *td) |
ebac4655 JA |
247 | { |
248 | struct timeval t; | |
249 | struct io_u *io_u, *v_io_u = NULL; | |
250 | struct io_completion_data icd; | |
53cdc686 | 251 | struct fio_file *f; |
e5b401d4 JA |
252 | int ret, i; |
253 | ||
254 | /* | |
255 | * sync io first and invalidate cache, to make sure we really | |
256 | * read from disk. | |
257 | */ | |
258 | for_each_file(td, f, i) { | |
858a3d47 | 259 | fio_io_sync(td, f); |
e5b401d4 JA |
260 | file_invalidate_cache(td, f); |
261 | } | |
ebac4655 JA |
262 | |
263 | td_set_runstate(td, TD_VERIFYING); | |
264 | ||
265 | do { | |
266 | if (td->terminate) | |
267 | break; | |
268 | ||
269 | gettimeofday(&t, NULL); | |
270 | if (runtime_exceeded(td, &t)) | |
271 | break; | |
272 | ||
273 | io_u = __get_io_u(td); | |
274 | if (!io_u) | |
275 | break; | |
276 | ||
aea47d44 | 277 | if (get_next_verify(td, io_u)) { |
ebac4655 JA |
278 | put_io_u(td, io_u); |
279 | break; | |
280 | } | |
281 | ||
53cdc686 JA |
282 | f = get_next_file(td); |
283 | if (!f) | |
284 | break; | |
285 | ||
286 | io_u->file = f; | |
287 | ||
aea47d44 | 288 | if (td_io_prep(td, io_u)) { |
ebac4655 JA |
289 | put_io_u(td, io_u); |
290 | break; | |
291 | } | |
292 | ||
45bee283 | 293 | ret = td_io_queue(td, io_u); |
ebac4655 | 294 | if (ret) { |
353a7e0e | 295 | td_verror(td, io_u->error); |
ebac4655 | 296 | put_io_u(td, io_u); |
ebac4655 JA |
297 | break; |
298 | } | |
299 | ||
300 | /* | |
301 | * we have one pending to verify, do that while | |
302 | * we are doing io on the next one | |
303 | */ | |
304 | if (do_io_u_verify(td, &v_io_u)) | |
305 | break; | |
306 | ||
45bee283 | 307 | ret = td_io_getevents(td, 1, 1, NULL); |
ebac4655 JA |
308 | if (ret != 1) { |
309 | if (ret < 0) | |
310 | td_verror(td, ret); | |
311 | break; | |
312 | } | |
313 | ||
2866c82d | 314 | v_io_u = td->io_ops->event(td, 0); |
ebac4655 JA |
315 | icd.nr = 1; |
316 | icd.error = 0; | |
317 | io_completed(td, v_io_u, &icd); | |
318 | ||
319 | if (icd.error) { | |
320 | td_verror(td, icd.error); | |
321 | put_io_u(td, v_io_u); | |
322 | v_io_u = NULL; | |
323 | break; | |
324 | } | |
325 | ||
ebac4655 JA |
326 | /* |
327 | * if we can't submit more io, we need to verify now | |
328 | */ | |
329 | if (queue_full(td) && do_io_u_verify(td, &v_io_u)) | |
330 | break; | |
331 | ||
332 | } while (1); | |
333 | ||
334 | do_io_u_verify(td, &v_io_u); | |
335 | ||
336 | if (td->cur_depth) | |
337 | cleanup_pending_aio(td); | |
338 | ||
339 | td_set_runstate(td, TD_RUNNING); | |
340 | } | |
341 | ||
b990b5c0 JA |
342 | /* |
343 | * Not really an io thread, all it does is burn CPU cycles in the specified | |
344 | * manner. | |
345 | */ | |
346 | static void do_cpuio(struct thread_data *td) | |
347 | { | |
348 | struct timeval e; | |
349 | int split = 100 / td->cpuload; | |
350 | int i = 0; | |
351 | ||
352 | while (!td->terminate) { | |
353 | gettimeofday(&e, NULL); | |
354 | ||
355 | if (runtime_exceeded(td, &e)) | |
356 | break; | |
357 | ||
358 | if (!(i % split)) | |
359 | __usec_sleep(10000); | |
360 | else | |
361 | usec_sleep(td, 10000); | |
362 | ||
363 | i++; | |
364 | } | |
365 | } | |
366 | ||
32cd46a0 | 367 | /* |
906c8d75 | 368 | * Main IO worker function. It retrieves io_u's to process and queues |
32cd46a0 JA |
369 | * and reaps them, checking for rate and errors along the way. |
370 | */ | |
ebac4655 JA |
371 | static void do_io(struct thread_data *td) |
372 | { | |
373 | struct io_completion_data icd; | |
374 | struct timeval s, e; | |
375 | unsigned long usec; | |
53cdc686 | 376 | struct fio_file *f; |
84585003 | 377 | int i, ret = 0; |
ebac4655 | 378 | |
5853e5a8 JA |
379 | td_set_runstate(td, TD_RUNNING); |
380 | ||
ebac4655 JA |
381 | while (td->this_io_bytes[td->ddir] < td->io_size) { |
382 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; | |
383 | struct timespec *timeout; | |
84585003 | 384 | int min_evts = 0; |
ebac4655 JA |
385 | struct io_u *io_u; |
386 | ||
387 | if (td->terminate) | |
388 | break; | |
389 | ||
53cdc686 JA |
390 | f = get_next_file(td); |
391 | if (!f) | |
392 | break; | |
393 | ||
394 | io_u = get_io_u(td, f); | |
ebac4655 JA |
395 | if (!io_u) |
396 | break; | |
397 | ||
398 | memcpy(&s, &io_u->start_time, sizeof(s)); | |
399 | ||
45bee283 | 400 | ret = td_io_queue(td, io_u); |
ebac4655 | 401 | if (ret) { |
353a7e0e | 402 | td_verror(td, io_u->error); |
ebac4655 | 403 | put_io_u(td, io_u); |
ebac4655 JA |
404 | break; |
405 | } | |
406 | ||
407 | add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time)); | |
408 | ||
409 | if (td->cur_depth < td->iodepth) { | |
410 | timeout = &ts; | |
411 | min_evts = 0; | |
412 | } else { | |
413 | timeout = NULL; | |
414 | min_evts = 1; | |
415 | } | |
416 | ||
45bee283 | 417 | ret = td_io_getevents(td, min_evts, td->cur_depth, timeout); |
ebac4655 | 418 | if (ret < 0) { |
87dc1ab1 | 419 | td_verror(td, ret); |
ebac4655 JA |
420 | break; |
421 | } else if (!ret) | |
422 | continue; | |
423 | ||
424 | icd.nr = ret; | |
425 | ios_completed(td, &icd); | |
426 | if (icd.error) { | |
427 | td_verror(td, icd.error); | |
428 | break; | |
429 | } | |
430 | ||
431 | /* | |
432 | * the rate is batched for now, it should work for batches | |
433 | * of completions except the very first one which may look | |
434 | * a little bursty | |
435 | */ | |
436 | gettimeofday(&e, NULL); | |
437 | usec = utime_since(&s, &e); | |
438 | ||
439 | rate_throttle(td, usec, icd.bytes_done[td->ddir]); | |
440 | ||
441 | if (check_min_rate(td, &e)) { | |
2f9ade3c JA |
442 | if (rate_quit) |
443 | terminate_threads(td->groupid); | |
ebac4655 JA |
444 | td_verror(td, ENOMEM); |
445 | break; | |
446 | } | |
447 | ||
448 | if (runtime_exceeded(td, &e)) | |
449 | break; | |
450 | ||
451 | if (td->thinktime) | |
452 | usec_sleep(td, td->thinktime); | |
ebac4655 JA |
453 | } |
454 | ||
84585003 JA |
455 | if (!ret) { |
456 | if (td->cur_depth) | |
457 | cleanup_pending_aio(td); | |
ebac4655 | 458 | |
84585003 JA |
459 | if (should_fsync(td) && td->end_fsync) { |
460 | td_set_runstate(td, TD_FSYNCING); | |
461 | for_each_file(td, f, i) | |
858a3d47 | 462 | fio_io_sync(td, f); |
84585003 | 463 | } |
5853e5a8 | 464 | } |
ebac4655 JA |
465 | } |
466 | ||
ebac4655 JA |
467 | static void cleanup_io_u(struct thread_data *td) |
468 | { | |
469 | struct list_head *entry, *n; | |
470 | struct io_u *io_u; | |
471 | ||
472 | list_for_each_safe(entry, n, &td->io_u_freelist) { | |
473 | io_u = list_entry(entry, struct io_u, list); | |
474 | ||
475 | list_del(&io_u->list); | |
476 | free(io_u); | |
477 | } | |
478 | ||
2f9ade3c | 479 | free_io_mem(td); |
ebac4655 JA |
480 | } |
481 | ||
482 | static int init_io_u(struct thread_data *td) | |
483 | { | |
484 | struct io_u *io_u; | |
485 | int i, max_units; | |
486 | char *p; | |
487 | ||
2866c82d | 488 | if (td->io_ops->flags & FIO_CPUIO) |
b990b5c0 JA |
489 | return 0; |
490 | ||
2866c82d | 491 | if (td->io_ops->flags & FIO_SYNCIO) |
ebac4655 JA |
492 | max_units = 1; |
493 | else | |
494 | max_units = td->iodepth; | |
495 | ||
496 | td->orig_buffer_size = td->max_bs * max_units + MASK; | |
497 | ||
2f9ade3c JA |
498 | if (allocate_io_mem(td)) |
499 | return 1; | |
ebac4655 | 500 | |
ebac4655 JA |
501 | p = ALIGN(td->orig_buffer); |
502 | for (i = 0; i < max_units; i++) { | |
503 | io_u = malloc(sizeof(*io_u)); | |
504 | memset(io_u, 0, sizeof(*io_u)); | |
505 | INIT_LIST_HEAD(&io_u->list); | |
506 | ||
507 | io_u->buf = p + td->max_bs * i; | |
b1ff3403 | 508 | io_u->index = i; |
ebac4655 JA |
509 | list_add(&io_u->list, &td->io_u_freelist); |
510 | } | |
511 | ||
512 | return 0; | |
513 | } | |
514 | ||
da86774e JA |
515 | static int switch_ioscheduler(struct thread_data *td) |
516 | { | |
517 | char tmp[256], tmp2[128]; | |
518 | FILE *f; | |
519 | int ret; | |
520 | ||
521 | sprintf(tmp, "%s/queue/scheduler", td->sysfs_root); | |
522 | ||
523 | f = fopen(tmp, "r+"); | |
524 | if (!f) { | |
525 | td_verror(td, errno); | |
526 | return 1; | |
527 | } | |
528 | ||
529 | /* | |
530 | * Set io scheduler. | |
531 | */ | |
532 | ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f); | |
533 | if (ferror(f) || ret != 1) { | |
534 | td_verror(td, errno); | |
535 | fclose(f); | |
536 | return 1; | |
537 | } | |
538 | ||
539 | rewind(f); | |
540 | ||
541 | /* | |
542 | * Read back and check that the selected scheduler is now the default. | |
543 | */ | |
544 | ret = fread(tmp, 1, sizeof(tmp), f); | |
545 | if (ferror(f) || ret < 0) { | |
546 | td_verror(td, errno); | |
547 | fclose(f); | |
548 | return 1; | |
549 | } | |
550 | ||
551 | sprintf(tmp2, "[%s]", td->ioscheduler); | |
552 | if (!strstr(tmp, tmp2)) { | |
3b70d7e5 | 553 | log_err("fio: io scheduler %s not found\n", td->ioscheduler); |
da86774e JA |
554 | td_verror(td, EINVAL); |
555 | fclose(f); | |
556 | return 1; | |
557 | } | |
558 | ||
559 | fclose(f); | |
560 | return 0; | |
561 | } | |
562 | ||
ebac4655 JA |
563 | static void clear_io_state(struct thread_data *td) |
564 | { | |
53cdc686 JA |
565 | struct fio_file *f; |
566 | int i; | |
ebac4655 | 567 | |
ebac4655 JA |
568 | td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0; |
569 | td->this_io_bytes[0] = td->this_io_bytes[1] = 0; | |
20dc95c4 | 570 | td->zone_bytes = 0; |
ebac4655 | 571 | |
53cdc686 JA |
572 | for_each_file(td, f, i) { |
573 | f->last_pos = 0; | |
574 | if (td->io_ops->flags & FIO_SYNCIO) | |
575 | lseek(f->fd, SEEK_SET, 0); | |
576 | ||
577 | if (f->file_map) | |
578 | memset(f->file_map, 0, f->num_maps * sizeof(long)); | |
579 | } | |
ebac4655 JA |
580 | } |
581 | ||
906c8d75 JA |
582 | /* |
583 | * Entry point for the thread based jobs. The process based jobs end up | |
584 | * here as well, after a little setup. | |
585 | */ | |
ebac4655 JA |
586 | static void *thread_main(void *data) |
587 | { | |
588 | struct thread_data *td = data; | |
ebac4655 JA |
589 | |
590 | if (!td->use_thread) | |
591 | setsid(); | |
592 | ||
593 | td->pid = getpid(); | |
594 | ||
aea47d44 JA |
595 | INIT_LIST_HEAD(&td->io_u_freelist); |
596 | INIT_LIST_HEAD(&td->io_u_busylist); | |
597 | INIT_LIST_HEAD(&td->io_hist_list); | |
598 | INIT_LIST_HEAD(&td->io_log_list); | |
599 | ||
ebac4655 JA |
600 | if (init_io_u(td)) |
601 | goto err; | |
602 | ||
603 | if (fio_setaffinity(td) == -1) { | |
604 | td_verror(td, errno); | |
605 | goto err; | |
606 | } | |
607 | ||
45bee283 | 608 | if (td_io_init(td)) |
ebac4655 JA |
609 | goto err; |
610 | ||
aea47d44 JA |
611 | if (init_iolog(td)) |
612 | goto err; | |
613 | ||
ebac4655 JA |
614 | if (td->ioprio) { |
615 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) { | |
616 | td_verror(td, errno); | |
617 | goto err; | |
618 | } | |
619 | } | |
620 | ||
1056eaad | 621 | if (nice(td->nice) == -1) { |
b6f4d880 JA |
622 | td_verror(td, errno); |
623 | goto err; | |
624 | } | |
625 | ||
75154845 JA |
626 | if (init_random_state(td)) |
627 | goto err; | |
628 | ||
56f9498d JA |
629 | if (td->ioscheduler && switch_ioscheduler(td)) |
630 | goto err; | |
da86774e | 631 | |
75154845 | 632 | td_set_runstate(td, TD_INITIALIZED); |
bbfd6b00 JA |
633 | fio_sem_up(&startup_sem); |
634 | fio_sem_down(&td->mutex); | |
ebac4655 | 635 | |
53cdc686 | 636 | if (!td->create_serialize && setup_files(td)) |
ebac4655 JA |
637 | goto err; |
638 | ||
ebac4655 JA |
639 | gettimeofday(&td->epoch, NULL); |
640 | ||
56f9498d | 641 | if (td->exec_prerun) |
4e0ba8af JA |
642 | system(td->exec_prerun); |
643 | ||
ebac4655 JA |
644 | while (td->loops--) { |
645 | getrusage(RUSAGE_SELF, &td->ru_start); | |
646 | gettimeofday(&td->start, NULL); | |
647 | memcpy(&td->stat_sample_time, &td->start, sizeof(td->start)); | |
648 | ||
649 | if (td->ratemin) | |
650 | memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate)); | |
651 | ||
652 | clear_io_state(td); | |
653 | prune_io_piece_log(td); | |
654 | ||
2866c82d | 655 | if (td->io_ops->flags & FIO_CPUIO) |
b990b5c0 JA |
656 | do_cpuio(td); |
657 | else | |
658 | do_io(td); | |
ebac4655 JA |
659 | |
660 | td->runtime[td->ddir] += mtime_since_now(&td->start); | |
aea47d44 | 661 | if (td_rw(td) && td->io_bytes[td->ddir ^ 1]) |
3d60d1ed JA |
662 | td->runtime[td->ddir ^ 1] = td->runtime[td->ddir]; |
663 | ||
ebac4655 JA |
664 | update_rusage_stat(td); |
665 | ||
666 | if (td->error || td->terminate) | |
667 | break; | |
668 | ||
669 | if (td->verify == VERIFY_NONE) | |
670 | continue; | |
671 | ||
672 | clear_io_state(td); | |
673 | gettimeofday(&td->start, NULL); | |
674 | ||
675 | do_verify(td); | |
676 | ||
677 | td->runtime[DDIR_READ] += mtime_since_now(&td->start); | |
678 | ||
679 | if (td->error || td->terminate) | |
680 | break; | |
681 | } | |
682 | ||
ebac4655 JA |
683 | if (td->bw_log) |
684 | finish_log(td, td->bw_log, "bw"); | |
685 | if (td->slat_log) | |
686 | finish_log(td, td->slat_log, "slat"); | |
687 | if (td->clat_log) | |
688 | finish_log(td, td->clat_log, "clat"); | |
843a7413 JA |
689 | if (td->write_iolog) |
690 | write_iolog_close(td); | |
56f9498d | 691 | if (td->exec_postrun) |
4e0ba8af | 692 | system(td->exec_postrun); |
ebac4655 JA |
693 | |
694 | if (exitall_on_terminate) | |
695 | terminate_threads(td->groupid); | |
696 | ||
697 | err: | |
53cdc686 | 698 | close_files(td); |
2866c82d | 699 | close_ioengine(td); |
ebac4655 | 700 | cleanup_io_u(td); |
ebac4655 JA |
701 | td_set_runstate(td, TD_EXITED); |
702 | return NULL; | |
703 | ||
704 | } | |
705 | ||
906c8d75 JA |
706 | /* |
707 | * We cannot pass the td data into a forked process, so attach the td and | |
708 | * pass it to the thread worker. | |
709 | */ | |
ebac4655 JA |
710 | static void *fork_main(int shmid, int offset) |
711 | { | |
712 | struct thread_data *td; | |
713 | void *data; | |
714 | ||
715 | data = shmat(shmid, NULL, 0); | |
716 | if (data == (void *) -1) { | |
717 | perror("shmat"); | |
718 | return NULL; | |
719 | } | |
720 | ||
721 | td = data + offset * sizeof(struct thread_data); | |
722 | thread_main(td); | |
723 | shmdt(data); | |
724 | return NULL; | |
725 | } | |
726 | ||
906c8d75 JA |
727 | /* |
728 | * Run over the job map and reap the threads that have exited, if any. | |
729 | */ | |
ebac4655 JA |
730 | static void reap_threads(int *nr_running, int *t_rate, int *m_rate) |
731 | { | |
34572e28 | 732 | struct thread_data *td; |
b990b5c0 | 733 | int i, cputhreads; |
ebac4655 JA |
734 | |
735 | /* | |
736 | * reap exited threads (TD_EXITED -> TD_REAPED) | |
737 | */ | |
34572e28 JA |
738 | cputhreads = 0; |
739 | for_each_td(td, i) { | |
84585003 JA |
740 | /* |
741 | * ->io_ops is NULL for a thread that has closed its | |
742 | * io engine | |
743 | */ | |
744 | if (td->io_ops && td->io_ops->flags & FIO_CPUIO) | |
b990b5c0 JA |
745 | cputhreads++; |
746 | ||
ebac4655 JA |
747 | if (td->runstate != TD_EXITED) |
748 | continue; | |
749 | ||
750 | td_set_runstate(td, TD_REAPED); | |
751 | ||
752 | if (td->use_thread) { | |
753 | long ret; | |
754 | ||
755 | if (pthread_join(td->thread, (void *) &ret)) | |
756 | perror("thread_join"); | |
757 | } else | |
758 | waitpid(td->pid, NULL, 0); | |
759 | ||
760 | (*nr_running)--; | |
761 | (*m_rate) -= td->ratemin; | |
762 | (*t_rate) -= td->rate; | |
763 | } | |
b990b5c0 JA |
764 | |
765 | if (*nr_running == cputhreads) | |
766 | terminate_threads(TERMINATE_ALL); | |
ebac4655 JA |
767 | } |
768 | ||
906c8d75 JA |
769 | /* |
770 | * Main function for kicking off and reaping jobs, as needed. | |
771 | */ | |
ebac4655 JA |
772 | static void run_threads(void) |
773 | { | |
ebac4655 JA |
774 | struct thread_data *td; |
775 | unsigned long spent; | |
776 | int i, todo, nr_running, m_rate, t_rate, nr_started; | |
fcb6ade2 | 777 | |
2f9ade3c JA |
778 | if (fio_pin_memory()) |
779 | return; | |
ebac4655 | 780 | |
c6ae0a5b JA |
781 | if (!terse_output) { |
782 | printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : ""); | |
783 | fflush(stdout); | |
784 | } | |
c04f7ec3 | 785 | |
4efa970e JA |
786 | signal(SIGINT, sig_handler); |
787 | signal(SIGALRM, sig_handler); | |
788 | ||
ebac4655 JA |
789 | todo = thread_number; |
790 | nr_running = 0; | |
791 | nr_started = 0; | |
792 | m_rate = t_rate = 0; | |
793 | ||
34572e28 | 794 | for_each_td(td, i) { |
263e529f | 795 | print_status_init(td->thread_number - 1); |
ebac4655 JA |
796 | |
797 | init_disk_util(td); | |
798 | ||
799 | if (!td->create_serialize) | |
800 | continue; | |
801 | ||
802 | /* | |
803 | * do file setup here so it happens sequentially, | |
804 | * we don't want X number of threads getting their | |
805 | * client data interspersed on disk | |
806 | */ | |
53cdc686 | 807 | if (setup_files(td)) { |
ebac4655 JA |
808 | td_set_runstate(td, TD_REAPED); |
809 | todo--; | |
810 | } | |
811 | } | |
812 | ||
263e529f | 813 | time_init(); |
ebac4655 JA |
814 | |
815 | while (todo) { | |
75154845 JA |
816 | struct thread_data *map[MAX_JOBS]; |
817 | struct timeval this_start; | |
818 | int this_jobs = 0, left; | |
819 | ||
ebac4655 JA |
820 | /* |
821 | * create threads (TD_NOT_CREATED -> TD_CREATED) | |
822 | */ | |
34572e28 | 823 | for_each_td(td, i) { |
ebac4655 JA |
824 | if (td->runstate != TD_NOT_CREATED) |
825 | continue; | |
826 | ||
827 | /* | |
828 | * never got a chance to start, killed by other | |
829 | * thread for some reason | |
830 | */ | |
831 | if (td->terminate) { | |
832 | todo--; | |
833 | continue; | |
834 | } | |
835 | ||
836 | if (td->start_delay) { | |
263e529f | 837 | spent = mtime_since_genesis(); |
ebac4655 JA |
838 | |
839 | if (td->start_delay * 1000 > spent) | |
840 | continue; | |
841 | } | |
842 | ||
843 | if (td->stonewall && (nr_started || nr_running)) | |
844 | break; | |
845 | ||
75154845 JA |
846 | /* |
847 | * Set state to created. Thread will transition | |
848 | * to TD_INITIALIZED when it's done setting up. | |
849 | */ | |
ebac4655 | 850 | td_set_runstate(td, TD_CREATED); |
75154845 | 851 | map[this_jobs++] = td; |
bbfd6b00 | 852 | fio_sem_init(&startup_sem, 1); |
ebac4655 JA |
853 | nr_started++; |
854 | ||
855 | if (td->use_thread) { | |
856 | if (pthread_create(&td->thread, NULL, thread_main, td)) { | |
857 | perror("thread_create"); | |
858 | nr_started--; | |
859 | } | |
860 | } else { | |
861 | if (fork()) | |
bbfd6b00 | 862 | fio_sem_down(&startup_sem); |
ebac4655 JA |
863 | else { |
864 | fork_main(shm_id, i); | |
865 | exit(0); | |
866 | } | |
867 | } | |
868 | } | |
869 | ||
870 | /* | |
75154845 JA |
871 | * Wait for the started threads to transition to |
872 | * TD_INITIALIZED. | |
ebac4655 | 873 | */ |
75154845 JA |
874 | gettimeofday(&this_start, NULL); |
875 | left = this_jobs; | |
876 | while (left) { | |
877 | if (mtime_since_now(&this_start) > JOB_START_TIMEOUT) | |
878 | break; | |
879 | ||
880 | usleep(100000); | |
881 | ||
882 | for (i = 0; i < this_jobs; i++) { | |
883 | td = map[i]; | |
884 | if (!td) | |
885 | continue; | |
b6f4d880 | 886 | if (td->runstate == TD_INITIALIZED) { |
75154845 JA |
887 | map[i] = NULL; |
888 | left--; | |
b6f4d880 JA |
889 | } else if (td->runstate >= TD_EXITED) { |
890 | map[i] = NULL; | |
891 | left--; | |
892 | todo--; | |
893 | nr_running++; /* work-around... */ | |
75154845 JA |
894 | } |
895 | } | |
896 | } | |
897 | ||
898 | if (left) { | |
3b70d7e5 | 899 | log_err("fio: %d jobs failed to start\n", left); |
75154845 JA |
900 | for (i = 0; i < this_jobs; i++) { |
901 | td = map[i]; | |
902 | if (!td) | |
903 | continue; | |
904 | kill(td->pid, SIGTERM); | |
905 | } | |
906 | break; | |
907 | } | |
908 | ||
909 | /* | |
b6f4d880 | 910 | * start created threads (TD_INITIALIZED -> TD_RUNNING). |
75154845 | 911 | */ |
34572e28 | 912 | for_each_td(td, i) { |
75154845 | 913 | if (td->runstate != TD_INITIALIZED) |
ebac4655 JA |
914 | continue; |
915 | ||
916 | td_set_runstate(td, TD_RUNNING); | |
917 | nr_running++; | |
918 | nr_started--; | |
919 | m_rate += td->ratemin; | |
920 | t_rate += td->rate; | |
75154845 | 921 | todo--; |
bbfd6b00 | 922 | fio_sem_up(&td->mutex); |
ebac4655 JA |
923 | } |
924 | ||
925 | reap_threads(&nr_running, &t_rate, &m_rate); | |
926 | ||
927 | if (todo) | |
928 | usleep(100000); | |
929 | } | |
930 | ||
931 | while (nr_running) { | |
932 | reap_threads(&nr_running, &t_rate, &m_rate); | |
933 | usleep(10000); | |
934 | } | |
935 | ||
936 | update_io_ticks(); | |
2f9ade3c | 937 | fio_unpin_memory(); |
ebac4655 JA |
938 | } |
939 | ||
ebac4655 JA |
940 | int main(int argc, char *argv[]) |
941 | { | |
942 | if (parse_options(argc, argv)) | |
943 | return 1; | |
944 | ||
945 | if (!thread_number) { | |
3b70d7e5 | 946 | log_err("Nothing to do\n"); |
ebac4655 JA |
947 | return 1; |
948 | } | |
949 | ||
950 | disk_util_timer_arm(); | |
951 | ||
952 | run_threads(); | |
953 | show_run_stats(); | |
954 | ||
955 | return 0; | |
956 | } |