diff --git a/src/cpp/ripple/TaggedCache.h b/modules/ripple_basics/containers/ripple_TaggedCache.h similarity index 73% rename from src/cpp/ripple/TaggedCache.h rename to modules/ripple_basics/containers/ripple_TaggedCache.h index e9b39994d3..191251923c 100644 --- a/src/cpp/ripple/TaggedCache.h +++ b/modules/ripple_basics/containers/ripple_TaggedCache.h @@ -1,15 +1,5 @@ -#ifndef __TAGGEDCACHE__ -#define __TAGGEDCACHE__ - -#include - -#include -#include -#include -#include -#include - -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 class TaggedCache +/** Combination cache/map container. + + NOTE: + + Timer must have this interface: + + static int Timer::getElapsedSeconds (); +*/ +template +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 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 int TaggedCache::getTargetSize() const +template +int TaggedCache::getTargetSize() const { boost::recursive_mutex::scoped_lock sl(mLock); return mTargetSize; } -template void TaggedCache::setTargetSize(int s) +template +void TaggedCache::setTargetSize(int s) { boost::recursive_mutex::scoped_lock sl(mLock); mTargetSize = s; @@ -111,56 +118,64 @@ template void TaggedCache::setTa WriteLog (lsDEBUG, TaggedCacheLog) << mName << " target size set to " << s; } -template int TaggedCache::getTargetAge() const +template +int TaggedCache::getTargetAge() const { boost::recursive_mutex::scoped_lock sl(mLock); return mTargetAge; } -template void TaggedCache::setTargetAge(int s) +template +void TaggedCache::setTargetAge(int s) { boost::recursive_mutex::scoped_lock sl(mLock); mTargetAge = s; WriteLog (lsDEBUG, TaggedCacheLog) << mName << " target age set to " << s; } -template int TaggedCache::getCacheSize() +template +int TaggedCache::getCacheSize() { boost::recursive_mutex::scoped_lock sl(mLock); return mCacheCount; } -template int TaggedCache::getTrackSize() +template +int TaggedCache::getTrackSize() { boost::recursive_mutex::scoped_lock sl(mLock); return mCache.size(); } -template float TaggedCache::getHitRate() +template +float TaggedCache::getHitRate() { boost::recursive_mutex::scoped_lock sl(mLock); return (static_cast(mHits) * 100) / (1.0f + mHits + mMisses); } -template void TaggedCache::clearStats() +template +void TaggedCache::clearStats() { boost::recursive_mutex::scoped_lock sl(mLock); mHits = 0; mMisses = 0; } -template void TaggedCache::clear() +template +void TaggedCache::clear() { boost::recursive_mutex::scoped_lock sl(mLock); mCache.clear(); mCacheCount = 0; } -template void TaggedCache::sweep() +template +void TaggedCache::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 void TaggedCache::sweep ", map-=" << mapRemovals; } -template bool TaggedCache::touch(const key_type& key) +template +bool TaggedCache::touch(const key_type& key) { // If present, make current in cache boost::recursive_mutex::scoped_lock sl(mLock); @@ -241,7 +257,8 @@ template bool TaggedCache::touch return false; } -template bool TaggedCache::del(const key_type& key, bool valid) +template +bool TaggedCache::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 bool TaggedCache::del(c return ret; } -template -bool TaggedCache::canonicalize(const key_type& key, boost::shared_ptr& data, bool replace) +template +bool TaggedCache::canonicalize(const key_type& key, boost::shared_ptr& 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::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::canonicalize(const key_type& key, boost::shared return false; } -template -boost::shared_ptr TaggedCache::fetch(const key_type& key) +template +boost::shared_ptr TaggedCache::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 TaggedCache::fetch(const key_type& key) return data_ptr(); } -template -bool TaggedCache::store(const key_type& key, const c_Data& data) +template +bool TaggedCache::store(const key_type& key, const c_Data& data) { data_ptr d = boost::make_shared(boost::cref(data)); return canonicalize(key, d); } -template -bool TaggedCache::retrieve(const key_type& key, c_Data& data) +template +bool TaggedCache::retrieve(const key_type& key, c_Data& data) { // retrieve the value of the stored data data_ptr entry = fetch(key); if (!entry) diff --git a/modules/ripple_basics/events/ripple_UptimeTimer.h b/modules/ripple_basics/events/ripple_UptimeTimer.h index d0ea915301..1f9488a265 100644 --- a/modules/ripple_basics/events/ripple_UptimeTimer.h +++ b/modules/ripple_basics/events/ripple_UptimeTimer.h @@ -25,7 +25,6 @@ system calls. (?) */ // VFALCO: TODO, determine if the non-manual timing is actually needed - class UptimeTimer { private: diff --git a/modules/ripple_basics/ripple_basics.h b/modules/ripple_basics/ripple_basics.h index 49d6600f10..8ecce394ae 100644 --- a/modules/ripple_basics/ripple_basics.h +++ b/modules/ripple_basics/ripple_basics.h @@ -61,17 +61,25 @@ namespace boost { #include #include // oof this one is ugly +// TaggedCache +#include +#include +#include +#include +#include + #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" diff --git a/newcoin.vcxproj b/newcoin.vcxproj index 6b50811d62..8258dd1e08 100644 --- a/newcoin.vcxproj +++ b/newcoin.vcxproj @@ -1178,6 +1178,7 @@ + @@ -1587,7 +1588,6 @@ - diff --git a/newcoin.vcxproj.filters b/newcoin.vcxproj.filters index 6a08f8b073..d2fd3b7611 100644 --- a/newcoin.vcxproj.filters +++ b/newcoin.vcxproj.filters @@ -1304,9 +1304,6 @@ 1. Modules\ripple_mess\containers - - 1. Modules\ripple_mess\containers - 1. Modules\ripple_mess\types @@ -1400,6 +1397,9 @@ 1. Modules\ripple_basics\containers + + 1. Modules\ripple_basics\containers + diff --git a/src/cpp/ripple/AcceptedLedger.cpp b/src/cpp/ripple/AcceptedLedger.cpp index 7cd3111814..4a3d774994 100644 --- a/src/cpp/ripple/AcceptedLedger.cpp +++ b/src/cpp/ripple/AcceptedLedger.cpp @@ -1,5 +1,5 @@ -TaggedCache AcceptedLedger::ALCache("AcceptedLedger", 4, 60); +TaggedCache AcceptedLedger::ALCache("AcceptedLedger", 4, 60); ALTransaction::ALTransaction(uint32 seq, SerializerIterator& sit) { diff --git a/src/cpp/ripple/AcceptedLedger.h b/src/cpp/ripple/AcceptedLedger.h index ad0aad6dd3..04b76097cf 100644 --- a/src/cpp/ripple/AcceptedLedger.h +++ b/src/cpp/ripple/AcceptedLedger.h @@ -72,7 +72,7 @@ protected: void insert(ALTransaction::ref); - static TaggedCache ALCache; + static TaggedCache ALCache; AcceptedLedger(Ledger::ref ledger); public: diff --git a/src/cpp/ripple/Application.cpp b/src/cpp/ripple/Application.cpp index 917be3b54a..ea28fccfde 100644 --- a/src/cpp/ripple/Application.cpp +++ b/src/cpp/ripple/Application.cpp @@ -13,7 +13,6 @@ #include "BitcoinUtil.h" #include "key.h" #include "utils.h" -#include "TaggedCache.h" #include "../database/SqliteDatabase.h" diff --git a/src/cpp/ripple/Application.h b/src/cpp/ripple/Application.h index 7f7fb82ef5..c3acee7f44 100644 --- a/src/cpp/ripple/Application.h +++ b/src/cpp/ripple/Application.h @@ -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 > NodeCache; -typedef TaggedCache< uint256, SLE > SLECache; +typedef TaggedCache< uint256, std::vector, UptimeTimerAdapter> NodeCache; +typedef TaggedCache< uint256, SLE, UptimeTimerAdapter> SLECache; class DatabaseCon { diff --git a/src/cpp/ripple/HashedObject.h b/src/cpp/ripple/HashedObject.h index 18f4e988d4..776dfc3de2 100644 --- a/src/cpp/ripple/HashedObject.h +++ b/src/cpp/ripple/HashedObject.h @@ -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 mCache; - KeyCache mNegativeCache; + TaggedCache mCache; + KeyCache mNegativeCache; boost::mutex mWriteMutex; boost::condition_variable mWriteCondition; diff --git a/src/cpp/ripple/LedgerAcquire.h b/src/cpp/ripple/LedgerAcquire.h index 76f98af5ac..e183095d92 100644 --- a/src/cpp/ripple/LedgerAcquire.h +++ b/src/cpp/ripple/LedgerAcquire.h @@ -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 mLedgers; - KeyCache mRecentFailures; + KeyCache mRecentFailures; public: LedgerAcquireMaster() : mRecentFailures("LedgerAcquireRecentFailures", 0, LEDGER_REACQUIRE_INTERVAL) { ; } diff --git a/src/cpp/ripple/LedgerHistory.h b/src/cpp/ripple/LedgerHistory.h index cfb9239c4e..6543c8615b 100644 --- a/src/cpp/ripple/LedgerHistory.h +++ b/src/cpp/ripple/LedgerHistory.h @@ -1,12 +1,11 @@ #ifndef __LEDGERHISTORY__ #define __LEDGERHISTORY__ -#include "TaggedCache.h" #include "Ledger.h" class LedgerHistory { - TaggedCache mLedgersByHash; + TaggedCache mLedgersByHash; std::map mLedgersByIndex; // accepted ledgers public: diff --git a/src/cpp/ripple/NetworkOPs.h b/src/cpp/ripple/NetworkOPs.h index 76a6cd9393..ec1927170f 100644 --- a/src/cpp/ripple/NetworkOPs.h +++ b/src/cpp/ripple/NetworkOPs.h @@ -148,7 +148,7 @@ protected: subMapType mSubTransactions; // all accepted transactions subMapType mSubRTTransactions; // all proposed and accepted transactions - TaggedCache< uint256, std::vector > mFetchPack; + TaggedCache< uint256, std::vector, UptimeTimerAdapter > mFetchPack; uint32 mLastFetchPack; uint32 mFetchSeq; diff --git a/src/cpp/ripple/SHAMap.h b/src/cpp/ripple/SHAMap.h index 774971019c..16b06b1cd3 100644 --- a/src/cpp/ripple/SHAMap.h +++ b/src/cpp/ripple/SHAMap.h @@ -356,7 +356,7 @@ protected: SHAMapType mType; - static KeyCache fullBelowCache; + static KeyCache fullBelowCache; void dirtyUp(std::stack& stack, const uint256& target, uint256 prevHash); std::stack getStack(const uint256& id, bool include_nonmatching_leaf, bool partialOk); diff --git a/src/cpp/ripple/SHAMapSync.cpp b/src/cpp/ripple/SHAMapSync.cpp index 2137d59430..e282609ea3 100644 --- a/src/cpp/ripple/SHAMapSync.cpp +++ b/src/cpp/ripple/SHAMapSync.cpp @@ -11,7 +11,7 @@ static const uint256 uZero; -KeyCache SHAMap::fullBelowCache("fullBelowCache", 65536, 240); +KeyCache SHAMap::fullBelowCache("fullBelowCache", 65536, 240); void SHAMap::getMissingNodes(std::vector& nodeIDs, std::vector& hashes, int max, SHAMapSyncFilter* filter) diff --git a/src/cpp/ripple/TransactionMaster.h b/src/cpp/ripple/TransactionMaster.h index 59b486280d..2b110d50d2 100644 --- a/src/cpp/ripple/TransactionMaster.h +++ b/src/cpp/ripple/TransactionMaster.h @@ -2,14 +2,13 @@ #define __TRANSACTIONMASTER__ #include "Transaction.h" -#include "TaggedCache.h" // Tracks all transactions in memory class TransactionMaster { protected: - TaggedCache mCache; + TaggedCache mCache; public: diff --git a/src/cpp/ripple/ValidationCollection.h b/src/cpp/ripple/ValidationCollection.h index 72015ea7b7..cdb93db95e 100644 --- a/src/cpp/ripple/ValidationCollection.h +++ b/src/cpp/ripple/ValidationCollection.h @@ -8,7 +8,6 @@ #include "uint256.h" #include "SerializedValidation.h" -#include "TaggedCache.h" #include "JobQueue.h" typedef boost::unordered_map ValidationSet; @@ -21,7 +20,7 @@ class ValidationCollection protected: boost::mutex mValidationLock; - TaggedCache mValidations; + TaggedCache mValidations; boost::unordered_map mCurrentValidations; std::vector mStaleValidations;