stats_mgr.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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 packet transaction id.
  136. typedef boost::multi_index_container<
  137. boost::shared_ptr<const T>,
  138. boost::multi_index::indexed_by<
  139. boost::multi_index::sequenced<>,
  140. boost::multi_index::hashed_non_unique<
  141. boost::multi_index::global_fun<
  142. const boost::shared_ptr<const T>&,
  143. uint32_t,
  144. &ExchangeStats::hashTransid
  145. >
  146. >,
  147. boost::multi_index::hashed_non_unique<
  148. boost::multi_index::const_mem_fun<
  149. T,
  150. uint32_t,
  151. &T::getTransid
  152. >
  153. >
  154. >
  155. > PktList;
  156. /// Packet list iterator for sequencial access to elements.
  157. typedef typename PktList::const_iterator PktListIterator;
  158. /// Packet list index to search packets using transaction id hash.
  159. typedef typename PktList::template nth_index<1>::type
  160. PktListTransidHashIndex;
  161. /// Packet list iterator to access packets using transaction id hash.
  162. typedef typename PktListTransidHashIndex::const_iterator
  163. PktListTransidHashIterator;
  164. /// Packet list index to search packets using transaction id.
  165. typedef typename PktList::template nth_index<2>::type
  166. PktListTransidIndex;
  167. /// Packet list iterator to access packets using transaction id.
  168. typedef typename PktListTransidIndex::const_iterator
  169. PktListTransidIterator;
  170. /// \brief Constructor
  171. ///
  172. /// \param xchg_type exchange type
  173. ExchangeStats(const ExchangeType xchg_type)
  174. : xchg_type_(xchg_type),
  175. min_delay_(std::numeric_limits<double>::max()),
  176. max_delay_(0.),
  177. sum_delay_(0.),
  178. orphans_(0),
  179. sum_delay_squared_(0.),
  180. ordered_lookups_(0),
  181. unordered_lookup_size_sum_(0),
  182. unordered_lookups_(0),
  183. sent_packets_num_(0),
  184. rcvd_packets_num_(0) {
  185. sent_packets_cache_ = sent_packets_.begin();
  186. }
  187. /// \brief Add new packet to list of sent packets.
  188. ///
  189. /// Method adds new packet to list of sent packets.
  190. ///
  191. /// \param packet packet object to be added.
  192. /// \throw isc::BadValue if packet is null.
  193. void appendSent(const boost::shared_ptr<const T>& packet) {
  194. if (!packet) {
  195. isc_throw(BadValue, "Packet is null");
  196. }
  197. ++sent_packets_num_;
  198. sent_packets_.template get<0>().push_back(packet);
  199. }
  200. /// \brief Add new packet to list of received packets.
  201. ///
  202. /// Method adds new packet to list of received packets.
  203. ///
  204. /// \param packet packet object to be added.
  205. /// \throw isc::BadValue if packet is null.
  206. void appendRcvd(const boost::shared_ptr<const T>& packet) {
  207. if (!packet) {
  208. isc_throw(BadValue, "Packet is null");
  209. }
  210. ++rcvd_packets_num_;
  211. rcvd_packets_.push_back(packet);
  212. }
  213. /// \brief Update delay counters.
  214. ///
  215. /// Method updates delay counters based on timestamps of
  216. /// sent and received packets.
  217. ///
  218. /// \param sent_packet sent packet
  219. /// \param rcvd_packet received packet
  220. /// \throw isc::BadValue if sent or received packet is null.
  221. /// \throw isc::Unexpected if failed to calculate timestamps
  222. void updateDelays(const boost::shared_ptr<const T>& sent_packet,
  223. const boost::shared_ptr<const T>& rcvd_packet) {
  224. if (!sent_packet) {
  225. isc_throw(BadValue, "Sent packet is null");
  226. }
  227. if (!rcvd_packet) {
  228. isc_throw(BadValue, "Received packet is null");
  229. }
  230. boost::posix_time::ptime sent_time = sent_packet->getTimestamp();
  231. boost::posix_time::ptime rcvd_time = rcvd_packet->getTimestamp();
  232. if (sent_time.is_not_a_date_time() ||
  233. rcvd_time.is_not_a_date_time()) {
  234. isc_throw(Unexpected,
  235. "Timestamp must be set for sent and "
  236. "received packet to measure RTT");
  237. }
  238. boost::posix_time::time_period period(sent_time, rcvd_time);
  239. // We don't bother calculating deltas in nanoseconds. It is much
  240. // more convenient to use seconds instead because we are going to
  241. // sum them up.
  242. double delta =
  243. static_cast<double>(period.length().total_nanoseconds()) / 1e9;
  244. if (delta < 0) {
  245. isc_throw(Unexpected, "Sent packet's timestamp must not be "
  246. "greater than received packet's timestamp");
  247. }
  248. // Record the minimum delay between sent and received packets.
  249. if (delta < min_delay_) {
  250. min_delay_ = delta;
  251. }
  252. // Record the maximum delay between sent and received packets.
  253. if (delta > max_delay_) {
  254. max_delay_ = delta;
  255. }
  256. // Update delay sum and square sum. That will be used to calculate
  257. // mean delays.
  258. sum_delay_ += delta;
  259. sum_delay_squared_ += delta * delta;
  260. }
  261. /// \brief Find packet on the list of sent packets.
  262. ///
  263. /// Method finds packet with specified transaction id on the list
  264. /// of sent packets. It is used to match received packet with
  265. /// corresponding sent packet.
  266. /// Since packets from the server most often come in the same order
  267. /// as they were sent by client, this method will first check if
  268. /// next sent packet matches. If it doesn't, function will search
  269. /// the packet using indexing by transaction id. This reduces
  270. /// packet search time significantly.
  271. ///
  272. /// \param rcvd_packet received packet to be matched with sent packet.
  273. /// \throw isc::BadValue if received packet is null.
  274. /// \return packet having specified transaction or NULL if packet
  275. /// not found
  276. boost::shared_ptr<const T> findSent(const boost::shared_ptr<const T>& rcvd_packet) {
  277. if (!rcvd_packet) {
  278. isc_throw(BadValue, "Received packet is null");
  279. }
  280. if (sent_packets_.size() == 0) {
  281. // List of sent packets is empty so there is no sense
  282. // to continue looking fo the packet. It also means
  283. // that the received packet we got has no corresponding
  284. // sent packet so orphans counter has to be updated.
  285. ++orphans_;
  286. return(boost::shared_ptr<const T>());
  287. } else if (sent_packets_cache_ == sent_packets_.end()) {
  288. // Even if there are still many unmatched packets on the
  289. // list we might hit the end of it because of unordered
  290. // lookups. The next logical step is to reset cache.
  291. sent_packets_cache_ = sent_packets_.begin();
  292. }
  293. // With this variable we will be signalling success or failure
  294. // to find the packet.
  295. bool packet_found = false;
  296. // Most likely responses are sent from the server in the same
  297. // order as client's requests to the server. We are caching
  298. // next sent packet and first try to match it with the next
  299. // incoming packet. We are successful if there is no
  300. // packet drop or out of order packets sent. This is actually
  301. // the fastest way to look for packets.
  302. if ((*sent_packets_cache_)->getTransid() == rcvd_packet->getTransid()) {
  303. ++ordered_lookups_;
  304. packet_found = true;
  305. } else {
  306. // If we are here, it means that we were unable to match the
  307. // next incoming packet with next sent packet so we need to
  308. // take a little more expensive approach to look packets using
  309. // alternative index (transaction id & 1023).
  310. PktListTransidHashIndex& idx = sent_packets_.template get<1>();
  311. // Packets are grouped using trasaction id masked with value
  312. // of 1023. For instance, packets with transaction id equal to
  313. // 1, 1024 ... will belong to the same group (a.k.a. bucket).
  314. // When using alternative index we don't find the packet but
  315. // bucket of packets and we need to iterate through the bucket
  316. // to find the one that has desired transaction id.
  317. std::pair<PktListTransidHashIterator,PktListTransidHashIterator> p =
  318. idx.equal_range(hashTransid(rcvd_packet));
  319. // We want to keep statistics of unordered lookups to make
  320. // sure that there is a right balance between number of
  321. // unordered lookups and ordered lookups. If number of unordered
  322. // lookups is high it may mean that many packets are lost or
  323. // sent out of order.
  324. ++unordered_lookups_;
  325. // We also want to keep the mean value of the bucket. The lower
  326. // bucket size the better. If bucket sizes appear to big we
  327. // might want to increase number of buckets.
  328. unordered_lookup_size_sum_ += std::distance(p.first, p.second);
  329. for (PktListTransidHashIterator it = p.first; it != p.second;
  330. ++it) {
  331. if ((*it)->getTransid() == rcvd_packet->getTransid()) {
  332. packet_found = true;
  333. sent_packets_cache_ =
  334. sent_packets_.template project<0>(it);
  335. break;
  336. }
  337. }
  338. }
  339. if (!packet_found) {
  340. // If we are here, it means that both ordered lookup and
  341. // unordered lookup failed. Searched packet is not on the list.
  342. ++orphans_;
  343. return(boost::shared_ptr<const T>());
  344. }
  345. boost::shared_ptr<const T> sent_packet(*sent_packets_cache_);
  346. // If packet was found, we assume it will be never searched
  347. // again. We want to delete this packet from the list to
  348. // improve performance of future searches.
  349. sent_packets_cache_ = eraseSent(sent_packets_cache_);
  350. return(sent_packet);
  351. }
  352. /// \brief Return minumum delay between sent and received packet.
  353. ///
  354. /// Method returns minimum delay between sent and received packet.
  355. ///
  356. /// \return minimum delay between packets.
  357. double getMinDelay() const { return(min_delay_); }
  358. /// \brief Return maxmimum delay between sent and received packet.
  359. ///
  360. /// Method returns maximum delay between sent and received packet.
  361. ///
  362. /// \return maximum delay between packets.
  363. double getMaxDelay() const { return(max_delay_); }
  364. /// \brief Return avarage packet delay.
  365. ///
  366. /// Method returns average packet delay. If no packets have been
  367. /// received for this exchange avg delay can't be calculated and
  368. /// thus method throws exception.
  369. ///
  370. /// \throw isc::InvalidOperation if no packets for this exchange
  371. /// have been received yet.
  372. /// \return average packet delay.
  373. double getAvgDelay() const {
  374. if (sum_delay_ == 0) {
  375. isc_throw(InvalidOperation, "no packets received");
  376. }
  377. return(sum_delay_ / rcvd_packets_num_);
  378. }
  379. /// \brief Return standard deviation of packet delay.
  380. ///
  381. /// Method returns standard deviation of packet delay. If no
  382. /// packets have been received for this exchange, the standard
  383. /// deviation can't be calculated and thus method throws
  384. /// exception.
  385. ///
  386. /// \throw isc::InvalidOperation if number of received packets
  387. /// for the exchange is equal to zero.
  388. /// \return standard deviation of packet delay.
  389. double getStdDevDelay() const {
  390. if (rcvd_packets_num_ == 0) {
  391. isc_throw(InvalidOperation, "no packets received");
  392. }
  393. return(sqrt(sum_delay_squared_ / rcvd_packets_num_ -
  394. getAvgDelay() * getAvgDelay()));
  395. }
  396. /// \brief Return number of orphant packets.
  397. ///
  398. /// Method returns number of received packets that had no matching
  399. /// sent packet. It is possible that such packet was late or not
  400. /// for us.
  401. ///
  402. /// \return number of orphant received packets.
  403. uint64_t getOrphans() const { return(orphans_); }
  404. /// \brief Return average unordered lookup set size.
  405. ///
  406. /// Method returns average unordered lookup set size.
  407. /// This value changes every time \ref ExchangeStats::findSent
  408. /// function performs unordered packet lookup.
  409. ///
  410. /// \throw isc::InvalidOperation if there have been no unordered
  411. /// lookups yet.
  412. /// \return average unordered lookup set size.
  413. double getAvgUnorderedLookupSetSize() const {
  414. if (unordered_lookups_ == 0) {
  415. isc_throw(InvalidOperation, "no unordered lookups");
  416. }
  417. return(static_cast<double>(unordered_lookup_size_sum_) /
  418. static_cast<double>(unordered_lookups_));
  419. }
  420. /// \brief Return number of unordered sent packets lookups
  421. ///
  422. /// Method returns number of unordered sent packet lookups.
  423. /// Unordered lookup is used when received packet was sent
  424. /// out of order by server - transaction id of received
  425. /// packet does not match transaction id of next sent packet.
  426. ///
  427. /// \return number of unordered lookups.
  428. uint64_t getUnorderedLookups() const { return(unordered_lookups_); }
  429. /// \brief Return number of ordered sent packets lookups
  430. ///
  431. /// Method returns number of ordered sent packet lookups.
  432. /// Ordered lookup is used when packets are received in the
  433. /// same order as they were sent to the server.
  434. /// If packets are skipped or received out of order, lookup
  435. /// function will use unordered lookup (with hash table).
  436. ///
  437. /// \return number of ordered lookups.
  438. uint64_t getOrderedLookups() const { return(ordered_lookups_); }
  439. /// \brief Return total number of sent packets
  440. ///
  441. /// Method returns total number of sent packets.
  442. ///
  443. /// \return number of sent packets.
  444. uint64_t getSentPacketsNum() const { return(sent_packets_num_); }
  445. /// \brief Return total number of received packets
  446. ///
  447. /// Method returns total number of received packets.
  448. ///
  449. /// \return number of received packets.
  450. uint64_t getRcvdPacketsNum() const { return(rcvd_packets_num_); }
  451. /// \brief Print main statistics for packet exchange.
  452. ///
  453. /// Method prints main statistics for particular exchange.
  454. /// Statistics includes: number of sent and received packets,
  455. /// number of dropped packets and number of orphans.
  456. void printMainStats() const {
  457. using namespace std;
  458. uint64_t drops = getRcvdPacketsNum() - getSentPacketsNum();
  459. cout << "sent packets: " << getSentPacketsNum() << endl
  460. << "received packets: " << getRcvdPacketsNum() << endl
  461. << "drops: " << drops << endl
  462. << "orphans: " << getOrphans() << endl;
  463. }
  464. /// \brief Print round trip time packets statistics.
  465. ///
  466. /// Method prints round trip time packets statistics. Statistics
  467. /// includes minimum packet delay, maximum packet delay, average
  468. /// packet delay and standard deviation of delays. Packet delay
  469. /// is a duration between sending a packet to server and receiving
  470. /// response from server.
  471. void printRTTStats() const {
  472. using namespace std;
  473. cout << fixed << setprecision(3)
  474. << "min delay: " << getMinDelay() * 1e3 << " ms" << endl
  475. << "avg delay: " << getAvgDelay() * 1e3 << " ms" << endl
  476. << "max delay: " << getMaxDelay() * 1e3 << " ms" << endl
  477. << "std deviation: " << getStdDevDelay() * 1e3 << " ms"
  478. << endl;
  479. }
  480. //// \brief Print timestamps for sent and received packets.
  481. ///
  482. /// Method prints timestamps for all sent and received packets for
  483. /// packet exchange.
  484. ///
  485. /// \throw isc::InvalidOperation if found packet with no timestamp set.
  486. void printTimestamps() {
  487. // We will be using boost::posix_time extensivelly here
  488. using namespace boost::posix_time;
  489. // Iterate through all received packets.
  490. for (PktListIterator it = rcvd_packets_.begin();
  491. it != rcvd_packets_.end();
  492. ++it) {
  493. boost::shared_ptr<const T> rcvd_packet = *it;
  494. // Search for corresponding sent packet using transaction id
  495. // of received packet.
  496. PktListTransidIndex& idx = archived_packets_.template get<2>();
  497. PktListTransidIterator it_archived =
  498. idx.find(rcvd_packet->getTransid());
  499. // This should not happen that there is no corresponding
  500. // sent packet. If it does however, we just drop the packet.
  501. if (it_archived != idx.end()) {
  502. boost::shared_ptr<const T> sent_packet = *it_archived;
  503. // Get sent and received packet times.
  504. ptime sent_time = sent_packet->getTimestamp();
  505. ptime rcvd_time = rcvd_packet->getTimestamp();
  506. // All sent and received packets should have timestamps
  507. // set but if there is a bug somewhere and packet does
  508. // not have timestamp we want to catch this here.
  509. if (sent_time.is_not_a_date_time() ||
  510. rcvd_time.is_not_a_date_time()) {
  511. isc_throw(InvalidOperation, "packet time is not set");
  512. }
  513. // Calculate durations of packets from beginning of epoch.
  514. ptime epoch_time(min_date_time);
  515. time_period sent_period(epoch_time, sent_time);
  516. time_period rcvd_period(epoch_time, rcvd_time);
  517. // Print timestamps for sent and received packet.
  518. std::cout << "sent / received: "
  519. << to_iso_string(sent_period.length())
  520. << " / " << to_iso_string(rcvd_period.length())
  521. << std::endl;
  522. }
  523. }
  524. }
  525. private:
  526. /// \brief Private default constructor.
  527. ///
  528. /// Default constructor is private because we want the client
  529. /// class to specify exchange type explicitely.
  530. ExchangeStats();
  531. /// \brief Erase packet from the list of sent packets.
  532. ///
  533. /// Method erases packet from the list of sent packets.
  534. ///
  535. /// \param it iterator pointing to packet to be erased.
  536. /// \return iterator pointing to packet following erased
  537. /// packet or sent_packets_.end() if packet not found.
  538. PktListIterator eraseSent(const PktListIterator it) {
  539. // We don't want to keep list of all sent packets
  540. // because it will affect packet lookup performance.
  541. // If packet is matched with received packet we
  542. // move it to list of archived packets. List of
  543. // archived packets may be used for diagnostics
  544. // when test is completed.
  545. archived_packets_.push_back(*it);
  546. return(sent_packets_.template get<0>().erase(it));
  547. }
  548. ExchangeType xchg_type_; ///< Packet exchange type.
  549. PktList sent_packets_; ///< List of sent packets.
  550. /// Iterator pointing to the packet on sent list which will most
  551. /// likely match next received packet. This is based on the
  552. /// assumption that server responds in order to incoming packets.
  553. PktListIterator sent_packets_cache_;
  554. PktList rcvd_packets_; ///< List of received packets.
  555. /// List of archived packets. All sent packets that have
  556. /// been matched with received packet are moved to this
  557. /// list for diagnostics purposes.
  558. PktList archived_packets_;
  559. double min_delay_; ///< Minimum delay between sent
  560. ///< and received packets.
  561. double max_delay_; ///< Maximum delay between sent
  562. ///< and received packets.
  563. double sum_delay_; ///< Sum of delays between sent
  564. ///< and received packets.
  565. double sum_delay_squared_; ///< Squared sum of delays between
  566. ///< sent and recived packets.
  567. uint64_t orphans_; ///< Number of orphant received packets.
  568. /// Sum of unordered lookup sets. Needed to calculate mean size of
  569. /// lookup set. It is desired that number of unordered lookups is
  570. /// minimal for performance reasons. Tracking number of lookups and
  571. /// mean size of the lookup set should give idea of packets serach
  572. /// complexity.
  573. uint64_t unordered_lookup_size_sum_;
  574. uint64_t unordered_lookups_; ///< Number of unordered sent packets
  575. ///< lookups.
  576. uint64_t ordered_lookups_; ///< Number of ordered sent packets
  577. ///< lookups.
  578. uint64_t sent_packets_num_; ///< Total number of sent packets.
  579. uint64_t rcvd_packets_num_; ///< Total number of received packets.
  580. };
  581. /// Pointer to ExchangeStats.
  582. typedef boost::shared_ptr<ExchangeStats> ExchangeStatsPtr;
  583. /// Map containing all specified exchange types.
  584. typedef typename std::map<ExchangeType, ExchangeStatsPtr> ExchangesMap;
  585. /// Iterator poiting to \ref ExchangesMap
  586. typedef typename ExchangesMap::const_iterator ExchangesMapIterator;
  587. /// Map containing custom counters.
  588. typedef typename std::map<std::string, CustomCounterPtr> CustomCountersMap;
  589. /// Iterator for \ref CustomCountersMap.
  590. typedef typename CustomCountersMap::const_iterator CustomCountersMapIterator;
  591. /// \brief Constructor.
  592. StatsMgr()
  593. : exchanges_(),
  594. custom_counters_() {
  595. }
  596. /// \brief Specify new exchange type.
  597. ///
  598. /// This method creates new \ref ExchangeStats object that will
  599. /// collect statistics data from packets exchange of the specified
  600. /// type.
  601. ///
  602. /// \param xchg_type exchange type.
  603. /// \throw isc::BadValue if exchange of specified type exists.
  604. void addExchangeStats(const ExchangeType xchg_type) {
  605. if (exchanges_.find(xchg_type) != exchanges_.end()) {
  606. isc_throw(BadValue, "Exchange of specified type already added.");
  607. }
  608. exchanges_[xchg_type] = ExchangeStatsPtr(new ExchangeStats(xchg_type));
  609. }
  610. /// \brief Add named custom uint64 counter.
  611. ///
  612. /// Method creates new named counter and stores in counter's map under
  613. /// key specified here as short_name.
  614. ///
  615. /// \param short_name key to use to access counter in the map.
  616. /// \param long_name name of the counter presented in the log file.
  617. void addCustomCounter(const std::string& short_name,
  618. const std::string& long_name) {
  619. if (custom_counters_.find(short_name) != custom_counters_.end()) {
  620. isc_throw(BadValue,
  621. "Custom counter " << short_name << " already added.");
  622. }
  623. custom_counters_[short_name] =
  624. CustomCounterPtr(new CustomCounter(long_name));
  625. }
  626. /// \brief Return specified counter.
  627. ///
  628. /// Method returns specified counter.
  629. ///
  630. /// \param counter_key key poiting to the counter in the counters map.
  631. /// \return pointer to specified counter object.
  632. CustomCounterPtr getCounter(const std::string& counter_key) {
  633. CustomCountersMapIterator it = custom_counters_.find(counter_key);
  634. if (it == custom_counters_.end()) {
  635. isc_throw(BadValue,
  636. "Custom counter " << counter_key << "does not exist");
  637. }
  638. return(it->second);
  639. }
  640. /// \brief Increment specified counter.
  641. ///
  642. /// Increement counter value by one.
  643. ///
  644. /// \param counter_key key poitinh to the counter in the counters map.
  645. /// \return pointer to specified counter after incrementation.
  646. const CustomCounter& IncrementCounter(const std::string& counter_key) {
  647. CustomCounterPtr counter = getCounter(counter_key);
  648. return(++(*counter));
  649. }
  650. /// \brief Adds new packet to the sent packets list.
  651. ///
  652. /// Method adds new packet to the sent packets list.
  653. /// Packets are added to the list sequentially and
  654. /// most often read sequentially.
  655. ///
  656. /// \param xchg_type exchange type.
  657. /// \param packet packet to be added to the list
  658. /// \throw isc::BadValue if invalid exchange type specified or
  659. /// packet is null.
  660. void passSentPacket(const ExchangeType xchg_type,
  661. const boost::shared_ptr<const T>& packet) {
  662. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  663. xchg_stats->appendSent(packet);
  664. }
  665. /// \brief Add new received packet and match with sent packet.
  666. ///
  667. /// Method adds new packet to the list of received packets. It
  668. /// also searches for corresponding packet on the list of sent
  669. /// packets. When packets are matched the statistics counters
  670. /// are updated accordingly for the particular exchange type.
  671. ///
  672. /// \param xchg_type exchange type.
  673. /// \param packet received packet
  674. /// \throw isc::BadValue if invalid exchange type specified
  675. /// or packet is null.
  676. /// \throw isc::Unexpected if corresponding packet was not
  677. /// found on the list of sent packets.
  678. void passRcvdPacket(const ExchangeType xchg_type,
  679. const boost::shared_ptr<const T>& packet) {
  680. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  681. boost::shared_ptr<const T> sent_packet
  682. = xchg_stats->findSent(packet);
  683. if (sent_packet) {
  684. xchg_stats->updateDelays(sent_packet, packet);
  685. xchg_stats->appendRcvd(packet);
  686. }
  687. }
  688. /// \brief Return minumum delay between sent and received packet.
  689. ///
  690. /// Method returns minimum delay between sent and received packet
  691. /// for specified exchange type.
  692. ///
  693. /// \param xchg_type exchange type.
  694. /// \throw isc::BadValue if invalid exchange type specified.
  695. /// \return minimum delay between packets.
  696. double getMinDelay(const ExchangeType xchg_type) const {
  697. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  698. return(xchg_stats->getMinDelay());
  699. }
  700. /// \brief Return maxmimum delay between sent and received packet.
  701. ///
  702. /// Method returns maximum delay between sent and received packet
  703. /// for specified exchange type.
  704. ///
  705. /// \param xchg_type exchange type.
  706. /// \throw isc::BadValue if invalid exchange type specified.
  707. /// \return maximum delay between packets.
  708. double getMaxDelay(const ExchangeType xchg_type) const {
  709. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  710. return(xchg_stats->getMaxDelay());
  711. }
  712. /// \brief Return avarage packet delay.
  713. ///
  714. /// Method returns average packet delay for specified
  715. /// exchange type.
  716. ///
  717. /// \return average packet delay.
  718. double getAvgDelay(const ExchangeType xchg_type) const {
  719. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  720. return(xchg_stats->getAvgDelay());
  721. }
  722. /// \brief Return standard deviation of packet delay.
  723. ///
  724. /// Method returns standard deviation of packet delay
  725. /// for specified exchange type.
  726. ///
  727. /// \return standard deviation of packet delay.
  728. double getStdDevDelay(const ExchangeType xchg_type) const {
  729. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  730. return(xchg_stats->getStdDevDelay());
  731. }
  732. /// \brief Return number of orphant packets.
  733. ///
  734. /// Method returns number of orphant packets for specified
  735. /// exchange type.
  736. ///
  737. /// \param xchg_type exchange type.
  738. /// \throw isc::BadValue if invalid exchange type specified.
  739. /// \return number of orphant packets so far.
  740. uint64_t getOrphans(const ExchangeType xchg_type) const {
  741. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  742. return(xchg_stats->getOrphans());
  743. }
  744. /// \brief Return average unordered lookup set size.
  745. ///
  746. /// Method returns average unordered lookup set size.
  747. /// This value changes every time \ref ExchangeStats::findSent
  748. /// function performs unordered packet lookup.
  749. ///
  750. /// \param xchg_type exchange type.
  751. /// \throw isc::BadValue if invalid exchange type specified.
  752. /// \return average unordered lookup set size.
  753. double getAvgUnorderedLookupSetSize(const ExchangeType xchg_type) const {
  754. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  755. return(xchg_stats->getAvgUnorderedLookupSetSize());
  756. }
  757. /// \brief Return number of unordered sent packets lookups
  758. ///
  759. /// Method returns number of unordered sent packet lookups.
  760. /// Unordered lookup is used when received packet was sent
  761. /// out of order by server - transaction id of received
  762. /// packet does not match transaction id of next sent packet.
  763. ///
  764. /// \param xchg_type exchange type.
  765. /// \throw isc::BadValue if invalid exchange type specified.
  766. /// \return number of unordered lookups.
  767. uint64_t getUnorderedLookups(const ExchangeType xchg_type) const {
  768. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  769. return(xchg_stats->getUnorderedLookups());
  770. }
  771. /// \brief Return number of ordered sent packets lookups
  772. ///
  773. /// Method returns number of ordered sent packet lookups.
  774. /// Ordered lookup is used when packets are received in the
  775. /// same order as they were sent to the server.
  776. /// If packets are skipped or received out of order, lookup
  777. /// function will use unordered lookup (with hash table).
  778. ///
  779. /// \param xchg_type exchange type.
  780. /// \throw isc::BadValue if invalid exchange type specified.
  781. /// \return number of ordered lookups.
  782. uint64_t getOrderedLookups(const ExchangeType xchg_type) const {
  783. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  784. return(xchg_stats->getOrderedLookups());
  785. }
  786. /// \brief Return total number of sent packets
  787. ///
  788. /// Method returns total number of sent packets for specified
  789. /// exchange type.
  790. ///
  791. /// \param xchg_type exchange type.
  792. /// \throw isc::BadValue if invalid exchange type specified.
  793. /// \return number of sent packets.
  794. uint64_t getSentPacketsNum(const ExchangeType xchg_type) const {
  795. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  796. return(xchg_stats->getSentPacketsNum());
  797. }
  798. /// \brief Return total number of received packets
  799. ///
  800. /// Method returns total number of received packets for specified
  801. /// exchange type.
  802. ///
  803. /// \param xchg_type exchange type.
  804. /// \throw isc::BadValue if invalid exchange type specified.
  805. /// \return number of received packets.
  806. uint64_t getRcvdPacketsNum(const ExchangeType xchg_type) const {
  807. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  808. return(xchg_stats->getRcvdPacketsNum());
  809. }
  810. /// \brief Return name of the exchange.
  811. ///
  812. /// Method returns name of the specified exchange type.
  813. /// This function is mainly for logging purposes.
  814. ///
  815. /// \param xchg_type exchange type.
  816. /// \return string representing name of the exchange.
  817. std::string exchangeToString(ExchangeType xchg_type) const {
  818. switch(xchg_type) {
  819. case XCHG_DO:
  820. return("DISCOVER-OFFER");
  821. case XCHG_RA:
  822. return("REQUEST-ACK");
  823. case XCHG_SA:
  824. return("SOLICIT-ADVERTISE");
  825. case XCHG_RR:
  826. return("REQUEST-REPLY");
  827. default:
  828. return("Unknown exchange type");
  829. }
  830. }
  831. /// \brief Print statistics counters for all exchange types.
  832. ///
  833. /// Method prints statistics for all exchange types.
  834. /// Statistics includes:
  835. /// - number of sent and received packets
  836. /// - number of dropped packets and number of orphans
  837. /// - minimum packets delay,
  838. /// - average packets delay,
  839. /// - maximum packets delay,
  840. /// - standard deviation of packets delay.
  841. void printStats() const {
  842. for (ExchangesMapIterator it = exchanges_.begin();
  843. it != exchanges_.end();
  844. ++it) {
  845. ExchangeStatsPtr xchg_stats = it->second;
  846. std::cout << "***Statistics for: " << exchangeToString(it->first)
  847. << "***" << std::endl;
  848. xchg_stats->printMainStats();
  849. std::cout << std::endl;
  850. xchg_stats->printRTTStats();
  851. std::cout << std::endl;
  852. }
  853. }
  854. /// \brief Print timestamps of all packets.
  855. ///
  856. /// Method prints timestamps of all sent and received
  857. /// packets for all defined exchange types.
  858. ///
  859. /// \throw isc::InvalidOperation if one of the packets has
  860. /// no timestamp value set.
  861. void printTimestamps() const {
  862. for (ExchangesMapIterator it = exchanges_.begin();
  863. it != exchanges_.end();
  864. ++it) {
  865. ExchangeStatsPtr xchg_stats = it->second;
  866. std::cout << "***Timestamps for packets: "
  867. << exchangeToString(it->first)
  868. << "***" << std::endl;
  869. xchg_stats->printTimestamps();
  870. std::cout << std::endl;
  871. }
  872. }
  873. /// \brief Print names and values of custom counters.
  874. ///
  875. /// Method prints names and values of custom counters. Custom counters
  876. /// are defined by client class for tracking different statistics.
  877. void printCustomCounters() const {
  878. if (custom_counters_.size() > 0) {
  879. std::cout << "***Various statistics counters***" << std::endl;
  880. }
  881. for (CustomCountersMapIterator it = custom_counters_.begin();
  882. it != custom_counters_.end();
  883. ++it) {
  884. CustomCounterPtr counter = it->second;
  885. std::cout << counter->getName() << ": " << counter->getValue() << std::endl;
  886. }
  887. }
  888. private:
  889. /// \brief Return exchange stats object for given exchange type
  890. ///
  891. /// Method returns exchange stats object for given exchange type.
  892. ///
  893. /// \param xchg_type exchange type.
  894. /// \throw isc::BadValue if invalid exchange type specified.
  895. /// \return exchange stats object.
  896. ExchangeStatsPtr getExchangeStats(const ExchangeType xchg_type) const {
  897. ExchangesMapIterator it = exchanges_.find(xchg_type);
  898. if (it == exchanges_.end()) {
  899. isc_throw(BadValue, "Packets exchange not specified");
  900. }
  901. ExchangeStatsPtr xchg_stats = it->second;
  902. return(xchg_stats);
  903. }
  904. ExchangesMap exchanges_; ///< Map of exchange types.
  905. CustomCountersMap custom_counters_; ///< Map with custom counters.
  906. };
  907. } // namespace perfdhcp
  908. } // namespace isc
  909. #endif // __STATS_MGR_H