]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/profiler/init.lua
CSM: Use server-like (and safe) HTTP API instead of Mainmenu-like
[dragonfireclient.git] / builtin / profiler / init.lua
1 --Minetest
2 --Copyright (C) 2016 T4im
3 --
4 --This program is free software; you can redistribute it and/or modify
5 --it under the terms of the GNU Lesser General Public License as published by
6 --the Free Software Foundation; either version 2.1 of the License, or
7 --(at your option) any later version.
8 --
9 --This program is distributed in the hope that it will be useful,
10 --but WITHOUT ANY WARRANTY; without even the implied warranty of
11 --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 --GNU Lesser General Public License for more details.
13 --
14 --You should have received a copy of the GNU Lesser General Public License along
15 --with this program; if not, write to the Free Software Foundation, Inc.,
16 --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 local function get_bool_default(name, default)
19         local val = core.settings:get_bool(name)
20         if val == nil then
21                 return default
22         end
23         return val
24 end
25
26 local profiler_path = core.get_builtin_path().."profiler"..DIR_DELIM
27 local profiler = {}
28 local sampler = assert(loadfile(profiler_path .. "sampling.lua"))(profiler)
29 local instrumentation  = assert(loadfile(profiler_path .. "instrumentation.lua"))(profiler, sampler, get_bool_default)
30 local reporter = dofile(profiler_path .. "reporter.lua")
31 profiler.instrument = instrumentation.instrument
32
33 ---
34 -- Delayed registration of the /profiler chat command
35 -- Is called later, after `core.register_chatcommand` was set up.
36 --
37 function profiler.init_chatcommand()
38         local instrument_profiler = get_bool_default("instrument.profiler", false)
39         if instrument_profiler then
40                 instrumentation.init_chatcommand()
41         end
42
43         local param_usage = "print [filter] | dump [filter] | save [format [filter]] | reset"
44         core.register_chatcommand("profiler", {
45                 description = "handle the profiler and profiling data",
46                 params = param_usage,
47                 privs = { server=true },
48                 func = function(name, param)
49                         local command, arg0 = string.match(param, "([^ ]+) ?(.*)")
50                         local args = arg0 and string.split(arg0, " ")
51
52                         if command == "dump" then
53                                 core.log("action", reporter.print(sampler.profile, arg0))
54                                 return true, "Statistics written to action log"
55                         elseif command == "print" then
56                                 return true, reporter.print(sampler.profile, arg0)
57                         elseif command == "save" then
58                                 return reporter.save(sampler.profile, args[1] or "txt", args[2])
59                         elseif command == "reset" then
60                                 sampler.reset()
61                                 return true, "Statistics were reset"
62                         end
63
64                         return false, string.format(
65                                 "Usage: %s\n" ..
66                                 "Format can be one of txt, csv, lua, json, json_pretty (structures may be subject to change).",
67                                 param_usage
68                         )
69                 end
70         })
71
72         if not instrument_profiler then
73                 instrumentation.init_chatcommand()
74         end
75 end
76
77 sampler.init()
78 instrumentation.init()
79
80 return profiler