linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Lu Jialin <lujialin4@huawei.com>
To: Zefan Li <lizefan.x@bytedance.com>, Tejun Heo <tj@kernel.org>,
	"Johannes Weiner" <hannes@cmpxchg.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Michal Hocko <mhocko@kernel.org>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	Shakeel Butt <shakeelb@google.com>,
	Muchun Song <songmuchun@bytedance.com>
Cc: Lu Jialin <lujialin4@huawei.com>,
	Xiu Jianfeng <xiujianfeng@huawei.com>, <cgroups@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>, <linux-mm@kvack.org>
Subject: [RFC 2/2] memcg: Adapt cgroup.top into per-memcg
Date: Fri, 26 Aug 2022 09:15:03 +0800	[thread overview]
Message-ID: <20220826011503.103894-3-lujialin4@huawei.com> (raw)
In-Reply-To: <20220826011503.103894-1-lujialin4@huawei.com>

cgroup.top is able to show resource usage information for each cgroups.
Currently only memory usage is monitored ,including usage,anon,file,
kmem(Bytes), stats for other resource types would be added as well.

Show case:
/ # mount -t cgroup2 none /sys/fs/cgroup
/ # cd /sys/fs/cgroup/
/sys/fs/cgroup # echo "+memory" > cgroup.subtree_control
/sys/fs/cgroup # mkdir test1
/sys/fs/cgroup # mkdir test2
/sys/fs/cgroup # mkdir test3
/sys/fs/cgroup # echo $$ > test2/cgroup.procs
/sys/fs/cgroup # cd /test
/test # ./memcg_malloc 512000 &
/test # ./memcg_malloc 512000 &
/test # ./memcg_malloc 512000 &
/test # cd /sys/fs/cgroup
/sys/fs/cgroup # echo $$ > test1/cgroup.procs
/sys/fs/cgroup # cd /test
/test # ./memcg_malloc 512000 &
/test # cd /sys/fs/cgroup
/sys/fs/cgroup # echo $$ > test3/cgroup.procs
/sys/fs/cgroup # cat cgroup.top
memory top:
name            usage           anon            file            kernel
test2           1974272         1671168         0               270336
test1           700416          569344          0               94208
test3           196608          86016           0               86016

Signed-off-by: Lu Jialin <lujialin4@huawei.com>
Co-developed-by: Xiu Jianfeng <xiujianfeng@huawei.com>
Signed-off-by: Xiu Jianfeng <xiujianfeng@huawei.com>
---
 mm/memcontrol.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 87 insertions(+)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index b69979c9ced5..e4d4afefe5a6 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -5480,6 +5480,92 @@ static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu)
 	}
 }
 
+static int cmp_usage(const void *a, const void *b)
+{
+	struct mem_cgroup *memcg_a = *(struct mem_cgroup **)a;
+	struct mem_cgroup *memcg_b = *(struct mem_cgroup **)b;
+
+	return page_counter_read(&memcg_b->memory) -
+	       page_counter_read(&memcg_a->memory);
+}
+
+static int child_memcg_css_count(struct cgroup_subsys_state *css)
+{
+	struct cgroup_subsys_state *child;
+	int count = 0;
+
+	css_for_each_child(child, css)
+		count++;
+
+	return count;
+}
+
+struct memory_top_info {
+	const char *name;
+	unsigned long idx;
+};
+
+static const struct memory_top_info memory_info[] = {
+	{"anon",			NR_ANON_MAPPED},
+	{"file",			NR_FILE_PAGES},
+	{"kernel",			MEMCG_KMEM},
+};
+
+static void mem_cgroup_css_top(struct cgroup_subsys_state *css,
+			       struct seq_file *seq)
+{
+	struct mem_cgroup **array;
+	struct cgroup_subsys_state *child;
+	int memcg_number = child_memcg_css_count(css);
+	int i, j;
+	int count = 0;
+
+	mem_cgroup_flush_stats();
+
+	array = kvmalloc_array(memcg_number, sizeof(struct mem_cgroup *),
+			       GFP_KERNEL);
+	if (!array)
+		return;
+
+	css_for_each_child(child, css) {
+		struct mem_cgroup *memcg = mem_cgroup_from_css(child);
+
+		if (count == memcg_number)
+			break;
+		array[count++] = memcg;
+	}
+
+	sort(array, memcg_number, sizeof(struct mem_cgroup *), cmp_usage, NULL);
+
+	seq_printf(seq, "%s top:\n", css->ss->name);
+
+	seq_puts(seq, "name\t\tusage\t\t");
+	for (j = 0; j < ARRAY_SIZE(memory_info); j++)
+		seq_printf(seq, "%s\t\t", memory_info[j].name);
+	seq_puts(seq, "\n");
+
+	for (i = 0; i < memcg_number; i++) {
+		struct mem_cgroup *memcg = array[i];
+		unsigned long usage = page_counter_read(&memcg->memory);
+		struct cgroup *cgroup = memcg->css.cgroup;
+		const char *name = cgroup->kn->name;
+
+		seq_printf(seq, "%s\t\t%lu\t\t", name, usage * PAGE_SIZE);
+		for (j = 0; j < ARRAY_SIZE(memory_info); j++) {
+			u64 size;
+
+			size = memcg_page_state_output(memcg,
+						       memory_info[j].idx);
+			seq_printf(seq, "%llu\t\t", size);
+		}
+		seq_puts(seq, "\n");
+	}
+
+	kvfree(array);
+
+}
+
+
 #ifdef CONFIG_MMU
 /* Handlers for move charge at task migration. */
 static int mem_cgroup_do_precharge(unsigned long count)
@@ -6600,6 +6686,7 @@ struct cgroup_subsys memory_cgrp_subsys = {
 	.css_free = mem_cgroup_css_free,
 	.css_reset = mem_cgroup_css_reset,
 	.css_rstat_flush = mem_cgroup_css_rstat_flush,
+	.css_top = mem_cgroup_css_top,
 	.can_attach = mem_cgroup_can_attach,
 	.cancel_attach = mem_cgroup_cancel_attach,
 	.post_attach = mem_cgroup_move_task,
-- 
2.17.1


  parent reply	other threads:[~2022-08-26  1:18 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-26  1:15 [RFC 0/2] Introduce cgroup.top interface Lu Jialin
2022-08-26  1:15 ` [RFC 1/2] cgroup: Introduce per-cgroup resource top show interface Lu Jialin
2022-08-26  1:15 ` Lu Jialin [this message]
2022-08-26  7:38   ` [RFC 2/2] memcg: Adapt cgroup.top into per-memcg Michal Hocko
2022-08-26  3:17 ` [RFC 0/2] Introduce cgroup.top interface Tejun Heo
2022-08-26  9:50   ` lujialin (A)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220826011503.103894-3-lujialin4@huawei.com \
    --to=lujialin4@huawei.com \
    --cc=akpm@linux-foundation.org \
    --cc=cgroups@vger.kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lizefan.x@bytedance.com \
    --cc=mhocko@kernel.org \
    --cc=roman.gushchin@linux.dev \
    --cc=shakeelb@google.com \
    --cc=songmuchun@bytedance.com \
    --cc=tj@kernel.org \
    --cc=xiujianfeng@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).