From 9f1521808921d87c04aa4c5cd5c40bffa780c7f2 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Wed, 29 Jun 2022 13:52:17 -0700 Subject: [PATCH] remove system lua VM (#3864) * well, that was a fun experiment * don't need that file * s Co-authored-by: Matthew Kennedy --- firmware/config/stm32f4ems/efifeatures.h | 3 - firmware/config/stm32f7ems/efifeatures.h | 2 - firmware/config/stm32h7ems/efifeatures.h | 2 - firmware/controllers/lua/lua.cpp | 84 +++++------------------- firmware/controllers/lua/lua.mk | 1 - firmware/controllers/lua/system_lua.cpp | 19 ------ 6 files changed, 16 insertions(+), 95 deletions(-) delete mode 100644 firmware/controllers/lua/system_lua.cpp diff --git a/firmware/config/stm32f4ems/efifeatures.h b/firmware/config/stm32f4ems/efifeatures.h index a45b6a6318..ab31efd24c 100644 --- a/firmware/config/stm32f4ems/efifeatures.h +++ b/firmware/config/stm32f4ems/efifeatures.h @@ -275,7 +275,6 @@ #if defined(EFI_HAS_EXT_SDRAM) #define ENABLE_PERF_TRACE TRUE #define LUA_USER_HEAP (1 * 1024 * 1024) - #define LUA_SYSTEM_HEAP (1 * 1024 * 1024) #elif defined(EFI_IS_F42x) // F42x has more memory, so we can: // - use compressed USB MSD image (requires 32k of memory) @@ -284,13 +283,11 @@ #define ENABLE_PERF_TRACE TRUE #define LUA_USER_HEAP 25000 - #define LUA_SYSTEM_HEAP 20000 #else // small memory F40x can't fit perf trace #define ENABLE_PERF_TRACE FALSE #define LUA_USER_HEAP 25000 - #define LUA_SYSTEM_HEAP 12000 #endif #ifndef EFI_LUA diff --git a/firmware/config/stm32f7ems/efifeatures.h b/firmware/config/stm32f7ems/efifeatures.h index c1e9e8e26e..2cba798ffb 100644 --- a/firmware/config/stm32f7ems/efifeatures.h +++ b/firmware/config/stm32f7ems/efifeatures.h @@ -68,5 +68,3 @@ #undef LUA_USER_HEAP #define LUA_USER_HEAP 100000 -#undef LUA_SYSTEM_HEAP -#define LUA_SYSTEM_HEAP 100000 diff --git a/firmware/config/stm32h7ems/efifeatures.h b/firmware/config/stm32h7ems/efifeatures.h index fbf4ce3606..a9cb0440f2 100644 --- a/firmware/config/stm32h7ems/efifeatures.h +++ b/firmware/config/stm32h7ems/efifeatures.h @@ -43,5 +43,3 @@ #undef LUA_USER_HEAP #define LUA_USER_HEAP 100000 -#undef LUA_SYSTEM_HEAP -#define LUA_SYSTEM_HEAP 100000 diff --git a/firmware/controllers/lua/lua.cpp b/firmware/controllers/lua/lua.cpp index eba4972087..d2c6f5564c 100644 --- a/firmware/controllers/lua/lua.cpp +++ b/firmware/controllers/lua/lua.cpp @@ -16,17 +16,11 @@ #define LUA_USER_HEAP 1 #endif // LUA_USER_HEAP -#ifndef LUA_SYSTEM_HEAP -#define LUA_SYSTEM_HEAP 1 -#endif // LUA_SYSTEM_HEAP - -#ifndef EFI_HAS_EXT_SDRAM -static char luaUserHeap[LUA_USER_HEAP]; -static char luaSystemHeap[LUA_SYSTEM_HEAP]; -#else -static char luaUserHeap[LUA_USER_HEAP] SDRAM_OPTIONAL; -static char luaSystemHeap[LUA_SYSTEM_HEAP] SDRAM_OPTIONAL; +static char luaUserHeap[LUA_USER_HEAP] +#ifdef EFI_HAS_EXT_SDRAM +SDRAM_OPTIONAL #endif +; class Heap { public: @@ -95,28 +89,17 @@ public: } }; -static Heap heaps[] = { luaUserHeap, -#if LUA_SYSTEM_HEAP > 1 -luaSystemHeap -#endif -}; +static Heap userHeap(luaUserHeap); -template static void* myAlloc(void* /*ud*/, void* ptr, size_t osize, size_t nsize) { - static_assert(HeapIdx < efi::size(heaps)); - if (engineConfiguration->debugMode == DBG_LUA) { - switch (HeapIdx) { - case 0: engine->outputChannels.debugIntField1 = heaps[HeapIdx].used(); break; - case 1: engine->outputChannels.debugIntField2 = heaps[HeapIdx].used(); break; - } + engine->outputChannels.debugIntField1 = userHeap.used(); } - return heaps[HeapIdx].realloc(ptr, osize, nsize); + return userHeap.realloc(ptr, osize, nsize); } #else // not EFI_PROD_CODE // Non-MCU code can use plain realloc function instead of custom implementation -template static void* myAlloc(void* /*ud*/, void* ptr, size_t /*osize*/, size_t nsize) { if (!nsize) { free(ptr); @@ -197,35 +180,6 @@ static bool loadScript(LuaHandle& ls, const char* scriptStr) { return true; } -static LuaHandle systemLua; - -const char* getSystemLuaScript(); - -void initSystemLua() { -#if LUA_SYSTEM_HEAP > 1 - efiAssertVoid(OBD_PCM_Processor_Fault, !systemLua, "system lua already init"); - - Timer startTimer; - startTimer.reset(); - - systemLua = setupLuaState(myAlloc<1>); - - efiAssertVoid(OBD_PCM_Processor_Fault, systemLua, "system lua init fail"); - - if (!loadScript(systemLua, getSystemLuaScript())) { - firmwareError(OBD_PCM_Processor_Fault, "system lua script load fail"); - systemLua = nullptr; - return; - } - - auto startTime = startTimer.getElapsedSeconds(); - -#if !EFI_UNIT_TEST - efiPrintf("System Lua loaded in %.2f ms using %d bytes", startTime * 1'000, heaps[1].used()); -#endif -#endif -} - #if !EFI_UNIT_TEST static bool interactivePending = false; static char interactiveCmd[100]; @@ -352,10 +306,8 @@ static bool runOneLua(lua_Alloc alloc, const char* script) { } void LuaThread::ThreadTask() { - //initSystemLua(); - while (!chThdShouldTerminateX()) { - bool wasOk = runOneLua(myAlloc<0>, config->luaScript); + bool wasOk = runOneLua(myAlloc, config->luaScript); // Reset any lua adjustments the script made engine->engineState.luaAdjustments = {}; @@ -382,7 +334,7 @@ void startLua() { // cute hack: let's check at runtime if you are a lucky owner of microRusEFI with extra RAM and use that extra RAM for extra Lua if (isStm32F42x()) { char *buffer = (char *)0x20020000; - heaps[0].reinit(buffer, 60000); + userHeap.reinit(buffer, 60000); } #endif @@ -409,27 +361,23 @@ void startLua() { }); addConsoleAction("luamemory", [](){ - for (size_t i = 0; i < efi::size(heaps); i++) { - auto heapSize = heaps[i].size(); - auto memoryUsed = heaps[i].used(); - float pct = 100.0f * memoryUsed / heapSize; - efiPrintf("Lua memory heap %d: %d / %d bytes = %.1f%%", i, memoryUsed, heapSize, pct); - } + auto heapSize = userHeap.size(); + auto memoryUsed = userHeap.used(); + float pct = 100.0f * memoryUsed / heapSize; + efiPrintf("Lua memory heap usage: %d / %d bytes = %.1f%%", memoryUsed, heapSize, pct); }); #endif } #else // not EFI_UNIT_TEST -void startLua() { - initSystemLua(); -} +void startLua() { } #include #include static LuaHandle runScript(const char* script) { - auto ls = setupLuaState(myAlloc<0>); + auto ls = setupLuaState(myAlloc); if (!ls) { throw std::logic_error("Call to setupLuaState failed, returned null"); @@ -495,7 +443,7 @@ int testLuaReturnsInteger(const char* script) { } void testLuaExecString(const char* script) { - auto ls = setupLuaState(myAlloc<0>); + auto ls = setupLuaState(myAlloc); if (!ls) { throw std::logic_error("Call to setupLuaState failed, returned null"); diff --git a/firmware/controllers/lua/lua.mk b/firmware/controllers/lua/lua.mk index fbe915439d..6daa02f259 100644 --- a/firmware/controllers/lua/lua.mk +++ b/firmware/controllers/lua/lua.mk @@ -6,7 +6,6 @@ ALLCPPSRC += $(LUA_DIR)/lua.cpp \ $(LUA_DIR)/lua_hooks_util.cpp \ $(LUA_DIR)/generated/output_lookup_generated.cpp \ $(LUA_DIR)/generated/value_lookup_generated.cpp \ - $(LUA_DIR)/system_lua.cpp \ $(LUA_DIR)/lua_can_rx.cpp \ ALLINC += $(LUA_DIR) $(LUA_DIR)/luaaa $(LUA_EXT) diff --git a/firmware/controllers/lua/system_lua.cpp b/firmware/controllers/lua/system_lua.cpp deleted file mode 100644 index de7c18c0b7..0000000000 --- a/firmware/controllers/lua/system_lua.cpp +++ /dev/null @@ -1,19 +0,0 @@ -const char* getSystemLuaScript() { - return R"LUASCRIPT( - -function getConfig(x) - return x -end - -primeTimer = Timer.new() -primeTimer:reset(); - -function pumpLogic() - local prime = primeTimer:getElapsedSeconds() > getConfig(0) - local spinning = getTimeSinceTrigger() < 1 - - return prime or spinning -end - - )LUASCRIPT"; -} \ No newline at end of file