[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250501041418.3324279-1-xu.xin16@zte.com.cn>
Date: Thu, 1 May 2025 04:14:18 +0000
From: xu.xin.sc@...il.com
To: xu.xin16@....com.cn
Cc: akpm@...ux-foundation.org,
david@...hat.com,
linux-fsdevel@...r.kernel.org,
linux-kernel@...r.kernel.org,
linux-mm@...ck.org,
wang.yaxin@....com.cn,
yang.yang29@....com.cn,
Haonan Chen <chen.haonan2@....com.cn>
Subject: [PATCH v2 3/9] memcontrol: introduce ksm_stat at memcg-v2
From: xu xin <xu.xin16@....com.cn>
With the enablement of container-level KSM (e.g., via prctl), there is a
growing demand for container-level observability of KSM behavior. However,
current cgroup implementations lack support for exposing KSM-related
metrics.
This patch introduces a new interface named ksm_stat
at the cgroup hierarchy level, enabling users to monitor KSM merging
statistics specifically for containers where this feature has been
activated, eliminating the need to manually inspect KSM information for
each individual process within the memcg-v2.
Users can obtain the KSM information of a cgroup just by:
`cat /sys/fs/cgroup/memory.ksm_stat`
Co-developed-by: Haonan Chen <chen.haonan2@....com.cn>
Signed-off-by: Haonan Chen <chen.haonan2@....com.cn>
Signed-off-by: xu xin <xu.xin16@....com.cn>
---
mm/memcontrol.c | 40 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 629e2ce2d830..69521a66639b 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -4388,6 +4388,40 @@ static int memory_numa_stat_show(struct seq_file *m, void *v)
}
#endif
+#ifdef CONFIG_KSM
+struct memcg_ksm_stat {
+ unsigned long ksm_rmap_items;
+};
+
+static int evaluate_memcg_ksm_stat(struct task_struct *task, void *arg)
+{
+ struct mm_struct *mm;
+ struct memcg_ksm_stat *ksm_stat = arg;
+
+ mm = get_task_mm(task);
+ if (mm) {
+ ksm_stat->ksm_rmap_items += mm->ksm_rmap_items;
+ mmput(mm);
+ }
+
+ return 0;
+}
+
+static int memcg_ksm_stat_show(struct seq_file *m, void *v)
+{
+ struct memcg_ksm_stat ksm_stat;
+ struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
+
+ /* Initialization */
+ ksm_stat.ksm_rmap_items = 0;
+ /* summing all processes'ksm statistic items of this cgroup hierarchy */
+ mem_cgroup_scan_tasks(memcg, evaluate_memcg_ksm_stat, &ksm_stat);
+ seq_printf(m, "ksm_rmap_items %lu\n", ksm_stat.ksm_rmap_items);
+
+ return 0;
+}
+#endif
+
static int memory_oom_group_show(struct seq_file *m, void *v)
{
struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
@@ -4554,6 +4588,12 @@ static struct cftype memory_files[] = {
.name = "numa_stat",
.seq_show = memory_numa_stat_show,
},
+#endif
+#ifdef CONFIG_KSM
+ {
+ .name = "ksm_stat",
+ .seq_show = memcg_ksm_stat_show,
+ },
#endif
{
.name = "oom.group",
--
2.15.2
Powered by blists - more mailing lists