Statistics.hh
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #ifndef HH_SENF_Utils_Statistics_
18 #define HH_SENF_Utils_Statistics_ 1
19 
20 // Custom includes
21 #include <map>
22 #include <vector>
23 #include <deque>
24 #include <boost/iterator/transform_iterator.hpp>
25 #include <boost/range/iterator_range.hpp>
26 #include <boost/noncopyable.hpp>
27 #include <boost/ptr_container/ptr_vector.hpp>
28 #include <boost/signals2.hpp>
30 #include <senf/Utils/Cpp11Support/smart_ptr.hh>
31 #include "StatisticAccumulator.hh"
32 #include "Exception.hh"
33 
34 //#include "Statistics.mpp"
35 //-/////////////////////////////////////////////////////////////////////////////////////////////////
36 
37 namespace senf {
38 
55  class Collector;
56  class Statistics;
57 
60  {
61  typedef std::map<unsigned, Collector> Children;
62 
63  // why we can't use ::__gnu_cxx::select2nd instead?!?!
64  struct Transform {
65  typedef Children::value_type & first_argument_type;
66  typedef Collector & result_type;
67  result_type operator()(first_argument_type i) const;
68  };
69  struct ConstTransform {
70  typedef Children::value_type const & first_argument_type;
71  typedef Collector const & result_type;
72  result_type operator()(first_argument_type i) const;
73  };
74 
75  typedef boost::transform_iterator<Transform,Children::iterator> ValueIterator;
76  typedef boost::transform_iterator<ConstTransform,Children::const_iterator> const_ValueIterator;
77 
78  struct OutputEntry;
79 
80  public:
81  //-////////////////////////////////////////////////////////////////////////
82  // Types
83 
84  typedef boost::iterator_range<ValueIterator> CollectorRange;
85  typedef boost::iterator_range<const_ValueIterator> const_CollectorRange;
86 
122  template <class Owner>
124  {
125  public:
126  template <class Target> Owner & connect(Target & target,
127  std::string label="") const;
129  template <class PTarget> Owner & connect(std::unique_ptr<PTarget> target,
130  std::string label="") const;
132  Owner & noconnect() const;
133  console::ScopedDirectory<> & dir() const;
135 
136 #ifdef DOXYGEN
137  private:
138 #endif
139  OutputProxy(Owner * owner, OutputEntry * entry);
140  template <class OtherOwner>
141  OutputProxy(Owner * owner, OutputProxy<OtherOwner> const & other);
142 
143  private:
144  Owner * owner_;
145  OutputEntry * entry_;
146 
147  template <class OtherOwner> friend class OutputProxy;
148  };
149 
150  //-////////////////////////////////////////////////////////////////////////
152  //\{
153 
154  unsigned cnt () const;
155  float min() const;
156  float avg() const;
157  float max() const;
158  float dev() const;
159 
160  virtual unsigned rank() const;
161 
164  //\}
165  //-////////////////////////////////////////////////////////////////////////
167  //\{
168 
169  Collector & operator[](unsigned rank);
171 
177  Collector const & operator[](unsigned rank) const;
179 
185  CollectorRange collectors();
186 
188  const_CollectorRange collectors() const;
190 
193  Collector & collect(unsigned rank);
194 
201  Statistics const & base() const;
202  Statistics & base();
203 
208  std::string path() const;
209 
214  //\}
215  //-////////////////////////////////////////////////////////////////////////
217 
218  OutputProxy<StatisticsBase> output(unsigned n = 1u);
220 
231  //\}
232  StatisticsData data() const;
233 
237  //-////////////////////////////////////////////////////////////////////////
238  // Exceptions
239 
241  { InvalidRankException() : senf::Exception("Invalid rank value") {} };
242 
244  { DuplicateRankException() : senf::Exception("Duplicate rank value") {} };
245 
246  void consoleList(unsigned level, std::ostream & os) const;
247 
248  protected:
249  StatisticsBase();
250  virtual ~StatisticsBase();
251  void enter(unsigned n, unsigned cnt, float min, float avg, float max, float dev);
252 
253  private:
254  virtual Statistics & v_base() = 0;
255  virtual std::string v_path() const = 0;
256 
257  void generateOutput();
258 
259  unsigned cnt_;
260  float min_;
261  float avg_;
262  float max_;
263  float dev_;
264  Children children_;
265 
266  struct QueueEntry {
267  unsigned cnt;
268  float min;
269  float avg;
270  float max;
271  float dev;
272  QueueEntry() : cnt(), min(), avg(), max(), dev() {}
273  QueueEntry(unsigned cnt_, float min_, float avg_, float max_, float dev_)
274  : cnt(cnt_), min(min_), avg(avg_), max(max_), dev(dev_) {}
275  };
276  typedef std::deque<QueueEntry> Queue;
277  Queue queue_;
278 
279  struct OutputEntry {
280  struct TargetBase
281  {
282  explicit TargetBase(std::string const & label_) : label (label_) {}
283  virtual ~TargetBase() {};
284  std::string label;
285  };
286 
287  template <class PTarget>
288  struct Target : public TargetBase
289  {
290  boost::scoped_ptr<PTarget> target_;
291  Target(std::unique_ptr<PTarget> target, std::string const & label)
292  : TargetBase (label), target_ (target.release()) {}
293  explicit Target(std::string const & label)
294  : TargetBase (label), target_ (0) {}
295  };
296 
297  OutputEntry();
298  explicit OutputEntry(unsigned n_);
299  OutputEntry(const OutputEntry& other);
300  OutputEntry& operator=(const OutputEntry& other);
301 
302  void initDir();
303  void consoleList(std::ostream & os);
304 
305  unsigned n;
306  unsigned cnt;
307  float min;
308  float avg;
309  float max;
310  float dev;
311 
312  boost::signals2::signal<void(unsigned,float,float,float,float)> signal;
313  boost::ptr_vector<TargetBase> targets_;
314 
316  };
317  typedef std::map<unsigned, OutputEntry> OutputMap;
318  OutputMap outputs_;
319  unsigned maxQueueLen_;
320  };
321 
436  : public StatisticsBase, boost::noncopyable
437  {
438  public:
439 #ifndef SENF_DISABLE_CONSOLE
441 #endif
442 
443  Statistics();
444 
445  void operator()(unsigned n, unsigned cnt, float min, float avg, float max, float dev);
447 
473  void operator()(unsigned cnt, float min, float avg, float max, float dev=0.0f);
475 
477  void operator()(unsigned cnt, float value, float dev=0.0f);
479 
481  void operator()(StatisticsData const & data);
483 
485  template <class Value>
486  void operator()(unsigned n, StatisticAccumulator<Value> & sa);
488 
495 
496  private:
497  void consoleCollect(std::vector<unsigned> & ranks);
498  void consoleList(std::ostream & os) const;
499  boost::shared_ptr<console::DirectoryNode> consoleOutput(
500  std::vector<unsigned> & ranks, unsigned window);
501  Statistics & v_base();
502  std::string v_path() const;
503  };
504 
512  class Collector : public StatisticsBase
513  {
514  public:
515  virtual unsigned rank() const;
516 
518 
519  bool updated() const;
520 
521  private:
522  Collector(StatisticsBase * owner, unsigned rank);
523  void enter(unsigned n, unsigned cnt, float min, float avg, float max, float dev);
524  Statistics & v_base();
525  std::string v_path() const;
526 
527  unsigned rank_;
528  unsigned i_;
529  unsigned l_;
530  unsigned accCnt_;
531  float accMin_;
532  float accSum_;
533  float accSumSq_;
534  float accMax_;
535  StatisticsBase * owner_;
536  bool updated_; // true indicates that StatisticsBase::enter() was called
537 
538  friend class StatisticsBase;
539  };
540 
541 }
542 
543 //-/////////////////////////////////////////////////////////////////////////////////////////////////
544 #include "Statistics.cci"
545 //#include "Statistics.ct"
546 #include "Statistics.cti"
547 #endif
548 
549 
550 // Local Variables:
551 // mode: c++
552 // fill-column: 100
553 // comment-column: 40
554 // c-file-style: "senf"
555 // indent-tabs-mode: nil
556 // ispell-local-dictionary: "american"
557 // compile-command: "scons -u test"
558 // End:
std::string path() const
Get the path to this collector.
Accumulate measurement values.
OutputProxy< StatisticsBase > output(unsigned n=1u)
Register output.
Definition: Statistics.cc:78
StatisticsData data() const
Get the Statistics data as senf::StatisticsData.
Internal: Generic Statistics collection.
Definition: Statistics.hh:59
Target(std::string const &label)
Definition: Statistics.hh:293
unsigned cnt() const
Last cnt value entered.
Collector & operator[](unsigned rank)
Get child collector.
Definition: Statistics.cc:51
StatsDataCollectorKernel signal
Collect statistics and generate log messages.
Definition: Statistics.hh:435
StatisticAccumulator public header.
void connect(connector::FastActiveOutput< PacketType > &source, connector::FastPassiveInput< PacketType > &target)
float avg() const
Last avg value entered.
float max() const
Last max value entered.
boost::iterator_range< ValueIterator > CollectorRange
Definition: Statistics.hh:78
float dev() const
Last dev value entered.
Exception public header.
virtual ~StatisticsBase()
boost::scoped_ptr< PTarget > target_
Definition: Statistics.hh:290
boost::iterator_range< const_ValueIterator > const_CollectorRange
Definition: Statistics.hh:85
Statistics const & base() const
Output connection interface.
Definition: Statistics.hh:123
Accumulated statistics collector.
Definition: Statistics.hh:512
Extensible exception base-class.
Definition: Exception.hh:164
CollectorRange collectors()
List all child collectors.
console::ScopedDirectory dir
Definition: Statistics.hh:440
void consoleList(unsigned level, std::ostream &os) const
Definition: Statistics.cc:93
Target(std::unique_ptr< PTarget > target, std::string const &label)
Definition: Statistics.hh:291
void enter(unsigned n, unsigned cnt, float min, float avg, float max, float dev)
Definition: Statistics.cc:36
Collector & collect(unsigned rank)
Register a new collector.
Definition: Statistics.cc:68
virtual unsigned rank() const
Return collectors rank value.
float min() const
Last min value entered.