Skip to content

Commit

Permalink
Simplified debug timer.
Browse files Browse the repository at this point in the history
  • Loading branch information
TLeonardUK committed Mar 18, 2024
1 parent c5a12ff commit 3c256d9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 65 deletions.
3 changes: 2 additions & 1 deletion Source/Server/Server/ServerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void ServerManager::RunUntilQuit()
WriteLog(true, ConsoleColor::Grey, "", "Log", "%zi players | %zi servers | %.2f ms update | connections auth %.2f login %.2f game %.2f p/s | tcp in %.2f out %.2f kb/s | udp in %.2f out %.2f kb/s | database queries %.2f p/s ",
PlayerCount,
ServerInstances.size(),
Debug::AllServerUpdateTime.GetAverage(),
Debug::AllServerUpdateTime.GetAverage() * 1000.0f,
Debug::AuthConnections.GetAverageRate(),
Debug::LoginConnections.GetAverageRate(),
Debug::GameConnections.GetAverageRate(),
Expand All @@ -149,6 +149,7 @@ void ServerManager::RunUntilQuit()
}

DebugCounter::PollAll();
DebugTimer::PollAll();

std::this_thread::sleep_for(std::chrono::microseconds(100));
}
Expand Down
85 changes: 32 additions & 53 deletions Source/Shared/Core/Utils/DebugTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include "Shared/Core/Utils/DebugTimer.h"

#pragma optimize("", off)

DebugTimer::DebugTimer(const std::string& InName, double InRollingWindow)
: Name(InName)
, RollingWindow(InRollingWindow)
Expand All @@ -31,50 +33,17 @@ std::string DebugTimer::GetName()

double DebugTimer::GetAverage()
{
std::scoped_lock lock(DataMutex);

if (Samples.empty())
{
return 0.0;
}

double Average = 0.0f;

for (Sample& Sample : Samples)
{
Average += Sample.Value;
}

return Average / Samples.size();
return Average;
}

double DebugTimer::GetPeak()
{
std::scoped_lock lock(DataMutex);

double Peak = 0.0f;

for (Sample& Sample : Samples)
{
if (Sample.Value > Peak)
{
Peak = Sample.Value;
}
}

return Peak;
}

double DebugTimer::GetCurrent()
{
std::scoped_lock lock(DataMutex);

if (Samples.empty())
{
return 0.0;
}

return Samples.back().Value;
return Current;
}

std::vector<DebugTimer*> DebugTimer::GetTimers()
Expand All @@ -84,27 +53,37 @@ std::vector<DebugTimer*> DebugTimer::GetTimers()

void DebugTimer::AddSample(double Interval)
{
std::scoped_lock lock(DataMutex);
AverageSum += Interval;
AverageSamples += 1.0;
if (Interval > PeakTracker)
{
PeakTracker = Interval;
}
}

void DebugTimer::Poll()
{
double CurrentTime = GetHighResolutionSeconds();
double Elapsed = CurrentTime - AverageTimer;
if (Elapsed > RollingWindow)
{
float Scale = RollingWindow / Elapsed;
float Sample = (AverageSum / AverageSamples) * Scale;

Current = Sample;
Peak = PeakTracker;
Average = (Average * 0.9f) + (Sample * 0.1f);
AverageSum = 0.0;
AverageSamples = 0.0;
PeakTracker = 0.0;
AverageTimer = CurrentTime;
}
}

Sample NewSample;
NewSample.Time = CurrentTime;
NewSample.Value = Interval;
Samples.push_back(NewSample);

// Trim old samples from list.
double OldestTime = CurrentTime - RollingWindow;
while (!Samples.empty())
void DebugTimer::PollAll()
{
for (DebugTimer* instance : Registry)
{
Sample& FirstSample = Samples.front();
if (FirstSample.Time < OldestTime)
{
Samples.erase(Samples.begin());
}
else
{
break;
}
instance->Poll();
}
}
24 changes: 13 additions & 11 deletions Source/Shared/Core/Utils/DebugTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@
class DebugTimer
{
public:
DebugTimer(const std::string& name, double RollingWindow = 10.0);
DebugTimer(const std::string& name, double RollingWindow = 5.0);
~DebugTimer();

std::string GetName();
double GetAverage();
double GetPeak();
double GetCurrent();

void Poll();

static std::vector<DebugTimer*> GetTimers();
static void PollAll();

protected:
friend struct DebugTimerScope;
Expand All @@ -36,19 +39,18 @@ class DebugTimer
private:
inline static std::vector<DebugTimer*> Registry;

std::mutex DataMutex;

struct Sample
{
double Time;
double Value;
};

std::list<Sample> Samples;

double RollingWindow;
std::string Name;

double Current = 0.0;
double Peak = 0.0;
double PeakTracker = 0.0;

double Average = 0.0;
double AverageTimer = 0.0;
double AverageSum = 0.0;
double AverageSamples = 0.0;

};

struct DebugTimerScope
Expand Down

0 comments on commit 3c256d9

Please sign in to comment.