EMANE  1.2.1
eventstatisticpublisher.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 - Adjacent Link LLC, Bridgewater, New Jersey
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * * Neither the name of Adjacent Link LLC nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
35 
36 // specialized hash for EventCountTable
37 namespace std
38 {
39  template<>
40  struct hash<std::pair<std::string,EMANE::EventId>>
41  {
42  typedef std::pair<std::string,EMANE::EventId> argument_type;
43  typedef std::size_t result_type;
44 
45  result_type operator()(argument_type const& s) const
46  {
47  result_type const h1{std::hash<std::string>()(s.first)};
48  result_type const h2{std::hash<EMANE::EventId>()(s.second)};
49  return h1 ^ (h2 << 1);
50  }
51  };
52 }
53 
54 namespace
55 {
56  const EMANE::StatisticTableLabels EventCountLabels =
57  {
58  "Src",
59  "Emulator UUID",
60  "Num Events Tx",
61  "Num Events Rx"
62  };
63 
65  {
66  EVENT_COUNT_COLUMN_EVENT = 0,
67  EVENT_COUNT_COLUMN_UUID = 1,
68  EVENT_COUNT_COLUMN_NUM_EVENTS_TX = 2,
69  EVENT_COUNT_COLUMN_NUM_EVENTS_RX = 3,
70  };
71 }
72 
74  rowLimit_{0}
75 {
76  auto statisticRegistrar = StatisticRegistrarProxy{*StatisticServiceSingleton::instance(),0};
77 
78  pNumEventsTx_ =
79  statisticRegistrar.registerNumeric<std::uint64_t>("num" + sPrefix +"EventsTx",
81  pNumEventsRx_ =
82  statisticRegistrar.registerNumeric<std::uint64_t>("num" + sPrefix + "EventsRx",
84  pEventCountTable_ =
85  statisticRegistrar.registerTable<EventCountTableKey>(sPrefix + "EventCountTable",
86  EventCountLabels,
87  [this](StatisticTablePublisher * pTable)
88  {
89  std::lock_guard<std::mutex> m(mutexEventCountTable_);
90  eventCountInfo_.clear();
91  pTable->clear();
92  },
93  sPrefix + " Event count table.");
94 }
95 
96 void EMANE::EventStatisticPublisher::update(Type type, const uuid_t & uuid, EventId eventId)
97 {
98  char buf[37];
99  uuid_unparse(uuid,buf);
100  auto key = EventCountTableKey{buf,eventId};
101 
102  std::lock_guard<std::mutex> m(mutexEventCountTable_);
103 
104  auto iter = eventCountInfo_.find(key);
105 
106  if(iter == eventCountInfo_.end())
107  {
108  if(eventCountInfo_.size() < rowLimit_)
109  {
110  iter = eventCountInfo_.insert({key,std::make_tuple(0,0)}).first;
111 
112  pEventCountTable_->addRow(key,
113  {Any{eventId},
114  Any{buf},
115  Any{0L},
116  Any{0L}});
117  }
118  }
119 
120 
121  if(type == Type::TYPE_RX)
122  {
123  ++*pNumEventsRx_;
124 
125  if(iter != eventCountInfo_.end())
126  {
127  auto & events = std::get<EVENT_COUNT_COLUMN_NUM_EVENTS_RX-2>(iter->second);
128 
129  events += 1;
130 
131  pEventCountTable_->setCell(key,
132  EVENT_COUNT_COLUMN_NUM_EVENTS_RX,
133  Any{events});
134  }
135 
136  }
137  else if(type == Type::TYPE_TX)
138  {
139  ++*pNumEventsTx_;
140 
141  if(iter != eventCountInfo_.end())
142  {
143  auto & events = std::get<EVENT_COUNT_COLUMN_NUM_EVENTS_TX-2>(iter->second);
144 
145  events += 1;
146 
147  pEventCountTable_->setCell(key,
148  EVENT_COUNT_COLUMN_NUM_EVENTS_TX,
149  Any{events});
150  }
151  }
152 }
153 
155 {
156  rowLimit_ = rows;
157 }
A StatisticTablePublisher produces two dimensional tables of Anys.
void setCell(const Key &key, std::size_t columnIndex, const Any &any)
std::uint16_t EventId
Definition: types.h:53
void update(Type type, const uuid_t &uuid, EventId eventId)
std::vector< std::string > StatisticTableLabels
void addRow(const Key &key, const std::vector< Any > &anys={})
EventStatisticPublisher(const std::string &sPrefix)
static StatisticService * instance()
Definition: singleton.h:56
The Any class can contain an instance of one of any type in its support type set. ...
Definition: any.h:49