stats_mgr.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 <map>
  17. #include <boost/noncopyable.hpp>
  18. #include <boost/shared_ptr.hpp>
  19. #include <boost/multi_index_container.hpp>
  20. #include <boost/multi_index/ordered_index.hpp>
  21. #include <boost/multi_index/sequenced_index.hpp>
  22. #include <boost/multi_index/mem_fun.hpp>
  23. #include <exceptions/exceptions.h>
  24. namespace isc {
  25. namespace perfdhcp {
  26. /// \brief Statistics Manager
  27. ///
  28. /// This class template is a storage for various performance statistics
  29. /// collected during performance tests execution with perfdhcp tool.
  30. ///
  31. /// Statistics Manager holds lists of sent and received packets and
  32. /// groups them into exchanges. For example: DHCPDISCOVER message and
  33. /// corresponding DHCPOFFER messages belong to one exchange, DHCPREQUEST
  34. /// and corresponding DHCPACK message belong to another exchange etc.
  35. /// In order to update statistics for a particular exchange type, client
  36. /// class passes sent and received packets. Internally, Statistics Manager
  37. /// tries to match transaction id of received packet with sent packet
  38. /// stored on the list of sent packets. When packets are matched the
  39. /// round trip time can be calculated.
  40. ///
  41. /// \tparam T class representing DHCPv4 or DHCPv6 packet.
  42. template <class T>
  43. class StatsMgr : public boost::noncopyable {
  44. public:
  45. /// DHCP packet exchange types.
  46. enum ExchangeType {
  47. XCHG_DO, ///< DHCPv4 DISCOVER-OFFER
  48. XCHG_RA, ///< DHCPv4 REQUEST-ACK
  49. XCHG_SA, ///< DHCPv6 SOLICIT-ADVERTISE
  50. XCHG_RR ///< DHCPv6 REQUEST-REPLY
  51. };
  52. /// \brief Exchange Statistics.
  53. ///
  54. /// This class collects statistics for exchanges. Parent class
  55. /// may define number of different packet exchanges like:
  56. /// DHCPv4 DISCOVER-OFFER, DHCPv6 SOLICIT-ADVERTISE etc. Performance
  57. /// statistics will be collected for each of those separately in
  58. /// corresponding instance of ExchangeStats.
  59. class ExchangeStats {
  60. public:
  61. /// \brief List of packets (sent or received).
  62. ///
  63. /// List of packets based on multi index container allows efficient
  64. /// search of packets based on their sequence (order in which they
  65. /// were inserted) as well as based on packet transaction id.
  66. typedef boost::multi_index_container<
  67. boost::shared_ptr<T>,
  68. boost::multi_index::indexed_by<
  69. boost::multi_index::sequenced<>,
  70. boost::multi_index::ordered_unique<
  71. boost::multi_index::const_mem_fun<
  72. T, uint32_t, &T::getTransid>
  73. >
  74. >
  75. > PktList;
  76. /// Packet list iterator for sequencial access to elements.
  77. typedef typename PktList::iterator PktListIterator;
  78. /// Packet list index to search packets using transaction id.
  79. typedef typename PktList::template nth_index<1>::type
  80. PktListTransidIndex;
  81. /// Packet list iterator to access packets using transaction id.
  82. typedef typename PktListTransidIndex::iterator PktListTransidIterator;
  83. /// \brief Constructor
  84. ///
  85. /// \param xchg_type exchange type
  86. ExchangeStats(const ExchangeType xchg_type)
  87. : xchg_type_(xchg_type) {
  88. sent_packets_cache_ = sent_packets_.begin();
  89. }
  90. /// \brief Add new packet to list of sent packets.
  91. ///
  92. /// Method adds new packet to list of sent packets.
  93. ///
  94. /// \param packet packet object to be appended.
  95. void appendSent(const boost::shared_ptr<T> packet) {
  96. sent_packets_.template get<0>().push_back(packet);
  97. }
  98. /// \brief Find packet on the list of sent packets.
  99. ///
  100. /// Method finds packet with specified transaction id on the list
  101. /// of sent packets. It is used to match received packet with
  102. /// corresponding sent packet.
  103. /// Since packets from the server most often come in the same order
  104. /// as they were sent by client, this method will first check if
  105. /// next sent packet matches. If it doesn't, function will search
  106. /// the packet using indexing by transaction id. This reduces
  107. /// packet search time significantly.
  108. ///
  109. /// \param transid transaction id of the packet to search
  110. /// \throw isc::Unexpected if packet could not be found
  111. /// \return packet having specified transaction id
  112. boost::shared_ptr<T> findSent(const uint32_t transid) {
  113. if (sent_packets_.size() == 0) {
  114. isc_throw(Unexpected, "Sent packets list is empty.");
  115. } else if (sent_packets_cache_ == sent_packets_.end()) {
  116. sent_packets_cache_ = sent_packets_.begin();
  117. }
  118. bool packet_found = false;
  119. if ((*sent_packets_cache_)->getTransid() == transid) {
  120. packet_found = true;
  121. } else {
  122. PktListTransidIndex& idx = sent_packets_.template get<1>();
  123. PktListTransidIterator it = idx.find(transid);
  124. if (it != idx.end()) {
  125. packet_found = true;
  126. sent_packets_cache_ = sent_packets_.template project<0>(it);
  127. }
  128. }
  129. if (!packet_found) {
  130. isc_throw(Unexpected, "Unable to find sent packet.");
  131. }
  132. boost::shared_ptr<T> sent_packet(*sent_packets_cache_);
  133. ++sent_packets_cache_;
  134. return sent_packet;
  135. }
  136. double getMinDelay() const { return min_delay_; }
  137. double getMaxDelay() const { return max_delay_; }
  138. double getSumDelay() const { return sum_delay_; }
  139. double getSquareSumDelay() const { return square_sum_delay_; }
  140. private:
  141. /// \brief Private default constructor.
  142. ///
  143. /// Default constructor is private because we want client
  144. /// class to specify exchange type explicitely.
  145. ExchangeStats();
  146. ExchangeType xchg_type_; ///< Packet exchange type.
  147. PktList sent_packets_; ///< List of sent packets.
  148. /// Iterator pointing to the packet on sent list which will most
  149. /// likely match next received packet. This is based on the
  150. /// assumption that server responds in order to incoming packets.
  151. PktListIterator sent_packets_cache_;
  152. PktList rcvd_packets_; ///< List of received packets.
  153. double min_delay_; ///< Minimum delay between sent
  154. ///< and received packets.
  155. double max_delay_; ///< Maximum delay between sent
  156. ///< and received packets.
  157. double sum_delay_; ///< Sum of delays between sent
  158. ///< and received packets.
  159. double square_sum_delay_; ///< Square sum of delays between
  160. ///< sent and recived packets.
  161. };
  162. /// Pointer to ExchangeStats.
  163. typedef boost::shared_ptr<ExchangeStats> ExchangeStatsPtr;
  164. /// Map containing all specified exchange types.
  165. typedef typename std::map<ExchangeType, ExchangeStatsPtr> ExchangesMap;
  166. /// Iterator poiting to \ref ExchangesMap
  167. typedef typename ExchangesMap::iterator ExchangesMapIterator;
  168. /// \brief Specify new exchange type.
  169. ///
  170. /// This method creates new \ref ExchangeStats object that will
  171. /// collect statistics data from packets exchange of the specified
  172. /// type.
  173. ///
  174. /// \param xchg_type exchange type.
  175. /// \throw isc::BadValue if exchange of specified type exists.
  176. void addExchangeStats(const ExchangeType xchg_type) {
  177. if (exchanges_.find(xchg_type) != exchanges_.end()) {
  178. isc_throw(BadValue, "Exchange of specified type already added.");
  179. }
  180. exchanges_[xchg_type] = ExchangeStatsPtr(new ExchangeStats(xchg_type));
  181. }
  182. /// \brief Adds new packet to the sent packets list.
  183. ///
  184. /// Method adds new packet to the sent packets list.
  185. /// Packets are added to the list sequentially and
  186. /// most often read sequentially.
  187. ///
  188. /// \param xchg_type exchange type.
  189. /// \param packet packet to be added to the list
  190. /// \throw isc::BadValue if invalid exchange type specified.
  191. void passSentPacket(const ExchangeType xchg_type,
  192. const boost::shared_ptr<T> packet) {
  193. ExchangesMapIterator it = exchanges_.find(xchg_type);
  194. if (it == exchanges_.end()) {
  195. isc_throw(BadValue, "Packets exchange not specified");
  196. }
  197. it->second->appendSent(packet);
  198. }
  199. /// \brief Add new received packet and match with sent packet.
  200. ///
  201. /// Method adds new packet to the list of received packets. It
  202. /// also searches for corresponding packet on the list of sent
  203. /// packets. When packets are matched the statistics counters
  204. /// are updated accordingly for the particular exchange type.
  205. ///
  206. /// \param xchg_type exchange type.
  207. /// \param packet received packet
  208. /// \throw isc::BadValue if invalid exchange type specified.
  209. /// \throw isc::Unexpected if corresponding packet was not
  210. /// found on the list of sent packets.
  211. void passRcvdPacket(const ExchangeType xchg_type,
  212. const boost::shared_ptr<T> packet) {
  213. ExchangesMapIterator it = exchanges_.find(xchg_type);
  214. if (it == exchanges_.end()) {
  215. isc_throw(BadValue, "Packets exchange not specified");
  216. }
  217. boost::shared_ptr<T> sent_packet
  218. = it->second->findSent(packet->getTransid());
  219. }
  220. private:
  221. ExchangesMap exchanges_; ///< Map of exchange types.
  222. };
  223. } // namespace perfdhcp
  224. } // namespace isc
  225. #endif // __STATS_MGR_H