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