Skip to content

Commit

Permalink
Better cgroup memory metrics
Browse files Browse the repository at this point in the history
* substract total_inactive_file from memory usage to show the same
values as docker
* get/report the dirty bytes cgroup metric
  • Loading branch information
Alexander Kukushkin committed Jan 27, 2020
1 parent bfc6872 commit b983b8b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bg_mon.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ static void prepare_statistics_output(struct evbuffer *evb)
evbuffer_add_printf(evb, "}}, \"cgroup\": {");
if (cm.available) {
evbuffer_add_printf(evb, "\"memory\": {\"limit\": %lu, \"usage\": %lu", cm.limit, cm.usage);
evbuffer_add_printf(evb, ", \"rss\": %lu, \"cache\": %lu", cm.rss, cm.cache);
evbuffer_add_printf(evb, ", \"rss\": %lu, \"cache\": %lu, \"dirty\": %lu", cm.rss, cm.cache, cm.dirty);
if (cc.available)
evbuffer_add_printf(evb, "}, ");
}
Expand Down
14 changes: 9 additions & 5 deletions system_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,20 @@ static cgroup_memory read_cgroup_memory_stats(void)
int i = 0, j = 0;
cgroup_memory cm = {0,};
char name[6], buf[255];
unsigned long value;
unsigned long value, total_inactive_file = 0;
struct _mem_tab {
const char *name;
unsigned long *value;
} mem_tab[] = {
{"cache", &cm.cache},
{"rss", &cm.rss},
{"hierarchical_memory_limit", &cm.limit},
{"total_cache", &cm.cache},
{"total_rss", &cm.rss},
{"total_dirty", &cm.dirty},
{"total_inactive_file", &total_inactive_file},
{NULL, NULL}
};

cm.available = true;
cm.limit = cgroup_read_ulong("limit_in_bytes") / 1024;
cm.usage = cgroup_read_ulong("usage_in_bytes") / 1024;

strcpy(memory_cgroup + memory_cgroup_len, "stat");
Expand All @@ -115,7 +117,7 @@ static cgroup_memory read_cgroup_memory_stats(void)

while (i < lengthof(mem_tab) - 1
&& fgets(buf, sizeof(buf), csfd)
&& sscanf(buf, "%5s %lu", name, &value) == 2) {
&& sscanf(buf, "%25s %lu", name, &value) == 2) {
for (j = 0; mem_tab[j].name != NULL; ++j) {
if (strcmp(mem_tab[j].name, name) == 0) {
++i;
Expand All @@ -126,6 +128,8 @@ static cgroup_memory read_cgroup_memory_stats(void)
}
fclose(csfd);

cm.usage = MAXIMUM(cm.usage - total_inactive_file, 0);

return cm;
}

Expand Down
1 change: 1 addition & 0 deletions system_stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ typedef struct {
unsigned long usage;
unsigned long rss;
unsigned long cache;
unsigned long dirty;
} cgroup_memory;

typedef struct {
Expand Down

0 comments on commit b983b8b

Please sign in to comment.