FairMQ  1.3.8
C++ Message Passing 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  RateLimiter(float rate) : tw_req(std::chrono::seconds(1)), start_time(clock::now())
52  {
53  if (rate <= 0) {
54  tw_req = std::chrono::nanoseconds(1);
55  } else {
56  tw_req = std::chrono::duration_cast<clock::duration>(tw_req / rate);
57  }
58  skip_check_count = std::max(1, int(std::chrono::milliseconds(5) / tw_req));
59  count = skip_check_count;
60  //std::cerr << "skip_check_count: " << skip_check_count << '\n';
61  }
62 
70  void maybe_sleep()
71  {
72  using namespace std::chrono;
73  if (--count == 0) {
74  auto now = clock::now();
75  if (tw == clock::duration::zero()) {
76  tw = (now - start_time) / skip_check_count;
77  } else {
78  tw = (1 * tw + 3 * (now - start_time) / skip_check_count) / 4;
79  }
80  //std::ostringstream s; s << "tw = " << std::setw(10) << duration_cast<nanoseconds>(tw).count() << "ns, req = " << duration_cast<nanoseconds>(tw_req).count() << "ns, ";
81  if (tw > tw_req * 65 / 64) {
82  // the time between maybe_sleep calls is more than 1% too long
83  // fix it by reducing ts towards 0 and if ts = 0 doesn't suffice, increase
84  // skip_check_count
85  if (ts > clock::duration::zero()) {
86  ts = std::max(clock::duration::zero(),
87  ts - (tw - tw_req) * skip_check_count * 1 / 2);
88  //std::cerr << s.str() << "maybe_sleep: going too slow; sleep less: " << duration_cast<microseconds>(ts).count() << "µs\n";
89  } else {
90  skip_check_count =
91  std::min(int(seconds(1) / tw_req), // recheck at least every second
92  (skip_check_count * 5 + 3) / 4);
93  //std::cerr << s.str() << "maybe_sleep: going too slow; work more: " << skip_check_count << "\n";
94  }
95  } else if (tw < tw_req * 63 / 64) {
96  // the time between maybe_sleep calls is more than 1% too short
97  // fix it by reducing skip_check_count towards 1 and if skip_check_count = 1
98  // doesn't suffice, increase ts
99 
100  // The minimum work count is defined such that a typical sleep time is greater
101  // than 1ms.
102  // The user requested 1/tw_req work iterations per second. Divided by 1000, that's
103  // the count per ms.
104  const int min_skip_count = std::max(1, int(milliseconds(5) / tw_req));
105  if (skip_check_count > min_skip_count) {
106  assert(ts == clock::duration::zero());
107  skip_check_count = std::max(min_skip_count, skip_check_count * 3 / 4);
108  //std::cerr << s.str() << "maybe_sleep: going too fast; work less: " << skip_check_count << "\n";
109  } else {
110  ts += (tw_req - tw) * (skip_check_count * 7) / 8;
111  //std::cerr << s.str() << "maybe_sleep: going too fast; sleep more: " << duration_cast<microseconds>(ts).count() << "µs\n";
112  }
113  }
114 
115  start_time = now;
116  count = skip_check_count;
117  if (ts > clock::duration::zero()) {
118  std::this_thread::sleep_for(ts);
119  }
120  }
121  }
122 
123 private:
124  clock::duration tw{},
125  ts{},
126  tw_req;
127  clock::time_point start_time;
128  int count = 1;
129  int skip_check_count = 1;
130 };
131 
132 } /* namespace tools */
133 } /* namespace mq */
134 } /* namespace fair */
135 
136 #endif // FAIR_MQ_TOOLS_RATELIMIT_H
void maybe_sleep()
Definition: RateLimit.h:70
RateLimiter(float rate)
Definition: RateLimit.h:51
Definition: FairMQStateMachine.cxx:40
Definition: RateLimit.h:39
Tools for interfacing containers to the transport via polymorphic allocators.
Definition: DeviceRunner.h:23

privacy