Add new benchmark style support to test transfer bandwidth for
zone device memory operations.

Cc: Andrew Morton <a...@linux-foundation.org>
Cc: David Hildenbrand <da...@redhat.com>
Cc: Zi Yan <z...@nvidia.com>
Cc: Joshua Hahn <joshua.hah...@gmail.com>
Cc: Rakie Kim <rakie....@sk.com>
Cc: Byungchul Park <byungc...@sk.com>
Cc: Gregory Price <gou...@gourry.net>
Cc: Ying Huang <ying.hu...@linux.alibaba.com>
Cc: Alistair Popple <apop...@nvidia.com>
Cc: Oscar Salvador <osalva...@suse.de>
Cc: Lorenzo Stoakes <lorenzo.stoa...@oracle.com>
Cc: Baolin Wang <baolin.w...@linux.alibaba.com>
Cc: "Liam R. Howlett" <liam.howl...@oracle.com>
Cc: Nico Pache <npa...@redhat.com>
Cc: Ryan Roberts <ryan.robe...@arm.com>
Cc: Dev Jain <dev.j...@arm.com>
Cc: Barry Song <bao...@kernel.org>
Cc: Lyude Paul <ly...@redhat.com>
Cc: Danilo Krummrich <d...@kernel.org>
Cc: David Airlie <airl...@gmail.com>
Cc: Simona Vetter <sim...@ffwll.ch>
Cc: Ralph Campbell <rcampb...@nvidia.com>
Cc: Mika Penttilä <mpent...@redhat.com>
Cc: Matthew Brost <matthew.br...@intel.com>
Cc: Francois Dugast <francois.dug...@intel.com>

Signed-off-by: Balbir Singh <balb...@nvidia.com>
---
 tools/testing/selftests/mm/hmm-tests.c | 197 ++++++++++++++++++++++++-
 1 file changed, 196 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/mm/hmm-tests.c 
b/tools/testing/selftests/mm/hmm-tests.c
index da3322a1282c..1325de70f44f 100644
--- a/tools/testing/selftests/mm/hmm-tests.c
+++ b/tools/testing/selftests/mm/hmm-tests.c
@@ -25,6 +25,7 @@
 #include <sys/stat.h>
 #include <sys/mman.h>
 #include <sys/ioctl.h>
