mirror of
https://github.com/brues-code/ClassicAPI.git
synced 2026-11-16 16:10:36 +00:00
pfDebug was a separate addon that profiled through ClassicAPI's own APIs: GetAddOnMemoryUsage and GetAddOnCPUUsage, GetFrameCPUUsage, GetFunctionCPUUsage, GetEventCPUUsage, and the intrinsic precall and postcall script hooks. Its code now lives in ClassicAPI_DebugTools under Profiler/, with its MIT notice (Copyright (c) 2018 Shagu). The analyzer tabs (Addons, Frames, Events, Globals) and the widget are pfDebug's, with the frame and template names renamed to ClassicAPIProfiler*. /profiler and /prof load DebugTools on demand through the same stub as /dump, then toggle the analyzer. The subcommands widget, reset and profile on|off are pfDebug's. What changed in the move: - The sampler starts only when the widget or the analyzer first opens, so a load for /dump costs nothing. It first takes every counter as it is, so the first CPU/s and KB/s cover one second, not the session. - Whether profiling runs is probed, not read from the cvar: a function is called once and GetFunctionCPUUsage must count that call. The scriptProfile cvar can already hold the next session's value. - A frame record starts at the frame's current CPU time and call count, and opening the analyzer takes every frame and event counter again, because the frame totals and the deep hooks keep counting while it is closed. pfDebug started records at 0, so the first scan and each reopen showed a frame's whole session as one second. - The /fstack lines (owner, OnUpdate and OnEvent owner and CPU, all scripts, deep allocations) are added at the end of FrameStackTooltip_OnTooltipSetFrameStack, not by hooking OnTooltipSetFrameStack. - No pfUI skin and no SavedVariables. The plain backdrop is pfDebug's own fallback. The widget no longer opens at login, because that would load DebugTools at login. README's Debug tools table gains /profiler and the new /fstack lines.
181 lines
5.6 KiB
Lua
181 lines
5.6 KiB
Lua
-- Profiler widget: Lua memory, garbage collection, frame rate and the addons
|
|
-- that make the most garbage and use the most CPU.
|
|
local _, ns = ...
|
|
|
|
local LINE_HEIGHT = 14
|
|
local stats, gc = ns.stats, ns.gc
|
|
|
|
local widget = CreateFrame("Button", "ClassicAPIProfilerWidget", UIParent)
|
|
ns.widget = widget
|
|
widget:Hide()
|
|
widget:SetPoint("CENTER", 0, 0)
|
|
widget:SetWidth(240)
|
|
widget:SetMovable(true)
|
|
widget:SetClampedToScreen(true)
|
|
widget:EnableMouse(true)
|
|
widget:RegisterForDrag("LeftButton")
|
|
widget:RegisterForClicks("LeftButtonUp", "RightButtonUp")
|
|
ns.CreateBackdrop(widget)
|
|
|
|
local lines = {}
|
|
local function AddLine(label)
|
|
local y = -4 - #lines * LINE_HEIGHT
|
|
|
|
local left = widget:CreateFontString(nil, "LOW", "GameFontWhite")
|
|
left:SetPoint("TOPLEFT", 5, y)
|
|
left:SetText(ns.ACCENT .. label)
|
|
|
|
local right = widget:CreateFontString(nil, "LOW", "GameFontWhite")
|
|
right:SetPoint("TOPRIGHT", -5, y)
|
|
right:SetJustifyH("RIGHT")
|
|
|
|
tinsert(lines, right)
|
|
return right
|
|
end
|
|
|
|
local memory = AddLine("Memory")
|
|
local rate = AddLine("Rate")
|
|
local cleanup = AddLine("Last Cleanup")
|
|
local framerate = AddLine("Framerate")
|
|
local cpu = AddLine("Lua CPU")
|
|
local topMem = AddLine("Top Alloc")
|
|
local topCpu = AddLine("Top CPU")
|
|
|
|
-- the largest allocation in a tenth of a second, draining over 5 seconds
|
|
local DRAIN = .1 / 5
|
|
local bar = CreateFrame("StatusBar", nil, widget)
|
|
bar:SetColorFill(1, .3, .3)
|
|
bar:SetPoint("BOTTOMLEFT", 1, 1)
|
|
bar:SetPoint("BOTTOMRIGHT", -1, 1)
|
|
bar:SetHeight(18)
|
|
bar:SetMinMaxValues(0, 1)
|
|
bar:SetValue(0)
|
|
|
|
local barText = bar:CreateFontString(nil, "OVERLAY", "GameFontWhite")
|
|
barText:SetPoint("LEFT", 4, 0)
|
|
|
|
widget:SetHeight(#lines * LINE_HEIGHT + 8 + 20)
|
|
|
|
-- lastMem is taken on the first tick while shown, so the growth while hidden
|
|
-- or while the other addons load never counts as a peak
|
|
local lastMem, peakElapsed = nil, 0
|
|
widget:SetScript("OnShow", function() ns.StartSampling() end)
|
|
widget:SetScript("OnHide", function() lastMem = nil end)
|
|
widget:SetScript("OnUpdate", function(self, elapsed)
|
|
peakElapsed = peakElapsed + elapsed
|
|
if peakElapsed < .1 then return end
|
|
peakElapsed = 0
|
|
|
|
local mem = gcinfo()
|
|
local grown = lastMem and mem - lastMem or 0
|
|
lastMem = mem
|
|
|
|
local value, peak = bar:GetValue(), select(2, bar:GetMinMaxValues())
|
|
if grown > value then
|
|
bar:SetMinMaxValues(0, grown)
|
|
bar:SetValue(grown)
|
|
barText:SetText(ns.ACCENT .. "Last Peak (100ms):|r " .. grown .. ns.GREY .. " KB")
|
|
else
|
|
bar:SetValue(max(value - peak * DRAIN, 0))
|
|
end
|
|
bar:SetColorFill(1, .3, .3, min(bar:GetValue() / 10, 1))
|
|
end)
|
|
|
|
local function Unit(value, unit)
|
|
return ns.WHITE .. value .. " " .. ns.GREY .. unit
|
|
end
|
|
|
|
ns.OnSample(function()
|
|
if not widget:IsShown() then return end
|
|
|
|
memory:SetText(ns.WHITE .. ns.KB(stats.mem) .. ns.GREY .. " / " .. ns.WHITE .. ns.KB(stats.threshold))
|
|
|
|
if stats.collected then
|
|
rate:SetText(ns.ACCENT .. "collected " .. ns.KB(gc.freed or 0))
|
|
elseif stats.memRate then
|
|
rate:SetText(ns.WHITE .. ns.KBRate(stats.memRate))
|
|
end
|
|
|
|
if gc.time then
|
|
cleanup:SetText(ns.WHITE .. gc.clock .. ns.GREY .. " (" .. SecondsToTime(GetTime() - gc.time) .. " ago)")
|
|
else
|
|
cleanup:SetText(ns.GREY .. "none yet")
|
|
end
|
|
|
|
local fps = GetFramerate()
|
|
framerate:SetText(Unit(format("%.0f", fps), "fps") .. ns.GREY .. " (" .. ns.WHITE .. format("%.1f", 1000 / fps) .. ns.GREY .. " ms)")
|
|
|
|
if ns.profiling then
|
|
cpu:SetText(ns.WHITE .. ns.MS(stats.scriptRate) .. ns.GREY .. "/s (" .. ns.WHITE .. format("%.1f%%", stats.scriptRate / 10) .. ns.GREY .. ")")
|
|
topCpu:SetText(stats.topCpu and ns.WHITE .. stats.topCpu.name .. " " .. ns.GREY .. ns.MS(stats.topCpu.cpuRate) .. "/s" or "")
|
|
else
|
|
cpu:SetText(ns.GREY .. "profiling off")
|
|
topCpu:SetText(ns.GREY .. "-")
|
|
end
|
|
|
|
local top = stats.topMem
|
|
if top and top.memRate > 0 then
|
|
topMem:SetText(ns.WHITE .. top.name .. " " .. ns.GREY .. ns.KBRate(top.memRate))
|
|
else
|
|
topMem:SetText(ns.GREY .. "-")
|
|
end
|
|
|
|
if GameTooltip:IsOwned(widget) then widget:GetScript("OnEnter")(widget) end
|
|
end)
|
|
|
|
ns.OnReset(function()
|
|
bar:SetValue(0)
|
|
end)
|
|
|
|
-- the top `count` loaded addons by `key`, largest first
|
|
local function Top(key, count)
|
|
local sorted = {}
|
|
for _, addon in ipairs(stats.list) do
|
|
if (addon[key] or 0) > 0 then tinsert(sorted, addon) end
|
|
end
|
|
table.sort(sorted, function(a, b) return a[key] > b[key] end)
|
|
for i = count + 1, #sorted do sorted[i] = nil end
|
|
return sorted
|
|
end
|
|
|
|
widget:SetScript("OnEnter", function(self)
|
|
GameTooltip:SetOwner(self, "ANCHOR_BOTTOMLEFT")
|
|
GameTooltip:ClearLines()
|
|
GameTooltip:AddLine(ns.ACCENT .. "Profiler")
|
|
|
|
GameTooltip:AddLine(" ")
|
|
GameTooltip:AddLine("Allocated per second", 1, .8, 0)
|
|
for _, addon in ipairs(Top("memRate", 8)) do
|
|
GameTooltip:AddDoubleLine(addon.name, ns.KBRate(addon.memRate), 1, 1, 1, 1, 1, 1)
|
|
end
|
|
|
|
if ns.profiling then
|
|
GameTooltip:AddLine(" ")
|
|
GameTooltip:AddLine("CPU per second", 1, .8, 0)
|
|
for _, addon in ipairs(Top("cpuRate", 8)) do
|
|
GameTooltip:AddDoubleLine(addon.name, ns.MS(addon.cpuRate), 1, 1, 1, 1, 1, 1)
|
|
end
|
|
end
|
|
|
|
GameTooltip:AddLine(" ")
|
|
GameTooltip:AddLine("Left-click: reset the peak", .6, .6, .6)
|
|
GameTooltip:AddLine("Right-click: open the analyzer", .6, .6, .6)
|
|
GameTooltip:AddLine("Drag: move", .6, .6, .6)
|
|
GameTooltip:Show()
|
|
end)
|
|
|
|
widget:SetScript("OnLeave", function()
|
|
GameTooltip:Hide()
|
|
end)
|
|
|
|
widget:SetScript("OnClick", function(self, button)
|
|
if button == "RightButton" then
|
|
ns.window:SetShown(not ns.window:IsShown())
|
|
else
|
|
bar:SetValue(0)
|
|
end
|
|
end)
|
|
|
|
widget:SetScript("OnDragStart", function(self) self:StartMoving() end)
|
|
widget:SetScript("OnDragStop", function(self) self:StopMovingOrSizing() end)
|