AP_Scripting: log files and runtime stats

This commit is contained in:
Iampete1 2021-11-13 17:42:49 +00:00 committed by Andrew Tridgell
parent f32f14b19c
commit 58ef0d0137
3 changed files with 26 additions and 1 deletions

View File

@ -78,7 +78,7 @@ const AP_Param::GroupInfo AP_Scripting::var_info[] = {
// @Param: DEBUG_OPTS
// @DisplayName: Scripting Debug Level
// @Description: Debugging options
// @Bitmask: 0:No Scripts to run message if all scripts have stopped, 1:Runtime messages for memory usage and execution time
// @Bitmask: 0:No Scripts to run message if all scripts have stopped, 1:Runtime messages for memory usage and execution time, 2:Suppress logging scripts to dataflash, 3:log runtime memory usage and execution time
// @User: Advanced
AP_GROUPINFO("DEBUG_OPTS", 4, AP_Scripting, _debug_options, 0),

View File

@ -205,6 +205,9 @@ void lua_scripts::load_all_scripts_in_dir(lua_State *L, const char *dirname) {
}
reschedule_script(script);
if ((_debug_options.get() & uint8_t(DebugLevel::SUPPRESS_SCRIPT_LOG)) == 0) {
AP::logger().log_file_content(filename);
}
}
AP::FS().closedir(d);
}
@ -463,6 +466,8 @@ void lua_scripts::run(void) {
if ((_debug_options.get() & uint8_t(DebugLevel::RUNTIME_MSG)) != 0) {
gcs().send_text(MAV_SEVERITY_DEBUG, "Lua: Running %s", scripts->name);
}
// copy name for logging, cant do it after as script reschedule moves the pointers
const char * script_name = scripts->name;
const int startMem = lua_gc(L, LUA_GCCOUNT, 0) * 1024 + lua_gc(L, LUA_GCCOUNTB, 0);
const uint32_t loadEnd = AP_HAL::micros();
@ -477,6 +482,24 @@ void lua_scripts::run(void) {
(int)endMem,
(int)(endMem - startMem));
}
if ((_debug_options.get() & uint8_t(DebugLevel::LOG_RUNTIME)) != 0) {
struct log_Scripting pkt{
LOG_PACKET_HEADER_INIT(LOG_SCRIPTING_MSG),
time_us : AP_HAL::micros64(),
name : {},
run_time : runEnd - loadEnd,
total_mem : endMem,
run_mem : endMem - startMem,
};
const char * name_short = strrchr(script_name, '/');
if ((strlen(script_name) > sizeof(pkt.name)) && (name_short != nullptr)) {
strncpy_noterm(pkt.name, name_short+1, sizeof(pkt.name));
} else {
strncpy_noterm(pkt.name, script_name, sizeof(pkt.name));
}
AP::logger().WriteBlock(&pkt, sizeof(pkt));
}
// garbage collect after each script, this shouldn't matter, but seems to resolve a memory leak
lua_gc(L, LUA_GCCOLLECT, 0);

View File

@ -67,6 +67,8 @@ public:
enum class DebugLevel {
NO_SCRIPTS_TO_RUN = 1U << 0,
RUNTIME_MSG = 1U << 1,
SUPPRESS_SCRIPT_LOG = 1U << 2,
LOG_RUNTIME = 1U << 3,
};
private: