From 671120415f74140cb1ccc7e6148c21a1b4a05962 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 15 Jun 2022 12:18:28 -0700 Subject: [PATCH] ircd::db::database::env: Reduce file size system calls with cache pattern. --- ircd/db_env.cc | 39 ++++++++++++++++++++++++++++++++++----- ircd/db_env.h | 1 + 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/ircd/db_env.cc b/ircd/db_env.cc index 7be6f2b73..9e7fb8b31 100644 --- a/ircd/db_env.cc +++ b/ircd/db_env.cc @@ -1348,6 +1348,8 @@ noexcept try }; #endif + assert(logical_size == -1UL || logical_size == fs::size(fd)); + fd = fs::fd{}; return Status::OK(); } @@ -1617,6 +1619,7 @@ noexcept try wopts.nodelay = nodelay; wopts.interruptible = false; fs::truncate(fd, size, wopts); + logical_size = size; return Status::OK(); } catch(const std::system_error &e) @@ -1668,6 +1671,9 @@ noexcept try }; #endif + if(likely(logical_size != -1UL) && offset + length > logical_size) + logical_size = -1UL; + if(opts.direct) return Status::OK(); @@ -1732,7 +1738,14 @@ noexcept try data(s), size(s) }; - fs::append(fd, buf, wopts); + const const_buffer appended + { + fs::append(fd, buf, wopts) + }; + + if(likely(logical_size != -1UL)) + logical_size += size(appended); + return Status::OK(); } catch(const std::system_error &e) @@ -1798,7 +1811,19 @@ noexcept try data(s), size(s) }; - fs::append(fd, buf, wopts); + const const_buffer appended + { + fs::append(fd, buf, wopts) + }; + + const auto append_break + { + offset + size(appended) + }; + + if(likely(logical_size != -1UL) && append_break > logical_size) + logical_size = append_break; + return Status::OK(); } catch(const std::system_error &e) @@ -2042,14 +2067,18 @@ noexcept try #ifdef RB_DEBUG_DB_ENV log::debug { - log, "[%s] wfile:%p fd:%d get file size", + log, "[%s] wfile:%p fd:%d get file size; cached:%zd", d.name, this, - int(fd) + int(fd), + logical_size, }; #endif - return fs::size(fd); + if(logical_size == -1UL) + logical_size = fs::size(fd); + + return logical_size; } catch(const std::exception &e) { diff --git a/ircd/db_env.h b/ircd/db_env.h index 66fad8973..266c43a83 100644 --- a/ircd/db_env.h +++ b/ircd/db_env.h @@ -227,6 +227,7 @@ ircd::db::database::env::writable_file bool nodelay {false}; WriteLifeTimeHint hint {WriteLifeTimeHint::WLTH_NOT_SET}; fs::fd fd; + size_t logical_size {-1UL}; size_t preallocation_block_size {0}; ssize_t preallocation_last_block {-1};