Changeset

3616:c0bc97c0ba61

mod_log_events_by_cpu_usage: Log events where more than a certain amount of CPU time was spent
author Kim Alvefur <zash@zash.se>
date Mon, 10 Jun 2019 15:23:52 +0200
parents 3615:5fe34e5f9829
children 3617:61b9c89db212
files mod_log_events_by_cpu_usage/README.markdown mod_log_events_by_cpu_usage/mod_log_events_by_cpu_usage.lua
diffstat 2 files changed, 31 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_log_events_by_cpu_usage/README.markdown	Mon Jun 10 15:23:52 2019 +0200
@@ -0,0 +1,10 @@
+This module logs events where more than a certain amount of CPU time was
+spent.
+
+``` lua
+log_cpu_threshold = 0.01 -- in seconds, so this is 10 milliseconds
+```
+
+Uses the Lua
+[`os.clock()`](http://www.lua.org/manual/5.2/manual.html#pdf-os.clock)
+function to estimate CPU usage.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_log_events_by_cpu_usage/mod_log_events_by_cpu_usage.lua	Mon Jun 10 15:23:52 2019 +0200
@@ -0,0 +1,21 @@
+module:set_global();
+
+local treshold = module:get_option_number("log_cpu_threshold", 0.01);
+
+function event_wrapper(handlers, event_name, event_data)
+	local cpu_before = os.clock();
+	local ret = handlers(event_name, event_data);
+	local cpu_after = os.clock();
+	if (cpu_after - cpu_before) > treshold then
+		module:log("warn", "%g seconds of CPU usage while processing event '%s'", (cpu_after - cpu_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