blob: 356580e74c1276ccde5af550116e3bfd6813321b [file] [log] [blame]
Jens Axboe53cdc682006-10-18 11:50:58 +02001#include <unistd.h>
2#include <fcntl.h>
3#include <string.h>
4#include <assert.h>
5#include <sys/stat.h>
6#include <sys/mman.h>
7
8#include "fio.h"
9#include "os.h"
10
Jens Axboe25205e92006-10-19 20:50:14 +020011/*
12 * Check if the file exists and it's large enough.
13 */
14static int file_ok(struct thread_data *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +020015{
Jens Axboeb2a15192006-10-18 14:10:42 +020016 struct stat st;
Jens Axboe53cdc682006-10-18 11:50:58 +020017
Jens Axboe4d9345a2007-02-07 13:14:57 +010018 if (td->filetype != FIO_TYPE_FILE || (td->io_ops->flags & FIO_NULLIO))
Jens Axboeb2a15192006-10-18 14:10:42 +020019 return 0;
20
Jens Axboefa01d132007-02-22 14:07:39 +010021 if (lstat(f->file_name, &st) == -1)
Jens Axboe25205e92006-10-19 20:50:14 +020022 return 1;
Jens Axboefa01d132007-02-22 14:07:39 +010023
24 /*
25 * if it's a special file, size is always ok for now
26 */
27 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
28 return 0;
29 if (st.st_size < (off_t) f->file_size)
Jens Axboe25205e92006-10-19 20:50:14 +020030 return 1;
31
32 return 0;
33}
34
35static int create_file(struct thread_data *td, struct fio_file *f)
36{
37 unsigned long long left;
38 unsigned int bs;
39 char *b;
40 int r;
Jens Axboeb2a15192006-10-18 14:10:42 +020041
Jens Axboe53cdc682006-10-18 11:50:58 +020042 f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
43 if (f->fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010044 td_verror(td, errno, "open");
Jens Axboe53cdc682006-10-18 11:50:58 +020045 return 1;
46 }
47
48 if (ftruncate(f->fd, f->file_size) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +010049 td_verror(td, errno, "ftruncate");
Jens Axboe53cdc682006-10-18 11:50:58 +020050 goto err;
51 }
52
Jens Axboe40f82982006-10-25 11:21:05 +020053 if (posix_fallocate(f->fd, 0, f->file_size) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010054 td_verror(td, errno, "posix_fallocate");
Jens Axboe40f82982006-10-25 11:21:05 +020055 goto err;
56 }
57
Jens Axboea00735e2006-11-03 08:58:08 +010058 b = malloc(td->max_bs[DDIR_WRITE]);
59 memset(b, 0, td->max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +020060
61 left = f->file_size;
62 while (left && !td->terminate) {
Jens Axboea00735e2006-11-03 08:58:08 +010063 bs = td->max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +020064 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 Axboee1161c32007-02-22 19:36:48 +010074 td_verror(td, errno, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +020075 else
Jens Axboee1161c32007-02-22 19:36:48 +010076 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +020077
78 break;
79 }
80 }
81
82 if (td->terminate)
83 unlink(f->file_name);
84 else if (td->create_fsync)
85 fsync(f->fd);
86
87 free(b);
88 close(f->fd);
89 f->fd = -1;
90 return 0;
91err:
92 close(f->fd);
93 f->fd = -1;
94 return 1;
95}
96
97static int create_files(struct thread_data *td)
98{
99 struct fio_file *f;
Jens Axboe25205e92006-10-19 20:50:14 +0200100 int i, err, need_create;
Jens Axboe53cdc682006-10-18 11:50:58 +0200101
Jens Axboef6971252006-11-15 10:00:31 +0100102 for_each_file(td, f, i)
103 f->file_size = td->total_file_size / td->nr_files;
104
Jens Axboe53cdc682006-10-18 11:50:58 +0200105 /*
106 * unless specifically asked for overwrite, let normal io extend it
107 */
Jens Axboef6971252006-11-15 10:00:31 +0100108 if (!td->overwrite)
Jens Axboe53cdc682006-10-18 11:50:58 +0200109 return 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200110
Jens Axboef1027062006-10-20 10:14:01 +0200111 need_create = 0;
Jens Axboeeff68612007-01-14 05:56:44 +0100112 if (td->filetype == FIO_TYPE_FILE) {
113 for_each_file(td, f, i) {
114 int file_there = !file_ok(td, f);
115
Jens Axboe413dd452007-02-23 09:26:09 +0100116 if (file_there && td_write(td) && !td->overwrite) {
Jens Axboeeff68612007-01-14 05:56:44 +0100117 unlink(f->file_name);
118 file_there = 0;
119 }
120
121 need_create += !file_there;
122 }
123 }
Jens Axboef1027062006-10-20 10:14:01 +0200124
125 if (!need_create)
126 return 0;
127
Jens Axboe53cdc682006-10-18 11:50:58 +0200128 if (!td->total_file_size) {
129 log_err("Need size for create\n");
Jens Axboee1161c32007-02-22 19:36:48 +0100130 td_verror(td, EINVAL, "file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200131 return 1;
132 }
133
Jens Axboe25205e92006-10-19 20:50:14 +0200134 temp_stall_ts = 1;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100135 fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200136 td->name, td->nr_uniq_files,
137 (td->total_file_size >> 20) / td->nr_uniq_files,
Jens Axboe25205e92006-10-19 20:50:14 +0200138 td->total_file_size >> 20);
139
140 err = 0;
141 for_each_file(td, f, i) {
Jens Axboe9b031dc2006-12-15 16:01:54 +0100142 /*
143 * Only unlink files that we created.
144 */
145 f->unlink = 0;
Jens Axboe25205e92006-10-19 20:50:14 +0200146 if (file_ok(td, f)) {
Jens Axboe9b031dc2006-12-15 16:01:54 +0100147 f->unlink = td->unlink;
Jens Axboe25205e92006-10-19 20:50:14 +0200148 err = create_file(td, f);
149 if (err)
150 break;
151 }
152 }
153
Jens Axboe53cdc682006-10-18 11:50:58 +0200154 temp_stall_ts = 0;
155 return err;
156}
157
158static int file_size(struct thread_data *td, struct fio_file *f)
159{
160 struct stat st;
161
Jens Axboef4ee22c2007-02-10 18:57:18 +0100162 /*
163 * if we are not doing real io, just pretend the file is as large
164 * as the size= given. this works fine with nrfiles > 1 as well,
165 * we only really care about it being at least as big as size=
166 */
167 if (td->io_ops->flags & FIO_NULLIO) {
168 f->real_file_size = f->file_size = td->total_file_size;
169 return 0;
170 }
171
Jens Axboe53cdc682006-10-18 11:50:58 +0200172 if (td->overwrite) {
173 if (fstat(f->fd, &st) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100174 td_verror(td, errno, "fstat");
Jens Axboe53cdc682006-10-18 11:50:58 +0200175 return 1;
176 }
177
178 f->real_file_size = st.st_size;
179
180 if (!f->file_size || f->file_size > f->real_file_size)
181 f->file_size = f->real_file_size;
Jens Axboe745508d2007-01-08 10:25:51 +0100182 } else
183 f->real_file_size = f->file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200184
Jens Axboe53cdc682006-10-18 11:50:58 +0200185 return 0;
186}
187
188static int bdev_size(struct thread_data *td, struct fio_file *f)
189{
190 unsigned long long bytes;
191 int r;
192
193 r = blockdev_size(f->fd, &bytes);
194 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100195 td_verror(td, r, "blockdev_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200196 return 1;
197 }
198
199 f->real_file_size = bytes;
200
201 /*
202 * no extend possibilities, so limit size to device size if too large
203 */
204 if (!f->file_size || f->file_size > f->real_file_size)
205 f->file_size = f->real_file_size;
206
207 f->file_size -= f->file_offset;
208 return 0;
209}
210
211static int get_file_size(struct thread_data *td, struct fio_file *f)
212{
213 int ret = 0;
214
215 if (td->filetype == FIO_TYPE_FILE)
216 ret = file_size(td, f);
217 else if (td->filetype == FIO_TYPE_BD)
218 ret = bdev_size(td, f);
219 else
220 f->real_file_size = -1;
221
222 if (ret)
223 return ret;
224
225 if (f->file_offset > f->real_file_size) {
226 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
227 return 1;
228 }
229
Jens Axboe53cdc682006-10-18 11:50:58 +0200230 return 0;
231}
232
Jens Axboee5b401d2006-10-18 16:03:40 +0200233int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
234{
235 int ret = 0;
236
237 /*
238 * FIXME: add blockdev flushing too
239 */
240 if (td->io_ops->flags & FIO_MMAPIO)
241 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
242 else if (td->filetype == FIO_TYPE_FILE)
243 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
244 else if (td->filetype == FIO_TYPE_BD)
245 ret = blockdev_invalidate_cache(f->fd);
246 else if (td->filetype == FIO_TYPE_CHAR)
247 ret = 0;
248
249 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100250 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200251 return 1;
252 }
253
254 return 0;
255}
256
Jens Axboe53cdc682006-10-18 11:50:58 +0200257static int __setup_file_mmap(struct thread_data *td, struct fio_file *f)
258{
259 int flags;
260
261 if (td_rw(td))
262 flags = PROT_READ | PROT_WRITE;
263 else if (td_write(td)) {
264 flags = PROT_WRITE;
265
266 if (td->verify != VERIFY_NONE)
267 flags |= PROT_READ;
268 } else
269 flags = PROT_READ;
270
271 f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset);
272 if (f->mmap == MAP_FAILED) {
273 f->mmap = NULL;
Jens Axboee1161c32007-02-22 19:36:48 +0100274 td_verror(td, errno, "mmap");
Jens Axboe53cdc682006-10-18 11:50:58 +0200275 return 1;
276 }
277
Jens Axboee5b401d2006-10-18 16:03:40 +0200278 if (td->invalidate_cache && file_invalidate_cache(td, f))
279 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200280
Jens Axboe413dd452007-02-23 09:26:09 +0100281 if (!td_random(td)) {
Jens Axboe53cdc682006-10-18 11:50:58 +0200282 if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100283 td_verror(td, errno, "madvise");
Jens Axboe53cdc682006-10-18 11:50:58 +0200284 return 1;
285 }
286 } else {
287 if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100288 td_verror(td, errno, "madvise");
Jens Axboe53cdc682006-10-18 11:50:58 +0200289 return 1;
290 }
291 }
292
293 return 0;
294}
295
296static int setup_files_mmap(struct thread_data *td)
297{
298 struct fio_file *f;
299 int i, err = 0;
300
301 for_each_file(td, f, i) {
302 err = __setup_file_mmap(td, f);
303 if (err)
304 break;
305 }
306
307 return err;
308}
309
310static int __setup_file_plain(struct thread_data *td, struct fio_file *f)
311{
Jens Axboee5b401d2006-10-18 16:03:40 +0200312 if (td->invalidate_cache && file_invalidate_cache(td, f))
313 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200314
Jens Axboe413dd452007-02-23 09:26:09 +0100315 if (!td_random(td)) {
Jens Axboe53cdc682006-10-18 11:50:58 +0200316 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100317 td_verror(td, errno, "fadvise");
Jens Axboe53cdc682006-10-18 11:50:58 +0200318 return 1;
319 }
320 } else {
321 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100322 td_verror(td, errno, "fadvise");
Jens Axboe53cdc682006-10-18 11:50:58 +0200323 return 1;
324 }
325 }
326
327 return 0;
328}
329
330static int setup_files_plain(struct thread_data *td)
331{
332 struct fio_file *f;
333 int i, err = 0;
334
335 for_each_file(td, f, i) {
336 err = __setup_file_plain(td, f);
337 if (err)
338 break;
339 }
340
341 return err;
342}
343
344static int setup_file(struct thread_data *td, struct fio_file *f)
345{
Jens Axboe53cdc682006-10-18 11:50:58 +0200346 int flags = 0;
347
Jens Axboeed92ac02007-02-06 14:43:52 +0100348 if (td->io_ops->flags & FIO_NETIO)
349 return 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200350
Jens Axboe4d9345a2007-02-07 13:14:57 +0100351 /*
352 * we need a valid file descriptor, but don't create a real file.
353 * lets just dup stdout, seems like a sensible approach.
354 */
355 if (td->io_ops->flags & FIO_NULLIO)
356 f->fd = dup(STDOUT_FILENO);
357 else {
358 if (td->odirect)
359 flags |= OS_O_DIRECT;
360 if (td->sync_io)
361 flags |= O_SYNC;
Jens Axboe7abf8332006-11-23 15:01:19 +0100362
Jens Axboe4d9345a2007-02-07 13:14:57 +0100363 if (td_write(td) || td_rw(td)) {
Jens Axboe53cdc682006-10-18 11:50:58 +0200364 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200365
Jens Axboe4d9345a2007-02-07 13:14:57 +0100366 if (td->filetype == FIO_TYPE_FILE) {
367 if (!td->overwrite)
368 flags |= O_TRUNC;
369
370 flags |= O_CREAT;
371 }
372
373 f->fd = open(f->file_name, flags, 0600);
374 } else {
375 if (td->filetype == FIO_TYPE_CHAR)
376 flags |= O_RDWR;
377 else
378 flags |= O_RDONLY;
379
380 f->fd = open(f->file_name, flags);
381 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200382 }
383
384 if (f->fd == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100385 int __e = errno;
386
387 td_verror(td, __e, "open");
388 if (__e == EINVAL && td->odirect)
Jens Axboe9d80e112007-02-22 19:37:53 +0100389 log_err("fio: destination does not support O_DIRECT\n");
Jens Axboe53cdc682006-10-18 11:50:58 +0200390 return 1;
391 }
392
393 if (get_file_size(td, f))
394 return 1;
395
396 return 0;
397}
398
Jens Axboe21972cd2006-11-23 12:39:16 +0100399int open_files(struct thread_data *td)
400{
401 struct fio_file *f;
402 int i, err = 0;
403
404 for_each_file(td, f, i) {
405 err = setup_file(td, f);
406 if (err)
407 break;
408 }
409
Jens Axboe7abf8332006-11-23 15:01:19 +0100410 if (!err)
411 return 0;
412
413 for_each_file(td, f, i) {
414 if (f->fd != -1) {
415 close(f->fd);
416 f->fd = -1;
417 }
418 }
419
Jens Axboe21972cd2006-11-23 12:39:16 +0100420 return err;
421}
422
Jens Axboe53cdc682006-10-18 11:50:58 +0200423int setup_files(struct thread_data *td)
424{
425 struct fio_file *f;
Jens Axboe21972cd2006-11-23 12:39:16 +0100426 int err, i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200427
428 /*
429 * if ioengine defines a setup() method, it's responsible for
430 * setting up everything in the td->files[] area.
431 */
432 if (td->io_ops->setup)
433 return td->io_ops->setup(td);
434
435 if (create_files(td))
436 return 1;
437
Jens Axboe21972cd2006-11-23 12:39:16 +0100438 err = open_files(td);
Jens Axboef1027062006-10-20 10:14:01 +0200439 if (err)
440 return err;
441
Jens Axboe0a7eb122006-10-20 10:36:42 +0200442 /*
443 * Recalculate the total file size now that files are set up.
444 */
445 td->total_file_size = 0;
446 for_each_file(td, f, i)
447 td->total_file_size += f->file_size;
448
Jens Axboef1027062006-10-20 10:14:01 +0200449 td->io_size = td->total_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200450 if (td->io_size == 0) {
451 log_err("%s: no io blocks\n", td->name);
Jens Axboee1161c32007-02-22 19:36:48 +0100452 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200453 return 1;
454 }
455
456 if (!td->zone_size)
457 td->zone_size = td->io_size;
458
459 td->total_io_size = td->io_size * td->loops;
460
461 if (td->io_ops->flags & FIO_MMAPIO)
Jens Axboe21972cd2006-11-23 12:39:16 +0100462 err = setup_files_mmap(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200463 else
Jens Axboe21972cd2006-11-23 12:39:16 +0100464 err = setup_files_plain(td);
465
466 for_each_file(td, f, i) {
467 if (f->fd != -1) {
468 close(f->fd);
469 f->fd = -1;
470 }
471 }
472
473 return err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200474}
475
476void close_files(struct thread_data *td)
477{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200478 struct fio_file *f;
Jens Axboe53cdc682006-10-18 11:50:58 +0200479 int i;
480
Jens Axboe0ab8db82006-10-18 17:16:23 +0200481 for_each_file(td, f, i) {
Jens Axboe9b031dc2006-12-15 16:01:54 +0100482 if (!td->filename && f->unlink &&
Jens Axboe02bcaa82006-11-24 10:42:00 +0100483 td->filetype == FIO_TYPE_FILE) {
Jens Axboe132ad462006-11-23 12:23:12 +0100484 unlink(f->file_name);
485 free(f->file_name);
486 f->file_name = NULL;
487 }
Jens Axboe21972cd2006-11-23 12:39:16 +0100488 if (f->fd != -1) {
489 close(f->fd);
490 f->fd = -1;
491 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200492 if (f->mmap) {
493 munmap(f->mmap, f->file_size);
494 f->mmap = NULL;
495 }
496 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200497
Jens Axboe132ad462006-11-23 12:23:12 +0100498 td->filename = NULL;
Jens Axboeb4a6a592006-10-20 13:54:47 +0200499 free(td->files);
500 td->files = NULL;
501 td->nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200502}