ewmscp  ..
Public Member Functions | Private Attributes | List of all members
throttle::watch Class Reference

class for limiting process load. More...

#include <throttle.h>

Collaboration diagram for throttle::watch:
[legend]

Public Member Functions

 watch (const std::string &name)
 
void update (double units=1.0)
 
void wait ()
 
template<typename T , class ... Types>
action (T(*f)(Types...), Types ... args)
 

Private Attributes

options::single< double > desiredRate
 
options::single< double > tau
 
options::single< double > p
 
options::single< double > i
 
double rateIntegral
 
double lastRate
 
clock_type::time_point lastStop
 
std::mutex updateMutex
 

Detailed Description

class for limiting process load.

Implemented as a limited PI-controller. Each instance creates it's own set of config options. The update() function tells the watch how much "rate" was produced just now, the wait() function will delay the current thread so that the desired rate will not be exceeded.

Definition at line 18 of file throttle.h.

Constructor & Destructor Documentation

◆ watch()

throttle::watch::watch ( const std::string &  name)
inline

Definition at line 28 of file throttle.h.

28  :
29  desiredRate('\0', name + "-rate", "throttle rate for " + name, 0),
30  tau('\0', name + "-tau", "time constant for " + name, 1),
31  p('\0', name + "-p", "prop constant for " + name, 1),
32  i('\0', name + "-i", "integral constant for " + name, 1),
33  lastStop(clock_type::now()) {
34  }

Member Function Documentation

◆ action()

template<typename T , class ... Types>
T throttle::watch::action ( T(*)(Types...)  f,
Types ...  args 
)
inline

Definition at line 64 of file throttle.h.

64  {
65  wait();
66  auto result = f(args ...);
67  update();
68  return result;
69  }

References f(), update(), and wait().

Here is the call graph for this function:

◆ update()

void throttle::watch::update ( double  units = 1.0)
inline

Definition at line 35 of file throttle.h.

35  {
36  if (desiredRate == 0) {
37  return;
38  }
39  std::unique_lock<decltype(updateMutex)> lock(updateMutex);
40  auto now = clock_type::now();
41  auto dt = std::chrono::duration_cast<std::chrono::duration<double>>(now - lastStop).count();
42  auto factor = dt / tau;
43  if (factor > 1) {
44  factor = 1;
45  }
46  lastRate = units / dt * factor + (1 - factor) * lastRate;
47  rateIntegral += (lastRate - desiredRate) * dt;
48  lastStop = now;
49  }

References desiredRate, lastRate, lastStop, rateIntegral, tau, and updateMutex.

Referenced by action(), inputHandler::davix::readerDavix::readBlock(), inputHandler::dcap::readerDcap::readBlock(), inputHandler::daosFs::readerDaosFs::readBlock(), inputHandler::libssh::readerLibssh::readBlock(), inputHandler::posixFile::readerPosixFile::readBlock(), outputHandler::davix::writerDavix::writeBlock(), outputHandler::dcap::writerDcap::writeBlock(), outputHandler::libssh::writerLibssh::writeBlock(), outputHandler::posixFile::writerPosixFile::writeBlock(), outputHandler::daosFs::writerDaosFs::writeBlock(), and throttle::action::~action().

Here is the caller graph for this function:

◆ wait()

void throttle::watch::wait ( )
inline

Definition at line 50 of file throttle.h.

50  {
51  if (desiredRate == 0) {
52  return;
53  }
54  auto dRate = (lastRate - desiredRate) * p + rateIntegral * i;
55  if (dRate <= 0) {
56  return;
57  }
58  auto w = dRate / desiredRate;
59  if (w > 1) {
60  w = 1;
61  }
62  std::this_thread::sleep_for(std::chrono::duration<double>(w));
63  }

References desiredRate, i, lastRate, p, and rateIntegral.

Referenced by action(), throttle::action::action(), inputHandler::davix::readerDavix::readBlock(), inputHandler::dcap::readerDcap::readBlock(), inputHandler::daosFs::readerDaosFs::readBlock(), inputHandler::libssh::readerLibssh::readBlock(), inputHandler::posixFile::readerPosixFile::readBlock(), outputHandler::davix::writerDavix::writeBlock(), outputHandler::dcap::writerDcap::writeBlock(), outputHandler::libssh::writerLibssh::writeBlock(), outputHandler::posixFile::writerPosixFile::writeBlock(), and outputHandler::daosFs::writerDaosFs::writeBlock().

Here is the caller graph for this function:

Member Data Documentation

◆ desiredRate

options::single<double> throttle::watch::desiredRate
private

Definition at line 19 of file throttle.h.

Referenced by update(), and wait().

◆ i

options::single<double> throttle::watch::i
private

Definition at line 22 of file throttle.h.

Referenced by wait().

◆ lastRate

double throttle::watch::lastRate
private

Definition at line 24 of file throttle.h.

Referenced by update(), and wait().

◆ lastStop

clock_type::time_point throttle::watch::lastStop
private

Definition at line 25 of file throttle.h.

Referenced by update().

◆ p

options::single<double> throttle::watch::p
private

Definition at line 21 of file throttle.h.

Referenced by wait().

◆ rateIntegral

double throttle::watch::rateIntegral
private

Definition at line 23 of file throttle.h.

Referenced by update(), and wait().

◆ tau

options::single<double> throttle::watch::tau
private

Definition at line 20 of file throttle.h.

Referenced by update().

◆ updateMutex

std::mutex throttle::watch::updateMutex
private

Definition at line 26 of file throttle.h.

Referenced by update().


The documentation for this class was generated from the following file:
throttle::watch::lastRate
double lastRate
Definition: throttle.h:24
throttle::watch::desiredRate
options::single< double > desiredRate
Definition: throttle.h:19
throttle::watch::p
options::single< double > p
Definition: throttle.h:21
throttle::watch::i
options::single< double > i
Definition: throttle.h:22
throttle::watch::tau
options::single< double > tau
Definition: throttle.h:20
throttle::watch::lastStop
clock_type::time_point lastStop
Definition: throttle.h:25
throttle::watch::wait
void wait()
Definition: throttle.h:50
throttle::watch::updateMutex
std::mutex updateMutex
Definition: throttle.h:26
throttle::watch::rateIntegral
double rateIntegral
Definition: throttle.h:23
f
int f(int a, int line)
Definition: cctest.cpp:4
throttle::watch::update
void update(double units=1.0)
Definition: throttle.h:35