1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 #ifndef CEPH_SHAREDCACHE_H
16 #define CEPH_SHAREDCACHE_H
21 #include <boost/smart_ptr/local_shared_ptr.hpp>
25 #include "common/ceph_mutex.h"
26 #include "common/dout.h"
27 #include "include/unordered_map.h"
29 // re-include our assert to clobber the system one; fix dout:
30 #include "include/ceph_assert.h"
32 template <class K, class V>
36 using VPtr = boost::local_shared_ptr<V>;
37 using WeakVPtr = boost::weak_ptr<V>;
39 using VPtr = std::shared_ptr<V>;
40 using WeakVPtr = std::weak_ptr<V>;
44 ceph::condition_variable cond;
49 using C = std::less<K>;
50 using H = std::hash<K>;
51 ceph::unordered_map<K, typename std::list<std::pair<K, VPtr> >::iterator, H> contents;
52 std::list<std::pair<K, VPtr> > lru;
54 std::map<K, std::pair<WeakVPtr, V*>, C> weak_refs;
56 void trim_cache(std::list<VPtr> *to_release) {
57 while (size > max_size) {
58 to_release->push_back(lru.back().second);
59 lru_remove(lru.back().first);
63 void lru_remove(const K& key) {
64 auto i = contents.find(key);
65 if (i == contents.end())
72 void lru_add(const K& key, const VPtr& val, std::list<VPtr> *to_release) {
73 auto i = contents.find(key);
74 if (i != contents.end()) {
75 lru.splice(lru.begin(), lru, i->second);
78 lru.push_front(make_pair(key, val));
79 contents[key] = lru.begin();
80 trim_cache(to_release);
84 void remove(const K& key, V *valptr) {
85 std::lock_guard l{lock};
86 auto i = weak_refs.find(key);
87 if (i != weak_refs.end() && i->second.second == valptr) {
95 SharedLRU<K, V> *cache;
97 Cleanup(SharedLRU<K, V> *cache, K key) : cache(cache), key(key) {}
98 void operator()(V *ptr) {
99 cache->remove(key, ptr);
105 SharedLRU(CephContext *cct = NULL, size_t max_size = 20)
107 lock{ceph::make_mutex("SharedLRU::lock")},
109 size(0), waiting(0) {
110 contents.rehash(max_size);
116 if (!weak_refs.empty()) {
117 lderr(cct) << "leaked refs:\n";
118 dump_weak_refs(*_dout);
120 if (cct->_conf.get_val<bool>("debug_asserts_on_shutdown")) {
121 ceph_assert(weak_refs.empty());
127 std::lock_guard locker{lock};
131 void set_cct(CephContext *c) {
135 void dump_weak_refs() {
136 lderr(cct) << "leaked refs:\n";
137 dump_weak_refs(*_dout);
141 void dump_weak_refs(std::ostream& out) {
142 for (const auto& [key, ref] : weak_refs) {
143 out << __func__ << " " << this << " weak_refs: "
144 << key << " = " << ref.second
145 << " with " << ref.first.use_count() << " refs"
150 //clear all strong reference from the lru.
153 VPtr val; // release any ref we have after we drop the lock
154 std::lock_guard locker{lock};
158 val = lru.back().second;
159 lru_remove(lru.back().first);
163 void clear(const K& key) {
164 VPtr val; // release any ref we have after we drop the lock
166 std::lock_guard l{lock};
167 typename map<K, pair<WeakVPtr, V*>, C>::iterator i = weak_refs.find(key);
168 if (i != weak_refs.end()) {
169 val = i->second.first.lock();
175 void purge(const K &key) {
176 VPtr val; // release any ref we have after we drop the lock
178 std::lock_guard l{lock};
179 typename map<K, pair<WeakVPtr, V*>, C>::iterator i = weak_refs.find(key);
180 if (i != weak_refs.end()) {
181 val = i->second.first.lock();
188 void set_size(size_t new_size) {
189 list<VPtr> to_release;
191 std::lock_guard l{lock};
193 trim_cache(&to_release);
197 // Returns K key s.t. key <= k for all currently cached k,v
198 K cached_key_lower_bound() {
199 std::lock_guard l{lock};
200 return weak_refs.begin()->first;
203 VPtr lower_bound(const K& key) {
205 list<VPtr> to_release;
207 std::unique_lock l{lock};
209 cond.wait(l, [this, &key, &val, &to_release] {
210 if (weak_refs.empty()) {
213 auto i = weak_refs.lower_bound(key);
214 if (i == weak_refs.end()) {
217 if (val = i->second.first.lock(); val) {
218 lru_add(i->first, val, &to_release);
228 bool get_next(const K &key, std::pair<K, VPtr> *next) {
229 std::pair<K, VPtr> r;
231 std::lock_guard l{lock};
233 typename std::map<K, std::pair<WeakVPtr, V*>, C>::iterator i = weak_refs.upper_bound(key);
235 while (i != weak_refs.end() &&
236 !(next_val = i->second.first.lock()))
239 if (i == weak_refs.end())
243 r = make_pair(i->first, next_val);
249 bool get_next(const K &key, std::pair<K, V> *next) {
250 std::pair<K, VPtr> r;
251 bool found = get_next(key, &r);
254 next->first = r.first;
255 ceph_assert(r.second);
256 next->second = *(r.second);
260 VPtr lookup(const K& key) {
262 std::list<VPtr> to_release;
264 std::unique_lock l{lock};
266 cond.wait(l, [this, &key, &val, &to_release] {
267 if (auto i = weak_refs.find(key); i != weak_refs.end()) {
268 if (val = i->second.first.lock(); val) {
269 lru_add(key, val, &to_release);
282 VPtr lookup_or_create(const K &key) {
284 list<VPtr> to_release;
286 std::unique_lock l{lock};
287 cond.wait(l, [this, &key, &val] {
288 if (auto i = weak_refs.find(key); i != weak_refs.end()) {
289 if (val = i->second.first.lock(); val) {
299 val = VPtr{new V{}, Cleanup{this, key}};
300 weak_refs.insert(make_pair(key, make_pair(val, val.get())));
302 lru_add(key, val, &to_release);
310 * Returns true iff there are no live references left to anything that has been
314 std::lock_guard l{lock};
315 return weak_refs.empty();
319 * Inserts a key if not present, or bumps it to the front of the LRU if
320 * it is, and then gives you a reference to the value. If the key already
321 * existed, you are responsible for deleting the new value you tried to
324 * @param key The key to insert
325 * @param value The value that goes with the key
326 * @param existed Set to true if the value was already in the
327 * map, false otherwise
328 * @return A reference to the map's value for the given key
330 VPtr add(const K& key, V *value, bool *existed = NULL) {
332 list<VPtr> to_release;
334 typename map<K, pair<WeakVPtr, V*>, C>::iterator actual;
335 std::unique_lock l{lock};
336 cond.wait(l, [this, &key, &actual, &val] {
337 actual = weak_refs.lower_bound(key);
338 if (actual != weak_refs.end() && actual->first == key) {
339 val = actual->second.first.lock();
358 val = VPtr(value, Cleanup(this, key));
359 weak_refs.insert(actual, make_pair(key, make_pair(val, value)));
361 lru_add(key, val, &to_release);
366 friend class SharedLRUTest;