4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
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
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief Statistics non-inline non-template implementation */
26 #include "Statistics.hh"
27 //#include "Statistics.ih"
32 #include <senf/Utils/Console/Console.hh>
33 #include "StatisticsTargets.hh"
35 //#include "Statistics.mpp"
37 ///////////////////////////////cc.p////////////////////////////////////////
39 ///////////////////////////////////////////////////////////////////////////
40 // senf::StatisticsBase
42 prefix_ void senf::StatisticsBase::enter(float min, float avg, float max, float dev)
52 prefix_ senf::Collector & senf::StatisticsBase::operator[](unsigned rank)
54 Children::iterator i (children_.find(rank));
55 if (i == children_.end())
56 throw InvalidRankException();
60 prefix_ senf::Collector & senf::StatisticsBase::collect(unsigned rank)
62 std::pair<Children::iterator, bool> state (
63 children_.insert(std::make_pair(rank, Collector(this, rank))) );
65 throw DuplicateRankException();
66 return state.first->second;
69 prefix_ senf::StatisticsBase::OutputProxy<senf::StatisticsBase>
70 senf::StatisticsBase::output(unsigned n)
72 OutputMap::iterator i (outputs_.find(n));
73 if (i == outputs_.end()) {
74 i = outputs_.insert(std::make_pair(n, OutputEntry(n))).first;
76 nm << "output" << path() << ":" << n;
77 base().dir.node().add(nm.str(), i->second.dir);
78 detail::StatisticsLoggerRegistry::instance().apply(*this, n, i->second.dir);
82 return OutputProxy<StatisticsBase>(this, &(i->second));
86 // generate an engineering style notation
88 std::string format_eng( float f)
93 while( f >= 1000.0f) {
99 sprintf( buf, " %3.2fe%+03d", f, n);
101 sprintf( buf, " %3.2f", f);
105 while( f <= -1000.0f) {
110 sprintf( buf, " %3.2fe%+03d", f, n);
112 sprintf( buf, " %3.2f", f);
115 sprintf( buf, " 0.00");
122 prefix_ void senf::StatisticsBase::consoleList(unsigned level, std::ostream & os)
125 os << boost::format("%s%-5d%|15t| %12s %12s+/-%12s %12s\n")
126 % std::string(2*level,' ')
133 OutputMap::const_iterator i (outputs_.begin());
134 OutputMap::const_iterator i_end (outputs_.end());
135 for (; i != i_end; ++i)
136 os << boost::format(" %3d %12s %12s+/-%12s %12s\n")
138 % format_eng(i->second.min/i->second.n)
139 % format_eng(i->second.avg/i->second.n)
140 % format_eng(i->second.dev/i->second.n)
141 % format_eng(i->second.max/i->second.n);
144 Children::const_iterator i (children_.begin());
145 Children::const_iterator const i_end (children_.end());
146 for (; i != i_end; ++i)
147 i->second.consoleList(level+1, os);
151 prefix_ void senf::StatisticsBase::generateOutput()
153 queue_.push_front(QueueEntry(min_, avg_, max_, dev_));
154 while (queue_.size() > maxQueueLen_)
157 OutputMap::iterator i (outputs_.begin());
158 OutputMap::iterator const i_end (outputs_.end());
159 for (; i != i_end; ++i) {
160 i->second.min = i->second.avg = i->second.max = i->second.dev = 0.0f;
161 Queue::const_iterator j (queue_.begin());
162 Queue::const_iterator const j_end (queue_.end());
164 for (; n < i->second.n && j != j_end; ++n, ++j) {
165 i->second.min += j->min;
166 i->second.avg += j->avg;
167 i->second.max += j->max;
168 i->second.dev += j->dev;
170 i->second.signal(i->second.min/n, i->second.avg/n, i->second.max/n, i->second.dev/n);
174 prefix_ void senf::StatisticsBase::signalChildren()
176 Children::iterator i (children_.begin());
177 Children::iterator const i_end (children_.end());
178 for (; i != i_end; ++i)
179 i->second.enter(min_, avg_, max_, dev_);
182 ///////////////////////////////////////////////////////////////////////////
185 prefix_ senf::Statistics::Statistics()
186 #ifndef SENF_DISABLE_CONSOLE
190 #ifndef SENF_DISABLE_CONSOLE
191 dir.add("list", &Statistics::consoleList)
192 .doc("List statistics collection intervals and current values.\n"
195 " RANK Number of values collected. Since the statistics collectors form\n"
196 " a tree, the value is indented according to it's tree location.\n"
197 " WIN Size of output average window.\n"
198 " MIN Last entered minimum value.\n"
199 " AVG Last entered average value.\n"
200 " DEV Standard deviation of average value over the collector rank.\n"
201 " MAX Last entered maximum value.");
202 dir.add("collect", &Statistics::consoleCollect)
203 .doc("Add statistics collection groups. The argument gives a sequence of collector\n"
204 "ranks each building on the preceding collector:\n"
206 " $ collect (10 60 60)\n"
208 "Will start by collecting every 10 values together to a new value. 60 of such\n"
209 "combined values will be collected together in the next step again followed by\n"
210 "a collection of 60 values. If the statistics is entered with a frequency of\n"
211 "10 values per second, this will provide combined statistics over the second,\n"
212 "minutes and hours ranges.\n"
214 "You may call collect multiple times. Any missing collection ranks will be\n"
216 .arg("ranks","chain of collector ranks");
217 dir.add("output", &Statistics::consoleOutput)
218 .doc("Generate statistics output. This statement will add an additional output\n"
219 "generator. This generator will be attached to the collector specified by\n"
220 "the {rank} parameter. This parameter is a chain of successive rank values\n"
221 "which specifies the exact collector to use. If the collector does not\n"
222 "exist, it will be created (this is like automatically calling 'collect'\n"
223 "with {rank} as argument).\n"
225 "If the output is to be sent somewhere it must be connected to a statistics\n"
228 "The output may optionally be built using a sliding average over the last\n"
233 "will output the basic statistics value each time a new value is entered.\n"
235 " $ output (10 60) 5\n"
237 "Assuming that new data values are entered 10 times per second, this command\n"
238 "will generate output once every minute. The value will be the average over\n"
239 "the last 5 minutes.")
240 .arg("rank","Rank chain selecting the value to generate output for")
241 .arg("window","Optional size of sliding average window",
242 senf::console::kw::default_value = 1u);
246 prefix_ void senf::Statistics::consoleList(std::ostream & os)
248 os << "RANK WIN MIN AVG DEV MAX\n";
249 StatisticsBase::consoleList(0, os);
252 prefix_ void senf::Statistics::consoleCollect(std::vector<unsigned> & ranks)
254 StatisticsBase * stats (this);
255 std::vector<unsigned>::const_iterator i (ranks.begin());
256 std::vector<unsigned>::const_iterator const i_end (ranks.end());
259 for (; i != i_end; ++i)
260 stats = &(*stats)[*i];
262 catch (InvalidRankException &) {}
264 for (; i != i_end; ++i)
265 stats = & (stats->collect(*i));
269 prefix_ boost::shared_ptr<senf::console::DirectoryNode>
270 senf::Statistics::consoleOutput(std::vector<unsigned> & ranks, unsigned window)
272 StatisticsBase * stats (this);
273 std::vector<unsigned>::const_iterator i (ranks.begin());
274 std::vector<unsigned>::const_iterator const i_end (ranks.end());
277 for (; i != i_end; ++i)
278 stats = &(*stats)[*i];
280 catch (InvalidRankException &) {}
282 for (; i != i_end; ++i)
283 stats = & (stats->collect(*i));
285 return stats->output(window).dir().node().thisptr();
288 prefix_ senf::Statistics & senf::Statistics::v_base()
293 prefix_ std::string senf::Statistics::v_path()
299 ///////////////////////////////////////////////////////////////////////////
302 prefix_ void senf::Collector::enter(float min, float avg, float max, float dev)
305 accSumSq_ += avg*avg + dev*dev;
306 if (min < accMin_) accMin_ = min;
307 if (max > accMax_) accMax_ = max;
308 // std::cerr << "! " << i_ << ' ' << avg << ' ' << dev << ' ' << accSum_ << ' ' << accSumSq_
311 float accAvg (accSum_ / i_);
312 float accDev (std::sqrt(accSumSq_ / i_ - accAvg*accAvg));
313 StatisticsBase::enter(accMin_, accAvg, accMax_, accDev);
322 prefix_ senf::Statistics & senf::Collector::v_base()
324 return owner_->base();
327 prefix_ std::string senf::Collector::v_path()
330 return owner_->path() + "-" + senf::str(rank_);
333 ///////////////////////////////cc.e////////////////////////////////////////
335 //#include "Statistics.mpp"
341 // comment-column: 40
342 // c-file-style: "senf"
343 // indent-tabs-mode: nil
344 // ispell-local-dictionary: "american"
345 // compile-command: "scons -u test"