linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC 0/2] Introduce cgroup.top interface
@ 2022-08-26  1:15 Lu Jialin
  2022-08-26  1:15 ` [RFC 1/2] cgroup: Introduce per-cgroup resource top show interface Lu Jialin
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Lu Jialin @ 2022-08-26  1:15 UTC (permalink / raw)
  To: Zefan Li, Tejun Heo, Johannes Weiner, Andrew Morton,
	Michal Hocko, Roman Gushchin, Shakeel Butt, Muchun Song
  Cc: Lu Jialin, Xiu Jianfeng, cgroups, linux-kernel, linux-mm

Cgroup is used to organize and manage resource available processes.
Currently there are no handy tool for gathering reousrce usage
information for each and every child cgroups, makes it hard to detect
resource outage and debug resource issues.

To overcome this, we present the cgroup.top interface. Just like the
top command, user is able to easily gather resource usage information
, allowing user to detect and respond to resource outage in child
cgroups

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


Lu Jialin (1):
  memcg: Adapt cgroup.top into per-memcg

Xiu Jianfeng (1):
  cgroup: Introduce per-cgroup resource top show interface

 include/linux/cgroup-defs.h |  1 +
 kernel/cgroup/cgroup.c      | 20 +++++++++
 mm/memcontrol.c             | 87 +++++++++++++++++++++++++++++++++++++
 3 files changed, 108 insertions(+)

-- 
2.17.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [RFC 1/2] cgroup: Introduce per-cgroup resource top show interface
  2022-08-26  1:15 [RFC 0/2] Introduce cgroup.top interface Lu Jialin
@ 2022-08-26  1:15 ` Lu Jialin
  2022-08-26  1:15 ` [RFC 2/2] memcg: Adapt cgroup.top into per-memcg Lu Jialin
  2022-08-26  3:17 ` [RFC 0/2] Introduce cgroup.top interface Tejun Heo
  2 siblings, 0 replies; 6+ messages in thread
From: Lu Jialin @ 2022-08-26  1:15 UTC (permalink / raw)
  To: Zefan Li, Tejun Heo, Johannes Weiner, Andrew Morton,
	Michal Hocko, Roman Gushchin, Shakeel Butt, Muchun Song
  Cc: Lu Jialin, Xiu Jianfeng, cgroups, linux-kernel, linux-mm

From: Xiu Jianfeng <xiujianfeng@huawei.com>

This patch introduces cgroup.top interface for each cgroup. When
accessed by userspace, cgroup.top is able to:
1. Sort cgroups by their usage on various resources(e.g. memory, cpu, etc.)
2. Display resource usage status on all child cgroups.

Signed-off-by: Xiu Jianfeng <xiujianfeng@huawei.com>
Co-developed-by: Lu Jialin <lujialin4@huawei.com>
Signed-off-by: Lu Jialin <lujialin4@huawei.com>
---
 include/linux/cgroup-defs.h |  1 +
 kernel/cgroup/cgroup.c      | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h
