Skip to contentSkip to conversation

memcontrol03, memcontrol04: Bind test process to a single CPU

PATCH In review Wei Gao via ltp 2 participants 6 messages Nov 13 Library code

lore
Share & tools
lore
#0 lore raw

v3 cover memcontrol03, memcontrol04: Bind test process to a single CPU

v2->v3:
- Move bind_to_current_cpu into include/tst_cpu.h
- Update perf_event_open02/getrusage03 use tst_bind_to_current_cpu

Wei Gao (4):
lib: Add tst_bind_to_current_cpu() helper
perf_event_open02: Use tst_bind_to_current_cpu() helper
getrusage03: Use tst_bind_to_current_cpu() helper memcontrol03, memcontrol04: Bind test process to a single CPU

 include/lapi/sched.h                          | 10 ++++++
 include/tst_cpu.h                             | 11 ++++++
 lib/tst_cpu.c                                 | 34 +++++++++++++++++++
 .../kernel/controllers/memcg/memcontrol03.c   |  2 ++
 .../kernel/controllers/memcg/memcontrol04.c   |  2 ++
 .../kernel/syscalls/getrusage/getrusage03.c   | 32 +----------------
 .../perf_event_open/perf_event_open02.c       | 22 +-----------
 7 files changed, 61 insertions(+), 52 deletions(-)
Signature · 5 lines
2.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH v3 1/4 lib: Add tst_bind_to_current_cpu() helper

Add tst_bind_to_cpu() and tst_bind_to_current_cpu() to the core LTP CPU library (include/tst_cpu.h, lib/tst_cpu.c). This allows tests to pin the test process to a specific or current CPU, which is useful for avoiding multi-CPU migration artifacts and per-CPU charge/counter batching variations.

Also add a fallback definition for sched_getcpu() in include/lapi/sched.h via getcpu() for older C libraries without sched_getcpu().

---
 include/lapi/sched.h | 10 ++++++++++
 include/tst_cpu.h    | 11 +++++++++++
 lib/tst_cpu.c        | 34 ++++++++++++++++++++++++++++++++++
 3 files changed, 55 insertions(+)
include/lapi/sched.h +10 -0
index 4fa6e7c5f..ea828664c 100644
@@ -115,6 +115,16 @@ static inline int getcpu(unsigned *cpu, unsigned *node)
 }
 #endif
 
+#ifndef HAVE_SCHED_GETCPU
+static inline int sched_getcpu(void)
+{
+	unsigned int cpu;
+	int ret = getcpu(&cpu, NULL);
+
+	return ret == 0 ? (int)cpu : ret;
+}
+#endif
+
 #ifndef SCHED_DEADLINE
 # define SCHED_DEADLINE	6
 #endif
include/tst_cpu.h +11 -0
index 340d0f3c7..7c6089379 100644
@@ -36,4 +36,15 @@ const char *tst_cpu_vendor(void);
 
 int tst_is_virt(int virt_type);
 
+/**
+ * tst_bind_to_cpu() - Pin calling process to the specified CPU.
+ * @cpu: CPU index to bind to.
+ */
+void tst_bind_to_cpu(int cpu);
+
+/**
+ * tst_bind_to_current_cpu() - Pin calling process to the current CPU.
+ */
+void tst_bind_to_current_cpu(void);
+
 #endif	/* TST_CPU_H__ */
lib/tst_cpu.c +34 -0
index 241bd3347..2f0e2f8ee 100644
@@ -22,6 +22,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include "test.h"
+#include "lapi/sched.h"
 #include "tso_safe_macros.h"
 #include "tso_safe_file_ops.h"
 #include "tso_safe_stdio.h"
@@ -139,3 +140,36 @@ const char *tst_cpu_vendor(void)
 
 	return cpu_vendor;
 }
