Skip to contentSkip to conversation

samples: introduce cgroup events listeners

PATCH Applied Dmitry Rokosov 2 participants 7 messages Nov 23, 2023 – Nov 25, 2023

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

v3 cover samples: introduce cgroup events listeners

To begin with, this patch series relocates the cgroup example code to the samples/cgroup directory, which is the appropriate location for such code snippets.

Furthermore, a new memcg events listener is introduced. This listener is a simple yet effective tool for monitoring memory events and managing counter changes during runtime.

Additionally, as per Andrew Morton's suggestion, a helpful reminder comment is included in the memcontrol implementation. This comment serves to ensure that the samples code is updated whenever new events are added.

Changes v3 since v2 at [2]:
- rename cgroup_v2_event_listener to memcg_event_listener per Andrew's suggestion

Changes v2 since v1 at [1]:
- create new samples subdir - cgroup
- move cgroup_event_listener for cgroup v1 to samples/cgroup
- add a reminder comment to memcontrol implementation

Links:

    [1] - https://lore.kernel.org/all/[email protected]/
    [2] - https://lore.kernel.org/all/[email protected]/

Dmitry Rokosov (3):
samples: introduce new samples subdir for cgroup samples/cgroup: introduce memcg memory.events listener
mm: memcg: add reminder comment for the memcg v2 events

 MAINTAINERS                                   |   1 +
 mm/memcontrol.c                               |   4 +
 samples/Kconfig                               |   6 +
 samples/Makefile                              |   1 +
 samples/cgroup/Makefile                       |   5 +
 .../cgroup/cgroup_event_listener.c            |   0
 samples/cgroup/memcg_event_listener.c         | 330 ++++++++++++++++++
 tools/cgroup/Makefile                         |  11 -
 8 files changed, 347 insertions(+), 11 deletions(-)
 create mode 100644 samples/cgroup/Makefile
 rename {tools => samples}/cgroup/cgroup_event_listener.c (100%)
 create mode 100644 samples/cgroup/memcg_event_listener.c
 delete mode 100644 tools/cgroup/Makefile
Signature · 1 line
2.36.0
#1 lore raw

PATCH v3 1/3 samples: introduce new samples subdir for cgroup

Move the cgroup_event_listener for cgroup v1 to the samples directory. This suggestion was proposed by Andrew Morton during the discussion [1].

Links:

    [1] - https://lore.kernel.org/all/[email protected]/
---
 MAINTAINERS                                       |  1 +
 samples/Kconfig                                   |  6 ++++++
 samples/Makefile                                  |  1 +
 samples/cgroup/Makefile                           |  5 +++++
 {tools => samples}/cgroup/cgroup_event_listener.c |  0
 tools/cgroup/Makefile                             | 11 -----------
 6 files changed, 13 insertions(+), 11 deletions(-)
 create mode 100644 samples/cgroup/Makefile
 rename {tools => samples}/cgroup/cgroup_event_listener.c (100%)
 delete mode 100644 tools/cgroup/Makefile
MAINTAINERS +1 -0
index d516295978a4..6a0a580c34dc 100644
@@ -5243,6 +5243,7 @@ L:	[email protected]
 S:	Maintained
 F:	mm/memcontrol.c
 F:	mm/swap_cgroup.c
