gem_tiled_swapping: Purge all page/swap caches first
[platform/upstream/intel-gpu-tools.git] / tests / gem_tiled_swapping.c
index c04a3bc..aecd543 100644 (file)
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include <assert.h>
 #include <fcntl.h>
 #include <inttypes.h>
 #include <errno.h>
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/ioctl.h>
-#include <sys/mman.h>
-#include "drm.h"
-#include "i915_drm.h"
+
+#include <drm.h>
+
+#include "ioctl_wrappers.h"
 #include "drmtest.h"
-#include "intel_gpu_tools.h"
+#include "intel_io.h"
+#include "igt_aux.h"
 
 #define WIDTH 512
 #define HEIGHT 512
@@ -79,6 +80,9 @@ create_bo_and_fill(int fd)
 
        /* Fill the BO with dwords starting at start_val */
        data = gem_mmap(fd, handle, LINEAR_DWORDS, PROT_READ | PROT_WRITE);
+       if (data == NULL && errno == ENOSPC)
+               return 0;
+
        for (i = 0; i < WIDTH*HEIGHT; i++)
                data[i] = i;
        munmap(data, LINEAR_DWORDS);
@@ -89,8 +93,7 @@ create_bo_and_fill(int fd)
 uint32_t *bo_handles;
 int *idx_arr;
 
-int
-main(int argc, char **argv)
+igt_simple_main
 {
        int fd;
        uint32_t *data;
@@ -98,48 +101,49 @@ main(int argc, char **argv)
        int count;
        current_tiling_mode = I915_TILING_X;
 
+       igt_skip_on_simulation();
+       intel_purge_vm_caches();
+
        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));
-       assert(bo_handles);
+       igt_assert(bo_handles);
 
        idx_arr = calloc(count, sizeof(int));
-       assert(idx_arr);
+       igt_assert(idx_arr);
 
-       if (intel_get_total_swap_mb() == 0) {
-               printf("no swap detected\n");
-               return 77;
-       }
+       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());
 
-       if (intel_get_total_ram_mb() / 4 > intel_get_total_swap_mb()) {
-               printf("not enough swap detected\n");
-               return 77;
-       }
+       igt_require(count < intel_get_avail_ram_mb() + intel_get_total_swap_mb());
 
-       for (i = 0; i < count; i++)
+       for (i = 0; i < count; i++) {
                bo_handles[i] = create_bo_and_fill(fd);
+               /* Not enough mmap address space possible. */
+               igt_require(bo_handles[i]);
+       }
 
        for (i = 0; i < count; i++)
                idx_arr[i] = i;
 
-       drmtest_permute_array(idx_arr, count,
-                             drmtest_exchange_int);
+       igt_permute_array(idx_arr, count,
+                             igt_exchange_int);
 
        for (i = 0; i < count/2; i++) {
                /* Check the target bo's contents. */
                data = gem_mmap(fd, bo_handles[idx_arr[i]],
                                LINEAR_DWORDS, PROT_READ | PROT_WRITE);
                for (j = 0; j < WIDTH*HEIGHT; j++)
-                       if (data[j] != j) {
-                               fprintf(stderr, "mismatch at %i: %i\n",
-                                               j, data[j]);
-                               exit(1);
-                       }
+                       igt_assert_f(data[j] == j,
+                                    "mismatch at %i: %i\n",
+                                    j, data[j]);
                munmap(data, LINEAR_DWORDS);
        }
 
        close(fd);
-
-       return 0;
 }