diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index c20aa97ce9..5ed955ae16 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -110,6 +110,7 @@ distribution. #include #include +#include #include #include #include @@ -3393,6 +3394,11 @@ static void recordRepeatRuntime(string name, uint32_t start_ms) { counters.incCounter(counters.update_lua_per_repeat[name.c_str()], start_ms); } +static void recordZScreenRuntime(string name, uint32_t start_ms) { + auto & counters = Core::getInstance().perf_counters; + counters.incCounter(counters.zscreen_per_focus[name.c_str()], start_ms); +} + static void setPreferredNumberFormat(color_ostream & out, int32_t type_int) { NumberFormatType type = (NumberFormatType)type_int; switch (type) { @@ -3432,6 +3438,7 @@ static const LuaWrapper::FunctionReg dfhack_internal_module[] = { WRAP(setClipboardTextCp437Multiline), WRAP(resetPerfCounters), WRAP(recordRepeatRuntime), + WRAP(recordZScreenRuntime), WRAP(setPreferredNumberFormat), { NULL, NULL } }; @@ -4100,6 +4107,9 @@ static int internal_getPerfCounters(lua_State *L) { summary["update_lua_ms"] = counters.update_lua_ms; summary["total_keybinding_ms"] = counters.total_keybinding_ms; summary["total_overlay_ms"] = counters.total_overlay_ms; + summary["total_zscreen_ms"] = std::accumulate( + std::begin(counters.zscreen_per_focus), std::end(counters.zscreen_per_focus), 0, + [](const uint32_t prev, const std::pair& p){ return prev + p.second; }); Lua::Push(L, summary); Lua::Push(L, translate_event_types(counters.event_manager_event_total_ms)); Lua::Push(L, mapify(translate_event_types(counters.event_manager_event_per_plugin_ms))); @@ -4107,7 +4117,8 @@ static int internal_getPerfCounters(lua_State *L) { Lua::Push(L, counters.state_change_per_plugin); Lua::Push(L, counters.update_lua_per_repeat); Lua::Push(L, counters.overlay_per_widget); - return 7; + Lua::Push(L, counters.zscreen_per_focus); + return 8; } static int internal_getClipboardTextCp437Multiline(lua_State *L) { diff --git a/library/include/Core.h b/library/include/Core.h index 642c649d63..919de81476 100644 --- a/library/include/Core.h +++ b/library/include/Core.h @@ -118,6 +118,7 @@ namespace DFHack std::unordered_map state_change_per_plugin; std::unordered_map update_lua_per_repeat; std::unordered_map overlay_per_widget; + std::unordered_map zscreen_per_focus; void reset(bool ignorePauseState = false); bool getIgnorePauseState(); diff --git a/library/lua/gui.lua b/library/lua/gui.lua index edbd81ac20..6f2fe77662 100644 --- a/library/lua/gui.lua +++ b/library/lua/gui.lua @@ -620,7 +620,6 @@ end ---@field subviews gui.View[] ---@field parent_view? gui.View ---@field focus_group gui.View.focus_group ----@field focus_path? string ---@field focus boolean ---@field active boolean|fun(): boolean ---@field visible boolean|fun(): boolean @@ -886,6 +885,7 @@ end ---@class gui.Screen.attrs: gui.View.attrs ---@field text_input_mode boolean ---@field request_full_screen_refresh boolean +---@field focus_path string ---@class gui.Screen.attrs.partial: gui.Screen.attrs @@ -1082,10 +1082,16 @@ function ZScreen:onIdle() end end +local function record_zscreen_runtime(self, start_ms) + dfhack.internal.recordZScreenRuntime(self.focus_path or 'unknown', start_ms) +end + ---@param dc gui.Painter function ZScreen:render(dc) self:renderParent() + local now_ms = dfhack.getTickCount() ZScreen.super.render(self, dc) + record_zscreen_runtime(self, now_ms) end ---@return boolean @@ -1095,6 +1101,7 @@ function ZScreen:hasFocus() end function ZScreen:onInput(keys) + local now_ms = dfhack.getTickCount() local has_mouse = self:isMouseOver() if not self:hasFocus() then if has_mouse and @@ -1103,6 +1110,7 @@ function ZScreen:onInput(keys) keys.CONTEXT_SCROLL_PAGEUP or keys.CONTEXT_SCROLL_PAGEDOWN) then self:raise() else + record_zscreen_runtime(self, now_ms) self:sendInputToParent(keys) return true end @@ -1112,7 +1120,9 @@ function ZScreen:onInput(keys) -- noop elseif self.pass_mouse_clicks and keys._MOUSE_L and not has_mouse then self.defocused = self.defocusable + record_zscreen_runtime(self, now_ms) self:sendInputToParent(keys) + return true elseif keys.LEAVESCREEN or keys._MOUSE_R then self:dismiss() else @@ -1134,9 +1144,12 @@ function ZScreen:onInput(keys) passit = require('gui.dwarfmode').getMapKey(keys) end if passit then + record_zscreen_runtime(self, now_ms) self:sendInputToParent(keys) + return true end end + record_zscreen_runtime(self, now_ms) return true end diff --git a/library/lua/script-manager.lua b/library/lua/script-manager.lua index c04ea17ae3..c2647fbe2d 100644 --- a/library/lua/script-manager.lua +++ b/library/lua/script-manager.lua @@ -234,11 +234,12 @@ end function print_timers() local summary, em_per_event, em_per_plugin_per_event, update_per_plugin, state_change_per_plugin, - update_lua_per_repeat, overlay_per_widget = dfhack.internal.getPerfCounters() + update_lua_per_repeat, overlay_per_widget, zscreen_per_focus = dfhack.internal.getPerfCounters() local elapsed = summary.elapsed_ms local total_update_time = summary.total_update_ms local total_overlay_time = summary.total_overlay_ms + local total_zscreen_time = summary.total_zscreen_ms print('Summary') print('-------') @@ -249,8 +250,8 @@ function print_timers() if elapsed <= 0 then return end - local sum = summary.total_keybinding_ms + total_update_time + total_overlay_time - print(format_relative_time(7, 'dfhack', sum, elapsed, 'elapsed'), '(does not include non-overlay interpose time)') + local sum = summary.total_keybinding_ms + total_update_time + total_overlay_time + total_zscreen_time + print(format_relative_time(7, 'dfhack', sum, elapsed, 'elapsed')) if sum > 0 then print() @@ -261,6 +262,7 @@ function print_timers() print(format_relative_time(10, 'keybinding', summary.total_keybinding_ms, sum, 'dfhack', elapsed, 'elapsed')) print(format_relative_time(10, 'update', total_update_time, sum, 'dfhack', elapsed, 'elapsed')) print(format_relative_time(10, 'overlay', total_overlay_time, sum, 'dfhack', elapsed, 'elapsed')) + print(format_relative_time(10, 'zscreen', total_zscreen_time, sum, 'dfhack', elapsed, 'elapsed')) end if total_update_time > 0 then @@ -327,6 +329,15 @@ function print_timers() print() print_sorted_timers(overlay_per_widget, 45, total_overlay_time, 'overlay', elapsed, 'elapsed') end + + if total_zscreen_time > 0 then + print() + print() + print('ZScreen details') + print('---------------') + print() + print_sorted_timers(zscreen_per_focus, 45, total_zscreen_time, 'zscreen', elapsed, 'elapsed') + end end return _ENV