Skip to content

Commit

Permalink
Merge pull request #4760 from myk002/myk_zscreen_perf
Browse files Browse the repository at this point in the history
[gui] instrument ZScreen with perf counters
  • Loading branch information
myk002 authored Jun 28, 2024
2 parents 44087ab + a90a3c8 commit f2c2df2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
13 changes: 12 additions & 1 deletion library/LuaApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ distribution.
#include <lualib.h>

#include <cstring>
#include <numeric>
#include <string>
#include <vector>
#include <map>
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -3432,6 +3438,7 @@ static const LuaWrapper::FunctionReg dfhack_internal_module[] = {
WRAP(setClipboardTextCp437Multiline),
WRAP(resetPerfCounters),
WRAP(recordRepeatRuntime),
WRAP(recordZScreenRuntime),
WRAP(setPreferredNumberFormat),
{ NULL, NULL }
};
Expand Down Expand Up @@ -4100,14 +4107,18 @@ 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<const std::string, uint32_t>& 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)));
Lua::Push(L, counters.update_per_plugin);
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) {
Expand Down
1 change: 1 addition & 0 deletions library/include/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ namespace DFHack
std::unordered_map<std::string, uint32_t> state_change_per_plugin;
std::unordered_map<std::string, uint32_t> update_lua_per_repeat;
std::unordered_map<std::string, uint32_t> overlay_per_widget;
std::unordered_map<std::string, uint32_t> zscreen_per_focus;

void reset(bool ignorePauseState = false);
bool getIgnorePauseState();
Expand Down
15 changes: 14 additions & 1 deletion library/lua/gui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
17 changes: 14 additions & 3 deletions library/lua/script-manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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('-------')
Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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

0 comments on commit f2c2df2

Please sign in to comment.