Add few debug statements and limit thread usage.
[fio.git] / filesetup.c
1 #include <unistd.h>
2 #include <fcntl.h>
3 #include <string.h>
4 #include <assert.h>
5 #include <dirent.h>
6 #include <sys/stat.h>
7 #include <sys/mman.h>
8 #include <sys/types.h>
9
10 #include "fio.h"
11 #include "os.h"
12
13 /*
14  * Check if the file exists and it's large enough.
15  */
16 static int file_ok(struct thread_data *td, struct fio_file *f)
17 {
18         struct stat st;
19
20         if (f->filetype != FIO_TYPE_FILE ||
21             (td->io_ops->flags & FIO_DISKLESSIO))
22                 return 0;
23
24         if (lstat(f->file_name, &st) == -1)
25                 return 1;
26
27         /*
28          * if it's a special file, size is always ok for now
29          */
30         if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
31                 return 0;
32         if (st.st_size < (off_t) f->file_size)
33                 return 1;
34
35         return 0;
36 }
37
38 static int create_file(struct thread_data *td, struct fio_file *f)
39 {
40         unsigned long long left;
41         unsigned int bs;
42         char *b;
43         int r;
44
45         f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
46         if (f->fd < 0) {
47                 td_verror(td, errno, "open");
48                 return 1;
49         }
50
51         if (ftruncate(f->fd, f->file_size) == -1) {
52                 td_verror(td, errno, "ftruncate");
53                 goto err;
54         }
55
56         if (posix_fallocate(f->fd, 0, f->file_size) < 0) {
57                 td_verror(td, errno, "posix_fallocate");
58                 goto err;
59         }
60
61         b = malloc(td->o.max_bs[DDIR_WRITE]);
62         memset(b, 0, td->o.max_bs[DDIR_WRITE]);
63
64         left = f->file_size;
65         while (left && !td->terminate) {
66                 bs = td->o.max_bs[DDIR_WRITE];
67                 if (bs > left)
68                         bs = left;
69
70                 r = write(f->fd, b, bs);
71
72                 if (r == (int) bs) {
73                         left -= bs;
74                         continue;
75                 } else {
76                         if (r < 0)
77                                 td_verror(td, errno, "write");
78                         else
79                                 td_verror(td, EIO, "write");
80
81                         break;
82                 }
83         }
84
85         if (td->terminate)
86                 unlink(f->file_name);
87         else if (td->o.create_fsync)
88                 fsync(f->fd);
89
90         free(b);
91         close(f->fd);
92         f->fd = -1;
93         return 0;
94 err:
95         close(f->fd);
96         f->fd = -1;
97         return 1;
98 }
99
100 static unsigned long long set_rand_file_size(struct thread_data *td,
101                                              unsigned long long total_size)
102 {
103         unsigned long long upper = total_size;
104         unsigned long long ret;
105         long r;
106
107         if (upper > td->o.file_size_high)
108                 upper = td->o.file_size_high;
109         else if (upper < td->o.file_size_low)
110                 return 0;
111         else if (!upper)
112                 return 0;
113
114         r = os_random_long(&td->file_size_state);
115         ret = td->o.file_size_low + (unsigned long long) ((double) upper * (r / (RAND_MAX + 1.0)));
116         ret -= (ret % td->o.rw_min_bs);
117         if (ret > upper)
118                 ret = upper;
119         return ret;
120 }
121
122 static int fill_file_size(struct thread_data *td, struct fio_file *f,
123                           unsigned long long *file_size, int new_files)
124 {
125         if (!td->o.file_size_low) {
126                 f->file_size = *file_size / new_files;
127                 f->real_file_size = f->file_size;
128         } else {
129                 /*
130                  * If we don't have enough space left for a file
131                  * of the minimum size, bail.
132                  */
133                 if (*file_size < td->o.file_size_low)
134                         return 1;
135
136                 f->file_size = set_rand_file_size(td, *file_size);
137                 f->real_file_size = f->file_size;
138                 *file_size -= f->file_size;
139         }
140
141         return 0;
142 }
143
144 static int create_files(struct thread_data *td)
145 {
146         struct fio_file *f;
147         int err, need_create, can_extend;
148         unsigned long long total_file_size, local_file_size, create_size;
149         unsigned int i, new_files;
150
151         new_files = 0;
152         total_file_size = td->o.size;
153         for_each_file(td, f, i) {
154                 unsigned long long s;
155
156                 f->file_offset = td->o.start_offset;
157
158                 if (f->filetype != FIO_TYPE_FILE)
159                         continue;
160                 if (!total_file_size)
161                         continue;
162
163                 if (f->flags & FIO_FILE_EXISTS) {
164                         if ((f->file_size > td->o.size / td->o.nr_files) ||
165                             !f->file_size)
166                                 f->file_size = td->o.size / td->o.nr_files;
167
168                         s = f->file_size;
169                         if (s > total_file_size)
170                                 s = total_file_size;
171
172                         total_file_size -= s;
173                 } else
174                         new_files++;
175         }
176
177         /*
178          * unless specifically asked for overwrite, let normal io extend it
179          */
180         can_extend = !td->o.overwrite && !(td->io_ops->flags & FIO_NOEXTEND);
181         if (can_extend && new_files) {
182                 for_each_file(td, f, i) {
183                         if (fill_file_size(td, f, &total_file_size, new_files)) {
184                                 log_info("fio: limited to %d files\n", i);
185                                 td->o.nr_files = i;
186                                 break;
187                         }
188                 }
189
190                 return 0;
191         }
192
193         local_file_size = total_file_size;
194         if (!local_file_size)
195                 local_file_size = -1;
196
197         total_file_size = 0;
198         need_create = 0;
199         create_size = 0;
200         for_each_file(td, f, i) {
201                 int file_there;
202
203                 if (f->filetype != FIO_TYPE_FILE)
204                         continue;
205                 if (f->flags & FIO_FILE_EXISTS) {
206                         total_file_size += f->file_size;
207                         continue;
208                 }
209
210                 if (fill_file_size(td, f, &local_file_size, new_files)) {
211                         log_info("fio: limited to %d files\n", i);
212                         new_files -= (td->o.nr_files - i);
213                         td->o.nr_files = i;
214                         break;
215                 }
216
217                 total_file_size += f->file_size;
218                 create_size += f->file_size;
219                 file_there = !file_ok(td, f);
220
221                 if (file_there && td_write(td) && !td->o.overwrite) {
222                         unlink(f->file_name);
223                         file_there = 0;
224                 }
225
226                 need_create += !file_there;
227         }
228
229         if (!need_create)
230                 return 0;
231
232         if (!td->o.size && !total_file_size) {
233                 log_err("Need size for create\n");
234                 td_verror(td, EINVAL, "file_size");
235                 return 1;
236         }
237
238         temp_stall_ts = 1;
239         log_info("%s: Laying out IO file(s) (%u files / %LuMiB)\n",
240                                 td->o.name, new_files, create_size >> 20);
241
242         err = 0;
243         for_each_file(td, f, i) {
244                 /*
245                  * Only unlink files that we created.
246                  */
247                 f->flags &= ~FIO_FILE_UNLINK;
248                 if (file_ok(td, f)) {
249                         if (td->o.unlink)
250                                 f->flags |= FIO_FILE_UNLINK;
251
252                         err = create_file(td, f);
253                         if (err)
254                                 break;
255                 }
256         }
257
258         temp_stall_ts = 0;
259         return err;
260 }
261
262 static int file_size(struct thread_data *td, struct fio_file *f)
263 {
264         struct stat st;
265
266         if (td->o.overwrite) {
267                 if (fstat(f->fd, &st) == -1) {
268                         td_verror(td, errno, "fstat");
269                         return 1;
270                 }
271
272                 f->real_file_size = st.st_size;
273
274                 if (!f->file_size || f->file_size > f->real_file_size)
275                         f->file_size = f->real_file_size;
276         } else
277                 f->real_file_size = f->file_size;
278
279         return 0;
280 }
281
282 static int bdev_size(struct thread_data *td, struct fio_file *f)
283 {
284         unsigned long long bytes;
285         int r;
286
287         r = blockdev_size(f->fd, &bytes);
288         if (r) {
289                 td_verror(td, r, "blockdev_size");
290                 return 1;
291         }
292
293         f->real_file_size = bytes;
294
295         /*
296          * no extend possibilities, so limit size to device size if too large
297          */
298         if (!f->file_size || f->file_size > f->real_file_size)
299                 f->file_size = f->real_file_size;
300
301         f->file_size -= f->file_offset;
302         return 0;
303 }
304
305 static int get_file_size(struct thread_data *td, struct fio_file *f)
306 {
307         int ret = 0;
308
309         if (f->filetype == FIO_TYPE_FILE) {
310                 if (!(f->flags & FIO_FILE_EXISTS))
311                         ret = file_size(td, f);
312         } else if (f->filetype == FIO_TYPE_BD)
313                 ret = bdev_size(td, f);
314         else
315                 f->real_file_size = -1;
316
317         if (ret)
318                 return ret;
319
320         if (f->file_offset > f->real_file_size) {
321                 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name, f->file_offset, f->real_file_size);
322                 return 1;
323         }
324
325         return 0;
326 }
327
328 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
329 {
330         int ret = 0;
331
332         if (td->o.odirect)
333                 return 0;
334
335         /*
336          * FIXME: add blockdev flushing too
337          */
338         if (f->mmap)
339                 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
340         else if (f->filetype == FIO_TYPE_FILE)
341                 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
342         else if (f->filetype == FIO_TYPE_BD)
343                 ret = blockdev_invalidate_cache(f->fd);
344         else if (f->filetype == FIO_TYPE_CHAR)
345                 ret = 0;
346
347         if (ret < 0) {
348                 td_verror(td, errno, "invalidate_cache");
349                 return 1;
350         }
351
352         return ret;
353 }
354
355 void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
356 {
357         close(f->fd);
358         f->fd = -1;
359 }
360
361 int generic_open_file(struct thread_data *td, struct fio_file *f)
362 {
363         int flags = 0;
364
365         if (td->o.odirect)
366                 flags |= OS_O_DIRECT;
367         if (td->o.sync_io)
368                 flags |= O_SYNC;
369
370         if (td_write(td) || td_rw(td)) {
371                 flags |= O_RDWR;
372
373                 if (f->filetype == FIO_TYPE_FILE)
374                         flags |= O_CREAT;
375
376                 f->fd = open(f->file_name, flags, 0600);
377         } else {
378                 if (f->filetype == FIO_TYPE_CHAR)
379                         flags |= O_RDWR;
380                 else
381                         flags |= O_RDONLY;
382
383                 f->fd = open(f->file_name, flags);
384         }
385
386         if (f->fd == -1) {
387                 char buf[FIO_VERROR_SIZE];
388                 int __e = errno;
389
390                 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
391
392                 td_verror(td, __e, buf);
393                 if (__e == EINVAL && td->o.odirect)
394                         log_err("fio: destination does not support O_DIRECT\n");
395                 if (__e == EMFILE)
396                         log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->o.nr_files);
397                 return 1;
398         }
399
400         if (get_file_size(td, f))
401                 goto err;
402
403         if (td->o.invalidate_cache && file_invalidate_cache(td, f))
404                 goto err;
405
406         if (!td_random(td)) {
407                 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
408                         td_verror(td, errno, "fadvise");
409                         goto err;
410                 }
411         } else {
412                 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
413                         td_verror(td, errno, "fadvise");
414                         goto err;
415                 }
416         }
417
418         return 0;
419 err:
420         close(f->fd);
421         return 1;
422 }
423
424 int open_files(struct thread_data *td)
425 {
426         struct fio_file *f;
427         unsigned int i;
428         int err = 0;
429
430         for_each_file(td, f, i) {
431                 err = td_io_open_file(td, f);
432                 if (err)
433                         break;
434
435                 if (td->o.open_files == td->nr_open_files)
436                         break;
437         }
438
439         if (!err)
440                 return 0;
441
442         for_each_file(td, f, i)
443                 td_io_close_file(td, f);
444
445         return err;
446 }
447
448 int setup_files(struct thread_data *td)
449 {
450         struct fio_file *f;
451         unsigned int i;
452         int err;
453
454         /*
455          * if ioengine defines a setup() method, it's responsible for
456          * setting up everything in the td->files[] area.
457          */
458         if (td->io_ops->setup)
459                 return td->io_ops->setup(td);
460
461         if (create_files(td))
462                 return 1;
463
464         err = open_files(td);
465         if (err)
466                 return err;
467
468         /*
469          * Recalculate the total file size now that files are set up.
470          */
471         td->o.size = 0;
472         for_each_file(td, f, i)
473                 td->o.size += f->file_size;
474
475         td->io_size = td->o.size;
476         if (td->io_size == 0) {
477                 log_err("%s: no io blocks\n", td->o.name);
478                 td_verror(td, EINVAL, "total_file_size");
479                 return 1;
480         }
481
482         if (!td->o.zone_size)
483                 td->o.zone_size = td->io_size;
484
485         td->total_io_size = td->io_size * td->o.loops;
486
487         for_each_file(td, f, i)
488                 td_io_close_file(td, f);
489
490         return err;
491 }
492
493 int init_random_map(struct thread_data *td)
494 {
495         int num_maps, blocks;
496         struct fio_file *f;
497         unsigned int i;
498
499         if (td->o.norandommap)
500                 return 0;
501
502         for_each_file(td, f, i) {
503                 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs;
504                 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
505                 f->file_map = malloc(num_maps * sizeof(long));
506                 if (!f->file_map) {
507                         log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
508                         return 1;
509                 }
510                 f->num_maps = num_maps;
511                 memset(f->file_map, 0, num_maps * sizeof(long));
512         }
513
514         return 0;
515 }
516
517 void close_files(struct thread_data *td)
518 {
519         struct fio_file *f;
520         unsigned int i;
521
522         for_each_file(td, f, i) {
523                 if (!f->file_name && (f->flags & FIO_FILE_UNLINK) &&
524                     f->filetype == FIO_TYPE_FILE) {
525                         unlink(f->file_name);
526                         free(f->file_name);
527                         f->file_name = NULL;
528                 }
529
530                 td_io_close_file(td, f);
531
532                 if (f->file_map) {
533                         free(f->file_map);
534                         f->file_map = NULL;
535                 }
536         }
537
538         td->o.filename = NULL;
539         td->files = NULL;
540         td->o.nr_files = 0;
541 }
542
543 static void get_file_type(struct fio_file *f)
544 {
545         struct stat sb;
546
547         f->filetype = FIO_TYPE_FILE;
548
549         if (!lstat(f->file_name, &sb)) {
550                 f->flags |= FIO_FILE_EXISTS;
551
552                 if (S_ISBLK(sb.st_mode))
553                         f->filetype = FIO_TYPE_BD;
554                 else if (S_ISCHR(sb.st_mode))
555                         f->filetype = FIO_TYPE_CHAR;
556                 else {
557                         /*
558                          * might as well do this here, and save a stat later on
559                          */
560                         f->real_file_size = sb.st_size;
561                         f->file_size = f->real_file_size;
562                 }
563         }
564 }
565
566 void add_file(struct thread_data *td, const char *fname)
567 {
568         int cur_files = td->files_index;
569         struct fio_file *f;
570
571         td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
572
573         f = &td->files[cur_files];
574         memset(f, 0, sizeof(*f));
575         f->fd = -1;
576         f->file_name = strdup(fname);
577
578         get_file_type(f);
579
580         td->files_index++;
581         if (f->filetype == FIO_TYPE_FILE)
582                 td->nr_normal_files++;
583 }
584
585 void get_file(struct fio_file *f)
586 {
587         f->references++;
588 }
589
590 void put_file(struct thread_data *td, struct fio_file *f)
591 {
592         if (!(f->flags & FIO_FILE_OPEN))
593                 return;
594
595         assert(f->references);
596         if (--f->references)
597                 return;
598
599         if (should_fsync(td) && td->o.fsync_on_close)
600                 fsync(f->fd);
601
602         if (td->io_ops->close_file)
603                 td->io_ops->close_file(td, f);
604         td->nr_open_files--;
605         f->flags &= ~FIO_FILE_OPEN;
606 }
607
608 static int recurse_dir(struct thread_data *td, const char *dirname)
609 {
610         struct dirent *dir;
611         int ret = 0;
612         DIR *D;
613
614         D = opendir(dirname);
615         if (!D) {
616                 td_verror(td, errno, "opendir");
617                 return 1;
618         }
619
620         while ((dir = readdir(D)) != NULL) {
621                 char full_path[PATH_MAX];
622                 struct stat sb;
623
624                 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
625                         continue;
626
627                 sprintf(full_path, "%s/%s", dirname, dir->d_name);
628
629                 if (lstat(full_path, &sb) == -1) {
630                         if (errno != ENOENT) {
631                                 td_verror(td, errno, "stat");
632                                 return 1;
633                         }
634                 }
635
636                 if (S_ISREG(sb.st_mode)) {
637                         add_file(td, full_path);
638                         td->o.nr_files++;
639                         continue;
640                 }
641
642                 if ((ret = recurse_dir(td, full_path)) != 0)
643                         break;
644         }
645
646         closedir(D);
647         return ret;
648 }
649
650 int add_dir_files(struct thread_data *td, const char *path)
651 {
652         return recurse_dir(td, path);
653 }