Software /
code /
prosody-modules
Comparison
mod_statistics_mem/mod_statistics_mem.lua @ 1379:403d5cd924eb
mod_statistics_mem: Module that collects memory usage stats from /proc
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 03 Apr 2014 20:57:22 +0200 |
comparison
equal
deleted
inserted
replaced
1378:aa371405db34 | 1379:403d5cd924eb |
---|---|
1 -- Probably Linux-specific memory statistics | |
2 | |
3 module:set_global(); | |
4 | |
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; | |
13 | |
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 | |
21 | |
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 | |
26 | |
27 | |
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; | |
43 }; | |
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; | |
57 }; | |
58 } | |
59 }); |