From b992f6b7c6c0da62499a19c2801be040369907b2 Mon Sep 17 00:00:00 2001 From: notaz Date: Mon, 2 Oct 2023 01:24:50 +0300 Subject: [PATCH] add a thp-based huge page alloc fallback --- frontend/libpicofe | 2 +- libpcsxcore/lightrec/mem.c | 6 +++++- libpcsxcore/psxmem.c | 21 +++++++++++++++++---- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/frontend/libpicofe b/frontend/libpicofe index 5dd225ec..b0ce6fa8 160000 --- a/frontend/libpicofe +++ b/frontend/libpicofe @@ -1 +1 @@ -Subproject commit 5dd225ecd6d5a04fd8e6f16c8f8ee65ee88c6fed +Subproject commit b0ce6fa8bd3c171debd5589f3ee8a95e26b1d61b diff --git a/libpcsxcore/lightrec/mem.c b/libpcsxcore/lightrec/mem.c index efabdb00..b5caa783 100644 --- a/libpcsxcore/lightrec/mem.c +++ b/libpcsxcore/lightrec/mem.c @@ -55,8 +55,12 @@ static void * mmap_huge(void *addr, size_t length, int prot, int flags, if (map == MAP_FAILED) { map = mmap(addr, length, prot, flags, fd, offset); - if (map != MAP_FAILED) + if (map != MAP_FAILED) { printf("Regular mmap to address 0x%lx succeeded\n", (uintptr_t) addr); +#ifdef MADV_HUGEPAGE + madvise(map, length, MADV_HUGEPAGE); +#endif + } } return map; diff --git a/libpcsxcore/psxmem.c b/libpcsxcore/psxmem.c index 42755e52..389bdba5 100644 --- a/libpcsxcore/psxmem.c +++ b/libpcsxcore/psxmem.c @@ -44,16 +44,29 @@ static void * psxMapDefault(unsigned long addr, size_t size, int is_fixed, enum psxMapTag tag) { -#if !P_HAVE_MMAP void *ptr; - - ptr = malloc(size); +#if !P_HAVE_MMAP + ptr = calloc(1, size); return ptr ? ptr : MAP_FAILED; #else int flags = MAP_PRIVATE | MAP_ANONYMOUS; - return mmap((void *)(uintptr_t)addr, size, + ptr = mmap((void *)(uintptr_t)addr, size, PROT_READ | PROT_WRITE, flags, -1, 0); +#ifdef MADV_HUGEPAGE + if (size >= 2*1024*1024) { + if (ptr != MAP_FAILED && ((uintptr_t)ptr & (2*1024*1024 - 1))) { + // try to manually realign assuming bottom-to-top alloc + munmap(ptr, size); + addr = (uintptr_t)ptr & ~(2*1024*1024 - 1); + ptr = mmap((void *)(uintptr_t)addr, size, + PROT_READ | PROT_WRITE, flags, -1, 0); + } + if (ptr != MAP_FAILED) + madvise(ptr, size, MADV_HUGEPAGE); + } +#endif + return ptr; #endif } -- 2.39.2