gem_tiled_swapping: Limit to available memory
authorChris Wilson <chris@chris-wilson.co.uk>
Thu, 3 Apr 2014 08:43:58 +0000 (09:43 +0100)
committerChris Wilson <chris@chris-wilson.co.uk>
Thu, 3 Apr 2014 08:45:55 +0000 (09:45 +0100)
If there is not enough free RAM+swap for us to execute our test, we will
hit OOM, so check first.

Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
lib/igt_aux.h
lib/intel_os.c
tests/gem_tiled_swapping.c

index 25a479f..948101d 100644 (file)
@@ -61,6 +61,7 @@ void igt_wait_for_keypress(void);
 
 /* These are separate to allow easier testing when porting, see the comment at
  * the bottom of intel_os.c. */
+uint64_t intel_get_avail_ram_mb(void);
 uint64_t intel_get_total_ram_mb(void);
 uint64_t intel_get_total_swap_mb(void);
 
index fb9f19c..6f0621c 100644 (file)
@@ -86,6 +86,40 @@ intel_get_total_ram_mb(void)
 }
 
 /**
+ * intel_get_avail_ram_mb:
+ *
+ * Returns:
+ * The amount of unused system RAM available in MB.
+ */
+uint64_t
+intel_get_avail_ram_mb(void)
+{
+       uint64_t retval;
+
+#ifdef HAVE_STRUCT_SYSINFO_TOTALRAM /* Linux */
+       struct sysinfo sysinf;
+       int ret;
+
+       ret = sysinfo(&sysinf);
+       assert(ret == 0);
+
+       retval = sysinf.freeram;
+       retval *= sysinf.mem_unit;
+#elif defined(_SC_PAGESIZE) && defined(_SC_PHYS_PAGES) /* Solaris */
+       long pagesize, npages;
+
+       pagesize = sysconf(_SC_PAGESIZE);
+        npages = sysconf(_SC_AVPHYS_PAGES);
+
+       retval = (uint64_t) pagesize * npages;
+#else
+#error "Unknown how to get available RAM for this OS"
+#endif
+
+       return retval / (1024*1024);
+}
+
+/**
  * intel_get_total_swap_mb:
  *
  * Returns:
index 7472364..eb1453c 100644 (file)
@@ -104,15 +104,22 @@ igt_simple_main
        igt_skip_on_simulation();
 
        fd = drm_open_any();
-       /* need slightly more than total ram */
-       count = intel_get_total_ram_mb() * 11 / 10;
+       /* need slightly more than available memory */
+       count = intel_get_total_ram_mb() + intel_get_total_swap_mb() / 4;
        bo_handles = calloc(count, sizeof(uint32_t));
        igt_assert(bo_handles);
 
        idx_arr = calloc(count, sizeof(int));
        igt_assert(idx_arr);
 
-       igt_require(intel_get_total_ram_mb() / 4 < intel_get_total_swap_mb());
+       igt_log(IGT_LOG_INFO,
+               "Using %d 1MiB objects (available RAM: %ld/%ld, swap: %ld)\n",
+               count,
+               (long)intel_get_avail_ram_mb(),
+               (long)intel_get_total_ram_mb(),
+               (long)intel_get_total_swap_mb());
+
+       igt_require(count < intel_get_avail_ram_mb() + intel_get_total_swap_mb());
 
        for (i = 0; i < count; i++) {
                bo_handles[i] = create_bo_and_fill(fd);