From d8bf23cac765af4bededef26e56f7cde7bafd4fe Mon Sep 17 00:00:00 2001
From: "ilya.kuksenok" <ilya.kuksenok@huawei.com>
Date: Mon, 27 Feb 2023 14:39:39 +0300
Subject: [PATCH] Add metrics

Signed-off-by: ilya.kuksenok <ilya.kuksenok@huawei.com>
---
 src/lxc/lxccontainer.c | 14 ++++++++++++++
 src/lxc/lxccontainer.h |  5 +++++
 2 files changed, 19 insertions(+)

diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
index 4b669bb..afbeb3c 100644
--- a/src/lxc/lxccontainer.c
+++ b/src/lxc/lxccontainer.c
@@ -6053,6 +6053,20 @@ static bool do_lxcapi_get_container_metrics(struct lxc_container *c,  struct lxc
 	metrics->cpu_use_nanos = metrics_get_ull(c, cgroup_ops, "cpuacct.usage");
 	metrics->pids_current = metrics_get_ull(c, cgroup_ops, "pids.current");
 
+	uint64_t usage_bytes; 
+	uint64_t workingset;
+	usage_bytes = metrics_match_get_ull(c,cgroup_ops, "memory.stat", "rss", 1) +  
+					    metrics_match_get_ull(c, cgroup_ops, "memory.stat", "cache", 1) + 
+					    metrics_match_get_ull(c, cgroup_ops, "memory.stat", "swap", 1);
+	// workingset = usage - inactive_file
+	workingset = usage_bytes - metrics_match_get_ull(c, cgroup_ops, "memory.stat", "total_inactive_file", 1);
+	// Avaliable bytes = limit - workingset 
+	metrics->avaliable_bytes = metrics_get_ull(c, cgroup_ops, "memory.limit_in_bytes") - workingset;
+	metrics->usage_bytes = usage_bytes;
+	metrics->rss_bytes = metrics_match_get_ull(c,cgroup_ops, "memory.stat", "rss", 1);
+	metrics->page_faults = metrics_match_get_ull(c,cgroup_ops, "memory.stat", "pgfault", 1);
+	metrics->major_page_faults = metrics_match_get_ull(c,cgroup_ops, "memory.stat", "pgmajfault", 1);
+
 	metrics->cpu_use_user = metrics_match_get_ull(c, cgroup_ops, "cpuacct.stat", "user", 1);
 	metrics->cpu_use_sys = metrics_match_get_ull(c, cgroup_ops, "cpuacct.stat", "system", 1);
 
diff --git a/src/lxc/lxccontainer.h b/src/lxc/lxccontainer.h
index 3680ade..68dfec9 100644
--- a/src/lxc/lxccontainer.h
+++ b/src/lxc/lxccontainer.h
@@ -68,6 +68,11 @@ struct lxc_container_metrics {
     /* Memory usage */
     uint64_t mem_used;
     uint64_t mem_limit;
+    uint64_t avaliable_bytes;
+    uint64_t usage_bytes;
+    uint64_t rss_bytes;
+    uint64_t page_faults;
+    uint64_t major_page_faults;
     /* Kernel Memory usage */
     uint64_t kmem_used;
     uint64_t kmem_limit;
-- 
2.34.1