+F:	samples/cgroup/*
 F:	tools/testing/selftests/cgroup/memcg_protection.m
 F:	tools/testing/selftests/cgroup/test_kmem.c
 F:	tools/testing/selftests/cgroup/test_memcontrol.c
samples/Kconfig +6 -0
index bf49ed0d7362..339c8e2ee749 100644
@@ -287,6 +287,12 @@ config SAMPLE_KMEMLEAK
           Build a sample program which have explicitly leaks memory to test
           kmemleak
 
+config SAMPLE_CGROUP
+	bool "Build cgroup sample code"
+	depends on CGROUPS && CC_CAN_LINK && HEADERS_INSTALL
+	help
+	  Build samples that demonstrate the usage of the cgroup API.
+
 source "samples/rust/Kconfig"
 
 endif # SAMPLES
samples/Makefile +1 -0
index 0a551c2b33f4..b85fa64390c5 100644
@@ -3,6 +3,7 @@
 
 subdir-$(CONFIG_SAMPLE_AUXDISPLAY)	+= auxdisplay
 subdir-$(CONFIG_SAMPLE_ANDROID_BINDERFS) += binderfs
+subdir-$(CONFIG_SAMPLE_CGROUP) += cgroup
 obj-$(CONFIG_SAMPLE_CONFIGFS)		+= configfs/
 obj-$(CONFIG_SAMPLE_CONNECTOR)		+= connector/
 obj-$(CONFIG_SAMPLE_FANOTIFY_ERROR)	+= fanotify/
samples/cgroup/Makefile new file +5 -0
new file mode 100644
index 000000000000..deef4530f5e7
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0
+
+userprogs-always-y += cgroup_event_listener
+
+userccflags += -I usr/include
samples/cgroup/cgroup_event_listener.c renamed from tools/cgroup/cgroup_event_listener.c +0 -0
similarity index 100%
rename from tools/cgroup/cgroup_event_listener.c
rename to samples/cgroup/cgroup_event_listener.c
tools/cgroup/Makefile deleted +0 -11
deleted file mode 100644
index ffca068e4a76..000000000000
@@ -1,11 +0,0 @@
-# SPDX-License-Identifier: GPL-2.0
-# Makefile for cgroup tools
-
-CFLAGS = -Wall -Wextra
-
-all: cgroup_event_listener
-%: %.c
-	$(CC) $(CFLAGS) -o $@ $^
-
-clean:
-	$(RM) cgroup_event_listener
Signature · 1 line
2.36.0
#2 lore raw

PATCH v3 2/3 samples/cgroup: introduce memcg memory.events listener

In reply to Dmitry Rokosov

This is a simple listener for memory events that handles counter changes in runtime. It can be set up for a specific memory cgroup v2.

The output example:
=====

$ /tmp/memcg_event_listener test
Initialized MEMCG events with counters:
MEMCG events:
	low: 0
	high: 0
	max: 0
	oom: 0
	oom_kill: 0
	oom_group_kill: 0
Started monitoring memory events from '/sys/fs/cgroup/test/memory.events'...
Received event in /sys/fs/cgroup/test/memory.events:
*** 1 MEMCG oom_kill event, change counter 0 => 1
Received event in /sys/fs/cgroup/test/memory.events:
*** 1 MEMCG oom_kill event, change counter 1 => 2
Received event in /sys/fs/cgroup/test/memory.events:
*** 1 MEMCG oom_kill event, change counter 2 => 3
Received event in /sys/fs/cgroup/test/memory.events:
*** 1 MEMCG oom_kill event, change counter 3 => 4
Received event in /sys/fs/cgroup/test/memory.events:
*** 2 MEMCG max events, change counter 0 => 2
Received event in /sys/fs/cgroup/test/memory.events:
*** 8 MEMCG max events, change counter 2 => 10
*** 1 MEMCG oom event, change counter 0 => 1
Received event in /sys/fs/cgroup/test/memory.events:
*** 1 MEMCG oom_kill event, change counter 4 => 5
^CExiting memcg event listener...
=====
---
 samples/cgroup/Makefile               |   2 +-
 samples/cgroup/memcg_event_listener.c | 330 ++++++++++++++++++++++++++
 2 files changed, 331 insertions(+), 1 deletion(-)
 create mode 100644 samples/cgroup/memcg_event_listener.c
samples/cgroup/Makefile +1 -1
index deef4530f5e7..526c8569707c 100644
@@ -1,5 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
 
-userprogs-always-y += cgroup_event_listener
+userprogs-always-y += cgroup_event_listener memcg_event_listener
 
 userccflags += -I usr/include
samples/cgroup/memcg_event_listener.c new file +330 -0

330 lines not on this page · raw message

Signature · 1 line
2.36.0
#3 lore raw

PATCH v3 3/3 mm: memcg: add reminder comment for the memcg v2 events

In reply to Dmitry Rokosov

To maintain the correct state, it is important to ensure that events for the memory cgroup v2 are aligned with the sample cgroup codes.

---
 mm/memcontrol.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/memcontrol.c +4 -0
index e8ca4bdcb03c..a75c4584f58f 100644
@@ -6555,6 +6555,10 @@ static ssize_t memory_max_write(struct kernfs_open_file *of,
 	return nbytes;
 }
 
+/*
+ * Note: don't forget to update the 'samples/cgroup/memcg_event_listener'
+ * if any new events become available.
+ */
 static void __memory_events_show(struct seq_file *m, atomic_long_t *events)
 {
 	seq_printf(m, "low %lu\n", atomic_long_read(&events[MEMCG_LOW]));
Signature · 1 line
2.36.0
maintainer
#4 lore raw
In reply to Dmitry Rokosov
Quoted 3 lines · On Thu, 23 Nov 2023 10:19:42 +0300 Dmitry Rokosov…

To begin with, this patch series relocates the cgroup example code to the samples/cgroup directory, which is the appropriate location for such code snippets.

butbut. Didn't we decide to do s/cgroup/memcg/ throughout?

#5 lore raw
Quoted 7 lines · Andrew Morton
Quoted 3 lines · On Thu, 23 Nov 2023 10:19:42 +0300 Dmitry Rokosov…

To begin with, this patch series relocates the cgroup example code to the samples/cgroup directory, which is the appropriate location for such code snippets.

butbut. Didn't we decide to do s/cgroup/memcg/ throughout?

I believe the samples directory should be named "samples/cgroup" instead of "memcg" because the cgroup v1 event listener cannot be renamed to "memcg" due to the common naming of cgroup v1 event_control (this sample uses that control to access eventfd).

Additionally, I think it would be a good idea to add the new samples for cgroup helpers in that directory.

That's why I have only renamed the new memcg listener.

Signature · 2 lines
Thank you,
Dmitry
#6 lore raw

Andrew,

Quoted 14 lines · Dmitry Rokosov
Quoted 7 lines · Andrew Morton
Quoted 3 lines · On Thu, 23 Nov 2023 10:19:42 +0300 Dmitry Rokosov…

To begin with, this patch series relocates the cgroup example code to the samples/cgroup directory, which is the appropriate location for such code snippets.

butbut. Didn't we decide to do s/cgroup/memcg/ throughout?

I believe the samples directory should be named "samples/cgroup" instead of "memcg" because the cgroup v1 event listener cannot be renamed to "memcg" due to the common naming of cgroup v1 event_control (this sample uses that control to access eventfd).

Quoted 4 lines

Additionally, I think it would be a good idea to add the new samples for cgroup helpers in that directory.

That's why I have only renamed the new memcg listener.

I looked into this more deeply. And yes, the old cgroup.event_control has the common name, but it's used in the memcg implementation only.

So, if we plan to introduce new samples for cgroup, I suggest we use the following naming convention:

1) Directory: samples/cgroup
2) V1 sample: memcg_v1_event_listener
3) V2 sample: memcg_v2_event_listener

Please let me know what you think about this. If it's okay with you, I will prepare the v4 version with the above changes. I would appreciate any feedback on that!"

Signature · 2 lines
Thank you,
Dmitry