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"
31 #include "Console/Console.hh"
32 #include "StatisticsTargets.hh"
34 //#include "Statistics.mpp"
36 ///////////////////////////////cc.p////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////////
39 // senf::StatisticsBase
41 prefix_ void senf::StatisticsBase::enter(float min, float avg, float max)
50 prefix_ senf::Collector & senf::StatisticsBase::operator[](unsigned rank)
52 Children::iterator i (children_.find(rank));
53 if (i == children_.end())
54 throw InvalidRankException();
58 prefix_ senf::Collector & senf::StatisticsBase::collect(unsigned rank)
60 std::pair<Children::iterator, bool> state (
61 children_.insert(std::make_pair(rank, Collector(this, rank))) );
63 throw DuplicateRankException();
64 return state.first->second;
67 prefix_ senf::StatisticsBase::OutputProxy<senf::StatisticsBase>
68 senf::StatisticsBase::output(unsigned n)
70 OutputMap::iterator i (outputs_.find(n));
71 if (i == outputs_.end()) {
72 i = outputs_.insert(std::make_pair(n, OutputEntry(n))).first;
74 nm << "output" << path() << ":" << n;
75 base().dir.node().add(nm.str(), i->second.dir);
76 detail::StatisticsLoggerRegistry::instance().apply(*this, n, i->second.dir);
80 return OutputProxy<StatisticsBase>(this, &(i->second));
84 // generate an engineering style notation i
86 char *format_eng( float f)
96 sprintf( buf, " %3.2fe%+03d", f, n);
100 while( f <= -1000.0f){
105 sprintf( buf, "-%3.2fe%+03d", f, n);
108 sprintf( buf, " 0.00");
115 prefix_ void senf::StatisticsBase::consoleList(unsigned level, std::ostream & os)
118 os << boost::format("%s%-5d%|15t| %12s %12s %12s\n")
119 % std::string(2*level,' ') % rank() % format_eng(min()) % format_eng(avg()) % format_eng(max());
121 OutputMap::const_iterator i (outputs_.begin());
122 OutputMap::const_iterator i_end (outputs_.end());
123 for (; i != i_end; ++i)
124 os << boost::format(" %3d %12s %12s %12s\n")
126 % format_eng(i->second.min/i->second.n)
127 % format_eng(i->second.avg/i->second.n)
128 % format_eng(i->second.max/i->second.n);
131 Children::const_iterator i (children_.begin());
132 Children::const_iterator const i_end (children_.end());
133 for (; i != i_end; ++i)
134 i->second.consoleList(level+1, os);
138 prefix_ void senf::StatisticsBase::generateOutput()
140 queue_.push_front(QueueEntry(min_, avg_, max_));
141 while (queue_.size() > maxQueueLen_)
144 OutputMap::iterator i (outputs_.begin());
145 OutputMap::iterator const i_end (outputs_.end());
146 for (; i != i_end; ++i) {
147 i->second.min = i->second.avg = i->second.max = 0.0f;
148 Queue::const_iterator j (queue_.begin());
149 Queue::const_iterator const j_end (queue_.end());
151 for (; n < i->second.n && j != j_end; ++n, ++j) {
152 i->second.min += j->min;
153 i->second.avg += j->avg;
154 i->second.max += j->max;
156 i->second.signal(i->second.min/n, i->second.avg/n, i->second.max/n);
160 prefix_ void senf::StatisticsBase::signalChildren()
162 Children::iterator i (children_.begin());
163 Children::iterator const i_end (children_.end());
164 for (; i != i_end; ++i)
165 i->second.enter(min_, avg_, max_);
168 ///////////////////////////////////////////////////////////////////////////
171 prefix_ senf::Statistics::Statistics()
172 #ifndef SENF_DISABLE_CONSOLE
176 #ifndef SENF_DISABLE_CONSOLE
177 dir.add("list", &Statistics::consoleList)
178 .doc("List statistics collection intervals and current values.\n"
181 " RANK Number of values collected. Since the statistics collectors form\n"
182 " a tree, the value is indented according to it's tree location.\n"
183 " WIN Size of output average window.\n"
184 " MIN Last entered minimum value.\n"
185 " AVG Last entered average value.\n"
186 " MAX Last entered maximum value.");
187 dir.add("collect", &Statistics::consoleCollect)
188 .doc("Add statistics collection groups. The argument gives a sequence of collector\n"
189 "ranks each building on the preceding collector:\n"
191 " $ collect (10 60 60)\n"
193 "Will start by collecting every 10 values together to a new value. 60 of such\n"
194 "combined values will be collected together in the next step again followed by\n"
195 "a collection of 60 values. If the statistics is entered with a frequency of\n"
196 "10 values per second, this will provide combined statistics over the second,\n"
197 "minutes and hours ranges.\n"
199 "You may call collect multiple times. Any missing collection ranks will be\n"
201 .arg("ranks","chain of collector ranks");
202 dir.add("output", &Statistics::consoleOutput)
203 .doc("Generate statistics output. This statement will add an additional output\n"
204 "generator. This generator will be attached to the collector specified by\n"
205 "the {rank} parameter. This parameter is a chain of successive rank values\n"
206 "which specifies the exact collector to use. If the collector does not\n"
207 "exist, it will be created (this is like automatically calling 'collect'\n"
208 "with {rank} as argument).\n"
210 "If the output is to be sent somewhere it must be connected to a statistics\n"
213 "The output may optionally be built using a sliding average over the last\n"
218 "will output the basic statistics value each time a new value is entered.\n"
220 " $ output (10 60) 5\n"
222 "Assuming that new data values are entered 10 times per second, this command\n"
223 "will generate output once every minute. The value will be the average over\n"
224 "the last 5 minutes.")
225 .arg("rank","Rank chain selecting the value to generate output for")
226 .arg("window","Optional size of sliding average window",
227 senf::console::kw::default_value = 1u);
231 prefix_ void senf::Statistics::consoleList(std::ostream & os)
233 os << "RANK WIN MIN AVG MAX\n";
234 StatisticsBase::consoleList(0, os);
237 prefix_ void senf::Statistics::consoleCollect(std::vector<unsigned> & ranks)
239 StatisticsBase * stats (this);
240 std::vector<unsigned>::const_iterator i (ranks.begin());
241 std::vector<unsigned>::const_iterator const i_end (ranks.end());
244 for (; i != i_end; ++i)
245 stats = &(*stats)[*i];
247 catch (InvalidRankException &) {}
249 for (; i != i_end; ++i)
250 stats = & (stats->collect(*i));
254 prefix_ boost::shared_ptr<senf::console::DirectoryNode>
255 senf::Statistics::consoleOutput(std::vector<unsigned> & ranks, unsigned window)
257 StatisticsBase * stats (this);
258 std::vector<unsigned>::const_iterator i (ranks.begin());
259 std::vector<unsigned>::const_iterator const i_end (ranks.end());
262 for (; i != i_end; ++i)
263 stats = &(*stats)[*i];
265 catch (InvalidRankException &) {}
267 for (; i != i_end; ++i)
268 stats = & (stats->collect(*i));
270 return stats->output(window).dir().node().thisptr();
273 prefix_ senf::Statistics & senf::Statistics::v_base()
278 prefix_ std::string senf::Statistics::v_path()
284 ///////////////////////////////////////////////////////////////////////////
287 prefix_ void senf::Collector::enter(float min, float avg, float max)
290 if (min < accMin_) accMin_ = min;
291 if (max > accMax_) accMax_ = max;
293 StatisticsBase::enter(accMin_, accAvg_ / rank_, accMax_);
301 prefix_ senf::Statistics & senf::Collector::v_base()
303 return owner_->base();
306 prefix_ std::string senf::Collector::v_path()
309 return owner_->path() + "-" + senf::str(rank_);
312 ///////////////////////////////cc.e////////////////////////////////////////
314 //#include "Statistics.mpp"
320 // comment-column: 40
321 // c-file-style: "senf"
322 // indent-tabs-mode: nil
323 // ispell-local-dictionary: "american"
324 // compile-command: "scons -u test"