stats_mgr.h 33 KB

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