All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH i-g-t] lib: add a function to lock memory into RAM
@ 2014-12-08 17:22 Thomas Wood
  2014-12-08 17:42 ` Daniel Vetter
  0 siblings, 1 reply; 3+ messages in thread
From: Thomas Wood @ 2014-12-08 17:22 UTC (permalink / raw)
  To: intel-gfx

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@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

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH i-g-t] lib: add a function to lock memory into RAM
  2014-12-08 17:22 [PATCH i-g-t] lib: add a function to lock memory into RAM Thomas Wood
@ 2014-12-08 17:42 ` Daniel Vetter
  2014-12-10 19:02   ` [PATCH i-g-t v2] " Thomas Wood
  0 siblings, 1 reply; 3+ messages in thread
From: Daniel Vetter @ 2014-12-08 17:42 UTC (permalink / raw)
  To: Thomas Wood; +Cc: intel-gfx

On Mon, Dec 08, 2014 at 05:22:12PM +0000, Thomas Wood wrote:
> 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@intel.com>

Some nitpicks below. Also please mention in the commit message how much
this speeds up the testcase.

btw as long as you still see lots of swapping going on with vmstat 1 then
the test still works.

Cheers, Daniel

> ---
>  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)

Imo this should be an igt_warn_on since if we leak mlocked memory until
the next test that needs it there's a good chance we've locked a bit too
much.

> +		igt_unlock_mem();
> +
> +	locked_size = size * 1024 * 1024;
> +
> +	locked_mem = malloc(locked_size);
> +	igt_assert(locked_mem);

Imo this should be an igt_require: On 32bit platforms with lots of memory
there might not be enough virtual address space for mlocking.

> +
> +	/* 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);

This should have an igt_assert to make sure we still do a useful test.

> +}
> +
> +/**
> + * 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);

We have machines with less than 512MB of memory (vpg has some underpowered
baytrails like that). So this needs a min(..., 0). And the calculation
below needs to substract that from the total 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
> 
> _______________________________________________
> Intel-gfx mailing list
> Intel-gfx@lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/intel-gfx

-- 
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH i-g-t v2] lib: add a function to lock memory into RAM
  2014-12-08 17:42 ` Daniel Vetter
@ 2014-12-10 19:02   ` Thomas Wood
  0 siblings, 0 replies; 3+ messages in thread
From: Thomas Wood @ 2014-12-10 19:02 UTC (permalink / raw)
  To: intel-gfx

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.

The following durations were recorded on a haswell system before this
change:

  Subtest non-threaded: SUCCESS (55.889s)
  Subtest threaded: SUCCESS (810.532s)

and after:

  Subtest non-threaded: SUCCESS (11.804s)
  Subtest threaded: SUCCESS (268.336s)

v2: add various assertions and requirements and make sure
    gem_tiled_swapping works on systems with less RAM (Daniel Vetter)

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

diff --git a/lib/igt_aux.c b/lib/igt_aux.c
index 3051d84..d8d8577 100644
--- a/lib/igt_aux.c
+++ b/lib/igt_aux.c
@@ -601,3 +601,60 @@ 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;
+	int ret;
+
+	if (size == 0) {
+		return;
+	}
+
+	if (locked_mem) {
+		igt_unlock_mem();
+		igt_warn("Unlocking previously locked memory.\n");
+	}
+
+	locked_size = size * 1024 * 1024;
+
+	locked_mem = malloc(locked_size);
+	igt_require_f(locked_mem,
+		      "Could not allocate enough memory to lock.\n");
+
+	/* write into each page to ensure it is allocated */
+	for (i = 0; i < locked_size; i += pagesize)
+		locked_mem[i] = i;
+
+	ret = mlock(locked_mem, locked_size);
+	igt_assert_f(ret == 0, "Could not lock memory into RAM.\n");
+}
+
+/**
+ * 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 63e1b06..c420b3f 100644
--- a/lib/igt_aux.h
+++ b/lib/igt_aux.h
@@ -96,4 +96,7 @@ void intel_require_memory(uint32_t count, uint32_t size, unsigned mode);
 	(b) = _tmp;		\
 } while (0)
 
+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..1637ec7 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)
@@ -151,13 +152,20 @@ igt_main
 	int fd, n, count, num_threads;
 
 	igt_fixture {
+		size_t lock_size;
+
 		current_tiling_mode = I915_TILING_X;
 
 		intel_purge_vm_caches();
 
 		fd = drm_open_any();
+
+		/* lock RAM, leaving only 512MB available */
+		lock_size = min(0, intel_get_total_ram_mb() - AVAIL_RAM);
+		igt_lock_mem(lock_size);
+
 		/* need slightly more than available memory */
-		count = intel_get_total_ram_mb() + intel_get_total_swap_mb() / 4;
+		count = lock_size * 1.25;
 		bo_handles = calloc(count, sizeof(uint32_t));
 		igt_assert(bo_handles);
 
-- 
2.1.0

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-12-10 19:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-12-08 17:22 [PATCH i-g-t] lib: add a function to lock memory into RAM Thomas Wood
2014-12-08 17:42 ` Daniel Vetter
2014-12-10 19:02   ` [PATCH i-g-t v2] " Thomas Wood

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.