diff --git a/src/resources/pandoc/datadir/profiler.lua b/src/resources/pandoc/datadir/profiler.lua index c1b3cb3eb3..4437ea6464 100644 --- a/src/resources/pandoc/datadir/profiler.lua +++ b/src/resources/pandoc/datadir/profiler.lua @@ -8,9 +8,8 @@ ]] local getTime = os.clock -local module = { - category = "unknown" -} +local category = "unknown" +local module = {} local outputfile local stack_count = 0 @@ -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() diff --git a/tools/profiler/convert-to-perfetto.ts b/tools/profiler/convert-to-perfetto.ts index a72f7929ae..103fc554a6 100644 --- a/tools/profiler/convert-to-perfetto.ts +++ b/tools/profiler/convert-to-perfetto.ts @@ -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) { @@ -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 @@ -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++);