Source · fairmq/tools/
RateLimit.hpublic header
Defines class fair::mq::tools::RateLimiter.
Described in Receive data with callbacks or loops.
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_H10#define FAIR_MQ_TOOLS_RATELIMIT_H11 12#include <cassert>13#include <string>14// #include <iostream>15#include <iomanip>16#include <thread>17#include <chrono>18 19namespace fair::mq::tools20{21 22/**23 * Objects of type RateLimiter can be used to limit a loop to a given rate of iterations per second.24 *25 * Example:26 * \code27 * RateLimiter limit(100); // 100 Hz28 * while (do_more_work()) {29 * work();30 * limit.maybe_sleep(); // this needs to be at the end of the loop for a31 * // correct time measurement of the first iterations32 * }33 * \endcode34 */35class RateLimiter36{37 using clock = std::chrono::steady_clock;38 39 public:40 /**41 * Constructs a rate limiter.42 *43 * \param rate Work rate in Hz (calls to maybe_sleep per second). Values less than/equal44 * to 0 set the rate to 1 GHz (which is impossible to achieve, even with a45 * loop that only calls RateLimiter::maybe_sleep).46 */47 explicit RateLimiter(float rate)48 : tw_req(std::chrono::seconds(1))49 , start_time(clock::now())50 {51 if (rate <= 0) {52 tw_req = std::chrono::nanoseconds(1);53 } else {54 tw_req = std::chrono::duration_cast<clock::duration>(tw_req / rate);55 }56 skip_check_count = std::max(1, int(std::chrono::milliseconds(5) / tw_req));57 count = skip_check_count;58 // std::cerr << "skip_check_count: " << skip_check_count << '\n';59 }60 61 /**62 * Call this function at the end of the iteration rate limited loop.63 *64 * This function might use `std::this_thread::sleep_for` to limit the iteration rate. If no65 * sleeps are necessary, the function will back off checking for the time to further allow66 * increased iteration rates (until the requested rate or 1s between rechecks is reached).67 */68 void maybe_sleep()69 {70 using namespace std::chrono;71 if (--count == 0) {72 auto now = clock::now();73 if (tw == clock::duration::zero()) {74 tw = (now - start_time) / skip_check_count;75 } else {76 tw = (1 * tw + 3 * (now - start_time) / skip_check_count) / 4;77 }78 // std::ostringstream s; s << "tw = " << std::setw(10) <<79 // duration_cast<nanoseconds>(tw).count() << "ns, req = " <<80 // 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 long83 // fix it by reducing ts towards 0 and if ts = 0 doesn't suffice, increase84 // skip_check_count85 if (ts > clock::duration::zero()) {86 ts = std::max(clock::duration::zero(), ts - (tw - tw_req) * skip_check_count * 1 / 2);87 // std::cerr << s.str() << "maybe_sleep: going too slow; sleep less: " <<88 // 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 second92 (skip_check_count * 5 + 3) / 4);93 // std::cerr << s.str() << "maybe_sleep: going too slow; work more: " <<94 // skip_check_count << "\n";95 }96 } else if (tw < tw_req * 63 / 64) {97 // the time between maybe_sleep calls is more than 1% too short98 // fix it by reducing skip_check_count towards 1 and if skip_check_count = 199 // doesn't suffice, increase ts100 101 // The minimum work count is defined such that a typical sleep time is greater102 // than 1ms.103 // The user requested 1/tw_req work iterations per second. Divided by 1000, that's104 // the count per ms.105 const int min_skip_count = std::max(1, int(milliseconds(5) / tw_req));106 if (skip_check_count > min_skip_count) {107 assert(ts == clock::duration::zero());108 skip_check_count = std::max(min_skip_count, skip_check_count * 3 / 4);109 // std::cerr << s.str() << "maybe_sleep: going too fast; work less: " <<110 // 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: " <<114 // duration_cast<microseconds>(ts).count() << "µs\n";115 }116 }117 118 start_time = now;119 count = skip_check_count;120 if (ts > clock::duration::zero()) {121 std::this_thread::sleep_for(ts);122 }123 }124 }125 126 private:127 clock::duration tw{}, //! deduced duration between maybe_sleep calls128 ts{}, //! sleep duration129 tw_req; //! requested duration between maybe_sleep calls130 clock::time_point start_time;131 int count = 1;132 int skip_check_count = 1;133};134 135} // namespace fair::mq::tools136 137#endif // FAIR_MQ_TOOLS_RATELIMIT_H