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