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
20 #include "common/Mutex.h"
21 #include "common/Cond.h"
22 #include "include/unordered_map.h"
24 // re-include our assert to clobber the system one; fix dout:
25 #include "include/assert.h"
27 template <class K, class V, class C = std::less<K>, class H = std::hash<K> >
30 typedef ceph::shared_ptr<V> VPtr;
31 typedef ceph::weak_ptr<V> WeakVPtr;
39 ceph::unordered_map<K, typename list<pair<K, VPtr> >::iterator, H> contents;
40 list<pair<K, VPtr> > lru;
42 map<K, pair<WeakVPtr, V*>, C> weak_refs;
44 void trim_cache(list<VPtr> *to_release) {
45 while (size > max_size) {
46 to_release->push_back(lru.back().second);
47 lru_remove(lru.back().first);
51 void lru_remove(const K& key) {
52 typename ceph::unordered_map<K, typename list<pair<K, VPtr> >::iterator, H>::iterator i =
54 if (i == contents.end())
61 void lru_add(const K& key, const VPtr& val, list<VPtr> *to_release) {
62 typename ceph::unordered_map<K, typename list<pair<K, VPtr> >::iterator, H>::iterator i =
64 if (i != contents.end()) {
65 lru.splice(lru.begin(), lru, i->second);
68 lru.push_front(make_pair(key, val));
69 contents[key] = lru.begin();
70 trim_cache(to_release);
74 void remove(const K& key, V *valptr) {
75 Mutex::Locker l(lock);
76 typename map<K, pair<WeakVPtr, V*>, C>::iterator i = weak_refs.find(key);
77 if (i != weak_refs.end() && i->second.second == valptr) {
85 SharedLRU<K, V, C> *cache;
87 Cleanup(SharedLRU<K, V, C> *cache, K key) : cache(cache), key(key) {}
88 void operator()(V *ptr) {
89 cache->remove(key, ptr);
95 SharedLRU(CephContext *cct = NULL, size_t max_size = 20)
96 : cct(cct), lock("SharedLRU::lock"), max_size(max_size),
98 contents.rehash(max_size);
104 if (!weak_refs.empty()) {
105 lderr(cct) << "leaked refs:\n";
106 dump_weak_refs(*_dout);
108 assert(weak_refs.empty());
112 /// adjust container comparator (for purposes of get_next sort order)
113 void reset_comparator(C comp) {
114 // get_next uses weak_refs; that's the only container we need to
116 map<K, pair<WeakVPtr, V*>, C> temp;
118 Mutex::Locker l(lock);
119 temp.swap(weak_refs);
121 // reconstruct with new comparator
122 weak_refs = map<K, pair<WeakVPtr, V*>, C>(comp);
123 weak_refs.insert(temp.begin(), temp.end());
127 return weak_refs.key_comp();
130 void set_cct(CephContext *c) {
134 void dump_weak_refs() {
135 lderr(cct) << "leaked refs:\n";
136 dump_weak_refs(*_dout);
140 void dump_weak_refs(ostream& out) {
141 for (typename map<K, pair<WeakVPtr, V*>, C>::iterator p = weak_refs.begin();
142 p != weak_refs.end();
144 out << __func__ << " " << this << " weak_refs: "
145 << p->first << " = " << p->second.second
146 << " with " << p->second.first.use_count() << " refs"
151 //clear all strong reference from the lru.
154 VPtr val; // release any ref we have after we drop the lock
155 Mutex::Locker l(lock);
159 val = lru.back().second;
160 lru_remove(lru.back().first);
164 void clear(const K& key) {
165 VPtr val; // release any ref we have after we drop the lock
167 Mutex::Locker l(lock);
168 typename map<K, pair<WeakVPtr, V*>, C>::iterator i = weak_refs.find(key);
169 if (i != weak_refs.end()) {
170 val = i->second.first.lock();
176 void purge(const K &key) {
177 VPtr val; // release any ref we have after we drop the lock
179 Mutex::Locker l(lock);
180 typename map<K, pair<WeakVPtr, V*>, C>::iterator i = weak_refs.find(key);
181 if (i != weak_refs.end()) {
182 val = i->second.first.lock();
189 void set_size(size_t new_size) {
190 list<VPtr> to_release;
192 Mutex::Locker l(lock);
194 trim_cache(&to_release);
198 // Returns K key s.t. key <= k for all currently cached k,v
199 K cached_key_lower_bound() {
200 Mutex::Locker l(lock);
201 return weak_refs.begin()->first;
204 VPtr lower_bound(const K& key) {
206 list<VPtr> to_release;
208 Mutex::Locker l(lock);
213 if (weak_refs.empty())
215 typename map<K, pair<WeakVPtr, V*>, C>::iterator i =
216 weak_refs.lower_bound(key);
217 if (i == weak_refs.end())
219 val = i->second.first.lock();
221 lru_add(i->first, val, &to_release);
232 bool get_next(const K &key, pair<K, VPtr> *next) {
235 Mutex::Locker l(lock);
237 typename map<K, pair<WeakVPtr, V*>, C>::iterator i = weak_refs.upper_bound(key);
239 while (i != weak_refs.end() &&
240 !(next_val = i->second.first.lock()))
243 if (i == weak_refs.end())
247 r = make_pair(i->first, next_val);
253 bool get_next(const K &key, pair<K, V> *next) {
255 bool found = get_next(key, &r);
258 next->first = r.first;
260 next->second = *(r.second);
264 VPtr lookup(const K& key) {
266 list<VPtr> to_release;
268 Mutex::Locker l(lock);
273 typename map<K, pair<WeakVPtr, V*>, C>::iterator i = weak_refs.find(key);
274 if (i != weak_refs.end()) {
275 val = i->second.first.lock();
277 lru_add(key, val, &to_release);
289 VPtr lookup_or_create(const K &key) {
291 list<VPtr> to_release;
293 Mutex::Locker l(lock);
297 typename map<K, pair<WeakVPtr, V*>, C>::iterator i = weak_refs.find(key);
298 if (i != weak_refs.end()) {
299 val = i->second.first.lock();
301 lru_add(key, val, &to_release);
311 V *new_value = new V();
312 VPtr new_val(new_value, Cleanup(this, key));
313 weak_refs.insert(make_pair(key, make_pair(new_val, new_value)));
314 lru_add(key, new_val, &to_release);
322 * Returns true iff there are no live references left to anything that has been
326 Mutex::Locker l(lock);
327 return weak_refs.empty();
331 * Inserts a key if not present, or bumps it to the front of the LRU if
332 * it is, and then gives you a reference to the value. If the key already
333 * existed, you are responsible for deleting the new value you tried to
336 * @param key The key to insert
337 * @param value The value that goes with the key
338 * @param existed Set to true if the value was already in the
339 * map, false otherwise
340 * @return A reference to the map's value for the given key
342 VPtr add(const K& key, V *value, bool *existed = NULL) {
344 list<VPtr> to_release;
346 Mutex::Locker l(lock);
347 typename map<K, pair<WeakVPtr, V*>, C>::iterator actual =
348 weak_refs.lower_bound(key);
349 if (actual != weak_refs.end() && actual->first == key) {
353 return actual->second.first.lock();
359 val = VPtr(value, Cleanup(this, key));
360 weak_refs.insert(actual, make_pair(key, make_pair(val, value)));
361 lru_add(key, val, &to_release);
366 friend class SharedLRUTest;