stats_mgr.h 31 KB

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