remove system lua VM (#3864)

* well, that was a fun experiment

* don't need that file

* s

Co-authored-by: Matthew Kennedy <makenne@microsoft.com>
This commit is contained in:
Matthew Kennedy 2022-06-29 13:52:17 -07:00 committed by GitHub
parent 7ff0f0d3e6
commit 9f15218089
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 16 additions and 95 deletions

View File

@ -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

View File

@ -68,5 +68,3 @@
#undef LUA_USER_HEAP
#define LUA_USER_HEAP 100000
#undef LUA_SYSTEM_HEAP
#define LUA_SYSTEM_HEAP 100000

View File

@ -43,5 +43,3 @@
#undef LUA_USER_HEAP
#define LUA_USER_HEAP 100000
#undef LUA_SYSTEM_HEAP
#define LUA_SYSTEM_HEAP 100000

View File

@ -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 <int HeapIdx>
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 <int /*ignored*/>
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 <stdexcept>
#include <string>
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");

View File

@ -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)

View File

@ -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";
}