126 lines
3.6 KiB
C++
126 lines
3.6 KiB
C++
#ifndef VSLAM_STATS_H
|
|
#define VSLAM_STATS_H
|
|
|
|
// Do dac runtime. Khong phai tien nghi -- day la thu ROS khong cho va vi vay
|
|
// repo cu phai doan mo:
|
|
// * khong biet mot subscriber bo lo bao nhieu ban tin (queue ROS drop im lang)
|
|
// * khong biet mot chuoi nodelet tre bao nhieu
|
|
// Moi Channel va moi stage deu phai bao cao duoc so lieu nay ra /api/status.
|
|
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <string>
|
|
|
|
#include "vslam/clock.h"
|
|
|
|
namespace vslam {
|
|
|
|
// Do tan so bang cua so truot, dung steady_clock.
|
|
// An toan cho mot nguoi ghi + nhieu nguoi doc.
|
|
class RateMonitor {
|
|
public:
|
|
explicit RateMonitor(double window_sec = 2.0);
|
|
|
|
// Goi moi lan co su kien.
|
|
void Tick(std::chrono::steady_clock::time_point now);
|
|
void Tick(); // dung steady_clock::now(), chi cho main/tool
|
|
|
|
// Hz uoc luong trong cua so. 0 neu chua du mau.
|
|
double hz() const;
|
|
|
|
// Giay ke tu su kien cuoi. +inf neu chua co su kien nao.
|
|
double age_sec(std::chrono::steady_clock::time_point now) const;
|
|
|
|
uint64_t count() const { return count_.load(std::memory_order_relaxed); }
|
|
void Reset();
|
|
|
|
private:
|
|
const double window_sec_;
|
|
std::atomic<uint64_t> count_{0};
|
|
std::atomic<int64_t> first_ns_{0}; // moc cua so hien tai
|
|
std::atomic<int64_t> last_ns_{0};
|
|
std::atomic<uint64_t> window_count_{0};
|
|
std::atomic<double> hz_{0.0};
|
|
};
|
|
|
|
// Dem su kien bi bo. Chia hai loai vi chung co nguyen nhan khac han nhau.
|
|
struct DropCounter {
|
|
std::atomic<uint64_t> total{0};
|
|
|
|
void Add(uint64_t n = 1) { total.fetch_add(n, std::memory_order_relaxed); }
|
|
uint64_t get() const { return total.load(std::memory_order_relaxed); }
|
|
void Reset() { total.store(0, std::memory_order_relaxed); }
|
|
};
|
|
|
|
// So lieu cua mot Channel.
|
|
struct ChannelStats {
|
|
std::string name;
|
|
uint64_t capacity = 0;
|
|
uint64_t published = 0; // tong so ban tin da Publish()
|
|
uint64_t overwritten = 0; // so ban tin bi ghi de truoc khi moi reader doc xong
|
|
uint64_t readers = 0;
|
|
uint64_t max_reader_lag = 0; // reader tut hau nhat, tinh bang so ban tin
|
|
uint64_t total_reader_drops = 0;
|
|
double hz = 0.0;
|
|
};
|
|
|
|
// So lieu cua ApproxTimeSync.
|
|
struct SyncStats {
|
|
uint64_t matched = 0;
|
|
uint64_t dropped_slop = 0; // co ban tin nhung lech thoi gian qua max_slop
|
|
uint64_t dropped_timeout = 0; // het han cho, phat voi input tuy chon de trong
|
|
uint64_t dropped_queue = 0; // pivot bi day ra khoi hang doi truoc khi khop
|
|
double last_slop_sec = 0.0;
|
|
};
|
|
|
|
// So lieu cua mot driver phan cung.
|
|
struct DriverStats {
|
|
double hz = 0.0;
|
|
uint64_t frames = 0;
|
|
uint64_t errors = 0;
|
|
uint64_t packets_lost = 0; // LiDAR: suy ra tu do dai sweep
|
|
uint64_t short_sweeps = 0; // sweep thieu tia
|
|
double stamp_skew_ms = 0.0; // MCU: lech dong ho host <-> thiet bi
|
|
double clock_skew_ppm = 0.0; // MCU: he so skew cua ClockSync, tinh theo ppm
|
|
bool connected = false;
|
|
std::string last_error;
|
|
};
|
|
|
|
// Do thoi luong mot doan code, giu p50/p95/max.
|
|
// Dung cho process_ms cua SLAM va cho vong perception.
|
|
class LatencyMonitor {
|
|
public:
|
|
explicit LatencyMonitor(size_t window = 128);
|
|
~LatencyMonitor();
|
|
|
|
void Add(double ms);
|
|
|
|
double p50() const;
|
|
double p95() const;
|
|
double max() const;
|
|
double last() const;
|
|
uint64_t count() const;
|
|
void Reset();
|
|
|
|
private:
|
|
struct Impl;
|
|
std::unique_ptr<Impl> impl_;
|
|
};
|
|
|
|
// RAII: do thoi gian song cua object roi day vao LatencyMonitor.
|
|
class ScopedLatency {
|
|
public:
|
|
ScopedLatency(LatencyMonitor* m, const Clock* clock);
|
|
~ScopedLatency();
|
|
|
|
private:
|
|
LatencyMonitor* m_;
|
|
std::chrono::steady_clock::time_point t0_;
|
|
};
|
|
|
|
} // namespace vslam
|
|
|
|
#endif // VSLAM_STATS_H
|