diff options
author | BuckarooBanzay <BuckarooBanzay@users.noreply.github.com> | 2021-03-14 21:21:37 +0100 |
---|---|---|
committer | BuckarooBanzay <BuckarooBanzay@users.noreply.github.com> | 2021-03-14 21:21:37 +0100 |
commit | fdb69489d823d3cb22f9757732245958f9734f3a (patch) | |
tree | faede629dfe505d68fd8dafc532d47caa492b41c /penalty.lua | |
parent | 40ecd2db7223d178de6d21807499ff17ad3cf2ce (diff) | |
download | mesecons_debug-fdb69489d823d3cb22f9757732245958f9734f3a.tar.gz mesecons_debug-fdb69489d823d3cb22f9757732245958f9734f3a.zip |
rework penalty stuff
addresses some issues in #6
Diffstat (limited to '')
-rw-r--r-- | penalty.lua | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/penalty.lua b/penalty.lua new file mode 100644 index 0000000..b7173b7 --- /dev/null +++ b/penalty.lua @@ -0,0 +1,60 @@ +local has_monitoring = minetest.get_modpath("monitoring") + +local mapblock_count, penalized_mapblock_count + +if has_monitoring then + mapblock_count = monitoring.gauge("mesecons_debug_mapblock_count", "count of tracked mapblocks") + penalized_mapblock_count = monitoring.gauge("mesecons_debug_penalized_mapblock_count", "count of penalized mapblocks") +end + +local timer = 0 +minetest.register_globalstep(function(dtime) + timer = timer + dtime + if timer < 1 then return end + timer=0 + + local penalized_count = 0 + local now = minetest.get_us_time() + local cleanup_time_micros = 300 * 1000 * 1000 + + mesecons_debug.context_store_size = 0 + for hash, ctx in pairs(mesecons_debug.context_store) do + local time_diff = now - ctx.mtime + if time_diff > cleanup_time_micros then + -- remove item + mesecons_debug.context_store[hash] = nil + + else + -- calculate moving average + ctx.avg_micros = math.floor((ctx.avg_micros * 0.8) + (ctx.micros * 0.2)) + -- reset cpu usage counter + ctx.micros = 0 + + -- apply penalty values + if ctx.avg_micros > (mesecons_debug.max_usage_micros * 10) then + -- 10 times the limit used, potential abuse, add a greater penalty value + ctx.penalty = math.min(ctx.penalty + 5, mesecons_debug.max_penalty) + + elseif ctx.avg_micros > mesecons_debug.max_usage_micros then + -- add penalty value + ctx.penalty = math.min(ctx.penalty + 0.2, mesecons_debug.max_penalty) + + elseif ctx.penalty > 0 then + -- remove penalty (very slowly) + ctx.penalty = math.max(ctx.penalty - 0.001, 0) + end + + mesecons_debug.context_store_size = mesecons_debug.context_store_size + 1 + if ctx.penalty > 0 then + penalized_count = penalized_count + 1 + end + + end + end + + if has_monitoring then + mapblock_count.set(mesecons_debug.context_store_size) + penalized_mapblock_count.set(penalized_count) + end + +end) |