Skip to content

Commit

Permalink
make lock scope of size_ in cache smaller
Browse files Browse the repository at this point in the history
  • Loading branch information
weicao committed Mar 20, 2013
1 parent 4ee185c commit c152e6d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/cache/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -437,8 +442,11 @@ void Cache::write_back()
}
}
}

ScopedMutex size_lock(&size_mtx_);
// update size
size_ = total_size;
size_lock.unlock();

vector<Node*> flushed_nodes;
size_t flushed_size = 0;
Expand Down
9 changes: 6 additions & 3 deletions src/cache/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,12 @@ class Cache {

Mutex tables_mtx_;
std::map<std::string, TableSettings> 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:
Expand All @@ -109,6 +110,8 @@ class Cache {
bid_t nid;
};

Mutex nodes_mtx_;

std::map<CacheKey, Node*> nodes_;

// ensure there is only one thread is doing evict/flush
Expand Down

0 comments on commit c152e6d

Please sign in to comment.