Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in_mem: add MemAvailable, Buffers and Cached from /proc/meminfo #3092

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 57 additions & 6 deletions plugins/in_mem/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@

struct flb_input_plugin in_mem_plugin;

struct meminfo {
unsigned mem_unit;
unsigned long cached_kb, available_kb, reclaimable_kb;
};

static int in_mem_collect(struct flb_input_instance *i_ins,
struct flb_config *config, void *in_context);
#if 0
Expand Down Expand Up @@ -86,9 +91,36 @@ static uint64_t calc_kb(unsigned long amount, unsigned int unit)
return (uint64_t) bytes;
}

static int parse_meminfo(struct meminfo *g)
{
char buf[60]; /* actual lines we expect are ~30 chars or less */
FILE *fp;
int seen_cached_and_available_and_reclaimable;

fp = fopen("/proc/meminfo", "r");
g->cached_kb = g->available_kb = g->reclaimable_kb = 0;
seen_cached_and_available_and_reclaimable = 3;
while (fgets(buf, sizeof(buf), fp)) {
if (sscanf(buf, "Cached: %lu %*s\n", &g->cached_kb) == 1)
if (--seen_cached_and_available_and_reclaimable == 0)
break;
if (sscanf(buf, "MemAvailable: %lu %*s\n", &g->available_kb) == 1)
if (--seen_cached_and_available_and_reclaimable == 0)
break;
if (sscanf(buf, "SReclaimable: %lu %*s\n", &g->reclaimable_kb) == 1)
if (--seen_cached_and_available_and_reclaimable == 0)
break;
}
/* Have to close because of NOFORK */
fclose(fp);

return seen_cached_and_available_and_reclaimable == 0;
}

static int mem_calc(struct flb_in_mem_info *m_info)
{
int ret;
struct meminfo G;
struct sysinfo info;

ret = sysinfo(&info);
Expand All @@ -97,16 +129,23 @@ static int mem_calc(struct flb_in_mem_info *m_info)
return -1;
}

ret = parse_meminfo(&G);

/* set values in KBs */
m_info->mem_total = calc_kb(info.totalram, info.mem_unit);

/*
* This value seems to be MemAvailable if it is supported
* or MemFree on legacy Linux.
*/
m_info->mem_free = calc_kb(info.freeram, info.mem_unit);

m_info->mem_used = m_info->mem_total - m_info->mem_free;
m_info->mem_buffer = calc_kb(info.bufferram, info.mem_unit);
m_info->mem_cache = G.cached_kb + G.reclaimable_kb;
if (ret) {
m_info->mem_available = G.available_kb;
}
else {
/* On kernels < 3.14, MemAvailable is not provided.
* Show alternate, more meaningful busy/free numbers by counting
* the whole buffer cache as available memory. */
m_info->mem_available = m_info->mem_cache + info.freeram;
};

m_info->swap_total = calc_kb(info.totalswap, info.mem_unit);
m_info->swap_free = calc_kb(info.freeswap, info.mem_unit);
Expand Down Expand Up @@ -218,6 +257,18 @@ static int in_mem_collect(struct flb_input_instance *i_ins,
msgpack_pack_str_body(&mp_pck, "Mem.free", 8);
msgpack_pack_uint64(&mp_pck, info.mem_free);

msgpack_pack_str(&mp_pck, 13);
msgpack_pack_str_body(&mp_pck, "Mem.available", 13);
msgpack_pack_uint64(&mp_pck, info.mem_available);

msgpack_pack_str(&mp_pck, 10);
msgpack_pack_str_body(&mp_pck, "Mem.buffer", 10);
msgpack_pack_uint64(&mp_pck, info.mem_buffer);

msgpack_pack_str(&mp_pck, 9);
msgpack_pack_str_body(&mp_pck, "Mem.cache", 9);
msgpack_pack_uint64(&mp_pck, info.mem_cache);

msgpack_pack_str(&mp_pck, 10);
msgpack_pack_str_body(&mp_pck, "Swap.total", 10);
msgpack_pack_uint64(&mp_pck, info.swap_total);
Expand Down
3 changes: 3 additions & 0 deletions plugins/in_mem/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ struct flb_in_mem_info {
uint64_t mem_total;
uint64_t mem_used;
uint64_t mem_free;
uint64_t mem_available;
uint64_t mem_buffer;
uint64_t mem_cache;
uint64_t swap_total;
uint64_t swap_used;
uint64_t swap_free;
Expand Down