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