]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/statbag.cc
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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.
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.
27 #include "pdnsexception.hh"
31 #include "arguments.hh"
36 #include "namespaces.hh"
43 void StatBag::exists(const string
&key
)
45 if(!d_keyDescrips
.count(key
))
47 throw PDNSException("Trying to deposit into unknown StatBag key '"+key
+"'");
51 string
StatBag::directory()
56 for(const auto& i
: d_stats
) {
57 o
<<i
.first
<<"="<<*(i
.second
)<<",";
61 for(const funcstats_t::value_type
& val
: d_funcstats
) {
62 o
<< val
.first
<<"="<<val
.second(val
.first
)<<",";
69 vector
<string
>StatBag::getEntries()
73 for(const auto& i
: d_stats
) {
74 ret
.push_back(i
.first
);
77 for(const funcstats_t::value_type
& val
: d_funcstats
) {
78 ret
.push_back(val
.first
);
86 string
StatBag::getDescrip(const string
&item
)
89 return d_keyDescrips
[item
];
92 void StatBag::declare(const string
&key
, const string
&descrip
)
94 AtomicCounter
*i
=new AtomicCounter(0);
96 d_keyDescrips
[key
]=descrip
;
99 void StatBag::declare(const string
&key
, const string
&descrip
, StatBag::func_t func
)
102 d_funcstats
[key
]=func
;
103 d_keyDescrips
[key
]=descrip
;
107 void StatBag::set(const string
&key
, unsigned long value
)
110 d_stats
[key
]->store(value
);
113 unsigned long StatBag::read(const string
&key
)
116 funcstats_t::const_iterator iter
= d_funcstats
.find(key
);
117 if(iter
!= d_funcstats
.end())
118 return iter
->second(iter
->first
);
119 return *d_stats
[key
];
122 unsigned long StatBag::readZero(const string
&key
)
125 unsigned long tmp
=*d_stats
[key
];
131 string
StatBag::getValueStr(const string
&key
)
138 string
StatBag::getValueStrZero(const string
&key
)
145 AtomicCounter
*StatBag::getPointer(const string
&key
)
153 for(const auto& i
: d_stats
) {
159 template<typename T
, typename Comp
>
160 StatRing
<T
,Comp
>::StatRing(unsigned int size
)
162 d_items
.set_capacity(size
);
163 pthread_mutex_init(&d_lock
, 0);
166 template<typename T
, typename Comp
>
167 void StatRing
<T
,Comp
>::account(const T
& t
)
170 d_items
.push_back(t
);
173 template<typename T
, typename Comp
>
174 unsigned int StatRing
<T
,Comp
>::getSize()
177 return d_items
.capacity();
180 template<typename T
, typename Comp
>
181 void StatRing
<T
,Comp
>::resize(unsigned int newsize
)
184 d_items
.set_capacity(newsize
);
188 template<typename T
, typename Comp
>
189 void StatRing
<T
,Comp
>::setHelp(const string
&str
)
194 template<typename T
, typename Comp
>
195 string StatRing
<T
,Comp
>::getHelp()
201 template<typename T
, typename Comp
>
202 vector
<pair
<T
, unsigned int> >StatRing
<T
,Comp
>::get() const
205 map
<T
,unsigned int, Comp
> res
;
206 for(typename
boost::circular_buffer
<T
>::const_iterator i
=d_items
.begin();i
!=d_items
.end();++i
) {
210 vector
<pair
<T
,unsigned int> > tmp
;
211 for(typename map
<T
, unsigned int>::const_iterator i
=res
.begin();i
!=res
.end();++i
)
214 sort(tmp
.begin(),tmp
.end(),popisort
);
219 void StatBag::declareRing(const string
&name
, const string
&help
, unsigned int size
)
221 d_rings
[name
]=StatRing
<string
, CIStringCompare
>(size
);
222 d_rings
[name
].setHelp(help
);
225 void StatBag::declareComboRing(const string
&name
, const string
&help
, unsigned int size
)
227 d_comborings
[name
]=StatRing
<SComboAddress
>(size
);
228 d_comborings
[name
].setHelp(help
);
232 vector
<pair
<string
, unsigned int> > StatBag::getRing(const string
&name
)
234 if(d_rings
.count(name
))
235 return d_rings
[name
].get();
237 typedef pair
<SComboAddress
, unsigned int> stor_t
;
238 vector
<stor_t
> raw
=d_comborings
[name
].get();
239 vector
<pair
<string
, unsigned int> > ret
;
240 for(const stor_t
& stor
: raw
) {
241 ret
.push_back(make_pair(stor
.first
.ca
.toString(), stor
.second
));
248 template<typename T
, typename Comp
>
249 void StatRing
<T
,Comp
>::reset()
255 void StatBag::resetRing(const string
&name
)
257 if(d_rings
.count(name
))
258 d_rings
[name
].reset();
260 d_comborings
[name
].reset();
263 void StatBag::resizeRing(const string
&name
, unsigned int newsize
)
265 if(d_rings
.count(name
))
266 d_rings
[name
].resize(newsize
);
268 d_comborings
[name
].resize(newsize
);
272 unsigned int StatBag::getRingSize(const string
&name
)
274 if(d_rings
.count(name
))
275 return d_rings
[name
].getSize();
277 return d_comborings
[name
].getSize();
280 string
StatBag::getRingTitle(const string
&name
)
282 if(d_rings
.count(name
))
283 return d_rings
[name
].getHelp();
285 return d_comborings
[name
].getHelp();
288 vector
<string
>StatBag::listRings()
291 for(auto i
=d_rings
.begin();i
!=d_rings
.end();++i
)
292 ret
.push_back(i
->first
);
293 for(auto i
=d_comborings
.begin();i
!=d_comborings
.end();++i
)
294 ret
.push_back(i
->first
);
299 bool StatBag::ringExists(const string
&name
)
301 return d_rings
.count(name
) || d_comborings
.count(name
);
304 template class StatRing
<std::string
, CIStringCompare
>;
305 template class StatRing
<SComboAddress
>;