Builtin/profiler: Replace game profiler (#4245)
[oweals/minetest.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 profiler_path = core.get_builtin_path()..DIR_DELIM.."profiler"..DIR_DELIM
19 local profiler = {}
20 local sampler = assert(loadfile(profiler_path .. "sampling.lua"))(profiler)
21 local instrumentation  = assert(loadfile(profiler_path .. "instrumentation.lua"))(profiler, sampler)
22 local reporter = dofile(profiler_path .. "reporter.lua")
23 profiler.instrument = instrumentation.instrument
24
25 ---
26 -- Delayed registration of the /profiler chat command
27 -- Is called later, after `core.register_chatcommand` was set up.
28 --
29 function profiler.init_chatcommand()
30         local instrument_profiler = core.setting_getbool("instrument.profiler") or false
31         if instrument_profiler then
32                 instrumentation.init_chatcommand()
33         end
34
35         local param_usage = "print [filter] | dump [filter] | save [format [filter]] | reset"
36         core.register_chatcommand("profiler", {
37                 description = "handle the profiler and profiling data",
38                 params = param_usage,
39                 privs = { server=true },
40                 func = function(name, param)
41                         local command, arg0 = string.match(param, "([^ ]+) ?(.*)")
42                         local args = arg0 and string.split(arg0, " ")
43
44                         if command == "dump" then
45                                 core.log("action", reporter.print(sampler.profile, arg0))
46                                 return true, "Statistics written to action log"
47                         elseif command == "print" then
48                                 return true, reporter.print(sampler.profile, arg0)
49                         elseif command == "save" then
50                                 return reporter.save(sampler.profile, args[1] or "txt", args[2])
51                         elseif command == "reset" then
52                                 sampler.reset()
53                                 return true, "Statistics were reset"
54                         end
55
56                         return false, string.format(
57                                 "Usage: %s\n" ..
58                                 "Format can be one of txt, csv, lua, json, json_pretty (structures may be subject to change).",
59                                 param_usage
60                         )
61                 end
62         })
63
64         if not instrument_profiler then
65                 instrumentation.init_chatcommand()
66         end
67 end
68
69 sampler.init()
70 instrumentation.init()
71
72 return profiler