DarkflameServer/dCommon/Metrics.hpp

65 lines
1.4 KiB
C++
Raw Normal View History

#pragma once
#include "dCommonVars.h"
#include <vector>
#include <map>
#include <unordered_map>
#include <chrono>
#define MAX_MEASURMENT_POINTS 1024
enum class MetricVariable : int32_t
{
2022-07-28 13:39:57 +00:00
GameLoop,
PacketHandling,
UpdateEntities,
UpdateSpawners,
Physics,
UpdateReplica,
Ghosting,
CPUTime,
Sleep,
Frame,
2022-09-04 10:47:53 +00:00
#ifdef BUILD_VISUAL_DEBUGGER
VisualDebugger
#endif
};
struct Metric
{
2022-07-28 13:39:57 +00:00
int64_t measurements[MAX_MEASURMENT_POINTS] = {};
size_t measurementIndex = 0;
size_t measurementSize = 0;
int64_t max = -1;
int64_t min = -1;
int64_t average = 0;
std::chrono::time_point<std::chrono::high_resolution_clock> activeMeasurement;
};
class Metrics
{
public:
2022-07-28 13:39:57 +00:00
~Metrics();
2022-07-28 13:39:57 +00:00
static void AddMeasurement(MetricVariable variable, int64_t value);
static void AddMeasurement(Metric* metric, int64_t value);
static const Metric* GetMetric(MetricVariable variable);
static void StartMeasurement(MetricVariable variable);
static void EndMeasurement(MetricVariable variable);
static float ToMiliseconds(int64_t nanoseconds);
static std::string MetricVariableToString(MetricVariable variable);
static const std::vector<MetricVariable>& GetAllMetrics();
2022-07-28 13:39:57 +00:00
static size_t GetPeakRSS();
static size_t GetCurrentRSS();
static size_t GetProcessID();
2022-07-28 13:39:57 +00:00
static void Clear();
private:
2022-07-28 13:39:57 +00:00
Metrics();
2022-07-28 13:39:57 +00:00
static std::unordered_map<MetricVariable, Metric*> m_Metrics;
static std::vector<MetricVariable> m_Variables;
};