FairMQ  1.4.14
C++ Message Queuing Library and Framework
RateLimit.h
1 /********************************************************************************
2  * Copyright (C) 2014-2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 
9 #ifndef FAIR_MQ_TOOLS_RATELIMIT_H
10 #define FAIR_MQ_TOOLS_RATELIMIT_H
11 
12 #include <cassert>
13 #include <string>
14 #include <iostream>
15 #include <iomanip>
16 #include <thread>
17 #include <chrono>
18 
19 namespace fair
20 {
21 namespace mq
22 {
23 namespace tools
24 {
25 
40 {
41  using clock = std::chrono::steady_clock;
42 
43 public:
51  explicit RateLimiter(float rate)
52  : tw_req(std::chrono::seconds(1))
53  , start_time(clock::now())
54  {
55  if (rate <= 0) {
56  tw_req = std::chrono::nanoseconds(1);
57  } else {
58  tw_req = std::chrono::duration_cast<clock::duration>(tw_req / rate);
59  }
60  skip_check_count = std::max(1, int(std::chrono::milliseconds(5) / tw_req));
61  count = skip_check_count;
62  //std::cerr << "skip_check_count: " << skip_check_count << '\n';
63  }
64 
72  void maybe_sleep()
73  {
74  using namespace std::chrono;
75  if (--count == 0) {
76  auto now = clock::now();
77  if (tw == clock::duration::zero()) {
78  tw = (now - start_time) / skip_check_count;
79  } else {
80  tw = (1 * tw + 3 * (now - start_time) / skip_check_count) / 4;
81  }
82  //std::ostringstream s; s << "tw = " << std::setw(10) << duration_cast<nanoseconds>(tw).count() << "ns, req = " << duration_cast<nanoseconds>(tw_req).count() << "ns, ";
83  if (tw > tw_req * 65 / 64) {
84  // the time between maybe_sleep calls is more than 1% too long
85  // fix it by reducing ts towards 0 and if ts = 0 doesn't suffice, increase
86  // skip_check_count
87  if (ts > clock::duration::zero()) {
88  ts = std::max(clock::duration::zero(),
89  ts - (tw - tw_req) * skip_check_count * 1 / 2);
90  //std::cerr << s.str() << "maybe_sleep: going too slow; sleep less: " << duration_cast<microseconds>(ts).count() << "µs\n";
91  } else {
92  skip_check_count =
93  std::min(int(seconds(1) / tw_req), // recheck at least every second
94  (skip_check_count * 5 + 3) / 4);
95  //std::cerr << s.str() << "maybe_sleep: going too slow; work more: " << skip_check_count << "\n";
96  }
97  } else if (tw < tw_req * 63 / 64) {
98  // the time between maybe_sleep calls is more than 1% too short
99  // fix it by reducing skip_check_count towards 1 and if skip_check_count = 1
100  // doesn't suffice, increase ts
101 
102  // The minimum work count is defined such that a typical sleep time is greater
103  // than 1ms.
104  // The user requested 1/tw_req work iterations per second. Divided by 1000, that's
105  // the count per ms.
106  const int min_skip_count = std::max(1, int(milliseconds(5) / tw_req));
107  if (skip_check_count > min_skip_count) {
108  assert(ts == clock::duration::zero());
109  skip_check_count = std::max(min_skip_count, skip_check_count * 3 / 4);
110  //std::cerr << s.str() << "maybe_sleep: going too fast; work less: " << skip_check_count << "\n";
111  } else {
112  ts += (tw_req - tw) * (skip_check_count * 7) / 8;
113  //std::cerr << s.str() << "maybe_sleep: going too fast; sleep more: " << duration_cast<microseconds>(ts).count() << "µs\n";
114  }
115  }
116 
117  start_time = now;
118  count = skip_check_count;
119  if (ts > clock::duration::zero()) {
120  std::this_thread::sleep_for(ts);
121  }
122  }
123  }
124 
125 private:
126  clock::duration tw{},
127  ts{},
128  tw_req;
129  clock::time_point start_time;
130  int count = 1;
131  int skip_check_count = 1;
132 };
133 
134 } /* namespace tools */
135 } /* namespace mq */
136 } /* namespace fair */
137 
138 #endif // FAIR_MQ_TOOLS_RATELIMIT_H
void maybe_sleep()
Definition: RateLimit.h:72
RateLimiter(float rate)
Definition: RateLimit.h:51
Definition: Error.h:56
Definition: RateLimit.h:39
Tools for interfacing containers to the transport via polymorphic allocators.
Definition: DeviceRunner.h:23

privacy