stats_mgr.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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/ordered_index.hpp>
  22. #include <boost/multi_index/sequenced_index.hpp>
  23. #include <boost/multi_index/mem_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. /// DHCP packet exchange types.
  48. enum ExchangeType {
  49. XCHG_DO, ///< DHCPv4 DISCOVER-OFFER
  50. XCHG_RA, ///< DHCPv4 REQUEST-ACK
  51. XCHG_SA, ///< DHCPv6 SOLICIT-ADVERTISE
  52. XCHG_RR ///< DHCPv6 REQUEST-REPLY
  53. };
  54. /// \brief Exchange Statistics.
  55. ///
  56. /// This class collects statistics for exchanges. Parent class
  57. /// may define number of different packet exchanges like:
  58. /// DHCPv4 DISCOVER-OFFER, DHCPv6 SOLICIT-ADVERTISE etc. Performance
  59. /// statistics will be collected for each of those separately in
  60. /// corresponding instance of ExchangeStats.
  61. class ExchangeStats {
  62. public:
  63. /// \brief List of packets (sent or received).
  64. ///
  65. /// List of packets based on multi index container allows efficient
  66. /// search of packets based on their sequence (order in which they
  67. /// were inserted) as well as based on packet transaction id.
  68. typedef boost::multi_index_container<
  69. boost::shared_ptr<T>,
  70. boost::multi_index::indexed_by<
  71. boost::multi_index::sequenced<>,
  72. boost::multi_index::ordered_unique<
  73. boost::multi_index::const_mem_fun<
  74. T, uint32_t, &T::getTransid>
  75. >
  76. >
  77. > PktList;
  78. /// Packet list iterator for sequencial access to elements.
  79. typedef typename PktList::iterator PktListIterator;
  80. /// Packet list index to search packets using transaction id.
  81. typedef typename PktList::template nth_index<1>::type
  82. PktListTransidIndex;
  83. /// Packet list iterator to access packets using transaction id.
  84. typedef typename PktListTransidIndex::iterator PktListTransidIterator;
  85. /// \brief Constructor
  86. ///
  87. /// \param xchg_type exchange type
  88. ExchangeStats(const ExchangeType xchg_type)
  89. : xchg_type_(xchg_type),
  90. min_delay_(std::numeric_limits<double>::max()),
  91. max_delay_(0.),
  92. sum_delay_(0.),
  93. orphans_(0),
  94. square_sum_delay_(0.) {
  95. sent_packets_cache_ = sent_packets_.begin();
  96. }
  97. /// \brief Add new packet to list of sent packets.
  98. ///
  99. /// Method adds new packet to list of sent packets.
  100. ///
  101. /// \param packet packet object to be appended.
  102. void appendSent(const boost::shared_ptr<T> packet) {
  103. sent_packets_.template get<0>().push_back(packet);
  104. }
  105. /// \brief Find packet on the list of sent packets.
  106. ///
  107. /// Method finds packet with specified transaction id on the list
  108. /// of sent packets. It is used to match received packet with
  109. /// corresponding sent packet.
  110. /// Since packets from the server most often come in the same order
  111. /// as they were sent by client, this method will first check if
  112. /// next sent packet matches. If it doesn't, function will search
  113. /// the packet using indexing by transaction id. This reduces
  114. /// packet search time significantly.
  115. ///
  116. /// \param transid transaction id of the packet to search
  117. /// \return packet having specified transaction or NULL if packet
  118. /// not found
  119. boost::shared_ptr<T> findSent(const uint32_t transid) {
  120. if (sent_packets_.size() == 0) {
  121. ++orphans_;
  122. return boost::shared_ptr<T>();
  123. } else if (sent_packets_cache_ == sent_packets_.end()) {
  124. sent_packets_cache_ = sent_packets_.begin();
  125. }
  126. bool packet_found = false;
  127. if ((*sent_packets_cache_)->getTransid() == transid) {
  128. packet_found = true;
  129. } else {
  130. PktListTransidIndex& idx = sent_packets_.template get<1>();
  131. PktListTransidIterator it = idx.find(transid);
  132. if (it != idx.end()) {
  133. packet_found = true;
  134. sent_packets_cache_ = sent_packets_.template project<0>(it);
  135. }
  136. }
  137. if (!packet_found) {
  138. ++orphans_;
  139. return boost::shared_ptr<T>();
  140. }
  141. boost::shared_ptr<T> sent_packet(*sent_packets_cache_);
  142. ++sent_packets_cache_;
  143. return sent_packet;
  144. }
  145. /// \brief Update delay counters.
  146. ///
  147. /// Method updates delay counters based on timestamps of
  148. /// sent and received packets.
  149. ///
  150. /// \param sent_packet sent packet
  151. /// \param rcvd_packet received packet
  152. /// \throw isc::Unexpected if failed to calculate timestamps
  153. void updateDelays(const boost::shared_ptr<T> sent_packet,
  154. const boost::shared_ptr<T> rcvd_packet) {
  155. boost::posix_time::ptime sent_time = sent_packet->getTimestamp();
  156. boost::posix_time::ptime rcvd_time = rcvd_packet->getTimestamp();
  157. if (sent_time.is_not_a_date_time() ||
  158. rcvd_time.is_not_a_date_time()) {
  159. isc_throw(Unexpected,
  160. "Timestamp must be set for sent and "
  161. "received packet to measure RTT");
  162. }
  163. boost::posix_time::time_period period(sent_time, rcvd_time);
  164. double delta =
  165. static_cast<double>(period.length().total_nanoseconds()) / 1e9;
  166. if (delta < 0) {
  167. isc_throw(Unexpected, "Sent packet's timestamp must not be "
  168. "greater than received packet's timestamp");
  169. }
  170. if (delta < min_delay_) {
  171. min_delay_ = delta;
  172. }
  173. if (delta > max_delay_) {
  174. max_delay_ = delta;
  175. }
  176. sum_delay_ += delta;
  177. square_sum_delay_ += delta * delta;
  178. }
  179. /// \brief Return minumum delay between sent and received packet.
  180. ///
  181. /// Method returns minimum delay between sent and received packet.
  182. ///
  183. /// \return minimum delay between packets.
  184. double getMinDelay() const { return min_delay_; }
  185. /// \brief Return maxmimum delay between sent and received packet.
  186. ///
  187. /// Method returns maximum delay between sent and received packet.
  188. ///
  189. /// \return maximum delay between packets.
  190. double getMaxDelay() const { return max_delay_; }
  191. /// \brief Return sum of delays between sent and received packets.
  192. ///
  193. /// Method returns sum of delays between sent and received packets.
  194. ///
  195. /// \return sum of delays between sent and received packets.
  196. double getSumDelay() const { return sum_delay_; }
  197. /// \brief Return square sum of delays between sent and received
  198. /// packets.
  199. ///
  200. /// Method returns square sum of delays between sent and received
  201. /// packets.
  202. ///
  203. /// \return square sum of delays between sent and received packets.
  204. double getSquareSumDelay() const { return square_sum_delay_; }
  205. /// \brief Return number of orphant packets.
  206. ///
  207. /// Method returns number of received packets that had no matching
  208. /// sent packet. It is possible that such packet was late or not
  209. /// for us.
  210. ///
  211. /// \return number of orphant received packets.
  212. uint64_t getOrphans() const { return orphans_; }
  213. private:
  214. /// \brief Private default constructor.
  215. ///
  216. /// Default constructor is private because we want the client
  217. /// class to specify exchange type explicitely.
  218. ExchangeStats();
  219. ExchangeType xchg_type_; ///< Packet exchange type.
  220. PktList sent_packets_; ///< List of sent packets.
  221. /// Iterator pointing to the packet on sent list which will most
  222. /// likely match next received packet. This is based on the
  223. /// assumption that server responds in order to incoming packets.
  224. PktListIterator sent_packets_cache_;
  225. PktList rcvd_packets_; ///< List of received packets.
  226. double min_delay_; ///< Minimum delay between sent
  227. ///< and received packets.
  228. double max_delay_; ///< Maximum delay between sent
  229. ///< and received packets.
  230. double sum_delay_; ///< Sum of delays between sent
  231. ///< and received packets.
  232. double square_sum_delay_; ///< Square sum of delays between
  233. ///< sent and recived packets.
  234. uint64_t orphans_; ///< Number of orphant received packets.
  235. };
  236. /// Pointer to ExchangeStats.
  237. typedef boost::shared_ptr<ExchangeStats> ExchangeStatsPtr;
  238. /// Map containing all specified exchange types.
  239. typedef typename std::map<ExchangeType, ExchangeStatsPtr> ExchangesMap;
  240. /// Iterator poiting to \ref ExchangesMap
  241. typedef typename ExchangesMap::const_iterator ExchangesMapIterator;
  242. /// \brief Specify new exchange type.
  243. ///
  244. /// This method creates new \ref ExchangeStats object that will
  245. /// collect statistics data from packets exchange of the specified
  246. /// type.
  247. ///
  248. /// \param xchg_type exchange type.
  249. /// \throw isc::BadValue if exchange of specified type exists.
  250. void addExchangeStats(const ExchangeType xchg_type) {
  251. if (exchanges_.find(xchg_type) != exchanges_.end()) {
  252. isc_throw(BadValue, "Exchange of specified type already added.");
  253. }
  254. exchanges_[xchg_type] = ExchangeStatsPtr(new ExchangeStats(xchg_type));
  255. }
  256. /// \brief Adds new packet to the sent packets list.
  257. ///
  258. /// Method adds new packet to the sent packets list.
  259. /// Packets are added to the list sequentially and
  260. /// most often read sequentially.
  261. ///
  262. /// \param xchg_type exchange type.
  263. /// \param packet packet to be added to the list
  264. /// \throw isc::BadValue if invalid exchange type specified.
  265. void passSentPacket(const ExchangeType xchg_type,
  266. const boost::shared_ptr<T> packet) {
  267. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  268. xchg_stats->appendSent(packet);
  269. }
  270. /// \brief Add new received packet and match with sent packet.
  271. ///
  272. /// Method adds new packet to the list of received packets. It
  273. /// also searches for corresponding packet on the list of sent
  274. /// packets. When packets are matched the statistics counters
  275. /// are updated accordingly for the particular exchange type.
  276. ///
  277. /// \param xchg_type exchange type.
  278. /// \param packet received packet
  279. /// \throw isc::BadValue if invalid exchange type specified.
  280. /// \throw isc::Unexpected if corresponding packet was not
  281. /// found on the list of sent packets.
  282. void passRcvdPacket(const ExchangeType xchg_type,
  283. const boost::shared_ptr<T> packet) {
  284. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  285. boost::shared_ptr<T> sent_packet
  286. = xchg_stats->findSent(packet->getTransid());
  287. if (sent_packet) {
  288. xchg_stats->updateDelays(sent_packet, packet);
  289. }
  290. }
  291. /// \brief Return number of orphant packets.
  292. ///
  293. /// Method returns number of orphant packets for specified
  294. /// exchange type.
  295. ///
  296. /// \param xchg_type exchange type.
  297. /// \throw isc::BadValue if invalid exchange type specified.
  298. /// \return number of orphant packets so far.
  299. uint64_t getOrphans(const ExchangeType xchg_type) const {
  300. ExchangeStatsPtr xchg_stats = getExchangeStats(xchg_type);
  301. return xchg_stats->getOrphans();
  302. }
  303. private:
  304. /// \brief Return exchange stats object for given exchange type
  305. ///
  306. /// Method returns exchange stats object for given exchange type.
  307. ///
  308. /// \param xchg_type exchange type.
  309. /// \throw isc::BadValue if invalid exchange type specified.
  310. /// \return exchange stats object.
  311. ExchangeStatsPtr getExchangeStats(const ExchangeType xchg_type) const {
  312. ExchangesMapIterator it = exchanges_.find(xchg_type);
  313. if (it == exchanges_.end()) {
  314. isc_throw(BadValue, "Packets exchange not specified");
  315. }
  316. ExchangeStatsPtr xchg_stats = it->second;
  317. return xchg_stats;
  318. }
  319. ExchangesMap exchanges_; ///< Map of exchange types.
  320. };
  321. } // namespace perfdhcp
  322. } // namespace isc
  323. #endif // __STATS_MGR_H