Changeset View
Changeset View
Standalone View
Standalone View
source/scriptinterface/ScriptContext.cpp
- This file was moved from source/scriptinterface/ScriptRuntime.cpp.
Show All 11 Lines | |||||
* GNU General Public License for more details. | * GNU General Public License for more details. | ||||
* | * | ||||
* You should have received a copy of the GNU General Public License | * You should have received a copy of the GNU General Public License | ||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
#include "precompiled.h" | #include "precompiled.h" | ||||
#include "ScriptRuntime.h" | #include "ScriptContext.h" | ||||
#include "ps/GameSetup/Config.h" | #include "ps/GameSetup/Config.h" | ||||
#include "ps/Profile.h" | #include "ps/Profile.h" | ||||
#include "scriptinterface/ScriptEngine.h" | #include "scriptinterface/ScriptEngine.h" | ||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
void GCSliceCallbackHook(JSRuntime* UNUSED(rt), JS::GCProgress progress, const JS::GCDescription& UNUSED(desc)) | void GCSliceCallbackHook(JSRuntime* UNUSED(rt), JS::GCProgress progress, const JS::GCDescription& UNUSED(desc)) | ||||
▲ Show 20 Lines • Show All 101 Lines • ▼ Show 20 Lines | else | ||||
LOGERROR("%s", msg.str().c_str()); | LOGERROR("%s", msg.str().c_str()); | ||||
// When running under Valgrind, print more information in the error message | // When running under Valgrind, print more information in the error message | ||||
// VALGRIND_PRINTF_BACKTRACE("->"); | // VALGRIND_PRINTF_BACKTRACE("->"); | ||||
} | } | ||||
} // anonymous namespace | } // anonymous namespace | ||||
shared_ptr<ScriptContext> ScriptContext::CreateContext(int contextSize, int heapGrowthBytesGCTrigger) | |||||
shared_ptr<ScriptRuntime> ScriptRuntime::CreateRuntime(int runtimeSize, int heapGrowthBytesGCTrigger) | |||||
{ | { | ||||
return shared_ptr<ScriptRuntime>(new ScriptRuntime(runtimeSize, heapGrowthBytesGCTrigger)); | return shared_ptr<ScriptContext>(new ScriptContext(contextSize, heapGrowthBytesGCTrigger)); | ||||
} | } | ||||
ScriptRuntime::ScriptRuntime(int runtimeSize, int heapGrowthBytesGCTrigger): | ScriptContext::ScriptContext(int contextSize, int heapGrowthBytesGCTrigger): | ||||
m_LastGCBytes(0), | m_LastGCBytes(0), | ||||
m_LastGCCheck(0.0f), | m_LastGCCheck(0.0f), | ||||
m_HeapGrowthBytesGCTrigger(heapGrowthBytesGCTrigger), | m_HeapGrowthBytesGCTrigger(heapGrowthBytesGCTrigger), | ||||
m_RuntimeSize(runtimeSize) | m_ContextSize(contextSize) | ||||
{ | { | ||||
ENSURE(ScriptEngine::IsInitialised() && "The ScriptEngine must be initialized before constructing any ScriptRuntimes!"); | ENSURE(ScriptEngine::IsInitialised() && "The ScriptEngine must be initialized before constructing any ScriptContexts!"); | ||||
m_rt = JS_NewRuntime(runtimeSize, JS::DefaultNurseryBytes, nullptr); | m_rt = JS_NewRuntime(contextSize, JS::DefaultNurseryBytes, nullptr); | ||||
ENSURE(m_rt); // TODO: error handling | ENSURE(m_rt); // TODO: error handling | ||||
JS::SetGCSliceCallback(m_rt, GCSliceCallbackHook); | JS::SetGCSliceCallback(m_rt, GCSliceCallbackHook); | ||||
JS_SetGCParameter(m_rt, JSGC_MAX_MALLOC_BYTES, m_RuntimeSize); | JS_SetGCParameter(m_rt, JSGC_MAX_MALLOC_BYTES, m_ContextSize); | ||||
JS_SetGCParameter(m_rt, JSGC_MAX_BYTES, m_RuntimeSize); | JS_SetGCParameter(m_rt, JSGC_MAX_BYTES, m_ContextSize); | ||||
JS_SetGCParameter(m_rt, JSGC_MODE, JSGC_MODE_INCREMENTAL); | JS_SetGCParameter(m_rt, JSGC_MODE, JSGC_MODE_INCREMENTAL); | ||||
// The whole heap-growth mechanism seems to work only for non-incremental GCs. | // The whole heap-growth mechanism seems to work only for non-incremental GCs. | ||||
// We disable it to make it more clear if full GCs happen triggered by this JSAPI internal mechanism. | // We disable it to make it more clear if full GCs happen triggered by this JSAPI internal mechanism. | ||||
JS_SetGCParameter(m_rt, JSGC_DYNAMIC_HEAP_GROWTH, false); | JS_SetGCParameter(m_rt, JSGC_DYNAMIC_HEAP_GROWTH, false); | ||||
ScriptEngine::GetSingleton().RegisterRuntime(m_rt); | |||||
m_cx = JS_NewContext(m_rt, STACK_CHUNK_SIZE); | m_cx = JS_NewContext(m_rt, STACK_CHUNK_SIZE); | ||||
ENSURE(m_cx); // TODO: error handling | ENSURE(m_cx); // TODO: error handling | ||||
JS_SetOffthreadIonCompilationEnabled(m_rt, true); | JS_SetOffthreadIonCompilationEnabled(m_rt, true); | ||||
// For GC debugging: | // For GC debugging: | ||||
// JS_SetGCZeal(m_cx, 2, JS_DEFAULT_ZEAL_FREQ); | // JS_SetGCZeal(m_cx, 2, JS_DEFAULT_ZEAL_FREQ); | ||||
JS_SetContextPrivate(m_cx, nullptr); | JS_SetContextPrivate(m_cx, nullptr); | ||||
JS_SetErrorReporter(m_rt, ErrorReporter); | JS_SetErrorReporter(m_rt, ErrorReporter); | ||||
JS_SetGlobalJitCompilerOption(m_rt, JSJITCOMPILER_ION_ENABLE, 1); | JS_SetGlobalJitCompilerOption(m_rt, JSJITCOMPILER_ION_ENABLE, 1); | ||||
JS_SetGlobalJitCompilerOption(m_rt, JSJITCOMPILER_BASELINE_ENABLE, 1); | JS_SetGlobalJitCompilerOption(m_rt, JSJITCOMPILER_BASELINE_ENABLE, 1); | ||||
JS::RuntimeOptionsRef(m_cx) | JS::RuntimeOptionsRef(m_cx) | ||||
.setExtraWarnings(true) | .setExtraWarnings(true) | ||||
.setWerror(false) | .setWerror(false) | ||||
.setStrictMode(true); | .setStrictMode(true); | ||||
ScriptEngine::GetSingleton().RegisterContext(m_cx); | |||||
} | } | ||||
ScriptRuntime::~ScriptRuntime() | ScriptContext::~ScriptContext() | ||||
{ | { | ||||
ENSURE(ScriptEngine::IsInitialised() && "The ScriptEngine must be active (initialized and not yet shut down) when destroying a ScriptContext!"); | |||||
JS_DestroyContext(m_cx); | JS_DestroyContext(m_cx); | ||||
JS_DestroyRuntime(m_rt); | JS_DestroyRuntime(m_rt); | ||||
ENSURE(ScriptEngine::IsInitialised() && "The ScriptEngine must be active (initialized and not yet shut down) when destroying a ScriptRuntime!"); | ScriptEngine::GetSingleton().UnRegisterContext(m_cx); | ||||
ScriptEngine::GetSingleton().UnRegisterRuntime(m_rt); | |||||
} | } | ||||
void ScriptRuntime::RegisterCompartment(JSCompartment* cmpt) | void ScriptContext::RegisterCompartment(JSCompartment* cmpt) | ||||
{ | { | ||||
ENSURE(cmpt); | ENSURE(cmpt); | ||||
m_Compartments.push_back(cmpt); | m_Compartments.push_back(cmpt); | ||||
} | } | ||||
void ScriptRuntime::UnRegisterCompartment(JSCompartment* cmpt) | void ScriptContext::UnRegisterCompartment(JSCompartment* cmpt) | ||||
{ | { | ||||
m_Compartments.remove(cmpt); | m_Compartments.remove(cmpt); | ||||
} | } | ||||
#define GC_DEBUG_PRINT 0 | #define GC_DEBUG_PRINT 0 | ||||
void ScriptRuntime::MaybeIncrementalGC(double delay) | void ScriptContext::MaybeIncrementalGC(double delay) | ||||
{ | { | ||||
PROFILE2("MaybeIncrementalGC"); | PROFILE2("MaybeIncrementalGC"); | ||||
if (JS::IsIncrementalGCEnabled(m_rt)) | if (JS::IsIncrementalGCEnabled(m_rt)) | ||||
{ | { | ||||
// The idea is to get the heap size after a completed GC and trigger the next GC when the heap size has | // The idea is to get the heap size after a completed GC and trigger the next GC when the heap size has | ||||
// reached m_LastGCBytes + X. | // reached m_LastGCBytes + X. | ||||
// In practice it doesn't quite work like that. When the incremental marking is completed, the sweeping kicks in. | // In practice it doesn't quite work like that. When the incremental marking is completed, the sweeping kicks in. | ||||
Show All 36 Lines | #if GC_DEBUG_PRINT | ||||
else | else | ||||
printf("GC needed because JSGC_BYTES - m_LastGCBytes > m_HeapGrowthBytesGCTrigger \n" | printf("GC needed because JSGC_BYTES - m_LastGCBytes > m_HeapGrowthBytesGCTrigger \n" | ||||
" JSGC_BYTES: %d KB \n m_LastGCBytes: %d KB \n m_HeapGrowthBytesGCTrigger: %d KB \n", | " JSGC_BYTES: %d KB \n m_LastGCBytes: %d KB \n m_HeapGrowthBytesGCTrigger: %d KB \n", | ||||
gcBytes / 1024, | gcBytes / 1024, | ||||
m_LastGCBytes / 1024, | m_LastGCBytes / 1024, | ||||
m_HeapGrowthBytesGCTrigger / 1024); | m_HeapGrowthBytesGCTrigger / 1024); | ||||
#endif | #endif | ||||
// A hack to make sure we never exceed the runtime size because we can't collect the memory | // A hack to make sure we never exceed the context size because we can't collect the memory | ||||
// fast enough. | // fast enough. | ||||
if (gcBytes > m_RuntimeSize / 2) | if (gcBytes > m_ContextSize / 2) | ||||
{ | { | ||||
if (JS::IsIncrementalGCInProgress(m_rt)) | if (JS::IsIncrementalGCInProgress(m_rt)) | ||||
{ | { | ||||
#if GC_DEBUG_PRINT | #if GC_DEBUG_PRINT | ||||
printf("Finishing incremental GC because gcBytes > m_RuntimeSize / 2. \n"); | printf("Finishing incremental GC because gcBytes > m_ContextSize / 2. \n"); | ||||
#endif | #endif | ||||
PrepareCompartmentsForIncrementalGC(); | PrepareCompartmentsForIncrementalGC(); | ||||
JS::FinishIncrementalGC(m_rt, JS::gcreason::REFRESH_FRAME); | JS::FinishIncrementalGC(m_rt, JS::gcreason::REFRESH_FRAME); | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
if (gcBytes > m_RuntimeSize * 0.75) | if (gcBytes > m_ContextSize * 0.75) | ||||
{ | { | ||||
ShrinkingGC(); | ShrinkingGC(); | ||||
#if GC_DEBUG_PRINT | #if GC_DEBUG_PRINT | ||||
printf("Running shrinking GC because gcBytes > m_RuntimeSize * 0.75. \n"); | printf("Running shrinking GC because gcBytes > m_ContextSize * 0.75. \n"); | ||||
#endif | #endif | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
#if GC_DEBUG_PRINT | #if GC_DEBUG_PRINT | ||||
printf("Running full GC because gcBytes > m_RuntimeSize / 2. \n"); | printf("Running full GC because gcBytes > m_ContextSize / 2. \n"); | ||||
#endif | #endif | ||||
JS_GC(m_rt); | JS_GC(m_rt); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
#if GC_DEBUG_PRINT | #if GC_DEBUG_PRINT | ||||
if (!JS::IsIncrementalGCInProgress(m_rt)) | if (!JS::IsIncrementalGCInProgress(m_rt)) | ||||
printf("Starting incremental GC \n"); | printf("Starting incremental GC \n"); | ||||
else | else | ||||
printf("Running incremental GC slice \n"); | printf("Running incremental GC slice \n"); | ||||
#endif | #endif | ||||
PrepareCompartmentsForIncrementalGC(); | PrepareCompartmentsForIncrementalGC(); | ||||
if (!JS::IsIncrementalGCInProgress(m_rt)) | if (!JS::IsIncrementalGCInProgress(m_rt)) | ||||
JS::StartIncrementalGC(m_rt, GC_NORMAL, JS::gcreason::REFRESH_FRAME, GCSliceTimeBudget); | JS::StartIncrementalGC(m_rt, GC_NORMAL, JS::gcreason::REFRESH_FRAME, GCSliceTimeBudget); | ||||
else | else | ||||
JS::IncrementalGCSlice(m_rt, JS::gcreason::REFRESH_FRAME, GCSliceTimeBudget); | JS::IncrementalGCSlice(m_rt, JS::gcreason::REFRESH_FRAME, GCSliceTimeBudget); | ||||
} | } | ||||
m_LastGCBytes = gcBytes; | m_LastGCBytes = gcBytes; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
void ScriptRuntime::ShrinkingGC() | void ScriptContext::ShrinkingGC() | ||||
{ | { | ||||
JS_SetGCParameter(m_rt, JSGC_MODE, JSGC_MODE_COMPARTMENT); | JS_SetGCParameter(m_rt, JSGC_MODE, JSGC_MODE_COMPARTMENT); | ||||
JS::PrepareForFullGC(m_rt); | JS::PrepareForFullGC(m_rt); | ||||
JS::GCForReason(m_rt, GC_SHRINK, JS::gcreason::REFRESH_FRAME); | JS::GCForReason(m_rt, GC_SHRINK, JS::gcreason::REFRESH_FRAME); | ||||
JS_SetGCParameter(m_rt, JSGC_MODE, JSGC_MODE_INCREMENTAL); | JS_SetGCParameter(m_rt, JSGC_MODE, JSGC_MODE_INCREMENTAL); | ||||
} | } | ||||
void ScriptRuntime::PrepareCompartmentsForIncrementalGC() const | void ScriptContext::PrepareCompartmentsForIncrementalGC() const | ||||
{ | { | ||||
for (JSCompartment* const& cmpt : m_Compartments) | for (JSCompartment* const& cmpt : m_Compartments) | ||||
JS::PrepareZoneForGC(js::GetCompartmentZone(cmpt)); | JS::PrepareZoneForGC(js::GetCompartmentZone(cmpt)); | ||||
} | } |
Wildfire Games · Phabricator