mod_measure_malloc/mod_measure_malloc.lua
author Matthew Wild <mwild1@gmail.com>
Tue, 13 Sep 2022 20:32:20 +0100
changeset 5043 c0d243b27e64
parent 4762 b9af1ccac98b
permissions -rw-r--r--
mod_sasl2, mod_sasl_bind2, mod_sasl2_sm: Bump XEP-0388 namespace This is the namespace for the proposed update to XEP-0388. I.e. not even experimental yet... but this is all a work in progress anyway.

module:set_global();

local metric = require"core.statsmanager".metric;
local pposix = require"util.pposix";

local allocated = metric(
	"gauge", "malloc_heap_allocated", "bytes",
	"Allocated bytes by mode of allocation",
	{"mode"}
);

local used = metric(
	"gauge", "malloc_heap_used", "bytes",
	"Used bytes"
):with_labels();

local unused = metric(
	"gauge", "malloc_heap_unused", "bytes",
	"Unused bytes"
):with_labels();

local returnable = metric(
	"gauge", "malloc_heap_returnable", "bytes",
	"Returnable bytes"
):with_labels();

module:hook("stats-update", function ()
	local meminfo = pposix.meminfo();
	if meminfo.allocated then
		allocated:with_labels("sbrk"):set(meminfo.allocated);
	end
	if meminfo.allocated_mmap then
		allocated:with_labels("mmap"):set(meminfo.allocated_mmap);
	end
	if meminfo.used then
		used:set(meminfo.used);
	end
	if meminfo.unused then
		unused:set(meminfo.unused);
	end
	if meminfo.returnable then
		returnable:set(meminfo.returnable);
	end
end);