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 profiler_path = core.get_builtin_path()..DIR_DELIM.."profiler"..DIR_DELIM
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
26 -- Delayed registration of the /profiler chat command
27 -- Is called later, after `core.register_chatcommand` was set up.
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()
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",
39 privs = { server=true },
40 func = function(name, param)
41 local command, arg0 = string.match(param, "([^ ]+) ?(.*)")
42 local args = arg0 and string.split(arg0, " ")
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
53 return true, "Statistics were reset"
56 return false, string.format(
58 "Format can be one of txt, csv, lua, json, json_pretty (structures may be subject to change).",
64 if not instrument_profiler then
65 instrumentation.init_chatcommand()
70 instrumentation.init()