Merge pull request #8328 from ZappoMan/punishSlowScripts

Fix punished scripts stopping behavior and application shutdown while scripts being punished
This commit is contained in:
Zach Pomerantz 2016-07-28 17:42:02 -07:00 committed by GitHub
commit a9c6d69aa6
2 changed files with 13 additions and 9 deletions

View file

@ -4685,7 +4685,8 @@ void Application::packetSent(quint64 length) {
void Application::registerScriptEngineWithApplicationServices(ScriptEngine* scriptEngine) { void Application::registerScriptEngineWithApplicationServices(ScriptEngine* scriptEngine) {
scriptEngine->setEmitScriptUpdatesFunction([this]() { scriptEngine->setEmitScriptUpdatesFunction([this]() {
return isPhysicsEnabled(); SharedNodePointer entityServerNode = DependencyManager::get<NodeList>()->soloNodeOfType(NodeType::EntityServer);
return !entityServerNode || isPhysicsEnabled();
}); });
// setup the packet senders and jurisdiction listeners of the script engine's scripting interfaces so // setup the packet senders and jurisdiction listeners of the script engine's scripting interfaces so

View file

@ -839,21 +839,24 @@ void ScriptEngine::run() {
// calculate a sleepUntil to be the time from our start time until the original target // calculate a sleepUntil to be the time from our start time until the original target
// sleepUntil for this frame. // sleepUntil for this frame.
const std::chrono::microseconds FRAME_DURATION(USECS_PER_SECOND / SCRIPT_FPS + 1); const std::chrono::microseconds FRAME_DURATION(USECS_PER_SECOND / SCRIPT_FPS + 1);
clock::time_point sleepUntil(startTime + thisFrame++ * FRAME_DURATION); clock::time_point targetSleepUntil(startTime + thisFrame++ * FRAME_DURATION);
// However, if our sleepUntil is not at least our average update time into the future // However, if our sleepUntil is not at least our average update time into the future
// it means our script is taking too long in it's updates, and we want to punish the // it means our script is taking too long in it's updates, and we want to punish the
// script a little bit. So we will force the sleepUntil to be at least our averageUpdate // script a little bit. So we will force the sleepUntil to be at least our averageUpdate
// time into the future. // time into the future.
auto wouldSleep = (sleepUntil - clock::now()); auto averageUpdate = totalUpdates / thisFrame;
auto avgerageUpdate = totalUpdates / thisFrame; auto sleepUntil = std::max(targetSleepUntil, beforeSleep + averageUpdate);
if (wouldSleep < avgerageUpdate) { // We don't want to actually sleep for too long, because it causes our scripts to hang
sleepUntil = beforeSleep + avgerageUpdate; // on shutdown and stop... so we want to loop and sleep until we've spent our time in
// purgatory, constantly checking to see if our script was asked to end
while (!_isFinished && clock::now() < sleepUntil) {
QCoreApplication::processEvents(); // before we sleep again, give events a chance to process
auto thisSleepUntil = std::min(sleepUntil, clock::now() + FRAME_DURATION);
std::this_thread::sleep_until(thisSleepUntil);
} }
std::this_thread::sleep_until(sleepUntil);
#ifdef SCRIPT_DELAY_DEBUG #ifdef SCRIPT_DELAY_DEBUG
{ {
auto actuallySleptUntil = clock::now(); auto actuallySleptUntil = clock::now();
@ -865,7 +868,7 @@ void ScriptEngine::run() {
qCDebug(scriptengine) << qCDebug(scriptengine) <<
"Frame:" << thisFrame << "Frame:" << thisFrame <<
"Slept (us):" << std::chrono::duration_cast<std::chrono::microseconds>(actuallySleptUntil - beforeSleep).count() << "Slept (us):" << std::chrono::duration_cast<std::chrono::microseconds>(actuallySleptUntil - beforeSleep).count() <<
"Avg Updates (us):" << avgerageUpdate.count() << "Avg Updates (us):" << averageUpdate.count() <<
"FPS:" << fps; "FPS:" << fps;
} }
} }