-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinit.lua
More file actions
75 lines (66 loc) · 1.86 KB
/
init.lua
File metadata and controls
75 lines (66 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
if not minetest.global_exists("jit") then
minetest.log("warning", "[jitprofiler] " ..
"Since LuaJIT is not being used, this mod is disabled.")
return
end
local profiler_start, profiler_stop
do
local ie = minetest.request_insecure_environment()
if not ie then
error("To use jitprofiler, add it to secure.trusted_mods")
end
local profile = ie.require("jit.profile")
local start, stop, dumpstack =
profile.start, profile.stop, profile.dumpstack
local tonumber = ie.tonumber
function profiler_start(period, outfile)
local function record(thread, samples, vmstate)
outfile:write(dumpstack(thread, "pF;", -100), vmstate,
" ", samples, "\n")
end
start("vfi" .. tonumber(period), record)
end
function profiler_stop()
stop()
end
end
local profiledir = minetest.get_worldpath() .. "/jitprofiles"
assert(minetest.mkdir(profiledir),
"Could not create profile directory " .. profiledir)
local outfile
minetest.register_chatcommand("jitprofiler_start", {
description = "Start LuaJIT's profiler",
params = "<period> <filename>",
privs = {server = true},
func = function(_name, param)
if outfile then
return false, "Profiler already running"
end
local period, filename = param:match("^(%d*)%s+(.*)$")
period = tonumber(period)
if not period or not filename then
return false
end
local path = profiledir .. "/" .. filename
local err
outfile, err = io.open(path, "w")
if not outfile then
return false, "Could not write to file: " .. err
end
profiler_start(period, outfile)
return true, "Profiler started and writing data to " .. path
end,
})
minetest.register_chatcommand("jitprofiler_stop", {
description = "Stop LuaJIT's profiler",
privs = {server = true},
func = function()
if not outfile then
return false, "Profiler not running"
end
profiler_stop()
outfile:close()
outfile = nil
return true, "Profiler stopped"
end,
})