Skip to contentSkip to conversation

cpuset_memory_spread: count only the test file's page cache

PATCH Superseded Changwei Zou via ltp 2 participants 2 messages Sep 4

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 cpuset_memory_spread: count only the test file's page cache

The cpuset_memory_spread test, written in 2009, checks the cpuset.memory_spread_page policy by having cpuset_mem_hog read a 100 MB DATAFILE and then comparing the global per-node FilePages counters in /sys/devices/system/node/nodeX/meminfo before and after.

Those counters also account for unrelated page-cache activity elsewhere on the system, so on large or busy NUMA machines the empirical thresholds (upperlimit/lowerlimit) become unreliable and the test fails spuriously, e.g.:

cpuset_memory_spread 5 TFAIL: hog the memory on the unexpected node(FilePages_For_Nodes(KB): _0: 7592 _1: 108328, Expect Nodes: 1).

Here 108328 KB even exceeds the 100 MB file size, showing the counter includes unrelated cache.

Instead of measuring the noisy global counters, account for only DATAFILE's own page-cache pages: after reading the file, cpuset_mem_hog mmaps it, faults every page in and uses move_pages(2) (with a NULL node array, so nothing is migrated) to learn the NUMA node each of the file's pages resides on. It writes the per-node totals (in KB) to a result file that the shell reads.

result_check() then simply verifies that the file's cache landed on the expected node(s) and that the other nodes hold at most a small fraction (UNEXPECTED_TOLERANCE, 5%) of it. Because only this file's pages are counted, unrelated page-cache activity can no longer perturb the result, and the check is page-size independent.

On non-NUMA machines the test is still skipped as before.

---
 .../cpuset_mem_hog.c                          | 130 +++++++++++++++++-
 .../cpuset_memory_spread_testset.sh           | 106 +++++++-------
 2 files changed, 175 insertions(+), 61 deletions(-)
testcases/kernel/controllers/cpuset/cpuset_memory_spread_test/cpuset_mem_hog.c +129 -1
index 56e039eee..c04aabd48 100644
@@ -27,17 +27,143 @@
 #include <ctype.h>
 #include <getopt.h>
 #include <err.h>
+#include <errno.h>
 #include <signal.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/syscall.h>
 #include <fcntl.h>
 
 #include "../cpuset_lib/common.h"
+#include "lapi/syscalls.h"
 
 #define BUFFER_SIZE 100
 
+/* The file whose page cache placement we test and where we report it. */
+#define DATAFILE	"DATAFILE"
+#define RESULTFILE	"cpuset_mem_hog_nodes"
+
+/* Query the residing NUMA node of at most this many pages per syscall. */
+#define MOVE_PAGES_CHUNK	1024
+
+/* Upper bound on the number of NUMA nodes we account for. */
+#define MAX_NODES		1024
+
 volatile int end;
 
+/*
+ * move_pages(2) with a NULL node array does not move anything; it only
+ * reports, in status[], the NUMA node each already-present page resides on.
+ */
+static long query_pages_node(unsigned long count, void **pages, int *status)
+{
+	return syscall(__NR_move_pages, 0, count, pages, NULL, status, 0);
+}
+
+/*
+ * Count only DATAFILE's own page-cache pages per NUMA node and write the
+ * result (node id and size in KB) to RESULTFILE.  Because we look exclusively
+ * at this file's pages -- rather than the global per-node FilePages counters
+ * in /sys -- unrelated page-cache activity on the system cannot perturb the
+ * measurement.
+ *
+ * return 0 on success, -1 on failure.
+ */
+static int count_file_pages(void)
+{
+	int fd;
+	struct stat st;
+	char *addr = MAP_FAILED;
+	long page_size = sysconf(_SC_PAGESIZE);
+	unsigned long npages, i, off;
+	void **pages = NULL;
+	int *status = NULL;
+	unsigned long *counts = NULL;
+	FILE *fp;
+	int ret = -1;
+
+	fd = open(DATAFILE, O_RDONLY);
+	if (fd == -1) {
+		warn("open %s failed", DATAFILE);
+		return -1;
+	}
+	if (fstat(fd, &st) == -1) {
+		warn("fstat %s failed", DATAFILE);
+		close(fd);
+		return -1;
+	}
+	npages = (st.st_size + page_size - 1) / page_size;
+	if (npages == 0) {
+		close(fd);
+		return -1;
+	}
+
+	addr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
+	close(fd);
+	if (addr == MAP_FAILED) {
+		warn("mmap %s failed", DATAFILE);
+		return -1;
+	}
+
+	pages = calloc(npages, sizeof(*pages));
+	status = calloc(npages, sizeof(*status));
+	counts = calloc(MAX_NODES, sizeof(*counts));
+	if (!pages || !status || !counts) {
+		warn("calloc failed");
+		goto out;
+	}
+
+	/*
+	 * Fault in every page so it maps the page-cache page already
+	 * populated by page_cache_hog(); query_pages_node() then reports
+	 * the node that cache page resides on.
+	 */
+	for (i = 0; i < npages; i++) {
+		volatile char c = addr[i * page_size];
+
+		(void)c;
+		pages[i] = addr + i * page_size;
+		status[i] = -1;
+	}
+
+	for (off = 0; off < npages; off += MOVE_PAGES_CHUNK) {
+		unsigned long n = npages - off;
+
+		if (n > MOVE_PAGES_CHUNK)
+			n = MOVE_PAGES_CHUNK;
+		if (query_pages_node(n, pages + off, status + off) == -1) {
+			warn("move_pages failed");
+			goto out;
+		}
+	}
+
+	for (i = 0; i < npages; i++) {
+		if (status[i] >= 0 && status[i] < MAX_NODES)
+			counts[status[i]]++;
+	}
+
+	fp = fopen(RESULTFILE, "w");
+	if (!fp) {
+		warn("open %s failed", RESULTFILE);
+		goto out;
+	}
+	for (i = 0; i < MAX_NODES; i++) {
+		if (counts[i])
+			fprintf(fp, "%lu %lu\n", i,
+				counts[i] * (unsigned long)page_size / 1024);
+	}
+	fclose(fp);
+	ret = 0;
+out:
+	if (addr != MAP_FAILED)
+		munmap(addr, st.st_size);
+	free(pages);
+	free(status);
+	free(counts);
+	return ret;
+}
+
 void sighandler1(UNUSED int signo)
 {
 }
@@ -54,7 +180,7 @@ int page_cache_hog(void)
 	char path[BUFFER_SIZE];
 	int ret = 0;
 
-	sprintf(path, "%s", "DATAFILE");
+	sprintf(path, "%s", DATAFILE);
 	fd = open(path, O_RDONLY);
 	if (fd == -1) {
 		warn("open %s failed", path);
@@ -81,6 +207,8 @@ int mem_hog(void)
 
 	while (!end) {
 		ret = page_cache_hog();
+		if (ret == 0)
+			ret = count_file_pages();
 
 		fd = open("./myfifo", O_WRONLY);
 		if (fd == -1)
testcases/kernel/controllers/cpuset/cpuset_memory_spread_test/cpuset_memory_spread_testset.sh +46 -60

165 lines not on this page · raw message

Signature · 5 lines
2.43.0


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

Hi Changwei,

testcases/kernel/controllers/cpuset/cpuset_memory_spread_test/cpuset_mem_hog.c line 138

Quoted 6 lines · Changwei Zou

cpuset_memory_spread: count only the test file's page cache

testcases/kernel/controllers/cpuset/cpuset_memory_spread_test/cpuset_mem_hog.c +4 -0
+		if (query_pages_node(n, pages + off, status + off) == -1) {
+			warn("move_pages failed");
+			goto out;
+		}

Medium: In Linux 7.3, move_pages() is built only when CONFIG_NUMA_MIGRATION=y. With it disabled this returns ENOSYS, the helper writes "0" to the FIFO, and every case reports TFAIL; require the config and report missing syscall support as TCONF.

testcases/kernel/controllers/cpuset/cpuset_memory_spread_test/cpuset_memory_spread_testset.sh freemem_check() · line 182

Quoted 4 lines
testcases/kernel/controllers/cpuset/cpuset_memory_spread_test/cpuset_memory_spread_testset.sh +4 -0
+	# the unexpected nodes must hold only a negligible fraction
+	if [ $((unexpected_sum * 100)) -ge $((total * UNEXPECTED_TOLERANCE)) ]; then
+		return 1
+	fi

Low: This rejects an unexpected share of exactly 5%, although the commit message and comments say that "at most" 5% is allowed. Use -gt, or document 5% as an exclusive limit.

Verdict - Needs revision

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