mod_admin_telnet: Add server:memory() command to view details of Prosody's memory usage
authorMatthew Wild <mwild1@gmail.com>
Thu, 16 May 2013 10:47:22 +0100
changeset 5579 e449e6342e36
parent 5578 b8101bc0630f
child 5580 db5d1a350cc7
mod_admin_telnet: Add server:memory() command to view details of Prosody's memory usage
plugins/mod_admin_telnet.lua
--- a/plugins/mod_admin_telnet.lua	Tue May 14 09:39:32 2013 +0100
+++ b/plugins/mod_admin_telnet.lua	Thu May 16 10:47:22 2013 +0100
@@ -236,6 +236,7 @@
 	elseif section == "server" then
 		print [[server:version() - Show the server's version number]]
 		print [[server:uptime() - Show how long the server has been running]]
+		print [[server:memory() - Show details about the server's memory usage]]
 		print [[server:shutdown(reason) - Shut down the server, with an optional reason to be broadcast to all connections]]
 	elseif section == "port" then
 		print [[port:list() - Lists all network ports prosody currently listens on]]
@@ -300,6 +301,26 @@
 	return true, "Shutdown initiated";
 end
 
+local function human(kb)
+	local unit = "K";
+	if kb > 1024 then
+		kb, unit = kb/1024, "M";
+	end
+	return ("%0.2f%sB"):format(kb, unit);
+end
+
+function def_env.server:memory()
+	if not pposix.meminfo then
+		return true, "Lua is using "..collectgarbage("count");
+	end
+	local mem, lua_mem = pposix.meminfo(), collectgarbage("count");
+	local print = self.session.print;
+	print("Process: "..human((mem.allocated+mem.allocated_mmap)/1024));
+	print("   Used: "..human(mem.used/1024).." ("..human(lua_mem).." by Lua)");
+	print("   Free: "..human(mem.unused/1024).." ("..human(mem.returnable/1024).." returnable)");
+	return true, "OK";
+end
+
 def_env.module = {};
 
 local function get_hosts_set(hosts, module)