]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/statbag.cc
2 PowerDNS Versatile Database Driven Nameserver
3 Copyright (C) 2002 - 2014 PowerDNS.COM BV
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2
7 as published by the Free Software Foundation
9 Additionally, the license of this program contains a special
10 exception which allows to distribute the program in binary form when
11 it is linked against OpenSSL.
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 St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "pdnsexception.hh"
32 #include "arguments.hh"
35 #include <boost/foreach.hpp>
37 #include "namespaces.hh"
44 void StatBag::exists(const string
&key
)
46 if(!d_keyDescrips
.count(key
))
48 throw PDNSException("Trying to deposit into unknown StatBag key '"+key
+"'");
52 string
StatBag::directory()
57 for(map
<string
, AtomicCounter
*>::const_iterator i
=d_stats
.begin();
61 o
<<i
->first
<<"="<<*(i
->second
)<<",";
65 BOOST_FOREACH(const funcstats_t::value_type
& val
, d_funcstats
) {
66 o
<< val
.first
<<"="<<val
.second(val
.first
)<<",";
73 vector
<string
>StatBag::getEntries()
77 for(map
<string
, AtomicCounter
*>::const_iterator i
=d_stats
.begin();
80 ret
.push_back(i
->first
);
82 BOOST_FOREACH(const funcstats_t::value_type
& val
, d_funcstats
) {
83 ret
.push_back(val
.first
);
91 string
StatBag::getDescrip(const string
&item
)
94 return d_keyDescrips
[item
];
97 void StatBag::declare(const string
&key
, const string
&descrip
)
99 AtomicCounter
*i
=new AtomicCounter(0);
101 d_keyDescrips
[key
]=descrip
;
104 void StatBag::declare(const string
&key
, const string
&descrip
, StatBag::func_t func
)
107 d_funcstats
[key
]=func
;
108 d_keyDescrips
[key
]=descrip
;
112 void StatBag::set(const string
&key
, AtomicCounter::native_t value
)
115 *d_stats
[key
]=AtomicCounter(value
);
118 AtomicCounter::native_t
StatBag::read(const string
&key
)
121 funcstats_t::const_iterator iter
= d_funcstats
.find(key
);
122 if(iter
!= d_funcstats
.end())
123 return iter
->second(iter
->first
);
124 return *d_stats
[key
];
127 AtomicCounter::native_t
StatBag::readZero(const string
&key
)
130 AtomicCounter::native_t tmp
=*d_stats
[key
];
136 string
StatBag::getValueStr(const string
&key
)
143 string
StatBag::getValueStrZero(const string
&key
)
150 AtomicCounter
*StatBag::getPointer(const string
&key
)
158 for(map
<string
, AtomicCounter
*>::const_iterator i
=d_stats
.begin();
167 template<typename T
, typename Comp
>
168 StatRing
<T
,Comp
>::StatRing(unsigned int size
)
170 d_items
.set_capacity(size
);
171 pthread_mutex_init(&d_lock
, 0);
174 template<typename T
, typename Comp
>
175 void StatRing
<T
,Comp
>::account(const T
& t
)
178 d_items
.push_back(t
);
181 template<typename T
, typename Comp
>
182 unsigned int StatRing
<T
,Comp
>::getSize()
185 return d_items
.capacity();
188 template<typename T
, typename Comp
>
189 void StatRing
<T
,Comp
>::resize(unsigned int newsize
)
192 d_items
.set_capacity(newsize
);
196 template<typename T
, typename Comp
>
197 void StatRing
<T
,Comp
>::setHelp(const string
&str
)
202 template<typename T
, typename Comp
>
203 string StatRing
<T
,Comp
>::getHelp()
209 template<typename T
, typename Comp
>
210 vector
<pair
<T
, unsigned int> >StatRing
<T
,Comp
>::get() const
213 map
<T
,unsigned int, Comp
> res
;
214 for(typename
boost::circular_buffer
<T
>::const_iterator i
=d_items
.begin();i
!=d_items
.end();++i
) {
218 vector
<pair
<T
,unsigned int> > tmp
;
219 for(typename map
<T
, unsigned int>::const_iterator i
=res
.begin();i
!=res
.end();++i
)
222 sort(tmp
.begin(),tmp
.end(),popisort
);
227 void StatBag::declareRing(const string
&name
, const string
&help
, unsigned int size
)
229 d_rings
[name
]=StatRing
<string
>(size
);
230 d_rings
[name
].setHelp(help
);
233 void StatBag::declareComboRing(const string
&name
, const string
&help
, unsigned int size
)
235 d_comborings
[name
]=StatRing
<SComboAddress
>(size
);
236 d_comborings
[name
].setHelp(help
);
240 vector
<pair
<string
, unsigned int> > StatBag::getRing(const string
&name
)
242 if(d_rings
.count(name
))
243 return d_rings
[name
].get();
245 typedef pair
<SComboAddress
, unsigned int> stor_t
;
246 vector
<stor_t
> raw
=d_comborings
[name
].get();
247 vector
<pair
<string
, unsigned int> > ret
;
248 BOOST_FOREACH(const stor_t
& stor
, raw
) {
249 ret
.push_back(make_pair(stor
.first
.ca
.toString(), stor
.second
));
256 template<typename T
, typename Comp
>
257 void StatRing
<T
,Comp
>::reset()
263 void StatBag::resetRing(const string
&name
)
265 if(d_rings
.count(name
))
266 d_rings
[name
].reset();
268 d_comborings
[name
].reset();
271 void StatBag::resizeRing(const string
&name
, unsigned int newsize
)
273 if(d_rings
.count(name
))
274 d_rings
[name
].resize(newsize
);
276 d_comborings
[name
].resize(newsize
);
280 unsigned int StatBag::getRingSize(const string
&name
)
282 if(d_rings
.count(name
))
283 return d_rings
[name
].getSize();
285 return d_comborings
[name
].getSize();
288 string
StatBag::getRingTitle(const string
&name
)
290 if(d_rings
.count(name
))
291 return d_rings
[name
].getHelp();
293 return d_comborings
[name
].getHelp();
296 vector
<string
>StatBag::listRings()
299 for(map
<string
,StatRing
<string
> >::const_iterator i
=d_rings
.begin();i
!=d_rings
.end();++i
)
300 ret
.push_back(i
->first
);
301 for(map
<string
,StatRing
<SComboAddress
> >::const_iterator i
=d_comborings
.begin();i
!=d_comborings
.end();++i
)
302 ret
.push_back(i
->first
);
307 bool StatBag::ringExists(const string
&name
)
309 return d_rings
.count(name
) || d_comborings
.count(name
);
312 template class StatRing
<std::string
>;
313 template class StatRing
<SComboAddress
>;