Commit | Line | Data |
---|---|---|
ebac4655 JA |
1 | /* |
2 | * fio - the flexible io tester | |
3 | * | |
4 | * Copyright (C) 2005 Jens Axboe <axboe@suse.de> | |
aae22ca7 | 5 | * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk> |
ebac4655 | 6 | * |
8e9fe637 JA |
7 | * The license below covers all files distributed with fio unless otherwise |
8 | * noted in the file itself. | |
9 | * | |
ebac4655 | 10 | * This program is free software; you can redistribute it and/or modify |
8e9fe637 JA |
11 | * it under the terms of the GNU General Public License version 2 as |
12 | * published by the Free Software Foundation. | |
ebac4655 JA |
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 | */ | |
ebac4655 JA |
24 | #include <unistd.h> |
25 | #include <fcntl.h> | |
26 | #include <string.h> | |
9b836561 | 27 | #include <limits.h> |
ebac4655 JA |
28 | #include <signal.h> |
29 | #include <time.h> | |
dbe1125e | 30 | #include <locale.h> |
36167d82 | 31 | #include <assert.h> |
a5b01f1b | 32 | #include <time.h> |
ebac4655 JA |
33 | #include <sys/stat.h> |
34 | #include <sys/wait.h> | |
35 | #include <sys/ipc.h> | |
36 | #include <sys/shm.h> | |
ebac4655 JA |
37 | #include <sys/mman.h> |
38 | ||
39 | #include "fio.h" | |
210f43b3 | 40 | #include "hash.h" |
2e5cdb11 | 41 | #include "smalloc.h" |
4f5af7b2 | 42 | #include "verify.h" |
a917a8b3 | 43 | #include "trim.h" |
7c9b1bce | 44 | #include "diskutil.h" |
a696fa2a | 45 | #include "cgroup.h" |
07b3232d | 46 | #include "profile.h" |
d54a144d | 47 | #include "lib/rand.h" |
91aea6ec | 48 | #include "memalign.h" |
009b1be4 | 49 | #include "server.h" |
ebac4655 | 50 | |
cfc99db7 JA |
51 | unsigned long page_mask; |
52 | unsigned long page_size; | |
d529ee19 JA |
53 | |
54 | #define PAGE_ALIGN(buf) \ | |
29d610e1 | 55 | (char *) (((unsigned long) (buf) + page_mask) & ~page_mask) |
ebac4655 JA |
56 | |
57 | int groupid = 0; | |
58 | int thread_number = 0; | |
9cedf167 JA |
59 | int nr_process = 0; |
60 | int nr_thread = 0; | |
ebac4655 | 61 | int shm_id = 0; |
53cdc686 | 62 | int temp_stall_ts; |
d3eeeabc | 63 | unsigned long done_secs = 0; |
ebac4655 | 64 | |
cdd18ad8 | 65 | static struct fio_mutex *startup_mutex; |
d2bbb8d9 | 66 | static struct fio_mutex *writeout_mutex; |
6ce15a32 | 67 | static volatile int fio_abort; |
437c9b71 | 68 | static int exit_value; |
be4ecfdf | 69 | static pthread_t gtod_thread; |
7985701b | 70 | static pthread_t disk_util_thread; |
dae5341c | 71 | static struct flist_head *cgroup_list; |
6adb38a1 | 72 | static char *cgroup_mnt; |
ebac4655 | 73 | |
d3cc4ebf JA |
74 | unsigned long arch_flags = 0; |
75 | ||
bb3884d8 JA |
76 | struct io_log *agg_io_log[2]; |
77 | ||
75154845 | 78 | #define JOB_START_TIMEOUT (5 * 1000) |
ebac4655 | 79 | |
cca84643 JA |
80 | static const char *fio_os_strings[os_nr] = { |
81 | "Invalid", | |
82 | "Linux", | |
83 | "AIX", | |
84 | "FreeBSD", | |
85 | "HP-UX", | |
86 | "OSX", | |
87 | "NetBSD", | |
88 | "Solaris", | |
89 | "Windows" | |
90 | }; | |
91 | ||
92 | static const char *fio_arch_strings[arch_nr] = { | |
93 | "Invalid", | |
94 | "x86-64", | |
95 | "x86", | |
96 | "ppc", | |
97 | "ia64", | |
98 | "s390", | |
99 | "alpha", | |
100 | "sparc", | |
101 | "sparc64", | |
102 | "arm", | |
103 | "sh", | |
104 | "hppa", | |
105 | "generic" | |
106 | }; | |
107 | ||
108 | const char *fio_get_os_string(int nr) | |
109 | { | |
110 | if (nr < os_nr) | |
111 | return fio_os_strings[nr]; | |
112 | ||
113 | return NULL; | |
114 | } | |
115 | ||
116 | const char *fio_get_arch_string(int nr) | |
117 | { | |
118 | if (nr < arch_nr) | |
119 | return fio_arch_strings[nr]; | |
120 | ||
121 | return NULL; | |
122 | } | |
123 | ||
b29ee5b3 | 124 | void td_set_runstate(struct thread_data *td, int runstate) |
6ce15a32 | 125 | { |
d8fab1b6 JA |
126 | if (td->runstate == runstate) |
127 | return; | |
128 | ||
5921e80c JA |
129 | dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", (int) td->pid, |
130 | td->runstate, runstate); | |
6ce15a32 JA |
131 | td->runstate = runstate; |
132 | } | |
133 | ||
cc0df00a | 134 | void fio_terminate_threads(int group_id) |
ebac4655 | 135 | { |
34572e28 | 136 | struct thread_data *td; |
ebac4655 JA |
137 | int i; |
138 | ||
6f88bcb0 JA |
139 | dprint(FD_PROCESS, "terminate group_id=%d\n", group_id); |
140 | ||
34572e28 | 141 | for_each_td(td, i) { |
ebac4655 | 142 | if (group_id == TERMINATE_ALL || groupid == td->groupid) { |
6f88bcb0 | 143 | dprint(FD_PROCESS, "setting terminate on %s/%d\n", |
5921e80c | 144 | td->o.name, (int) td->pid); |
ad830ed7 JA |
145 | td->terminate = 1; |
146 | td->o.start_delay = 0; | |
147 | ||
7a93ab19 JA |
148 | /* |
149 | * if the thread is running, just let it exit | |
150 | */ | |
6aa27816 MC |
151 | if (!td->pid) |
152 | continue; | |
153 | else if (td->runstate < TD_RAMP) | |
03e20d68 | 154 | kill(td->pid, SIGTERM); |
f63f7f3b JA |
155 | else { |
156 | struct ioengine_ops *ops = td->io_ops; | |
157 | ||
03e20d68 BC |
158 | if (ops && (ops->flags & FIO_SIGTERM)) |
159 | kill(td->pid, SIGTERM); | |
f63f7f3b | 160 | } |
ebac4655 JA |
161 | } |
162 | } | |
163 | } | |
164 | ||
697a606c JA |
165 | static void sig_int(int sig) |
166 | { | |
167 | if (threads) { | |
4ceb30d4 | 168 | log_info("\nfio: terminating on signal %d\n", sig); |
009b1be4 | 169 | exit_backend = 1; |
5ec10eaa | 170 | fflush(stdout); |
deaa1af2 | 171 | exit_value = 128; |
cc0df00a | 172 | fio_terminate_threads(TERMINATE_ALL); |
ebac4655 JA |
173 | } |
174 | } | |
175 | ||
7985701b | 176 | static void *disk_thread_main(void *data) |
a5b01f1b | 177 | { |
7985701b JA |
178 | fio_mutex_up(startup_mutex); |
179 | ||
180 | while (threads) { | |
181 | usleep(DISK_UTIL_MSEC * 1000); | |
9a793c2f JA |
182 | if (!threads) |
183 | break; | |
7985701b | 184 | update_io_ticks(); |
cf451d1e | 185 | |
af9c9fb3 | 186 | if (!is_backend) |
cf451d1e | 187 | print_thread_status(); |
7985701b JA |
188 | } |
189 | ||
190 | return NULL; | |
a5b01f1b JA |
191 | } |
192 | ||
7985701b | 193 | static int create_disk_util_thread(void) |
a5b01f1b | 194 | { |
7985701b | 195 | int ret; |
a5b01f1b | 196 | |
69204d6e | 197 | ret = pthread_create(&disk_util_thread, NULL, disk_thread_main, NULL); |
7985701b JA |
198 | if (ret) { |
199 | log_err("Can't create disk util thread: %s\n", strerror(ret)); | |
200 | return 1; | |
201 | } | |
a5b01f1b | 202 | |
7985701b JA |
203 | ret = pthread_detach(disk_util_thread); |
204 | if (ret) { | |
205 | log_err("Can't detatch disk util thread: %s\n", strerror(ret)); | |
206 | return 1; | |
207 | } | |
03e20d68 | 208 | |
7985701b JA |
209 | dprint(FD_MUTEX, "wait on startup_mutex\n"); |
210 | fio_mutex_down(startup_mutex); | |
211 | dprint(FD_MUTEX, "done waiting on startup_mutex\n"); | |
212 | return 0; | |
a5b01f1b JA |
213 | } |
214 | ||
697a606c JA |
215 | static void set_sig_handlers(void) |
216 | { | |
217 | struct sigaction act; | |
218 | ||
697a606c JA |
219 | memset(&act, 0, sizeof(act)); |
220 | act.sa_handler = sig_int; | |
221 | act.sa_flags = SA_RESTART; | |
222 | sigaction(SIGINT, &act, NULL); | |
de79c915 | 223 | |
5f8f9726 | 224 | memset(&act, 0, sizeof(act)); |
b20b6be4 | 225 | act.sa_handler = sig_int; |
5f8f9726 | 226 | act.sa_flags = SA_RESTART; |
03e20d68 | 227 | sigaction(SIGTERM, &act, NULL); |
cc0df00a JA |
228 | |
229 | if (is_backend) { | |
230 | memset(&act, 0, sizeof(act)); | |
231 | act.sa_handler = sig_int; | |
232 | act.sa_flags = SA_RESTART; | |
233 | sigaction(SIGPIPE, &act, NULL); | |
234 | } | |
697a606c JA |
235 | } |
236 | ||
906c8d75 JA |
237 | /* |
238 | * Check if we are above the minimum rate given. | |
239 | */ | |
581e7141 | 240 | static int __check_min_rate(struct thread_data *td, struct timeval *now, |
bd93e34b | 241 | enum fio_ddir ddir) |
ebac4655 | 242 | { |
0904200b | 243 | unsigned long long bytes = 0; |
4e991c23 | 244 | unsigned long iops = 0; |
ebac4655 JA |
245 | unsigned long spent; |
246 | unsigned long rate; | |
581e7141 JA |
247 | unsigned int ratemin = 0; |
248 | unsigned int rate_iops = 0; | |
249 | unsigned int rate_iops_min = 0; | |
ebac4655 | 250 | |
ff58fced JA |
251 | assert(ddir_rw(ddir)); |
252 | ||
d982d873 JA |
253 | if (!td->o.ratemin[ddir] && !td->o.rate_iops_min[ddir]) |
254 | return 0; | |
255 | ||
ebac4655 JA |
256 | /* |
257 | * allow a 2 second settle period in the beginning | |
258 | */ | |
259 | if (mtime_since(&td->start, now) < 2000) | |
260 | return 0; | |
261 | ||
c8eeb9df | 262 | iops += td->this_io_blocks[ddir]; |
581e7141 JA |
263 | bytes += td->this_io_bytes[ddir]; |
264 | ratemin += td->o.ratemin[ddir]; | |
265 | rate_iops += td->o.rate_iops[ddir]; | |
266 | rate_iops_min += td->o.rate_iops_min[ddir]; | |
0904200b | 267 | |
ebac4655 JA |
268 | /* |
269 | * if rate blocks is set, sample is running | |
270 | */ | |
581e7141 JA |
271 | if (td->rate_bytes[ddir] || td->rate_blocks[ddir]) { |
272 | spent = mtime_since(&td->lastrate[ddir], now); | |
2dc1bbeb | 273 | if (spent < td->o.ratecycle) |
ebac4655 JA |
274 | return 0; |
275 | ||
581e7141 | 276 | if (td->o.rate[ddir]) { |
4e991c23 JA |
277 | /* |
278 | * check bandwidth specified rate | |
279 | */ | |
581e7141 | 280 | if (bytes < td->rate_bytes[ddir]) { |
5ec10eaa | 281 | log_err("%s: min rate %u not met\n", td->o.name, |
581e7141 | 282 | ratemin); |
4e991c23 JA |
283 | return 1; |
284 | } else { | |
581e7141 JA |
285 | rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent; |
286 | if (rate < ratemin || | |
287 | bytes < td->rate_bytes[ddir]) { | |
5ec10eaa | 288 | log_err("%s: min rate %u not met, got" |
b22989b9 | 289 | " %luKB/sec\n", td->o.name, |
581e7141 | 290 | ratemin, rate); |
4e991c23 JA |
291 | return 1; |
292 | } | |
293 | } | |
413dd459 | 294 | } else { |
4e991c23 JA |
295 | /* |
296 | * checks iops specified rate | |
297 | */ | |
581e7141 | 298 | if (iops < rate_iops) { |
5ec10eaa | 299 | log_err("%s: min iops rate %u not met\n", |
581e7141 | 300 | td->o.name, rate_iops); |
413dd459 | 301 | return 1; |
4e991c23 | 302 | } else { |
581e7141 JA |
303 | rate = ((iops - td->rate_blocks[ddir]) * 1000) / spent; |
304 | if (rate < rate_iops_min || | |
305 | iops < td->rate_blocks[ddir]) { | |
5ec10eaa JA |
306 | log_err("%s: min iops rate %u not met," |
307 | " got %lu\n", td->o.name, | |
581e7141 | 308 | rate_iops_min, rate); |
4e991c23 | 309 | } |
413dd459 | 310 | } |
ebac4655 JA |
311 | } |
312 | } | |
313 | ||
581e7141 JA |
314 | td->rate_bytes[ddir] = bytes; |
315 | td->rate_blocks[ddir] = iops; | |
316 | memcpy(&td->lastrate[ddir], now, sizeof(*now)); | |
ebac4655 JA |
317 | return 0; |
318 | } | |
319 | ||
581e7141 JA |
320 | static int check_min_rate(struct thread_data *td, struct timeval *now, |
321 | unsigned long *bytes_done) | |
322 | { | |
323 | int ret = 0; | |
324 | ||
325 | if (bytes_done[0]) | |
326 | ret |= __check_min_rate(td, now, 0); | |
327 | if (bytes_done[1]) | |
328 | ret |= __check_min_rate(td, now, 1); | |
329 | ||
330 | return ret; | |
331 | } | |
332 | ||
ebac4655 JA |
333 | static inline int runtime_exceeded(struct thread_data *td, struct timeval *t) |
334 | { | |
2dc1bbeb | 335 | if (!td->o.timeout) |
ebac4655 | 336 | return 0; |
2dc1bbeb | 337 | if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000) |
ebac4655 JA |
338 | return 1; |
339 | ||
340 | return 0; | |
341 | } | |
342 | ||
906c8d75 JA |
343 | /* |
344 | * When job exits, we can cancel the in-flight IO if we are using async | |
345 | * io. Attempt to do so. | |
346 | */ | |
ebac4655 JA |
347 | static void cleanup_pending_aio(struct thread_data *td) |
348 | { | |
01743ee1 | 349 | struct flist_head *entry, *n; |
ebac4655 JA |
350 | struct io_u *io_u; |
351 | int r; | |
352 | ||
353 | /* | |
354 | * get immediately available events, if any | |
355 | */ | |
581e7141 | 356 | r = io_u_queued_complete(td, 0, NULL); |
b2fdda43 JA |
357 | if (r < 0) |
358 | return; | |
ebac4655 JA |
359 | |
360 | /* | |
361 | * now cancel remaining active events | |
362 | */ | |
2866c82d | 363 | if (td->io_ops->cancel) { |
01743ee1 JA |
364 | flist_for_each_safe(entry, n, &td->io_u_busylist) { |
365 | io_u = flist_entry(entry, struct io_u, list); | |
ebac4655 | 366 | |
0c6e7517 JA |
367 | /* |
368 | * if the io_u isn't in flight, then that generally | |
369 | * means someone leaked an io_u. complain but fix | |
370 | * it up, so we don't stall here. | |
371 | */ | |
372 | if ((io_u->flags & IO_U_F_FLIGHT) == 0) { | |
373 | log_err("fio: non-busy IO on busy list\n"); | |
ebac4655 | 374 | put_io_u(td, io_u); |
0c6e7517 JA |
375 | } else { |
376 | r = td->io_ops->cancel(td, io_u); | |
377 | if (!r) | |
378 | put_io_u(td, io_u); | |
379 | } | |
ebac4655 JA |
380 | } |
381 | } | |
382 | ||
97601024 | 383 | if (td->cur_depth) |
581e7141 | 384 | r = io_u_queued_complete(td, td->cur_depth, NULL); |
ebac4655 JA |
385 | } |
386 | ||
858a3d47 JA |
387 | /* |
388 | * Helper to handle the final sync of a file. Works just like the normal | |
389 | * io path, just does everything sync. | |
390 | */ | |
391 | static int fio_io_sync(struct thread_data *td, struct fio_file *f) | |
392 | { | |
393 | struct io_u *io_u = __get_io_u(td); | |
858a3d47 JA |
394 | int ret; |
395 | ||
396 | if (!io_u) | |
397 | return 1; | |
398 | ||
399 | io_u->ddir = DDIR_SYNC; | |
400 | io_u->file = f; | |
401 | ||
402 | if (td_io_prep(td, io_u)) { | |
403 | put_io_u(td, io_u); | |
404 | return 1; | |
405 | } | |
406 | ||
755200a3 | 407 | requeue: |
858a3d47 | 408 | ret = td_io_queue(td, io_u); |
36167d82 | 409 | if (ret < 0) { |
e1161c32 | 410 | td_verror(td, io_u->error, "td_io_queue"); |
858a3d47 | 411 | put_io_u(td, io_u); |
858a3d47 | 412 | return 1; |
36167d82 | 413 | } else if (ret == FIO_Q_QUEUED) { |
581e7141 | 414 | if (io_u_queued_complete(td, 1, NULL) < 0) |
36167d82 | 415 | return 1; |
36167d82 JA |
416 | } else if (ret == FIO_Q_COMPLETED) { |
417 | if (io_u->error) { | |
e1161c32 | 418 | td_verror(td, io_u->error, "td_io_queue"); |
36167d82 JA |
419 | return 1; |
420 | } | |
858a3d47 | 421 | |
581e7141 | 422 | if (io_u_sync_complete(td, io_u, NULL) < 0) |
b2fdda43 | 423 | return 1; |
755200a3 JA |
424 | } else if (ret == FIO_Q_BUSY) { |
425 | if (td_io_commit(td)) | |
426 | return 1; | |
427 | goto requeue; | |
858a3d47 JA |
428 | } |
429 | ||
430 | return 0; | |
431 | } | |
432 | ||
687c6a23 JA |
433 | static inline void __update_tv_cache(struct thread_data *td) |
434 | { | |
435 | fio_gettime(&td->tv_cache, NULL); | |
436 | } | |
437 | ||
993bf48b JA |
438 | static inline void update_tv_cache(struct thread_data *td) |
439 | { | |
440 | if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask) | |
687c6a23 | 441 | __update_tv_cache(td); |
993bf48b JA |
442 | } |
443 | ||
f2bba182 RR |
444 | static int break_on_this_error(struct thread_data *td, int *retptr) |
445 | { | |
446 | int ret = *retptr; | |
447 | ||
448 | if (ret < 0 || td->error) { | |
449 | int err; | |
450 | ||
1ec99eea JA |
451 | if (!td->o.continue_on_error) |
452 | return 1; | |
f2bba182 RR |
453 | |
454 | if (ret < 0) | |
455 | err = -ret; | |
456 | else | |
457 | err = td->error; | |
458 | ||
f2bba182 RR |
459 | if (td_non_fatal_error(err)) { |
460 | /* | |
461 | * Continue with the I/Os in case of | |
462 | * a non fatal error. | |
463 | */ | |
8a4f8a24 | 464 | update_error_count(td, err); |
f2bba182 RR |
465 | td_clear_error(td); |
466 | *retptr = 0; | |
467 | return 0; | |
8a4f8a24 RR |
468 | } else if (td->o.fill_device && err == ENOSPC) { |
469 | /* | |
470 | * We expect to hit this error if | |
471 | * fill_device option is set. | |
472 | */ | |
473 | td_clear_error(td); | |
474 | td->terminate = 1; | |
475 | return 1; | |
f2bba182 RR |
476 | } else { |
477 | /* | |
478 | * Stop the I/O in case of a fatal | |
479 | * error. | |
480 | */ | |
8a4f8a24 | 481 | update_error_count(td, err); |
f2bba182 RR |
482 | return 1; |
483 | } | |
484 | } | |
485 | ||
486 | return 0; | |
487 | } | |
488 | ||
906c8d75 | 489 | /* |
34403fb1 | 490 | * The main verify engine. Runs over the writes we previously submitted, |
906c8d75 JA |
491 | * reads the blocks back in, and checks the crc/md5 of the data. |
492 | */ | |
1e97cce9 | 493 | static void do_verify(struct thread_data *td) |
ebac4655 | 494 | { |
53cdc686 | 495 | struct fio_file *f; |
36167d82 | 496 | struct io_u *io_u; |
af52b345 JA |
497 | int ret, min_events; |
498 | unsigned int i; | |
e5b401d4 | 499 | |
008618a8 JA |
500 | dprint(FD_VERIFY, "starting loop\n"); |
501 | ||
e5b401d4 JA |
502 | /* |
503 | * sync io first and invalidate cache, to make sure we really | |
504 | * read from disk. | |
505 | */ | |
506 | for_each_file(td, f, i) { | |
d6aed795 | 507 | if (!fio_file_open(f)) |
3d7b485f | 508 | continue; |
b2fdda43 JA |
509 | if (fio_io_sync(td, f)) |
510 | break; | |
511 | if (file_invalidate_cache(td, f)) | |
512 | break; | |
e5b401d4 | 513 | } |
ebac4655 | 514 | |
b2fdda43 JA |
515 | if (td->error) |
516 | return; | |
517 | ||
ebac4655 JA |
518 | td_set_runstate(td, TD_VERIFYING); |
519 | ||
36167d82 JA |
520 | io_u = NULL; |
521 | while (!td->terminate) { | |
4950421a | 522 | int ret2, full; |
5451792e | 523 | |
993bf48b JA |
524 | update_tv_cache(td); |
525 | ||
526 | if (runtime_exceeded(td, &td->tv_cache)) { | |
687c6a23 JA |
527 | __update_tv_cache(td); |
528 | if (runtime_exceeded(td, &td->tv_cache)) { | |
529 | td->terminate = 1; | |
530 | break; | |
531 | } | |
069c2918 | 532 | } |
02bcaa8c | 533 | |
dcb4830d RR |
534 | io_u = __get_io_u(td); |
535 | if (!io_u) | |
536 | break; | |
537 | ||
069c2918 JA |
538 | if (get_next_verify(td, io_u)) { |
539 | put_io_u(td, io_u); | |
ebac4655 | 540 | break; |
069c2918 | 541 | } |
ebac4655 | 542 | |
069c2918 JA |
543 | if (td_io_prep(td, io_u)) { |
544 | put_io_u(td, io_u); | |
53cdc686 | 545 | break; |
069c2918 | 546 | } |
d7762cf8 | 547 | |
e8462bd8 JA |
548 | if (td->o.verify_async) |
549 | io_u->end_io = verify_io_u_async; | |
550 | else | |
551 | io_u->end_io = verify_io_u; | |
53cdc686 | 552 | |
11786802 | 553 | ret = td_io_queue(td, io_u); |
36167d82 JA |
554 | switch (ret) { |
555 | case FIO_Q_COMPLETED: | |
f2bba182 | 556 | if (io_u->error) { |
22819ec2 | 557 | ret = -io_u->error; |
f2bba182 RR |
558 | clear_io_u(td, io_u); |
559 | } else if (io_u->resid) { | |
36167d82 | 560 | int bytes = io_u->xfer_buflen - io_u->resid; |
ebac4655 | 561 | |
9e9d164e JA |
562 | /* |
563 | * zero read, fail | |
564 | */ | |
565 | if (!bytes) { | |
41d9f0fd | 566 | td_verror(td, EIO, "full resid"); |
9e9d164e JA |
567 | put_io_u(td, io_u); |
568 | break; | |
569 | } | |
8400d9b2 | 570 | |
36167d82 JA |
571 | io_u->xfer_buflen = io_u->resid; |
572 | io_u->xfer_buf += bytes; | |
8400d9b2 JA |
573 | io_u->offset += bytes; |
574 | ||
ff58fced JA |
575 | if (ddir_rw(io_u->ddir)) |
576 | td->ts.short_io_u[io_u->ddir]++; | |
30061b97 | 577 | |
2b13e716 | 578 | f = io_u->file; |
d460eb31 | 579 | if (io_u->offset == f->real_file_size) |
8400d9b2 JA |
580 | goto sync_done; |
581 | ||
11786802 JA |
582 | requeue_io_u(td, &io_u); |
583 | } else { | |
8400d9b2 | 584 | sync_done: |
581e7141 | 585 | ret = io_u_sync_complete(td, io_u, NULL); |
11786802 JA |
586 | if (ret < 0) |
587 | break; | |
36167d82 | 588 | } |
36167d82 JA |
589 | continue; |
590 | case FIO_Q_QUEUED: | |
591 | break; | |
755200a3 JA |
592 | case FIO_Q_BUSY: |
593 | requeue_io_u(td, &io_u); | |
5451792e JA |
594 | ret2 = td_io_commit(td); |
595 | if (ret2 < 0) | |
596 | ret = ret2; | |
755200a3 | 597 | break; |
36167d82 JA |
598 | default: |
599 | assert(ret < 0); | |
e1161c32 | 600 | td_verror(td, -ret, "td_io_queue"); |
ebac4655 JA |
601 | break; |
602 | } | |
603 | ||
f2bba182 | 604 | if (break_on_this_error(td, &ret)) |
3af6ef39 JA |
605 | break; |
606 | ||
ebac4655 | 607 | /* |
3af6ef39 | 608 | * if we can queue more, do so. but check if there are |
ccc02050 JA |
609 | * completed io_u's first. Note that we can get BUSY even |
610 | * without IO queued, if the system is resource starved. | |
ebac4655 | 611 | */ |
ccc02050 | 612 | full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth); |
4950421a | 613 | if (full || !td->o.iodepth_batch_complete) { |
6a96276c RR |
614 | min_events = min(td->o.iodepth_batch_complete, |
615 | td->cur_depth); | |
1f84f1d9 | 616 | if (full && !min_events && td->o.iodepth_batch_complete != 0) |
838bc709 | 617 | min_events = 1; |
e916b390 | 618 | |
4950421a JA |
619 | do { |
620 | /* | |
621 | * Reap required number of io units, if any, | |
622 | * and do the verification on them through | |
623 | * the callback handler | |
624 | */ | |
581e7141 | 625 | if (io_u_queued_complete(td, min_events, NULL) < 0) { |
4950421a JA |
626 | ret = -1; |
627 | break; | |
628 | } | |
629 | } while (full && (td->cur_depth > td->o.iodepth_low)); | |
630 | } | |
631 | if (ret < 0) | |
ebac4655 | 632 | break; |
36167d82 | 633 | } |
ebac4655 | 634 | |
c01c0395 JA |
635 | if (!td->error) { |
636 | min_events = td->cur_depth; | |
637 | ||
638 | if (min_events) | |
581e7141 | 639 | ret = io_u_queued_complete(td, min_events, NULL); |
c01c0395 | 640 | } else |
ebac4655 JA |
641 | cleanup_pending_aio(td); |
642 | ||
643 | td_set_runstate(td, TD_RUNNING); | |
008618a8 JA |
644 | |
645 | dprint(FD_VERIFY, "exiting loop\n"); | |
ebac4655 JA |
646 | } |
647 | ||
32cd46a0 | 648 | /* |
906c8d75 | 649 | * Main IO worker function. It retrieves io_u's to process and queues |
32cd46a0 JA |
650 | * and reaps them, checking for rate and errors along the way. |
651 | */ | |
ebac4655 JA |
652 | static void do_io(struct thread_data *td) |
653 | { | |
af52b345 JA |
654 | unsigned int i; |
655 | int ret = 0; | |
ebac4655 | 656 | |
b29ee5b3 JA |
657 | if (in_ramp_time(td)) |
658 | td_set_runstate(td, TD_RAMP); | |
659 | else | |
660 | td_set_runstate(td, TD_RUNNING); | |
5853e5a8 | 661 | |
457bf399 | 662 | while ( (td->o.read_iolog_file && !flist_empty(&td->io_log_list)) || |
a917a8b3 | 663 | (!flist_empty(&td->trim_list)) || |
457bf399 | 664 | ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) ) { |
97601024 | 665 | struct timeval comp_time; |
581e7141 | 666 | unsigned long bytes_done[2] = { 0, 0 }; |
84585003 | 667 | int min_evts = 0; |
ebac4655 | 668 | struct io_u *io_u; |
4950421a | 669 | int ret2, full; |
ebac4655 JA |
670 | |
671 | if (td->terminate) | |
672 | break; | |
673 | ||
993bf48b | 674 | update_tv_cache(td); |
97601024 | 675 | |
993bf48b | 676 | if (runtime_exceeded(td, &td->tv_cache)) { |
687c6a23 JA |
677 | __update_tv_cache(td); |
678 | if (runtime_exceeded(td, &td->tv_cache)) { | |
679 | td->terminate = 1; | |
680 | break; | |
681 | } | |
97601024 | 682 | } |
36167d82 | 683 | |
dcb4830d RR |
684 | io_u = get_io_u(td); |
685 | if (!io_u) | |
686 | break; | |
687 | ||
b67740d3 JA |
688 | /* |
689 | * Add verification end_io handler, if asked to verify | |
690 | * a previously written file. | |
691 | */ | |
715d2b3e JA |
692 | if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ && |
693 | !td_rw(td)) { | |
e8462bd8 JA |
694 | if (td->o.verify_async) |
695 | io_u->end_io = verify_io_u_async; | |
696 | else | |
697 | io_u->end_io = verify_io_u; | |
d8fab1b6 | 698 | td_set_runstate(td, TD_VERIFYING); |
b29ee5b3 JA |
699 | } else if (in_ramp_time(td)) |
700 | td_set_runstate(td, TD_RAMP); | |
701 | else | |
d8fab1b6 | 702 | td_set_runstate(td, TD_RUNNING); |
b67740d3 | 703 | |
11786802 | 704 | ret = td_io_queue(td, io_u); |
36167d82 JA |
705 | switch (ret) { |
706 | case FIO_Q_COMPLETED: | |
f2bba182 | 707 | if (io_u->error) { |
5451792e | 708 | ret = -io_u->error; |
f2bba182 RR |
709 | clear_io_u(td, io_u); |
710 | } else if (io_u->resid) { | |
36167d82 | 711 | int bytes = io_u->xfer_buflen - io_u->resid; |
d460eb31 | 712 | struct fio_file *f = io_u->file; |
36167d82 | 713 | |
9e9d164e JA |
714 | /* |
715 | * zero read, fail | |
716 | */ | |
717 | if (!bytes) { | |
41d9f0fd | 718 | td_verror(td, EIO, "full resid"); |
9e9d164e JA |
719 | put_io_u(td, io_u); |
720 | break; | |
721 | } | |
722 | ||
cec6b55d | 723 | io_u->xfer_buflen = io_u->resid; |
36167d82 | 724 | io_u->xfer_buf += bytes; |
5a7c5680 JA |
725 | io_u->offset += bytes; |
726 | ||
ff58fced JA |
727 | if (ddir_rw(io_u->ddir)) |
728 | td->ts.short_io_u[io_u->ddir]++; | |
30061b97 | 729 | |
d460eb31 | 730 | if (io_u->offset == f->real_file_size) |
5a7c5680 JA |
731 | goto sync_done; |
732 | ||
11786802 JA |
733 | requeue_io_u(td, &io_u); |
734 | } else { | |
5a7c5680 | 735 | sync_done: |
581e7141 JA |
736 | if (__should_check_rate(td, 0) || |
737 | __should_check_rate(td, 1)) | |
993bf48b JA |
738 | fio_gettime(&comp_time, NULL); |
739 | ||
581e7141 JA |
740 | ret = io_u_sync_complete(td, io_u, bytes_done); |
741 | if (ret < 0) | |
742 | break; | |
cec6b55d | 743 | } |
36167d82 JA |
744 | break; |
745 | case FIO_Q_QUEUED: | |
7e77dd02 JA |
746 | /* |
747 | * if the engine doesn't have a commit hook, | |
748 | * the io_u is really queued. if it does have such | |
749 | * a hook, it has to call io_u_queued() itself. | |
750 | */ | |
751 | if (td->io_ops->commit == NULL) | |
752 | io_u_queued(td, io_u); | |
36167d82 | 753 | break; |
755200a3 JA |
754 | case FIO_Q_BUSY: |
755 | requeue_io_u(td, &io_u); | |
5451792e JA |
756 | ret2 = td_io_commit(td); |
757 | if (ret2 < 0) | |
758 | ret = ret2; | |
755200a3 | 759 | break; |
36167d82 JA |
760 | default: |
761 | assert(ret < 0); | |
762 | put_io_u(td, io_u); | |
763 | break; | |
ebac4655 JA |
764 | } |
765 | ||
f2bba182 | 766 | if (break_on_this_error(td, &ret)) |
36167d82 JA |
767 | break; |
768 | ||
97601024 | 769 | /* |
ccc02050 JA |
770 | * See if we need to complete some commands. Note that we |
771 | * can get BUSY even without IO queued, if the system is | |
772 | * resource starved. | |
97601024 | 773 | */ |
ccc02050 | 774 | full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth); |
4950421a | 775 | if (full || !td->o.iodepth_batch_complete) { |
6a96276c RR |
776 | min_evts = min(td->o.iodepth_batch_complete, |
777 | td->cur_depth); | |
1f84f1d9 | 778 | if (full && !min_evts && td->o.iodepth_batch_complete != 0) |
36167d82 | 779 | min_evts = 1; |
4950421a | 780 | |
581e7141 JA |
781 | if (__should_check_rate(td, 0) || |
782 | __should_check_rate(td, 1)) | |
993bf48b | 783 | fio_gettime(&comp_time, NULL); |
4950421a JA |
784 | |
785 | do { | |
581e7141 JA |
786 | ret = io_u_queued_complete(td, min_evts, bytes_done); |
787 | if (ret < 0) | |
4950421a JA |
788 | break; |
789 | ||
4950421a | 790 | } while (full && (td->cur_depth > td->o.iodepth_low)); |
ebac4655 JA |
791 | } |
792 | ||
4950421a JA |
793 | if (ret < 0) |
794 | break; | |
581e7141 | 795 | if (!(bytes_done[0] + bytes_done[1])) |
97601024 JA |
796 | continue; |
797 | ||
581e7141 JA |
798 | if (!in_ramp_time(td) && should_check_rate(td, bytes_done)) { |
799 | if (check_min_rate(td, &comp_time, bytes_done)) { | |
721938ae | 800 | if (exitall_on_terminate) |
cc0df00a | 801 | fio_terminate_threads(td->groupid); |
721938ae JA |
802 | td_verror(td, EIO, "check_min_rate"); |
803 | break; | |
804 | } | |
ebac4655 JA |
805 | } |
806 | ||
2dc1bbeb | 807 | if (td->o.thinktime) { |
9c1f7434 JA |
808 | unsigned long long b; |
809 | ||
810 | b = td->io_blocks[0] + td->io_blocks[1]; | |
2dc1bbeb | 811 | if (!(b % td->o.thinktime_blocks)) { |
48097d5c JA |
812 | int left; |
813 | ||
2dc1bbeb | 814 | if (td->o.thinktime_spin) |
6dd6f2cd | 815 | usec_spin(td->o.thinktime_spin); |
48097d5c | 816 | |
2dc1bbeb | 817 | left = td->o.thinktime - td->o.thinktime_spin; |
48097d5c JA |
818 | if (left) |
819 | usec_sleep(td, left); | |
820 | } | |
9c1f7434 | 821 | } |
ebac4655 JA |
822 | } |
823 | ||
d708ac5e JA |
824 | if (td->trim_entries) |
825 | printf("trim entries %ld\n", td->trim_entries); | |
a917a8b3 | 826 | |
aa31f1f1 SL |
827 | if (td->o.fill_device && td->error == ENOSPC) { |
828 | td->error = 0; | |
829 | td->terminate = 1; | |
830 | } | |
4d2413c6 | 831 | if (!td->error) { |
3d7c391d JA |
832 | struct fio_file *f; |
833 | ||
c01c0395 | 834 | i = td->cur_depth; |
d06093e7 | 835 | if (i) { |
581e7141 | 836 | ret = io_u_queued_complete(td, i, NULL); |
d06093e7 SL |
837 | if (td->o.fill_device && td->error == ENOSPC) |
838 | td->error = 0; | |
839 | } | |
ebac4655 | 840 | |
2dc1bbeb | 841 | if (should_fsync(td) && td->o.end_fsync) { |
84585003 | 842 | td_set_runstate(td, TD_FSYNCING); |
3d7b485f JA |
843 | |
844 | for_each_file(td, f, i) { | |
d6aed795 | 845 | if (!fio_file_open(f)) |
3d7b485f | 846 | continue; |
858a3d47 | 847 | fio_io_sync(td, f); |
3d7b485f | 848 | } |
84585003 | 849 | } |
c01c0395 JA |
850 | } else |
851 | cleanup_pending_aio(td); | |
2ba1c290 JA |
852 | |
853 | /* | |
854 | * stop job if we failed doing any IO | |
855 | */ | |
856 | if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0) | |
857 | td->done = 1; | |
ebac4655 JA |
858 | } |
859 | ||
ebac4655 JA |
860 | static void cleanup_io_u(struct thread_data *td) |
861 | { | |
01743ee1 | 862 | struct flist_head *entry, *n; |
ebac4655 JA |
863 | struct io_u *io_u; |
864 | ||
01743ee1 JA |
865 | flist_for_each_safe(entry, n, &td->io_u_freelist) { |
866 | io_u = flist_entry(entry, struct io_u, list); | |
ebac4655 | 867 | |
01743ee1 | 868 | flist_del(&io_u->list); |
91aea6ec | 869 | fio_memfree(io_u, sizeof(*io_u)); |
ebac4655 JA |
870 | } |
871 | ||
2f9ade3c | 872 | free_io_mem(td); |
ebac4655 JA |
873 | } |
874 | ||
875 | static int init_io_u(struct thread_data *td) | |
876 | { | |
877 | struct io_u *io_u; | |
a00735e6 | 878 | unsigned int max_bs; |
eb7ccf38 | 879 | int cl_align, i, max_units; |
ebac4655 JA |
880 | char *p; |
881 | ||
1e3c09af | 882 | max_units = td->o.iodepth; |
2dc1bbeb | 883 | max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]); |
5ec10eaa JA |
884 | td->orig_buffer_size = (unsigned long long) max_bs |
885 | * (unsigned long long) max_units; | |
886 | ||
887 | if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) { | |
888 | unsigned long bs; | |
74b025b0 | 889 | |
5ec10eaa JA |
890 | bs = td->orig_buffer_size + td->o.hugepage_size - 1; |
891 | td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1); | |
892 | } | |
ebac4655 | 893 | |
c3990645 JA |
894 | if (td->orig_buffer_size != (size_t) td->orig_buffer_size) { |
895 | log_err("fio: IO memory too large. Reduce max_bs or iodepth\n"); | |
896 | return 1; | |
897 | } | |
898 | ||
2f9ade3c JA |
899 | if (allocate_io_mem(td)) |
900 | return 1; | |
ebac4655 | 901 | |
1c04bf78 JA |
902 | if (td->o.odirect || td->o.mem_align || |
903 | (td->io_ops->flags & FIO_RAWIO)) | |
d529ee19 | 904 | p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align; |
b3f378e9 JA |
905 | else |
906 | p = td->orig_buffer; | |
907 | ||
eb7ccf38 JA |
908 | cl_align = os_cache_line_size(); |
909 | ||
ebac4655 | 910 | for (i = 0; i < max_units; i++) { |
eb7ccf38 JA |
911 | void *ptr; |
912 | ||
db1cd90b JA |
913 | if (td->terminate) |
914 | return 1; | |
eb7ccf38 | 915 | |
91aea6ec JA |
916 | ptr = fio_memalign(cl_align, sizeof(*io_u)); |
917 | if (!ptr) { | |
918 | log_err("fio: unable to allocate aligned memory\n"); | |
eb7ccf38 JA |
919 | break; |
920 | } | |
921 | ||
922 | io_u = ptr; | |
ebac4655 | 923 | memset(io_u, 0, sizeof(*io_u)); |
01743ee1 | 924 | INIT_FLIST_HEAD(&io_u->list); |
d529ee19 | 925 | dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i); |
ebac4655 | 926 | |
b4c5e1ac JA |
927 | if (!(td->io_ops->flags & FIO_NOIO)) { |
928 | io_u->buf = p + max_bs * i; | |
d529ee19 | 929 | dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf); |
e9459e5a | 930 | |
3545a109 | 931 | if (td_write(td)) |
5973cafb | 932 | io_u_fill_buffer(td, io_u, max_bs); |
3545a109 | 933 | if (td_write(td) && td->o.verify_pattern_bytes) { |
cbe8d756 RR |
934 | /* |
935 | * Fill the buffer with the pattern if we are | |
936 | * going to be doing writes. | |
937 | */ | |
7d9fb455 | 938 | fill_pattern(td, io_u->buf, max_bs, io_u, 0, 0); |
cbe8d756 | 939 | } |
b4c5e1ac | 940 | } |
6b9cea23 | 941 | |
b1ff3403 | 942 | io_u->index = i; |
0c6e7517 | 943 | io_u->flags = IO_U_F_FREE; |
01743ee1 | 944 | flist_add(&io_u->list, &td->io_u_freelist); |
ebac4655 JA |
945 | } |
946 | ||
947 | return 0; | |
948 | } | |
949 | ||
da86774e JA |
950 | static int switch_ioscheduler(struct thread_data *td) |
951 | { | |
952 | char tmp[256], tmp2[128]; | |
953 | FILE *f; | |
954 | int ret; | |
955 | ||
ba0fbe10 | 956 | if (td->io_ops->flags & FIO_DISKLESSIO) |
f48b467c JA |
957 | return 0; |
958 | ||
da86774e JA |
959 | sprintf(tmp, "%s/queue/scheduler", td->sysfs_root); |
960 | ||
961 | f = fopen(tmp, "r+"); | |
962 | if (!f) { | |
cbf5c1d7 | 963 | if (errno == ENOENT) { |
5ec10eaa JA |
964 | log_err("fio: os or kernel doesn't support IO scheduler" |
965 | " switching\n"); | |
cbf5c1d7 JA |
966 | return 0; |
967 | } | |
968 | td_verror(td, errno, "fopen iosched"); | |
da86774e JA |
969 | return 1; |
970 | } | |
971 | ||
972 | /* | |
973 | * Set io scheduler. | |
974 | */ | |
2dc1bbeb | 975 | ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f); |
da86774e | 976 | if (ferror(f) || ret != 1) { |
e1161c32 | 977 | td_verror(td, errno, "fwrite"); |
da86774e JA |
978 | fclose(f); |
979 | return 1; | |
980 | } | |
981 | ||
982 | rewind(f); | |
983 | ||
984 | /* | |
985 | * Read back and check that the selected scheduler is now the default. | |
986 | */ | |
987 | ret = fread(tmp, 1, sizeof(tmp), f); | |
988 | if (ferror(f) || ret < 0) { | |
e1161c32 | 989 | td_verror(td, errno, "fread"); |
da86774e JA |
990 | fclose(f); |
991 | return 1; | |
992 | } | |
993 | ||
2dc1bbeb | 994 | sprintf(tmp2, "[%s]", td->o.ioscheduler); |
da86774e | 995 | if (!strstr(tmp, tmp2)) { |
2dc1bbeb | 996 | log_err("fio: io scheduler %s not found\n", td->o.ioscheduler); |
e1161c32 | 997 | td_verror(td, EINVAL, "iosched_switch"); |
da86774e JA |
998 | fclose(f); |
999 | return 1; | |
1000 | } | |
1001 | ||
1002 | fclose(f); | |
1003 | return 0; | |
1004 | } | |
1005 | ||
492158cf JA |
1006 | static int keep_running(struct thread_data *td) |
1007 | { | |
1008 | unsigned long long io_done; | |
1009 | ||
20e354ef JA |
1010 | if (td->done) |
1011 | return 0; | |
492158cf JA |
1012 | if (td->o.time_based) |
1013 | return 1; | |
1014 | if (td->o.loops) { | |
1015 | td->o.loops--; | |
1016 | return 1; | |
1017 | } | |
1018 | ||
5ec10eaa JA |
1019 | io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE] |
1020 | + td->io_skip_bytes; | |
492158cf JA |
1021 | if (io_done < td->o.size) |
1022 | return 1; | |
1023 | ||
1024 | return 0; | |
1025 | } | |
1026 | ||
b29ee5b3 | 1027 | static void reset_io_counters(struct thread_data *td) |
ebac4655 | 1028 | { |
f0505a14 | 1029 | td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0; |
ebac4655 | 1030 | td->this_io_bytes[0] = td->this_io_bytes[1] = 0; |
c8eeb9df JA |
1031 | td->stat_io_blocks[0] = td->stat_io_blocks[1] = 0; |
1032 | td->this_io_blocks[0] = td->this_io_blocks[1] = 0; | |
20dc95c4 | 1033 | td->zone_bytes = 0; |
581e7141 JA |
1034 | td->rate_bytes[0] = td->rate_bytes[1] = 0; |
1035 | td->rate_blocks[0] = td->rate_blocks[1] = 0; | |
ebac4655 | 1036 | |
c1324df1 JA |
1037 | td->last_was_sync = 0; |
1038 | ||
2ba1c290 JA |
1039 | /* |
1040 | * reset file done count if we are to start over | |
1041 | */ | |
1042 | if (td->o.time_based || td->o.loops) | |
48f5abd3 | 1043 | td->nr_done_files = 0; |
b29ee5b3 JA |
1044 | } |
1045 | ||
1046 | void reset_all_stats(struct thread_data *td) | |
1047 | { | |
1048 | struct timeval tv; | |
1049 | int i; | |
1050 | ||
1051 | reset_io_counters(td); | |
1052 | ||
1053 | for (i = 0; i < 2; i++) { | |
1054 | td->io_bytes[i] = 0; | |
1055 | td->io_blocks[i] = 0; | |
1056 | td->io_issues[i] = 0; | |
1057 | td->ts.total_io_u[i] = 0; | |
1058 | } | |
9b836561 | 1059 | |
b29ee5b3 | 1060 | fio_gettime(&tv, NULL); |
cda99fa0 SSP |
1061 | td->ts.runtime[0] = 0; |
1062 | td->ts.runtime[1] = 0; | |
b29ee5b3 JA |
1063 | memcpy(&td->epoch, &tv, sizeof(tv)); |
1064 | memcpy(&td->start, &tv, sizeof(tv)); | |
1065 | } | |
1066 | ||
df9c26b1 | 1067 | static void clear_io_state(struct thread_data *td) |
b29ee5b3 JA |
1068 | { |
1069 | struct fio_file *f; | |
1070 | unsigned int i; | |
b29ee5b3 JA |
1071 | |
1072 | reset_io_counters(td); | |
281f9b63 | 1073 | |
24ffd2c2 | 1074 | close_files(td); |
df9c26b1 | 1075 | for_each_file(td, f, i) |
d6aed795 | 1076 | fio_file_clear_done(f); |
f0153948 JA |
1077 | |
1078 | /* | |
1079 | * Set the same seed to get repeatable runs | |
1080 | */ | |
1081 | td_fill_rand_seeds(td); | |
ebac4655 JA |
1082 | } |
1083 | ||
398e8c4d JA |
1084 | static int exec_string(const char *string) |
1085 | { | |
1086 | int ret, newlen = strlen(string) + 1 + 8; | |
1087 | char *str; | |
1088 | ||
1089 | str = malloc(newlen); | |
1090 | sprintf(str, "sh -c %s", string); | |
1091 | ||
1092 | ret = system(str); | |
1093 | if (ret == -1) | |
1094 | log_err("fio: exec of cmd <%s> failed\n", str); | |
1095 | ||
1096 | free(str); | |
1097 | return ret; | |
1098 | } | |
1099 | ||
906c8d75 JA |
1100 | /* |
1101 | * Entry point for the thread based jobs. The process based jobs end up | |
1102 | * here as well, after a little setup. | |
1103 | */ | |
ebac4655 JA |
1104 | static void *thread_main(void *data) |
1105 | { | |
cda99fa0 | 1106 | unsigned long long elapsed; |
ebac4655 | 1107 | struct thread_data *td = data; |
e8462bd8 | 1108 | pthread_condattr_t attr; |
a978ba68 | 1109 | int clear_state; |
ebac4655 | 1110 | |
47f767c1 | 1111 | if (!td->o.use_thread) { |
ebac4655 | 1112 | setsid(); |
47f767c1 JA |
1113 | td->pid = getpid(); |
1114 | } else | |
1115 | td->pid = gettid(); | |
ebac4655 | 1116 | |
5921e80c | 1117 | dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid); |
ee56ad50 | 1118 | |
01743ee1 JA |
1119 | INIT_FLIST_HEAD(&td->io_u_freelist); |
1120 | INIT_FLIST_HEAD(&td->io_u_busylist); | |
1121 | INIT_FLIST_HEAD(&td->io_u_requeues); | |
1122 | INIT_FLIST_HEAD(&td->io_log_list); | |
1123 | INIT_FLIST_HEAD(&td->io_hist_list); | |
e8462bd8 | 1124 | INIT_FLIST_HEAD(&td->verify_list); |
0d29de83 | 1125 | INIT_FLIST_HEAD(&td->trim_list); |
e8462bd8 | 1126 | pthread_mutex_init(&td->io_u_lock, NULL); |
bb5d7d0b | 1127 | td->io_hist_tree = RB_ROOT; |
aea47d44 | 1128 | |
e8462bd8 JA |
1129 | pthread_condattr_init(&attr); |
1130 | pthread_cond_init(&td->verify_cond, &attr); | |
1131 | pthread_cond_init(&td->free_cond, &attr); | |
1132 | ||
db1cd90b | 1133 | td_set_runstate(td, TD_INITIALIZED); |
29adda3c | 1134 | dprint(FD_MUTEX, "up startup_mutex\n"); |
cdd18ad8 | 1135 | fio_mutex_up(startup_mutex); |
29adda3c | 1136 | dprint(FD_MUTEX, "wait on td->mutex\n"); |
cdd18ad8 | 1137 | fio_mutex_down(td->mutex); |
29adda3c | 1138 | dprint(FD_MUTEX, "done waiting on td->mutex\n"); |
db1cd90b JA |
1139 | |
1140 | /* | |
cdd18ad8 | 1141 | * the ->mutex mutex is now no longer used, close it to avoid |
db1cd90b JA |
1142 | * eating a file descriptor |
1143 | */ | |
cdd18ad8 | 1144 | fio_mutex_remove(td->mutex); |
db1cd90b | 1145 | |
dfc6e751 SH |
1146 | /* |
1147 | * A new gid requires privilege, so we need to do this before setting | |
1148 | * the uid. | |
1149 | */ | |
e0b0d892 JA |
1150 | if (td->o.gid != -1U && setgid(td->o.gid)) { |
1151 | td_verror(td, errno, "setgid"); | |
1152 | goto err; | |
1153 | } | |
dfc6e751 SH |
1154 | if (td->o.uid != -1U && setuid(td->o.uid)) { |
1155 | td_verror(td, errno, "setuid"); | |
1156 | goto err; | |
1157 | } | |
e0b0d892 | 1158 | |
be18a94f JA |
1159 | /* |
1160 | * If we have a gettimeofday() thread, make sure we exclude that | |
1161 | * thread from this job | |
1162 | */ | |
1163 | if (td->o.gtod_cpu) | |
1164 | fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu); | |
1165 | ||
1166 | /* | |
1167 | * Set affinity first, in case it has an impact on the memory | |
1168 | * allocations. | |
1169 | */ | |
1170 | if (td->o.cpumask_set && fio_setaffinity(td->pid, td->o.cpumask) == -1) { | |
1171 | td_verror(td, errno, "cpu_set_affinity"); | |
1172 | goto err; | |
1173 | } | |
1174 | ||
d84f8d49 JA |
1175 | /* |
1176 | * May alter parameters that init_io_u() will use, so we need to | |
1177 | * do this first. | |
1178 | */ | |
1179 | if (init_iolog(td)) | |
1180 | goto err; | |
1181 | ||
ebac4655 | 1182 | if (init_io_u(td)) |
db1cd90b | 1183 | goto err; |
ebac4655 | 1184 | |
e8462bd8 JA |
1185 | if (td->o.verify_async && verify_async_init(td)) |
1186 | goto err; | |
1187 | ||
ac684785 | 1188 | if (td->ioprio_set) { |
ebac4655 | 1189 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) { |
e1161c32 | 1190 | td_verror(td, errno, "ioprio_set"); |
db1cd90b | 1191 | goto err; |
ebac4655 JA |
1192 | } |
1193 | } | |
1194 | ||
6adb38a1 | 1195 | if (td->o.cgroup_weight && cgroup_setup(td, cgroup_list, &cgroup_mnt)) |
a696fa2a JA |
1196 | goto err; |
1197 | ||
2dc1bbeb | 1198 | if (nice(td->o.nice) == -1) { |
e1161c32 | 1199 | td_verror(td, errno, "nice"); |
db1cd90b | 1200 | goto err; |
b6f4d880 JA |
1201 | } |
1202 | ||
2dc1bbeb | 1203 | if (td->o.ioscheduler && switch_ioscheduler(td)) |
db1cd90b | 1204 | goto err; |
37f56873 | 1205 | |
2dc1bbeb | 1206 | if (!td->o.create_serialize && setup_files(td)) |
ebac4655 JA |
1207 | goto err; |
1208 | ||
7d6c5283 JA |
1209 | if (td_io_init(td)) |
1210 | goto err; | |
1211 | ||
68727076 JA |
1212 | if (init_random_map(td)) |
1213 | goto err; | |
1214 | ||
2dc1bbeb | 1215 | if (td->o.exec_prerun) { |
398e8c4d | 1216 | if (exec_string(td->o.exec_prerun)) |
69cfd7e0 JA |
1217 | goto err; |
1218 | } | |
4e0ba8af | 1219 | |
afad68f7 ZY |
1220 | if (td->o.pre_read) { |
1221 | if (pre_read_files(td) < 0) | |
1222 | goto err; | |
1223 | } | |
1224 | ||
69008999 | 1225 | fio_gettime(&td->epoch, NULL); |
c8aaba19 | 1226 | getrusage(RUSAGE_SELF, &td->ru_start); |
69008999 | 1227 | |
a978ba68 | 1228 | clear_state = 0; |
492158cf | 1229 | while (keep_running(td)) { |
02bcaa8c | 1230 | fio_gettime(&td->start, NULL); |
c8eeb9df JA |
1231 | memcpy(&td->bw_sample_time, &td->start, sizeof(td->start)); |
1232 | memcpy(&td->iops_sample_time, &td->start, sizeof(td->start)); | |
993bf48b | 1233 | memcpy(&td->tv_cache, &td->start, sizeof(td->start)); |
ebac4655 | 1234 | |
cf2dd8d6 | 1235 | if (td->o.ratemin[0] || td->o.ratemin[1]) |
c8eeb9df | 1236 | memcpy(&td->lastrate, &td->bw_sample_time, |
5ec10eaa | 1237 | sizeof(td->lastrate)); |
ebac4655 | 1238 | |
df9c26b1 JA |
1239 | if (clear_state) |
1240 | clear_io_state(td); | |
a978ba68 | 1241 | |
ebac4655 JA |
1242 | prune_io_piece_log(td); |
1243 | ||
ba0fbe10 | 1244 | do_io(td); |
ebac4655 | 1245 | |
a978ba68 JA |
1246 | clear_state = 1; |
1247 | ||
38d77cae | 1248 | if (td_read(td) && td->io_bytes[DDIR_READ]) { |
0a4957c6 | 1249 | elapsed = utime_since_now(&td->start); |
cda99fa0 | 1250 | td->ts.runtime[DDIR_READ] += elapsed; |
38d77cae JA |
1251 | } |
1252 | if (td_write(td) && td->io_bytes[DDIR_WRITE]) { | |
0a4957c6 | 1253 | elapsed = utime_since_now(&td->start); |
cda99fa0 | 1254 | td->ts.runtime[DDIR_WRITE] += elapsed; |
38d77cae | 1255 | } |
5ec10eaa | 1256 | |
ebac4655 JA |
1257 | if (td->error || td->terminate) |
1258 | break; | |
1259 | ||
e84c73a8 SL |
1260 | if (!td->o.do_verify || |
1261 | td->o.verify == VERIFY_NONE || | |
b67740d3 | 1262 | (td->io_ops->flags & FIO_UNIDIR)) |
ebac4655 JA |
1263 | continue; |
1264 | ||
df9c26b1 | 1265 | clear_io_state(td); |
a978ba68 | 1266 | |
02bcaa8c | 1267 | fio_gettime(&td->start, NULL); |
ebac4655 JA |
1268 | |
1269 | do_verify(td); | |
1270 | ||
cda99fa0 | 1271 | td->ts.runtime[DDIR_READ] += utime_since_now(&td->start); |
ebac4655 JA |
1272 | |
1273 | if (td->error || td->terminate) | |
1274 | break; | |
1275 | } | |
1276 | ||
36dff966 | 1277 | update_rusage_stat(td); |
cda99fa0 SSP |
1278 | td->ts.runtime[0] = (td->ts.runtime[0] + 999) / 1000; |
1279 | td->ts.runtime[1] = (td->ts.runtime[1] + 999) / 1000; | |
756867bd JA |
1280 | td->ts.total_run_time = mtime_since_now(&td->epoch); |
1281 | td->ts.io_bytes[0] = td->io_bytes[0]; | |
1282 | td->ts.io_bytes[1] = td->io_bytes[1]; | |
1283 | ||
d2bbb8d9 | 1284 | fio_mutex_down(writeout_mutex); |
7b9f733a | 1285 | if (td->bw_log) { |
f484470b | 1286 | if (td->o.bw_log_file) { |
7b9f733a | 1287 | finish_log_named(td, td->bw_log, |
f484470b JA |
1288 | td->o.bw_log_file, "bw"); |
1289 | } else | |
7b9f733a | 1290 | finish_log(td, td->bw_log, "bw"); |
e3cedca7 | 1291 | } |
7b9f733a | 1292 | if (td->lat_log) { |
02af0988 | 1293 | if (td->o.lat_log_file) { |
7b9f733a | 1294 | finish_log_named(td, td->lat_log, |
02af0988 JA |
1295 | td->o.lat_log_file, "lat"); |
1296 | } else | |
7b9f733a | 1297 | finish_log(td, td->lat_log, "lat"); |
02af0988 | 1298 | } |
7b9f733a | 1299 | if (td->slat_log) { |
f484470b | 1300 | if (td->o.lat_log_file) { |
7b9f733a | 1301 | finish_log_named(td, td->slat_log, |
1ca95738 | 1302 | td->o.lat_log_file, "slat"); |
f484470b | 1303 | } else |
7b9f733a | 1304 | finish_log(td, td->slat_log, "slat"); |
e3cedca7 | 1305 | } |
7b9f733a | 1306 | if (td->clat_log) { |
f484470b | 1307 | if (td->o.lat_log_file) { |
7b9f733a | 1308 | finish_log_named(td, td->clat_log, |
f484470b JA |
1309 | td->o.lat_log_file, "clat"); |
1310 | } else | |
7b9f733a | 1311 | finish_log(td, td->clat_log, "clat"); |
e3cedca7 | 1312 | } |
c8eeb9df JA |
1313 | if (td->iops_log) { |
1314 | if (td->o.iops_log_file) { | |
1315 | finish_log_named(td, td->iops_log, | |
1316 | td->o.iops_log_file, "iops"); | |
1317 | } else | |
1318 | finish_log(td, td->iops_log, "iops"); | |
1319 | } | |
1320 | ||
d2bbb8d9 | 1321 | fio_mutex_up(writeout_mutex); |
398e8c4d JA |
1322 | if (td->o.exec_postrun) |
1323 | exec_string(td->o.exec_postrun); | |
ebac4655 JA |
1324 | |
1325 | if (exitall_on_terminate) | |
cc0df00a | 1326 | fio_terminate_threads(td->groupid); |
ebac4655 JA |
1327 | |
1328 | err: | |
5bf13a5a | 1329 | if (td->error) |
4ceb30d4 | 1330 | log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error, |
5ec10eaa | 1331 | td->verror); |
b4707ed7 JA |
1332 | |
1333 | if (td->o.verify_async) | |
1334 | verify_async_exit(td); | |
1335 | ||
24ffd2c2 | 1336 | close_and_free_files(td); |
2866c82d | 1337 | close_ioengine(td); |
ebac4655 | 1338 | cleanup_io_u(td); |
6adb38a1 | 1339 | cgroup_shutdown(td, &cgroup_mnt); |
f29b25a3 | 1340 | |
d2ce18b5 JA |
1341 | if (td->o.cpumask_set) { |
1342 | int ret = fio_cpuset_exit(&td->o.cpumask); | |
1343 | ||
1344 | td_verror(td, ret, "fio_cpuset_exit"); | |
1345 | } | |
4e78e405 | 1346 | |
f29b25a3 JA |
1347 | /* |
1348 | * do this very late, it will log file closing as well | |
1349 | */ | |
1350 | if (td->o.write_iolog_file) | |
1351 | write_iolog_close(td); | |
1352 | ||
d23bb327 | 1353 | options_mem_free(td); |
ebac4655 | 1354 | td_set_runstate(td, TD_EXITED); |
43d76807 | 1355 | return (void *) (unsigned long) td->error; |
ebac4655 JA |
1356 | } |
1357 | ||
906c8d75 JA |
1358 | /* |
1359 | * We cannot pass the td data into a forked process, so attach the td and | |
1360 | * pass it to the thread worker. | |
1361 | */ | |
a6418147 | 1362 | static int fork_main(int shmid, int offset) |
ebac4655 JA |
1363 | { |
1364 | struct thread_data *td; | |
a6418147 | 1365 | void *data, *ret; |
ebac4655 | 1366 | |
65258961 | 1367 | #ifndef __hpux |
ebac4655 JA |
1368 | data = shmat(shmid, NULL, 0); |
1369 | if (data == (void *) -1) { | |
a6418147 JA |
1370 | int __err = errno; |
1371 | ||
ebac4655 | 1372 | perror("shmat"); |
a6418147 | 1373 | return __err; |
ebac4655 | 1374 | } |
65258961 JA |
1375 | #else |
1376 | /* | |
1377 | * HP-UX inherits shm mappings? | |
1378 | */ | |
1379 | data = threads; | |
1380 | #endif | |
ebac4655 JA |
1381 | |
1382 | td = data + offset * sizeof(struct thread_data); | |
a6418147 | 1383 | ret = thread_main(td); |
ebac4655 | 1384 | shmdt(data); |
43d76807 | 1385 | return (int) (unsigned long) ret; |
ebac4655 JA |
1386 | } |
1387 | ||
906c8d75 JA |
1388 | /* |
1389 | * Run over the job map and reap the threads that have exited, if any. | |
1390 | */ | |
ebac4655 JA |
1391 | static void reap_threads(int *nr_running, int *t_rate, int *m_rate) |
1392 | { | |
34572e28 | 1393 | struct thread_data *td; |
1f809d15 | 1394 | int i, cputhreads, realthreads, pending, status, ret; |
ebac4655 JA |
1395 | |
1396 | /* | |
1397 | * reap exited threads (TD_EXITED -> TD_REAPED) | |
1398 | */ | |
1f809d15 | 1399 | realthreads = pending = cputhreads = 0; |
34572e28 | 1400 | for_each_td(td, i) { |
3707f45b | 1401 | int flags = 0; |
a2f77c9f | 1402 | |
84585003 JA |
1403 | /* |
1404 | * ->io_ops is NULL for a thread that has closed its | |
1405 | * io engine | |
1406 | */ | |
ba0fbe10 | 1407 | if (td->io_ops && !strcmp(td->io_ops->name, "cpuio")) |
b990b5c0 | 1408 | cputhreads++; |
1f809d15 JA |
1409 | else |
1410 | realthreads++; | |
b990b5c0 | 1411 | |
a1dedc1f AC |
1412 | if (!td->pid) { |
1413 | pending++; | |
1414 | continue; | |
1415 | } | |
1416 | if (td->runstate == TD_REAPED) | |
a2f77c9f | 1417 | continue; |
2dc1bbeb | 1418 | if (td->o.use_thread) { |
3707f45b JA |
1419 | if (td->runstate == TD_EXITED) { |
1420 | td_set_runstate(td, TD_REAPED); | |
1421 | goto reaped; | |
1422 | } | |
1423 | continue; | |
1424 | } | |
a2f77c9f JA |
1425 | |
1426 | flags = WNOHANG; | |
1427 | if (td->runstate == TD_EXITED) | |
1428 | flags = 0; | |
1429 | ||
1430 | /* | |
1431 | * check if someone quit or got killed in an unusual way | |
1432 | */ | |
1433 | ret = waitpid(td->pid, &status, flags); | |
3707f45b | 1434 | if (ret < 0) { |
a2f77c9f | 1435 | if (errno == ECHILD) { |
5921e80c JA |
1436 | log_err("fio: pid=%d disappeared %d\n", |
1437 | (int) td->pid, td->runstate); | |
a2f77c9f JA |
1438 | td_set_runstate(td, TD_REAPED); |
1439 | goto reaped; | |
1440 | } | |
1441 | perror("waitpid"); | |
1442 | } else if (ret == td->pid) { | |
1443 | if (WIFSIGNALED(status)) { | |
fab6aa71 JA |
1444 | int sig = WTERMSIG(status); |
1445 | ||
03e20d68 | 1446 | if (sig != SIGTERM) |
5ec10eaa | 1447 | log_err("fio: pid=%d, got signal=%d\n", |
5921e80c | 1448 | (int) td->pid, sig); |
fab6aa71 JA |
1449 | td_set_runstate(td, TD_REAPED); |
1450 | goto reaped; | |
1451 | } | |
a2f77c9f JA |
1452 | if (WIFEXITED(status)) { |
1453 | if (WEXITSTATUS(status) && !td->error) | |
1454 | td->error = WEXITSTATUS(status); | |
a2f77c9f | 1455 | |
a2f77c9f JA |
1456 | td_set_runstate(td, TD_REAPED); |
1457 | goto reaped; | |
a6418147 JA |
1458 | } |
1459 | } | |
ebac4655 | 1460 | |
a2f77c9f JA |
1461 | /* |
1462 | * thread is not dead, continue | |
1463 | */ | |
e48676ba | 1464 | pending++; |
a2f77c9f | 1465 | continue; |
fab6aa71 | 1466 | reaped: |
ebac4655 | 1467 | (*nr_running)--; |
581e7141 JA |
1468 | (*m_rate) -= (td->o.ratemin[0] + td->o.ratemin[1]); |
1469 | (*t_rate) -= (td->o.rate[0] + td->o.rate[1]); | |
6f88bcb0 JA |
1470 | if (!td->pid) |
1471 | pending--; | |
a2f77c9f JA |
1472 | |
1473 | if (td->error) | |
1474 | exit_value++; | |
d3eeeabc JA |
1475 | |
1476 | done_secs += mtime_since_now(&td->epoch) / 1000; | |
ebac4655 | 1477 | } |
b990b5c0 | 1478 | |
1f809d15 | 1479 | if (*nr_running == cputhreads && !pending && realthreads) |
cc0df00a | 1480 | fio_terminate_threads(TERMINATE_ALL); |
ebac4655 JA |
1481 | } |
1482 | ||
be4ecfdf JA |
1483 | static void *gtod_thread_main(void *data) |
1484 | { | |
1485 | fio_mutex_up(startup_mutex); | |
1486 | ||
1487 | /* | |
1488 | * As long as we have jobs around, update the clock. It would be nice | |
1489 | * to have some way of NOT hammering that CPU with gettimeofday(), | |
1490 | * but I'm not sure what to use outside of a simple CPU nop to relax | |
1491 | * it - we don't want to lose precision. | |
1492 | */ | |
1493 | while (threads) { | |
1494 | fio_gtod_update(); | |
1495 | nop; | |
1496 | } | |
1497 | ||
1498 | return NULL; | |
1499 | } | |
1500 | ||
1501 | static int fio_start_gtod_thread(void) | |
1502 | { | |
304a47c7 | 1503 | pthread_attr_t attr; |
f42c153d JA |
1504 | int ret; |
1505 | ||
304a47c7 VA |
1506 | pthread_attr_init(&attr); |
1507 | pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN); | |
1508 | ret = pthread_create(>od_thread, &attr, gtod_thread_main, NULL); | |
1509 | pthread_attr_destroy(&attr); | |
f42c153d JA |
1510 | if (ret) { |
1511 | log_err("Can't create gtod thread: %s\n", strerror(ret)); | |
be4ecfdf JA |
1512 | return 1; |
1513 | } | |
f42c153d JA |
1514 | |
1515 | ret = pthread_detach(gtod_thread); | |
1516 | if (ret) { | |
1517 | log_err("Can't detatch gtod thread: %s\n", strerror(ret)); | |
be4ecfdf JA |
1518 | return 1; |
1519 | } | |
1520 | ||
29adda3c | 1521 | dprint(FD_MUTEX, "wait on startup_mutex\n"); |
be4ecfdf | 1522 | fio_mutex_down(startup_mutex); |
29adda3c | 1523 | dprint(FD_MUTEX, "done waiting on startup_mutex\n"); |
be4ecfdf JA |
1524 | return 0; |
1525 | } | |
1526 | ||
906c8d75 JA |
1527 | /* |
1528 | * Main function for kicking off and reaping jobs, as needed. | |
1529 | */ | |
ebac4655 JA |
1530 | static void run_threads(void) |
1531 | { | |
ebac4655 JA |
1532 | struct thread_data *td; |
1533 | unsigned long spent; | |
1534 | int i, todo, nr_running, m_rate, t_rate, nr_started; | |
fcb6ade2 | 1535 | |
2f9ade3c JA |
1536 | if (fio_pin_memory()) |
1537 | return; | |
ebac4655 | 1538 | |
be4ecfdf JA |
1539 | if (fio_gtod_offload && fio_start_gtod_thread()) |
1540 | return; | |
1541 | ||
cc0df00a JA |
1542 | set_sig_handlers(); |
1543 | ||
c6ae0a5b | 1544 | if (!terse_output) { |
4ceb30d4 | 1545 | log_info("Starting "); |
9cedf167 | 1546 | if (nr_thread) |
4ceb30d4 | 1547 | log_info("%d thread%s", nr_thread, |
5ec10eaa | 1548 | nr_thread > 1 ? "s" : ""); |
9cedf167 JA |
1549 | if (nr_process) { |
1550 | if (nr_thread) | |
1551 | printf(" and "); | |
4ceb30d4 | 1552 | log_info("%d process%s", nr_process, |
5ec10eaa | 1553 | nr_process > 1 ? "es" : ""); |
9cedf167 | 1554 | } |
4ceb30d4 | 1555 | log_info("\n"); |
c6ae0a5b JA |
1556 | fflush(stdout); |
1557 | } | |
c04f7ec3 | 1558 | |
ebac4655 JA |
1559 | todo = thread_number; |
1560 | nr_running = 0; | |
1561 | nr_started = 0; | |
1562 | m_rate = t_rate = 0; | |
1563 | ||
34572e28 | 1564 | for_each_td(td, i) { |
263e529f | 1565 | print_status_init(td->thread_number - 1); |
ebac4655 | 1566 | |
067430bb | 1567 | if (!td->o.create_serialize) |
ebac4655 JA |
1568 | continue; |
1569 | ||
1570 | /* | |
1571 | * do file setup here so it happens sequentially, | |
1572 | * we don't want X number of threads getting their | |
1573 | * client data interspersed on disk | |
1574 | */ | |
53cdc686 | 1575 | if (setup_files(td)) { |
5bf13a5a JA |
1576 | exit_value++; |
1577 | if (td->error) | |
5921e80c JA |
1578 | log_err("fio: pid=%d, err=%d/%s\n", |
1579 | (int) td->pid, td->error, td->verror); | |
ebac4655 JA |
1580 | td_set_runstate(td, TD_REAPED); |
1581 | todo--; | |
c69e8875 JA |
1582 | } else { |
1583 | struct fio_file *f; | |
2b13e716 | 1584 | unsigned int j; |
c69e8875 JA |
1585 | |
1586 | /* | |
1587 | * for sharing to work, each job must always open | |
1588 | * its own files. so close them, if we opened them | |
1589 | * for creation | |
1590 | */ | |
2b13e716 | 1591 | for_each_file(td, f, j) { |
22a57ba8 JA |
1592 | if (fio_file_open(f)) |
1593 | td_io_close_file(td, f); | |
22a57ba8 | 1594 | } |
5ec10eaa | 1595 | } |
ebac4655 JA |
1596 | } |
1597 | ||
a2f77c9f JA |
1598 | set_genesis_time(); |
1599 | ||
ebac4655 | 1600 | while (todo) { |
fca70358 | 1601 | struct thread_data *map[REAL_MAX_JOBS]; |
75154845 JA |
1602 | struct timeval this_start; |
1603 | int this_jobs = 0, left; | |
1604 | ||
ebac4655 JA |
1605 | /* |
1606 | * create threads (TD_NOT_CREATED -> TD_CREATED) | |
1607 | */ | |
34572e28 | 1608 | for_each_td(td, i) { |
ebac4655 JA |
1609 | if (td->runstate != TD_NOT_CREATED) |
1610 | continue; | |
1611 | ||
1612 | /* | |
1613 | * never got a chance to start, killed by other | |
1614 | * thread for some reason | |
1615 | */ | |
1616 | if (td->terminate) { | |
1617 | todo--; | |
1618 | continue; | |
1619 | } | |
1620 | ||
2dc1bbeb | 1621 | if (td->o.start_delay) { |
263e529f | 1622 | spent = mtime_since_genesis(); |
ebac4655 | 1623 | |
2dc1bbeb | 1624 | if (td->o.start_delay * 1000 > spent) |
ebac4655 JA |
1625 | continue; |
1626 | } | |
1627 | ||
6f88bcb0 JA |
1628 | if (td->o.stonewall && (nr_started || nr_running)) { |
1629 | dprint(FD_PROCESS, "%s: stonewall wait\n", | |
1630 | td->o.name); | |
ebac4655 | 1631 | break; |
6f88bcb0 | 1632 | } |
ebac4655 | 1633 | |
067430bb JA |
1634 | init_disk_util(td); |
1635 | ||
75154845 JA |
1636 | /* |
1637 | * Set state to created. Thread will transition | |
1638 | * to TD_INITIALIZED when it's done setting up. | |
1639 | */ | |
ebac4655 | 1640 | td_set_runstate(td, TD_CREATED); |
75154845 | 1641 | map[this_jobs++] = td; |
ebac4655 JA |
1642 | nr_started++; |
1643 | ||
2dc1bbeb | 1644 | if (td->o.use_thread) { |
f42c153d JA |
1645 | int ret; |
1646 | ||
ee56ad50 | 1647 | dprint(FD_PROCESS, "will pthread_create\n"); |
f42c153d JA |
1648 | ret = pthread_create(&td->thread, NULL, |
1649 | thread_main, td); | |
1650 | if (ret) { | |
1651 | log_err("pthread_create: %s\n", | |
1652 | strerror(ret)); | |
ebac4655 | 1653 | nr_started--; |
c8bb6faf | 1654 | break; |
ebac4655 | 1655 | } |
f42c153d JA |
1656 | ret = pthread_detach(td->thread); |
1657 | if (ret) | |
1658 | log_err("pthread_detach: %s", | |
1659 | strerror(ret)); | |
ebac4655 | 1660 | } else { |
5e1d306e | 1661 | pid_t pid; |
ee56ad50 | 1662 | dprint(FD_PROCESS, "will fork\n"); |
5e1d306e JA |
1663 | pid = fork(); |
1664 | if (!pid) { | |
a6418147 JA |
1665 | int ret = fork_main(shm_id, i); |
1666 | ||
5e1d306e JA |
1667 | _exit(ret); |
1668 | } else if (i == fio_debug_jobno) | |
1669 | *fio_debug_jobp = pid; | |
ebac4655 | 1670 | } |
29adda3c | 1671 | dprint(FD_MUTEX, "wait on startup_mutex\n"); |
656b1393 JA |
1672 | if (fio_mutex_down_timeout(startup_mutex, 10)) { |
1673 | log_err("fio: job startup hung? exiting.\n"); | |
cc0df00a | 1674 | fio_terminate_threads(TERMINATE_ALL); |
656b1393 JA |
1675 | fio_abort = 1; |
1676 | nr_started--; | |
1677 | break; | |
1678 | } | |
29adda3c | 1679 | dprint(FD_MUTEX, "done waiting on startup_mutex\n"); |
ebac4655 JA |
1680 | } |
1681 | ||
1682 | /* | |
75154845 JA |
1683 | * Wait for the started threads to transition to |
1684 | * TD_INITIALIZED. | |
ebac4655 | 1685 | */ |
02bcaa8c | 1686 | fio_gettime(&this_start, NULL); |
75154845 | 1687 | left = this_jobs; |
6ce15a32 | 1688 | while (left && !fio_abort) { |
75154845 JA |
1689 | if (mtime_since_now(&this_start) > JOB_START_TIMEOUT) |
1690 | break; | |
1691 | ||
1692 | usleep(100000); | |
1693 | ||
1694 | for (i = 0; i < this_jobs; i++) { | |
1695 | td = map[i]; | |
1696 | if (!td) | |
1697 | continue; | |
b6f4d880 | 1698 | if (td->runstate == TD_INITIALIZED) { |
75154845 JA |
1699 | map[i] = NULL; |
1700 | left--; | |
b6f4d880 JA |
1701 | } else if (td->runstate >= TD_EXITED) { |
1702 | map[i] = NULL; | |
1703 | left--; | |
1704 | todo--; | |
1705 | nr_running++; /* work-around... */ | |
75154845 JA |
1706 | } |
1707 | } | |
1708 | } | |
1709 | ||
1710 | if (left) { | |
3b70d7e5 | 1711 | log_err("fio: %d jobs failed to start\n", left); |
75154845 JA |
1712 | for (i = 0; i < this_jobs; i++) { |
1713 | td = map[i]; | |
1714 | if (!td) | |
1715 | continue; | |
1716 | kill(td->pid, SIGTERM); | |
1717 | } | |
1718 | break; | |
1719 | } | |
1720 | ||
1721 | /* | |
b6f4d880 | 1722 | * start created threads (TD_INITIALIZED -> TD_RUNNING). |
75154845 | 1723 | */ |
34572e28 | 1724 | for_each_td(td, i) { |
75154845 | 1725 | if (td->runstate != TD_INITIALIZED) |
ebac4655 JA |
1726 | continue; |
1727 | ||
b29ee5b3 JA |
1728 | if (in_ramp_time(td)) |
1729 | td_set_runstate(td, TD_RAMP); | |
1730 | else | |
1731 | td_set_runstate(td, TD_RUNNING); | |
ebac4655 JA |
1732 | nr_running++; |
1733 | nr_started--; | |
581e7141 JA |
1734 | m_rate += td->o.ratemin[0] + td->o.ratemin[1]; |
1735 | t_rate += td->o.rate[0] + td->o.rate[1]; | |
75154845 | 1736 | todo--; |
cdd18ad8 | 1737 | fio_mutex_up(td->mutex); |
ebac4655 JA |
1738 | } |
1739 | ||
1740 | reap_threads(&nr_running, &t_rate, &m_rate); | |
1741 | ||
602d6ff9 JA |
1742 | if (todo) { |
1743 | if (is_backend) | |
1744 | fio_server_idle_loop(); | |
1745 | else | |
1746 | usleep(100000); | |
1747 | } | |
ebac4655 JA |
1748 | } |
1749 | ||
1750 | while (nr_running) { | |
1751 | reap_threads(&nr_running, &t_rate, &m_rate); | |
cc0df00a JA |
1752 | |
1753 | if (is_backend) | |
1754 | fio_server_idle_loop(); | |
70e0c316 JA |
1755 | else |
1756 | usleep(10000); | |
ebac4655 JA |
1757 | } |
1758 | ||
1759 | update_io_ticks(); | |
2f9ade3c | 1760 | fio_unpin_memory(); |
ebac4655 JA |
1761 | } |
1762 | ||
50d16976 | 1763 | int exec_run(void) |
ebac4655 | 1764 | { |
a37f69b7 | 1765 | if (nr_clients) |
37db14fe | 1766 | return fio_handle_clients(); |
07b3232d JA |
1767 | if (exec_profile && load_profile(exec_profile)) |
1768 | return 1; | |
1769 | ||
74929ac2 JA |
1770 | if (!thread_number) |
1771 | return 0; | |
1772 | ||
bb3884d8 JA |
1773 | if (write_bw_log) { |
1774 | setup_log(&agg_io_log[DDIR_READ]); | |
1775 | setup_log(&agg_io_log[DDIR_WRITE]); | |
1776 | } | |
1777 | ||
cdd18ad8 | 1778 | startup_mutex = fio_mutex_init(0); |
03e20d68 BC |
1779 | if (startup_mutex == NULL) |
1780 | return 1; | |
d2bbb8d9 | 1781 | writeout_mutex = fio_mutex_init(1); |
03e20d68 BC |
1782 | if (writeout_mutex == NULL) |
1783 | return 1; | |
07739b57 | 1784 | |
a2f77c9f | 1785 | set_genesis_time(); |
7985701b | 1786 | create_disk_util_thread(); |
ebac4655 | 1787 | |
dae5341c JA |
1788 | cgroup_list = smalloc(sizeof(*cgroup_list)); |
1789 | INIT_FLIST_HEAD(cgroup_list); | |
1790 | ||
ebac4655 | 1791 | run_threads(); |
6ce15a32 | 1792 | |
bb3884d8 | 1793 | if (!fio_abort) { |
6ce15a32 | 1794 | show_run_stats(); |
bb3884d8 | 1795 | if (write_bw_log) { |
5ec10eaa JA |
1796 | __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log"); |
1797 | __finish_log(agg_io_log[DDIR_WRITE], | |
1798 | "agg-write_bw.log"); | |
bb3884d8 JA |
1799 | } |
1800 | } | |
ebac4655 | 1801 | |
dae5341c JA |
1802 | cgroup_kill(cgroup_list); |
1803 | sfree(cgroup_list); | |
61eb313e | 1804 | sfree(cgroup_mnt); |
39f22027 | 1805 | |
cdd18ad8 | 1806 | fio_mutex_remove(startup_mutex); |
d2bbb8d9 | 1807 | fio_mutex_remove(writeout_mutex); |
437c9b71 | 1808 | return exit_value; |
ebac4655 | 1809 | } |
50d16976 JA |
1810 | |
1811 | void reset_fio_state(void) | |
1812 | { | |
1813 | groupid = 0; | |
1814 | thread_number = 0; | |
1815 | nr_process = 0; | |
1816 | nr_thread = 0; | |
1817 | done_secs = 0; | |
1818 | } | |
1819 | ||
0ad5edcc JA |
1820 | static int endian_check(void) |
1821 | { | |
1822 | union { | |
1823 | uint8_t c[8]; | |
1824 | uint64_t v; | |
1825 | } u; | |
1826 | int le = 0, be = 0; | |
1827 | ||
1828 | u.v = 0x12; | |
1829 | if (u.c[7] == 0x12) | |
1830 | be = 1; | |
1831 | else if (u.c[0] == 0x12) | |
1832 | le = 1; | |
1833 | ||
1834 | #if defined(FIO_LITTLE_ENDIAN) | |
1835 | if (be) | |
1836 | return 1; | |
1837 | #elif defined(FIO_BIG_ENDIAN) | |
1838 | if (le) | |
1839 | return 1; | |
1840 | #else | |
1841 | return 1; | |
1842 | #endif | |
1843 | ||
1844 | if (!le && !be) | |
1845 | return 1; | |
1846 | ||
1847 | return 0; | |
1848 | } | |
1849 | ||
50d16976 JA |
1850 | int main(int argc, char *argv[], char *envp[]) |
1851 | { | |
1852 | long ps; | |
1853 | ||
0ad5edcc JA |
1854 | if (endian_check()) { |
1855 | log_err("fio: endianness settings appear wrong.\n"); | |
1856 | log_err("fio: please report this to fio@vger.kernel.org\n"); | |
1857 | return 1; | |
1858 | } | |
1859 | ||
50d16976 JA |
1860 | arch_init(envp); |
1861 | ||
1862 | sinit(); | |
1863 | ||
1864 | /* | |
1865 | * We need locale for number printing, if it isn't set then just | |
1866 | * go with the US format. | |
1867 | */ | |
1868 | if (!getenv("LC_NUMERIC")) | |
1869 | setlocale(LC_NUMERIC, "en_US"); | |
1870 | ||
1871 | ps = sysconf(_SC_PAGESIZE); | |
1872 | if (ps < 0) { | |
1873 | log_err("Failed to get page size\n"); | |
1874 | return 1; | |
1875 | } | |
1876 | ||
1877 | page_size = ps; | |
1878 | page_mask = ps - 1; | |
1879 | ||
1880 | fio_keywords_init(); | |
1881 | ||
1882 | if (parse_options(argc, argv)) | |
1883 | return 1; | |
1884 | ||
1885 | return exec_run(); | |
1886 | } |