blob: 225aaa4d4550c2ea385f8eaaee70b9b8d1bc321d [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 Axboe6ce15a32007-01-12 09:04:41 +010050static volatile int fio_abort;
Jens Axboeebac4652005-12-08 15:25:21 +010051
52#define TERMINATE_ALL (-1)
Jens Axboe75154842006-06-01 13:56:09 +020053#define JOB_START_TIMEOUT (5 * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +010054
Jens Axboe6ce15a32007-01-12 09:04:41 +010055static inline void td_set_runstate(struct thread_data *td, int runstate)
56{
57 td->runstate = runstate;
58}
59
60static void terminate_threads(int group_id, int forced_kill)
Jens Axboeebac4652005-12-08 15:25:21 +010061{
Jens Axboe34572e22006-10-20 09:33:18 +020062 struct thread_data *td;
Jens Axboeebac4652005-12-08 15:25:21 +010063 int i;
64
Jens Axboe34572e22006-10-20 09:33:18 +020065 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +010066 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
67 td->terminate = 1;
68 td->start_delay = 0;
Jens Axboe6ce15a32007-01-12 09:04:41 +010069 if (forced_kill)
70 td_set_runstate(td, TD_EXITED);
Jens Axboeebac4652005-12-08 15:25:21 +010071 }
72 }
73}
74
75static void sig_handler(int sig)
76{
77 switch (sig) {
78 case SIGALRM:
79 update_io_ticks();
80 disk_util_timer_arm();
81 print_thread_status();
82 break;
Jens Axboe6ce15a32007-01-12 09:04:41 +010083 case SIGSEGV:
84 fprintf(stderr, "fio: got segfault, aborting\n");
85 terminate_threads(TERMINATE_ALL, 1);
86 fio_abort = 1;
87 exit(0);
Jens Axboeebac4652005-12-08 15:25:21 +010088 default:
Jens Axboe6ce15a32007-01-12 09:04:41 +010089 printf("\nfio: terminating on signal %d\n", sig);
Jens Axboeebac4652005-12-08 15:25:21 +010090 fflush(stdout);
Jens Axboe6ce15a32007-01-12 09:04:41 +010091 terminate_threads(TERMINATE_ALL, 0);
Jens Axboeebac4652005-12-08 15:25:21 +010092 break;
93 }
94}
95
Jens Axboe906c8d72006-06-13 09:37:56 +020096/*
Jens Axboe906c8d72006-06-13 09:37:56 +020097 * Check if we are above the minimum rate given.
98 */
Jens Axboeebac4652005-12-08 15:25:21 +010099static int check_min_rate(struct thread_data *td, struct timeval *now)
100{
101 unsigned long spent;
102 unsigned long rate;
103 int ddir = td->ddir;
104
105 /*
106 * allow a 2 second settle period in the beginning
107 */
108 if (mtime_since(&td->start, now) < 2000)
109 return 0;
110
111 /*
112 * if rate blocks is set, sample is running
113 */
114 if (td->rate_bytes) {
115 spent = mtime_since(&td->lastrate, now);
116 if (spent < td->ratecycle)
117 return 0;
118
119 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
120 if (rate < td->ratemin) {
Jens Axboe1e97cce2006-12-05 11:44:16 +0100121 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 +0100122 return 1;
123 }
124 }
125
126 td->rate_bytes = td->this_io_bytes[ddir];
127 memcpy(&td->lastrate, now, sizeof(*now));
128 return 0;
129}
130
131static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
132{
133 if (!td->timeout)
134 return 0;
135 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
136 return 1;
137
138 return 0;
139}
140
Jens Axboe53cdc682006-10-18 11:50:58 +0200141static struct fio_file *get_next_file(struct thread_data *td)
142{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200143 unsigned int old_next_file = td->next_file;
Jens Axboeb2a15192006-10-18 14:10:42 +0200144 struct fio_file *f;
Jens Axboe53cdc682006-10-18 11:50:58 +0200145
Jens Axboeb2a15192006-10-18 14:10:42 +0200146 do {
147 f = &td->files[td->next_file];
148
149 td->next_file++;
150 if (td->next_file >= td->nr_files)
151 td->next_file = 0;
152
153 if (f->fd != -1)
154 break;
155
156 f = NULL;
157 } while (td->next_file != old_next_file);
Jens Axboe53cdc682006-10-18 11:50:58 +0200158
159 return f;
160}
161
Jens Axboe906c8d72006-06-13 09:37:56 +0200162/*
163 * When job exits, we can cancel the in-flight IO if we are using async
164 * io. Attempt to do so.
165 */
Jens Axboeebac4652005-12-08 15:25:21 +0100166static void cleanup_pending_aio(struct thread_data *td)
167{
168 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
169 struct list_head *entry, *n;
170 struct io_completion_data icd;
171 struct io_u *io_u;
172 int r;
173
174 /*
175 * get immediately available events, if any
176 */
Jens Axboe45bee282006-10-18 15:26:44 +0200177 r = td_io_getevents(td, 0, td->cur_depth, &ts);
Jens Axboeebac4652005-12-08 15:25:21 +0100178 if (r > 0) {
179 icd.nr = r;
180 ios_completed(td, &icd);
181 }
182
183 /*
184 * now cancel remaining active events
185 */
Jens Axboe2866c822006-10-09 15:57:48 +0200186 if (td->io_ops->cancel) {
Jens Axboeebac4652005-12-08 15:25:21 +0100187 list_for_each_safe(entry, n, &td->io_u_busylist) {
188 io_u = list_entry(entry, struct io_u, list);
189
Jens Axboe2866c822006-10-09 15:57:48 +0200190 r = td->io_ops->cancel(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100191 if (!r)
192 put_io_u(td, io_u);
193 }
194 }
195
196 if (td->cur_depth) {
Jens Axboe45bee282006-10-18 15:26:44 +0200197 r = td_io_getevents(td, td->cur_depth, td->cur_depth, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100198 if (r > 0) {
199 icd.nr = r;
200 ios_completed(td, &icd);
201 }
202 }
203}
204
Jens Axboe906c8d72006-06-13 09:37:56 +0200205/*
Jens Axboe858a3d42006-10-24 14:54:44 +0200206 * Helper to handle the final sync of a file. Works just like the normal
207 * io path, just does everything sync.
208 */
209static int fio_io_sync(struct thread_data *td, struct fio_file *f)
210{
211 struct io_u *io_u = __get_io_u(td);
212 struct io_completion_data icd;
213 int ret;
214
215 if (!io_u)
216 return 1;
217
218 io_u->ddir = DDIR_SYNC;
219 io_u->file = f;
220
221 if (td_io_prep(td, io_u)) {
222 put_io_u(td, io_u);
223 return 1;
224 }
225
226 ret = td_io_queue(td, io_u);
227 if (ret) {
Jens Axboe353a7e02006-10-25 09:16:07 +0200228 td_verror(td, io_u->error);
Jens Axboe858a3d42006-10-24 14:54:44 +0200229 put_io_u(td, io_u);
Jens Axboe858a3d42006-10-24 14:54:44 +0200230 return 1;
231 }
232
233 ret = td_io_getevents(td, 1, td->cur_depth, NULL);
234 if (ret < 0) {
Jens Axboe353a7e02006-10-25 09:16:07 +0200235 td_verror(td, ret);
Jens Axboe858a3d42006-10-24 14:54:44 +0200236 return 1;
237 }
238
239 icd.nr = ret;
240 ios_completed(td, &icd);
241 if (icd.error) {
242 td_verror(td, icd.error);
243 return 1;
244 }
245
246 return 0;
247}
248
249/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200250 * The main verify engine. Runs over the writes we previusly submitted,
251 * reads the blocks back in, and checks the crc/md5 of the data.
252 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100253static void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100254{
Jens Axboeebac4652005-12-08 15:25:21 +0100255 struct io_u *io_u, *v_io_u = NULL;
256 struct io_completion_data icd;
Jens Axboe53cdc682006-10-18 11:50:58 +0200257 struct fio_file *f;
Jens Axboee5b401d2006-10-18 16:03:40 +0200258 int ret, i;
259
260 /*
261 * sync io first and invalidate cache, to make sure we really
262 * read from disk.
263 */
264 for_each_file(td, f, i) {
Jens Axboe858a3d42006-10-24 14:54:44 +0200265 fio_io_sync(td, f);
Jens Axboee5b401d2006-10-18 16:03:40 +0200266 file_invalidate_cache(td, f);
267 }
Jens Axboeebac4652005-12-08 15:25:21 +0100268
269 td_set_runstate(td, TD_VERIFYING);
270
271 do {
272 if (td->terminate)
273 break;
274
Jens Axboeebac4652005-12-08 15:25:21 +0100275 io_u = __get_io_u(td);
276 if (!io_u)
277 break;
278
Jens Axboe02bcaa82006-11-24 10:42:00 +0100279 if (runtime_exceeded(td, &io_u->start_time)) {
280 put_io_u(td, io_u);
281 break;
282 }
283
Jens Axboeaea47d42006-05-26 19:27:29 +0200284 if (get_next_verify(td, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100285 put_io_u(td, io_u);
286 break;
287 }
288
Jens Axboe53cdc682006-10-18 11:50:58 +0200289 f = get_next_file(td);
290 if (!f)
291 break;
292
293 io_u->file = f;
294
Jens Axboeaea47d42006-05-26 19:27:29 +0200295 if (td_io_prep(td, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100296 put_io_u(td, io_u);
297 break;
298 }
299
Jens Axboe45bee282006-10-18 15:26:44 +0200300 ret = td_io_queue(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100301 if (ret) {
Jens Axboe353a7e02006-10-25 09:16:07 +0200302 td_verror(td, io_u->error);
Jens Axboeebac4652005-12-08 15:25:21 +0100303 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100304 break;
305 }
306
307 /*
308 * we have one pending to verify, do that while
309 * we are doing io on the next one
310 */
311 if (do_io_u_verify(td, &v_io_u))
312 break;
313
Jens Axboe45bee282006-10-18 15:26:44 +0200314 ret = td_io_getevents(td, 1, 1, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100315 if (ret != 1) {
316 if (ret < 0)
317 td_verror(td, ret);
318 break;
319 }
320
Jens Axboe2866c822006-10-09 15:57:48 +0200321 v_io_u = td->io_ops->event(td, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100322 icd.nr = 1;
323 icd.error = 0;
Jens Axboe02bcaa82006-11-24 10:42:00 +0100324 fio_gettime(&icd.time, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100325 io_completed(td, v_io_u, &icd);
326
327 if (icd.error) {
328 td_verror(td, icd.error);
329 put_io_u(td, v_io_u);
330 v_io_u = NULL;
331 break;
332 }
333
Jens Axboeebac4652005-12-08 15:25:21 +0100334 /*
335 * if we can't submit more io, we need to verify now
336 */
337 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
338 break;
339
340 } while (1);
341
342 do_io_u_verify(td, &v_io_u);
343
344 if (td->cur_depth)
345 cleanup_pending_aio(td);
346
347 td_set_runstate(td, TD_RUNNING);
348}
349
Jens Axboe32cd46a2006-06-07 13:40:40 +0200350/*
Jens Axboeb990b5c2006-09-14 09:48:22 +0200351 * Not really an io thread, all it does is burn CPU cycles in the specified
352 * manner.
353 */
354static void do_cpuio(struct thread_data *td)
355{
356 struct timeval e;
357 int split = 100 / td->cpuload;
358 int i = 0;
359
360 while (!td->terminate) {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100361 fio_gettime(&e, NULL);
Jens Axboeb990b5c2006-09-14 09:48:22 +0200362
363 if (runtime_exceeded(td, &e))
364 break;
365
366 if (!(i % split))
367 __usec_sleep(10000);
368 else
369 usec_sleep(td, 10000);
370
371 i++;
372 }
373}
374
375/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200376 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200377 * and reaps them, checking for rate and errors along the way.
378 */
Jens Axboeebac4652005-12-08 15:25:21 +0100379static void do_io(struct thread_data *td)
380{
381 struct io_completion_data icd;
Jens Axboe02bcaa82006-11-24 10:42:00 +0100382 struct timeval s;
Jens Axboeebac4652005-12-08 15:25:21 +0100383 unsigned long usec;
Jens Axboe53cdc682006-10-18 11:50:58 +0200384 struct fio_file *f;
Jens Axboe84585002006-10-19 20:26:22 +0200385 int i, ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100386
Jens Axboe5853e5a2006-06-08 14:41:05 +0200387 td_set_runstate(td, TD_RUNNING);
388
Jens Axboeebac4652005-12-08 15:25:21 +0100389 while (td->this_io_bytes[td->ddir] < td->io_size) {
Jens Axboeebac4652005-12-08 15:25:21 +0100390 struct timespec *timeout;
Jens Axboe84585002006-10-19 20:26:22 +0200391 int min_evts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100392 struct io_u *io_u;
393
394 if (td->terminate)
395 break;
396
Jens Axboe53cdc682006-10-18 11:50:58 +0200397 f = get_next_file(td);
398 if (!f)
399 break;
400
401 io_u = get_io_u(td, f);
Jens Axboeebac4652005-12-08 15:25:21 +0100402 if (!io_u)
403 break;
404
405 memcpy(&s, &io_u->start_time, sizeof(s));
406
Jens Axboe45bee282006-10-18 15:26:44 +0200407 ret = td_io_queue(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100408 if (ret) {
Jens Axboe353a7e02006-10-25 09:16:07 +0200409 td_verror(td, io_u->error);
Jens Axboeebac4652005-12-08 15:25:21 +0100410 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100411 break;
412 }
413
414 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
415
416 if (td->cur_depth < td->iodepth) {
Jens Axboe8d7ad8d2007-01-09 21:22:18 +0100417 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
418
Jens Axboeebac4652005-12-08 15:25:21 +0100419 timeout = &ts;
420 min_evts = 0;
421 } else {
422 timeout = NULL;
423 min_evts = 1;
424 }
425
Jens Axboe45bee282006-10-18 15:26:44 +0200426 ret = td_io_getevents(td, min_evts, td->cur_depth, timeout);
Jens Axboeebac4652005-12-08 15:25:21 +0100427 if (ret < 0) {
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200428 td_verror(td, ret);
Jens Axboeebac4652005-12-08 15:25:21 +0100429 break;
430 } else if (!ret)
431 continue;
432
433 icd.nr = ret;
434 ios_completed(td, &icd);
435 if (icd.error) {
436 td_verror(td, icd.error);
437 break;
438 }
439
440 /*
441 * the rate is batched for now, it should work for batches
442 * of completions except the very first one which may look
443 * a little bursty
444 */
Jens Axboe02bcaa82006-11-24 10:42:00 +0100445 usec = utime_since(&s, &icd.time);
Jens Axboeebac4652005-12-08 15:25:21 +0100446
Jens Axboea00735e2006-11-03 08:58:08 +0100447 rate_throttle(td, usec, icd.bytes_done[td->ddir], td->ddir);
Jens Axboeebac4652005-12-08 15:25:21 +0100448
Jens Axboe02bcaa82006-11-24 10:42:00 +0100449 if (check_min_rate(td, &icd.time)) {
Jens Axboe98aa62d2006-11-07 14:16:19 +0100450 if (exitall_on_terminate)
Jens Axboe6ce15a32007-01-12 09:04:41 +0100451 terminate_threads(td->groupid, 0);
Jens Axboefd841462007-01-13 12:20:30 +0100452 td_verror(td, ENODATA);
Jens Axboeebac4652005-12-08 15:25:21 +0100453 break;
454 }
455
Jens Axboe02bcaa82006-11-24 10:42:00 +0100456 if (runtime_exceeded(td, &icd.time))
Jens Axboeebac4652005-12-08 15:25:21 +0100457 break;
458
Jens Axboe9c1f7432007-01-03 20:43:19 +0100459 if (td->thinktime) {
460 unsigned long long b;
461
462 b = td->io_blocks[0] + td->io_blocks[1];
Jens Axboe25309a32007-01-10 11:33:14 +0100463 if (!(b % td->thinktime_blocks))
Jens Axboe9c1f7432007-01-03 20:43:19 +0100464 usec_sleep(td, td->thinktime);
465 }
Jens Axboeebac4652005-12-08 15:25:21 +0100466 }
467
Jens Axboe4d2413c2006-11-23 11:58:59 +0100468 if (!td->error) {
Jens Axboe84585002006-10-19 20:26:22 +0200469 if (td->cur_depth)
470 cleanup_pending_aio(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100471
Jens Axboe84585002006-10-19 20:26:22 +0200472 if (should_fsync(td) && td->end_fsync) {
473 td_set_runstate(td, TD_FSYNCING);
474 for_each_file(td, f, i)
Jens Axboe858a3d42006-10-24 14:54:44 +0200475 fio_io_sync(td, f);
Jens Axboe84585002006-10-19 20:26:22 +0200476 }
Jens Axboe5853e5a2006-06-08 14:41:05 +0200477 }
Jens Axboeebac4652005-12-08 15:25:21 +0100478}
479
Jens Axboeebac4652005-12-08 15:25:21 +0100480static void cleanup_io_u(struct thread_data *td)
481{
482 struct list_head *entry, *n;
483 struct io_u *io_u;
484
485 list_for_each_safe(entry, n, &td->io_u_freelist) {
486 io_u = list_entry(entry, struct io_u, list);
487
488 list_del(&io_u->list);
489 free(io_u);
490 }
491
Jens Axboe2f9ade32006-10-20 11:25:52 +0200492 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100493}
494
Jens Axboe6b9cea22006-10-30 17:00:24 +0100495/*
496 * "randomly" fill the buffer contents
497 */
Jens Axboe66eeb292006-11-01 08:35:44 +0100498static void fill_rand_buf(struct io_u *io_u, int max_bs)
Jens Axboe6b9cea22006-10-30 17:00:24 +0100499{
Jens Axboe66eeb292006-11-01 08:35:44 +0100500 int *ptr = io_u->buf;
Jens Axboe6b9cea22006-10-30 17:00:24 +0100501
502 while ((void *) ptr - io_u->buf < max_bs) {
503 *ptr = rand() * 0x9e370001;
504 ptr++;
505 }
506}
507
Jens Axboeebac4652005-12-08 15:25:21 +0100508static int init_io_u(struct thread_data *td)
509{
510 struct io_u *io_u;
Jens Axboea00735e2006-11-03 08:58:08 +0100511 unsigned int max_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100512 int i, max_units;
513 char *p;
514
Jens Axboe2866c822006-10-09 15:57:48 +0200515 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200516 return 0;
517
Jens Axboe2866c822006-10-09 15:57:48 +0200518 if (td->io_ops->flags & FIO_SYNCIO)
Jens Axboeebac4652005-12-08 15:25:21 +0100519 max_units = 1;
520 else
521 max_units = td->iodepth;
522
Jens Axboea00735e2006-11-03 08:58:08 +0100523 max_bs = max(td->max_bs[DDIR_READ], td->max_bs[DDIR_WRITE]);
Jens Axboe74b025b2006-12-19 15:18:14 +0100524 td->orig_buffer_size = max_bs * max_units;
525
Jens Axboed0bdaf42006-12-20 14:40:44 +0100526 if (td->mem_type == MEM_SHMHUGE || td->mem_type == MEM_MMAPHUGE)
Jens Axboe56bb17f2006-12-20 20:27:36 +0100527 td->orig_buffer_size = (td->orig_buffer_size + td->hugepage_size - 1) & ~(td->hugepage_size - 1);
Jens Axboe74b025b2006-12-19 15:18:14 +0100528 else
529 td->orig_buffer_size += MASK;
Jens Axboeebac4652005-12-08 15:25:21 +0100530
Jens Axboe2f9ade32006-10-20 11:25:52 +0200531 if (allocate_io_mem(td))
532 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100533
Jens Axboeebac4652005-12-08 15:25:21 +0100534 p = ALIGN(td->orig_buffer);
535 for (i = 0; i < max_units; i++) {
536 io_u = malloc(sizeof(*io_u));
537 memset(io_u, 0, sizeof(*io_u));
538 INIT_LIST_HEAD(&io_u->list);
539
Jens Axboea00735e2006-11-03 08:58:08 +0100540 io_u->buf = p + max_bs * i;
Jens Axboe6b9cea22006-10-30 17:00:24 +0100541 if (td_write(td) || td_rw(td))
Jens Axboea00735e2006-11-03 08:58:08 +0100542 fill_rand_buf(io_u, max_bs);
Jens Axboe6b9cea22006-10-30 17:00:24 +0100543
Jens Axboeb1ff3402006-02-17 11:35:58 +0100544 io_u->index = i;
Jens Axboeebac4652005-12-08 15:25:21 +0100545 list_add(&io_u->list, &td->io_u_freelist);
546 }
547
548 return 0;
549}
550
Jens Axboeda867742006-06-06 10:39:10 -0700551static int switch_ioscheduler(struct thread_data *td)
552{
553 char tmp[256], tmp2[128];
554 FILE *f;
555 int ret;
556
Jens Axboef48b4672006-10-27 11:30:07 +0200557 if (td->io_ops->flags & FIO_CPUIO)
558 return 0;
559
Jens Axboeda867742006-06-06 10:39:10 -0700560 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
561
562 f = fopen(tmp, "r+");
563 if (!f) {
564 td_verror(td, errno);
565 return 1;
566 }
567
568 /*
569 * Set io scheduler.
570 */
571 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
572 if (ferror(f) || ret != 1) {
573 td_verror(td, errno);
574 fclose(f);
575 return 1;
576 }
577
578 rewind(f);
579
580 /*
581 * Read back and check that the selected scheduler is now the default.
582 */
583 ret = fread(tmp, 1, sizeof(tmp), f);
584 if (ferror(f) || ret < 0) {
585 td_verror(td, errno);
586 fclose(f);
587 return 1;
588 }
589
590 sprintf(tmp2, "[%s]", td->ioscheduler);
591 if (!strstr(tmp, tmp2)) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200592 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700593 td_verror(td, EINVAL);
594 fclose(f);
595 return 1;
596 }
597
598 fclose(f);
599 return 0;
600}
601
Jens Axboeebac4652005-12-08 15:25:21 +0100602static void clear_io_state(struct thread_data *td)
603{
Jens Axboe53cdc682006-10-18 11:50:58 +0200604 struct fio_file *f;
605 int i;
Jens Axboeebac4652005-12-08 15:25:21 +0100606
Jens Axboeebac4652005-12-08 15:25:21 +0100607 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
608 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100609 td->zone_bytes = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100610
Jens Axboe53cdc682006-10-18 11:50:58 +0200611 for_each_file(td, f, i) {
612 f->last_pos = 0;
613 if (td->io_ops->flags & FIO_SYNCIO)
614 lseek(f->fd, SEEK_SET, 0);
615
616 if (f->file_map)
617 memset(f->file_map, 0, f->num_maps * sizeof(long));
618 }
Jens Axboeebac4652005-12-08 15:25:21 +0100619}
620
Jens Axboe906c8d72006-06-13 09:37:56 +0200621/*
622 * Entry point for the thread based jobs. The process based jobs end up
623 * here as well, after a little setup.
624 */
Jens Axboeebac4652005-12-08 15:25:21 +0100625static void *thread_main(void *data)
626{
Jens Axboe69008992006-11-24 13:12:56 +0100627 unsigned long long runtime[2];
Jens Axboeebac4652005-12-08 15:25:21 +0100628 struct thread_data *td = data;
Jens Axboeebac4652005-12-08 15:25:21 +0100629
630 if (!td->use_thread)
631 setsid();
632
633 td->pid = getpid();
634
Jens Axboeaea47d42006-05-26 19:27:29 +0200635 INIT_LIST_HEAD(&td->io_u_freelist);
636 INIT_LIST_HEAD(&td->io_u_busylist);
637 INIT_LIST_HEAD(&td->io_hist_list);
638 INIT_LIST_HEAD(&td->io_log_list);
639
Jens Axboeebac4652005-12-08 15:25:21 +0100640 if (init_io_u(td))
641 goto err;
642
643 if (fio_setaffinity(td) == -1) {
644 td_verror(td, errno);
645 goto err;
646 }
647
Jens Axboe45bee282006-10-18 15:26:44 +0200648 if (td_io_init(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100649 goto err;
650
Jens Axboeaea47d42006-05-26 19:27:29 +0200651 if (init_iolog(td))
652 goto err;
653
Jens Axboeebac4652005-12-08 15:25:21 +0100654 if (td->ioprio) {
655 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
656 td_verror(td, errno);
657 goto err;
658 }
659 }
660
Jens Axboe1056eaa2006-07-13 10:33:45 -0700661 if (nice(td->nice) == -1) {
Jens Axboeb6f4d882006-06-02 10:32:51 +0200662 td_verror(td, errno);
663 goto err;
664 }
665
Jens Axboe75154842006-06-01 13:56:09 +0200666 if (init_random_state(td))
667 goto err;
668
Jens Axboe56f94982006-10-24 09:33:42 +0200669 if (td->ioscheduler && switch_ioscheduler(td))
670 goto err;
Jens Axboeda867742006-06-06 10:39:10 -0700671
Jens Axboe75154842006-06-01 13:56:09 +0200672 td_set_runstate(td, TD_INITIALIZED);
Jens Axboebbfd6b02006-06-07 19:42:54 +0200673 fio_sem_up(&startup_sem);
674 fio_sem_down(&td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100675
Jens Axboe53cdc682006-10-18 11:50:58 +0200676 if (!td->create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100677 goto err;
Jens Axboe21972cd2006-11-23 12:39:16 +0100678 if (open_files(td))
679 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +0100680
Jens Axboe56f94982006-10-24 09:33:42 +0200681 if (td->exec_prerun)
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200682 system(td->exec_prerun);
683
Jens Axboe69008992006-11-24 13:12:56 +0100684 fio_gettime(&td->epoch, NULL);
Jens Axboe36dff962006-11-24 13:29:20 +0100685 getrusage(RUSAGE_SELF, &td->ru_start);
Jens Axboe69008992006-11-24 13:12:56 +0100686
687 runtime[0] = runtime[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100688 while (td->loops--) {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100689 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100690 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
691
692 if (td->ratemin)
693 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
694
695 clear_io_state(td);
696 prune_io_piece_log(td);
697
Jens Axboe2866c822006-10-09 15:57:48 +0200698 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200699 do_cpuio(td);
700 else
701 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100702
Jens Axboe69008992006-11-24 13:12:56 +0100703 runtime[td->ddir] += utime_since_now(&td->start);
Jens Axboeaea47d42006-05-26 19:27:29 +0200704 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
Jens Axboe69008992006-11-24 13:12:56 +0100705 runtime[td->ddir ^ 1] = runtime[td->ddir];
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200706
Jens Axboeebac4652005-12-08 15:25:21 +0100707 if (td->error || td->terminate)
708 break;
709
710 if (td->verify == VERIFY_NONE)
711 continue;
712
713 clear_io_state(td);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100714 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100715
716 do_verify(td);
717
Jens Axboe69008992006-11-24 13:12:56 +0100718 runtime[DDIR_READ] += utime_since_now(&td->start);
Jens Axboeebac4652005-12-08 15:25:21 +0100719
720 if (td->error || td->terminate)
721 break;
722 }
723
Jens Axboe36dff962006-11-24 13:29:20 +0100724 update_rusage_stat(td);
Jens Axboe69008992006-11-24 13:12:56 +0100725 fio_gettime(&td->end_time, NULL);
726 td->runtime[0] = runtime[0] / 1000;
727 td->runtime[1] = runtime[1] / 1000;
728
Jens Axboeebac4652005-12-08 15:25:21 +0100729 if (td->bw_log)
730 finish_log(td, td->bw_log, "bw");
731 if (td->slat_log)
732 finish_log(td, td->slat_log, "slat");
733 if (td->clat_log)
734 finish_log(td, td->clat_log, "clat");
Jens Axboe076efc72006-10-27 11:24:25 +0200735 if (td->write_iolog_file)
Jens Axboe843a7412006-06-01 21:14:21 -0700736 write_iolog_close(td);
Jens Axboe56f94982006-10-24 09:33:42 +0200737 if (td->exec_postrun)
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200738 system(td->exec_postrun);
Jens Axboeebac4652005-12-08 15:25:21 +0100739
740 if (exitall_on_terminate)
Jens Axboe6ce15a32007-01-12 09:04:41 +0100741 terminate_threads(td->groupid, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100742
743err:
Jens Axboe53cdc682006-10-18 11:50:58 +0200744 close_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +0200745 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100746 cleanup_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100747 td_set_runstate(td, TD_EXITED);
748 return NULL;
749
750}
751
Jens Axboe906c8d72006-06-13 09:37:56 +0200752/*
753 * We cannot pass the td data into a forked process, so attach the td and
754 * pass it to the thread worker.
755 */
Jens Axboeebac4652005-12-08 15:25:21 +0100756static void *fork_main(int shmid, int offset)
757{
758 struct thread_data *td;
759 void *data;
760
761 data = shmat(shmid, NULL, 0);
762 if (data == (void *) -1) {
763 perror("shmat");
764 return NULL;
765 }
766
767 td = data + offset * sizeof(struct thread_data);
768 thread_main(td);
769 shmdt(data);
770 return NULL;
771}
772
Jens Axboe906c8d72006-06-13 09:37:56 +0200773/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200774 * Run over the job map and reap the threads that have exited, if any.
775 */
Jens Axboeebac4652005-12-08 15:25:21 +0100776static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
777{
Jens Axboe34572e22006-10-20 09:33:18 +0200778 struct thread_data *td;
Jens Axboe4d2413c2006-11-23 11:58:59 +0100779 int i, cputhreads, pending;
Jens Axboeebac4652005-12-08 15:25:21 +0100780
781 /*
782 * reap exited threads (TD_EXITED -> TD_REAPED)
783 */
Jens Axboe4d2413c2006-11-23 11:58:59 +0100784 pending = cputhreads = 0;
Jens Axboe34572e22006-10-20 09:33:18 +0200785 for_each_td(td, i) {
Jens Axboe84585002006-10-19 20:26:22 +0200786 /*
787 * ->io_ops is NULL for a thread that has closed its
788 * io engine
789 */
790 if (td->io_ops && td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200791 cputhreads++;
792
Jens Axboe4d2413c2006-11-23 11:58:59 +0100793 if (td->runstate != TD_EXITED) {
794 if (td->runstate < TD_RUNNING)
795 pending++;
796
Jens Axboeebac4652005-12-08 15:25:21 +0100797 continue;
Jens Axboe4d2413c2006-11-23 11:58:59 +0100798 }
Jens Axboeebac4652005-12-08 15:25:21 +0100799
800 td_set_runstate(td, TD_REAPED);
801
802 if (td->use_thread) {
803 long ret;
804
805 if (pthread_join(td->thread, (void *) &ret))
806 perror("thread_join");
807 } else
808 waitpid(td->pid, NULL, 0);
809
810 (*nr_running)--;
811 (*m_rate) -= td->ratemin;
812 (*t_rate) -= td->rate;
813 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200814
Jens Axboe4d2413c2006-11-23 11:58:59 +0100815 if (*nr_running == cputhreads && !pending)
Jens Axboe6ce15a32007-01-12 09:04:41 +0100816 terminate_threads(TERMINATE_ALL, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100817}
818
Jens Axboe906c8d72006-06-13 09:37:56 +0200819/*
820 * Main function for kicking off and reaping jobs, as needed.
821 */
Jens Axboeebac4652005-12-08 15:25:21 +0100822static void run_threads(void)
823{
Jens Axboeebac4652005-12-08 15:25:21 +0100824 struct thread_data *td;
825 unsigned long spent;
826 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +0200827
Jens Axboe2f9ade32006-10-20 11:25:52 +0200828 if (fio_pin_memory())
829 return;
Jens Axboeebac4652005-12-08 15:25:21 +0100830
Jens Axboec6ae0a52006-06-12 11:04:44 +0200831 if (!terse_output) {
832 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
833 fflush(stdout);
834 }
Jens Axboec04f7ec2006-05-31 10:13:16 +0200835
Jens Axboe4efa9702006-05-31 11:56:49 +0200836 signal(SIGINT, sig_handler);
837 signal(SIGALRM, sig_handler);
Jens Axboe6ce15a32007-01-12 09:04:41 +0100838 signal(SIGSEGV, sig_handler);
Jens Axboe4efa9702006-05-31 11:56:49 +0200839
Jens Axboeebac4652005-12-08 15:25:21 +0100840 todo = thread_number;
841 nr_running = 0;
842 nr_started = 0;
843 m_rate = t_rate = 0;
844
Jens Axboe34572e22006-10-20 09:33:18 +0200845 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +0200846 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +0100847
Jens Axboe380cf262007-01-11 14:01:27 +0100848 if (!td->create_serialize) {
849 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100850 continue;
Jens Axboe380cf262007-01-11 14:01:27 +0100851 }
Jens Axboeebac4652005-12-08 15:25:21 +0100852
853 /*
854 * do file setup here so it happens sequentially,
855 * we don't want X number of threads getting their
856 * client data interspersed on disk
857 */
Jens Axboe53cdc682006-10-18 11:50:58 +0200858 if (setup_files(td)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100859 td_set_runstate(td, TD_REAPED);
860 todo--;
861 }
Jens Axboe380cf262007-01-11 14:01:27 +0100862
863 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100864 }
865
Jens Axboeebac4652005-12-08 15:25:21 +0100866 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +0200867 struct thread_data *map[MAX_JOBS];
868 struct timeval this_start;
869 int this_jobs = 0, left;
870
Jens Axboeebac4652005-12-08 15:25:21 +0100871 /*
872 * create threads (TD_NOT_CREATED -> TD_CREATED)
873 */
Jens Axboe34572e22006-10-20 09:33:18 +0200874 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +0100875 if (td->runstate != TD_NOT_CREATED)
876 continue;
877
878 /*
879 * never got a chance to start, killed by other
880 * thread for some reason
881 */
882 if (td->terminate) {
883 todo--;
884 continue;
885 }
886
887 if (td->start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +0200888 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +0100889
890 if (td->start_delay * 1000 > spent)
891 continue;
892 }
893
894 if (td->stonewall && (nr_started || nr_running))
895 break;
896
Jens Axboe75154842006-06-01 13:56:09 +0200897 /*
898 * Set state to created. Thread will transition
899 * to TD_INITIALIZED when it's done setting up.
900 */
Jens Axboeebac4652005-12-08 15:25:21 +0100901 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +0200902 map[this_jobs++] = td;
Jens Axboebbfd6b02006-06-07 19:42:54 +0200903 fio_sem_init(&startup_sem, 1);
Jens Axboeebac4652005-12-08 15:25:21 +0100904 nr_started++;
905
906 if (td->use_thread) {
907 if (pthread_create(&td->thread, NULL, thread_main, td)) {
908 perror("thread_create");
909 nr_started--;
910 }
911 } else {
912 if (fork())
Jens Axboebbfd6b02006-06-07 19:42:54 +0200913 fio_sem_down(&startup_sem);
Jens Axboeebac4652005-12-08 15:25:21 +0100914 else {
915 fork_main(shm_id, i);
916 exit(0);
917 }
918 }
919 }
920
921 /*
Jens Axboe75154842006-06-01 13:56:09 +0200922 * Wait for the started threads to transition to
923 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +0100924 */
Jens Axboe02bcaa82006-11-24 10:42:00 +0100925 fio_gettime(&this_start, NULL);
Jens Axboe75154842006-06-01 13:56:09 +0200926 left = this_jobs;
Jens Axboe6ce15a32007-01-12 09:04:41 +0100927 while (left && !fio_abort) {
Jens Axboe75154842006-06-01 13:56:09 +0200928 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
929 break;
930
931 usleep(100000);
932
933 for (i = 0; i < this_jobs; i++) {
934 td = map[i];
935 if (!td)
936 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +0200937 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +0200938 map[i] = NULL;
939 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +0200940 } else if (td->runstate >= TD_EXITED) {
941 map[i] = NULL;
942 left--;
943 todo--;
944 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +0200945 }
946 }
947 }
948
949 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200950 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +0200951 for (i = 0; i < this_jobs; i++) {
952 td = map[i];
953 if (!td)
954 continue;
955 kill(td->pid, SIGTERM);
956 }
957 break;
958 }
959
960 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +0200961 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +0200962 */
Jens Axboe34572e22006-10-20 09:33:18 +0200963 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +0200964 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +0100965 continue;
966
967 td_set_runstate(td, TD_RUNNING);
968 nr_running++;
969 nr_started--;
970 m_rate += td->ratemin;
971 t_rate += td->rate;
Jens Axboe75154842006-06-01 13:56:09 +0200972 todo--;
Jens Axboebbfd6b02006-06-07 19:42:54 +0200973 fio_sem_up(&td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100974 }
975
976 reap_threads(&nr_running, &t_rate, &m_rate);
977
978 if (todo)
979 usleep(100000);
980 }
981
982 while (nr_running) {
983 reap_threads(&nr_running, &t_rate, &m_rate);
984 usleep(10000);
985 }
986
987 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +0200988 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +0100989}
990
Jens Axboeebac4652005-12-08 15:25:21 +0100991int main(int argc, char *argv[])
992{
993 if (parse_options(argc, argv))
994 return 1;
995
996 if (!thread_number) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200997 log_err("Nothing to do\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100998 return 1;
999 }
1000
1001 disk_util_timer_arm();
1002
1003 run_threads();
Jens Axboe6ce15a32007-01-12 09:04:41 +01001004
1005 if (!fio_abort)
1006 show_run_stats();
Jens Axboeebac4652005-12-08 15:25:21 +01001007
1008 return 0;
1009}