upload
This commit is contained in:
150
libs/vslam_common/src/stats.cc
Normal file
150
libs/vslam_common/src/stats.cc
Normal file
@@ -0,0 +1,150 @@
|
||||
#include "vslam/stats.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
namespace vslam {
|
||||
namespace {
|
||||
|
||||
int64_t ToNs(std::chrono::steady_clock::time_point tp) {
|
||||
return std::chrono::duration_cast<std::chrono::nanoseconds>(tp.time_since_epoch())
|
||||
.count();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// --- RateMonitor -----------------------------------------------------------
|
||||
|
||||
RateMonitor::RateMonitor(double window_sec) : window_sec_(window_sec) {}
|
||||
|
||||
void RateMonitor::Tick(std::chrono::steady_clock::time_point now) {
|
||||
const int64_t ns = ToNs(now);
|
||||
count_.fetch_add(1, std::memory_order_relaxed);
|
||||
last_ns_.store(ns, std::memory_order_relaxed);
|
||||
|
||||
const int64_t first = first_ns_.load(std::memory_order_relaxed);
|
||||
if (first == 0) {
|
||||
first_ns_.store(ns, std::memory_order_relaxed);
|
||||
window_count_.store(1, std::memory_order_relaxed);
|
||||
return;
|
||||
}
|
||||
|
||||
const uint64_t wc = window_count_.fetch_add(1, std::memory_order_relaxed) + 1;
|
||||
const double elapsed = static_cast<double>(ns - first) * 1e-9;
|
||||
if (elapsed >= window_sec_) {
|
||||
hz_.store(elapsed > 0.0 ? static_cast<double>(wc) / elapsed : 0.0,
|
||||
std::memory_order_relaxed);
|
||||
// Mo cua so moi. Khong dung cua so truot that (deque timestamp) vi o
|
||||
// 200 Hz thi chi phi bo nho va khoa khong dang -- sai so cua cua so nhay
|
||||
// la chap nhan duoc cho muc dich chan doan.
|
||||
first_ns_.store(ns, std::memory_order_relaxed);
|
||||
window_count_.store(0, std::memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
void RateMonitor::Tick() { Tick(std::chrono::steady_clock::now()); }
|
||||
|
||||
double RateMonitor::hz() const { return hz_.load(std::memory_order_relaxed); }
|
||||
|
||||
double RateMonitor::age_sec(std::chrono::steady_clock::time_point now) const {
|
||||
const int64_t last = last_ns_.load(std::memory_order_relaxed);
|
||||
if (last == 0) return std::numeric_limits<double>::infinity();
|
||||
return static_cast<double>(ToNs(now) - last) * 1e-9;
|
||||
}
|
||||
|
||||
void RateMonitor::Reset() {
|
||||
count_.store(0, std::memory_order_relaxed);
|
||||
first_ns_.store(0, std::memory_order_relaxed);
|
||||
last_ns_.store(0, std::memory_order_relaxed);
|
||||
window_count_.store(0, std::memory_order_relaxed);
|
||||
hz_.store(0.0, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
// --- LatencyMonitor --------------------------------------------------------
|
||||
|
||||
struct LatencyMonitor::Impl {
|
||||
mutable std::mutex mu;
|
||||
std::vector<double> ring;
|
||||
size_t head = 0;
|
||||
size_t filled = 0;
|
||||
double max_seen = 0.0;
|
||||
double last = 0.0;
|
||||
uint64_t count = 0;
|
||||
|
||||
explicit Impl(size_t window) : ring(window, 0.0) {}
|
||||
|
||||
// Goi khi da giu mu.
|
||||
double PercentileLocked(double p) const {
|
||||
if (filled == 0) return 0.0;
|
||||
std::vector<double> tmp(ring.begin(), ring.begin() + static_cast<long>(filled));
|
||||
const size_t idx = static_cast<size_t>(p * static_cast<double>(filled - 1));
|
||||
std::nth_element(tmp.begin(), tmp.begin() + static_cast<long>(idx), tmp.end());
|
||||
return tmp[idx];
|
||||
}
|
||||
};
|
||||
|
||||
LatencyMonitor::LatencyMonitor(size_t window)
|
||||
: impl_(std::make_unique<Impl>(window == 0 ? 1 : window)) {}
|
||||
|
||||
LatencyMonitor::~LatencyMonitor() = default;
|
||||
|
||||
void LatencyMonitor::Add(double ms) {
|
||||
std::lock_guard<std::mutex> lk(impl_->mu);
|
||||
impl_->ring[impl_->head] = ms;
|
||||
impl_->head = (impl_->head + 1) % impl_->ring.size();
|
||||
if (impl_->filled < impl_->ring.size()) impl_->filled++;
|
||||
if (ms > impl_->max_seen) impl_->max_seen = ms;
|
||||
impl_->last = ms;
|
||||
impl_->count++;
|
||||
}
|
||||
|
||||
double LatencyMonitor::p50() const {
|
||||
std::lock_guard<std::mutex> lk(impl_->mu);
|
||||
return impl_->PercentileLocked(0.50);
|
||||
}
|
||||
|
||||
double LatencyMonitor::p95() const {
|
||||
std::lock_guard<std::mutex> lk(impl_->mu);
|
||||
return impl_->PercentileLocked(0.95);
|
||||
}
|
||||
|
||||
double LatencyMonitor::max() const {
|
||||
std::lock_guard<std::mutex> lk(impl_->mu);
|
||||
return impl_->max_seen;
|
||||
}
|
||||
|
||||
double LatencyMonitor::last() const {
|
||||
std::lock_guard<std::mutex> lk(impl_->mu);
|
||||
return impl_->last;
|
||||
}
|
||||
|
||||
uint64_t LatencyMonitor::count() const {
|
||||
std::lock_guard<std::mutex> lk(impl_->mu);
|
||||
return impl_->count;
|
||||
}
|
||||
|
||||
void LatencyMonitor::Reset() {
|
||||
std::lock_guard<std::mutex> lk(impl_->mu);
|
||||
std::fill(impl_->ring.begin(), impl_->ring.end(), 0.0);
|
||||
impl_->head = 0;
|
||||
impl_->filled = 0;
|
||||
impl_->max_seen = 0.0;
|
||||
impl_->last = 0.0;
|
||||
impl_->count = 0;
|
||||
}
|
||||
|
||||
// --- ScopedLatency ---------------------------------------------------------
|
||||
|
||||
ScopedLatency::ScopedLatency(LatencyMonitor* m, const Clock* clock)
|
||||
: m_(m), t0_(clock != nullptr ? clock->Steady() : std::chrono::steady_clock::now()) {}
|
||||
|
||||
ScopedLatency::~ScopedLatency() {
|
||||
if (m_ == nullptr) return;
|
||||
const auto dt = std::chrono::steady_clock::now() - t0_;
|
||||
m_->Add(std::chrono::duration<double, std::milli>(dt).count());
|
||||
}
|
||||
|
||||
} // namespace vslam
|
||||
Reference in New Issue
Block a user