Mercurial > prosody-modules
comparison mod_statistics/stats.lib.lua @ 1077:b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sat, 15 Jun 2013 20:57:18 +0100 |
| parents | 5616cab8b6d6 |
| children | 3af947e2e6d4 |
comparison
equal
deleted
inserted
replaced
| 1076:5616cab8b6d6 | 1077:b73d44afdafa |
|---|---|
| 1 local it = require "util.iterators"; | 1 local it = require "util.iterators"; |
| 2 local log = require "util.logger".init("stats"); | 2 local log = require "util.logger".init("stats"); |
| 3 local has_pposix, pposix = pcall(require, "util.pposix"); | |
| 4 local human; | |
| 5 do | |
| 6 local tostring = tostring; | |
| 7 local s_format = string.format; | |
| 8 local m_floor = math.floor; | |
| 9 local m_max = math.max; | |
| 10 local prefixes = "kMGTPEZY"; | |
| 11 local multiplier = 1024; | |
| 12 | |
| 13 function human(num) | |
| 14 num = tonumber(num) or 0; | |
| 15 local m = 0; | |
| 16 while num >= multiplier and m < #prefixes do | |
| 17 num = num / multiplier; | |
| 18 m = m + 1; | |
| 19 end | |
| 20 | |
| 21 return s_format("%0."..m_max(0,3-#tostring(m_floor(num))).."f%sB", | |
| 22 num, m > 0 and (prefixes:sub(m,m) .. "i") or ""); | |
| 23 end | |
| 24 end | |
| 3 | 25 |
| 4 local last_cpu_wall, last_cpu_clock; | 26 local last_cpu_wall, last_cpu_clock; |
| 5 local get_time = require "socket".gettime; | 27 local get_time = require "socket".gettime; |
| 6 | 28 |
| 7 local active_sessions, active_jids = {}, {}; | 29 local active_sessions, active_jids = {}, {}; |
| 42 get = function () return prosody.start_time; end; | 64 get = function () return prosody.start_time; end; |
| 43 tostring = function (up_since) | 65 tostring = function (up_since) |
| 44 return tostring(os.time()-up_since).."s"; | 66 return tostring(os.time()-up_since).."s"; |
| 45 end; | 67 end; |
| 46 }; | 68 }; |
| 47 memory_used = { | 69 memory_lua = { |
| 48 get = function () return collectgarbage("count")/1024; end; | 70 get = function () return math.ceil(collectgarbage("count")*1024); end; |
| 49 tostring = "%0.2fMB"; | 71 tostring = human; |
| 50 }; | |
| 51 memory_process = { | |
| 52 get = function () return pposix.meminfo().allocated/1048576; end; | |
| 53 tostring = "%0.2fMB"; | |
| 54 }; | 72 }; |
| 55 time = { | 73 time = { |
| 56 tostring = function () return os.date("%T"); end; | 74 tostring = function () return os.date("%T"); end; |
| 57 }; | 75 }; |
| 58 cpu = { | 76 cpu = { |
| 65 last_cpu_wall, last_cpu_clock = new_wall, new_clock; | 83 last_cpu_wall, last_cpu_clock = new_wall, new_clock; |
| 66 return math.ceil(pc); | 84 return math.ceil(pc); |
| 67 end; | 85 end; |
| 68 tostring = "%s%%"; | 86 tostring = "%s%%"; |
| 69 }; | 87 }; |
| 88 }; | |
| 89 | |
| 90 if has_pposix and pposix.meminfo then | |
| 91 stats.memory_allocated = { | |
| 92 get = function () return math.ceil(pposix.meminfo().allocated); end; | |
| 93 tostring = human; | |
| 94 } | |
| 95 stats.memory_used = { | |
| 96 get = function () return math.ceil(pposix.meminfo().used); end; | |
| 97 tostring = human; | |
| 98 } | |
| 99 stats.memory_unused = { | |
| 100 get = function () return math.ceil(pposix.meminfo().unused); end; | |
| 101 tostring = human; | |
| 102 } | |
| 103 stats.memory_returnable = { | |
| 104 get = function () return math.ceil(pposix.meminfo().returnable); end; | |
| 105 tostring = human; | |
| 106 } | |
| 107 end | |
| 108 | |
| 109 local pagesize = 4096; | |
| 110 stats.memory_total = { | |
| 111 get = function () | |
| 112 local statm, err = io.open"/proc/self/statm"; | |
| 113 if statm then | |
| 114 local total = statm:read"*n"; | |
| 115 statm:close(); | |
| 116 return total * pagesize; | |
| 117 else | |
| 118 module:log("debug", err); | |
| 119 end | |
| 120 end; | |
| 121 tostring = human; | |
| 122 }; | |
| 123 stats.memory_rss = { | |
| 124 get = function () | |
| 125 local statm, err = io.open"/proc/self/statm"; | |
| 126 if statm then | |
| 127 statm:read"*n"; -- Total size, ignore | |
| 128 local rss = statm:read"*n"; | |
| 129 statm:close(); | |
| 130 return rss * pagesize; | |
| 131 else | |
| 132 module:log("debug", err); | |
| 133 end | |
| 134 end; | |
| 135 tostring = human; | |
| 70 }; | 136 }; |
| 71 | 137 |
| 72 local add_statistics_filter; -- forward decl | 138 local add_statistics_filter; -- forward decl |
| 73 if prosody and prosody.arg then -- ensures we aren't in prosodyctl | 139 if prosody and prosody.arg then -- ensures we aren't in prosodyctl |
| 74 setmetatable(active_sessions, { | 140 setmetatable(active_sessions, { |
