diff options
| author | Mason Reed <mason@vector35.com> | 2025-04-14 17:17:42 -0400 |
|---|---|---|
| committer | Mason Reed <mason@vector35.com> | 2025-04-14 20:45:14 -0400 |
| commit | 8c3bd6e14eafe4b1d0af509c7abc7e6bd9a97c15 (patch) | |
| tree | f977f4191e9ef94e02c809f52734940da32da247 /view/sharedcache/core/SharedCacheController.cpp | |
| parent | 9b7b429106f16b5704cf27baf9f3d69ff4a0857a (diff) | |
[SharedCache] Evict file accessors from the global file accessor cache on file metadata destruction
Previously we would just let them continue to exist after view destruction, this is fine assuming we expect the user to be re-opening the same cache over and over, but
if a user wants to load a bunch of different caches they might want to evict the old file accessors so that COW pages are known to be evicted, along with other paged in memory.
Diffstat (limited to 'view/sharedcache/core/SharedCacheController.cpp')
| -rw-r--r-- | view/sharedcache/core/SharedCacheController.cpp | 46 |
1 files changed, 28 insertions, 18 deletions
diff --git a/view/sharedcache/core/SharedCacheController.cpp b/view/sharedcache/core/SharedCacheController.cpp index e0cd036f..6e275b4b 100644 --- a/view/sharedcache/core/SharedCacheController.cpp +++ b/view/sharedcache/core/SharedCacheController.cpp @@ -13,39 +13,49 @@ std::shared_mutex GlobalControllersMutex; std::map<ViewId, DSCRef<SharedCacheController>>& GlobalControllers() { // To make initialization order consistent we place the static in a function. - static std::map<ViewId, DSCRef<SharedCacheController>> g_dscViews = {}; - return g_dscViews; + static std::map<ViewId, DSCRef<SharedCacheController>> g_controllers = {}; + return g_controllers; } -ViewId GetViewIdFromView(BinaryView& view) +ViewId GetViewIdFromFileMetadata(const FileMetadata& file) { // Currently the view id is just the views session id. // NOTE: If we want more than one shared cache controller per view we would need to make this more unique. - return view.GetFile()->GetSessionId(); + return file.GetSessionId(); } -void DeleteController(BinaryView& view) +void DeleteController(const FileMetadata& file) { - const auto id = GetViewIdFromView(view); + const auto id = GetViewIdFromFileMetadata(file); std::unique_lock<std::shared_mutex> lock(GlobalControllersMutex); - auto& dscViews = GlobalControllers(); - if (auto it = dscViews.find(id); it != dscViews.end()) + auto& controllers = GlobalControllers(); + if (auto it = controllers.find(id); it != controllers.end()) { + auto controller = it->second; // Someone is still holding the controller, lets warn about this. - if (it->second->m_refs > 1) + if (controller->m_refs > 1) LogWarn("Deleting SharedCacheController for view %llx, but there are still %d references", id, - it->second->m_refs.load()); - dscViews.erase(it); - LogDebug("Deleted SharedCacheController for view %s", view.GetFile()->GetFilename().c_str()); + controller->m_refs.load()); + + // Go through the file accessor cache and remove the entries we reference. + auto& fileAccessorCache = FileAccessorCache::Global(); + for (const auto& entry : controller->GetCache().GetEntries()) + { + auto accessorId = GetCacheAccessorID(entry.GetFilePath()); + fileAccessorCache.RemoveAccessor(accessorId); + } + + controllers.erase(it); + LogDebug("Deleted SharedCacheController for view %s", file.GetFilename().c_str()); } } void RegisterSharedCacheControllerDestructor() { BNObjectDestructionCallbacks callbacks = {}; - callbacks.destructBinaryView = [](void* ctx, BNBinaryView* obj) -> void { - auto view = BinaryView(obj); - DeleteController(view); + callbacks.destructFileMetadata = [](void* ctx, BNFileMetadata* obj) -> void { + const auto file = FileMetadata(obj); + DeleteController(file); }; BNRegisterObjectDestructionCallbacks(&callbacks); } @@ -63,7 +73,7 @@ SharedCacheController::SharedCacheController(SharedCache&& cache, Ref<Logger> lo DSCRef<SharedCacheController> SharedCacheController::Initialize(BinaryView& view, SharedCache&& cache) { - auto id = GetViewIdFromView(view); + auto id = GetViewIdFromFileMetadata(*view.GetFile()); std::unique_lock<std::shared_mutex> lock(GlobalControllersMutex); auto logger = new Logger("SharedCache.Controller", view.GetFile()->GetSessionId()); DSCRef<SharedCacheController> controller = new SharedCacheController(std::move(cache), logger); @@ -94,9 +104,9 @@ DSCRef<SharedCacheController> SharedCacheController::Initialize(BinaryView& view return controller; } -DSCRef<SharedCacheController> SharedCacheController::FromView(BinaryView& view) +DSCRef<SharedCacheController> SharedCacheController::FromView(const BinaryView& view) { - auto id = GetViewIdFromView(view); + auto id = GetViewIdFromFileMetadata(*view.GetFile()); std::shared_lock<std::shared_mutex> lock(GlobalControllersMutex); auto& dscViews = GlobalControllers(); auto dscView = dscViews.find(id); |
