Commit 271a7b2e authored by Balbir Singh's avatar Balbir Singh Committed by Andrew Morton
Browse files

selftests/mm/hmm-tests: new throughput tests including THP

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

Link: https://lkml.kernel.org/r/20251001065707.920170-16-balbirs@nvidia.com


Signed-off-by: default avatarBalbir Singh <balbirs@nvidia.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Zi Yan <ziy@nvidia.com>
Cc: Joshua Hahn <joshua.hahnjy@gmail.com>
Cc: Rakie Kim <rakie.kim@sk.com>
Cc: Byungchul Park <byungchul@sk.com>
Cc: Gregory Price <gourry@gourry.net>
Cc: Ying Huang <ying.huang@linux.alibaba.com>
Cc: Alistair Popple <apopple@nvidia.com>
Cc: Oscar Salvador <osalvador@suse.de>
Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: "Liam R. Howlett" <Liam.Howlett@oracle.com>
Cc: Nico Pache <npache@redhat.com>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Dev Jain <dev.jain@arm.com>
Cc: Barry Song <baohua@kernel.org>
Cc: Lyude Paul <lyude@redhat.com>
Cc: Danilo Krummrich <dakr@kernel.org>
Cc: David Airlie <airlied@gmail.com>
Cc: Simona Vetter <simona@ffwll.ch>
Cc: Ralph Campbell <rcampbell@nvidia.com>
Cc: Mika Penttilä <mpenttil@redhat.com>
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: Francois Dugast <francois.dugast@intel.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent 24c2c5b8
Loading
Loading
Loading
Loading
+196 −1
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/time.h>


/*
@@ -209,8 +210,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);
}
@@ -2657,4 +2660,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