mod_muc_webchat_url: Fix default url
[prosody-modules.git] / mod_statistics_mem / mod_statistics_mem.lua
blobdfcadb981f7bfea155f1ed5d078b38f26a7bc4fb
1 -- Probably Linux-specific memory statistics
3 module:set_global();
5 local human;
6 do
7 local tostring = tostring;
8 local s_format = string.format;
9 local m_floor = math.floor;
10 local m_max = math.max;
11 local prefixes = "kMGTPEZY";
12 local multiplier = 1024;
14 function human(num)
15 num = tonumber(num) or 0;
16 local m = 0;
17 while num >= multiplier and m < #prefixes do
18 num = num / multiplier;
19 m = m + 1;
20 end
22 return s_format("%0."..m_max(0,3-#tostring(m_floor(num))).."f%sB",
23 num, m > 0 and (prefixes:sub(m,m) .. "i") or "");
24 end
25 end
28 local pagesize = 4096; -- according to getpagesize()
29 module:provides("statistics", {
30 statistics = {
31 memory_total = { -- virtual memory
32 get = function ()
33 local statm, err = io.open"/proc/self/statm";
34 if statm then
35 local total = statm:read"*n";
36 statm:close();
37 return total * pagesize;
38 else
39 module:log("debug", err);
40 end
41 end;
42 tostring = human;
44 memory_rss = { -- actual in-memory data size
45 get = function ()
46 local statm, err = io.open"/proc/self/statm";
47 if statm then
48 statm:read"*n"; -- Total size, ignore
49 local rss = statm:read"*n";
50 statm:close();
51 return rss * pagesize;
52 else
53 module:log("debug", err);
54 end
55 end;
56 tostring = human;
59 });