Files
ternfs-XTXMarkets/cpp/core/Timings.hpp
Francesco Mazzoli 1ec63f9710 Implement scrubbing functionality
Fixes #32. This also involves some reworking of the block request machinery
to make it more robust and faster. The scrubbing is done assuming that
the overwhelming majority of block checking will go through.
2023-11-05 18:33:00 +00:00

54 lines
1.3 KiB
C++

#pragma once
#include <stdint.h>
#include <atomic>
#include <math.h>
#include "Exception.hpp"
#include "Time.hpp"
#include "Msgs.hpp"
#include "Metrics.hpp"
struct Timings {
// static stuff
double _growth;
double _invLogGrowth;
uint64_t _firstUpperBound;
double _growthDivUpperBound;
// actual data
EggsTime _startedAt;
std::vector<std::atomic<uint64_t>> _bins;
public:
Timings(Duration firstUpperBound, double growth, int bins);
Timings() = default;
static Timings Standard() {
// 100ns to ~20mins, 10% error
return Timings(100_ns, 1.2, 128);
}
void add(Duration d) {
int64_t inanos = d.ns;
if (unlikely(inanos <= 0)) { return; }
uint64_t nanos = inanos;
int bin = std::min<int>(_bins.size()-1, std::max<int>(0, log((double)nanos * _growthDivUpperBound) * _invLogGrowth));
_bins[bin]++;
}
uint64_t count() const {
uint64_t total = 0;
for (const auto& bin : _bins) {
total += bin.load();
}
return total;
}
Duration mean() const;
Duration percentile(double p) const;
void toStats(const std::string& prefix, std::vector<Stat>& stats);
// void toMetrics(MetricsBuilder& builder, const std::string& name, const std::vector<std::pair<std::string, std::string>>& tags);
void reset();
};