From 2d867a9dd68cffa4bdc805df1d5cb2cbb9217cd1 Mon Sep 17 00:00:00 2001 From: Mathias Agopian Date: Tue, 21 Jan 2025 16:15:45 -0800 Subject: [PATCH] Handle tags now work with heap allocated handles (#8370) - uninlined getHandleTag so that the code is not duplicated when multiple backends are supported (e.g. GLES and Vulkan) - added a lock for mDebugTags because, even if rare, it technically can be accessed from multiple threads. This would happen only on a failure. There should never be any contention on this lock. --- .../include/private/backend/HandleAllocator.h | 44 ++++++++++--------- filament/backend/src/HandleAllocator.cpp | 42 ++++++++++++++++-- 2 files changed, 63 insertions(+), 23 deletions(-) diff --git a/filament/backend/include/private/backend/HandleAllocator.h b/filament/backend/include/private/backend/HandleAllocator.h index 9333336302..97ce6feeb4 100644 --- a/filament/backend/include/private/backend/HandleAllocator.h +++ b/filament/backend/include/private/backend/HandleAllocator.h @@ -31,6 +31,7 @@ #include #include +#include #include #include #include @@ -44,11 +45,26 @@ namespace filament::backend { +// This is used to not duplicate the code for the Tags management +class DebugTag { +public: + DebugTag(); + void writeHandleTag(HandleBase::HandleId key, utils::CString&& tag) noexcept; + utils::CString findHandleTag(HandleBase::HandleId key) const noexcept; + +private: + // This is used to associate a tag to a handle. mDebugTags is only written the in the main + // driver thread, but it can be accessed from any thread, because it's called from handle_cast<> + // which is used by synchronous calls. + mutable utils::Mutex mDebugTagLock; + tsl::robin_map mDebugTags; +}; + /* * A utility class to efficiently allocate and manage Handle<> */ template -class HandleAllocator { +class HandleAllocator : public DebugTag { public: HandleAllocator(const char* name, size_t size, bool disableUseAfterFreeCheck) noexcept; HandleAllocator(HandleAllocator const& rhs) = delete; @@ -200,6 +216,8 @@ public: return static_cast(p); } + utils::CString getHandleTag(HandleBase::HandleId key) const noexcept; + template bool is_valid(Handle& handle) { if (!handle) { @@ -227,24 +245,12 @@ public: void associateTagToHandle(HandleBase::HandleId id, utils::CString&& tag) noexcept { // TODO: for now, only pool handles check for use-after-free, so we only keep tags for // those - if (isPoolHandle(id)) { + uint32_t key = id; + if (UTILS_LIKELY(isPoolHandle(id))) { // Truncate the age to get the debug tag - uint32_t const key = id & ~(HANDLE_DEBUG_TAG_MASK ^ HANDLE_AGE_MASK); - // This line is the costly part. In the future, we could potentially use a custom - // allocator. - mDebugTags[key] = std::move(tag); + key &= ~(HANDLE_DEBUG_TAG_MASK ^ HANDLE_AGE_MASK); } - } - - utils::CString getHandleTag(HandleBase::HandleId id) const noexcept { - if (!isPoolHandle(id)) { - return "(no tag)"; - } - uint32_t const key = id & ~(HANDLE_DEBUG_TAG_MASK ^ HANDLE_AGE_MASK); - if (auto pos = mDebugTags.find(key); pos != mDebugTags.end()) { - return pos->second; - } - return "(no tag)"; + writeHandleTag(key, std::move(tag)); } private: @@ -347,9 +353,8 @@ private: if (UTILS_LIKELY(p)) { uint32_t const tag = (uint32_t(age) << HANDLE_AGE_SHIFT) & HANDLE_AGE_MASK; return arenaPointerToHandle(p, tag); - } else { - return allocateHandleSlow(SIZE); } + return allocateHandleSlow(SIZE); } template @@ -421,7 +426,6 @@ private: // Below is only used when running out of space in the HandleArena mutable utils::Mutex mLock; tsl::robin_map mOverflowMap; - tsl::robin_map mDebugTags; HandleBase::HandleId mId = 0; bool mUseAfterFreeCheckDisabled = false; }; diff --git a/filament/backend/src/HandleAllocator.cpp b/filament/backend/src/HandleAllocator.cpp index 6cdcaa4af4..d1d9440af1 100644 --- a/filament/backend/src/HandleAllocator.cpp +++ b/filament/backend/src/HandleAllocator.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -29,7 +30,9 @@ #include #include #include +#include +#include #include #include @@ -80,9 +83,6 @@ HandleAllocator::HandleAllocator(const char* name, size_t size, bool disableUseAfterFreeCheck) noexcept : mHandleArena(name, size, disableUseAfterFreeCheck), mUseAfterFreeCheckDisabled(disableUseAfterFreeCheck) { - // Reserve initial space for debug tags. This prevents excessive calls to malloc when the first - // few tags are set. - mDebugTags.reserve(512); } template @@ -147,6 +147,42 @@ void HandleAllocator::deallocateHandleSlow(HandleBase::HandleId id, ::free(p); } +template +UTILS_NOINLINE +CString HandleAllocator::getHandleTag(HandleBase::HandleId id) const noexcept { + uint32_t key = id; + if (UTILS_LIKELY(isPoolHandle(id))) { + // Truncate the age to get the debug tag + key &= ~(HANDLE_DEBUG_TAG_MASK ^ HANDLE_AGE_MASK); + } + return findHandleTag(key); +} + +DebugTag::DebugTag() { + // Reserve initial space for debug tags. This prevents excessive calls to malloc when the first + // few tags are set. + mDebugTags.reserve(512); +} + +UTILS_NOINLINE +CString DebugTag::findHandleTag(HandleBase::HandleId key) const noexcept { + std::unique_lock const lock(mDebugTagLock); + if (auto pos = mDebugTags.find(key); pos != mDebugTags.end()) { + return pos->second; + } + return "(no tag)"; +} + +UTILS_NOINLINE +void DebugTag::writeHandleTag(HandleBase::HandleId key, CString&& tag) noexcept { + // This line is the costly part. In the future, we could potentially use a custom + // allocator. + std::unique_lock const lock(mDebugTagLock); + // Pool based tags will be recycled after a certain age. Heap-based tag will never be recycled, therefore, + // this can grow indefinitely, once we're in the slow mode. + mDebugTags[key] = std::move(tag); +} + // Explicit template instantiations. #if defined (FILAMENT_SUPPORTS_OPENGL) template class HandleAllocatorGL;