Software /
code /
prosody-modules
Annotate
mod_measure_memory/mod_measure_memory.lua @ 1623:2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 10 Mar 2015 16:34:40 +0100 |
child | 1655:4d38b8c03dfe |
rev | line source |
---|---|
1623
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 module:set_global(); |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 local measure = require"core.statsmanager".measure; |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 local have_pposix, pposix = pcall(require, "util.pposix"); |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 local measures = {}; |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 setmetatable(measures, { |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 __index = function (t, k, m) |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 m = measure("sizes", "memory."..k); t[k] = m; return m; |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 end |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 }); |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 module:hook("stats-update", function () |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 measures.lua(collectgarbage("count")*1024); |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 end); |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 if have_pposix and pposix.meminfo then |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 module:hook_global("stats-update", function () |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 local m = measures; |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 for k, v in pairs(pposix.meminfo()) do |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 m[k](v); |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 end |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 end); |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 end |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 local statm = io.open("/proc/self/statm"); |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 if statm then |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 statm:close(); |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 local pagesize = module:get_option_number("memory_pagesize", 4096); -- getconf PAGESIZE |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 module:hook("stats-update", function () |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 local statm, err = io.open("/proc/self/statm"); |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 if not statm then |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 module:log("error", tostring(err)); |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 return; |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 end |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 -- virtual memory (caches, opened librarys, everything) |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 measures.total(statm:read("*n") * pagesize); |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 -- resident set size (actually used memory) |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 measures.rss(statm:read("*n") * pagesize); |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 statm:close(); |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 end); |
2c39af0fb93b
mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 end |