diff --git a/.github/workflows/luacheck.yml b/.github/workflows/luacheck.yml new file mode 100644 index 0000000..a03fe92 --- /dev/null +++ b/.github/workflows/luacheck.yml @@ -0,0 +1,17 @@ +name: luacheck + +on: [push] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v1 + - name: apt + run: sudo apt-get install -y luarocks + - name: luacheck install + run: luarocks install --local luacheck + - name: luacheck run + run: $HOME/.luarocks/bin/luacheck ./ diff --git a/.luacheckrc b/.luacheckrc new file mode 100644 index 0000000..53a7bd1 --- /dev/null +++ b/.luacheckrc @@ -0,0 +1,18 @@ +allow_defined_top = true + +globals = { + "monitoring", + "minetest", + "mesecon" +} + +read_globals = { + -- Stdlib + string = {fields = {"split"}}, + table = {fields = {"copy", "getn"}}, + + -- Minetest + "vector", "ItemStack", + "dump" + +} diff --git a/action_on.lua b/action_on.lua new file mode 100644 index 0000000..126688b --- /dev/null +++ b/action_on.lua @@ -0,0 +1,22 @@ + +function register_action_on_metric(nodename, metricname, prettyname) + local nodedef = minetest.registered_nodes[nodename] + if nodedef and nodedef.mesecons and nodedef.mesecons.effector and nodedef.mesecons.effector.action_on then + local metric_count = monitoring.counter("mesecons_" .. metricname .. "_count", + "number of " .. prettyname .. " executes") + local metric_time = monitoring.counter("mesecons_" .. metricname .. "_time", + "total time of " .. prettyname .. " executes in us") + + nodedef.mesecons.effector.action_on = metric_count.wrap( metric_time.wraptime(nodedef.mesecons.effector.action_on) ) + + end +end + +register_action_on_metric("mesecons_pistons:piston_normal_off", "piston_normal_on", "Piston") +register_action_on_metric("mesecons_pistons:piston_sticky_off", "piston_sticky_on", "Sticky piston") + +register_action_on_metric("mesecons_movestones:movestone", "movestone", "Movestone") +register_action_on_metric("mesecons_movestones:sticky_movestone", "sticky_movestone", "Sticky movestone") +register_action_on_metric("mesecons_movestones:movestone_vertical", "movestone_vertical", "Vertical movestone") +register_action_on_metric("mesecons_movestones:sticky_movestone_vertical", + "sticky_movestone_vertical", "Vertical sticky movestone") diff --git a/functions.lua b/functions.lua new file mode 100644 index 0000000..10b522d --- /dev/null +++ b/functions.lua @@ -0,0 +1,40 @@ + + +local metrics = {} -- { { count=, time= }, ... } + + +function register_function(name) + local entry = {} + entry.count = monitoring.counter("mesecons_function_" .. name .. "_count", + "number of " .. name .. " function executes") + entry.time = monitoring.counter("mesecons_function_" .. name .. "_time", + "time of " .. name .." function executes") + + metrics[name] = entry +end + +register_function("change"); +register_function("lc_interrupt"); +register_function("lc_digiline_relay"); +register_function("deactivate"); +register_function("activate"); +register_function("receptor_on"); +register_function("receptor_off"); +register_function("other"); + +local old_execute = mesecon.queue.execute +mesecon.queue.execute = function(self, action) + + local t0 = minetest.get_us_time() + old_execute(self, action) + local t1 = minetest.get_us_time() + local delta_t = t1 - t0 + + local metric = metrics[action.func] + if not metric then + metric = metrics["other"] + end + + metric.count.inc() + metric.time.inc(delta_t) +end diff --git a/globals.lua b/globals.lua new file mode 100644 index 0000000..a9beee0 --- /dev/null +++ b/globals.lua @@ -0,0 +1,30 @@ + + +monitoring.wrap_global({"mesecon", "queue", "add_action"}, "mesecons_queue_action_add") +local execute_calls_metric = monitoring.wrap_global({"mesecon", "queue", "execute"}, "mesecons_queue_execute") + +if monitoring.settings.handle_errors then + -- enable error handling in mesecons queue + local old_execute = mesecon.queue.execute + mesecon.queue.execute = function(self, action) + monitoring.protected_call(execute_calls_metric, function() + old_execute(self, action) + end, action.pos) + end +end + +if minetest.settings:get_bool("monitoring.mesecons.verbose") then + + monitoring.wrap_global({"mesecon", "get_node_force"}, "mesecons_get_node_force") + monitoring.wrap_global({"mesecon", "activate"}, "mesecons_activate") + monitoring.wrap_global({"mesecon", "deactivate"}, "mesecons_deactivate") + monitoring.wrap_global({"mesecon", "turnon"}, "mesecons_turnon") + monitoring.wrap_global({"mesecon", "turnoff"}, "mesecons_turnoff") + monitoring.wrap_global({"mesecon", "changesignal"}, "mesecons_changesignal") + monitoring.wrap_global({"mesecon", "swap_node_force"}, "swap_node_force") + + monitoring.wrap_global({"mesecon", "vm_begin"}, "mesecons_vm_begin") + monitoring.wrap_global({"mesecon", "vm_abort"}, "mesecons_vm_abort") + monitoring.wrap_global({"mesecon", "vm_commit"}, "mesecons_vm_commit") + +end diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..ffb8a4d --- /dev/null +++ b/init.lua @@ -0,0 +1,19 @@ +if not minetest.get_modpath("mesecons") then + print("[monitoring] mesecons extension not loaded") + return +else + print("[monitoring] mesecons extension loaded") +end + + +local MP = minetest.get_modpath("monitoring_mesecons") + +dofile(MP.."/queue.lua") +dofile(MP.."/globals.lua") +dofile(MP.."/luac.lua") + +if minetest.settings:get_bool("monitoring.mesecons.verbose") then + dofile(MP.."/functions.lua") +end + +dofile(MP.."/action_on.lua") diff --git a/license.txt b/license.txt new file mode 100644 index 0000000..795da89 --- /dev/null +++ b/license.txt @@ -0,0 +1,56 @@ +License of source code +---------------------- + +The MIT License (MIT) +Copyright (C) 2018-2019 Thomas Rudin + +Permission is hereby granted, free of charge, to any person obtaining a copy of this +software and associated documentation files (the "Software"), to deal in the Software +without restriction, including without limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of the Software, and to permit +persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or +substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE +FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. + +For more details: +https://opensource.org/licenses/MIT + + +Licenses of media (textures) +---------------------------- + +Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) +Copyright (C) 2019 Thomas Rudin + +You are free to: +Share — copy and redistribute the material in any medium or format. +Adapt — remix, transform, and build upon the material for any purpose, even commercially. +The licensor cannot revoke these freedoms as long as you follow the license terms. + +Under the following terms: + +Attribution — You must give appropriate credit, provide a link to the license, and +indicate if changes were made. You may do so in any reasonable manner, but not in any way +that suggests the licensor endorses you or your use. + +ShareAlike — If you remix, transform, or build upon the material, you must distribute +your contributions under the same license as the original. + +No additional restrictions — You may not apply legal terms or technological measures that +legally restrict others from doing anything the license permits. + +Notices: + +You do not have to comply with the license for elements of the material in the public +domain or where your use is permitted by an applicable exception or limitation. +No warranties are given. The license may not give you all of the permissions necessary +for your intended use. For example, other rights such as publicity, privacy, or moral +rights may limit how you use the material. diff --git a/luac.lua b/luac.lua new file mode 100644 index 0000000..2867776 --- /dev/null +++ b/luac.lua @@ -0,0 +1,47 @@ + +local BASENAME = "mesecons_luacontroller:luacontroller" + + +local metric_count = monitoring.counter( + "mesecons_luacontroller_nodetimer_count", + "number of luac nodetimer calls" +); + +local metric_time = monitoring.counter( + "mesecons_luacontroller_nodetimer_time", + "time of luac nodetimer calls" +); + +local metric_time_max = monitoring.gauge( + "mesecons_luacontroller_nodetimer_time_max", + "max time of luac nodetimer calls", + { autoflush=true } +) + +for a = 0, 1 do -- 0 = off 1 = on +for b = 0, 1 do +for c = 0, 1 do +for d = 0, 1 do + local cid = tostring(d)..tostring(c)..tostring(b)..tostring(a) + local node_name = BASENAME..cid + + local def = minetest.registered_nodes[node_name] + + if def then + local old_on_timer = def.on_timer + + def.on_timer = function(...) + local t0 = minetest.get_us_time() + old_on_timer(...) + local t1 = minetest.get_us_time() + local diff = t1 -t0 + + metric_time_max.setmax(diff) + metric_time.inc(diff) + metric_count.inc(1) + end + end +end +end +end +end diff --git a/mod.conf b/mod.conf new file mode 100644 index 0000000..ed7bf12 --- /dev/null +++ b/mod.conf @@ -0,0 +1,3 @@ +name = monitoring_mesecons +depends = monitoring +optional_depends = mesecons, mesecons_pistons, mesecons_movestones, mesecons_debug, mesecons_luacontroller diff --git a/queue.lua b/queue.lua new file mode 100644 index 0000000..5291ff7 --- /dev/null +++ b/queue.lua @@ -0,0 +1,23 @@ + + + +-- queue size metric +local metric_action_queue_size = monitoring.gauge( + "mesecons_action_queue_size", + "size of action queue" +) + +local metric_action_queue_size_max = monitoring.gauge( + "mesecons_action_queue_size_max", + "max size of action queue", + { autoflush=true } +) + +minetest.register_globalstep(function() + if not mesecon.queue.actions then + return + end + + metric_action_queue_size.set(#mesecon.queue.actions) + metric_action_queue_size_max.setmax(#mesecon.queue.actions) +end) diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..42bcf71 --- /dev/null +++ b/readme.md @@ -0,0 +1,17 @@ + +# Monitoring extension for mesecons + +Base-mod: https://github.com/thomasrudin-mt/monitoring + +# Metrics + +* mesecons_action_queue_count *number of action queue items* +* various, see `metrics.lua` + +# Settings + +* **monitoring.mesecons.verbose** more metrics + +# License + +MIT