foundationdb/fdbrpc/Smoother.h
Markus Pilman dbe9baff1f Several small compilation fixes for new versions of gcc
There are several missing includes for cmath in the code, I added those.

Next, Coro returns a reference to a stack variable and this causes a
warning. As this is probably ok for Coro, I disabled the warning in
that file for GCC. I want to have this warning in the build system as
it is generally a very useful warning to have.

Another change is that major and minor are deprecated for a while now.
I replaced those with gnu_dev_major and gnu_dev_minor.

ErrorOr currently implements operators ==, !=, and <. These do not
compile because Error does not implement ==. This compiles on older
versions of gcc and clang because ErrorOr<T>::operator== is not used
anywhere. It is still wrong though and newer gcc versions complain.
I simply removed these methods.

The most interesting fix is that TraceEvent::~TraceEvent is currently
throwing exceptions. This is illegal behavior in C++11 and a idea in
older versions of C++. For now I simply removed the throw, but this
might need some more thought.
2019-01-03 12:44:19 -08:00

95 lines
2.6 KiB
C

/*
* Smoother.h
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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"
#include <cmath>
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;
};
#endif