From 22a57ba81de2c3f458797e9158da760c9e0ea435 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Tue, 9 Jun 2009 14:34:35 +0200 Subject: [PATCH 1/1] Fix problem with too many opened files If using raw block devices, we would leak the fd from the bdev_size() function. Also add some debug triggers to catch this in the future. Signed-off-by: Jens Axboe --- filesetup.c | 11 +++++++++-- fio.c | 8 ++++++-- ioengines.c | 10 +++++++--- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/filesetup.c b/filesetup.c index 57928133..15c43160 100644 --- a/filesetup.c +++ b/filesetup.c @@ -246,6 +246,7 @@ static int bdev_size(struct thread_data *td, struct fio_file *f) } f->real_file_size = bytes; + td->io_ops->close_file(td, f); return 0; err: td->io_ops->close_file(td, f); @@ -726,7 +727,9 @@ void close_and_free_files(struct thread_data *td) unlink(f->file_name); } - td_io_close_file(td, f); + if (fio_file_open(f)) + td_io_close_file(td, f); + remove_file_hash(f); sfree(f->file_name); @@ -847,8 +850,10 @@ int put_file(struct thread_data *td, struct fio_file *f) dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references); - if (!fio_file_open(f)) + if (!fio_file_open(f)) { + assert(f->fd == -1); return 0; + } assert(f->references); if (--f->references) @@ -865,6 +870,7 @@ int put_file(struct thread_data *td, struct fio_file *f) td->nr_open_files--; fio_file_clear_open(f); + assert(f->fd == -1); return ret; } @@ -1004,6 +1010,7 @@ void dup_files(struct thread_data *td, struct thread_data *org) log_err("fio: smalloc OOM\n"); assert(0); } + __f->fd = -1; if (f->file_name) { __f->file_name = smalloc_strdup(f->file_name); diff --git a/fio.c b/fio.c index 70d4b2b1..6464b6d9 100644 --- a/fio.c +++ b/fio.c @@ -1368,8 +1368,12 @@ static void run_threads(void) * its own files. so close them, if we opened them * for creation */ - for_each_file(td, f, i) - td_io_close_file(td, f); + for_each_file(td, f, i) { + if (fio_file_open(f)) + td_io_close_file(td, f); + else + assert(f->fd == -1); + } } init_disk_util(td); diff --git a/ioengines.c b/ioengines.c index 51cf1ba4..230e846b 100644 --- a/ioengines.c +++ b/ioengines.c @@ -318,6 +318,9 @@ int td_io_commit(struct thread_data *td) int td_io_open_file(struct thread_data *td, struct fio_file *f) { + assert(!fio_file_open(f)); + assert(f->fd == -1); + if (td->io_ops->open_file(td, f)) { if (td->error == EINVAL && td->o.odirect) log_err("fio: destination does not support O_DIRECT\n"); @@ -327,6 +330,8 @@ int td_io_open_file(struct thread_data *td, struct fio_file *f) td->o.nr_files); } + assert(f->fd == -1); + assert(!fio_file_open(f)); return 1; } @@ -366,9 +371,6 @@ int td_io_open_file(struct thread_data *td, struct fio_file *f) } } - if (f->file_map) - memset(f->file_map, 0, f->num_maps * sizeof(int)); - #ifdef FIO_OS_DIRECTIO /* * Some OS's have a distinct call to mark the file non-buffered, @@ -396,6 +398,8 @@ err: int td_io_close_file(struct thread_data *td, struct fio_file *f) { + assert(f->references); + if (!fio_file_closing(f)) log_file(td, f, FIO_LOG_CLOSE_FILE); -- 2.25.1