fix: Adds idle cpu config and forces on in debug (#790)

* Adds `core.enableIdleCPU` to modernuo.json
* Forces idle CPU on in DEBUG
* Reverts idle detection back to CPS
This commit is contained in:
Kamron Batman 2021-09-17 00:15:16 -07:00 • committed by GitHub
parent b6641c4853
commit 56c348ac68
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 52 additions and 80 deletions

View file

@ -155,6 +155,11 @@ namespace Server
set => _now = value;
}
private static long _cycleIndex = 1;
private static float[] _cyclesPerSecond = new float[100];
public static float CyclesPerSecond => _cyclesPerSecond[(_cycleIndex - 1) % _cyclesPerSecond.Length];
public static bool MultiProcessor { get; private set; }
public static int ProcessorCount { get; private set; }
@ -488,40 +493,52 @@ namespace Server
{
try
{
#if DEBUG
var idleCPU = true;
#else
var idleCPU = ServerConfiguration.GetOrUpdateSetting("core.enableIdleCPU", false);
#endif
long last = TickCount;
const int interval = 100;
int idleCount = 0;
const float ticksPerSecond = 1000 * interval;
int sample = 0;
while (!Closing)
{
_tickCount = TickCount;
_now = DateTime.UtcNow;
var events = Mobile.ProcessDeltaQueue();
events += Item.ProcessDeltaQueue();
events += Timer.Slice(_tickCount);
Mobile.ProcessDeltaQueue();
Item.ProcessDeltaQueue();
Timer.Slice(_tickCount);
// Handle networking
events += TcpServer.Slice();
events += NetState.HandleAllReceives();
events += NetState.Slice();
TcpServer.Slice();
NetState.HandleAllReceives();
NetState.Slice();
// Execute captured post-await methods (like Timer.Pause)
events += LoopContext.ExecuteTasks();
LoopContext.ExecuteTasks();
Timer.CheckTimerPool(); // Check for pool depletion so we can async refill it.
_tickCount = 0;
_now = DateTime.MinValue;
if (events > 0)
if (idleCPU && ++sample % interval == 0)
{
idleCount = 0;
continue;
}
var now = TickCount;
if (++idleCount > interval)
{
Thread.Sleep(1);
var cyclesPerSecond = ticksPerSecond / (now - last);
_cyclesPerSecond[_cycleIndex++ % _cyclesPerSecond.Length] = cyclesPerSecond;
last = now;
if (cyclesPerSecond > 80)
{
Thread.Sleep(2);
}
}
}
}