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