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> |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 6 | #include <sys/stat.h> |
| 7 | #include <sys/mman.h> |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 8 | #include <sys/types.h> |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 9 | |
| 10 | #include "fio.h" |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 11 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 12 | static int extend_file(struct thread_data *td, struct fio_file *f) |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 13 | { |
Jens Axboe | ea44365 | 2007-03-29 14:52:13 +0200 | [diff] [blame] | 14 | int r, new_layout = 0, unlink_file = 0, flags; |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 15 | unsigned long long left; |
| 16 | unsigned int bs; |
| 17 | char *b; |
Jens Axboe | b2a1519 | 2006-10-18 14:10:42 +0200 | [diff] [blame] | 18 | |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 19 | /* |
| 20 | * check if we need to lay the file out complete again. fio |
| 21 | * does that for operations involving reads, or for writes |
| 22 | * where overwrite is set |
| 23 | */ |
| 24 | if (td_read(td) || (td_write(td) && td->o.overwrite)) |
| 25 | new_layout = 1; |
Jens Axboe | ea44365 | 2007-03-29 14:52:13 +0200 | [diff] [blame] | 26 | if (td_write(td) && !td->o.overwrite) |
| 27 | unlink_file = 1; |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 28 | |
Jens Axboe | ea44365 | 2007-03-29 14:52:13 +0200 | [diff] [blame] | 29 | if ((unlink_file || new_layout) && (f->flags & FIO_FILE_EXISTS)) { |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 30 | if (unlink(f->file_name) < 0) { |
| 31 | td_verror(td, errno, "unlink"); |
| 32 | return 1; |
| 33 | } |
| 34 | } |
| 35 | |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 36 | flags = O_WRONLY | O_CREAT; |
| 37 | if (new_layout) |
| 38 | flags |= O_TRUNC; |
| 39 | |
| 40 | f->fd = open(f->file_name, flags, 0644); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 41 | if (f->fd < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 42 | td_verror(td, errno, "open"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 43 | return 1; |
| 44 | } |
| 45 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 46 | if (ftruncate(f->fd, f->real_file_size) == -1) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 47 | td_verror(td, errno, "ftruncate"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 48 | goto err; |
| 49 | } |
| 50 | |
Jens Axboe | e8be2ad | 2007-03-29 10:31:51 +0200 | [diff] [blame] | 51 | if (!new_layout) |
| 52 | goto done; |
| 53 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 54 | if (posix_fallocate(f->fd, 0, f->real_file_size) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 55 | td_verror(td, errno, "posix_fallocate"); |
Jens Axboe | 40f8298 | 2006-10-25 11:21:05 +0200 | [diff] [blame] | 56 | goto err; |
| 57 | } |
| 58 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 59 | b = malloc(td->o.max_bs[DDIR_WRITE]); |
| 60 | memset(b, 0, td->o.max_bs[DDIR_WRITE]); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 61 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 62 | left = f->real_file_size; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 63 | while (left && !td->terminate) { |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 64 | bs = td->o.max_bs[DDIR_WRITE]; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 65 | if (bs > left) |
| 66 | bs = left; |
| 67 | |
| 68 | r = write(f->fd, b, bs); |
| 69 | |
| 70 | if (r == (int) bs) { |
| 71 | left -= bs; |
| 72 | continue; |
| 73 | } else { |
| 74 | if (r < 0) |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 75 | td_verror(td, errno, "write"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 76 | else |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 77 | td_verror(td, EIO, "write"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 78 | |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if (td->terminate) |
| 84 | unlink(f->file_name); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 85 | else if (td->o.create_fsync) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 86 | fsync(f->fd); |
| 87 | |
| 88 | free(b); |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 89 | done: |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 90 | close(f->fd); |
| 91 | f->fd = -1; |
| 92 | return 0; |
| 93 | err: |
| 94 | close(f->fd); |
| 95 | f->fd = -1; |
| 96 | return 1; |
| 97 | } |
| 98 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 99 | static unsigned long long get_rand_file_size(struct thread_data *td) |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 100 | { |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 101 | unsigned long long ret; |
| 102 | long r; |
| 103 | |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 104 | r = os_random_long(&td->file_size_state); |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 105 | ret = td->o.file_size_low + (unsigned long long) ((double) td->o.file_size_high * (r / (RAND_MAX + 1.0))); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 106 | ret -= (ret % td->o.rw_min_bs); |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 107 | return ret; |
| 108 | } |
| 109 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 110 | static int file_size(struct thread_data *td, struct fio_file *f) |
| 111 | { |
| 112 | struct stat st; |
| 113 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 114 | if (fstat(f->fd, &st) == -1) { |
| 115 | td_verror(td, errno, "fstat"); |
| 116 | return 1; |
| 117 | } |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 118 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 119 | f->real_file_size = st.st_size; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static int bdev_size(struct thread_data *td, struct fio_file *f) |
| 124 | { |
| 125 | unsigned long long bytes; |
| 126 | int r; |
| 127 | |
| 128 | r = blockdev_size(f->fd, &bytes); |
| 129 | if (r) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 130 | td_verror(td, r, "blockdev_size"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 131 | return 1; |
| 132 | } |
| 133 | |
| 134 | f->real_file_size = bytes; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static int get_file_size(struct thread_data *td, struct fio_file *f) |
| 139 | { |
| 140 | int ret = 0; |
| 141 | |
Jens Axboe | 409b341 | 2007-03-28 09:57:01 +0200 | [diff] [blame] | 142 | if (f->flags & FIO_SIZE_KNOWN) |
| 143 | return 0; |
| 144 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 145 | if (f->filetype == FIO_TYPE_FILE) |
| 146 | ret = file_size(td, f); |
| 147 | else if (f->filetype == FIO_TYPE_BD) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 148 | ret = bdev_size(td, f); |
| 149 | else |
| 150 | f->real_file_size = -1; |
| 151 | |
| 152 | if (ret) |
| 153 | return ret; |
| 154 | |
| 155 | if (f->file_offset > f->real_file_size) { |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 156 | log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name, f->file_offset, f->real_file_size); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 157 | return 1; |
| 158 | } |
| 159 | |
Jens Axboe | 409b341 | 2007-03-28 09:57:01 +0200 | [diff] [blame] | 160 | f->flags |= FIO_SIZE_KNOWN; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 164 | int file_invalidate_cache(struct thread_data *td, struct fio_file *f) |
| 165 | { |
| 166 | int ret = 0; |
| 167 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 168 | if (td->o.odirect) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 169 | return 0; |
| 170 | |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 171 | /* |
| 172 | * FIXME: add blockdev flushing too |
| 173 | */ |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 174 | if (f->mmap) |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 175 | ret = madvise(f->mmap, f->io_size, MADV_DONTNEED); |
Jens Axboe | 467d1b6 | 2007-03-15 14:11:10 +0100 | [diff] [blame] | 176 | else if (f->filetype == FIO_TYPE_FILE) |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 177 | ret = fadvise(f->fd, f->file_offset, f->io_size, POSIX_FADV_DONTNEED); |
Jens Axboe | 7e0e25c | 2007-03-27 12:49:56 +0200 | [diff] [blame] | 178 | else if (f->filetype == FIO_TYPE_BD) { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 179 | ret = blockdev_invalidate_cache(f->fd); |
Jens Axboe | 7e0e25c | 2007-03-27 12:49:56 +0200 | [diff] [blame] | 180 | if (ret < 0 && errno == EACCES && geteuid()) { |
| 181 | log_err("fio: only root may flush block devices. Cache flush bypassed!\n"); |
| 182 | ret = 0; |
| 183 | } |
| 184 | } else if (f->filetype == FIO_TYPE_CHAR) |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 185 | ret = 0; |
| 186 | |
| 187 | if (ret < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 188 | td_verror(td, errno, "invalidate_cache"); |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 189 | return 1; |
| 190 | } |
| 191 | |
Jens Axboe | ad2da60 | 2007-02-26 12:33:27 +0100 | [diff] [blame] | 192 | return ret; |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 195 | void 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] | 196 | { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 197 | close(f->fd); |
| 198 | f->fd = -1; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 199 | } |
| 200 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 201 | int generic_open_file(struct thread_data *td, struct fio_file *f) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 202 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 203 | int flags = 0; |
| 204 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 205 | if (td->o.odirect) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 206 | flags |= OS_O_DIRECT; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 207 | if (td->o.sync_io) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 208 | flags |= O_SYNC; |
Jens Axboe | 7abf833 | 2006-11-23 15:01:19 +0100 | [diff] [blame] | 209 | |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 210 | if (td_write(td) || td_rw(td)) { |
| 211 | flags |= O_RDWR; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 212 | |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 213 | if (f->filetype == FIO_TYPE_FILE) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 214 | flags |= O_CREAT; |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 215 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 216 | f->fd = open(f->file_name, flags, 0600); |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 217 | } else { |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 218 | if (f->filetype == FIO_TYPE_CHAR) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 219 | flags |= O_RDWR; |
| 220 | else |
| 221 | flags |= O_RDONLY; |
| 222 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 223 | f->fd = open(f->file_name, flags); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | if (f->fd == -1) { |
Jens Axboe | e4e3325 | 2007-03-21 13:07:54 +0100 | [diff] [blame] | 227 | char buf[FIO_VERROR_SIZE]; |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 228 | int __e = errno; |
| 229 | |
Jens Axboe | e4e3325 | 2007-03-21 13:07:54 +0100 | [diff] [blame] | 230 | snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name); |
| 231 | |
| 232 | td_verror(td, __e, buf); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 233 | } |
| 234 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 235 | if (get_file_size(td, f)) |
| 236 | goto err; |
| 237 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 238 | return 0; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 239 | err: |
| 240 | close(f->fd); |
| 241 | return 1; |
| 242 | } |
| 243 | |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 244 | int open_files(struct thread_data *td) |
| 245 | { |
| 246 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 247 | unsigned int i; |
| 248 | int err = 0; |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 249 | |
| 250 | for_each_file(td, f, i) { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 251 | err = td_io_open_file(td, f); |
Jens Axboe | 9bf27b4 | 2007-03-27 19:49:31 +0200 | [diff] [blame] | 252 | if (err) { |
| 253 | if (td->error == EMFILE) { |
| 254 | log_err("fio: limited open files to: %d\n", td->nr_open_files); |
| 255 | td->o.open_files = td->nr_open_files; |
| 256 | err = 0; |
| 257 | clear_error(td); |
| 258 | } |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 259 | break; |
Jens Axboe | 9bf27b4 | 2007-03-27 19:49:31 +0200 | [diff] [blame] | 260 | } |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 261 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 262 | if (td->o.open_files == td->nr_open_files) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 263 | break; |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 264 | } |
| 265 | |
Jens Axboe | 7abf833 | 2006-11-23 15:01:19 +0100 | [diff] [blame] | 266 | if (!err) |
| 267 | return 0; |
| 268 | |
Jens Axboe | bdb4e2e | 2007-03-01 09:54:57 +0100 | [diff] [blame] | 269 | for_each_file(td, f, i) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 270 | td_io_close_file(td, f); |
Jens Axboe | 7abf833 | 2006-11-23 15:01:19 +0100 | [diff] [blame] | 271 | |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 272 | return err; |
| 273 | } |
| 274 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 275 | /* |
| 276 | * open/close all files, so that ->real_file_size gets set |
| 277 | */ |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 278 | static int get_file_sizes(struct thread_data *td) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 279 | { |
| 280 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 281 | unsigned int i; |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 282 | int err = 0; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 283 | |
| 284 | for_each_file(td, f, i) { |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 285 | if (td->io_ops->open_file(td, f)) { |
| 286 | log_err("%s\n", td->verror); |
| 287 | err = 1; |
Jens Axboe | 541d66d | 2007-03-27 19:50:11 +0200 | [diff] [blame] | 288 | clear_error(td); |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 289 | } else |
Jens Axboe | 000b080 | 2007-03-27 15:56:10 +0200 | [diff] [blame] | 290 | td->io_ops->close_file(td, f); |
Jens Axboe | 409b341 | 2007-03-28 09:57:01 +0200 | [diff] [blame] | 291 | |
| 292 | if (f->real_file_size == -1ULL && td->o.size) |
| 293 | f->real_file_size = td->o.size / td->o.nr_files; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 294 | } |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 295 | |
| 296 | return err; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Open the files and setup files sizes, creating files if necessary. |
| 301 | */ |
| 302 | int setup_files(struct thread_data *td) |
| 303 | { |
| 304 | unsigned long long total_size, extend_size; |
| 305 | struct fio_file *f; |
| 306 | unsigned int i; |
Jens Axboe | 000b080 | 2007-03-27 15:56:10 +0200 | [diff] [blame] | 307 | int err = 0, need_extend; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 308 | |
| 309 | /* |
| 310 | * if ioengine defines a setup() method, it's responsible for |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 311 | * opening the files and setting f->real_file_size to indicate |
| 312 | * the valid range for that file. |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 313 | */ |
| 314 | if (td->io_ops->setup) |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 315 | err = td->io_ops->setup(td); |
| 316 | else |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 317 | err = get_file_sizes(td); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 318 | |
Jens Axboe | f102706 | 2006-10-20 10:14:01 +0200 | [diff] [blame] | 319 | if (err) |
| 320 | return err; |
| 321 | |
Jens Axboe | 0a7eb12 | 2006-10-20 10:36:42 +0200 | [diff] [blame] | 322 | /* |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 323 | * check sizes. if the files/devices do not exist and the size |
| 324 | * isn't passed to fio, abort. |
Jens Axboe | 0a7eb12 | 2006-10-20 10:36:42 +0200 | [diff] [blame] | 325 | */ |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 326 | total_size = 0; |
| 327 | for_each_file(td, f, i) { |
| 328 | if (f->real_file_size == -1ULL) |
| 329 | total_size = -1ULL; |
| 330 | else |
| 331 | total_size += f->real_file_size; |
| 332 | } |
Jens Axboe | 0a7eb12 | 2006-10-20 10:36:42 +0200 | [diff] [blame] | 333 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 334 | /* |
| 335 | * device/file sizes are zero and no size given, punt |
| 336 | */ |
Jens Axboe | 409b341 | 2007-03-28 09:57:01 +0200 | [diff] [blame] | 337 | if ((!total_size || total_size == -1ULL) && !td->o.size) { |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 338 | log_err("%s: you need to specify size=\n", td->o.name); |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 339 | td_verror(td, EINVAL, "total_file_size"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 340 | return 1; |
| 341 | } |
| 342 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 343 | /* |
| 344 | * now file sizes are known, so we can set ->io_size. if size= is |
| 345 | * not given, ->io_size is just equal to ->real_file_size. if size |
| 346 | * is given, ->io_size is size / nr_files. |
| 347 | */ |
| 348 | extend_size = total_size = 0; |
| 349 | need_extend = 0; |
| 350 | for_each_file(td, f, i) { |
| 351 | if (!td->o.file_size_low) { |
| 352 | /* |
| 353 | * no file size range given, file size is equal to |
| 354 | * total size divided by number of files. if that is |
| 355 | * zero, set it to the real file size. |
| 356 | */ |
| 357 | f->io_size = td->o.size / td->o.nr_files; |
| 358 | if (!f->io_size) |
| 359 | f->io_size = f->real_file_size; |
| 360 | } else if (f->real_file_size < td->o.file_size_low || |
| 361 | f->real_file_size > td->o.file_size_high) { |
| 362 | /* |
| 363 | * file size given. if it's fixed, use that. if it's a |
| 364 | * range, generate a random size in-between. |
| 365 | */ |
| 366 | if (td->o.file_size_low == td->o.file_size_high) |
| 367 | f->io_size = td->o.file_size_low; |
| 368 | else |
| 369 | f->io_size = get_rand_file_size(td); |
| 370 | } else |
| 371 | f->io_size = f->real_file_size; |
| 372 | |
| 373 | if (f->io_size == -1ULL) |
| 374 | total_size = -1ULL; |
| 375 | else |
| 376 | total_size += f->io_size; |
| 377 | |
| 378 | if (f->filetype == FIO_TYPE_FILE && |
| 379 | f->io_size > f->real_file_size && |
| 380 | !(td->io_ops->flags & FIO_DISKLESSIO)) { |
| 381 | need_extend++; |
| 382 | extend_size += f->io_size; |
| 383 | f->flags |= FIO_FILE_EXTEND; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | if (!td->o.size) |
| 388 | td->o.size = total_size; |
| 389 | |
| 390 | /* |
| 391 | * See if we need to extend some files |
| 392 | */ |
| 393 | if (need_extend) { |
| 394 | temp_stall_ts = 1; |
| 395 | log_info("%s: Laying out IO file(s) (%u files / %LuMiB)\n", |
| 396 | td->o.name, need_extend, extend_size >> 20); |
| 397 | |
| 398 | for_each_file(td, f, i) { |
| 399 | if (!(f->flags & FIO_FILE_EXTEND)) |
| 400 | continue; |
| 401 | |
Jens Axboe | 409b341 | 2007-03-28 09:57:01 +0200 | [diff] [blame] | 402 | assert(f->filetype == FIO_TYPE_FILE); |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 403 | f->flags &= ~FIO_FILE_EXTEND; |
| 404 | f->real_file_size = f->io_size; |
| 405 | err = extend_file(td, f); |
| 406 | if (err) |
| 407 | break; |
| 408 | } |
| 409 | temp_stall_ts = 0; |
| 410 | } |
| 411 | |
| 412 | if (err) |
| 413 | return err; |
| 414 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 415 | if (!td->o.zone_size) |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 416 | td->o.zone_size = td->o.size; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 417 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 418 | td->total_io_size = td->o.size * td->o.loops; |
| 419 | return 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 420 | } |
| 421 | |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 422 | int init_random_map(struct thread_data *td) |
| 423 | { |
| 424 | int num_maps, blocks; |
| 425 | struct fio_file *f; |
| 426 | unsigned int i; |
| 427 | |
| 428 | if (td->o.norandommap) |
| 429 | return 0; |
| 430 | |
| 431 | for_each_file(td, f, i) { |
| 432 | blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs; |
| 433 | num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP; |
| 434 | f->file_map = malloc(num_maps * sizeof(long)); |
| 435 | if (!f->file_map) { |
| 436 | log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n"); |
| 437 | return 1; |
| 438 | } |
| 439 | f->num_maps = num_maps; |
| 440 | memset(f->file_map, 0, num_maps * sizeof(long)); |
| 441 | } |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 446 | void close_files(struct thread_data *td) |
| 447 | { |
Jens Axboe | 0ab8db8 | 2006-10-18 17:16:23 +0200 | [diff] [blame] | 448 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 449 | unsigned int i; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 450 | |
Jens Axboe | 0ab8db8 | 2006-10-18 17:16:23 +0200 | [diff] [blame] | 451 | for_each_file(td, f, i) { |
Jens Axboe | fa1da86 | 2007-03-23 15:20:54 +0100 | [diff] [blame] | 452 | if ((f->flags & FIO_FILE_UNLINK) && |
| 453 | f->filetype == FIO_TYPE_FILE) |
Jens Axboe | 132ad46 | 2006-11-23 12:23:12 +0100 | [diff] [blame] | 454 | unlink(f->file_name); |
Jens Axboe | bdb4e2e | 2007-03-01 09:54:57 +0100 | [diff] [blame] | 455 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 456 | td_io_close_file(td, f); |
Jens Axboe | b3dc7f0 | 2007-03-09 12:52:15 +0100 | [diff] [blame] | 457 | |
Jens Axboe | fa1da86 | 2007-03-23 15:20:54 +0100 | [diff] [blame] | 458 | free(f->file_name); |
| 459 | f->file_name = NULL; |
| 460 | |
Jens Axboe | c343981 | 2007-03-20 13:54:53 +0100 | [diff] [blame] | 461 | if (f->file_map) { |
Jens Axboe | b3dc7f0 | 2007-03-09 12:52:15 +0100 | [diff] [blame] | 462 | free(f->file_map); |
Jens Axboe | c343981 | 2007-03-20 13:54:53 +0100 | [diff] [blame] | 463 | f->file_map = NULL; |
| 464 | } |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 465 | } |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 466 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 467 | td->o.filename = NULL; |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 468 | free(td->files); |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 469 | td->files = NULL; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 470 | td->o.nr_files = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 471 | } |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 472 | |
Jens Axboe | e3bab46 | 2007-03-13 11:22:09 +0100 | [diff] [blame] | 473 | static void get_file_type(struct fio_file *f) |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 474 | { |
| 475 | struct stat sb; |
| 476 | |
| 477 | f->filetype = FIO_TYPE_FILE; |
| 478 | |
Jens Axboe | e3bab46 | 2007-03-13 11:22:09 +0100 | [diff] [blame] | 479 | if (!lstat(f->file_name, &sb)) { |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 480 | if (S_ISBLK(sb.st_mode)) |
| 481 | f->filetype = FIO_TYPE_BD; |
| 482 | else if (S_ISCHR(sb.st_mode)) |
| 483 | f->filetype = FIO_TYPE_CHAR; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | void add_file(struct thread_data *td, const char *fname) |
| 488 | { |
Jens Axboe | 7b4e4fe | 2007-03-13 12:51:40 +0100 | [diff] [blame] | 489 | int cur_files = td->files_index; |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame] | 490 | char file_name[PATH_MAX]; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 491 | struct fio_file *f; |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame] | 492 | int len = 0; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 493 | |
| 494 | td->files = realloc(td->files, (cur_files + 1) * sizeof(*f)); |
| 495 | |
| 496 | f = &td->files[cur_files]; |
| 497 | memset(f, 0, sizeof(*f)); |
| 498 | f->fd = -1; |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame] | 499 | |
| 500 | if (td->o.directory) |
| 501 | len = sprintf(file_name, "%s/", td->o.directory); |
| 502 | |
| 503 | sprintf(file_name + len, "%s", fname); |
| 504 | f->file_name = strdup(file_name); |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 505 | |
Jens Axboe | e3bab46 | 2007-03-13 11:22:09 +0100 | [diff] [blame] | 506 | get_file_type(f); |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 507 | |
Jens Axboe | 7b4e4fe | 2007-03-13 12:51:40 +0100 | [diff] [blame] | 508 | td->files_index++; |
Jens Axboe | 1549441 | 2007-03-13 11:52:47 +0100 | [diff] [blame] | 509 | if (f->filetype == FIO_TYPE_FILE) |
| 510 | td->nr_normal_files++; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 511 | } |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 512 | |
| 513 | void get_file(struct fio_file *f) |
| 514 | { |
| 515 | f->references++; |
| 516 | } |
| 517 | |
| 518 | void put_file(struct thread_data *td, struct fio_file *f) |
| 519 | { |
| 520 | if (!(f->flags & FIO_FILE_OPEN)) |
| 521 | return; |
| 522 | |
| 523 | assert(f->references); |
| 524 | if (--f->references) |
| 525 | return; |
| 526 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 527 | if (should_fsync(td) && td->o.fsync_on_close) |
Jens Axboe | ebb1415 | 2007-03-13 14:42:15 +0100 | [diff] [blame] | 528 | fsync(f->fd); |
| 529 | |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 530 | if (td->io_ops->close_file) |
| 531 | td->io_ops->close_file(td, f); |
Jens Axboe | 1020a13 | 2007-03-29 11:15:06 +0200 | [diff] [blame] | 532 | |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 533 | td->nr_open_files--; |
| 534 | f->flags &= ~FIO_FILE_OPEN; |
| 535 | } |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 536 | |
| 537 | static int recurse_dir(struct thread_data *td, const char *dirname) |
| 538 | { |
| 539 | struct dirent *dir; |
| 540 | int ret = 0; |
| 541 | DIR *D; |
| 542 | |
| 543 | D = opendir(dirname); |
| 544 | if (!D) { |
Jens Axboe | 0ddb270 | 2007-03-27 19:43:53 +0200 | [diff] [blame] | 545 | char buf[FIO_VERROR_SIZE]; |
| 546 | |
| 547 | snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname); |
| 548 | td_verror(td, errno, buf); |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 549 | return 1; |
| 550 | } |
| 551 | |
| 552 | while ((dir = readdir(D)) != NULL) { |
| 553 | char full_path[PATH_MAX]; |
| 554 | struct stat sb; |
| 555 | |
Jens Axboe | e85b2b8 | 2007-03-14 09:39:06 +0100 | [diff] [blame] | 556 | if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) |
| 557 | continue; |
Jens Axboe | 96d32d5 | 2007-03-14 09:16:09 +0100 | [diff] [blame] | 558 | |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 559 | sprintf(full_path, "%s/%s", dirname, dir->d_name); |
| 560 | |
| 561 | if (lstat(full_path, &sb) == -1) { |
| 562 | if (errno != ENOENT) { |
| 563 | td_verror(td, errno, "stat"); |
| 564 | return 1; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | if (S_ISREG(sb.st_mode)) { |
| 569 | add_file(td, full_path); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 570 | td->o.nr_files++; |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 571 | continue; |
| 572 | } |
Jens Axboe | 0ddb270 | 2007-03-27 19:43:53 +0200 | [diff] [blame] | 573 | if (!S_ISDIR(sb.st_mode)) |
| 574 | continue; |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 575 | |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 576 | if ((ret = recurse_dir(td, full_path)) != 0) |
| 577 | break; |
| 578 | } |
| 579 | |
| 580 | closedir(D); |
| 581 | return ret; |
| 582 | } |
| 583 | |
| 584 | int add_dir_files(struct thread_data *td, const char *path) |
| 585 | { |
Jens Axboe | 0ddb270 | 2007-03-27 19:43:53 +0200 | [diff] [blame] | 586 | int ret = recurse_dir(td, path); |
| 587 | |
| 588 | if (!ret) |
| 589 | log_info("fio: opendir added %d files\n", td->o.nr_files); |
| 590 | |
| 591 | return ret; |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 592 | } |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 593 | |
| 594 | void dup_files(struct thread_data *td, struct thread_data *org) |
| 595 | { |
| 596 | struct fio_file *f; |
| 597 | unsigned int i; |
| 598 | size_t bytes; |
| 599 | |
| 600 | if (!org->files) |
| 601 | return; |
| 602 | |
| 603 | bytes = org->files_index * sizeof(*f); |
| 604 | td->files = malloc(bytes); |
| 605 | memcpy(td->files, org->files, bytes); |
| 606 | |
| 607 | for_each_file(td, f, i) { |
| 608 | if (f->file_name) |
| 609 | f->file_name = strdup(f->file_name); |
| 610 | } |
| 611 | } |