From: bert hubert Date: Thu, 3 Dec 2015 19:21:16 +0000 (+0100) Subject: move ring stuff to own file, so it can share between dnsdist-lua and dnsdist-web... X-Git-Tag: dnsdist-1.0.0-alpha1~126^2~3 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=03ebf8b237524e00c10c918e023a5bca39f5f0f1;p=pdns move ring stuff to own file, so it can share between dnsdist-lua and dnsdist-web, plus we get the locking in one place --- diff --git a/pdns/Makefile.am b/pdns/Makefile.am index ab4c12522..118727b78 100644 --- a/pdns/Makefile.am +++ b/pdns/Makefile.am @@ -617,6 +617,7 @@ dnsdist_SOURCES = \ dnsdist-carbon.cc \ dnsdist-lua.cc \ dnsdist-lua2.cc \ + dnsdist-rings.cc \ dnsdist-tcp.cc \ dnsdist-web.cc \ dnslabeltext.cc \ diff --git a/pdns/dnsdist-lua.cc b/pdns/dnsdist-lua.cc index 0b9931342..a5ff50334 100644 --- a/pdns/dnsdist-lua.cc +++ b/pdns/dnsdist-lua.cc @@ -653,7 +653,7 @@ vector> setupLua(bool client, const std::string& confi } }); - // something needs to be done about this, unlocked will 'mostly' work + g_lua.writeFunction("topClients", [](unsigned int top) { map counts; unsigned int total=0; diff --git a/pdns/dnsdist-rings.cc b/pdns/dnsdist-rings.cc new file mode 100644 index 000000000..828935a32 --- /dev/null +++ b/pdns/dnsdist-rings.cc @@ -0,0 +1,38 @@ +#include "dnsdist.hh" +#include "lock.hh" + +unsigned int Rings::numDistinctRequestors() +{ + std::set s; + WriteLock wl(&queryLock); + for(const auto& q : queryRing) + s.insert(q.requestor); + return s.size(); +} + +vector > Rings::getTopBandwidth(int numentries) +{ + map counts; + { + WriteLock wl(&queryLock); + for(const auto& q : queryRing) + counts[q.requestor]+=q.size; + } + + { + std::lock_guard lock(respMutex); + for(const auto& r : respRing) + counts[r.requestor]+=r.size; + } + + typedef vector> ret_t; + ret_t ret; + for(const auto& p : counts) + ret.push_back({p.second, p.first}); + partial_sort(ret.begin(), ret.begin()+numentries, ret.end(), [](const ret_t::value_type&a, const ret_t::value_type&b) + { + return(b.second < a.second); + }); + ret.resize(numentries); + return ret; +} diff --git a/pdns/dnsdist.hh b/pdns/dnsdist.hh index 54e56e64f..edc9d15f0 100644 --- a/pdns/dnsdist.hh +++ b/pdns/dnsdist.hh @@ -194,6 +194,7 @@ struct Rings { struct timespec when; ComboAddress requestor; DNSName name; + uint16_t size; uint16_t qtype; }; boost::circular_buffer queryRing; @@ -210,6 +211,9 @@ struct Rings { boost::circular_buffer respRing; std::mutex respMutex; pthread_rwlock_t queryLock; + + vector > getTopBandwidth(int numentries); + unsigned int numDistinctRequestors(); }; extern Rings g_rings;