mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-02 14:16:16 -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -67,16 +67,16 @@ void RsTickEvent::tick_events()
|
|||
}
|
||||
}
|
||||
|
||||
std::list<uint32_t> toProcess;
|
||||
std::list<uint32_t>::iterator it;
|
||||
std::list<EventData> toProcess;
|
||||
std::list<EventData>::iterator it;
|
||||
|
||||
{
|
||||
RsStackMutex stack(mEventMtx); /********** STACK LOCKED MTX ******/
|
||||
while((!mEvents.empty()) && (mEvents.begin()->first <= now))
|
||||
{
|
||||
std::multimap<time_t, uint32_t>::iterator it = mEvents.begin();
|
||||
uint32_t event_type = it->second;
|
||||
toProcess.push_back(event_type);
|
||||
std::multimap<time_t, EventData>::iterator it = mEvents.begin();
|
||||
uint32_t event_type = it->second.mEventType;
|
||||
toProcess.push_back(it->second);
|
||||
mEvents.erase(it);
|
||||
|
||||
count_adjust_locked(event_type, -1);
|
||||
|
@ -86,40 +86,53 @@ void RsTickEvent::tick_events()
|
|||
for(it = toProcess.begin(); it != toProcess.end(); it++)
|
||||
{
|
||||
std::cerr << "RsTickEvent::tick_events() calling handle_event(";
|
||||
std::cerr << *it << ")";
|
||||
std::cerr << it->mEventType << ", " << it->mEventLabel << ")";
|
||||
std::cerr << std::endl;
|
||||
handle_event(*it);
|
||||
handle_event(it->mEventType, it->mEventLabel);
|
||||
}
|
||||
}
|
||||
|
||||
void RsTickEvent::schedule_now(uint32_t event_type)
|
||||
{
|
||||
RsTickEvent::schedule_in(event_type, 0);
|
||||
std::string elabel;
|
||||
RsTickEvent::schedule_in(event_type, 0, elabel);
|
||||
}
|
||||
|
||||
void RsTickEvent::schedule_event(uint32_t event_type, time_t when)
|
||||
|
||||
void RsTickEvent::schedule_now(uint32_t event_type, const std::string &elabel)
|
||||
{
|
||||
RsTickEvent::schedule_in(event_type, 0, elabel);
|
||||
}
|
||||
|
||||
void RsTickEvent::schedule_event(uint32_t event_type, time_t when, const std::string &elabel)
|
||||
{
|
||||
RsStackMutex stack(mEventMtx); /********** STACK LOCKED MTX ******/
|
||||
mEvents.insert(std::make_pair(when, event_type));
|
||||
mEvents.insert(std::make_pair(when, EventData(event_type, elabel)));
|
||||
|
||||
count_adjust_locked(event_type, 1);
|
||||
}
|
||||
|
||||
void RsTickEvent::schedule_in(uint32_t event_type, uint32_t in_secs)
|
||||
{
|
||||
std::cerr << "RsTickEvent::schedule_in(" << event_type << ") in " << in_secs << " secs";
|
||||
std::cerr << std::endl;
|
||||
|
||||
RsStackMutex stack(mEventMtx); /********** STACK LOCKED MTX ******/
|
||||
time_t event_time = time(NULL) + in_secs;
|
||||
mEvents.insert(std::make_pair(event_time, event_type));
|
||||
|
||||
count_adjust_locked(event_type, 1);
|
||||
std::string elabel;
|
||||
RsTickEvent::schedule_in(event_type, in_secs, elabel);
|
||||
}
|
||||
|
||||
void RsTickEvent::handle_event(uint32_t event_type)
|
||||
|
||||
void RsTickEvent::schedule_in(uint32_t event_type, uint32_t in_secs, const std::string &elabel)
|
||||
{
|
||||
std::cerr << "RsTickEvent::handle_event(" << event_type << ") ERROR Not Handled";
|
||||
std::cerr << "RsTickEvent::schedule_in(" << event_type << ", " << elabel << ") in " << in_secs << " secs";
|
||||
std::cerr << std::endl;
|
||||
|
||||
time_t event_time = time(NULL) + in_secs;
|
||||
RsTickEvent::schedule_event(event_type, event_time, elabel);
|
||||
}
|
||||
|
||||
|
||||
void RsTickEvent::handle_event(uint32_t event_type, const std::string &elabel)
|
||||
{
|
||||
std::cerr << "RsTickEvent::handle_event(" << event_type << ", " << elabel;
|
||||
std::cerr << ") ERROR Not Handled";
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
|
||||
|
|
|
@ -44,24 +44,41 @@ class RsTickEvent
|
|||
void tick_events();
|
||||
|
||||
void schedule_now(uint32_t event_type);
|
||||
void schedule_event(uint32_t event_type, time_t when);
|
||||
void schedule_now(uint32_t event_type, const std::string &elabel);
|
||||
|
||||
void schedule_event(uint32_t event_type, time_t when, const std::string &elabel);
|
||||
|
||||
void schedule_in(uint32_t event_type, uint32_t in_secs);
|
||||
void schedule_in(uint32_t event_type, uint32_t in_secs, const std::string &elabel);
|
||||
|
||||
int32_t event_count(uint32_t event_type);
|
||||
bool prev_event_ago(uint32_t event_type, int32_t &age);
|
||||
|
||||
protected:
|
||||
|
||||
// Overloaded to handle the events.
|
||||
virtual void handle_event(uint32_t event_type);
|
||||
virtual void handle_event(uint32_t event_type, const std::string &event_label);
|
||||
|
||||
private:
|
||||
|
||||
class EventData
|
||||
{
|
||||
public:
|
||||
EventData() :mEventType(0) { return; }
|
||||
EventData(uint32_t etype) :mEventType(etype) { return; }
|
||||
EventData(uint32_t etype, std::string elabel) :mEventLabel(elabel), mEventType(etype) { return; }
|
||||
|
||||
std::string mEventLabel;
|
||||
uint32_t mEventType;
|
||||
};
|
||||
|
||||
void count_adjust_locked(uint32_t event_type, int32_t change);
|
||||
void note_event_locked(uint32_t event_type);
|
||||
|
||||
RsMutex mEventMtx;
|
||||
std::map<uint32_t, int32_t> mEventCount;
|
||||
std::map<uint32_t, time_t> mPreviousEvent;
|
||||
std::multimap<time_t, uint32_t> mEvents;
|
||||
std::multimap<time_t, EventData> mEvents;
|
||||
};
|
||||
|
||||
#endif // RS_UTIL_TICK_EVENT
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue