-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and change right click to return to last root path
- Loading branch information
Showing
3 changed files
with
221 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
-- the frames itself are stored in this table as well | ||
local frames = {} | ||
|
||
frames.current = nil | ||
|
||
frames.minDeltaTime, frames.maxDeltaTime = nil, nil | ||
frames.minMemUsage, frames.maxMemUsage = nil, nil | ||
|
||
local function getNodeCount(node) | ||
assert(node.parent) | ||
local counter = 1 | ||
for _, child in ipairs(node.parent.children) do | ||
if child.name == node.name then | ||
counter = counter + 1 | ||
if child == node then | ||
break | ||
end | ||
end | ||
end | ||
return counter | ||
end | ||
|
||
local function buildGraph(data) | ||
local frames = {} | ||
local nodeStack = {} | ||
for _, event in ipairs(data) do | ||
local name, time, memory, annotation = unpack(event) | ||
local top = nodeStack[#nodeStack] | ||
if name ~= "pop" then | ||
local node = { | ||
name = name, | ||
startTime = time, | ||
memoryStart = memory, | ||
annotation = annotation, | ||
parent = top, | ||
children = {}, | ||
} | ||
if top then | ||
node.path = {unpack(top.path)} | ||
table.insert(node.path, {node.name, getNodeCount(node)}) | ||
else | ||
node.path = {} | ||
end | ||
|
||
if name == "frame" then | ||
if #nodeStack > 0 then | ||
error("Profiling data malformed: Pushed a new frame when the last one was not popped yet!") | ||
end | ||
|
||
node.index = #frames + 1 | ||
table.insert(frames, node) | ||
else | ||
if not top then | ||
error("Profiling data malformed: Pushed a profiling zone without a 'frame' profiling zone on the stack!") | ||
end | ||
|
||
table.insert(top.children, node) | ||
end | ||
|
||
table.insert(nodeStack, node) | ||
else | ||
if not top then | ||
error("Profiling data malformed: Popped a profiling zone on an empty stack!") | ||
end | ||
|
||
top.endTime = time + 1e-8 | ||
top.deltaTime = top.endTime - top.startTime | ||
top.memoryEnd = memory | ||
top.memoryDelta = top.memoryEnd - top.memoryStart | ||
table.remove(nodeStack) | ||
end | ||
end | ||
return frames | ||
end | ||
|
||
local function updateRange(newFrames, valueList, key, cutoffPercent, cutoffMin) | ||
if #frames == 0 then | ||
for _, frame in ipairs(newFrames) do | ||
table.insert(valueList, frame[key]) | ||
end | ||
table.sort(valueList) | ||
else | ||
for _, frame in ipairs(newFrames) do | ||
local value = frame[key] | ||
local i = 1 | ||
while valueList[i] and valueList[i] < value do | ||
i = i + 1 | ||
end | ||
table.insert(valueList, i, value) | ||
i = i + 1 | ||
end | ||
end | ||
|
||
local margin = 0 | ||
if cutoffPercent then | ||
assert(cutoffMin) | ||
-- cut off the lowest and highest cutoffPercent of the values | ||
margin = math.max(cutoffMin, math.floor(cutoffPercent * #valueList)) | ||
end | ||
if cutoffMin and #valueList > cutoffMin * 5 then | ||
return valueList[1 + margin], valueList[#valueList - margin] | ||
else | ||
return valueList[1], valueList[#valueList] | ||
end | ||
end | ||
|
||
local deltaTimes = {} | ||
local memUsages = {} | ||
|
||
local function updateRanges(newFrames) | ||
frames.minDeltaTime, frames.maxDeltaTime = | ||
updateRange(newFrames, deltaTimes, "deltaTime", 0.005, 5) | ||
frames.minMemUsage, frames.maxMemUsage = | ||
updateRange(newFrames, memUsages, "memoryEnd") | ||
end | ||
|
||
function frames.addFrames(data) | ||
local newFrames = buildGraph(data) | ||
for i, frame in ipairs(newFrames) do | ||
table.insert(frames, frame) | ||
frame.index = #frames | ||
end | ||
if frames.current == nil then | ||
frames.current = frames[1] | ||
end | ||
updateRanges(newFrames) | ||
end | ||
|
||
return frames |
Oops, something went wrong.