feat(core): Adds CPU conservation (#364)

This commit is contained in:
Kamron Batman 2020-12-26 13:53:43 -08:00 • committed by GitHub
parent 03d0a4664e
commit d73afdba8e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -153,6 +153,8 @@ namespace Server
} }
} }
public static bool ConserveCPU { get; private set; }
public static string Arguments public static string Arguments
{ {
get get
@ -199,6 +201,11 @@ namespace Server
public static bool EJ => Expansion >= Expansion.EJ; public static bool EJ => Expansion >= Expansion.EJ;
public static void Configure()
{
ConserveCPU = ServerConfiguration.GetOrUpdateSetting("core.conserveCpu", true);
}
public static string FindDataFile(string path, bool throwNotFound = true, bool warnNotFound = false) public static string FindDataFile(string path, bool throwNotFound = true, bool warnNotFound = false)
{ {
string fullPath = null; string fullPath = null;
@ -464,7 +471,7 @@ namespace Server
{ {
try try
{ {
long now, last = TickCount; long last = TickCount;
const int sampleInterval = 100; const int sampleInterval = 100;
const float ticksPerSecond = 1000.0f * sampleInterval; const float ticksPerSecond = 1000.0f * sampleInterval;
@ -487,16 +494,21 @@ namespace Server
NetState.ProcessDisposedQueue(); NetState.ProcessDisposedQueue();
// Execute other stuff // Execute other stuff
if (sample++ % sampleInterval != 0) if (sample++ % sampleInterval != 0)
{ {
continue; continue;
} }
now = TickCount; var now = TickCount;
m_CyclesPerSecond[m_CycleIndex % m_CyclesPerSecond.Length] = ticksPerSecond / (now - last); var cyclesPerSecond = ticksPerSecond / (now - last);
m_CyclesPerSecond[m_CycleIndex % m_CyclesPerSecond.Length] = cyclesPerSecond;
m_CycleIndex = Math.Max(unchecked(m_CycleIndex + 1), 0); m_CycleIndex = Math.Max(unchecked(m_CycleIndex + 1), 0);
last = now; last = now;
if (ConserveCPU && cyclesPerSecond >= 100)
{
Thread.Sleep(1);
}
} }
} }
catch (Exception e) catch (Exception e)