Skip to content

Commit fe0f321

Browse files
terminusacmel
authored andcommitted
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>
1 parent bdc22a8 commit fe0f321

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

tools/perf/bench/mem-functions.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
#include <string.h>
2323
#include <unistd.h>
2424
#include <sys/time.h>
25+
#include <sys/mman.h>
2526
#include <errno.h>
2627
#include <linux/time64.h>
27-
#include <linux/zalloc.h>
2828

2929
#define K 1024
3030

@@ -286,16 +286,33 @@ static int do_memcpy(const struct function *r, struct bench_params *p,
286286
return 0;
287287
}
288288

289+
static void *bench_mmap(size_t size, bool populate)
290+
{
291+
void *p;
292+
int extra = populate ? MAP_POPULATE : 0;
293+
294+
p = mmap(NULL, size, PROT_READ|PROT_WRITE,
295+
extra | MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
296+
297+
return p == MAP_FAILED ? NULL : p;
298+
}
299+
300+
static void bench_munmap(void *p, size_t size)
301+
{
302+
if (p)
303+
munmap(p, size);
304+
}
305+
289306
static bool mem_alloc(struct bench_mem_info *info, struct bench_params *p,
290307
void **src, void **dst)
291308
{
292309
bool failed;
293310

294-
*dst = zalloc(p->size);
311+
*dst = bench_mmap(p->size, true);
295312
failed = *dst == NULL;
296313

297314
if (info->alloc_src) {
298-
*src = zalloc(p->size);
315+
*src = bench_mmap(p->size, true);
299316
failed = failed || *src == NULL;
300317
}
301318

@@ -306,8 +323,8 @@ static void mem_free(struct bench_mem_info *info __maybe_unused,
306323
struct bench_params *p __maybe_unused,
307324
void **src, void **dst)
308325
{
309-
free(*dst);
310-
free(*src);
326+
bench_munmap(*dst, p->size);
327+
bench_munmap(*src, p->size);
311328

312329
*dst = *src = NULL;
313330
}

0 commit comments

Comments
 (0)