blob: 2b760ddd4beb0c56244637a626284b6faa47d9f8 [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 Axboec04f7ec2006-05-31 10:13:16 +020048extern unsigned long long mlock_size;
49
Jens Axboe3d60d1e2006-05-25 06:31:06 +020050#define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync))
Jens Axboeebac4652005-12-08 15:25:21 +010051
Jens Axboebbfd6b02006-06-07 19:42:54 +020052static volatile int startup_sem;
Jens Axboeebac4652005-12-08 15:25:21 +010053
54#define TERMINATE_ALL (-1)
Jens Axboe75154842006-06-01 13:56:09 +020055#define JOB_START_TIMEOUT (5 * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +010056
57static void terminate_threads(int group_id)
58{
59 int i;
60
61 for (i = 0; i < thread_number; i++) {
62 struct thread_data *td = &threads[i];
63
64 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
65 td->terminate = 1;
66 td->start_delay = 0;
67 }
68 }
69}
70
71static void sig_handler(int sig)
72{
73 switch (sig) {
74 case SIGALRM:
75 update_io_ticks();
76 disk_util_timer_arm();
77 print_thread_status();
78 break;
79 default:
80 printf("\nfio: terminating on signal\n");
81 fflush(stdout);
82 terminate_threads(TERMINATE_ALL);
83 break;
84 }
85}
86
Jens Axboe906c8d72006-06-13 09:37:56 +020087/*
88 * The ->file_map[] contains a map of blocks we have or have not done io
89 * to yet. Used to make sure we cover the entire range in a fair fashion.
90 */
Jens Axboe53cdc682006-10-18 11:50:58 +020091static int random_map_free(struct thread_data *td, struct fio_file *f,
92 unsigned long long block)
Jens Axboeebac4652005-12-08 15:25:21 +010093{
Jens Axboe53cdc682006-10-18 11:50:58 +020094 unsigned int idx = RAND_MAP_IDX(td, f, block);
95 unsigned int bit = RAND_MAP_BIT(td, f, block);
Jens Axboeebac4652005-12-08 15:25:21 +010096
Jens Axboe53cdc682006-10-18 11:50:58 +020097 return (f->file_map[idx] & (1UL << bit)) == 0;
Jens Axboeebac4652005-12-08 15:25:21 +010098}
99
Jens Axboe906c8d72006-06-13 09:37:56 +0200100/*
101 * Return the next free block in the map.
102 */
Jens Axboe53cdc682006-10-18 11:50:58 +0200103static int get_next_free_block(struct thread_data *td, struct fio_file *f,
104 unsigned long long *b)
Jens Axboeebac4652005-12-08 15:25:21 +0100105{
106 int i;
107
108 *b = 0;
109 i = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200110 while ((*b) * td->min_bs < f->file_size) {
111 if (f->file_map[i] != -1UL) {
112 *b += ffz(f->file_map[i]);
Jens Axboeebac4652005-12-08 15:25:21 +0100113 return 0;
114 }
115
116 *b += BLOCKS_PER_MAP;
117 i++;
118 }
119
120 return 1;
121}
122
Jens Axboe906c8d72006-06-13 09:37:56 +0200123/*
124 * Mark a given offset as used in the map.
125 */
Jens Axboe53cdc682006-10-18 11:50:58 +0200126static void mark_random_map(struct thread_data *td, struct fio_file *f,
127 struct io_u *io_u)
Jens Axboeebac4652005-12-08 15:25:21 +0100128{
Jens Axboe200bc852006-06-01 16:10:12 +0200129 unsigned long long block = io_u->offset / (unsigned long long) td->min_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100130 unsigned int blocks = 0;
131
132 while (blocks < (io_u->buflen / td->min_bs)) {
133 unsigned int idx, bit;
134
Jens Axboe53cdc682006-10-18 11:50:58 +0200135 if (!random_map_free(td, f, block))
Jens Axboeebac4652005-12-08 15:25:21 +0100136 break;
137
Jens Axboe53cdc682006-10-18 11:50:58 +0200138 idx = RAND_MAP_IDX(td, f, block);
139 bit = RAND_MAP_BIT(td, f, block);
Jens Axboeebac4652005-12-08 15:25:21 +0100140
Jens Axboe53cdc682006-10-18 11:50:58 +0200141 assert(idx < f->num_maps);
Jens Axboeebac4652005-12-08 15:25:21 +0100142
Jens Axboe53cdc682006-10-18 11:50:58 +0200143 f->file_map[idx] |= (1UL << bit);
Jens Axboeebac4652005-12-08 15:25:21 +0100144 block++;
145 blocks++;
146 }
147
148 if ((blocks * td->min_bs) < io_u->buflen)
149 io_u->buflen = blocks * td->min_bs;
150}
151
Jens Axboe906c8d72006-06-13 09:37:56 +0200152/*
153 * For random io, generate a random new block and see if it's used. Repeat
154 * until we find a free one. For sequential io, just return the end of
155 * the last io issued.
156 */
Jens Axboe53cdc682006-10-18 11:50:58 +0200157static int get_next_offset(struct thread_data *td, struct fio_file *f,
158 unsigned long long *offset)
Jens Axboe20dc95c2005-12-09 10:29:35 +0100159{
160 unsigned long long b, rb;
161 long r;
162
163 if (!td->sequential) {
Jens Axboe085227a2006-06-01 15:54:09 -0700164 unsigned long long max_blocks = td->io_size / td->min_bs;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100165 int loops = 50;
166
167 do {
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200168 r = os_random_long(&td->random_state);
Jens Axboe085227a2006-06-01 15:54:09 -0700169 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
Jens Axboe53cdc682006-10-18 11:50:58 +0200170 rb = b + (f->file_offset / td->min_bs);
Jens Axboe20dc95c2005-12-09 10:29:35 +0100171 loops--;
Jens Axboe53cdc682006-10-18 11:50:58 +0200172 } while (!random_map_free(td, f, rb) && loops);
Jens Axboe20dc95c2005-12-09 10:29:35 +0100173
174 if (!loops) {
Jens Axboe53cdc682006-10-18 11:50:58 +0200175 if (get_next_free_block(td, f, &b))
Jens Axboe20dc95c2005-12-09 10:29:35 +0100176 return 1;
177 }
178 } else
Jens Axboe53cdc682006-10-18 11:50:58 +0200179 b = f->last_pos / td->min_bs;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100180
Jens Axboe53cdc682006-10-18 11:50:58 +0200181 *offset = (b * td->min_bs) + f->file_offset;
182 if (*offset > f->file_size)
Jens Axboe20dc95c2005-12-09 10:29:35 +0100183 return 1;
184
185 return 0;
186}
187
188static unsigned int get_next_buflen(struct thread_data *td)
189{
190 unsigned int buflen;
191 long r;
192
193 if (td->min_bs == td->max_bs)
194 buflen = td->min_bs;
195 else {
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200196 r = os_random_long(&td->bsrange_state);
Jens Axboe20dc95c2005-12-09 10:29:35 +0100197 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
198 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
199 }
200
Jens Axboeb2a15192006-10-18 14:10:42 +0200201 if (buflen > td->io_size - td->this_io_bytes[td->ddir]) {
202 /*
203 * if using direct/raw io, we may not be able to
204 * shrink the size. so just fail it.
205 */
206 if (td->io_ops->flags & FIO_RAWIO)
207 return 0;
208
Jens Axboe20dc95c2005-12-09 10:29:35 +0100209 buflen = td->io_size - td->this_io_bytes[td->ddir];
Jens Axboeb2a15192006-10-18 14:10:42 +0200210 }
Jens Axboe20dc95c2005-12-09 10:29:35 +0100211
212 return buflen;
213}
214
Jens Axboe906c8d72006-06-13 09:37:56 +0200215/*
216 * Check if we are above the minimum rate given.
217 */
Jens Axboeebac4652005-12-08 15:25:21 +0100218static int check_min_rate(struct thread_data *td, struct timeval *now)
219{
220 unsigned long spent;
221 unsigned long rate;
222 int ddir = td->ddir;
223
224 /*
225 * allow a 2 second settle period in the beginning
226 */
227 if (mtime_since(&td->start, now) < 2000)
228 return 0;
229
230 /*
231 * if rate blocks is set, sample is running
232 */
233 if (td->rate_bytes) {
234 spent = mtime_since(&td->lastrate, now);
235 if (spent < td->ratecycle)
236 return 0;
237
238 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
239 if (rate < td->ratemin) {
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200240 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 +0100241 if (rate_quit)
242 terminate_threads(td->groupid);
243 return 1;
244 }
245 }
246
247 td->rate_bytes = td->this_io_bytes[ddir];
248 memcpy(&td->lastrate, now, sizeof(*now));
249 return 0;
250}
251
252static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
253{
254 if (!td->timeout)
255 return 0;
256 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
257 return 1;
258
259 return 0;
260}
261
Jens Axboe906c8d72006-06-13 09:37:56 +0200262/*
263 * Return the data direction for the next io_u. If the job is a
264 * mixed read/write workload, check the rwmix cycle and switch if
265 * necessary.
266 */
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200267static int get_rw_ddir(struct thread_data *td)
268{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200269 if (td_rw(td)) {
270 struct timeval now;
271 unsigned long elapsed;
272
273 gettimeofday(&now, NULL);
274 elapsed = mtime_since_now(&td->rwmix_switch);
275
276 /*
277 * Check if it's time to seed a new data direction.
278 */
279 if (elapsed >= td->rwmixcycle) {
Jens Axboec1ee2ca2006-06-07 22:42:37 +0200280 int v;
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200281 long r;
282
Jens Axboec1ee2ca2006-06-07 22:42:37 +0200283 r = os_random_long(&td->rwmix_state);
284 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200285 if (v < td->rwmixread)
286 td->rwmix_ddir = DDIR_READ;
287 else
288 td->rwmix_ddir = DDIR_WRITE;
289 memcpy(&td->rwmix_switch, &now, sizeof(now));
290 }
291 return td->rwmix_ddir;
292 } else if (td_read(td))
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200293 return DDIR_READ;
294 else
295 return DDIR_WRITE;
296}
297
Jens Axboeaea47d42006-05-26 19:27:29 +0200298static int td_io_prep(struct thread_data *td, struct io_u *io_u)
Jens Axboe20dc95c2005-12-09 10:29:35 +0100299{
Jens Axboe2866c822006-10-09 15:57:48 +0200300 if (td->io_ops->prep && td->io_ops->prep(td, io_u))
Jens Axboe20dc95c2005-12-09 10:29:35 +0100301 return 1;
302
303 return 0;
304}
305
Jens Axboeb1ff3402006-02-17 11:35:58 +0100306void put_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboeebac4652005-12-08 15:25:21 +0100307{
Jens Axboe53cdc682006-10-18 11:50:58 +0200308 io_u->file = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +0100309 list_del(&io_u->list);
310 list_add(&io_u->list, &td->io_u_freelist);
311 td->cur_depth--;
312}
313
Jens Axboe53cdc682006-10-18 11:50:58 +0200314static int fill_io_u(struct thread_data *td, struct fio_file *f,
315 struct io_u *io_u)
Jens Axboe843a7412006-06-01 21:14:21 -0700316{
317 /*
318 * If using an iolog, grab next piece if any available.
319 */
320 if (td->read_iolog)
321 return read_iolog_get(td, io_u);
322
Jens Axboeaea47d42006-05-26 19:27:29 +0200323 /*
324 * No log, let the seq/rand engine retrieve the next position.
325 */
Jens Axboe53cdc682006-10-18 11:50:58 +0200326 if (!get_next_offset(td, f, &io_u->offset)) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200327 io_u->buflen = get_next_buflen(td);
328
329 if (io_u->buflen) {
330 io_u->ddir = get_rw_ddir(td);
Jens Axboe843a7412006-06-01 21:14:21 -0700331
332 /*
333 * If using a write iolog, store this entry.
334 */
335 if (td->write_iolog)
336 write_iolog_put(td, io_u);
337
Jens Axboe53cdc682006-10-18 11:50:58 +0200338 io_u->file = f;
Jens Axboeaea47d42006-05-26 19:27:29 +0200339 return 0;
340 }
341 }
342
343 return 1;
344}
345
Jens Axboe22f78b32006-06-07 11:30:07 +0200346#define queue_full(td) list_empty(&(td)->io_u_freelist)
Jens Axboeebac4652005-12-08 15:25:21 +0100347
Jens Axboeb1ff3402006-02-17 11:35:58 +0100348struct io_u *__get_io_u(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100349{
Jens Axboe22f78b32006-06-07 11:30:07 +0200350 struct io_u *io_u = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +0100351
Jens Axboe22f78b32006-06-07 11:30:07 +0200352 if (!queue_full(td)) {
353 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100354
Jens Axboe22f78b32006-06-07 11:30:07 +0200355 io_u->error = 0;
356 io_u->resid = 0;
357 list_del(&io_u->list);
358 list_add(&io_u->list, &td->io_u_busylist);
359 td->cur_depth++;
360 }
361
Jens Axboeebac4652005-12-08 15:25:21 +0100362 return io_u;
363}
364
Jens Axboe906c8d72006-06-13 09:37:56 +0200365/*
366 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
367 * etc. The returned io_u is fully ready to be prepped and submitted.
368 */
Jens Axboe53cdc682006-10-18 11:50:58 +0200369static struct io_u *get_io_u(struct thread_data *td, struct fio_file *f)
Jens Axboeebac4652005-12-08 15:25:21 +0100370{
371 struct io_u *io_u;
372
373 io_u = __get_io_u(td);
374 if (!io_u)
375 return NULL;
376
Jens Axboe20dc95c2005-12-09 10:29:35 +0100377 if (td->zone_bytes >= td->zone_size) {
378 td->zone_bytes = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200379 f->last_pos += td->zone_skip;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100380 }
381
Jens Axboe53cdc682006-10-18 11:50:58 +0200382 if (fill_io_u(td, f, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100383 put_io_u(td, io_u);
384 return NULL;
385 }
386
Jens Axboeb2a15192006-10-18 14:10:42 +0200387 if (io_u->buflen + io_u->offset > f->file_size) {
388 if (td->io_ops->flags & FIO_RAWIO) {
389 put_io_u(td, io_u);
390 return NULL;
391 }
392
Jens Axboe53cdc682006-10-18 11:50:58 +0200393 io_u->buflen = f->file_size - io_u->offset;
Jens Axboeb2a15192006-10-18 14:10:42 +0200394 }
Jens Axboeebac4652005-12-08 15:25:21 +0100395
396 if (!io_u->buflen) {
397 put_io_u(td, io_u);
398 return NULL;
399 }
400
Jens Axboe843a7412006-06-01 21:14:21 -0700401 if (!td->read_iolog && !td->sequential)
Jens Axboe53cdc682006-10-18 11:50:58 +0200402 mark_random_map(td, f, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100403
Jens Axboe53cdc682006-10-18 11:50:58 +0200404 f->last_pos += io_u->buflen;
Jens Axboeebac4652005-12-08 15:25:21 +0100405
406 if (td->verify != VERIFY_NONE)
Jens Axboee29d1b72006-10-18 15:43:15 +0200407 populate_verify_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100408
Jens Axboeaea47d42006-05-26 19:27:29 +0200409 if (td_io_prep(td, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100410 put_io_u(td, io_u);
411 return NULL;
412 }
413
414 gettimeofday(&io_u->start_time, NULL);
415 return io_u;
416}
417
418static inline void td_set_runstate(struct thread_data *td, int runstate)
419{
Jens Axboeebac4652005-12-08 15:25:21 +0100420 td->runstate = runstate;
421}
422
Jens Axboe53cdc682006-10-18 11:50:58 +0200423static struct fio_file *get_next_file(struct thread_data *td)
424{
Jens Axboeb2a15192006-10-18 14:10:42 +0200425 int old_next_file = td->next_file;
426 struct fio_file *f;
Jens Axboe53cdc682006-10-18 11:50:58 +0200427
Jens Axboeb2a15192006-10-18 14:10:42 +0200428 do {
429 f = &td->files[td->next_file];
430
431 td->next_file++;
432 if (td->next_file >= td->nr_files)
433 td->next_file = 0;
434
435 if (f->fd != -1)
436 break;
437
438 f = NULL;
439 } while (td->next_file != old_next_file);
Jens Axboe53cdc682006-10-18 11:50:58 +0200440
441 return f;
442}
443
444static int td_io_sync(struct thread_data *td, struct fio_file *f)
Jens Axboeebac4652005-12-08 15:25:21 +0100445{
Jens Axboe2866c822006-10-09 15:57:48 +0200446 if (td->io_ops->sync)
Jens Axboe53cdc682006-10-18 11:50:58 +0200447 return td->io_ops->sync(td, f);
Jens Axboeebac4652005-12-08 15:25:21 +0100448
449 return 0;
450}
451
Jens Axboe45bee282006-10-18 15:26:44 +0200452static int td_io_getevents(struct thread_data *td, int min, int max,
Jens Axboeebac4652005-12-08 15:25:21 +0100453 struct timespec *t)
454{
Jens Axboe2866c822006-10-09 15:57:48 +0200455 return td->io_ops->getevents(td, min, max, t);
Jens Axboeebac4652005-12-08 15:25:21 +0100456}
457
Jens Axboe45bee282006-10-18 15:26:44 +0200458static int td_io_queue(struct thread_data *td, struct io_u *io_u)
Jens Axboeebac4652005-12-08 15:25:21 +0100459{
460 gettimeofday(&io_u->issue_time, NULL);
461
Jens Axboe2866c822006-10-09 15:57:48 +0200462 return td->io_ops->queue(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100463}
464
465#define iocb_time(iocb) ((unsigned long) (iocb)->data)
466
467static void io_completed(struct thread_data *td, struct io_u *io_u,
468 struct io_completion_data *icd)
469{
470 struct timeval e;
471 unsigned long msec;
472
473 gettimeofday(&e, NULL);
474
475 if (!io_u->error) {
Jens Axboe20dc95c2005-12-09 10:29:35 +0100476 unsigned int bytes = io_u->buflen - io_u->resid;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200477 const int idx = io_u->ddir;
Jens Axboeebac4652005-12-08 15:25:21 +0100478
479 td->io_blocks[idx]++;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100480 td->io_bytes[idx] += bytes;
481 td->zone_bytes += bytes;
482 td->this_io_bytes[idx] += bytes;
Jens Axboeebac4652005-12-08 15:25:21 +0100483
484 msec = mtime_since(&io_u->issue_time, &e);
485
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200486 add_clat_sample(td, idx, msec);
487 add_bw_sample(td, idx);
Jens Axboeebac4652005-12-08 15:25:21 +0100488
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200489 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
Jens Axboeebac4652005-12-08 15:25:21 +0100490 log_io_piece(td, io_u);
491
Jens Axboe20dc95c2005-12-09 10:29:35 +0100492 icd->bytes_done[idx] += bytes;
Jens Axboeebac4652005-12-08 15:25:21 +0100493 } else
494 icd->error = io_u->error;
495}
496
497static void ios_completed(struct thread_data *td,struct io_completion_data *icd)
498{
499 struct io_u *io_u;
500 int i;
501
502 icd->error = 0;
503 icd->bytes_done[0] = icd->bytes_done[1] = 0;
504
505 for (i = 0; i < icd->nr; i++) {
Jens Axboe2866c822006-10-09 15:57:48 +0200506 io_u = td->io_ops->event(td, i);
Jens Axboeebac4652005-12-08 15:25:21 +0100507
508 io_completed(td, io_u, icd);
509 put_io_u(td, io_u);
510 }
511}
512
Jens Axboe906c8d72006-06-13 09:37:56 +0200513/*
514 * When job exits, we can cancel the in-flight IO if we are using async
515 * io. Attempt to do so.
516 */
Jens Axboeebac4652005-12-08 15:25:21 +0100517static void cleanup_pending_aio(struct thread_data *td)
518{
519 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
520 struct list_head *entry, *n;
521 struct io_completion_data icd;
522 struct io_u *io_u;
523 int r;
524
525 /*
526 * get immediately available events, if any
527 */
Jens Axboe45bee282006-10-18 15:26:44 +0200528 r = td_io_getevents(td, 0, td->cur_depth, &ts);
Jens Axboeebac4652005-12-08 15:25:21 +0100529 if (r > 0) {
530 icd.nr = r;
531 ios_completed(td, &icd);
532 }
533
534 /*
535 * now cancel remaining active events
536 */
Jens Axboe2866c822006-10-09 15:57:48 +0200537 if (td->io_ops->cancel) {
Jens Axboeebac4652005-12-08 15:25:21 +0100538 list_for_each_safe(entry, n, &td->io_u_busylist) {
539 io_u = list_entry(entry, struct io_u, list);
540
Jens Axboe2866c822006-10-09 15:57:48 +0200541 r = td->io_ops->cancel(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100542 if (!r)
543 put_io_u(td, io_u);
544 }
545 }
546
547 if (td->cur_depth) {
Jens Axboe45bee282006-10-18 15:26:44 +0200548 r = td_io_getevents(td, td->cur_depth, td->cur_depth, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100549 if (r > 0) {
550 icd.nr = r;
551 ios_completed(td, &icd);
552 }
553 }
554}
555
Jens Axboe906c8d72006-06-13 09:37:56 +0200556/*
557 * The main verify engine. Runs over the writes we previusly submitted,
558 * reads the blocks back in, and checks the crc/md5 of the data.
559 */
Jens Axboea9619d42006-10-18 15:47:42 +0200560void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100561{
562 struct timeval t;
563 struct io_u *io_u, *v_io_u = NULL;
564 struct io_completion_data icd;
Jens Axboe53cdc682006-10-18 11:50:58 +0200565 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100566 int ret;
567
568 td_set_runstate(td, TD_VERIFYING);
569
570 do {
571 if (td->terminate)
572 break;
573
574 gettimeofday(&t, NULL);
575 if (runtime_exceeded(td, &t))
576 break;
577
578 io_u = __get_io_u(td);
579 if (!io_u)
580 break;
581
Jens Axboeaea47d42006-05-26 19:27:29 +0200582 if (get_next_verify(td, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100583 put_io_u(td, io_u);
584 break;
585 }
586
Jens Axboe53cdc682006-10-18 11:50:58 +0200587 f = get_next_file(td);
588 if (!f)
589 break;
590
591 io_u->file = f;
592
Jens Axboeaea47d42006-05-26 19:27:29 +0200593 if (td_io_prep(td, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100594 put_io_u(td, io_u);
595 break;
596 }
597
Jens Axboe45bee282006-10-18 15:26:44 +0200598 ret = td_io_queue(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100599 if (ret) {
600 put_io_u(td, io_u);
601 td_verror(td, ret);
602 break;
603 }
604
605 /*
606 * we have one pending to verify, do that while
607 * we are doing io on the next one
608 */
609 if (do_io_u_verify(td, &v_io_u))
610 break;
611
Jens Axboe45bee282006-10-18 15:26:44 +0200612 ret = td_io_getevents(td, 1, 1, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100613 if (ret != 1) {
614 if (ret < 0)
615 td_verror(td, ret);
616 break;
617 }
618
Jens Axboe2866c822006-10-09 15:57:48 +0200619 v_io_u = td->io_ops->event(td, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100620 icd.nr = 1;
621 icd.error = 0;
622 io_completed(td, v_io_u, &icd);
623
624 if (icd.error) {
625 td_verror(td, icd.error);
626 put_io_u(td, v_io_u);
627 v_io_u = NULL;
628 break;
629 }
630
Jens Axboeebac4652005-12-08 15:25:21 +0100631 /*
632 * if we can't submit more io, we need to verify now
633 */
634 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
635 break;
636
637 } while (1);
638
639 do_io_u_verify(td, &v_io_u);
640
641 if (td->cur_depth)
642 cleanup_pending_aio(td);
643
644 td_set_runstate(td, TD_RUNNING);
645}
646
Jens Axboe32cd46a2006-06-07 13:40:40 +0200647/*
Jens Axboeb990b5c2006-09-14 09:48:22 +0200648 * Not really an io thread, all it does is burn CPU cycles in the specified
649 * manner.
650 */
651static void do_cpuio(struct thread_data *td)
652{
653 struct timeval e;
654 int split = 100 / td->cpuload;
655 int i = 0;
656
657 while (!td->terminate) {
658 gettimeofday(&e, NULL);
659
660 if (runtime_exceeded(td, &e))
661 break;
662
663 if (!(i % split))
664 __usec_sleep(10000);
665 else
666 usec_sleep(td, 10000);
667
668 i++;
669 }
670}
671
672/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200673 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200674 * and reaps them, checking for rate and errors along the way.
675 */
Jens Axboeebac4652005-12-08 15:25:21 +0100676static void do_io(struct thread_data *td)
677{
678 struct io_completion_data icd;
679 struct timeval s, e;
680 unsigned long usec;
Jens Axboe53cdc682006-10-18 11:50:58 +0200681 struct fio_file *f;
682 int i;
Jens Axboeebac4652005-12-08 15:25:21 +0100683
Jens Axboe5853e5a2006-06-08 14:41:05 +0200684 td_set_runstate(td, TD_RUNNING);
685
Jens Axboeebac4652005-12-08 15:25:21 +0100686 while (td->this_io_bytes[td->ddir] < td->io_size) {
687 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
688 struct timespec *timeout;
689 int ret, min_evts = 0;
690 struct io_u *io_u;
691
692 if (td->terminate)
693 break;
694
Jens Axboe53cdc682006-10-18 11:50:58 +0200695 f = get_next_file(td);
696 if (!f)
697 break;
698
699 io_u = get_io_u(td, f);
Jens Axboeebac4652005-12-08 15:25:21 +0100700 if (!io_u)
701 break;
702
703 memcpy(&s, &io_u->start_time, sizeof(s));
704
Jens Axboe45bee282006-10-18 15:26:44 +0200705 ret = td_io_queue(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100706 if (ret) {
707 put_io_u(td, io_u);
708 td_verror(td, ret);
709 break;
710 }
711
712 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
713
714 if (td->cur_depth < td->iodepth) {
715 timeout = &ts;
716 min_evts = 0;
717 } else {
718 timeout = NULL;
719 min_evts = 1;
720 }
721
Jens Axboe45bee282006-10-18 15:26:44 +0200722 ret = td_io_getevents(td, min_evts, td->cur_depth, timeout);
Jens Axboeebac4652005-12-08 15:25:21 +0100723 if (ret < 0) {
724 td_verror(td, ret);
725 break;
726 } else if (!ret)
727 continue;
728
729 icd.nr = ret;
730 ios_completed(td, &icd);
731 if (icd.error) {
732 td_verror(td, icd.error);
733 break;
734 }
735
736 /*
737 * the rate is batched for now, it should work for batches
738 * of completions except the very first one which may look
739 * a little bursty
740 */
741 gettimeofday(&e, NULL);
742 usec = utime_since(&s, &e);
743
744 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
745
746 if (check_min_rate(td, &e)) {
747 td_verror(td, ENOMEM);
748 break;
749 }
750
751 if (runtime_exceeded(td, &e))
752 break;
753
754 if (td->thinktime)
755 usec_sleep(td, td->thinktime);
756
757 if (should_fsync(td) && td->fsync_blocks &&
758 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
Jens Axboe53cdc682006-10-18 11:50:58 +0200759 td_io_sync(td, f);
Jens Axboeebac4652005-12-08 15:25:21 +0100760 }
761
762 if (td->cur_depth)
763 cleanup_pending_aio(td);
764
Jens Axboe5853e5a2006-06-08 14:41:05 +0200765 if (should_fsync(td) && td->end_fsync) {
766 td_set_runstate(td, TD_FSYNCING);
Jens Axboe53cdc682006-10-18 11:50:58 +0200767 for_each_file(td, f, i)
768 td_io_sync(td, f);
Jens Axboe5853e5a2006-06-08 14:41:05 +0200769 }
Jens Axboeebac4652005-12-08 15:25:21 +0100770}
771
Jens Axboe45bee282006-10-18 15:26:44 +0200772static int td_io_init(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100773{
Jens Axboe2866c822006-10-09 15:57:48 +0200774 if (td->io_ops->init)
775 return td->io_ops->init(td);
776
777 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100778}
779
780static void cleanup_io_u(struct thread_data *td)
781{
782 struct list_head *entry, *n;
783 struct io_u *io_u;
784
785 list_for_each_safe(entry, n, &td->io_u_freelist) {
786 io_u = list_entry(entry, struct io_u, list);
787
788 list_del(&io_u->list);
789 free(io_u);
790 }
791
792 if (td->mem_type == MEM_MALLOC)
793 free(td->orig_buffer);
794 else if (td->mem_type == MEM_SHM) {
795 struct shmid_ds sbuf;
796
797 shmdt(td->orig_buffer);
798 shmctl(td->shm_id, IPC_RMID, &sbuf);
799 } else if (td->mem_type == MEM_MMAP)
800 munmap(td->orig_buffer, td->orig_buffer_size);
801 else
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200802 log_err("Bad memory type %d\n", td->mem_type);
Jens Axboeebac4652005-12-08 15:25:21 +0100803
804 td->orig_buffer = NULL;
805}
806
807static int init_io_u(struct thread_data *td)
808{
809 struct io_u *io_u;
810 int i, max_units;
811 char *p;
812
Jens Axboe2866c822006-10-09 15:57:48 +0200813 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200814 return 0;
815
Jens Axboe2866c822006-10-09 15:57:48 +0200816 if (td->io_ops->flags & FIO_SYNCIO)
Jens Axboeebac4652005-12-08 15:25:21 +0100817 max_units = 1;
818 else
819 max_units = td->iodepth;
820
821 td->orig_buffer_size = td->max_bs * max_units + MASK;
822
823 if (td->mem_type == MEM_MALLOC)
824 td->orig_buffer = malloc(td->orig_buffer_size);
825 else if (td->mem_type == MEM_SHM) {
826 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
827 if (td->shm_id < 0) {
828 td_verror(td, errno);
829 perror("shmget");
830 return 1;
831 }
832
833 td->orig_buffer = shmat(td->shm_id, NULL, 0);
834 if (td->orig_buffer == (void *) -1) {
835 td_verror(td, errno);
836 perror("shmat");
837 td->orig_buffer = NULL;
838 return 1;
839 }
840 } else if (td->mem_type == MEM_MMAP) {
841 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
842 if (td->orig_buffer == MAP_FAILED) {
843 td_verror(td, errno);
844 perror("mmap");
845 td->orig_buffer = NULL;
846 return 1;
847 }
848 }
849
Jens Axboeebac4652005-12-08 15:25:21 +0100850 p = ALIGN(td->orig_buffer);
851 for (i = 0; i < max_units; i++) {
852 io_u = malloc(sizeof(*io_u));
853 memset(io_u, 0, sizeof(*io_u));
854 INIT_LIST_HEAD(&io_u->list);
855
856 io_u->buf = p + td->max_bs * i;
Jens Axboeb1ff3402006-02-17 11:35:58 +0100857 io_u->index = i;
Jens Axboeebac4652005-12-08 15:25:21 +0100858 list_add(&io_u->list, &td->io_u_freelist);
859 }
860
861 return 0;
862}
863
Jens Axboeda867742006-06-06 10:39:10 -0700864static int switch_ioscheduler(struct thread_data *td)
865{
866 char tmp[256], tmp2[128];
867 FILE *f;
868 int ret;
869
870 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
871
872 f = fopen(tmp, "r+");
873 if (!f) {
874 td_verror(td, errno);
875 return 1;
876 }
877
878 /*
879 * Set io scheduler.
880 */
881 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
882 if (ferror(f) || ret != 1) {
883 td_verror(td, errno);
884 fclose(f);
885 return 1;
886 }
887
888 rewind(f);
889
890 /*
891 * Read back and check that the selected scheduler is now the default.
892 */
893 ret = fread(tmp, 1, sizeof(tmp), f);
894 if (ferror(f) || ret < 0) {
895 td_verror(td, errno);
896 fclose(f);
897 return 1;
898 }
899
900 sprintf(tmp2, "[%s]", td->ioscheduler);
901 if (!strstr(tmp, tmp2)) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200902 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700903 td_verror(td, EINVAL);
904 fclose(f);
905 return 1;
906 }
907
908 fclose(f);
909 return 0;
910}
911
Jens Axboeebac4652005-12-08 15:25:21 +0100912static void clear_io_state(struct thread_data *td)
913{
Jens Axboe53cdc682006-10-18 11:50:58 +0200914 struct fio_file *f;
915 int i;
Jens Axboeebac4652005-12-08 15:25:21 +0100916
Jens Axboeebac4652005-12-08 15:25:21 +0100917 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
918 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100919 td->zone_bytes = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100920
Jens Axboe53cdc682006-10-18 11:50:58 +0200921 for_each_file(td, f, i) {
922 f->last_pos = 0;
923 if (td->io_ops->flags & FIO_SYNCIO)
924 lseek(f->fd, SEEK_SET, 0);
925
926 if (f->file_map)
927 memset(f->file_map, 0, f->num_maps * sizeof(long));
928 }
Jens Axboeebac4652005-12-08 15:25:21 +0100929}
930
Jens Axboe906c8d72006-06-13 09:37:56 +0200931/*
932 * Entry point for the thread based jobs. The process based jobs end up
933 * here as well, after a little setup.
934 */
Jens Axboeebac4652005-12-08 15:25:21 +0100935static void *thread_main(void *data)
936{
937 struct thread_data *td = data;
Jens Axboeebac4652005-12-08 15:25:21 +0100938
939 if (!td->use_thread)
940 setsid();
941
942 td->pid = getpid();
943
Jens Axboeaea47d42006-05-26 19:27:29 +0200944 INIT_LIST_HEAD(&td->io_u_freelist);
945 INIT_LIST_HEAD(&td->io_u_busylist);
946 INIT_LIST_HEAD(&td->io_hist_list);
947 INIT_LIST_HEAD(&td->io_log_list);
948
Jens Axboeebac4652005-12-08 15:25:21 +0100949 if (init_io_u(td))
950 goto err;
951
952 if (fio_setaffinity(td) == -1) {
953 td_verror(td, errno);
954 goto err;
955 }
956
Jens Axboe45bee282006-10-18 15:26:44 +0200957 if (td_io_init(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100958 goto err;
959
Jens Axboeaea47d42006-05-26 19:27:29 +0200960 if (init_iolog(td))
961 goto err;
962
Jens Axboeebac4652005-12-08 15:25:21 +0100963 if (td->ioprio) {
964 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
965 td_verror(td, errno);
966 goto err;
967 }
968 }
969
Jens Axboe1056eaa2006-07-13 10:33:45 -0700970 if (nice(td->nice) == -1) {
Jens Axboeb6f4d882006-06-02 10:32:51 +0200971 td_verror(td, errno);
972 goto err;
973 }
974
Jens Axboe75154842006-06-01 13:56:09 +0200975 if (init_random_state(td))
976 goto err;
977
Jens Axboeda867742006-06-06 10:39:10 -0700978 if (td->ioscheduler && switch_ioscheduler(td))
979 goto err;
980
Jens Axboe75154842006-06-01 13:56:09 +0200981 td_set_runstate(td, TD_INITIALIZED);
Jens Axboebbfd6b02006-06-07 19:42:54 +0200982 fio_sem_up(&startup_sem);
983 fio_sem_down(&td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100984
Jens Axboe53cdc682006-10-18 11:50:58 +0200985 if (!td->create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100986 goto err;
987
Jens Axboeebac4652005-12-08 15:25:21 +0100988 gettimeofday(&td->epoch, NULL);
989
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200990 if (td->exec_prerun)
991 system(td->exec_prerun);
992
Jens Axboeebac4652005-12-08 15:25:21 +0100993 while (td->loops--) {
994 getrusage(RUSAGE_SELF, &td->ru_start);
995 gettimeofday(&td->start, NULL);
996 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
997
998 if (td->ratemin)
999 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1000
1001 clear_io_state(td);
1002 prune_io_piece_log(td);
1003
Jens Axboe2866c822006-10-09 15:57:48 +02001004 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +02001005 do_cpuio(td);
1006 else
1007 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001008
1009 td->runtime[td->ddir] += mtime_since_now(&td->start);
Jens Axboeaea47d42006-05-26 19:27:29 +02001010 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001011 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
1012
Jens Axboeebac4652005-12-08 15:25:21 +01001013 update_rusage_stat(td);
1014
1015 if (td->error || td->terminate)
1016 break;
1017
1018 if (td->verify == VERIFY_NONE)
1019 continue;
1020
1021 clear_io_state(td);
1022 gettimeofday(&td->start, NULL);
1023
1024 do_verify(td);
1025
1026 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1027
1028 if (td->error || td->terminate)
1029 break;
1030 }
1031
Jens Axboeebac4652005-12-08 15:25:21 +01001032 if (td->bw_log)
1033 finish_log(td, td->bw_log, "bw");
1034 if (td->slat_log)
1035 finish_log(td, td->slat_log, "slat");
1036 if (td->clat_log)
1037 finish_log(td, td->clat_log, "clat");
Jens Axboe843a7412006-06-01 21:14:21 -07001038 if (td->write_iolog)
1039 write_iolog_close(td);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +02001040 if (td->exec_postrun)
1041 system(td->exec_postrun);
Jens Axboeebac4652005-12-08 15:25:21 +01001042
1043 if (exitall_on_terminate)
1044 terminate_threads(td->groupid);
1045
1046err:
Jens Axboe53cdc682006-10-18 11:50:58 +02001047 close_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +02001048 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001049 cleanup_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001050 td_set_runstate(td, TD_EXITED);
1051 return NULL;
1052
1053}
1054
Jens Axboe906c8d72006-06-13 09:37:56 +02001055/*
1056 * We cannot pass the td data into a forked process, so attach the td and
1057 * pass it to the thread worker.
1058 */
Jens Axboeebac4652005-12-08 15:25:21 +01001059static void *fork_main(int shmid, int offset)
1060{
1061 struct thread_data *td;
1062 void *data;
1063
1064 data = shmat(shmid, NULL, 0);
1065 if (data == (void *) -1) {
1066 perror("shmat");
1067 return NULL;
1068 }
1069
1070 td = data + offset * sizeof(struct thread_data);
1071 thread_main(td);
1072 shmdt(data);
1073 return NULL;
1074}
1075
Jens Axboe906c8d72006-06-13 09:37:56 +02001076/*
Jens Axboe906c8d72006-06-13 09:37:56 +02001077 * Run over the job map and reap the threads that have exited, if any.
1078 */
Jens Axboeebac4652005-12-08 15:25:21 +01001079static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1080{
Jens Axboeb990b5c2006-09-14 09:48:22 +02001081 int i, cputhreads;
Jens Axboeebac4652005-12-08 15:25:21 +01001082
1083 /*
1084 * reap exited threads (TD_EXITED -> TD_REAPED)
1085 */
Jens Axboeb990b5c2006-09-14 09:48:22 +02001086 for (i = 0, cputhreads = 0; i < thread_number; i++) {
Jens Axboeebac4652005-12-08 15:25:21 +01001087 struct thread_data *td = &threads[i];
1088
Jens Axboe2866c822006-10-09 15:57:48 +02001089 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +02001090 cputhreads++;
1091
Jens Axboeebac4652005-12-08 15:25:21 +01001092 if (td->runstate != TD_EXITED)
1093 continue;
1094
1095 td_set_runstate(td, TD_REAPED);
1096
1097 if (td->use_thread) {
1098 long ret;
1099
1100 if (pthread_join(td->thread, (void *) &ret))
1101 perror("thread_join");
1102 } else
1103 waitpid(td->pid, NULL, 0);
1104
1105 (*nr_running)--;
1106 (*m_rate) -= td->ratemin;
1107 (*t_rate) -= td->rate;
1108 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001109
1110 if (*nr_running == cputhreads)
1111 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +01001112}
1113
Jens Axboefcb6ade2006-05-31 12:14:35 +02001114static void fio_unpin_memory(void *pinned)
1115{
1116 if (pinned) {
1117 if (munlock(pinned, mlock_size) < 0)
1118 perror("munlock");
1119 munmap(pinned, mlock_size);
1120 }
1121}
1122
1123static void *fio_pin_memory(void)
1124{
Jens Axboe32cd46a2006-06-07 13:40:40 +02001125 unsigned long long phys_mem;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001126 void *ptr;
1127
1128 if (!mlock_size)
1129 return NULL;
1130
1131 /*
1132 * Don't allow mlock of more than real_mem-128MB
1133 */
Jens Axboe32cd46a2006-06-07 13:40:40 +02001134 phys_mem = os_phys_mem();
1135 if (phys_mem) {
1136 if ((mlock_size + 128 * 1024 * 1024) > phys_mem) {
1137 mlock_size = phys_mem - 128 * 1024 * 1024;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001138 fprintf(f_out, "fio: limiting mlocked memory to %lluMiB\n", mlock_size >> 20);
Jens Axboefcb6ade2006-05-31 12:14:35 +02001139 }
1140 }
1141
1142 ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1143 if (!ptr) {
1144 perror("malloc locked mem");
1145 return NULL;
1146 }
1147 if (mlock(ptr, mlock_size) < 0) {
1148 munmap(ptr, mlock_size);
1149 perror("mlock");
1150 return NULL;
1151 }
1152
1153 return ptr;
1154}
1155
Jens Axboe906c8d72006-06-13 09:37:56 +02001156/*
1157 * Main function for kicking off and reaping jobs, as needed.
1158 */
Jens Axboeebac4652005-12-08 15:25:21 +01001159static void run_threads(void)
1160{
Jens Axboeebac4652005-12-08 15:25:21 +01001161 struct thread_data *td;
1162 unsigned long spent;
1163 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001164 void *mlocked_mem;
1165
1166 mlocked_mem = fio_pin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001167
Jens Axboec6ae0a52006-06-12 11:04:44 +02001168 if (!terse_output) {
1169 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
1170 fflush(stdout);
1171 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001172
Jens Axboe4efa9702006-05-31 11:56:49 +02001173 signal(SIGINT, sig_handler);
1174 signal(SIGALRM, sig_handler);
1175
Jens Axboeebac4652005-12-08 15:25:21 +01001176 todo = thread_number;
1177 nr_running = 0;
1178 nr_started = 0;
1179 m_rate = t_rate = 0;
1180
1181 for (i = 0; i < thread_number; i++) {
1182 td = &threads[i];
1183
Jens Axboe263e5292006-10-18 15:37:01 +02001184 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001185
1186 init_disk_util(td);
1187
1188 if (!td->create_serialize)
1189 continue;
1190
1191 /*
1192 * do file setup here so it happens sequentially,
1193 * we don't want X number of threads getting their
1194 * client data interspersed on disk
1195 */
Jens Axboe53cdc682006-10-18 11:50:58 +02001196 if (setup_files(td)) {
Jens Axboeebac4652005-12-08 15:25:21 +01001197 td_set_runstate(td, TD_REAPED);
1198 todo--;
1199 }
1200 }
1201
Jens Axboe263e5292006-10-18 15:37:01 +02001202 time_init();
Jens Axboeebac4652005-12-08 15:25:21 +01001203
1204 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +02001205 struct thread_data *map[MAX_JOBS];
1206 struct timeval this_start;
1207 int this_jobs = 0, left;
1208
Jens Axboeebac4652005-12-08 15:25:21 +01001209 /*
1210 * create threads (TD_NOT_CREATED -> TD_CREATED)
1211 */
1212 for (i = 0; i < thread_number; i++) {
1213 td = &threads[i];
1214
1215 if (td->runstate != TD_NOT_CREATED)
1216 continue;
1217
1218 /*
1219 * never got a chance to start, killed by other
1220 * thread for some reason
1221 */
1222 if (td->terminate) {
1223 todo--;
1224 continue;
1225 }
1226
1227 if (td->start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +02001228 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +01001229
1230 if (td->start_delay * 1000 > spent)
1231 continue;
1232 }
1233
1234 if (td->stonewall && (nr_started || nr_running))
1235 break;
1236
Jens Axboe75154842006-06-01 13:56:09 +02001237 /*
1238 * Set state to created. Thread will transition
1239 * to TD_INITIALIZED when it's done setting up.
1240 */
Jens Axboeebac4652005-12-08 15:25:21 +01001241 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001242 map[this_jobs++] = td;
Jens Axboebbfd6b02006-06-07 19:42:54 +02001243 fio_sem_init(&startup_sem, 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001244 nr_started++;
1245
1246 if (td->use_thread) {
1247 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1248 perror("thread_create");
1249 nr_started--;
1250 }
1251 } else {
1252 if (fork())
Jens Axboebbfd6b02006-06-07 19:42:54 +02001253 fio_sem_down(&startup_sem);
Jens Axboeebac4652005-12-08 15:25:21 +01001254 else {
1255 fork_main(shm_id, i);
1256 exit(0);
1257 }
1258 }
1259 }
1260
1261 /*
Jens Axboe75154842006-06-01 13:56:09 +02001262 * Wait for the started threads to transition to
1263 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001264 */
Jens Axboe75154842006-06-01 13:56:09 +02001265 gettimeofday(&this_start, NULL);
1266 left = this_jobs;
1267 while (left) {
1268 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1269 break;
1270
1271 usleep(100000);
1272
1273 for (i = 0; i < this_jobs; i++) {
1274 td = map[i];
1275 if (!td)
1276 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001277 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001278 map[i] = NULL;
1279 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001280 } else if (td->runstate >= TD_EXITED) {
1281 map[i] = NULL;
1282 left--;
1283 todo--;
1284 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001285 }
1286 }
1287 }
1288
1289 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001290 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001291 for (i = 0; i < this_jobs; i++) {
1292 td = map[i];
1293 if (!td)
1294 continue;
1295 kill(td->pid, SIGTERM);
1296 }
1297 break;
1298 }
1299
1300 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001301 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001302 */
Jens Axboeebac4652005-12-08 15:25:21 +01001303 for (i = 0; i < thread_number; i++) {
1304 td = &threads[i];
1305
Jens Axboe75154842006-06-01 13:56:09 +02001306 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001307 continue;
1308
1309 td_set_runstate(td, TD_RUNNING);
1310 nr_running++;
1311 nr_started--;
1312 m_rate += td->ratemin;
1313 t_rate += td->rate;
Jens Axboe75154842006-06-01 13:56:09 +02001314 todo--;
Jens Axboebbfd6b02006-06-07 19:42:54 +02001315 fio_sem_up(&td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001316 }
1317
1318 reap_threads(&nr_running, &t_rate, &m_rate);
1319
1320 if (todo)
1321 usleep(100000);
1322 }
1323
1324 while (nr_running) {
1325 reap_threads(&nr_running, &t_rate, &m_rate);
1326 usleep(10000);
1327 }
1328
1329 update_io_ticks();
Jens Axboefcb6ade2006-05-31 12:14:35 +02001330 fio_unpin_memory(mlocked_mem);
Jens Axboeebac4652005-12-08 15:25:21 +01001331}
1332
Jens Axboeebac4652005-12-08 15:25:21 +01001333int main(int argc, char *argv[])
1334{
1335 if (parse_options(argc, argv))
1336 return 1;
1337
1338 if (!thread_number) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001339 log_err("Nothing to do\n");
Jens Axboeebac4652005-12-08 15:25:21 +01001340 return 1;
1341 }
1342
1343 disk_util_timer_arm();
1344
1345 run_threads();
1346 show_run_stats();
1347
1348 return 0;
1349}