Trim some unnecessary locks from root, and cleanup elsewhere.

This commit is contained in:
Adam Ierymenko 2019-09-16 14:48:27 -07:00
parent e08fc81397
commit e245eb1eb5
No known key found for this signature in database
GPG key ID: C8877CF2D7A5D7F3
4 changed files with 101 additions and 72 deletions

View file

@ -157,6 +157,12 @@
#define ZT_PACKED_STRUCT(D) D __attribute__((packed))
#endif
#if __cplusplus > 199711L
#ifndef __CPP11__
#define __CPP11__
#endif
#endif
/**
* Length of a ZeroTier address in bytes
*/

View file

@ -16,6 +16,7 @@
#include "Constants.hpp"
#include "Mutex.hpp"
#include "AtomicCounter.hpp"
#define ZT_METER_HISTORY_LENGTH 4
#define ZT_METER_HISTORY_TICK_DURATION 1000
@ -35,40 +36,23 @@ public:
_ts = 0;
_count = 0;
}
ZT_ALWAYS_INLINE Meter(const Meter &m) { *this = m; }
ZT_ALWAYS_INLINE Meter &operator=(const Meter &m)
{
m._lock.lock();
for(int i=0;i<ZT_METER_HISTORY_LENGTH;++i)
_history[i] = m._history[i];
_ts = m._ts;
_count = m._count;
m._lock.unlock();
return *this;
}
template<typename I>
ZT_ALWAYS_INLINE void log(const int64_t now,I count)
{
_lock.lock();
const int64_t since = now - _ts;
if (since >= ZT_METER_HISTORY_TICK_DURATION) {
_ts = now;
for(int i=1;i<ZT_METER_HISTORY_LENGTH;++i)
_history[i-1] = _history[i];
_history[ZT_METER_HISTORY_LENGTH-1] = (double)_count / ((double)since / 1000.0);
_count = 0;
_history[(unsigned int)(++_hptr) % ZT_METER_HISTORY_LENGTH] = (double)_count / ((double)since / 1000.0);
_count = (uint64_t)count;
} else {
_count += (uint64_t)count;
}
_count += (uint64_t)count;
_lock.unlock();
}
ZT_ALWAYS_INLINE double perSecond(const int64_t now) const
{
double r = 0.0,n = 0.0;
_lock.lock();
const int64_t since = (now - _ts);
if (since >= ZT_METER_HISTORY_TICK_DURATION) {
r += (double)_count / ((double)since / 1000.0);
@ -78,16 +62,14 @@ public:
r += _history[i];
n += 1.0;
}
_lock.unlock();
return r / n;
}
private:
double _history[ZT_METER_HISTORY_LENGTH];
int64_t _ts;
uint64_t _count;
Mutex _lock;
volatile double _history[ZT_METER_HISTORY_LENGTH];
volatile int64_t _ts;
volatile uint64_t _count;
volatile AtomicCounter _hptr;
};
} // namespace ZeroTier

View file

@ -46,6 +46,17 @@ class RuntimeEnvironment;
*/
class Topology
{
private:
static _RootRankingFunction
{
ZT_ALWAYS_INLINE _RootRankingFunction() : bestRoot(),bestRootLatency(0xffff) {}
ZT_ALWAYS_INLINE bool operator()(const SharedPtr<Peer> &peer,const std::vector<InetAddress> &phy)
{
}
SharedPtr<Peer> bestRoot;
unsigned int bestRootLatency;
};
public:
ZT_ALWAYS_INLINE Topology(const RuntimeEnvironment *renv,const Identity &myId) :
RR(renv),
@ -296,9 +307,80 @@ public:
}
#endif
ZT_ALWAYS_INLINE SharedPtr<Peer> root(const int64_t now)
/**
* Apply a function or function object to all roots
*
* This locks the root list during execution but other operations
* are fine.
*
* @param f Function to apply f(peer,IPs)
* @tparam F function or function object type
*/
template<typename F>
inline void eachRoot(F f)
{
return SharedPtr<Peer>();
{
Mutex::Lock l(_staticRoots_l);
Hashtable< Identity,std::vector<InetAddress> >::Iterator i(_staticRoots);
Identity *k = (Identity *)0;
std::vector<InetAddress> *v = (std::vector<InetAddress> *)0;
while (i.next(k,v)) {
if (!v->empty()) {
const SharedPtr<Peer> *ap;
{
Mutex::Lock l2(_peers_l);
ap = _peers.get(k->address());
}
if (ap) {
if (!f(*ap,*v))
return;
} else {
SharedPtr<Peer> p(new Peer(RR,_myIdentity,*k));
{
Mutex::Lock l2(_peers_l);
_peers.set(k->address(),p);
}
if (!f(p,*v))
return;
}
}
}
}
{
Mutex::Lock l(_dynamicRoots_l);
Hashtable< Str,Locator >::Iterator i(_dynamicRoots);
Str *k = (Str *)0;
Locator *v = (Locator *)0;
while (i.next(k,v)) {
if (*v) {
for(std::vector<Identity>::const_iterator id(v->virt().begin());id!=v->virt().end();++id) {
const SharedPtr<Peer> *ap;
{
Mutex::Lock l2(_peers_l);
ap = _peers.get(id->address());
}
if (ap) {
if (!f(*ap,v->phy()))
return;
} else {
SharedPtr<Peer> p(new Peer(RR,_myIdentity,*id));
{
Mutex::Lock l2(_peers_l);
_peers.set(id->address(),p);
}
if (!f(p,v->phy()))
return;
}
}
}
}
}
}
inline SharedPtr<Peer> root(const int64_t now)
{
_RootRankingFunc rrf;
eachRoot(rrf);
}
/**
@ -487,12 +569,6 @@ private:
Hashtable< Identity,std::vector<InetAddress> > _staticRoots;
Hashtable< Str,Locator > _dynamicRoots;
//std::vector<Root> _roots;
//SharedPtr<Peer> _bestRoot;
//int64_t _lastRankedBestRoot;
//Mutex _roots_m;
//Mutex _bestRoot_m;
Mutex _peers_l;
Mutex _paths_l;
Mutex _staticRoots_l;