stats_mgr.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. /// DHCP packet exchange types.
  49. enum ExchangeType {
  50. XCHG_DO, ///< DHCPv4 DISCOVER-OFFER
  51. XCHG_RA, ///< DHCPv4 REQUEST-ACK
  52. XCHG_SA, ///< DHCPv6 SOLICIT-ADVERTISE
  53. XCHG_RR ///< DHCPv6 REQUEST-REPLY
  54. };
  55. /// \brief Exchange Statistics.
  56. ///
  57. /// This class collects statistics for exchanges. Parent class
  58. /// may define number of different packet exchanges like:
  59. /// DHCPv4 DISCOVER-OFFER, DHCPv6 SOLICIT-ADVERTISE etc. Performance
  60. /// statistics will be collected for each of those separately in
  61. /// corresponding instance of ExchangeStats.
  62. class ExchangeStats {
  63. public:
  64. static uint32_t transid_hash(boost::shared_ptr<T> packet) {
  65. return packet->getTransid() & 1023;
  66. }
  67. /// \brief List of packets (sent or received).
  68. ///
  69. /// List of packets based on multi index container allows efficient
  70. /// search of packets based on their sequence (order in which they
  71. /// were inserted) as well as based on packet transaction id.
  72. typedef boost::multi_index_container<
  73. boost::shared_ptr<T>,
  74. boost::multi_index::indexed_by<
  75. boost::multi_index::sequenced<>,
  76. boost::multi_index::hashed_non_unique<
  77. boost::multi_index::global_fun<
  78. boost::shared_ptr<T>, uint32_t, &ExchangeStats::transid_hash
  79. >
  80. >
  81. >
  82. > PktList;
  83. /// Packet list iterator for sequencial access to elements.
  84. typedef typename PktList::iterator PktListIterator;
  85. /// Packet list index to search packets using transaction id.
  86. typedef typename PktList::template nth_index<1>::type
  87. PktListTransidIndex;
  88. /// Packet list iterator to access packets using transaction id.
  89. typedef typename PktListTransidIndex::iterator PktListTransidIterator;
  90. /// \brief Constructor
  91. ///
  92. /// \param xchg_type exchange type
  93. ExchangeStats(const ExchangeType xchg_type)
  94. : xchg_type_(xchg_type),
  95. min_delay_(std::numeric_limits<double>::max()),
  96. max_delay_(0.),
  97. sum_delay_(0.),
  98. orphans_(0),
  99. square_sum_delay_(0.),
  100. ordered_lookups_(0),
  101. unordered_lookup_size_sum_(0),
  102. unordered_lookups_(0) {
  103. sent_packets_cache_ = sent_packets_.begin();
  104. }
  105. /// \brief Add new packet to list of sent packets.
  106. ///
  107. /// Method adds new packet to list of sent packets.
  108. ///
  109. /// \param packet packet object to be added.
  110. void appendSent(const boost::shared_ptr<T> packet) {
  111. sent_packets_.template get<0>().push_back(packet);
  112. }
  113. /// \brief Add new packet to list of received packets.
  114. ///
  115. /// Method adds new packet to list of received packets.
  116. ///
  117. /// \param packet packet object to be added.
  118. void appendRcvd(const boost::shared_ptr<T> packet) {
  119. rcvd_packets_.template get<0>().push_back(packet);
  120. }
  121. /// \brief Find packet on the list of sent packets.
  122. ///
  123. /// Method finds packet with specified transaction id on the list
  124. /// of sent packets. It is used to match received packet with
  125. /// corresponding sent packet.
  126. /// Since packets from the server most often come in the same order
  127. /// as they were sent by client, this method will first check if
  128. /// next sent packet matches. If it doesn't, function will search
  129. /// the packet using indexing by transaction id. This reduces
  130. /// packet search time significantly.
  131. ///
  132. /// \param transid transaction id of the packet to search
  133. /// \return packet having specified transaction or NULL if packet
  134. /// not found
  135. boost::shared_ptr<T> findSent(const uint32_t transid) {
  136. if (sent_packets_.size() == 0) {
  137. ++orphans_;
  138. return boost::shared_ptr<T>();
  139. } else if (sent_packets_cache_ == sent_packets_.end()) {
  140. sent_packets_cache_ = sent_packets_.begin();
  141. }
  142. bool packet_found = false;
  143. if ((*sent_packets_cache_)->getTransid() == transid) {
  144. ++ordered_lookups_;
  145. packet_found = true;
  146. } else {
  147. PktListTransidIndex& idx = sent_packets_.template get<1>();
  148. std::pair<PktListTransidIterator,PktListTransidIterator> p =
  149. idx.equal_range(transid & 1023);
  150. ++unordered_lookups_;
  151. unordered_lookup_size_sum_ += std::distance(p.first, p.second);
  152. for (PktListTransidIterator it = p.first; it != p.second;
  153. ++it) {
  154. if ((*it)->getTransid() == transid) {
  155. packet_found = true;
  156. sent_packets_cache_ =
  157. sent_packets_.template project<0>(it);
  158. break;
  159. }
  160. }
  161. }
  162. if (!packet_found) {
  163. ++orphans_;
  164. return boost::shared_ptr<T>();
  165. }
  166. boost::shared_ptr<T> sent_packet(*sent_packets_cache_);
  167. ++sent_packets_cache_;
  168. return sent_packet;
  169. }
  170. /// \brief Update delay counters.
  171. ///
  172. /// Method updates delay counters based on timestamps of
  173. /// sent and received packets.
  174. ///
  175. /// \param sent_packet sent packet
  176. /// \param rcvd_packet received packet
  177. /// \throw isc::Unexpected if failed to calculate timestamps
  178. void updateDelays(const boost::shared_ptr<T> sent_packet,
  179. const boost::shared_ptr<T> rcvd_packet) {
  180. boost::posix_time::ptime sent_time = sent_packet->getTimestamp();
  181. boost::posix_time::ptime rcvd_time = rcvd_packet->getTimestamp();
  182. if (sent_time.is_not_a_date_time() ||
  183. rcvd_time.is_not_a_date_time()) {
  184. isc_throw(Unexpected,
  185. "Timestamp must be set for sent and "
  186. "received packet to measure RTT");
  187. }
  188. boost::posix_time::time_period period(sent_time, rcvd_time);
  189. double delta =
  190. static_cast<double>(period.length().total_nanoseconds()) / 1e9;
  191. if (delta < 0) {
  192. isc_throw(Unexpected, "Sent packet's timestamp must not be "
  193. "greater than received packet's timestamp");
  194. }
  195. if (delta < min_delay_) {
  196. min_delay_ = delta;
  197. }
  198. if (delta > max_delay_) {
  199. max_delay_ = delta;
  200. }
  201. sum_delay_ += delta;
  202. square_sum_delay_ += delta * delta;
  203. }
  204. /// \brief Return minumum delay between sent and received packet.
  205. ///
  206. /// Method returns minimum delay between sent and received packet.
  207. ///
  208. /// \return minimum delay between packets.
  209. double getMinDelay() const { return min_delay_; }
  210. /// \brief Return maxmimum delay between sent and received packet.
  211. ///
  212. /// Method returns maximum delay between sent and received packet.
  213. ///
  214. /// \return maximum delay between packets.
  215. double getMaxDelay() const { return max_delay_; }
  216. /// \brief Return sum of delays between sent and received packets.
  217. ///
  218. /// Method returns sum of delays between sent and received packets.
  219. ///
  220. /// \return sum of delays between sent and received packets.
  221. double getSumDelay() const { return sum_delay_; }
  222. /// \brief Return square sum of delays between sent and received
  223. /// packets.
  224. ///
  225. /// Method returns square sum of delays between sent and received
  226. /// packets.
  227. ///
  228. /// \return square sum of delays between sent and received packets.
  229. double getSquareSumDelay() const { return square_sum_delay_; }
  230. /// \brief Return number of orphant packets.
  231. ///
  232. /// Method returns number of received packets that had no matching
  233. /// sent packet. It is possible that such packet was late or not
  234. /// for us.
  235. ///
  236. /// \return number of orphant received packets.
  237. uint64_t getOrphans() const { return orphans_; }
  238. /// \brief Return average unordered lookup set size.
  239. ///
  240. /// Method returns average unordered lookup set size.
  241. /// This value is changes every time \findSet function uses
  242. /// unordered packet lookup using transaction id.
  243. ///
  244. /// \return average unordered lookup set size.
  245. double getAvgUnorderedLookupSetSize() const {
  246. return static_cast<double>(unordered_lookup_size_sum_) /
  247. static_cast<double>(unordered_lookups_);
  248. }
  249. /// \brief Return number of unordered sent packets lookups
  250. ///
  251. /// Method returns number of unordered sent packet lookups.
  252. /// Unordered lookup is used when received packet was sent
  253. /// out of order by server - transaction id of received
  254. /// packet does not match transaction id of next sent packet.
  255. ///
  256. /// \return number of unordered lookups.
  257. uint64_t getUnorderedLookups() const { return unordered_lookups_; }
  258. /// \brief Return number of ordered sent packets lookups
  259. ///
  260. /// Method returns number of ordered sent packet lookups.
  261. /// Ordered lookup is used when packets are received in the
  262. /// same order as they were sent to the server.
  263. /// If packets are skipped or received out of order, lookup
  264. /// function will use unordered lookup (with hash table).
  265. ///
  266. /// \return number of ordered lookups.
  267. uint64_t getOrderedLookups() const { return ordered_lookups_; }
  268. private:
  269. /// \brief Private default constructor.
  270. ///
  271. /// Default constructor is private because we want the client
  272. /// class to specify exchange type explicitely.
  273. ExchangeStats();
  274. ExchangeType xchg_type_; ///< Packet exchange type.
  275. PktList sent_packets_; ///< List of sent packets.
  276. /// Iterator pointing to the packet on sent list which will most
  277. /// likely match next received packet. This is based on the
  278. /// assumption that server responds in order to incoming packets.
  279. PktListIterator sent_packets_cache_;
  280. PktList rcvd_packets_; ///< List of received packets.
  281. double min_delay_; ///< Minimum delay between sent
  282. ///< and received packets.
  283. double max_delay_; ///< Maximum delay between sent
  284. ///< and received packets.
  285. double sum_delay_; ///< Sum of delays between sent
  286. ///< and received packets.
  287. double square_sum_delay_; ///< Square sum of delays between
  288. ///< sent and recived packets.
  289. uint64_t orphans_; ///< Number of orphant received packets.
  290. /// Sum of unordered lookup sets. Needed to calculate mean size of
  291. /// lookup set. It is desired that number of unordered lookups is
  292. /// minimal for performance reasons. Tracking number of lookups and
  293. /// mean size of the lookup set should give idea of packets serach
  294. /// complexity.
  295. uint64_t unordered_lookup_size_sum_;
  296. uint64_t unordered_lookups_; ///< Number of unordered sent packets
  297. ///< lookups.
  298. uint64_t ordered_lookups_; ///< Number of ordered sent packets
  299. ///< lookups.
  300. };
  301. /// Pointer to ExchangeStats.
  302. typedef boost::shared_ptr<ExchangeStats> ExchangeStatsPtr;
  303. /// Map containing all specified exchange types.
  304. typedef typename std::map<ExchangeType, ExchangeStatsPtr> ExchangesMap;
  305. /// Iterator poiting to \ref ExchangesMap
  306. typedef typename ExchangesMap::const_iterator ExchangesMapIterator;
  307. /// \brief Specify new exchange type.
  308. ///
  309. /// This method creates new \ref ExchangeStats object that will
  310. /// collect statistics data from packets exchange of the specified
  311. /// type.
  312. ///
  313. /// \param xchg_type exchange type.
  314. /// \throw isc::BadValue if exchange of specified type exists.
  315. void addExchangeStats(const ExchangeType xchg_type) {
  316. if (exchanges_.find(xchg_type) != exchanges_.end()) {
  317. isc_throw(BadValue, "Exchange of specified type already added.");
  318. }
  319. exchanges_[xchg_type] = ExchangeStatsPtr(new ExchangeStats(xchg_type));
  320. }
  321. /// \brief Adds new packet to the sent packets list.
  322. ///
  323. /// Method adds new packet to the sent packets list.
  324. /// Packets are added to the list sequentially and
  325. /// most often read sequentially.
  326. ///
  327. /// \param xchg_type exchange type.
  328. /// \param packet packet to be added to the list
  329. /// \throw isc::BadValue if invalid exchange type specified.
  330. void passSentPacket(const ExchangeType xchg_type,
  331. const boost::shared_ptr<T> packet) {
  332. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  333. xchg_stats->appendSent(packet);
  334. }
  335. /// \brief Add new received packet and match with sent packet.
  336. ///
  337. /// Method adds new packet to the list of received packets. It
  338. /// also searches for corresponding packet on the list of sent
  339. /// packets. When packets are matched the statistics counters
  340. /// are updated accordingly for the particular exchange type.
  341. ///
  342. /// \param xchg_type exchange type.
  343. /// \param packet received packet
  344. /// \throw isc::BadValue if invalid exchange type specified.
  345. /// \throw isc::Unexpected if corresponding packet was not
  346. /// found on the list of sent packets.
  347. void passRcvdPacket(const ExchangeType xchg_type,
  348. const boost::shared_ptr<T> packet) {
  349. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  350. boost::shared_ptr<T> sent_packet
  351. = xchg_stats->findSent(packet->getTransid());
  352. if (sent_packet) {
  353. xchg_stats->updateDelays(sent_packet, packet);
  354. xchg_stats->appendRcvd(packet);
  355. }
  356. }
  357. /// \brief Return number of orphant packets.
  358. ///
  359. /// Method returns number of orphant packets for specified
  360. /// exchange type.
  361. ///
  362. /// \param xchg_type exchange type.
  363. /// \throw isc::BadValue if invalid exchange type specified.
  364. /// \return number of orphant packets so far.
  365. uint64_t getOrphans(const ExchangeType xchg_type) const {
  366. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  367. return xchg_stats->getOrphans();
  368. }
  369. /// \brief Return average unordered lookup set size.
  370. ///
  371. /// Method returns average unordered lookup set size.
  372. /// This value is changes every time \findSet function uses
  373. /// unordered packet lookup using transaction id.
  374. ///
  375. /// \param xchg_type exchange type.
  376. /// \return average unordered lookup set size.
  377. double getAvgUnorderedLookupSetSize(const ExchangeType xchg_type) const {
  378. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  379. return xchg_stats->getAvgUnorderedLookupSetSize();
  380. }
  381. /// \brief Return number of unordered sent packets lookups
  382. ///
  383. /// Method returns number of unordered sent packet lookups.
  384. /// Unordered lookup is used when received packet was sent
  385. /// out of order by server - transaction id of received
  386. /// packet does not match transaction id of next sent packet.
  387. ///
  388. /// \return number of unordered lookups.
  389. uint64_t getUnorderedLookups(const ExchangeType xchg_type) const {
  390. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  391. return xchg_stats->getUnorderedLookups();
  392. }
  393. /// \brief Return number of ordered sent packets lookups
  394. ///
  395. /// Method returns number of ordered sent packet lookups.
  396. /// Ordered lookup is used when packets are received in the
  397. /// same order as they were sent to the server.
  398. /// If packets are skipped or received out of order, lookup
  399. /// function will use unordered lookup (with hash table).
  400. ///
  401. /// \return number of ordered lookups.
  402. uint64_t getOrderedLookups(const ExchangeType xchg_type) const {
  403. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  404. return xchg_stats->getOrderedLookups();
  405. }
  406. private:
  407. /// \brief Return exchange stats object for given exchange type
  408. ///
  409. /// Method returns exchange stats object for given exchange type.
  410. ///
  411. /// \param xchg_type exchange type.
  412. /// \throw isc::BadValue if invalid exchange type specified.
  413. /// \return exchange stats object.
  414. ExchangeStatsPtr getExchangeStats(const ExchangeType xchg_type) const {
  415. ExchangesMapIterator it = exchanges_.find(xchg_type);
  416. if (it == exchanges_.end()) {
  417. isc_throw(BadValue, "Packets exchange not specified");
  418. }
  419. ExchangeStatsPtr xchg_stats = it->second;
  420. return xchg_stats;
  421. }
  422. ExchangesMap exchanges_; ///< Map of exchange types.
  423. };
  424. } // namespace perfdhcp
  425. } // namespace isc
  426. #endif // __STATS_MGR_H