[LTP] [PATCH v4 06/31] testcases: sysfs: Add sys_cpu_topology01

Cyril Hrubis chrubis@suse.cz
Thu Aug 27 13:21:32 CEST 2026


A test for /sys/devices/system/cpu/* files.

Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
 include/tst_path_defs.h                       |   1 +
 runtest/sysfs                                 |   1 +
 .../sysfs/devices/system/cpu/.gitignore       |   1 +
 .../kernel/sysfs/devices/system/cpu/Makefile  |   7 ++
 .../devices/system/cpu/sys_cpu_topology01.c   | 102 ++++++++++++++++++
 5 files changed, 112 insertions(+)
 create mode 100644 testcases/kernel/sysfs/devices/system/cpu/.gitignore
 create mode 100644 testcases/kernel/sysfs/devices/system/cpu/Makefile
 create mode 100644 testcases/kernel/sysfs/devices/system/cpu/sys_cpu_topology01.c

diff --git a/include/tst_path_defs.h b/include/tst_path_defs.h
index c96a1454c..d915ecdb0 100644
--- a/include/tst_path_defs.h
+++ b/include/tst_path_defs.h
@@ -94,6 +94,7 @@
 
 /* SYSFS DEVICES */
 #define PATH_SYS_CLOCKSOURCE			"/sys/devices/system/clocksource"
+#define PATH_SYS_CPU				"/sys/devices/system/cpu"
 #define PATH_SYS_NODE				"/sys/devices/system/node"
 
 /* SYSFS */
diff --git a/runtest/sysfs b/runtest/sysfs
index 347bd78c1..4fc8f1eb2 100644
--- a/runtest/sysfs
+++ b/runtest/sysfs
@@ -2,3 +2,4 @@ sys_power01 sys_power01
 sys_kernel01 sys_kernel01
 sys_clocksource01 sys_clocksource01
 sys_node01 sys_node01
+sys_cpu_topology01 sys_cpu_topology01
diff --git a/testcases/kernel/sysfs/devices/system/cpu/.gitignore b/testcases/kernel/sysfs/devices/system/cpu/.gitignore
new file mode 100644
index 000000000..0615aff77
--- /dev/null
+++ b/testcases/kernel/sysfs/devices/system/cpu/.gitignore
@@ -0,0 +1 @@
+/sys_cpu_topology01
diff --git a/testcases/kernel/sysfs/devices/system/cpu/Makefile b/testcases/kernel/sysfs/devices/system/cpu/Makefile
new file mode 100644
index 000000000..781865569
--- /dev/null
+++ b/testcases/kernel/sysfs/devices/system/cpu/Makefile
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) 2026 Cyril Hrubis <chrubis@suse.cz>
+
+top_srcdir		?= ../../../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/sysfs/devices/system/cpu/sys_cpu_topology01.c b/testcases/kernel/sysfs/devices/system/cpu/sys_cpu_topology01.c
new file mode 100644
index 000000000..b181f2603
--- /dev/null
+++ b/testcases/kernel/sysfs/devices/system/cpu/sys_cpu_topology01.c
@@ -0,0 +1,102 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2026 Cyril Hrubis <chrubis@suse.cz>
+ */
+
+/*\
+ * Sanity checks for the CPU topology attributes exported under
+ * /sys/devices/system/cpu/.
+ *
+ * The kernel exports several cpulist files describing the state of the CPUs:
+ *
+ * - possible - CPUs that could possibly be brought online during this boot
+ * - present  - CPUs that are populated (physically present)
+ * - online   - CPUs that are currently online and usable
+ * - offline  - CPUs that are known to the system but currently offline
+ *
+ * These sets are nested, so the test verifies that:
+ *
+ * - online is a subset of present
+ * - present is a subset of possible
+ * - offline is a subset of possible
+ * - kernel_max is greater than or equal to the highest possible CPU id
+ *
+ * The number of online CPUs is also cross-checked against /proc/stat, which
+ * contains one ``cpuN`` line for each online CPU.
+ *
+ * All checks skip gracefully with TCONF when a particular attribute is not
+ * present.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+#include <limits.h>
+#include "tst_test.h"
+#include "tst_safe_stdio.h"
+#include "tst_sysfs_assert.h"
+#include "tst_path_defs.h"
+
+static int count_proc_stat_cpus(void)
+{
+	FILE *f;
+	char line[4096];
+	int cpus = 0;
+
+	f = SAFE_FOPEN("/proc/stat", "r");
+
+	while (fgets(line, sizeof(line), f)) {
+		if (!strncmp(line, "cpu", 3) && isdigit(line[3]))
+			cpus++;
+	}
+
+	SAFE_FCLOSE(f);
+
+	return cpus;
+}
+
+static void check_online_proc_stat(void)
+{
+	int count, max_id;
+	int stat_cpus;
+
+	if (TST_SYSFS_ASSERT_PARSE_LIST(&count, &max_id, PATH_SYS_CPU "/online"))
+		return;
+
+	stat_cpus = count_proc_stat_cpus();
+
+	if (count == stat_cpus) {
+		tst_res(TPASS,
+			PATH_SYS_CPU "/online count (%d) == /proc/stat cpu lines (%d)",
+			count, stat_cpus);
+	} else {
+		tst_res(TFAIL,
+			PATH_SYS_CPU "/online count (%d) != /proc/stat cpu lines (%d)",
+			count, stat_cpus);
+	}
+}
+
+static void check_kernel_max(void)
+{
+	int count, max_id;
+
+	if (TST_SYSFS_ASSERT_PARSE_LIST(&count, &max_id, PATH_SYS_CPU "/possible"))
+		return;
+
+	TST_SYSFS_ASSERT_RANGELL(max_id, LONG_MAX, PATH_SYS_CPU "/kernel_max");
+}
+
+static void run(void)
+{
+	TST_SYSFS_ASSERT_LIST_SUBSET(PATH_SYS_CPU "/online", PATH_SYS_CPU "/present");
+	TST_SYSFS_ASSERT_LIST_SUBSET(PATH_SYS_CPU "/present", PATH_SYS_CPU "/possible");
+	TST_SYSFS_ASSERT_LIST_SUBSET(PATH_SYS_CPU "/offline", PATH_SYS_CPU "/possible");
+
+	check_kernel_max();
+
+	check_online_proc_stat();
+}
+
+static struct tst_test test = {
+	.test_all = run,
+};
-- 
2.54.0



More information about the ltp mailing list