rate_control.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright (C) 2013 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 RATE_CONTROL_H
  15. #define RATE_CONTROL_H
  16. #include <boost/date_time/posix_time/posix_time.hpp>
  17. namespace isc {
  18. namespace perfdhcp {
  19. /// \brief A message sending rate control class for perfdhcp.
  20. ///
  21. /// This class provides the means to control the rate at which messages
  22. /// of the specific type are sent by perfdhcp. Each message type,
  23. /// for which the desired rate can be specified, has a corresponding
  24. /// \c RateControl object. So, the perfdhcp is using up to three objects
  25. /// of this type in the same time, to control the rate of the following
  26. /// messages being sent:
  27. /// - Discover(DHCPv4) or Solicit (DHCPv6)
  28. /// - Renew (DHCPv6) or Request (DHCPv4) to renew leases.
  29. /// - Release
  30. ///
  31. /// The purpose of the RateControl class is to track the due time for
  32. /// sending next message (or bunch of messages) to keep outbound rate
  33. /// of particular messages on the desired level. The due time is calculated
  34. /// using the desired rate value and the timestamp when the last message of
  35. /// the particular type has been sent. That puts the responsibility on the
  36. /// \c TestControl class to invoke the \c RateControl::updateSendDue, every
  37. /// time the message is sent.
  38. ///
  39. /// The \c RateControl object returns the number of messages to be sent at
  40. /// the time. Typically the number returned is 0, if perfdhcp shouldn't send
  41. /// any messages yet, or 1 (sometimes more) if the send due time has been
  42. /// reached.
  43. class RateControl {
  44. public:
  45. /// \brief Default constructor.
  46. RateControl();
  47. /// \brief Constructor which sets desired rate and aggressivity.
  48. ///
  49. /// \param rate A desired rate.
  50. /// \param aggressivity A desired aggressivity.
  51. RateControl(const int rate, const int aggressivity);
  52. /// \brief Returns the value of aggressivity.
  53. int getAggressivity() const {
  54. return (aggressivity_);
  55. }
  56. /// \brief Returns current due time to send next message.
  57. boost::posix_time::ptime getDue() const {
  58. return (send_due_);
  59. }
  60. /// \brief Returns number of messages to be sent "now".
  61. ///
  62. /// This function calculates how many masseges of the given type should
  63. /// be sent immediately when the call to the function returns, to catch
  64. /// up with the desired message rate.
  65. ///
  66. /// The value returned depends on the due time calculated with the
  67. /// \c RateControl::updateSendDue function and the current time. If
  68. /// the due time has been hit, the non-zero number of messages is returned.
  69. /// If the due time hasn't been hit, the number returned is 0.
  70. ///
  71. /// \return A number of messages to be sent immediately.
  72. uint64_t getOutboundMessageCount();
  73. /// \brief Returns the rate.
  74. int getRate() const {
  75. return (rate_);
  76. }
  77. /// \brief Returns the value of the late send flag.
  78. ///
  79. /// The flag returned by this function indicates whether the new due time
  80. /// calculated by the \c RateControl::updateSendDue has been in the past.
  81. /// This value is used by the \c TestControl object to increment the counter
  82. /// of the late sent messages in the \c StatsMgr.
  83. bool isLateSent() const {
  84. return (late_sent_);
  85. }
  86. /// \brief Sets the value of aggressivity.
  87. ///
  88. /// \param aggressivity A new value of aggressivity.
  89. void setAggressivity(const int aggressivity) {
  90. aggressivity_ = aggressivity;
  91. }
  92. /// \brief Sets the new rate.
  93. ///
  94. /// \param A new value of rate.
  95. void setRate(const int rate) {
  96. rate_ = rate;
  97. }
  98. /// \brief Sets the value of the due time.
  99. ///
  100. /// This function is intended for unit testing. It manipulates the value of
  101. /// the due time. The parameter passed to this function specifies the
  102. /// (positive or negative) number of seconds relative to current time.
  103. ///
  104. /// \param A number of seconds relative to current time which constitutes
  105. /// the new due time.
  106. void setRelativeDue(const int offset);
  107. /// \brief Sets the timestamp of the last sent message to current time.
  108. void updateSendTime();
  109. protected:
  110. /// \brief Convenience function returning current time.
  111. ///
  112. /// \return current time.
  113. static boost::posix_time::ptime currentTime();
  114. /// \brief Calculates the send due.
  115. ///
  116. /// This function calculates the send due timestamp using the current time
  117. /// and desired rate. The due timestamp is calculated as a sum of the
  118. /// timestamp when the last message was sent and the reciprocal of the rate
  119. /// in micro or nanoseconds (depending on the timer resolution). If the rate
  120. /// is not specified, the duration between two consecutive sends is one
  121. /// timer tick.
  122. void updateSendDue();
  123. /// \brief Holds a timestamp when the next message should be sent.
  124. boost::posix_time::ptime send_due_;
  125. /// \brief Holds a timestamp when the last message was sent.
  126. boost::posix_time::ptime last_sent_;
  127. /// \brief Holds an aggressivity value.
  128. int aggressivity_;
  129. /// \brief Holds a desired rate value.
  130. int rate_;
  131. /// \brief A flag which indicates that the calculated due time is in the
  132. /// past.
  133. bool late_sent_;
  134. };
  135. }
  136. }
  137. #endif