[LTP] [PATCH v3 06/31] testcases: sysfs: Add sys_cpu_topology01
Cyril Hrubis
chrubis@suse.cz
Tue Aug 25 13:36:00 CEST 2026
A test for /sys/devices/system/cpu/* files.
Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
runtest/sysfs | 1 +
.../sysfs/devices/system/cpu/.gitignore | 1 +
.../kernel/sysfs/devices/system/cpu/Makefile | 7 ++
.../devices/system/cpu/sys_cpu_topology01.c | 103 ++++++++++++++++++
4 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/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..d2aefa85e
--- /dev/null
+++ b/testcases/kernel/sysfs/devices/system/cpu/sys_cpu_topology01.c
@@ -0,0 +1,103 @@
+// 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"
+
+#define SYS_CPU "/sys/devices/system/cpu"
+
+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, SYS_CPU "/online"))
+ return;
+
+ stat_cpus = count_proc_stat_cpus();
+
+ if (count == stat_cpus) {
+ tst_res(TPASS,
+ SYS_CPU "/online count (%d) == /proc/stat cpu lines (%d)",
+ count, stat_cpus);
+ } else {
+ tst_res(TFAIL,
+ 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, SYS_CPU "/possible"))
+ return;
+
+ TST_SYSFS_ASSERT_RANGELL(max_id, LONG_MAX, SYS_CPU "/kernel_max");
+}
+
+static void run(void)
+{
+ TST_SYSFS_ASSERT_LIST_SUBSET(SYS_CPU "/online", SYS_CPU "/present");
+ TST_SYSFS_ASSERT_LIST_SUBSET(SYS_CPU "/present", SYS_CPU "/possible");
+ TST_SYSFS_ASSERT_LIST_SUBSET(SYS_CPU "/offline", SYS_CPU "/possible");
+
+ check_kernel_max();
+
+ check_online_proc_stat();
+}
+
+static struct tst_test test = {
+ .test_all = run,
+};
--
2.51.0
More information about the ltp
mailing list