+
+void tst_bind_to_cpu(int cpu)
+{
+	size_t mask_size;
+	cpu_set_t *mask;
+
+	tst_resm(TINFO, "Binding test process to CPU %d", cpu);
+
+	mask = CPU_ALLOC(cpu + 1);
+	if (!mask)
+		tst_brkm(TBROK | TERRNO, NULL, "CPU_ALLOC() failed");
+
+	mask_size = CPU_ALLOC_SIZE(cpu + 1);
+	CPU_ZERO_S(mask_size, mask);
+	CPU_SET_S(cpu, mask_size, mask);
+
+	if (sched_setaffinity(0, mask_size, mask) < 0) {
+		CPU_FREE(mask);
+		tst_brkm(TBROK | TERRNO, NULL, "sched_setaffinity() failed");
+	}
+
+	CPU_FREE(mask);
+}
+
+void tst_bind_to_current_cpu(void)
+{
+	int cpu = sched_getcpu();
+
+	if (cpu < 0)
+		tst_brkm(TBROK | TERRNO, NULL, "sched_getcpu() failed");
+
+	tst_bind_to_cpu(cpu);
+}
Signature · 5 lines
2.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Re: [LTP] lib: Add tst_bind_to_current_cpu() helper

Hi Wei,

Quoted 1 line · Wei Gao

lib: Add tst_bind_to_current_cpu() helper

Verdict - Reviewed

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

PATCH v3 2/4 perf_event_open02: Use tst_bind_to_current_cpu() helper

In reply to Wei Gao via ltp

Replace the local duplicate bind_to_current_cpu() implementation with the tst_bind_to_current_cpu() library helper.

---
 .../perf_event_open/perf_event_open02.c       | 22 +------------------
 1 file changed, 1 insertion(+), 21 deletions(-)
testcases/kernel/syscalls/perf_event_open/perf_event_open02.c +1 -21
index e38c3f133..1e100982e 100644
@@ -174,26 +174,6 @@ static int count_hardware_counters(void)
 }
 #endif /* __s390__ */
 
