]>
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(map
<string
, AtomicCounter
*>::const_iterator i
=d_stats
.begin();
60 o
<<i
->first
<<"="<<*(i
->second
)<<",";
64 for(const funcstats_t::value_type
& val
: d_funcstats
) {
65 o
<< val
.first
<<"="<<val
.second(val
.first
)<<",";
72 vector
<string
>StatBag::getEntries()
76 for(map
<string
, AtomicCounter
*>::const_iterator i
=d_stats
.begin();
79 ret
.push_back(i
->first
);
81 for(const funcstats_t::value_type
& val
: d_funcstats
) {
82 ret
.push_back(val
.first
);
90 string
StatBag::getDescrip(const string
&item
)
93 return d_keyDescrips
[item
];
96 void StatBag::declare(const string
&key
, const string
&descrip
)
98 AtomicCounter
*i
=new AtomicCounter(0);
100 d_keyDescrips
[key
]=descrip
;
103 void StatBag::declare(const string
&key
, const string
&descrip
, StatBag::func_t func
)
106 d_funcstats
[key
]=func
;
107 d_keyDescrips
[key
]=descrip
;
111 void StatBag::set(const string
&key
, unsigned long value
)
114 d_stats
[key
]->store(value
);
117 unsigned long StatBag::read(const string
&key
)
120 funcstats_t::const_iterator iter
= d_funcstats
.find(key
);
121 if(iter
!= d_funcstats
.end())
122 return iter
->second(iter
->first
);
123 return *d_stats
[key
];
126 unsigned long StatBag::readZero(const string
&key
)
129 unsigned long tmp
=*d_stats
[key
];
135 string
StatBag::getValueStr(const string
&key
)
142 string
StatBag::getValueStrZero(const string
&key
)
149 AtomicCounter
*StatBag::getPointer(const string
&key
)
157 for(map
<string
, AtomicCounter
*>::const_iterator i
=d_stats
.begin();
166 template<typename T
, typename Comp
>
167 StatRing
<T
,Comp
>::StatRing(unsigned int size
)
169 d_items
.set_capacity(size
);
170 pthread_mutex_init(&d_lock
, 0);
173 template<typename T
, typename Comp
>
174 void StatRing
<T
,Comp
>::account(const T
& t
)
177 d_items
.push_back(t
);
180 template<typename T
, typename Comp
>
181 unsigned int StatRing
<T
,Comp
>::getSize()
184 return d_items
.capacity();
187 template<typename T
, typename Comp
>
188 void StatRing
<T
,Comp
>::resize(unsigned int newsize
)
191 d_items
.set_capacity(newsize
);
195 template<typename T
, typename Comp
>
196 void StatRing
<T
,Comp
>::setHelp(const string
&str
)
201 template<typename T
, typename Comp
>
202 string StatRing
<T
,Comp
>::getHelp()
208 template<typename T
, typename Comp
>
209 vector
<pair
<T
, unsigned int> >StatRing
<T
,Comp
>::get() const
212 map
<T
,unsigned int, Comp
> res
;
213 for(typename
boost::circular_buffer
<T
>::const_iterator i
=d_items
.begin();i
!=d_items
.end();++i
) {
217 vector
<pair
<T
,unsigned int> > tmp
;
218 for(typename map
<T
, unsigned int>::const_iterator i
=res
.begin();i
!=res
.end();++i
)
221 sort(tmp
.begin(),tmp
.end(),popisort
);
226 void StatBag::declareRing(const string
&name
, const string
&help
, unsigned int size
)
228 d_rings
[name
]=StatRing
<string
>(size
);
229 d_rings
[name
].setHelp(help
);
232 void StatBag::declareComboRing(const string
&name
, const string
&help
, unsigned int size
)
234 d_comborings
[name
]=StatRing
<SComboAddress
>(size
);
235 d_comborings
[name
].setHelp(help
);
239 vector
<pair
<string
, unsigned int> > StatBag::getRing(const string
&name
)
241 if(d_rings
.count(name
))
242 return d_rings
[name
].get();
244 typedef pair
<SComboAddress
, unsigned int> stor_t
;
245 vector
<stor_t
> raw
=d_comborings
[name
].get();
246 vector
<pair
<string
, unsigned int> > ret
;
247 for(const stor_t
& stor
: raw
) {
248 ret
.push_back(make_pair(stor
.first
.ca
.toString(), stor
.second
));
255 template<typename T
, typename Comp
>
256 void StatRing
<T
,Comp
>::reset()
262 void StatBag::resetRing(const string
&name
)
264 if(d_rings
.count(name
))
265 d_rings
[name
].reset();
267 d_comborings
[name
].reset();
270 void StatBag::resizeRing(const string
&name
, unsigned int newsize
)
272 if(d_rings
.count(name
))
273 d_rings
[name
].resize(newsize
);
275 d_comborings
[name
].resize(newsize
);
279 unsigned int StatBag::getRingSize(const string
&name
)
281 if(d_rings
.count(name
))
282 return d_rings
[name
].getSize();
284 return d_comborings
[name
].getSize();
287 string
StatBag::getRingTitle(const string
&name
)
289 if(d_rings
.count(name
))
290 return d_rings
[name
].getHelp();
292 return d_comborings
[name
].getHelp();
295 vector
<string
>StatBag::listRings()
298 for(map
<string
,StatRing
<string
> >::const_iterator i
=d_rings
.begin();i
!=d_rings
.end();++i
)
299 ret
.push_back(i
->first
);
300 for(map
<string
,StatRing
<SComboAddress
> >::const_iterator i
=d_comborings
.begin();i
!=d_comborings
.end();++i
)
301 ret
.push_back(i
->first
);
306 bool StatBag::ringExists(const string
&name
)
308 return d_rings
.count(name
) || d_comborings
.count(name
);
311 template class StatRing
<std::string
>;
312 template class StatRing
<SComboAddress
>;