13 static int extend_file(struct thread_data *td, struct fio_file *f)
15 unsigned long long left;
20 if (f->flags & FIO_FILE_EXISTS) {
21 if (unlink(f->file_name) < 0) {
22 td_verror(td, errno, "unlink");
27 f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
29 td_verror(td, errno, "open");
33 if (ftruncate(f->fd, f->real_file_size) == -1) {
34 td_verror(td, errno, "ftruncate");
38 if (posix_fallocate(f->fd, 0, f->real_file_size) < 0) {
39 td_verror(td, errno, "posix_fallocate");
43 b = malloc(td->o.max_bs[DDIR_WRITE]);
44 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
46 left = f->real_file_size;
47 while (left && !td->terminate) {
48 bs = td->o.max_bs[DDIR_WRITE];
52 r = write(f->fd, b, bs);
59 td_verror(td, errno, "write");
61 td_verror(td, EIO, "write");
69 else if (td->o.create_fsync)
82 static unsigned long long get_rand_file_size(struct thread_data *td)
84 unsigned long long ret;
87 r = os_random_long(&td->file_size_state);
88 ret = td->o.file_size_low + (unsigned long long) ((double) td->o.file_size_high * (r / (RAND_MAX + 1.0)));
89 ret -= (ret % td->o.rw_min_bs);
93 static int file_size(struct thread_data *td, struct fio_file *f)
97 if (fstat(f->fd, &st) == -1) {
98 td_verror(td, errno, "fstat");
102 f->real_file_size = st.st_size;
106 static int bdev_size(struct thread_data *td, struct fio_file *f)
108 unsigned long long bytes;
111 r = blockdev_size(f->fd, &bytes);
113 td_verror(td, r, "blockdev_size");
117 f->real_file_size = bytes;
121 static int get_file_size(struct thread_data *td, struct fio_file *f)
125 if (f->filetype == FIO_TYPE_FILE)
126 ret = file_size(td, f);
127 else if (f->filetype == FIO_TYPE_BD)
128 ret = bdev_size(td, f);
130 f->real_file_size = -1;
135 if (f->file_offset > f->real_file_size) {
136 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name, f->file_offset, f->real_file_size);
143 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
151 * FIXME: add blockdev flushing too
154 ret = madvise(f->mmap, f->io_size, MADV_DONTNEED);
155 else if (f->filetype == FIO_TYPE_FILE)
156 ret = fadvise(f->fd, f->file_offset, f->io_size, POSIX_FADV_DONTNEED);
157 else if (f->filetype == FIO_TYPE_BD) {
158 ret = blockdev_invalidate_cache(f->fd);
159 if (ret < 0 && errno == EACCES && geteuid()) {
160 log_err("fio: only root may flush block devices. Cache flush bypassed!\n");
163 } else if (f->filetype == FIO_TYPE_CHAR)
167 td_verror(td, errno, "invalidate_cache");
174 void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
180 int generic_open_file(struct thread_data *td, struct fio_file *f)
185 flags |= OS_O_DIRECT;
189 if (td_write(td) || td_rw(td)) {
192 if (f->filetype == FIO_TYPE_FILE)
195 f->fd = open(f->file_name, flags, 0600);
197 if (f->filetype == FIO_TYPE_CHAR)
202 f->fd = open(f->file_name, flags);
206 char buf[FIO_VERROR_SIZE];
209 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
211 td_verror(td, __e, buf);
214 if (get_file_size(td, f))
217 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
220 if (td->o.fadvise_hint) {
222 flags = POSIX_FADV_RANDOM;
224 flags = POSIX_FADV_SEQUENTIAL;
226 if (fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
227 td_verror(td, errno, "fadvise");
238 int open_files(struct thread_data *td)
244 for_each_file(td, f, i) {
245 err = td_io_open_file(td, f);
249 if (td->o.open_files == td->nr_open_files)
256 for_each_file(td, f, i)
257 td_io_close_file(td, f);
263 * open/close all files, so that ->real_file_size gets set
265 static int get_file_sizes(struct thread_data *td)
271 for_each_file(td, f, i) {
272 err = td->io_ops->open_file(td, f);
275 memset(td->verror, 0, sizeof(td->verror));
280 td->io_ops->close_file(td, f);
287 * Open the files and setup files sizes, creating files if necessary.
289 int setup_files(struct thread_data *td)
291 unsigned long long total_size, extend_size;
294 int err, need_extend;
297 * if ioengine defines a setup() method, it's responsible for
298 * opening the files and setting f->real_file_size to indicate
299 * the valid range for that file.
301 if (td->io_ops->setup)
302 err = td->io_ops->setup(td);
304 err = get_file_sizes(td);
310 * check sizes. if the files/devices do not exist and the size
311 * isn't passed to fio, abort.
314 for_each_file(td, f, i) {
315 if (f->real_file_size == -1ULL)
318 total_size += f->real_file_size;
322 * device/file sizes are zero and no size given, punt
324 if (!total_size && !td->o.size) {
325 log_err("%s: you need to specify size=\n", td->o.name);
326 td_verror(td, EINVAL, "total_file_size");
331 * now file sizes are known, so we can set ->io_size. if size= is
332 * not given, ->io_size is just equal to ->real_file_size. if size
333 * is given, ->io_size is size / nr_files.
335 extend_size = total_size = 0;
337 for_each_file(td, f, i) {
338 if (!td->o.file_size_low) {
340 * no file size range given, file size is equal to
341 * total size divided by number of files. if that is
342 * zero, set it to the real file size.
344 f->io_size = td->o.size / td->o.nr_files;
346 f->io_size = f->real_file_size;
347 } else if (f->real_file_size < td->o.file_size_low ||
348 f->real_file_size > td->o.file_size_high) {
350 * file size given. if it's fixed, use that. if it's a
351 * range, generate a random size in-between.
353 if (td->o.file_size_low == td->o.file_size_high)
354 f->io_size = td->o.file_size_low;
356 f->io_size = get_rand_file_size(td);
358 f->io_size = f->real_file_size;
360 if (f->io_size == -1ULL)
363 total_size += f->io_size;
365 if (f->filetype == FIO_TYPE_FILE &&
366 f->io_size > f->real_file_size &&
367 !(td->io_ops->flags & FIO_DISKLESSIO)) {
369 extend_size += f->io_size;
370 f->flags |= FIO_FILE_EXTEND;
375 td->o.size = total_size;
378 * See if we need to extend some files
382 log_info("%s: Laying out IO file(s) (%u files / %LuMiB)\n",
383 td->o.name, need_extend, extend_size >> 20);
385 for_each_file(td, f, i) {
386 if (!(f->flags & FIO_FILE_EXTEND))
389 f->flags &= ~FIO_FILE_EXTEND;
390 f->real_file_size = f->io_size;
391 err = extend_file(td, f);
401 if (!td->o.zone_size)
402 td->o.zone_size = td->o.size;
404 td->total_io_size = td->o.size * td->o.loops;
408 int init_random_map(struct thread_data *td)
410 int num_maps, blocks;
414 if (td->o.norandommap)
417 for_each_file(td, f, i) {
418 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs;
419 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
420 f->file_map = malloc(num_maps * sizeof(long));
422 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
425 f->num_maps = num_maps;
426 memset(f->file_map, 0, num_maps * sizeof(long));
432 void close_files(struct thread_data *td)
437 for_each_file(td, f, i) {
438 if ((f->flags & FIO_FILE_UNLINK) &&
439 f->filetype == FIO_TYPE_FILE)
440 unlink(f->file_name);
442 td_io_close_file(td, f);
453 td->o.filename = NULL;
459 static void get_file_type(struct fio_file *f)
463 f->filetype = FIO_TYPE_FILE;
465 if (!lstat(f->file_name, &sb)) {
466 if (S_ISBLK(sb.st_mode))
467 f->filetype = FIO_TYPE_BD;
468 else if (S_ISCHR(sb.st_mode))
469 f->filetype = FIO_TYPE_CHAR;
473 void add_file(struct thread_data *td, const char *fname)
475 int cur_files = td->files_index;
476 char file_name[PATH_MAX];
480 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
482 f = &td->files[cur_files];
483 memset(f, 0, sizeof(*f));
487 len = sprintf(file_name, "%s/", td->o.directory);
489 sprintf(file_name + len, "%s", fname);
490 f->file_name = strdup(file_name);
495 if (f->filetype == FIO_TYPE_FILE)
496 td->nr_normal_files++;
499 void get_file(struct fio_file *f)
504 void put_file(struct thread_data *td, struct fio_file *f)
506 if (!(f->flags & FIO_FILE_OPEN))
509 assert(f->references);
513 if (should_fsync(td) && td->o.fsync_on_close)
516 if (td->io_ops->close_file)
517 td->io_ops->close_file(td, f);
519 f->flags &= ~FIO_FILE_OPEN;
522 static int recurse_dir(struct thread_data *td, const char *dirname)
528 D = opendir(dirname);
530 td_verror(td, errno, "opendir");
534 while ((dir = readdir(D)) != NULL) {
535 char full_path[PATH_MAX];
538 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
541 sprintf(full_path, "%s/%s", dirname, dir->d_name);
543 if (lstat(full_path, &sb) == -1) {
544 if (errno != ENOENT) {
545 td_verror(td, errno, "stat");
550 if (S_ISREG(sb.st_mode)) {
551 add_file(td, full_path);
556 if ((ret = recurse_dir(td, full_path)) != 0)
564 int add_dir_files(struct thread_data *td, const char *path)
566 return recurse_dir(td, path);
569 void dup_files(struct thread_data *td, struct thread_data *org)
578 bytes = org->files_index * sizeof(*f);
579 td->files = malloc(bytes);
580 memcpy(td->files, org->files, bytes);
582 for_each_file(td, f, i) {
584 f->file_name = strdup(f->file_name);