Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | #include <fcntl.h> |
| 3 | #include <string.h> |
| 4 | #include <assert.h> |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 5 | #include <dirent.h> |
YAMAMOTO Takashi | d74ac84 | 2010-06-09 09:44:53 +0200 | [diff] [blame] | 6 | #include <libgen.h> |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 7 | #include <sys/stat.h> |
| 8 | #include <sys/mman.h> |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 9 | #include <sys/types.h> |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 10 | |
| 11 | #include "fio.h" |
Jens Axboe | f17c439 | 2008-03-01 18:40:46 +0100 | [diff] [blame] | 12 | #include "smalloc.h" |
Jens Axboe | 4906e0b | 2008-03-01 18:59:51 +0100 | [diff] [blame] | 13 | #include "filehash.h" |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 14 | #include "os/os.h" |
Jens Axboe | 2316296 | 2012-11-07 19:47:47 +0100 | [diff] [blame] | 15 | #include "hash.h" |
Jens Axboe | 7ebd796 | 2012-11-28 21:24:46 +0100 | [diff] [blame] | 16 | #include "lib/axmap.h" |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 17 | |
Jens Axboe | 97ac992 | 2013-01-24 15:00:25 -0700 | [diff] [blame] | 18 | #ifdef CONFIG_LINUX_FALLOCATE |
Eric Gouriou | a596f04 | 2011-06-17 09:11:45 +0200 | [diff] [blame] | 19 | #include <linux/falloc.h> |
| 20 | #endif |
| 21 | |
Jens Axboe | 7172cfe | 2007-07-23 14:36:00 +0200 | [diff] [blame] | 22 | static int root_warn; |
| 23 | |
Jens Axboe | c592b9f | 2009-06-03 09:29:28 +0200 | [diff] [blame] | 24 | static inline void clear_error(struct thread_data *td) |
| 25 | { |
| 26 | td->error = 0; |
| 27 | td->verror[0] = '\0'; |
| 28 | } |
| 29 | |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 30 | /* |
| 31 | * Leaves f->fd open on success, caller must close |
| 32 | */ |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 33 | static int extend_file(struct thread_data *td, struct fio_file *f) |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 34 | { |
Jens Axboe | ea44365 | 2007-03-29 14:52:13 +0200 | [diff] [blame] | 35 | int r, new_layout = 0, unlink_file = 0, flags; |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 36 | unsigned long long left; |
| 37 | unsigned int bs; |
| 38 | char *b; |
Jens Axboe | b2a1519 | 2006-10-18 14:10:42 +0200 | [diff] [blame] | 39 | |
Jens Axboe | 4241ea8 | 2007-09-12 08:18:36 +0200 | [diff] [blame] | 40 | if (read_only) { |
| 41 | log_err("fio: refusing extend of file due to read-only\n"); |
| 42 | return 0; |
| 43 | } |
| 44 | |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 45 | /* |
| 46 | * check if we need to lay the file out complete again. fio |
| 47 | * does that for operations involving reads, or for writes |
| 48 | * where overwrite is set |
| 49 | */ |
Jens Axboe | ffdfabd | 2008-03-26 09:18:14 +0100 | [diff] [blame] | 50 | if (td_read(td) || (td_write(td) && td->o.overwrite) || |
| 51 | (td_write(td) && td->io_ops->flags & FIO_NOEXTEND)) |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 52 | new_layout = 1; |
Jens Axboe | ea44365 | 2007-03-29 14:52:13 +0200 | [diff] [blame] | 53 | if (td_write(td) && !td->o.overwrite) |
| 54 | unlink_file = 1; |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 55 | |
Jens Axboe | 6ae1f57 | 2008-02-21 10:20:00 +0100 | [diff] [blame] | 56 | if (unlink_file || new_layout) { |
Jens Axboe | bd199f2 | 2008-03-26 09:57:18 +0100 | [diff] [blame] | 57 | dprint(FD_FILE, "layout unlink %s\n", f->file_name); |
Zhang, Yanmin | 982016d | 2008-02-26 15:35:52 +0100 | [diff] [blame] | 58 | if ((unlink(f->file_name) < 0) && (errno != ENOENT)) { |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 59 | td_verror(td, errno, "unlink"); |
| 60 | return 1; |
| 61 | } |
| 62 | } |
| 63 | |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 64 | flags = O_WRONLY | O_CREAT; |
| 65 | if (new_layout) |
| 66 | flags |= O_TRUNC; |
| 67 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 68 | dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags); |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 69 | f->fd = open(f->file_name, flags, 0644); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 70 | if (f->fd < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 71 | td_verror(td, errno, "open"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 72 | return 1; |
| 73 | } |
| 74 | |
Jens Axboe | 97ac992 | 2013-01-24 15:00:25 -0700 | [diff] [blame] | 75 | #ifdef CONFIG_POSIX_FALLOCATE |
Eric Gouriou | a596f04 | 2011-06-17 09:11:45 +0200 | [diff] [blame] | 76 | if (!td->o.fill_device) { |
| 77 | switch (td->o.fallocate_mode) { |
| 78 | case FIO_FALLOCATE_NONE: |
| 79 | break; |
| 80 | case FIO_FALLOCATE_POSIX: |
| 81 | dprint(FD_FILE, "posix_fallocate file %s size %llu\n", |
Jens Axboe | 4b91ee8 | 2013-02-25 10:18:33 +0100 | [diff] [blame] | 82 | f->file_name, |
| 83 | (unsigned long long) f->real_file_size); |
Jens Axboe | 7bc8c2c | 2010-01-28 11:31:31 +0100 | [diff] [blame] | 84 | |
Eric Gouriou | a596f04 | 2011-06-17 09:11:45 +0200 | [diff] [blame] | 85 | r = posix_fallocate(f->fd, 0, f->real_file_size); |
| 86 | if (r > 0) { |
| 87 | log_err("fio: posix_fallocate fails: %s\n", |
| 88 | strerror(r)); |
| 89 | } |
| 90 | break; |
Jens Axboe | 97ac992 | 2013-01-24 15:00:25 -0700 | [diff] [blame] | 91 | #ifdef CONFIG_LINUX_FALLOCATE |
Eric Gouriou | a596f04 | 2011-06-17 09:11:45 +0200 | [diff] [blame] | 92 | case FIO_FALLOCATE_KEEP_SIZE: |
| 93 | dprint(FD_FILE, |
| 94 | "fallocate(FALLOC_FL_KEEP_SIZE) " |
Jens Axboe | 4b91ee8 | 2013-02-25 10:18:33 +0100 | [diff] [blame] | 95 | "file %s size %llu\n", f->file_name, |
| 96 | (unsigned long long) f->real_file_size); |
Eric Gouriou | a596f04 | 2011-06-17 09:11:45 +0200 | [diff] [blame] | 97 | |
| 98 | r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0, |
| 99 | f->real_file_size); |
Jens Axboe | 888677a | 2013-04-10 22:19:46 +0200 | [diff] [blame] | 100 | if (r != 0) |
Eric Gouriou | a596f04 | 2011-06-17 09:11:45 +0200 | [diff] [blame] | 101 | td_verror(td, errno, "fallocate"); |
Jens Axboe | 888677a | 2013-04-10 22:19:46 +0200 | [diff] [blame] | 102 | |
Eric Gouriou | a596f04 | 2011-06-17 09:11:45 +0200 | [diff] [blame] | 103 | break; |
Jens Axboe | 97ac992 | 2013-01-24 15:00:25 -0700 | [diff] [blame] | 104 | #endif /* CONFIG_LINUX_FALLOCATE */ |
Eric Gouriou | a596f04 | 2011-06-17 09:11:45 +0200 | [diff] [blame] | 105 | default: |
| 106 | log_err("fio: unknown fallocate mode: %d\n", |
| 107 | td->o.fallocate_mode); |
| 108 | assert(0); |
Jens Axboe | 7bc8c2c | 2010-01-28 11:31:31 +0100 | [diff] [blame] | 109 | } |
| 110 | } |
Jens Axboe | 97ac992 | 2013-01-24 15:00:25 -0700 | [diff] [blame] | 111 | #endif /* CONFIG_POSIX_FALLOCATE */ |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 112 | |
Shawn Lewis | fc74ac1 | 2008-01-11 09:45:11 +0100 | [diff] [blame] | 113 | if (!new_layout) |
| 114 | goto done; |
| 115 | |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 116 | /* |
| 117 | * The size will be -1ULL when fill_device is used, so don't truncate |
| 118 | * or fallocate this file, just write it |
| 119 | */ |
| 120 | if (!td->o.fill_device) { |
| 121 | dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name, |
Jens Axboe | 4b91ee8 | 2013-02-25 10:18:33 +0100 | [diff] [blame] | 122 | (unsigned long long) f->real_file_size); |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 123 | if (ftruncate(f->fd, f->real_file_size) == -1) { |
| 124 | td_verror(td, errno, "ftruncate"); |
| 125 | goto err; |
| 126 | } |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 127 | } |
Jens Axboe | 40f8298 | 2006-10-25 11:21:05 +0200 | [diff] [blame] | 128 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 129 | b = malloc(td->o.max_bs[DDIR_WRITE]); |
| 130 | memset(b, 0, td->o.max_bs[DDIR_WRITE]); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 131 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 132 | left = f->real_file_size; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 133 | while (left && !td->terminate) { |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 134 | bs = td->o.max_bs[DDIR_WRITE]; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 135 | if (bs > left) |
| 136 | bs = left; |
| 137 | |
| 138 | r = write(f->fd, b, bs); |
| 139 | |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 140 | if (r > 0) { |
| 141 | left -= r; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 142 | continue; |
| 143 | } else { |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 144 | if (r < 0) { |
| 145 | int __e = errno; |
| 146 | |
| 147 | if (__e == ENOSPC) { |
| 148 | if (td->o.fill_device) |
| 149 | break; |
| 150 | log_info("fio: ENOSPC on laying out " |
| 151 | "file, stopping\n"); |
| 152 | break; |
| 153 | } |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 154 | td_verror(td, errno, "write"); |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 155 | } else |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 156 | td_verror(td, EIO, "write"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 157 | |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | |
Jens Axboe | ffdfabd | 2008-03-26 09:18:14 +0100 | [diff] [blame] | 162 | if (td->terminate) { |
| 163 | dprint(FD_FILE, "terminate unlink %s\n", f->file_name); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 164 | unlink(f->file_name); |
Jens Axboe | ffdfabd | 2008-03-26 09:18:14 +0100 | [diff] [blame] | 165 | } else if (td->o.create_fsync) { |
Jens Axboe | 98e1ac4 | 2008-03-06 11:56:48 +0100 | [diff] [blame] | 166 | if (fsync(f->fd) < 0) { |
| 167 | td_verror(td, errno, "fsync"); |
| 168 | goto err; |
| 169 | } |
| 170 | } |
Jens Axboe | 0d1cd20 | 2009-06-05 22:11:52 +0200 | [diff] [blame] | 171 | if (td->o.fill_device && !td_write(td)) { |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 172 | fio_file_clear_size_known(f); |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 173 | if (td_io_get_file_size(td, f)) |
| 174 | goto err; |
| 175 | if (f->io_size > f->real_file_size) |
| 176 | f->io_size = f->real_file_size; |
| 177 | } |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 178 | |
| 179 | free(b); |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 180 | done: |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 181 | return 0; |
| 182 | err: |
| 183 | close(f->fd); |
| 184 | f->fd = -1; |
| 185 | return 1; |
| 186 | } |
| 187 | |
Zhang, Yanmin | afad68f | 2009-05-20 11:30:55 +0200 | [diff] [blame] | 188 | static int pre_read_file(struct thread_data *td, struct fio_file *f) |
| 189 | { |
Jens Axboe | b0f6586 | 2009-05-20 11:52:15 +0200 | [diff] [blame] | 190 | int r, did_open = 0, old_runstate; |
Zhang, Yanmin | afad68f | 2009-05-20 11:30:55 +0200 | [diff] [blame] | 191 | unsigned long long left; |
| 192 | unsigned int bs; |
| 193 | char *b; |
| 194 | |
Jens Axboe | 9c0d224 | 2009-07-01 12:26:28 +0200 | [diff] [blame] | 195 | if (td->io_ops->flags & FIO_PIPEIO) |
| 196 | return 0; |
| 197 | |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 198 | if (!fio_file_open(f)) { |
Jens Axboe | b0f6586 | 2009-05-20 11:52:15 +0200 | [diff] [blame] | 199 | if (td->io_ops->open_file(td, f)) { |
| 200 | log_err("fio: cannot pre-read, failed to open file\n"); |
| 201 | return 1; |
| 202 | } |
| 203 | did_open = 1; |
| 204 | } |
| 205 | |
| 206 | old_runstate = td->runstate; |
| 207 | td_set_runstate(td, TD_PRE_READING); |
| 208 | |
Zhang, Yanmin | afad68f | 2009-05-20 11:30:55 +0200 | [diff] [blame] | 209 | bs = td->o.max_bs[DDIR_READ]; |
| 210 | b = malloc(bs); |
| 211 | memset(b, 0, bs); |
| 212 | |
| 213 | lseek(f->fd, f->file_offset, SEEK_SET); |
| 214 | left = f->io_size; |
| 215 | |
| 216 | while (left && !td->terminate) { |
| 217 | if (bs > left) |
| 218 | bs = left; |
| 219 | |
| 220 | r = read(f->fd, b, bs); |
| 221 | |
| 222 | if (r == (int) bs) { |
| 223 | left -= bs; |
| 224 | continue; |
| 225 | } else { |
| 226 | td_verror(td, EIO, "pre_read"); |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | |
Jens Axboe | b0f6586 | 2009-05-20 11:52:15 +0200 | [diff] [blame] | 231 | td_set_runstate(td, old_runstate); |
| 232 | |
| 233 | if (did_open) |
| 234 | td->io_ops->close_file(td, f); |
Zhang, Yanmin | afad68f | 2009-05-20 11:30:55 +0200 | [diff] [blame] | 235 | free(b); |
| 236 | return 0; |
| 237 | } |
| 238 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 239 | static unsigned long long get_rand_file_size(struct thread_data *td) |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 240 | { |
Jens Axboe | dc873b6 | 2008-06-04 20:13:04 +0200 | [diff] [blame] | 241 | unsigned long long ret, sized; |
Jens Axboe | 1294c3e | 2011-05-11 08:15:18 +0200 | [diff] [blame] | 242 | unsigned long r; |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 243 | |
Jens Axboe | 4c07ad8 | 2011-03-28 09:51:09 +0200 | [diff] [blame] | 244 | if (td->o.use_os_rand) { |
| 245 | r = os_random_long(&td->file_size_state); |
| 246 | sized = td->o.file_size_high - td->o.file_size_low; |
| 247 | ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0))); |
| 248 | } else { |
| 249 | r = __rand(&td->__file_size_state); |
| 250 | sized = td->o.file_size_high - td->o.file_size_low; |
| 251 | ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0))); |
| 252 | } |
| 253 | |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 254 | ret += td->o.file_size_low; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 255 | ret -= (ret % td->o.rw_min_bs); |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 256 | return ret; |
| 257 | } |
| 258 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 259 | static int file_size(struct thread_data *td, struct fio_file *f) |
| 260 | { |
| 261 | struct stat st; |
| 262 | |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 263 | if (stat(f->file_name, &st) == -1) { |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 264 | td_verror(td, errno, "fstat"); |
| 265 | return 1; |
| 266 | } |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 267 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 268 | f->real_file_size = st.st_size; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static int bdev_size(struct thread_data *td, struct fio_file *f) |
| 273 | { |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 274 | unsigned long long bytes = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 275 | int r; |
| 276 | |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 277 | if (td->io_ops->open_file(td, f)) { |
| 278 | log_err("fio: failed opening blockdev %s for size check\n", |
| 279 | f->file_name); |
| 280 | return 1; |
| 281 | } |
| 282 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 283 | r = blockdev_size(f, &bytes); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 284 | if (r) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 285 | td_verror(td, r, "blockdev_size"); |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 286 | goto err; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 287 | } |
| 288 | |
Jens Axboe | 7ab077a | 2009-02-02 15:07:37 +0100 | [diff] [blame] | 289 | if (!bytes) { |
| 290 | log_err("%s: zero sized block device?\n", f->file_name); |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 291 | goto err; |
Jens Axboe | 7ab077a | 2009-02-02 15:07:37 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 294 | f->real_file_size = bytes; |
Jens Axboe | 22a57ba | 2009-06-09 14:34:35 +0200 | [diff] [blame] | 295 | td->io_ops->close_file(td, f); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 296 | return 0; |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 297 | err: |
| 298 | td->io_ops->close_file(td, f); |
| 299 | return 1; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Jens Axboe | 4ccdccd | 2010-06-24 10:27:22 +0200 | [diff] [blame] | 302 | static int char_size(struct thread_data *td, struct fio_file *f) |
| 303 | { |
| 304 | #ifdef FIO_HAVE_CHARDEV_SIZE |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 305 | unsigned long long bytes = 0; |
Jens Axboe | 4ccdccd | 2010-06-24 10:27:22 +0200 | [diff] [blame] | 306 | int r; |
| 307 | |
| 308 | if (td->io_ops->open_file(td, f)) { |
| 309 | log_err("fio: failed opening blockdev %s for size check\n", |
| 310 | f->file_name); |
| 311 | return 1; |
| 312 | } |
| 313 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 314 | r = chardev_size(f, &bytes); |
Jens Axboe | 4ccdccd | 2010-06-24 10:27:22 +0200 | [diff] [blame] | 315 | if (r) { |
| 316 | td_verror(td, r, "chardev_size"); |
| 317 | goto err; |
| 318 | } |
| 319 | |
| 320 | if (!bytes) { |
| 321 | log_err("%s: zero sized char device?\n", f->file_name); |
| 322 | goto err; |
| 323 | } |
| 324 | |
| 325 | f->real_file_size = bytes; |
| 326 | td->io_ops->close_file(td, f); |
| 327 | return 0; |
| 328 | err: |
| 329 | td->io_ops->close_file(td, f); |
| 330 | return 1; |
| 331 | #else |
| 332 | f->real_file_size = -1ULL; |
| 333 | return 0; |
| 334 | #endif |
| 335 | } |
| 336 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 337 | static int get_file_size(struct thread_data *td, struct fio_file *f) |
| 338 | { |
| 339 | int ret = 0; |
| 340 | |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 341 | if (fio_file_size_known(f)) |
Jens Axboe | 409b341 | 2007-03-28 09:57:01 +0200 | [diff] [blame] | 342 | return 0; |
| 343 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 344 | if (f->filetype == FIO_TYPE_FILE) |
| 345 | ret = file_size(td, f); |
| 346 | else if (f->filetype == FIO_TYPE_BD) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 347 | ret = bdev_size(td, f); |
Jens Axboe | 4ccdccd | 2010-06-24 10:27:22 +0200 | [diff] [blame] | 348 | else if (f->filetype == FIO_TYPE_CHAR) |
| 349 | ret = char_size(td, f); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 350 | else |
| 351 | f->real_file_size = -1; |
| 352 | |
| 353 | if (ret) |
| 354 | return ret; |
| 355 | |
| 356 | if (f->file_offset > f->real_file_size) { |
Bruce Cran | 0f2152c | 2010-12-15 10:33:03 +0100 | [diff] [blame] | 357 | log_err("%s: offset extends end (%llu > %llu)\n", td->o.name, |
Jens Axboe | 4e0a8fa | 2013-04-15 11:40:57 +0200 | [diff] [blame] | 358 | (unsigned long long) f->file_offset, |
| 359 | (unsigned long long) f->real_file_size); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 360 | return 1; |
| 361 | } |
| 362 | |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 363 | fio_file_set_size_known(f); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 364 | return 0; |
| 365 | } |
| 366 | |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 367 | static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f, |
| 368 | unsigned long long off, |
| 369 | unsigned long long len) |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 370 | { |
| 371 | int ret = 0; |
| 372 | |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 373 | if (len == -1ULL) |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 374 | len = f->io_size; |
| 375 | if (off == -1ULL) |
| 376 | off = f->file_offset; |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 377 | |
Jens Axboe | 0d1cd20 | 2009-06-05 22:11:52 +0200 | [diff] [blame] | 378 | if (len == -1ULL || off == -1ULL) |
| 379 | return 0; |
| 380 | |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 381 | dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off, |
| 382 | len); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 383 | |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 384 | /* |
| 385 | * FIXME: add blockdev flushing too |
| 386 | */ |
Jens Axboe | a1c5807 | 2009-08-04 23:17:02 +0200 | [diff] [blame] | 387 | if (f->mmap_ptr) { |
Bruce Cran | 03e20d6 | 2011-01-02 20:14:54 +0100 | [diff] [blame] | 388 | ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED); |
Jens Axboe | a1c5807 | 2009-08-04 23:17:02 +0200 | [diff] [blame] | 389 | #ifdef FIO_MADV_FREE |
Bruce Cran | 03e20d6 | 2011-01-02 20:14:54 +0100 | [diff] [blame] | 390 | (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE); |
Jens Axboe | a1c5807 | 2009-08-04 23:17:02 +0200 | [diff] [blame] | 391 | #endif |
| 392 | } else if (f->filetype == FIO_TYPE_FILE) { |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 393 | ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED); |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 394 | } else if (f->filetype == FIO_TYPE_BD) { |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 395 | ret = blockdev_invalidate_cache(f); |
Jens Axboe | 7e0e25c | 2007-03-27 12:49:56 +0200 | [diff] [blame] | 396 | if (ret < 0 && errno == EACCES && geteuid()) { |
Jens Axboe | 7172cfe | 2007-07-23 14:36:00 +0200 | [diff] [blame] | 397 | if (!root_warn) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 398 | log_err("fio: only root may flush block " |
| 399 | "devices. Cache flush bypassed!\n"); |
Jens Axboe | 7172cfe | 2007-07-23 14:36:00 +0200 | [diff] [blame] | 400 | root_warn = 1; |
| 401 | } |
Jens Axboe | 7e0e25c | 2007-03-27 12:49:56 +0200 | [diff] [blame] | 402 | ret = 0; |
| 403 | } |
Jens Axboe | b5605e9 | 2007-04-16 20:42:33 +0200 | [diff] [blame] | 404 | } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE) |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 405 | ret = 0; |
| 406 | |
| 407 | if (ret < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 408 | td_verror(td, errno, "invalidate_cache"); |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 409 | return 1; |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 410 | } else if (ret > 0) { |
| 411 | td_verror(td, ret, "invalidate_cache"); |
| 412 | return 1; |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 413 | } |
| 414 | |
Jens Axboe | ad2da60 | 2007-02-26 12:33:27 +0100 | [diff] [blame] | 415 | return ret; |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 416 | |
| 417 | } |
| 418 | |
| 419 | int file_invalidate_cache(struct thread_data *td, struct fio_file *f) |
| 420 | { |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 421 | if (!fio_file_open(f)) |
Jens Axboe | a5fb461 | 2008-05-28 10:54:01 +0200 | [diff] [blame] | 422 | return 0; |
| 423 | |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 424 | return __file_invalidate_cache(td, f, -1ULL, -1ULL); |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 425 | } |
| 426 | |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 427 | int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 428 | { |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 429 | int ret = 0; |
| 430 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 431 | dprint(FD_FILE, "fd close %s\n", f->file_name); |
Jens Axboe | 4906e0b | 2008-03-01 18:59:51 +0100 | [diff] [blame] | 432 | |
| 433 | remove_file_hash(f); |
| 434 | |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 435 | if (close(f->fd) < 0) |
| 436 | ret = errno; |
| 437 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 438 | f->fd = -1; |
Jens Axboe | e6c4d73 | 2012-12-12 08:16:27 +0100 | [diff] [blame] | 439 | |
| 440 | if (f->shadow_fd != -1) { |
| 441 | close(f->shadow_fd); |
| 442 | f->shadow_fd = -1; |
| 443 | } |
| 444 | |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 445 | return ret; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 446 | } |
| 447 | |
Dmitry Monakhov | 1ccc6dc | 2012-09-19 23:22:53 +0400 | [diff] [blame] | 448 | int file_lookup_open(struct fio_file *f, int flags) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 449 | { |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 450 | struct fio_file *__f; |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 451 | int from_hash; |
| 452 | |
| 453 | __f = lookup_file_hash(f->file_name); |
| 454 | if (__f) { |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 455 | dprint(FD_FILE, "found file in hash %s\n", f->file_name); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 456 | /* |
| 457 | * racy, need the __f->lock locked |
| 458 | */ |
| 459 | f->lock = __f->lock; |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 460 | from_hash = 1; |
| 461 | } else { |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 462 | dprint(FD_FILE, "file not found in hash %s\n", f->file_name); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 463 | from_hash = 0; |
| 464 | } |
| 465 | |
Jens Axboe | e8670ef | 2008-03-06 12:06:30 +0100 | [diff] [blame] | 466 | f->fd = open(f->file_name, flags, 0600); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 467 | return from_hash; |
| 468 | } |
| 469 | |
Jens Axboe | e6c4d73 | 2012-12-12 08:16:27 +0100 | [diff] [blame] | 470 | static int file_close_shadow_fds(struct thread_data *td) |
| 471 | { |
| 472 | struct fio_file *f; |
| 473 | int num_closed = 0; |
| 474 | unsigned int i; |
| 475 | |
| 476 | for_each_file(td, f, i) { |
| 477 | if (f->shadow_fd == -1) |
| 478 | continue; |
| 479 | |
| 480 | close(f->shadow_fd); |
| 481 | f->shadow_fd = -1; |
| 482 | num_closed++; |
| 483 | } |
| 484 | |
| 485 | return num_closed; |
| 486 | } |
| 487 | |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 488 | int generic_open_file(struct thread_data *td, struct fio_file *f) |
| 489 | { |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 490 | int is_std = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 491 | int flags = 0; |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 492 | int from_hash = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 493 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 494 | dprint(FD_FILE, "fd open %s\n", f->file_name); |
| 495 | |
Shaohua Li | 6eaf09d | 2012-09-14 08:49:43 +0200 | [diff] [blame] | 496 | if (td_trim(td) && f->filetype != FIO_TYPE_BD) { |
| 497 | log_err("fio: trim only applies to block device\n"); |
| 498 | return 1; |
| 499 | } |
| 500 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 501 | if (!strcmp(f->file_name, "-")) { |
| 502 | if (td_rw(td)) { |
| 503 | log_err("fio: can't read/write to stdin/out\n"); |
| 504 | return 1; |
| 505 | } |
| 506 | is_std = 1; |
Jens Axboe | ce98fa6 | 2007-04-17 13:27:32 +0200 | [diff] [blame] | 507 | |
| 508 | /* |
| 509 | * move output logging to stderr, if we are writing to stdout |
| 510 | */ |
| 511 | if (td_write(td)) |
| 512 | f_out = stderr; |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 513 | } |
| 514 | |
Shaohua Li | 6eaf09d | 2012-09-14 08:49:43 +0200 | [diff] [blame] | 515 | if (td_trim(td)) |
| 516 | goto skip_flags; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 517 | if (td->o.odirect) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 518 | flags |= OS_O_DIRECT; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 519 | if (td->o.sync_io) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 520 | flags |= O_SYNC; |
Jens Axboe | 814452b | 2009-03-04 12:53:13 +0100 | [diff] [blame] | 521 | if (td->o.create_on_open) |
| 522 | flags |= O_CREAT; |
Shaohua Li | 6eaf09d | 2012-09-14 08:49:43 +0200 | [diff] [blame] | 523 | skip_flags: |
| 524 | if (f->filetype != FIO_TYPE_FILE) |
| 525 | flags |= FIO_O_NOATIME; |
Jens Axboe | 7abf833 | 2006-11-23 15:01:19 +0100 | [diff] [blame] | 526 | |
Aaron Carroll | 056f345 | 2007-11-26 09:08:53 +0100 | [diff] [blame] | 527 | open_again: |
Jens Axboe | 660a1cb | 2007-04-17 15:37:47 +0200 | [diff] [blame] | 528 | if (td_write(td)) { |
Jens Axboe | 1730815 | 2008-03-07 13:42:31 +0100 | [diff] [blame] | 529 | if (!read_only) |
| 530 | flags |= O_RDWR; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 531 | |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 532 | if (f->filetype == FIO_TYPE_FILE) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 533 | flags |= O_CREAT; |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 534 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 535 | if (is_std) |
| 536 | f->fd = dup(STDOUT_FILENO); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 537 | else |
| 538 | from_hash = file_lookup_open(f, flags); |
Shaohua Li | 6eaf09d | 2012-09-14 08:49:43 +0200 | [diff] [blame] | 539 | } else if (td_read(td)) { |
Jens Axboe | 4241ea8 | 2007-09-12 08:18:36 +0200 | [diff] [blame] | 540 | if (f->filetype == FIO_TYPE_CHAR && !read_only) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 541 | flags |= O_RDWR; |
| 542 | else |
| 543 | flags |= O_RDONLY; |
| 544 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 545 | if (is_std) |
| 546 | f->fd = dup(STDIN_FILENO); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 547 | else |
| 548 | from_hash = file_lookup_open(f, flags); |
Shaohua Li | 6eaf09d | 2012-09-14 08:49:43 +0200 | [diff] [blame] | 549 | } else { //td trim |
| 550 | flags |= O_RDWR; |
| 551 | from_hash = file_lookup_open(f, flags); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | if (f->fd == -1) { |
Jens Axboe | e4e3325 | 2007-03-21 13:07:54 +0100 | [diff] [blame] | 555 | char buf[FIO_VERROR_SIZE]; |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 556 | int __e = errno; |
| 557 | |
Jens Axboe | 835d9b9 | 2009-06-09 15:23:38 +0200 | [diff] [blame] | 558 | if (__e == EPERM && (flags & FIO_O_NOATIME)) { |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 559 | flags &= ~FIO_O_NOATIME; |
Aaron Carroll | 056f345 | 2007-11-26 09:08:53 +0100 | [diff] [blame] | 560 | goto open_again; |
| 561 | } |
Jens Axboe | e6c4d73 | 2012-12-12 08:16:27 +0100 | [diff] [blame] | 562 | if (__e == EMFILE && file_close_shadow_fds(td)) |
| 563 | goto open_again; |
Aaron Carroll | 056f345 | 2007-11-26 09:08:53 +0100 | [diff] [blame] | 564 | |
Ken Raeburn | 98ffb8f | 2013-01-30 22:31:09 +0100 | [diff] [blame] | 565 | snprintf(buf, sizeof(buf), "open(%s)", f->file_name); |
Jens Axboe | e4e3325 | 2007-03-21 13:07:54 +0100 | [diff] [blame] | 566 | |
Jens Axboe | a93c5f0 | 2012-06-11 08:53:53 +0200 | [diff] [blame] | 567 | if (__e == EINVAL && (flags & OS_O_DIRECT)) { |
| 568 | log_err("fio: looks like your file system does not " \ |
| 569 | "support direct=1/buffered=0\n"); |
| 570 | } |
| 571 | |
Jens Axboe | e4e3325 | 2007-03-21 13:07:54 +0100 | [diff] [blame] | 572 | td_verror(td, __e, buf); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 573 | } |
| 574 | |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 575 | if (!from_hash && f->fd != -1) { |
| 576 | if (add_file_hash(f)) { |
Jens Axboe | 3f0ca9b | 2011-05-24 21:36:24 +0200 | [diff] [blame] | 577 | int fio_unused ret; |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 578 | |
| 579 | /* |
Jens Axboe | e6c4d73 | 2012-12-12 08:16:27 +0100 | [diff] [blame] | 580 | * Stash away descriptor for later close. This is to |
| 581 | * work-around a "feature" on Linux, where a close of |
| 582 | * an fd that has been opened for write will trigger |
| 583 | * udev to call blkid to check partitions, fs id, etc. |
| 584 | * That polutes the device cache, which can slow down |
| 585 | * unbuffered accesses. |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 586 | */ |
Jens Axboe | e6c4d73 | 2012-12-12 08:16:27 +0100 | [diff] [blame] | 587 | if (f->shadow_fd == -1) |
| 588 | f->shadow_fd = f->fd; |
| 589 | else { |
| 590 | /* |
| 591 | * OK to ignore, we haven't done anything |
| 592 | * with it |
| 593 | */ |
| 594 | ret = generic_close_file(td, f); |
| 595 | } |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 596 | goto open_again; |
| 597 | } |
| 598 | } |
Jens Axboe | 4906e0b | 2008-03-01 18:59:51 +0100 | [diff] [blame] | 599 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 600 | return 0; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 601 | } |
| 602 | |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 603 | int generic_get_file_size(struct thread_data *td, struct fio_file *f) |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 604 | { |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 605 | return get_file_size(td, f); |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 606 | } |
| 607 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 608 | /* |
| 609 | * open/close all files, so that ->real_file_size gets set |
| 610 | */ |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 611 | static int get_file_sizes(struct thread_data *td) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 612 | { |
| 613 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 614 | unsigned int i; |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 615 | int err = 0; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 616 | |
| 617 | for_each_file(td, f, i) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 618 | dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i, |
| 619 | f->file_name); |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 620 | |
Jens Axboe | 99a47c6 | 2009-04-07 22:20:56 +0200 | [diff] [blame] | 621 | if (td_io_get_file_size(td, f)) { |
Jens Axboe | 40b44f4 | 2007-04-11 12:43:35 +0200 | [diff] [blame] | 622 | if (td->error != ENOENT) { |
| 623 | log_err("%s\n", td->verror); |
| 624 | err = 1; |
| 625 | } |
Jens Axboe | 541d66d | 2007-03-27 19:50:11 +0200 | [diff] [blame] | 626 | clear_error(td); |
Jens Axboe | 07eb79d | 2007-04-12 13:02:38 +0200 | [diff] [blame] | 627 | } |
Jens Axboe | 409b341 | 2007-03-28 09:57:01 +0200 | [diff] [blame] | 628 | |
| 629 | if (f->real_file_size == -1ULL && td->o.size) |
| 630 | f->real_file_size = td->o.size / td->o.nr_files; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 631 | } |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 632 | |
| 633 | return err; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 634 | } |
| 635 | |
Jens Axboe | 2e3bd4c | 2010-05-17 12:29:57 +0200 | [diff] [blame] | 636 | struct fio_mount { |
| 637 | struct flist_head list; |
| 638 | const char *base; |
| 639 | char __base[256]; |
| 640 | unsigned int key; |
| 641 | }; |
| 642 | |
| 643 | /* |
| 644 | * Get free number of bytes for each file on each unique mount. |
| 645 | */ |
| 646 | static unsigned long long get_fs_free_counts(struct thread_data *td) |
| 647 | { |
| 648 | struct flist_head *n, *tmp; |
Jens Axboe | 68b0316 | 2010-06-24 09:22:41 +0200 | [diff] [blame] | 649 | unsigned long long ret = 0; |
Jens Axboe | 2e3bd4c | 2010-05-17 12:29:57 +0200 | [diff] [blame] | 650 | struct fio_mount *fm; |
| 651 | FLIST_HEAD(list); |
| 652 | struct fio_file *f; |
| 653 | unsigned int i; |
| 654 | |
| 655 | for_each_file(td, f, i) { |
| 656 | struct stat sb; |
| 657 | char buf[256]; |
| 658 | |
Jens Axboe | 4ccdccd | 2010-06-24 10:27:22 +0200 | [diff] [blame] | 659 | if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) { |
| 660 | if (f->real_file_size != -1ULL) |
| 661 | ret += f->real_file_size; |
Jens Axboe | 68b0316 | 2010-06-24 09:22:41 +0200 | [diff] [blame] | 662 | continue; |
| 663 | } else if (f->filetype != FIO_TYPE_FILE) |
| 664 | continue; |
| 665 | |
Jens Axboe | 2e3bd4c | 2010-05-17 12:29:57 +0200 | [diff] [blame] | 666 | strcpy(buf, f->file_name); |
| 667 | |
| 668 | if (stat(buf, &sb) < 0) { |
| 669 | if (errno != ENOENT) |
| 670 | break; |
| 671 | strcpy(buf, "."); |
| 672 | if (stat(buf, &sb) < 0) |
| 673 | break; |
| 674 | } |
| 675 | |
| 676 | fm = NULL; |
| 677 | flist_for_each(n, &list) { |
| 678 | fm = flist_entry(n, struct fio_mount, list); |
| 679 | if (fm->key == sb.st_dev) |
| 680 | break; |
| 681 | |
| 682 | fm = NULL; |
| 683 | } |
| 684 | |
| 685 | if (fm) |
| 686 | continue; |
| 687 | |
| 688 | fm = malloc(sizeof(*fm)); |
| 689 | strcpy(fm->__base, buf); |
| 690 | fm->base = basename(fm->__base); |
| 691 | fm->key = sb.st_dev; |
| 692 | flist_add(&fm->list, &list); |
| 693 | } |
| 694 | |
Jens Axboe | 2e3bd4c | 2010-05-17 12:29:57 +0200 | [diff] [blame] | 695 | flist_for_each_safe(n, tmp, &list) { |
| 696 | unsigned long long sz; |
| 697 | |
| 698 | fm = flist_entry(n, struct fio_mount, list); |
| 699 | flist_del(&fm->list); |
| 700 | |
| 701 | sz = get_fs_size(fm->base); |
| 702 | if (sz && sz != -1ULL) |
| 703 | ret += sz; |
| 704 | |
| 705 | free(fm); |
| 706 | } |
| 707 | |
| 708 | return ret; |
| 709 | } |
| 710 | |
Jens Axboe | 293b8c1 | 2013-01-04 13:16:54 +0100 | [diff] [blame] | 711 | uint64_t get_start_offset(struct thread_data *td) |
Dan Ehrenberg | ce95d65 | 2012-08-16 08:58:21 +0200 | [diff] [blame] | 712 | { |
| 713 | return td->o.start_offset + |
| 714 | (td->thread_number - 1) * td->o.offset_increment; |
| 715 | } |
| 716 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 717 | /* |
| 718 | * Open the files and setup files sizes, creating files if necessary. |
| 719 | */ |
| 720 | int setup_files(struct thread_data *td) |
| 721 | { |
| 722 | unsigned long long total_size, extend_size; |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 723 | struct thread_options *o = &td->o; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 724 | struct fio_file *f; |
| 725 | unsigned int i; |
Jens Axboe | 000b080 | 2007-03-27 15:56:10 +0200 | [diff] [blame] | 726 | int err = 0, need_extend; |
Jens Axboe | e90391e | 2013-04-13 20:40:53 +0200 | [diff] [blame] | 727 | int old_state; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 728 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 729 | dprint(FD_FILE, "setup files\n"); |
| 730 | |
Jens Axboe | e90391e | 2013-04-13 20:40:53 +0200 | [diff] [blame] | 731 | old_state = td->runstate; |
| 732 | td_set_runstate(td, TD_SETTING_UP); |
| 733 | |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 734 | if (o->read_iolog_file) |
Jens Axboe | 25460cf | 2012-05-02 13:58:02 +0200 | [diff] [blame] | 735 | goto done; |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 736 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 737 | /* |
| 738 | * if ioengine defines a setup() method, it's responsible for |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 739 | * opening the files and setting f->real_file_size to indicate |
| 740 | * the valid range for that file. |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 741 | */ |
| 742 | if (td->io_ops->setup) |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 743 | err = td->io_ops->setup(td); |
| 744 | else |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 745 | err = get_file_sizes(td); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 746 | |
Jens Axboe | f102706 | 2006-10-20 10:14:01 +0200 | [diff] [blame] | 747 | if (err) |
Jens Axboe | e90391e | 2013-04-13 20:40:53 +0200 | [diff] [blame] | 748 | goto err_out; |
Jens Axboe | f102706 | 2006-10-20 10:14:01 +0200 | [diff] [blame] | 749 | |
Jens Axboe | 0a7eb12 | 2006-10-20 10:36:42 +0200 | [diff] [blame] | 750 | /* |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 751 | * check sizes. if the files/devices do not exist and the size |
| 752 | * isn't passed to fio, abort. |
Jens Axboe | 0a7eb12 | 2006-10-20 10:36:42 +0200 | [diff] [blame] | 753 | */ |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 754 | total_size = 0; |
| 755 | for_each_file(td, f, i) { |
| 756 | if (f->real_file_size == -1ULL) |
| 757 | total_size = -1ULL; |
| 758 | else |
| 759 | total_size += f->real_file_size; |
| 760 | } |
Jens Axboe | 0a7eb12 | 2006-10-20 10:36:42 +0200 | [diff] [blame] | 761 | |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 762 | if (o->fill_device) |
Jens Axboe | 2e3bd4c | 2010-05-17 12:29:57 +0200 | [diff] [blame] | 763 | td->fill_device_size = get_fs_free_counts(td); |
| 764 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 765 | /* |
| 766 | * device/file sizes are zero and no size given, punt |
| 767 | */ |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 768 | if ((!total_size || total_size == -1ULL) && !o->size && |
| 769 | !(td->io_ops->flags & FIO_NOIO) && !o->fill_device && |
| 770 | !(o->nr_files && (o->file_size_low || o->file_size_high))) { |
| 771 | log_err("%s: you need to specify size=\n", o->name); |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 772 | td_verror(td, EINVAL, "total_file_size"); |
Jens Axboe | e90391e | 2013-04-13 20:40:53 +0200 | [diff] [blame] | 773 | goto err_out; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 774 | } |
| 775 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 776 | /* |
| 777 | * now file sizes are known, so we can set ->io_size. if size= is |
| 778 | * not given, ->io_size is just equal to ->real_file_size. if size |
| 779 | * is given, ->io_size is size / nr_files. |
| 780 | */ |
| 781 | extend_size = total_size = 0; |
| 782 | need_extend = 0; |
| 783 | for_each_file(td, f, i) { |
Dan Ehrenberg | ce95d65 | 2012-08-16 08:58:21 +0200 | [diff] [blame] | 784 | f->file_offset = get_start_offset(td); |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 785 | |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 786 | if (!o->file_size_low) { |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 787 | /* |
| 788 | * no file size range given, file size is equal to |
| 789 | * total size divided by number of files. if that is |
| 790 | * zero, set it to the real file size. |
| 791 | */ |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 792 | f->io_size = o->size / o->nr_files; |
Jens Axboe | 65bdb10 | 2008-01-24 13:13:12 +0100 | [diff] [blame] | 793 | if (!f->io_size) |
Jens Axboe | 273f8c9 | 2008-01-25 14:02:15 +0100 | [diff] [blame] | 794 | f->io_size = f->real_file_size - f->file_offset; |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 795 | } else if (f->real_file_size < o->file_size_low || |
| 796 | f->real_file_size > o->file_size_high) { |
| 797 | if (f->file_offset > o->file_size_low) |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 798 | goto err_offset; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 799 | /* |
| 800 | * file size given. if it's fixed, use that. if it's a |
| 801 | * range, generate a random size in-between. |
| 802 | */ |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 803 | if (o->file_size_low == o->file_size_high) |
| 804 | f->io_size = o->file_size_low - f->file_offset; |
| 805 | else { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 806 | f->io_size = get_rand_file_size(td) |
| 807 | - f->file_offset; |
| 808 | } |
Jens Axboe | 65bdb10 | 2008-01-24 13:13:12 +0100 | [diff] [blame] | 809 | } else |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 810 | f->io_size = f->real_file_size - f->file_offset; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 811 | |
| 812 | if (f->io_size == -1ULL) |
| 813 | total_size = -1ULL; |
Shaohua Li | 4d00256 | 2012-09-21 08:32:32 +0200 | [diff] [blame] | 814 | else { |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 815 | if (o->size_percent) |
| 816 | f->io_size = (f->io_size * o->size_percent) / 100; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 817 | total_size += f->io_size; |
Shaohua Li | 4d00256 | 2012-09-21 08:32:32 +0200 | [diff] [blame] | 818 | } |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 819 | |
| 820 | if (f->filetype == FIO_TYPE_FILE && |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 821 | (f->io_size + f->file_offset) > f->real_file_size && |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 822 | !(td->io_ops->flags & FIO_DISKLESSIO)) { |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 823 | if (!o->create_on_open) { |
Jens Axboe | 814452b | 2009-03-04 12:53:13 +0100 | [diff] [blame] | 824 | need_extend++; |
| 825 | extend_size += (f->io_size + f->file_offset); |
| 826 | } else |
| 827 | f->real_file_size = f->io_size + f->file_offset; |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 828 | fio_file_set_extend(f); |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 829 | } |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 830 | } |
| 831 | |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 832 | if (!o->size || o->size > total_size) |
| 833 | o->size = total_size; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 834 | |
| 835 | /* |
| 836 | * See if we need to extend some files |
| 837 | */ |
| 838 | if (need_extend) { |
| 839 | temp_stall_ts = 1; |
Jens Axboe | f3afa57 | 2012-09-17 13:34:16 +0200 | [diff] [blame] | 840 | if (output_format == FIO_OUTPUT_NORMAL) |
Shaozhi Shawn Ye | a7ba8c5 | 2008-09-10 09:04:18 +0200 | [diff] [blame] | 841 | log_info("%s: Laying out IO file(s) (%u file(s) /" |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 842 | " %lluMB)\n", o->name, need_extend, |
Shaozhi Shawn Ye | a7ba8c5 | 2008-09-10 09:04:18 +0200 | [diff] [blame] | 843 | extend_size >> 20); |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 844 | |
| 845 | for_each_file(td, f, i) { |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 846 | unsigned long long old_len = -1ULL, extend_len = -1ULL; |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 847 | |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 848 | if (!fio_file_extend(f)) |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 849 | continue; |
| 850 | |
Jens Axboe | 409b341 | 2007-03-28 09:57:01 +0200 | [diff] [blame] | 851 | assert(f->filetype == FIO_TYPE_FILE); |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 852 | fio_file_clear_extend(f); |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 853 | if (!o->fill_device) { |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 854 | old_len = f->real_file_size; |
Jens Axboe | 0b9d69e | 2009-09-11 22:29:54 +0200 | [diff] [blame] | 855 | extend_len = f->io_size + f->file_offset - |
| 856 | old_len; |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 857 | } |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 858 | f->real_file_size = (f->io_size + f->file_offset); |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 859 | err = extend_file(td, f); |
| 860 | if (err) |
| 861 | break; |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 862 | |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 863 | err = __file_invalidate_cache(td, f, old_len, |
| 864 | extend_len); |
| 865 | close(f->fd); |
| 866 | f->fd = -1; |
| 867 | if (err) |
| 868 | break; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 869 | } |
| 870 | temp_stall_ts = 0; |
| 871 | } |
| 872 | |
| 873 | if (err) |
Jens Axboe | e90391e | 2013-04-13 20:40:53 +0200 | [diff] [blame] | 874 | goto err_out; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 875 | |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 876 | if (!o->zone_size) |
| 877 | o->zone_size = o->size; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 878 | |
Jens Axboe | ea966f8 | 2007-09-03 10:09:37 +0200 | [diff] [blame] | 879 | /* |
| 880 | * iolog already set the total io size, if we read back |
| 881 | * stored entries. |
| 882 | */ |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 883 | if (!o->read_iolog_file) |
| 884 | td->total_io_size = o->size * o->loops; |
Jens Axboe | 25460cf | 2012-05-02 13:58:02 +0200 | [diff] [blame] | 885 | |
| 886 | done: |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 887 | if (o->create_only) |
Jens Axboe | 25460cf | 2012-05-02 13:58:02 +0200 | [diff] [blame] | 888 | td->done = 1; |
| 889 | |
Jens Axboe | e90391e | 2013-04-13 20:40:53 +0200 | [diff] [blame] | 890 | td_set_runstate(td, old_state); |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 891 | return 0; |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 892 | err_offset: |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 893 | log_err("%s: you need to specify valid offset=\n", o->name); |
Jens Axboe | e90391e | 2013-04-13 20:40:53 +0200 | [diff] [blame] | 894 | err_out: |
| 895 | td_set_runstate(td, old_state); |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 896 | return 1; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 897 | } |
| 898 | |
Zhang, Yanmin | afad68f | 2009-05-20 11:30:55 +0200 | [diff] [blame] | 899 | int pre_read_files(struct thread_data *td) |
| 900 | { |
| 901 | struct fio_file *f; |
| 902 | unsigned int i; |
| 903 | |
| 904 | dprint(FD_FILE, "pre_read files\n"); |
| 905 | |
| 906 | for_each_file(td, f, i) { |
| 907 | pre_read_file(td, f); |
| 908 | } |
| 909 | |
| 910 | return 1; |
| 911 | } |
| 912 | |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 913 | static int __init_rand_distribution(struct thread_data *td, struct fio_file *f) |
| 914 | { |
Jens Axboe | 2316296 | 2012-11-07 19:47:47 +0100 | [diff] [blame] | 915 | unsigned int range_size, seed; |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 916 | unsigned long nranges; |
Jens Axboe | 21415db | 2013-01-04 13:09:29 +0100 | [diff] [blame] | 917 | uint64_t file_size; |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 918 | |
| 919 | range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]); |
Jens Axboe | 21415db | 2013-01-04 13:09:29 +0100 | [diff] [blame] | 920 | file_size = min(f->real_file_size, f->io_size); |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 921 | |
Jens Axboe | 21415db | 2013-01-04 13:09:29 +0100 | [diff] [blame] | 922 | nranges = (file_size + range_size - 1) / range_size; |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 923 | |
Jens Axboe | 2316296 | 2012-11-07 19:47:47 +0100 | [diff] [blame] | 924 | seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number; |
Jens Axboe | 8425687 | 2012-11-17 10:06:36 -0700 | [diff] [blame] | 925 | if (!td->o.rand_repeatable) |
| 926 | seed = td->rand_seeds[4]; |
| 927 | |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 928 | if (td->o.random_distribution == FIO_RAND_DIST_ZIPF) |
Jens Axboe | 888677a | 2013-04-10 22:19:46 +0200 | [diff] [blame] | 929 | zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed); |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 930 | else |
Jens Axboe | 888677a | 2013-04-10 22:19:46 +0200 | [diff] [blame] | 931 | pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed); |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 932 | |
| 933 | return 1; |
| 934 | } |
| 935 | |
| 936 | static int init_rand_distribution(struct thread_data *td) |
| 937 | { |
| 938 | struct fio_file *f; |
| 939 | unsigned int i; |
| 940 | int state; |
| 941 | |
| 942 | if (td->o.random_distribution == FIO_RAND_DIST_RANDOM) |
| 943 | return 0; |
| 944 | |
| 945 | state = td->runstate; |
| 946 | td_set_runstate(td, TD_SETTING_UP); |
| 947 | for_each_file(td, f, i) |
| 948 | __init_rand_distribution(td, f); |
| 949 | td_set_runstate(td, state); |
| 950 | |
| 951 | return 1; |
| 952 | } |
| 953 | |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 954 | int init_random_map(struct thread_data *td) |
| 955 | { |
Jens Axboe | 51ede0b | 2012-11-22 13:50:29 +0100 | [diff] [blame] | 956 | unsigned long long blocks; |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 957 | struct fio_file *f; |
| 958 | unsigned int i; |
| 959 | |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 960 | if (init_rand_distribution(td)) |
| 961 | return 0; |
Jens Axboe | 3831a84 | 2012-11-26 19:59:14 +0100 | [diff] [blame] | 962 | if (!td_random(td)) |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 963 | return 0; |
| 964 | |
| 965 | for_each_file(td, f, i) { |
Jens Axboe | 38f30c8 | 2013-01-11 14:03:05 +0100 | [diff] [blame] | 966 | uint64_t file_size = min(f->real_file_size, f->io_size); |
| 967 | |
Jens Axboe | 53737ae | 2013-02-21 15:18:17 +0100 | [diff] [blame] | 968 | blocks = file_size / (unsigned long long) td->o.rw_min_bs; |
| 969 | |
Jens Axboe | 8055e41 | 2012-11-26 08:43:47 +0100 | [diff] [blame] | 970 | if (td->o.random_generator == FIO_RAND_GEN_LFSR) { |
Jens Axboe | 82af46b | 2012-12-04 13:12:25 +0100 | [diff] [blame] | 971 | unsigned long seed; |
| 972 | |
| 973 | seed = td->rand_seeds[FIO_RAND_BLOCK_OFF]; |
| 974 | |
Alex Pyrgiotis | d474cbc | 2013-03-08 14:37:03 +0200 | [diff] [blame] | 975 | if (!lfsr_init(&f->lfsr, blocks, seed, seed & 0xF)) |
Jens Axboe | 8055e41 | 2012-11-26 08:43:47 +0100 | [diff] [blame] | 976 | continue; |
Jens Axboe | 3831a84 | 2012-11-26 19:59:14 +0100 | [diff] [blame] | 977 | } else if (!td->o.norandommap) { |
Jens Axboe | 7ebd796 | 2012-11-28 21:24:46 +0100 | [diff] [blame] | 978 | f->io_axmap = axmap_new(blocks); |
| 979 | if (f->io_axmap) |
Jens Axboe | 8055e41 | 2012-11-26 08:43:47 +0100 | [diff] [blame] | 980 | continue; |
Jens Axboe | 1cad712 | 2012-11-29 21:37:11 +0100 | [diff] [blame] | 981 | } else if (td->o.norandommap) |
| 982 | continue; |
Jens Axboe | ceadd59 | 2012-02-02 08:41:28 +0100 | [diff] [blame] | 983 | |
Jens Axboe | 2b386d2 | 2008-03-26 10:32:57 +0100 | [diff] [blame] | 984 | if (!td->o.softrandommap) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 985 | log_err("fio: failed allocating random map. If running" |
| 986 | " a large number of jobs, try the 'norandommap'" |
Jens Axboe | 2b386d2 | 2008-03-26 10:32:57 +0100 | [diff] [blame] | 987 | " option or set 'softrandommap'. Or give" |
| 988 | " a larger --alloc-size to fio.\n"); |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 989 | return 1; |
| 990 | } |
Jens Axboe | 303032a | 2008-03-26 10:11:10 +0100 | [diff] [blame] | 991 | |
| 992 | log_info("fio: file %s failed allocating random map. Running " |
| 993 | "job without.\n", f->file_name); |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | return 0; |
| 997 | } |
| 998 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 999 | void close_files(struct thread_data *td) |
| 1000 | { |
Jens Axboe | 0ab8db8 | 2006-10-18 17:16:23 +0200 | [diff] [blame] | 1001 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1002 | unsigned int i; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 1003 | |
Jens Axboe | 2be3eec | 2009-06-10 09:05:13 +0200 | [diff] [blame] | 1004 | for_each_file(td, f, i) { |
| 1005 | if (fio_file_open(f)) |
| 1006 | td_io_close_file(td, f); |
| 1007 | } |
Jens Axboe | 24ffd2c | 2008-03-01 15:47:08 +0100 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | void close_and_free_files(struct thread_data *td) |
| 1011 | { |
| 1012 | struct fio_file *f; |
| 1013 | unsigned int i; |
| 1014 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 1015 | dprint(FD_FILE, "close files\n"); |
| 1016 | |
Jens Axboe | 0ab8db8 | 2006-10-18 17:16:23 +0200 | [diff] [blame] | 1017 | for_each_file(td, f, i) { |
Jens Axboe | ffdfabd | 2008-03-26 09:18:14 +0100 | [diff] [blame] | 1018 | if (td->o.unlink && f->filetype == FIO_TYPE_FILE) { |
| 1019 | dprint(FD_FILE, "free unlink %s\n", f->file_name); |
Jens Axboe | 132ad46 | 2006-11-23 12:23:12 +0100 | [diff] [blame] | 1020 | unlink(f->file_name); |
Jens Axboe | ffdfabd | 2008-03-26 09:18:14 +0100 | [diff] [blame] | 1021 | } |
Jens Axboe | bdb4e2e | 2007-03-01 09:54:57 +0100 | [diff] [blame] | 1022 | |
Jens Axboe | 22a57ba | 2009-06-09 14:34:35 +0200 | [diff] [blame] | 1023 | if (fio_file_open(f)) |
| 1024 | td_io_close_file(td, f); |
| 1025 | |
Shaozhi Shawn Ye | b9fbcf2 | 2008-09-10 09:05:17 +0200 | [diff] [blame] | 1026 | remove_file_hash(f); |
Jens Axboe | b3dc7f0 | 2007-03-09 12:52:15 +0100 | [diff] [blame] | 1027 | |
Jens Axboe | f17c439 | 2008-03-01 18:40:46 +0100 | [diff] [blame] | 1028 | sfree(f->file_name); |
Jens Axboe | fa1da86 | 2007-03-23 15:20:54 +0100 | [diff] [blame] | 1029 | f->file_name = NULL; |
Jens Axboe | 7ebd796 | 2012-11-28 21:24:46 +0100 | [diff] [blame] | 1030 | axmap_free(f->io_axmap); |
| 1031 | f->io_axmap = NULL; |
Jens Axboe | 78d99e6 | 2008-03-01 18:41:50 +0100 | [diff] [blame] | 1032 | sfree(f); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 1033 | } |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 1034 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1035 | td->o.filename = NULL; |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 1036 | free(td->files); |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 1037 | free(td->file_locks); |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 1038 | td->files_index = 0; |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 1039 | td->files = NULL; |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 1040 | td->file_locks = NULL; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1041 | td->o.nr_files = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 1042 | } |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1043 | |
Jens Axboe | e3bab46 | 2007-03-13 11:22:09 +0100 | [diff] [blame] | 1044 | static void get_file_type(struct fio_file *f) |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1045 | { |
| 1046 | struct stat sb; |
| 1047 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 1048 | if (!strcmp(f->file_name, "-")) |
| 1049 | f->filetype = FIO_TYPE_PIPE; |
| 1050 | else |
| 1051 | f->filetype = FIO_TYPE_FILE; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1052 | |
Bruce Cran | 3892182 | 2012-02-22 17:55:16 +0000 | [diff] [blame] | 1053 | /* \\.\ is the device namespace in Windows, where every file is |
| 1054 | * a block device */ |
| 1055 | if (strncmp(f->file_name, "\\\\.\\", 4) == 0) |
| 1056 | f->filetype = FIO_TYPE_BD; |
| 1057 | |
Jens Axboe | b30d395 | 2010-02-06 23:36:26 +0100 | [diff] [blame] | 1058 | if (!stat(f->file_name, &sb)) { |
Bruce Cran | 3892182 | 2012-02-22 17:55:16 +0000 | [diff] [blame] | 1059 | if (S_ISBLK(sb.st_mode)) |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1060 | f->filetype = FIO_TYPE_BD; |
| 1061 | else if (S_ISCHR(sb.st_mode)) |
| 1062 | f->filetype = FIO_TYPE_CHAR; |
Jens Axboe | b5605e9 | 2007-04-16 20:42:33 +0200 | [diff] [blame] | 1063 | else if (S_ISFIFO(sb.st_mode)) |
| 1064 | f->filetype = FIO_TYPE_PIPE; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1065 | } |
| 1066 | } |
| 1067 | |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 1068 | int add_file(struct thread_data *td, const char *fname) |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1069 | { |
Jens Axboe | 7b4e4fe | 2007-03-13 12:51:40 +0100 | [diff] [blame] | 1070 | int cur_files = td->files_index; |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame] | 1071 | char file_name[PATH_MAX]; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1072 | struct fio_file *f; |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame] | 1073 | int len = 0; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1074 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 1075 | dprint(FD_FILE, "add file %s\n", fname); |
| 1076 | |
Jens Axboe | f17c439 | 2008-03-01 18:40:46 +0100 | [diff] [blame] | 1077 | f = smalloc(sizeof(*f)); |
Jens Axboe | c48c0be | 2008-07-31 19:55:02 +0200 | [diff] [blame] | 1078 | if (!f) { |
| 1079 | log_err("fio: smalloc OOM\n"); |
| 1080 | assert(0); |
| 1081 | } |
Jens Axboe | 0b9d69e | 2009-09-11 22:29:54 +0200 | [diff] [blame] | 1082 | |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1083 | f->fd = -1; |
Jens Axboe | e6c4d73 | 2012-12-12 08:16:27 +0100 | [diff] [blame] | 1084 | f->shadow_fd = -1; |
Jens Axboe | 33c4881 | 2013-01-21 09:46:06 -0700 | [diff] [blame] | 1085 | fio_file_reset(td, f); |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame] | 1086 | |
Jens Axboe | fc99bc0 | 2009-03-04 15:27:42 +0100 | [diff] [blame] | 1087 | if (td->files_size <= td->files_index) { |
Jianpeng Ma | 1983e32 | 2013-01-10 13:19:27 +0100 | [diff] [blame] | 1088 | unsigned int new_size = td->o.nr_files + 1; |
Jens Axboe | 126d65c | 2008-03-01 18:04:31 +0100 | [diff] [blame] | 1089 | |
Jens Axboe | fc99bc0 | 2009-03-04 15:27:42 +0100 | [diff] [blame] | 1090 | dprint(FD_FILE, "resize file array to %d files\n", new_size); |
| 1091 | |
| 1092 | td->files = realloc(td->files, new_size * sizeof(f)); |
Jianpeng Ma | d537c08 | 2013-01-11 08:52:55 +0100 | [diff] [blame] | 1093 | if (td->files == NULL) { |
| 1094 | log_err("fio: realloc OOM\n"); |
| 1095 | assert(0); |
| 1096 | } |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 1097 | if (td->o.file_lock_mode != FILE_LOCK_NONE) { |
| 1098 | td->file_locks = realloc(td->file_locks, new_size); |
| 1099 | if (!td->file_locks) { |
| 1100 | log_err("fio: realloc OOM\n"); |
| 1101 | assert(0); |
| 1102 | } |
| 1103 | td->file_locks[cur_files] = FILE_LOCK_NONE; |
| 1104 | } |
Jens Axboe | fc99bc0 | 2009-03-04 15:27:42 +0100 | [diff] [blame] | 1105 | td->files_size = new_size; |
| 1106 | } |
Jens Axboe | 8bb7679 | 2009-03-04 15:37:52 +0100 | [diff] [blame] | 1107 | td->files[cur_files] = f; |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 1108 | f->fileno = cur_files; |
Jens Axboe | 126d65c | 2008-03-01 18:04:31 +0100 | [diff] [blame] | 1109 | |
Jens Axboe | 07eb79d | 2007-04-12 13:02:38 +0200 | [diff] [blame] | 1110 | /* |
| 1111 | * init function, io engine may not be loaded yet |
| 1112 | */ |
| 1113 | if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO)) |
| 1114 | f->real_file_size = -1ULL; |
| 1115 | |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame] | 1116 | if (td->o.directory) |
| 1117 | len = sprintf(file_name, "%s/", td->o.directory); |
| 1118 | |
| 1119 | sprintf(file_name + len, "%s", fname); |
Jens Axboe | f17c439 | 2008-03-01 18:40:46 +0100 | [diff] [blame] | 1120 | f->file_name = smalloc_strdup(file_name); |
Jens Axboe | c48c0be | 2008-07-31 19:55:02 +0200 | [diff] [blame] | 1121 | if (!f->file_name) { |
| 1122 | log_err("fio: smalloc OOM\n"); |
| 1123 | assert(0); |
| 1124 | } |
Jens Axboe | 0b9d69e | 2009-09-11 22:29:54 +0200 | [diff] [blame] | 1125 | |
Jens Axboe | e3bab46 | 2007-03-13 11:22:09 +0100 | [diff] [blame] | 1126 | get_file_type(f); |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1127 | |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1128 | switch (td->o.file_lock_mode) { |
| 1129 | case FILE_LOCK_NONE: |
| 1130 | break; |
| 1131 | case FILE_LOCK_READWRITE: |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 1132 | f->rwlock = fio_rwlock_init(); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1133 | break; |
| 1134 | case FILE_LOCK_EXCLUSIVE: |
Jens Axboe | 521da52 | 2012-08-02 11:21:36 +0200 | [diff] [blame] | 1135 | f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1136 | break; |
| 1137 | default: |
| 1138 | log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode); |
| 1139 | assert(0); |
| 1140 | } |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 1141 | |
Jens Axboe | 7b4e4fe | 2007-03-13 12:51:40 +0100 | [diff] [blame] | 1142 | td->files_index++; |
Jens Axboe | 1549441 | 2007-03-13 11:52:47 +0100 | [diff] [blame] | 1143 | if (f->filetype == FIO_TYPE_FILE) |
| 1144 | td->nr_normal_files++; |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 1145 | |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1146 | dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name, |
| 1147 | cur_files); |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 1148 | |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 1149 | return cur_files; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1150 | } |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1151 | |
Jens Axboe | 49ffb4a | 2010-08-24 15:01:37 +0200 | [diff] [blame] | 1152 | int add_file_exclusive(struct thread_data *td, const char *fname) |
| 1153 | { |
| 1154 | struct fio_file *f; |
| 1155 | unsigned int i; |
| 1156 | |
| 1157 | for_each_file(td, f, i) { |
| 1158 | if (!strcmp(f->file_name, fname)) |
| 1159 | return i; |
| 1160 | } |
| 1161 | |
| 1162 | return add_file(td, fname); |
| 1163 | } |
| 1164 | |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1165 | void get_file(struct fio_file *f) |
| 1166 | { |
Jens Axboe | 8172fe9 | 2008-02-01 21:51:02 +0100 | [diff] [blame] | 1167 | dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references); |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 1168 | assert(fio_file_open(f)); |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1169 | f->references++; |
| 1170 | } |
| 1171 | |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 1172 | int put_file(struct thread_data *td, struct fio_file *f) |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1173 | { |
Jens Axboe | 98e1ac4 | 2008-03-06 11:56:48 +0100 | [diff] [blame] | 1174 | int f_ret = 0, ret = 0; |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 1175 | |
Jens Axboe | 8172fe9 | 2008-02-01 21:51:02 +0100 | [diff] [blame] | 1176 | dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references); |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 1177 | |
Jens Axboe | 22a57ba | 2009-06-09 14:34:35 +0200 | [diff] [blame] | 1178 | if (!fio_file_open(f)) { |
| 1179 | assert(f->fd == -1); |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 1180 | return 0; |
Jens Axboe | 22a57ba | 2009-06-09 14:34:35 +0200 | [diff] [blame] | 1181 | } |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1182 | |
| 1183 | assert(f->references); |
| 1184 | if (--f->references) |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 1185 | return 0; |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1186 | |
Jens Axboe | d424d4d | 2007-04-17 09:05:10 +0200 | [diff] [blame] | 1187 | if (should_fsync(td) && td->o.fsync_on_close) |
Jens Axboe | 98e1ac4 | 2008-03-06 11:56:48 +0100 | [diff] [blame] | 1188 | f_ret = fsync(f->fd); |
Jens Axboe | ebb1415 | 2007-03-13 14:42:15 +0100 | [diff] [blame] | 1189 | |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1190 | if (td->io_ops->close_file) |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 1191 | ret = td->io_ops->close_file(td, f); |
Jens Axboe | 1020a13 | 2007-03-29 11:15:06 +0200 | [diff] [blame] | 1192 | |
Jens Axboe | 98e1ac4 | 2008-03-06 11:56:48 +0100 | [diff] [blame] | 1193 | if (!ret) |
Jens Axboe | a5fb461 | 2008-05-28 10:54:01 +0200 | [diff] [blame] | 1194 | ret = f_ret; |
Jens Axboe | 98e1ac4 | 2008-03-06 11:56:48 +0100 | [diff] [blame] | 1195 | |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1196 | td->nr_open_files--; |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 1197 | fio_file_clear_open(f); |
Jens Axboe | 22a57ba | 2009-06-09 14:34:35 +0200 | [diff] [blame] | 1198 | assert(f->fd == -1); |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 1199 | return ret; |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1200 | } |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1201 | |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1202 | void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir) |
Jens Axboe | b2bd2bd | 2008-03-01 18:19:52 +0100 | [diff] [blame] | 1203 | { |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1204 | if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE) |
| 1205 | return; |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 1206 | |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1207 | if (td->o.file_lock_mode == FILE_LOCK_READWRITE) { |
| 1208 | if (ddir == DDIR_READ) |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 1209 | fio_rwlock_read(f->rwlock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1210 | else |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 1211 | fio_rwlock_write(f->rwlock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1212 | } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) |
| 1213 | fio_mutex_down(f->lock); |
| 1214 | |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 1215 | td->file_locks[f->fileno] = td->o.file_lock_mode; |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | void unlock_file(struct thread_data *td, struct fio_file *f) |
| 1219 | { |
| 1220 | if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE) |
| 1221 | return; |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1222 | |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 1223 | if (td->o.file_lock_mode == FILE_LOCK_READWRITE) |
| 1224 | fio_rwlock_unlock(f->rwlock); |
| 1225 | else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1226 | fio_mutex_up(f->lock); |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 1227 | |
| 1228 | td->file_locks[f->fileno] = FILE_LOCK_NONE; |
Jens Axboe | b2bd2bd | 2008-03-01 18:19:52 +0100 | [diff] [blame] | 1229 | } |
| 1230 | |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1231 | void unlock_file_all(struct thread_data *td, struct fio_file *f) |
Jens Axboe | b2bd2bd | 2008-03-01 18:19:52 +0100 | [diff] [blame] | 1232 | { |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 1233 | if (td->file_locks[f->fileno] != FILE_LOCK_NONE) |
| 1234 | unlock_file(td, f); |
Jens Axboe | b2bd2bd | 2008-03-01 18:19:52 +0100 | [diff] [blame] | 1235 | } |
| 1236 | |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1237 | static int recurse_dir(struct thread_data *td, const char *dirname) |
| 1238 | { |
| 1239 | struct dirent *dir; |
| 1240 | int ret = 0; |
| 1241 | DIR *D; |
| 1242 | |
| 1243 | D = opendir(dirname); |
| 1244 | if (!D) { |
Jens Axboe | 0ddb270 | 2007-03-27 19:43:53 +0200 | [diff] [blame] | 1245 | char buf[FIO_VERROR_SIZE]; |
| 1246 | |
Ken Raeburn | 98ffb8f | 2013-01-30 22:31:09 +0100 | [diff] [blame] | 1247 | snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname); |
Jens Axboe | 0ddb270 | 2007-03-27 19:43:53 +0200 | [diff] [blame] | 1248 | td_verror(td, errno, buf); |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1249 | return 1; |
| 1250 | } |
| 1251 | |
| 1252 | while ((dir = readdir(D)) != NULL) { |
| 1253 | char full_path[PATH_MAX]; |
| 1254 | struct stat sb; |
| 1255 | |
Jens Axboe | e85b2b8 | 2007-03-14 09:39:06 +0100 | [diff] [blame] | 1256 | if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) |
| 1257 | continue; |
Jens Axboe | 96d32d5 | 2007-03-14 09:16:09 +0100 | [diff] [blame] | 1258 | |
Bruce Cran | b9fd788 | 2012-02-20 17:01:46 +0000 | [diff] [blame] | 1259 | sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name); |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1260 | |
| 1261 | if (lstat(full_path, &sb) == -1) { |
| 1262 | if (errno != ENOENT) { |
| 1263 | td_verror(td, errno, "stat"); |
| 1264 | return 1; |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | if (S_ISREG(sb.st_mode)) { |
| 1269 | add_file(td, full_path); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1270 | td->o.nr_files++; |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1271 | continue; |
| 1272 | } |
Jens Axboe | 0ddb270 | 2007-03-27 19:43:53 +0200 | [diff] [blame] | 1273 | if (!S_ISDIR(sb.st_mode)) |
| 1274 | continue; |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1275 | |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1276 | ret = recurse_dir(td, full_path); |
| 1277 | if (ret) |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1278 | break; |
| 1279 | } |
| 1280 | |
| 1281 | closedir(D); |
| 1282 | return ret; |
| 1283 | } |
| 1284 | |
| 1285 | int add_dir_files(struct thread_data *td, const char *path) |
| 1286 | { |
Jens Axboe | 0ddb270 | 2007-03-27 19:43:53 +0200 | [diff] [blame] | 1287 | int ret = recurse_dir(td, path); |
| 1288 | |
| 1289 | if (!ret) |
| 1290 | log_info("fio: opendir added %d files\n", td->o.nr_files); |
| 1291 | |
| 1292 | return ret; |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1293 | } |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 1294 | |
| 1295 | void dup_files(struct thread_data *td, struct thread_data *org) |
| 1296 | { |
| 1297 | struct fio_file *f; |
| 1298 | unsigned int i; |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 1299 | |
| 1300 | dprint(FD_FILE, "dup files: %d\n", org->files_index); |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 1301 | |
| 1302 | if (!org->files) |
| 1303 | return; |
| 1304 | |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 1305 | td->files = malloc(org->files_index * sizeof(f)); |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 1306 | |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 1307 | if (td->o.file_lock_mode != FILE_LOCK_NONE) |
| 1308 | td->file_locks = malloc(org->files_index); |
| 1309 | |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 1310 | for_each_file(org, f, i) { |
Jens Axboe | b0fe421 | 2008-03-01 18:22:27 +0100 | [diff] [blame] | 1311 | struct fio_file *__f; |
| 1312 | |
Jens Axboe | f17c439 | 2008-03-01 18:40:46 +0100 | [diff] [blame] | 1313 | __f = smalloc(sizeof(*__f)); |
Jens Axboe | c48c0be | 2008-07-31 19:55:02 +0200 | [diff] [blame] | 1314 | if (!__f) { |
| 1315 | log_err("fio: smalloc OOM\n"); |
| 1316 | assert(0); |
| 1317 | } |
Jens Axboe | 22a57ba | 2009-06-09 14:34:35 +0200 | [diff] [blame] | 1318 | __f->fd = -1; |
Jens Axboe | 33c4881 | 2013-01-21 09:46:06 -0700 | [diff] [blame] | 1319 | fio_file_reset(td, __f); |
Jens Axboe | 0b9d69e | 2009-09-11 22:29:54 +0200 | [diff] [blame] | 1320 | |
Aaron Carroll | bc3456f | 2008-06-25 09:20:37 +0200 | [diff] [blame] | 1321 | if (f->file_name) { |
Jens Axboe | f17c439 | 2008-03-01 18:40:46 +0100 | [diff] [blame] | 1322 | __f->file_name = smalloc_strdup(f->file_name); |
Jens Axboe | c48c0be | 2008-07-31 19:55:02 +0200 | [diff] [blame] | 1323 | if (!__f->file_name) { |
| 1324 | log_err("fio: smalloc OOM\n"); |
| 1325 | assert(0); |
| 1326 | } |
Jens Axboe | 0b9d69e | 2009-09-11 22:29:54 +0200 | [diff] [blame] | 1327 | |
Aaron Carroll | bc3456f | 2008-06-25 09:20:37 +0200 | [diff] [blame] | 1328 | __f->filetype = f->filetype; |
| 1329 | } |
Jens Axboe | b0fe421 | 2008-03-01 18:22:27 +0100 | [diff] [blame] | 1330 | |
| 1331 | td->files[i] = __f; |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 1332 | } |
| 1333 | } |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 1334 | |
| 1335 | /* |
| 1336 | * Returns the index that matches the filename, or -1 if not there |
| 1337 | */ |
| 1338 | int get_fileno(struct thread_data *td, const char *fname) |
| 1339 | { |
| 1340 | struct fio_file *f; |
| 1341 | unsigned int i; |
| 1342 | |
| 1343 | for_each_file(td, f, i) |
| 1344 | if (!strcmp(f->file_name, fname)) |
| 1345 | return i; |
| 1346 | |
| 1347 | return -1; |
| 1348 | } |
| 1349 | |
| 1350 | /* |
| 1351 | * For log usage, where we add/open/close files automatically |
| 1352 | */ |
| 1353 | void free_release_files(struct thread_data *td) |
| 1354 | { |
| 1355 | close_files(td); |
| 1356 | td->files_index = 0; |
| 1357 | td->nr_normal_files = 0; |
| 1358 | } |
Jens Axboe | 33c4881 | 2013-01-21 09:46:06 -0700 | [diff] [blame] | 1359 | |
| 1360 | void fio_file_reset(struct thread_data *td, struct fio_file *f) |
| 1361 | { |
| 1362 | f->last_pos = f->file_offset; |
| 1363 | f->last_start = -1ULL; |
| 1364 | if (f->io_axmap) |
| 1365 | axmap_reset(f->io_axmap); |
| 1366 | if (td->o.random_generator == FIO_RAND_GEN_LFSR) |
| 1367 | lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]); |
| 1368 | } |