Skip to content

Commit 9ba10ee

Browse files
committed
fix profiler data generation bugs
1 parent 74e55b5 commit 9ba10ee

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

src/resources/pandoc/datadir/profiler.lua

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
]]
99

1010
local getTime = os.clock
11-
local module = {
12-
category = "unknown"
13-
}
11+
local category = "unknown"
12+
local module = {}
1413
local outputfile
1514
local stack_count = 0
1615

@@ -32,20 +31,24 @@ local onDebugHook = function(hookType, line)
3231
if string.match(source, ".lua$") then
3332
outputfile:write(name, " ", source, " ", information.linedefined, "\n")
3433
end
35-
no = no + 1
36-
information = debug.getinfo(no, "nS")
34+
no = no + 1
35+
information = debug.getinfo(no, "nS")
3736
end
38-
outputfile:write(stack_count, " ", now, " ", module.category, " ", line, "\n")
37+
outputfile:write(stack_count, " ", now, " ", category, " ", line, "\n")
3938
stack_count = stack_count + 1
4039
end
4140

42-
function module.start(filename)
41+
function module.setcategory(c)
42+
category = c
43+
end
44+
45+
function module.start(filename, ms)
4346
outputfile = io.open(filename, "a")
4447
if outputfile == nil then
4548
error("Could not open profiler.txt for writing")
4649
return
4750
end
48-
debug.sethook(onDebugHook, "t", 5) -- NB: "t" debugging only exists in our patched Lua interpreter/pandoc binary!
51+
debug.sethook(onDebugHook, "t", ms or 5) -- NB: "t" debugging only exists in our patched Lua interpreter/pandoc binary!
4952
end
5053

5154
function module.stop()

tools/profiler/convert-to-perfetto.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,10 @@ let prevStack: LuaStackFrame[] = [];
110110
let prevCat: string = "";
111111
const perfettoStack: number[] = [];
112112

113+
let now = 0;
113114
for (const stack of stacks) {
114-
const thisStackFrames = stack.frames;
115-
thisStackFrames.reverse();
115+
debugger;
116+
const thisStackFrames = stack.frames.toReversed().slice(frameSkip);
116117

117118
let overlappingI = 0;
118119
while (overlappingI < thisStackFrames.length && overlappingI < prevStack.length) {
@@ -123,20 +124,31 @@ for (const stack of stacks) {
123124
overlappingI++;
124125
}
125126
// pop off the stack
126-
for (let i = prevStack.length - 1; i >= Math.max(frameSkip, overlappingI); --i) {
127+
for (let i = prevStack.length - 1; i >= overlappingI; --i) {
127128
const prevFrame = prevStack[i];
129+
let newNow = stack.time * 1000000;
130+
if (newNow <= now) {
131+
newNow = now + 1;
132+
}
133+
now = newNow;
134+
128135
traceEvents.push({
129136
ph: "E",
130137
name: prevFrame.location,
131138
sf: String(perfettoStack.pop()),
132-
ts: stack.time * 1000000,
139+
ts: now,
133140
cat: prevCat !== "" ? prevCat : undefined
134141
});
135142
}
136143

137144
// push on the stack
138-
for (let i = Math.max(frameSkip, overlappingI); i < thisStackFrames.length; ++i) {
145+
for (let i = overlappingI; i < thisStackFrames.length; ++i) {
139146
const nextFrame = thisStackFrames[i];
147+
let newNow = stack.time * 1000000;
148+
if (newNow <= now) {
149+
newNow = now + 1;
150+
}
151+
now = newNow;
140152
stackFrames[stackNo] = {
141153
name: nextFrame.location,
142154
parent: perfettoStack.length ? String(perfettoStack[perfettoStack.length - 1]) : undefined
@@ -145,7 +157,7 @@ for (const stack of stacks) {
145157
ph: "B",
146158
name: nextFrame.location,
147159
sf: String(stackNo),
148-
ts: stack.time * 1000000,
160+
ts: newNow,
149161
cat: stack.category !== "" ? stack.category : undefined
150162
});
151163
perfettoStack.push(stackNo++);

0 commit comments

Comments
 (0)