EMANE  1.2.1
otastatisticpublisher.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2017 - 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 
33 #include "otastatisticpublisher.h"
35 
36 // specialized hash for PacketCountTable
37 namespace std
38 {
39  template<>
40  struct hash<std::pair<std::string,EMANE::NEMId>>
41  {
42  typedef std::pair<std::string,EMANE::NEMId> 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::NEMId>()(s.second)};
49  return h1 ^ (h2 << 1);
50  }
51  };
52 }
53 
54 namespace
55 {
56  const EMANE::StatisticTableLabels PacketCountLabels =
57  {
58  "Src",
59  "Emulator UUID",
60  "Num Pkts Tx",
61  "Num Pkts Rx",
62  "Pkts Rx Drop Miss Part",
63  };
64 
66  {
67  PACKET_COUNT_COLUMN_NEM = 0,
68  PACKET_COUNT_COLUMN_UUID = 1,
69  PACKET_COUNT_COLUMN_NUM_PACKETS_TX = 2,
70  PACKET_COUNT_COLUMN_NUM_PACKETS_RX = 3,
71  PACKET_COUNT_COLUMN_NUM_PACKETS_RX_DROP_MISS_PART = 4,
72  };
73 }
74 
76  rowLimit_{0}
77 {
78  auto statisticRegistrar = StatisticRegistrarProxy{*StatisticServiceSingleton::instance(),0};
79 
80  pNumOTAChannelDownstreamPackets_ =
81  statisticRegistrar.registerNumeric<std::uint64_t>("numOTAChannelDownstreamPackets",
83  pNumOTAChannelUpstreamPackets_ =
84  statisticRegistrar.registerNumeric<std::uint64_t>("numOTAChannelUpstreamPackets",
86  pNumOTAChannelUpstreamPacketsDroppedMissingPart_ =
87  statisticRegistrar.registerNumeric<std::uint64_t>("numOTAChannelUpstreamPacketsDroppedMissingPart",
89 
90  pPacketCountTable_ =
91  statisticRegistrar.registerTable<PacketCountTableKey>("OTAChannelPacketCountTable",
92  PacketCountLabels,
93  [this](StatisticTablePublisher * pTable)
94  {
95  std::lock_guard<std::mutex> m(mutexPacketCountTable_);
96  packetCountInfo_.clear();
97  pTable->clear();
98  },
99  "OTA packet count table.");
100 }
101 
102 void EMANE::OTAStatisticPublisher::update(Type type, const uuid_t & uuid, NEMId nemId)
103 {
104  char buf[37];
105  uuid_unparse(uuid,buf);
106  auto key = PacketCountTableKey{buf,nemId};
107 
108  std::lock_guard<std::mutex> m(mutexPacketCountTable_);
109 
110  auto iter = packetCountInfo_.find(key);
111 
112  if(iter == packetCountInfo_.end())
113  {
114  if(packetCountInfo_.size() < rowLimit_)
115  {
116  iter = packetCountInfo_.insert({key,std::make_tuple(0,0,0)}).first;
117 
118  pPacketCountTable_->addRow(key,
119  {Any{nemId},
120  Any{buf},
121  Any{0L},
122  Any{0L},
123  Any{0L}});
124  }
125  }
126 
128  {
129  ++*pNumOTAChannelUpstreamPackets_;
130 
131  if(iter != packetCountInfo_.end())
132  {
133  auto & packets = std::get<PACKET_COUNT_COLUMN_NUM_PACKETS_RX-2>(iter->second);
134 
135  packets += 1;
136 
137  pPacketCountTable_->setCell(key,
138  PACKET_COUNT_COLUMN_NUM_PACKETS_RX,
139  Any{packets});
140  }
141 
142  }
143  else if(type == Type::TYPE_DOWNSTREAM_PACKET_SUCCESS)
144  {
145  ++*pNumOTAChannelDownstreamPackets_;
146 
147  if(iter != packetCountInfo_.end())
148  {
149  auto & packets = std::get<PACKET_COUNT_COLUMN_NUM_PACKETS_TX-2>(iter->second);
150 
151  packets += 1;
152 
153  pPacketCountTable_->setCell(key,
154  PACKET_COUNT_COLUMN_NUM_PACKETS_TX,
155  Any{packets});
156  }
157  }
159  {
160  ++*pNumOTAChannelUpstreamPacketsDroppedMissingPart_;
161 
162  if(iter != packetCountInfo_.end())
163  {
164  auto & packets = std::get<PACKET_COUNT_COLUMN_NUM_PACKETS_RX_DROP_MISS_PART-2>(iter->second);
165 
166  packets += 1;
167 
168  pPacketCountTable_->setCell(key,
169  PACKET_COUNT_COLUMN_NUM_PACKETS_RX_DROP_MISS_PART,
170  Any{packets});
171  }
172  }
173 }
174 
176 {
177  rowLimit_ = rows;
178 }
A StatisticTablePublisher produces two dimensional tables of Anys.
void setCell(const Key &key, std::size_t columnIndex, const Any &any)
void update(Type type, const uuid_t &uuid, NEMId nemId)
std::vector< std::string > StatisticTableLabels
std::uint16_t NEMId
Definition: types.h:52
void addRow(const Key &key, const std::vector< Any > &anys={})
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