stats_mgr.h 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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, const bool archive_enabled)
  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. sent_packets_(),
  236. rcvd_packets_(),
  237. archived_packets_(),
  238. archive_enabled_(archive_enabled) {
  239. next_sent_ = sent_packets_.begin();
  240. }
  241. /// \brief Add new packet to list of sent packets.
  242. ///
  243. /// Method adds new packet to list of sent packets.
  244. ///
  245. /// \param packet packet object to be added.
  246. /// \throw isc::BadValue if packet is null.
  247. void appendSent(const boost::shared_ptr<const T>& packet) {
  248. if (!packet) {
  249. isc_throw(BadValue, "Packet is null");
  250. }
  251. ++sent_packets_num_;
  252. sent_packets_.template get<0>().push_back(packet);
  253. }
  254. /// \brief Add new packet to list of received packets.
  255. ///
  256. /// Method adds new packet to list of received packets.
  257. ///
  258. /// \param packet packet object to be added.
  259. /// \throw isc::BadValue if packet is null.
  260. void appendRcvd(const boost::shared_ptr<const T>& packet) {
  261. if (!packet) {
  262. isc_throw(BadValue, "Packet is null");
  263. }
  264. rcvd_packets_.push_back(packet);
  265. }
  266. /// \brief Update delay counters.
  267. ///
  268. /// Method updates delay counters based on timestamps of
  269. /// sent and received packets.
  270. ///
  271. /// \param sent_packet sent packet
  272. /// \param rcvd_packet received packet
  273. /// \throw isc::BadValue if sent or received packet is null.
  274. /// \throw isc::Unexpected if failed to calculate timestamps
  275. void updateDelays(const boost::shared_ptr<const T>& sent_packet,
  276. const boost::shared_ptr<const T>& rcvd_packet) {
  277. if (!sent_packet) {
  278. isc_throw(BadValue, "Sent packet is null");
  279. }
  280. if (!rcvd_packet) {
  281. isc_throw(BadValue, "Received packet is null");
  282. }
  283. boost::posix_time::ptime sent_time = sent_packet->getTimestamp();
  284. boost::posix_time::ptime rcvd_time = rcvd_packet->getTimestamp();
  285. if (sent_time.is_not_a_date_time() ||
  286. rcvd_time.is_not_a_date_time()) {
  287. isc_throw(Unexpected,
  288. "Timestamp must be set for sent and "
  289. "received packet to measure RTT");
  290. }
  291. boost::posix_time::time_period period(sent_time, rcvd_time);
  292. // We don't bother calculating deltas in nanoseconds. It is much
  293. // more convenient to use seconds instead because we are going to
  294. // sum them up.
  295. double delta =
  296. static_cast<double>(period.length().total_nanoseconds()) / 1e9;
  297. if (delta < 0) {
  298. isc_throw(Unexpected, "Sent packet's timestamp must not be "
  299. "greater than received packet's timestamp");
  300. }
  301. // Record the minimum delay between sent and received packets.
  302. if (delta < min_delay_) {
  303. min_delay_ = delta;
  304. }
  305. // Record the maximum delay between sent and received packets.
  306. if (delta > max_delay_) {
  307. max_delay_ = delta;
  308. }
  309. // Update delay sum and square sum. That will be used to calculate
  310. // mean delays.
  311. sum_delay_ += delta;
  312. sum_delay_squared_ += delta * delta;
  313. }
  314. /// \brief Find packet on the list of sent packets.
  315. ///
  316. /// Method finds packet with specified transaction id on the list
  317. /// of sent packets. It is used to match received packet with
  318. /// corresponding sent packet.
  319. /// Since packets from the server most often come in the same order
  320. /// as they were sent by client, this method will first check if
  321. /// next sent packet matches. If it doesn't, function will search
  322. /// the packet using indexing by transaction id. This reduces
  323. /// packet search time significantly.
  324. ///
  325. /// \param rcvd_packet received packet to be matched with sent packet.
  326. /// \throw isc::BadValue if received packet is null.
  327. /// \return packet having specified transaction or NULL if packet
  328. /// not found
  329. boost::shared_ptr<const T> findSent(const boost::shared_ptr<const T>& rcvd_packet) {
  330. if (!rcvd_packet) {
  331. isc_throw(BadValue, "Received packet is null");
  332. }
  333. if (sent_packets_.size() == 0) {
  334. // List of sent packets is empty so there is no sense
  335. // to continue looking fo the packet. It also means
  336. // that the received packet we got has no corresponding
  337. // sent packet so orphans counter has to be updated.
  338. ++orphans_;
  339. return(boost::shared_ptr<const T>());
  340. } else if (next_sent_ == sent_packets_.end()) {
  341. // Even if there are still many unmatched packets on the
  342. // list we might hit the end of it because of unordered
  343. // lookups. The next logical step is to reset iterator.
  344. next_sent_ = sent_packets_.begin();
  345. }
  346. // With this variable we will be signalling success or failure
  347. // to find the packet.
  348. bool packet_found = false;
  349. // Most likely responses are sent from the server in the same
  350. // order as client's requests to the server. We are caching
  351. // next sent packet and first try to match it with the next
  352. // incoming packet. We are successful if there is no
  353. // packet drop or out of order packets sent. This is actually
  354. // the fastest way to look for packets.
  355. if ((*next_sent_)->getTransid() == rcvd_packet->getTransid()) {
  356. ++ordered_lookups_;
  357. packet_found = true;
  358. } else {
  359. // If we are here, it means that we were unable to match the
  360. // next incoming packet with next sent packet so we need to
  361. // take a little more expensive approach to look packets using
  362. // alternative index (transaction id & 1023).
  363. PktListTransidHashIndex& idx = sent_packets_.template get<1>();
  364. // Packets are grouped using trasaction id masked with value
  365. // of 1023. For instance, packets with transaction id equal to
  366. // 1, 1024 ... will belong to the same group (a.k.a. bucket).
  367. // When using alternative index we don't find the packet but
  368. // bucket of packets and we need to iterate through the bucket
  369. // to find the one that has desired transaction id.
  370. std::pair<PktListTransidHashIterator,PktListTransidHashIterator> p =
  371. idx.equal_range(hashTransid(rcvd_packet));
  372. // We want to keep statistics of unordered lookups to make
  373. // sure that there is a right balance between number of
  374. // unordered lookups and ordered lookups. If number of unordered
  375. // lookups is high it may mean that many packets are lost or
  376. // sent out of order.
  377. ++unordered_lookups_;
  378. // We also want to keep the mean value of the bucket. The lower
  379. // bucket size the better. If bucket sizes appear to big we
  380. // might want to increase number of buckets.
  381. unordered_lookup_size_sum_ += std::distance(p.first, p.second);
  382. for (PktListTransidHashIterator it = p.first; it != p.second;
  383. ++it) {
  384. if ((*it)->getTransid() == rcvd_packet->getTransid()) {
  385. packet_found = true;
  386. next_sent_ =
  387. sent_packets_.template project<0>(it);
  388. break;
  389. }
  390. }
  391. }
  392. if (!packet_found) {
  393. // If we are here, it means that both ordered lookup and
  394. // unordered lookup failed. Searched packet is not on the list.
  395. ++orphans_;
  396. return(boost::shared_ptr<const T>());
  397. }
  398. ++rcvd_packets_num_;
  399. boost::shared_ptr<const T> sent_packet(*next_sent_);
  400. // If packet was found, we assume it will be never searched
  401. // again. We want to delete this packet from the list to
  402. // improve performance of future searches.
  403. next_sent_ = eraseSent(next_sent_);
  404. return(sent_packet);
  405. }
  406. /// \brief Return minumum delay between sent and received packet.
  407. ///
  408. /// Method returns minimum delay between sent and received packet.
  409. ///
  410. /// \return minimum delay between packets.
  411. double getMinDelay() const { return(min_delay_); }
  412. /// \brief Return maxmimum delay between sent and received packet.
  413. ///
  414. /// Method returns maximum delay between sent and received packet.
  415. ///
  416. /// \return maximum delay between packets.
  417. double getMaxDelay() const { return(max_delay_); }
  418. /// \brief Return avarage packet delay.
  419. ///
  420. /// Method returns average packet delay. If no packets have been
  421. /// received for this exchange avg delay can't be calculated and
  422. /// thus method throws exception.
  423. ///
  424. /// \throw isc::InvalidOperation if no packets for this exchange
  425. /// have been received yet.
  426. /// \return average packet delay.
  427. double getAvgDelay() const {
  428. if (rcvd_packets_num_ == 0) {
  429. isc_throw(InvalidOperation, "no packets received");
  430. }
  431. return(sum_delay_ / rcvd_packets_num_);
  432. }
  433. /// \brief Return standard deviation of packet delay.
  434. ///
  435. /// Method returns standard deviation of packet delay. If no
  436. /// packets have been received for this exchange, the standard
  437. /// deviation can't be calculated and thus method throws
  438. /// exception.
  439. ///
  440. /// \throw isc::InvalidOperation if number of received packets
  441. /// for the exchange is equal to zero.
  442. /// \return standard deviation of packet delay.
  443. double getStdDevDelay() const {
  444. if (rcvd_packets_num_ == 0) {
  445. isc_throw(InvalidOperation, "no packets received");
  446. }
  447. return(sqrt(sum_delay_squared_ / rcvd_packets_num_ -
  448. getAvgDelay() * getAvgDelay()));
  449. }
  450. /// \brief Return number of orphant packets.
  451. ///
  452. /// Method returns number of received packets that had no matching
  453. /// sent packet. It is possible that such packet was late or not
  454. /// for us.
  455. ///
  456. /// \return number of orphant received packets.
  457. uint64_t getOrphans() const { return(orphans_); }
  458. /// \brief Return average unordered lookup set size.
  459. ///
  460. /// Method returns average unordered lookup set size.
  461. /// This value changes every time \ref ExchangeStats::findSent
  462. /// function performs unordered packet lookup.
  463. ///
  464. /// \throw isc::InvalidOperation if there have been no unordered
  465. /// lookups yet.
  466. /// \return average unordered lookup set size.
  467. double getAvgUnorderedLookupSetSize() const {
  468. if (unordered_lookups_ == 0) {
  469. isc_throw(InvalidOperation, "no unordered lookups");
  470. }
  471. return(static_cast<double>(unordered_lookup_size_sum_) /
  472. static_cast<double>(unordered_lookups_));
  473. }
  474. /// \brief Return number of unordered sent packets lookups
  475. ///
  476. /// Method returns number of unordered sent packet lookups.
  477. /// Unordered lookup is used when received packet was sent
  478. /// out of order by server - transaction id of received
  479. /// packet does not match transaction id of next sent packet.
  480. ///
  481. /// \return number of unordered lookups.
  482. uint64_t getUnorderedLookups() const { return(unordered_lookups_); }
  483. /// \brief Return number of ordered sent packets lookups
  484. ///
  485. /// Method returns number of ordered sent packet lookups.
  486. /// Ordered lookup is used when packets are received in the
  487. /// same order as they were sent to the server.
  488. /// If packets are skipped or received out of order, lookup
  489. /// function will use unordered lookup (with hash table).
  490. ///
  491. /// \return number of ordered lookups.
  492. uint64_t getOrderedLookups() const { return(ordered_lookups_); }
  493. /// \brief Return total number of sent packets
  494. ///
  495. /// Method returns total number of sent packets.
  496. ///
  497. /// \return number of sent packets.
  498. uint64_t getSentPacketsNum() const { return(sent_packets_num_); }
  499. /// \brief Return total number of received packets
  500. ///
  501. /// Method returns total number of received packets.
  502. ///
  503. /// \return number of received packets.
  504. uint64_t getRcvdPacketsNum() const { return(rcvd_packets_num_); }
  505. /// \brief Print main statistics for packet exchange.
  506. ///
  507. /// Method prints main statistics for particular exchange.
  508. /// Statistics includes: number of sent and received packets,
  509. /// number of dropped packets and number of orphans.
  510. void printMainStats() const {
  511. using namespace std;
  512. uint64_t drops = getRcvdPacketsNum() - getSentPacketsNum();
  513. cout << "sent packets: " << getSentPacketsNum() << endl
  514. << "received packets: " << getRcvdPacketsNum() << endl
  515. << "drops: " << drops << endl
  516. << "orphans: " << getOrphans() << endl;
  517. }
  518. /// \brief Print round trip time packets statistics.
  519. ///
  520. /// Method prints round trip time packets statistics. Statistics
  521. /// includes minimum packet delay, maximum packet delay, average
  522. /// packet delay and standard deviation of delays. Packet delay
  523. /// is a duration between sending a packet to server and receiving
  524. /// response from server.
  525. void printRTTStats() const {
  526. using namespace std;
  527. try {
  528. cout << fixed << setprecision(3)
  529. << "min delay: " << getMinDelay() * 1e3 << " ms" << endl
  530. << "avg delay: " << getAvgDelay() * 1e3 << " ms" << endl
  531. << "max delay: " << getMaxDelay() * 1e3 << " ms" << endl
  532. << "std deviation: " << getStdDevDelay() * 1e3 << " ms"
  533. << endl;
  534. } catch (const Exception& e) {
  535. cout << "Unavailable! No packets received." << endl;
  536. }
  537. }
  538. //// \brief Print timestamps for sent and received packets.
  539. ///
  540. /// Method prints timestamps for all sent and received packets for
  541. /// packet exchange. In order to run this method the packets
  542. /// archiving mode has to be enabled during object constructions.
  543. /// Otherwise sent packets are not stored during tests execution
  544. /// and this method has no ability to get and print their timestamps.
  545. ///
  546. /// \throw isc::InvalidOperation if found packet with no timestamp or
  547. /// if packets archive mode is disabled.
  548. void printTimestamps() {
  549. // If archive mode is disabled there is no sense to proceed
  550. // because we don't have packets and their timestamps.
  551. if (!archive_enabled_) {
  552. isc_throw(isc::InvalidOperation,
  553. "packets archive mode is disabled");
  554. }
  555. if (rcvd_packets_num_ == 0) {
  556. std::cout << "Unavailable! No packets received." << std::endl;
  557. }
  558. // We will be using boost::posix_time extensivelly here
  559. using namespace boost::posix_time;
  560. // Iterate through all received packets.
  561. for (PktListIterator it = rcvd_packets_.begin();
  562. it != rcvd_packets_.end();
  563. ++it) {
  564. boost::shared_ptr<const T> rcvd_packet = *it;
  565. PktListTransidHashIndex& idx =
  566. archived_packets_.template get<1>();
  567. std::pair<PktListTransidHashIterator,
  568. PktListTransidHashIterator> p =
  569. idx.equal_range(hashTransid(rcvd_packet));
  570. for (PktListTransidHashIterator it_archived = p.first;
  571. it_archived != p.second;
  572. ++it) {
  573. if ((*it_archived)->getTransid() ==
  574. rcvd_packet->getTransid()) {
  575. boost::shared_ptr<const T> sent_packet = *it_archived;
  576. // Get sent and received packet times.
  577. ptime sent_time = sent_packet->getTimestamp();
  578. ptime rcvd_time = rcvd_packet->getTimestamp();
  579. // All sent and received packets should have timestamps
  580. // set but if there is a bug somewhere and packet does
  581. // not have timestamp we want to catch this here.
  582. if (sent_time.is_not_a_date_time() ||
  583. rcvd_time.is_not_a_date_time()) {
  584. isc_throw(InvalidOperation,
  585. "packet time is not set");
  586. }
  587. // Calculate durations of packets from beginning of epoch.
  588. ptime epoch_time(min_date_time);
  589. time_period sent_period(epoch_time, sent_time);
  590. time_period rcvd_period(epoch_time, rcvd_time);
  591. // Print timestamps for sent and received packet.
  592. std::cout << "sent / received: "
  593. << to_iso_string(sent_period.length())
  594. << " / "
  595. << to_iso_string(rcvd_period.length())
  596. << std::endl;
  597. break;
  598. }
  599. }
  600. }
  601. }
  602. private:
  603. /// \brief Private default constructor.
  604. ///
  605. /// Default constructor is private because we want the client
  606. /// class to specify exchange type explicitely.
  607. ExchangeStats();
  608. /// \brief Erase packet from the list of sent packets.
  609. ///
  610. /// Method erases packet from the list of sent packets.
  611. ///
  612. /// \param it iterator pointing to packet to be erased.
  613. /// \return iterator pointing to packet following erased
  614. /// packet or sent_packets_.end() if packet not found.
  615. PktListIterator eraseSent(const PktListIterator it) {
  616. if (archive_enabled_) {
  617. // We don't want to keep list of all sent packets
  618. // because it will affect packet lookup performance.
  619. // If packet is matched with received packet we
  620. // move it to list of archived packets. List of
  621. // archived packets may be used for diagnostics
  622. // when test is completed.
  623. archived_packets_.push_back(*it);
  624. }
  625. // get<0>() template returns sequencial index to
  626. // container.
  627. return(sent_packets_.template get<0>().erase(it));
  628. }
  629. ExchangeType xchg_type_; ///< Packet exchange type.
  630. PktList sent_packets_; ///< List of sent packets.
  631. /// Iterator pointing to the packet on sent list which will most
  632. /// likely match next received packet. This is based on the
  633. /// assumption that server responds in order to incoming packets.
  634. PktListIterator next_sent_;
  635. PktList rcvd_packets_; ///< List of received packets.
  636. /// List of archived packets. All sent packets that have
  637. /// been matched with received packet are moved to this
  638. /// list for diagnostics purposes.
  639. PktList archived_packets_;
  640. /// Indicates all packets have to be preserved after matching.
  641. /// By default this is disabled which means that when received
  642. /// packet is matched with sent packet both are deleted. This
  643. /// is important when test is executed for extended period of
  644. /// time and high memory usage might be the issue.
  645. /// When timestamps listing is specified from the command line
  646. /// (using diagnostics selector), all packets have to be preserved
  647. /// so as the printing method may read their timestamps and
  648. /// print it to user. In such usage model it will be rare to
  649. /// run test for extended period of time so it should be fine
  650. /// to keep all packets archived throughout the test.
  651. bool archive_enabled_;
  652. double min_delay_; ///< Minimum delay between sent
  653. ///< and received packets.
  654. double max_delay_; ///< Maximum delay between sent
  655. ///< and received packets.
  656. double sum_delay_; ///< Sum of delays between sent
  657. ///< and received packets.
  658. double sum_delay_squared_; ///< Squared sum of delays between
  659. ///< sent and recived packets.
  660. uint64_t orphans_; ///< Number of orphant received packets.
  661. /// Sum of unordered lookup sets. Needed to calculate mean size of
  662. /// lookup set. It is desired that number of unordered lookups is
  663. /// minimal for performance reasons. Tracking number of lookups and
  664. /// mean size of the lookup set should give idea of packets serach
  665. /// complexity.
  666. uint64_t unordered_lookup_size_sum_;
  667. uint64_t unordered_lookups_; ///< Number of unordered sent packets
  668. ///< lookups.
  669. uint64_t ordered_lookups_; ///< Number of ordered sent packets
  670. ///< lookups.
  671. uint64_t sent_packets_num_; ///< Total number of sent packets.
  672. uint64_t rcvd_packets_num_; ///< Total number of received packets.
  673. };
  674. /// Pointer to ExchangeStats.
  675. typedef boost::shared_ptr<ExchangeStats> ExchangeStatsPtr;
  676. /// Map containing all specified exchange types.
  677. typedef typename std::map<ExchangeType, ExchangeStatsPtr> ExchangesMap;
  678. /// Iterator poiting to \ref ExchangesMap
  679. typedef typename ExchangesMap::const_iterator ExchangesMapIterator;
  680. /// Map containing custom counters.
  681. typedef typename std::map<std::string, CustomCounterPtr> CustomCountersMap;
  682. /// Iterator for \ref CustomCountersMap.
  683. typedef typename CustomCountersMap::const_iterator CustomCountersMapIterator;
  684. /// \brief Default constructor.
  685. ///
  686. /// This constructor by default disables packets archiving mode.
  687. /// In this mode all packets from the list of sent packets are
  688. /// moved to list of archived packets once they have been matched
  689. /// with received packets. This is required if it has been selected
  690. /// from the command line to print timestamps for all packets after
  691. /// the test. If this is not selected archiving should be disabled
  692. /// for performance reasons and to avoid waste of memory for storing
  693. /// large list of archived packets.
  694. StatsMgr() :
  695. exchanges_(),
  696. custom_counters_(),
  697. archive_enabled_(false) {
  698. }
  699. /// \brief Constructor.
  700. ///
  701. /// Use this constructor to set packets archive mode.
  702. ///
  703. /// \param archive_enabled true indicates that packets
  704. /// archive mode is enabled.
  705. StatsMgr(const bool archive_enabled) :
  706. exchanges_(),
  707. custom_counters_(),
  708. archive_enabled_(archive_enabled) {
  709. }
  710. /// \brief Specify new exchange type.
  711. ///
  712. /// This method creates new \ref ExchangeStats object that will
  713. /// collect statistics data from packets exchange of the specified
  714. /// type.
  715. ///
  716. /// \param xchg_type exchange type.
  717. /// \throw isc::BadValue if exchange of specified type exists.
  718. void addExchangeStats(const ExchangeType xchg_type) {
  719. if (exchanges_.find(xchg_type) != exchanges_.end()) {
  720. isc_throw(BadValue, "Exchange of specified type already added.");
  721. }
  722. exchanges_[xchg_type] =
  723. ExchangeStatsPtr(new ExchangeStats(xchg_type, archive_enabled_));
  724. }
  725. /// \brief Add named custom uint64 counter.
  726. ///
  727. /// Method creates new named counter and stores in counter's map under
  728. /// key specified here as short_name.
  729. ///
  730. /// \param short_name key to use to access counter in the map.
  731. /// \param long_name name of the counter presented in the log file.
  732. void addCustomCounter(const std::string& short_name,
  733. const std::string& long_name) {
  734. if (custom_counters_.find(short_name) != custom_counters_.end()) {
  735. isc_throw(BadValue,
  736. "Custom counter " << short_name << " already added.");
  737. }
  738. custom_counters_[short_name] =
  739. CustomCounterPtr(new CustomCounter(long_name));
  740. }
  741. /// \brief Return specified counter.
  742. ///
  743. /// Method returns specified counter.
  744. ///
  745. /// \param counter_key key poiting to the counter in the counters map.
  746. /// The short counter name has to be used to access counter.
  747. /// \return pointer to specified counter object.
  748. CustomCounterPtr getCounter(const std::string& counter_key) {
  749. CustomCountersMapIterator it = custom_counters_.find(counter_key);
  750. if (it == custom_counters_.end()) {
  751. isc_throw(BadValue,
  752. "Custom counter " << counter_key << "does not exist");
  753. }
  754. return(it->second);
  755. }
  756. /// \brief Increment specified counter.
  757. ///
  758. /// Increement counter value by one.
  759. ///
  760. /// \param counter_key key poitinh to the counter in the counters map.
  761. /// \return pointer to specified counter after incrementation.
  762. const CustomCounter& IncrementCounter(const std::string& counter_key) {
  763. CustomCounterPtr counter = getCounter(counter_key);
  764. return(++(*counter));
  765. }
  766. /// \brief Adds new packet to the sent packets list.
  767. ///
  768. /// Method adds new packet to the sent packets list.
  769. /// Packets are added to the list sequentially and
  770. /// most often read sequentially.
  771. ///
  772. /// \param xchg_type exchange type.
  773. /// \param packet packet to be added to the list
  774. /// \throw isc::BadValue if invalid exchange type specified or
  775. /// packet is null.
  776. void passSentPacket(const ExchangeType xchg_type,
  777. const boost::shared_ptr<const T>& packet) {
  778. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  779. xchg_stats->appendSent(packet);
  780. }
  781. /// \brief Add new received packet and match with sent packet.
  782. ///
  783. /// Method adds new packet to the list of received packets. It
  784. /// also searches for corresponding packet on the list of sent
  785. /// packets. When packets are matched the statistics counters
  786. /// are updated accordingly for the particular exchange type.
  787. ///
  788. /// \param xchg_type exchange type.
  789. /// \param packet received packet
  790. /// \throw isc::BadValue if invalid exchange type specified
  791. /// or packet is null.
  792. /// \throw isc::Unexpected if corresponding packet was not
  793. /// found on the list of sent packets.
  794. void passRcvdPacket(const ExchangeType xchg_type,
  795. const boost::shared_ptr<const T>& packet) {
  796. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  797. boost::shared_ptr<const T> sent_packet
  798. = xchg_stats->findSent(packet);
  799. if (sent_packet) {
  800. xchg_stats->updateDelays(sent_packet, packet);
  801. if (archive_enabled_) {
  802. xchg_stats->appendRcvd(packet);
  803. }
  804. }
  805. }
  806. /// \brief Return minumum delay between sent and received packet.
  807. ///
  808. /// Method returns minimum delay between sent and received packet
  809. /// for specified exchange type.
  810. ///
  811. /// \param xchg_type exchange type.
  812. /// \throw isc::BadValue if invalid exchange type specified.
  813. /// \return minimum delay between packets.
  814. double getMinDelay(const ExchangeType xchg_type) const {
  815. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  816. return(xchg_stats->getMinDelay());
  817. }
  818. /// \brief Return maxmimum delay between sent and received packet.
  819. ///
  820. /// Method returns maximum delay between sent and received packet
  821. /// for specified exchange type.
  822. ///
  823. /// \param xchg_type exchange type.
  824. /// \throw isc::BadValue if invalid exchange type specified.
  825. /// \return maximum delay between packets.
  826. double getMaxDelay(const ExchangeType xchg_type) const {
  827. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  828. return(xchg_stats->getMaxDelay());
  829. }
  830. /// \brief Return avarage packet delay.
  831. ///
  832. /// Method returns average packet delay for specified
  833. /// exchange type.
  834. ///
  835. /// \return average packet delay.
  836. double getAvgDelay(const ExchangeType xchg_type) const {
  837. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  838. return(xchg_stats->getAvgDelay());
  839. }
  840. /// \brief Return standard deviation of packet delay.
  841. ///
  842. /// Method returns standard deviation of packet delay
  843. /// for specified exchange type.
  844. ///
  845. /// \return standard deviation of packet delay.
  846. double getStdDevDelay(const ExchangeType xchg_type) const {
  847. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  848. return(xchg_stats->getStdDevDelay());
  849. }
  850. /// \brief Return number of orphant packets.
  851. ///
  852. /// Method returns number of orphant packets for specified
  853. /// exchange type.
  854. ///
  855. /// \param xchg_type exchange type.
  856. /// \throw isc::BadValue if invalid exchange type specified.
  857. /// \return number of orphant packets so far.
  858. uint64_t getOrphans(const ExchangeType xchg_type) const {
  859. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  860. return(xchg_stats->getOrphans());
  861. }
  862. /// \brief Return average unordered lookup set size.
  863. ///
  864. /// Method returns average unordered lookup set size.
  865. /// This value changes every time \ref ExchangeStats::findSent
  866. /// function performs unordered packet lookup.
  867. ///
  868. /// \param xchg_type exchange type.
  869. /// \throw isc::BadValue if invalid exchange type specified.
  870. /// \return average unordered lookup set size.
  871. double getAvgUnorderedLookupSetSize(const ExchangeType xchg_type) const {
  872. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  873. return(xchg_stats->getAvgUnorderedLookupSetSize());
  874. }
  875. /// \brief Return number of unordered sent packets lookups
  876. ///
  877. /// Method returns number of unordered sent packet lookups.
  878. /// Unordered lookup is used when received packet was sent
  879. /// out of order by server - transaction id of received
  880. /// packet does not match transaction id of next sent packet.
  881. ///
  882. /// \param xchg_type exchange type.
  883. /// \throw isc::BadValue if invalid exchange type specified.
  884. /// \return number of unordered lookups.
  885. uint64_t getUnorderedLookups(const ExchangeType xchg_type) const {
  886. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  887. return(xchg_stats->getUnorderedLookups());
  888. }
  889. /// \brief Return number of ordered sent packets lookups
  890. ///
  891. /// Method returns number of ordered sent packet lookups.
  892. /// Ordered lookup is used when packets are received in the
  893. /// same order as they were sent to the server.
  894. /// If packets are skipped or received out of order, lookup
  895. /// function will use unordered lookup (with hash table).
  896. ///
  897. /// \param xchg_type exchange type.
  898. /// \throw isc::BadValue if invalid exchange type specified.
  899. /// \return number of ordered lookups.
  900. uint64_t getOrderedLookups(const ExchangeType xchg_type) const {
  901. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  902. return(xchg_stats->getOrderedLookups());
  903. }
  904. /// \brief Return total number of sent packets
  905. ///
  906. /// Method returns total number of sent packets for specified
  907. /// exchange type.
  908. ///
  909. /// \param xchg_type exchange type.
  910. /// \throw isc::BadValue if invalid exchange type specified.
  911. /// \return number of sent packets.
  912. uint64_t getSentPacketsNum(const ExchangeType xchg_type) const {
  913. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  914. return(xchg_stats->getSentPacketsNum());
  915. }
  916. /// \brief Return total number of received packets
  917. ///
  918. /// Method returns total number of received packets for specified
  919. /// exchange type.
  920. ///
  921. /// \param xchg_type exchange type.
  922. /// \throw isc::BadValue if invalid exchange type specified.
  923. /// \return number of received packets.
  924. uint64_t getRcvdPacketsNum(const ExchangeType xchg_type) const {
  925. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  926. return(xchg_stats->getRcvdPacketsNum());
  927. }
  928. /// \brief Return name of the exchange.
  929. ///
  930. /// Method returns name of the specified exchange type.
  931. /// This function is mainly for logging purposes.
  932. ///
  933. /// \param xchg_type exchange type.
  934. /// \return string representing name of the exchange.
  935. std::string exchangeToString(ExchangeType xchg_type) const {
  936. switch(xchg_type) {
  937. case XCHG_DO:
  938. return("DISCOVER-OFFER");
  939. case XCHG_RA:
  940. return("REQUEST-ACK");
  941. case XCHG_SA:
  942. return("SOLICIT-ADVERTISE");
  943. case XCHG_RR:
  944. return("REQUEST-REPLY");
  945. default:
  946. return("Unknown exchange type");
  947. }
  948. }
  949. /// \brief Print statistics counters for all exchange types.
  950. ///
  951. /// Method prints statistics for all exchange types.
  952. /// Statistics includes:
  953. /// - number of sent and received packets
  954. /// - number of dropped packets and number of orphans
  955. /// - minimum packets delay,
  956. /// - average packets delay,
  957. /// - maximum packets delay,
  958. /// - standard deviation of packets delay.
  959. ///
  960. /// \throw isc::InvalidOperation if no exchange type added to
  961. /// track statistics.
  962. void printStats() const {
  963. if (exchanges_.size() == 0) {
  964. isc_throw(isc::InvalidOperation,
  965. "no exchange type added for tracking");
  966. }
  967. for (ExchangesMapIterator it = exchanges_.begin();
  968. it != exchanges_.end();
  969. ++it) {
  970. ExchangeStatsPtr xchg_stats = it->second;
  971. std::cout << "***Statistics for: " << exchangeToString(it->first)
  972. << "***" << std::endl;
  973. xchg_stats->printMainStats();
  974. std::cout << std::endl;
  975. xchg_stats->printRTTStats();
  976. std::cout << std::endl;
  977. }
  978. }
  979. /// \brief Print timestamps of all packets.
  980. ///
  981. /// Method prints timestamps of all sent and received
  982. /// packets for all defined exchange types.
  983. ///
  984. /// \throw isc::InvalidOperation if one of the packets has
  985. /// no timestamp value set or if packets archive mode is
  986. /// disabled.
  987. ///
  988. /// \throw isc::InvalidOperation if no exchange type added to
  989. /// track statistics or packets archive mode is disabled.
  990. void printTimestamps() const {
  991. if (exchanges_.size() == 0) {
  992. isc_throw(isc::InvalidOperation,
  993. "no exchange type added for tracking");
  994. }
  995. for (ExchangesMapIterator it = exchanges_.begin();
  996. it != exchanges_.end();
  997. ++it) {
  998. ExchangeStatsPtr xchg_stats = it->second;
  999. std::cout << "***Timestamps for packets: "
  1000. << exchangeToString(it->first)
  1001. << "***" << std::endl;
  1002. xchg_stats->printTimestamps();
  1003. std::cout << std::endl;
  1004. }
  1005. }
  1006. /// \brief Print names and values of custom counters.
  1007. ///
  1008. /// Method prints names and values of custom counters. Custom counters
  1009. /// are defined by client class for tracking different statistics.
  1010. ///
  1011. /// \throw isc::InvalidOperation if no custom counters added for tracking.
  1012. void printCustomCounters() const {
  1013. if (custom_counters_.size() == 0) {
  1014. isc_throw(isc::InvalidOperation, "no custom counters specified");
  1015. }
  1016. for (CustomCountersMapIterator it = custom_counters_.begin();
  1017. it != custom_counters_.end();
  1018. ++it) {
  1019. CustomCounterPtr counter = it->second;
  1020. std::cout << counter->getName() << ": " << counter->getValue()
  1021. << std::endl;
  1022. }
  1023. }
  1024. private:
  1025. /// \brief Return exchange stats object for given exchange type
  1026. ///
  1027. /// Method returns exchange stats object for given exchange type.
  1028. ///
  1029. /// \param xchg_type exchange type.
  1030. /// \throw isc::BadValue if invalid exchange type specified.
  1031. /// \return exchange stats object.
  1032. ExchangeStatsPtr getExchangeStats(const ExchangeType xchg_type) const {
  1033. ExchangesMapIterator it = exchanges_.find(xchg_type);
  1034. if (it == exchanges_.end()) {
  1035. isc_throw(BadValue, "Packets exchange not specified");
  1036. }
  1037. ExchangeStatsPtr xchg_stats = it->second;
  1038. return(xchg_stats);
  1039. }
  1040. ExchangesMap exchanges_; ///< Map of exchange types.
  1041. CustomCountersMap custom_counters_; ///< Map with custom counters.
  1042. /// Indicates that packets from list of sent packets should be
  1043. /// archived (moved to list of archived packets) once they are
  1044. /// matched with received packets. This is required when it has
  1045. /// been selected from the command line to print packets'
  1046. /// timestamps after test. This may affect performance and
  1047. /// consume large amount of memory when the test is running
  1048. /// for extended period of time and many packets have to be
  1049. /// archived.
  1050. bool archive_enabled_;
  1051. };
  1052. } // namespace perfdhcp
  1053. } // namespace isc
  1054. #endif // __STATS_MGR_H