stats_mgr.h 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  1. // Copyright (C) 2012-2013 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 <dhcp/pkt4.h>
  17. #include <dhcp/pkt6.h>
  18. #include <exceptions/exceptions.h>
  19. #include <boost/noncopyable.hpp>
  20. #include <boost/shared_ptr.hpp>
  21. #include <boost/multi_index_container.hpp>
  22. #include <boost/multi_index/hashed_index.hpp>
  23. #include <boost/multi_index/sequenced_index.hpp>
  24. #include <boost/multi_index/global_fun.hpp>
  25. #include <boost/multi_index/mem_fun.hpp>
  26. #include <boost/date_time/posix_time/posix_time.hpp>
  27. #include <iostream>
  28. #include <map>
  29. namespace isc {
  30. namespace perfdhcp {
  31. /// \brief Statistics Manager
  32. ///
  33. /// This class template is a storage for various performance statistics
  34. /// collected during performance tests execution with perfdhcp tool.
  35. ///
  36. /// Statistics Manager holds lists of sent and received packets and
  37. /// groups them into exchanges. For example: DHCPDISCOVER message and
  38. /// corresponding DHCPOFFER messages belong to one exchange, DHCPREQUEST
  39. /// and corresponding DHCPACK message belong to another exchange etc.
  40. /// In order to update statistics for a particular exchange type, client
  41. /// class passes sent and received packets. Internally, Statistics Manager
  42. /// tries to match transaction id of received packet with sent packet
  43. /// stored on the list of sent packets. When packets are matched the
  44. /// round trip time can be calculated.
  45. ///
  46. /// \param T class representing DHCPv4 or DHCPv6 packet.
  47. template <class T = dhcp::Pkt4>
  48. class StatsMgr : public boost::noncopyable {
  49. public:
  50. /// \brief Custom Counter
  51. ///
  52. /// This class represents custom statistics counters. Client class
  53. /// may create unlimited number of counters. Such counters are
  54. /// being stored in map in Statistics Manager and access using
  55. /// unique string key.
  56. class CustomCounter {
  57. public:
  58. /// \brief Constructor.
  59. ///
  60. /// This constructor sets counter name. This name is used in
  61. /// log file to report value of each counter.
  62. ///
  63. /// \param name name of the counter used in log file.
  64. CustomCounter(const std::string& name) :
  65. counter_(0),
  66. name_(name) { };
  67. /// \brief Increment operator.
  68. const CustomCounter& operator++() {
  69. ++counter_;
  70. return (*this);
  71. }
  72. /// \brief Increment operator.
  73. const CustomCounter& operator++(int) {
  74. CustomCounter& this_counter(*this);
  75. operator++();
  76. return (this_counter);
  77. }
  78. const CustomCounter& operator+=(int val) {
  79. counter_ += val;
  80. return (*this);
  81. }
  82. /// \brief Return counter value.
  83. ///
  84. /// Method returns counter value.
  85. ///
  86. /// \return counter value.
  87. uint64_t getValue() const { return(counter_); }
  88. /// \brief Return counter name.
  89. ///
  90. /// Method returns counter name.
  91. ///
  92. /// \return counter name.
  93. const std::string& getName() const { return(name_); }
  94. private:
  95. /// \brief Default constructor.
  96. ///
  97. /// Default constructor is private because we don't want client
  98. /// class to call it because we want client class to specify
  99. /// counter's name.
  100. CustomCounter() { };
  101. uint64_t counter_; ///< Counter's value.
  102. std::string name_; ///< Counter's name.
  103. };
  104. typedef typename boost::shared_ptr<CustomCounter> CustomCounterPtr;
  105. /// DHCP packet exchange types.
  106. enum ExchangeType {
  107. XCHG_DO, ///< DHCPv4 DISCOVER-OFFER
  108. XCHG_RA, ///< DHCPv4 REQUEST-ACK
  109. XCHG_SA, ///< DHCPv6 SOLICIT-ADVERTISE
  110. XCHG_RR, ///< DHCPv6 REQUEST-REPLY
  111. XCHG_RN, ///< DHCPv6 RENEW-REPLY
  112. XCHG_RL ///< DHCPv6 RELEASE-REPLY
  113. };
  114. /// \brief Exchange Statistics.
  115. ///
  116. /// This class collects statistics for exchanges. Parent class
  117. /// may define number of different packet exchanges like:
  118. /// DHCPv4 DISCOVER-OFFER, DHCPv6 SOLICIT-ADVERTISE etc. Performance
  119. /// statistics will be collected for each of those separately in
  120. /// corresponding instance of ExchangeStats.
  121. class ExchangeStats {
  122. public:
  123. /// \brief Hash transaction id of the packet.
  124. ///
  125. /// Function hashes transaction id of the packet. Hashing is
  126. /// non-unique. Many packets may have the same hash value and thus
  127. /// they belong to the same packet buckets. Packet buckets are
  128. /// used for unordered packets search with multi index container.
  129. ///
  130. /// \param packet packet which transaction id is to be hashed.
  131. /// \throw isc::BadValue if packet is null.
  132. /// \return transaction id hash.
  133. static uint32_t hashTransid(const boost::shared_ptr<T>& packet) {
  134. if (!packet) {
  135. isc_throw(BadValue, "Packet is null");
  136. }
  137. return(packet->getTransid() & 1023);
  138. }
  139. /// \brief List of packets (sent or received).
  140. ///
  141. /// List of packets based on multi index container allows efficient
  142. /// search of packets based on their sequence (order in which they
  143. /// were inserted) as well as based on their hashed transaction id.
  144. /// The first index (sequenced) provides the way to use container
  145. /// as a regular list (including iterators, removal of elements from
  146. /// the middle of the collection etc.). This index is meant to be used
  147. /// more frequently than the latter one and it is based on the
  148. /// assumption that responses from the DHCP server are received in
  149. /// order. In this case, when next packet is received it can be
  150. /// matched with next packet on the list of sent packets. This
  151. /// prevents intensive searches on the list of sent packets every
  152. /// time new packet arrives. In many cases however packets can be
  153. /// dropped by the server or may be sent out of order and we still
  154. /// want to have ability to search packets using transaction id.
  155. /// The second index can be used for this purpose. This index is
  156. /// hashing transaction ids using custom function \ref hashTransid.
  157. /// Note that other possibility would be to simply specify index
  158. /// that uses transaction id directly (instead of hashing with
  159. /// \ref hashTransid). In this case however we have chosen to use
  160. /// hashing function because it shortens the index size to just
  161. /// 1023 values maximum. Search operation on this index generally
  162. /// returns the range of packets that have the same transaction id
  163. /// hash assigned but most often these ranges will be short so further
  164. /// search within a range to find a packet with pacrticular transaction
  165. /// id will not be intensive.
  166. ///
  167. /// Example 1: Add elements to the list
  168. /// \code
  169. /// PktList packets_collection();
  170. /// boost::shared_ptr<Pkt4> pkt1(new Pkt4(...));
  171. /// boost::shared_ptr<Pkt4> pkt2(new Pkt4(...));
  172. /// // Add new packet to the container, it will be available through
  173. /// // both indexes
  174. /// packets_collection.push_back(pkt1);
  175. /// // Here is another way to add packet to the container. The result
  176. /// // is exactly the same as previously.
  177. /// packets_collection.template get<0>().push_back(pkt2);
  178. /// \endcode
  179. ///
  180. /// Example 2: Access elements through sequencial index
  181. /// \code
  182. /// PktList packets_collection();
  183. /// ... # Add elements to the container
  184. /// for (PktListIterator it = packets_collection.begin();
  185. /// it != packets_collection.end();
  186. /// ++it) {
  187. /// boost::shared_ptr<Pkt4> pkt = *it;
  188. /// # Do something with packet;
  189. /// }
  190. /// \endcode
  191. ///
  192. /// Example 3: Access elements through hashed index
  193. /// \code
  194. /// // Get the instance of the second search index.
  195. /// PktListTransidHashIndex& idx = sent_packets_.template get<1>();
  196. /// // Get the range (bucket) of packets sharing the same transaction
  197. /// // id hash.
  198. /// std::pair<PktListTransidHashIterator,PktListTransidHashIterator> p =
  199. /// idx.equal_range(hashTransid(rcvd_packet));
  200. /// // Iterate through the returned bucket.
  201. /// for (PktListTransidHashIterator it = p.first; it != p.second;
  202. /// ++it) {
  203. /// boost::shared_ptr pkt = *it;
  204. /// ... # Do something with the packet (e.g. check transaction id)
  205. /// }
  206. /// \endcode
  207. typedef boost::multi_index_container<
  208. // Container holds shared_ptr<Pkt4> or shared_ptr<Pkt6> objects.
  209. boost::shared_ptr<T>,
  210. // List container indexes.
  211. boost::multi_index::indexed_by<
  212. // Sequenced index provides the way to use this container
  213. // in the same way as std::list.
  214. boost::multi_index::sequenced<>,
  215. // The other index keeps products of transaction id.
  216. boost::multi_index::hashed_non_unique<
  217. // Specify hash function to get the product of
  218. // transaction id. This product is obtained by calling
  219. // hashTransid() function.
  220. boost::multi_index::global_fun<
  221. // Hashing function takes shared_ptr<Pkt4> or
  222. // shared_ptr<Pkt6> as argument.
  223. const boost::shared_ptr<T>&,
  224. // ... and returns uint32 value.
  225. uint32_t,
  226. // ... and here is a reference to it.
  227. &ExchangeStats::hashTransid
  228. >
  229. >
  230. >
  231. > PktList;
  232. /// Packet list iterator for sequencial access to elements.
  233. typedef typename PktList::iterator PktListIterator;
  234. /// Packet list index to search packets using transaction id hash.
  235. typedef typename PktList::template nth_index<1>::type
  236. PktListTransidHashIndex;
  237. /// Packet list iterator to access packets using transaction id hash.
  238. typedef typename PktListTransidHashIndex::const_iterator
  239. PktListTransidHashIterator;
  240. /// \brief Constructor
  241. ///
  242. /// \param xchg_type exchange type
  243. /// \param drop_time maximum time elapsed before packet is
  244. /// assumed dropped. Negative value disables it.
  245. /// \param archive_enabled if true packets archive mode is enabled.
  246. /// In this mode all packets are stored throughout the test execution.
  247. /// \param boot_time Holds the timestamp when perfdhcp has been started.
  248. ExchangeStats(const ExchangeType xchg_type,
  249. const double drop_time,
  250. const bool archive_enabled,
  251. const boost::posix_time::ptime boot_time)
  252. : xchg_type_(xchg_type),
  253. sent_packets_(),
  254. rcvd_packets_(),
  255. archived_packets_(),
  256. archive_enabled_(archive_enabled),
  257. drop_time_(drop_time),
  258. min_delay_(std::numeric_limits<double>::max()),
  259. max_delay_(0.),
  260. sum_delay_(0.),
  261. sum_delay_squared_(0.),
  262. orphans_(0),
  263. collected_(0),
  264. unordered_lookup_size_sum_(0),
  265. unordered_lookups_(0),
  266. ordered_lookups_(0),
  267. sent_packets_num_(0),
  268. rcvd_packets_num_(0),
  269. boot_time_(boot_time)
  270. {
  271. next_sent_ = sent_packets_.begin();
  272. }
  273. /// \brief Add new packet to list of sent packets.
  274. ///
  275. /// Method adds new packet to list of sent packets.
  276. ///
  277. /// \param packet packet object to be added.
  278. /// \throw isc::BadValue if packet is null.
  279. void appendSent(const boost::shared_ptr<T>& packet) {
  280. if (!packet) {
  281. isc_throw(BadValue, "Packet is null");
  282. }
  283. ++sent_packets_num_;
  284. sent_packets_.template get<0>().push_back(packet);
  285. }
  286. /// \brief Add new packet to list of received packets.
  287. ///
  288. /// Method adds new packet to list of received packets.
  289. ///
  290. /// \param packet packet object to be added.
  291. /// \throw isc::BadValue if packet is null.
  292. void appendRcvd(const boost::shared_ptr<T>& packet) {
  293. if (!packet) {
  294. isc_throw(BadValue, "Packet is null");
  295. }
  296. rcvd_packets_.push_back(packet);
  297. }
  298. /// \brief Update delay counters.
  299. ///
  300. /// Method updates delay counters based on timestamps of
  301. /// sent and received packets.
  302. ///
  303. /// \param sent_packet sent packet
  304. /// \param rcvd_packet received packet
  305. /// \throw isc::BadValue if sent or received packet is null.
  306. /// \throw isc::Unexpected if failed to calculate timestamps
  307. void updateDelays(const boost::shared_ptr<T>& sent_packet,
  308. const boost::shared_ptr<T>& rcvd_packet) {
  309. if (!sent_packet) {
  310. isc_throw(BadValue, "Sent packet is null");
  311. }
  312. if (!rcvd_packet) {
  313. isc_throw(BadValue, "Received packet is null");
  314. }
  315. boost::posix_time::ptime sent_time = sent_packet->getTimestamp();
  316. boost::posix_time::ptime rcvd_time = rcvd_packet->getTimestamp();
  317. if (sent_time.is_not_a_date_time() ||
  318. rcvd_time.is_not_a_date_time()) {
  319. isc_throw(Unexpected,
  320. "Timestamp must be set for sent and "
  321. "received packet to measure RTT");
  322. }
  323. boost::posix_time::time_period period(sent_time, rcvd_time);
  324. // We don't bother calculating deltas in nanoseconds. It is much
  325. // more convenient to use seconds instead because we are going to
  326. // sum them up.
  327. double delta =
  328. static_cast<double>(period.length().total_nanoseconds()) / 1e9;
  329. if (delta < 0) {
  330. isc_throw(Unexpected, "Sent packet's timestamp must not be "
  331. "greater than received packet's timestamp");
  332. }
  333. // Record the minimum delay between sent and received packets.
  334. if (delta < min_delay_) {
  335. min_delay_ = delta;
  336. }
  337. // Record the maximum delay between sent and received packets.
  338. if (delta > max_delay_) {
  339. max_delay_ = delta;
  340. }
  341. // Update delay sum and square sum. That will be used to calculate
  342. // mean delays.
  343. sum_delay_ += delta;
  344. sum_delay_squared_ += delta * delta;
  345. }
  346. /// \brief Match received packet with the corresponding sent packet.
  347. ///
  348. /// Method finds packet with specified transaction id on the list
  349. /// of sent packets. It is used to match received packet with
  350. /// corresponding sent packet.
  351. /// Since packets from the server most often come in the same order
  352. /// as they were sent by client, this method will first check if
  353. /// next sent packet matches. If it doesn't, function will search
  354. /// the packet using indexing by transaction id. This reduces
  355. /// packet search time significantly.
  356. ///
  357. /// \param rcvd_packet received packet to be matched with sent packet.
  358. /// \throw isc::BadValue if received packet is null.
  359. /// \return packet having specified transaction or NULL if packet
  360. /// not found
  361. boost::shared_ptr<T>
  362. matchPackets(const boost::shared_ptr<T>& rcvd_packet) {
  363. using namespace boost::posix_time;
  364. if (!rcvd_packet) {
  365. isc_throw(BadValue, "Received packet is null");
  366. }
  367. if (sent_packets_.size() == 0) {
  368. // List of sent packets is empty so there is no sense
  369. // to continue looking fo the packet. It also means
  370. // that the received packet we got has no corresponding
  371. // sent packet so orphans counter has to be updated.
  372. ++orphans_;
  373. return(boost::shared_ptr<T>());
  374. } else if (next_sent_ == sent_packets_.end()) {
  375. // Even if there are still many unmatched packets on the
  376. // list we might hit the end of it because of unordered
  377. // lookups. The next logical step is to reset iterator.
  378. next_sent_ = sent_packets_.begin();
  379. }
  380. // With this variable we will be signalling success or failure
  381. // to find the packet.
  382. bool packet_found = false;
  383. // Most likely responses are sent from the server in the same
  384. // order as client's requests to the server. We are caching
  385. // next sent packet and first try to match it with the next
  386. // incoming packet. We are successful if there is no
  387. // packet drop or out of order packets sent. This is actually
  388. // the fastest way to look for packets.
  389. if ((*next_sent_)->getTransid() == rcvd_packet->getTransid()) {
  390. ++ordered_lookups_;
  391. packet_found = true;
  392. } else {
  393. // If we are here, it means that we were unable to match the
  394. // next incoming packet with next sent packet so we need to
  395. // take a little more expensive approach to look packets using
  396. // alternative index (transaction id & 1023).
  397. PktListTransidHashIndex& idx = sent_packets_.template get<1>();
  398. // Packets are grouped using trasaction id masked with value
  399. // of 1023. For instance, packets with transaction id equal to
  400. // 1, 1024 ... will belong to the same group (a.k.a. bucket).
  401. // When using alternative index we don't find the packet but
  402. // bucket of packets and we need to iterate through the bucket
  403. // to find the one that has desired transaction id.
  404. std::pair<PktListTransidHashIterator,PktListTransidHashIterator> p =
  405. idx.equal_range(hashTransid(rcvd_packet));
  406. // We want to keep statistics of unordered lookups to make
  407. // sure that there is a right balance between number of
  408. // unordered lookups and ordered lookups. If number of unordered
  409. // lookups is high it may mean that many packets are lost or
  410. // sent out of order.
  411. ++unordered_lookups_;
  412. // We also want to keep the mean value of the bucket. The lower
  413. // bucket size the better. If bucket sizes appear to big we
  414. // might want to increase number of buckets.
  415. unordered_lookup_size_sum_ += std::distance(p.first, p.second);
  416. for (PktListTransidHashIterator it = p.first; it != p.second;
  417. ++it) {
  418. if ((*it)->getTransid() == rcvd_packet->getTransid()) {
  419. packet_found = true;
  420. next_sent_ =
  421. sent_packets_.template project<0>(it);
  422. break;
  423. }
  424. ptime now = microsec_clock::universal_time();
  425. ptime packet_time = (*it)->getTimestamp();
  426. time_period packet_period(packet_time, now);
  427. if (!packet_period.is_null()) {
  428. double period_fractional =
  429. packet_period.length().total_seconds() +
  430. (static_cast<double>(packet_period.length().fractional_seconds())
  431. / packet_period.length().ticks_per_second());
  432. if (drop_time_ > 0 && (period_fractional > drop_time_)) {
  433. // The packet pointed to by 'it' is timed out so we
  434. // have to remove it. Removal may invalidate the
  435. // next_sent_ pointer if it points to the packet
  436. // being removed. So, we set the next_sent_ to point
  437. // to the next packet after removed one. This
  438. // pointer will be further updated in the following
  439. // iterations, if the subsequent packets are also
  440. // timed out.
  441. next_sent_ = eraseSent(sent_packets_.template project<0>(it));
  442. ++collected_;
  443. }
  444. }
  445. }
  446. }
  447. if (!packet_found) {
  448. // If we are here, it means that both ordered lookup and
  449. // unordered lookup failed. Searched packet is not on the list.
  450. ++orphans_;
  451. return(boost::shared_ptr<T>());
  452. }
  453. // Packet is matched so we count it. We don't count unmatched packets
  454. // as they are counted as orphans with a separate counter.
  455. ++rcvd_packets_num_;
  456. boost::shared_ptr<T> sent_packet(*next_sent_);
  457. // If packet was found, we assume it will be never searched
  458. // again. We want to delete this packet from the list to
  459. // improve performance of future searches.
  460. next_sent_ = eraseSent(next_sent_);
  461. return(sent_packet);
  462. }
  463. /// \brief Return minumum delay between sent and received packet.
  464. ///
  465. /// Method returns minimum delay between sent and received packet.
  466. ///
  467. /// \return minimum delay between packets.
  468. double getMinDelay() const { return(min_delay_); }
  469. /// \brief Return maxmimum delay between sent and received packet.
  470. ///
  471. /// Method returns maximum delay between sent and received packet.
  472. ///
  473. /// \return maximum delay between packets.
  474. double getMaxDelay() const { return(max_delay_); }
  475. /// \brief Return average packet delay.
  476. ///
  477. /// Method returns average packet delay. If no packets have been
  478. /// received for this exchange avg delay can't be calculated and
  479. /// thus method throws exception.
  480. ///
  481. /// \throw isc::InvalidOperation if no packets for this exchange
  482. /// have been received yet.
  483. /// \return average packet delay.
  484. double getAvgDelay() const {
  485. if (rcvd_packets_num_ == 0) {
  486. isc_throw(InvalidOperation, "no packets received");
  487. }
  488. return(sum_delay_ / rcvd_packets_num_);
  489. }
  490. /// \brief Return standard deviation of packet delay.
  491. ///
  492. /// Method returns standard deviation of packet delay. If no
  493. /// packets have been received for this exchange, the standard
  494. /// deviation can't be calculated and thus method throws
  495. /// exception.
  496. ///
  497. /// \throw isc::InvalidOperation if number of received packets
  498. /// for the exchange is equal to zero.
  499. /// \return standard deviation of packet delay.
  500. double getStdDevDelay() const {
  501. if (rcvd_packets_num_ == 0) {
  502. isc_throw(InvalidOperation, "no packets received");
  503. }
  504. return(sqrt(sum_delay_squared_ / rcvd_packets_num_ -
  505. getAvgDelay() * getAvgDelay()));
  506. }
  507. /// \brief Return number of orphant packets.
  508. ///
  509. /// Method returns number of received packets that had no matching
  510. /// sent packet. It is possible that such packet was late or not
  511. /// for us.
  512. ///
  513. /// \return number of orphant received packets.
  514. uint64_t getOrphans() const { return(orphans_); }
  515. /// \brief Return number of garbage collected packets.
  516. ///
  517. /// Method returns number of garbage collected timed out
  518. /// packets. Packet is assumed timed out when duration
  519. /// between sending it to server and receiving server's
  520. /// response is greater than value specified with -d<value>
  521. /// command line argument.
  522. ///
  523. /// \return number of garbage collected packets.
  524. uint64_t getCollectedNum() const { return(collected_); }
  525. /// \brief Return average unordered lookup set size.
  526. ///
  527. /// Method returns average unordered lookup set size.
  528. /// This value changes every time \ref ExchangeStats::matchPackets
  529. /// function performs unordered packet lookup.
  530. ///
  531. /// \throw isc::InvalidOperation if there have been no unordered
  532. /// lookups yet.
  533. /// \return average unordered lookup set size.
  534. double getAvgUnorderedLookupSetSize() const {
  535. if (unordered_lookups_ == 0) {
  536. isc_throw(InvalidOperation, "no unordered lookups");
  537. }
  538. return(static_cast<double>(unordered_lookup_size_sum_) /
  539. static_cast<double>(unordered_lookups_));
  540. }
  541. /// \brief Return number of unordered sent packets lookups
  542. ///
  543. /// Method returns number of unordered sent packet lookups.
  544. /// Unordered lookup is used when received packet was sent
  545. /// out of order by server - transaction id of received
  546. /// packet does not match transaction id of next sent packet.
  547. ///
  548. /// \return number of unordered lookups.
  549. uint64_t getUnorderedLookups() const { return(unordered_lookups_); }
  550. /// \brief Return number of ordered sent packets lookups
  551. ///
  552. /// Method returns number of ordered sent packet lookups.
  553. /// Ordered lookup is used when packets are received in the
  554. /// same order as they were sent to the server.
  555. /// If packets are skipped or received out of order, lookup
  556. /// function will use unordered lookup (with hash table).
  557. ///
  558. /// \return number of ordered lookups.
  559. uint64_t getOrderedLookups() const { return(ordered_lookups_); }
  560. /// \brief Return total number of sent packets
  561. ///
  562. /// Method returns total number of sent packets.
  563. ///
  564. /// \return number of sent packets.
  565. uint64_t getSentPacketsNum() const { return(sent_packets_num_); }
  566. /// \brief Return total number of received packets
  567. ///
  568. /// Method returns total number of received packets.
  569. ///
  570. /// \return number of received packets.
  571. uint64_t getRcvdPacketsNum() const { return(rcvd_packets_num_); }
  572. /// \brief Return number of dropped packets.
  573. ///
  574. /// Method returns number of dropped packets.
  575. ///
  576. /// \return number of dropped packets.
  577. uint64_t getDroppedPacketsNum() const {
  578. uint64_t drops = 0;
  579. if (getSentPacketsNum() > getRcvdPacketsNum()) {
  580. drops = getSentPacketsNum() - getRcvdPacketsNum();
  581. }
  582. return(drops);
  583. }
  584. /// \brief Print main statistics for packet exchange.
  585. ///
  586. /// Method prints main statistics for particular exchange.
  587. /// Statistics includes: number of sent and received packets,
  588. /// number of dropped packets and number of orphans.
  589. void printMainStats() const {
  590. using namespace std;
  591. cout << "sent packets: " << getSentPacketsNum() << endl
  592. << "received packets: " << getRcvdPacketsNum() << endl
  593. << "drops: " << getDroppedPacketsNum() << endl
  594. << "orphans: " << getOrphans() << endl;
  595. }
  596. /// \brief Print round trip time packets statistics.
  597. ///
  598. /// Method prints round trip time packets statistics. Statistics
  599. /// includes minimum packet delay, maximum packet delay, average
  600. /// packet delay and standard deviation of delays. Packet delay
  601. /// is a duration between sending a packet to server and receiving
  602. /// response from server.
  603. void printRTTStats() const {
  604. using namespace std;
  605. try {
  606. cout << fixed << setprecision(3)
  607. << "min delay: " << getMinDelay() * 1e3 << " ms" << endl
  608. << "avg delay: " << getAvgDelay() * 1e3 << " ms" << endl
  609. << "max delay: " << getMaxDelay() * 1e3 << " ms" << endl
  610. << "std deviation: " << getStdDevDelay() * 1e3 << " ms"
  611. << endl
  612. << "collected packets: " << getCollectedNum() << endl;
  613. } catch (const Exception& e) {
  614. cout << "Delay summary unavailable! No packets received." << endl;
  615. }
  616. }
  617. //// \brief Print timestamps for sent and received packets.
  618. ///
  619. /// Method prints timestamps for all sent and received packets for
  620. /// packet exchange. In order to run this method the packets
  621. /// archiving mode has to be enabled during object constructions.
  622. /// Otherwise sent packets are not stored during tests execution
  623. /// and this method has no ability to get and print their timestamps.
  624. ///
  625. /// \throw isc::InvalidOperation if found packet with no timestamp or
  626. /// if packets archive mode is disabled.
  627. void printTimestamps() {
  628. // If archive mode is disabled there is no sense to proceed
  629. // because we don't have packets and their timestamps.
  630. if (!archive_enabled_) {
  631. isc_throw(isc::InvalidOperation,
  632. "packets archive mode is disabled");
  633. }
  634. if (rcvd_packets_num_ == 0) {
  635. std::cout << "Unavailable! No packets received." << std::endl;
  636. }
  637. // We will be using boost::posix_time extensively here
  638. using namespace boost::posix_time;
  639. // Iterate through all received packets.
  640. for (PktListIterator it = rcvd_packets_.begin();
  641. it != rcvd_packets_.end();
  642. ++it) {
  643. boost::shared_ptr<T> rcvd_packet = *it;
  644. PktListTransidHashIndex& idx =
  645. archived_packets_.template get<1>();
  646. std::pair<PktListTransidHashIterator,
  647. PktListTransidHashIterator> p =
  648. idx.equal_range(hashTransid(rcvd_packet));
  649. for (PktListTransidHashIterator it_archived = p.first;
  650. it_archived != p.second;
  651. ++it_archived) {
  652. if ((*it_archived)->getTransid() ==
  653. rcvd_packet->getTransid()) {
  654. boost::shared_ptr<T> sent_packet = *it_archived;
  655. // Get sent and received packet times.
  656. ptime sent_time = sent_packet->getTimestamp();
  657. ptime rcvd_time = rcvd_packet->getTimestamp();
  658. // All sent and received packets should have timestamps
  659. // set but if there is a bug somewhere and packet does
  660. // not have timestamp we want to catch this here.
  661. if (sent_time.is_not_a_date_time() ||
  662. rcvd_time.is_not_a_date_time()) {
  663. isc_throw(InvalidOperation,
  664. "packet time is not set");
  665. }
  666. // Calculate durations of packets from beginning of epoch.
  667. time_period sent_period(boot_time_, sent_time);
  668. time_period rcvd_period(boot_time_, rcvd_time);
  669. // Print timestamps for sent and received packet.
  670. std::cout << "sent / received: "
  671. << to_iso_string(sent_period.length())
  672. << " / "
  673. << to_iso_string(rcvd_period.length())
  674. << std::endl;
  675. break;
  676. }
  677. }
  678. }
  679. }
  680. private:
  681. /// \brief Private default constructor.
  682. ///
  683. /// Default constructor is private because we want the client
  684. /// class to specify exchange type explicitly.
  685. ExchangeStats();
  686. /// \brief Erase packet from the list of sent packets.
  687. ///
  688. /// Method erases packet from the list of sent packets.
  689. ///
  690. /// \param it iterator pointing to packet to be erased.
  691. /// \return iterator pointing to packet following erased
  692. /// packet or sent_packets_.end() if packet not found.
  693. PktListIterator eraseSent(const PktListIterator it) {
  694. if (archive_enabled_) {
  695. // We don't want to keep list of all sent packets
  696. // because it will affect packet lookup performance.
  697. // If packet is matched with received packet we
  698. // move it to list of archived packets. List of
  699. // archived packets may be used for diagnostics
  700. // when test is completed.
  701. archived_packets_.push_back(*it);
  702. }
  703. // get<0>() template returns sequencial index to
  704. // container.
  705. return(sent_packets_.template get<0>().erase(it));
  706. }
  707. ExchangeType xchg_type_; ///< Packet exchange type.
  708. PktList sent_packets_; ///< List of sent packets.
  709. /// Iterator pointing to the packet on sent list which will most
  710. /// likely match next received packet. This is based on the
  711. /// assumption that server responds in order to incoming packets.
  712. PktListIterator next_sent_;
  713. PktList rcvd_packets_; ///< List of received packets.
  714. /// List of archived packets. All sent packets that have
  715. /// been matched with received packet are moved to this
  716. /// list for diagnostics purposes.
  717. PktList archived_packets_;
  718. /// Indicates all packets have to be preserved after matching.
  719. /// By default this is disabled which means that when received
  720. /// packet is matched with sent packet both are deleted. This
  721. /// is important when test is executed for extended period of
  722. /// time and high memory usage might be the issue.
  723. /// When timestamps listing is specified from the command line
  724. /// (using diagnostics selector), all packets have to be preserved
  725. /// so as the printing method may read their timestamps and
  726. /// print it to user. In such usage model it will be rare to
  727. /// run test for extended period of time so it should be fine
  728. /// to keep all packets archived throughout the test.
  729. bool archive_enabled_;
  730. /// Maxmimum time elapsed between sending and receiving packet
  731. /// before packet is assumed dropped.
  732. double drop_time_;
  733. double min_delay_; ///< Minimum delay between sent
  734. ///< and received packets.
  735. double max_delay_; ///< Maximum delay between sent
  736. ///< and received packets.
  737. double sum_delay_; ///< Sum of delays between sent
  738. ///< and received packets.
  739. double sum_delay_squared_; ///< Squared sum of delays between
  740. ///< sent and recived packets.
  741. uint64_t orphans_; ///< Number of orphant received packets.
  742. uint64_t collected_; ///< Number of garbage collected packets.
  743. /// Sum of unordered lookup sets. Needed to calculate mean size of
  744. /// lookup set. It is desired that number of unordered lookups is
  745. /// minimal for performance reasons. Tracking number of lookups and
  746. /// mean size of the lookup set should give idea of packets serach
  747. /// complexity.
  748. uint64_t unordered_lookup_size_sum_;
  749. uint64_t unordered_lookups_; ///< Number of unordered sent packets
  750. ///< lookups.
  751. uint64_t ordered_lookups_; ///< Number of ordered sent packets
  752. ///< lookups.
  753. uint64_t sent_packets_num_; ///< Total number of sent packets.
  754. uint64_t rcvd_packets_num_; ///< Total number of received packets.
  755. boost::posix_time::ptime boot_time_; ///< Time when test is started.
  756. };
  757. /// Pointer to ExchangeStats.
  758. typedef boost::shared_ptr<ExchangeStats> ExchangeStatsPtr;
  759. /// Map containing all specified exchange types.
  760. typedef typename std::map<ExchangeType, ExchangeStatsPtr> ExchangesMap;
  761. /// Iterator poiting to \ref ExchangesMap
  762. typedef typename ExchangesMap::const_iterator ExchangesMapIterator;
  763. /// Map containing custom counters.
  764. typedef typename std::map<std::string, CustomCounterPtr> CustomCountersMap;
  765. /// Iterator for \ref CustomCountersMap.
  766. typedef typename CustomCountersMap::const_iterator CustomCountersMapIterator;
  767. /// \brief Constructor.
  768. ///
  769. /// This constructor by default disables packets archiving mode.
  770. /// In this mode all packets from the list of sent packets are
  771. /// moved to list of archived packets once they have been matched
  772. /// with received packets. This is required if it has been selected
  773. /// from the command line to print timestamps for all packets after
  774. /// the test. If this is not selected archiving should be disabled
  775. /// for performance reasons and to avoid waste of memory for storing
  776. /// large list of archived packets.
  777. ///
  778. /// \param archive_enabled true indicates that packets
  779. /// archive mode is enabled.
  780. StatsMgr(const bool archive_enabled = false) :
  781. exchanges_(),
  782. archive_enabled_(archive_enabled),
  783. boot_time_(boost::posix_time::microsec_clock::universal_time()) {
  784. }
  785. /// \brief Specify new exchange type.
  786. ///
  787. /// This method creates new \ref ExchangeStats object that will
  788. /// collect statistics data from packets exchange of the specified
  789. /// type.
  790. ///
  791. /// \param xchg_type exchange type.
  792. /// \param drop_time maximum time elapsed before packet is
  793. /// assumed dropped. Negative value disables it.
  794. /// \throw isc::BadValue if exchange of specified type exists.
  795. void addExchangeStats(const ExchangeType xchg_type,
  796. const double drop_time = -1) {
  797. if (exchanges_.find(xchg_type) != exchanges_.end()) {
  798. isc_throw(BadValue, "Exchange of specified type already added.");
  799. }
  800. exchanges_[xchg_type] =
  801. ExchangeStatsPtr(new ExchangeStats(xchg_type,
  802. drop_time,
  803. archive_enabled_,
  804. boot_time_));
  805. }
  806. /// \brief Check if the exchange type has been specified.
  807. ///
  808. /// This method checks if the \ref ExchangeStats object of a particular type
  809. /// exists (has been added using \ref addExchangeStats function).
  810. ///
  811. /// \param xchg_type A type of the exchange being repersented by the
  812. /// \ref ExchangeStats object.
  813. ///
  814. /// \return true if the \ref ExchangeStats object has been added for a
  815. /// specified exchange type.
  816. bool hasExchangeStats(const ExchangeType xchg_type) const {
  817. return (exchanges_.find(xchg_type) != exchanges_.end());
  818. }
  819. /// \brief Add named custom uint64 counter.
  820. ///
  821. /// Method creates new named counter and stores in counter's map under
  822. /// key specified here as short_name.
  823. ///
  824. /// \param short_name key to use to access counter in the map.
  825. /// \param long_name name of the counter presented in the log file.
  826. void addCustomCounter(const std::string& short_name,
  827. const std::string& long_name) {
  828. if (custom_counters_.find(short_name) != custom_counters_.end()) {
  829. isc_throw(BadValue,
  830. "Custom counter " << short_name << " already added.");
  831. }
  832. custom_counters_[short_name] =
  833. CustomCounterPtr(new CustomCounter(long_name));
  834. }
  835. /// \brief Check if any packet drops occured.
  836. ///
  837. // \return true, if packet drops occured.
  838. bool droppedPackets() const {
  839. for (ExchangesMapIterator it = exchanges_.begin();
  840. it != exchanges_.end();
  841. ++it) {
  842. if (it->second->getDroppedPacketsNum() > 0) {
  843. return (true);
  844. }
  845. }
  846. return (false);
  847. }
  848. /// \brief Return specified counter.
  849. ///
  850. /// Method returns specified counter.
  851. ///
  852. /// \param counter_key key poiting to the counter in the counters map.
  853. /// The short counter name has to be used to access counter.
  854. /// \return pointer to specified counter object.
  855. CustomCounterPtr getCounter(const std::string& counter_key) {
  856. CustomCountersMapIterator it = custom_counters_.find(counter_key);
  857. if (it == custom_counters_.end()) {
  858. isc_throw(BadValue,
  859. "Custom counter " << counter_key << "does not exist");
  860. }
  861. return(it->second);
  862. }
  863. /// \brief Increment specified counter.
  864. ///
  865. /// Increment counter value by one.
  866. ///
  867. /// \param counter_key key poiting to the counter in the counters map.
  868. /// \param value value to increment counter by.
  869. /// \return pointer to specified counter after incrementation.
  870. const CustomCounter& incrementCounter(const std::string& counter_key,
  871. const uint64_t value = 1) {
  872. CustomCounterPtr counter = getCounter(counter_key);
  873. *counter += value;
  874. return (*counter);
  875. }
  876. /// \brief Adds new packet to the sent packets list.
  877. ///
  878. /// Method adds new packet to the sent packets list.
  879. /// Packets are added to the list sequentially and
  880. /// most often read sequentially.
  881. ///
  882. /// \param xchg_type exchange type.
  883. /// \param packet packet to be added to the list
  884. /// \throw isc::BadValue if invalid exchange type specified or
  885. /// packet is null.
  886. void passSentPacket(const ExchangeType xchg_type,
  887. const boost::shared_ptr<T>& packet) {
  888. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  889. xchg_stats->appendSent(packet);
  890. }
  891. /// \brief Add new received packet and match with sent packet.
  892. ///
  893. /// Method adds new packet to the list of received packets. It
  894. /// also searches for corresponding packet on the list of sent
  895. /// packets. When packets are matched the statistics counters
  896. /// are updated accordingly for the particular exchange type.
  897. ///
  898. /// \param xchg_type exchange type.
  899. /// \param packet received packet
  900. /// \throw isc::BadValue if invalid exchange type specified
  901. /// or packet is null.
  902. /// \throw isc::Unexpected if corresponding packet was not
  903. /// found on the list of sent packets.
  904. boost::shared_ptr<T>
  905. passRcvdPacket(const ExchangeType xchg_type,
  906. const boost::shared_ptr<T>& packet) {
  907. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  908. boost::shared_ptr<T> sent_packet
  909. = xchg_stats->matchPackets(packet);
  910. if (sent_packet) {
  911. xchg_stats->updateDelays(sent_packet, packet);
  912. if (archive_enabled_) {
  913. xchg_stats->appendRcvd(packet);
  914. }
  915. }
  916. return(sent_packet);
  917. }
  918. /// \brief Return minumum delay between sent and received packet.
  919. ///
  920. /// Method returns minimum delay between sent and received packet
  921. /// for specified exchange type.
  922. ///
  923. /// \param xchg_type exchange type.
  924. /// \throw isc::BadValue if invalid exchange type specified.
  925. /// \return minimum delay between packets.
  926. double getMinDelay(const ExchangeType xchg_type) const {
  927. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  928. return(xchg_stats->getMinDelay());
  929. }
  930. /// \brief Return maxmimum delay between sent and received packet.
  931. ///
  932. /// Method returns maximum delay between sent and received packet
  933. /// for specified exchange type.
  934. ///
  935. /// \param xchg_type exchange type.
  936. /// \throw isc::BadValue if invalid exchange type specified.
  937. /// \return maximum delay between packets.
  938. double getMaxDelay(const ExchangeType xchg_type) const {
  939. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  940. return(xchg_stats->getMaxDelay());
  941. }
  942. /// \brief Return average packet delay.
  943. ///
  944. /// Method returns average packet delay for specified
  945. /// exchange type.
  946. ///
  947. /// \return average packet delay.
  948. double getAvgDelay(const ExchangeType xchg_type) const {
  949. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  950. return(xchg_stats->getAvgDelay());
  951. }
  952. /// \brief Return standard deviation of packet delay.
  953. ///
  954. /// Method returns standard deviation of packet delay
  955. /// for specified exchange type.
  956. ///
  957. /// \return standard deviation of packet delay.
  958. double getStdDevDelay(const ExchangeType xchg_type) const {
  959. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  960. return(xchg_stats->getStdDevDelay());
  961. }
  962. /// \brief Return number of orphant packets.
  963. ///
  964. /// Method returns number of orphant packets for specified
  965. /// exchange type.
  966. ///
  967. /// \param xchg_type exchange type.
  968. /// \throw isc::BadValue if invalid exchange type specified.
  969. /// \return number of orphant packets so far.
  970. uint64_t getOrphans(const ExchangeType xchg_type) const {
  971. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  972. return(xchg_stats->getOrphans());
  973. }
  974. /// \brief Return average unordered lookup set size.
  975. ///
  976. /// Method returns average unordered lookup set size.
  977. /// This value changes every time \ref ExchangeStats::matchPackets
  978. /// function performs unordered packet lookup.
  979. ///
  980. /// \param xchg_type exchange type.
  981. /// \throw isc::BadValue if invalid exchange type specified.
  982. /// \return average unordered lookup set size.
  983. double getAvgUnorderedLookupSetSize(const ExchangeType xchg_type) const {
  984. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  985. return(xchg_stats->getAvgUnorderedLookupSetSize());
  986. }
  987. /// \brief Return number of unordered sent packets lookups
  988. ///
  989. /// Method returns number of unordered sent packet lookups.
  990. /// Unordered lookup is used when received packet was sent
  991. /// out of order by server - transaction id of received
  992. /// packet does not match transaction id of next sent packet.
  993. ///
  994. /// \param xchg_type exchange type.
  995. /// \throw isc::BadValue if invalid exchange type specified.
  996. /// \return number of unordered lookups.
  997. uint64_t getUnorderedLookups(const ExchangeType xchg_type) const {
  998. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  999. return(xchg_stats->getUnorderedLookups());
  1000. }
  1001. /// \brief Return number of ordered sent packets lookups
  1002. ///
  1003. /// Method returns number of ordered sent packet lookups.
  1004. /// Ordered lookup is used when packets are received in the
  1005. /// same order as they were sent to the server.
  1006. /// If packets are skipped or received out of order, lookup
  1007. /// function will use unordered lookup (with hash table).
  1008. ///
  1009. /// \param xchg_type exchange type.
  1010. /// \throw isc::BadValue if invalid exchange type specified.
  1011. /// \return number of ordered lookups.
  1012. uint64_t getOrderedLookups(const ExchangeType xchg_type) const {
  1013. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  1014. return(xchg_stats->getOrderedLookups());
  1015. }
  1016. /// \brief Return total number of sent packets
  1017. ///
  1018. /// Method returns total number of sent packets for specified
  1019. /// exchange type.
  1020. ///
  1021. /// \param xchg_type exchange type.
  1022. /// \throw isc::BadValue if invalid exchange type specified.
  1023. /// \return number of sent packets.
  1024. uint64_t getSentPacketsNum(const ExchangeType xchg_type) const {
  1025. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  1026. return(xchg_stats->getSentPacketsNum());
  1027. }
  1028. /// \brief Return total number of received packets
  1029. ///
  1030. /// Method returns total number of received packets for specified
  1031. /// exchange type.
  1032. ///
  1033. /// \param xchg_type exchange type.
  1034. /// \throw isc::BadValue if invalid exchange type specified.
  1035. /// \return number of received packets.
  1036. uint64_t getRcvdPacketsNum(const ExchangeType xchg_type) const {
  1037. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  1038. return(xchg_stats->getRcvdPacketsNum());
  1039. }
  1040. /// \brief Return total number of dropped packets.
  1041. ///
  1042. /// Method returns total number of dropped packets for specified
  1043. /// exchange type.
  1044. ///
  1045. /// \param xchg_type exchange type.
  1046. /// \throw isc::BadValue if invalid exchange type specified.
  1047. /// \return number of dropped packets.
  1048. uint64_t getDroppedPacketsNum(const ExchangeType xchg_type) const {
  1049. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  1050. return(xchg_stats->getDroppedPacketsNum());
  1051. }
  1052. /// \brief Return number of garbage collected packets.
  1053. ///
  1054. /// Method returns number of garbage collected timed out
  1055. /// packets. Packet is assumed timed out when duration
  1056. /// between sending it to server and receiving server's
  1057. /// response is greater than value specified with -d<value>
  1058. /// command line argument.
  1059. ///
  1060. /// \throw isc::BadValue if invalid exchange type specified.
  1061. /// \return number of garbage collected packets.
  1062. uint64_t getCollectedNum(const ExchangeType xchg_type) const {
  1063. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  1064. return(xchg_stats->getCollectedNum());
  1065. }
  1066. /// \brief Get time period since the start of test.
  1067. ///
  1068. /// Calculate dna return period since the test start. This
  1069. /// can be specifically helpful when calculating packet
  1070. /// exchange rates.
  1071. ///
  1072. /// \return test period so far.
  1073. boost::posix_time::time_period getTestPeriod() const {
  1074. using namespace boost::posix_time;
  1075. time_period test_period(boot_time_,
  1076. microsec_clock::universal_time());
  1077. return test_period;
  1078. }
  1079. /// \brief Return name of the exchange.
  1080. ///
  1081. /// Method returns name of the specified exchange type.
  1082. /// This function is mainly for logging purposes.
  1083. ///
  1084. /// \param xchg_type exchange type.
  1085. /// \return string representing name of the exchange.
  1086. std::string exchangeToString(ExchangeType xchg_type) const {
  1087. switch(xchg_type) {
  1088. case XCHG_DO:
  1089. return("DISCOVER-OFFER");
  1090. case XCHG_RA:
  1091. return("REQUEST-ACK");
  1092. case XCHG_SA:
  1093. return("SOLICIT-ADVERTISE");
  1094. case XCHG_RR:
  1095. return("REQUEST-REPLY");
  1096. case XCHG_RN:
  1097. return("RENEW-REPLY");
  1098. case XCHG_RL:
  1099. return("RELEASE-REPLY");
  1100. default:
  1101. return("Unknown exchange type");
  1102. }
  1103. }
  1104. /// \brief Print statistics counters for all exchange types.
  1105. ///
  1106. /// Method prints statistics for all exchange types.
  1107. /// Statistics includes:
  1108. /// - number of sent and received packets
  1109. /// - number of dropped packets and number of orphans
  1110. /// - minimum packets delay,
  1111. /// - average packets delay,
  1112. /// - maximum packets delay,
  1113. /// - standard deviation of packets delay.
  1114. ///
  1115. /// \throw isc::InvalidOperation if no exchange type added to
  1116. /// track statistics.
  1117. void printStats() const {
  1118. if (exchanges_.empty()) {
  1119. isc_throw(isc::InvalidOperation,
  1120. "no exchange type added for tracking");
  1121. }
  1122. for (ExchangesMapIterator it = exchanges_.begin();
  1123. it != exchanges_.end();
  1124. ++it) {
  1125. ExchangeStatsPtr xchg_stats = it->second;
  1126. std::cout << "***Statistics for: " << exchangeToString(it->first)
  1127. << "***" << std::endl;
  1128. xchg_stats->printMainStats();
  1129. std::cout << std::endl;
  1130. xchg_stats->printRTTStats();
  1131. std::cout << std::endl;
  1132. }
  1133. }
  1134. /// \brief Print intermediate statistics.
  1135. ///
  1136. /// Method prints intermediate statistics for all exchanges.
  1137. /// Statistics includes sent, received and dropped packets
  1138. /// counters.
  1139. void printIntermediateStats() const {
  1140. std::ostringstream stream_sent;
  1141. std::ostringstream stream_rcvd;
  1142. std::ostringstream stream_drops;
  1143. std::string sep("");
  1144. for (ExchangesMapIterator it = exchanges_.begin();
  1145. it != exchanges_.end(); ++it) {
  1146. if (it != exchanges_.begin()) {
  1147. sep = "/";
  1148. }
  1149. stream_sent << sep << it->second->getSentPacketsNum();
  1150. stream_rcvd << sep << it->second->getRcvdPacketsNum();
  1151. stream_drops << sep << it->second->getDroppedPacketsNum();
  1152. }
  1153. std::cout << "sent: " << stream_sent.str()
  1154. << "; received: " << stream_rcvd.str()
  1155. << "; drops: " << stream_drops.str()
  1156. << std::endl;
  1157. }
  1158. /// \brief Print timestamps of all packets.
  1159. ///
  1160. /// Method prints timestamps of all sent and received
  1161. /// packets for all defined exchange types.
  1162. ///
  1163. /// \throw isc::InvalidOperation if one of the packets has
  1164. /// no timestamp value set or if packets archive mode is
  1165. /// disabled.
  1166. ///
  1167. /// \throw isc::InvalidOperation if no exchange type added to
  1168. /// track statistics or packets archive mode is disabled.
  1169. void printTimestamps() const {
  1170. if (exchanges_.empty()) {
  1171. isc_throw(isc::InvalidOperation,
  1172. "no exchange type added for tracking");
  1173. }
  1174. for (ExchangesMapIterator it = exchanges_.begin();
  1175. it != exchanges_.end();
  1176. ++it) {
  1177. ExchangeStatsPtr xchg_stats = it->second;
  1178. std::cout << "***Timestamps for packets: "
  1179. << exchangeToString(it->first)
  1180. << "***" << std::endl;
  1181. xchg_stats->printTimestamps();
  1182. std::cout << std::endl;
  1183. }
  1184. }
  1185. /// \brief Print names and values of custom counters.
  1186. ///
  1187. /// Method prints names and values of custom counters. Custom counters
  1188. /// are defined by client class for tracking different statistics.
  1189. ///
  1190. /// \throw isc::InvalidOperation if no custom counters added for tracking.
  1191. void printCustomCounters() const {
  1192. if (custom_counters_.empty()) {
  1193. isc_throw(isc::InvalidOperation, "no custom counters specified");
  1194. }
  1195. for (CustomCountersMapIterator it = custom_counters_.begin();
  1196. it != custom_counters_.end();
  1197. ++it) {
  1198. CustomCounterPtr counter = it->second;
  1199. std::cout << counter->getName() << ": " << counter->getValue()
  1200. << std::endl;
  1201. }
  1202. }
  1203. private:
  1204. /// \brief Return exchange stats object for given exchange type
  1205. ///
  1206. /// Method returns exchange stats object for given exchange type.
  1207. ///
  1208. /// \param xchg_type exchange type.
  1209. /// \throw isc::BadValue if invalid exchange type specified.
  1210. /// \return exchange stats object.
  1211. ExchangeStatsPtr getExchangeStats(const ExchangeType xchg_type) const {
  1212. ExchangesMapIterator it = exchanges_.find(xchg_type);
  1213. if (it == exchanges_.end()) {
  1214. isc_throw(BadValue, "Packets exchange not specified");
  1215. }
  1216. ExchangeStatsPtr xchg_stats = it->second;
  1217. return(xchg_stats);
  1218. }
  1219. ExchangesMap exchanges_; ///< Map of exchange types.
  1220. CustomCountersMap custom_counters_; ///< Map with custom counters.
  1221. /// Indicates that packets from list of sent packets should be
  1222. /// archived (moved to list of archived packets) once they are
  1223. /// matched with received packets. This is required when it has
  1224. /// been selected from the command line to print packets'
  1225. /// timestamps after test. This may affect performance and
  1226. /// consume large amount of memory when the test is running
  1227. /// for extended period of time and many packets have to be
  1228. /// archived.
  1229. bool archive_enabled_;
  1230. boost::posix_time::ptime boot_time_; ///< Time when test is started.
  1231. };
  1232. } // namespace perfdhcp
  1233. } // namespace isc
  1234. #endif // STATS_MGR_H