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 Signed-off-by: Ankur Arora --- tools/perf/bench/mem-functions.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/tools/perf/bench/mem-functions.c b/tools/perf/bench/mem-functions.c index 60ea20277507..e97962dd8f81 100644 --- a/tools/perf/bench/mem-functions.c +++ b/tools/perf/bench/mem-functions.c @@ -22,9 +22,9 @@ #include #include #include +#include #include #include -#include #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; } -- 2.31.1