Files
PerfLog/PerfLog.lua
T

1791 lines
66 KiB
Lua

--[[
!!PerfLog - performance diagnostics for WoW 1.12 (Lua 5.0)
The addon loads first (the "!!" prefix) so that it can:
* attribute every frame to the addon that created it;
* wrap the OnEvent / OnUpdate / OnShow / OnHide scripts of every frame
to measure the CPU time and Lua memory allocated by each handler;
* track FPS, frame times, hitches, garbage collection, latency...
Everything is written to <WoW>\CustomData\PerfLog_<Character>_<Realm>_<date>_<client>.log
through WriteCustomFile (nampower). Without nampower it falls back to the
SavedVariables (WTF\Account\<account>\SavedVariables\!!PerfLog.lua).
Open the file with Viewer\PerfLogViewer.html (drag and drop).
Format: one record per line, tab-separated fields.
S key value session header
A addon enabled loaded addon list
C cvar value graphics / nampower cvars
R id owner script name source declaration of a profiled handler
T t dur frames maxMs n33 n50 n100 lat memKB thrKB allocKB gc freedKB luaMs selfMs combat loading group children bgSec gcMs
H t frameMs luaMs allocKB gc freedKB spikes events combat allocTop gcMs
With ducielperf >= 0.4.1 (S gcStats "exact"): allocKB = bytes allocated, gc = number
of collections (T) / 1 if any (H), freedKB = bytes freed, gcMs = GC time, all exact
over the tick / frame. Otherwise (S gcStats "gcinfo estimate") they are guessed from
gcinfo() drops as before, and gcMs is 0 (T) / "-" (H).
P t window id key calls ms maxMs allocKB gc
G t window bucket allocKB allocCount exact allocations per addon (ducielperf DLL)
X t frames frameMs then for fileRead decompress fileFind modelOpen modelProcess
textureLoad textureDone packet gc: <name>Ms <name>Calls <name>Bytes (gcBytes = freed)
engine counters over the tick (ducielperf), same t as its T line
K t addon KB Lua memory held by each loaded addon (ClassicAPI), at each
loading screen and on /perflog mem; followed by "M t mem ..."
M t boss_start <name> / boss_kill <name> (<s>s) / boss_wipe <name> (<s>s) BigWigs fights;
the profile window (P/E/G/B) is closed at each, so windows
line up with fights
B t window results largeResults largeKB liveBuffers strings built through luaL_Buffer
in the window, those over 512 bytes and their size (ducielperf
>= 0.5.0 builds them garbage-free; S strbuf says if active)
Y hitchT frameMs [name|ms|calls|KB ...] engine counters of a hitch's slow frame
(ducielperf), counters >= 0.5 ms only, t = the H line's t
E t window event fires dispatches
M t type text markers (combat, zone, loading screens, background, /perflog mark)
F t ms bytes cost of one disk write
Allocation tracking (G lines, allocTop): with ducielperf >= 0.3.0 the DLL counts
every Lua allocation and charges it to the addon whose code is running
(including allocations made inside game API calls). Buckets are addon folder
names, "FrameXML", "GlueXML", "(engine)" (no Lua running) and "(unknown)"
(no source file, e.g. inline XML handlers). allocTop = the 3 biggest
allocators since the previous second ("name|KB;..."), "-" if not available.
/perflog alloc on|off (on by default when the DLL is present)
Engine counters (X / Y lines, ducielperf >= 0.4.0): time, calls and bytes of
disk/MPQ reads, decompression, file lookups, model and texture loading and
server packets, per tick and for the slow frame of each hitch. Times are
inclusive and overlap: never add them up. Freeze watchdog: when the DLL
writes a report to Logs\ducielperf_freezes.log, PerfLog adds an
"M t dp_freeze <reports>" marker. PerfLog never switches the watchdog or the
counting off. /perflog fs on|off (PerfLog reading the counters; on by default)
Background window: when WoW is not the active window it throttles itself to
a fixed low frame rate (about 4 FPS here, i.e. ~260 ms frames). Those frames
are not hitches. A run of bgRun frames that are all >= bgMinMs, within bgTol
of each other and with almost no Lua time is treated as "background": the
hitches of the run are dropped, a bg_start / bg_end marker is written, and
the T line gets the number of seconds spent in background (bgSec).
]]
local ADDON_NAME = "!!PerfLog"
local VERSION = "1.8.0"
local GetTime, gcinfo, pairs, type, tostring, pcall = GetTime, gcinfo, pairs, type, tostring, pcall
local format, gsub, sfind = string.format, string.gsub, string.find
local tconcat, tsort, floor = table.concat, table.sort, math.floor
------------------------------------------------------------------------
-- Configuration
------------------------------------------------------------------------
local DEFAULTS = {
enabled = true, -- write the log
profile = true, -- profile handlers
hitchMs = 60, -- a frame longer than this = hitch
spikeMs = 1, -- handler calls >= this are kept to explain a hitch
window = 30, -- seconds between two profile dumps
tick = 1, -- seconds between two T lines
flush = 10, -- seconds between two disk writes
scanBatch = 100, -- max frames inspected per rendered frame
scanBudgetMs = 1, -- max scanner time per rendered frame (ms)
rescan = 20, -- seconds between two full scanner passes
bgMinMs = 150, -- background: frames at least this long...
bgRun = 3, -- ...this many in a row...
bgTol = 0.15, -- ...each within 15% of the previous one
alloc = true, -- per-addon allocation tracking (ducielperf DLL, if present)
frameStats = true, -- engine frame counters (ducielperf DLL, if present)
memSnap = true, -- Lua memory per addon at each loading screen (ClassicAPI)
}
local cfg = {}
for k, v in pairs(DEFAULTS) do cfg[k] = v end
local PROF = true -- profiling active (read from the config at ADDON_LOADED)
local SCRIPTS = { "OnEvent", "OnUpdate", "OnShow", "OnHide" }
local NSCRIPTS = 4
local PROFILED = { OnEvent = true, OnUpdate = true, OnShow = true, OnHide = true }
local function Print(msg)
if DEFAULT_CHAT_FRAME then
DEFAULT_CHAT_FRAME:AddMessage("|cff33ff99PerfLog|r " .. tostring(msg))
end
end
local function San(s)
s = tostring(s or "?")
s = gsub(s, "[\t\r\n]", " ")
return s
end
------------------------------------------------------------------------
-- High resolution clock
------------------------------------------------------------------------
local clock, CLOCK_NAME
if type(debugprofilestop) == "function" then
if type(debugprofilestart) == "function" then debugprofilestart() end
clock = debugprofilestop
CLOCK_NAME = "debugprofilestop"
else
clock = function() return GetTime() * 1000 end
CLOCK_NAME = "GetTime"
end
local function MeasureClockResolution()
local best
local changes = 0
local a = clock()
for i = 1, 300000 do
local b = clock()
if b ~= a then
local d = b - a
if d > 0 and (not best or d < best) then best = d end
a = b
changes = changes + 1
if changes >= 30 then break end
end
end
return best or -1
end
local T0 = GetTime()
local function Now() return GetTime() - T0 end
-- client id: GetTime() = uptime of this WoW process, differs between clients
local CLIENT_ID = format("%04d", math.mod(floor(GetTime() * 1000), 10000))
------------------------------------------------------------------------
-- Output (buffer + file)
------------------------------------------------------------------------
local buf, nbuf = {}, 0
local fileName
local writeMode -- "file", "sv" or nil (not decided yet)
local bytesWritten, linesWritten, flushCount = 0, 0, 0
local lastFlushMs = 0
local SV_MAX_BYTES = 6 * 1024 * 1024
local function W(line)
nbuf = nbuf + 1
buf[nbuf] = line
end
local function SVAppend(text)
PerfLogDB = PerfLogDB or {}
if PerfLogDB.session ~= fileName then
PerfLogDB.session = fileName
PerfLogDB.chunks = {}
PerfLogDB.bytes = 0
end
local chunks = PerfLogDB.chunks
table.insert(chunks, text)
PerfLogDB.bytes = (PerfLogDB.bytes or 0) + string.len(text)
-- keep the beginning (header) and drop the oldest chunks after it
while PerfLogDB.bytes > SV_MAX_BYTES and table.getn(chunks) > 2 do
local removed = table.remove(chunks, 2)
PerfLogDB.bytes = PerfLogDB.bytes - string.len(removed)
end
end
local function Flush()
if nbuf == 0 or not writeMode or not fileName then return end
if not cfg.enabled then
for i = 1, nbuf do buf[i] = nil end
nbuf = 0
return
end
local text = tconcat(buf, "\n", 1, nbuf) .. "\n"
local n = nbuf
for i = 1, nbuf do buf[i] = nil end
nbuf = 0
local t0 = clock()
if writeMode == "file" then
local ok, err = pcall(WriteCustomFile, fileName, text, "a")
if not ok then
Print("WriteCustomFile failed (" .. tostring(err) .. "), falling back to SavedVariables.")
writeMode = "sv"
SVAppend(text)
end
else
SVAppend(text)
end
lastFlushMs = clock() - t0
bytesWritten = bytesWritten + string.len(text)
linesWritten = linesWritten + n
flushCount = flushCount + 1
W(format("F\t%.3f\t%.2f\t%d", Now(), lastFlushMs, string.len(text)))
end
------------------------------------------------------------------------
-- Attributing frames to addons
------------------------------------------------------------------------
local WEAK = { __mode = "k" }
local frameOwner = setmetatable({}, WEAK) -- frame -> addon
local ownerSrc = setmetatable({}, WEAK) -- frame -> "stack" | "load"
local OURS = setmetatable({}, WEAK) -- our own frames (not profiled)
local addonNames -- addon folder names (fallback when Lua truncates the path with "...")
-- PERF: debugstack() is slow (ClassicAPI's version): calling it for every
-- CreateFrame while the addons load cost ~2.7 s per /reload (ducielperf
-- watchdog). The calling file is now found with error(msg, level), which only
-- formats "file:line:" of one stack level (CallerOwner). debugstack() is kept
-- as a last resort after PLAYER_LOGIN only; before that, frames nobody claimed
-- are tagged at each ADDON_LOADED by TagUntagged.
local addonsLoading = true
local error = error
-- addon named by a debugstack() dump or an error position ("file:line: ")
local function OwnerFromText(st)
for a in string.gfind(st, "[Aa][Dd][Dd][Oo][Nn][Ss][\\/]([^\\/:\n]+)[\\/]") do
if a ~= ADDON_NAME then return a end
end
-- long paths are truncated by Lua ("...odules\nameplates.lua"): look for
-- a known addon folder name surrounded by path separators
if sfind(st, "%.%.%.") and GetNumAddOns then
if not addonNames then
addonNames = {}
for i = 1, GetNumAddOns() do
local name = GetAddOnInfo(i)
if name and name ~= ADDON_NAME then table.insert(addonNames, name) end
end
end
for line in string.gfind(st, "[^\n]+") do
if not sfind(line, ADDON_NAME, 1, true) then
for i = 1, table.getn(addonNames) do
local nm = addonNames[i]
if sfind(line, "\\" .. nm .. "\\", 1, true) or sfind(line, "/" .. nm .. "/", 1, true) then
return nm
end
end
end
end
end
if sfind(st, "FrameXML") then return "FrameXML" end
return nil
end
-- Walk up the Lua stack one level at a time: error("", lvl) formats only that
-- level's "file:line: " ("" for C functions and past the end of the stack).
-- Raising an error is slow inside WoW (~1.1 s per /reload when the walk began at
-- PerfLog's own levels), so CallerOwner must be called DIRECTLY by the hook:
-- level 4 is then the code that called the hook (1 = pcall, 2 = CallerOwner,
-- 3 = the hook). Usually one error per lookup, at most 4.
local function CallerOwner(first)
for lvl = first, first + 3 do
local _, pos = pcall(error, "", lvl)
if type(pos) == "string" and pos ~= "" and not sfind(pos, ADDON_NAME, 1, true) then
local o = OwnerFromText(pos)
if o then return o end
end
end
return nil
end
-- last resort, after login only: the full stack dump (slow)
local function StackOwner()
if addonsLoading then return nil end
if type(debugstack) ~= "function" then return nil end
local ok, st = pcall(debugstack)
if not ok or type(st) ~= "string" then return nil end
return OwnerFromText(st)
end
local function SafeCall(obj, method)
local f = obj and obj[method]
if type(f) ~= "function" then return nil end
local ok, r = pcall(f, obj)
if ok then return r end
return nil
end
local function OwnerOf(f)
local o = frameOwner[f]
if o then return o, ownerSrc[f] or "load" end
local p = f
for i = 1, 8 do
p = SafeCall(p, "GetParent")
if not p then break end
o = frameOwner[p]
if o then return o, "parent" end
end
return "?", "none"
end
local function FrameLabel(f)
local n = SafeCall(f, "GetName")
if n then return n end
local t = SafeCall(f, "GetObjectType") or "Frame"
local p = SafeCall(f, "GetParent")
local pn = p and SafeCall(p, "GetName")
if pn then return "<" .. t .. ">@" .. pn end
return "<" .. t .. ">"
end
-- Tag every frame that has no owner yet as belonging to `owner`
local function TagUntagged(owner)
local n = 0
local f = EnumerateFrames()
while f do
if not frameOwner[f] then
frameOwner[f] = owner
ownerSrc[f] = "load"
n = n + 1
end
f = EnumerateFrames(f)
end
return n
end
------------------------------------------------------------------------
-- Handler profiling
------------------------------------------------------------------------
local infos = {} -- id -> info
local nextId = 0
local slots = setmetatable({}, WEAK) -- frame -> { script -> info }
local wrapInfo = setmetatable({}, { __mode = "k" })-- wrapper -> info
-- state shared by all wrappers
local depth = 0
local ownAt = {} -- depth -> owner of the wrapped handler running there
-- owner of the innermost profiled handler running now, if known (free)
-- Hooks look for the owner of the code calling them with:
-- HandlerOwner() or CallerOwner(4) or StackOwner()
-- (CallerOwner called directly from the hook, see above)
local function HandlerOwner()
local o = depth > 0 and ownAt[depth]
if o and o ~= "?" then return o end
return nil
end
local childT, childM, evAt = {}, {}, {}
local fLua = 0 -- Lua ms (handlers) since the last frame
local frameNo = 0
local SPIKE_MAX = 64
local spikeN = 0
local spikeInfo, spikeKey, spikeMs = {}, {}, {}
local evStamp, evFrameCount = {}, {} -- event -> frameNo / dispatches in that frame
local evDispatch = {} -- event -> dispatches (window)
local evFires = {} -- event -> occurrences (window, via RegisterAllEvents)
local spikeThreshold = DEFAULTS.spikeMs
local function MakeWrapper(info)
local isEvent = (info.script == "OnEvent")
local script = info.script
local stats = info.stats
return function(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12)
local orig = info.orig
if not orig then return end
if not PROF then
return orig(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12)
end
local key = script
local d = depth + 1
if isEvent then
key = event or a2 or "?"
-- a chain of hooks (HookScript) counts as a single dispatch
if d == 1 or evAt[d - 1] ~= key then
evDispatch[key] = (evDispatch[key] or 0) + 1
if evStamp[key] ~= frameNo then
evStamp[key] = frameNo
evFrameCount[key] = 1
else
evFrameCount[key] = evFrameCount[key] + 1
end
end
end
evAt[d] = key
ownAt[d] = info.owner -- frames created in this handler belong to its addon
depth = d
childT[d] = 0
childM[d] = 0
local m0 = gcinfo()
local t0 = clock()
orig(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12)
local dt = clock() - t0
local dm = gcinfo() - m0
depth = d - 1
if dt < 0 then dt = 0 end
local gc = dm < 0
local ex = dt - (childT[d] or 0)
if ex < 0 then ex = 0 end
local exm = dm - (childM[d] or 0)
if d > 1 then
childT[d - 1] = (childT[d - 1] or 0) + dt
if not gc then childM[d - 1] = (childM[d - 1] or 0) + dm end
else
fLua = fLua + dt
end
local s = stats[key]
if not s then
-- window: 1 calls, 2 ms, 3 max, 4 KB, 5 gc; cumulative: 6..10
s = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
stats[key] = s
end
s[1] = s[1] + 1
s[2] = s[2] + ex
if ex > s[3] then s[3] = ex end
if gc then s[5] = s[5] + 1 elseif exm > 0 then s[4] = s[4] + exm end
if ex >= spikeThreshold and spikeN < SPIKE_MAX then
spikeN = spikeN + 1
spikeInfo[spikeN] = info
spikeKey[spikeN] = key
spikeMs[spikeN] = ex
end
end
end
local function EmitInfo(info)
W(format("R\t%d\t%s\t%s\t%s\t%s", info.id, San(info.owner), info.script, San(info.name), info.src))
end
local function NewInfo(frame, script, owner, src)
nextId = nextId + 1
local info = { id = nextId, script = script, owner = owner, src = src,
name = FrameLabel(frame), stats = {}, frame = frame }
info.wrapper = MakeWrapper(info)
wrapInfo[info.wrapper] = info
infos[nextId] = info
local s = slots[frame]
if not s then s = {}; slots[frame] = s end
s[script] = info
EmitInfo(info)
return info
end
------------------------------------------------------------------------
-- Hooking SetScript / GetScript (per widget type)
------------------------------------------------------------------------
local hookedMT = {}
local origSet, origGet = {}, {}
local validScripts = {}
local hookedTypes = 0
local function MethodTable(f)
local mt = getmetatable(f)
if type(mt) ~= "table" then return nil end
local idx = mt.__index
if type(idx) == "table" then return idx end
return nil
end
local function HookMethodTable(idx)
if hookedMT[idx] then return end
hookedMT[idx] = true
local oSet, oGet = idx.SetScript, idx.GetScript
if type(oSet) ~= "function" or type(oGet) ~= "function" then return end
origSet[idx] = oSet
origGet[idx] = oGet
hookedTypes = hookedTypes + 1
idx.SetScript = function(self, sc, func)
if not PROFILED[sc] or OURS[self] then
return oSet(self, sc, func)
end
if type(func) ~= "function" or wrapInfo[func] then
if func == nil then
local s = slots[self]
local info = s and s[sc]
if info then info.orig = nil end
end
return oSet(self, sc, func)
end
local s = slots[self]
local info = s and s[sc]
if not info then
local o, src = HandlerOwner() or CallerOwner(4) or StackOwner(), "stack"
if not o then o, src = OwnerOf(self) end
info = NewInfo(self, sc, o, src)
elseif info.src ~= "stack" and not info.reowned then
-- an addon replaces a handler set by XML: re-attribute it once
-- (once the owner can be found: not while the addons load)
local o = HandlerOwner() or CallerOwner(4) or StackOwner()
if o then
info.reowned = true
info.src = "stack"
if o ~= info.owner then
info.owner = o
EmitInfo(info)
end
end
end
info.orig = func
return oSet(self, sc, info.wrapper)
end
idx.GetScript = function(self, sc)
local f = oGet(self, sc)
if f then
local info = wrapInfo[f]
if info then return info.orig end
end
return f
end
end
local function HookAllExistingTypes()
local f = EnumerateFrames()
while f do
local idx = MethodTable(f)
if idx and not hookedMT[idx] then HookMethodTable(idx) end
f = EnumerateFrames(f)
end
end
local validByType = {} -- widget type -> { script -> supported }
local function ValidScriptsFor(idx, f, oGet)
local v = idx and validScripts[idx]
if v then return v end
-- PERF: without a method table (metatable __index is a function on some
-- clients, e.g. hookedWidgetTypes 0) the cache above never hits, so every
-- scan of every frame built a new table and called GetScript with up to 4
-- scripts the widget may not support, each failure building an error
-- message: ~340 KB/s of garbage (ducielperf). Cache per widget type instead.
local ot
if not idx then
local got = f.GetObjectType
if type(got) == "function" then
local ok, r = pcall(got, f)
if ok and type(r) == "string" then ot = r end
end
v = ot and validByType[ot]
if v then return v end
end
v = {}
for i = 1, NSCRIPTS do
local sc = SCRIPTS[i]
v[sc] = pcall(oGet, f, sc) and true or false
end
if idx then validScripts[idx] = v elseif ot then validByType[ot] = v end
return v
end
-- Wrap the scripts of a frame that do not go through us yet
local function ScanFrame(f)
if OURS[f] then return end
local idx = MethodTable(f)
if idx and not hookedMT[idx] then HookMethodTable(idx) end
local oGet = (idx and origGet[idx]) or f.GetScript
if type(oGet) ~= "function" then return end
local valid = ValidScriptsFor(idx, f, oGet)
for i = 1, NSCRIPTS do
local sc = SCRIPTS[i]
if valid[sc] then
local cur = oGet(f, sc)
-- SetScript is only looked up when a handler has to be wrapped
local oSet = cur and not wrapInfo[cur] and ((idx and origSet[idx]) or f.SetScript)
if type(oSet) == "function" then
-- always a NEW info here: `cur` may be a chain (HookScript) that
-- already calls our previous wrapper -> otherwise infinite recursion.
local o, src
local s = slots[f]
local prev = s and s[sc]
if prev then o, src = prev.owner, prev.src else o, src = OwnerOf(f) end
local info = NewInfo(f, sc, o, src)
info.orig = cur
oSet(f, sc, info.wrapper)
end
end
end
end
local scanCursor, scanRunning, scanNextAt = nil, false, 0
local scanCycleFrames, lastScanFrames = 0, 0
local function ScanStep(n)
local f = scanCursor
if not scanRunning then return end
local t0 = clock()
local budget = cfg.scanBudgetMs
for i = 1, n do
if clock() - t0 > budget then break end
f = EnumerateFrames(f)
if not f then
scanRunning = false
scanCursor = nil
lastScanFrames = scanCycleFrames
scanNextAt = GetTime() + cfg.rescan
return
end
scanCycleFrames = scanCycleFrames + 1
ScanFrame(f)
end
scanCursor = f
end
local function StartScan()
scanRunning = true
scanCursor = nil
scanCycleFrames = 0
end
local function FullScanNow()
local f = EnumerateFrames()
local n = 0
while f do
ScanFrame(f)
n = n + 1
f = EnumerateFrames(f)
end
lastScanFrames = n
return n
end
------------------------------------------------------------------------
-- Per-addon allocation tracking (ducielperf DLL >= 0.3.0)
-- DucielPerf_AllocTracking(1) switches it on (and resets the DLL counters),
-- DucielPerf_AllocTracking() returns 1 when on.
-- DucielPerf_GetAllocations(bytesT, countT) fills cumulative bytes / counts
-- per bucket; the same tables are reused every time (no garbage).
------------------------------------------------------------------------
local AL = {
avail = type(DucielPerf_AllocTracking) == "function" and type(DucielPerf_GetAllocations) == "function",
on = false, -- PerfLog reads the counters
started = "", -- "enabled" (PerfLog switched it on) / "already on"
curB = {}, curN = {}, -- filled by DucielPerf_GetAllocations
winB = {}, winN = {}, -- counters at the previous window dump (G lines)
secB = {}, -- bytes at the previous tick (hitch attribution)
reads = 0, lastMs = 0, maxMs = 0, errors = 0, buckets = 0,
}
local function AllocIsOn()
local ok, v = pcall(DucielPerf_AllocTracking)
return ok and (v == 1 or v == true)
end
local function AllocRead()
local c0 = clock()
local ok = pcall(DucielPerf_GetAllocations, AL.curB, AL.curN)
local dt = clock() - c0
if not ok then
AL.errors = AL.errors + 1
return false
end
AL.reads = AL.reads + 1
AL.lastMs = dt
if dt > AL.maxMs then AL.maxMs = dt end
return true
end
-- deltas are computed from the counter values at this moment
local function AllocBaseline()
local winB, winN, secB = AL.winB, AL.winN, AL.secB
for k in pairs(winB) do winB[k] = nil end
for k in pairs(winN) do winN[k] = nil end
for k in pairs(secB) do secB[k] = nil end
if AllocRead() then
for name, b in pairs(AL.curB) do
winB[name] = b
secB[name] = b
winN[name] = AL.curN[name] or 0
end
end
end
-- on: make sure the DLL tracks (switch it on only if it is not on already,
-- since switching on resets its counters). off: PerfLog stops reading it.
local function AllocSet(on)
AL.avail = type(DucielPerf_AllocTracking) == "function" and type(DucielPerf_GetAllocations) == "function"
if not AL.avail then AL.on = false; return false end
if on then
if AllocIsOn() then
AL.started = "already on"
else
pcall(DucielPerf_AllocTracking, 1)
AL.started = "enabled"
end
AL.on = AllocIsOn()
if AL.on then AllocBaseline() end
else
if AllocIsOn() then pcall(DucielPerf_AllocTracking, 0) end
AL.on = false
end
return AL.on
end
-- G lines: allocations of each bucket since the previous window
local function AllocDump(t, win)
if not AL.on or not AllocRead() then return end
local curB, curN, winB, winN = AL.curB, AL.curN, AL.winB, AL.winN
local nb = 0
for name, b in pairs(curB) do
nb = nb + 1
local n = curN[name] or 0
local db = b - (winB[name] or 0)
local dn = n - (winN[name] or 0)
if db < 0 or dn < 0 then db = b; dn = n end -- the DLL counters were reset
if db > 0 then
W(format("G\t%.3f\t%.1f\t%s\t%.1f\t%.0f", t, win, San(name), db / 1024, dn))
end
winB[name] = b
winN[name] = n
end
AL.buckets = nb
end
-- once per second: remember the counters (hitch attribution)
local function AllocTick()
if not AL.on or not AllocRead() then return end
local secB = AL.secB
for name, b in pairs(AL.curB) do secB[name] = b end
end
-- hitch frame: the 3 biggest allocators since the previous second, "name|KB;..."
local function AllocTop()
if not AL.on or not AllocRead() then return "-" end
local secB = AL.secB
local n1, n2, n3
local b1, b2, b3 = 0, 0, 0
for name, b in pairs(AL.curB) do
local d = b - (secB[name] or 0)
if d > b1 then
n3, b3 = n2, b2; n2, b2 = n1, b1; n1, b1 = name, d
elseif d > b2 then
n3, b3 = n2, b2; n2, b2 = name, d
elseif d > b3 then
n3, b3 = name, d
end
end
if not n1 then return "-" end
local s = format("%s|%.1f", San(n1), b1 / 1024)
if n2 then s = s .. format(";%s|%.1f", San(n2), b2 / 1024) end
if n3 then s = s .. format(";%s|%.1f", San(n3), b3 / 1024) end
return s
end
-- ducielperf is a WeirdUtils build: its version is in GetWeirdUtilsVersion()
-- (DucielPerf_GetVersion is a C export of the DLL, not a Lua function)
local function AllocVersion()
if type(GetWeirdUtilsVersion) ~= "function" then return "absent" end
local ok, t = pcall(GetWeirdUtilsVersion)
if not ok or type(t) ~= "table" or not t.ducielperf then return "absent" end
return tostring(t.ducielperf) .. (t.alloctrack and (" (alloctrack " .. tostring(t.alloctrack) .. ")") or "")
end
------------------------------------------------------------------------
-- Engine frame counters and freeze watchdog (ducielperf DLL >= 0.4.0)
-- DucielPerf_FrameStats([on]) -> 1 | nil (switching on resets the counters)
-- DucielPerf_GetFrameStats([lastT [, worstT [, totalT]]]) -> frames
-- keys: frameMs, <name>Ms / <name>Calls / <name>Bytes, totalT.frames
-- DucielPerf_Watchdog() -> on, thresholdMs, reportsWritten
-- Times are inclusive and overlap (a read that decompresses counts in both).
-- A DLL frame runs from the end of one rendered frame to the end of the next.
------------------------------------------------------------------------
-- "gc" (ducielperf >= 0.4.1): gcMs / gcCalls / gcBytes (= bytes freed); older DLLs give 0
local FS_NAMES = { "fileRead", "decompress", "fileFind", "modelOpen",
"modelProcess", "textureLoad", "textureDone", "packet", "gc" }
local FS_N = 9
local FS = {
avail = false, on = false, started = "",
keys = {}, -- 1..24: fileReadMs, fileReadCalls, fileReadBytes, decompressMs...
last = {}, tot = {}, prev = {}, vals = {}, a = {},
pendT = nil, -- hitch waiting for its counters (see DpHitchEnd)
pattern = "X\t%.3f\t%d\t%.1f" .. string.rep("\t%.2f\t%.0f\t%.0f", FS_N),
wdAvail = false, wdOn = false, wdMs = 0, wdReports = nil, wdSeen = 0,
}
for i = 1, FS_N do
local n = FS_NAMES[i]
FS.keys[3 * i - 2] = n .. "Ms"
FS.keys[3 * i - 1] = n .. "Calls"
FS.keys[3 * i] = n .. "Bytes"
end
local function FsIsOn()
local ok, v = pcall(DucielPerf_FrameStats)
return ok and v ~= nil and v ~= false
end
-- fills the given tables (nil = skipped); true if the call worked, and the
-- DLL's frame count (identifies the frame now in lastT)
local function FsRead(lastT, totT)
local ok, n = pcall(DucielPerf_GetFrameStats, lastT, nil, totT)
return ok, n
end
-- on: read the counters, switching counting on only if it is off (the DLL's
-- startup config normally has it on). off: PerfLog stops reading them; the
-- DLL's counting is left as it is.
local function FsSet(on)
FS.avail = type(DucielPerf_FrameStats) == "function" and type(DucielPerf_GetFrameStats) == "function"
if not FS.avail then FS.on = false; return false end
if on then
if FsIsOn() then
FS.started = "already on"
else
pcall(DucielPerf_FrameStats, 1)
FS.started = "enabled"
end
FS.on = FsIsOn()
if FS.on and FsRead(nil, FS.tot) then
local prev = FS.prev
for k, v in pairs(FS.tot) do prev[k] = v end
end
else
FS.on = false
FS.pendT = nil
end
return FS.on
end
-- watchdog state; returns reportsWritten or nil
local function WdRead()
if type(DucielPerf_Watchdog) ~= "function" then FS.wdAvail = false; return nil end
FS.wdAvail = true
local ok, on, thr, n = pcall(DucielPerf_Watchdog)
if not ok then return nil end
FS.wdOn = on and true or false
FS.wdMs = thr or 0
return n or 0
end
-- once per tick, before the T line of the same t:
-- X line = engine counters summed over the tick (deltas of totalT)
-- M dp_freeze marker when the watchdog wrote new reports
local function DpTick(t, maxMs)
if FS.on and FsRead(nil, FS.tot) then
local tot, prev, vals, keys = FS.tot, FS.prev, FS.vals, FS.keys
local reset = (tot.frames or 0) < (prev.frames or 0) -- someone reset the DLL counters
local frames = (tot.frames or 0) - (reset and 0 or (prev.frames or 0))
local fms = (tot.frameMs or 0) - (reset and 0 or (prev.frameMs or 0))
for i = 1, 3 * FS_N do
local k = keys[i]
local v = tot[k] or 0
vals[i] = reset and v or (v - (prev[k] or 0))
prev[k] = v
end
prev.frames = tot.frames
prev.frameMs = tot.frameMs
W(format(FS.pattern, t, frames, fms, unpack(vals)))
end
local n = WdRead()
if n then
if FS.wdReports and n > FS.wdReports then
FS.wdSeen = FS.wdSeen + (n - FS.wdReports)
W(format("M\t%.3f\tdp_freeze\t%d (+%d, worst frame this second %.0f ms)", t, n, n - FS.wdReports, maxMs))
end
FS.wdReports = n
end
end
-- Hitch: depending on where in the frame the time went, the slow DLL frame is
-- either the last completed one now or the one that completes next. Keep the
-- counters of now (DpHitchStart), compare with the next frame's (DpHitchEnd)
-- and write the longer one as a Y line attached to the H line by its time.
local function DpHitchStart(t, ms)
if not FS.on then return end
FS.pendMs = ms
local ok, n = FsRead(FS.last, nil)
if not ok then return end
local a = FS.a
for k, v in pairs(FS.last) do a[k] = v end
FS.aFrame = n
FS.pendT = t
end
-- Of the two candidate DLL frames, keep the one whose length is closest to the
-- hitch's own duration. FS.usedFrame: DLL frame already given to the previous
-- Y line, so that two hitches in a row don't both get the same frame.
local function DpHitchEnd()
local t = FS.pendT
if not t then return end
FS.pendT = nil
local ms = FS.pendMs or 0
local src, srcN = nil, nil
if FS.aFrame ~= FS.usedFrame then src, srcN = FS.a, FS.aFrame end
local ok, n = FsRead(FS.last, nil)
if ok and n ~= FS.usedFrame and n ~= FS.aFrame
and (not src or math.abs((FS.last.frameMs or 0) - ms) < math.abs((src.frameMs or 0) - ms)) then
src, srcN = FS.last, n
end
if not src then return end
FS.usedFrame = srcN
local keys = FS.keys
local s = format("Y\t%.3f\t%.1f", t, src.frameMs or 0)
for i = 1, FS_N do
local ms = src[keys[3 * i - 2]] or 0
if ms >= 0.5 then
s = s .. format("\t%s|%.1f|%.0f|%.0f", FS_NAMES[i], ms, src[keys[3 * i - 1]] or 0, (src[keys[3 * i]] or 0) / 1024)
end
end
W(s)
end
------------------------------------------------------------------------
-- Lua garbage collector statistics (ducielperf DLL >= 0.4.1, always on)
-- DucielPerf_GetGCStats(t) fills cycles, totalMs, lastMs, maxMs, lastFreed,
-- totalFreed, allocatedSinceLastCall, memory, threshold (bytes).
-- allocatedSinceLastCall has one baseline: PerfLog is its only caller, once
-- per OnUpdate, so each call covers exactly one PerfLog frame (arg1).
------------------------------------------------------------------------
local GCS = { avail = false, base = false, t = {}, cycles = 0, ms = 0, freed = 0 }
local function GcRead()
local ok = pcall(DucielPerf_GetGCStats, GCS.t)
return ok
end
local function GcInit()
GCS.avail = type(DucielPerf_GetGCStats) == "function"
GCS.base = false
if GCS.avail and GcRead() then
local t = GCS.t
GCS.cycles, GCS.ms, GCS.freed = t.cycles or 0, t.totalMs or 0, t.totalFreed or 0
GCS.base = true
end
end
-- this frame: allocKB, collections, freedKB, gcMs (nil without the DLL)
local function GcFrame()
if not GCS.base or not GcRead() then return nil end
local t = GCS.t
local cyc, gms, fr = t.cycles or 0, t.totalMs or 0, t.totalFreed or 0
local dc, dms, dfr = cyc - GCS.cycles, gms - GCS.ms, fr - GCS.freed
GCS.cycles, GCS.ms, GCS.freed = cyc, gms, fr
if dc < 0 or dms < 0 or dfr < 0 then dc, dms, dfr = 0, 0, 0 end
return (t.allocatedSinceLastCall or 0) / 1024, dc, dfr / 1024, dms
end
------------------------------------------------------------------------
-- Garbage-free string building (ducielperf DLL >= 0.5.0, "strbuf")
-- DucielPerf_GetStringBufferStats() -> results, largeResults, largeBytes, liveBuffers
-- (exists only while the replacement is active). largeResults/largeBytes =
-- strings over 512 bytes built by table.concat, string.rep/gsub/format...:
-- the ones that used to create garbage, i.e. what the fix now saves.
------------------------------------------------------------------------
local SB = { r = 0, lr = 0, lb = 0, base = false }
local function SbInit()
SB.base = false
if type(DucielPerf_GetStringBufferStats) ~= "function" then return end
local ok, r, lr, lb = pcall(DucielPerf_GetStringBufferStats)
if ok then SB.r, SB.lr, SB.lb, SB.base = r or 0, lr or 0, lb or 0, true end
end
-- B line once per window
local function SbDump(t, win)
if not SB.base then return end
local ok, r, lr, lb, live = pcall(DucielPerf_GetStringBufferStats)
if not ok then return end
r, lr, lb = r or 0, lr or 0, lb or 0
local dr, dlr, dlb = r - SB.r, lr - SB.lr, lb - SB.lb
if dr < 0 or dlr < 0 or dlb < 0 then dr, dlr, dlb = r, lr, lb end
SB.r, SB.lr, SB.lb = r, lr, lb
W(format("B\t%.3f\t%.1f\t%.0f\t%.0f\t%.1f\t%.0f", t, win, dr, dlr, dlb / 1024, live or 0))
end
-- current BigWigs boss fight (see "Boss fights" below; declared here for the session header)
local BOSS = { hooked = false, mod = nil, name = nil, start = 0 }
-- ducielperf features PerfLog uses (one function: OnEvent is close to Lua 5.0's 32 upvalue limit)
local function DllSetup()
if cfg.alloc then AllocSet(true) end
if cfg.frameStats then FsSet(true) end
GcInit()
SbInit()
end
------------------------------------------------------------------------
-- Periodic dumps
------------------------------------------------------------------------
local lastWindowAt = 0
local function DumpProfile()
local t = Now()
local win = t - lastWindowAt
lastWindowAt = t
for id, info in pairs(infos) do
for key, s in pairs(info.stats) do
if s[1] > 0 then
W(format("P\t%.3f\t%.1f\t%d\t%s\t%d\t%.3f\t%.3f\t%d\t%d", t, win, id, key, s[1], s[2], s[3], s[4], s[5]))
s[6] = s[6] + s[1]; s[7] = s[7] + s[2]
if s[3] > s[8] then s[8] = s[3] end
s[9] = s[9] + s[4]; s[10] = s[10] + s[5]
s[1] = 0; s[2] = 0; s[3] = 0; s[4] = 0; s[5] = 0
end
end
end
for ev, n in pairs(evDispatch) do
local fires = evFires[ev] or 0
if n > 0 or fires > 0 then
W(format("E\t%.3f\t%.1f\t%s\t%d\t%d", t, win, ev, fires, n))
end
evDispatch[ev] = 0
end
for ev, n in pairs(evFires) do
if n > 0 and not evDispatch[ev] then
W(format("E\t%.3f\t%.1f\t%s\t%d\t%d", t, win, ev, n, 0))
end
evFires[ev] = 0
end
AllocDump(t, win)
SbDump(t, win)
end
------------------------------------------------------------------------
-- Session info
------------------------------------------------------------------------
local function S(k, v) W("S\t" .. k .. "\t" .. San(v)) end
local CVARS = {
"gxApi", "gxResolution", "gxRefresh", "gxColorBits", "gxDepthBits", "gxMultisample",
"gxMultisampleQuality", "gxVSync", "gxTripleBuffer", "gxWindow", "gxMaximize", "gxCursor",
"maxfps", "maxfpsbk", "farclip", "horizonfarclip", "farclipoverride",
"groundEffectDensity", "groundEffectDist", "smallCull", "detailDoodadAlpha", "doodadAnim",
"texLodBias", "spellEffectLevel", "weatherDensity", "unitDrawDist", "shadowLevel",
"M2UsePixelShaders", "M2UseZFill", "M2UseClipPlanes", "M2UseThreads", "M2BatchDoodads",
"M2Faster", "pixelShaders", "fullAlpha", "frillDensity", "lod", "SkyCloudLOD", "waterLOD",
"mapShadows", "showfootprints", "particleDensity", "anisotropic", "trilinear",
"textureCacheSize", "nameplateDistance", "UnitNameRenderMode", "showShadow",
"NP_EnableUnitEventsGuid", "NP_EnableUnitEventsGuidFiltering", "NP_EnableUnitEventsRaid",
"NP_EnableUnitEventsParty", "NP_EnableUnitEventsPet", "NP_EnableUnitEventsMouseover",
"NP_NameplateDistance", "NP_EnableAuraCastEvents", "NP_EnableAutoAttackEvents",
"NP_EnableSpellStartEvents", "NP_EnableSpellGoEvents", "NP_EnableSpellHealEvents",
"NP_EnableSpellEnergizeEvents", "NP_EnableEnhancedTooltips",
}
local function WriteSessionInfo()
S("addonVersion", VERSION)
S("date", date("%Y-%m-%d %H:%M:%S"))
S("gettime0", format("%.3f", T0))
local v, b, d = GetBuildInfo()
S("build", tostring(v) .. " " .. tostring(b) .. " " .. tostring(d))
S("realm", GetRealmName and GetRealmName() or "?")
S("char", UnitName and UnitName("player") or "?")
S("clientId", CLIENT_ID)
S("clock", CLOCK_NAME)
S("clockResMs", format("%.4f", MeasureClockResolution()))
S("writeMode", writeMode)
S("debugstack", type(debugstack) == "function" and "yes" or "no")
S("hookedWidgetTypes", hookedTypes)
do
local mt = getmetatable(UIParent)
local idx = type(mt) == "table" and mt.__index
S("frameMeta", type(mt) .. "/" .. type(idx) .. "/" .. type(type(idx) == "table" and rawget(idx, "SetScript")))
end
S("superwow", SUPERWOW_VERSION or "absent")
if type(GetNampowerVersion) == "function" then
local ok, a, b2, c = pcall(GetNampowerVersion)
S("nampower", ok and (tostring(a) .. "." .. tostring(b2) .. "." .. tostring(c)) or "?")
else
S("nampower", "absent")
end
S("classicapi", CLASSIC_API_VERSION or (C_AddOns and "present") or "absent")
if type(GetWeirdUtilsVersion) == "function" then
local ok, t = pcall(GetWeirdUtilsVersion)
local parts = {}
if ok and type(t) == "table" then
for k2, v2 in pairs(t) do table.insert(parts, tostring(k2) .. "=" .. tostring(v2)) end
end
S("weirdutils", tconcat(parts, ","))
else
S("weirdutils", "absent")
end
S("unitxp", type(UnitXP) == "function" and "present" or "absent")
S("ducielperf", AllocVersion())
S("allocTracking", AL.on and ("on (" .. AL.started .. ")") or (AL.avail and "off" or "absent"))
S("frameStats", FS.on and ("on (" .. FS.started .. ")") or (FS.avail and "off" or "absent"))
S("gcStats", GCS.base and "exact (ducielperf)" or "gcinfo estimate")
-- ducielperf >= 0.5.0 builds long strings without garbage: allocation and GC
-- figures are not comparable with logs where this is "off/absent"
S("strbuf", SB.base and "on (garbage-free string building)" or "off/absent")
S("bossMarkers", BOSS.hooked and "BigWigs" or "off (BigWigs not found)")
S("memSnapshots", (type(UpdateAddOnMemoryUsage) == "function" and type(GetAddOnMemoryUsage) == "function")
and (cfg.memSnap and "each loading screen + /perflog mem" or "/perflog mem only") or "absent")
do
local n = WdRead()
FS.wdReports = n
S("watchdog", not FS.wdAvail and "absent" or
(FS.wdOn and format("on %d ms, %d reports so far", FS.wdMs, n or 0) or "off"))
end
S("hitchMs", cfg.hitchMs)
S("window", cfg.window)
S("bgDetect", format("%d frames >= %d ms within %d%%", cfg.bgRun, cfg.bgMinMs, cfg.bgTol * 100))
S("profile", PROF and 1 or 0)
-- addons
local n = GetNumAddOns()
for i = 1, n do
local name, title, notes, enabled, loadable, reason = GetAddOnInfo(i)
local loaded = IsAddOnLoaded(i)
W(format("A\t%s\t%d\t%d", San(name), enabled and 1 or 0, loaded and 1 or 0))
end
-- cvars
for i = 1, table.getn(CVARS) do
local ok, val = pcall(GetCVar, CVARS[i])
if ok and val ~= nil then W("C\t" .. CVARS[i] .. "\t" .. San(val)) end
end
end
local function Mark(kind, text)
W(format("M\t%.3f\t%s\t%s", Now(), kind, San(text or "")))
end
------------------------------------------------------------------------
-- Driver frame
------------------------------------------------------------------------
local driver = CreateFrame("Frame", "PerfLogDriverFrame")
OURS[driver] = true
local lastMem = gcinfo()
-- counters of the current second (in a table: Lua 5.0 has a 32 upvalue limit)
local tk = { acc = 0, frames = 0, max = 0, n33 = 0, n50 = 0, n100 = 0,
alloc = 0, gc = 0, freed = 0, lua = 0, self = 0, bg = 0, gcMs = 0 }
-- background (throttled window) detection
-- on: in background; run/runMs/ref: current run of long regular frames;
-- pend: hitch lines of the current run, written only if the run is not background
local bgs = { on = false, run = 0, runMs = 0, ref = 0, pend = {}, npend = 0,
since = 0, frames = 0, periods = 0, skipped = 0, total = 0 }
-- general state
local st = { nextFlushAt = 0, nextWindowAt = 0, loading = true, loadingUntil = 0,
loadStart = GetTime(), inCombat = false, groupSize = 1, hitchCount = 0,
childrenOK = WorldFrame and type(WorldFrame.GetNumChildren) == "function" }
------------------------------------------------------------------------
-- Boss fights (BigWigs)
-- BigWigs calls BigWigsBossRecords:StartBossfight(module) when a boss is
-- engaged and :EndBossfight(module) when it dies. A wipe or reset reboots
-- the module, which sets module.engaged back to false: checked once per tick.
-- M boss_start <name> / M boss_kill <name> (<s>s) / M boss_wipe <name> (<s>s)
-- The profile window (P/E/G/B lines) is closed at each of these markers, so
-- windows line up with fights.
------------------------------------------------------------------------
local function BossWindowBreak(now)
DumpProfile()
st.nextWindowAt = now + cfg.window
end
local function BossEnd(kind)
local now = GetTime()
Mark(kind, format("%s (%.0fs)", BOSS.name or "?", now - BOSS.start))
BOSS.mod, BOSS.name = nil, nil
BossWindowBreak(now)
end
local function BossStart(module)
if not module or not module.bossSync or module.trashMod then return end
if BOSS.mod then BossEnd("boss_wipe") end -- previous fight never ended
local now = GetTime()
BossWindowBreak(now)
local name = module.translatedName
if type(name) ~= "string" then name = tostring(module.bossSync) end
BOSS.mod, BOSS.name, BOSS.start = module, name, now
Mark("boss_start", name)
end
-- once per tick: a fight that stopped without a kill = wipe / reset
local function BossTick()
local m = BOSS.mod
if m and not m.engaged then BossEnd("boss_wipe") end
end
local function HookBigWigs()
local rec = BigWigsBossRecords
if BOSS.hooked or type(rec) ~= "table" then return end
local oStart, oEnd = rec.StartBossfight, rec.EndBossfight
if type(oStart) ~= "function" or type(oEnd) ~= "function" then return end
BOSS.hooked = true
rec.StartBossfight = function(self, module)
pcall(BossStart, module)
return oStart(self, module)
end
rec.EndBossfight = function(self, module)
if BOSS.mod and module == BOSS.mod then pcall(BossEnd, "boss_kill") end
return oEnd(self, module)
end
end
------------------------------------------------------------------------
-- Lua memory per addon (ClassicAPI UpdateAddOnMemoryUsage / GetAddOnMemoryUsage)
-- The recount walks all Lua memory (~30 ms with 55 MB), so it runs at each
-- loading screen (hidden by it) and on /perflog mem, never on its own in combat.
-- K t addon KB (one line per loaded addon, then M mem <reason>...)
------------------------------------------------------------------------
local function MemSnapshot(reason)
if type(UpdateAddOnMemoryUsage) ~= "function" or type(GetAddOnMemoryUsage) ~= "function" then
return nil
end
local t0 = clock()
if not pcall(UpdateAddOnMemoryUsage) then return nil end
local dt = clock() - t0
local t, sum, n, top, topKB = Now(), 0, 0, nil, 0
for i = 1, GetNumAddOns() do
if IsAddOnLoaded(i) then
local name = GetAddOnInfo(i)
local ok, kb = pcall(GetAddOnMemoryUsage, i)
if ok and type(kb) == "number" then
W(format("K\t%.3f\t%s\t%.1f", t, San(name), kb))
sum, n = sum + kb, n + 1
if kb > topKB then top, topKB = name, kb end
end
end
end
local total = gcinfo()
Mark("mem", format("%s: %.1f MB in %d addons of %.1f MB Lua memory, biggest %s %.1f MB, recount %.0f ms",
reason, sum / 1024, n, total / 1024, tostring(top), topKB / 1024, dt))
return sum, total, dt, top, topKB
end
local function SpikeString()
if spikeN == 0 then return "-" end
-- selection sort of the 5 biggest
local out = {}
local used = {}
for k = 1, 5 do
local best, bi = -1, nil
for i = 1, spikeN do
if not used[i] and spikeMs[i] > best then best = spikeMs[i]; bi = i end
end
if not bi then break end
used[bi] = true
table.insert(out, format("%d|%s|%.1f", spikeInfo[bi].id, spikeKey[bi], spikeMs[bi]))
end
return tconcat(out, ";")
end
local function EventString()
local list = {}
for ev, stamp in pairs(evStamp) do
if stamp == frameNo then table.insert(list, ev) end
end
if table.getn(list) == 0 then return "-" end
tsort(list, function(a, b) return evFrameCount[a] > evFrameCount[b] end)
local out = {}
for i = 1, math.min(6, table.getn(list)) do
table.insert(out, list[i] .. "|" .. evFrameCount[list[i]])
end
return tconcat(out, ";")
end
local function GroupSize()
local r = GetNumRaidMembers and GetNumRaidMembers() or 0
if r > 0 then return r end
local p = GetNumPartyMembers and GetNumPartyMembers() or 0
return p + 1
end
local function EmitTick(now, mem, thr)
local _, _, lat = GetNetStats()
local children = 0
if st.childrenOK then children = WorldFrame:GetNumChildren() or 0 end
W(format("T\t%.3f\t%.3f\t%d\t%.1f\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%.2f\t%.2f\t%d\t%d\t%d\t%d\t%.3f\t%.2f",
now - T0, tk.acc, tk.frames, tk.max, tk.n33, tk.n50, tk.n100, lat or 0, mem, thr or 0,
tk.alloc, tk.gc, tk.freed, tk.lua, tk.self, st.inCombat and 1 or 0, st.loading and 1 or 0,
st.groupSize, children, tk.bg, tk.gcMs))
tk.acc, tk.frames, tk.max, tk.n33, tk.n50, tk.n100 = 0, 0, 0, 0, 0, 0
tk.alloc, tk.gc, tk.freed, tk.lua, tk.self, tk.bg, tk.gcMs = 0, 0, 0, 0, 0, 0, 0
end
-- pending hitches of a run that turned out NOT to be background: write them
local function BgFlushPending()
for i = 1, bgs.npend do
W(bgs.pend[i])
bgs.pend[i] = nil
st.hitchCount = st.hitchCount + 1
end
bgs.npend = 0
end
-- pending hitches of a run that IS background: drop them
local function BgDropPending()
for i = 1, bgs.npend do bgs.pend[i] = nil end
bgs.skipped = bgs.skipped + bgs.npend
bgs.npend = 0
end
-- Called once per frame before the hitch test; returns true while in background
local function BgUpdate(ms, el, now)
if bgs.on then
if ms < cfg.bgMinMs then
bgs.on = false
bgs.run, bgs.runMs = 0, 0
Mark("bg_end", format("%.1fs, %d frames", now - bgs.since, bgs.frames))
return false
end
bgs.frames = bgs.frames + 1
tk.bg = tk.bg + el
bgs.total = bgs.total + el
return true
end
local long = ms >= cfg.bgMinMs and fLua < ms * 0.25
if long and bgs.run > 0 and math.abs(ms - bgs.ref) <= bgs.ref * cfg.bgTol then
bgs.run = bgs.run + 1
bgs.runMs = bgs.runMs + ms
else
-- the previous run is over and was not background: its hitches were real
BgFlushPending()
if long then bgs.run, bgs.runMs = 1, ms else bgs.run, bgs.runMs = 0, 0 end
end
bgs.ref = ms
if bgs.run >= cfg.bgRun then
bgs.on = true
bgs.periods = bgs.periods + 1
bgs.frames = bgs.run
bgs.since = now - bgs.runMs / 1000
tk.bg = tk.bg + bgs.runMs / 1000
bgs.total = bgs.total + bgs.runMs / 1000
BgDropPending()
Mark("bg_start", format("%.0f ms frames", ms))
return true
end
return false
end
local function Periodic(now)
-- incremental scanner
if scanRunning then
ScanStep(cfg.scanBatch)
elseif now >= scanNextAt and scanNextAt > 0 then
StartScan()
end
if now >= st.nextWindowAt then
st.nextWindowAt = now + cfg.window
DumpProfile()
end
if now >= st.nextFlushAt or nbuf > 3000 then
st.nextFlushAt = now + cfg.flush
Flush()
end
end
local function OnUpdate()
local c0 = clock()
local el = arg1 or 0
local ms = el * 1000
local now = GetTime()
depth = 0
-- counters of the previous frame's hitch (Y line), now that the next DLL frame is complete
DpHitchEnd()
local mem, thr = gcinfo()
local dmem, gcn, freed, gcMs = GcFrame()
local gc
if dmem then
-- exact (ducielperf): bytes allocated, collections, bytes freed and GC time
-- since the previous OnUpdate, i.e. over the same span as arg1
gc = gcn > 0 and 1 or 0
tk.gc = tk.gc + gcn
tk.freed = tk.freed + freed
tk.alloc = tk.alloc + dmem
tk.gcMs = tk.gcMs + gcMs
else
-- estimate: a drop of gcinfo() = a collection
dmem = mem - lastMem
gc, freed = 0, 0
if dmem < 0 then
gc = 1; freed = -dmem
tk.gc = tk.gc + 1; tk.freed = tk.freed + freed
else
tk.alloc = tk.alloc + dmem
end
end
if st.loading and st.loadingUntil > 0 and now >= st.loadingUntil then
st.loading = false
end
-- window in background (throttled frame rate): not a slow frame
local bg = BgUpdate(ms, el, now)
-- slow frame?
if not bg then
if ms >= 33 then tk.n33 = tk.n33 + 1 end
if ms >= 50 then tk.n50 = tk.n50 + 1 end
if ms >= 100 then tk.n100 = tk.n100 + 1 end
if ms > tk.max then tk.max = ms end
if ms >= cfg.hitchMs and not st.loading then
local line = format("H\t%.3f\t%.1f\t%.2f\t%d\t%d\t%d\t%s\t%s\t%d\t%s\t%s",
now - T0, ms, fLua, (gc == 1 and not gcMs) and 0 or dmem, gc, freed, SpikeString(), EventString(),
st.inCombat and 1 or 0, AllocTop(), gcMs and format("%.2f", gcMs) or "-")
DpHitchStart(now - T0, ms)
if bgs.run > 0 then
-- part of a run of long regular frames: wait to know if it is background
bgs.npend = bgs.npend + 1
bgs.pend[bgs.npend] = line
else
st.hitchCount = st.hitchCount + 1
W(line)
end
end
end
tk.lua = tk.lua + fLua
fLua = 0
spikeN = 0
frameNo = frameNo + 1
tk.acc = tk.acc + el
tk.frames = tk.frames + 1
if tk.acc >= cfg.tick then
DpTick(now - T0, tk.max) -- before EmitTick, which resets tk.max
EmitTick(now, mem, thr)
AllocTick()
BossTick()
end
Periodic(now)
-- our own cost (includes disk writes)
tk.self = tk.self + (clock() - c0)
-- re-sync memory after our own work so that our allocations are not
-- counted as allocations of the game
lastMem = gcinfo()
end
------------------------------------------------------------------------
-- Events
------------------------------------------------------------------------
local allEvents = pcall(driver.RegisterAllEvents, driver)
local MY_EVENTS = {
"ADDON_LOADED", "PLAYER_LOGIN", "PLAYER_LOGOUT", "PLAYER_ENTERING_WORLD",
"PLAYER_LEAVING_WORLD", "PLAYER_REGEN_DISABLED", "PLAYER_REGEN_ENABLED",
"ZONE_CHANGED_NEW_AREA", "RAID_ROSTER_UPDATE", "PARTY_MEMBERS_CHANGED",
}
if not allEvents then
for i = 1, table.getn(MY_EVENTS) do driver:RegisterEvent(MY_EVENTS[i]) end
end
local loadTagged = {}
-- File name unique per client: character + realm + date + process id
-- (GetTime = uptime of each client, differs between two clients)
local function FileSafe(s)
s = gsub(tostring(s or ""), "[^%w%-]", "")
if s == "" then s = "X" end
return s
end
local function MakeFileName()
local char = UnitName and UnitName("player") or "?"
local realm = GetRealmName and GetRealmName() or "?"
return "PerfLog_" .. FileSafe(char) .. "_" .. FileSafe(realm) .. "_" ..
date("%Y%m%d_%H%M%S") .. "_" .. CLIENT_ID .. ".log"
end
-- handlers set during an addon's XML OnLoad (owner unknown until its ADDON_LOADED)
local function ReassignUnknownOwners()
for id, info in pairs(infos) do
if info.owner == "?" and info.frame then
local o, src = OwnerOf(info.frame)
if o ~= "?" then
info.owner = o
info.src = src
EmitInfo(info)
end
end
end
end
local function OnLogin()
addonsLoading = false -- every addon has had its ADDON_LOADED: call stacks are needed again
st.groupSize = GroupSize()
fileName = MakeFileName()
WriteSessionInfo()
local t0 = clock()
local n = FullScanNow()
S("initialScanFrames", n)
S("initialScanMs", format("%.1f", clock() - t0))
S("handlersWrapped", nextId)
scanNextAt = GetTime() + cfg.rescan
Mark("login", "")
Flush()
if cfg.enabled then
Print("logging to CustomData\\" .. tostring(fileName) ..
(writeMode == "sv" and " (SavedVariables: WriteCustomFile not available)" or "") ..
". |cffaaaaaa/perflog for help|r")
end
end
-- Load trace: CustomData\PerfLog_loadtrace.txt is rewritten at each load and each
-- step is appended immediately (the main log only flushes every 10 s, so a freeze
-- during loading leaves nothing in it). After a freeze, the last line shows the
-- last step that completed; the culprit is whatever runs right after it.
local function Trace(msg)
if type(WriteCustomFile) ~= "function" then return end
pcall(WriteCustomFile, "PerfLog_loadtrace.txt",
format("%s\t%.3f\t%s\n", date("%H:%M:%S"), GetTime(), msg), "a")
end
local function OnEvent()
local ev = event
if ev == "ADDON_LOADED" then
if arg1 == ADDON_NAME and type(WriteCustomFile) == "function" then
pcall(WriteCustomFile, "PerfLog_loadtrace.txt",
"# load started " .. date("%Y-%m-%d %H:%M:%S") .. "\n", "w")
else
Trace("addon_loaded " .. tostring(arg1))
end
elseif ev == "PLAYER_LOGIN" or ev == "PLAYER_ENTERING_WORLD" then
Trace(ev)
elseif ev == "PLAYER_LOGOUT" then
Trace("PLAYER_LOGOUT (PerfLog handler)")
end
if allEvents then evFires[ev] = (evFires[ev] or 0) + 1 end
if ev == "ADDON_LOADED" then
local name = arg1
if name == ADDON_NAME then
if type(PerfLogConfig) ~= "table" then PerfLogConfig = {} end
if PerfLogConfig.cfgVersion ~= 2 then
-- v1.0: scanner too aggressive (caused hitches) -> new values
PerfLogConfig.scanBatch = nil
PerfLogConfig.rescan = nil
PerfLogConfig.cfgVersion = 2
end
for k, v in pairs(DEFAULTS) do
if PerfLogConfig[k] == nil then PerfLogConfig[k] = v end
cfg[k] = PerfLogConfig[k]
end
PROF = cfg.profile and true or false
spikeThreshold = cfg.spikeMs
-- before the other addons load, so their loading allocations are counted
DllSetup()
if type(WriteCustomFile) == "function" then
writeMode = "file"
else
writeMode = "sv"
PerfLogDB = {}
end
else
if name == "BigWigs" then HookBigWigs() end
local n = TagUntagged(name)
loadTagged[name] = n
ReassignUnknownOwners()
W(format("M\t%.3f\taddon_loaded\t%s (%d frames)", Now(), San(name), n))
end
elseif ev == "PLAYER_LOGIN" then
OnLogin()
elseif ev == "PLAYER_ENTERING_WORLD" then
local dur = GetTime() - st.loadStart
Mark("loading_end", format("%.1fs%s", dur, arg2 and " (reload)" or ""))
st.loading = true
st.loadingUntil = GetTime() + 3
st.inCombat = UnitAffectingCombat and UnitAffectingCombat("player") and true or false
Mark("zone", GetRealZoneText and GetRealZoneText() or "?")
-- memory per addon: the recount is hidden by the loading screen
if cfg.memSnap and not st.inCombat then MemSnapshot("zone " .. (GetRealZoneText and GetRealZoneText() or "?")) end
elseif ev == "PLAYER_LEAVING_WORLD" then
st.loading = true
st.loadingUntil = 0
st.loadStart = GetTime()
Mark("loading_start", "")
Flush()
elseif ev == "PLAYER_REGEN_DISABLED" then
st.inCombat = true
Mark("combat_start", UnitName and UnitName("target") or "")
elseif ev == "PLAYER_REGEN_ENABLED" then
st.inCombat = false
Mark("combat_end", "")
elseif ev == "ZONE_CHANGED_NEW_AREA" then
Mark("zone", GetRealZoneText and GetRealZoneText() or "?")
elseif ev == "RAID_ROSTER_UPDATE" or ev == "PARTY_MEMBERS_CHANGED" then
local g = GroupSize()
if g ~= st.groupSize then
st.groupSize = g
Mark("group", tostring(g))
end
elseif ev == "PLAYER_LOGOUT" then
BgFlushPending()
Mark("logout", "")
DumpProfile()
Flush()
end
end
driver:SetScript("OnEvent", OnEvent)
driver:SetScript("OnUpdate", OnUpdate)
------------------------------------------------------------------------
-- Installing hooks (when this file loads, before other addons)
------------------------------------------------------------------------
do
local n = TagUntagged("FrameXML")
W(format("M\t%.3f\taddon_loaded\tFrameXML (%d frames)", Now(), n))
HookAllExistingTypes()
-- make sure common widget types are hooked even without an existing instance
local probe = { "Frame", "Button", "CheckButton", "StatusBar", "Slider", "ScrollFrame", "Model" }
for i = 1, table.getn(probe) do
local ok, f = pcall(CreateFrame, probe[i])
if ok and f then
OURS[f] = true
local idx = MethodTable(f)
if idx then HookMethodTable(idx) end
if f.Hide then f:Hide() end
end
end
-- attribute frames created through CreateFrame
local oCreateFrame = CreateFrame
CreateFrame = function(ftype, name, parent, inherits)
local f = oCreateFrame(ftype, name, parent, inherits)
if f and not frameOwner[f] then
-- CallerOwner(4) = the code that called CreateFrame; nil = left to TagUntagged
local o = HandlerOwner() or CallerOwner(4) or StackOwner()
if o then
frameOwner[f] = o
ownerSrc[f] = "stack"
end
local idx = MethodTable(f)
if idx and not hookedMT[idx] then HookMethodTable(idx) end
end
return f
end
end
------------------------------------------------------------------------
-- Commands
------------------------------------------------------------------------
local function TopReport(nOwners, nHandlers)
local byOwner, ownerMem, rows = {}, {}, {}
local total = 0
for id, info in pairs(infos) do
for key, s in pairs(info.stats) do
local ms = s[2] + s[7]
local kb = s[4] + s[9]
if ms > 0 or kb > 0 then
byOwner[info.owner] = (byOwner[info.owner] or 0) + ms
ownerMem[info.owner] = (ownerMem[info.owner] or 0) + kb
total = total + ms
table.insert(rows, { ms = ms, kb = kb, n = s[1] + s[6], label = info.owner .. " " .. info.name .. ":" .. key })
end
end
end
local owners = {}
for o, ms in pairs(byOwner) do table.insert(owners, { o = o, ms = ms, kb = ownerMem[o] }) end
tsort(owners, function(a, b) return a.ms > b.ms end)
tsort(rows, function(a, b) return a.ms > b.ms end)
local up = Now()
Print(format("Top addons (cumulative Lua CPU over %.0fs, total %.0f ms = %.1f%%):", up, total, up > 0 and total / (up * 10) or 0))
for i = 1, math.min(nOwners, table.getn(owners)) do
local o = owners[i]
Print(format(" %d. %s: %.0f ms (%.2f ms/s), %.0f KB allocated", i, o.o, o.ms, o.ms / math.max(up, 1), o.kb or 0))
end
Print("Top handlers:")
for i = 1, math.min(nHandlers, table.getn(rows)) do
local r = rows[i]
Print(format(" %d. %s: %.0f ms / %d calls, %.0f KB", i, r.label, r.ms, r.n, r.kb))
end
end
local function SaveCfg()
if type(PerfLogConfig) == "table" then
for k in pairs(DEFAULTS) do PerfLogConfig[k] = cfg[k] end
end
end
SLASH_PERFLOG1 = "/perflog"
SLASH_PERFLOG2 = "/plog"
SlashCmdList["PERFLOG"] = function(msg)
msg = msg or ""
local _, _, cmd, rest = sfind(msg, "^%s*(%S*)%s*(.-)%s*$")
cmd = string.lower(cmd or "")
if cmd == "mark" or cmd == "m" then
Mark("user", rest)
Print("marker added: " .. rest)
elseif cmd == "top" then
TopReport(10, 10)
elseif cmd == "flush" then
DumpProfile()
Flush()
Print("log written.")
elseif cmd == "on" or cmd == "off" then
cfg.enabled = (cmd == "on")
SaveCfg()
Mark("logging", cmd)
Print("logging: " .. cmd)
elseif cmd == "profile" then
if rest == "on" then PROF = true elseif rest == "off" then PROF = false else PROF = not PROF end
cfg.profile = PROF
SaveCfg()
Mark("profile", PROF and "on" or "off")
Print("handler profiling: " .. (PROF and "on" or "off"))
elseif cmd == "alloc" then
local want
if rest == "on" then want = true elseif rest == "off" then want = false else want = not AL.on end
AllocSet(want)
if not AL.avail then
Print("allocation tracking needs ducielperf 0.3.0+ (DucielPerf_AllocTracking not found).")
else
cfg.alloc = want
SaveCfg()
Mark("alloc", AL.on and ("on (" .. AL.started .. ")") or "off")
Print("per-addon allocation tracking: " .. (AL.on and ("on (" .. AL.started .. ")") or "off"))
end
elseif cmd == "fs" or cmd == "framestats" then
local want
if rest == "on" then want = true elseif rest == "off" then want = false else want = not FS.on end
FsSet(want)
if not FS.avail then
Print("engine frame counters need ducielperf 0.4.0+ (DucielPerf_FrameStats not found).")
else
cfg.frameStats = want
SaveCfg()
Mark("frameStats", FS.on and ("on (" .. FS.started .. ")") or "off")
Print("engine frame counters: " .. (FS.on and ("on (" .. FS.started .. ")") or "off (PerfLog stops reading them; the DLL keeps counting)"))
end
elseif cmd == "mem" then
if st.inCombat and rest ~= "force" then
Print("in combat: the recount takes ~30 ms or more. /perflog mem force to do it anyway.")
else
local sum, total, dt = MemSnapshot("/perflog mem")
if not sum then
Print("Lua memory per addon needs ClassicAPI (UpdateAddOnMemoryUsage not found).")
else
local list = {}
for i = 1, GetNumAddOns() do
if IsAddOnLoaded(i) then
local name = GetAddOnInfo(i)
local ok, kb = pcall(GetAddOnMemoryUsage, i)
if ok and type(kb) == "number" then table.insert(list, { name = name, kb = kb }) end
end
end
tsort(list, function(a, b) return a.kb > b.kb end)
Print(format("Lua memory: %.1f MB in addons of %.1f MB in total (recount %.0f ms). Biggest:", sum / 1024, total / 1024, dt))
for i = 1, math.min(10, table.getn(list)) do
Print(format(" %d. %s: %.1f MB", i, list[i].name, list[i].kb / 1024))
end
end
end
elseif cmd == "hitch" and tonumber(rest) then
cfg.hitchMs = tonumber(rest); SaveCfg()
Print("hitch threshold: " .. cfg.hitchMs .. " ms")
elseif cmd == "window" and tonumber(rest) then
cfg.window = math.max(5, tonumber(rest)); SaveCfg()
Print("profile window: " .. cfg.window .. " s")
elseif cmd == "gc" then
local before = gcinfo()
local t0 = clock()
collectgarbage()
local dt = clock() - t0
local after = gcinfo()
Mark("user_gc", format("%d->%d KB in %.1f ms", before, after, dt))
Print(format("forced GC: %d KB -> %d KB in %.1f ms", before, after, dt))
elseif cmd == "status" or cmd == "" then
local mem, thr = gcinfo()
Print(format("v%s file: %s (%s)", VERSION, tostring(fileName), tostring(writeMode)))
Print(format(" log %s, profiling %s, %d handlers profiled, %d widget types hooked, clock %s",
cfg.enabled and "on" or "off", PROF and "on" or "off", nextId, hookedTypes, CLOCK_NAME))
Print(format(" %d lines / %.0f KB written, %d hitches, Lua memory %.1f MB (GC at %.1f MB)",
linesWritten, bytesWritten / 1024, st.hitchCount, mem / 1024, (thr or 0) / 1024))
Print(format(" window in background: %d time(s), %.0fs in total, %d throttled frames not counted as hitches%s",
bgs.periods, bgs.total, bgs.skipped, bgs.on and " (in background now)" or ""))
if AL.on then
Print(format(" allocation tracking on (%s): %d buckets, %d reads, last %.3f ms, max %.3f ms, %d errors",
AL.started, AL.buckets, AL.reads, AL.lastMs, AL.maxMs, AL.errors))
else
Print(" allocation tracking " .. (AL.avail and "off" or "unavailable (ducielperf 0.3.0+ not found)"))
end
if GCS.base then
local g = GCS.t
Print(format(" GC (exact, ducielperf): %d collections, %.0f ms in total, longest %.1f ms, last %.1f ms (%.0f KB freed), threshold %.1f MB",
g.cycles or 0, g.totalMs or 0, g.maxMs or 0, g.lastMs or 0, (g.lastFreed or 0) / 1024, (g.threshold or 0) / 1048576))
end
Print(" engine frame counters " .. (FS.on and ("on (" .. FS.started .. ")") or (FS.avail and "off" or "unavailable")) ..
", freeze watchdog " .. (not FS.wdAvail and "unavailable" or
(FS.wdOn and format("on (%d ms), %d reports this session", FS.wdMs, FS.wdSeen) or "off")))
Print("|cffaaaaaaCommands: /perflog mark <text> | top | flush | on | off | profile [on|off] | alloc [on|off] | fs [on|off] | mem | hitch <ms> | window <s> | gc|r")
else
Print("unknown command. /perflog for help.")
end
end