From db2bee546354a03ca9a94a647161b041b3033491 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Thu, 28 Apr 2022 15:32:55 +0200 Subject: Reduce risk of reallocating backing std::vector in CbWriter::AddBinary Shard up g_MappingLock in IoBufferExtendedCore::Materialize() to reduce contention during high load Don't queue upstream cache records if we don't have any upstreams --- zencore/iobuffer.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'zencore/iobuffer.cpp') diff --git a/zencore/iobuffer.cpp b/zencore/iobuffer.cpp index 8a3ab8427..c069aa0f1 100644 --- a/zencore/iobuffer.cpp +++ b/zencore/iobuffer.cpp @@ -226,7 +226,15 @@ IoBufferExtendedCore::~IoBufferExtendedCore() m_DataPtr = nullptr; } -static RwLock g_MappingLock; +static RwLock g_MappingLock[0x40]; + +static RwLock& +MappingLockForInstance(const IoBufferExtendedCore* instance) +{ + intptr_t base = (intptr_t)instance; + size_t lock_index = ((base >> 8) ^ (base >> 16)) & 0x3f; + return g_MappingLock[lock_index]; +} void IoBufferExtendedCore::Materialize() const @@ -237,7 +245,7 @@ IoBufferExtendedCore::Materialize() const if (m_Flags.load(std::memory_order_acquire) & kIsMaterialized) return; - RwLock::ExclusiveLockScope _(g_MappingLock); + RwLock::ExclusiveLockScope _(MappingLockForInstance(this)); // Someone could have gotten here first // We can use memory_order_relaxed on this load because the mutex has already provided the fence -- cgit v1.2.3 From 042ebaa2822400d8cab69c51126f61f131ecfc8e Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Thu, 28 Apr 2022 16:56:32 +0200 Subject: naming cleanup --- zencore/iobuffer.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'zencore/iobuffer.cpp') diff --git a/zencore/iobuffer.cpp b/zencore/iobuffer.cpp index c069aa0f1..ccf92da62 100644 --- a/zencore/iobuffer.cpp +++ b/zencore/iobuffer.cpp @@ -226,14 +226,17 @@ IoBufferExtendedCore::~IoBufferExtendedCore() m_DataPtr = nullptr; } -static RwLock g_MappingLock[0x40]; +static constexpr size_t MappingLockCount = 64; +static_assert((MappingLockCount & (MappingLockCount - 1)) == 0, "MappingLockCount must be power of two"); + +static RwLock g_MappingLocks[MappingLockCount]; static RwLock& MappingLockForInstance(const IoBufferExtendedCore* instance) { intptr_t base = (intptr_t)instance; - size_t lock_index = ((base >> 8) ^ (base >> 16)) & 0x3f; - return g_MappingLock[lock_index]; + size_t lock_index = ((base >> 8) ^ (base >> 16)) & (MappingLockCount - 1u); + return g_MappingLocks[lock_index]; } void -- cgit v1.2.3 From f9f58ce2f568d8e3d39acad746242bbe340f68cc Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Fri, 29 Apr 2022 08:52:01 +0200 Subject: use IsPow2 for mapping lock count --- zencore/iobuffer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zencore/iobuffer.cpp') diff --git a/zencore/iobuffer.cpp b/zencore/iobuffer.cpp index ccf92da62..46b9ab336 100644 --- a/zencore/iobuffer.cpp +++ b/zencore/iobuffer.cpp @@ -227,7 +227,7 @@ IoBufferExtendedCore::~IoBufferExtendedCore() } static constexpr size_t MappingLockCount = 64; -static_assert((MappingLockCount & (MappingLockCount - 1)) == 0, "MappingLockCount must be power of two"); +static_assert(IsPow2(MappingLockCount), "MappingLockCount must be power of two"); static RwLock g_MappingLocks[MappingLockCount]; -- cgit v1.2.3 From 308d60e0289b2adc5c0738fe25273176e780735f Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Sun, 8 May 2022 00:11:11 +0200 Subject: Make sure blockstore owner and block store state does not get out of sync when fetching a chunk Move MarkAsDeleteOnClose() to IoBuffer(ExtendedCore) and set it on close, SetFileInformationByHandle sometimes fails if done in parallel with FileMapping --- zencore/iobuffer.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'zencore/iobuffer.cpp') diff --git a/zencore/iobuffer.cpp b/zencore/iobuffer.cpp index 46b9ab336..c4b7f7bdf 100644 --- a/zencore/iobuffer.cpp +++ b/zencore/iobuffer.cpp @@ -211,6 +211,18 @@ IoBufferExtendedCore::~IoBufferExtendedCore() if (LocalFlags & kOwnsFile) { + if (m_DeleteOnClose) + { +#if ZEN_PLATFORM_WINDOWS + // Mark file for deletion when final handle is closed + FILE_DISPOSITION_INFO Fdi{.DeleteFile = TRUE}; + + SetFileInformationByHandle(m_FileHandle, FileDispositionInfo, &Fdi, sizeof Fdi); +#else + std::filesystem::path FilePath = zen::PathFromHandle(m_FileHandle); + unlink(FilePath.c_str()); +#endif + } #if ZEN_PLATFORM_WINDOWS BOOL Success = CloseHandle(m_FileHandle); #else @@ -298,6 +310,9 @@ IoBufferExtendedCore::Materialize() const if (MappedBase == nullptr) { +#if ZEN_PLATFORM_WINDOWS + CloseHandle(NewMmapHandle); +#endif // ZEN_PLATFORM_WINDOWS throw std::system_error(std::error_code(zen::GetLastError(), std::system_category()), fmt::format("MapViewOfFile failed (offset {:#x}, size {:#x}) file: '{}'", MapOffset, @@ -327,6 +342,12 @@ IoBufferExtendedCore::GetFileReference(IoBufferFileReference& OutRef) const return true; } +void +IoBufferExtendedCore::MarkAsDeleteOnClose() +{ + m_DeleteOnClose = true; +} + ////////////////////////////////////////////////////////////////////////// IoBuffer::IoBuffer(size_t InSize) : m_Core(new IoBufferCore(InSize)) @@ -389,6 +410,15 @@ IoBuffer::GetFileReference(IoBufferFileReference& OutRef) const return false; } +void +IoBuffer::MarkAsDeleteOnClose() +{ + if (IoBufferExtendedCore* ExtCore = m_Core->ExtendedCore()) + { + ExtCore->MarkAsDeleteOnClose(); + } +} + ////////////////////////////////////////////////////////////////////////// IoBuffer -- cgit v1.2.3