diff options
author | Thomas Rudin <thomas@rudin.io> | 2019-10-09 20:59:42 +0200 |
---|---|---|
committer | Thomas Rudin <thomas@rudin.io> | 2019-10-09 20:59:42 +0200 |
commit | f1ffd844c0ae52823bef0dbbbc51cce35217c898 (patch) | |
tree | 95910324797f2968e5576f5f3a278b2b0a6aefaa | |
parent | 3c99500ff8d0e7344d1dcb443428c34a8ef9b2c2 (diff) | |
download | mesecons_debug-f1ffd844c0ae52823bef0dbbbc51cce35217c898.tar.gz mesecons_debug-f1ffd844c0ae52823bef0dbbbc51cce35217c898.zip |
radical cleanup
-rw-r--r-- | .luacheckrc | 2 | ||||
-rw-r--r-- | add_action.lua | 37 | ||||
-rw-r--r-- | api_action_on.lua | 49 | ||||
-rw-r--r-- | api_nodetimer.lua | 49 | ||||
-rw-r--r-- | circuit_breaker.lua | 138 | ||||
-rw-r--r-- | dump_queue.lua | 20 | ||||
-rw-r--r-- | globalstep.lua | 122 | ||||
-rw-r--r-- | init.lua | 6 | ||||
-rw-r--r-- | overrides.lua | 42 | ||||
-rw-r--r-- | register.lua | 49 |
10 files changed, 44 insertions, 470 deletions
diff --git a/.luacheckrc b/.luacheckrc index 550811a..e3a07c4 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -1,6 +1,6 @@ -allow_defined_top = true globals = { + "mesecons_debug", "mesecon", "minetest" } diff --git a/add_action.lua b/add_action.lua deleted file mode 100644 index 30061a1..0000000 --- a/add_action.lua +++ /dev/null @@ -1,37 +0,0 @@ --- replaces the provided add_action from the mesecons mod - - --- If add_action with twice the same overwritecheck and same position are called, the first one is overwritten --- use overwritecheck nil to never overwrite, but just add the event to the queue --- priority specifies the order actions are executed within one globalstep, highest first --- should be between 0 and 1 -function mesecon.queue:add_action(pos, func, params, time, overwritecheck, priority) - -- Create Action Table: - time = time or 0 -- time <= 0 --> execute, time > 0 --> wait time until execution - priority = priority or 1 - local action = { pos=mesecon.tablecopy(pos), - func=func, - params=mesecon.tablecopy(params or {}), - time=time, - owcheck=(overwritecheck and mesecon.tablecopy(overwritecheck)) or nil, - priority=priority} - - local toremove = nil - -- Otherwise, add the action to the queue - if overwritecheck then -- check if old action has to be overwritten / removed: - for i, ac in ipairs(mesecon.queue.actions) do - if vector.equals(pos, ac.pos) - and action.func == ac.func then - -- and mesecon.cmpAny(overwritecheck, ac.owcheck)) then - toremove = i - break - end - end - end - - if (toremove ~= nil) then - table.remove(mesecon.queue.actions, toremove) - end - - table.insert(mesecon.queue.actions, action) -end diff --git a/api_action_on.lua b/api_action_on.lua deleted file mode 100644 index 6046f90..0000000 --- a/api_action_on.lua +++ /dev/null @@ -1,49 +0,0 @@ - - --- node.mesecons.effector.action_on() wrapper --- def = { node = "", suffix = "" } -mesecons_debug.register_action_on_toggle = function(def) - - local nodedef = minetest.registered_nodes[def.node] - - local old_action_on = nodedef and - nodedef.mesecons and - nodedef.mesecons.effector and - nodedef.mesecons.effector.action_on - - if not old_action_on then - minetest.log( - "action", - "[mesecons_debug] invalid definition for " .. def.node - ) - return - end - - local enabled = true - - nodedef.mesecons.effector.action_on = function(...) - if enabled then - old_action_on(...) - end - end - - minetest.register_chatcommand("mesecons_debug_disable_" .. def.suffix, { - description = "disables the mesecon action_on() function for " .. def.node, - privs = {mesecons_debug=true}, - func = function() - enabled = false - return true, "Disabled action_on() for " .. def.node - end - }) - - minetest.register_chatcommand("mesecons_debug_enable_" .. def.suffix, { - description = "enables the mesecon action_on() function for " .. def.node, - privs = {mesecons_debug=true}, - func = function() - enabled = true - return true, "Enabled action_on() for " .. def.node - end - }) - - -end diff --git a/api_nodetimer.lua b/api_nodetimer.lua deleted file mode 100644 index 8e64277..0000000 --- a/api_nodetimer.lua +++ /dev/null @@ -1,49 +0,0 @@ - - --- node.on_timer wrapper --- def = { node = "", suffix = "" } -mesecons_debug.register_nodetimer_toggle = function(def) - - local nodedef = minetest.registered_nodes[def.node] - - local old_on_timer = nodedef and nodedef.on_timer - - if not old_on_timer then - minetest.log( - "action", - "[mesecons_debug] invalid definition for " .. def.node - ) - return - end - - local enabled = true - - nodedef.on_timer = function(...) - if enabled then - return old_on_timer(...) - else - -- rerun nodetimer again - return true - end - end - - minetest.register_chatcommand("mesecons_debug_disable_" .. def.suffix, { - description = "disables the nodetimer for " .. def.node, - privs = {mesecons_debug=true}, - func = function() - enabled = false - return true, "Disabled nodetimer for " .. def.node - end - }) - - minetest.register_chatcommand("mesecons_debug_enable_" .. def.suffix, { - description = "enables the nodetimer for " .. def.node, - privs = {mesecons_debug=true}, - func = function() - enabled = true - return true, "Enabled nodetimer for " .. def.node - end - }) - - -end diff --git a/circuit_breaker.lua b/circuit_breaker.lua deleted file mode 100644 index ce1452a..0000000 --- a/circuit_breaker.lua +++ /dev/null @@ -1,138 +0,0 @@ - --- "circuit break" mapblocks in which mesecons took too long to execute --- TODO: toggleable hud with current cpu usage - --- sample/reset interval -local sample_interval = 10 - --- util --- minetest.hash_node_position(get_blockpos(pos)) -local function get_blockpos(pos) - return {x = math.floor(pos.x / 16), - y = math.floor(pos.y / 16), - z = math.floor(pos.z / 16)} -end - - --- per block cpu time usage in micros -local per_block_time_usage = {} - --- max per block cpu time usage in micros -local max_per_block_time_usage = {} - --- disabled/dark mapblocks -local dark_mapblocks = {} - --- switch off setting -local max_time_setting -local dark_time - -function update_settings() - max_time_setting = tonumber( minetest.settings:get("mesecons_debug.circuit_breaker") or "75000" ) - dark_time = tonumber( minetest.settings:get("mesecons_debug.dark_time") or "30000000" ) -end - -update_settings() - --- periodic timer -local timer = 0 -minetest.register_globalstep(function(dtime) - timer = timer + dtime - if timer < sample_interval then return end - timer=0 - - -- reset time usage - per_block_time_usage = {} - - -- update settings, if changed - update_settings() - -end) - --- mesecon mod overrides -local old_execute = mesecon.queue.execute -mesecon.queue.execute = function(self, action) - local blockpos = get_blockpos(action.pos) - local hash = minetest.hash_node_position(blockpos) - local t0 = minetest.get_us_time() - - local dark_timer = dark_mapblocks[hash] - if dark_timer and dark_timer < t0 then - -- timeout expired, disable mapblock throttling - dark_mapblocks[hash] = nil - dark_timer = nil - end - - local time_usage = per_block_time_usage[hash] or 0 - - old_execute(self, action) - local t1 = minetest.get_us_time() - local diff = t1 -t0 - time_usage = time_usage + diff - - -- update max stats - if (max_per_block_time_usage[hash] or 0) < time_usage then - max_per_block_time_usage[hash] = time_usage - end - - if time_usage > max_time_setting and not dark_timer then - -- time usage exceeded, throttle mapblock - dark_mapblocks[hash] = t1 + dark_time - minetest.log("warning", "[mesecons_debug] throttled mapblock at " .. - minetest.pos_to_string(action.pos)) - end - - -- update time usage - per_block_time_usage[hash] = time_usage -end - -local old_add_action = mesecon.queue.add_action -mesecon.queue.add_action = function(self, pos, func, params, time, overwritecheck, priority) - time = time or 0 - local blockpos = get_blockpos(pos) - local hash = minetest.hash_node_position(blockpos) - - local dark_timer = dark_mapblocks[hash] - if dark_timer then - -- throttle add actions - time = time + 1 - end - - old_add_action(self, pos, func, params, time, overwritecheck, priority) -end - - --- chat commands - -minetest.register_chatcommand("mesecons_debug_circuit_breaker_stats", { - description = "shows the stats for the current mapblock", - func = function(name) - local player = minetest.get_player_by_name(name) - local pos = player:get_pos() - local blockpos = get_blockpos(pos) - local hash = minetest.hash_node_position(blockpos) - local time_usage = max_per_block_time_usage[hash] or 0 - - local t0 = minetest.get_us_time() - local dark_timer = dark_mapblocks[hash] - - local msg = "Max-time usage: " .. time_usage .. " micro-seconds " .. - "(sampled over " .. sample_interval .. " seconds)" - - if dark_timer and dark_timer > t0 then - msg = msg .. " [Mapblock throttled!]" - end - - return true, msg - end -}) - -minetest.register_chatcommand("mesecons_debug_circuit_breaker_stats_reset", { - description = "resets the max stats", - privs = {mesecons_debug=true}, - func = function() - max_per_block_time_usage = {} - dark_mapblocks = {} - return true, "circuit breaker stats cleared!" - end -}) diff --git a/dump_queue.lua b/dump_queue.lua deleted file mode 100644 index b3b409f..0000000 --- a/dump_queue.lua +++ /dev/null @@ -1,20 +0,0 @@ - - -mesecons_debug.dump_queue = function() - minetest.log("warning", "[dump_queue] dumping mesecons action-queue") - - local fname = minetest.get_worldpath().."/mesecons_dump_" .. minetest.get_us_time() .. ".json" - - local f = io.open(fname, "w") - local data_string, err = minetest.write_json(mesecon.queue.actions) - if err then - error(err) - end - f:write(data_string) - io.close(f) - - minetest.log("action", "[dump_queue] dumped " .. #mesecon.queue.actions .. - " actions to " .. fname .. - " bytes: " .. string.len(data_string) - ) -end diff --git a/globalstep.lua b/globalstep.lua deleted file mode 100644 index e28d79e..0000000 --- a/globalstep.lua +++ /dev/null @@ -1,122 +0,0 @@ - --- enable/disable mesecons entirely - -local enabled = true -local queue_dump_counter = 0 - -local step_index = 0 - --- globalstep on/off -for i, globalstep in ipairs(minetest.registered_globalsteps) do - local info = minetest.callback_origins[globalstep] - if not info then - break - end - - local modname = info.mod - - if modname == "mesecons" then - step_index = step_index + 1 - if step_index > 1 then - -- only override first globalstep in mesecons - break - end - - local cooldown = 0 - local last_run_time = 0 - - local fn = function(dtime) - if cooldown > 0 then - cooldown = cooldown - 1 - return - end - - if enabled then - - local max_globalstep_time = tonumber(minetest.settings:get("mesecons_debug_max_globalstep_time")) or 75000 - local min_delay_time = tonumber(minetest.settings:get("mesecons_debug_min_delay_time")) or 200000 - local cooldown_steps = tonumber(minetest.settings:get("mesecons_debug_cooldown_steps")) or 5 - local autoflush = minetest.settings:get_bool("mesecons_debug_autoflush", false) - - local now = minetest.get_us_time() - if (now - last_run_time) < min_delay_time then - -- adhere to min delay - return - end - - last_run_time = now - - if queue_dump_counter > 0 then - -- dump action queue - mesecons_debug.dump_queue() - queue_dump_counter = queue_dump_counter - 1 - end - - -- execute with time measurement - local t0 = minetest.get_us_time() - globalstep(dtime) - local t1 = minetest.get_us_time() - local diff = t1 - t0 - - if diff > max_globalstep_time then - cooldown = cooldown_steps - minetest.log("warning", "[mesecons_debug] cooldown triggered") - if autoflush then - mesecon.queue.actions = {} - end - end - end - end - - minetest.callback_origins[fn] = info - minetest.registered_globalsteps[i] = fn - end -end - --- execute() -local old_execute = mesecon.queue.execute -mesecon.queue.execute = function(...) - if enabled then - old_execute(...) - end -end - --- add_action() -local old_add_action = mesecon.queue.add_action -mesecon.queue.add_action = function(...) - if enabled then - old_add_action(...) - end -end - - --- mesecons commands -minetest.register_chatcommand("dump_queue", { - description = "dumps the current actionqueue to a file for later processing", - privs = { mesecons_debug = true }, - func = function() - queue_dump_counter = 10 - return true, "processing.." - end -}) - - -minetest.register_chatcommand("mesecons_enable", { - description = "enables the mesecons globlastep", - privs = {mesecons_debug=true}, - func = function() - enabled = true - return true, "mesecons enabled" - end -}) - -minetest.register_chatcommand("mesecons_disable", { - description = "disables the mesecons globlastep", - privs = {mesecons_debug=true}, - func = function() - enabled = false - -- flush actions, while we are on it - mesecon.queue.actions = {} - return true, "mesecons disabled" - end -}) @@ -4,11 +4,7 @@ mesecons_debug = {} ---- dofile(MP.."/add_action.lua") dofile(MP.."/privs.lua") -dofile(MP.."/api_action_on.lua") -dofile(MP.."/api_nodetimer.lua") -dofile(MP.."/register.lua") dofile(MP.."/flush.lua") -dofile(MP.."/globalstep.lua") -dofile(MP.."/dump_queue.lua") +dofile(MP.."/overrides.lua") print("[OK] mesecons_debug loaded") diff --git a/overrides.lua b/overrides.lua new file mode 100644 index 0000000..4bf241e --- /dev/null +++ b/overrides.lua @@ -0,0 +1,42 @@ + +-- enable/disable mesecons entirely + +local enabled = true + + +-- execute() +local old_execute = mesecon.queue.execute +mesecon.queue.execute = function(...) + if enabled then + old_execute(...) + end +end + +-- add_action() +local old_add_action = mesecon.queue.add_action +mesecon.queue.add_action = function(...) + if enabled then + old_add_action(...) + end +end + + +minetest.register_chatcommand("mesecons_enable", { + description = "enables the mesecons globlastep", + privs = {mesecons_debug=true}, + func = function() + enabled = true + return true, "mesecons enabled" + end +}) + +minetest.register_chatcommand("mesecons_disable", { + description = "disables the mesecons globlastep", + privs = {mesecons_debug=true}, + func = function() + enabled = false + -- flush actions, while we are on it + mesecon.queue.actions = {} + return true, "mesecons disabled" + end +}) diff --git a/register.lua b/register.lua deleted file mode 100644 index ba2ece8..0000000 --- a/register.lua +++ /dev/null @@ -1,49 +0,0 @@ - --- mesecons.effector.action_on - -mesecons_debug.register_action_on_toggle({ - node = "pipeworks:filter", - suffix = "pipeworks_filter" -}) - -mesecons_debug.register_action_on_toggle({ - node = "pipeworks:mese_filter", - suffix = "pipeworks_mese_filter" -}) - -mesecons_debug.register_action_on_toggle({ - node = "pipeworks:dispenser_off", - suffix = "pipeworks_dispenser" -}) - -mesecons_debug.register_action_on_toggle({ - node = "pipeworks:deployer_off", - suffix = "pipeworks_deployer" -}) - -mesecons_debug.register_action_on_toggle({ - node = "pipeworks:nodebreaker_off", - suffix = "pipeworks_nodebreaker" -}) - -mesecons_debug.register_action_on_toggle({ - node = "technic:constructor_mk1_off", - suffix = "constructor_mk1" -}) - -mesecons_debug.register_action_on_toggle({ - node = "technic:constructor_mk2_off", - suffix = "constructor_mk2" -}) - -mesecons_debug.register_action_on_toggle({ - node = "technic:constructor_mk3_off", - suffix = "constructor_mk3" -}) - --- nodetimer - -mesecons_debug.register_nodetimer_toggle({ - node = "mesecons_blinkyplant:blinky_plant_off", - suffix = "blinky_plant" -}) |