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 | 4f5af7b | 2009-06-03 08:45:40 +0200 | [diff] [blame] | 40 | #include "verify.h" |
Jens Axboe | 7c9b1bc | 2009-06-03 09:25:57 +0200 | [diff] [blame] | 41 | #include "diskutil.h" |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame^] | 42 | #include "cgroup.h" |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 43 | |
Jens Axboe | cfc99db | 2007-03-14 10:34:47 +0100 | [diff] [blame] | 44 | unsigned long page_mask; |
| 45 | unsigned long page_size; |
Jens Axboe | d529ee1 | 2009-07-01 10:33:03 +0200 | [diff] [blame] | 46 | |
| 47 | #define PAGE_ALIGN(buf) \ |
Jens Axboe | 29d610e | 2007-02-06 13:25:33 +0100 | [diff] [blame] | 48 | (char *) (((unsigned long) (buf) + page_mask) & ~page_mask) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 49 | |
| 50 | int groupid = 0; |
| 51 | int thread_number = 0; |
Jens Axboe | 9cedf16 | 2007-03-12 11:29:30 +0100 | [diff] [blame] | 52 | int nr_process = 0; |
| 53 | int nr_thread = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 54 | int shm_id = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 55 | int temp_stall_ts; |
Jens Axboe | d3eeeab | 2008-04-06 12:19:05 +0200 | [diff] [blame] | 56 | unsigned long done_secs = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 57 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 58 | static struct fio_mutex *startup_mutex; |
Fabio Checconi | d2bbb8d | 2009-05-18 12:54:50 +0200 | [diff] [blame] | 59 | static struct fio_mutex *writeout_mutex; |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame] | 60 | static volatile int fio_abort; |
Jens Axboe | 437c9b7 | 2007-02-13 18:20:37 +0100 | [diff] [blame] | 61 | static int exit_value; |
Jens Axboe | ccb0fa2 | 2008-05-30 23:18:00 +0200 | [diff] [blame] | 62 | static struct itimerval itimer; |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 63 | static pthread_t gtod_thread; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 64 | |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 65 | struct io_log *agg_io_log[2]; |
| 66 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 67 | #define TERMINATE_ALL (-1) |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 68 | #define JOB_START_TIMEOUT (5 * 1000) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 69 | |
Jens Axboe | b29ee5b | 2008-09-11 10:17:26 +0200 | [diff] [blame] | 70 | void td_set_runstate(struct thread_data *td, int runstate) |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame] | 71 | { |
Jens Axboe | d8fab1b | 2008-03-06 10:25:17 +0100 | [diff] [blame] | 72 | if (td->runstate == runstate) |
| 73 | return; |
| 74 | |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 75 | dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", (int) td->pid, |
| 76 | td->runstate, runstate); |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame] | 77 | td->runstate = runstate; |
| 78 | } |
| 79 | |
Jens Axboe | 390c40e | 2007-03-05 12:35:29 +0100 | [diff] [blame] | 80 | static void terminate_threads(int group_id) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 81 | { |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 82 | struct thread_data *td; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 83 | int i; |
| 84 | |
Jens Axboe | 6f88bcb | 2008-03-19 10:29:07 +0100 | [diff] [blame] | 85 | dprint(FD_PROCESS, "terminate group_id=%d\n", group_id); |
| 86 | |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 87 | for_each_td(td, i) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 88 | if (group_id == TERMINATE_ALL || groupid == td->groupid) { |
Jens Axboe | 6f88bcb | 2008-03-19 10:29:07 +0100 | [diff] [blame] | 89 | dprint(FD_PROCESS, "setting terminate on %s/%d\n", |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 90 | td->o.name, (int) td->pid); |
Jens Axboe | ad830ed | 2008-02-18 21:11:24 +0100 | [diff] [blame] | 91 | td->terminate = 1; |
| 92 | td->o.start_delay = 0; |
| 93 | |
Jens Axboe | 7a93ab1 | 2007-04-18 12:25:41 +0200 | [diff] [blame] | 94 | /* |
| 95 | * if the thread is running, just let it exit |
| 96 | */ |
| 97 | if (td->runstate < TD_RUNNING) |
Jens Axboe | c1302d4 | 2007-03-05 20:18:31 +0100 | [diff] [blame] | 98 | kill(td->pid, SIGQUIT); |
Jens Axboe | f63f7f3 | 2008-03-01 15:25:24 +0100 | [diff] [blame] | 99 | else { |
| 100 | struct ioengine_ops *ops = td->io_ops; |
| 101 | |
| 102 | if (ops && (ops->flags & FIO_SIGQUIT)) |
| 103 | kill(td->pid, SIGQUIT); |
| 104 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
Jens Axboe | ccb0fa2 | 2008-05-30 23:18:00 +0200 | [diff] [blame] | 109 | static void status_timer_arm(void) |
| 110 | { |
| 111 | itimer.it_value.tv_sec = 0; |
| 112 | itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000; |
| 113 | setitimer(ITIMER_REAL, &itimer, NULL); |
| 114 | } |
| 115 | |
Jens Axboe | 02444ad | 2008-10-07 11:33:00 +0200 | [diff] [blame] | 116 | static void sig_alrm(int fio_unused sig) |
Jens Axboe | ccb0fa2 | 2008-05-30 23:18:00 +0200 | [diff] [blame] | 117 | { |
Jens Axboe | 697a606 | 2008-05-31 00:04:45 +0200 | [diff] [blame] | 118 | if (threads) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 119 | update_io_ticks(); |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 120 | print_thread_status(); |
Jens Axboe | ccb0fa2 | 2008-05-30 23:18:00 +0200 | [diff] [blame] | 121 | status_timer_arm(); |
Jens Axboe | 697a606 | 2008-05-31 00:04:45 +0200 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Jens Axboe | 5f8f972 | 2008-11-18 19:10:21 +0100 | [diff] [blame] | 125 | /* |
| 126 | * Happens on thread runs with ctrl-c, ignore our own SIGQUIT |
| 127 | */ |
| 128 | static void sig_quit(int sig) |
| 129 | { |
| 130 | } |
| 131 | |
Jens Axboe | 697a606 | 2008-05-31 00:04:45 +0200 | [diff] [blame] | 132 | static void sig_int(int sig) |
| 133 | { |
| 134 | if (threads) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 135 | printf("\nfio: terminating on signal %d\n", sig); |
| 136 | fflush(stdout); |
| 137 | terminate_threads(TERMINATE_ALL); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Jens Axboe | 02444ad | 2008-10-07 11:33:00 +0200 | [diff] [blame] | 141 | static void sig_ill(int fio_unused sig) |
Jens Axboe | de79c91 | 2008-08-04 21:43:55 +0200 | [diff] [blame] | 142 | { |
| 143 | if (!threads) |
| 144 | return; |
| 145 | |
| 146 | log_err("fio: illegal instruction. your cpu does not support " |
| 147 | "the sse4.2 instruction for crc32c\n"); |
| 148 | terminate_threads(TERMINATE_ALL); |
| 149 | exit(4); |
| 150 | } |
| 151 | |
Jens Axboe | 697a606 | 2008-05-31 00:04:45 +0200 | [diff] [blame] | 152 | static void set_sig_handlers(void) |
| 153 | { |
| 154 | struct sigaction act; |
| 155 | |
| 156 | memset(&act, 0, sizeof(act)); |
| 157 | act.sa_handler = sig_alrm; |
| 158 | act.sa_flags = SA_RESTART; |
| 159 | sigaction(SIGALRM, &act, NULL); |
| 160 | |
| 161 | memset(&act, 0, sizeof(act)); |
| 162 | act.sa_handler = sig_int; |
| 163 | act.sa_flags = SA_RESTART; |
| 164 | sigaction(SIGINT, &act, NULL); |
Jens Axboe | de79c91 | 2008-08-04 21:43:55 +0200 | [diff] [blame] | 165 | |
| 166 | memset(&act, 0, sizeof(act)); |
| 167 | act.sa_handler = sig_ill; |
| 168 | act.sa_flags = SA_RESTART; |
| 169 | sigaction(SIGILL, &act, NULL); |
Jens Axboe | 5f8f972 | 2008-11-18 19:10:21 +0100 | [diff] [blame] | 170 | |
| 171 | memset(&act, 0, sizeof(act)); |
| 172 | act.sa_handler = sig_quit; |
| 173 | act.sa_flags = SA_RESTART; |
| 174 | sigaction(SIGQUIT, &act, NULL); |
Jens Axboe | 697a606 | 2008-05-31 00:04:45 +0200 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 178 | * Check if we are above the minimum rate given. |
| 179 | */ |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 180 | static int __check_min_rate(struct thread_data *td, struct timeval *now, |
| 181 | enum td_ddir ddir) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 182 | { |
Jens Axboe | 0904200 | 2007-02-23 10:56:22 +0100 | [diff] [blame] | 183 | unsigned long long bytes = 0; |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 184 | unsigned long iops = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 185 | unsigned long spent; |
| 186 | unsigned long rate; |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 187 | unsigned int ratemin = 0; |
| 188 | unsigned int rate_iops = 0; |
| 189 | unsigned int rate_iops_min = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 190 | |
Jens Axboe | d982d87 | 2009-06-10 06:35:48 +0200 | [diff] [blame] | 191 | if (!td->o.ratemin[ddir] && !td->o.rate_iops_min[ddir]) |
| 192 | return 0; |
| 193 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 194 | /* |
| 195 | * allow a 2 second settle period in the beginning |
| 196 | */ |
| 197 | if (mtime_since(&td->start, now) < 2000) |
| 198 | return 0; |
| 199 | |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 200 | iops += td->io_blocks[ddir]; |
| 201 | bytes += td->this_io_bytes[ddir]; |
| 202 | ratemin += td->o.ratemin[ddir]; |
| 203 | rate_iops += td->o.rate_iops[ddir]; |
| 204 | rate_iops_min += td->o.rate_iops_min[ddir]; |
Jens Axboe | 0904200 | 2007-02-23 10:56:22 +0100 | [diff] [blame] | 205 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 206 | /* |
| 207 | * if rate blocks is set, sample is running |
| 208 | */ |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 209 | if (td->rate_bytes[ddir] || td->rate_blocks[ddir]) { |
| 210 | spent = mtime_since(&td->lastrate[ddir], now); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 211 | if (spent < td->o.ratecycle) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 212 | return 0; |
| 213 | |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 214 | if (td->o.rate[ddir]) { |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 215 | /* |
| 216 | * check bandwidth specified rate |
| 217 | */ |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 218 | if (bytes < td->rate_bytes[ddir]) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 219 | log_err("%s: min rate %u not met\n", td->o.name, |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 220 | ratemin); |
Jens Axboe | 413dd45 | 2007-02-23 09:26:09 +0100 | [diff] [blame] | 221 | return 1; |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 222 | } else { |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 223 | rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent; |
| 224 | if (rate < ratemin || |
| 225 | bytes < td->rate_bytes[ddir]) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 226 | log_err("%s: min rate %u not met, got" |
Jens Axboe | b22989b | 2009-07-17 22:29:23 +0200 | [diff] [blame] | 227 | " %luKB/sec\n", td->o.name, |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 228 | ratemin, rate); |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 229 | return 1; |
| 230 | } |
| 231 | } |
| 232 | } else { |
| 233 | /* |
| 234 | * checks iops specified rate |
| 235 | */ |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 236 | if (iops < rate_iops) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 237 | log_err("%s: min iops rate %u not met\n", |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 238 | td->o.name, rate_iops); |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 239 | return 1; |
| 240 | } else { |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 241 | rate = ((iops - td->rate_blocks[ddir]) * 1000) / spent; |
| 242 | if (rate < rate_iops_min || |
| 243 | iops < td->rate_blocks[ddir]) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 244 | log_err("%s: min iops rate %u not met," |
| 245 | " got %lu\n", td->o.name, |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 246 | rate_iops_min, rate); |
Jens Axboe | 4e991c2 | 2007-03-15 11:41:11 +0100 | [diff] [blame] | 247 | } |
Jens Axboe | 413dd45 | 2007-02-23 09:26:09 +0100 | [diff] [blame] | 248 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 252 | td->rate_bytes[ddir] = bytes; |
| 253 | td->rate_blocks[ddir] = iops; |
| 254 | memcpy(&td->lastrate[ddir], now, sizeof(*now)); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 258 | static int check_min_rate(struct thread_data *td, struct timeval *now, |
| 259 | unsigned long *bytes_done) |
| 260 | { |
| 261 | int ret = 0; |
| 262 | |
| 263 | if (bytes_done[0]) |
| 264 | ret |= __check_min_rate(td, now, 0); |
| 265 | if (bytes_done[1]) |
| 266 | ret |= __check_min_rate(td, now, 1); |
| 267 | |
| 268 | return ret; |
| 269 | } |
| 270 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 271 | static inline int runtime_exceeded(struct thread_data *td, struct timeval *t) |
| 272 | { |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 273 | if (!td->o.timeout) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 274 | return 0; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 275 | if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 276 | return 1; |
| 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 281 | /* |
| 282 | * When job exits, we can cancel the in-flight IO if we are using async |
| 283 | * io. Attempt to do so. |
| 284 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 285 | static void cleanup_pending_aio(struct thread_data *td) |
| 286 | { |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 287 | struct flist_head *entry, *n; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 288 | struct io_u *io_u; |
| 289 | int r; |
| 290 | |
| 291 | /* |
| 292 | * get immediately available events, if any |
| 293 | */ |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 294 | r = io_u_queued_complete(td, 0, NULL); |
Jens Axboe | b2fdda4 | 2007-02-20 20:52:51 +0100 | [diff] [blame] | 295 | if (r < 0) |
| 296 | return; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 297 | |
| 298 | /* |
| 299 | * now cancel remaining active events |
| 300 | */ |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 301 | if (td->io_ops->cancel) { |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 302 | flist_for_each_safe(entry, n, &td->io_u_busylist) { |
| 303 | io_u = flist_entry(entry, struct io_u, list); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 304 | |
Jens Axboe | 0c6e751 | 2007-02-22 11:19:39 +0100 | [diff] [blame] | 305 | /* |
| 306 | * if the io_u isn't in flight, then that generally |
| 307 | * means someone leaked an io_u. complain but fix |
| 308 | * it up, so we don't stall here. |
| 309 | */ |
| 310 | if ((io_u->flags & IO_U_F_FLIGHT) == 0) { |
| 311 | log_err("fio: non-busy IO on busy list\n"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 312 | put_io_u(td, io_u); |
Jens Axboe | 0c6e751 | 2007-02-22 11:19:39 +0100 | [diff] [blame] | 313 | } else { |
| 314 | r = td->io_ops->cancel(td, io_u); |
| 315 | if (!r) |
| 316 | put_io_u(td, io_u); |
| 317 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 321 | if (td->cur_depth) |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 322 | r = io_u_queued_complete(td, td->cur_depth, NULL); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 323 | } |
| 324 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 325 | /* |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 326 | * Helper to handle the final sync of a file. Works just like the normal |
| 327 | * io path, just does everything sync. |
| 328 | */ |
| 329 | static int fio_io_sync(struct thread_data *td, struct fio_file *f) |
| 330 | { |
| 331 | struct io_u *io_u = __get_io_u(td); |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 332 | int ret; |
| 333 | |
| 334 | if (!io_u) |
| 335 | return 1; |
| 336 | |
| 337 | io_u->ddir = DDIR_SYNC; |
| 338 | io_u->file = f; |
| 339 | |
| 340 | if (td_io_prep(td, io_u)) { |
| 341 | put_io_u(td, io_u); |
| 342 | return 1; |
| 343 | } |
| 344 | |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 345 | requeue: |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 346 | ret = td_io_queue(td, io_u); |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 347 | if (ret < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 348 | td_verror(td, io_u->error, "td_io_queue"); |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 349 | put_io_u(td, io_u); |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 350 | return 1; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 351 | } else if (ret == FIO_Q_QUEUED) { |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 352 | if (io_u_queued_complete(td, 1, NULL) < 0) |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 353 | return 1; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 354 | } else if (ret == FIO_Q_COMPLETED) { |
| 355 | if (io_u->error) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 356 | td_verror(td, io_u->error, "td_io_queue"); |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 357 | return 1; |
| 358 | } |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 359 | |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 360 | if (io_u_sync_complete(td, io_u, NULL) < 0) |
Jens Axboe | b2fdda4 | 2007-02-20 20:52:51 +0100 | [diff] [blame] | 361 | return 1; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 362 | } else if (ret == FIO_Q_BUSY) { |
| 363 | if (td_io_commit(td)) |
| 364 | return 1; |
| 365 | goto requeue; |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
Jens Axboe | 993bf48 | 2008-11-14 13:04:53 +0100 | [diff] [blame] | 371 | static inline void update_tv_cache(struct thread_data *td) |
| 372 | { |
| 373 | if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask) |
| 374 | fio_gettime(&td->tv_cache, NULL); |
| 375 | } |
| 376 | |
Radha Ramachandran | f2bba18 | 2009-06-15 08:40:16 +0200 | [diff] [blame] | 377 | static int break_on_this_error(struct thread_data *td, int *retptr) |
| 378 | { |
| 379 | int ret = *retptr; |
| 380 | |
| 381 | if (ret < 0 || td->error) { |
| 382 | int err; |
| 383 | |
Jens Axboe | 1ec99ee | 2009-06-15 12:37:30 +0200 | [diff] [blame] | 384 | if (!td->o.continue_on_error) |
| 385 | return 1; |
Radha Ramachandran | f2bba18 | 2009-06-15 08:40:16 +0200 | [diff] [blame] | 386 | |
| 387 | if (ret < 0) |
| 388 | err = -ret; |
| 389 | else |
| 390 | err = td->error; |
| 391 | |
Radha Ramachandran | f2bba18 | 2009-06-15 08:40:16 +0200 | [diff] [blame] | 392 | if (td_non_fatal_error(err)) { |
| 393 | /* |
| 394 | * Continue with the I/Os in case of |
| 395 | * a non fatal error. |
| 396 | */ |
Radha Ramachandran | 8a4f8a2 | 2009-07-02 08:30:49 +0200 | [diff] [blame] | 397 | update_error_count(td, err); |
Radha Ramachandran | f2bba18 | 2009-06-15 08:40:16 +0200 | [diff] [blame] | 398 | td_clear_error(td); |
| 399 | *retptr = 0; |
| 400 | return 0; |
Radha Ramachandran | 8a4f8a2 | 2009-07-02 08:30:49 +0200 | [diff] [blame] | 401 | } else if (td->o.fill_device && err == ENOSPC) { |
| 402 | /* |
| 403 | * We expect to hit this error if |
| 404 | * fill_device option is set. |
| 405 | */ |
| 406 | td_clear_error(td); |
| 407 | td->terminate = 1; |
| 408 | return 1; |
Radha Ramachandran | f2bba18 | 2009-06-15 08:40:16 +0200 | [diff] [blame] | 409 | } else { |
| 410 | /* |
| 411 | * Stop the I/O in case of a fatal |
| 412 | * error. |
| 413 | */ |
Radha Ramachandran | 8a4f8a2 | 2009-07-02 08:30:49 +0200 | [diff] [blame] | 414 | update_error_count(td, err); |
Radha Ramachandran | f2bba18 | 2009-06-15 08:40:16 +0200 | [diff] [blame] | 415 | return 1; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 422 | /* |
Jens Axboe | 34403fb | 2007-03-02 21:43:25 +0100 | [diff] [blame] | 423 | * The main verify engine. Runs over the writes we previously submitted, |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 424 | * reads the blocks back in, and checks the crc/md5 of the data. |
| 425 | */ |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 426 | static void do_verify(struct thread_data *td) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 427 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 428 | struct fio_file *f; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 429 | struct io_u *io_u; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 430 | int ret, min_events; |
| 431 | unsigned int i; |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 432 | |
| 433 | /* |
| 434 | * sync io first and invalidate cache, to make sure we really |
| 435 | * read from disk. |
| 436 | */ |
| 437 | for_each_file(td, f, i) { |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 438 | if (!fio_file_open(f)) |
Jens Axboe | 3d7b485 | 2007-03-13 14:50:28 +0100 | [diff] [blame] | 439 | continue; |
Jens Axboe | b2fdda4 | 2007-02-20 20:52:51 +0100 | [diff] [blame] | 440 | if (fio_io_sync(td, f)) |
| 441 | break; |
| 442 | if (file_invalidate_cache(td, f)) |
| 443 | break; |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 444 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 445 | |
Jens Axboe | b2fdda4 | 2007-02-20 20:52:51 +0100 | [diff] [blame] | 446 | if (td->error) |
| 447 | return; |
| 448 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 449 | td_set_runstate(td, TD_VERIFYING); |
| 450 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 451 | io_u = NULL; |
| 452 | while (!td->terminate) { |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame] | 453 | int ret2, full; |
Jens Axboe | 5451792 | 2007-03-05 10:06:06 +0100 | [diff] [blame] | 454 | |
Jens Axboe | 993bf48 | 2008-11-14 13:04:53 +0100 | [diff] [blame] | 455 | update_tv_cache(td); |
| 456 | |
| 457 | if (runtime_exceeded(td, &td->tv_cache)) { |
Jens Axboe | 90a87d4 | 2007-04-18 09:02:32 +0200 | [diff] [blame] | 458 | td->terminate = 1; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 459 | break; |
Jens Axboe | 069c291 | 2007-02-21 23:02:39 +0100 | [diff] [blame] | 460 | } |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 461 | |
Radha Ramachandran | dcb4830 | 2009-06-16 08:31:29 +0200 | [diff] [blame] | 462 | io_u = __get_io_u(td); |
| 463 | if (!io_u) |
| 464 | break; |
| 465 | |
Jens Axboe | 069c291 | 2007-02-21 23:02:39 +0100 | [diff] [blame] | 466 | if (get_next_verify(td, io_u)) { |
| 467 | put_io_u(td, io_u); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 468 | break; |
Jens Axboe | 069c291 | 2007-02-21 23:02:39 +0100 | [diff] [blame] | 469 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 470 | |
Jens Axboe | 069c291 | 2007-02-21 23:02:39 +0100 | [diff] [blame] | 471 | if (td_io_prep(td, io_u)) { |
| 472 | put_io_u(td, io_u); |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 473 | break; |
Jens Axboe | 069c291 | 2007-02-21 23:02:39 +0100 | [diff] [blame] | 474 | } |
Jens Axboe | d7762cf | 2007-02-23 12:34:57 +0100 | [diff] [blame] | 475 | |
Jens Axboe | e8462bd | 2009-07-06 12:59:04 +0200 | [diff] [blame] | 476 | if (td->o.verify_async) |
| 477 | io_u->end_io = verify_io_u_async; |
| 478 | else |
| 479 | io_u->end_io = verify_io_u; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 480 | |
Jens Axboe | 1178680 | 2007-03-05 18:33:22 +0100 | [diff] [blame] | 481 | ret = td_io_queue(td, io_u); |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 482 | switch (ret) { |
| 483 | case FIO_Q_COMPLETED: |
Radha Ramachandran | f2bba18 | 2009-06-15 08:40:16 +0200 | [diff] [blame] | 484 | if (io_u->error) { |
Jens Axboe | 22819ec | 2007-02-18 07:47:14 +0100 | [diff] [blame] | 485 | ret = -io_u->error; |
Radha Ramachandran | f2bba18 | 2009-06-15 08:40:16 +0200 | [diff] [blame] | 486 | clear_io_u(td, io_u); |
| 487 | } else if (io_u->resid) { |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 488 | int bytes = io_u->xfer_buflen - io_u->resid; |
Jens Axboe | d460eb3 | 2007-04-16 21:39:33 +0200 | [diff] [blame] | 489 | struct fio_file *f = io_u->file; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 490 | |
Jens Axboe | 9e9d164 | 2007-03-12 09:37:46 +0100 | [diff] [blame] | 491 | /* |
| 492 | * zero read, fail |
| 493 | */ |
| 494 | if (!bytes) { |
Jens Axboe | 41d9f0f | 2008-06-02 12:19:50 +0200 | [diff] [blame] | 495 | td_verror(td, EIO, "full resid"); |
Jens Axboe | 9e9d164 | 2007-03-12 09:37:46 +0100 | [diff] [blame] | 496 | put_io_u(td, io_u); |
| 497 | break; |
| 498 | } |
Jens Axboe | 8400d9b | 2007-03-28 11:10:09 +0200 | [diff] [blame] | 499 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 500 | io_u->xfer_buflen = io_u->resid; |
| 501 | io_u->xfer_buf += bytes; |
Jens Axboe | 8400d9b | 2007-03-28 11:10:09 +0200 | [diff] [blame] | 502 | io_u->offset += bytes; |
| 503 | |
Jens Axboe | 30061b9 | 2007-04-17 13:31:34 +0200 | [diff] [blame] | 504 | td->ts.short_io_u[io_u->ddir]++; |
| 505 | |
Jens Axboe | d460eb3 | 2007-04-16 21:39:33 +0200 | [diff] [blame] | 506 | if (io_u->offset == f->real_file_size) |
Jens Axboe | 8400d9b | 2007-03-28 11:10:09 +0200 | [diff] [blame] | 507 | goto sync_done; |
| 508 | |
Jens Axboe | 1178680 | 2007-03-05 18:33:22 +0100 | [diff] [blame] | 509 | requeue_io_u(td, &io_u); |
| 510 | } else { |
Jens Axboe | 8400d9b | 2007-03-28 11:10:09 +0200 | [diff] [blame] | 511 | sync_done: |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 512 | ret = io_u_sync_complete(td, io_u, NULL); |
Jens Axboe | 1178680 | 2007-03-05 18:33:22 +0100 | [diff] [blame] | 513 | if (ret < 0) |
| 514 | break; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 515 | } |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 516 | continue; |
| 517 | case FIO_Q_QUEUED: |
| 518 | break; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 519 | case FIO_Q_BUSY: |
| 520 | requeue_io_u(td, &io_u); |
Jens Axboe | 5451792 | 2007-03-05 10:06:06 +0100 | [diff] [blame] | 521 | ret2 = td_io_commit(td); |
| 522 | if (ret2 < 0) |
| 523 | ret = ret2; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 524 | break; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 525 | default: |
| 526 | assert(ret < 0); |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 527 | td_verror(td, -ret, "td_io_queue"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 528 | break; |
| 529 | } |
| 530 | |
Radha Ramachandran | f2bba18 | 2009-06-15 08:40:16 +0200 | [diff] [blame] | 531 | if (break_on_this_error(td, &ret)) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 532 | break; |
| 533 | |
Jens Axboe | 3af6ef3 | 2007-02-18 06:57:43 +0100 | [diff] [blame] | 534 | /* |
| 535 | * if we can queue more, do so. but check if there are |
| 536 | * completed io_u's first. |
| 537 | */ |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame] | 538 | full = queue_full(td) || ret == FIO_Q_BUSY; |
| 539 | if (full || !td->o.iodepth_batch_complete) { |
Radha Ramachandran | 6a96276 | 2009-11-05 08:33:49 +0100 | [diff] [blame] | 540 | min_events = min(td->o.iodepth_batch_complete, |
| 541 | td->cur_depth); |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame] | 542 | if (full && !min_events) |
Jens Axboe | 838bc70 | 2008-05-22 13:08:23 +0200 | [diff] [blame] | 543 | min_events = 1; |
Jens Axboe | e916b39 | 2007-02-20 14:37:26 +0100 | [diff] [blame] | 544 | |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame] | 545 | do { |
| 546 | /* |
| 547 | * Reap required number of io units, if any, |
| 548 | * and do the verification on them through |
| 549 | * the callback handler |
| 550 | */ |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 551 | if (io_u_queued_complete(td, min_events, NULL) < 0) { |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame] | 552 | ret = -1; |
| 553 | break; |
| 554 | } |
| 555 | } while (full && (td->cur_depth > td->o.iodepth_low)); |
| 556 | } |
| 557 | if (ret < 0) |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 558 | break; |
| 559 | } |
| 560 | |
Jens Axboe | c01c039 | 2007-02-26 12:43:42 +0100 | [diff] [blame] | 561 | if (!td->error) { |
| 562 | min_events = td->cur_depth; |
| 563 | |
| 564 | if (min_events) |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 565 | ret = io_u_queued_complete(td, min_events, NULL); |
Jens Axboe | c01c039 | 2007-02-26 12:43:42 +0100 | [diff] [blame] | 566 | } else |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 567 | cleanup_pending_aio(td); |
| 568 | |
| 569 | td_set_runstate(td, TD_RUNNING); |
| 570 | } |
| 571 | |
Jens Axboe | 32cd46a | 2006-06-07 13:40:40 +0200 | [diff] [blame] | 572 | /* |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 573 | * 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] | 574 | * and reaps them, checking for rate and errors along the way. |
| 575 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 576 | static void do_io(struct thread_data *td) |
| 577 | { |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 578 | unsigned int i; |
| 579 | int ret = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 580 | |
Jens Axboe | b29ee5b | 2008-09-11 10:17:26 +0200 | [diff] [blame] | 581 | if (in_ramp_time(td)) |
| 582 | td_set_runstate(td, TD_RAMP); |
| 583 | else |
| 584 | td_set_runstate(td, TD_RUNNING); |
Jens Axboe | 5853e5a | 2006-06-08 14:41:05 +0200 | [diff] [blame] | 585 | |
Glen Ogilvie | 457bf39 | 2009-11-03 21:52:36 +0100 | [diff] [blame] | 586 | while ( (td->o.read_iolog_file && !flist_empty(&td->io_log_list)) || |
| 587 | ((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] | 588 | struct timeval comp_time; |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 589 | unsigned long bytes_done[2] = { 0, 0 }; |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 590 | int min_evts = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 591 | struct io_u *io_u; |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame] | 592 | int ret2, full; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 593 | |
| 594 | if (td->terminate) |
| 595 | break; |
| 596 | |
Jens Axboe | 993bf48 | 2008-11-14 13:04:53 +0100 | [diff] [blame] | 597 | update_tv_cache(td); |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 598 | |
Jens Axboe | 993bf48 | 2008-11-14 13:04:53 +0100 | [diff] [blame] | 599 | if (runtime_exceeded(td, &td->tv_cache)) { |
Jens Axboe | 90a87d4 | 2007-04-18 09:02:32 +0200 | [diff] [blame] | 600 | td->terminate = 1; |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 601 | break; |
| 602 | } |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 603 | |
Radha Ramachandran | dcb4830 | 2009-06-16 08:31:29 +0200 | [diff] [blame] | 604 | io_u = get_io_u(td); |
| 605 | if (!io_u) |
| 606 | break; |
| 607 | |
Jens Axboe | b67740d | 2007-07-26 12:22:30 +0200 | [diff] [blame] | 608 | /* |
| 609 | * Add verification end_io handler, if asked to verify |
| 610 | * a previously written file. |
| 611 | */ |
Jens Axboe | 715d2b3 | 2009-09-04 20:53:53 +0200 | [diff] [blame] | 612 | if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ && |
| 613 | !td_rw(td)) { |
Jens Axboe | e8462bd | 2009-07-06 12:59:04 +0200 | [diff] [blame] | 614 | if (td->o.verify_async) |
| 615 | io_u->end_io = verify_io_u_async; |
| 616 | else |
| 617 | io_u->end_io = verify_io_u; |
Jens Axboe | d8fab1b | 2008-03-06 10:25:17 +0100 | [diff] [blame] | 618 | td_set_runstate(td, TD_VERIFYING); |
Jens Axboe | b29ee5b | 2008-09-11 10:17:26 +0200 | [diff] [blame] | 619 | } else if (in_ramp_time(td)) |
| 620 | td_set_runstate(td, TD_RAMP); |
| 621 | else |
Jens Axboe | d8fab1b | 2008-03-06 10:25:17 +0100 | [diff] [blame] | 622 | td_set_runstate(td, TD_RUNNING); |
Jens Axboe | b67740d | 2007-07-26 12:22:30 +0200 | [diff] [blame] | 623 | |
Jens Axboe | 1178680 | 2007-03-05 18:33:22 +0100 | [diff] [blame] | 624 | ret = td_io_queue(td, io_u); |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 625 | switch (ret) { |
| 626 | case FIO_Q_COMPLETED: |
Radha Ramachandran | f2bba18 | 2009-06-15 08:40:16 +0200 | [diff] [blame] | 627 | if (io_u->error) { |
Jens Axboe | 5451792 | 2007-03-05 10:06:06 +0100 | [diff] [blame] | 628 | ret = -io_u->error; |
Radha Ramachandran | f2bba18 | 2009-06-15 08:40:16 +0200 | [diff] [blame] | 629 | clear_io_u(td, io_u); |
| 630 | } else if (io_u->resid) { |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 631 | int bytes = io_u->xfer_buflen - io_u->resid; |
Jens Axboe | d460eb3 | 2007-04-16 21:39:33 +0200 | [diff] [blame] | 632 | struct fio_file *f = io_u->file; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 633 | |
Jens Axboe | 9e9d164 | 2007-03-12 09:37:46 +0100 | [diff] [blame] | 634 | /* |
| 635 | * zero read, fail |
| 636 | */ |
| 637 | if (!bytes) { |
Jens Axboe | 41d9f0f | 2008-06-02 12:19:50 +0200 | [diff] [blame] | 638 | td_verror(td, EIO, "full resid"); |
Jens Axboe | 9e9d164 | 2007-03-12 09:37:46 +0100 | [diff] [blame] | 639 | put_io_u(td, io_u); |
| 640 | break; |
| 641 | } |
| 642 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 643 | io_u->xfer_buflen = io_u->resid; |
| 644 | io_u->xfer_buf += bytes; |
Jens Axboe | 5a7c568 | 2007-03-28 11:07:20 +0200 | [diff] [blame] | 645 | io_u->offset += bytes; |
| 646 | |
Jens Axboe | 30061b9 | 2007-04-17 13:31:34 +0200 | [diff] [blame] | 647 | td->ts.short_io_u[io_u->ddir]++; |
| 648 | |
Jens Axboe | d460eb3 | 2007-04-16 21:39:33 +0200 | [diff] [blame] | 649 | if (io_u->offset == f->real_file_size) |
Jens Axboe | 5a7c568 | 2007-03-28 11:07:20 +0200 | [diff] [blame] | 650 | goto sync_done; |
| 651 | |
Jens Axboe | 1178680 | 2007-03-05 18:33:22 +0100 | [diff] [blame] | 652 | requeue_io_u(td, &io_u); |
| 653 | } else { |
Jens Axboe | 5a7c568 | 2007-03-28 11:07:20 +0200 | [diff] [blame] | 654 | sync_done: |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 655 | if (__should_check_rate(td, 0) || |
| 656 | __should_check_rate(td, 1)) |
Jens Axboe | 993bf48 | 2008-11-14 13:04:53 +0100 | [diff] [blame] | 657 | fio_gettime(&comp_time, NULL); |
| 658 | |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 659 | ret = io_u_sync_complete(td, io_u, bytes_done); |
| 660 | if (ret < 0) |
| 661 | break; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 662 | } |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 663 | break; |
| 664 | case FIO_Q_QUEUED: |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 665 | /* |
| 666 | * if the engine doesn't have a commit hook, |
| 667 | * the io_u is really queued. if it does have such |
| 668 | * a hook, it has to call io_u_queued() itself. |
| 669 | */ |
| 670 | if (td->io_ops->commit == NULL) |
| 671 | io_u_queued(td, io_u); |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 672 | break; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 673 | case FIO_Q_BUSY: |
| 674 | requeue_io_u(td, &io_u); |
Jens Axboe | 5451792 | 2007-03-05 10:06:06 +0100 | [diff] [blame] | 675 | ret2 = td_io_commit(td); |
| 676 | if (ret2 < 0) |
| 677 | ret = ret2; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 678 | break; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 679 | default: |
| 680 | assert(ret < 0); |
| 681 | put_io_u(td, io_u); |
| 682 | break; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 683 | } |
| 684 | |
Radha Ramachandran | f2bba18 | 2009-06-15 08:40:16 +0200 | [diff] [blame] | 685 | if (break_on_this_error(td, &ret)) |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 686 | break; |
| 687 | |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 688 | /* |
| 689 | * See if we need to complete some commands |
| 690 | */ |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame] | 691 | full = queue_full(td) || ret == FIO_Q_BUSY; |
| 692 | if (full || !td->o.iodepth_batch_complete) { |
Radha Ramachandran | 6a96276 | 2009-11-05 08:33:49 +0100 | [diff] [blame] | 693 | min_evts = min(td->o.iodepth_batch_complete, |
| 694 | td->cur_depth); |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame] | 695 | if (full && !min_evts) |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 696 | min_evts = 1; |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame] | 697 | |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 698 | if (__should_check_rate(td, 0) || |
| 699 | __should_check_rate(td, 1)) |
Jens Axboe | 993bf48 | 2008-11-14 13:04:53 +0100 | [diff] [blame] | 700 | fio_gettime(&comp_time, NULL); |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame] | 701 | |
| 702 | do { |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 703 | ret = io_u_queued_complete(td, min_evts, bytes_done); |
| 704 | if (ret < 0) |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame] | 705 | break; |
| 706 | |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame] | 707 | } while (full && (td->cur_depth > td->o.iodepth_low)); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 708 | } |
| 709 | |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame] | 710 | if (ret < 0) |
| 711 | break; |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 712 | if (!(bytes_done[0] + bytes_done[1])) |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 713 | continue; |
| 714 | |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 715 | if (!in_ramp_time(td) && should_check_rate(td, bytes_done)) { |
| 716 | if (check_min_rate(td, &comp_time, bytes_done)) { |
Jens Axboe | 721938a | 2008-09-10 09:46:16 +0200 | [diff] [blame] | 717 | if (exitall_on_terminate) |
| 718 | terminate_threads(td->groupid); |
| 719 | td_verror(td, EIO, "check_min_rate"); |
| 720 | break; |
| 721 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 722 | } |
| 723 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 724 | if (td->o.thinktime) { |
Jens Axboe | 9c1f743 | 2007-01-03 20:43:19 +0100 | [diff] [blame] | 725 | unsigned long long b; |
| 726 | |
| 727 | b = td->io_blocks[0] + td->io_blocks[1]; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 728 | if (!(b % td->o.thinktime_blocks)) { |
Jens Axboe | 48097d5 | 2007-02-17 06:30:44 +0100 | [diff] [blame] | 729 | int left; |
| 730 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 731 | if (td->o.thinktime_spin) |
Jens Axboe | 6dd6f2c | 2008-12-10 13:24:12 +0100 | [diff] [blame] | 732 | usec_spin(td->o.thinktime_spin); |
Jens Axboe | 48097d5 | 2007-02-17 06:30:44 +0100 | [diff] [blame] | 733 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 734 | left = td->o.thinktime - td->o.thinktime_spin; |
Jens Axboe | 48097d5 | 2007-02-17 06:30:44 +0100 | [diff] [blame] | 735 | if (left) |
| 736 | usec_sleep(td, left); |
| 737 | } |
Jens Axboe | 9c1f743 | 2007-01-03 20:43:19 +0100 | [diff] [blame] | 738 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 739 | } |
| 740 | |
Shawn Lewis | aa31f1f | 2008-01-11 09:45:11 +0100 | [diff] [blame] | 741 | if (td->o.fill_device && td->error == ENOSPC) { |
| 742 | td->error = 0; |
| 743 | td->terminate = 1; |
| 744 | } |
Jens Axboe | 4d2413c | 2006-11-23 11:58:59 +0100 | [diff] [blame] | 745 | if (!td->error) { |
Jens Axboe | 3d7c391 | 2007-02-19 13:16:12 +0100 | [diff] [blame] | 746 | struct fio_file *f; |
| 747 | |
Jens Axboe | c01c039 | 2007-02-26 12:43:42 +0100 | [diff] [blame] | 748 | i = td->cur_depth; |
| 749 | if (i) |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 750 | ret = io_u_queued_complete(td, i, NULL); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 751 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 752 | if (should_fsync(td) && td->o.end_fsync) { |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 753 | td_set_runstate(td, TD_FSYNCING); |
Jens Axboe | 3d7b485 | 2007-03-13 14:50:28 +0100 | [diff] [blame] | 754 | |
| 755 | for_each_file(td, f, i) { |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 756 | if (!fio_file_open(f)) |
Jens Axboe | 3d7b485 | 2007-03-13 14:50:28 +0100 | [diff] [blame] | 757 | continue; |
Jens Axboe | 858a3d4 | 2006-10-24 14:54:44 +0200 | [diff] [blame] | 758 | fio_io_sync(td, f); |
Jens Axboe | 3d7b485 | 2007-03-13 14:50:28 +0100 | [diff] [blame] | 759 | } |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 760 | } |
Jens Axboe | c01c039 | 2007-02-26 12:43:42 +0100 | [diff] [blame] | 761 | } else |
| 762 | cleanup_pending_aio(td); |
Jens Axboe | 2ba1c29 | 2008-02-01 13:16:38 +0100 | [diff] [blame] | 763 | |
| 764 | /* |
| 765 | * stop job if we failed doing any IO |
| 766 | */ |
| 767 | if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0) |
| 768 | td->done = 1; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 769 | } |
| 770 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 771 | static void cleanup_io_u(struct thread_data *td) |
| 772 | { |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 773 | struct flist_head *entry, *n; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 774 | struct io_u *io_u; |
| 775 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 776 | flist_for_each_safe(entry, n, &td->io_u_freelist) { |
| 777 | io_u = flist_entry(entry, struct io_u, list); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 778 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 779 | flist_del(&io_u->list); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 780 | free(io_u); |
| 781 | } |
| 782 | |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 783 | free_io_mem(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | static int init_io_u(struct thread_data *td) |
| 787 | { |
| 788 | struct io_u *io_u; |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 789 | unsigned int max_bs; |
Jens Axboe | eb7ccf3 | 2009-04-29 09:48:04 +0200 | [diff] [blame] | 790 | int cl_align, i, max_units; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 791 | char *p; |
| 792 | |
Jens Axboe | 1e3c09a | 2008-02-04 10:48:13 +0100 | [diff] [blame] | 793 | max_units = td->o.iodepth; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 794 | 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] | 795 | td->orig_buffer_size = (unsigned long long) max_bs |
| 796 | * (unsigned long long) max_units; |
Jens Axboe | 74b025b | 2006-12-19 15:18:14 +0100 | [diff] [blame] | 797 | |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 798 | if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) { |
| 799 | unsigned long bs; |
| 800 | |
| 801 | bs = td->orig_buffer_size + td->o.hugepage_size - 1; |
| 802 | td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1); |
| 803 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 804 | |
Jens Axboe | c399064 | 2007-07-19 10:17:09 +0200 | [diff] [blame] | 805 | if (td->orig_buffer_size != (size_t) td->orig_buffer_size) { |
| 806 | log_err("fio: IO memory too large. Reduce max_bs or iodepth\n"); |
| 807 | return 1; |
| 808 | } |
| 809 | |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 810 | if (allocate_io_mem(td)) |
| 811 | return 1; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 812 | |
Jens Axboe | fc966be | 2009-07-03 08:30:15 +0200 | [diff] [blame] | 813 | if (td->o.odirect || td->o.mem_align) |
Jens Axboe | d529ee1 | 2009-07-01 10:33:03 +0200 | [diff] [blame] | 814 | p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align; |
Jens Axboe | b3f378e | 2007-07-20 12:39:22 +0200 | [diff] [blame] | 815 | else |
| 816 | p = td->orig_buffer; |
| 817 | |
Jens Axboe | eb7ccf3 | 2009-04-29 09:48:04 +0200 | [diff] [blame] | 818 | cl_align = os_cache_line_size(); |
| 819 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 820 | for (i = 0; i < max_units; i++) { |
Jens Axboe | eb7ccf3 | 2009-04-29 09:48:04 +0200 | [diff] [blame] | 821 | void *ptr; |
| 822 | |
Jens Axboe | db1cd90 | 2007-04-26 13:47:07 +0200 | [diff] [blame] | 823 | if (td->terminate) |
| 824 | return 1; |
Jens Axboe | eb7ccf3 | 2009-04-29 09:48:04 +0200 | [diff] [blame] | 825 | |
| 826 | if (posix_memalign(&ptr, cl_align, sizeof(*io_u))) { |
| 827 | log_err("fio: posix_memalign=%s\n", strerror(errno)); |
| 828 | break; |
| 829 | } |
| 830 | |
| 831 | io_u = ptr; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 832 | memset(io_u, 0, sizeof(*io_u)); |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 833 | INIT_FLIST_HEAD(&io_u->list); |
Jens Axboe | d529ee1 | 2009-07-01 10:33:03 +0200 | [diff] [blame] | 834 | dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 835 | |
Jens Axboe | b4c5e1a | 2007-10-25 18:44:45 +0200 | [diff] [blame] | 836 | if (!(td->io_ops->flags & FIO_NOIO)) { |
| 837 | io_u->buf = p + max_bs * i; |
Jens Axboe | d529ee1 | 2009-07-01 10:33:03 +0200 | [diff] [blame] | 838 | dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf); |
Jens Axboe | e9459e5 | 2007-04-17 15:46:32 +0200 | [diff] [blame] | 839 | |
Jens Axboe | 5973caf | 2008-05-21 19:52:35 +0200 | [diff] [blame] | 840 | if (td_write(td) && !td->o.refill_buffers) |
| 841 | io_u_fill_buffer(td, io_u, max_bs); |
Jens Axboe | b4c5e1a | 2007-10-25 18:44:45 +0200 | [diff] [blame] | 842 | } |
Jens Axboe | 6b9cea2 | 2006-10-30 17:00:24 +0100 | [diff] [blame] | 843 | |
Jens Axboe | b1ff340 | 2006-02-17 11:35:58 +0100 | [diff] [blame] | 844 | io_u->index = i; |
Jens Axboe | 0c6e751 | 2007-02-22 11:19:39 +0100 | [diff] [blame] | 845 | io_u->flags = IO_U_F_FREE; |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 846 | flist_add(&io_u->list, &td->io_u_freelist); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | return 0; |
| 850 | } |
| 851 | |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 852 | static int switch_ioscheduler(struct thread_data *td) |
| 853 | { |
| 854 | char tmp[256], tmp2[128]; |
| 855 | FILE *f; |
| 856 | int ret; |
| 857 | |
Jens Axboe | ba0fbe1 | 2007-03-09 14:34:23 +0100 | [diff] [blame] | 858 | if (td->io_ops->flags & FIO_DISKLESSIO) |
Jens Axboe | f48b467 | 2006-10-27 11:30:07 +0200 | [diff] [blame] | 859 | return 0; |
| 860 | |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 861 | sprintf(tmp, "%s/queue/scheduler", td->sysfs_root); |
| 862 | |
| 863 | f = fopen(tmp, "r+"); |
| 864 | if (!f) { |
Jens Axboe | cbf5c1d | 2007-04-16 12:56:00 +0200 | [diff] [blame] | 865 | if (errno == ENOENT) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 866 | log_err("fio: os or kernel doesn't support IO scheduler" |
| 867 | " switching\n"); |
Jens Axboe | cbf5c1d | 2007-04-16 12:56:00 +0200 | [diff] [blame] | 868 | return 0; |
| 869 | } |
| 870 | td_verror(td, errno, "fopen iosched"); |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 871 | return 1; |
| 872 | } |
| 873 | |
| 874 | /* |
| 875 | * Set io scheduler. |
| 876 | */ |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 877 | ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f); |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 878 | if (ferror(f) || ret != 1) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 879 | td_verror(td, errno, "fwrite"); |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 880 | fclose(f); |
| 881 | return 1; |
| 882 | } |
| 883 | |
| 884 | rewind(f); |
| 885 | |
| 886 | /* |
| 887 | * Read back and check that the selected scheduler is now the default. |
| 888 | */ |
| 889 | ret = fread(tmp, 1, sizeof(tmp), f); |
| 890 | if (ferror(f) || ret < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 891 | td_verror(td, errno, "fread"); |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 892 | fclose(f); |
| 893 | return 1; |
| 894 | } |
| 895 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 896 | sprintf(tmp2, "[%s]", td->o.ioscheduler); |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 897 | if (!strstr(tmp, tmp2)) { |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 898 | log_err("fio: io scheduler %s not found\n", td->o.ioscheduler); |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 899 | td_verror(td, EINVAL, "iosched_switch"); |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 900 | fclose(f); |
| 901 | return 1; |
| 902 | } |
| 903 | |
| 904 | fclose(f); |
| 905 | return 0; |
| 906 | } |
| 907 | |
Jens Axboe | 492158c | 2007-05-24 10:32:47 +0200 | [diff] [blame] | 908 | static int keep_running(struct thread_data *td) |
| 909 | { |
| 910 | unsigned long long io_done; |
| 911 | |
Jens Axboe | 20e354e | 2007-07-23 14:36:16 +0200 | [diff] [blame] | 912 | if (td->done) |
| 913 | return 0; |
Jens Axboe | 492158c | 2007-05-24 10:32:47 +0200 | [diff] [blame] | 914 | if (td->o.time_based) |
| 915 | return 1; |
| 916 | if (td->o.loops) { |
| 917 | td->o.loops--; |
| 918 | return 1; |
| 919 | } |
| 920 | |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 921 | io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE] |
| 922 | + td->io_skip_bytes; |
Jens Axboe | 492158c | 2007-05-24 10:32:47 +0200 | [diff] [blame] | 923 | if (io_done < td->o.size) |
| 924 | return 1; |
| 925 | |
| 926 | return 0; |
| 927 | } |
| 928 | |
Jens Axboe | b29ee5b | 2008-09-11 10:17:26 +0200 | [diff] [blame] | 929 | static void reset_io_counters(struct thread_data *td) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 930 | { |
Jens Axboe | 756867b | 2007-03-06 15:19:24 +0100 | [diff] [blame] | 931 | 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] | 932 | td->this_io_bytes[0] = td->this_io_bytes[1] = 0; |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 933 | td->zone_bytes = 0; |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 934 | td->rate_bytes[0] = td->rate_bytes[1] = 0; |
| 935 | td->rate_blocks[0] = td->rate_blocks[1] = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 936 | |
Jens Axboe | c1324df | 2007-02-19 19:06:41 +0100 | [diff] [blame] | 937 | td->last_was_sync = 0; |
| 938 | |
Jens Axboe | 2ba1c29 | 2008-02-01 13:16:38 +0100 | [diff] [blame] | 939 | /* |
| 940 | * reset file done count if we are to start over |
| 941 | */ |
| 942 | if (td->o.time_based || td->o.loops) |
Jens Axboe | 48f5abd | 2007-07-20 13:25:04 +0200 | [diff] [blame] | 943 | td->nr_done_files = 0; |
Jens Axboe | 5bfc35d | 2009-04-07 13:28:12 +0200 | [diff] [blame] | 944 | |
| 945 | /* |
| 946 | * Set the same seed to get repeatable runs |
| 947 | */ |
| 948 | td_fill_rand_seeds(td); |
Jens Axboe | b29ee5b | 2008-09-11 10:17:26 +0200 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | void reset_all_stats(struct thread_data *td) |
| 952 | { |
| 953 | struct timeval tv; |
| 954 | int i; |
| 955 | |
| 956 | reset_io_counters(td); |
| 957 | |
| 958 | for (i = 0; i < 2; i++) { |
| 959 | td->io_bytes[i] = 0; |
| 960 | td->io_blocks[i] = 0; |
| 961 | td->io_issues[i] = 0; |
| 962 | td->ts.total_io_u[i] = 0; |
| 963 | } |
| 964 | |
| 965 | fio_gettime(&tv, NULL); |
| 966 | memcpy(&td->epoch, &tv, sizeof(tv)); |
| 967 | memcpy(&td->start, &tv, sizeof(tv)); |
| 968 | } |
| 969 | |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 970 | static void clear_io_state(struct thread_data *td) |
Jens Axboe | b29ee5b | 2008-09-11 10:17:26 +0200 | [diff] [blame] | 971 | { |
| 972 | struct fio_file *f; |
| 973 | unsigned int i; |
Jens Axboe | b29ee5b | 2008-09-11 10:17:26 +0200 | [diff] [blame] | 974 | |
| 975 | reset_io_counters(td); |
Jens Axboe | 281f9b6 | 2007-05-23 09:34:42 +0200 | [diff] [blame] | 976 | |
Jens Axboe | 24ffd2c | 2008-03-01 15:47:08 +0100 | [diff] [blame] | 977 | close_files(td); |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 978 | for_each_file(td, f, i) |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 979 | fio_file_clear_done(f); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 980 | } |
| 981 | |
Jens Axboe | 398e8c4 | 2009-05-20 12:10:02 +0200 | [diff] [blame] | 982 | static int exec_string(const char *string) |
| 983 | { |
| 984 | int ret, newlen = strlen(string) + 1 + 8; |
| 985 | char *str; |
| 986 | |
| 987 | str = malloc(newlen); |
| 988 | sprintf(str, "sh -c %s", string); |
| 989 | |
| 990 | ret = system(str); |
| 991 | if (ret == -1) |
| 992 | log_err("fio: exec of cmd <%s> failed\n", str); |
| 993 | |
| 994 | free(str); |
| 995 | return ret; |
| 996 | } |
| 997 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 998 | /* |
| 999 | * Entry point for the thread based jobs. The process based jobs end up |
| 1000 | * here as well, after a little setup. |
| 1001 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1002 | static void *thread_main(void *data) |
| 1003 | { |
Shawn Lewis | 1092731 | 2007-11-21 09:38:13 +0100 | [diff] [blame] | 1004 | unsigned long long runtime[2], elapsed; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1005 | struct thread_data *td = data; |
Jens Axboe | e8462bd | 2009-07-06 12:59:04 +0200 | [diff] [blame] | 1006 | pthread_condattr_t attr; |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 1007 | int clear_state; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1008 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1009 | if (!td->o.use_thread) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1010 | setsid(); |
| 1011 | |
| 1012 | td->pid = getpid(); |
| 1013 | |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 1014 | dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid); |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 1015 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 1016 | INIT_FLIST_HEAD(&td->io_u_freelist); |
| 1017 | INIT_FLIST_HEAD(&td->io_u_busylist); |
| 1018 | INIT_FLIST_HEAD(&td->io_u_requeues); |
| 1019 | INIT_FLIST_HEAD(&td->io_log_list); |
| 1020 | INIT_FLIST_HEAD(&td->io_hist_list); |
Jens Axboe | e8462bd | 2009-07-06 12:59:04 +0200 | [diff] [blame] | 1021 | INIT_FLIST_HEAD(&td->verify_list); |
| 1022 | pthread_mutex_init(&td->io_u_lock, NULL); |
Jens Axboe | bb5d7d0 | 2007-03-27 10:21:25 +0200 | [diff] [blame] | 1023 | td->io_hist_tree = RB_ROOT; |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 1024 | |
Jens Axboe | e8462bd | 2009-07-06 12:59:04 +0200 | [diff] [blame] | 1025 | pthread_condattr_init(&attr); |
| 1026 | pthread_cond_init(&td->verify_cond, &attr); |
| 1027 | pthread_cond_init(&td->free_cond, &attr); |
| 1028 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1029 | td_set_runstate(td, TD_INITIALIZED); |
Jens Axboe | 29adda3 | 2009-01-05 19:06:39 +0100 | [diff] [blame] | 1030 | dprint(FD_MUTEX, "up startup_mutex\n"); |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 1031 | fio_mutex_up(startup_mutex); |
Jens Axboe | 29adda3 | 2009-01-05 19:06:39 +0100 | [diff] [blame] | 1032 | dprint(FD_MUTEX, "wait on td->mutex\n"); |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 1033 | fio_mutex_down(td->mutex); |
Jens Axboe | 29adda3 | 2009-01-05 19:06:39 +0100 | [diff] [blame] | 1034 | dprint(FD_MUTEX, "done waiting on td->mutex\n"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1035 | |
Jens Axboe | 37f5687 | 2007-03-09 12:42:35 +0100 | [diff] [blame] | 1036 | /* |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 1037 | * the ->mutex mutex is now no longer used, close it to avoid |
Jens Axboe | 37f5687 | 2007-03-09 12:42:35 +0100 | [diff] [blame] | 1038 | * eating a file descriptor |
| 1039 | */ |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 1040 | fio_mutex_remove(td->mutex); |
Jens Axboe | 37f5687 | 2007-03-09 12:42:35 +0100 | [diff] [blame] | 1041 | |
Jens Axboe | d84f8d4 | 2007-05-16 08:47:46 +0200 | [diff] [blame] | 1042 | /* |
| 1043 | * May alter parameters that init_io_u() will use, so we need to |
| 1044 | * do this first. |
| 1045 | */ |
| 1046 | if (init_iolog(td)) |
| 1047 | goto err; |
| 1048 | |
Jens Axboe | db1cd90 | 2007-04-26 13:47:07 +0200 | [diff] [blame] | 1049 | if (init_io_u(td)) |
| 1050 | goto err; |
| 1051 | |
Jens Axboe | e8462bd | 2009-07-06 12:59:04 +0200 | [diff] [blame] | 1052 | if (td->o.verify_async && verify_async_init(td)) |
| 1053 | goto err; |
| 1054 | |
| 1055 | if (td->o.cpumask_set && fio_setaffinity(td->pid, td->o.cpumask) == -1) { |
Jens Axboe | db1cd90 | 2007-04-26 13:47:07 +0200 | [diff] [blame] | 1056 | td_verror(td, errno, "cpu_set_affinity"); |
| 1057 | goto err; |
| 1058 | } |
| 1059 | |
Jens Axboe | f484470 | 2008-12-09 08:04:29 +0100 | [diff] [blame] | 1060 | /* |
| 1061 | * If we have a gettimeofday() thread, make sure we exclude that |
| 1062 | * thread from this job |
| 1063 | */ |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 1064 | if (td->o.gtod_cpu) { |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 1065 | fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu); |
Jens Axboe | e8462bd | 2009-07-06 12:59:04 +0200 | [diff] [blame] | 1066 | if (fio_setaffinity(td->pid, td->o.cpumask) == -1) { |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 1067 | td_verror(td, errno, "cpu_set_affinity"); |
| 1068 | goto err; |
| 1069 | } |
| 1070 | } |
| 1071 | |
Jens Axboe | ac68478 | 2007-11-08 08:29:07 +0100 | [diff] [blame] | 1072 | if (td->ioprio_set) { |
Jens Axboe | db1cd90 | 2007-04-26 13:47:07 +0200 | [diff] [blame] | 1073 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) { |
| 1074 | td_verror(td, errno, "ioprio_set"); |
| 1075 | goto err; |
| 1076 | } |
| 1077 | } |
| 1078 | |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame^] | 1079 | if (td->o.cgroup_weight && cgroup_setup(td)) |
| 1080 | goto err; |
| 1081 | |
Jens Axboe | db1cd90 | 2007-04-26 13:47:07 +0200 | [diff] [blame] | 1082 | if (nice(td->o.nice) == -1) { |
| 1083 | td_verror(td, errno, "nice"); |
| 1084 | goto err; |
| 1085 | } |
| 1086 | |
| 1087 | if (td->o.ioscheduler && switch_ioscheduler(td)) |
| 1088 | goto err; |
| 1089 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1090 | if (!td->o.create_serialize && setup_files(td)) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1091 | goto err; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 1092 | |
| 1093 | if (td_io_init(td)) |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 1094 | goto err; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1095 | |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 1096 | if (init_random_map(td)) |
| 1097 | goto err; |
| 1098 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1099 | if (td->o.exec_prerun) { |
Jens Axboe | 398e8c4 | 2009-05-20 12:10:02 +0200 | [diff] [blame] | 1100 | if (exec_string(td->o.exec_prerun)) |
Jens Axboe | 69cfd7e | 2007-02-07 13:58:53 +0100 | [diff] [blame] | 1101 | goto err; |
| 1102 | } |
Jens Axboe | 4e0ba8a | 2006-06-06 09:36:28 +0200 | [diff] [blame] | 1103 | |
Zhang, Yanmin | afad68f | 2009-05-20 11:30:55 +0200 | [diff] [blame] | 1104 | if (td->o.pre_read) { |
| 1105 | if (pre_read_files(td) < 0) |
| 1106 | goto err; |
| 1107 | } |
| 1108 | |
Jens Axboe | 6900899 | 2006-11-24 13:12:56 +0100 | [diff] [blame] | 1109 | fio_gettime(&td->epoch, NULL); |
Jens Axboe | 756867b | 2007-03-06 15:19:24 +0100 | [diff] [blame] | 1110 | getrusage(RUSAGE_SELF, &td->ts.ru_start); |
Jens Axboe | 6900899 | 2006-11-24 13:12:56 +0100 | [diff] [blame] | 1111 | |
| 1112 | runtime[0] = runtime[1] = 0; |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 1113 | clear_state = 0; |
Jens Axboe | 492158c | 2007-05-24 10:32:47 +0200 | [diff] [blame] | 1114 | while (keep_running(td)) { |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 1115 | fio_gettime(&td->start, NULL); |
Jens Axboe | c5d3d79 | 2009-07-31 11:20:52 +0200 | [diff] [blame] | 1116 | memcpy(&td->ts.stat_sample_time[0], &td->start, |
| 1117 | sizeof(td->start)); |
| 1118 | memcpy(&td->ts.stat_sample_time[1], &td->start, |
| 1119 | sizeof(td->start)); |
Jens Axboe | 993bf48 | 2008-11-14 13:04:53 +0100 | [diff] [blame] | 1120 | memcpy(&td->tv_cache, &td->start, sizeof(td->start)); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1121 | |
Jens Axboe | cf2dd8d | 2009-06-09 12:58:37 +0200 | [diff] [blame] | 1122 | if (td->o.ratemin[0] || td->o.ratemin[1]) |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1123 | memcpy(&td->lastrate, &td->ts.stat_sample_time, |
| 1124 | sizeof(td->lastrate)); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1125 | |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 1126 | if (clear_state) |
| 1127 | clear_io_state(td); |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 1128 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1129 | prune_io_piece_log(td); |
| 1130 | |
Jens Axboe | ba0fbe1 | 2007-03-09 14:34:23 +0100 | [diff] [blame] | 1131 | do_io(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1132 | |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 1133 | clear_state = 1; |
| 1134 | |
Jens Axboe | 38d77ca | 2007-03-21 14:20:20 +0100 | [diff] [blame] | 1135 | if (td_read(td) && td->io_bytes[DDIR_READ]) { |
Jens Axboe | 0a4957c | 2009-06-03 10:30:33 +0200 | [diff] [blame] | 1136 | elapsed = utime_since_now(&td->start); |
Jens Axboe | 38d77ca | 2007-03-21 14:20:20 +0100 | [diff] [blame] | 1137 | runtime[DDIR_READ] += elapsed; |
| 1138 | } |
| 1139 | if (td_write(td) && td->io_bytes[DDIR_WRITE]) { |
Jens Axboe | 0a4957c | 2009-06-03 10:30:33 +0200 | [diff] [blame] | 1140 | elapsed = utime_since_now(&td->start); |
Jens Axboe | 38d77ca | 2007-03-21 14:20:20 +0100 | [diff] [blame] | 1141 | runtime[DDIR_WRITE] += elapsed; |
| 1142 | } |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1143 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1144 | if (td->error || td->terminate) |
| 1145 | break; |
| 1146 | |
Shawn Lewis | e84c73a | 2007-08-02 22:19:32 +0200 | [diff] [blame] | 1147 | if (!td->o.do_verify || |
| 1148 | td->o.verify == VERIFY_NONE || |
Jens Axboe | b67740d | 2007-07-26 12:22:30 +0200 | [diff] [blame] | 1149 | (td->io_ops->flags & FIO_UNIDIR)) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1150 | continue; |
| 1151 | |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 1152 | clear_io_state(td); |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 1153 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 1154 | fio_gettime(&td->start, NULL); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1155 | |
| 1156 | do_verify(td); |
| 1157 | |
Jens Axboe | 6900899 | 2006-11-24 13:12:56 +0100 | [diff] [blame] | 1158 | runtime[DDIR_READ] += utime_since_now(&td->start); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1159 | |
| 1160 | if (td->error || td->terminate) |
| 1161 | break; |
| 1162 | } |
| 1163 | |
Jens Axboe | 36dff96 | 2006-11-24 13:29:20 +0100 | [diff] [blame] | 1164 | update_rusage_stat(td); |
ljzhang,Yaxin Hu,Jianchao Tang | 47f0cc4 | 2007-07-26 11:00:29 +0200 | [diff] [blame] | 1165 | td->ts.runtime[0] = (runtime[0] + 999) / 1000; |
| 1166 | td->ts.runtime[1] = (runtime[1] + 999) / 1000; |
Jens Axboe | 756867b | 2007-03-06 15:19:24 +0100 | [diff] [blame] | 1167 | td->ts.total_run_time = mtime_since_now(&td->epoch); |
| 1168 | td->ts.io_bytes[0] = td->io_bytes[0]; |
| 1169 | td->ts.io_bytes[1] = td->io_bytes[1]; |
Jens Axboe | 6900899 | 2006-11-24 13:12:56 +0100 | [diff] [blame] | 1170 | |
Fabio Checconi | d2bbb8d | 2009-05-18 12:54:50 +0200 | [diff] [blame] | 1171 | fio_mutex_down(writeout_mutex); |
Jens Axboe | e3cedca | 2008-11-19 19:57:52 +0100 | [diff] [blame] | 1172 | if (td->ts.bw_log) { |
Jens Axboe | f484470 | 2008-12-09 08:04:29 +0100 | [diff] [blame] | 1173 | if (td->o.bw_log_file) { |
| 1174 | finish_log_named(td, td->ts.bw_log, |
| 1175 | td->o.bw_log_file, "bw"); |
| 1176 | } else |
Jens Axboe | e3cedca | 2008-11-19 19:57:52 +0100 | [diff] [blame] | 1177 | finish_log(td, td->ts.bw_log, "bw"); |
| 1178 | } |
| 1179 | if (td->ts.slat_log) { |
Jens Axboe | f484470 | 2008-12-09 08:04:29 +0100 | [diff] [blame] | 1180 | if (td->o.lat_log_file) { |
| 1181 | finish_log_named(td, td->ts.slat_log, |
Radha Ramachandran | 1ca9573 | 2009-04-29 07:23:26 +0200 | [diff] [blame] | 1182 | td->o.lat_log_file, "slat"); |
Jens Axboe | f484470 | 2008-12-09 08:04:29 +0100 | [diff] [blame] | 1183 | } else |
Jens Axboe | e3cedca | 2008-11-19 19:57:52 +0100 | [diff] [blame] | 1184 | finish_log(td, td->ts.slat_log, "slat"); |
| 1185 | } |
| 1186 | if (td->ts.clat_log) { |
Jens Axboe | f484470 | 2008-12-09 08:04:29 +0100 | [diff] [blame] | 1187 | if (td->o.lat_log_file) { |
| 1188 | finish_log_named(td, td->ts.clat_log, |
| 1189 | td->o.lat_log_file, "clat"); |
| 1190 | } else |
Jens Axboe | e3cedca | 2008-11-19 19:57:52 +0100 | [diff] [blame] | 1191 | finish_log(td, td->ts.clat_log, "clat"); |
| 1192 | } |
Fabio Checconi | d2bbb8d | 2009-05-18 12:54:50 +0200 | [diff] [blame] | 1193 | fio_mutex_up(writeout_mutex); |
Jens Axboe | 398e8c4 | 2009-05-20 12:10:02 +0200 | [diff] [blame] | 1194 | if (td->o.exec_postrun) |
| 1195 | exec_string(td->o.exec_postrun); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1196 | |
| 1197 | if (exitall_on_terminate) |
Jens Axboe | 390c40e | 2007-03-05 12:35:29 +0100 | [diff] [blame] | 1198 | terminate_threads(td->groupid); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1199 | |
| 1200 | err: |
Jens Axboe | 5bf13a5 | 2007-02-17 01:51:47 +0100 | [diff] [blame] | 1201 | if (td->error) |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 1202 | 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] | 1203 | td->verror); |
Jens Axboe | b4707ed | 2009-11-04 12:51:17 +0100 | [diff] [blame] | 1204 | |
| 1205 | if (td->o.verify_async) |
| 1206 | verify_async_exit(td); |
| 1207 | |
Jens Axboe | 24ffd2c | 2008-03-01 15:47:08 +0100 | [diff] [blame] | 1208 | close_and_free_files(td); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 1209 | close_ioengine(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1210 | cleanup_io_u(td); |
Jens Axboe | a696fa2 | 2009-12-04 10:05:02 +0100 | [diff] [blame^] | 1211 | cgroup_shutdown(td); |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 1212 | |
Jens Axboe | d2ce18b | 2008-12-12 20:51:40 +0100 | [diff] [blame] | 1213 | if (td->o.cpumask_set) { |
| 1214 | int ret = fio_cpuset_exit(&td->o.cpumask); |
| 1215 | |
| 1216 | td_verror(td, ret, "fio_cpuset_exit"); |
| 1217 | } |
Jens Axboe | 4e78e40 | 2008-12-12 20:40:27 +0100 | [diff] [blame] | 1218 | |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 1219 | /* |
| 1220 | * do this very late, it will log file closing as well |
| 1221 | */ |
| 1222 | if (td->o.write_iolog_file) |
| 1223 | write_iolog_close(td); |
| 1224 | |
Jens Axboe | d23bb32 | 2007-03-23 15:57:56 +0100 | [diff] [blame] | 1225 | options_mem_free(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1226 | td_set_runstate(td, TD_EXITED); |
Jens Axboe | 43d7680 | 2007-02-21 16:35:29 +0100 | [diff] [blame] | 1227 | return (void *) (unsigned long) td->error; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1228 | } |
| 1229 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1230 | /* |
| 1231 | * We cannot pass the td data into a forked process, so attach the td and |
| 1232 | * pass it to the thread worker. |
| 1233 | */ |
Jens Axboe | a641814 | 2007-02-17 04:47:18 +0100 | [diff] [blame] | 1234 | static int fork_main(int shmid, int offset) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1235 | { |
| 1236 | struct thread_data *td; |
Jens Axboe | a641814 | 2007-02-17 04:47:18 +0100 | [diff] [blame] | 1237 | void *data, *ret; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1238 | |
| 1239 | data = shmat(shmid, NULL, 0); |
| 1240 | if (data == (void *) -1) { |
Jens Axboe | a641814 | 2007-02-17 04:47:18 +0100 | [diff] [blame] | 1241 | int __err = errno; |
| 1242 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1243 | perror("shmat"); |
Jens Axboe | a641814 | 2007-02-17 04:47:18 +0100 | [diff] [blame] | 1244 | return __err; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | td = data + offset * sizeof(struct thread_data); |
Jens Axboe | a641814 | 2007-02-17 04:47:18 +0100 | [diff] [blame] | 1248 | ret = thread_main(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1249 | shmdt(data); |
Jens Axboe | 43d7680 | 2007-02-21 16:35:29 +0100 | [diff] [blame] | 1250 | return (int) (unsigned long) ret; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1251 | } |
| 1252 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1253 | /* |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1254 | * Run over the job map and reap the threads that have exited, if any. |
| 1255 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1256 | static void reap_threads(int *nr_running, int *t_rate, int *m_rate) |
| 1257 | { |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 1258 | struct thread_data *td; |
Jens Axboe | 1f809d1 | 2007-10-25 18:34:02 +0200 | [diff] [blame] | 1259 | int i, cputhreads, realthreads, pending, status, ret; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1260 | |
| 1261 | /* |
| 1262 | * reap exited threads (TD_EXITED -> TD_REAPED) |
| 1263 | */ |
Jens Axboe | 1f809d1 | 2007-10-25 18:34:02 +0200 | [diff] [blame] | 1264 | realthreads = pending = cputhreads = 0; |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 1265 | for_each_td(td, i) { |
Jens Axboe | 3707f45 | 2007-02-22 12:26:57 +0100 | [diff] [blame] | 1266 | int flags = 0; |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1267 | |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 1268 | /* |
| 1269 | * ->io_ops is NULL for a thread that has closed its |
| 1270 | * io engine |
| 1271 | */ |
Jens Axboe | ba0fbe1 | 2007-03-09 14:34:23 +0100 | [diff] [blame] | 1272 | if (td->io_ops && !strcmp(td->io_ops->name, "cpuio")) |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 1273 | cputhreads++; |
Jens Axboe | 1f809d1 | 2007-10-25 18:34:02 +0200 | [diff] [blame] | 1274 | else |
| 1275 | realthreads++; |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 1276 | |
Aaron Carroll | a1dedc1 | 2008-02-20 09:14:12 +0100 | [diff] [blame] | 1277 | if (!td->pid) { |
| 1278 | pending++; |
| 1279 | continue; |
| 1280 | } |
| 1281 | if (td->runstate == TD_REAPED) |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1282 | continue; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1283 | if (td->o.use_thread) { |
Jens Axboe | 3707f45 | 2007-02-22 12:26:57 +0100 | [diff] [blame] | 1284 | if (td->runstate == TD_EXITED) { |
| 1285 | td_set_runstate(td, TD_REAPED); |
| 1286 | goto reaped; |
| 1287 | } |
| 1288 | continue; |
| 1289 | } |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1290 | |
| 1291 | flags = WNOHANG; |
| 1292 | if (td->runstate == TD_EXITED) |
| 1293 | flags = 0; |
| 1294 | |
| 1295 | /* |
| 1296 | * check if someone quit or got killed in an unusual way |
| 1297 | */ |
| 1298 | ret = waitpid(td->pid, &status, flags); |
Jens Axboe | 3707f45 | 2007-02-22 12:26:57 +0100 | [diff] [blame] | 1299 | if (ret < 0) { |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1300 | if (errno == ECHILD) { |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 1301 | log_err("fio: pid=%d disappeared %d\n", |
| 1302 | (int) td->pid, td->runstate); |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1303 | td_set_runstate(td, TD_REAPED); |
| 1304 | goto reaped; |
| 1305 | } |
| 1306 | perror("waitpid"); |
| 1307 | } else if (ret == td->pid) { |
| 1308 | if (WIFSIGNALED(status)) { |
Jens Axboe | fab6aa7 | 2007-02-17 06:19:24 +0100 | [diff] [blame] | 1309 | int sig = WTERMSIG(status); |
| 1310 | |
Jens Axboe | d924494 | 2007-03-05 12:32:32 +0100 | [diff] [blame] | 1311 | if (sig != SIGQUIT) |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1312 | log_err("fio: pid=%d, got signal=%d\n", |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 1313 | (int) td->pid, sig); |
Jens Axboe | fab6aa7 | 2007-02-17 06:19:24 +0100 | [diff] [blame] | 1314 | td_set_runstate(td, TD_REAPED); |
| 1315 | goto reaped; |
| 1316 | } |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1317 | if (WIFEXITED(status)) { |
| 1318 | if (WEXITSTATUS(status) && !td->error) |
| 1319 | td->error = WEXITSTATUS(status); |
Jens Axboe | fab6aa7 | 2007-02-17 06:19:24 +0100 | [diff] [blame] | 1320 | |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1321 | td_set_runstate(td, TD_REAPED); |
| 1322 | goto reaped; |
Jens Axboe | a641814 | 2007-02-17 04:47:18 +0100 | [diff] [blame] | 1323 | } |
| 1324 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1325 | |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1326 | /* |
| 1327 | * thread is not dead, continue |
| 1328 | */ |
gurudas pai | e48676b | 2007-04-11 12:44:27 +0200 | [diff] [blame] | 1329 | pending++; |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1330 | continue; |
Jens Axboe | fab6aa7 | 2007-02-17 06:19:24 +0100 | [diff] [blame] | 1331 | reaped: |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1332 | (*nr_running)--; |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 1333 | (*m_rate) -= (td->o.ratemin[0] + td->o.ratemin[1]); |
| 1334 | (*t_rate) -= (td->o.rate[0] + td->o.rate[1]); |
Jens Axboe | 6f88bcb | 2008-03-19 10:29:07 +0100 | [diff] [blame] | 1335 | if (!td->pid) |
| 1336 | pending--; |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1337 | |
| 1338 | if (td->error) |
| 1339 | exit_value++; |
Jens Axboe | d3eeeab | 2008-04-06 12:19:05 +0200 | [diff] [blame] | 1340 | |
| 1341 | done_secs += mtime_since_now(&td->epoch) / 1000; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1342 | } |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 1343 | |
Jens Axboe | 1f809d1 | 2007-10-25 18:34:02 +0200 | [diff] [blame] | 1344 | if (*nr_running == cputhreads && !pending && realthreads) |
Jens Axboe | 390c40e | 2007-03-05 12:35:29 +0100 | [diff] [blame] | 1345 | terminate_threads(TERMINATE_ALL); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1346 | } |
| 1347 | |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 1348 | static void *gtod_thread_main(void *data) |
| 1349 | { |
| 1350 | fio_mutex_up(startup_mutex); |
| 1351 | |
| 1352 | /* |
| 1353 | * As long as we have jobs around, update the clock. It would be nice |
| 1354 | * to have some way of NOT hammering that CPU with gettimeofday(), |
| 1355 | * but I'm not sure what to use outside of a simple CPU nop to relax |
| 1356 | * it - we don't want to lose precision. |
| 1357 | */ |
| 1358 | while (threads) { |
| 1359 | fio_gtod_update(); |
| 1360 | nop; |
| 1361 | } |
| 1362 | |
| 1363 | return NULL; |
| 1364 | } |
| 1365 | |
| 1366 | static int fio_start_gtod_thread(void) |
| 1367 | { |
Jens Axboe | f42c153 | 2009-01-05 19:07:13 +0100 | [diff] [blame] | 1368 | int ret; |
| 1369 | |
| 1370 | ret = pthread_create(>od_thread, NULL, gtod_thread_main, NULL); |
| 1371 | if (ret) { |
| 1372 | log_err("Can't create gtod thread: %s\n", strerror(ret)); |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 1373 | return 1; |
| 1374 | } |
Jens Axboe | f42c153 | 2009-01-05 19:07:13 +0100 | [diff] [blame] | 1375 | |
| 1376 | ret = pthread_detach(gtod_thread); |
| 1377 | if (ret) { |
| 1378 | log_err("Can't detatch gtod thread: %s\n", strerror(ret)); |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 1379 | return 1; |
| 1380 | } |
| 1381 | |
Jens Axboe | 29adda3 | 2009-01-05 19:06:39 +0100 | [diff] [blame] | 1382 | dprint(FD_MUTEX, "wait on startup_mutex\n"); |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 1383 | fio_mutex_down(startup_mutex); |
Jens Axboe | 29adda3 | 2009-01-05 19:06:39 +0100 | [diff] [blame] | 1384 | dprint(FD_MUTEX, "done waiting on startup_mutex\n"); |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 1385 | return 0; |
| 1386 | } |
| 1387 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1388 | /* |
| 1389 | * Main function for kicking off and reaping jobs, as needed. |
| 1390 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1391 | static void run_threads(void) |
| 1392 | { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1393 | struct thread_data *td; |
| 1394 | unsigned long spent; |
| 1395 | int i, todo, nr_running, m_rate, t_rate, nr_started; |
Jens Axboe | fcb6ade | 2006-05-31 12:14:35 +0200 | [diff] [blame] | 1396 | |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 1397 | if (fio_pin_memory()) |
| 1398 | return; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1399 | |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 1400 | if (fio_gtod_offload && fio_start_gtod_thread()) |
| 1401 | return; |
| 1402 | |
Jens Axboe | c6ae0a5 | 2006-06-12 11:04:44 +0200 | [diff] [blame] | 1403 | if (!terse_output) { |
Jens Axboe | 9cedf16 | 2007-03-12 11:29:30 +0100 | [diff] [blame] | 1404 | printf("Starting "); |
| 1405 | if (nr_thread) |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1406 | printf("%d thread%s", nr_thread, |
| 1407 | nr_thread > 1 ? "s" : ""); |
Jens Axboe | 9cedf16 | 2007-03-12 11:29:30 +0100 | [diff] [blame] | 1408 | if (nr_process) { |
| 1409 | if (nr_thread) |
| 1410 | printf(" and "); |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1411 | printf("%d process%s", nr_process, |
| 1412 | nr_process > 1 ? "es" : ""); |
Jens Axboe | 9cedf16 | 2007-03-12 11:29:30 +0100 | [diff] [blame] | 1413 | } |
| 1414 | printf("\n"); |
Jens Axboe | c6ae0a5 | 2006-06-12 11:04:44 +0200 | [diff] [blame] | 1415 | fflush(stdout); |
| 1416 | } |
Jens Axboe | c04f7ec | 2006-05-31 10:13:16 +0200 | [diff] [blame] | 1417 | |
Jens Axboe | 697a606 | 2008-05-31 00:04:45 +0200 | [diff] [blame] | 1418 | set_sig_handlers(); |
Jens Axboe | 4efa970 | 2006-05-31 11:56:49 +0200 | [diff] [blame] | 1419 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1420 | todo = thread_number; |
| 1421 | nr_running = 0; |
| 1422 | nr_started = 0; |
| 1423 | m_rate = t_rate = 0; |
| 1424 | |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 1425 | for_each_td(td, i) { |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 1426 | print_status_init(td->thread_number - 1); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1427 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1428 | if (!td->o.create_serialize) { |
Jens Axboe | 380cf26 | 2007-01-11 14:01:27 +0100 | [diff] [blame] | 1429 | init_disk_util(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1430 | continue; |
Jens Axboe | 380cf26 | 2007-01-11 14:01:27 +0100 | [diff] [blame] | 1431 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1432 | |
| 1433 | /* |
| 1434 | * do file setup here so it happens sequentially, |
| 1435 | * we don't want X number of threads getting their |
| 1436 | * client data interspersed on disk |
| 1437 | */ |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 1438 | if (setup_files(td)) { |
Jens Axboe | 5bf13a5 | 2007-02-17 01:51:47 +0100 | [diff] [blame] | 1439 | exit_value++; |
| 1440 | if (td->error) |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 1441 | log_err("fio: pid=%d, err=%d/%s\n", |
| 1442 | (int) td->pid, td->error, td->verror); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1443 | td_set_runstate(td, TD_REAPED); |
| 1444 | todo--; |
Jens Axboe | c69e887 | 2008-03-01 18:50:48 +0100 | [diff] [blame] | 1445 | } else { |
| 1446 | struct fio_file *f; |
| 1447 | unsigned int i; |
| 1448 | |
| 1449 | /* |
| 1450 | * for sharing to work, each job must always open |
| 1451 | * its own files. so close them, if we opened them |
| 1452 | * for creation |
| 1453 | */ |
Jens Axboe | 22a57ba | 2009-06-09 14:34:35 +0200 | [diff] [blame] | 1454 | for_each_file(td, f, i) { |
| 1455 | if (fio_file_open(f)) |
| 1456 | td_io_close_file(td, f); |
Jens Axboe | 22a57ba | 2009-06-09 14:34:35 +0200 | [diff] [blame] | 1457 | } |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1458 | } |
Jens Axboe | 380cf26 | 2007-01-11 14:01:27 +0100 | [diff] [blame] | 1459 | |
| 1460 | init_disk_util(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1461 | } |
| 1462 | |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1463 | set_genesis_time(); |
| 1464 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1465 | while (todo) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1466 | struct thread_data *map[MAX_JOBS]; |
| 1467 | struct timeval this_start; |
| 1468 | int this_jobs = 0, left; |
| 1469 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1470 | /* |
| 1471 | * create threads (TD_NOT_CREATED -> TD_CREATED) |
| 1472 | */ |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 1473 | for_each_td(td, i) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1474 | if (td->runstate != TD_NOT_CREATED) |
| 1475 | continue; |
| 1476 | |
| 1477 | /* |
| 1478 | * never got a chance to start, killed by other |
| 1479 | * thread for some reason |
| 1480 | */ |
| 1481 | if (td->terminate) { |
| 1482 | todo--; |
| 1483 | continue; |
| 1484 | } |
| 1485 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1486 | if (td->o.start_delay) { |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 1487 | spent = mtime_since_genesis(); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1488 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1489 | if (td->o.start_delay * 1000 > spent) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1490 | continue; |
| 1491 | } |
| 1492 | |
Jens Axboe | 6f88bcb | 2008-03-19 10:29:07 +0100 | [diff] [blame] | 1493 | if (td->o.stonewall && (nr_started || nr_running)) { |
| 1494 | dprint(FD_PROCESS, "%s: stonewall wait\n", |
| 1495 | td->o.name); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1496 | break; |
Jens Axboe | 6f88bcb | 2008-03-19 10:29:07 +0100 | [diff] [blame] | 1497 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1498 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1499 | /* |
| 1500 | * Set state to created. Thread will transition |
| 1501 | * to TD_INITIALIZED when it's done setting up. |
| 1502 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1503 | td_set_runstate(td, TD_CREATED); |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1504 | map[this_jobs++] = td; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1505 | nr_started++; |
| 1506 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1507 | if (td->o.use_thread) { |
Jens Axboe | f42c153 | 2009-01-05 19:07:13 +0100 | [diff] [blame] | 1508 | int ret; |
| 1509 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 1510 | dprint(FD_PROCESS, "will pthread_create\n"); |
Jens Axboe | f42c153 | 2009-01-05 19:07:13 +0100 | [diff] [blame] | 1511 | ret = pthread_create(&td->thread, NULL, |
| 1512 | thread_main, td); |
| 1513 | if (ret) { |
| 1514 | log_err("pthread_create: %s\n", |
| 1515 | strerror(ret)); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1516 | nr_started--; |
Jens Axboe | c8bb6fa | 2007-03-12 11:03:04 +0100 | [diff] [blame] | 1517 | break; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1518 | } |
Jens Axboe | f42c153 | 2009-01-05 19:07:13 +0100 | [diff] [blame] | 1519 | ret = pthread_detach(td->thread); |
| 1520 | if (ret) |
| 1521 | log_err("pthread_detach: %s", |
| 1522 | strerror(ret)); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1523 | } else { |
Jens Axboe | 5e1d306 | 2008-05-23 11:55:53 +0200 | [diff] [blame] | 1524 | pid_t pid; |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 1525 | dprint(FD_PROCESS, "will fork\n"); |
Jens Axboe | 5e1d306 | 2008-05-23 11:55:53 +0200 | [diff] [blame] | 1526 | pid = fork(); |
| 1527 | if (!pid) { |
Jens Axboe | a641814 | 2007-02-17 04:47:18 +0100 | [diff] [blame] | 1528 | int ret = fork_main(shm_id, i); |
| 1529 | |
Jens Axboe | 5e1d306 | 2008-05-23 11:55:53 +0200 | [diff] [blame] | 1530 | _exit(ret); |
| 1531 | } else if (i == fio_debug_jobno) |
| 1532 | *fio_debug_jobp = pid; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1533 | } |
Jens Axboe | 29adda3 | 2009-01-05 19:06:39 +0100 | [diff] [blame] | 1534 | dprint(FD_MUTEX, "wait on startup_mutex\n"); |
Jens Axboe | 656b139 | 2009-07-01 23:02:10 +0200 | [diff] [blame] | 1535 | if (fio_mutex_down_timeout(startup_mutex, 10)) { |
| 1536 | log_err("fio: job startup hung? exiting.\n"); |
| 1537 | terminate_threads(TERMINATE_ALL); |
| 1538 | fio_abort = 1; |
| 1539 | nr_started--; |
| 1540 | break; |
| 1541 | } |
Jens Axboe | 29adda3 | 2009-01-05 19:06:39 +0100 | [diff] [blame] | 1542 | dprint(FD_MUTEX, "done waiting on startup_mutex\n"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | /* |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1546 | * Wait for the started threads to transition to |
| 1547 | * TD_INITIALIZED. |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1548 | */ |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 1549 | fio_gettime(&this_start, NULL); |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1550 | left = this_jobs; |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame] | 1551 | while (left && !fio_abort) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1552 | if (mtime_since_now(&this_start) > JOB_START_TIMEOUT) |
| 1553 | break; |
| 1554 | |
| 1555 | usleep(100000); |
| 1556 | |
| 1557 | for (i = 0; i < this_jobs; i++) { |
| 1558 | td = map[i]; |
| 1559 | if (!td) |
| 1560 | continue; |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 1561 | if (td->runstate == TD_INITIALIZED) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1562 | map[i] = NULL; |
| 1563 | left--; |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 1564 | } else if (td->runstate >= TD_EXITED) { |
| 1565 | map[i] = NULL; |
| 1566 | left--; |
| 1567 | todo--; |
| 1568 | nr_running++; /* work-around... */ |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1569 | } |
| 1570 | } |
| 1571 | } |
| 1572 | |
| 1573 | if (left) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 1574 | log_err("fio: %d jobs failed to start\n", left); |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1575 | for (i = 0; i < this_jobs; i++) { |
| 1576 | td = map[i]; |
| 1577 | if (!td) |
| 1578 | continue; |
| 1579 | kill(td->pid, SIGTERM); |
| 1580 | } |
| 1581 | break; |
| 1582 | } |
| 1583 | |
| 1584 | /* |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 1585 | * start created threads (TD_INITIALIZED -> TD_RUNNING). |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1586 | */ |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 1587 | for_each_td(td, i) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1588 | if (td->runstate != TD_INITIALIZED) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1589 | continue; |
| 1590 | |
Jens Axboe | b29ee5b | 2008-09-11 10:17:26 +0200 | [diff] [blame] | 1591 | if (in_ramp_time(td)) |
| 1592 | td_set_runstate(td, TD_RAMP); |
| 1593 | else |
| 1594 | td_set_runstate(td, TD_RUNNING); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1595 | nr_running++; |
| 1596 | nr_started--; |
Jens Axboe | 581e714 | 2009-06-09 12:47:16 +0200 | [diff] [blame] | 1597 | m_rate += td->o.ratemin[0] + td->o.ratemin[1]; |
| 1598 | t_rate += td->o.rate[0] + td->o.rate[1]; |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1599 | todo--; |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 1600 | fio_mutex_up(td->mutex); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | reap_threads(&nr_running, &t_rate, &m_rate); |
| 1604 | |
| 1605 | if (todo) |
| 1606 | usleep(100000); |
| 1607 | } |
| 1608 | |
| 1609 | while (nr_running) { |
| 1610 | reap_threads(&nr_running, &t_rate, &m_rate); |
| 1611 | usleep(10000); |
| 1612 | } |
| 1613 | |
| 1614 | update_io_ticks(); |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 1615 | fio_unpin_memory(); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1616 | } |
| 1617 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1618 | int main(int argc, char *argv[]) |
| 1619 | { |
Jens Axboe | 29d610e | 2007-02-06 13:25:33 +0100 | [diff] [blame] | 1620 | long ps; |
| 1621 | |
Jens Axboe | 2e5cdb1 | 2008-03-01 18:52:49 +0100 | [diff] [blame] | 1622 | sinit(); |
| 1623 | |
Jens Axboe | dbe1125 | 2007-02-11 00:19:51 +0100 | [diff] [blame] | 1624 | /* |
| 1625 | * We need locale for number printing, if it isn't set then just |
| 1626 | * go with the US format. |
| 1627 | */ |
| 1628 | if (!getenv("LC_NUMERIC")) |
| 1629 | setlocale(LC_NUMERIC, "en_US"); |
| 1630 | |
Jens Axboe | 29d610e | 2007-02-06 13:25:33 +0100 | [diff] [blame] | 1631 | ps = sysconf(_SC_PAGESIZE); |
| 1632 | if (ps < 0) { |
| 1633 | log_err("Failed to get page size\n"); |
| 1634 | return 1; |
| 1635 | } |
| 1636 | |
Jens Axboe | cfc99db | 2007-03-14 10:34:47 +0100 | [diff] [blame] | 1637 | page_size = ps; |
Jens Axboe | 29d610e | 2007-02-06 13:25:33 +0100 | [diff] [blame] | 1638 | page_mask = ps - 1; |
| 1639 | |
Jens Axboe | 74929ac | 2009-08-05 11:42:37 +0200 | [diff] [blame] | 1640 | fio_keywords_init(); |
| 1641 | |
| 1642 | if (parse_options(argc, argv)) |
| 1643 | return 1; |
| 1644 | |
| 1645 | if (!thread_number) |
| 1646 | return 0; |
| 1647 | |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 1648 | if (write_bw_log) { |
| 1649 | setup_log(&agg_io_log[DDIR_READ]); |
| 1650 | setup_log(&agg_io_log[DDIR_WRITE]); |
| 1651 | } |
| 1652 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 1653 | startup_mutex = fio_mutex_init(0); |
Fabio Checconi | d2bbb8d | 2009-05-18 12:54:50 +0200 | [diff] [blame] | 1654 | writeout_mutex = fio_mutex_init(1); |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 1655 | |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 1656 | set_genesis_time(); |
| 1657 | |
Jens Axboe | ccb0fa2 | 2008-05-30 23:18:00 +0200 | [diff] [blame] | 1658 | status_timer_arm(); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1659 | |
| 1660 | run_threads(); |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame] | 1661 | |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 1662 | if (!fio_abort) { |
Jens Axboe | 6ce15a3 | 2007-01-12 09:04:41 +0100 | [diff] [blame] | 1663 | show_run_stats(); |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 1664 | if (write_bw_log) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1665 | __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log"); |
| 1666 | __finish_log(agg_io_log[DDIR_WRITE], |
| 1667 | "agg-write_bw.log"); |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 1668 | } |
| 1669 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1670 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 1671 | fio_mutex_remove(startup_mutex); |
Fabio Checconi | d2bbb8d | 2009-05-18 12:54:50 +0200 | [diff] [blame] | 1672 | fio_mutex_remove(writeout_mutex); |
Jens Axboe | 437c9b7 | 2007-02-13 18:20:37 +0100 | [diff] [blame] | 1673 | return exit_value; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1674 | } |