diff mod_statistics/stats.lib.lua @ 2209:2733cb8c82a9

mod_statistics: Add 'statistics_meminfo_interval' option to control the number of seconds between calls to mallinfo(). Between calls, cached results are used.
author Matthew Wild <mwild1@gmail.com>
date Mon, 13 Jun 2016 13:15:07 +0100
parents 3e2c4f424797
children dded110af017
line wrap: on
line diff
--- a/mod_statistics/stats.lib.lua	Sun Jun 12 03:21:48 2016 +0200
+++ b/mod_statistics/stats.lib.lua	Mon Jun 13 13:15:07 2016 +0100
@@ -32,6 +32,8 @@
 	c2s_sessions, s2s_sessions = module:shared("/*/c2s/sessions", "/*/s2s/sessions");
 end
 
+local memory_update_interval = module:get_option_number("statistics_meminfo_interval", 60);
+
 local stats = {
 	total_users = {
 		get = function () return it.count(it.keys(bare_sessions)); end
@@ -90,20 +92,30 @@
 };
 
 if has_pposix and pposix.meminfo then
+
+	local cached_meminfo, last_cache_update;
+	local function meminfo()
+		if not cached_meminfo or (os.time() - last_cache_update) > memory_update_interval then
+			cached_meminfo = pposix.meminfo();
+			last_cache_update = os.time();
+		end
+		return cached_meminfo;
+	end
+
 	stats.memory_allocated = {
-		get = function () return math.ceil(pposix.meminfo().allocated); end;
+		get = function () return math.ceil(meminfo().allocated); end;
 		tostring = human;
 	}
 	stats.memory_used = {
-		get = function () return math.ceil(pposix.meminfo().used); end;
+		get = function () return math.ceil(meminfo().used); end;
 		tostring = human;
 	}
 	stats.memory_unused = {
-		get = function () return math.ceil(pposix.meminfo().unused); end;
+		get = function () return math.ceil(meminfo().unused); end;
 		tostring = human;
 	}
 	stats.memory_returnable = {
-		get = function () return math.ceil(pposix.meminfo().returnable); end;
+		get = function () return math.ceil(meminfo().returnable); end;
 		tostring = human;
 	}
 end