From c152e6d01d67d57363b800b821fcb9af4720cb61 Mon Sep 17 00:00:00 2001 From: Wei Cao Date: Wed, 20 Mar 2013 16:09:40 +0800 Subject: [PATCH] make lock scope of size_ in cache smaller --- src/cache/cache.cpp | 12 ++++++++++-- src/cache/cache.h | 9 ++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/cache/cache.cpp b/src/cache/cache.cpp index cff9e34..9673b1b 100644 --- a/src/cache/cache.cpp +++ b/src/cache/cache.cpp @@ -254,14 +254,14 @@ bool Cache::get_table_settings(const std::string& tbn, TableSettings& tbs) bool Cache::must_evict() { - ScopedMutex lock(&nodes_mtx_); + ScopedMutex lock(&size_mtx_); return size_ >= options_.cache_limit; } bool Cache::need_evict() { - ScopedMutex lock(&nodes_mtx_); + ScopedMutex lock(&size_mtx_); size_t threshold = (options_.cache_limit * options_.cache_evict_high_watermark) / 100; @@ -328,8 +328,11 @@ void Cache::evict() } } } + + ScopedMutex size_lock(&size_mtx_); // update size size_ = total_size; + size_lock.unlock(); LRUComparator comp; sort(clean_nodes.begin(), clean_nodes.end(), comp); @@ -358,9 +361,11 @@ void Cache::evict() delete node; } + size_lock.lock(); // update size assert(size_ >= evicted_size); size_ -= evicted_size; + size_lock.unlock(); lock.unlock(); @@ -437,8 +442,11 @@ void Cache::write_back() } } } + + ScopedMutex size_lock(&size_mtx_); // update size size_ = total_size; + size_lock.unlock(); vector flushed_nodes; size_t flushed_size = 0; diff --git a/src/cache/cache.h b/src/cache/cache.h index 29776d7..4e6281b 100644 --- a/src/cache/cache.h +++ b/src/cache/cache.h @@ -88,11 +88,12 @@ class Cache { Mutex tables_mtx_; std::map tables_; - - Mutex nodes_mtx_; + + // TODO make me atomic + Mutex size_mtx_; // total memory size occupied by nodes, // updated everytime the flusher thread runs - size_t size_; + size_t size_; class CacheKey { public: @@ -109,6 +110,8 @@ class Cache { bid_t nid; }; + Mutex nodes_mtx_; + std::map nodes_; // ensure there is only one thread is doing evict/flush