init.lua 2.7 KB

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