2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* Smoother.h
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FLOW_SMOOTHER_H
|
|
|
|
#define FLOW_SMOOTHER_H
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "flow/flow.h"
|
2018-05-06 00:38:09 +08:00
|
|
|
#include <cmath>
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
struct Smoother {
|
|
|
|
// Times (t) are expected to be nondecreasing
|
|
|
|
|
|
|
|
explicit Smoother( double eFoldingTime ) : eFoldingTime(eFoldingTime) { reset(0); }
|
|
|
|
void reset(double value) { time = 0; total = value; estimate = value; }
|
|
|
|
|
|
|
|
void setTotal( double total, double t = now() ) { addDelta( total - this->total, t); }
|
|
|
|
void addDelta( double delta, double t = now() ) {
|
|
|
|
update(t);
|
|
|
|
total += delta;
|
|
|
|
}
|
|
|
|
// smoothTotal() is a continuous (under)estimate of the sum of all addDeltas()
|
|
|
|
double smoothTotal( double t = now() ) {
|
|
|
|
update(t);
|
|
|
|
return estimate;
|
|
|
|
}
|
|
|
|
// smoothRate() is d/dt[smoothTotal], and is NOT continuous
|
|
|
|
double smoothRate( double t = now() ) {
|
|
|
|
update(t);
|
|
|
|
return (total-estimate) / eFoldingTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
void update(double t) {
|
|
|
|
double elapsed = t - time;
|
|
|
|
if(elapsed) {
|
|
|
|
time = t;
|
|
|
|
estimate += (total-estimate) * (1-exp( -elapsed/eFoldingTime ));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double eFoldingTime;
|
|
|
|
double time, total, estimate;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TimerSmoother {
|
|
|
|
// Times (t) are expected to be nondecreasing
|
|
|
|
|
|
|
|
explicit TimerSmoother( double eFoldingTime ) : eFoldingTime(eFoldingTime) { reset(0); }
|
|
|
|
void reset(double value) { time = 0; total = value; estimate = value; }
|
|
|
|
|
|
|
|
void setTotal( double total, double t = timer() ) { addDelta( total - this->total, t); }
|
|
|
|
void addDelta( double delta, double t = timer() ) {
|
|
|
|
update(t);
|
|
|
|
total += delta;
|
|
|
|
}
|
|
|
|
// smoothTotal() is a continuous (under)estimate of the sum of all addDeltas()
|
|
|
|
double smoothTotal( double t = timer() ) {
|
|
|
|
update(t);
|
|
|
|
return estimate;
|
|
|
|
}
|
|
|
|
// smoothRate() is d/dt[smoothTotal], and is NOT continuous
|
|
|
|
double smoothRate( double t = timer() ) {
|
|
|
|
update(t);
|
|
|
|
return (total-estimate) / eFoldingTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
void update(double t) {
|
|
|
|
double elapsed = t - time;
|
|
|
|
time = t;
|
|
|
|
estimate += (total-estimate) * (1-exp( -elapsed/eFoldingTime ));
|
|
|
|
}
|
|
|
|
|
|
|
|
double eFoldingTime;
|
|
|
|
double time, total, estimate;
|
|
|
|
};
|
|
|
|
|
2018-05-06 00:38:09 +08:00
|
|
|
#endif
|