Skip to contentSkip to conversation

memcg: add support to generate the total count of children from root

PATCH In review Aaron Tomlin 2 participants 7 messages Nov 24, 2020 – Nov 26, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 memcg: add support to generate the total count of children from root

Each memory-controlled cgroup is assigned a unique ID and the total number of memory cgroups is limited to MEM_CGROUP_ID_MAX.

This patch provides the ability to determine the number of memory cgroups from the root memory cgroup, only.
A value of 1 (i.e. self count) is returned if there are no children. For example, the number of memory cgroups can be established by reading the /sys/fs/cgroup/memory/memory.total_cnt file.

---
 mm/memcontrol.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
mm/memcontrol.c +18 -0
index 29459a6ce1c7..a4f7cb40e233 100644
@@ -4535,6 +4535,19 @@ static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
 	return 0;
 }
 
+static int mem_cgroup_total_count_read(struct cgroup_subsys_state *css,
+				      struct cftype *cft)
+{
+	struct mem_cgroup *iter, *memcg = mem_cgroup_from_css(css);
+	int num = 0;
+
+	for_each_mem_cgroup_tree(iter, memcg)
+		num++;
+
+	/* Returns 1 (i.e. self count) if no children. */
+	return num;
+}
+
 #ifdef CONFIG_CGROUP_WRITEBACK
 
 #include <trace/events/writeback.h>
@@ -5050,6 +5063,11 @@ static struct cftype mem_cgroup_legacy_files[] = {
 		.write_u64 = mem_cgroup_oom_control_write,
 		.private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
 	},
+	{
+		.name = "total_cnt",
+		.flags = CFTYPE_ONLY_ON_ROOT,
+		.read_u64 = mem_cgroup_total_count_read,
+	},
 	{
 		.name = "pressure_level",
 	},
Signature · 1 line
2.26.2
#1 lore raw
Quoted 8 lines · Aaron Tomlin

Each memory-controlled cgroup is assigned a unique ID and the total number of memory cgroups is limited to MEM_CGROUP_ID_MAX.

This patch provides the ability to determine the number of memory cgroups from the root memory cgroup, only.
A value of 1 (i.e. self count) is returned if there are no children. For example, the number of memory cgroups can be established by reading the /sys/fs/cgroup/memory/memory.total_cnt file.

Could you add some explanation why is this information useful for userspace? Who is going to use it and why a simple scripting on top of cgroupfs is insufficient.

Quoted 44 lines
---
 mm/memcontrol.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
mm/memcontrol.c +18 -0
index 29459a6ce1c7..a4f7cb40e233 100644
@@ -4535,6 +4535,19 @@ static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
 	return 0;
 }
 
+static int mem_cgroup_total_count_read(struct cgroup_subsys_state *css,
+				      struct cftype *cft)
+{
+	struct mem_cgroup *iter, *memcg = mem_cgroup_from_css(css);
+	int num = 0;
+
+	for_each_mem_cgroup_tree(iter, memcg)
+		num++;
+
+	/* Returns 1 (i.e. self count) if no children. */
+	return num;
+}
+
 #ifdef CONFIG_CGROUP_WRITEBACK
 
 #include <trace/events/writeback.h>
@@ -5050,6 +5063,11 @@ static struct cftype mem_cgroup_legacy_files[] = {
 		.write_u64 = mem_cgroup_oom_control_write,
 		.private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
 	},
+	{
+		.name = "total_cnt",
+		.flags = CFTYPE_ONLY_ON_ROOT,
+		.read_u64 = mem_cgroup_total_count_read,
+	},
 	{
 		.name = "pressure_level",
 	},
Signature · 1 line
2.26.2
Signature · 2 lines
Michal Hocko
SUSE Labs
#2 lore raw
Quoted 10 lines · Michal Hocko
Quoted 8 lines · Aaron Tomlin

Each memory-controlled cgroup is assigned a unique ID and the total number of memory cgroups is limited to MEM_CGROUP_ID_MAX.

This patch provides the ability to determine the number of memory cgroups from the root memory cgroup, only.
A value of 1 (i.e. self count) is returned if there are no children. For example, the number of memory cgroups can be established by reading the /sys/fs/cgroup/memory/memory.total_cnt file.

Hi Michal,

Quoted 4 lines

Could you add some explanation why is this information useful for userspace? Who is going to use it and why a simple scripting on top of cgroupfs is insufficient.

