[Intel-gfx] [PATCH i-g-t] lib: add a function to lock memory into RAM

Thomas Wood thomas.wood at intel.com
Mon Dec 8 09:22:12 PST 2014


Add a function to lock memory into RAM and use it in the
gem_tiled_swapping test to reduce the amount of allocated memory
required to force swapping. This also reduces the amount of time
required for the test to complete, since the data set is smaller.

Signed-off-by: Thomas Wood <thomas.wood at intel.com>
---
 lib/igt_aux.c              | 48 ++++++++++++++++++++++++++++++++++++++++++++++
 lib/igt_aux.h              |  3 +++
 tests/gem_tiled_swapping.c |  7 ++++++-
 3 files changed, 57 insertions(+), 1 deletion(-)

diff --git a/lib/igt_aux.c b/lib/igt_aux.c
index 3051d84..a98aa8f 100644
--- a/lib/igt_aux.c
+++ b/lib/igt_aux.c
@@ -601,3 +601,51 @@ struct type_name connector_type_names[] = {
 };
 
 type_name_fn(connector_type)
+
+
+/**
+ * igt_lock_mem: @size: the amount of memory to lock into RAM, in MB
+ *
+ * Allocate @size MB of memory and lock it into RAM. This releases any
+ * previously locked memory.
+ *
+ * Use #igt_unlock_mem to release the currently locked memory.
+ */
+static char *locked_mem;
+static size_t locked_size;
+
+void igt_lock_mem(size_t size)
+{
+	long pagesize = sysconf(_SC_PAGESIZE);
+	size_t i;
+
+	if (locked_mem)
+		igt_unlock_mem();
+
+	locked_size = size * 1024 * 1024;
+
+	locked_mem = malloc(locked_size);
+	igt_assert(locked_mem);
+
+	/* write into each page to ensure it is allocated */
+	for (i = 0; i < locked_size; i += pagesize)
+		locked_mem[i] = i;
+
+	mlock(locked_mem, locked_size);
+}
+
+/**
+ * igt_unlock_mem:
+ *
+ * Release and free the RAM used by #igt_lock_mem.
+ */
+void igt_unlock_mem(void)
+{
+	if (!locked_mem)
+		return;
+
+	munlock(locked_mem, locked_size);
+
+	free(locked_mem);
+	locked_mem = NULL;
+}
diff --git a/lib/igt_aux.h b/lib/igt_aux.h
index 6c83c53..b61555b 100644
--- a/lib/igt_aux.h
+++ b/lib/igt_aux.h
@@ -90,4 +90,7 @@ void intel_require_memory(uint32_t count, uint32_t size, unsigned mode);
 #define min(a, b) ((a) < (b) ? (a) : (b))
 #define max(a, b) ((a) > (b) ? (a) : (b))
 
+void igt_lock_mem(size_t size);
+void igt_unlock_mem(void);
+
 #endif /* IGT_AUX_H */
diff --git a/tests/gem_tiled_swapping.c b/tests/gem_tiled_swapping.c
index 3fac52f..0684ab2 100644
--- a/tests/gem_tiled_swapping.c
+++ b/tests/gem_tiled_swapping.c
@@ -70,6 +70,7 @@ IGT_TEST_DESCRIPTION("Exercise swizzle code for swapping.");
 static uint32_t current_tiling_mode;
 
 #define PAGE_SIZE 4096
+#define AVAIL_RAM 512
 
 static uint32_t
 create_bo_and_fill(int fd)
@@ -156,8 +157,12 @@ igt_main
 		intel_purge_vm_caches();
 
 		fd = drm_open_any();
+
+		/* lock RAM, leaving only 512MB available */
+		igt_lock_mem(intel_get_total_ram_mb() - AVAIL_RAM);
+
 		/* need slightly more than available memory */
-		count = intel_get_total_ram_mb() + intel_get_total_swap_mb() / 4;
+		count = AVAIL_RAM * 1.25;
 		bo_handles = calloc(count, sizeof(uint32_t));
 		igt_assert(bo_handles);
 
-- 
2.1.0



More information about the Intel-gfx mailing list