2 --Copyright (C) 2016 T4im
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.
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.
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.
18 local function get_bool_default(name, default)
19 local val = core.settings:get_bool(name)
26 local profiler_path = core.get_builtin_path()..DIR_DELIM.."profiler"..DIR_DELIM
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
34 -- Delayed registration of the /profiler chat command
35 -- Is called later, after `core.register_chatcommand` was set up.
37 function profiler.init_chatcommand()
38 local instrument_profiler = get_bool_default("instrument.profiler", false)
39 if instrument_profiler then
40 instrumentation.init_chatcommand()
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",
47 privs = { server=true },
48 func = function(name, param)
49 local command, arg0 = string.match(param, "([^ ]+) ?(.*)")
50 local args = arg0 and string.split(arg0, " ")
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
61 return true, "Statistics were reset"
64 return false, string.format(
66 "Format can be one of txt, csv, lua, json, json_pretty (structures may be subject to change).",
72 if not instrument_profiler then
73 instrumentation.init_chatcommand()
78 instrumentation.init()