Fix a memory leak. Better TaggedCache logging.

This commit is contained in:
JoelKatz
2012-11-01 07:14:45 -07:00
parent d9ff4f2ad4
commit 7372d4423f
5 changed files with 27 additions and 12 deletions

View File

@@ -18,7 +18,7 @@
#include <boost/thread.hpp>
SETUP_LOG();
LogPartition TaggedCachePartition("TaggedCache");
Application* theApp = NULL;
DatabaseCon::DatabaseCon(const std::string& strName, const char *initStrings[], int initCount)
@@ -39,7 +39,7 @@ DatabaseCon::~DatabaseCon()
Application::Application() :
mIOWork(mIOService), mAuxWork(mAuxService), mUNL(mIOService),
mNetOps(mIOService, &mMasterLedger), mTempNodeCache(16384, 90), mHashedObjectStore(16384, 300),
mNetOps(mIOService, &mMasterLedger), mTempNodeCache("NodeCache", 16384, 90), mHashedObjectStore(16384, 300),
mSNTPClient(mAuxService), mRpcDB(NULL), mTxnDB(NULL), mLedgerDB(NULL), mWalletDB(NULL),
mHashNodeDB(NULL), mNetNodeDB(NULL),
mConnectionPool(mIOService), mPeerDoor(NULL), mRPCDoor(NULL), mSweepTimer(mAuxService)

View File

@@ -12,7 +12,7 @@ SETUP_LOG();
DECLARE_INSTANCE(HashedObject);
HashedObjectStore::HashedObjectStore(int cacheSize, int cacheAge) :
mCache(cacheSize, cacheAge), mWritePending(false)
mCache("HashedObjectStore", cacheSize, cacheAge), mWritePending(false)
{
mWriteSet.reserve(128);
}

View File

@@ -19,7 +19,7 @@
// FIXME: Need to clean up ledgers by index, probably should switch to just mapping sequence to hash
LedgerHistory::LedgerHistory() : mLedgersByHash(CACHED_LEDGER_NUM, CACHED_LEDGER_AGE)
LedgerHistory::LedgerHistory() : mLedgersByHash("LedgerCache", CACHED_LEDGER_NUM, CACHED_LEDGER_AGE)
{ ; }
void LedgerHistory::addLedger(Ledger::pointer ledger)

View File

@@ -1,12 +1,17 @@
#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>
#include "Log.h"
extern LogPartition TaggedCachePartition;
// This class implemented a cache and a map. The cache keeps objects alive
// in the map. The map allows multiple code paths that reference objects
// with the same tag to get the same actual object.
@@ -30,6 +35,7 @@ public:
protected:
mutable boost::recursive_mutex mLock;
std::string mName;
int mTargetSize, mTargetAge;
boost::unordered_map<key_type, cache_entry> mCache; // Hold strong reference to recent objects
@@ -38,7 +44,8 @@ protected:
boost::unordered_map<key_type, weak_data_ptr> mMap; // Track stored objects
public:
TaggedCache(int size, int age) : mTargetSize(size), mTargetAge(age), mLastSweep(time(NULL)) { ; }
TaggedCache(const char *name, int size, int age)
: mName(name), mTargetSize(size), mTargetAge(age), mLastSweep(time(NULL)) { ; }
int getTargetSize() const;
int getTargetAge() const;
@@ -92,32 +99,40 @@ template<typename c_Key, typename c_Data> void TaggedCache<c_Key, c_Data>::sweep
if (mCache.size() < mTargetSize)
return;
time_t now = time(NULL);
if ((mLastSweep + 10) < now)
return;
mLastSweep = now;
time_t target = now - mTargetAge;
mLastSweep = time(NULL);
time_t target = mLastSweep - mTargetAge;
// Pass 1, remove old objects from cache
int cacheRemovals = 0;
typename boost::unordered_map<key_type, cache_entry>::iterator cit = mCache.begin();
while (cit != mCache.end())
{
if (cit->second.first < target)
{
++cacheRemovals;
mCache.erase(cit++);
}
else
++cit;
}
// Pass 2, remove dead objects from map
int mapRemovals = 0;
typename boost::unordered_map<key_type, weak_data_ptr>::iterator mit = mMap.begin();
while (mit != mMap.end())
{
if (mit->second.expired())
{
++mapRemovals;
mMap.erase(mit++);
}
else
++mit;
}
if (TaggedCachePartition.doLog(lsTRACE) && (mapRemovals || cacheRemovals))
Log(lsTRACE, TaggedCachePartition) << mName << ": cache = " << mCache.size() << "-" << cacheRemovals <<
", map = " << mMap.size() << "-" << mapRemovals;
}
template<typename c_Key, typename c_Data> bool TaggedCache<c_Key, c_Data>::touch(const key_type& key)

View File

@@ -13,7 +13,7 @@
#define CACHED_TRANSACTION_AGE 1800
#endif
TransactionMaster::TransactionMaster() : mCache(CACHED_TRANSACTION_NUM, CACHED_TRANSACTION_AGE)
TransactionMaster::TransactionMaster() : mCache("TransactionCache", CACHED_TRANSACTION_NUM, CACHED_TRANSACTION_AGE)
{
;
}