index 4bcf56b3491c..ba11e09f8f03 100644
--- a/include/linux/cgroup-defs.h
+++ b/include/linux/cgroup-defs.h
@@ -651,6 +651,7 @@ struct cgroup_subsys {
 	int (*css_extra_stat_show)(struct seq_file *seq,
 				   struct cgroup_subsys_state *css);
 
+	void (*css_top)(struct cgroup_subsys_state *css, struct seq_file *seq);
 	int (*can_attach)(struct cgroup_taskset *tset);
 	void (*cancel_attach)(struct cgroup_taskset *tset);
 	void (*attach)(struct cgroup_taskset *tset);
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index ffaccd6373f1..01bd1a734a01 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -3867,6 +3867,22 @@ static ssize_t cgroup_kill_write(struct kernfs_open_file *of, char *buf,
 	return ret ?: nbytes;
 }
 
+static int cgroup_top_show(struct seq_file *seq, void *v)
+{
+	struct cgroup *cgrp = seq_css(seq)->cgroup;
+	struct cgroup_subsys_state *css;
+	int ssid;
+
+	rcu_read_lock();
+	for_each_css(css, ssid, cgrp) {
+		if (css->ss->css_top)
+			css->ss->css_top(css, seq);
+	}
+	rcu_read_unlock();
+
+	return 0;
+}
+
 static int cgroup_file_open(struct kernfs_open_file *of)
 {
 	struct cftype *cft = of_cft(of);
@@ -5125,6 +5141,10 @@ static struct cftype cgroup_base_files[] = {
 		.release = cgroup_pressure_release,
 	},
 #endif /* CONFIG_PSI */
+	{
+		.name = "cgroup.top",
+		.seq_show = cgroup_top_show,
+	},
 	{ }	/* terminate */
 };
 
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [RFC 2/2] memcg: Adapt cgroup.top into per-memcg
  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
  2022-08-26  7:38   ` Michal Hocko
  2022-08-26  3:17 ` [RFC 0/2] Introduce cgroup.top interface Tejun Heo
  2 siblings, 1 reply; 6+ messages in thread
From: Lu Jialin @ 2022-08-26  1:15 UTC (permalink / raw)
  To: Zefan Li, Tejun Heo, Johannes Weiner, Andrew Morton,
	Michal Hocko, Roman Gushchin, Shakeel Butt, Muchun Song
  Cc: Lu Jialin, Xiu Jianfeng, cgroups, linux-kernel, linux-mm

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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [RFC 0/2] Introduce cgroup.top interface
  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 ` [RFC 2/2] memcg: Adapt cgroup.top into per-memcg Lu Jialin
@ 2022-08-26  3:17 ` Tejun Heo
  2022-08-26  9:50   ` lujialin (A)
  2 siblings, 1 reply; 6+ messages in thread
From: Tejun Heo @ 2022-08-26  3:17 UTC (permalink / raw)
  To: Lu Jialin
  Cc: Zefan Li, Johannes Weiner, Andrew Morton, Michal Hocko,
	Roman Gushchin, Shakeel Butt, Muchun Song, Xiu Jianfeng, cgroups,
	linux-kernel, linux-mm

Hello,

On Fri, Aug 26, 2022 at 09:15:01AM +0800, Lu Jialin wrote:
> Cgroup is used to organize and manage resource available processes.
> Currently there are no handy tool for gathering reousrce usage
> information for each and every child cgroups, makes it hard to detect
> resource outage and debug resource issues.
> 
> To overcome this, we present the cgroup.top interface. Just like the
> top command, user is able to easily gather resource usage information
> , allowing user to detect and respond to resource outage in child
> cgroups

I don't think this is something we want build into the kernel. Maybe what
you want is something similar to below?

  https://github.com/facebookincubator/below

Thanks.

-- 
tejun

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC 2/2] memcg: Adapt cgroup.top into per-memcg
  2022-08-26  1:15 ` [RFC 2/2] memcg: Adapt cgroup.top into per-memcg Lu Jialin
@ 2022-08-26  7:38   ` Michal Hocko
  0 siblings, 0 replies; 6+ messages in thread
From: Michal Hocko @ 2022-08-26  7:38 UTC (permalink / raw)
  To: Lu Jialin
  Cc: Zefan Li, Tejun Heo, Johannes Weiner, Andrew Morton,
	Roman Gushchin, Shakeel Butt, Muchun Song, Xiu Jianfeng, cgroups,
	linux-kernel, linux-mm

On Fri 26-08-22 09:15:03, Lu Jialin wrote:
> 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

This information is already present in memory.stat and can be easily
post-processed to generate similar output. Why is that insufficient?
-- 
Michal Hocko
SUSE Labs

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC 0/2] Introduce cgroup.top interface
  2022-08-26  3:17 ` [RFC 0/2] Introduce cgroup.top interface Tejun Heo
@ 2022-08-26  9:50   ` lujialin (A)
  0 siblings, 0 replies; 6+ messages in thread
From: lujialin (A) @ 2022-08-26  9:50 UTC (permalink / raw)
  To: Tejun Heo
  Cc: Zefan Li, Johannes Weiner, Andrew Morton, Michal Hocko,
	Roman Gushchin, Shakeel Butt, Muchun Song, Xiu Jianfeng, cgroups,
	linux-kernel, linux-mm

Ok, I got it, thanks

在 2022/8/26 11:17, Tejun Heo 写道:
> Hello,
>
> On Fri, Aug 26, 2022 at 09:15:01AM +0800, Lu Jialin wrote:
>> Cgroup is used to organize and manage resource available processes.
>> Currently there are no handy tool for gathering reousrce usage
>> information for each and every child cgroups, makes it hard to detect
>> resource outage and debug resource issues.
>>
>> To overcome this, we present the cgroup.top interface. Just like the
>> top command, user is able to easily gather resource usage information
>> , allowing user to detect and respond to resource outage in child
>> cgroups
> I don't think this is something we want build into the kernel. Maybe what
> you want is something similar to below?
>
>    https://github.com/facebookincubator/below
>
> Thanks.
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-08-26  9:51 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [RFC 2/2] memcg: Adapt cgroup.top into per-memcg Lu Jialin
2022-08-26  7:38   ` Michal Hocko
2022-08-26  3:17 ` [RFC 0/2] Introduce cgroup.top interface Tejun Heo
2022-08-26  9:50   ` lujialin (A)

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).