Thank you for your feedback.

Indeed, one can use a command/script to manually calculate this. Having said that, one that creates a significant number of memory-controlled cgroups may prefer a quick, simple and reliable method to generate the aforementioned data, for management purposes only. As such, I thought this patch might be particularly useful.

Kind regards,

Signature · 1 line
Aaron Tomlin
#3 lore raw
Quoted 21 lines · Aaron Tomlin
Quoted 10 lines · Michal Hocko
Quoted 8 lines · Aaron Tomlin

Each memory-controlled cgroup is assigned a unique ID and the total number of memory cgroups is limited to MEM_CGROUP_ID_MAX.

This patch provides the ability to determine the number of memory cgroups from the root memory cgroup, only.
A value of 1 (i.e. self count) is returned if there are no children. For example, the number of memory cgroups can be established by reading the /sys/fs/cgroup/memory/memory.total_cnt file.

Hi Michal,

Quoted 4 lines

Could you add some explanation why is this information useful for userspace? Who is going to use it and why a simple scripting on top of cgroupfs is insufficient.

Quoted 7 lines

Thank you for your feedback.

Indeed, one can use a command/script to manually calculate this. Having said that, one that creates a significant number of memory-controlled cgroups may prefer a quick, simple and reliable method to generate the aforementioned data, for management purposes only. As such, I thought this patch might be particularly useful.

This like any other user visible interface would be a much easier sell if there was a clear usecase to justify it. I do not see anything controversial about exporting such a value but my general take is that we are only adding new interface when existing ones are insufficient. A performance might be a very good reason but that would really require to come with some real life numbers.

Thanks!

Signature · 2 lines
Michal Hocko
SUSE Labs
#4 lore raw

On Tue, 24 Nov 2020 at 13:36, Michal Hocko <[email protected]> wrote:

Michal,

Quoted 6 lines

This like any other user visible interface would be a much easier sell if there was a clear usecase to justify it. I do not see anything controversial about exporting such a value but my general take is that we are only adding new interface when existing ones are insufficient. A performance might be a very good reason but that would really require to come with some real life numbers.

Fair enough and understood.

At this stage, I unfortunately do not have such supporting data. This was only useful in an isolated situation. Having said this, I thought that the aforementioned interface would be helpful to others, in particular, given the known limitation.

Kind regards,

Signature · 1 line
Aaron Tomlin
#5 lore raw
Quoted 7 lines · Aaron Tomlin
Quoted 6 lines · Michal Hocko

This like any other user visible interface would be a much easier sell if there was a clear usecase to justify it. I do not see anything controversial about exporting such a value but my general take is that we are only adding new interface when existing ones are insufficient. A performance might be a very good reason but that would really require to come with some real life numbers.

Michal,

Quoted 6 lines

Fair enough and understood.

At this stage, I unfortunately do not have such supporting data. This was only useful in an isolated situation. Having said this, I thought that the aforementioned interface would be helpful to others, in particular, given the known limitation.

Furthermore, I can see that this is already provided via /proc/cgroups (see proc_cgroupstats_show()). As such, another possibility:
the proposed interface could reliably produce the difference between the maximum permitted memory-controlled cgroup count and the used count (i.e. the remaining memory cgroup count, from root); albeit, I doubt that this would be particularly useful to others i.e., the use-case would be rather limited.

Kind regards,

Signature · 1 line
Aaron Tomlin
#6 lore raw
In reply to Michal Hocko
Quoted 11 lines · Michal Hocko
Quoted 8 lines · Aaron Tomlin

Each memory-controlled cgroup is assigned a unique ID and the total number of memory cgroups is limited to MEM_CGROUP_ID_MAX.

This patch provides the ability to determine the number of memory cgroups from the root memory cgroup, only.
A value of 1 (i.e. self count) is returned if there are no children. For example, the number of memory cgroups can be established by reading the /sys/fs/cgroup/memory/memory.total_cnt file.

Hi Michal,

Quoted 3 lines

Could you add some explanation why is this information useful for userspace? Who is going to use it and why a simple scripting on top of cgroupfs is insufficient.

Thank you for your feedback.

Indeed, one can use a command/script to manually calculate this. Having said that, one that creates a significant number of memory-controlled cgroups may prefer a quick, simple and reliable method to generate the aforementioned data, for management purposes only. As such, I thought this patch might be particularly useful.

Kind regards,

Signature · 1 line
Aaron Tomlin