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"
33 #include <senf/Utils/Console/Console.hh>
34 #include <senf/Utils/Format.hh>
35 #include "StatisticsTargets.hh"
37 //#include "Statistics.mpp"
39 ///////////////////////////////cc.p////////////////////////////////////////
41 ///////////////////////////////////////////////////////////////////////////
42 // senf::StatisticsBase
44 prefix_ void senf::StatisticsBase::enter(unsigned n, float min, float avg, float max, float dev)
50 for (unsigned i (0); i < n; ++i)
52 Children::iterator i (children_.begin());
53 Children::iterator const i_end (children_.end());
54 for (; i != i_end; ++i)
55 i->second.enter(n, min_, avg_, max_, dev_);
58 prefix_ senf::Collector & senf::StatisticsBase::operator[](unsigned rank)
60 Children::iterator i (children_.find(rank));
61 if (i == children_.end())
62 throw InvalidRankException();
66 prefix_ senf::Collector & senf::StatisticsBase::collect(unsigned rank)
68 std::pair<Children::iterator, bool> state (
69 children_.insert(std::make_pair(rank, Collector(this, rank))) );
71 throw DuplicateRankException();
72 return state.first->second;
75 prefix_ senf::StatisticsBase::OutputProxy<senf::StatisticsBase>
76 senf::StatisticsBase::output(unsigned n)
78 OutputMap::iterator i (outputs_.find(n));
79 if (i == outputs_.end()) {
80 i = outputs_.insert(std::make_pair(n, OutputEntry(n))).first;
82 nm << "output" << path() << ":" << n;
83 base().dir.node().add(nm.str(), i->second.dir);
84 detail::StatisticsLoggerRegistry::instance().apply(*this, n, i->second.dir);
88 return OutputProxy<StatisticsBase>(this, &(i->second));
91 prefix_ void senf::StatisticsBase::consoleList(unsigned level, std::ostream & os)
94 namespace fmt = senf::format;
96 os << boost::format("%s%-5d%|15t| %12.5g %19.5g %12.5g\n")
97 % std::string(2*level,' ') % rank()
98 % fmt::eng(min()).setw() % fmt::eng(avg(),dev()).setw() % fmt::eng(max()).setw();
100 OutputMap::const_iterator i (outputs_.begin());
101 OutputMap::const_iterator i_end (outputs_.end());
102 for (; i != i_end; ++i)
103 os << boost::format(" %3d %12.5g %19.5g %12.5g\n")
105 % fmt::eng(i->second.min).setw()
106 % fmt::eng(i->second.avg, i->second.dev).setw()
107 % fmt::eng(i->second.max).setw();
110 Children::const_iterator i (children_.begin());
111 Children::const_iterator const i_end (children_.end());
112 for (; i != i_end; ++i)
113 i->second.consoleList(level+1, os);
117 prefix_ void senf::StatisticsBase::generateOutput()
119 queue_.push_front(QueueEntry(min_, avg_, max_, dev_));
120 while (queue_.size() > maxQueueLen_)
123 OutputMap::iterator i (outputs_.begin());
124 OutputMap::iterator const i_end (outputs_.end());
125 for (; i != i_end; ++i) {
126 i->second.min = i->second.avg = i->second.max = i->second.dev = 0.0f;
127 Queue::const_iterator j (queue_.begin());
128 Queue::const_iterator const j_end (queue_.end());
130 for (; n < i->second.n && j != j_end; ++n, ++j) {
131 i->second.min += j->min;
132 i->second.avg += j->avg;
133 i->second.max += j->max;
134 i->second.dev += j->dev;
140 i->second.signal(i->second.min, i->second.avg, i->second.max, i->second.dev);
144 ///////////////////////////////////////////////////////////////////////////
147 prefix_ senf::Statistics::Statistics()
148 #ifndef SENF_DISABLE_CONSOLE
152 #ifndef SENF_DISABLE_CONSOLE
153 namespace fty = senf::console::factory;
155 dir.add("list", fty::Command(&Statistics::consoleList, this)
156 .doc("List statistics collection intervals and current values.\n"
159 " RANK Number of values collected. Since the statistics collectors form\n"
160 " a tree, the value is indented according to it's tree location.\n"
161 " WIN Size of output average window.\n"
162 " MIN Last entered minimum value.\n"
163 " AVG Last entered average value.\n"
164 " DEV Standard deviation of average value over the collector rank.\n"
165 " MAX Last entered maximum value.") );
166 dir.add("collect", fty::Command(&Statistics::consoleCollect, this)
167 .doc("Add statistics collection groups. The argument gives a sequence of collector\n"
168 "ranks each building on the preceding collector:\n"
170 " $ collect (10 60 60)\n"
172 "Will start by collecting every 10 values together to a new value. 60 of such\n"
173 "combined values will be collected together in the next step again followed by\n"
174 "a collection of 60 values. If the statistics is entered with a frequency of\n"
175 "10 values per second, this will provide combined statistics over the second,\n"
176 "minutes and hours ranges.\n"
178 "You may call collect multiple times. Any missing collection ranks will be\n"
180 .arg("ranks","chain of collector ranks") );
181 dir.add("output", fty::Command(&Statistics::consoleOutput, this)
182 .doc("Generate statistics output. This statement will add an additional output\n"
183 "generator. This generator will be attached to the collector specified by\n"
184 "the {rank} parameter. This parameter is a chain of successive rank values\n"
185 "which specifies the exact collector to use. If the collector does not\n"
186 "exist, it will be created (this is like automatically calling 'collect'\n"
187 "with {rank} as argument).\n"
189 "If the output is to be sent somewhere it must be connected to a statistics\n"
192 "The output may optionally be built using a sliding average over the last\n"
197 "will output the basic statistics value each time a new value is entered.\n"
199 " $ output (10 60) 5\n"
201 "Assuming that new data values are entered 10 times per second, this command\n"
202 "will generate output once every minute. The value will be the average over\n"
203 "the last 5 minutes.")
204 .arg("rank","Rank chain selecting the value to generate output for")
205 .arg("window","Optional size of sliding average window",
206 senf::console::kw::default_value = 1u) );
210 prefix_ void senf::Statistics::consoleList(std::ostream & os)
212 os << "RANK WIN MIN AVG MAX\n";
213 StatisticsBase::consoleList(0, os);
216 prefix_ void senf::Statistics::consoleCollect(std::vector<unsigned> & ranks)
218 StatisticsBase * stats (this);
219 std::vector<unsigned>::const_iterator i (ranks.begin());
220 std::vector<unsigned>::const_iterator const i_end (ranks.end());
223 for (; i != i_end; ++i)
224 stats = &(*stats)[*i];
226 catch (InvalidRankException &) {}
228 for (; i != i_end; ++i)
229 stats = & (stats->collect(*i));
233 prefix_ boost::shared_ptr<senf::console::DirectoryNode>
234 senf::Statistics::consoleOutput(std::vector<unsigned> & ranks, unsigned window)
236 StatisticsBase * stats (this);
237 std::vector<unsigned>::const_iterator i (ranks.begin());
238 std::vector<unsigned>::const_iterator const i_end (ranks.end());
241 for (; i != i_end; ++i)
242 stats = &(*stats)[*i];
244 catch (InvalidRankException &) {}
246 for (; i != i_end; ++i)
247 stats = & (stats->collect(*i));
249 return stats->output(window).dir().node().thisptr();
252 prefix_ senf::Statistics & senf::Statistics::v_base()
257 prefix_ std::string senf::Statistics::v_path()
263 ///////////////////////////////////////////////////////////////////////////
266 prefix_ void senf::Collector::enter(unsigned n, float min, float avg, float max, float dev)
268 if (min < accMin_) accMin_ = min;
269 if (max > accMax_) accMax_ = max;
271 if (i_ + n >= rank_) {
272 accSum_ += (rank_-i_)*avg;
273 accSumSq_ += (rank_-i_)*(rank_-i_)*(avg*avg + dev*dev);
274 float accAvg (accSum_ / rank_);
275 float accDev (std::sqrt(std::max(0.0f,accSumSq_ / rank_ - accAvg*accAvg)));
276 StatisticsBase::enter(1, accMin_, accAvg, accMax_, accDev);
285 std::div_t d (std::div(int(n), int(rank_)));
286 StatisticsBase::enter(d.quot, min, avg, max, dev);
293 accSumSq_ += n*n*(avg*avg+dev*dev);
295 if (min < accMin_) accMin_ = min;
296 if (max > accMax_) accMax_ = max;
300 prefix_ senf::Statistics & senf::Collector::v_base()
302 return owner_->base();
305 prefix_ std::string senf::Collector::v_path()
308 return owner_->path() + "-" + senf::str(rank_);
311 ///////////////////////////////cc.e////////////////////////////////////////
313 //#include "Statistics.mpp"
319 // comment-column: 40
320 // c-file-style: "senf"
321 // indent-tabs-mode: nil
322 // ispell-local-dictionary: "american"
323 // compile-command: "scons -u test"