stats_mgr.h 13 KB

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