blob: e8181a1c317f6a8a7a595100f9beed12126e9e5f [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 *
Jens Axboe8e9fe632006-10-24 15:11:09 +02007 * The license below covers all files distributed with fio unless otherwise
8 * noted in the file itself.
9 *
Jens Axboeebac4652005-12-08 15:25:21 +010010 * This program is free software; you can redistribute it and/or modify
Jens Axboe8e9fe632006-10-24 15:11:09 +020011 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
Jens Axboeebac4652005-12-08 15:25:21 +010013 *
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 */
Jens Axboeebac4652005-12-08 15:25:21 +010024#include <unistd.h>
25#include <fcntl.h>
26#include <string.h>
Jens Axboeebac4652005-12-08 15:25:21 +010027#include <signal.h>
28#include <time.h>
Jens Axboeebac4652005-12-08 15:25:21 +010029#include <assert.h>
Jens Axboeebac4652005-12-08 15:25:21 +010030#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
44int groupid = 0;
45int thread_number = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010046int shm_id = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +020047int temp_stall_ts;
Jens Axboeebac4652005-12-08 15:25:21 +010048
Jens Axboebbfd6b02006-06-07 19:42:54 +020049static volatile int startup_sem;
Jens Axboeebac4652005-12-08 15:25:21 +010050
51#define TERMINATE_ALL (-1)
Jens Axboe75154842006-06-01 13:56:09 +020052#define JOB_START_TIMEOUT (5 * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +010053
54static void terminate_threads(int group_id)
55{
Jens Axboe34572e22006-10-20 09:33:18 +020056 struct thread_data *td;
Jens Axboeebac4652005-12-08 15:25:21 +010057 int i;
58
Jens Axboe34572e22006-10-20 09:33:18 +020059 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +010060 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
61 td->terminate = 1;
62 td->start_delay = 0;
63 }
64 }
65}
66
67static void sig_handler(int sig)
68{
69 switch (sig) {
70 case SIGALRM:
71 update_io_ticks();
72 disk_util_timer_arm();
73 print_thread_status();
74 break;
75 default:
76 printf("\nfio: terminating on signal\n");
77 fflush(stdout);
78 terminate_threads(TERMINATE_ALL);
79 break;
80 }
81}
82
Jens Axboe906c8d72006-06-13 09:37:56 +020083/*
Jens Axboe906c8d72006-06-13 09:37:56 +020084 * Check if we are above the minimum rate given.
85 */
Jens Axboeebac4652005-12-08 15:25:21 +010086static int check_min_rate(struct thread_data *td, struct timeval *now)
87{
88 unsigned long spent;
89 unsigned long rate;
90 int ddir = td->ddir;
91
92 /*
93 * allow a 2 second settle period in the beginning
94 */
95 if (mtime_since(&td->start, now) < 2000)
96 return 0;
97
98 /*
99 * if rate blocks is set, sample is running
100 */
101 if (td->rate_bytes) {
102 spent = mtime_since(&td->lastrate, now);
103 if (spent < td->ratecycle)
104 return 0;
105
106 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
107 if (rate < td->ratemin) {
Jens Axboe1e97cce2006-12-05 11:44:16 +0100108 fprintf(f_out, "%s: min rate %u not met, got %luKiB/sec\n", td->name, td->ratemin, rate);
Jens Axboeebac4652005-12-08 15:25:21 +0100109 return 1;
110 }
111 }
112
113 td->rate_bytes = td->this_io_bytes[ddir];
114 memcpy(&td->lastrate, now, sizeof(*now));
115 return 0;
116}
117
118static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
119{
120 if (!td->timeout)
121 return 0;
122 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
123 return 1;
124
125 return 0;
126}
127
Jens Axboeebac4652005-12-08 15:25:21 +0100128static inline void td_set_runstate(struct thread_data *td, int runstate)
129{
Jens Axboeebac4652005-12-08 15:25:21 +0100130 td->runstate = runstate;
131}
132
Jens Axboe53cdc682006-10-18 11:50:58 +0200133static struct fio_file *get_next_file(struct thread_data *td)
134{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200135 unsigned int old_next_file = td->next_file;
Jens Axboeb2a15192006-10-18 14:10:42 +0200136 struct fio_file *f;
Jens Axboe53cdc682006-10-18 11:50:58 +0200137
Jens Axboeb2a15192006-10-18 14:10:42 +0200138 do {
139 f = &td->files[td->next_file];
140
141 td->next_file++;
142 if (td->next_file >= td->nr_files)
143 td->next_file = 0;
144
145 if (f->fd != -1)
146 break;
147
148 f = NULL;
149 } while (td->next_file != old_next_file);
Jens Axboe53cdc682006-10-18 11:50:58 +0200150
151 return f;
152}
153
Jens Axboe906c8d72006-06-13 09:37:56 +0200154/*
155 * When job exits, we can cancel the in-flight IO if we are using async
156 * io. Attempt to do so.
157 */
Jens Axboeebac4652005-12-08 15:25:21 +0100158static void cleanup_pending_aio(struct thread_data *td)
159{
160 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
161 struct list_head *entry, *n;
162 struct io_completion_data icd;
163 struct io_u *io_u;
164 int r;
165
166 /*
167 * get immediately available events, if any
168 */
Jens Axboe45bee282006-10-18 15:26:44 +0200169 r = td_io_getevents(td, 0, td->cur_depth, &ts);
Jens Axboeebac4652005-12-08 15:25:21 +0100170 if (r > 0) {
171 icd.nr = r;
172 ios_completed(td, &icd);
173 }
174
175 /*
176 * now cancel remaining active events
177 */
Jens Axboe2866c822006-10-09 15:57:48 +0200178 if (td->io_ops->cancel) {
Jens Axboeebac4652005-12-08 15:25:21 +0100179 list_for_each_safe(entry, n, &td->io_u_busylist) {
180 io_u = list_entry(entry, struct io_u, list);
181
Jens Axboe2866c822006-10-09 15:57:48 +0200182 r = td->io_ops->cancel(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100183 if (!r)
184 put_io_u(td, io_u);
185 }
186 }
187
188 if (td->cur_depth) {
Jens Axboe45bee282006-10-18 15:26:44 +0200189 r = td_io_getevents(td, td->cur_depth, td->cur_depth, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100190 if (r > 0) {
191 icd.nr = r;
192 ios_completed(td, &icd);
193 }
194 }
195}
196
Jens Axboe906c8d72006-06-13 09:37:56 +0200197/*
Jens Axboe858a3d42006-10-24 14:54:44 +0200198 * Helper to handle the final sync of a file. Works just like the normal
199 * io path, just does everything sync.
200 */
201static int fio_io_sync(struct thread_data *td, struct fio_file *f)
202{
203 struct io_u *io_u = __get_io_u(td);
204 struct io_completion_data icd;
205 int ret;
206
207 if (!io_u)
208 return 1;
209
210 io_u->ddir = DDIR_SYNC;
211 io_u->file = f;
212
213 if (td_io_prep(td, io_u)) {
214 put_io_u(td, io_u);
215 return 1;
216 }
217
218 ret = td_io_queue(td, io_u);
219 if (ret) {
Jens Axboe353a7e02006-10-25 09:16:07 +0200220 td_verror(td, io_u->error);
Jens Axboe858a3d42006-10-24 14:54:44 +0200221 put_io_u(td, io_u);
Jens Axboe858a3d42006-10-24 14:54:44 +0200222 return 1;
223 }
224
225 ret = td_io_getevents(td, 1, td->cur_depth, NULL);
226 if (ret < 0) {
Jens Axboe353a7e02006-10-25 09:16:07 +0200227 td_verror(td, ret);
Jens Axboe858a3d42006-10-24 14:54:44 +0200228 return 1;
229 }
230
231 icd.nr = ret;
232 ios_completed(td, &icd);
233 if (icd.error) {
234 td_verror(td, icd.error);
235 return 1;
236 }
237
238 return 0;
239}
240
241/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200242 * The main verify engine. Runs over the writes we previusly submitted,
243 * reads the blocks back in, and checks the crc/md5 of the data.
244 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100245static void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100246{
Jens Axboeebac4652005-12-08 15:25:21 +0100247 struct io_u *io_u, *v_io_u = NULL;
248 struct io_completion_data icd;
Jens Axboe53cdc682006-10-18 11:50:58 +0200249 struct fio_file *f;
Jens Axboee5b401d2006-10-18 16:03:40 +0200250 int ret, i;
251
252 /*
253 * sync io first and invalidate cache, to make sure we really
254 * read from disk.
255 */
256 for_each_file(td, f, i) {
Jens Axboe858a3d42006-10-24 14:54:44 +0200257 fio_io_sync(td, f);
Jens Axboee5b401d2006-10-18 16:03:40 +0200258 file_invalidate_cache(td, f);
259 }
Jens Axboeebac4652005-12-08 15:25:21 +0100260
261 td_set_runstate(td, TD_VERIFYING);
262
263 do {
264 if (td->terminate)
265 break;
266
Jens Axboeebac4652005-12-08 15:25:21 +0100267 io_u = __get_io_u(td);
268 if (!io_u)
269 break;
270
Jens Axboe02bcaa82006-11-24 10:42:00 +0100271 if (runtime_exceeded(td, &io_u->start_time)) {
272 put_io_u(td, io_u);
273 break;
274 }
275
Jens Axboeaea47d42006-05-26 19:27:29 +0200276 if (get_next_verify(td, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100277 put_io_u(td, io_u);
278 break;
279 }
280
Jens Axboe53cdc682006-10-18 11:50:58 +0200281 f = get_next_file(td);
282 if (!f)
283 break;
284
285 io_u->file = f;
286
Jens Axboeaea47d42006-05-26 19:27:29 +0200287 if (td_io_prep(td, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100288 put_io_u(td, io_u);
289 break;
290 }
291
Jens Axboe45bee282006-10-18 15:26:44 +0200292 ret = td_io_queue(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100293 if (ret) {
Jens Axboe353a7e02006-10-25 09:16:07 +0200294 td_verror(td, io_u->error);
Jens Axboeebac4652005-12-08 15:25:21 +0100295 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100296 break;
297 }
298
299 /*
300 * we have one pending to verify, do that while
301 * we are doing io on the next one
302 */
303 if (do_io_u_verify(td, &v_io_u))
304 break;
305
Jens Axboe45bee282006-10-18 15:26:44 +0200306 ret = td_io_getevents(td, 1, 1, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100307 if (ret != 1) {
308 if (ret < 0)
309 td_verror(td, ret);
310 break;
311 }
312
Jens Axboe2866c822006-10-09 15:57:48 +0200313 v_io_u = td->io_ops->event(td, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100314 icd.nr = 1;
315 icd.error = 0;
Jens Axboe02bcaa82006-11-24 10:42:00 +0100316 fio_gettime(&icd.time, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100317 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
Jens Axboeebac4652005-12-08 15:25:21 +0100326 /*
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
Jens Axboe32cd46a2006-06-07 13:40:40 +0200342/*
Jens Axboeb990b5c2006-09-14 09:48:22 +0200343 * Not really an io thread, all it does is burn CPU cycles in the specified
344 * manner.
345 */
346static 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) {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100353 fio_gettime(&e, NULL);
Jens Axboeb990b5c2006-09-14 09:48:22 +0200354
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
367/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200368 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200369 * and reaps them, checking for rate and errors along the way.
370 */
Jens Axboeebac4652005-12-08 15:25:21 +0100371static void do_io(struct thread_data *td)
372{
373 struct io_completion_data icd;
Jens Axboe02bcaa82006-11-24 10:42:00 +0100374 struct timeval s;
Jens Axboeebac4652005-12-08 15:25:21 +0100375 unsigned long usec;
Jens Axboe53cdc682006-10-18 11:50:58 +0200376 struct fio_file *f;
Jens Axboe84585002006-10-19 20:26:22 +0200377 int i, ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100378
Jens Axboe5853e5a2006-06-08 14:41:05 +0200379 td_set_runstate(td, TD_RUNNING);
380
Jens Axboeebac4652005-12-08 15:25:21 +0100381 while (td->this_io_bytes[td->ddir] < td->io_size) {
382 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
383 struct timespec *timeout;
Jens Axboe84585002006-10-19 20:26:22 +0200384 int min_evts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100385 struct io_u *io_u;
386
387 if (td->terminate)
388 break;
389
Jens Axboe53cdc682006-10-18 11:50:58 +0200390 f = get_next_file(td);
391 if (!f)
392 break;
393
394 io_u = get_io_u(td, f);
Jens Axboeebac4652005-12-08 15:25:21 +0100395 if (!io_u)
396 break;
397
398 memcpy(&s, &io_u->start_time, sizeof(s));
399
Jens Axboe45bee282006-10-18 15:26:44 +0200400 ret = td_io_queue(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100401 if (ret) {
Jens Axboe353a7e02006-10-25 09:16:07 +0200402 td_verror(td, io_u->error);
Jens Axboeebac4652005-12-08 15:25:21 +0100403 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100404 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
Jens Axboe45bee282006-10-18 15:26:44 +0200417 ret = td_io_getevents(td, min_evts, td->cur_depth, timeout);
Jens Axboeebac4652005-12-08 15:25:21 +0100418 if (ret < 0) {
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200419 td_verror(td, ret);
Jens Axboeebac4652005-12-08 15:25:21 +0100420 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 */
Jens Axboe02bcaa82006-11-24 10:42:00 +0100436 usec = utime_since(&s, &icd.time);
Jens Axboeebac4652005-12-08 15:25:21 +0100437
Jens Axboea00735e2006-11-03 08:58:08 +0100438 rate_throttle(td, usec, icd.bytes_done[td->ddir], td->ddir);
Jens Axboeebac4652005-12-08 15:25:21 +0100439
Jens Axboe02bcaa82006-11-24 10:42:00 +0100440 if (check_min_rate(td, &icd.time)) {
Jens Axboe98aa62d2006-11-07 14:16:19 +0100441 if (exitall_on_terminate)
Jens Axboe2f9ade32006-10-20 11:25:52 +0200442 terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +0100443 td_verror(td, ENOMEM);
444 break;
445 }
446
Jens Axboe02bcaa82006-11-24 10:42:00 +0100447 if (runtime_exceeded(td, &icd.time))
Jens Axboeebac4652005-12-08 15:25:21 +0100448 break;
449
Jens Axboe9c1f7432007-01-03 20:43:19 +0100450 if (td->thinktime) {
451 unsigned long long b;
452
453 b = td->io_blocks[0] + td->io_blocks[1];
454 if (!(td->thinktime_blocks % b))
455 usec_sleep(td, td->thinktime);
456 }
Jens Axboeebac4652005-12-08 15:25:21 +0100457 }
458
Jens Axboe4d2413c2006-11-23 11:58:59 +0100459 if (!td->error) {
Jens Axboe84585002006-10-19 20:26:22 +0200460 if (td->cur_depth)
461 cleanup_pending_aio(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100462
Jens Axboe84585002006-10-19 20:26:22 +0200463 if (should_fsync(td) && td->end_fsync) {
464 td_set_runstate(td, TD_FSYNCING);
465 for_each_file(td, f, i)
Jens Axboe858a3d42006-10-24 14:54:44 +0200466 fio_io_sync(td, f);
Jens Axboe84585002006-10-19 20:26:22 +0200467 }
Jens Axboe5853e5a2006-06-08 14:41:05 +0200468 }
Jens Axboeebac4652005-12-08 15:25:21 +0100469}
470
Jens Axboeebac4652005-12-08 15:25:21 +0100471static void cleanup_io_u(struct thread_data *td)
472{
473 struct list_head *entry, *n;
474 struct io_u *io_u;
475
476 list_for_each_safe(entry, n, &td->io_u_freelist) {
477 io_u = list_entry(entry, struct io_u, list);
478
479 list_del(&io_u->list);
480 free(io_u);
481 }
482
Jens Axboe2f9ade32006-10-20 11:25:52 +0200483 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100484}
485
Jens Axboe6b9cea22006-10-30 17:00:24 +0100486/*
487 * "randomly" fill the buffer contents
488 */
Jens Axboe66eeb292006-11-01 08:35:44 +0100489static void fill_rand_buf(struct io_u *io_u, int max_bs)
Jens Axboe6b9cea22006-10-30 17:00:24 +0100490{
Jens Axboe66eeb292006-11-01 08:35:44 +0100491 int *ptr = io_u->buf;
Jens Axboe6b9cea22006-10-30 17:00:24 +0100492
493 while ((void *) ptr - io_u->buf < max_bs) {
494 *ptr = rand() * 0x9e370001;
495 ptr++;
496 }
497}
498
Jens Axboeebac4652005-12-08 15:25:21 +0100499static int init_io_u(struct thread_data *td)
500{
501 struct io_u *io_u;
Jens Axboea00735e2006-11-03 08:58:08 +0100502 unsigned int max_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100503 int i, max_units;
504 char *p;
505
Jens Axboe2866c822006-10-09 15:57:48 +0200506 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200507 return 0;
508
Jens Axboe2866c822006-10-09 15:57:48 +0200509 if (td->io_ops->flags & FIO_SYNCIO)
Jens Axboeebac4652005-12-08 15:25:21 +0100510 max_units = 1;
511 else
512 max_units = td->iodepth;
513
Jens Axboea00735e2006-11-03 08:58:08 +0100514 max_bs = max(td->max_bs[DDIR_READ], td->max_bs[DDIR_WRITE]);
Jens Axboe74b025b2006-12-19 15:18:14 +0100515 td->orig_buffer_size = max_bs * max_units;
516
Jens Axboed0bdaf42006-12-20 14:40:44 +0100517 if (td->mem_type == MEM_SHMHUGE || td->mem_type == MEM_MMAPHUGE)
Jens Axboe56bb17f2006-12-20 20:27:36 +0100518 td->orig_buffer_size = (td->orig_buffer_size + td->hugepage_size - 1) & ~(td->hugepage_size - 1);
Jens Axboe74b025b2006-12-19 15:18:14 +0100519 else
520 td->orig_buffer_size += MASK;
Jens Axboeebac4652005-12-08 15:25:21 +0100521
Jens Axboe2f9ade32006-10-20 11:25:52 +0200522 if (allocate_io_mem(td))
523 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100524
Jens Axboeebac4652005-12-08 15:25:21 +0100525 p = ALIGN(td->orig_buffer);
526 for (i = 0; i < max_units; i++) {
527 io_u = malloc(sizeof(*io_u));
528 memset(io_u, 0, sizeof(*io_u));
529 INIT_LIST_HEAD(&io_u->list);
530
Jens Axboea00735e2006-11-03 08:58:08 +0100531 io_u->buf = p + max_bs * i;
Jens Axboe6b9cea22006-10-30 17:00:24 +0100532 if (td_write(td) || td_rw(td))
Jens Axboea00735e2006-11-03 08:58:08 +0100533 fill_rand_buf(io_u, max_bs);
Jens Axboe6b9cea22006-10-30 17:00:24 +0100534
Jens Axboeb1ff3402006-02-17 11:35:58 +0100535 io_u->index = i;
Jens Axboeebac4652005-12-08 15:25:21 +0100536 list_add(&io_u->list, &td->io_u_freelist);
537 }
538
539 return 0;
540}
541
Jens Axboeda867742006-06-06 10:39:10 -0700542static int switch_ioscheduler(struct thread_data *td)
543{
544 char tmp[256], tmp2[128];
545 FILE *f;
546 int ret;
547
Jens Axboef48b4672006-10-27 11:30:07 +0200548 if (td->io_ops->flags & FIO_CPUIO)
549 return 0;
550
Jens Axboeda867742006-06-06 10:39:10 -0700551 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
552
553 f = fopen(tmp, "r+");
554 if (!f) {
555 td_verror(td, errno);
556 return 1;
557 }
558
559 /*
560 * Set io scheduler.
561 */
562 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
563 if (ferror(f) || ret != 1) {
564 td_verror(td, errno);
565 fclose(f);
566 return 1;
567 }
568
569 rewind(f);
570
571 /*
572 * Read back and check that the selected scheduler is now the default.
573 */
574 ret = fread(tmp, 1, sizeof(tmp), f);
575 if (ferror(f) || ret < 0) {
576 td_verror(td, errno);
577 fclose(f);
578 return 1;
579 }
580
581 sprintf(tmp2, "[%s]", td->ioscheduler);
582 if (!strstr(tmp, tmp2)) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200583 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700584 td_verror(td, EINVAL);
585 fclose(f);
586 return 1;
587 }
588
589 fclose(f);
590 return 0;
591}
592
Jens Axboeebac4652005-12-08 15:25:21 +0100593static void clear_io_state(struct thread_data *td)
594{
Jens Axboe53cdc682006-10-18 11:50:58 +0200595 struct fio_file *f;
596 int i;
Jens Axboeebac4652005-12-08 15:25:21 +0100597
Jens Axboeebac4652005-12-08 15:25:21 +0100598 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
599 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100600 td->zone_bytes = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100601
Jens Axboe53cdc682006-10-18 11:50:58 +0200602 for_each_file(td, f, i) {
603 f->last_pos = 0;
604 if (td->io_ops->flags & FIO_SYNCIO)
605 lseek(f->fd, SEEK_SET, 0);
606
607 if (f->file_map)
608 memset(f->file_map, 0, f->num_maps * sizeof(long));
609 }
Jens Axboeebac4652005-12-08 15:25:21 +0100610}
611
Jens Axboe906c8d72006-06-13 09:37:56 +0200612/*
613 * Entry point for the thread based jobs. The process based jobs end up
614 * here as well, after a little setup.
615 */
Jens Axboeebac4652005-12-08 15:25:21 +0100616static void *thread_main(void *data)
617{
Jens Axboe69008992006-11-24 13:12:56 +0100618 unsigned long long runtime[2];
Jens Axboeebac4652005-12-08 15:25:21 +0100619 struct thread_data *td = data;
Jens Axboeebac4652005-12-08 15:25:21 +0100620
621 if (!td->use_thread)
622 setsid();
623
624 td->pid = getpid();
625
Jens Axboeaea47d42006-05-26 19:27:29 +0200626 INIT_LIST_HEAD(&td->io_u_freelist);
627 INIT_LIST_HEAD(&td->io_u_busylist);
628 INIT_LIST_HEAD(&td->io_hist_list);
629 INIT_LIST_HEAD(&td->io_log_list);
630
Jens Axboeebac4652005-12-08 15:25:21 +0100631 if (init_io_u(td))
632 goto err;
633
634 if (fio_setaffinity(td) == -1) {
635 td_verror(td, errno);
636 goto err;
637 }
638
Jens Axboe45bee282006-10-18 15:26:44 +0200639 if (td_io_init(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100640 goto err;
641
Jens Axboeaea47d42006-05-26 19:27:29 +0200642 if (init_iolog(td))
643 goto err;
644
Jens Axboeebac4652005-12-08 15:25:21 +0100645 if (td->ioprio) {
646 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
647 td_verror(td, errno);
648 goto err;
649 }
650 }
651
Jens Axboe1056eaa2006-07-13 10:33:45 -0700652 if (nice(td->nice) == -1) {
Jens Axboeb6f4d882006-06-02 10:32:51 +0200653 td_verror(td, errno);
654 goto err;
655 }
656
Jens Axboe75154842006-06-01 13:56:09 +0200657 if (init_random_state(td))
658 goto err;
659
Jens Axboe56f94982006-10-24 09:33:42 +0200660 if (td->ioscheduler && switch_ioscheduler(td))
661 goto err;
Jens Axboeda867742006-06-06 10:39:10 -0700662
Jens Axboe75154842006-06-01 13:56:09 +0200663 td_set_runstate(td, TD_INITIALIZED);
Jens Axboebbfd6b02006-06-07 19:42:54 +0200664 fio_sem_up(&startup_sem);
665 fio_sem_down(&td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100666
Jens Axboe53cdc682006-10-18 11:50:58 +0200667 if (!td->create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100668 goto err;
Jens Axboe21972cd2006-11-23 12:39:16 +0100669 if (open_files(td))
670 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +0100671
Jens Axboe56f94982006-10-24 09:33:42 +0200672 if (td->exec_prerun)
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200673 system(td->exec_prerun);
674
Jens Axboe69008992006-11-24 13:12:56 +0100675 fio_gettime(&td->epoch, NULL);
Jens Axboe36dff962006-11-24 13:29:20 +0100676 getrusage(RUSAGE_SELF, &td->ru_start);
Jens Axboe69008992006-11-24 13:12:56 +0100677
678 runtime[0] = runtime[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100679 while (td->loops--) {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100680 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100681 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
682
683 if (td->ratemin)
684 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
685
686 clear_io_state(td);
687 prune_io_piece_log(td);
688
Jens Axboe2866c822006-10-09 15:57:48 +0200689 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200690 do_cpuio(td);
691 else
692 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100693
Jens Axboe69008992006-11-24 13:12:56 +0100694 runtime[td->ddir] += utime_since_now(&td->start);
Jens Axboeaea47d42006-05-26 19:27:29 +0200695 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
Jens Axboe69008992006-11-24 13:12:56 +0100696 runtime[td->ddir ^ 1] = runtime[td->ddir];
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200697
Jens Axboeebac4652005-12-08 15:25:21 +0100698 if (td->error || td->terminate)
699 break;
700
701 if (td->verify == VERIFY_NONE)
702 continue;
703
704 clear_io_state(td);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100705 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100706
707 do_verify(td);
708
Jens Axboe69008992006-11-24 13:12:56 +0100709 runtime[DDIR_READ] += utime_since_now(&td->start);
Jens Axboeebac4652005-12-08 15:25:21 +0100710
711 if (td->error || td->terminate)
712 break;
713 }
714
Jens Axboe36dff962006-11-24 13:29:20 +0100715 update_rusage_stat(td);
Jens Axboe69008992006-11-24 13:12:56 +0100716 fio_gettime(&td->end_time, NULL);
717 td->runtime[0] = runtime[0] / 1000;
718 td->runtime[1] = runtime[1] / 1000;
719
Jens Axboeebac4652005-12-08 15:25:21 +0100720 if (td->bw_log)
721 finish_log(td, td->bw_log, "bw");
722 if (td->slat_log)
723 finish_log(td, td->slat_log, "slat");
724 if (td->clat_log)
725 finish_log(td, td->clat_log, "clat");
Jens Axboe076efc72006-10-27 11:24:25 +0200726 if (td->write_iolog_file)
Jens Axboe843a7412006-06-01 21:14:21 -0700727 write_iolog_close(td);
Jens Axboe56f94982006-10-24 09:33:42 +0200728 if (td->exec_postrun)
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200729 system(td->exec_postrun);
Jens Axboeebac4652005-12-08 15:25:21 +0100730
731 if (exitall_on_terminate)
732 terminate_threads(td->groupid);
733
734err:
Jens Axboe53cdc682006-10-18 11:50:58 +0200735 close_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +0200736 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100737 cleanup_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100738 td_set_runstate(td, TD_EXITED);
739 return NULL;
740
741}
742
Jens Axboe906c8d72006-06-13 09:37:56 +0200743/*
744 * We cannot pass the td data into a forked process, so attach the td and
745 * pass it to the thread worker.
746 */
Jens Axboeebac4652005-12-08 15:25:21 +0100747static void *fork_main(int shmid, int offset)
748{
749 struct thread_data *td;
750 void *data;
751
752 data = shmat(shmid, NULL, 0);
753 if (data == (void *) -1) {
754 perror("shmat");
755 return NULL;
756 }
757
758 td = data + offset * sizeof(struct thread_data);
759 thread_main(td);
760 shmdt(data);
761 return NULL;
762}
763
Jens Axboe906c8d72006-06-13 09:37:56 +0200764/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200765 * Run over the job map and reap the threads that have exited, if any.
766 */
Jens Axboeebac4652005-12-08 15:25:21 +0100767static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
768{
Jens Axboe34572e22006-10-20 09:33:18 +0200769 struct thread_data *td;
Jens Axboe4d2413c2006-11-23 11:58:59 +0100770 int i, cputhreads, pending;
Jens Axboeebac4652005-12-08 15:25:21 +0100771
772 /*
773 * reap exited threads (TD_EXITED -> TD_REAPED)
774 */
Jens Axboe4d2413c2006-11-23 11:58:59 +0100775 pending = cputhreads = 0;
Jens Axboe34572e22006-10-20 09:33:18 +0200776 for_each_td(td, i) {
Jens Axboe84585002006-10-19 20:26:22 +0200777 /*
778 * ->io_ops is NULL for a thread that has closed its
779 * io engine
780 */
781 if (td->io_ops && td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200782 cputhreads++;
783
Jens Axboe4d2413c2006-11-23 11:58:59 +0100784 if (td->runstate != TD_EXITED) {
785 if (td->runstate < TD_RUNNING)
786 pending++;
787
Jens Axboeebac4652005-12-08 15:25:21 +0100788 continue;
Jens Axboe4d2413c2006-11-23 11:58:59 +0100789 }
Jens Axboeebac4652005-12-08 15:25:21 +0100790
791 td_set_runstate(td, TD_REAPED);
792
793 if (td->use_thread) {
794 long ret;
795
796 if (pthread_join(td->thread, (void *) &ret))
797 perror("thread_join");
798 } else
799 waitpid(td->pid, NULL, 0);
800
801 (*nr_running)--;
802 (*m_rate) -= td->ratemin;
803 (*t_rate) -= td->rate;
804 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200805
Jens Axboe4d2413c2006-11-23 11:58:59 +0100806 if (*nr_running == cputhreads && !pending)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200807 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +0100808}
809
Jens Axboe906c8d72006-06-13 09:37:56 +0200810/*
811 * Main function for kicking off and reaping jobs, as needed.
812 */
Jens Axboeebac4652005-12-08 15:25:21 +0100813static void run_threads(void)
814{
Jens Axboeebac4652005-12-08 15:25:21 +0100815 struct thread_data *td;
816 unsigned long spent;
817 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +0200818
Jens Axboe2f9ade32006-10-20 11:25:52 +0200819 if (fio_pin_memory())
820 return;
Jens Axboeebac4652005-12-08 15:25:21 +0100821
Jens Axboec6ae0a52006-06-12 11:04:44 +0200822 if (!terse_output) {
823 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
824 fflush(stdout);
825 }
Jens Axboec04f7ec2006-05-31 10:13:16 +0200826
Jens Axboe4efa9702006-05-31 11:56:49 +0200827 signal(SIGINT, sig_handler);
828 signal(SIGALRM, sig_handler);
829
Jens Axboeebac4652005-12-08 15:25:21 +0100830 todo = thread_number;
831 nr_running = 0;
832 nr_started = 0;
833 m_rate = t_rate = 0;
834
Jens Axboe34572e22006-10-20 09:33:18 +0200835 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +0200836 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +0100837
838 init_disk_util(td);
839
840 if (!td->create_serialize)
841 continue;
842
843 /*
844 * do file setup here so it happens sequentially,
845 * we don't want X number of threads getting their
846 * client data interspersed on disk
847 */
Jens Axboe53cdc682006-10-18 11:50:58 +0200848 if (setup_files(td)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100849 td_set_runstate(td, TD_REAPED);
850 todo--;
851 }
852 }
853
Jens Axboeebac4652005-12-08 15:25:21 +0100854 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +0200855 struct thread_data *map[MAX_JOBS];
856 struct timeval this_start;
857 int this_jobs = 0, left;
858
Jens Axboeebac4652005-12-08 15:25:21 +0100859 /*
860 * create threads (TD_NOT_CREATED -> TD_CREATED)
861 */
Jens Axboe34572e22006-10-20 09:33:18 +0200862 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +0100863 if (td->runstate != TD_NOT_CREATED)
864 continue;
865
866 /*
867 * never got a chance to start, killed by other
868 * thread for some reason
869 */
870 if (td->terminate) {
871 todo--;
872 continue;
873 }
874
875 if (td->start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +0200876 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +0100877
878 if (td->start_delay * 1000 > spent)
879 continue;
880 }
881
882 if (td->stonewall && (nr_started || nr_running))
883 break;
884
Jens Axboe75154842006-06-01 13:56:09 +0200885 /*
886 * Set state to created. Thread will transition
887 * to TD_INITIALIZED when it's done setting up.
888 */
Jens Axboeebac4652005-12-08 15:25:21 +0100889 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +0200890 map[this_jobs++] = td;
Jens Axboebbfd6b02006-06-07 19:42:54 +0200891 fio_sem_init(&startup_sem, 1);
Jens Axboeebac4652005-12-08 15:25:21 +0100892 nr_started++;
893
894 if (td->use_thread) {
895 if (pthread_create(&td->thread, NULL, thread_main, td)) {
896 perror("thread_create");
897 nr_started--;
898 }
899 } else {
900 if (fork())
Jens Axboebbfd6b02006-06-07 19:42:54 +0200901 fio_sem_down(&startup_sem);
Jens Axboeebac4652005-12-08 15:25:21 +0100902 else {
903 fork_main(shm_id, i);
904 exit(0);
905 }
906 }
907 }
908
909 /*
Jens Axboe75154842006-06-01 13:56:09 +0200910 * Wait for the started threads to transition to
911 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +0100912 */
Jens Axboe02bcaa82006-11-24 10:42:00 +0100913 fio_gettime(&this_start, NULL);
Jens Axboe75154842006-06-01 13:56:09 +0200914 left = this_jobs;
915 while (left) {
916 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
917 break;
918
919 usleep(100000);
920
921 for (i = 0; i < this_jobs; i++) {
922 td = map[i];
923 if (!td)
924 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +0200925 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +0200926 map[i] = NULL;
927 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +0200928 } else if (td->runstate >= TD_EXITED) {
929 map[i] = NULL;
930 left--;
931 todo--;
932 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +0200933 }
934 }
935 }
936
937 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200938 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +0200939 for (i = 0; i < this_jobs; i++) {
940 td = map[i];
941 if (!td)
942 continue;
943 kill(td->pid, SIGTERM);
944 }
945 break;
946 }
947
948 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +0200949 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +0200950 */
Jens Axboe34572e22006-10-20 09:33:18 +0200951 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +0200952 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +0100953 continue;
954
955 td_set_runstate(td, TD_RUNNING);
956 nr_running++;
957 nr_started--;
958 m_rate += td->ratemin;
959 t_rate += td->rate;
Jens Axboe75154842006-06-01 13:56:09 +0200960 todo--;
Jens Axboebbfd6b02006-06-07 19:42:54 +0200961 fio_sem_up(&td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100962 }
963
964 reap_threads(&nr_running, &t_rate, &m_rate);
965
966 if (todo)
967 usleep(100000);
968 }
969
970 while (nr_running) {
971 reap_threads(&nr_running, &t_rate, &m_rate);
972 usleep(10000);
973 }
974
975 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +0200976 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +0100977}
978
Jens Axboeebac4652005-12-08 15:25:21 +0100979int main(int argc, char *argv[])
980{
981 if (parse_options(argc, argv))
982 return 1;
983
984 if (!thread_number) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200985 log_err("Nothing to do\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100986 return 1;
987 }
988
989 disk_util_timer_arm();
990
991 run_threads();
992 show_run_stats();
993
994 return 0;
995}