TSFTHistogram.cc
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 #include "TSFTHistogram.hh"
18 
19 // Custom includes
20 #include <senf/Utils/membind.hh>
24 
25 #define prefix_
26 //-/////////////////////////////////////////////////////////////////////////////////////////////////
27 
29  : tsftMapInitialized_(false),
30  duration_(senf::ClockService::seconds(60)),
31  src_(senf::MACAddress::None),
32  enabled_(false)
33 {
34  namespace fty = senf::console::factory;
35 
36  dir.add("duration", fty::Variable(duration_)
37  .doc( "set the TSFT histogram collection duration. Default 60s."));
38  dir.add("start", fty::Command(
40  .doc("enables/resets the TSFT Histogram accepting any SRC address."));
41  dir.add("start", fty::Command(
43  .doc("enables/resets the TSFT Histogram accepting only the specified SRC address."));
44  dir.add("enabled", fty::Command(
45  SENF_MEMBINDFNP(bool, TSFTHistogram, enabled, () const))
46  .doc("returns the TSFT Histogram state"));
47  dir.add("dump", fty::Command(
48  SENF_MEMBINDFNP(void, TSFTHistogram, dump, (std::ostream &) const))
49  .doc("dumps the current TSFT Histogram"));
50 }
51 
52 prefix_ void senf::emu::TSFTHistogram::update(senf::MACAddress const & src, boost::uint64_t const & tsft)
53 {
54  // are we done ?
55  if ((senf::scheduler::now() - startTime_) > duration_) {
56  enabled_ = false;
57  return;
58  }
59 
60  // first call
61  if (!tsftMapInitialized_) {
62  tsftMapInitialized_ = true;
63  lastTSFT_ = tsft;
64  return;
65  }
66 
67  // we either accept all frames, or frame from only one source
68  if (src_ && (src != src_)) {
69  return;
70  }
71 
72  boost::uint32_t diff;
73  if (tsft >= lastTSFT_) {
74  diff = boost::uint32_t(tsft - lastTSFT_);
75  }
76  else {
77  diff = boost::uint32_t(boost::uint64_t(-1) - lastTSFT_ + tsft);
78  }
79 
80  TSFTMap::iterator it( tsftMap_.find(diff));
81  if (it != tsftMap_.end()) {
82  it->second++;
83  }
84  else {
85  tsftMap_.insert( std::make_pair( diff, 1u)); // 1 => count the current packet
86  }
87 
88  lastTSFT_ = tsft;
89 }
90 
91 
93 {
95 }
96 
98 {
99  src_ = src;
100  startTime_ = senf::scheduler::now();
101  tsftMapInitialized_ = false;
102  tsftMap_.clear();
103  enabled_ = true;
104  return true;
105 }
106 
108  const
109 {
110  os << "=== TSFT Histogram (times in us) of " << src_ << ", age " << senf::ClockService::in_seconds(senf::scheduler::now() - startTime_) << "s ===" << std::endl;
111 
112  if (tsftMap_.size() == 0) {
113  os << "no values collected." << std::endl;
114  return;
115  }
116 
117  // current filter: sum of squares to find peaks
118  boost::uint64_t avg = 0;
119 
120  TSFTMap::const_iterator it;
121  for( it = tsftMap_.begin(); it != tsftMap_.end(); it++){
122  avg += it->second*it->second;
123  }
124  avg /= tsftMap_.size();
125 
126  for( it = tsftMap_.begin(); it != tsftMap_.end(); it++){
127  if ((it->second * it->second) > avg) {
128  os << it->first << "," << it->second << std::endl;
129  }
130  }
131 }
132 
133 //-/////////////////////////////////////////////////////////////////////////////////////////////////
134 #undef prefix_
135 
136 
137 // Local Variables:
138 // mode: c++
139 // fill-column: 100
140 // comment-column: 40
141 // c-file-style: "senf"
142 // indent-tabs-mode: nil
143 // ispell-local-dictionary: "american"
144 // compile-command: "scons -u test"
145 // End:
static MACAddress const None
#define SENF_MEMBINDFNP(ret, cls, fn, args)
senf::console::ScopedDirectory dir
#define prefix_
void update(senf::MACAddress const &src, boost::uint64_t const &tsft)
TSFT Histogram header.
static SENF_CLOCKSERVICE_CONSTEXPR int64_type in_seconds(clock_type const &v)
void dump(std::ostream &os) const
NodeType & add(std::string const &name, boost::shared_ptr< NodeType > node)