Commit 354e50d4 authored by Dan Melnic's avatar Dan Melnic Committed by Facebook GitHub Bot

Add support for microsecond intervals in the function scheduler

Summary: Add support for microsecond intervals in the function scheduler

Reviewed By: arushiagg

Differential Revision: D23433411

fbshipit-source-id: 39d201feaaed8007ec880a799129ed94bedd827a
parent 16d63941
......@@ -25,7 +25,7 @@
#include <folly/String.h>
#include <folly/system/ThreadName.h>
using std::chrono::milliseconds;
using std::chrono::microseconds;
using std::chrono::steady_clock;
namespace folly {
......@@ -33,11 +33,11 @@ namespace folly {
namespace {
struct ConsistentDelayFunctor {
const milliseconds constInterval;
const microseconds constInterval;
explicit ConsistentDelayFunctor(milliseconds interval)
explicit ConsistentDelayFunctor(microseconds interval)
: constInterval(interval) {
if (interval < milliseconds::zero()) {
if (interval < microseconds::zero()) {
throw std::invalid_argument(
"FunctionScheduler: "
"time interval must be non-negative");
......@@ -53,18 +53,18 @@ struct ConsistentDelayFunctor {
};
struct ConstIntervalFunctor {
const milliseconds constInterval;
const microseconds constInterval;
explicit ConstIntervalFunctor(milliseconds interval)
explicit ConstIntervalFunctor(microseconds interval)
: constInterval(interval) {
if (interval < milliseconds::zero()) {
if (interval < microseconds::zero()) {
throw std::invalid_argument(
"FunctionScheduler: "
"time interval must be non-negative");
}
}
milliseconds operator()() const {
microseconds operator()() const {
return constInterval;
}
};
......@@ -73,25 +73,25 @@ struct PoissonDistributionFunctor {
std::default_random_engine generator;
std::poisson_distribution<int> poissonRandom;
explicit PoissonDistributionFunctor(double meanPoissonMs)
: poissonRandom(meanPoissonMs) {
if (meanPoissonMs < 0.0) {
explicit PoissonDistributionFunctor(microseconds meanPoissonUsec)
: poissonRandom(meanPoissonUsec.count()) {
if (meanPoissonUsec.count() < 0) {
throw std::invalid_argument(
"FunctionScheduler: "
"Poisson mean interval must be non-negative");
}
}
milliseconds operator()() {
return milliseconds(poissonRandom(generator));
microseconds operator()() {
return microseconds(poissonRandom(generator));
}
};
struct UniformDistributionFunctor {
std::default_random_engine generator;
std::uniform_int_distribution<milliseconds::rep> dist;
std::uniform_int_distribution<microseconds::rep> dist;
UniformDistributionFunctor(milliseconds minInterval, milliseconds maxInterval)
UniformDistributionFunctor(microseconds minInterval, microseconds maxInterval)
: generator(Random::rand32()),
dist(minInterval.count(), maxInterval.count()) {
if (minInterval > maxInterval) {
......@@ -99,15 +99,15 @@ struct UniformDistributionFunctor {
"FunctionScheduler: "
"min time interval must be less or equal than max interval");
}
if (minInterval < milliseconds::zero()) {
if (minInterval < microseconds::zero()) {
throw std::invalid_argument(
"FunctionScheduler: "
"time interval must be non-negative");
}
}
milliseconds operator()() {
return milliseconds(dist(generator));
microseconds operator()() {
return microseconds(dist(generator));
}
};
......@@ -122,30 +122,30 @@ FunctionScheduler::~FunctionScheduler() {
void FunctionScheduler::addFunction(
Function<void()>&& cb,
milliseconds interval,
microseconds interval,
StringPiece nameID,
milliseconds startDelay) {
microseconds startDelay) {
addFunctionInternal(
std::move(cb),
ConstIntervalFunctor(interval),
nameID.str(),
to<std::string>(interval.count(), "ms"),
to<std::string>(interval.count(), "us"),
startDelay,
false /*runOnce*/);
}
void FunctionScheduler::addFunction(
Function<void()>&& cb,
milliseconds interval,
microseconds interval,
const LatencyDistribution& latencyDistr,
StringPiece nameID,
milliseconds startDelay) {
microseconds startDelay) {
if (latencyDistr.isPoisson) {
addFunctionInternal(
std::move(cb),
PoissonDistributionFunctor(latencyDistr.poissonMean),
nameID.str(),
to<std::string>(latencyDistr.poissonMean, "ms (Poisson mean)"),
to<std::string>(latencyDistr.poissonMean.count(), "us (Poisson mean)"),
startDelay,
false /*runOnce*/);
} else {
......@@ -156,10 +156,10 @@ void FunctionScheduler::addFunction(
void FunctionScheduler::addFunctionOnce(
Function<void()>&& cb,
StringPiece nameID,
milliseconds startDelay) {
microseconds startDelay) {
addFunctionInternal(
std::move(cb),
ConstIntervalFunctor(milliseconds::zero()),
ConstIntervalFunctor(microseconds::zero()),
nameID.str(),
"once",
startDelay,
......@@ -168,30 +168,30 @@ void FunctionScheduler::addFunctionOnce(
void FunctionScheduler::addFunctionUniformDistribution(
Function<void()>&& cb,
milliseconds minInterval,
milliseconds maxInterval,
microseconds minInterval,
microseconds maxInterval,
StringPiece nameID,
milliseconds startDelay) {
microseconds startDelay) {
addFunctionInternal(
std::move(cb),
UniformDistributionFunctor(minInterval, maxInterval),
nameID.str(),
to<std::string>(
"[", minInterval.count(), " , ", maxInterval.count(), "] ms"),
"[", minInterval.count(), " , ", maxInterval.count(), "] us"),
startDelay,
false /*runOnce*/);
}
void FunctionScheduler::addFunctionConsistentDelay(
Function<void()>&& cb,
milliseconds interval,
microseconds interval,
StringPiece nameID,
milliseconds startDelay) {
microseconds startDelay) {
addFunctionInternal(
std::move(cb),
ConsistentDelayFunctor(interval),
nameID.str(),
to<std::string>(interval.count(), "ms"),
to<std::string>(interval.count(), "us"),
startDelay,
false /*runOnce*/);
}
......@@ -201,7 +201,7 @@ void FunctionScheduler::addFunctionGenericDistribution(
IntervalDistributionFunc&& intervalFunc,
const std::string& nameID,
const std::string& intervalDescr,
milliseconds startDelay) {
microseconds startDelay) {
addFunctionInternal(
std::move(cb),
std::move(intervalFunc),
......@@ -216,7 +216,7 @@ void FunctionScheduler::addFunctionGenericNextRunTimeFunctor(
NextRunTimeFunc&& fn,
const std::string& nameID,
const std::string& intervalDescr,
milliseconds startDelay) {
microseconds startDelay) {
addFunctionInternal(
std::move(cb),
std::move(fn),
......@@ -232,7 +232,7 @@ void FunctionScheduler::addFunctionToHeapChecked(
RepeatFuncNextRunTimeFunc&& fn,
const std::string& nameID,
const std::string& intervalDescr,
milliseconds startDelay,
microseconds startDelay,
bool runOnce) {
if (!cb) {
throw std::invalid_argument(
......@@ -243,7 +243,7 @@ void FunctionScheduler::addFunctionToHeapChecked(
"FunctionScheduler: "
"interval distribution or next run time function must be set");
}
if (startDelay < milliseconds::zero()) {
if (startDelay < microseconds::zero()) {
throw std::invalid_argument(
"FunctionScheduler: start delay must be non-negative");
}
......@@ -277,7 +277,7 @@ void FunctionScheduler::addFunctionInternal(
NextRunTimeFunc&& fn,
const std::string& nameID,
const std::string& intervalDescr,
milliseconds startDelay,
microseconds startDelay,
bool runOnce) {
return addFunctionToHeapChecked(
std::move(cb), std::move(fn), nameID, intervalDescr, startDelay, runOnce);
......@@ -288,7 +288,7 @@ void FunctionScheduler::addFunctionInternal(
IntervalDistributionFunc&& fn,
const std::string& nameID,
const std::string& intervalDescr,
milliseconds startDelay,
microseconds startDelay,
bool runOnce) {
return addFunctionToHeapChecked(
std::move(cb), std::move(fn), nameID, intervalDescr, startDelay, runOnce);
......@@ -465,7 +465,7 @@ void FunctionScheduler::run() {
}
auto sleepTime = functions_.back()->getNextRunTime() - now;
if (sleepTime < milliseconds::zero()) {
if (sleepTime < microseconds::zero()) {
// We need to run this function now
runOneFunction(lock, now);
runningCondvar_.notify_all();
......
......@@ -80,9 +80,9 @@ class FunctionScheduler {
*/
struct LatencyDistribution {
bool isPoisson;
double poissonMean;
std::chrono::microseconds poissonMean;
LatencyDistribution(bool poisson, double mean)
LatencyDistribution(bool poisson, std::chrono::microseconds mean)
: isPoisson(poisson), poissonMean(mean) {}
};
......@@ -99,9 +99,9 @@ class FunctionScheduler {
*/
void addFunction(
Function<void()>&& cb,
std::chrono::milliseconds interval,
std::chrono::microseconds interval,
StringPiece nameID = StringPiece(),
std::chrono::milliseconds startDelay = std::chrono::milliseconds(0));
std::chrono::microseconds startDelay = std::chrono::microseconds(0));
/*
* Add a new function to the FunctionScheduler with a specified
......@@ -109,10 +109,10 @@ class FunctionScheduler {
*/
void addFunction(
Function<void()>&& cb,
std::chrono::milliseconds interval,
std::chrono::microseconds interval,
const LatencyDistribution& latencyDistr,
StringPiece nameID = StringPiece(),
std::chrono::milliseconds startDelay = std::chrono::milliseconds(0));
std::chrono::microseconds startDelay = std::chrono::microseconds(0));
/**
* Adds a new function to the FunctionScheduler to run only once.
......@@ -120,7 +120,7 @@ class FunctionScheduler {
void addFunctionOnce(
Function<void()>&& cb,
StringPiece nameID = StringPiece(),
std::chrono::milliseconds startDelay = std::chrono::milliseconds(0));
std::chrono::microseconds startDelay = std::chrono::microseconds(0));
/**
* Add a new function to the FunctionScheduler with the time
......@@ -129,10 +129,10 @@ class FunctionScheduler {
*/
void addFunctionUniformDistribution(
Function<void()>&& cb,
std::chrono::milliseconds minInterval,
std::chrono::milliseconds maxInterval,
std::chrono::microseconds minInterval,
std::chrono::microseconds maxInterval,
StringPiece nameID,
std::chrono::milliseconds startDelay);
std::chrono::microseconds startDelay);
/**
* Add a new function to the FunctionScheduler whose start times are attempted
......@@ -143,15 +143,15 @@ class FunctionScheduler {
*/
void addFunctionConsistentDelay(
Function<void()>&& cb,
std::chrono::milliseconds interval,
std::chrono::microseconds interval,
StringPiece nameID = StringPiece(),
std::chrono::milliseconds startDelay = std::chrono::milliseconds(0));
std::chrono::microseconds startDelay = std::chrono::microseconds(0));
/**
* A type alias for function that is called to determine the time
* interval for the next scheduled run.
*/
using IntervalDistributionFunc = Function<std::chrono::milliseconds()>;
using IntervalDistributionFunc = Function<std::chrono::microseconds()>;
/**
* A type alias for function that returns the next run time, given the current
* run time and the current start time.
......@@ -174,7 +174,7 @@ class FunctionScheduler {
IntervalDistributionFunc&& intervalFunc,
const std::string& nameID,
const std::string& intervalDescr,
std::chrono::milliseconds startDelay);
std::chrono::microseconds startDelay);
/**
* Like addFunctionGenericDistribution, adds a new function to the
......@@ -186,7 +186,7 @@ class FunctionScheduler {
NextRunTimeFunc&& fn,
const std::string& nameID,
const std::string& intervalDescr,
std::chrono::milliseconds startDelay);
std::chrono::microseconds startDelay);
/**
* Cancels the function with the specified name, so it will no longer be run.
......@@ -239,7 +239,7 @@ class FunctionScheduler {
NextRunTimeFunc nextRunTimeFunc;
std::chrono::steady_clock::time_point nextRunTime;
std::string name;
std::chrono::milliseconds startDelay;
std::chrono::microseconds startDelay;
std::string intervalDescr;
bool runOnce;
......@@ -248,7 +248,7 @@ class FunctionScheduler {
IntervalDistributionFunc&& intervalFn,
const std::string& nameID,
const std::string& intervalDistDescription,
std::chrono::milliseconds delay,
std::chrono::microseconds delay,
bool once)
: RepeatFunc(
std::move(cback),
......@@ -263,7 +263,7 @@ class FunctionScheduler {
NextRunTimeFunc&& nextRunTimeFn,
const std::string& nameID,
const std::string& intervalDistDescription,
std::chrono::milliseconds delay,
std::chrono::microseconds delay,
bool once)
: cb(std::move(cback)),
nextRunTimeFunc(std::move(nextRunTimeFn)),
......@@ -329,7 +329,7 @@ class FunctionScheduler {
RepeatFuncNextRunTimeFunc&& fn,
const std::string& nameID,
const std::string& intervalDescr,
std::chrono::milliseconds startDelay,
std::chrono::microseconds startDelay,
bool runOnce);
void addFunctionInternal(
......@@ -337,14 +337,14 @@ class FunctionScheduler {
NextRunTimeFunc&& fn,
const std::string& nameID,
const std::string& intervalDescr,
std::chrono::milliseconds startDelay,
std::chrono::microseconds startDelay,
bool runOnce);
void addFunctionInternal(
Function<void()>&& cb,
IntervalDistributionFunc&& fn,
const std::string& nameID,
const std::string& intervalDescr,
std::chrono::milliseconds startDelay,
std::chrono::microseconds startDelay,
bool runOnce);
// Return true if the current function is being canceled
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment