mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-06-09 16:24:22 +00:00
chore: simplify metrics (#1987)
* chore: simplify metrics rename to hpp and remove unused includes * feedback
This commit is contained in:
@@ -1,105 +1,77 @@
|
|||||||
#include "Metrics.hpp"
|
#include "Metrics.h"
|
||||||
|
|
||||||
|
#include "StringifiedEnum.h"
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
std::unordered_map<MetricVariable, Metric*> Metrics::m_Metrics = {};
|
namespace {
|
||||||
std::vector<MetricVariable> Metrics::m_Variables = {
|
std::unordered_map<MetricVariable, Metric> g_Metrics = {};
|
||||||
MetricVariable::GameLoop,
|
std::vector<MetricVariable> g_Variables = {
|
||||||
MetricVariable::PacketHandling,
|
MetricVariable::GameLoop,
|
||||||
MetricVariable::UpdateEntities,
|
MetricVariable::PacketHandling,
|
||||||
MetricVariable::UpdateSpawners,
|
MetricVariable::UpdateEntities,
|
||||||
MetricVariable::Physics,
|
MetricVariable::UpdateSpawners,
|
||||||
MetricVariable::UpdateReplica,
|
MetricVariable::Physics,
|
||||||
MetricVariable::Ghosting,
|
MetricVariable::UpdateReplica,
|
||||||
MetricVariable::CPUTime,
|
MetricVariable::Ghosting,
|
||||||
MetricVariable::Sleep,
|
MetricVariable::CPUTime,
|
||||||
MetricVariable::Frame,
|
MetricVariable::Sleep,
|
||||||
};
|
MetricVariable::Frame,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
void Metrics::AddMeasurement(MetricVariable variable, int64_t value) {
|
void Metrics::AddMeasurement(MetricVariable variable, int64_t value) {
|
||||||
const auto& iter = m_Metrics.find(variable);
|
auto& metric = g_Metrics[variable];
|
||||||
|
|
||||||
Metric* metric;
|
|
||||||
|
|
||||||
if (iter == m_Metrics.end()) {
|
|
||||||
metric = new Metric();
|
|
||||||
|
|
||||||
m_Metrics[variable] = metric;
|
|
||||||
} else {
|
|
||||||
metric = iter->second;
|
|
||||||
}
|
|
||||||
|
|
||||||
AddMeasurement(metric, value);
|
AddMeasurement(metric, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Metrics::AddMeasurement(Metric* metric, int64_t value) {
|
void Metrics::AddMeasurement(Metric& metric, int64_t value) {
|
||||||
const auto index = metric->measurementIndex;
|
const auto index = metric.measurementIndex;
|
||||||
|
|
||||||
metric->measurements[index] = value;
|
metric.measurements[index] = value;
|
||||||
|
|
||||||
if (metric->max == -1 || value > metric->max) {
|
if (metric.max == -1 || value > metric.max) {
|
||||||
metric->max = value;
|
metric.max = value;
|
||||||
} else if (metric->min == -1 || metric->min > value) {
|
} else if (metric.min == -1 || metric.min > value) {
|
||||||
metric->min = value;
|
metric.min = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (metric->measurementSize < MAX_MEASURMENT_POINTS) {
|
if (metric.measurementSize < MAX_MEASURMENT_POINTS) {
|
||||||
metric->measurementSize++;
|
metric.measurementSize++;
|
||||||
}
|
}
|
||||||
|
|
||||||
metric->measurementIndex = (index + 1) % MAX_MEASURMENT_POINTS;
|
metric.measurementIndex = (index + 1) % MAX_MEASURMENT_POINTS;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Metric* Metrics::GetMetric(MetricVariable variable) {
|
const Metric& Metrics::GetMetric(MetricVariable variable) {
|
||||||
const auto& iter = m_Metrics.find(variable);
|
auto& metric = g_Metrics[variable];
|
||||||
|
|
||||||
if (iter == m_Metrics.end()) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
Metric* metric = iter->second;
|
|
||||||
|
|
||||||
int64_t average = 0;
|
int64_t average = 0;
|
||||||
|
|
||||||
for (size_t i = 0; i < metric->measurementSize; i++) {
|
for (size_t i = 0; i < metric.measurementSize; i++) {
|
||||||
average += metric->measurements[i];
|
average += metric.measurements[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
average /= metric->measurementSize;
|
average /= metric.measurementSize;
|
||||||
|
|
||||||
metric->average = average;
|
metric.average = average;
|
||||||
|
|
||||||
return metric;
|
return metric;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Metrics::StartMeasurement(MetricVariable variable) {
|
void Metrics::StartMeasurement(MetricVariable variable) {
|
||||||
const auto& iter = m_Metrics.find(variable);
|
auto& metric = g_Metrics[variable];
|
||||||
|
|
||||||
Metric* metric;
|
metric.activeMeasurement = std::chrono::high_resolution_clock::now();
|
||||||
|
|
||||||
if (iter == m_Metrics.end()) {
|
|
||||||
metric = new Metric();
|
|
||||||
|
|
||||||
m_Metrics[variable] = metric;
|
|
||||||
} else {
|
|
||||||
metric = iter->second;
|
|
||||||
}
|
|
||||||
|
|
||||||
metric->activeMeasurement = std::chrono::high_resolution_clock::now();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Metrics::EndMeasurement(MetricVariable variable) {
|
void Metrics::EndMeasurement(MetricVariable variable) {
|
||||||
const auto end = std::chrono::high_resolution_clock::now();
|
const auto end = std::chrono::high_resolution_clock::now();
|
||||||
|
|
||||||
const auto& iter = m_Metrics.find(variable);
|
auto& metric = g_Metrics[variable];
|
||||||
|
|
||||||
if (iter == m_Metrics.end()) {
|
const auto elapsed = end - metric.activeMeasurement;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Metric* metric = iter->second;
|
|
||||||
|
|
||||||
const auto elapsed = end - metric->activeMeasurement;
|
|
||||||
|
|
||||||
const auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed).count();
|
const auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed).count();
|
||||||
|
|
||||||
@@ -110,44 +82,12 @@ float Metrics::ToMiliseconds(int64_t nanoseconds) {
|
|||||||
return static_cast<float>(nanoseconds) / 1e6;
|
return static_cast<float>(nanoseconds) / 1e6;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Metrics::MetricVariableToString(MetricVariable variable) {
|
const std::string_view Metrics::MetricVariableToString(MetricVariable variable) {
|
||||||
switch (variable) {
|
return StringifiedEnum::ToString(variable);
|
||||||
case MetricVariable::GameLoop:
|
|
||||||
return "GameLoop";
|
|
||||||
case MetricVariable::PacketHandling:
|
|
||||||
return "PacketHandling";
|
|
||||||
case MetricVariable::UpdateEntities:
|
|
||||||
return "UpdateEntities";
|
|
||||||
case MetricVariable::UpdateSpawners:
|
|
||||||
return "UpdateSpawners";
|
|
||||||
case MetricVariable::Physics:
|
|
||||||
return "Physics";
|
|
||||||
case MetricVariable::UpdateReplica:
|
|
||||||
return "UpdateReplica";
|
|
||||||
case MetricVariable::Sleep:
|
|
||||||
return "Sleep";
|
|
||||||
case MetricVariable::CPUTime:
|
|
||||||
return "CPUTime";
|
|
||||||
case MetricVariable::Frame:
|
|
||||||
return "Frame";
|
|
||||||
case MetricVariable::Ghosting:
|
|
||||||
return "Ghosting";
|
|
||||||
|
|
||||||
default:
|
|
||||||
return "Invalid";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::vector<MetricVariable>& Metrics::GetAllMetrics() {
|
const std::vector<MetricVariable>& Metrics::GetAllMetrics() {
|
||||||
return m_Variables;
|
return g_Variables;
|
||||||
}
|
|
||||||
|
|
||||||
void Metrics::Clear() {
|
|
||||||
for (const auto& pair : m_Metrics) {
|
|
||||||
delete pair.second;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_Metrics.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* RSS Memory utilities
|
/* RSS Memory utilities
|
||||||
|
|||||||
48
dCommon/Metrics.h
Normal file
48
dCommon/Metrics.h
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "dCommonVars.h"
|
||||||
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
#include <string_view>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
#define MAX_MEASURMENT_POINTS 1024
|
||||||
|
|
||||||
|
enum class MetricVariable : int32_t {
|
||||||
|
GameLoop,
|
||||||
|
PacketHandling,
|
||||||
|
UpdateEntities,
|
||||||
|
UpdateSpawners,
|
||||||
|
Physics,
|
||||||
|
UpdateReplica,
|
||||||
|
Ghosting,
|
||||||
|
CPUTime,
|
||||||
|
Sleep,
|
||||||
|
Frame,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Metric {
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace Metrics {
|
||||||
|
void AddMeasurement(MetricVariable variable, int64_t value);
|
||||||
|
void AddMeasurement(Metric& metric, int64_t value);
|
||||||
|
const Metric& GetMetric(MetricVariable variable);
|
||||||
|
void StartMeasurement(MetricVariable variable);
|
||||||
|
void EndMeasurement(MetricVariable variable);
|
||||||
|
float ToMiliseconds(int64_t nanoseconds);
|
||||||
|
const std::string_view MetricVariableToString(MetricVariable variable);
|
||||||
|
const std::vector<MetricVariable>& GetAllMetrics();
|
||||||
|
|
||||||
|
size_t GetPeakRSS();
|
||||||
|
size_t GetCurrentRSS();
|
||||||
|
size_t GetProcessID();
|
||||||
|
};
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include "dCommonVars.h"
|
|
||||||
#include <vector>
|
|
||||||
#include <map>
|
|
||||||
#include <unordered_map>
|
|
||||||
#include <chrono>
|
|
||||||
|
|
||||||
#define MAX_MEASURMENT_POINTS 1024
|
|
||||||
|
|
||||||
enum class MetricVariable : int32_t
|
|
||||||
{
|
|
||||||
GameLoop,
|
|
||||||
PacketHandling,
|
|
||||||
UpdateEntities,
|
|
||||||
UpdateSpawners,
|
|
||||||
Physics,
|
|
||||||
UpdateReplica,
|
|
||||||
Ghosting,
|
|
||||||
CPUTime,
|
|
||||||
Sleep,
|
|
||||||
Frame,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Metric
|
|
||||||
{
|
|
||||||
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:
|
|
||||||
~Metrics();
|
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
static size_t GetPeakRSS();
|
|
||||||
static size_t GetCurrentRSS();
|
|
||||||
static size_t GetProcessID();
|
|
||||||
|
|
||||||
static void Clear();
|
|
||||||
|
|
||||||
private:
|
|
||||||
Metrics();
|
|
||||||
|
|
||||||
static std::unordered_map<MetricVariable, Metric*> m_Metrics;
|
|
||||||
static std::vector<MetricVariable> m_Variables;
|
|
||||||
};
|
|
||||||
@@ -10,7 +10,6 @@
|
|||||||
#include "SkillComponent.h"
|
#include "SkillComponent.h"
|
||||||
#include "SwitchComponent.h"
|
#include "SwitchComponent.h"
|
||||||
#include "UserManager.h"
|
#include "UserManager.h"
|
||||||
#include "Metrics.hpp"
|
|
||||||
#include "dZoneManager.h"
|
#include "dZoneManager.h"
|
||||||
#include "MissionComponent.h"
|
#include "MissionComponent.h"
|
||||||
#include "Game.h"
|
#include "Game.h"
|
||||||
|
|||||||
@@ -18,7 +18,8 @@
|
|||||||
#include "DluAssert.h"
|
#include "DluAssert.h"
|
||||||
|
|
||||||
#include "CDActivitiesTable.h"
|
#include "CDActivitiesTable.h"
|
||||||
#include "Metrics.hpp"
|
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
namespace LeaderboardManager {
|
namespace LeaderboardManager {
|
||||||
std::map<GameID, Leaderboard::Type> leaderboardCache;
|
std::map<GameID, Leaderboard::Type> leaderboardCache;
|
||||||
|
|||||||
@@ -23,7 +23,6 @@
|
|||||||
#include "SkillComponent.h"
|
#include "SkillComponent.h"
|
||||||
#include "QuickBuildComponent.h"
|
#include "QuickBuildComponent.h"
|
||||||
#include "DestroyableComponent.h"
|
#include "DestroyableComponent.h"
|
||||||
#include "Metrics.hpp"
|
|
||||||
#include "CDComponentsRegistryTable.h"
|
#include "CDComponentsRegistryTable.h"
|
||||||
#include "CDPhysicsComponentTable.h"
|
#include "CDPhysicsComponentTable.h"
|
||||||
#include "dNavMesh.h"
|
#include "dNavMesh.h"
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
#include "dpShapeSphere.h"
|
#include "dpShapeSphere.h"
|
||||||
#include "dZoneManager.h"
|
#include "dZoneManager.h"
|
||||||
#include "EntityInfo.h"
|
#include "EntityInfo.h"
|
||||||
#include "Metrics.hpp"
|
#include "Metrics.h"
|
||||||
#include "PlayerManager.h"
|
#include "PlayerManager.h"
|
||||||
#include "SlashCommandHandler.h"
|
#include "SlashCommandHandler.h"
|
||||||
#include "UserManager.h"
|
#include "UserManager.h"
|
||||||
@@ -1288,18 +1288,14 @@ namespace DEVGMCommands {
|
|||||||
response.Insert("serverInfo", true);
|
response.Insert("serverInfo", true);
|
||||||
auto* info = response.InsertArray("data");
|
auto* info = response.InsertArray("data");
|
||||||
for (const auto variable : Metrics::GetAllMetrics()) {
|
for (const auto variable : Metrics::GetAllMetrics()) {
|
||||||
auto& metricData = info->PushDebug(StringifiedEnum::ToString(variable));
|
auto& metricData = info->PushDebug(Metrics::MetricVariableToString(variable));
|
||||||
|
|
||||||
auto* metric = Metrics::GetMetric(variable);
|
const auto& metric = Metrics::GetMetric(variable);
|
||||||
|
|
||||||
if (metric == nullptr) {
|
metricData.PushDebug<AMFStringValue>("Maximum") = std::to_string(Metrics::ToMiliseconds(metric.max)) + "ms";
|
||||||
continue;
|
metricData.PushDebug<AMFStringValue>("Minimum") = std::to_string(Metrics::ToMiliseconds(metric.min)) + "ms";
|
||||||
}
|
metricData.PushDebug<AMFStringValue>("Average") = std::to_string(Metrics::ToMiliseconds(metric.average)) + "ms";
|
||||||
|
metricData.PushDebug<AMFStringValue>("Measurements Count") = std::to_string(metric.measurementSize);
|
||||||
metricData.PushDebug<AMFStringValue>("Maximum") = std::to_string(Metrics::ToMiliseconds(metric->max)) + "ms";
|
|
||||||
metricData.PushDebug<AMFStringValue>("Minimum") = std::to_string(Metrics::ToMiliseconds(metric->min)) + "ms";
|
|
||||||
metricData.PushDebug<AMFStringValue>("Average") = std::to_string(Metrics::ToMiliseconds(metric->average)) + "ms";
|
|
||||||
metricData.PushDebug<AMFStringValue>("Measurements Count") = std::to_string(metric->measurementSize);
|
|
||||||
}
|
}
|
||||||
auto& processInfo = info->PushDebug("Process Info");
|
auto& processInfo = info->PushDebug("Process Info");
|
||||||
processInfo.PushDebug<AMFStringValue>("Peak RSS") = std::to_string(static_cast<double>(Metrics::GetPeakRSS()) / 1.024e6) + "MB";
|
processInfo.PushDebug<AMFStringValue>("Peak RSS") = std::to_string(static_cast<double>(Metrics::GetPeakRSS()) / 1.024e6) + "MB";
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
#include "dConfig.h"
|
#include "dConfig.h"
|
||||||
#include "dpWorld.h"
|
#include "dpWorld.h"
|
||||||
#include "dZoneManager.h"
|
#include "dZoneManager.h"
|
||||||
#include "Metrics.hpp"
|
#include "Metrics.h"
|
||||||
#include "PerformanceManager.h"
|
#include "PerformanceManager.h"
|
||||||
#include "Diagnostics.h"
|
#include "Diagnostics.h"
|
||||||
#include "BinaryPathFinder.h"
|
#include "BinaryPathFinder.h"
|
||||||
@@ -1538,7 +1538,6 @@ void FinalizeShutdown() {
|
|||||||
LOG("Shutdown complete, zone (%i), instance (%i)", Game::server->GetZoneID(), g_InstanceID);
|
LOG("Shutdown complete, zone (%i), instance (%i)", Game::server->GetZoneID(), g_InstanceID);
|
||||||
|
|
||||||
//Delete our objects here:
|
//Delete our objects here:
|
||||||
Metrics::Clear();
|
|
||||||
dpWorld::Shutdown();
|
dpWorld::Shutdown();
|
||||||
Database::Destroy("WorldServer");
|
Database::Destroy("WorldServer");
|
||||||
if (Game::chatFilter) delete Game::chatFilter;
|
if (Game::chatFilter) delete Game::chatFilter;
|
||||||
|
|||||||
Reference in New Issue
Block a user