Kea  2.1.7-git
rate_control.cc
Go to the documentation of this file.
1 // Copyright (C) 2013-2019 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 
10 
11 #include <exceptions/exceptions.h>
12 
13 
14 namespace isc {
15 namespace perfdhcp {
16 
17 using namespace boost::posix_time;
18 
20  : rate_(0), total_pkts_sent_count_(0) {
21 }
22 
23 RateControl::RateControl(const unsigned int rate)
24  : rate_(rate), total_pkts_sent_count_(0) {
25 }
26 
27 uint64_t
28 RateControl::getOutboundMessageCount(bool const waiting_to_exit /* = false */) {
29  if (total_pkts_sent_count_ == 0) {
32  return 1;
33  }
34 
35  // If rate is not limited, then each time send 1 packet.
36  if (getRate() == 0) {
37  return 1;
38  }
39 
40  // If we've entered exit wait time's zone, stop sending.
41  if (waiting_to_exit) {
42  return 0;
43  }
44 
45  // Estimate number of packets to sent. If we are behind of time we will
46  // try to catch up to upkeep request rate by sending more packets in one cycle.
47  auto now = currentTime();
48  time_period period(start_time_, now);
49  time_duration duration = period.length();
50  uint64_t should_sent_pkts_count = static_cast<double>(getRate()) / static_cast<double>(time_duration::ticks_per_second()) * duration.ticks();
51  if (should_sent_pkts_count <= total_pkts_sent_count_) {
52  return 0;
53  }
54  auto pending_pkts_count = should_sent_pkts_count - total_pkts_sent_count_;
55 
56  // Reduce bursts to have more uniform traffic.
57  if (pending_pkts_count > 3) {
58  pending_pkts_count = 3;
59  }
60  total_pkts_sent_count_ += pending_pkts_count;
61 
62  return pending_pkts_count;
63 }
64 
65 boost::posix_time::ptime
67  return (microsec_clock::universal_time());
68 }
69 
70 void
71 RateControl::setRate(const int rate) {
72  if (rate < 0) {
73  isc_throw(isc::BadValue, "invalid value of rate " << rate
74  << ", expected non-negative value");
75  }
76  rate_ = rate;
77 }
78 
79 } // namespace perfdhcp
80 } // namespace isc
boost::posix_time::ptime currentTime()
Convenience function returning current time.
Definition: rate_control.cc:66
RateControl()
Default constructor.
Definition: rate_control.cc:19
unsigned int rate_
Holds a desired rate value.
Definition: rate_control.h:93
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
boost::posix_time::ptime start_time_
Holds time of start of testing.
Definition: rate_control.h:105
unsigned int getRate() const
Returns the rate.
Definition: rate_control.h:75
uint64_t total_pkts_sent_count_
Holds number of packets send from the beginning.
Definition: rate_control.h:99
void setRate(const int rate)
Sets the new rate.
Definition: rate_control.cc:71
Defines the logger used by the top-level component of kea-lfc.
uint64_t getOutboundMessageCount(bool const waiting_to_exit=false)
Returns number of messages to be sent "now".
Definition: rate_control.cc:28