]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/dnsdist-rings.cc
auth: switch circleci mssql image
[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 */
03b00917
RG
22
23#include "dnsdist-rings.hh"
03ebf8b2 24
a683e8bd 25size_t Rings::numDistinctRequestors()
03ebf8b2 26{
27 std::set<ComboAddress, ComboAddress::addressOnlyLessThan> s;
cdcf7eeb 28 for (const auto& shard : d_shards) {
cdcb5fbd
RG
29 std::lock_guard<std::mutex> rl(shard->queryLock);
30 for(const auto& q : shard->queryRing) {
01f6920b
RG
31 s.insert(q.requestor);
32 }
33 }
03ebf8b2 34 return s.size();
35}
36
7fc00937 37std::unordered_map<int, vector<boost::variant<string,double>>> Rings::getTopBandwidth(unsigned int numentries)
03ebf8b2 38{
39 map<ComboAddress, unsigned int, ComboAddress::addressOnlyLessThan> counts;
7fc00937 40 uint64_t total=0;
cdcf7eeb 41 for (const auto& shard : d_shards) {
01f6920b 42 {
cdcb5fbd
RG
43 std::lock_guard<std::mutex> rl(shard->queryLock);
44 for(const auto& q : shard->queryRing) {
01f6920b
RG
45 counts[q.requestor]+=q.size;
46 total+=q.size;
47 }
7fc00937 48 }
01f6920b 49 {
cdcb5fbd
RG
50 std::lock_guard<std::mutex> rl(shard->respLock);
51 for(const auto& r : shard->respRing) {
01f6920b
RG
52 counts[r.requestor]+=r.size;
53 total+=r.size;
54 }
7fc00937 55 }
03ebf8b2 56 }
57
58 typedef vector<pair<unsigned int, ComboAddress>> ret_t;
7fc00937
RG
59 ret_t rcounts;
60 rcounts.reserve(counts.size());
03ebf8b2 61 for(const auto& p : counts)
7fc00937
RG
62 rcounts.push_back({p.second, p.first});
63 numentries = rcounts.size() < numentries ? rcounts.size() : numentries;
64 partial_sort(rcounts.begin(), rcounts.begin()+numentries, rcounts.end(), [](const ret_t::value_type&a, const ret_t::value_type&b)
03ebf8b2 65 {
7fc00937 66 return(b.first < a.first);
03ebf8b2 67 });
7fc00937
RG
68 std::unordered_map<int, vector<boost::variant<string,double>>> ret;
69 uint64_t rest = 0;
70 unsigned int count = 1;
71 for(const auto& rc : rcounts) {
72 if(count==numentries+1) {
73 rest+=rc.first;
74 }
75 else {
76 ret.insert({count++, {rc.second.toString(), rc.first, 100.0*rc.first/total}});
77 }
78 }
79 ret.insert({count, {"Rest", rest, total > 0 ? 100.0*rest/total : 100.0}});
03ebf8b2 80 return ret;
81}