2
0

init.lua 2.7 KB

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