perf bench mem: Switch from zalloc() to mmap()

Using mmap() ensures that the buffer is always aligned at a fixed
boundary. Switch to that to remove one source of variability.

Since we always want to read/write from the allocated buffers map
with pagetables pre-populated.

Reviewed-by: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Ankur Arora <ankur.a.arora@oracle.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Mateusz Guzik <mjguzik@gmail.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Raghavendra K T <raghavendra.kt@amd.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Ankur Arora
2025-09-19 12:43:12 -03:00
committed by Arnaldo Carvalho de Melo
parent bdc22a83dc
commit fe0f3216dd
+22 -5
View File
@@ -22,9 +22,9 @@
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <errno.h>
#include <linux/time64.h>
#include <linux/zalloc.h>
#define K 1024
@@ -286,16 +286,33 @@ static int do_memcpy(const struct function *r, struct bench_params *p,
return 0;
}
static void *bench_mmap(size_t size, bool populate)
{
void *p;
int extra = populate ? MAP_POPULATE : 0;
p = mmap(NULL, size, PROT_READ|PROT_WRITE,
extra | MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
return p == MAP_FAILED ? NULL : p;
}
static void bench_munmap(void *p, size_t size)
{
if (p)
munmap(p, size);
}
static bool mem_alloc(struct bench_mem_info *info, struct bench_params *p,
void **src, void **dst)
{
bool failed;
*dst = zalloc(p->size);
*dst = bench_mmap(p->size, true);
failed = *dst == NULL;
if (info->alloc_src) {
*src = zalloc(p->size);
*src = bench_mmap(p->size, true);
failed = failed || *src == NULL;
}
@@ -306,8 +323,8 @@ static void mem_free(struct bench_mem_info *info __maybe_unused,
struct bench_params *p __maybe_unused,
void **src, void **dst)
{
free(*dst);
free(*src);
bench_munmap(*dst, p->size);
bench_munmap(*src, p->size);
*dst = *src = NULL;
}