Move TaggedCache to ripple_basics and remove dependency on upTime

This commit is contained in:
Vinnie Falco
2013-05-25 15:31:45 -07:00
parent 2576e54b6e
commit fb8c370d70
17 changed files with 85 additions and 68 deletions

View File

@@ -1,15 +1,5 @@
#ifndef __TAGGEDCACHE__
#define __TAGGEDCACHE__
#include <string>
#include <boost/thread/recursive_mutex.hpp>
#include <boost/unordered_map.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/ref.hpp>
#include <boost/make_shared.hpp>
extern int upTime();
#ifndef RIPPLE_TAGGEDCACHE_H
#define RIPPLE_TAGGEDCACHE_H
// This class implements a cache and a map. The cache keeps objects alive
// in the map. The map allows multiple code paths that reference objects
@@ -26,7 +16,16 @@ struct TaggedCacheLog { };
SETUP_LOG (TaggedCacheLog)
template <typename c_Key, typename c_Data> class TaggedCache
/** Combination cache/map container.
NOTE:
Timer must have this interface:
static int Timer::getElapsedSeconds ();
*/
template <typename c_Key, typename c_Data, class Timer>
class TaggedCache
{
public:
typedef c_Key key_type;
@@ -48,7 +47,7 @@ protected:
bool isCached() { return !!ptr; }
bool isExpired() { return weak_ptr.expired(); }
data_ptr lock() { return weak_ptr.lock(); }
void touch() { last_use = upTime(); }
void touch() { last_use = Timer::getElapsedSeconds (); }
};
typedef std::pair<key_type, cache_entry> cache_pair;
@@ -69,9 +68,15 @@ protected:
public:
TaggedCache(const char *name, int size, int age)
: mName(name), mTargetSize(size), mTargetAge(age), mCacheCount(0), mLastSweep(upTime()),
mHits(0), mMisses(0)
{ ; }
: mName(name)
, mTargetSize(size)
, mTargetAge(age)
, mCacheCount(0)
, mLastSweep(Timer::getElapsedSeconds())
, mHits(0)
, mMisses(0)
{
}
int getTargetSize() const;
int getTargetAge() const;
@@ -96,13 +101,15 @@ public:
boost::recursive_mutex& peekMutex() { return mLock; }
};
template<typename c_Key, typename c_Data> int TaggedCache<c_Key, c_Data>::getTargetSize() const
template<typename c_Key, typename c_Data, class Timer>
int TaggedCache<c_Key, c_Data, Timer>::getTargetSize() const
{
boost::recursive_mutex::scoped_lock sl(mLock);
return mTargetSize;
}
template<typename c_Key, typename c_Data> void TaggedCache<c_Key, c_Data>::setTargetSize(int s)
template<typename c_Key, typename c_Data, class Timer>
void TaggedCache<c_Key, c_Data, Timer>::setTargetSize(int s)
{
boost::recursive_mutex::scoped_lock sl(mLock);
mTargetSize = s;
@@ -111,56 +118,64 @@ template<typename c_Key, typename c_Data> void TaggedCache<c_Key, c_Data>::setTa
WriteLog (lsDEBUG, TaggedCacheLog) << mName << " target size set to " << s;
}
template<typename c_Key, typename c_Data> int TaggedCache<c_Key, c_Data>::getTargetAge() const
template<typename c_Key, typename c_Data, class Timer>
int TaggedCache<c_Key, c_Data, Timer>::getTargetAge() const
{
boost::recursive_mutex::scoped_lock sl(mLock);
return mTargetAge;
}
template<typename c_Key, typename c_Data> void TaggedCache<c_Key, c_Data>::setTargetAge(int s)
template<typename c_Key, typename c_Data, class Timer>
void TaggedCache<c_Key, c_Data, Timer>::setTargetAge(int s)
{
boost::recursive_mutex::scoped_lock sl(mLock);
mTargetAge = s;
WriteLog (lsDEBUG, TaggedCacheLog) << mName << " target age set to " << s;
}
template<typename c_Key, typename c_Data> int TaggedCache<c_Key, c_Data>::getCacheSize()
template<typename c_Key, typename c_Data, class Timer>
int TaggedCache<c_Key, c_Data, Timer>::getCacheSize()
{
boost::recursive_mutex::scoped_lock sl(mLock);
return mCacheCount;
}
template<typename c_Key, typename c_Data> int TaggedCache<c_Key, c_Data>::getTrackSize()
template<typename c_Key, typename c_Data, class Timer>
int TaggedCache<c_Key, c_Data, Timer>::getTrackSize()
{
boost::recursive_mutex::scoped_lock sl(mLock);
return mCache.size();
}
template<typename c_Key, typename c_Data> float TaggedCache<c_Key, c_Data>::getHitRate()
template<typename c_Key, typename c_Data, class Timer>
float TaggedCache<c_Key, c_Data, Timer>::getHitRate()
{
boost::recursive_mutex::scoped_lock sl(mLock);
return (static_cast<float>(mHits) * 100) / (1.0f + mHits + mMisses);
}
template<typename c_Key, typename c_Data> void TaggedCache<c_Key, c_Data>::clearStats()
template<typename c_Key, typename c_Data, class Timer>
void TaggedCache<c_Key, c_Data, Timer>::clearStats()
{
boost::recursive_mutex::scoped_lock sl(mLock);
mHits = 0;
mMisses = 0;
}
template<typename c_Key, typename c_Data> void TaggedCache<c_Key, c_Data>::clear()
template<typename c_Key, typename c_Data, class Timer>
void TaggedCache<c_Key, c_Data, Timer>::clear()
{
boost::recursive_mutex::scoped_lock sl(mLock);
mCache.clear();
mCacheCount = 0;
}
template<typename c_Key, typename c_Data> void TaggedCache<c_Key, c_Data>::sweep()
template<typename c_Key, typename c_Data, class Timer>
void TaggedCache<c_Key, c_Data, Timer>::sweep()
{
boost::recursive_mutex::scoped_lock sl(mLock);
int mLastSweep = upTime();
int mLastSweep = Timer::getElapsedSeconds ();
int target = mLastSweep - mTargetAge;
int cacheRemovals = 0, mapRemovals = 0, cc = 0;
@@ -213,7 +228,8 @@ template<typename c_Key, typename c_Data> void TaggedCache<c_Key, c_Data>::sweep
", map-=" << mapRemovals;
}
template<typename c_Key, typename c_Data> bool TaggedCache<c_Key, c_Data>::touch(const key_type& key)
template<typename c_Key, typename c_Data, class Timer>
bool TaggedCache<c_Key, c_Data, Timer>::touch(const key_type& key)
{ // If present, make current in cache
boost::recursive_mutex::scoped_lock sl(mLock);
@@ -241,7 +257,8 @@ template<typename c_Key, typename c_Data> bool TaggedCache<c_Key, c_Data>::touch
return false;
}
template<typename c_Key, typename c_Data> bool TaggedCache<c_Key, c_Data>::del(const key_type& key, bool valid)
template<typename c_Key, typename c_Data, class Timer>
bool TaggedCache<c_Key, c_Data, Timer>::del(const key_type& key, bool valid)
{ // Remove from cache, if !valid, remove from map too. Returns true if removed from cache
boost::recursive_mutex::scoped_lock sl(mLock);
@@ -263,8 +280,8 @@ template<typename c_Key, typename c_Data> bool TaggedCache<c_Key, c_Data>::del(c
return ret;
}
template<typename c_Key, typename c_Data>
bool TaggedCache<c_Key, c_Data>::canonicalize(const key_type& key, boost::shared_ptr<c_Data>& data, bool replace)
template<typename c_Key, typename c_Data, class Timer>
bool TaggedCache<c_Key, c_Data, Timer>::canonicalize(const key_type& key, boost::shared_ptr<c_Data>& data, bool replace)
{ // Return canonical value, store if needed, refresh in cache
// Return values: true=we had the data already
boost::recursive_mutex::scoped_lock sl(mLock);
@@ -272,7 +289,7 @@ bool TaggedCache<c_Key, c_Data>::canonicalize(const key_type& key, boost::shared
cache_iterator cit = mCache.find(key);
if (cit == mCache.end())
{
mCache.insert(cache_pair(key, cache_entry(upTime(), data)));
mCache.insert(cache_pair(key, cache_entry(Timer::getElapsedSeconds(), data)));
++mCacheCount;
return false;
}
@@ -315,8 +332,8 @@ bool TaggedCache<c_Key, c_Data>::canonicalize(const key_type& key, boost::shared
return false;
}
template<typename c_Key, typename c_Data>
boost::shared_ptr<c_Data> TaggedCache<c_Key, c_Data>::fetch(const key_type& key)
template<typename c_Key, typename c_Data, class Timer>
boost::shared_ptr<c_Data> TaggedCache<c_Key, c_Data, Timer>::fetch(const key_type& key)
{ // fetch us a shared pointer to the stored data object
boost::recursive_mutex::scoped_lock sl(mLock);
@@ -346,15 +363,15 @@ boost::shared_ptr<c_Data> TaggedCache<c_Key, c_Data>::fetch(const key_type& key)
return data_ptr();
}
template<typename c_Key, typename c_Data>
bool TaggedCache<c_Key, c_Data>::store(const key_type& key, const c_Data& data)
template<typename c_Key, typename c_Data, class Timer>
bool TaggedCache<c_Key, c_Data, Timer>::store(const key_type& key, const c_Data& data)
{
data_ptr d = boost::make_shared<c_Data>(boost::cref(data));
return canonicalize(key, d);
}
template<typename c_Key, typename c_Data>
bool TaggedCache<c_Key, c_Data>::retrieve(const key_type& key, c_Data& data)
template<typename c_Key, typename c_Data, class Timer>
bool TaggedCache<c_Key, c_Data, Timer>::retrieve(const key_type& key, c_Data& data)
{ // retrieve the value of the stored data
data_ptr entry = fetch(key);
if (!entry)

View File

@@ -25,7 +25,6 @@
system calls. (?)
*/
// VFALCO: TODO, determine if the non-manual timing is actually needed
class UptimeTimer
{
private:

View File

@@ -61,17 +61,25 @@ namespace boost {
#include <boost/foreach.hpp>
#include <boost/icl/interval_set.hpp> // oof this one is ugly
// TaggedCache
#include <boost/thread/recursive_mutex.hpp>
#include <boost/unordered_map.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/ref.hpp>
#include <boost/make_shared.hpp>
#include "../ripple_json/ripple_json.h"
#include "types/ripple_IntegerTypes.h"
#include "types/ripple_IntegerTypes.h" // must come first
#include "diagnostic/ripple_Log.h" // Needed by others
#include "containers/ripple_KeyCache.h"
#include "containers/ripple_RangeSet.h"
#include "containers/ripple_SecureAllocator.h"
#include "diagnostic/ripple_Log.h"
#include "containers/ripple_TaggedCache.h"
#include "events/ripple_UptimeTimer.h"

View File

@@ -1178,6 +1178,7 @@
<ClInclude Include="modules\ripple_basics\containers\ripple_KeyCache.h" />
<ClInclude Include="modules\ripple_basics\containers\ripple_RangeSet.h" />
<ClInclude Include="modules\ripple_basics\containers\ripple_SecureAllocator.h" />
<ClInclude Include="modules\ripple_basics\containers\ripple_TaggedCache.h" />
<ClInclude Include="modules\ripple_basics\diagnostic\ripple_Log.h" />
<ClInclude Include="modules\ripple_basics\events\ripple_UptimeTimer.h" />
<ClInclude Include="modules\ripple_basics\ripple_basics.h" />
@@ -1587,7 +1588,6 @@
<ClInclude Include="src\cpp\ripple\SHAMapSync.h" />
<ClInclude Include="src\cpp\ripple\SNTPClient.h" />
<ClInclude Include="src\cpp\ripple\Suppression.h" />
<ClInclude Include="src\cpp\ripple\TaggedCache.h" />
<ClInclude Include="src\cpp\ripple\Transaction.h" />
<ClInclude Include="src\cpp\ripple\TransactionEngine.h" />
<ClInclude Include="src\cpp\ripple\TransactionErr.h" />

View File

@@ -1304,9 +1304,6 @@
<ClInclude Include="src\cpp\ripple\SHAMapSync.h">
<Filter>1. Modules\ripple_mess\containers</Filter>
</ClInclude>
<ClInclude Include="src\cpp\ripple\TaggedCache.h">
<Filter>1. Modules\ripple_mess\containers</Filter>
</ClInclude>
<ClInclude Include="src\cpp\ripple\ScopedLock.h">
<Filter>1. Modules\ripple_mess\types</Filter>
</ClInclude>
@@ -1400,6 +1397,9 @@
<ClInclude Include="modules\ripple_basics\containers\ripple_SecureAllocator.h">
<Filter>1. Modules\ripple_basics\containers</Filter>
</ClInclude>
<ClInclude Include="modules\ripple_basics\containers\ripple_TaggedCache.h">
<Filter>1. Modules\ripple_basics\containers</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="SConstruct" />

View File

@@ -1,5 +1,5 @@
TaggedCache<uint256, AcceptedLedger> AcceptedLedger::ALCache("AcceptedLedger", 4, 60);
TaggedCache<uint256, AcceptedLedger, UptimeTimerAdapter> AcceptedLedger::ALCache("AcceptedLedger", 4, 60);
ALTransaction::ALTransaction(uint32 seq, SerializerIterator& sit)
{

View File

@@ -72,7 +72,7 @@ protected:
void insert(ALTransaction::ref);
static TaggedCache<uint256, AcceptedLedger> ALCache;
static TaggedCache<uint256, AcceptedLedger, UptimeTimerAdapter> ALCache;
AcceptedLedger(Ledger::ref ledger);
public:

View File

@@ -13,7 +13,6 @@
#include "BitcoinUtil.h"
#include "key.h"
#include "utils.h"
#include "TaggedCache.h"
#include "../database/SqliteDatabase.h"

View File

@@ -20,7 +20,6 @@
#include "Peer.h"
#include "NetworkOPs.h"
#include "WSDoor.h"
#include "TaggedCache.h"
#include "ValidationCollection.h"
#include "Suppression.h"
#include "SNTPClient.h"
@@ -33,8 +32,8 @@
class RPCDoor;
class PeerDoor;
typedef TaggedCache< uint256, std::vector<unsigned char> > NodeCache;
typedef TaggedCache< uint256, SLE > SLECache;
typedef TaggedCache< uint256, std::vector<unsigned char>, UptimeTimerAdapter> NodeCache;
typedef TaggedCache< uint256, SLE, UptimeTimerAdapter> SLECache;
class DatabaseCon
{

View File

@@ -8,13 +8,12 @@
#include "uint256.h"
#include "ScopedLock.h"
#include "TaggedCache.h"
#include "InstanceCounter.h"
// VFALCO: TODO, Move this to someplace sensible!!
// Adapter to furnish uptime information to KeyCache via UptimeTimer singleton
struct KeyCacheUptimeTimer
struct UptimeTimerAdapter
{
inline static int getElapsedSeconds ()
{
@@ -62,8 +61,8 @@ public:
class HashedObjectStore
{
protected:
TaggedCache<uint256, HashedObject> mCache;
KeyCache <uint256, KeyCacheUptimeTimer> mNegativeCache;
TaggedCache<uint256, HashedObject, UptimeTimerAdapter> mCache;
KeyCache <uint256, UptimeTimerAdapter> mNegativeCache;
boost::mutex mWriteMutex;
boost::condition_variable mWriteCondition;

View File

@@ -15,7 +15,6 @@
#include "Ledger.h"
#include "Peer.h"
#include "TaggedCache.h"
#include "InstanceCounter.h"
#include "ripple.pb.h"
@@ -146,7 +145,7 @@ class LedgerAcquireMaster
protected:
boost::mutex mLock;
std::map<uint256, LedgerAcquire::pointer> mLedgers;
KeyCache<uint256, KeyCacheUptimeTimer> mRecentFailures;
KeyCache<uint256, UptimeTimerAdapter> mRecentFailures;
public:
LedgerAcquireMaster() : mRecentFailures("LedgerAcquireRecentFailures", 0, LEDGER_REACQUIRE_INTERVAL) { ; }

View File

@@ -1,12 +1,11 @@
#ifndef __LEDGERHISTORY__
#define __LEDGERHISTORY__
#include "TaggedCache.h"
#include "Ledger.h"
class LedgerHistory
{
TaggedCache<uint256, Ledger> mLedgersByHash;
TaggedCache<uint256, Ledger, UptimeTimerAdapter> mLedgersByHash;
std::map<uint32, uint256> mLedgersByIndex; // accepted ledgers
public:

View File

@@ -148,7 +148,7 @@ protected:
subMapType mSubTransactions; // all accepted transactions
subMapType mSubRTTransactions; // all proposed and accepted transactions
TaggedCache< uint256, std::vector<unsigned char> > mFetchPack;
TaggedCache< uint256, std::vector<unsigned char>, UptimeTimerAdapter > mFetchPack;
uint32 mLastFetchPack;
uint32 mFetchSeq;

View File

@@ -356,7 +356,7 @@ protected:
SHAMapType mType;
static KeyCache <uint256, KeyCacheUptimeTimer> fullBelowCache;
static KeyCache <uint256, UptimeTimerAdapter> fullBelowCache;
void dirtyUp(std::stack<SHAMapTreeNode::pointer>& stack, const uint256& target, uint256 prevHash);
std::stack<SHAMapTreeNode::pointer> getStack(const uint256& id, bool include_nonmatching_leaf, bool partialOk);

View File

@@ -11,7 +11,7 @@
static const uint256 uZero;
KeyCache <uint256, KeyCacheUptimeTimer> SHAMap::fullBelowCache("fullBelowCache", 65536, 240);
KeyCache <uint256, UptimeTimerAdapter> SHAMap::fullBelowCache("fullBelowCache", 65536, 240);
void SHAMap::getMissingNodes(std::vector<SHAMapNode>& nodeIDs, std::vector<uint256>& hashes, int max,
SHAMapSyncFilter* filter)

View File

@@ -2,14 +2,13 @@
#define __TRANSACTIONMASTER__
#include "Transaction.h"
#include "TaggedCache.h"
// Tracks all transactions in memory
class TransactionMaster
{
protected:
TaggedCache<uint256, Transaction> mCache;
TaggedCache<uint256, Transaction, UptimeTimerAdapter> mCache;
public:

View File

@@ -8,7 +8,6 @@
#include "uint256.h"
#include "SerializedValidation.h"
#include "TaggedCache.h"
#include "JobQueue.h"
typedef boost::unordered_map<uint160, SerializedValidation::pointer> ValidationSet;
@@ -21,7 +20,7 @@ class ValidationCollection
protected:
boost::mutex mValidationLock;
TaggedCache<uint256, ValidationSet> mValidations;
TaggedCache<uint256, ValidationSet, UptimeTimerAdapter> mValidations;
boost::unordered_map<uint160, SerializedValidation::pointer> mCurrentValidations;
std::vector<SerializedValidation::pointer> mStaleValidations;