-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcprof.lua
More file actions
356 lines (291 loc) · 9.99 KB
/
Copy pathcprof.lua
File metadata and controls
356 lines (291 loc) · 9.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
jit.off()
do -- load dll/so
local path = love.filesystem.getSaveDirectory()
local os = love.system.getOS()
if os == "Windows" then
path = path .. "\\Mods\\cprof\\?.dll"
else
path = path .. "/Mods/cprof/?.so"
end
local prevPackagePath = package.path
local prevPackageCpath = package.cpath
package.path = ""
package.cpath = path
cprof = require("cprof")
package.path = prevPackagePath
package.cpath = prevPackageCpath
end
-- TODO: fix love.draw call count
cprof.functionNames = {}
cprof.frameCount = 0
local unknownFunctionNames = {}
local updateFunctionNames = false
local doProfile = false
local currentState = ""
local startOnStateChange = false
local stopOnStateChange = false
local timeFormat = "per frame avg"
local displayMinMs = 0.1
local displayMinPct = 5
local sortBy = 'by time'
local function finfo(f)
if cprof.functionNames[f] then
return cprof.functionNames[f] -- TODO: tooltip that shows "file[line:line]"
else
if not unknownFunctionNames[f] then
updateFunctionNames = true
unknownFunctionNames[f] = 0
end
end
local info = debug.getinfo(f, "S")
local res = ""
if info.source then
if string.sub(info.source,1,1) == '@' or string.sub(info.source,1,1) == '=' then
res = res .. info.source
else
res = res .. "<from string>" -- TODO: show the code on hover (tooltip)
end
end
res = res .. "[" .. tostring(info.linedefined) .. ":" .. tostring(info.lastlinedefined) .. "]"
return res
end
local function scanForGlobalFunctions()
local scanned = {}
scanned[_G] = true
local toScan = {{t=_G,p=""}}
while #toScan > 0 do
local t = toScan[#toScan]
toScan[#toScan] = nil
local path = t.p
local t = t.t
for k,v in pairs(t) do
if type(k) ~= "string" then goto continue end
if type(v) == "function" and unknownFunctionNames[v] then
-- figure out what name to give this function
local name = path .. k
if prof.pop == prof.push and v == prof.pop then
name = "prof.noop"
end
-- check for conflicts (debug)
if cprof.functionNames[v] and cprof.functionNames[v] ~= name then
print("conflict: ", cprof.functionNames[v], name)
end
-- apply name
cprof.functionNames[v] = name
-- mark as done
unknownFunctionNames[v] = nil
elseif type(v) == "table" and not scanned[v] then
scanned[v] = true
toScan[#toScan+1] = {t=v, p=path .. k .. "."}
end
::continue::
end
end
end
local function simpletimef(time)
time = time / 1000
if time > 1000 then
return string.format("%6.2f s ", time/1000)
else
return string.format("%6.2f ms", time)
end
end
local function getTime(info, parentInfo, rootInfo)
if timeFormat == 'total' then return info.t
elseif timeFormat == 'per frame avg' then return info.t / cprof.frameCount
elseif timeFormat == 'per call avg' then return info.t / info.c
elseif timeFormat == 'percent' then return info.t / rootInfo.t
elseif timeFormat == 'parent percent' then return info.t / parentInfo.t
else
log("[cprof] error! 'timeFormat' is invalid!", "error")
return info.t
end
end
local function getCallcount(info, parentInfo, rootInfo)
if timeFormat == 'total' then return info.c
elseif timeFormat == 'per frame avg' then return info.c / cprof.frameCount
elseif timeFormat == 'per call avg' then return info.c / parentInfo.c
elseif timeFormat == 'percent' then return info.c
elseif timeFormat == 'parent percent' then return info.c
else
log("[cprof] error! 'timeFormat' is invalid!", "error")
return info.c
end
end
local function timeAsPercent()
if timeFormat == 'total' then return false
elseif timeFormat == 'per frame avg' then return false
elseif timeFormat == 'per call avg' then return false
elseif timeFormat == 'percent' then return true
elseif timeFormat == 'parent percent' then return true
else return false
end
end
local function displayFormatTime(time)
if timeAsPercent() then
return string.format("%4.1f %% ", 100 * time)
else
return simpletimef(time)
end
end
local function displayFormat(time, callcount, func)
if timeFormat == 'total' then return string.format("%s [%i] %s", displayFormatTime(time), callcount, finfo(func))
elseif timeFormat == 'per frame avg' then return string.format("%s [%.2f] %s", displayFormatTime(time), callcount, finfo(func))
elseif timeFormat == 'per call avg' then return string.format("%s [%.2f] %s", displayFormatTime(time), callcount, finfo(func))
elseif timeFormat == 'percent' then return string.format("%s [%i] %s", displayFormatTime(time), callcount, finfo(func))
elseif timeFormat == 'parent percent' then return string.format("%s [%i] %s", displayFormatTime(time), callcount, finfo(func))
else
log("[cprof] error! 'timeFormat' is invalid!", "error")
return "<error> whoops! </error>"
end
end
local function shouldDisplay(time)
if timeAsPercent() then
return time >= displayMinPct/100
else
return time >= displayMinMs*1000 -- time is in microsecs
end
end
local function accuracyNote()
imgui.SameLine()
imgui.TextDisabled("(!)")
if imgui.IsItemHovered() then
imgui.SetNextWindowSize(imgui.ImVec2_Float(200, 0))
if imgui.BeginTooltip() then
imgui.TextWrapped("inaccurate; shown time is only an upper bound")
imgui.EndTooltip()
end
end
end
local function drawProfTable(info, infoParent, infoRoot)
local selfTime = info.t
local func = info.f
local accurate = true
infoParent = infoParent or info
infoRoot = infoRoot or info
local time = getTime(info, infoParent, infoRoot)
local callcount = getCallcount(info, infoParent, infoRoot)
local subcalls = {}
for key,child in pairs(info) do
if key == "t" then goto continue end
if key == "c" then goto continue end
if key == "p" then goto continue end
if key == "f" then goto continue end
if key == "i" then accurate = false goto continue end
selfTime = selfTime - child.t
local time = getTime(child, info, infoRoot)
local callcount = getCallcount(child, info, infoRoot)
if shouldDisplay(time) then
table.insert(subcalls, {next=child, time=time, callcount=callcount})
end
::continue::
end
-- sort by time taken
if sortBy == 'by time' then
table.sort(subcalls, function(a, b) return a.time > b.time end)
elseif sortBy == 'by callcount' then
table.sort(subcalls, function(a, b) return a.callcount > b.callcount end)
end
selfTime = getTime({t=selfTime, c=info.c}, info, infoRoot) -- this is evil, and i know it... but it works so no hate, mkay?
-- print
local text = displayFormat(time, callcount, func)
if #subcalls == 0 then
imgui.BulletText(text)
if not accurate then accuracyNote() end
elseif imgui.TreeNode_StrStr(tostring(func), "%s", text) then
if not accurate then accuracyNote() end
if shouldDisplay(selfTime) then
imgui.BulletText(string.format("%s (self)", displayFormatTime(selfTime)))
end
for _,v in ipairs(subcalls) do
drawProfTable(v.next, info, infoRoot)
end
imgui.TreePop()
elseif not accurate then accuracyNote() end
end
local function imguiEnum(id, current, options)
if imgui.BeginCombo(id, current) then
for _, v in ipairs(options) do
if imgui.Selectable_Bool(v, v == current) then
current = v
end
end
imgui.EndCombo()
end
return current
end
function cprof.draw()
cprof.stop()
local root = cprof.getProfTable()
if root ~= {} then
helpers.SetNextWindowPos(0, 25, 'ImGuiCond_FirstUseEver')
helpers.SetNextWindowSize(250, 600, 'ImGuiCond_FirstUseEver')
imgui.Begin("Profiler info")
local doReset = false
if imgui.BeginTable("##prof_control", 2) then
imgui.TableNextRow();imgui.TableNextColumn()
doProfile = helpers.InputBool('Enabled', doProfile)
if doProfile then
startOnStateChange = helpers.InputBool('Reset on new state', startOnStateChange)
else
startOnStateChange = helpers.InputBool('Enable on new state', startOnStateChange)
end
stopOnStateChange = helpers.InputBool('Stop on new state', stopOnStateChange)
doReset = imgui.Button("reset");
imgui.TableNextColumn();
timeFormat = imguiEnum("time format", timeFormat, {
'total', 'per frame avg', 'per call avg', 'percent', 'parent percent'
})
if timeAsPercent() then
displayMinPct = helpers.InputFloat("min display %", displayMinPct, 5, 20)
else
displayMinMs = helpers.InputFloat("min display ms", displayMinMs, 0.1, 1)
end
sortBy = imguiEnum("sort", sortBy, {'by time', 'by callcount'})
imgui.EndTable()
end
local size = imgui.GetContentRegionAvail()
if imgui.BeginListBox("##calltree", size) then
if cprof.frameCount > 0 then
if timeFormat == 'total' then imgui.Text(string.format("Profiler: %s", simpletimef(cprof.getProfTime())))
elseif timeFormat == 'per frame avg' then imgui.Text(string.format("Profiler: %s", simpletimef(cprof.getProfTime() / cprof.frameCount)))
elseif timeFormat == 'per call avg' then imgui.Text(string.format("Profiler (per frame): %s", simpletimef(cprof.getProfTime() / cprof.frameCount)))
elseif timeFormat == 'percent' then imgui.Text(string.format("Profiler (per frame): %s", simpletimef(cprof.getProfTime() / cprof.frameCount)))
elseif timeFormat == 'parent percent' then imgui.Text(string.format("Profiler (per frame): %s", simpletimef(cprof.getProfTime() / cprof.frameCount)))
else log("[cprof] error! 'timeFormat' is invalid!", "error") end
else
imgui.Text(string.format("Profiler: %s", simpletimef(0)))
end
imgui.Text(string.format("Frames: %i", cprof.frameCount))
for k,v in pairs(root) do
drawProfTable(v)
end
imgui.EndListBox()
end
imgui.End()
if updateFunctionNames then
scanForGlobalFunctions(_G, "")
updateFunctionNames = false
end
local newState = cs.name ~= currentState
currentState = cs.name
if startOnStateChange and newState then
doReset = true
doProfile = true
if stopOnStateChange then
startOnStateChange = false
end
elseif stopOnStateChange and newState then
doProfile = false
end
if doReset then
cprof.reset()
cprof.frameCount = 0
end
end
if doProfile then
cprof.start()
cprof.frameCount = cprof.frameCount + 1
end
end
return cprof