blob: fc653b3d9016e9b7553ac5cadbb37a870ba00a9d [file] [log] [blame]
Jens Axboeebac4652005-12-08 15:25:21 +01001/*
2 * fio - the flexible io tester
3 *
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
Jens Axboeaae22ca2006-09-05 10:46:22 +02005 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
Jens Axboeebac4652005-12-08 15:25:21 +01006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
Jens Axboeebac4652005-12-08 15:25:21 +010022#include <unistd.h>
23#include <fcntl.h>
24#include <string.h>
Jens Axboeebac4652005-12-08 15:25:21 +010025#include <signal.h>
26#include <time.h>
Jens Axboeebac4652005-12-08 15:25:21 +010027#include <assert.h>
Jens Axboeebac4652005-12-08 15:25:21 +010028#include <sys/stat.h>
29#include <sys/wait.h>
30#include <sys/ipc.h>
31#include <sys/shm.h>
32#include <sys/ioctl.h>
33#include <sys/mman.h>
34
35#include "fio.h"
36#include "os.h"
37
38#define MASK (4095)
39
40#define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
41
42int groupid = 0;
43int thread_number = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010044int shm_id = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +020045int temp_stall_ts;
Jens Axboec1d57252006-10-09 19:56:04 +020046char *fio_inst_prefix = _INST_PREFIX;
Jens Axboeebac4652005-12-08 15:25:21 +010047
Jens Axboebbfd6b02006-06-07 19:42:54 +020048static volatile int startup_sem;
Jens Axboeebac4652005-12-08 15:25:21 +010049
50#define TERMINATE_ALL (-1)
Jens Axboe75154842006-06-01 13:56:09 +020051#define JOB_START_TIMEOUT (5 * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +010052
53static void terminate_threads(int group_id)
54{
Jens Axboe34572e22006-10-20 09:33:18 +020055 struct thread_data *td;
Jens Axboeebac4652005-12-08 15:25:21 +010056 int i;
57
Jens Axboe34572e22006-10-20 09:33:18 +020058 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +010059 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
60 td->terminate = 1;
61 td->start_delay = 0;
62 }
63 }
64}
65
66static void sig_handler(int sig)
67{
68 switch (sig) {
69 case SIGALRM:
70 update_io_ticks();
71 disk_util_timer_arm();
72 print_thread_status();
73 break;
74 default:
75 printf("\nfio: terminating on signal\n");
76 fflush(stdout);
77 terminate_threads(TERMINATE_ALL);
78 break;
79 }
80}
81
Jens Axboe906c8d72006-06-13 09:37:56 +020082/*
Jens Axboe906c8d72006-06-13 09:37:56 +020083 * Check if we are above the minimum rate given.
84 */
Jens Axboeebac4652005-12-08 15:25:21 +010085static int check_min_rate(struct thread_data *td, struct timeval *now)
86{
87 unsigned long spent;
88 unsigned long rate;
89 int ddir = td->ddir;
90
91 /*
92 * allow a 2 second settle period in the beginning
93 */
94 if (mtime_since(&td->start, now) < 2000)
95 return 0;
96
97 /*
98 * if rate blocks is set, sample is running
99 */
100 if (td->rate_bytes) {
101 spent = mtime_since(&td->lastrate, now);
102 if (spent < td->ratecycle)
103 return 0;
104
105 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
106 if (rate < td->ratemin) {
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200107 fprintf(f_out, "%s: min rate %d not met, got %ldKiB/sec\n", td->name, td->ratemin, rate);
Jens Axboeebac4652005-12-08 15:25:21 +0100108 return 1;
109 }
110 }
111
112 td->rate_bytes = td->this_io_bytes[ddir];
113 memcpy(&td->lastrate, now, sizeof(*now));
114 return 0;
115}
116
117static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
118{
119 if (!td->timeout)
120 return 0;
121 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
122 return 1;
123
124 return 0;
125}
126
Jens Axboeebac4652005-12-08 15:25:21 +0100127static inline void td_set_runstate(struct thread_data *td, int runstate)
128{
Jens Axboeebac4652005-12-08 15:25:21 +0100129 td->runstate = runstate;
130}
131
Jens Axboe53cdc682006-10-18 11:50:58 +0200132static struct fio_file *get_next_file(struct thread_data *td)
133{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200134 unsigned int old_next_file = td->next_file;
Jens Axboeb2a15192006-10-18 14:10:42 +0200135 struct fio_file *f;
Jens Axboe53cdc682006-10-18 11:50:58 +0200136
Jens Axboeb2a15192006-10-18 14:10:42 +0200137 do {
138 f = &td->files[td->next_file];
139
140 td->next_file++;
141 if (td->next_file >= td->nr_files)
142 td->next_file = 0;
143
144 if (f->fd != -1)
145 break;
146
147 f = NULL;
148 } while (td->next_file != old_next_file);
Jens Axboe53cdc682006-10-18 11:50:58 +0200149
150 return f;
151}
152
Jens Axboe906c8d72006-06-13 09:37:56 +0200153/*
154 * When job exits, we can cancel the in-flight IO if we are using async
155 * io. Attempt to do so.
156 */
Jens Axboeebac4652005-12-08 15:25:21 +0100157static void cleanup_pending_aio(struct thread_data *td)
158{
159 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
160 struct list_head *entry, *n;
161 struct io_completion_data icd;
162 struct io_u *io_u;
163 int r;
164
165 /*
166 * get immediately available events, if any
167 */
Jens Axboe45bee282006-10-18 15:26:44 +0200168 r = td_io_getevents(td, 0, td->cur_depth, &ts);
Jens Axboeebac4652005-12-08 15:25:21 +0100169 if (r > 0) {
170 icd.nr = r;
171 ios_completed(td, &icd);
172 }
173
174 /*
175 * now cancel remaining active events
176 */
Jens Axboe2866c822006-10-09 15:57:48 +0200177 if (td->io_ops->cancel) {
Jens Axboeebac4652005-12-08 15:25:21 +0100178 list_for_each_safe(entry, n, &td->io_u_busylist) {
179 io_u = list_entry(entry, struct io_u, list);
180
Jens Axboe2866c822006-10-09 15:57:48 +0200181 r = td->io_ops->cancel(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100182 if (!r)
183 put_io_u(td, io_u);
184 }
185 }
186
187 if (td->cur_depth) {
Jens Axboe45bee282006-10-18 15:26:44 +0200188 r = td_io_getevents(td, td->cur_depth, td->cur_depth, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100189 if (r > 0) {
190 icd.nr = r;
191 ios_completed(td, &icd);
192 }
193 }
194}
195
Jens Axboe906c8d72006-06-13 09:37:56 +0200196/*
197 * The main verify engine. Runs over the writes we previusly submitted,
198 * reads the blocks back in, and checks the crc/md5 of the data.
199 */
Jens Axboea9619d42006-10-18 15:47:42 +0200200void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100201{
202 struct timeval t;
203 struct io_u *io_u, *v_io_u = NULL;
204 struct io_completion_data icd;
Jens Axboe53cdc682006-10-18 11:50:58 +0200205 struct fio_file *f;
Jens Axboee5b401d2006-10-18 16:03:40 +0200206 int ret, i;
207
208 /*
209 * sync io first and invalidate cache, to make sure we really
210 * read from disk.
211 */
212 for_each_file(td, f, i) {
213 td_io_sync(td, f);
214 file_invalidate_cache(td, f);
215 }
Jens Axboeebac4652005-12-08 15:25:21 +0100216
217 td_set_runstate(td, TD_VERIFYING);
218
219 do {
220 if (td->terminate)
221 break;
222
223 gettimeofday(&t, NULL);
224 if (runtime_exceeded(td, &t))
225 break;
226
227 io_u = __get_io_u(td);
228 if (!io_u)
229 break;
230
Jens Axboeaea47d42006-05-26 19:27:29 +0200231 if (get_next_verify(td, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100232 put_io_u(td, io_u);
233 break;
234 }
235
Jens Axboe53cdc682006-10-18 11:50:58 +0200236 f = get_next_file(td);
237 if (!f)
238 break;
239
240 io_u->file = f;
241
Jens Axboeaea47d42006-05-26 19:27:29 +0200242 if (td_io_prep(td, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100243 put_io_u(td, io_u);
244 break;
245 }
246
Jens Axboe45bee282006-10-18 15:26:44 +0200247 ret = td_io_queue(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100248 if (ret) {
249 put_io_u(td, io_u);
250 td_verror(td, ret);
251 break;
252 }
253
254 /*
255 * we have one pending to verify, do that while
256 * we are doing io on the next one
257 */
258 if (do_io_u_verify(td, &v_io_u))
259 break;
260
Jens Axboe45bee282006-10-18 15:26:44 +0200261 ret = td_io_getevents(td, 1, 1, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100262 if (ret != 1) {
263 if (ret < 0)
264 td_verror(td, ret);
265 break;
266 }
267
Jens Axboe2866c822006-10-09 15:57:48 +0200268 v_io_u = td->io_ops->event(td, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100269 icd.nr = 1;
270 icd.error = 0;
271 io_completed(td, v_io_u, &icd);
272
273 if (icd.error) {
274 td_verror(td, icd.error);
275 put_io_u(td, v_io_u);
276 v_io_u = NULL;
277 break;
278 }
279
Jens Axboeebac4652005-12-08 15:25:21 +0100280 /*
281 * if we can't submit more io, we need to verify now
282 */
283 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
284 break;
285
286 } while (1);
287
288 do_io_u_verify(td, &v_io_u);
289
290 if (td->cur_depth)
291 cleanup_pending_aio(td);
292
293 td_set_runstate(td, TD_RUNNING);
294}
295
Jens Axboe32cd46a2006-06-07 13:40:40 +0200296/*
Jens Axboeb990b5c2006-09-14 09:48:22 +0200297 * Not really an io thread, all it does is burn CPU cycles in the specified
298 * manner.
299 */
300static void do_cpuio(struct thread_data *td)
301{
302 struct timeval e;
303 int split = 100 / td->cpuload;
304 int i = 0;
305
306 while (!td->terminate) {
307 gettimeofday(&e, NULL);
308
309 if (runtime_exceeded(td, &e))
310 break;
311
312 if (!(i % split))
313 __usec_sleep(10000);
314 else
315 usec_sleep(td, 10000);
316
317 i++;
318 }
319}
320
321/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200322 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200323 * and reaps them, checking for rate and errors along the way.
324 */
Jens Axboeebac4652005-12-08 15:25:21 +0100325static void do_io(struct thread_data *td)
326{
327 struct io_completion_data icd;
328 struct timeval s, e;
329 unsigned long usec;
Jens Axboe53cdc682006-10-18 11:50:58 +0200330 struct fio_file *f;
Jens Axboe84585002006-10-19 20:26:22 +0200331 int i, ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100332
Jens Axboe5853e5a2006-06-08 14:41:05 +0200333 td_set_runstate(td, TD_RUNNING);
334
Jens Axboeebac4652005-12-08 15:25:21 +0100335 while (td->this_io_bytes[td->ddir] < td->io_size) {
336 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
337 struct timespec *timeout;
Jens Axboe84585002006-10-19 20:26:22 +0200338 int min_evts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100339 struct io_u *io_u;
340
341 if (td->terminate)
342 break;
343
Jens Axboe53cdc682006-10-18 11:50:58 +0200344 f = get_next_file(td);
345 if (!f)
346 break;
347
348 io_u = get_io_u(td, f);
Jens Axboeebac4652005-12-08 15:25:21 +0100349 if (!io_u)
350 break;
351
352 memcpy(&s, &io_u->start_time, sizeof(s));
353
Jens Axboe45bee282006-10-18 15:26:44 +0200354 ret = td_io_queue(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100355 if (ret) {
356 put_io_u(td, io_u);
357 td_verror(td, ret);
358 break;
359 }
360
361 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
362
363 if (td->cur_depth < td->iodepth) {
364 timeout = &ts;
365 min_evts = 0;
366 } else {
367 timeout = NULL;
368 min_evts = 1;
369 }
370
Jens Axboe45bee282006-10-18 15:26:44 +0200371 ret = td_io_getevents(td, min_evts, td->cur_depth, timeout);
Jens Axboeebac4652005-12-08 15:25:21 +0100372 if (ret < 0) {
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200373 td_verror(td, ret);
Jens Axboeebac4652005-12-08 15:25:21 +0100374 break;
375 } else if (!ret)
376 continue;
377
378 icd.nr = ret;
379 ios_completed(td, &icd);
380 if (icd.error) {
381 td_verror(td, icd.error);
382 break;
383 }
384
385 /*
386 * the rate is batched for now, it should work for batches
387 * of completions except the very first one which may look
388 * a little bursty
389 */
390 gettimeofday(&e, NULL);
391 usec = utime_since(&s, &e);
392
393 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
394
395 if (check_min_rate(td, &e)) {
Jens Axboe2f9ade32006-10-20 11:25:52 +0200396 if (rate_quit)
397 terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +0100398 td_verror(td, ENOMEM);
399 break;
400 }
401
402 if (runtime_exceeded(td, &e))
403 break;
404
405 if (td->thinktime)
406 usec_sleep(td, td->thinktime);
Jens Axboeebac4652005-12-08 15:25:21 +0100407 }
408
Jens Axboe84585002006-10-19 20:26:22 +0200409 if (!ret) {
410 if (td->cur_depth)
411 cleanup_pending_aio(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100412
Jens Axboe84585002006-10-19 20:26:22 +0200413 if (should_fsync(td) && td->end_fsync) {
414 td_set_runstate(td, TD_FSYNCING);
415 for_each_file(td, f, i)
416 td_io_sync(td, f);
417 }
Jens Axboe5853e5a2006-06-08 14:41:05 +0200418 }
Jens Axboeebac4652005-12-08 15:25:21 +0100419}
420
Jens Axboeebac4652005-12-08 15:25:21 +0100421static void cleanup_io_u(struct thread_data *td)
422{
423 struct list_head *entry, *n;
424 struct io_u *io_u;
425
426 list_for_each_safe(entry, n, &td->io_u_freelist) {
427 io_u = list_entry(entry, struct io_u, list);
428
429 list_del(&io_u->list);
430 free(io_u);
431 }
432
Jens Axboe2f9ade32006-10-20 11:25:52 +0200433 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100434}
435
436static int init_io_u(struct thread_data *td)
437{
438 struct io_u *io_u;
439 int i, max_units;
440 char *p;
441
Jens Axboe2866c822006-10-09 15:57:48 +0200442 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200443 return 0;
444
Jens Axboe2866c822006-10-09 15:57:48 +0200445 if (td->io_ops->flags & FIO_SYNCIO)
Jens Axboeebac4652005-12-08 15:25:21 +0100446 max_units = 1;
447 else
448 max_units = td->iodepth;
449
450 td->orig_buffer_size = td->max_bs * max_units + MASK;
451
Jens Axboe2f9ade32006-10-20 11:25:52 +0200452 if (allocate_io_mem(td))
453 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100454
Jens Axboeebac4652005-12-08 15:25:21 +0100455 p = ALIGN(td->orig_buffer);
456 for (i = 0; i < max_units; i++) {
457 io_u = malloc(sizeof(*io_u));
458 memset(io_u, 0, sizeof(*io_u));
459 INIT_LIST_HEAD(&io_u->list);
460
461 io_u->buf = p + td->max_bs * i;
Jens Axboeb1ff3402006-02-17 11:35:58 +0100462 io_u->index = i;
Jens Axboeebac4652005-12-08 15:25:21 +0100463 list_add(&io_u->list, &td->io_u_freelist);
464 }
465
466 return 0;
467}
468
Jens Axboeda867742006-06-06 10:39:10 -0700469static int switch_ioscheduler(struct thread_data *td)
470{
471 char tmp[256], tmp2[128];
472 FILE *f;
473 int ret;
474
475 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
476
477 f = fopen(tmp, "r+");
478 if (!f) {
479 td_verror(td, errno);
480 return 1;
481 }
482
483 /*
484 * Set io scheduler.
485 */
486 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
487 if (ferror(f) || ret != 1) {
488 td_verror(td, errno);
489 fclose(f);
490 return 1;
491 }
492
493 rewind(f);
494
495 /*
496 * Read back and check that the selected scheduler is now the default.
497 */
498 ret = fread(tmp, 1, sizeof(tmp), f);
499 if (ferror(f) || ret < 0) {
500 td_verror(td, errno);
501 fclose(f);
502 return 1;
503 }
504
505 sprintf(tmp2, "[%s]", td->ioscheduler);
506 if (!strstr(tmp, tmp2)) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200507 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700508 td_verror(td, EINVAL);
509 fclose(f);
510 return 1;
511 }
512
513 fclose(f);
514 return 0;
515}
516
Jens Axboeebac4652005-12-08 15:25:21 +0100517static void clear_io_state(struct thread_data *td)
518{
Jens Axboe53cdc682006-10-18 11:50:58 +0200519 struct fio_file *f;
520 int i;
Jens Axboeebac4652005-12-08 15:25:21 +0100521
Jens Axboeebac4652005-12-08 15:25:21 +0100522 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
523 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100524 td->zone_bytes = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100525
Jens Axboe53cdc682006-10-18 11:50:58 +0200526 for_each_file(td, f, i) {
527 f->last_pos = 0;
528 if (td->io_ops->flags & FIO_SYNCIO)
529 lseek(f->fd, SEEK_SET, 0);
530
531 if (f->file_map)
532 memset(f->file_map, 0, f->num_maps * sizeof(long));
533 }
Jens Axboeebac4652005-12-08 15:25:21 +0100534}
535
Jens Axboe906c8d72006-06-13 09:37:56 +0200536/*
537 * Entry point for the thread based jobs. The process based jobs end up
538 * here as well, after a little setup.
539 */
Jens Axboeebac4652005-12-08 15:25:21 +0100540static void *thread_main(void *data)
541{
542 struct thread_data *td = data;
Jens Axboeebac4652005-12-08 15:25:21 +0100543
544 if (!td->use_thread)
545 setsid();
546
547 td->pid = getpid();
548
Jens Axboeaea47d42006-05-26 19:27:29 +0200549 INIT_LIST_HEAD(&td->io_u_freelist);
550 INIT_LIST_HEAD(&td->io_u_busylist);
551 INIT_LIST_HEAD(&td->io_hist_list);
552 INIT_LIST_HEAD(&td->io_log_list);
553
Jens Axboeebac4652005-12-08 15:25:21 +0100554 if (init_io_u(td))
555 goto err;
556
557 if (fio_setaffinity(td) == -1) {
558 td_verror(td, errno);
559 goto err;
560 }
561
Jens Axboe45bee282006-10-18 15:26:44 +0200562 if (td_io_init(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100563 goto err;
564
Jens Axboeaea47d42006-05-26 19:27:29 +0200565 if (init_iolog(td))
566 goto err;
567
Jens Axboeebac4652005-12-08 15:25:21 +0100568 if (td->ioprio) {
569 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
570 td_verror(td, errno);
571 goto err;
572 }
573 }
574
Jens Axboe1056eaa2006-07-13 10:33:45 -0700575 if (nice(td->nice) == -1) {
Jens Axboeb6f4d882006-06-02 10:32:51 +0200576 td_verror(td, errno);
577 goto err;
578 }
579
Jens Axboe75154842006-06-01 13:56:09 +0200580 if (init_random_state(td))
581 goto err;
582
Jens Axboe56f94982006-10-24 09:33:42 +0200583 if (td->ioscheduler && switch_ioscheduler(td))
584 goto err;
Jens Axboeda867742006-06-06 10:39:10 -0700585
Jens Axboe75154842006-06-01 13:56:09 +0200586 td_set_runstate(td, TD_INITIALIZED);
Jens Axboebbfd6b02006-06-07 19:42:54 +0200587 fio_sem_up(&startup_sem);
588 fio_sem_down(&td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100589
Jens Axboe53cdc682006-10-18 11:50:58 +0200590 if (!td->create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100591 goto err;
592
Jens Axboeebac4652005-12-08 15:25:21 +0100593 gettimeofday(&td->epoch, NULL);
594
Jens Axboe56f94982006-10-24 09:33:42 +0200595 if (td->exec_prerun)
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200596 system(td->exec_prerun);
597
Jens Axboeebac4652005-12-08 15:25:21 +0100598 while (td->loops--) {
599 getrusage(RUSAGE_SELF, &td->ru_start);
600 gettimeofday(&td->start, NULL);
601 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
602
603 if (td->ratemin)
604 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
605
606 clear_io_state(td);
607 prune_io_piece_log(td);
608
Jens Axboe2866c822006-10-09 15:57:48 +0200609 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200610 do_cpuio(td);
611 else
612 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100613
614 td->runtime[td->ddir] += mtime_since_now(&td->start);
Jens Axboeaea47d42006-05-26 19:27:29 +0200615 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200616 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
617
Jens Axboeebac4652005-12-08 15:25:21 +0100618 update_rusage_stat(td);
619
620 if (td->error || td->terminate)
621 break;
622
623 if (td->verify == VERIFY_NONE)
624 continue;
625
626 clear_io_state(td);
627 gettimeofday(&td->start, NULL);
628
629 do_verify(td);
630
631 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
632
633 if (td->error || td->terminate)
634 break;
635 }
636
Jens Axboeebac4652005-12-08 15:25:21 +0100637 if (td->bw_log)
638 finish_log(td, td->bw_log, "bw");
639 if (td->slat_log)
640 finish_log(td, td->slat_log, "slat");
641 if (td->clat_log)
642 finish_log(td, td->clat_log, "clat");
Jens Axboe843a7412006-06-01 21:14:21 -0700643 if (td->write_iolog)
644 write_iolog_close(td);
Jens Axboe56f94982006-10-24 09:33:42 +0200645 if (td->exec_postrun)
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200646 system(td->exec_postrun);
Jens Axboeebac4652005-12-08 15:25:21 +0100647
648 if (exitall_on_terminate)
649 terminate_threads(td->groupid);
650
651err:
Jens Axboe53cdc682006-10-18 11:50:58 +0200652 close_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +0200653 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100654 cleanup_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100655 td_set_runstate(td, TD_EXITED);
656 return NULL;
657
658}
659
Jens Axboe906c8d72006-06-13 09:37:56 +0200660/*
661 * We cannot pass the td data into a forked process, so attach the td and
662 * pass it to the thread worker.
663 */
Jens Axboeebac4652005-12-08 15:25:21 +0100664static void *fork_main(int shmid, int offset)
665{
666 struct thread_data *td;
667 void *data;
668
669 data = shmat(shmid, NULL, 0);
670 if (data == (void *) -1) {
671 perror("shmat");
672 return NULL;
673 }
674
675 td = data + offset * sizeof(struct thread_data);
676 thread_main(td);
677 shmdt(data);
678 return NULL;
679}
680
Jens Axboe906c8d72006-06-13 09:37:56 +0200681/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200682 * Run over the job map and reap the threads that have exited, if any.
683 */
Jens Axboeebac4652005-12-08 15:25:21 +0100684static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
685{
Jens Axboe34572e22006-10-20 09:33:18 +0200686 struct thread_data *td;
Jens Axboeb990b5c2006-09-14 09:48:22 +0200687 int i, cputhreads;
Jens Axboeebac4652005-12-08 15:25:21 +0100688
689 /*
690 * reap exited threads (TD_EXITED -> TD_REAPED)
691 */
Jens Axboe34572e22006-10-20 09:33:18 +0200692 cputhreads = 0;
693 for_each_td(td, i) {
Jens Axboe84585002006-10-19 20:26:22 +0200694 /*
695 * ->io_ops is NULL for a thread that has closed its
696 * io engine
697 */
698 if (td->io_ops && td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200699 cputhreads++;
700
Jens Axboeebac4652005-12-08 15:25:21 +0100701 if (td->runstate != TD_EXITED)
702 continue;
703
704 td_set_runstate(td, TD_REAPED);
705
706 if (td->use_thread) {
707 long ret;
708
709 if (pthread_join(td->thread, (void *) &ret))
710 perror("thread_join");
711 } else
712 waitpid(td->pid, NULL, 0);
713
714 (*nr_running)--;
715 (*m_rate) -= td->ratemin;
716 (*t_rate) -= td->rate;
717 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200718
719 if (*nr_running == cputhreads)
720 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +0100721}
722
Jens Axboe906c8d72006-06-13 09:37:56 +0200723/*
724 * Main function for kicking off and reaping jobs, as needed.
725 */
Jens Axboeebac4652005-12-08 15:25:21 +0100726static void run_threads(void)
727{
Jens Axboeebac4652005-12-08 15:25:21 +0100728 struct thread_data *td;
729 unsigned long spent;
730 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +0200731
Jens Axboe2f9ade32006-10-20 11:25:52 +0200732 if (fio_pin_memory())
733 return;
Jens Axboeebac4652005-12-08 15:25:21 +0100734
Jens Axboec6ae0a52006-06-12 11:04:44 +0200735 if (!terse_output) {
736 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
737 fflush(stdout);
738 }
Jens Axboec04f7ec2006-05-31 10:13:16 +0200739
Jens Axboe4efa9702006-05-31 11:56:49 +0200740 signal(SIGINT, sig_handler);
741 signal(SIGALRM, sig_handler);
742
Jens Axboeebac4652005-12-08 15:25:21 +0100743 todo = thread_number;
744 nr_running = 0;
745 nr_started = 0;
746 m_rate = t_rate = 0;
747
Jens Axboe34572e22006-10-20 09:33:18 +0200748 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +0200749 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +0100750
751 init_disk_util(td);
752
753 if (!td->create_serialize)
754 continue;
755
756 /*
757 * do file setup here so it happens sequentially,
758 * we don't want X number of threads getting their
759 * client data interspersed on disk
760 */
Jens Axboe53cdc682006-10-18 11:50:58 +0200761 if (setup_files(td)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100762 td_set_runstate(td, TD_REAPED);
763 todo--;
764 }
765 }
766
Jens Axboe263e5292006-10-18 15:37:01 +0200767 time_init();
Jens Axboeebac4652005-12-08 15:25:21 +0100768
769 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +0200770 struct thread_data *map[MAX_JOBS];
771 struct timeval this_start;
772 int this_jobs = 0, left;
773
Jens Axboeebac4652005-12-08 15:25:21 +0100774 /*
775 * create threads (TD_NOT_CREATED -> TD_CREATED)
776 */
Jens Axboe34572e22006-10-20 09:33:18 +0200777 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +0100778 if (td->runstate != TD_NOT_CREATED)
779 continue;
780
781 /*
782 * never got a chance to start, killed by other
783 * thread for some reason
784 */
785 if (td->terminate) {
786 todo--;
787 continue;
788 }
789
790 if (td->start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +0200791 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +0100792
793 if (td->start_delay * 1000 > spent)
794 continue;
795 }
796
797 if (td->stonewall && (nr_started || nr_running))
798 break;
799
Jens Axboe75154842006-06-01 13:56:09 +0200800 /*
801 * Set state to created. Thread will transition
802 * to TD_INITIALIZED when it's done setting up.
803 */
Jens Axboeebac4652005-12-08 15:25:21 +0100804 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +0200805 map[this_jobs++] = td;
Jens Axboebbfd6b02006-06-07 19:42:54 +0200806 fio_sem_init(&startup_sem, 1);
Jens Axboeebac4652005-12-08 15:25:21 +0100807 nr_started++;
808
809 if (td->use_thread) {
810 if (pthread_create(&td->thread, NULL, thread_main, td)) {
811 perror("thread_create");
812 nr_started--;
813 }
814 } else {
815 if (fork())
Jens Axboebbfd6b02006-06-07 19:42:54 +0200816 fio_sem_down(&startup_sem);
Jens Axboeebac4652005-12-08 15:25:21 +0100817 else {
818 fork_main(shm_id, i);
819 exit(0);
820 }
821 }
822 }
823
824 /*
Jens Axboe75154842006-06-01 13:56:09 +0200825 * Wait for the started threads to transition to
826 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +0100827 */
Jens Axboe75154842006-06-01 13:56:09 +0200828 gettimeofday(&this_start, NULL);
829 left = this_jobs;
830 while (left) {
831 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
832 break;
833
834 usleep(100000);
835
836 for (i = 0; i < this_jobs; i++) {
837 td = map[i];
838 if (!td)
839 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +0200840 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +0200841 map[i] = NULL;
842 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +0200843 } else if (td->runstate >= TD_EXITED) {
844 map[i] = NULL;
845 left--;
846 todo--;
847 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +0200848 }
849 }
850 }
851
852 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200853 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +0200854 for (i = 0; i < this_jobs; i++) {
855 td = map[i];
856 if (!td)
857 continue;
858 kill(td->pid, SIGTERM);
859 }
860 break;
861 }
862
863 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +0200864 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +0200865 */
Jens Axboe34572e22006-10-20 09:33:18 +0200866 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +0200867 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +0100868 continue;
869
870 td_set_runstate(td, TD_RUNNING);
871 nr_running++;
872 nr_started--;
873 m_rate += td->ratemin;
874 t_rate += td->rate;
Jens Axboe75154842006-06-01 13:56:09 +0200875 todo--;
Jens Axboebbfd6b02006-06-07 19:42:54 +0200876 fio_sem_up(&td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100877 }
878
879 reap_threads(&nr_running, &t_rate, &m_rate);
880
881 if (todo)
882 usleep(100000);
883 }
884
885 while (nr_running) {
886 reap_threads(&nr_running, &t_rate, &m_rate);
887 usleep(10000);
888 }
889
890 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +0200891 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +0100892}
893
Jens Axboeebac4652005-12-08 15:25:21 +0100894int main(int argc, char *argv[])
895{
896 if (parse_options(argc, argv))
897 return 1;
898
899 if (!thread_number) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200900 log_err("Nothing to do\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100901 return 1;
902 }
903
904 disk_util_timer_arm();
905
906 run_threads();
907 show_run_stats();
908
909 return 0;
910}