-static void bind_to_current_cpu(void)
-{
-#ifdef HAVE_SCHED_GETCPU
-	int cpu = sched_getcpu();
-	size_t mask_size;
-	cpu_set_t *mask;
-
-	if (cpu == -1)
-		tst_brk(TBROK | TERRNO, "sched_getcpu() failed");
-
-	mask = CPU_ALLOC(cpu + 1);
-	mask_size = CPU_ALLOC_SIZE(cpu + 1);
-	CPU_ZERO_S(mask_size, mask);
-	CPU_SET(cpu, mask);
-	if (sched_setaffinity(0, mask_size, mask) == -1)
-		tst_brk(TBROK | TERRNO, "sched_setaffinity() failed");
-	CPU_FREE(mask);
-#endif
-}
-
 static void setup(void)
 {
 	int i;
@@ -214,7 +194,7 @@ static void setup(void)
 	if (access(PATH_KERN_PERF_EVENT_PARANOID, F_OK) == -1)
 		tst_brk(TCONF, "Kernel doesn't have perf_event support");
 
-	bind_to_current_cpu();
+	tst_bind_to_current_cpu();
 #ifdef __s390__
 	/*
 	 * On s390 the "time_enabled" and "time_running" values are always the
Signature · 5 lines
2.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

PATCH v3 3/4 getrusage03: Use tst_bind_to_current_cpu() helper

In reply to Wei Gao via ltp

Replace the local duplicate pin_to_current_cpu() implementation with the tst_bind_to_current_cpu() library helper.

---
 .../kernel/syscalls/getrusage/getrusage03.c   | 32 +------------------
 1 file changed, 1 insertion(+), 31 deletions(-)
testcases/kernel/syscalls/getrusage/getrusage03.c +1 -31
index d4dca8070..090c46dfc 100644
@@ -18,8 +18,6 @@
 #include <stdlib.h>
 
 #include "tst_test.h"
-#include "lapi/cpuset.h"
-#include "lapi/sched.h"
 #include "getrusage03.h"
 
 #define TESTBIN "getrusage03_child"
@@ -33,34 +31,6 @@ static const char *const resource[] = {
 	NULL,
 };
 
-/*
- * Pin the test to the CPU it runs on. Every forked or exec'd child inherits
- * the affinity, so only that CPU can hold RSS counter updates outside the
- * fast percpu_counter read.
- */
-static void pin_to_current_cpu(void)
-{
-	unsigned int cpu;
-	cpu_set_t *set;
-	size_t size;
-
-	if (getcpu(&cpu, NULL))
-		tst_brk(TBROK | TERRNO, "getcpu() failed");
-
-	set = CPU_ALLOC(cpu + 1);
-	if (!set)
-		tst_brk(TBROK | TERRNO, "CPU_ALLOC() failed");
-
-	size = CPU_ALLOC_SIZE(cpu + 1);
-	CPU_ZERO_S(size, set);
-	CPU_SET_S(cpu, size, set);
-	if (sched_setaffinity(0, size, set))
-		tst_brk(TBROK | TERRNO, "sched_setaffinity() failed");
-
-	CPU_FREE(set);
-	tst_res(TINFO, "Pinned to CPU %u", cpu);
-}
-
 /*
  * The pinned CPU can still hold up to one percpu_counter batch,
  * max(32, 2 * online CPUs) pages, outside the fast RSS read.
@@ -80,7 +50,7 @@ static void setup(void)
 	tst_res(TINFO, "%li online CPUs, lower RSS allowance %li KiB",
 		online_cpus, lower_allowance);
 
-	pin_to_current_cpu();
+	tst_bind_to_current_cpu();
 }
 
 static void check_maxrss(long actual, long expected, const char *name,
Signature · 5 lines
2.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

PATCH v3 4/4 memcontrol03, memcontrol04: Bind test process to a single CPU

In reply to Wei Gao via ltp

On multi-CPU systems, memcontrol03 and memcontrol04 can intermittently fail with over-reclaim of protected pagecache under trunk_B:

memcontrol03.c:104: TINFO: Child 142113 in trunk_G: Allocating anon: 150994944 memcontrol03.c:121: TPASS: Child 142113 exited memcontrol03.c:214: TPASS: Expect: (A/B memory.current=54394880) ~= 52428800
...
memcontrol03.c:104: TINFO: Child 142114 in trunk_G: Allocating anon: 174063616 memcontrol03.c:116: TPASS: Child 142114 killed by OOM memcontrol03.c:230: TFAIL: Expect: (A/B memory.current=43319296) ~= 52428800

In the kernel, per-CPU charge batching (MEMCG_CHARGE_BATCH = 64 pages) caches charges in per-CPU memcg_stock. On multi-CPU systems (especially with large page sizes like 64KB on ppc64le where each CPU caches up to 4MB), allocating memory across CPUs allows cached stock charges to accumulate in ancestor trunk_A. Because trunk_A has memory.max = 200M, this extra stock pressure forces the reclaimer to evict pagecache in trunk_B down to ~35-43MB instead of the expected ~50MB.

Set .setup = tst_bind_to_current_cpu in both tests. Since child processes inherit CPU affinity on fork(), this confines stock batching to a single CPU across all test children, preventing multi-CPU stock accumulation and ensuring reproducible reclaim behavior.

---
 testcases/kernel/controllers/memcg/memcontrol03.c | 2 ++
 testcases/kernel/controllers/memcg/memcontrol04.c | 2 ++
 2 files changed, 4 insertions(+)
testcases/kernel/controllers/memcg/memcontrol03.c +2 -0
index 25b84c24e..73d1e7e04 100644
@@ -243,6 +243,8 @@ static void cleanup(void)
 }
 
 static struct tst_test test = {
+	/* reduce per CPU stocks for more stable results */
+	.setup = tst_bind_to_current_cpu,
 	.cleanup = cleanup,
 	.test_all = test_memcg_min,
 	.mount_device = 1,
testcases/kernel/controllers/memcg/memcontrol04.c +2 -0
index f1e136df4..462782d6d 100644
@@ -242,6 +242,8 @@ static void cleanup(void)
 }
 
 static struct tst_test test = {
+	/* reduce per CPU stocks for more stable results */
+	.setup = tst_bind_to_current_cpu,
 	.cleanup = cleanup,
 	.test_all = test_memcg_low,
 	.mount_device = 1,
Signature · 5 lines
2.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp