2021-12-05 17:54:36 +00:00
|
|
|
#include "Metrics.hpp"
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
|
|
|
|
std::unordered_map<MetricVariable, Metric*> Metrics::m_Metrics = {};
|
|
|
|
std::vector<MetricVariable> Metrics::m_Variables = {
|
2022-07-28 13:39:57 +00:00
|
|
|
MetricVariable::GameLoop,
|
|
|
|
MetricVariable::PacketHandling,
|
|
|
|
MetricVariable::UpdateEntities,
|
|
|
|
MetricVariable::UpdateSpawners,
|
|
|
|
MetricVariable::Physics,
|
|
|
|
MetricVariable::UpdateReplica,
|
|
|
|
MetricVariable::Ghosting,
|
|
|
|
MetricVariable::CPUTime,
|
|
|
|
MetricVariable::Sleep,
|
|
|
|
MetricVariable::Frame,
|
2023-04-13 07:45:03 +00:00
|
|
|
MetricVariable::Leaderboard,
|
2021-12-05 17:54:36 +00:00
|
|
|
};
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void Metrics::AddMeasurement(MetricVariable variable, int64_t value) {
|
|
|
|
const auto& iter = m_Metrics.find(variable);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
Metric* metric;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (iter == m_Metrics.end()) {
|
|
|
|
metric = new Metric();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
m_Metrics[variable] = metric;
|
|
|
|
} else {
|
|
|
|
metric = iter->second;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
AddMeasurement(metric, value);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void Metrics::AddMeasurement(Metric* metric, int64_t value) {
|
|
|
|
const auto index = metric->measurementIndex;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
metric->measurements[index] = value;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (metric->max == -1 || value > metric->max) {
|
|
|
|
metric->max = value;
|
|
|
|
} else if (metric->min == -1 || metric->min > value) {
|
|
|
|
metric->min = value;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (metric->measurementSize < MAX_MEASURMENT_POINTS) {
|
|
|
|
metric->measurementSize++;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
metric->measurementIndex = (index + 1) % MAX_MEASURMENT_POINTS;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
const Metric* Metrics::GetMetric(MetricVariable variable) {
|
|
|
|
const auto& iter = m_Metrics.find(variable);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (iter == m_Metrics.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
Metric* metric = iter->second;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
int64_t average = 0;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (size_t i = 0; i < metric->measurementSize; i++) {
|
|
|
|
average += metric->measurements[i];
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
average /= metric->measurementSize;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
metric->average = average;
|
|
|
|
|
|
|
|
return metric;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void Metrics::StartMeasurement(MetricVariable variable) {
|
|
|
|
const auto& iter = m_Metrics.find(variable);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
Metric* metric;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (iter == m_Metrics.end()) {
|
|
|
|
metric = new Metric();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
m_Metrics[variable] = metric;
|
|
|
|
} else {
|
|
|
|
metric = iter->second;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
metric->activeMeasurement = std::chrono::high_resolution_clock::now();
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void Metrics::EndMeasurement(MetricVariable variable) {
|
|
|
|
const auto end = std::chrono::high_resolution_clock::now();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
const auto& iter = m_Metrics.find(variable);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (iter == m_Metrics.end()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
Metric* metric = iter->second;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
const auto elapsed = end - metric->activeMeasurement;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
const auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed).count();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
AddMeasurement(metric, nanoseconds);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
float Metrics::ToMiliseconds(int64_t nanoseconds) {
|
|
|
|
return (float)nanoseconds / 1e6;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
std::string Metrics::MetricVariableToString(MetricVariable variable) {
|
|
|
|
switch (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";
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
const std::vector<MetricVariable>& Metrics::GetAllMetrics() {
|
|
|
|
return m_Variables;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void Metrics::Clear() {
|
|
|
|
for (const auto& pair : m_Metrics) {
|
|
|
|
delete pair.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Metrics.clear();
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* RSS Memory utilities
|
|
|
|
*
|
|
|
|
* Author: David Robert Nadeau
|
|
|
|
* Site: http://NadeauSoftware.com/
|
|
|
|
* License: Creative Commons Attribution 3.0 Unported License
|
|
|
|
* http://creativecommons.org/licenses/by/3.0/deed.en_US
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <windows.h>
|
|
|
|
#include <psapi.h>
|
|
|
|
|
|
|
|
#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
|
|
|
|
#if defined(__APPLE__) && defined(__MACH__)
|
|
|
|
#include <mach/mach.h>
|
|
|
|
|
|
|
|
#elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <procfs.h>
|
|
|
|
|
|
|
|
#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#else
|
|
|
|
#error "Cannot define getPeakRSS( ) or getCurrentRSS( ) for an unknown OS."
|
|
|
|
#endif
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Returns the peak (maximum so far) resident set size (physical
|
|
|
|
* memory use) measured in bytes, or zero if the value cannot be
|
|
|
|
* determined on this OS.
|
|
|
|
*/
|
|
|
|
size_t Metrics::GetPeakRSS() {
|
2021-12-05 17:54:36 +00:00
|
|
|
#if defined(_WIN32)
|
2022-07-28 13:39:57 +00:00
|
|
|
/* Windows -------------------------------------------------- */
|
|
|
|
PROCESS_MEMORY_COUNTERS info;
|
|
|
|
GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
|
|
|
|
return (size_t)info.PeakWorkingSetSize;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
#elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
|
2022-07-28 13:39:57 +00:00
|
|
|
/* AIX and Solaris ------------------------------------------ */
|
|
|
|
struct psinfo psinfo;
|
|
|
|
int fd = -1;
|
|
|
|
if ((fd = open("/proc/self/psinfo", O_RDONLY)) == -1)
|
|
|
|
return (size_t)0L; /* Can't open? */
|
|
|
|
if (read(fd, &psinfo, sizeof(psinfo)) != sizeof(psinfo)) {
|
|
|
|
close(fd);
|
|
|
|
return (size_t)0L; /* Can't read? */
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return (size_t)(psinfo.pr_rssize * 1024L);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
|
2022-07-28 13:39:57 +00:00
|
|
|
/* BSD, Linux, and OSX -------------------------------------- */
|
|
|
|
struct rusage rusage;
|
|
|
|
getrusage(RUSAGE_SELF, &rusage);
|
2021-12-05 17:54:36 +00:00
|
|
|
#if defined(__APPLE__) && defined(__MACH__)
|
2022-07-28 13:39:57 +00:00
|
|
|
return (size_t)rusage.ru_maxrss;
|
2021-12-05 17:54:36 +00:00
|
|
|
#else
|
2022-07-28 13:39:57 +00:00
|
|
|
return (size_t)(rusage.ru_maxrss * 1024L);
|
2021-12-05 17:54:36 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#else
|
2022-07-28 13:39:57 +00:00
|
|
|
/* Unknown OS ----------------------------------------------- */
|
|
|
|
return (size_t)0L; /* Unsupported. */
|
2021-12-05 17:54:36 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current resident set size (physical memory use) measured
|
|
|
|
* in bytes, or zero if the value cannot be determined on this OS.
|
|
|
|
*/
|
2022-07-28 13:39:57 +00:00
|
|
|
size_t Metrics::GetCurrentRSS() {
|
2021-12-05 17:54:36 +00:00
|
|
|
#if defined(_WIN32)
|
2022-07-28 13:39:57 +00:00
|
|
|
/* Windows -------------------------------------------------- */
|
|
|
|
PROCESS_MEMORY_COUNTERS info;
|
|
|
|
GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
|
|
|
|
return (size_t)info.WorkingSetSize;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
#elif defined(__APPLE__) && defined(__MACH__)
|
2022-07-28 13:39:57 +00:00
|
|
|
/* OSX ------------------------------------------------------ */
|
|
|
|
struct mach_task_basic_info info;
|
|
|
|
mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
|
|
|
|
if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO,
|
|
|
|
(task_info_t)&info, &infoCount) != KERN_SUCCESS)
|
|
|
|
return (size_t)0L; /* Can't access? */
|
|
|
|
return (size_t)info.resident_size;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
|
2022-07-28 13:39:57 +00:00
|
|
|
/* Linux ---------------------------------------------------- */
|
|
|
|
long rss = 0L;
|
|
|
|
FILE* fp = NULL;
|
|
|
|
if ((fp = fopen("/proc/self/statm", "r")) == NULL)
|
|
|
|
return (size_t)0L; /* Can't open? */
|
|
|
|
if (fscanf(fp, "%*s%ld", &rss) != 1) {
|
|
|
|
fclose(fp);
|
|
|
|
return (size_t)0L; /* Can't read? */
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
return (size_t)rss * (size_t)sysconf(_SC_PAGESIZE);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
#else
|
2022-07-28 13:39:57 +00:00
|
|
|
/* AIX, BSD, Solaris, and Unknown OS ------------------------ */
|
|
|
|
return (size_t)0L; /* Unsupported. */
|
2021-12-05 17:54:36 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
size_t Metrics::GetProcessID() {
|
2021-12-05 17:54:36 +00:00
|
|
|
#if defined(_WIN32)
|
2022-07-28 13:39:57 +00:00
|
|
|
return GetCurrentProcessId();
|
2021-12-05 17:54:36 +00:00
|
|
|
#else
|
2022-07-28 13:39:57 +00:00
|
|
|
return getpid();
|
2021-12-05 17:54:36 +00:00
|
|
|
#endif
|
|
|
|
}
|