[PATCH] Catch processes/threads that unexpectedly exited
[fio.git] / filesetup.c
1 #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
11 /*
12  * Check if the file exists and it's large enough.
13  */
14 static int file_ok(struct thread_data *td, struct fio_file *f)
15 {
16         struct stat st;
17
18         if (td->filetype != FIO_TYPE_FILE || (td->io_ops->flags & FIO_NULLIO))
19                 return 0;
20
21         if (stat(f->file_name, &st) == -1)
22                 return 1;
23         else if (st.st_size < (off_t) f->file_size)
24                 return 1;
25
26         return 0;
27 }
28
29 static int create_file(struct thread_data *td, struct fio_file *f)
30 {
31         unsigned long long left;
32         unsigned int bs;
33         char *b;
34         int r;
35
36         f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
37         if (f->fd < 0) {
38                 td_verror(td, errno);
39                 return 1;
40         }
41
42         if (ftruncate(f->fd, f->file_size) == -1) {
43                 td_verror(td, errno);
44                 goto err;
45         }
46
47         if (posix_fallocate(f->fd, 0, f->file_size) < 0) {
48                 td_verror(td, errno);
49                 goto err;
50         }
51
52         b = malloc(td->max_bs[DDIR_WRITE]);
53         memset(b, 0, td->max_bs[DDIR_WRITE]);
54
55         left = f->file_size;
56         while (left && !td->terminate) {
57                 bs = td->max_bs[DDIR_WRITE];
58                 if (bs > left)
59                         bs = left;
60
61                 r = write(f->fd, b, bs);
62
63                 if (r == (int) bs) {
64                         left -= bs;
65                         continue;
66                 } else {
67                         if (r < 0)
68                                 td_verror(td, errno);
69                         else
70                                 td_verror(td, EIO);
71
72                         break;
73                 }
74         }
75
76         if (td->terminate)
77                 unlink(f->file_name);
78         else if (td->create_fsync)
79                 fsync(f->fd);
80
81         free(b);
82         close(f->fd);
83         f->fd = -1;
84         return 0;
85 err:
86         close(f->fd);
87         f->fd = -1;
88         return 1;
89 }
90
91 static int create_files(struct thread_data *td)
92 {
93         struct fio_file *f;
94         int i, err, need_create;
95
96         for_each_file(td, f, i)
97                 f->file_size = td->total_file_size / td->nr_files;
98
99         /*
100          * unless specifically asked for overwrite, let normal io extend it
101          */
102         if (!td->overwrite)
103                 return 0;
104
105         need_create = 0;
106         if (td->filetype == FIO_TYPE_FILE) {
107                 for_each_file(td, f, i) {
108                         int file_there = !file_ok(td, f);
109
110                         if (file_there && td->ddir == DDIR_WRITE &&
111                             !td->overwrite) {
112                                 unlink(f->file_name);
113                                 file_there = 0;
114                         }
115
116                         need_create += !file_there;
117                 }
118         }
119
120         if (!need_create)
121                 return 0;
122
123         if (!td->total_file_size) {
124                 log_err("Need size for create\n");
125                 td_verror(td, EINVAL);
126                 return 1;
127         }
128
129         temp_stall_ts = 1;
130         fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
131                                 td->name, td->nr_uniq_files,
132                                 (td->total_file_size >> 20) / td->nr_uniq_files,
133                                 td->total_file_size >> 20);
134
135         err = 0;
136         for_each_file(td, f, i) {
137                 /*
138                  * Only unlink files that we created.
139                  */
140                 f->unlink = 0;
141                 if (file_ok(td, f)) {
142                         f->unlink = td->unlink;
143                         err = create_file(td, f);
144                         if (err)
145                                 break;
146                 }
147         }
148
149         temp_stall_ts = 0;
150         return err;
151 }
152
153 static int file_size(struct thread_data *td, struct fio_file *f)
154 {
155         struct stat st;
156
157         /*
158          * if we are not doing real io, just pretend the file is as large
159          * as the size= given. this works fine with nrfiles > 1 as well,
160          * we only really care about it being at least as big as size=
161          */
162         if (td->io_ops->flags & FIO_NULLIO) {
163                 f->real_file_size = f->file_size = td->total_file_size;
164                 return 0;
165         }
166
167         if (td->overwrite) {
168                 if (fstat(f->fd, &st) == -1) {
169                         td_verror(td, errno);
170                         return 1;
171                 }
172
173                 f->real_file_size = st.st_size;
174
175                 if (!f->file_size || f->file_size > f->real_file_size)
176                         f->file_size = f->real_file_size;
177         } else
178                 f->real_file_size = f->file_size;
179
180         return 0;
181 }
182
183 static int bdev_size(struct thread_data *td, struct fio_file *f)
184 {
185         unsigned long long bytes;
186         int r;
187
188         r = blockdev_size(f->fd, &bytes);
189         if (r) {
190                 td_verror(td, r);
191                 return 1;
192         }
193
194         f->real_file_size = bytes;
195
196         /*
197          * no extend possibilities, so limit size to device size if too large
198          */
199         if (!f->file_size || f->file_size > f->real_file_size)
200                 f->file_size = f->real_file_size;
201
202         f->file_size -= f->file_offset;
203         return 0;
204 }
205
206 static int get_file_size(struct thread_data *td, struct fio_file *f)
207 {
208         int ret = 0;
209
210         if (td->filetype == FIO_TYPE_FILE)
211                 ret = file_size(td, f);
212         else if (td->filetype == FIO_TYPE_BD)
213                 ret = bdev_size(td, f);
214         else
215                 f->real_file_size = -1;
216
217         if (ret)
218                 return ret;
219
220         if (f->file_offset > f->real_file_size) {
221                 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
222                 return 1;
223         }
224
225         return 0;
226 }
227
228 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
229 {
230         int ret = 0;
231
232         /*
233          * FIXME: add blockdev flushing too
234          */
235         if (td->io_ops->flags & FIO_MMAPIO)
236                 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
237         else if (td->filetype == FIO_TYPE_FILE)
238                 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
239         else if (td->filetype == FIO_TYPE_BD)
240                 ret = blockdev_invalidate_cache(f->fd);
241         else if (td->filetype == FIO_TYPE_CHAR)
242                 ret = 0;
243
244         if (ret < 0) {
245                 td_verror(td, errno);
246                 return 1;
247         }
248
249         return 0;
250 }
251
252 static int __setup_file_mmap(struct thread_data *td, struct fio_file *f)
253 {
254         int flags;
255
256         if (td_rw(td))
257                 flags = PROT_READ | PROT_WRITE;
258         else if (td_write(td)) {
259                 flags = PROT_WRITE;
260
261                 if (td->verify != VERIFY_NONE)
262                         flags |= PROT_READ;
263         } else
264                 flags = PROT_READ;
265
266         f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset);
267         if (f->mmap == MAP_FAILED) {
268                 f->mmap = NULL;
269                 td_verror(td, errno);
270                 return 1;
271         }
272
273         if (td->invalidate_cache && file_invalidate_cache(td, f))
274                 return 1;
275
276         if (td->sequential) {
277                 if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) {
278                         td_verror(td, errno);
279                         return 1;
280                 }
281         } else {
282                 if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) {
283                         td_verror(td, errno);
284                         return 1;
285                 }
286         }
287
288         return 0;
289 }
290
291 static int setup_files_mmap(struct thread_data *td)
292 {
293         struct fio_file *f;
294         int i, err = 0;
295
296         for_each_file(td, f, i) {
297                 err = __setup_file_mmap(td, f);
298                 if (err)
299                         break;
300         }
301
302         return err;
303 }
304
305 static int __setup_file_plain(struct thread_data *td, struct fio_file *f)
306 {
307         if (td->invalidate_cache && file_invalidate_cache(td, f))
308                 return 1;
309
310         if (td->sequential) {
311                 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
312                         td_verror(td, errno);
313                         return 1;
314                 }
315         } else {
316                 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
317                         td_verror(td, errno);
318                         return 1;
319                 }
320         }
321
322         return 0;
323 }
324
325 static int setup_files_plain(struct thread_data *td)
326 {
327         struct fio_file *f;
328         int i, err = 0;
329
330         for_each_file(td, f, i) {
331                 err = __setup_file_plain(td, f);
332                 if (err)
333                         break;
334         }
335
336         return err;
337 }
338
339 static int setup_file(struct thread_data *td, struct fio_file *f)
340 {
341         int flags = 0;
342
343         if (td->io_ops->flags & FIO_NETIO)
344                 return 0;
345
346         /*
347          * we need a valid file descriptor, but don't create a real file.
348          * lets just dup stdout, seems like a sensible approach.
349          */
350         if (td->io_ops->flags & FIO_NULLIO)
351                 f->fd = dup(STDOUT_FILENO);
352         else {
353                 if (td->odirect)
354                         flags |= OS_O_DIRECT;
355                 if (td->sync_io)
356                         flags |= O_SYNC;
357
358                 if (td_write(td) || td_rw(td)) {
359                         flags |= O_RDWR;
360
361                         if (td->filetype == FIO_TYPE_FILE) {
362                                 if (!td->overwrite)
363                                         flags |= O_TRUNC;
364
365                                 flags |= O_CREAT;
366                         }
367
368                         f->fd = open(f->file_name, flags, 0600);
369                 } else {
370                         if (td->filetype == FIO_TYPE_CHAR)
371                                 flags |= O_RDWR;
372                         else
373                                 flags |= O_RDONLY;
374
375                         f->fd = open(f->file_name, flags);
376                 }
377         }
378
379         if (f->fd == -1) {
380                 td_verror(td, errno);
381                 return 1;
382         }
383
384         if (get_file_size(td, f))
385                 return 1;
386
387         return 0;
388 }
389
390 int open_files(struct thread_data *td)
391 {
392         struct fio_file *f;
393         int i, err = 0;
394
395         for_each_file(td, f, i) {
396                 err = setup_file(td, f);
397                 if (err)
398                         break;
399         }
400
401         if (!err)
402                 return 0;
403
404         for_each_file(td, f, i) {
405                 if (f->fd != -1) {
406                         close(f->fd);
407                         f->fd = -1;
408                 }
409         }
410
411         return err;
412 }
413
414 int setup_files(struct thread_data *td)
415 {
416         struct fio_file *f;
417         int err, i;
418
419         /*
420          * if ioengine defines a setup() method, it's responsible for
421          * setting up everything in the td->files[] area.
422          */
423         if (td->io_ops->setup)
424                 return td->io_ops->setup(td);
425
426         if (create_files(td))
427                 return 1;
428
429         err = open_files(td);
430         if (err)
431                 return err;
432
433         /*
434          * Recalculate the total file size now that files are set up.
435          */
436         td->total_file_size = 0;
437         for_each_file(td, f, i)
438                 td->total_file_size += f->file_size;
439
440         td->io_size = td->total_file_size;
441         if (td->io_size == 0) {
442                 log_err("%s: no io blocks\n", td->name);
443                 td_verror(td, EINVAL);
444                 return 1;
445         }
446
447         if (!td->zone_size)
448                 td->zone_size = td->io_size;
449
450         td->total_io_size = td->io_size * td->loops;
451
452         if (td->io_ops->flags & FIO_MMAPIO)
453                 err = setup_files_mmap(td);
454         else
455                 err = setup_files_plain(td);
456
457         for_each_file(td, f, i) {
458                 if (f->fd != -1) {
459                         close(f->fd);
460                         f->fd = -1;
461                 }
462         }
463
464         return err;
465 }
466
467 void close_files(struct thread_data *td)
468 {
469         struct fio_file *f;
470         int i;
471
472         for_each_file(td, f, i) {
473                 if (!td->filename && f->unlink &&
474                     td->filetype == FIO_TYPE_FILE) {
475                         unlink(f->file_name);
476                         free(f->file_name);
477                         f->file_name = NULL;
478                 }
479                 if (f->fd != -1) {
480                         close(f->fd);
481                         f->fd = -1;
482                 }
483                 if (f->mmap) {
484                         munmap(f->mmap, f->file_size);
485                         f->mmap = NULL;
486                 }
487         }
488
489         td->filename = NULL;
490         free(td->files);
491         td->files = NULL;
492         td->nr_files = 0;
493 }