14 #include <boost/noncopyable.hpp> 15 #include <boost/shared_ptr.hpp> 16 #include <boost/multi_index_container.hpp> 17 #include <boost/multi_index/ordered_index.hpp> 18 #include <boost/multi_index/sequenced_index.hpp> 19 #include <boost/multi_index/global_fun.hpp> 20 #include <boost/multi_index/mem_fun.hpp> 21 #include <boost/date_time/posix_time/posix_time.hpp> 89 return (this_counter);
161 return(packet->getTransid() & 1023);
235 typedef boost::multi_index_container<
239 boost::multi_index::indexed_by<
242 boost::multi_index::sequenced<>,
247 boost::multi_index::ordered_non_unique<
251 boost::multi_index::global_fun<
266 typedef typename PktList::template nth_index<1>::type
269 typedef typename PktListTransidHashIndex::const_iterator
272 typedef typename std::queue<PktListTransidHashIterator>
284 const double drop_time,
285 const bool archive_enabled,
286 const boost::posix_time::ptime boot_time);
298 static_cast<void>(sent_packets_.template get<0>().push_back(packet));
312 static_cast<void>(rcvd_packets_.push_back(packet));
324 void updateDelays(
const dhcp::PktPtr& sent_packet,
325 const dhcp::PktPtr& rcvd_packet);
342 dhcp::PktPtr matchPackets(
const dhcp::PktPtr& rcvd_packet);
368 if (rcvd_packets_num_ == 0) {
371 return(sum_delay_ / rcvd_packets_num_);
385 if (rcvd_packets_num_ == 0) {
388 return(sqrt(sum_delay_squared_ / rcvd_packets_num_ -
389 getAvgDelay() * getAvgDelay()));
422 if (unordered_lookups_ == 0) {
425 return(static_cast<double>(unordered_lookup_size_sum_) /
426 static_cast<double>(unordered_lookups_));
471 if (getSentPacketsNum() > getRcvdPacketsNum()) {
472 drops = getSentPacketsNum() - getRcvdPacketsNum();
516 auto sent = getSentPacketsNum();
517 auto drops = getDroppedPacketsNum();
518 double drops_ratio = 100.0 *
static_cast<double>(drops) / static_cast<double>(sent);
520 cout <<
"sent packets: " << sent << endl
521 <<
"received packets: " << getRcvdPacketsNum() << endl
522 <<
"drops: " << drops << endl
523 <<
"drops ratio: " << drops_ratio <<
" %" << endl
524 <<
"orphans: " << getOrphans() << endl
525 <<
"rejected leases: " << getRejLeasesNum() << endl
526 <<
"non unique addresses: " << getNonUniqueAddrNum() << endl;
539 cout << fixed << setprecision(3)
540 <<
"min delay: " << getMinDelay() * 1e3 <<
" ms" << endl
541 <<
"avg delay: " << getAvgDelay() * 1e3 <<
" ms" << endl
542 <<
"max delay: " << getMaxDelay() * 1e3 <<
" ms" << endl
543 <<
"std deviation: " << getStdDevDelay() * 1e3 <<
" ms" 545 <<
"collected packets: " << getCollectedNum() << endl;
548 cout <<
"min delay: n/a" << endl
549 <<
"avg delay: n/a" << endl
550 <<
"max delay: n/a" << endl
551 <<
"std deviation: n/a" << endl
552 <<
"collected packets: 0" << endl;
566 void printTimestamps();
569 return(std::make_tuple(sent_packets_.begin(), sent_packets_.end()));
580 std::string receivedLeases()
const;
583 void printLeases()
const;
603 PktListIterator eraseSent(
const PktListIterator it) {
604 if (archive_enabled_) {
611 static_cast<void>(archived_packets_.push_back(*it));
615 return(sent_packets_.template get<0>().erase(it));
624 PktListIterator next_sent_;
644 bool archive_enabled_;
655 double sum_delay_squared_;
668 uint64_t unordered_lookup_size_sum_;
670 uint64_t unordered_lookups_;
671 uint64_t ordered_lookups_;
675 uint64_t sent_packets_num_;
676 uint64_t rcvd_packets_num_;
678 uint64_t non_unique_addr_num_;
679 uint64_t rejected_leases_num_;
681 boost::posix_time::ptime boot_time_;
689 typedef typename std::map<ExchangeType, ExchangeStatsPtr>
ExchangesMap;
735 const double drop_time = -1) {
736 if (exchanges_.find(xchg_type) != exchanges_.end()) {
739 exchanges_[xchg_type] =
757 return (exchanges_.find(xchg_type) != exchanges_.end());
768 const std::string& long_name) {
769 if (custom_counters_.find(short_name) != custom_counters_.end()) {
771 "Custom counter " << short_name <<
" already added.");
773 custom_counters_[short_name] =
781 for (ExchangesMapIterator it = exchanges_.begin();
782 it != exchanges_.end();
784 if (it->second->getDroppedPacketsNum() > 0) {
798 CustomCounterPtr
getCounter(
const std::string& counter_key) {
799 CustomCountersMapIterator it = custom_counters_.find(counter_key);
800 if (it == custom_counters_.end()) {
802 "Custom counter " << counter_key <<
"does not exist");
815 const uint64_t value = 1) {
816 CustomCounterPtr counter = getCounter(counter_key);
832 const dhcp::PktPtr& packet) {
833 ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
834 xchg_stats->appendSent(packet);
852 const dhcp::PktPtr& packet) {
853 ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
854 dhcp::PktPtr sent_packet = xchg_stats->matchPackets(packet);
857 xchg_stats->updateDelays(sent_packet, packet);
858 if (archive_enabled_) {
859 xchg_stats->appendRcvd(packet);
874 ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
875 return(xchg_stats->getMinDelay());
887 ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
888 return(xchg_stats->getMaxDelay());
898 ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
899 return(xchg_stats->getAvgDelay());
909 ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
910 return(xchg_stats->getStdDevDelay());
922 ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
923 return(xchg_stats->getOrphans());
936 ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
937 return(xchg_stats->getAvgUnorderedLookupSetSize());
951 ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
952 return(xchg_stats->getUnorderedLookups());
967 ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
968 return(xchg_stats->getOrderedLookups());
980 ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
981 return(xchg_stats->getSentPacketsNum());
993 ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
994 return(xchg_stats->getRcvdPacketsNum());
1006 ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
1007 return(xchg_stats->getDroppedPacketsNum());
1021 ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
1022 return(xchg_stats->getCollectedNum());
1034 ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
1035 return(xchg_stats->getRejLeasesNum());
1043 ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
1044 xchg_stats->updateRejLeases();
1052 ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
1053 xchg_stats->updateNonUniqueAddr();
1065 ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
1066 return(xchg_stats->getNonUniqueAddrNum());
1078 time_period test_period(boot_time_,
1079 microsec_clock::universal_time());
1097 if (exchanges_.empty()) {
1099 "no exchange type added for tracking");
1101 for (ExchangesMapIterator it = exchanges_.begin();
1102 it != exchanges_.end();
1104 ExchangeStatsPtr xchg_stats = it->second;
1105 std::cout <<
"***Statistics for: " << it->first
1106 <<
"***" << std::endl;
1107 xchg_stats->printMainStats();
1108 std::cout << std::endl;
1109 xchg_stats->printRTTStats();
1110 std::cout << std::endl;
1124 std::ostringstream stream_sent;
1125 std::ostringstream stream_rcvd;
1126 std::ostringstream stream_drops;
1127 std::ostringstream stream_reject;
1128 std::string sep(
"");
1129 for (ExchangesMapIterator it = exchanges_.begin();
1130 it != exchanges_.end(); ++it) {
1132 if (it != exchanges_.begin()) {
1139 stream_sent << sep << it->second->getSentPacketsNum();
1140 stream_rcvd << sep << it->second->getRcvdPacketsNum();
1141 stream_drops << sep << it->second->getDroppedPacketsNum();
1142 stream_reject << sep << it->second->getRejLeasesNum();
1146 std::cout << stream_sent.str()
1147 << clean_sep << stream_rcvd.str()
1148 << clean_sep << stream_drops.str()
1149 << clean_sep << stream_reject.str()
1153 std::cout <<
"sent: " << stream_sent.str()
1154 <<
"; received: " << stream_rcvd.str()
1155 <<
"; drops: " << stream_drops.str()
1156 <<
"; rejected: " << stream_reject.str()
1173 if (exchanges_.empty()) {
1175 "no exchange type added for tracking");
1177 for (ExchangesMapIterator it = exchanges_.begin();
1178 it != exchanges_.end();
1180 ExchangeStatsPtr xchg_stats = it->second;
1181 std::cout <<
"***Timestamps for packets: " 1183 <<
"***" << std::endl;
1184 xchg_stats->printTimestamps();
1185 std::cout << std::endl;
1190 void printLeases()
const;
1199 if (custom_counters_.empty()) {
1202 for (CustomCountersMapIterator it = custom_counters_.begin();
1203 it != custom_counters_.end();
1205 CustomCounterPtr counter = it->second;
1206 std::cout << counter->getName() <<
": " << counter->getValue()
1211 std::tuple<typename ExchangeStats::PktListIterator, typename ExchangeStats::PktListIterator>
getSentPackets(
const ExchangeType xchg_type)
const {
1212 ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
1213 std::tuple<typename ExchangeStats::PktListIterator, typename ExchangeStats::PktListIterator> sent_packets_its = xchg_stats->getSentPackets();
1214 return(sent_packets_its);
1226 ExchangeStatsPtr getExchangeStats(
const ExchangeType xchg_type)
const {
1227 ExchangesMapIterator it = exchanges_.find(xchg_type);
1228 if (it == exchanges_.end()) {
1231 ExchangeStatsPtr xchg_stats = it->second;
1235 ExchangesMap exchanges_;
1236 CustomCountersMap custom_counters_;
1246 bool archive_enabled_;
1248 boost::posix_time::ptime boot_time_;
1258 #endif // STATS_MGR_H const CustomCounter & incrementCounter(const std::string &counter_key, const uint64_t value=1)
Increment specified counter.
uint64_t getRcvdPacketsNum() const
Return total number of received packets.
void printRTTStats() const
Print round trip time packets statistics.
void printStats() const
Print statistics counters for all exchange types.
double getStdDevDelay() const
Return standard deviation of packet delay.
uint64_t getRejLeasesNum(const ExchangeType xchg_type) const
Return total number of rejected leases.
double getStdDevDelay(const ExchangeType xchg_type) const
Return standard deviation of packet delay.
DHCPv6 SOLICIT-ADVERTISE.
ExchangeType
DHCP packet exchange types.
ExchangesMap::const_iterator ExchangesMapIterator
Iterator pointing to ExchangesMap.
CustomCounter(const std::string &name)
Constructor.
uint64_t getRcvdPacketsNum(const ExchangeType xchg_type) const
Return total number of received packets.
static uint32_t hashTransid(const dhcp::PktPtr &packet)
Hash transaction id of the packet.
CustomCountersMap::const_iterator CustomCountersMapIterator
Iterator for CustomCountersMap.
std::queue< PktListTransidHashIterator > PktListRemovalQueue
Packet list iterator queue for removal.
uint64_t getNonUniqueAddrNum() const
Return total number of non unique addresses.
double getMaxDelay(const ExchangeType xchg_type) const
Return maximum delay between sent and received packet.
double getAvgUnorderedLookupSetSize() const
Return average unordered lookup set size.
double getAvgUnorderedLookupSetSize(const ExchangeType xchg_type) const
Return average unordered lookup set size.
void updateRejLeases()
Increase number of rejected leases.
uint64_t getDroppedPacketsNum() const
Return number of dropped packets.
void appendSent(const dhcp::PktPtr &packet)
Add new packet to list of sent packets.
void printCustomCounters() const
Print names and values of custom counters.
boost::multi_index_container< dhcp::PktPtr, boost::multi_index::indexed_by< boost::multi_index::sequenced<>, boost::multi_index::ordered_non_unique< boost::multi_index::global_fun< const dhcp::PktPtr &, uint32_t, &ExchangeStats::hashTransid > > > > PktList
List of packets (sent or received).
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
uint64_t getUnorderedLookups() const
Return number of unordered sent packets lookups.
double getMinDelay(const ExchangeType xchg_type) const
Return minimum delay between sent and received packet.
uint64_t getNonUniqueAddrNum(const ExchangeType xchg_type) const
Return total number of non unique addresses.
uint64_t getOrderedLookups() const
Return number of ordered sent packets lookups.
DHCPv4 REQUEST-ACK (renewal)
std::map< std::string, CustomCounterPtr > CustomCountersMap
Map containing custom counters.
uint64_t getOrphans() const
Return number of orphan packets.
boost::shared_ptr< StatsMgr > StatsMgrPtr
Pointer to Statistics Manager;.
int dhcpVersion(ExchangeType const exchange_type)
Get the DHCP version that fits the exchange type.
void updateRejLeases(const ExchangeType xchg_type)
Increase total number of rejected leases.
std::ostream & operator<<(std::ostream &os, ExchangeType xchg_type)
Return name of the exchange.
uint64_t getCollectedNum() const
Return number of garbage collected packets.
CustomCounterPtr getCounter(const std::string &counter_key)
Return specified counter.
static int malformed_pkts_
const std::string & getName() const
Return counter name.
void printIntermediateStats(bool clean_report, std::string clean_sep) const
Print intermediate statistics.
boost::shared_ptr< isc::dhcp::Pkt > PktPtr
A pointer to either Pkt4 or Pkt6 packet.
dhcp::PktPtr passRcvdPacket(const ExchangeType xchg_type, const dhcp::PktPtr &packet)
Add new received packet and match with sent packet.
const CustomCounter & operator+=(int val)
void updateNonUniqueAddr()
Increase number of non unique addresses.
boost::posix_time::time_period getTestPeriod() const
Get time period since the start of test.
bool hasExchangeStats(const ExchangeType xchg_type) const
Check if the exchange type has been specified.
const CustomCounter & operator++()
Increment operator.
This is a base class for exceptions thrown from the DNS library module.
void printMainStats() const
Print main statistics for packet exchange.
Defines the logger used by the top-level component of kea-lfc.
PktList::iterator PktListIterator
Packet list iterator for sequential access to elements.
PktList::template nth_index< 1 >::type PktListTransidHashIndex
Packet list index to search packets using transaction id hash.
void appendRcvd(const dhcp::PktPtr &packet)
Add new packet to list of received packets.
uint64_t getDroppedPacketsNum(const ExchangeType xchg_type) const
Return total number of dropped packets.
uint64_t getOrphans(const ExchangeType xchg_type) const
Return number of orphan packets.
void addExchangeStats(const ExchangeType xchg_type, const double drop_time=-1)
Specify new exchange type.
PktListTransidHashIndex::const_iterator PktListTransidHashIterator
Packet list iterator to access packets using transaction id hash.
A generic exception that is thrown if a function is called in a prohibited way.
double getMaxDelay() const
Return maximum delay between sent and received packet.
double getAvgDelay(const ExchangeType xchg_type) const
Return average packet delay.
uint64_t getUnorderedLookups(const ExchangeType xchg_type) const
Return number of unordered sent packets lookups.
uint64_t getRejLeasesNum() const
Return total number of rejected leases.
uint64_t getOrderedLookups(const ExchangeType xchg_type) const
Return number of ordered sent packets lookups.
uint64_t getValue() const
Return counter value.
void addCustomCounter(const std::string &short_name, const std::string &long_name)
Add named custom uint64 counter.
bool droppedPackets() const
Check if any packet drops occurred.
double getMinDelay() const
Return minimum delay between sent and received packet.
uint64_t getSentPacketsNum() const
Return total number of sent packets.
uint64_t getCollectedNum(const ExchangeType xchg_type) const
Return number of garbage collected packets.
double getAvgDelay() const
Return average packet delay.
std::tuple< PktListIterator, PktListIterator > getSentPackets()
boost::shared_ptr< ExchangeStats > ExchangeStatsPtr
Pointer to ExchangeStats.
boost::shared_ptr< CustomCounter > CustomCounterPtr
std::map< ExchangeType, ExchangeStatsPtr > ExchangesMap
Map containing all specified exchange types.
void printTimestamps() const
Print timestamps of all packets.
void updateNonUniqueAddrNum(const ExchangeType xchg_type)
Increase total number of non unique addresses.
uint64_t getSentPacketsNum(const ExchangeType xchg_type) const
Return total number of sent packets.
std::tuple< typename ExchangeStats::PktListIterator, typename ExchangeStats::PktListIterator > getSentPackets(const ExchangeType xchg_type) const
void passSentPacket(const ExchangeType xchg_type, const dhcp::PktPtr &packet)
Adds new packet to the sent packets list.
const CustomCounter & operator++(int)
Increment operator.