[PATCH 1/2] lib/igt_os: Add igt_get_meminfo

Jonathan Cavitt jonathan.cavitt at intel.com
Tue Apr 29 19:29:55 UTC 2025


Add a function, igt_get_meminfo, that allows IGT to query the value of a
specific meminfo param.  This can have practical use later, E.G. for
determining if a test can run given the current meminfo settings.

Signed-off-by: Jonathan Cavitt <jonathan.cavitt at intel.com>
---
 lib/igt_os.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 lib/igt_os.h |  2 ++
 2 files changed, 45 insertions(+)

diff --git a/lib/igt_os.c b/lib/igt_os.c
index ac8960f8b0..0fa10ee750 100644
--- a/lib/igt_os.c
+++ b/lib/igt_os.c
@@ -99,6 +99,49 @@ static uint64_t get_meminfo(const char *info, const char *tag)
 	return 0;
 }
 
+/**
+ * igt_get_meminfo:
+ * @field: name of meminfo field to get the value of
+ *
+ * Returns:
+ * The value of the meminfo field @field
+ */
+uint64_t igt_get_meminfo(const char *field)
+{
+	uint64_t retval;
+	char *info;
+	char *query;
+	int fd;
+
+	fd = open("/proc", O_RDONLY);
+	info = igt_sysfs_get(fd, "meminfo");
+	close(fd);
+
+	if (!info) {
+		igt_warn("Could not open /proc/meminfo");
+		return 0;
+	}
+
+	/*
+	 * In get_meminfo, it is expected that the provided tag ends in a colon (:).
+	 * If the colon is missing, the search will fail to find the meminfo field.
+	 * Userspace should not be required to provide this extra colon in the
+	 * meminfo field name, so append it silently to the provided name before
+	 * passing it to get_meminfo.
+	 */
+	query = malloc(strlen(field) + 1);
+	if (!query) {
+		igt_warn("Failed to alloc search query");
+		return 0;
+	}
+	sprintf(query, "%s:", field);
+
+	retval = get_meminfo(info, query);
+	free(query);
+
+	return retval;
+}
+
 /**
  * igt_get_avail_ram_mb:
  *
diff --git a/lib/igt_os.h b/lib/igt_os.h
index b9af0a4b7c..415895f76a 100644
--- a/lib/igt_os.h
+++ b/lib/igt_os.h
@@ -28,6 +28,8 @@
 #include <stddef.h>
 #include <stdint.h>
 
+uint64_t igt_get_meminfo(const char *field);
+
 /* These are separate to allow easier testing when porting, see the comment at
  * the bottom of intel_os.c. */
 uint64_t igt_get_total_ram_mb(void);
-- 
2.43.0



More information about the igt-dev mailing list