Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * fio - the flexible io tester |
| 3 | * |
| 4 | * Copyright (C) 2005 Jens Axboe <axboe@suse.de> |
Jens Axboe | aae22ca | 2006-09-05 10:46:22 +0200 | [diff] [blame] | 5 | * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk> |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 6 | * |
Jens Axboe | 8e9fe63 | 2006-10-24 15:11:09 +0200 | [diff] [blame] | 7 | * The license below covers all files distributed with fio unless otherwise |
| 8 | * noted in the file itself. |
| 9 | * |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
Jens Axboe | 8e9fe63 | 2006-10-24 15:11:09 +0200 | [diff] [blame] | 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | * |
| 23 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <string.h> |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 27 | #include <signal.h> |
| 28 | #include <time.h> |
Jens Axboe | dbe1125 | 2007-02-11 00:19:51 +0100 | [diff] [blame] | 29 | #include <locale.h> |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 30 | #include <assert.h> |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 31 | #include <sys/stat.h> |
| 32 | #include <sys/wait.h> |
| 33 | #include <sys/ipc.h> |
| 34 | #include <sys/shm.h> |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 35 | #include <sys/mman.h> |
| 36 | |
| 37 | #include "fio.h" |
Jens Axboe | 210f43b | 2007-04-17 19:52:18 +0200 | [diff] [blame] | 38 | #include "hash.h" |
Jens Axboe | 2e5cdb1 | 2008-03-01 18:52:49 +0100 | [diff] [blame] | 39 | #include "smalloc.h" |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 40 | |
Jens Axboe | cfc99db | 2007-03-14 10:34:47 +0100 | [diff] [blame] | 41 | unsigned long page_mask; |
| 42 | unsigned long page_size; |
Jens Axboe | 29d610e | 2007-02-06 13:25:33 +0100 | [diff] [blame] | 43 | #define ALIGN(buf) \ |
| 44 | (char *) (((unsigned long) (buf) + page_mask) & ~page_mask) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 45 | |
| 46 | int groupid = 0; |
| 47 | int thread_number = 0; |
Jens Axboe | 9cedf16 | 2007-03-12 11:29:30 +0100 | [diff] [blame] | 48 | int nr_process = 0; |
| 49 | int nr_thread = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 50 | int shm_id = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 51 | int temp_stall_ts; |
Jens Axboe | d3eeeab | 2008-04-06 12:19:05 +0200 | [diff] [blame] | 52 | unsigned long done_secs = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 53 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 54 | static struct fio_mutex *startup_mutex; |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame] | 55 | static volatile int fio_abort; |
Jens Axboe | 437c9b7 | 2007-02-13 18:20:37 +0100 | [diff] [blame] | 56 | static int exit_value; |
Jens Axboe | ccb0fa2 | 2008-05-30 23:18:00 +0200 | [diff] [blame] | 57 | static struct itimerval itimer; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 58 | |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 59 | struct io_log *agg_io_log[2]; |
| 60 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 61 | #define TERMINATE_ALL (-1) |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 62 | #define JOB_START_TIMEOUT (5 * 1000) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 63 | |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame] | 64 | static inline void td_set_runstate(struct thread_data *td, int runstate) |
| 65 | { |
Jens Axboe | d8fab1b | 2008-03-06 10:25:17 +0100 | [diff] [blame] | 66 | if (td->runstate == runstate) |
| 67 | return; |
| 68 | |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 69 | dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", (int) td->pid, |
| 70 | td->runstate, runstate); |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame] | 71 | td->runstate = runstate; |
| 72 | } |
| 73 | |
Jens Axboe | 390c40e | 2007-03-05 12:35:29 +0100 | [diff] [blame] | 74 | static void terminate_threads(int group_id) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 75 | { |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 76 | struct thread_data *td; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 77 | int i; |
| 78 | |
Jens Axboe | 6f88bcb | 2008-03-19 10:29:07 +0100 | [diff] [blame] | 79 | dprint(FD_PROCESS, "terminate group_id=%d\n", group_id); |
| 80 | |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 81 | for_each_td(td, i) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 82 | if (group_id == TERMINATE_ALL || groupid == td->groupid) { |
Jens Axboe | 6f88bcb | 2008-03-19 10:29:07 +0100 | [diff] [blame] | 83 | dprint(FD_PROCESS, "setting terminate on %s/%d\n", |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 84 | td->o.name, (int) td->pid); |
Jens Axboe | ad830ed | 2008-02-18 21:11:24 +0100 | [diff] [blame] | 85 | td->terminate = 1; |
| 86 | td->o.start_delay = 0; |
| 87 | |
Jens Axboe | 7a93ab1 | 2007-04-18 12:25:41 +0200 | [diff] [blame] | 88 | /* |
| 89 | * if the thread is running, just let it exit |
| 90 | */ |
| 91 | if (td->runstate < TD_RUNNING) |
Jens Axboe | c1302d4 | 2007-03-05 20:18:31 +0100 | [diff] [blame] | 92 | kill(td->pid, SIGQUIT); |
Jens Axboe | f63f7f3 | 2008-03-01 15:25:24 +0100 | [diff] [blame] | 93 | else { |
| 94 | struct ioengine_ops *ops = td->io_ops; |
| 95 | |
| 96 | if (ops && (ops->flags & FIO_SIGQUIT)) |
| 97 | kill(td->pid, SIGQUIT); |
| 98 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
Jens Axboe | ccb0fa2 | 2008-05-30 23:18:00 +0200 | [diff] [blame] | 103 | static void status_timer_arm(void) |
| 104 | { |
| 105 | itimer.it_value.tv_sec = 0; |
| 106 | itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000; |
| 107 | setitimer(ITIMER_REAL, &itimer, NULL); |
| 108 | } |
| 109 | |
Jens Axboe | 697a606 | 2008-05-31 00:04:45 +0200 | [diff] [blame] | 110 | static void sig_alrm(int sig) |
Jens Axboe | ccb0fa2 | 2008-05-30 23:18:00 +0200 | [diff] [blame] | 111 | { |
Jens Axboe | 697a606 | 2008-05-31 00:04:45 +0200 | [diff] [blame] | 112 | if (threads) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 113 | update_io_ticks(); |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 114 | print_thread_status(); |
Jens Axboe | ccb0fa2 | 2008-05-30 23:18:00 +0200 | [diff] [blame] | 115 | status_timer_arm(); |
Jens Axboe | 697a606 | 2008-05-31 00:04:45 +0200 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | static void sig_int(int sig) |
| 120 | { |
| 121 | if (threads) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 122 | printf("\nfio: terminating on signal %d\n", sig); |
| 123 | fflush(stdout); |
| 124 | terminate_threads(TERMINATE_ALL); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Jens Axboe | 697a606 | 2008-05-31 00:04:45 +0200 | [diff] [blame] | 128 | static void set_sig_handlers(void) |
| 129 | { |
| 130 | struct sigaction act; |
| 131 | |
| 132 | memset(&act, 0, sizeof(act)); |
| 133 | act.sa_handler = sig_alrm; |
| 134 | act.sa_flags = SA_RESTART; |
| 135 | sigaction(SIGALRM, &act, NULL); |
| 136 | |
| 137 | memset(&act, 0, sizeof(act)); |
| 138 | act.sa_handler = sig_int; |
| 139 | act.sa_flags = SA_RESTART; |
| 140 | sigaction(SIGINT, &act, NULL); |
| 141 | } |
| 142 | |
| 143 | /* |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 144 | * Check if we are above the minimum rate given. |
| 145 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 146 | static int check_min_rate(struct thread_data *td, struct timeval *now) |
| 147 | { |
Jens Axboe | 0904200 | 2007-02-23 10:56:22 +0100 | [diff] [blame] | 148 | unsigned long long bytes = 0; |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 149 | unsigned long iops = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 150 | unsigned long spent; |
| 151 | unsigned long rate; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 152 | |
| 153 | /* |
Jens Axboe | 780bf1a | 2007-02-28 11:13:49 +0100 | [diff] [blame] | 154 | * No minimum rate set, always ok |
| 155 | */ |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 156 | if (!td->o.ratemin && !td->o.rate_iops_min) |
Jens Axboe | 780bf1a | 2007-02-28 11:13:49 +0100 | [diff] [blame] | 157 | return 0; |
| 158 | |
| 159 | /* |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 160 | * allow a 2 second settle period in the beginning |
| 161 | */ |
| 162 | if (mtime_since(&td->start, now) < 2000) |
| 163 | return 0; |
| 164 | |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 165 | if (td_read(td)) { |
| 166 | iops += td->io_blocks[DDIR_READ]; |
Jens Axboe | 0904200 | 2007-02-23 10:56:22 +0100 | [diff] [blame] | 167 | bytes += td->this_io_bytes[DDIR_READ]; |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 168 | } |
| 169 | if (td_write(td)) { |
| 170 | iops += td->io_blocks[DDIR_WRITE]; |
Jens Axboe | 0904200 | 2007-02-23 10:56:22 +0100 | [diff] [blame] | 171 | bytes += td->this_io_bytes[DDIR_WRITE]; |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 172 | } |
Jens Axboe | 0904200 | 2007-02-23 10:56:22 +0100 | [diff] [blame] | 173 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 174 | /* |
| 175 | * if rate blocks is set, sample is running |
| 176 | */ |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 177 | if (td->rate_bytes || td->rate_blocks) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 178 | spent = mtime_since(&td->lastrate, now); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 179 | if (spent < td->o.ratecycle) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 180 | return 0; |
| 181 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 182 | if (td->o.rate) { |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 183 | /* |
| 184 | * check bandwidth specified rate |
| 185 | */ |
| 186 | if (bytes < td->rate_bytes) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 187 | log_err("%s: min rate %u not met\n", td->o.name, |
| 188 | td->o.ratemin); |
Jens Axboe | 413dd45 | 2007-02-23 09:26:09 +0100 | [diff] [blame] | 189 | return 1; |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 190 | } else { |
| 191 | rate = (bytes - td->rate_bytes) / spent; |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 192 | if (rate < td->o.ratemin || |
| 193 | bytes < td->rate_bytes) { |
| 194 | log_err("%s: min rate %u not met, got" |
| 195 | " %luKiB/sec\n", td->o.name, |
| 196 | td->o.ratemin, rate); |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 197 | return 1; |
| 198 | } |
| 199 | } |
| 200 | } else { |
| 201 | /* |
| 202 | * checks iops specified rate |
| 203 | */ |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 204 | if (iops < td->o.rate_iops) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 205 | log_err("%s: min iops rate %u not met\n", |
| 206 | td->o.name, td->o.rate_iops); |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 207 | return 1; |
| 208 | } else { |
| 209 | rate = (iops - td->rate_blocks) / spent; |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 210 | if (rate < td->o.rate_iops_min || |
| 211 | iops < td->rate_blocks) { |
| 212 | log_err("%s: min iops rate %u not met," |
| 213 | " got %lu\n", td->o.name, |
| 214 | td->o.rate_iops_min, |
| 215 | rate); |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 216 | } |
Jens Axboe | 413dd45 | 2007-02-23 09:26:09 +0100 | [diff] [blame] | 217 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
Jens Axboe | 0904200 | 2007-02-23 10:56:22 +0100 | [diff] [blame] | 221 | td->rate_bytes = bytes; |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 222 | td->rate_blocks = iops; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 223 | memcpy(&td->lastrate, now, sizeof(*now)); |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | static inline int runtime_exceeded(struct thread_data *td, struct timeval *t) |
| 228 | { |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 229 | if (!td->o.timeout) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 230 | return 0; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 231 | if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 232 | return 1; |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 237 | /* |
| 238 | * When job exits, we can cancel the in-flight IO if we are using async |
| 239 | * io. Attempt to do so. |
| 240 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 241 | static void cleanup_pending_aio(struct thread_data *td) |
| 242 | { |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 243 | struct flist_head *entry, *n; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 244 | struct io_u *io_u; |
| 245 | int r; |
| 246 | |
| 247 | /* |
| 248 | * get immediately available events, if any |
| 249 | */ |
Jens Axboe | d7762cf | 2007-02-23 12:34:57 +0100 | [diff] [blame] | 250 | r = io_u_queued_complete(td, 0); |
Jens Axboe | b2fdda4 | 2007-02-20 20:52:51 +0100 | [diff] [blame] | 251 | if (r < 0) |
| 252 | return; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 253 | |
| 254 | /* |
| 255 | * now cancel remaining active events |
| 256 | */ |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 257 | if (td->io_ops->cancel) { |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 258 | flist_for_each_safe(entry, n, &td->io_u_busylist) { |
| 259 | io_u = flist_entry(entry, struct io_u, list); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 260 | |
Jens Axboe | 0c6e751 | 2007-02-22 11:19:39 +0100 | [diff] [blame] | 261 | /* |
| 262 | * if the io_u isn't in flight, then that generally |
| 263 | * means someone leaked an io_u. complain but fix |
| 264 | * it up, so we don't stall here. |
| 265 | */ |
| 266 | if ((io_u->flags & IO_U_F_FLIGHT) == 0) { |
| 267 | log_err("fio: non-busy IO on busy list\n"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 268 | put_io_u(td, io_u); |
Jens Axboe | 0c6e751 | 2007-02-22 11:19:39 +0100 | [diff] [blame] | 269 | } else { |
| 270 | r = td->io_ops->cancel(td, io_u); |
| 271 | if (!r) |
| 272 | put_io_u(td, io_u); |
| 273 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 277 | if (td->cur_depth) |
Jens Axboe | d7762cf | 2007-02-23 12:34:57 +0100 | [diff] [blame] | 278 | r = io_u_queued_complete(td, td->cur_depth); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 279 | } |
| 280 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 281 | /* |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 282 | * Helper to handle the final sync of a file. Works just like the normal |
| 283 | * io path, just does everything sync. |
| 284 | */ |
| 285 | static int fio_io_sync(struct thread_data *td, struct fio_file *f) |
| 286 | { |
| 287 | struct io_u *io_u = __get_io_u(td); |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 288 | int ret; |
| 289 | |
| 290 | if (!io_u) |
| 291 | return 1; |
| 292 | |
| 293 | io_u->ddir = DDIR_SYNC; |
| 294 | io_u->file = f; |
| 295 | |
| 296 | if (td_io_prep(td, io_u)) { |
| 297 | put_io_u(td, io_u); |
| 298 | return 1; |
| 299 | } |
| 300 | |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 301 | requeue: |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 302 | ret = td_io_queue(td, io_u); |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 303 | if (ret < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 304 | td_verror(td, io_u->error, "td_io_queue"); |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 305 | put_io_u(td, io_u); |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 306 | return 1; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 307 | } else if (ret == FIO_Q_QUEUED) { |
Jens Axboe | d7762cf | 2007-02-23 12:34:57 +0100 | [diff] [blame] | 308 | if (io_u_queued_complete(td, 1) < 0) |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 309 | return 1; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 310 | } else if (ret == FIO_Q_COMPLETED) { |
| 311 | if (io_u->error) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 312 | td_verror(td, io_u->error, "td_io_queue"); |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 313 | return 1; |
| 314 | } |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 315 | |
Jens Axboe | d7762cf | 2007-02-23 12:34:57 +0100 | [diff] [blame] | 316 | if (io_u_sync_complete(td, io_u) < 0) |
Jens Axboe | b2fdda4 | 2007-02-20 20:52:51 +0100 | [diff] [blame] | 317 | return 1; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 318 | } else if (ret == FIO_Q_BUSY) { |
| 319 | if (td_io_commit(td)) |
| 320 | return 1; |
| 321 | goto requeue; |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | /* |
Jens Axboe | 34403fb | 2007-03-02 21:43:25 +0100 | [diff] [blame] | 328 | * The main verify engine. Runs over the writes we previously submitted, |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 329 | * reads the blocks back in, and checks the crc/md5 of the data. |
| 330 | */ |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 331 | static void do_verify(struct thread_data *td) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 332 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 333 | struct fio_file *f; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 334 | struct io_u *io_u; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 335 | int ret, min_events; |
| 336 | unsigned int i; |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 337 | |
| 338 | /* |
| 339 | * sync io first and invalidate cache, to make sure we really |
| 340 | * read from disk. |
| 341 | */ |
| 342 | for_each_file(td, f, i) { |
Jens Axboe | 3d7b485 | 2007-03-13 14:50:28 +0100 | [diff] [blame] | 343 | if (!(f->flags & FIO_FILE_OPEN)) |
| 344 | continue; |
Jens Axboe | b2fdda4 | 2007-02-20 20:52:51 +0100 | [diff] [blame] | 345 | if (fio_io_sync(td, f)) |
| 346 | break; |
| 347 | if (file_invalidate_cache(td, f)) |
| 348 | break; |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 349 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 350 | |
Jens Axboe | b2fdda4 | 2007-02-20 20:52:51 +0100 | [diff] [blame] | 351 | if (td->error) |
| 352 | return; |
| 353 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 354 | td_set_runstate(td, TD_VERIFYING); |
| 355 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 356 | io_u = NULL; |
| 357 | while (!td->terminate) { |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame^] | 358 | int ret2, full; |
Jens Axboe | 5451792 | 2007-03-05 10:06:06 +0100 | [diff] [blame] | 359 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 360 | io_u = __get_io_u(td); |
| 361 | if (!io_u) |
| 362 | break; |
| 363 | |
Jens Axboe | 069c291 | 2007-02-21 23:02:39 +0100 | [diff] [blame] | 364 | if (runtime_exceeded(td, &io_u->start_time)) { |
| 365 | put_io_u(td, io_u); |
Jens Axboe | 90a87d4 | 2007-04-18 09:02:32 +0200 | [diff] [blame] | 366 | td->terminate = 1; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 367 | break; |
Jens Axboe | 069c291 | 2007-02-21 23:02:39 +0100 | [diff] [blame] | 368 | } |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 369 | |
Jens Axboe | 069c291 | 2007-02-21 23:02:39 +0100 | [diff] [blame] | 370 | if (get_next_verify(td, io_u)) { |
| 371 | put_io_u(td, io_u); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 372 | break; |
Jens Axboe | 069c291 | 2007-02-21 23:02:39 +0100 | [diff] [blame] | 373 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 374 | |
Jens Axboe | 069c291 | 2007-02-21 23:02:39 +0100 | [diff] [blame] | 375 | if (td_io_prep(td, io_u)) { |
| 376 | put_io_u(td, io_u); |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 377 | break; |
Jens Axboe | 069c291 | 2007-02-21 23:02:39 +0100 | [diff] [blame] | 378 | } |
Jens Axboe | d7762cf | 2007-02-23 12:34:57 +0100 | [diff] [blame] | 379 | |
| 380 | io_u->end_io = verify_io_u; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 381 | |
Jens Axboe | 1178680 | 2007-03-05 18:33:22 +0100 | [diff] [blame] | 382 | ret = td_io_queue(td, io_u); |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 383 | switch (ret) { |
| 384 | case FIO_Q_COMPLETED: |
| 385 | if (io_u->error) |
Jens Axboe | 22819ec | 2007-02-18 07:47:14 +0100 | [diff] [blame] | 386 | ret = -io_u->error; |
Jens Axboe | 9e9d164 | 2007-03-12 09:37:46 +0100 | [diff] [blame] | 387 | else if (io_u->resid) { |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 388 | int bytes = io_u->xfer_buflen - io_u->resid; |
Jens Axboe | d460eb3 | 2007-04-16 21:39:33 +0200 | [diff] [blame] | 389 | struct fio_file *f = io_u->file; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 390 | |
Jens Axboe | 9e9d164 | 2007-03-12 09:37:46 +0100 | [diff] [blame] | 391 | /* |
| 392 | * zero read, fail |
| 393 | */ |
| 394 | if (!bytes) { |
Jens Axboe | 41d9f0f | 2008-06-02 12:19:50 +0200 | [diff] [blame] | 395 | td_verror(td, EIO, "full resid"); |
Jens Axboe | 9e9d164 | 2007-03-12 09:37:46 +0100 | [diff] [blame] | 396 | put_io_u(td, io_u); |
| 397 | break; |
| 398 | } |
Jens Axboe | 8400d9b | 2007-03-28 11:10:09 +0200 | [diff] [blame] | 399 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 400 | io_u->xfer_buflen = io_u->resid; |
| 401 | io_u->xfer_buf += bytes; |
Jens Axboe | 8400d9b | 2007-03-28 11:10:09 +0200 | [diff] [blame] | 402 | io_u->offset += bytes; |
| 403 | |
Jens Axboe | 30061b9 | 2007-04-17 13:31:34 +0200 | [diff] [blame] | 404 | td->ts.short_io_u[io_u->ddir]++; |
| 405 | |
Jens Axboe | d460eb3 | 2007-04-16 21:39:33 +0200 | [diff] [blame] | 406 | if (io_u->offset == f->real_file_size) |
Jens Axboe | 8400d9b | 2007-03-28 11:10:09 +0200 | [diff] [blame] | 407 | goto sync_done; |
| 408 | |
Jens Axboe | 1178680 | 2007-03-05 18:33:22 +0100 | [diff] [blame] | 409 | requeue_io_u(td, &io_u); |
| 410 | } else { |
Jens Axboe | 8400d9b | 2007-03-28 11:10:09 +0200 | [diff] [blame] | 411 | sync_done: |
Jens Axboe | 1178680 | 2007-03-05 18:33:22 +0100 | [diff] [blame] | 412 | ret = io_u_sync_complete(td, io_u); |
| 413 | if (ret < 0) |
| 414 | break; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 415 | } |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 416 | continue; |
| 417 | case FIO_Q_QUEUED: |
| 418 | break; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 419 | case FIO_Q_BUSY: |
| 420 | requeue_io_u(td, &io_u); |
Jens Axboe | 5451792 | 2007-03-05 10:06:06 +0100 | [diff] [blame] | 421 | ret2 = td_io_commit(td); |
| 422 | if (ret2 < 0) |
| 423 | ret = ret2; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 424 | break; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 425 | default: |
| 426 | assert(ret < 0); |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 427 | td_verror(td, -ret, "td_io_queue"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 428 | break; |
| 429 | } |
| 430 | |
Jens Axboe | 9978463 | 2007-02-19 10:06:17 +0100 | [diff] [blame] | 431 | if (ret < 0 || td->error) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 432 | break; |
| 433 | |
Jens Axboe | 3af6ef3 | 2007-02-18 06:57:43 +0100 | [diff] [blame] | 434 | /* |
| 435 | * if we can queue more, do so. but check if there are |
| 436 | * completed io_u's first. |
| 437 | */ |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame^] | 438 | full = queue_full(td) || ret == FIO_Q_BUSY; |
| 439 | if (full || !td->o.iodepth_batch_complete) { |
| 440 | min_events = td->o.iodepth_batch_complete; |
| 441 | if (full && !min_events) |
Jens Axboe | 838bc70 | 2008-05-22 13:08:23 +0200 | [diff] [blame] | 442 | min_events = 1; |
Jens Axboe | e916b39 | 2007-02-20 14:37:26 +0100 | [diff] [blame] | 443 | |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame^] | 444 | do { |
| 445 | /* |
| 446 | * Reap required number of io units, if any, |
| 447 | * and do the verification on them through |
| 448 | * the callback handler |
| 449 | */ |
| 450 | if (io_u_queued_complete(td, min_events) < 0) { |
| 451 | ret = -1; |
| 452 | break; |
| 453 | } |
| 454 | } while (full && (td->cur_depth > td->o.iodepth_low)); |
| 455 | } |
| 456 | if (ret < 0) |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 457 | break; |
| 458 | } |
| 459 | |
Jens Axboe | c01c039 | 2007-02-26 12:43:42 +0100 | [diff] [blame] | 460 | if (!td->error) { |
| 461 | min_events = td->cur_depth; |
| 462 | |
| 463 | if (min_events) |
| 464 | ret = io_u_queued_complete(td, min_events); |
| 465 | } else |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 466 | cleanup_pending_aio(td); |
| 467 | |
| 468 | td_set_runstate(td, TD_RUNNING); |
| 469 | } |
| 470 | |
Jens Axboe | 32cd46a | 2006-06-07 13:40:40 +0200 | [diff] [blame] | 471 | /* |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 472 | * Main IO worker function. It retrieves io_u's to process and queues |
Jens Axboe | 32cd46a | 2006-06-07 13:40:40 +0200 | [diff] [blame] | 473 | * and reaps them, checking for rate and errors along the way. |
| 474 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 475 | static void do_io(struct thread_data *td) |
| 476 | { |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 477 | struct timeval s; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 478 | unsigned long usec; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 479 | unsigned int i; |
| 480 | int ret = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 481 | |
Jens Axboe | 5853e5a | 2006-06-08 14:41:05 +0200 | [diff] [blame] | 482 | td_set_runstate(td, TD_RUNNING); |
| 483 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 484 | while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) { |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 485 | struct timeval comp_time; |
| 486 | long bytes_done = 0; |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 487 | int min_evts = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 488 | struct io_u *io_u; |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame^] | 489 | int ret2, full; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 490 | |
| 491 | if (td->terminate) |
| 492 | break; |
| 493 | |
Jens Axboe | 3d7c391 | 2007-02-19 13:16:12 +0100 | [diff] [blame] | 494 | io_u = get_io_u(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 495 | if (!io_u) |
| 496 | break; |
| 497 | |
| 498 | memcpy(&s, &io_u->start_time, sizeof(s)); |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 499 | |
| 500 | if (runtime_exceeded(td, &s)) { |
| 501 | put_io_u(td, io_u); |
Jens Axboe | 90a87d4 | 2007-04-18 09:02:32 +0200 | [diff] [blame] | 502 | td->terminate = 1; |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 503 | break; |
| 504 | } |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 505 | |
Jens Axboe | b67740d | 2007-07-26 12:22:30 +0200 | [diff] [blame] | 506 | /* |
| 507 | * Add verification end_io handler, if asked to verify |
| 508 | * a previously written file. |
| 509 | */ |
Jens Axboe | cc3fc85 | 2008-03-06 11:47:53 +0100 | [diff] [blame] | 510 | if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) { |
Jens Axboe | b67740d | 2007-07-26 12:22:30 +0200 | [diff] [blame] | 511 | io_u->end_io = verify_io_u; |
Jens Axboe | d8fab1b | 2008-03-06 10:25:17 +0100 | [diff] [blame] | 512 | td_set_runstate(td, TD_VERIFYING); |
| 513 | } else |
| 514 | td_set_runstate(td, TD_RUNNING); |
Jens Axboe | b67740d | 2007-07-26 12:22:30 +0200 | [diff] [blame] | 515 | |
Jens Axboe | 1178680 | 2007-03-05 18:33:22 +0100 | [diff] [blame] | 516 | ret = td_io_queue(td, io_u); |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 517 | switch (ret) { |
| 518 | case FIO_Q_COMPLETED: |
Jens Axboe | 5451792 | 2007-03-05 10:06:06 +0100 | [diff] [blame] | 519 | if (io_u->error) |
| 520 | ret = -io_u->error; |
Jens Axboe | 9e9d164 | 2007-03-12 09:37:46 +0100 | [diff] [blame] | 521 | else if (io_u->resid) { |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 522 | int bytes = io_u->xfer_buflen - io_u->resid; |
Jens Axboe | d460eb3 | 2007-04-16 21:39:33 +0200 | [diff] [blame] | 523 | struct fio_file *f = io_u->file; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 524 | |
Jens Axboe | 9e9d164 | 2007-03-12 09:37:46 +0100 | [diff] [blame] | 525 | /* |
| 526 | * zero read, fail |
| 527 | */ |
| 528 | if (!bytes) { |
Jens Axboe | 41d9f0f | 2008-06-02 12:19:50 +0200 | [diff] [blame] | 529 | td_verror(td, EIO, "full resid"); |
Jens Axboe | 9e9d164 | 2007-03-12 09:37:46 +0100 | [diff] [blame] | 530 | put_io_u(td, io_u); |
| 531 | break; |
| 532 | } |
| 533 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 534 | io_u->xfer_buflen = io_u->resid; |
| 535 | io_u->xfer_buf += bytes; |
Jens Axboe | 5a7c568 | 2007-03-28 11:07:20 +0200 | [diff] [blame] | 536 | io_u->offset += bytes; |
| 537 | |
Jens Axboe | 30061b9 | 2007-04-17 13:31:34 +0200 | [diff] [blame] | 538 | td->ts.short_io_u[io_u->ddir]++; |
| 539 | |
Jens Axboe | d460eb3 | 2007-04-16 21:39:33 +0200 | [diff] [blame] | 540 | if (io_u->offset == f->real_file_size) |
Jens Axboe | 5a7c568 | 2007-03-28 11:07:20 +0200 | [diff] [blame] | 541 | goto sync_done; |
| 542 | |
Jens Axboe | 1178680 | 2007-03-05 18:33:22 +0100 | [diff] [blame] | 543 | requeue_io_u(td, &io_u); |
| 544 | } else { |
Jens Axboe | 5a7c568 | 2007-03-28 11:07:20 +0200 | [diff] [blame] | 545 | sync_done: |
Jens Axboe | 1178680 | 2007-03-05 18:33:22 +0100 | [diff] [blame] | 546 | fio_gettime(&comp_time, NULL); |
| 547 | bytes_done = io_u_sync_complete(td, io_u); |
| 548 | if (bytes_done < 0) |
| 549 | ret = bytes_done; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 550 | } |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 551 | break; |
| 552 | case FIO_Q_QUEUED: |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 553 | /* |
| 554 | * if the engine doesn't have a commit hook, |
| 555 | * the io_u is really queued. if it does have such |
| 556 | * a hook, it has to call io_u_queued() itself. |
| 557 | */ |
| 558 | if (td->io_ops->commit == NULL) |
| 559 | io_u_queued(td, io_u); |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 560 | break; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 561 | case FIO_Q_BUSY: |
| 562 | requeue_io_u(td, &io_u); |
Jens Axboe | 5451792 | 2007-03-05 10:06:06 +0100 | [diff] [blame] | 563 | ret2 = td_io_commit(td); |
| 564 | if (ret2 < 0) |
| 565 | ret = ret2; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 566 | break; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 567 | default: |
| 568 | assert(ret < 0); |
| 569 | put_io_u(td, io_u); |
| 570 | break; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 571 | } |
| 572 | |
Jens Axboe | 9978463 | 2007-02-19 10:06:17 +0100 | [diff] [blame] | 573 | if (ret < 0 || td->error) |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 574 | break; |
| 575 | |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 576 | /* |
| 577 | * See if we need to complete some commands |
| 578 | */ |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame^] | 579 | full = queue_full(td) || ret == FIO_Q_BUSY; |
| 580 | if (full || !td->o.iodepth_batch_complete) { |
| 581 | min_evts = td->o.iodepth_batch_complete; |
| 582 | if (full && !min_evts) |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 583 | min_evts = 1; |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame^] | 584 | |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 585 | fio_gettime(&comp_time, NULL); |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame^] | 586 | |
| 587 | do { |
| 588 | ret = io_u_queued_complete(td, min_evts); |
| 589 | if (ret <= 0) |
| 590 | break; |
| 591 | |
| 592 | bytes_done += ret; |
| 593 | } while (full && (td->cur_depth > td->o.iodepth_low)); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 594 | } |
| 595 | |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame^] | 596 | if (ret < 0) |
| 597 | break; |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 598 | if (!bytes_done) |
| 599 | continue; |
| 600 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 601 | /* |
| 602 | * the rate is batched for now, it should work for batches |
| 603 | * of completions except the very first one which may look |
| 604 | * a little bursty |
| 605 | */ |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 606 | usec = utime_since(&s, &comp_time); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 607 | |
Jens Axboe | 413dd45 | 2007-02-23 09:26:09 +0100 | [diff] [blame] | 608 | rate_throttle(td, usec, bytes_done); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 609 | |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 610 | if (check_min_rate(td, &comp_time)) { |
Jens Axboe | 98aa62d | 2006-11-07 14:16:19 +0100 | [diff] [blame] | 611 | if (exitall_on_terminate) |
Jens Axboe | 390c40e | 2007-03-05 12:35:29 +0100 | [diff] [blame] | 612 | terminate_threads(td->groupid); |
Jens Axboe | 41d9f0f | 2008-06-02 12:19:50 +0200 | [diff] [blame] | 613 | td_verror(td, EIO, "check_min_rate"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 614 | break; |
| 615 | } |
| 616 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 617 | if (td->o.thinktime) { |
Jens Axboe | 9c1f743 | 2007-01-03 20:43:19 +0100 | [diff] [blame] | 618 | unsigned long long b; |
| 619 | |
| 620 | b = td->io_blocks[0] + td->io_blocks[1]; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 621 | if (!(b % td->o.thinktime_blocks)) { |
Jens Axboe | 48097d5 | 2007-02-17 06:30:44 +0100 | [diff] [blame] | 622 | int left; |
| 623 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 624 | if (td->o.thinktime_spin) |
| 625 | __usec_sleep(td->o.thinktime_spin); |
Jens Axboe | 48097d5 | 2007-02-17 06:30:44 +0100 | [diff] [blame] | 626 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 627 | left = td->o.thinktime - td->o.thinktime_spin; |
Jens Axboe | 48097d5 | 2007-02-17 06:30:44 +0100 | [diff] [blame] | 628 | if (left) |
| 629 | usec_sleep(td, left); |
| 630 | } |
Jens Axboe | 9c1f743 | 2007-01-03 20:43:19 +0100 | [diff] [blame] | 631 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 632 | } |
| 633 | |
Shawn Lewis | aa31f1f | 2008-01-11 09:45:11 +0100 | [diff] [blame] | 634 | if (td->o.fill_device && td->error == ENOSPC) { |
| 635 | td->error = 0; |
| 636 | td->terminate = 1; |
| 637 | } |
Jens Axboe | 4d2413c | 2006-11-23 11:58:59 +0100 | [diff] [blame] | 638 | if (!td->error) { |
Jens Axboe | 3d7c391 | 2007-02-19 13:16:12 +0100 | [diff] [blame] | 639 | struct fio_file *f; |
| 640 | |
Jens Axboe | c01c039 | 2007-02-26 12:43:42 +0100 | [diff] [blame] | 641 | i = td->cur_depth; |
| 642 | if (i) |
| 643 | ret = io_u_queued_complete(td, i); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 644 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 645 | if (should_fsync(td) && td->o.end_fsync) { |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 646 | td_set_runstate(td, TD_FSYNCING); |
Jens Axboe | 3d7b485 | 2007-03-13 14:50:28 +0100 | [diff] [blame] | 647 | |
| 648 | for_each_file(td, f, i) { |
| 649 | if (!(f->flags & FIO_FILE_OPEN)) |
| 650 | continue; |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 651 | fio_io_sync(td, f); |
Jens Axboe | 3d7b485 | 2007-03-13 14:50:28 +0100 | [diff] [blame] | 652 | } |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 653 | } |
Jens Axboe | c01c039 | 2007-02-26 12:43:42 +0100 | [diff] [blame] | 654 | } else |
| 655 | cleanup_pending_aio(td); |
Jens Axboe | 2ba1c29 | 2008-02-01 13:16:38 +0100 | [diff] [blame] | 656 | |
| 657 | /* |
| 658 | * stop job if we failed doing any IO |
| 659 | */ |
| 660 | if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0) |
| 661 | td->done = 1; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 662 | } |
| 663 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 664 | static void cleanup_io_u(struct thread_data *td) |
| 665 | { |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 666 | struct flist_head *entry, *n; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 667 | struct io_u *io_u; |
| 668 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 669 | flist_for_each_safe(entry, n, &td->io_u_freelist) { |
| 670 | io_u = flist_entry(entry, struct io_u, list); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 671 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 672 | flist_del(&io_u->list); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 673 | free(io_u); |
| 674 | } |
| 675 | |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 676 | free_io_mem(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | static int init_io_u(struct thread_data *td) |
| 680 | { |
| 681 | struct io_u *io_u; |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 682 | unsigned int max_bs; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 683 | int i, max_units; |
| 684 | char *p; |
| 685 | |
Jens Axboe | 1e3c09a | 2008-02-04 10:48:13 +0100 | [diff] [blame] | 686 | max_units = td->o.iodepth; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 687 | max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]); |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 688 | td->orig_buffer_size = (unsigned long long) max_bs |
| 689 | * (unsigned long long) max_units; |
Jens Axboe | 74b025b | 2006-12-19 15:18:14 +0100 | [diff] [blame] | 690 | |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 691 | if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) { |
| 692 | unsigned long bs; |
| 693 | |
| 694 | bs = td->orig_buffer_size + td->o.hugepage_size - 1; |
| 695 | td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1); |
| 696 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 697 | |
Jens Axboe | c399064 | 2007-07-19 10:17:09 +0200 | [diff] [blame] | 698 | if (td->orig_buffer_size != (size_t) td->orig_buffer_size) { |
| 699 | log_err("fio: IO memory too large. Reduce max_bs or iodepth\n"); |
| 700 | return 1; |
| 701 | } |
| 702 | |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 703 | if (allocate_io_mem(td)) |
| 704 | return 1; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 705 | |
Jens Axboe | b3f378e | 2007-07-20 12:39:22 +0200 | [diff] [blame] | 706 | if (td->o.odirect) |
| 707 | p = ALIGN(td->orig_buffer); |
| 708 | else |
| 709 | p = td->orig_buffer; |
| 710 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 711 | for (i = 0; i < max_units; i++) { |
Jens Axboe | db1cd90 | 2007-04-26 13:47:07 +0200 | [diff] [blame] | 712 | if (td->terminate) |
| 713 | return 1; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 714 | io_u = malloc(sizeof(*io_u)); |
| 715 | memset(io_u, 0, sizeof(*io_u)); |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 716 | INIT_FLIST_HEAD(&io_u->list); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 717 | |
Jens Axboe | b4c5e1a | 2007-10-25 18:44:45 +0200 | [diff] [blame] | 718 | if (!(td->io_ops->flags & FIO_NOIO)) { |
| 719 | io_u->buf = p + max_bs * i; |
Jens Axboe | e9459e5 | 2007-04-17 15:46:32 +0200 | [diff] [blame] | 720 | |
Jens Axboe | 5973caf | 2008-05-21 19:52:35 +0200 | [diff] [blame] | 721 | if (td_write(td) && !td->o.refill_buffers) |
| 722 | io_u_fill_buffer(td, io_u, max_bs); |
Jens Axboe | b4c5e1a | 2007-10-25 18:44:45 +0200 | [diff] [blame] | 723 | } |
Jens Axboe | 6b9cea2 | 2006-10-30 17:00:24 +0100 | [diff] [blame] | 724 | |
Jens Axboe | b1ff340 | 2006-02-17 11:35:58 +0100 | [diff] [blame] | 725 | io_u->index = i; |
Jens Axboe | 0c6e751 | 2007-02-22 11:19:39 +0100 | [diff] [blame] | 726 | io_u->flags = IO_U_F_FREE; |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 727 | flist_add(&io_u->list, &td->io_u_freelist); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 728 | } |
| 729 | |
Jens Axboe | 433afcb | 2007-02-22 10:39:01 +0100 | [diff] [blame] | 730 | io_u_init_timeout(); |
| 731 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 732 | return 0; |
| 733 | } |
| 734 | |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 735 | static int switch_ioscheduler(struct thread_data *td) |
| 736 | { |
| 737 | char tmp[256], tmp2[128]; |
| 738 | FILE *f; |
| 739 | int ret; |
| 740 | |
Jens Axboe | ba0fbe1 | 2007-03-09 14:34:23 +0100 | [diff] [blame] | 741 | if (td->io_ops->flags & FIO_DISKLESSIO) |
Jens Axboe | f48b467 | 2006-10-27 11:30:07 +0200 | [diff] [blame] | 742 | return 0; |
| 743 | |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 744 | sprintf(tmp, "%s/queue/scheduler", td->sysfs_root); |
| 745 | |
| 746 | f = fopen(tmp, "r+"); |
| 747 | if (!f) { |
Jens Axboe | cbf5c1d | 2007-04-16 12:56:00 +0200 | [diff] [blame] | 748 | if (errno == ENOENT) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 749 | log_err("fio: os or kernel doesn't support IO scheduler" |
| 750 | " switching\n"); |
Jens Axboe | cbf5c1d | 2007-04-16 12:56:00 +0200 | [diff] [blame] | 751 | return 0; |
| 752 | } |
| 753 | td_verror(td, errno, "fopen iosched"); |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 754 | return 1; |
| 755 | } |
| 756 | |
| 757 | /* |
| 758 | * Set io scheduler. |
| 759 | */ |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 760 | ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f); |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 761 | if (ferror(f) || ret != 1) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 762 | td_verror(td, errno, "fwrite"); |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 763 | fclose(f); |
| 764 | return 1; |
| 765 | } |
| 766 | |
| 767 | rewind(f); |
| 768 | |
| 769 | /* |
| 770 | * Read back and check that the selected scheduler is now the default. |
| 771 | */ |
| 772 | ret = fread(tmp, 1, sizeof(tmp), f); |
| 773 | if (ferror(f) || ret < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 774 | td_verror(td, errno, "fread"); |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 775 | fclose(f); |
| 776 | return 1; |
| 777 | } |
| 778 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 779 | sprintf(tmp2, "[%s]", td->o.ioscheduler); |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 780 | if (!strstr(tmp, tmp2)) { |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 781 | log_err("fio: io scheduler %s not found\n", td->o.ioscheduler); |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 782 | td_verror(td, EINVAL, "iosched_switch"); |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 783 | fclose(f); |
| 784 | return 1; |
| 785 | } |
| 786 | |
| 787 | fclose(f); |
| 788 | return 0; |
| 789 | } |
| 790 | |
Jens Axboe | 492158c | 2007-05-24 10:32:47 +0200 | [diff] [blame] | 791 | static int keep_running(struct thread_data *td) |
| 792 | { |
| 793 | unsigned long long io_done; |
| 794 | |
Jens Axboe | 20e354e | 2007-07-23 14:36:16 +0200 | [diff] [blame] | 795 | if (td->done) |
| 796 | return 0; |
Jens Axboe | 492158c | 2007-05-24 10:32:47 +0200 | [diff] [blame] | 797 | if (td->o.time_based) |
| 798 | return 1; |
| 799 | if (td->o.loops) { |
| 800 | td->o.loops--; |
| 801 | return 1; |
| 802 | } |
| 803 | |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 804 | io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE] |
| 805 | + td->io_skip_bytes; |
Jens Axboe | 492158c | 2007-05-24 10:32:47 +0200 | [diff] [blame] | 806 | if (io_done < td->o.size) |
| 807 | return 1; |
| 808 | |
| 809 | return 0; |
| 810 | } |
| 811 | |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 812 | static int clear_io_state(struct thread_data *td) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 813 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 814 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 815 | unsigned int i; |
| 816 | int ret; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 817 | |
Jens Axboe | 756867b | 2007-03-06 15:19:24 +0100 | [diff] [blame] | 818 | td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 819 | td->this_io_bytes[0] = td->this_io_bytes[1] = 0; |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 820 | td->zone_bytes = 0; |
Jens Axboe | d7d3b49 | 2007-03-12 08:02:10 +0100 | [diff] [blame] | 821 | td->rate_bytes = 0; |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 822 | td->rate_blocks = 0; |
Jens Axboe | 38d77ca | 2007-03-21 14:20:20 +0100 | [diff] [blame] | 823 | td->rw_end_set[0] = td->rw_end_set[1] = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 824 | |
Jens Axboe | c1324df | 2007-02-19 19:06:41 +0100 | [diff] [blame] | 825 | td->last_was_sync = 0; |
| 826 | |
Jens Axboe | 2ba1c29 | 2008-02-01 13:16:38 +0100 | [diff] [blame] | 827 | /* |
| 828 | * reset file done count if we are to start over |
| 829 | */ |
| 830 | if (td->o.time_based || td->o.loops) |
Jens Axboe | 48f5abd | 2007-07-20 13:25:04 +0200 | [diff] [blame] | 831 | td->nr_done_files = 0; |
Jens Axboe | 281f9b6 | 2007-05-23 09:34:42 +0200 | [diff] [blame] | 832 | |
Jens Axboe | 24ffd2c | 2008-03-01 15:47:08 +0100 | [diff] [blame] | 833 | close_files(td); |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 834 | |
| 835 | ret = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 836 | for_each_file(td, f, i) { |
Jens Axboe | 281f9b6 | 2007-05-23 09:34:42 +0200 | [diff] [blame] | 837 | f->flags &= ~FIO_FILE_DONE; |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 838 | ret = td_io_open_file(td, f); |
| 839 | if (ret) |
| 840 | break; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 841 | } |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 842 | |
| 843 | return ret; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 844 | } |
| 845 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 846 | /* |
| 847 | * Entry point for the thread based jobs. The process based jobs end up |
| 848 | * here as well, after a little setup. |
| 849 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 850 | static void *thread_main(void *data) |
| 851 | { |
Shawn Lewis | 1092731 | 2007-11-21 09:38:13 +0100 | [diff] [blame] | 852 | unsigned long long runtime[2], elapsed; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 853 | struct thread_data *td = data; |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 854 | int clear_state; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 855 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 856 | if (!td->o.use_thread) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 857 | setsid(); |
| 858 | |
| 859 | td->pid = getpid(); |
| 860 | |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 861 | dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid); |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 862 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 863 | INIT_FLIST_HEAD(&td->io_u_freelist); |
| 864 | INIT_FLIST_HEAD(&td->io_u_busylist); |
| 865 | INIT_FLIST_HEAD(&td->io_u_requeues); |
| 866 | INIT_FLIST_HEAD(&td->io_log_list); |
| 867 | INIT_FLIST_HEAD(&td->io_hist_list); |
Jens Axboe | bb5d7d0 | 2007-03-27 10:21:25 +0200 | [diff] [blame] | 868 | td->io_hist_tree = RB_ROOT; |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 869 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 870 | td_set_runstate(td, TD_INITIALIZED); |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 871 | fio_mutex_up(startup_mutex); |
| 872 | fio_mutex_down(td->mutex); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 873 | |
Jens Axboe | 37f5687 | 2007-03-09 12:42:35 +0100 | [diff] [blame] | 874 | /* |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 875 | * the ->mutex mutex is now no longer used, close it to avoid |
Jens Axboe | 37f5687 | 2007-03-09 12:42:35 +0100 | [diff] [blame] | 876 | * eating a file descriptor |
| 877 | */ |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 878 | fio_mutex_remove(td->mutex); |
Jens Axboe | 37f5687 | 2007-03-09 12:42:35 +0100 | [diff] [blame] | 879 | |
Jens Axboe | d84f8d4 | 2007-05-16 08:47:46 +0200 | [diff] [blame] | 880 | /* |
| 881 | * May alter parameters that init_io_u() will use, so we need to |
| 882 | * do this first. |
| 883 | */ |
| 884 | if (init_iolog(td)) |
| 885 | goto err; |
| 886 | |
Jens Axboe | db1cd90 | 2007-04-26 13:47:07 +0200 | [diff] [blame] | 887 | if (init_io_u(td)) |
| 888 | goto err; |
| 889 | |
Jens Axboe | 375b269 | 2007-05-22 09:13:02 +0200 | [diff] [blame] | 890 | if (td->o.cpumask_set && fio_setaffinity(td) == -1) { |
Jens Axboe | db1cd90 | 2007-04-26 13:47:07 +0200 | [diff] [blame] | 891 | td_verror(td, errno, "cpu_set_affinity"); |
| 892 | goto err; |
| 893 | } |
| 894 | |
Jens Axboe | ac68478 | 2007-11-08 08:29:07 +0100 | [diff] [blame] | 895 | if (td->ioprio_set) { |
Jens Axboe | db1cd90 | 2007-04-26 13:47:07 +0200 | [diff] [blame] | 896 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) { |
| 897 | td_verror(td, errno, "ioprio_set"); |
| 898 | goto err; |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | if (nice(td->o.nice) == -1) { |
| 903 | td_verror(td, errno, "nice"); |
| 904 | goto err; |
| 905 | } |
| 906 | |
| 907 | if (td->o.ioscheduler && switch_ioscheduler(td)) |
| 908 | goto err; |
| 909 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 910 | if (!td->o.create_serialize && setup_files(td)) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 911 | goto err; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 912 | |
| 913 | if (td_io_init(td)) |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 914 | goto err; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 915 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 916 | if (open_files(td)) |
Jens Axboe | 7d6c528 | 2007-02-05 10:52:10 +0100 | [diff] [blame] | 917 | goto err; |
| 918 | |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 919 | if (init_random_map(td)) |
| 920 | goto err; |
| 921 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 922 | if (td->o.exec_prerun) { |
| 923 | if (system(td->o.exec_prerun) < 0) |
Jens Axboe | 69cfd7e | 2007-02-07 13:58:53 +0100 | [diff] [blame] | 924 | goto err; |
| 925 | } |
Jens Axboe | 4e0ba8a | 2006-06-06 09:36:28 +0200 | [diff] [blame] | 926 | |
Jens Axboe | 6900899 | 2006-11-24 13:12:56 +0100 | [diff] [blame] | 927 | fio_gettime(&td->epoch, NULL); |
Jens Axboe | 433afcb | 2007-02-22 10:39:01 +0100 | [diff] [blame] | 928 | memcpy(&td->timeout_end, &td->epoch, sizeof(td->epoch)); |
Jens Axboe | 756867b | 2007-03-06 15:19:24 +0100 | [diff] [blame] | 929 | getrusage(RUSAGE_SELF, &td->ts.ru_start); |
Jens Axboe | 6900899 | 2006-11-24 13:12:56 +0100 | [diff] [blame] | 930 | |
| 931 | runtime[0] = runtime[1] = 0; |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 932 | clear_state = 0; |
Jens Axboe | 492158c | 2007-05-24 10:32:47 +0200 | [diff] [blame] | 933 | while (keep_running(td)) { |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 934 | fio_gettime(&td->start, NULL); |
Jens Axboe | 756867b | 2007-03-06 15:19:24 +0100 | [diff] [blame] | 935 | memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start)); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 936 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 937 | if (td->o.ratemin) |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 938 | memcpy(&td->lastrate, &td->ts.stat_sample_time, |
| 939 | sizeof(td->lastrate)); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 940 | |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 941 | if (clear_state && clear_io_state(td)) |
| 942 | break; |
| 943 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 944 | prune_io_piece_log(td); |
| 945 | |
Jens Axboe | ba0fbe1 | 2007-03-09 14:34:23 +0100 | [diff] [blame] | 946 | do_io(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 947 | |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 948 | clear_state = 1; |
| 949 | |
Jens Axboe | 38d77ca | 2007-03-21 14:20:20 +0100 | [diff] [blame] | 950 | if (td_read(td) && td->io_bytes[DDIR_READ]) { |
| 951 | if (td->rw_end_set[DDIR_READ]) |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 952 | elapsed = utime_since(&td->start, |
| 953 | &td->rw_end[DDIR_READ]); |
Jens Axboe | 38d77ca | 2007-03-21 14:20:20 +0100 | [diff] [blame] | 954 | else |
| 955 | elapsed = utime_since_now(&td->start); |
| 956 | |
| 957 | runtime[DDIR_READ] += elapsed; |
| 958 | } |
| 959 | if (td_write(td) && td->io_bytes[DDIR_WRITE]) { |
| 960 | if (td->rw_end_set[DDIR_WRITE]) |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 961 | elapsed = utime_since(&td->start, |
| 962 | &td->rw_end[DDIR_WRITE]); |
Jens Axboe | 38d77ca | 2007-03-21 14:20:20 +0100 | [diff] [blame] | 963 | else |
| 964 | elapsed = utime_since_now(&td->start); |
| 965 | |
| 966 | runtime[DDIR_WRITE] += elapsed; |
| 967 | } |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 968 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 969 | if (td->error || td->terminate) |
| 970 | break; |
| 971 | |
Shawn Lewis | e84c73a | 2007-08-02 22:19:32 +0200 | [diff] [blame] | 972 | if (!td->o.do_verify || |
| 973 | td->o.verify == VERIFY_NONE || |
Jens Axboe | b67740d | 2007-07-26 12:22:30 +0200 | [diff] [blame] | 974 | (td->io_ops->flags & FIO_UNIDIR)) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 975 | continue; |
| 976 | |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 977 | if (clear_io_state(td)) |
| 978 | break; |
| 979 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 980 | fio_gettime(&td->start, NULL); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 981 | |
| 982 | do_verify(td); |
| 983 | |
Jens Axboe | 6900899 | 2006-11-24 13:12:56 +0100 | [diff] [blame] | 984 | runtime[DDIR_READ] += utime_since_now(&td->start); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 985 | |
| 986 | if (td->error || td->terminate) |
| 987 | break; |
| 988 | } |
| 989 | |
Jens Axboe | 36dff96 | 2006-11-24 13:29:20 +0100 | [diff] [blame] | 990 | update_rusage_stat(td); |
ljzhang,Yaxin Hu,Jianchao Tang | 47f0cc4 | 2007-07-26 11:00:29 +0200 | [diff] [blame] | 991 | td->ts.runtime[0] = (runtime[0] + 999) / 1000; |
| 992 | td->ts.runtime[1] = (runtime[1] + 999) / 1000; |
Jens Axboe | 756867b | 2007-03-06 15:19:24 +0100 | [diff] [blame] | 993 | td->ts.total_run_time = mtime_since_now(&td->epoch); |
| 994 | td->ts.io_bytes[0] = td->io_bytes[0]; |
| 995 | td->ts.io_bytes[1] = td->io_bytes[1]; |
Jens Axboe | 6900899 | 2006-11-24 13:12:56 +0100 | [diff] [blame] | 996 | |
Jens Axboe | 756867b | 2007-03-06 15:19:24 +0100 | [diff] [blame] | 997 | if (td->ts.bw_log) |
| 998 | finish_log(td, td->ts.bw_log, "bw"); |
| 999 | if (td->ts.slat_log) |
| 1000 | finish_log(td, td->ts.slat_log, "slat"); |
| 1001 | if (td->ts.clat_log) |
| 1002 | finish_log(td, td->ts.clat_log, "clat"); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1003 | if (td->o.exec_postrun) { |
| 1004 | if (system(td->o.exec_postrun) < 0) |
| 1005 | log_err("fio: postrun %s failed\n", td->o.exec_postrun); |
Jens Axboe | 69cfd7e | 2007-02-07 13:58:53 +0100 | [diff] [blame] | 1006 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1007 | |
| 1008 | if (exitall_on_terminate) |
Jens Axboe | 390c40e | 2007-03-05 12:35:29 +0100 | [diff] [blame] | 1009 | terminate_threads(td->groupid); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1010 | |
| 1011 | err: |
Jens Axboe | 5bf13a5 | 2007-02-17 01:51:47 +0100 | [diff] [blame] | 1012 | if (td->error) |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 1013 | printf("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error, |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1014 | td->verror); |
Jens Axboe | 24ffd2c | 2008-03-01 15:47:08 +0100 | [diff] [blame] | 1015 | close_and_free_files(td); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 1016 | close_ioengine(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1017 | cleanup_io_u(td); |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 1018 | |
| 1019 | /* |
| 1020 | * do this very late, it will log file closing as well |
| 1021 | */ |
| 1022 | if (td->o.write_iolog_file) |
| 1023 | write_iolog_close(td); |
| 1024 | |
Jens Axboe | d23bb32 | 2007-03-23 15:57:56 +0100 | [diff] [blame] | 1025 | options_mem_free(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1026 | td_set_runstate(td, TD_EXITED); |
Jens Axboe | 43d7680 | 2007-02-21 16:35:29 +0100 | [diff] [blame] | 1027 | return (void *) (unsigned long) td->error; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1028 | } |
| 1029 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1030 | /* |
| 1031 | * We cannot pass the td data into a forked process, so attach the td and |
| 1032 | * pass it to the thread worker. |
| 1033 | */ |
Jens Axboe | a641814 | 2007-02-17 04:47:18 +0100 | [diff] [blame] | 1034 | static int fork_main(int shmid, int offset) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1035 | { |
| 1036 | struct thread_data *td; |
Jens Axboe | a641814 | 2007-02-17 04:47:18 +0100 | [diff] [blame] | 1037 | void *data, *ret; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1038 | |
| 1039 | data = shmat(shmid, NULL, 0); |
| 1040 | if (data == (void *) -1) { |
Jens Axboe | a641814 | 2007-02-17 04:47:18 +0100 | [diff] [blame] | 1041 | int __err = errno; |
| 1042 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1043 | perror("shmat"); |
Jens Axboe | a641814 | 2007-02-17 04:47:18 +0100 | [diff] [blame] | 1044 | return __err; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | td = data + offset * sizeof(struct thread_data); |
Jens Axboe | a641814 | 2007-02-17 04:47:18 +0100 | [diff] [blame] | 1048 | ret = thread_main(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1049 | shmdt(data); |
Jens Axboe | 43d7680 | 2007-02-21 16:35:29 +0100 | [diff] [blame] | 1050 | return (int) (unsigned long) ret; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1051 | } |
| 1052 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1053 | /* |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1054 | * Run over the job map and reap the threads that have exited, if any. |
| 1055 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1056 | static void reap_threads(int *nr_running, int *t_rate, int *m_rate) |
| 1057 | { |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 1058 | struct thread_data *td; |
Jens Axboe | 1f809d1 | 2007-10-25 18:34:02 +0200 | [diff] [blame] | 1059 | int i, cputhreads, realthreads, pending, status, ret; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1060 | |
| 1061 | /* |
| 1062 | * reap exited threads (TD_EXITED -> TD_REAPED) |
| 1063 | */ |
Jens Axboe | 1f809d1 | 2007-10-25 18:34:02 +0200 | [diff] [blame] | 1064 | realthreads = pending = cputhreads = 0; |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 1065 | for_each_td(td, i) { |
Jens Axboe | 3707f45 | 2007-02-22 12:26:57 +0100 | [diff] [blame] | 1066 | int flags = 0; |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1067 | |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 1068 | /* |
| 1069 | * ->io_ops is NULL for a thread that has closed its |
| 1070 | * io engine |
| 1071 | */ |
Jens Axboe | ba0fbe1 | 2007-03-09 14:34:23 +0100 | [diff] [blame] | 1072 | if (td->io_ops && !strcmp(td->io_ops->name, "cpuio")) |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 1073 | cputhreads++; |
Jens Axboe | 1f809d1 | 2007-10-25 18:34:02 +0200 | [diff] [blame] | 1074 | else |
| 1075 | realthreads++; |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 1076 | |
Aaron Carroll | a1dedc1 | 2008-02-20 09:14:12 +0100 | [diff] [blame] | 1077 | if (!td->pid) { |
| 1078 | pending++; |
| 1079 | continue; |
| 1080 | } |
| 1081 | if (td->runstate == TD_REAPED) |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1082 | continue; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1083 | if (td->o.use_thread) { |
Jens Axboe | 3707f45 | 2007-02-22 12:26:57 +0100 | [diff] [blame] | 1084 | if (td->runstate == TD_EXITED) { |
| 1085 | td_set_runstate(td, TD_REAPED); |
| 1086 | goto reaped; |
| 1087 | } |
| 1088 | continue; |
| 1089 | } |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1090 | |
| 1091 | flags = WNOHANG; |
| 1092 | if (td->runstate == TD_EXITED) |
| 1093 | flags = 0; |
| 1094 | |
| 1095 | /* |
| 1096 | * check if someone quit or got killed in an unusual way |
| 1097 | */ |
| 1098 | ret = waitpid(td->pid, &status, flags); |
Jens Axboe | 3707f45 | 2007-02-22 12:26:57 +0100 | [diff] [blame] | 1099 | if (ret < 0) { |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1100 | if (errno == ECHILD) { |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 1101 | log_err("fio: pid=%d disappeared %d\n", |
| 1102 | (int) td->pid, td->runstate); |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1103 | td_set_runstate(td, TD_REAPED); |
| 1104 | goto reaped; |
| 1105 | } |
| 1106 | perror("waitpid"); |
| 1107 | } else if (ret == td->pid) { |
| 1108 | if (WIFSIGNALED(status)) { |
Jens Axboe | fab6aa7 | 2007-02-17 06:19:24 +0100 | [diff] [blame] | 1109 | int sig = WTERMSIG(status); |
| 1110 | |
Jens Axboe | d924494 | 2007-03-05 12:32:32 +0100 | [diff] [blame] | 1111 | if (sig != SIGQUIT) |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1112 | log_err("fio: pid=%d, got signal=%d\n", |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 1113 | (int) td->pid, sig); |
Jens Axboe | fab6aa7 | 2007-02-17 06:19:24 +0100 | [diff] [blame] | 1114 | td_set_runstate(td, TD_REAPED); |
| 1115 | goto reaped; |
| 1116 | } |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1117 | if (WIFEXITED(status)) { |
| 1118 | if (WEXITSTATUS(status) && !td->error) |
| 1119 | td->error = WEXITSTATUS(status); |
Jens Axboe | fab6aa7 | 2007-02-17 06:19:24 +0100 | [diff] [blame] | 1120 | |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1121 | td_set_runstate(td, TD_REAPED); |
| 1122 | goto reaped; |
Jens Axboe | a641814 | 2007-02-17 04:47:18 +0100 | [diff] [blame] | 1123 | } |
| 1124 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1125 | |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1126 | /* |
| 1127 | * thread is not dead, continue |
| 1128 | */ |
gurudas pai | e48676b | 2007-04-11 12:44:27 +0200 | [diff] [blame] | 1129 | pending++; |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1130 | continue; |
Jens Axboe | fab6aa7 | 2007-02-17 06:19:24 +0100 | [diff] [blame] | 1131 | reaped: |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1132 | (*nr_running)--; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1133 | (*m_rate) -= td->o.ratemin; |
| 1134 | (*t_rate) -= td->o.rate; |
Jens Axboe | 6f88bcb | 2008-03-19 10:29:07 +0100 | [diff] [blame] | 1135 | if (!td->pid) |
| 1136 | pending--; |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1137 | |
| 1138 | if (td->error) |
| 1139 | exit_value++; |
Jens Axboe | d3eeeab | 2008-04-06 12:19:05 +0200 | [diff] [blame] | 1140 | |
| 1141 | done_secs += mtime_since_now(&td->epoch) / 1000; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1142 | } |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 1143 | |
Jens Axboe | 1f809d1 | 2007-10-25 18:34:02 +0200 | [diff] [blame] | 1144 | if (*nr_running == cputhreads && !pending && realthreads) |
Jens Axboe | 390c40e | 2007-03-05 12:35:29 +0100 | [diff] [blame] | 1145 | terminate_threads(TERMINATE_ALL); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1146 | } |
| 1147 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1148 | /* |
| 1149 | * Main function for kicking off and reaping jobs, as needed. |
| 1150 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1151 | static void run_threads(void) |
| 1152 | { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1153 | struct thread_data *td; |
| 1154 | unsigned long spent; |
| 1155 | int i, todo, nr_running, m_rate, t_rate, nr_started; |
Jens Axboe | fcb6ade | 2006-05-31 12:14:35 +0200 | [diff] [blame] | 1156 | |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 1157 | if (fio_pin_memory()) |
| 1158 | return; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1159 | |
Jens Axboe | c6ae0a5 | 2006-06-12 11:04:44 +0200 | [diff] [blame] | 1160 | if (!terse_output) { |
Jens Axboe | 9cedf16 | 2007-03-12 11:29:30 +0100 | [diff] [blame] | 1161 | printf("Starting "); |
| 1162 | if (nr_thread) |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1163 | printf("%d thread%s", nr_thread, |
| 1164 | nr_thread > 1 ? "s" : ""); |
Jens Axboe | 9cedf16 | 2007-03-12 11:29:30 +0100 | [diff] [blame] | 1165 | if (nr_process) { |
| 1166 | if (nr_thread) |
| 1167 | printf(" and "); |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1168 | printf("%d process%s", nr_process, |
| 1169 | nr_process > 1 ? "es" : ""); |
Jens Axboe | 9cedf16 | 2007-03-12 11:29:30 +0100 | [diff] [blame] | 1170 | } |
| 1171 | printf("\n"); |
Jens Axboe | c6ae0a5 | 2006-06-12 11:04:44 +0200 | [diff] [blame] | 1172 | fflush(stdout); |
| 1173 | } |
Jens Axboe | c04f7ec | 2006-05-31 10:13:16 +0200 | [diff] [blame] | 1174 | |
Jens Axboe | 697a606 | 2008-05-31 00:04:45 +0200 | [diff] [blame] | 1175 | set_sig_handlers(); |
Jens Axboe | 4efa970 | 2006-05-31 11:56:49 +0200 | [diff] [blame] | 1176 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1177 | todo = thread_number; |
| 1178 | nr_running = 0; |
| 1179 | nr_started = 0; |
| 1180 | m_rate = t_rate = 0; |
| 1181 | |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 1182 | for_each_td(td, i) { |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 1183 | print_status_init(td->thread_number - 1); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1184 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1185 | if (!td->o.create_serialize) { |
Jens Axboe | 380cf26 | 2007-01-11 14:01:27 +0100 | [diff] [blame] | 1186 | init_disk_util(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1187 | continue; |
Jens Axboe | 380cf26 | 2007-01-11 14:01:27 +0100 | [diff] [blame] | 1188 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1189 | |
| 1190 | /* |
| 1191 | * do file setup here so it happens sequentially, |
| 1192 | * we don't want X number of threads getting their |
| 1193 | * client data interspersed on disk |
| 1194 | */ |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 1195 | if (setup_files(td)) { |
Jens Axboe | 5bf13a5 | 2007-02-17 01:51:47 +0100 | [diff] [blame] | 1196 | exit_value++; |
| 1197 | if (td->error) |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 1198 | log_err("fio: pid=%d, err=%d/%s\n", |
| 1199 | (int) td->pid, td->error, td->verror); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1200 | td_set_runstate(td, TD_REAPED); |
| 1201 | todo--; |
Jens Axboe | c69e887 | 2008-03-01 18:50:48 +0100 | [diff] [blame] | 1202 | } else { |
| 1203 | struct fio_file *f; |
| 1204 | unsigned int i; |
| 1205 | |
| 1206 | /* |
| 1207 | * for sharing to work, each job must always open |
| 1208 | * its own files. so close them, if we opened them |
| 1209 | * for creation |
| 1210 | */ |
| 1211 | for_each_file(td, f, i) |
| 1212 | td_io_close_file(td, f); |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1213 | } |
Jens Axboe | 380cf26 | 2007-01-11 14:01:27 +0100 | [diff] [blame] | 1214 | |
| 1215 | init_disk_util(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1216 | } |
| 1217 | |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1218 | set_genesis_time(); |
| 1219 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1220 | while (todo) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1221 | struct thread_data *map[MAX_JOBS]; |
| 1222 | struct timeval this_start; |
| 1223 | int this_jobs = 0, left; |
| 1224 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1225 | /* |
| 1226 | * create threads (TD_NOT_CREATED -> TD_CREATED) |
| 1227 | */ |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 1228 | for_each_td(td, i) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1229 | if (td->runstate != TD_NOT_CREATED) |
| 1230 | continue; |
| 1231 | |
| 1232 | /* |
| 1233 | * never got a chance to start, killed by other |
| 1234 | * thread for some reason |
| 1235 | */ |
| 1236 | if (td->terminate) { |
| 1237 | todo--; |
| 1238 | continue; |
| 1239 | } |
| 1240 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1241 | if (td->o.start_delay) { |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 1242 | spent = mtime_since_genesis(); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1243 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1244 | if (td->o.start_delay * 1000 > spent) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1245 | continue; |
| 1246 | } |
| 1247 | |
Jens Axboe | 6f88bcb | 2008-03-19 10:29:07 +0100 | [diff] [blame] | 1248 | if (td->o.stonewall && (nr_started || nr_running)) { |
| 1249 | dprint(FD_PROCESS, "%s: stonewall wait\n", |
| 1250 | td->o.name); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1251 | break; |
Jens Axboe | 6f88bcb | 2008-03-19 10:29:07 +0100 | [diff] [blame] | 1252 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1253 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1254 | /* |
| 1255 | * Set state to created. Thread will transition |
| 1256 | * to TD_INITIALIZED when it's done setting up. |
| 1257 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1258 | td_set_runstate(td, TD_CREATED); |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1259 | map[this_jobs++] = td; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1260 | nr_started++; |
| 1261 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1262 | if (td->o.use_thread) { |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 1263 | dprint(FD_PROCESS, "will pthread_create\n"); |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1264 | if (pthread_create(&td->thread, NULL, |
| 1265 | thread_main, td)) { |
Jens Axboe | f4705a7 | 2008-03-10 10:52:22 +0100 | [diff] [blame] | 1266 | perror("pthread_create"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1267 | nr_started--; |
Jens Axboe | c8bb6fa | 2007-03-12 11:03:04 +0100 | [diff] [blame] | 1268 | break; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1269 | } |
Jens Axboe | f4705a7 | 2008-03-10 10:52:22 +0100 | [diff] [blame] | 1270 | if (pthread_detach(td->thread) < 0) |
| 1271 | perror("pthread_detach"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1272 | } else { |
Jens Axboe | 5e1d306 | 2008-05-23 11:55:53 +0200 | [diff] [blame] | 1273 | pid_t pid; |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 1274 | dprint(FD_PROCESS, "will fork\n"); |
Jens Axboe | 5e1d306 | 2008-05-23 11:55:53 +0200 | [diff] [blame] | 1275 | pid = fork(); |
| 1276 | if (!pid) { |
Jens Axboe | a641814 | 2007-02-17 04:47:18 +0100 | [diff] [blame] | 1277 | int ret = fork_main(shm_id, i); |
| 1278 | |
Jens Axboe | 5e1d306 | 2008-05-23 11:55:53 +0200 | [diff] [blame] | 1279 | _exit(ret); |
| 1280 | } else if (i == fio_debug_jobno) |
| 1281 | *fio_debug_jobp = pid; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1282 | } |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 1283 | fio_mutex_down(startup_mutex); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | /* |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1287 | * Wait for the started threads to transition to |
| 1288 | * TD_INITIALIZED. |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1289 | */ |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 1290 | fio_gettime(&this_start, NULL); |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1291 | left = this_jobs; |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame] | 1292 | while (left && !fio_abort) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1293 | if (mtime_since_now(&this_start) > JOB_START_TIMEOUT) |
| 1294 | break; |
| 1295 | |
| 1296 | usleep(100000); |
| 1297 | |
| 1298 | for (i = 0; i < this_jobs; i++) { |
| 1299 | td = map[i]; |
| 1300 | if (!td) |
| 1301 | continue; |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 1302 | if (td->runstate == TD_INITIALIZED) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1303 | map[i] = NULL; |
| 1304 | left--; |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 1305 | } else if (td->runstate >= TD_EXITED) { |
| 1306 | map[i] = NULL; |
| 1307 | left--; |
| 1308 | todo--; |
| 1309 | nr_running++; /* work-around... */ |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1310 | } |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | if (left) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 1315 | log_err("fio: %d jobs failed to start\n", left); |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1316 | for (i = 0; i < this_jobs; i++) { |
| 1317 | td = map[i]; |
| 1318 | if (!td) |
| 1319 | continue; |
| 1320 | kill(td->pid, SIGTERM); |
| 1321 | } |
| 1322 | break; |
| 1323 | } |
| 1324 | |
| 1325 | /* |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 1326 | * start created threads (TD_INITIALIZED -> TD_RUNNING). |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1327 | */ |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 1328 | for_each_td(td, i) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1329 | if (td->runstate != TD_INITIALIZED) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1330 | continue; |
| 1331 | |
| 1332 | td_set_runstate(td, TD_RUNNING); |
| 1333 | nr_running++; |
| 1334 | nr_started--; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1335 | m_rate += td->o.ratemin; |
| 1336 | t_rate += td->o.rate; |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1337 | todo--; |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 1338 | fio_mutex_up(td->mutex); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | reap_threads(&nr_running, &t_rate, &m_rate); |
| 1342 | |
| 1343 | if (todo) |
| 1344 | usleep(100000); |
| 1345 | } |
| 1346 | |
| 1347 | while (nr_running) { |
| 1348 | reap_threads(&nr_running, &t_rate, &m_rate); |
| 1349 | usleep(10000); |
| 1350 | } |
| 1351 | |
| 1352 | update_io_ticks(); |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 1353 | fio_unpin_memory(); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1354 | } |
| 1355 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1356 | int main(int argc, char *argv[]) |
| 1357 | { |
Jens Axboe | 29d610e | 2007-02-06 13:25:33 +0100 | [diff] [blame] | 1358 | long ps; |
| 1359 | |
Jens Axboe | 2e5cdb1 | 2008-03-01 18:52:49 +0100 | [diff] [blame] | 1360 | sinit(); |
| 1361 | |
Jens Axboe | dbe1125 | 2007-02-11 00:19:51 +0100 | [diff] [blame] | 1362 | /* |
| 1363 | * We need locale for number printing, if it isn't set then just |
| 1364 | * go with the US format. |
| 1365 | */ |
| 1366 | if (!getenv("LC_NUMERIC")) |
| 1367 | setlocale(LC_NUMERIC, "en_US"); |
| 1368 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1369 | if (parse_options(argc, argv)) |
| 1370 | return 1; |
| 1371 | |
Jens Axboe | 4b472fa | 2007-04-04 11:04:34 +0200 | [diff] [blame] | 1372 | if (!thread_number) |
| 1373 | return 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1374 | |
Jens Axboe | 29d610e | 2007-02-06 13:25:33 +0100 | [diff] [blame] | 1375 | ps = sysconf(_SC_PAGESIZE); |
| 1376 | if (ps < 0) { |
| 1377 | log_err("Failed to get page size\n"); |
| 1378 | return 1; |
| 1379 | } |
| 1380 | |
Jens Axboe | cfc99db | 2007-03-14 10:34:47 +0100 | [diff] [blame] | 1381 | page_size = ps; |
Jens Axboe | 29d610e | 2007-02-06 13:25:33 +0100 | [diff] [blame] | 1382 | page_mask = ps - 1; |
| 1383 | |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 1384 | if (write_bw_log) { |
| 1385 | setup_log(&agg_io_log[DDIR_READ]); |
| 1386 | setup_log(&agg_io_log[DDIR_WRITE]); |
| 1387 | } |
| 1388 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 1389 | startup_mutex = fio_mutex_init(0); |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 1390 | |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1391 | set_genesis_time(); |
| 1392 | |
Jens Axboe | ccb0fa2 | 2008-05-30 23:18:00 +0200 | [diff] [blame] | 1393 | status_timer_arm(); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1394 | |
| 1395 | run_threads(); |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame] | 1396 | |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 1397 | if (!fio_abort) { |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame] | 1398 | show_run_stats(); |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 1399 | if (write_bw_log) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1400 | __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log"); |
| 1401 | __finish_log(agg_io_log[DDIR_WRITE], |
| 1402 | "agg-write_bw.log"); |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 1403 | } |
| 1404 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1405 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 1406 | fio_mutex_remove(startup_mutex); |
Jens Axboe | 437c9b7 | 2007-02-13 18:20:37 +0100 | [diff] [blame] | 1407 | return exit_value; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1408 | } |