From 469b89370d259bf4eac2a7c750750e72041ec2bf Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Mon, 30 Oct 2023 18:05:32 -0700 Subject: [PATCH] fix: Adds back Average CPS to Admin Gump (#1573) --- Projects/Server/Main.cs | 29 ++++++++++++++++++--------- Projects/UOContent/Gumps/AdminGump.cs | 4 ++++ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/Projects/Server/Main.cs b/Projects/Server/Main.cs index a39a93814..d3d12556e 100644 --- a/Projects/Server/Main.cs +++ b/Projects/Server/Main.cs @@ -144,6 +144,7 @@ public static class Core } var timestamp = Stopwatch.GetTimestamp(); + return timestamp > _maxTickCountBeforePrecisionLoss ? timestamp / _ticksPerMillisecond // No precision loss @@ -167,10 +168,12 @@ public static class Core public static long Uptime => Thread.CurrentThread != Thread ? 0 : TickCount - _firstTick; - private static long _cycleIndex = 1; - private static float[] _cyclesPerSecond = new float[128]; + private static long _cycleIndex; + private static readonly double[] _cyclesPerSecond = new double[128]; - public static float CyclesPerSecond => _cyclesPerSecond[(_cycleIndex - 1) % _cyclesPerSecond.Length]; + public static double CyclesPerSecond => _cyclesPerSecond[_cycleIndex]; + + public static double AverageCPS => _cyclesPerSecond.Average(); public static bool MultiProcessor { get; private set; } @@ -534,9 +537,10 @@ public static class Core var idleCPU = ServerConfiguration.GetOrUpdateSetting("core.enableIdleCPU", false); #endif - long last = TickCount; + var cycleCount = _cyclesPerSecond.Length; + long last = Stopwatch.GetTimestamp(); const int interval = 100; - const float ticksPerSecond = 1000 * interval; + double frequency = Stopwatch.Frequency * interval; int sample = 0; @@ -568,15 +572,20 @@ public static class Core _tickCount = 0; _now = DateTime.MinValue; - if (idleCPU && ++sample % interval == 0) + if (++sample % interval == 0) { - var now = TickCount; + var now = Stopwatch.GetTimestamp(); + + var cyclesPerSecond = frequency / (now - last); + _cyclesPerSecond[_cycleIndex++] = cyclesPerSecond; + if (_cycleIndex == cycleCount) + { + _cycleIndex = 0; + } - var cyclesPerSecond = ticksPerSecond / (now - last); - _cyclesPerSecond[_cycleIndex++ % _cyclesPerSecond.Length] = cyclesPerSecond; last = now; - if (cyclesPerSecond > 80) + if (idleCPU && cyclesPerSecond > 125) { Thread.Sleep(2); } diff --git a/Projects/UOContent/Gumps/AdminGump.cs b/Projects/UOContent/Gumps/AdminGump.cs index 16b9c8357..8fca4fff6 100644 --- a/Projects/UOContent/Gumps/AdminGump.cs +++ b/Projects/UOContent/Gumps/AdminGump.cs @@ -226,6 +226,10 @@ namespace Server.Gumps } case AdminGumpPage.Information_Perf: { + AddLabel(20, 130, LabelHue, "Cycles Per Second:"); + AddLabel(40, 150, LabelHue, $"Current: {Core.CyclesPerSecond:N2}"); + AddLabel(40, 170, LabelHue, $"Average: {Core.AverageCPS:N2}"); + using var sb = ValueStringBuilder.Create(); ThreadPool.GetAvailableThreads(out var curUser, out var curIOCP);