[PATCH i-g-t 2/2] lib/igt_sysfs: Add helper to set the CPU scaling governor

Francois Dugast francois.dugast at intel.com
Tue Dec 10 09:19:32 UTC 2024


CPU scaling governor is relevant for IGT to have reproducible results
when it comes to tests duration on a given machine. Add helpers to
set it easily from IGT, removing the need for an external script.

Signed-off-by: Francois Dugast <francois.dugast at intel.com>
---
 lib/igt_sysfs.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++
 lib/igt_sysfs.h |  3 +++
 2 files changed, 55 insertions(+)

diff --git a/lib/igt_sysfs.c b/lib/igt_sysfs.c
index f33478cea..68cb53981 100644
--- a/lib/igt_sysfs.c
+++ b/lib/igt_sysfs.c
@@ -1690,3 +1690,55 @@ uint32_t xe_sysfs_get_gt_freq(int xe_device, int gt, const char *freq_name)
 	close(gt_fd);
 	return freq;
 }
+
+static char *xe_sysfs_cpu_path(int cpu, char *path, int pathlen)
+{
+	snprintf(path, pathlen, "/sys/devices/system/cpu/cpu%d", cpu);
+
+	if (!access(path, F_OK))
+		return path;
+	return NULL;
+}
+
+/**
+ * xe_sysfs_get_num_cpus:
+ *
+ * Reads number of CPU sysfs entries.
+ * Asserts for at least one CPU entry.
+ * (see xe_sysfs_cpu_path).
+ *
+ * Returns: Number of CPUs.
+ */
+int xe_sysfs_get_num_cpus(void)
+{
+	int num_cpus = 0;
+	char path[96];
+
+	while (xe_sysfs_cpu_path(num_cpus, path, sizeof(path)))
+		++num_cpus;
+
+	igt_assert_f(num_cpus > 0, "No CPU sysfs entry is found.");
+
+	return num_cpus;
+}
+
+/**
+ * xe_sysfs_set_cpu_governor
+ * @cpu: CPU number
+ * @governor: CPU scaling governor
+ *
+ * Set the scaling governor of one CPU.
+ *
+ */
+void xe_sysfs_set_cpu_governor(int cpu, const char *governor)
+{
+	char path[96];
+	int sysfs;
+
+	sysfs = open(xe_sysfs_cpu_path(cpu, path, sizeof(path)), O_DIRECTORY);
+	igt_assert(sysfs);
+
+	igt_assert(igt_sysfs_set(sysfs, "cpufreq/scaling_governor", governor));
+
+	close(sysfs);
+}
diff --git a/lib/igt_sysfs.h b/lib/igt_sysfs.h
index 6a141a5de..b44545851 100644
--- a/lib/igt_sysfs.h
+++ b/lib/igt_sysfs.h
@@ -200,4 +200,7 @@ bool xe_sysfs_engine_class_set_property(int xe_device, int gt, uint16_t class, c
 int xe_sysfs_set_gt_freq(int fd, int gt_id, const char *freq_name, uint32_t freq);
 uint32_t xe_sysfs_get_gt_freq(int fd, int gt_id, const char *freq_name);
 
+int xe_sysfs_get_num_cpus(void);
+void xe_sysfs_set_cpu_governor(int cpu, const char *governor);
+
 #endif /* __IGT_SYSFS_H__ */
-- 
2.43.0



More information about the igt-dev mailing list