stats_mgr.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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/date_time/posix_time/posix_time.hpp>
  25. #include <exceptions/exceptions.h>
  26. namespace isc {
  27. namespace perfdhcp {
  28. /// \brief Statistics Manager
  29. ///
  30. /// This class template is a storage for various performance statistics
  31. /// collected during performance tests execution with perfdhcp tool.
  32. ///
  33. /// Statistics Manager holds lists of sent and received packets and
  34. /// groups them into exchanges. For example: DHCPDISCOVER message and
  35. /// corresponding DHCPOFFER messages belong to one exchange, DHCPREQUEST
  36. /// and corresponding DHCPACK message belong to another exchange etc.
  37. /// In order to update statistics for a particular exchange type, client
  38. /// class passes sent and received packets. Internally, Statistics Manager
  39. /// tries to match transaction id of received packet with sent packet
  40. /// stored on the list of sent packets. When packets are matched the
  41. /// round trip time can be calculated.
  42. ///
  43. /// \tparam T class representing DHCPv4 or DHCPv6 packet.
  44. template <class T>
  45. class StatsMgr : public boost::noncopyable {
  46. public:
  47. /// \brief Custom Counter
  48. ///
  49. /// This class represents custom statistics counters. Client class
  50. /// may create unlimited number of counters. Such counters are
  51. /// being stored in map in Statistics Manager and access using
  52. /// unique string key.
  53. class CustomCounter {
  54. public:
  55. /// \brief Constructor.
  56. ///
  57. /// This constructor sets counter name. This name is used in
  58. /// log file to report value of each counter.
  59. ///
  60. /// \param name name of the counter used in log file.
  61. CustomCounter(const std::string& name) :
  62. counter_(0),
  63. name_(name) { };
  64. /// \brief Increment operator.
  65. const CustomCounter& operator++() {
  66. ++counter_;
  67. return *this;
  68. }
  69. /// \brief Increment operator.
  70. const CustomCounter& operator++(int) {
  71. CustomCounter& this_counter(*this);
  72. operator++();
  73. return this_counter;
  74. }
  75. /// \brief Return counter value.
  76. ///
  77. /// Method returns counter value.
  78. ///
  79. /// \return counter value.
  80. uint64_t getValue() const {
  81. return counter_;
  82. }
  83. /// \brief Return counter name.
  84. ///
  85. /// Method returns counter name.
  86. ///
  87. /// \return counter name.
  88. const std::string& getName() const {
  89. return name_;
  90. }
  91. private:
  92. /// \brief Default constructor.
  93. ///
  94. /// Default constrcutor is private because we don't want client
  95. /// class to call it because we want client class to specify
  96. /// counter's name.
  97. CustomCounter() { };
  98. uint64_t counter_; ///< Counter's value.
  99. std::string name_; ///< Counter's name.
  100. };
  101. typedef typename boost::shared_ptr<CustomCounter> CustomCounterPtr;
  102. /// DHCP packet exchange types.
  103. enum ExchangeType {
  104. XCHG_DO, ///< DHCPv4 DISCOVER-OFFER
  105. XCHG_RA, ///< DHCPv4 REQUEST-ACK
  106. XCHG_SA, ///< DHCPv6 SOLICIT-ADVERTISE
  107. XCHG_RR ///< DHCPv6 REQUEST-REPLY
  108. };
  109. /// \brief Exchange Statistics.
  110. ///
  111. /// This class collects statistics for exchanges. Parent class
  112. /// may define number of different packet exchanges like:
  113. /// DHCPv4 DISCOVER-OFFER, DHCPv6 SOLICIT-ADVERTISE etc. Performance
  114. /// statistics will be collected for each of those separately in
  115. /// corresponding instance of ExchangeStats.
  116. class ExchangeStats {
  117. public:
  118. static uint32_t transid_hash(const boost::shared_ptr<T> packet) {
  119. return packet->getTransid() & 1023;
  120. }
  121. /// \brief List of packets (sent or received).
  122. ///
  123. /// List of packets based on multi index container allows efficient
  124. /// search of packets based on their sequence (order in which they
  125. /// were inserted) as well as based on packet transaction id.
  126. typedef boost::multi_index_container<
  127. boost::shared_ptr<T>,
  128. boost::multi_index::indexed_by<
  129. boost::multi_index::sequenced<>,
  130. boost::multi_index::hashed_non_unique<
  131. boost::multi_index::global_fun<
  132. boost::shared_ptr<T>,
  133. uint32_t,
  134. &ExchangeStats::transid_hash
  135. >
  136. >
  137. >
  138. > PktList;
  139. /// Packet list iterator for sequencial access to elements.
  140. typedef typename PktList::const_iterator PktListIterator;
  141. /// Packet list index to search packets using transaction id.
  142. typedef typename PktList::template nth_index<1>::type
  143. PktListTransidIndex;
  144. /// Packet list iterator to access packets using transaction id.
  145. typedef typename PktListTransidIndex::const_iterator PktListTransidIterator;
  146. /// \brief Constructor
  147. ///
  148. /// \param xchg_type exchange type
  149. ExchangeStats(const ExchangeType xchg_type)
  150. : xchg_type_(xchg_type),
  151. min_delay_(std::numeric_limits<double>::max()),
  152. max_delay_(0.),
  153. sum_delay_(0.),
  154. orphans_(0),
  155. square_sum_delay_(0.),
  156. ordered_lookups_(0),
  157. unordered_lookup_size_sum_(0),
  158. unordered_lookups_(0),
  159. sent_packets_num_(0),
  160. rcvd_packets_num_(0) {
  161. sent_packets_cache_ = sent_packets_.begin();
  162. }
  163. /// \brief Add new packet to list of sent packets.
  164. ///
  165. /// Method adds new packet to list of sent packets.
  166. ///
  167. /// \param packet packet object to be added.
  168. void appendSent(const boost::shared_ptr<T> packet) {
  169. ++sent_packets_num_;
  170. sent_packets_.template get<0>().push_back(packet);
  171. }
  172. /// \brief Add new packet to list of received packets.
  173. ///
  174. /// Method adds new packet to list of received packets.
  175. ///
  176. /// \param packet packet object to be added.
  177. void appendRcvd(const boost::shared_ptr<T> packet) {
  178. ++rcvd_packets_num_;
  179. rcvd_packets_.template get<0>().push_back(packet);
  180. }
  181. /// \brief Update delay counters.
  182. ///
  183. /// Method updates delay counters based on timestamps of
  184. /// sent and received packets.
  185. ///
  186. /// \param sent_packet sent packet
  187. /// \param rcvd_packet received packet
  188. /// \throw isc::Unexpected if failed to calculate timestamps
  189. void updateDelays(const boost::shared_ptr<const T> sent_packet,
  190. const boost::shared_ptr<const T> rcvd_packet) {
  191. boost::posix_time::ptime sent_time = sent_packet->getTimestamp();
  192. boost::posix_time::ptime rcvd_time = rcvd_packet->getTimestamp();
  193. if (sent_time.is_not_a_date_time() ||
  194. rcvd_time.is_not_a_date_time()) {
  195. isc_throw(Unexpected,
  196. "Timestamp must be set for sent and "
  197. "received packet to measure RTT");
  198. }
  199. boost::posix_time::time_period period(sent_time, rcvd_time);
  200. // We don't bother calculating deltas in nanoseconds. It is much
  201. // more convenient to use seconds instead because we are going to
  202. // sum them up.
  203. double delta =
  204. static_cast<double>(period.length().total_nanoseconds()) / 1e9;
  205. if (delta < 0) {
  206. isc_throw(Unexpected, "Sent packet's timestamp must not be "
  207. "greater than received packet's timestamp");
  208. }
  209. // Record the minimum delay between sent and received packets.
  210. if (delta < min_delay_) {
  211. min_delay_ = delta;
  212. }
  213. // Record the maximum delay between sent and received packets.
  214. if (delta > max_delay_) {
  215. max_delay_ = delta;
  216. }
  217. // Update delay sum and square sum. That will be used to calculate
  218. // mean delays.
  219. sum_delay_ += delta;
  220. square_sum_delay_ += delta * delta;
  221. }
  222. /// \brief Find packet on the list of sent packets.
  223. ///
  224. /// Method finds packet with specified transaction id on the list
  225. /// of sent packets. It is used to match received packet with
  226. /// corresponding sent packet.
  227. /// Since packets from the server most often come in the same order
  228. /// as they were sent by client, this method will first check if
  229. /// next sent packet matches. If it doesn't, function will search
  230. /// the packet using indexing by transaction id. This reduces
  231. /// packet search time significantly.
  232. ///
  233. /// \param transid transaction id of the packet to search
  234. /// \return packet having specified transaction or NULL if packet
  235. /// not found
  236. boost::shared_ptr<T> findSent(const uint32_t transid) {
  237. if (sent_packets_.size() == 0) {
  238. // List of sent packets is empty so there is no sense
  239. // to continue looking fo the packet. It also means
  240. // that the received packet we got has no corresponding
  241. // sent packet so orphans counter has to be updated.
  242. ++orphans_;
  243. return boost::shared_ptr<T>();
  244. } else if (sent_packets_cache_ == sent_packets_.end()) {
  245. // Even if there are still many unmatched packets on the
  246. // list we might hit the end of it because of unordered
  247. // lookups. The next logical step is to reset cache.
  248. sent_packets_cache_ = sent_packets_.begin();
  249. }
  250. // With this variable we will be signalling success or failure
  251. // to find the packet.
  252. bool packet_found = false;
  253. // Most likely responses are sent from the server in the same
  254. // order as client's requests to the server. We are caching
  255. // next sent packet and first try to match it with the next
  256. // incoming packet. We are successful if there is no
  257. // packet drop or out of order packets sent. This is actually
  258. // the fastest way to look for packets.
  259. if ((*sent_packets_cache_)->getTransid() == transid) {
  260. ++ordered_lookups_;
  261. packet_found = true;
  262. } else {
  263. // If we are here, it means that we were unable to match the
  264. // next incoming packet with next sent packet so we need to
  265. // take a little more expensive approach to look packets using
  266. // alternative index (transaction id & 1023).
  267. PktListTransidIndex& idx = sent_packets_.template get<1>();
  268. // Packets are grouped using trasaction id masked with value
  269. // of 1023. For instance, packets with transaction id equal to
  270. // 1, 1024 ... will belong to the same group (a.k.a. bucket).
  271. // When using alternative index we don't find the packet but
  272. // bucket of packets and we need to iterate through the bucket
  273. // to find the one that has desired transaction id.
  274. std::pair<PktListTransidIterator,PktListTransidIterator> p =
  275. idx.equal_range(transid & 1023);
  276. // We want to keep statistics of unordered lookups to make
  277. // sure that there is a right balance between number of
  278. // unordered lookups and ordered lookups. If number of unordered
  279. // lookups is high it may mean that many packets are lost or
  280. // sent out of order.
  281. ++unordered_lookups_;
  282. // We also want to keep the mean value of the bucket. The lower
  283. // bucket size the better. If bucket sizes appear to big we
  284. // might want to increase number of buckets.
  285. unordered_lookup_size_sum_ += std::distance(p.first, p.second);
  286. for (PktListTransidIterator it = p.first; it != p.second;
  287. ++it) {
  288. if ((*it)->getTransid() == transid) {
  289. packet_found = true;
  290. sent_packets_cache_ =
  291. sent_packets_.template project<0>(it);
  292. break;
  293. }
  294. }
  295. }
  296. if (!packet_found) {
  297. // If we are here, it means that both ordered lookup and
  298. // unordered lookup failed. Searched packet is not on the list.
  299. ++orphans_;
  300. return boost::shared_ptr<T>();
  301. }
  302. boost::shared_ptr<T> sent_packet(*sent_packets_cache_);
  303. // If packet was found, we assume it will be never searched
  304. // again. We want to delete this packet from the list to
  305. // improve performance of future searches.
  306. sent_packets_cache_ = eraseSent(sent_packets_cache_);
  307. return sent_packet;
  308. }
  309. /// \brief Return minumum delay between sent and received packet.
  310. ///
  311. /// Method returns minimum delay between sent and received packet.
  312. ///
  313. /// \return minimum delay between packets.
  314. double getMinDelay() const { return min_delay_; }
  315. /// \brief Return maxmimum delay between sent and received packet.
  316. ///
  317. /// Method returns maximum delay between sent and received packet.
  318. ///
  319. /// \return maximum delay between packets.
  320. double getMaxDelay() const { return max_delay_; }
  321. /// \brief Return sum of delays between sent and received packets.
  322. ///
  323. /// Method returns sum of delays between sent and received packets.
  324. ///
  325. /// \return sum of delays between sent and received packets.
  326. double getSumDelay() const { return sum_delay_; }
  327. /// \brief Return square sum of delays between sent and received
  328. /// packets.
  329. ///
  330. /// Method returns square sum of delays between sent and received
  331. /// packets.
  332. ///
  333. /// \return square sum of delays between sent and received packets.
  334. double getSquareSumDelay() const { return square_sum_delay_; }
  335. /// \brief Return number of orphant packets.
  336. ///
  337. /// Method returns number of received packets that had no matching
  338. /// sent packet. It is possible that such packet was late or not
  339. /// for us.
  340. ///
  341. /// \return number of orphant received packets.
  342. uint64_t getOrphans() const { return orphans_; }
  343. /// \brief Return average unordered lookup set size.
  344. ///
  345. /// Method returns average unordered lookup set size.
  346. /// This value is changes every time \findSet function uses
  347. /// unordered packet lookup using transaction id.
  348. ///
  349. /// \return average unordered lookup set size.
  350. double getAvgUnorderedLookupSetSize() const {
  351. if (unordered_lookups_ == 0) {
  352. return 0.;
  353. }
  354. return static_cast<double>(unordered_lookup_size_sum_) /
  355. static_cast<double>(unordered_lookups_);
  356. }
  357. /// \brief Return number of unordered sent packets lookups
  358. ///
  359. /// Method returns number of unordered sent packet lookups.
  360. /// Unordered lookup is used when received packet was sent
  361. /// out of order by server - transaction id of received
  362. /// packet does not match transaction id of next sent packet.
  363. ///
  364. /// \return number of unordered lookups.
  365. uint64_t getUnorderedLookups() const { return unordered_lookups_; }
  366. /// \brief Return number of ordered sent packets lookups
  367. ///
  368. /// Method returns number of ordered sent packet lookups.
  369. /// Ordered lookup is used when packets are received in the
  370. /// same order as they were sent to the server.
  371. /// If packets are skipped or received out of order, lookup
  372. /// function will use unordered lookup (with hash table).
  373. ///
  374. /// \return number of ordered lookups.
  375. uint64_t getOrderedLookups() const { return ordered_lookups_; }
  376. /// \brief Return total number of sent packets
  377. ///
  378. /// Method returns total number of sent packets.
  379. ///
  380. /// \return number of sent packets.
  381. uint64_t getSentPacketsNum() const {
  382. return sent_packets_num_;
  383. }
  384. /// \brief Return total number of received packets
  385. ///
  386. /// Method returns total number of received packets.
  387. ///
  388. /// \return number of received packets.
  389. uint64_t getRcvdPacketsNum() const {
  390. return rcvd_packets_num_;
  391. }
  392. private:
  393. /// \brief Private default constructor.
  394. ///
  395. /// Default constructor is private because we want the client
  396. /// class to specify exchange type explicitely.
  397. ExchangeStats();
  398. /// \brief Erase packet from the list of sent packets.
  399. ///
  400. /// Method erases packet from the list of sent packets.
  401. ///
  402. /// \param it iterator pointing to packet to be erased.
  403. /// \return iterator pointing to packet following erased
  404. /// packet or sent_packets_.end() if packet not found.
  405. PktListIterator eraseSent(const PktListIterator it) {
  406. return sent_packets_.template get<0>().erase(it);
  407. }
  408. ExchangeType xchg_type_; ///< Packet exchange type.
  409. PktList sent_packets_; ///< List of sent packets.
  410. /// Iterator pointing to the packet on sent list which will most
  411. /// likely match next received packet. This is based on the
  412. /// assumption that server responds in order to incoming packets.
  413. PktListIterator sent_packets_cache_;
  414. PktList rcvd_packets_; ///< List of received packets.
  415. double min_delay_; ///< Minimum delay between sent
  416. ///< and received packets.
  417. double max_delay_; ///< Maximum delay between sent
  418. ///< and received packets.
  419. double sum_delay_; ///< Sum of delays between sent
  420. ///< and received packets.
  421. double square_sum_delay_; ///< Square sum of delays between
  422. ///< sent and recived packets.
  423. uint64_t orphans_; ///< Number of orphant received packets.
  424. /// Sum of unordered lookup sets. Needed to calculate mean size of
  425. /// lookup set. It is desired that number of unordered lookups is
  426. /// minimal for performance reasons. Tracking number of lookups and
  427. /// mean size of the lookup set should give idea of packets serach
  428. /// complexity.
  429. uint64_t unordered_lookup_size_sum_;
  430. uint64_t unordered_lookups_; ///< Number of unordered sent packets
  431. ///< lookups.
  432. uint64_t ordered_lookups_; ///< Number of ordered sent packets
  433. ///< lookups.
  434. uint64_t sent_packets_num_; ///< Total number of sent packets.
  435. uint64_t rcvd_packets_num_; ///< Total number of received packets.
  436. };
  437. /// Pointer to ExchangeStats.
  438. typedef boost::shared_ptr<ExchangeStats> ExchangeStatsPtr;
  439. /// Map containing all specified exchange types.
  440. typedef typename std::map<ExchangeType, ExchangeStatsPtr> ExchangesMap;
  441. /// Iterator poiting to \ref ExchangesMap
  442. typedef typename ExchangesMap::const_iterator ExchangesMapIterator;
  443. /// Map containing custom counters.
  444. typedef typename std::map<std::string, CustomCounterPtr> CustomCountersMap;
  445. /// Iterator for \ref CustomCountersMap.
  446. typedef typename CustomCountersMap::const_iterator CustomCountersMapIterator;
  447. /// \brief Constructor.
  448. StatsMgr()
  449. : exchanges_(),
  450. custom_counters_() {
  451. }
  452. /// \brief Specify new exchange type.
  453. ///
  454. /// This method creates new \ref ExchangeStats object that will
  455. /// collect statistics data from packets exchange of the specified
  456. /// type.
  457. ///
  458. /// \param xchg_type exchange type.
  459. /// \throw isc::BadValue if exchange of specified type exists.
  460. void addExchangeStats(const ExchangeType xchg_type) {
  461. if (exchanges_.find(xchg_type) != exchanges_.end()) {
  462. isc_throw(BadValue, "Exchange of specified type already added.");
  463. }
  464. exchanges_[xchg_type] = ExchangeStatsPtr(new ExchangeStats(xchg_type));
  465. }
  466. /// \brief Add named custom uint64 counter.
  467. ///
  468. /// Method creates new named counter and stores in counter's map under
  469. /// key specified here as short_name.
  470. ///
  471. /// \param short_name key to use to access counter in the map.
  472. /// \param long_name name of the counter presented in the log file.
  473. void addCustomCounter(const std::string& short_name,
  474. const std::string& long_name) {
  475. if (custom_counters_.find(short_name) != custom_counters_.end()) {
  476. isc_throw(BadValue,
  477. "Custom counter " << short_name << " already added.");
  478. }
  479. custom_counters_[short_name] =
  480. CustomCounterPtr(new CustomCounter(long_name));
  481. }
  482. /// \brief Return specified counter.
  483. ///
  484. /// Method returns specified counter.
  485. ///
  486. /// \param counter_key key poiting to the counter in the counters map.
  487. /// \return pointer to specified counter object.
  488. CustomCounterPtr getCounter(const std::string& counter_key) {
  489. CustomCountersMapIterator it = custom_counters_.find(counter_key);
  490. if (it == custom_counters_.end()) {
  491. isc_throw(BadValue,
  492. "Custom counter " << counter_key << "does not exist");
  493. }
  494. return it->second;
  495. }
  496. /// \brief Increment specified counter.
  497. ///
  498. /// Increement counter value by one.
  499. ///
  500. /// \param counter_key key poitinh to the counter in the counters map.
  501. /// \return pointer to specified counter after incrementation.
  502. const CustomCounter& IncrementCounter(const std::string& counter_key) {
  503. CustomCounterPtr counter = getCounter(counter_key);
  504. return ++(*counter);
  505. }
  506. /// \brief Adds new packet to the sent packets list.
  507. ///
  508. /// Method adds new packet to the sent packets list.
  509. /// Packets are added to the list sequentially and
  510. /// most often read sequentially.
  511. ///
  512. /// \param xchg_type exchange type.
  513. /// \param packet packet to be added to the list
  514. /// \throw isc::BadValue if invalid exchange type specified.
  515. void passSentPacket(const ExchangeType xchg_type,
  516. const boost::shared_ptr<T> packet) {
  517. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  518. xchg_stats->appendSent(packet);
  519. }
  520. /// \brief Add new received packet and match with sent packet.
  521. ///
  522. /// Method adds new packet to the list of received packets. It
  523. /// also searches for corresponding packet on the list of sent
  524. /// packets. When packets are matched the statistics counters
  525. /// are updated accordingly for the particular exchange type.
  526. ///
  527. /// \param xchg_type exchange type.
  528. /// \param packet received packet
  529. /// \throw isc::BadValue if invalid exchange type specified.
  530. /// \throw isc::Unexpected if corresponding packet was not
  531. /// found on the list of sent packets.
  532. void passRcvdPacket(const ExchangeType xchg_type,
  533. const boost::shared_ptr<T> packet) {
  534. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  535. boost::shared_ptr<T> sent_packet
  536. = xchg_stats->findSent(packet->getTransid());
  537. if (sent_packet) {
  538. xchg_stats->updateDelays(sent_packet, packet);
  539. xchg_stats->appendRcvd(packet);
  540. }
  541. }
  542. /// \brief Return minumum delay between sent and received packet.
  543. ///
  544. /// Method returns minimum delay between sent and received packet
  545. /// for specified exchange type.
  546. ///
  547. /// \param xchg_type exchange type.
  548. /// \throw isc::BadValue if invalid exchange type specified.
  549. /// \return minimum delay between packets.
  550. double getMinDelay(const ExchangeType xchg_type) const {
  551. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  552. return xchg_stats->getMinDelay();
  553. }
  554. /// \brief Return maxmimum delay between sent and received packet.
  555. ///
  556. /// Method returns maximum delay between sent and received packet
  557. /// for specified exchange type.
  558. ///
  559. /// \param xchg_type exchange type.
  560. /// \throw isc::BadValue if invalid exchange type specified.
  561. /// \return maximum delay between packets.
  562. double getMaxDelay(const ExchangeType xchg_type) const {
  563. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  564. return xchg_stats->getMaxDelay();
  565. }
  566. /// \brief Return sum of delays between sent and received packets.
  567. ///
  568. /// Method returns sum of delays between sent and received packets
  569. /// for specified exchange type.
  570. ///
  571. /// \param xchg_type exchange type.
  572. /// \throw isc::BadValue if invalid exchange type specified.
  573. /// \return sum of delays between sent and received packets.
  574. double getSumDelay(const ExchangeType xchg_type) const {
  575. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  576. return xchg_stats->getSumDelay();
  577. }
  578. /// \brief Return square sum of delays between sent and received
  579. /// packets.
  580. ///
  581. /// Method returns square sum of delays between sent and received
  582. /// packets for specified exchange type.
  583. ///
  584. /// \param xchg_type exchange type.
  585. /// \throw isc::BadValue if invalid exchange type specified.
  586. /// \return square sum of delays between sent and received packets.
  587. double getSquareSumDelay(const ExchangeType xchg_type) const {
  588. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  589. return xchg_stats->getSquareSumDelay();
  590. }
  591. /// \brief Return number of orphant packets.
  592. ///
  593. /// Method returns number of orphant packets for specified
  594. /// exchange type.
  595. ///
  596. /// \param xchg_type exchange type.
  597. /// \throw isc::BadValue if invalid exchange type specified.
  598. /// \return number of orphant packets so far.
  599. uint64_t getOrphans(const ExchangeType xchg_type) const {
  600. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  601. return xchg_stats->getOrphans();
  602. }
  603. /// \brief Return average unordered lookup set size.
  604. ///
  605. /// Method returns average unordered lookup set size.
  606. /// This value is changes every time \findSet function uses
  607. /// unordered packet lookup using transaction id.
  608. ///
  609. /// \param xchg_type exchange type.
  610. /// \throw isc::BadValue if invalid exchange type specified.
  611. /// \return average unordered lookup set size.
  612. double getAvgUnorderedLookupSetSize(const ExchangeType xchg_type) const {
  613. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  614. return xchg_stats->getAvgUnorderedLookupSetSize();
  615. }
  616. /// \brief Return number of unordered sent packets lookups
  617. ///
  618. /// Method returns number of unordered sent packet lookups.
  619. /// Unordered lookup is used when received packet was sent
  620. /// out of order by server - transaction id of received
  621. /// packet does not match transaction id of next sent packet.
  622. ///
  623. /// \param xchg_type exchange type.
  624. /// \throw isc::BadValue if invalid exchange type specified.
  625. /// \return number of unordered lookups.
  626. uint64_t getUnorderedLookups(const ExchangeType xchg_type) const {
  627. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  628. return xchg_stats->getUnorderedLookups();
  629. }
  630. /// \brief Return number of ordered sent packets lookups
  631. ///
  632. /// Method returns number of ordered sent packet lookups.
  633. /// Ordered lookup is used when packets are received in the
  634. /// same order as they were sent to the server.
  635. /// If packets are skipped or received out of order, lookup
  636. /// function will use unordered lookup (with hash table).
  637. ///
  638. /// \param xchg_type exchange type.
  639. /// \throw isc::BadValue if invalid exchange type specified.
  640. /// \return number of ordered lookups.
  641. uint64_t getOrderedLookups(const ExchangeType xchg_type) const {
  642. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  643. return xchg_stats->getOrderedLookups();
  644. }
  645. /// \brief Return total number of sent packets
  646. ///
  647. /// Method returns total number of sent packets for specified
  648. /// exchange type.
  649. ///
  650. /// \param xchg_type exchange type.
  651. /// \throw isc::BadValue if invalid exchange type specified.
  652. /// \return number of sent packets.
  653. uint64_t getSentPacketsNum(const ExchangeType xchg_type) const {
  654. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  655. return xchg_stats->getSentPacketsNum();
  656. }
  657. /// \brief Return total number of received packets
  658. ///
  659. /// Method returns total number of received packets for specified
  660. /// exchange type.
  661. ///
  662. /// \param xchg_type exchange type.
  663. /// \throw isc::BadValue if invalid exchange type specified.
  664. /// \return number of received packets.
  665. uint64_t getRcvdPacketsNum(const ExchangeType xchg_type) const {
  666. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  667. return xchg_stats->getRcvdPacketsNum();
  668. }
  669. private:
  670. /// \brief Return exchange stats object for given exchange type
  671. ///
  672. /// Method returns exchange stats object for given exchange type.
  673. ///
  674. /// \param xchg_type exchange type.
  675. /// \throw isc::BadValue if invalid exchange type specified.
  676. /// \return exchange stats object.
  677. ExchangeStatsPtr getExchangeStats(const ExchangeType xchg_type) const {
  678. ExchangesMapIterator it = exchanges_.find(xchg_type);
  679. if (it == exchanges_.end()) {
  680. isc_throw(BadValue, "Packets exchange not specified");
  681. }
  682. ExchangeStatsPtr xchg_stats = it->second;
  683. return xchg_stats;
  684. }
  685. ExchangesMap exchanges_; ///< Map of exchange types.
  686. CustomCountersMap custom_counters_; ///< Map with custom counters.
  687. };
  688. } // namespace perfdhcp
  689. } // namespace isc
  690. #endif // __STATS_MGR_H