From 4210d1cfc58f2d8a3da0730389ee63d68c6fe914 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Mon, 27 Mar 2017 11:38:02 -0700 Subject: [PATCH] Improve on map re-allocation strategy Don't bother reallocating hugepage mappings, the cache associativity issues are hopefully not all that noticeable with big pages to begin with, and with fewer big pages to go around it's likely less effective to try to change the mapping anyway. For the regular page case, force any remapping to over-map the old mapping, which will free the old pages. That might cause the new mapping to have similar allocation patterns as the old one, but not using excessive amounts of memory still makes this the better option. Signed-off-by: Linus Torvalds --- test-tlb.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/test-tlb.c b/test-tlb.c index dda2a07..94cac5d 100644 --- a/test-tlb.c +++ b/test-tlb.c @@ -149,18 +149,33 @@ static void randomize_map(void *map, unsigned long size, unsigned long stride) // Hugepage size #define HUGEPAGE (2*1024*1024) -static void *create_map(unsigned long size, unsigned long stride) +static void *create_map(void *map, unsigned long size, unsigned long stride) { unsigned int flags = MAP_PRIVATE | MAP_ANONYMOUS; unsigned long off, mapsize; unsigned int *lastpos; - void *map; + + /* + * If we're using hugepages, we will just re-use any existing + * hugepage map - the issues with different physical page + * allocations for cache associativity testing just isn't worth + * it with large pages. + * + * With regular pages, just mmap over the old allocation to + * force new page allocations. Hopefully this will then make + * the virtual mapping different enough to matter for timings. + */ + if (map) { + if (test_hugepage) + return map; + flags |= MAP_FIXED; + } mapsize = size; if (test_hugepage) mapsize += 2*HUGEPAGE; - map = mmap(NULL, mapsize, PROT_READ | PROT_WRITE, flags, -1, 0); + map = mmap(map, mapsize, PROT_READ | PROT_WRITE, flags, -1, 0); if (map == MAP_FAILED) die("mmap failed"); @@ -229,11 +244,12 @@ int main(int argc, char **argv) if (stride < 4 || size < stride) die("bad arguments: test-tlb [-H] "); + map = NULL; cycles = 1e10; for (int i = 0; i < 5; i++) { double d; - map = create_map(size, stride); + map = create_map(map, size, stride); if (random_list) randomize_map(map, size, stride);