Changeset

3615:5fe34e5f9829

mod_log_events_by_memory: Log events where Lua memory usage increased
author Kim Alvefur <zash@zash.se>
date Mon, 10 Jun 2019 14:38:06 +0200
parents 3614:f74444b0e187
children 3616:c0bc97c0ba61
files mod_log_events_by_memory/README.markdown mod_log_events_by_memory/mod_log_events_by_memory.lua
diffstat 2 files changed, 29 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_log_events_by_memory/README.markdown	Mon Jun 10 14:38:06 2019 +0200
@@ -0,0 +1,8 @@
+This module compares the memory usage reported by Lua before and after
+each event and reports it to the log if it exceeds the configuration
+setting `log_memory_threshold` (in bytes).
+
+``` lua
+log_memory_threshold = 20*1024
+```
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_log_events_by_memory/mod_log_events_by_memory.lua	Mon Jun 10 14:38:06 2019 +0200
@@ -0,0 +1,21 @@
+module:set_global();
+
+local treshold = module:get_option_number("log_memory_threshold", 20*1024);
+
+function event_wrapper(handlers, event_name, event_data)
+	local memory_before = collectgarbage("count")*1024;
+	local ret = handlers(event_name, event_data);
+	local memory_after = collectgarbage("count")*1024;
+	if (memory_after - memory_before) > treshold then
+		module:log("warn", "Memory increased by %g bytes while processing event '%s'", (memory_after - memory_before), event_name);
+	end
+	return ret;
+end
+
+local http_events = require "net.http.server"._events;
+module:wrap_object_event(http_events, false, event_wrapper);
+
+module:wrap_event(false, event_wrapper);
+function module.add_host(module)
+	module:wrap_event(false, event_wrapper);
+end