mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-11 02:25:34 -04:00
* Added the basics of GxsCircles service to libretroshare.
- Defined the control group stuff. - Background task to determine friend membership. - Caching of Circle info, - Added GXS interface class to gxs/rsgixs.h - TODO: Serialiser is incomplete. - TODO: SubCircles to be done in Phase 2. * Improvements to RsMemCache: - Added Value& ref(Key) to avoid data copying. - Added Statistics to check cache performance. - Fixed up bugs in tracking membership. * Improvements to RsTickEvent: - Added additional string parameter for more specificity. git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-gxs-b1@5910 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
baa949eaac
commit
5a55c1b5d6
18 changed files with 2353 additions and 427 deletions
|
@ -29,6 +29,7 @@
|
|||
#include <time.h>
|
||||
#include <iostream>
|
||||
#include <inttypes.h>
|
||||
#include <string>
|
||||
|
||||
/************************************************************************************/
|
||||
/************************************************************************************/
|
||||
|
@ -49,11 +50,16 @@ template<class Key, class Value> class RsMemCache
|
|||
{
|
||||
public:
|
||||
|
||||
RsMemCache(uint32_t max_size = DEFAULT_MEM_CACHE_SIZE)
|
||||
:mDataCount(0), mMaxSize(max_size) { return; }
|
||||
RsMemCache(uint32_t max_size = DEFAULT_MEM_CACHE_SIZE, std::string name = "UnknownMemCache")
|
||||
:mDataCount(0), mMaxSize(max_size), mName(name)
|
||||
{
|
||||
clearStats();
|
||||
return;
|
||||
}
|
||||
|
||||
bool is_cached(const Key &key) const;
|
||||
bool fetch(const Key &key, Value &data);
|
||||
Value &ref(const Key &key); // like map[] installs empty one if non-existent.
|
||||
bool store(const Key &key, const Value &data);
|
||||
|
||||
bool resize(); // should be called periodically to cleanup old entries.
|
||||
|
@ -80,6 +86,18 @@ template<class Key, class Value> class RsMemCache
|
|||
std::multimap<time_t, Key> mLruMap;
|
||||
uint32_t mDataCount;
|
||||
uint32_t mMaxSize;
|
||||
std::string mName;
|
||||
|
||||
// some statistics.
|
||||
void printStats(std::ostream &out);
|
||||
void clearStats();
|
||||
|
||||
mutable uint32_t mStats_inserted;
|
||||
mutable uint32_t mStats_dropped;
|
||||
mutable uint32_t mStats_iscached;
|
||||
mutable uint32_t mStats_cachemiss;
|
||||
mutable uint32_t mStats_access;
|
||||
mutable uint32_t mStats_accessmiss;
|
||||
};
|
||||
|
||||
|
||||
|
@ -92,10 +110,12 @@ template<class Key, class Value> bool RsMemCache<Key, Value>::is_cached(const Ke
|
|||
std::cerr << "RsMemCache::is_cached(" << key << ") false";
|
||||
std::cerr << std::endl;
|
||||
|
||||
mStats_cachemiss++;
|
||||
return false;
|
||||
}
|
||||
std::cerr << "RsMemCache::is_cached(" << key << ") false";
|
||||
std::cerr << std::endl;
|
||||
mStats_iscached++;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
@ -103,6 +123,7 @@ template<class Key, class Value> bool RsMemCache<Key, Value>::is_cached(const Ke
|
|||
|
||||
template<class Key, class Value> bool RsMemCache<Key, Value>::fetch(const Key &key, Value &data)
|
||||
{
|
||||
printStats(std::cerr);
|
||||
typename std::map<Key, cache_data>::iterator it;
|
||||
it = mDataMap.find(key);
|
||||
if (it == mDataMap.end())
|
||||
|
@ -110,6 +131,7 @@ template<class Key, class Value> bool RsMemCache<Key, Value>::fetch(const Key &k
|
|||
std::cerr << "RsMemCache::fetch(" << key << ") false";
|
||||
std::cerr << std::endl;
|
||||
|
||||
mStats_accessmiss++;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -125,13 +147,54 @@ template<class Key, class Value> bool RsMemCache<Key, Value>::fetch(const Key &k
|
|||
|
||||
update_lrumap(key, old_ts, new_ts);
|
||||
|
||||
mStats_access++;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
template<class Key, class Value> Value &RsMemCache<Key, Value>::ref(const Key &key)
|
||||
{
|
||||
printStats(std::cerr);
|
||||
typename std::map<Key, cache_data>::iterator it;
|
||||
it = mDataMap.find(key);
|
||||
if (it == mDataMap.end())
|
||||
{
|
||||
std::cerr << "RsMemCache::ref(" << key << ") ERROR missing Key inserting Empty Data in LRU slot";
|
||||
std::cerr << std::endl;
|
||||
|
||||
// insert operation.
|
||||
time_t new_ts = 0;
|
||||
Value data;
|
||||
mDataMap[key] = cache_data(key, data, new_ts);
|
||||
mDataCount++;
|
||||
|
||||
update_lrumap(key, 0, new_ts);
|
||||
it = mDataMap.find(key);
|
||||
|
||||
mStats_accessmiss++;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "RsMemCache::ref(" << key << ") OK";
|
||||
std::cerr << std::endl;
|
||||
|
||||
/* update ts on data */
|
||||
time_t old_ts = it->second.ts;
|
||||
time_t new_ts = time(NULL);
|
||||
it->second.ts = new_ts;
|
||||
|
||||
update_lrumap(key, old_ts, new_ts);
|
||||
|
||||
mStats_access++;
|
||||
}
|
||||
return it->second.data;
|
||||
}
|
||||
|
||||
template<class Key, class Value> bool RsMemCache<Key, Value>::store(const Key &key, const Value &data)
|
||||
{
|
||||
std::cerr << "RsMemCache::store()";
|
||||
std::cerr << std::endl;
|
||||
printStats(std::cerr);
|
||||
|
||||
/* update lrumap entry */
|
||||
time_t old_ts = 0;
|
||||
|
@ -148,13 +211,16 @@ template<class Key, class Value> bool RsMemCache<Key, Value>::store(const Key &k
|
|||
|
||||
old_ts = it->second.ts;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
mDataCount++;
|
||||
}
|
||||
|
||||
mDataMap[key] = cache_data(key, data, new_ts);
|
||||
mDataCount++;
|
||||
|
||||
update_lrumap(key, old_ts, new_ts);
|
||||
|
||||
mStats_inserted++;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -203,6 +269,7 @@ template<class Key, class Value> bool RsMemCache<Key, Value>::resize()
|
|||
{
|
||||
std::cerr << "RsMemCache::resize()";
|
||||
std::cerr << std::endl;
|
||||
printStats(std::cerr);
|
||||
|
||||
int count_to_clear = 0;
|
||||
{
|
||||
|
@ -259,6 +326,7 @@ template<class Key, class Value> bool RsMemCache<Key, Value>::discard_LRU(int co
|
|||
std::cerr << std::endl;
|
||||
mDataMap.erase(it);
|
||||
mDataCount--;
|
||||
mStats_dropped++;
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -273,6 +341,39 @@ template<class Key, class Value> bool RsMemCache<Key, Value>::discard_LRU(int co
|
|||
return true;
|
||||
}
|
||||
|
||||
// These aren't templated functions.
|
||||
template<class Key, class Value> void RsMemCache<Key, Value>::printStats(std::ostream &out)
|
||||
{
|
||||
typename std::multimap<time_t, Key>::iterator mit = mLruMap.begin();
|
||||
time_t age = 0;
|
||||
if (mit != mLruMap.end())
|
||||
{
|
||||
age = time(NULL) - mit->first;
|
||||
}
|
||||
|
||||
out << "RsMemCache<" << mName << ">::printStats() Size: " << mDataCount << " Size2: " << mDataMap.size() << " Size3: " << mLruMap.size() << " MaxSize: " << mMaxSize << " LRU Age: " << age;
|
||||
out << std::endl;
|
||||
|
||||
out << "\tInsertions: " << mStats_inserted << " Drops: " << mStats_dropped;
|
||||
out << std::endl;
|
||||
|
||||
out << "\tCache Hits: " << mStats_iscached << " Misses: " << mStats_cachemiss;
|
||||
out << std::endl;
|
||||
|
||||
out << "\tAccess Hits: " << mStats_access << " Misses: " << mStats_accessmiss;
|
||||
out << std::endl;
|
||||
}
|
||||
|
||||
template<class Key, class Value> void RsMemCache<Key, Value>::clearStats()
|
||||
{
|
||||
mStats_inserted = 0;
|
||||
mStats_dropped = 0;
|
||||
mStats_iscached = 0;
|
||||
mStats_cachemiss = 0;
|
||||
mStats_access = 0;
|
||||
mStats_accessmiss = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue