[LTP] [PATCH 07/13] testcases/kernel/mem: Move NUMA bits to numa_helper.h

Cyril Hrubis
2024-12-18T18:47:05Z

Testcases that use these macros and functions include numa_helper.h
anyways so there is no point in keeping numa related functions in mem.h.

Note that the unused clear_node() function was removed.

Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
testcases/kernel/include/numa_helper.h | 21 +++++++++
testcases/kernel/lib/numa_cpuset.c | 64 ++++++++++++++++++++++++++
testcases/kernel/mem/include/mem.h | 24 ----------
testcases/kernel/mem/ksm/ksm02.c | 2 +-
testcases/kernel/mem/ksm/ksm04.c | 2 +-
testcases/kernel/mem/lib/mem.c | 57 -----------------------
testcases/kernel/mem/oom/oom04.c | 2 +-
testcases/kernel/mem/oom/oom05.c | 2 +-
8 files changed, 89 insertions(+), 85 deletions(-)
create mode 100644 testcases/kernel/lib/numa_cpuset.c

diff --git a/testcases/kernel/include/numa_helper.h b/testcases/kernel/include/numa_helper.h
index ed45ec8a8..e67ddaaca 100644
--- a/testcases/kernel/include/numa_helper.h
+++ b/testcases/kernel/include/numa_helper.h
@@ -27,13 +27,34 @@
# include <numaif.h>
#endif

+struct tst_cg_group;
+
#define NH_MEMS (1 << 0)
#define NH_CPUS (1 << 1)

+#if defined(__powerpc__) || defined(__powerpc64__)
+# define MAXNODES 256
+#else
+# define MAXNODES 512
+#endif
+
+#define TESTMEM (1UL<<30)
+
+#define BITS_PER_LONG (8 * sizeof(long))
+
+#define PATH_SYS_SYSTEM "/sys/devices/system"
+
+static inline void set_node(unsigned long *array, unsigned int node)
+{
+ array[node / BITS_PER_LONG] |= 1UL << (node % BITS_PER_LONG);
+}
+
unsigned long get_max_node(void);
int get_allowed_nodes_arr(int flag, int *num_nodes, int **nodes);
int get_allowed_nodes(int flag, int count, ...);
void nh_dump_nodes(void);
int is_numa(void (*cleanup_fn)(void), int flag, int min_nodes);

+void write_node_cpusets(const struct tst_cg_group *cg, long nd);
+
#endif /* NUMA_HELPER_H */
diff --git a/testcases/kernel/lib/numa_cpuset.c b/testcases/kernel/lib/numa_cpuset.c
new file mode 100644
index 000000000..088d2d4e5
--- /dev/null
+++ b/testcases/kernel/lib/numa_cpuset.c
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) Linux Test Project, 2011-2021
+ * Copyright (c) Cyril Hrubis <chrubis@suse.cz> 2024
+ */
+
+#define TST_NO_DEFAULT_MAIN
+#include "tst_test.h"
+#include "tst_cgroup.h"
+#include "numa_helper.h"
+
+static void gather_node_cpus(char *cpus, long nd)
+{
+ int ncpus = 0;
+ int i;
+ long online;
+ char buf[BUFSIZ];
+ char path[BUFSIZ], path1[BUFSIZ];
+
+ while (tst_path_exists(PATH_SYS_SYSTEM "/cpu/cpu%d", ncpus))
+ ncpus++;
+
+ for (i = 0; i < ncpus; i++) {
+ snprintf(path, BUFSIZ,
+ PATH_SYS_SYSTEM "/node/node%ld/cpu%d", nd, i);
+ if (tst_path_exists("%s", path)) {
+ snprintf(path1, BUFSIZ, "%s/online", path);
+ /*
+ * if there is no online knob, then the cpu cannot
+ * be taken offline
+ */
+ if (tst_path_exists("%s", path1)) {
+ SAFE_FILE_SCANF(path1, "%ld", &online);
+ if (online == 0)
+ continue;
+ }
+ sprintf(buf, "%d,", i);
+ strcat(cpus, buf);
+ }
+ }
+ /* Remove the trailing comma. */
+ cpus[strlen(cpus) - 1] = '\0';
+}
+
+void write_node_cpusets(const struct tst_cg_group *cg, long nd)
+{
+ char cpus[BUFSIZ] = "";
+
+ SAFE_CG_PRINTF(cg, "cpuset.mems", "%ld", nd);
+
+ gather_node_cpus(cpus, nd);
+ /*
+ * If the 'nd' node doesn't contain any CPUs,
+ * the first ID of CPU '0' will be used as
+ * the value of cpuset.cpus.
+ */
+ if (strlen(cpus) != 0) {
+ SAFE_CG_PRINT(cg, "cpuset.cpus", cpus);
+ } else {
+ tst_res(TINFO, "No CPUs in the node%ld; "
+ "using only CPU0", nd);
+ SAFE_CG_PRINT(cg, "cpuset.cpus", "0");
+ }
+}
diff --git a/testcases/kernel/mem/include/mem.h b/testcases/kernel/mem/include/mem.h
index ba5a996a7..e0f443b4b 100644
--- a/testcases/kernel/mem/include/mem.h
+++ b/testcases/kernel/mem/include/mem.h
@@ -9,35 +9,14 @@
#include "ksm_helper.h"
#include "tst_memutils.h"

-#if defined(__powerpc__) || defined(__powerpc64__)
-#define MAXNODES 256
-#else
-#define MAXNODES 512
-#endif
#define MB (1UL<<20)
#define KB (1UL<<10)
-#define PATH_SYS_SYSTEM "/sys/devices/system"
#define PATH_SYSVM "/proc/sys/vm/"
#define PATH_MEMINFO "/proc/meminfo"
-#define BITS_PER_LONG (8 * sizeof(long))
-
-static inline void set_node(unsigned long *array, unsigned int node)
-{
- array[node / BITS_PER_LONG] |= 1UL << (node % BITS_PER_LONG);
-}
-
-static inline void clean_node(unsigned long *array)
-{
- unsigned int i;
-
- for (i = 0; i < MAXNODES / BITS_PER_LONG; i++)
- array[i] &= 0UL;
-}

/* OOM */

#define LENGTH (3UL<<30)
-#define TESTMEM (1UL<<30)
#define NORMAL 1
#define MLOCK 2
#define KSM 3
@@ -58,7 +37,4 @@ void ksm_group_check(int run, int pg_shared, int pg_sharing, int pg_volatile,

void write_memcg(void);

-/* cpuset/memcg - include/tst_cgroup.h */
-void write_cpusets(const struct tst_cg_group *cg, long nd);
-
#endif
diff --git a/testcases/kernel/mem/ksm/ksm02.c b/testcases/kernel/mem/ksm/ksm02.c
index 3707de95d..a388e82ab 100644
--- a/testcases/kernel/mem/ksm/ksm02.c
+++ b/testcases/kernel/mem/ksm/ksm02.c
@@ -76,7 +76,7 @@ static void verify_ksm(void)
}
create_same_memory(size, num, unit);

- write_cpusets(tst_cg, node);
+ write_node_cpusets(tst_cg, node);
SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
create_same_memory(size, num, unit);
SAFE_CG_PRINTF(tst_cg_drain, "cgroup.procs", "%d", getpid());
diff --git a/testcases/kernel/mem/ksm/ksm04.c b/testcases/kernel/mem/ksm/ksm04.c
index f558b23be..31f3ff2eb 100644
--- a/testcases/kernel/mem/ksm/ksm04.c
+++ b/testcases/kernel/mem/ksm/ksm04.c
@@ -63,7 +63,7 @@ static void verify_ksm(void)
}
create_same_memory(size, num, unit);

- write_cpusets(tst_cg, node);
+ write_node_cpusets(tst_cg, node);
create_same_memory(size, num, unit);
}

diff --git a/testcases/kernel/mem/lib/mem.c b/testcases/kernel/mem/lib/mem.c
index 02199349d..c7c05aa03 100644
--- a/testcases/kernel/mem/lib/mem.c
+++ b/testcases/kernel/mem/lib/mem.c
@@ -527,60 +527,3 @@ void create_same_memory(int size, int num, int unit)
tst_res(TFAIL, "child exit status is %d",
WEXITSTATUS(status));
}
-
-/* THP */
-
-/* cpuset/memcg */
-static void gather_node_cpus(char *cpus, long nd)
-{
- int ncpus = 0;
- int i;
- long online;
- char buf[BUFSIZ];
- char path[BUFSIZ], path1[BUFSIZ];
-
- while (tst_path_exists(PATH_SYS_SYSTEM "/cpu/cpu%d", ncpus))
- ncpus++;
-
- for (i = 0; i < ncpus; i++) {
- snprintf(path, BUFSIZ,
- PATH_SYS_SYSTEM "/node/node%ld/cpu%d", nd, i);
- if (tst_path_exists("%s", path)) {
- snprintf(path1, BUFSIZ, "%s/online", path);
- /*
- * if there is no online knob, then the cpu cannot
- * be taken offline
- */
- if (tst_path_exists("%s", path1)) {
- SAFE_FILE_SCANF(path1, "%ld", &online);
- if (online == 0)
- continue;
- }
- sprintf(buf, "%d,", i);
- strcat(cpus, buf);
- }
- }
- /* Remove the trailing comma. */
- cpus[strlen(cpus) - 1] = '\0';
-}
-
-void write_cpusets(const struct tst_cg_group *cg, long nd)
-{
- char cpus[BUFSIZ] = "";
-
- SAFE_CG_PRINTF(cg, "cpuset.mems", "%ld", nd);
-
- gather_node_cpus(cpus, nd);
- /*
- * If the 'nd' node doesn't contain any CPUs,
- * the first ID of CPU '0' will be used as
- * the value of cpuset.cpus.
- */
- if (strlen(cpus) != 0) {
- SAFE_CG_PRINT(cg, "cpuset.cpus", cpus);
- } else {
- tst_res(TINFO, "No CPUs in the node%ld; "
- "using only CPU0", nd);
- SAFE_CG_PRINT(cg, "cpuset.cpus", "0");
- }
-}
diff --git a/testcases/kernel/mem/oom/oom04.c b/testcases/kernel/mem/oom/oom04.c
index 3f0bcf7e2..47087658d 100644
--- a/testcases/kernel/mem/oom/oom04.c
+++ b/testcases/kernel/mem/oom/oom04.c
@@ -60,7 +60,7 @@ static void setup(void)
if (ret < 0)
tst_brk(TBROK, "Failed to get a memory node "
"using get_allowed_nodes()");
- write_cpusets(tst_cg, memnode);
+ write_node_cpusets(tst_cg, memnode);
SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
}

diff --git a/testcases/kernel/mem/oom/oom05.c b/testcases/kernel/mem/oom/oom05.c
index 74f0f78b3..f3851f4f2 100644
--- a/testcases/kernel/mem/oom/oom05.c
+++ b/testcases/kernel/mem/oom/oom05.c
@@ -80,7 +80,7 @@ void setup(void)
tst_brk(TBROK, "Failed to get a memory node "
"using get_allowed_nodes()");

- write_cpusets(tst_cg, memnode);
+ write_node_cpusets(tst_cg, memnode);
SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
}
--
2.45.2