Add Lua getUsage() function that returns percent of already used up i… (#5313)

* Add Lua getUsage() function that returns percent of already used up instructions in current script execution cycle. It can be used to avoid script being killed when doing long lasting tasks (like telemetry logs loading and parsing)

* Compilation fixes

* Doc fix

* Disable Lua script instructions limit in DEBUG mode
This commit is contained in:
Damjan Adamic
2017-11-10 17:23:37 +01:00
committed by Andre Bernet
parent 6c7e4b5597
commit 955d5775cb
3 changed files with 34 additions and 2 deletions
+17 -2
View File
@@ -44,7 +44,7 @@ ScriptInternalData standaloneScript;
uint16_t maxLuaInterval = 0;
uint16_t maxLuaDuration = 0;
bool luaLcdAllowed;
int instructionsPercent = 0;
uint8_t instructionsPercent = 0;
char lua_warning_info[LUA_WARNING_INFO_LEN+1];
struct our_longjmp * global_lj = 0;
#if defined(COLORLCD)
@@ -65,17 +65,32 @@ int custom_lua_atpanic(lua_State * L)
void luaHook(lua_State * L, lua_Debug *ar)
{
instructionsPercent++;
#if defined(DEBUG)
// Disable Lua script instructions limit in DEBUG mode,
// just report max value reached
static uint16_t max = 0;
if (instructionsPercent > 100) {
if (max + 10 < instructionsPercent) {
max = instructionsPercent;
TRACE("LUA instructionsPercent %u%%", (uint32_t)max);
}
}
else if (instructionsPercent < 10) {
max = 0;
}
#else
if (instructionsPercent > 100) {
// From now on, as soon as a line is executed, error
// keep erroring until you're script reaches the top
lua_sethook(L, luaHook, LUA_MASKLINE, 0);
luaL_error(L, "CPU limit");
}
#endif
}
void luaSetInstructionsLimit(lua_State * L, int count)
{
instructionsPercent=0;
instructionsPercent = 0;
lua_sethook(L, luaHook, LUA_MASKCOUNT, count);
}