]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/dnsdist-rings.cc
Merge pull request #5523 from rubenk/fix-typos-in-logmessage
[thirdparty/pdns.git] / pdns / dnsdist-rings.cc
CommitLineData
12471842
PL
1/*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
03ebf8b2 22#include "dnsdist.hh"
23#include "lock.hh"
24
a683e8bd 25size_t Rings::numDistinctRequestors()
03ebf8b2 26{
27 std::set<ComboAddress, ComboAddress::addressOnlyLessThan> s;
7fc00937 28 ReadLock rl(&queryLock);
03ebf8b2 29 for(const auto& q : queryRing)
30 s.insert(q.requestor);
31 return s.size();
32}
33
7fc00937 34std::unordered_map<int, vector<boost::variant<string,double>>> Rings::getTopBandwidth(unsigned int numentries)
03ebf8b2 35{
36 map<ComboAddress, unsigned int, ComboAddress::addressOnlyLessThan> counts;
7fc00937 37 uint64_t total=0;
03ebf8b2 38 {
7fc00937
RG
39 ReadLock rl(&queryLock);
40 for(const auto& q : queryRing) {
03ebf8b2 41 counts[q.requestor]+=q.size;
7fc00937
RG
42 total+=q.size;
43 }
03ebf8b2 44 }
45
46 {
47 std::lock_guard<std::mutex> lock(respMutex);
7fc00937 48 for(const auto& r : respRing) {
03ebf8b2 49 counts[r.requestor]+=r.size;
7fc00937
RG
50 total+=r.size;
51 }
03ebf8b2 52 }
53
54 typedef vector<pair<unsigned int, ComboAddress>> ret_t;
7fc00937
RG
55 ret_t rcounts;
56 rcounts.reserve(counts.size());
03ebf8b2 57 for(const auto& p : counts)
7fc00937
RG
58 rcounts.push_back({p.second, p.first});
59 numentries = rcounts.size() < numentries ? rcounts.size() : numentries;
60 partial_sort(rcounts.begin(), rcounts.begin()+numentries, rcounts.end(), [](const ret_t::value_type&a, const ret_t::value_type&b)
03ebf8b2 61 {
7fc00937 62 return(b.first < a.first);
03ebf8b2 63 });
7fc00937
RG
64 std::unordered_map<int, vector<boost::variant<string,double>>> ret;
65 uint64_t rest = 0;
66 unsigned int count = 1;
67 for(const auto& rc : rcounts) {
68 if(count==numentries+1) {
69 rest+=rc.first;
70 }
71 else {
72 ret.insert({count++, {rc.second.toString(), rc.first, 100.0*rc.first/total}});
73 }
74 }
75 ret.insert({count, {"Rest", rest, total > 0 ? 100.0*rest/total : 100.0}});
03ebf8b2 76 return ret;
77}