Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/resources/pandoc/datadir/profiler.lua
12922 views
1
--[[
2
A low-overhead minimal lua profiler.
3
4
It requires cooperation from the lua interpreter itself, which we patch, and then
5
compile a custom pandoc binary.
6
7
In other words, this is not meant to be used by regular quarto users.
8
]]
9
10
local getTime = os.clock
11
local category = "unknown"
12
local module = {}
13
local outputfile
14
local stack_count = 0
15
16
-- don't colect coverage for this module
17
-- luacov: disable
18
local onDebugHook = function(hookType, line)
19
if hookType ~= "alarm" then
20
print("Internal Error - Unexpected hook type in debug hook: " .. hookType)
21
print("For Quarto profiling to work, you need to run a patched version of Pandoc.")
22
print("This feature is currently only meant to be used by Quarto developers.")
23
os.exit(1)
24
end
25
local no = 2
26
local information = debug.getinfo(no, "nS")
27
local now = os.clock()
28
while information ~= nil do
29
local source = information.source or "unknown"
30
local name = information.name or "anon"
31
if source:sub(1, 1) == "@" then
32
outputfile:write(name, " ", source, " ", information.linedefined, "\n")
33
else
34
outputfile:write(name, " ", "<inline>", " ", 0, "\n")
35
end
36
no = no + 1
37
information = debug.getinfo(no, "nS")
38
end
39
outputfile:write(stack_count, " ", now, " ", category, " ", line, "\n")
40
stack_count = stack_count + 1
41
end
42
43
function module.setcategory(c)
44
category = c
45
end
46
47
function module.start(filename, ms)
48
outputfile = io.open(filename, "a")
49
if outputfile == nil then
50
error("Could not open " .. filename .. " for writing")
51
return
52
end
53
debug.sethook(onDebugHook, "t", ms or 5) -- NB: "t" debugging only exists in our patched Lua interpreter/pandoc binary!
54
end
55
56
function module.stop()
57
debug.sethook()
58
outputfile:close()
59
end
60
-- luacov: enable
61
62
return module
63
64