stats_mgr.h 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef __STATS_MGR_H
  15. #define __STATS_MGR_H
  16. #include <iostream>
  17. #include <map>
  18. #include <boost/noncopyable.hpp>
  19. #include <boost/shared_ptr.hpp>
  20. #include <boost/multi_index_container.hpp>
  21. #include <boost/multi_index/hashed_index.hpp>
  22. #include <boost/multi_index/sequenced_index.hpp>
  23. #include <boost/multi_index/global_fun.hpp>
  24. #include <boost/multi_index/mem_fun.hpp>
  25. #include <boost/date_time/posix_time/posix_time.hpp>
  26. #include <exceptions/exceptions.h>
  27. namespace isc {
  28. namespace perfdhcp {
  29. /// \brief Statistics Manager
  30. ///
  31. /// This class template is a storage for various performance statistics
  32. /// collected during performance tests execution with perfdhcp tool.
  33. ///
  34. /// Statistics Manager holds lists of sent and received packets and
  35. /// groups them into exchanges. For example: DHCPDISCOVER message and
  36. /// corresponding DHCPOFFER messages belong to one exchange, DHCPREQUEST
  37. /// and corresponding DHCPACK message belong to another exchange etc.
  38. /// In order to update statistics for a particular exchange type, client
  39. /// class passes sent and received packets. Internally, Statistics Manager
  40. /// tries to match transaction id of received packet with sent packet
  41. /// stored on the list of sent packets. When packets are matched the
  42. /// round trip time can be calculated.
  43. ///
  44. /// \tparam T class representing DHCPv4 or DHCPv6 packet.
  45. template <class T>
  46. class StatsMgr : public boost::noncopyable {
  47. public:
  48. /// \brief Custom Counter
  49. ///
  50. /// This class represents custom statistics counters. Client class
  51. /// may create unlimited number of counters. Such counters are
  52. /// being stored in map in Statistics Manager and access using
  53. /// unique string key.
  54. class CustomCounter {
  55. public:
  56. /// \brief Constructor.
  57. ///
  58. /// This constructor sets counter name. This name is used in
  59. /// log file to report value of each counter.
  60. ///
  61. /// \param name name of the counter used in log file.
  62. CustomCounter(const std::string& name) :
  63. counter_(0),
  64. name_(name) { };
  65. /// \brief Increment operator.
  66. const CustomCounter& operator++() {
  67. ++counter_;
  68. return(*this);
  69. }
  70. /// \brief Increment operator.
  71. const CustomCounter& operator++(int) {
  72. CustomCounter& this_counter(*this);
  73. operator++();
  74. return(this_counter);
  75. }
  76. /// \brief Return counter value.
  77. ///
  78. /// Method returns counter value.
  79. ///
  80. /// \return counter value.
  81. uint64_t getValue() const { return counter_; }
  82. /// \brief Return counter name.
  83. ///
  84. /// Method returns counter name.
  85. ///
  86. /// \return counter name.
  87. const std::string& getName() const { return name_; }
  88. private:
  89. /// \brief Default constructor.
  90. ///
  91. /// Default constrcutor is private because we don't want client
  92. /// class to call it because we want client class to specify
  93. /// counter's name.
  94. CustomCounter() { };
  95. uint64_t counter_; ///< Counter's value.
  96. std::string name_; ///< Counter's name.
  97. };
  98. typedef typename boost::shared_ptr<CustomCounter> CustomCounterPtr;
  99. /// DHCP packet exchange types.
  100. enum ExchangeType {
  101. XCHG_DO, ///< DHCPv4 DISCOVER-OFFER
  102. XCHG_RA, ///< DHCPv4 REQUEST-ACK
  103. XCHG_SA, ///< DHCPv6 SOLICIT-ADVERTISE
  104. XCHG_RR ///< DHCPv6 REQUEST-REPLY
  105. };
  106. /// \brief Exchange Statistics.
  107. ///
  108. /// This class collects statistics for exchanges. Parent class
  109. /// may define number of different packet exchanges like:
  110. /// DHCPv4 DISCOVER-OFFER, DHCPv6 SOLICIT-ADVERTISE etc. Performance
  111. /// statistics will be collected for each of those separately in
  112. /// corresponding instance of ExchangeStats.
  113. class ExchangeStats {
  114. public:
  115. /// \brief Hash transaction id of the packet.
  116. ///
  117. /// Function hashes transaction id of the packet. Hashing is
  118. /// non-unique. Many packets may have the same hash value and thus
  119. /// they belong to the same packet buckets. Packet buckets are
  120. /// used for unordered packets search with multi index container.
  121. ///
  122. /// \param packet packet which transaction id is to be hashed.
  123. /// \throw isc::BadValue if packet is null.
  124. /// \return transaction id hash.
  125. static uint32_t hashTransid(const boost::shared_ptr<const T>& packet) {
  126. if (!packet) {
  127. isc_throw(BadValue, "Packet is null");
  128. }
  129. return(packet->getTransid() & 1023);
  130. }
  131. /// \brief List of packets (sent or received).
  132. ///
  133. /// List of packets based on multi index container allows efficient
  134. /// search of packets based on their sequence (order in which they
  135. /// were inserted) as well as based on their hashed transaction id.
  136. /// The first index (sequenced) provides the way to use container
  137. /// as a regular list (including iterators, removal of elements from
  138. /// the middle of the collection etc.). This index is meant to be used
  139. /// more frequently than the latter one and it is based on the
  140. /// assumption that responses from the DHCP server are received in
  141. /// order. In this case, when next packet is received it can be
  142. /// matched with next packet on the list of sent packets. This
  143. /// prevents intensive searches on the list of sent packets every
  144. /// time new packet arrives. In many cases however packets can be
  145. /// dropped by the server or may be sent out of order and we still
  146. /// want to have ability to search packets using transaction id.
  147. /// The second index can be used for this purpose. This index is
  148. /// hashing transaction ids using custom function \ref hashTransid.
  149. /// Note that other possibility would be to simply specify index
  150. /// that uses transaction id directly (instead of hashing with
  151. /// \ref hashTransid). In this case however we have chosen to use
  152. /// hashing function because it shortens the index size to just
  153. /// 1023 values maximum. Search operation on this index generally
  154. /// returns the range of packets that have the same transaction id
  155. /// hash assigned but most often these ranges will be short so further
  156. /// search within a range to find a packet with pacrticular transaction
  157. /// id will not be intensive.
  158. ///
  159. /// Example 1: Add elements to the list
  160. /// \code
  161. /// PktList packets_collection();
  162. /// boost::shared_ptr<Pkt4> pkt1(new Pkt4(...));
  163. /// boost::shared_ptr<Pkt4> pkt2(new Pkt4(...));
  164. /// // Add new packet to the container, it will be available through
  165. /// // both indexes
  166. /// packets_collection.push_back(pkt1);
  167. /// // Here is another way to add packet to the container. The result
  168. /// // is exactly the same as previously.
  169. /// packets_collection.template get<0>().push_back(pkt2);
  170. /// \endcode
  171. ///
  172. /// Example 2: Access elements through sequencial index
  173. /// \code
  174. /// PktList packets_collection();
  175. /// ... # Add elements to the container
  176. /// for (PktListIterator it = packets_collection.begin();
  177. /// it != packets_collection.end();
  178. /// ++it) {
  179. /// boost::shared_ptr<Pkt4> pkt = *it;
  180. /// # Do something with packet;
  181. /// }
  182. /// \endcode
  183. ///
  184. /// Example 3: Access elements through hashed index
  185. /// \code
  186. /// // Get the instance of the second search index.
  187. /// PktListTransidHashIndex& idx = sent_packets_.template get<1>();
  188. /// // Get the range (bucket) of packets sharing the same transaction
  189. /// // id hash.
  190. /// std::pair<PktListTransidHashIterator,PktListTransidHashIterator> p =
  191. /// idx.equal_range(hashTransid(rcvd_packet));
  192. /// // Iterate through the returned bucket.
  193. /// for (PktListTransidHashIterator it = p.first; it != p.second;
  194. /// ++it) {
  195. /// boost::shared_ptr pkt = *it;
  196. /// ... # Do something with the packet (e.g. check transaction id)
  197. /// }
  198. /// \endcode
  199. typedef boost::multi_index_container<
  200. boost::shared_ptr<const T>,
  201. boost::multi_index::indexed_by<
  202. boost::multi_index::sequenced<>,
  203. boost::multi_index::hashed_non_unique<
  204. boost::multi_index::global_fun<
  205. const boost::shared_ptr<const T>&,
  206. uint32_t,
  207. &ExchangeStats::hashTransid
  208. >
  209. >
  210. >
  211. > PktList;
  212. /// Packet list iterator for sequencial access to elements.
  213. typedef typename PktList::const_iterator PktListIterator;
  214. /// Packet list index to search packets using transaction id hash.
  215. typedef typename PktList::template nth_index<1>::type
  216. PktListTransidHashIndex;
  217. /// Packet list iterator to access packets using transaction id hash.
  218. typedef typename PktListTransidHashIndex::const_iterator
  219. PktListTransidHashIterator;
  220. /// \brief Constructor
  221. ///
  222. /// \param xchg_type exchange type
  223. ExchangeStats(const ExchangeType xchg_type)
  224. : xchg_type_(xchg_type),
  225. min_delay_(std::numeric_limits<double>::max()),
  226. max_delay_(0.),
  227. sum_delay_(0.),
  228. orphans_(0),
  229. sum_delay_squared_(0.),
  230. ordered_lookups_(0),
  231. unordered_lookup_size_sum_(0),
  232. unordered_lookups_(0),
  233. sent_packets_num_(0),
  234. rcvd_packets_num_(0) {
  235. next_sent_ = sent_packets_.begin();
  236. }
  237. /// \brief Add new packet to list of sent packets.
  238. ///
  239. /// Method adds new packet to list of sent packets.
  240. ///
  241. /// \param packet packet object to be added.
  242. /// \throw isc::BadValue if packet is null.
  243. void appendSent(const boost::shared_ptr<const T>& packet) {
  244. if (!packet) {
  245. isc_throw(BadValue, "Packet is null");
  246. }
  247. ++sent_packets_num_;
  248. sent_packets_.template get<0>().push_back(packet);
  249. }
  250. /// \brief Add new packet to list of received packets.
  251. ///
  252. /// Method adds new packet to list of received packets.
  253. ///
  254. /// \param packet packet object to be added.
  255. /// \throw isc::BadValue if packet is null.
  256. void appendRcvd(const boost::shared_ptr<const T>& packet) {
  257. if (!packet) {
  258. isc_throw(BadValue, "Packet is null");
  259. }
  260. ++rcvd_packets_num_;
  261. rcvd_packets_.push_back(packet);
  262. }
  263. /// \brief Update delay counters.
  264. ///
  265. /// Method updates delay counters based on timestamps of
  266. /// sent and received packets.
  267. ///
  268. /// \param sent_packet sent packet
  269. /// \param rcvd_packet received packet
  270. /// \throw isc::BadValue if sent or received packet is null.
  271. /// \throw isc::Unexpected if failed to calculate timestamps
  272. void updateDelays(const boost::shared_ptr<const T>& sent_packet,
  273. const boost::shared_ptr<const T>& rcvd_packet) {
  274. if (!sent_packet) {
  275. isc_throw(BadValue, "Sent packet is null");
  276. }
  277. if (!rcvd_packet) {
  278. isc_throw(BadValue, "Received packet is null");
  279. }
  280. boost::posix_time::ptime sent_time = sent_packet->getTimestamp();
  281. boost::posix_time::ptime rcvd_time = rcvd_packet->getTimestamp();
  282. if (sent_time.is_not_a_date_time() ||
  283. rcvd_time.is_not_a_date_time()) {
  284. isc_throw(Unexpected,
  285. "Timestamp must be set for sent and "
  286. "received packet to measure RTT");
  287. }
  288. boost::posix_time::time_period period(sent_time, rcvd_time);
  289. // We don't bother calculating deltas in nanoseconds. It is much
  290. // more convenient to use seconds instead because we are going to
  291. // sum them up.
  292. double delta =
  293. static_cast<double>(period.length().total_nanoseconds()) / 1e9;
  294. if (delta < 0) {
  295. isc_throw(Unexpected, "Sent packet's timestamp must not be "
  296. "greater than received packet's timestamp");
  297. }
  298. // Record the minimum delay between sent and received packets.
  299. if (delta < min_delay_) {
  300. min_delay_ = delta;
  301. }
  302. // Record the maximum delay between sent and received packets.
  303. if (delta > max_delay_) {
  304. max_delay_ = delta;
  305. }
  306. // Update delay sum and square sum. That will be used to calculate
  307. // mean delays.
  308. sum_delay_ += delta;
  309. sum_delay_squared_ += delta * delta;
  310. }
  311. /// \brief Find packet on the list of sent packets.
  312. ///
  313. /// Method finds packet with specified transaction id on the list
  314. /// of sent packets. It is used to match received packet with
  315. /// corresponding sent packet.
  316. /// Since packets from the server most often come in the same order
  317. /// as they were sent by client, this method will first check if
  318. /// next sent packet matches. If it doesn't, function will search
  319. /// the packet using indexing by transaction id. This reduces
  320. /// packet search time significantly.
  321. ///
  322. /// \param rcvd_packet received packet to be matched with sent packet.
  323. /// \throw isc::BadValue if received packet is null.
  324. /// \return packet having specified transaction or NULL if packet
  325. /// not found
  326. boost::shared_ptr<const T> findSent(const boost::shared_ptr<const T>& rcvd_packet) {
  327. if (!rcvd_packet) {
  328. isc_throw(BadValue, "Received packet is null");
  329. }
  330. if (sent_packets_.size() == 0) {
  331. // List of sent packets is empty so there is no sense
  332. // to continue looking fo the packet. It also means
  333. // that the received packet we got has no corresponding
  334. // sent packet so orphans counter has to be updated.
  335. ++orphans_;
  336. return(boost::shared_ptr<const T>());
  337. } else if (next_sent_ == sent_packets_.end()) {
  338. // Even if there are still many unmatched packets on the
  339. // list we might hit the end of it because of unordered
  340. // lookups. The next logical step is to reset iterator.
  341. next_sent_ = sent_packets_.begin();
  342. }
  343. // With this variable we will be signalling success or failure
  344. // to find the packet.
  345. bool packet_found = false;
  346. // Most likely responses are sent from the server in the same
  347. // order as client's requests to the server. We are caching
  348. // next sent packet and first try to match it with the next
  349. // incoming packet. We are successful if there is no
  350. // packet drop or out of order packets sent. This is actually
  351. // the fastest way to look for packets.
  352. if ((*next_sent_)->getTransid() == rcvd_packet->getTransid()) {
  353. ++ordered_lookups_;
  354. packet_found = true;
  355. } else {
  356. // If we are here, it means that we were unable to match the
  357. // next incoming packet with next sent packet so we need to
  358. // take a little more expensive approach to look packets using
  359. // alternative index (transaction id & 1023).
  360. PktListTransidHashIndex& idx = sent_packets_.template get<1>();
  361. // Packets are grouped using trasaction id masked with value
  362. // of 1023. For instance, packets with transaction id equal to
  363. // 1, 1024 ... will belong to the same group (a.k.a. bucket).
  364. // When using alternative index we don't find the packet but
  365. // bucket of packets and we need to iterate through the bucket
  366. // to find the one that has desired transaction id.
  367. std::pair<PktListTransidHashIterator,PktListTransidHashIterator> p =
  368. idx.equal_range(hashTransid(rcvd_packet));
  369. // We want to keep statistics of unordered lookups to make
  370. // sure that there is a right balance between number of
  371. // unordered lookups and ordered lookups. If number of unordered
  372. // lookups is high it may mean that many packets are lost or
  373. // sent out of order.
  374. ++unordered_lookups_;
  375. // We also want to keep the mean value of the bucket. The lower
  376. // bucket size the better. If bucket sizes appear to big we
  377. // might want to increase number of buckets.
  378. unordered_lookup_size_sum_ += std::distance(p.first, p.second);
  379. for (PktListTransidHashIterator it = p.first; it != p.second;
  380. ++it) {
  381. if ((*it)->getTransid() == rcvd_packet->getTransid()) {
  382. packet_found = true;
  383. next_sent_ =
  384. sent_packets_.template project<0>(it);
  385. break;
  386. }
  387. }
  388. }
  389. if (!packet_found) {
  390. // If we are here, it means that both ordered lookup and
  391. // unordered lookup failed. Searched packet is not on the list.
  392. ++orphans_;
  393. return(boost::shared_ptr<const T>());
  394. }
  395. boost::shared_ptr<const T> sent_packet(*next_sent_);
  396. // If packet was found, we assume it will be never searched
  397. // again. We want to delete this packet from the list to
  398. // improve performance of future searches.
  399. next_sent_ = eraseSent(next_sent_);
  400. return(sent_packet);
  401. }
  402. /// \brief Return minumum delay between sent and received packet.
  403. ///
  404. /// Method returns minimum delay between sent and received packet.
  405. ///
  406. /// \return minimum delay between packets.
  407. double getMinDelay() const { return(min_delay_); }
  408. /// \brief Return maxmimum delay between sent and received packet.
  409. ///
  410. /// Method returns maximum delay between sent and received packet.
  411. ///
  412. /// \return maximum delay between packets.
  413. double getMaxDelay() const { return(max_delay_); }
  414. /// \brief Return avarage packet delay.
  415. ///
  416. /// Method returns average packet delay. If no packets have been
  417. /// received for this exchange avg delay can't be calculated and
  418. /// thus method throws exception.
  419. ///
  420. /// \throw isc::InvalidOperation if no packets for this exchange
  421. /// have been received yet.
  422. /// \return average packet delay.
  423. double getAvgDelay() const {
  424. if (sum_delay_ == 0) {
  425. isc_throw(InvalidOperation, "no packets received");
  426. }
  427. return(sum_delay_ / rcvd_packets_num_);
  428. }
  429. /// \brief Return standard deviation of packet delay.
  430. ///
  431. /// Method returns standard deviation of packet delay. If no
  432. /// packets have been received for this exchange, the standard
  433. /// deviation can't be calculated and thus method throws
  434. /// exception.
  435. ///
  436. /// \throw isc::InvalidOperation if number of received packets
  437. /// for the exchange is equal to zero.
  438. /// \return standard deviation of packet delay.
  439. double getStdDevDelay() const {
  440. if (rcvd_packets_num_ == 0) {
  441. isc_throw(InvalidOperation, "no packets received");
  442. }
  443. return(sqrt(sum_delay_squared_ / rcvd_packets_num_ -
  444. getAvgDelay() * getAvgDelay()));
  445. }
  446. /// \brief Return number of orphant packets.
  447. ///
  448. /// Method returns number of received packets that had no matching
  449. /// sent packet. It is possible that such packet was late or not
  450. /// for us.
  451. ///
  452. /// \return number of orphant received packets.
  453. uint64_t getOrphans() const { return(orphans_); }
  454. /// \brief Return average unordered lookup set size.
  455. ///
  456. /// Method returns average unordered lookup set size.
  457. /// This value changes every time \ref ExchangeStats::findSent
  458. /// function performs unordered packet lookup.
  459. ///
  460. /// \throw isc::InvalidOperation if there have been no unordered
  461. /// lookups yet.
  462. /// \return average unordered lookup set size.
  463. double getAvgUnorderedLookupSetSize() const {
  464. if (unordered_lookups_ == 0) {
  465. isc_throw(InvalidOperation, "no unordered lookups");
  466. }
  467. return(static_cast<double>(unordered_lookup_size_sum_) /
  468. static_cast<double>(unordered_lookups_));
  469. }
  470. /// \brief Return number of unordered sent packets lookups
  471. ///
  472. /// Method returns number of unordered sent packet lookups.
  473. /// Unordered lookup is used when received packet was sent
  474. /// out of order by server - transaction id of received
  475. /// packet does not match transaction id of next sent packet.
  476. ///
  477. /// \return number of unordered lookups.
  478. uint64_t getUnorderedLookups() const { return(unordered_lookups_); }
  479. /// \brief Return number of ordered sent packets lookups
  480. ///
  481. /// Method returns number of ordered sent packet lookups.
  482. /// Ordered lookup is used when packets are received in the
  483. /// same order as they were sent to the server.
  484. /// If packets are skipped or received out of order, lookup
  485. /// function will use unordered lookup (with hash table).
  486. ///
  487. /// \return number of ordered lookups.
  488. uint64_t getOrderedLookups() const { return(ordered_lookups_); }
  489. /// \brief Return total number of sent packets
  490. ///
  491. /// Method returns total number of sent packets.
  492. ///
  493. /// \return number of sent packets.
  494. uint64_t getSentPacketsNum() const { return(sent_packets_num_); }
  495. /// \brief Return total number of received packets
  496. ///
  497. /// Method returns total number of received packets.
  498. ///
  499. /// \return number of received packets.
  500. uint64_t getRcvdPacketsNum() const { return(rcvd_packets_num_); }
  501. /// \brief Print main statistics for packet exchange.
  502. ///
  503. /// Method prints main statistics for particular exchange.
  504. /// Statistics includes: number of sent and received packets,
  505. /// number of dropped packets and number of orphans.
  506. void printMainStats() const {
  507. using namespace std;
  508. uint64_t drops = getRcvdPacketsNum() - getSentPacketsNum();
  509. cout << "sent packets: " << getSentPacketsNum() << endl
  510. << "received packets: " << getRcvdPacketsNum() << endl
  511. << "drops: " << drops << endl
  512. << "orphans: " << getOrphans() << endl;
  513. }
  514. /// \brief Print round trip time packets statistics.
  515. ///
  516. /// Method prints round trip time packets statistics. Statistics
  517. /// includes minimum packet delay, maximum packet delay, average
  518. /// packet delay and standard deviation of delays. Packet delay
  519. /// is a duration between sending a packet to server and receiving
  520. /// response from server.
  521. void printRTTStats() const {
  522. using namespace std;
  523. cout << fixed << setprecision(3)
  524. << "min delay: " << getMinDelay() * 1e3 << " ms" << endl
  525. << "avg delay: " << getAvgDelay() * 1e3 << " ms" << endl
  526. << "max delay: " << getMaxDelay() * 1e3 << " ms" << endl
  527. << "std deviation: " << getStdDevDelay() * 1e3 << " ms"
  528. << endl;
  529. }
  530. //// \brief Print timestamps for sent and received packets.
  531. ///
  532. /// Method prints timestamps for all sent and received packets for
  533. /// packet exchange.
  534. ///
  535. /// \throw isc::InvalidOperation if found packet with no timestamp set.
  536. void printTimestamps() {
  537. // We will be using boost::posix_time extensivelly here
  538. using namespace boost::posix_time;
  539. // Iterate through all received packets.
  540. for (PktListIterator it = rcvd_packets_.begin();
  541. it != rcvd_packets_.end();
  542. ++it) {
  543. boost::shared_ptr<const T> rcvd_packet = *it;
  544. PktListTransidHashIndex& idx =
  545. archived_packets_.template get<1>();
  546. std::pair<PktListTransidHashIterator,
  547. PktListTransidHashIterator> p =
  548. idx.equal_range(hashTransid(rcvd_packet));
  549. for (PktListTransidHashIterator it_archived = p.first;
  550. it_archived != p.second;
  551. ++it) {
  552. if ((*it_archived)->getTransid() ==
  553. rcvd_packet->getTransid()) {
  554. boost::shared_ptr<const T> sent_packet = *it_archived;
  555. // Get sent and received packet times.
  556. ptime sent_time = sent_packet->getTimestamp();
  557. ptime rcvd_time = rcvd_packet->getTimestamp();
  558. // All sent and received packets should have timestamps
  559. // set but if there is a bug somewhere and packet does
  560. // not have timestamp we want to catch this here.
  561. if (sent_time.is_not_a_date_time() ||
  562. rcvd_time.is_not_a_date_time()) {
  563. isc_throw(InvalidOperation, "packet time is not set");
  564. }
  565. // Calculate durations of packets from beginning of epoch.
  566. ptime epoch_time(min_date_time);
  567. time_period sent_period(epoch_time, sent_time);
  568. time_period rcvd_period(epoch_time, rcvd_time);
  569. // Print timestamps for sent and received packet.
  570. std::cout << "sent / received: "
  571. << to_iso_string(sent_period.length())
  572. << " / "
  573. << to_iso_string(rcvd_period.length())
  574. << std::endl;
  575. break;
  576. }
  577. }
  578. }
  579. }
  580. private:
  581. /// \brief Private default constructor.
  582. ///
  583. /// Default constructor is private because we want the client
  584. /// class to specify exchange type explicitely.
  585. ExchangeStats();
  586. /// \brief Erase packet from the list of sent packets.
  587. ///
  588. /// Method erases packet from the list of sent packets.
  589. ///
  590. /// \param it iterator pointing to packet to be erased.
  591. /// \return iterator pointing to packet following erased
  592. /// packet or sent_packets_.end() if packet not found.
  593. PktListIterator eraseSent(const PktListIterator it) {
  594. // We don't want to keep list of all sent packets
  595. // because it will affect packet lookup performance.
  596. // If packet is matched with received packet we
  597. // move it to list of archived packets. List of
  598. // archived packets may be used for diagnostics
  599. // when test is completed.
  600. archived_packets_.push_back(*it);
  601. // get<0>() template returns sequencial index to
  602. // container.
  603. return(sent_packets_.template get<0>().erase(it));
  604. }
  605. ExchangeType xchg_type_; ///< Packet exchange type.
  606. PktList sent_packets_; ///< List of sent packets.
  607. /// Iterator pointing to the packet on sent list which will most
  608. /// likely match next received packet. This is based on the
  609. /// assumption that server responds in order to incoming packets.
  610. PktListIterator next_sent_;
  611. PktList rcvd_packets_; ///< List of received packets.
  612. /// List of archived packets. All sent packets that have
  613. /// been matched with received packet are moved to this
  614. /// list for diagnostics purposes.
  615. PktList archived_packets_;
  616. double min_delay_; ///< Minimum delay between sent
  617. ///< and received packets.
  618. double max_delay_; ///< Maximum delay between sent
  619. ///< and received packets.
  620. double sum_delay_; ///< Sum of delays between sent
  621. ///< and received packets.
  622. double sum_delay_squared_; ///< Squared sum of delays between
  623. ///< sent and recived packets.
  624. uint64_t orphans_; ///< Number of orphant received packets.
  625. /// Sum of unordered lookup sets. Needed to calculate mean size of
  626. /// lookup set. It is desired that number of unordered lookups is
  627. /// minimal for performance reasons. Tracking number of lookups and
  628. /// mean size of the lookup set should give idea of packets serach
  629. /// complexity.
  630. uint64_t unordered_lookup_size_sum_;
  631. uint64_t unordered_lookups_; ///< Number of unordered sent packets
  632. ///< lookups.
  633. uint64_t ordered_lookups_; ///< Number of ordered sent packets
  634. ///< lookups.
  635. uint64_t sent_packets_num_; ///< Total number of sent packets.
  636. uint64_t rcvd_packets_num_; ///< Total number of received packets.
  637. };
  638. /// Pointer to ExchangeStats.
  639. typedef boost::shared_ptr<ExchangeStats> ExchangeStatsPtr;
  640. /// Map containing all specified exchange types.
  641. typedef typename std::map<ExchangeType, ExchangeStatsPtr> ExchangesMap;
  642. /// Iterator poiting to \ref ExchangesMap
  643. typedef typename ExchangesMap::const_iterator ExchangesMapIterator;
  644. /// Map containing custom counters.
  645. typedef typename std::map<std::string, CustomCounterPtr> CustomCountersMap;
  646. /// Iterator for \ref CustomCountersMap.
  647. typedef typename CustomCountersMap::const_iterator CustomCountersMapIterator;
  648. /// \brief Constructor.
  649. StatsMgr()
  650. : exchanges_(),
  651. custom_counters_() {
  652. }
  653. /// \brief Specify new exchange type.
  654. ///
  655. /// This method creates new \ref ExchangeStats object that will
  656. /// collect statistics data from packets exchange of the specified
  657. /// type.
  658. ///
  659. /// \param xchg_type exchange type.
  660. /// \throw isc::BadValue if exchange of specified type exists.
  661. void addExchangeStats(const ExchangeType xchg_type) {
  662. if (exchanges_.find(xchg_type) != exchanges_.end()) {
  663. isc_throw(BadValue, "Exchange of specified type already added.");
  664. }
  665. exchanges_[xchg_type] = ExchangeStatsPtr(new ExchangeStats(xchg_type));
  666. }
  667. /// \brief Add named custom uint64 counter.
  668. ///
  669. /// Method creates new named counter and stores in counter's map under
  670. /// key specified here as short_name.
  671. ///
  672. /// \param short_name key to use to access counter in the map.
  673. /// \param long_name name of the counter presented in the log file.
  674. void addCustomCounter(const std::string& short_name,
  675. const std::string& long_name) {
  676. if (custom_counters_.find(short_name) != custom_counters_.end()) {
  677. isc_throw(BadValue,
  678. "Custom counter " << short_name << " already added.");
  679. }
  680. custom_counters_[short_name] =
  681. CustomCounterPtr(new CustomCounter(long_name));
  682. }
  683. /// \brief Return specified counter.
  684. ///
  685. /// Method returns specified counter.
  686. ///
  687. /// \param counter_key key poiting to the counter in the counters map.
  688. /// The short counter name has to be used to access counter.
  689. /// \return pointer to specified counter object.
  690. CustomCounterPtr getCounter(const std::string& counter_key) {
  691. CustomCountersMapIterator it = custom_counters_.find(counter_key);
  692. if (it == custom_counters_.end()) {
  693. isc_throw(BadValue,
  694. "Custom counter " << counter_key << "does not exist");
  695. }
  696. return(it->second);
  697. }
  698. /// \brief Increment specified counter.
  699. ///
  700. /// Increement counter value by one.
  701. ///
  702. /// \param counter_key key poitinh to the counter in the counters map.
  703. /// \return pointer to specified counter after incrementation.
  704. const CustomCounter& IncrementCounter(const std::string& counter_key) {
  705. CustomCounterPtr counter = getCounter(counter_key);
  706. return(++(*counter));
  707. }
  708. /// \brief Adds new packet to the sent packets list.
  709. ///
  710. /// Method adds new packet to the sent packets list.
  711. /// Packets are added to the list sequentially and
  712. /// most often read sequentially.
  713. ///
  714. /// \param xchg_type exchange type.
  715. /// \param packet packet to be added to the list
  716. /// \throw isc::BadValue if invalid exchange type specified or
  717. /// packet is null.
  718. void passSentPacket(const ExchangeType xchg_type,
  719. const boost::shared_ptr<const T>& packet) {
  720. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  721. xchg_stats->appendSent(packet);
  722. }
  723. /// \brief Add new received packet and match with sent packet.
  724. ///
  725. /// Method adds new packet to the list of received packets. It
  726. /// also searches for corresponding packet on the list of sent
  727. /// packets. When packets are matched the statistics counters
  728. /// are updated accordingly for the particular exchange type.
  729. ///
  730. /// \param xchg_type exchange type.
  731. /// \param packet received packet
  732. /// \throw isc::BadValue if invalid exchange type specified
  733. /// or packet is null.
  734. /// \throw isc::Unexpected if corresponding packet was not
  735. /// found on the list of sent packets.
  736. void passRcvdPacket(const ExchangeType xchg_type,
  737. const boost::shared_ptr<const T>& packet) {
  738. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  739. boost::shared_ptr<const T> sent_packet
  740. = xchg_stats->findSent(packet);
  741. if (sent_packet) {
  742. xchg_stats->updateDelays(sent_packet, packet);
  743. xchg_stats->appendRcvd(packet);
  744. }
  745. }
  746. /// \brief Return minumum delay between sent and received packet.
  747. ///
  748. /// Method returns minimum delay between sent and received packet
  749. /// for specified exchange type.
  750. ///
  751. /// \param xchg_type exchange type.
  752. /// \throw isc::BadValue if invalid exchange type specified.
  753. /// \return minimum delay between packets.
  754. double getMinDelay(const ExchangeType xchg_type) const {
  755. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  756. return(xchg_stats->getMinDelay());
  757. }
  758. /// \brief Return maxmimum delay between sent and received packet.
  759. ///
  760. /// Method returns maximum delay between sent and received packet
  761. /// for specified exchange type.
  762. ///
  763. /// \param xchg_type exchange type.
  764. /// \throw isc::BadValue if invalid exchange type specified.
  765. /// \return maximum delay between packets.
  766. double getMaxDelay(const ExchangeType xchg_type) const {
  767. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  768. return(xchg_stats->getMaxDelay());
  769. }
  770. /// \brief Return avarage packet delay.
  771. ///
  772. /// Method returns average packet delay for specified
  773. /// exchange type.
  774. ///
  775. /// \return average packet delay.
  776. double getAvgDelay(const ExchangeType xchg_type) const {
  777. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  778. return(xchg_stats->getAvgDelay());
  779. }
  780. /// \brief Return standard deviation of packet delay.
  781. ///
  782. /// Method returns standard deviation of packet delay
  783. /// for specified exchange type.
  784. ///
  785. /// \return standard deviation of packet delay.
  786. double getStdDevDelay(const ExchangeType xchg_type) const {
  787. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  788. return(xchg_stats->getStdDevDelay());
  789. }
  790. /// \brief Return number of orphant packets.
  791. ///
  792. /// Method returns number of orphant packets for specified
  793. /// exchange type.
  794. ///
  795. /// \param xchg_type exchange type.
  796. /// \throw isc::BadValue if invalid exchange type specified.
  797. /// \return number of orphant packets so far.
  798. uint64_t getOrphans(const ExchangeType xchg_type) const {
  799. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  800. return(xchg_stats->getOrphans());
  801. }
  802. /// \brief Return average unordered lookup set size.
  803. ///
  804. /// Method returns average unordered lookup set size.
  805. /// This value changes every time \ref ExchangeStats::findSent
  806. /// function performs unordered packet lookup.
  807. ///
  808. /// \param xchg_type exchange type.
  809. /// \throw isc::BadValue if invalid exchange type specified.
  810. /// \return average unordered lookup set size.
  811. double getAvgUnorderedLookupSetSize(const ExchangeType xchg_type) const {
  812. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  813. return(xchg_stats->getAvgUnorderedLookupSetSize());
  814. }
  815. /// \brief Return number of unordered sent packets lookups
  816. ///
  817. /// Method returns number of unordered sent packet lookups.
  818. /// Unordered lookup is used when received packet was sent
  819. /// out of order by server - transaction id of received
  820. /// packet does not match transaction id of next sent packet.
  821. ///
  822. /// \param xchg_type exchange type.
  823. /// \throw isc::BadValue if invalid exchange type specified.
  824. /// \return number of unordered lookups.
  825. uint64_t getUnorderedLookups(const ExchangeType xchg_type) const {
  826. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  827. return(xchg_stats->getUnorderedLookups());
  828. }
  829. /// \brief Return number of ordered sent packets lookups
  830. ///
  831. /// Method returns number of ordered sent packet lookups.
  832. /// Ordered lookup is used when packets are received in the
  833. /// same order as they were sent to the server.
  834. /// If packets are skipped or received out of order, lookup
  835. /// function will use unordered lookup (with hash table).
  836. ///
  837. /// \param xchg_type exchange type.
  838. /// \throw isc::BadValue if invalid exchange type specified.
  839. /// \return number of ordered lookups.
  840. uint64_t getOrderedLookups(const ExchangeType xchg_type) const {
  841. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  842. return(xchg_stats->getOrderedLookups());
  843. }
  844. /// \brief Return total number of sent packets
  845. ///
  846. /// Method returns total number of sent packets for specified
  847. /// exchange type.
  848. ///
  849. /// \param xchg_type exchange type.
  850. /// \throw isc::BadValue if invalid exchange type specified.
  851. /// \return number of sent packets.
  852. uint64_t getSentPacketsNum(const ExchangeType xchg_type) const {
  853. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  854. return(xchg_stats->getSentPacketsNum());
  855. }
  856. /// \brief Return total number of received packets
  857. ///
  858. /// Method returns total number of received packets for specified
  859. /// exchange type.
  860. ///
  861. /// \param xchg_type exchange type.
  862. /// \throw isc::BadValue if invalid exchange type specified.
  863. /// \return number of received packets.
  864. uint64_t getRcvdPacketsNum(const ExchangeType xchg_type) const {
  865. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  866. return(xchg_stats->getRcvdPacketsNum());
  867. }
  868. /// \brief Return name of the exchange.
  869. ///
  870. /// Method returns name of the specified exchange type.
  871. /// This function is mainly for logging purposes.
  872. ///
  873. /// \param xchg_type exchange type.
  874. /// \return string representing name of the exchange.
  875. std::string exchangeToString(ExchangeType xchg_type) const {
  876. switch(xchg_type) {
  877. case XCHG_DO:
  878. return("DISCOVER-OFFER");
  879. case XCHG_RA:
  880. return("REQUEST-ACK");
  881. case XCHG_SA:
  882. return("SOLICIT-ADVERTISE");
  883. case XCHG_RR:
  884. return("REQUEST-REPLY");
  885. default:
  886. return("Unknown exchange type");
  887. }
  888. }
  889. /// \brief Print statistics counters for all exchange types.
  890. ///
  891. /// Method prints statistics for all exchange types.
  892. /// Statistics includes:
  893. /// - number of sent and received packets
  894. /// - number of dropped packets and number of orphans
  895. /// - minimum packets delay,
  896. /// - average packets delay,
  897. /// - maximum packets delay,
  898. /// - standard deviation of packets delay.
  899. void printStats() const {
  900. for (ExchangesMapIterator it = exchanges_.begin();
  901. it != exchanges_.end();
  902. ++it) {
  903. ExchangeStatsPtr xchg_stats = it->second;
  904. std::cout << "***Statistics for: " << exchangeToString(it->first)
  905. << "***" << std::endl;
  906. xchg_stats->printMainStats();
  907. std::cout << std::endl;
  908. xchg_stats->printRTTStats();
  909. std::cout << std::endl;
  910. }
  911. }
  912. /// \brief Print timestamps of all packets.
  913. ///
  914. /// Method prints timestamps of all sent and received
  915. /// packets for all defined exchange types.
  916. ///
  917. /// \throw isc::InvalidOperation if one of the packets has
  918. /// no timestamp value set.
  919. void printTimestamps() const {
  920. for (ExchangesMapIterator it = exchanges_.begin();
  921. it != exchanges_.end();
  922. ++it) {
  923. ExchangeStatsPtr xchg_stats = it->second;
  924. std::cout << "***Timestamps for packets: "
  925. << exchangeToString(it->first)
  926. << "***" << std::endl;
  927. xchg_stats->printTimestamps();
  928. std::cout << std::endl;
  929. }
  930. }
  931. /// \brief Print names and values of custom counters.
  932. ///
  933. /// Method prints names and values of custom counters. Custom counters
  934. /// are defined by client class for tracking different statistics.
  935. void printCustomCounters() const {
  936. if (custom_counters_.size() > 0) {
  937. std::cout << "***Various statistics counters***" << std::endl;
  938. }
  939. for (CustomCountersMapIterator it = custom_counters_.begin();
  940. it != custom_counters_.end();
  941. ++it) {
  942. CustomCounterPtr counter = it->second;
  943. std::cout << counter->getName() << ": " << counter->getValue() << std::endl;
  944. }
  945. }
  946. private:
  947. /// \brief Return exchange stats object for given exchange type
  948. ///
  949. /// Method returns exchange stats object for given exchange type.
  950. ///
  951. /// \param xchg_type exchange type.
  952. /// \throw isc::BadValue if invalid exchange type specified.
  953. /// \return exchange stats object.
  954. ExchangeStatsPtr getExchangeStats(const ExchangeType xchg_type) const {
  955. ExchangesMapIterator it = exchanges_.find(xchg_type);
  956. if (it == exchanges_.end()) {
  957. isc_throw(BadValue, "Packets exchange not specified");
  958. }
  959. ExchangeStatsPtr xchg_stats = it->second;
  960. return(xchg_stats);
  961. }
  962. ExchangesMap exchanges_; ///< Map of exchange types.
  963. CustomCountersMap custom_counters_; ///< Map with custom counters.
  964. };
  965. } // namespace perfdhcp
  966. } // namespace isc
  967. #endif // __STATS_MGR_H