+#include <sys/time.h>
 
 
 /*
@@ -207,8 +208,10 @@ static void hmm_buffer_free(struct hmm_buffer *buffer)
        if (buffer == NULL)
                return;
 
-       if (buffer->ptr)
+       if (buffer->ptr) {
                munmap(buffer->ptr, buffer->size);
+               buffer->ptr = NULL;
+       }
        free(buffer->mirror);
        free(buffer);
 }
@@ -2466,4 +2469,196 @@ TEST_F(hmm, migrate_anon_huge_zero_err)
        buffer->ptr = old_ptr;
        hmm_buffer_free(buffer);
 }
+
+struct benchmark_results {
+       double sys_to_dev_time;
+       double dev_to_sys_time;
+       double throughput_s2d;
+       double throughput_d2s;
+};
+
+static double get_time_ms(void)
+{
+       struct timeval tv;
+
+       gettimeofday(&tv, NULL);
+       return (tv.tv_sec * 1000.0) + (tv.tv_usec / 1000.0);
+}
+
+static inline struct hmm_buffer *hmm_buffer_alloc(unsigned long size)
+{
+       struct hmm_buffer *buffer;
+
+       buffer = malloc(sizeof(*buffer));
+
+       buffer->fd = -1;
+       buffer->size = size;
+       buffer->mirror = malloc(size);
+       memset(buffer->mirror, 0xFF, size);
+       return buffer;
+}
+
+static void print_benchmark_results(const char *test_name, size_t buffer_size,
+                                    struct benchmark_results *thp,
+                                    struct benchmark_results *regular)
+{
+       double s2d_improvement = ((regular->sys_to_dev_time - 
thp->sys_to_dev_time) /
+                                regular->sys_to_dev_time) * 100.0;
+       double d2s_improvement = ((regular->dev_to_sys_time - 
thp->dev_to_sys_time) /
+                                regular->dev_to_sys_time) * 100.0;
+       double throughput_s2d_improvement = ((thp->throughput_s2d - 
regular->throughput_s2d) /
+                                           regular->throughput_s2d) * 100.0;
+       double throughput_d2s_improvement = ((thp->throughput_d2s - 
regular->throughput_d2s) /
+                                           regular->throughput_d2s) * 100.0;
+
+       printf("\n=== %s (%.1f MB) ===\n", test_name, buffer_size / (1024.0 * 
1024.0));
+       printf("                     | With THP        | Without THP     | 
Improvement\n");
+       
printf("---------------------------------------------------------------------\n");
+       printf("Sys->Dev Migration   | %.3f ms        | %.3f ms        | 
%.1f%%\n",
+              thp->sys_to_dev_time, regular->sys_to_dev_time, s2d_improvement);
+       printf("Dev->Sys Migration   | %.3f ms        | %.3f ms        | 
%.1f%%\n",
+              thp->dev_to_sys_time, regular->dev_to_sys_time, d2s_improvement);
+       printf("S->D Throughput      | %.2f GB/s      | %.2f GB/s      | 
%.1f%%\n",
+              thp->throughput_s2d, regular->throughput_s2d, 
throughput_s2d_improvement);
+       printf("D->S Throughput      | %.2f GB/s      | %.2f GB/s      | 
%.1f%%\n",
+              thp->throughput_d2s, regular->throughput_d2s, 
throughput_d2s_improvement);
+}
+
+/*
+ * Run a single migration benchmark
+ * fd: file descriptor for hmm device
+ * use_thp: whether to use THP
+ * buffer_size: size of buffer to allocate
+ * iterations: number of iterations
+ * results: where to store results
+ */
+static inline int run_migration_benchmark(int fd, int use_thp, size_t 
buffer_size,
+                                          int iterations, struct 
benchmark_results *results)
+{
+       struct hmm_buffer *buffer;
+       unsigned long npages = buffer_size / sysconf(_SC_PAGESIZE);
+       double start, end;
+       double s2d_total = 0, d2s_total = 0;
+       int ret, i;
+       int *ptr;
+
+       buffer = hmm_buffer_alloc(buffer_size);
+
+       /* Map memory */
+       buffer->ptr = mmap(NULL, buffer_size, PROT_READ | PROT_WRITE,
+                         MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+
+       if (!buffer->ptr)
+               return -1;
+
+       /* Apply THP hint if requested */
+       if (use_thp)
+               ret = madvise(buffer->ptr, buffer_size, MADV_HUGEPAGE);
+       else
+               ret = madvise(buffer->ptr, buffer_size, MADV_NOHUGEPAGE);
+
+       if (ret)
+               return ret;
+
+       /* Initialize memory to make sure pages are allocated */
+       ptr = (int *)buffer->ptr;
+       for (i = 0; i < buffer_size / sizeof(int); i++)
+               ptr[i] = i & 0xFF;
+
+       /* Warmup iteration */
+       ret = hmm_migrate_sys_to_dev(fd, buffer, npages);
+       if (ret)
+               return ret;
+
+       ret = hmm_migrate_dev_to_sys(fd, buffer, npages);
+       if (ret)
+               return ret;
+
+       /* Benchmark iterations */
+       for (i = 0; i < iterations; i++) {
+               /* System to device migration */
+               start = get_time_ms();
+
+               ret = hmm_migrate_sys_to_dev(fd, buffer, npages);
+               if (ret)
+                       return ret;
+
+               end = get_time_ms();
+               s2d_total += (end - start);
+
+               /* Device to system migration */
+               start = get_time_ms();
+
+               ret = hmm_migrate_dev_to_sys(fd, buffer, npages);
+               if (ret)
+                       return ret;
+
+               end = get_time_ms();
+               d2s_total += (end - start);
+       }
+
+       /* Calculate average times and throughput */
+       results->sys_to_dev_time = s2d_total / iterations;
+       results->dev_to_sys_time = d2s_total / iterations;
+       results->throughput_s2d = (buffer_size / (1024.0 * 1024.0 * 1024.0)) /
+                                (results->sys_to_dev_time / 1000.0);
+       results->throughput_d2s = (buffer_size / (1024.0 * 1024.0 * 1024.0)) /
+                                (results->dev_to_sys_time / 1000.0);
+
+       /* Cleanup */
+       hmm_buffer_free(buffer);
+       return 0;
+}
+
+/*
+ * Benchmark THP migration with different buffer sizes
+ */
+TEST_F_TIMEOUT(hmm, benchmark_thp_migration, 120)
+{
+       struct benchmark_results thp_results, regular_results;
+       size_t thp_size = 2 * 1024 * 1024; /* 2MB - typical THP size */
+       int iterations = 5;
+
+       printf("\nHMM THP Migration Benchmark\n");
+       printf("---------------------------\n");
+       printf("System page size: %ld bytes\n", sysconf(_SC_PAGESIZE));
+
+       /* Test different buffer sizes */
+       size_t test_sizes[] = {
+               thp_size / 4,      /* 512KB - smaller than THP */
+               thp_size / 2,      /* 1MB - half THP */
+               thp_size,          /* 2MB - single THP */
+               thp_size * 2,      /* 4MB - two THPs */
+               thp_size * 4,      /* 8MB - four THPs */
+               thp_size * 8,       /* 16MB - eight THPs */
+               thp_size * 128,       /* 256MB - one twenty eight THPs */
+       };
+
+       static const char *const test_names[] = {
+               "Small Buffer (512KB)",
+               "Half THP Size (1MB)",
+               "Single THP Size (2MB)",
+               "Two THP Size (4MB)",
+               "Four THP Size (8MB)",
+               "Eight THP Size (16MB)",
+               "One twenty eight THP Size (256MB)"
+       };
+
+       int num_tests = ARRAY_SIZE(test_sizes);
+
+       /* Run all tests */
+       for (int i = 0; i < num_tests; i++) {
+               /* Test with THP */
+               ASSERT_EQ(run_migration_benchmark(self->fd, 1, test_sizes[i],
+                                       iterations, &thp_results), 0);
+
+               /* Test without THP */
+               ASSERT_EQ(run_migration_benchmark(self->fd, 0, test_sizes[i],
+                                       iterations, &regular_results), 0);
+
+               /* Print results */
+               print_benchmark_results(test_names[i], test_sizes[i],
+                                       &thp_results, &regular_results);
+       }
+}
 TEST_HARNESS_MAIN
-- 
2.50.1

Reply via email to