Skip to content

Commit

Permalink
fix profiler data generation bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
cscheid committed Aug 30, 2024
1 parent 74e55b5 commit 9ba10ee
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
19 changes: 11 additions & 8 deletions src/resources/pandoc/datadir/profiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
]]

local getTime = os.clock
local module = {
category = "unknown"
}
local category = "unknown"
local module = {}
local outputfile
local stack_count = 0

Expand All @@ -32,20 +31,24 @@ local onDebugHook = function(hookType, line)
if string.match(source, ".lua$") then
outputfile:write(name, " ", source, " ", information.linedefined, "\n")
end
no = no + 1
information = debug.getinfo(no, "nS")
no = no + 1
information = debug.getinfo(no, "nS")
end
outputfile:write(stack_count, " ", now, " ", module.category, " ", line, "\n")
outputfile:write(stack_count, " ", now, " ", category, " ", line, "\n")
stack_count = stack_count + 1
end

function module.start(filename)
function module.setcategory(c)
category = c
end

function module.start(filename, ms)
outputfile = io.open(filename, "a")
if outputfile == nil then
error("Could not open profiler.txt for writing")
return
end
debug.sethook(onDebugHook, "t", 5) -- NB: "t" debugging only exists in our patched Lua interpreter/pandoc binary!
debug.sethook(onDebugHook, "t", ms or 5) -- NB: "t" debugging only exists in our patched Lua interpreter/pandoc binary!
end

function module.stop()
Expand Down
24 changes: 18 additions & 6 deletions tools/profiler/convert-to-perfetto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ let prevStack: LuaStackFrame[] = [];
let prevCat: string = "";
const perfettoStack: number[] = [];

let now = 0;
for (const stack of stacks) {
const thisStackFrames = stack.frames;
thisStackFrames.reverse();
debugger;
const thisStackFrames = stack.frames.toReversed().slice(frameSkip);

let overlappingI = 0;
while (overlappingI < thisStackFrames.length && overlappingI < prevStack.length) {
Expand All @@ -123,20 +124,31 @@ for (const stack of stacks) {
overlappingI++;
}
// pop off the stack
for (let i = prevStack.length - 1; i >= Math.max(frameSkip, overlappingI); --i) {
for (let i = prevStack.length - 1; i >= overlappingI; --i) {
const prevFrame = prevStack[i];
let newNow = stack.time * 1000000;
if (newNow <= now) {
newNow = now + 1;
}
now = newNow;

traceEvents.push({
ph: "E",
name: prevFrame.location,
sf: String(perfettoStack.pop()),
ts: stack.time * 1000000,
ts: now,
cat: prevCat !== "" ? prevCat : undefined
});
}

// push on the stack
for (let i = Math.max(frameSkip, overlappingI); i < thisStackFrames.length; ++i) {
for (let i = overlappingI; i < thisStackFrames.length; ++i) {
const nextFrame = thisStackFrames[i];
let newNow = stack.time * 1000000;
if (newNow <= now) {
newNow = now + 1;
}
now = newNow;
stackFrames[stackNo] = {
name: nextFrame.location,
parent: perfettoStack.length ? String(perfettoStack[perfettoStack.length - 1]) : undefined
Expand All @@ -145,7 +157,7 @@ for (const stack of stacks) {
ph: "B",
name: nextFrame.location,
sf: String(stackNo),
ts: stack.time * 1000000,
ts: newNow,
cat: stack.category !== "" ? stack.category : undefined
});
perfettoStack.push(stackNo++);
Expand Down

0 comments on commit 9ba10ee

Please sign in to comment.