_adaptive sleep
This commit is contained in:
@@ -165,16 +165,41 @@ int32 Engine::Main(const Char* cmdLine)
|
||||
while (!ShouldExit())
|
||||
{
|
||||
// Reduce CPU usage by introducing idle time if the engine is running very fast and has enough time to spend
|
||||
if ((useSleep && Time::UpdateFPS > ZeroTolerance) || !Platform::GetHasFocus())
|
||||
if ((useSleep && Time::UpdateFPS > ZeroTolerance && Time::PhysicsFPS > ZeroTolerance && Time::DrawFPS > ZeroTolerance))
|
||||
{
|
||||
double nextTick = Time::GetNextTick();
|
||||
double timeToTick = nextTick - Platform::GetTimeSeconds();
|
||||
|
||||
// Sleep less than needed, some platforms may sleep slightly more than requested
|
||||
if (timeToTick > 0.002)
|
||||
if (timeToTick > 0)
|
||||
{
|
||||
PROFILE_CPU_NAMED("Idle");
|
||||
Platform::Sleep(1);
|
||||
// Sleep less than needed, some platforms may sleep slightly more than requested
|
||||
static double adaptiveMinSleep = 0.00001;
|
||||
if (timeToTick > adaptiveMinSleep)
|
||||
{
|
||||
PROFILE_CPU_NAMED("Idle");
|
||||
while (timeToTick > adaptiveMinSleep)
|
||||
{
|
||||
Platform::Sleep(1);
|
||||
//Platform::Sleep(-1111);
|
||||
timeToTick = nextTick - Platform::GetTimeSeconds();
|
||||
}
|
||||
}
|
||||
|
||||
if (timeToTick < 0)
|
||||
{
|
||||
adaptiveMinSleep += -timeToTick * 0.15;
|
||||
//Log::Logger::Write(LogType::Info, TEXT("{0}, adaptive: {1}"), -timeToTick * 1000.0, adaptiveMinSleep);
|
||||
}
|
||||
else
|
||||
{
|
||||
adaptiveMinSleep -= 0.000000001;
|
||||
PROFILE_CPU_NAMED("IdleBusyLoop");
|
||||
while (timeToTick > 0)
|
||||
{
|
||||
Platform::Sleep(-1111);
|
||||
timeToTick = nextTick - Platform::GetTimeSeconds();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,6 +243,12 @@ int32 Engine::Main(const Char* cmdLine)
|
||||
Time::OnEndDraw();
|
||||
FrameMark;
|
||||
}
|
||||
|
||||
/*{
|
||||
PROFILE_CPU_NAMED("CollectResults");
|
||||
// Collect physics simulation results (does nothing if Simulate hasn't been called in the previous loop step)
|
||||
Physics::CollectResults();
|
||||
}*/
|
||||
}
|
||||
|
||||
// Call on exit event
|
||||
|
||||
@@ -370,6 +370,15 @@ void Win32Platform::SetThreadAffinityMask(uint64 affinityMask)
|
||||
|
||||
void Win32Platform::Sleep(int32 milliseconds)
|
||||
{
|
||||
if (milliseconds < 0)
|
||||
{
|
||||
::SwitchToThread();
|
||||
return;
|
||||
}
|
||||
|
||||
//::Sleep(milliseconds);
|
||||
//return;
|
||||
|
||||
static thread_local HANDLE timer = NULL;
|
||||
if (timer == NULL)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user