From b66628baca83b1b2a552c45f8b8950303fb6ef04 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Wed, 4 Nov 2020 14:56:40 +0300 Subject: [PATCH] Improve low-level mmap utilities (Linux/BSD) Add madvise (MADV_WILLNEED) on utils::memory_commit Add madvise (MADV_FREE or MADV_DONTNEED) on utils::memory_decommit Improve shm_open pseudo-random name (not used on Linux) --- Utilities/JIT.cpp | 4 +--- Utilities/VirtualMemory.cpp | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Utilities/JIT.cpp b/Utilities/JIT.cpp index 73ba3a7159..56e45e2de7 100644 --- a/Utilities/JIT.cpp +++ b/Utilities/JIT.cpp @@ -88,9 +88,7 @@ static u8* add_jit_memory(std::size_t size, uint align) if (olda != newa) [[unlikely]] { -#ifdef CAN_OVERCOMMIT - madvise(pointer + olda, newa - olda, MADV_WILLNEED); -#else +#ifndef CAN_OVERCOMMIT // Commit more memory utils::memory_commit(pointer + olda, newa - olda, Prot); #endif diff --git a/Utilities/VirtualMemory.cpp b/Utilities/VirtualMemory.cpp index 6214e938d4..e3e496ce78 100644 --- a/Utilities/VirtualMemory.cpp +++ b/Utilities/VirtualMemory.cpp @@ -112,6 +112,7 @@ namespace utils #else const u64 ptr64 = reinterpret_cast(pointer); verify(HERE), ::mprotect(reinterpret_cast(ptr64 & -4096), size + (ptr64 & 4095), +prot) != -1; + verify(HERE), ::madvise(reinterpret_cast(ptr64 & -4096), size + (ptr64 & 4095), MADV_WILLNEED) != -1; #endif } @@ -120,7 +121,13 @@ namespace utils #ifdef _WIN32 verify(HERE), ::VirtualFree(pointer, size, MEM_DECOMMIT); #else + const u64 ptr64 = reinterpret_cast(pointer); verify(HERE), ::mmap(pointer, size, PROT_NONE, MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0) != reinterpret_cast(-1); +#ifdef MADV_FREE + verify(HERE), ::madvise(reinterpret_cast(ptr64 & -4096), size + (ptr64 & 4095), MADV_FREE) != -1; +#else + verify(HERE), ::madvise(reinterpret_cast(ptr64 & -4096), size + (ptr64 & 4095), MADV_DONTNEED) != -1; +#endif #endif } @@ -130,7 +137,14 @@ namespace utils memory_decommit(pointer, size); memory_commit(pointer, size, prot); #else + const u64 ptr64 = reinterpret_cast(pointer); +#ifdef MADV_FREE + verify(HERE), ::madvise(reinterpret_cast(ptr64 & -4096), size + (ptr64 & 4095), MADV_FREE) != -1; +#else + verify(HERE), ::madvise(reinterpret_cast(ptr64 & -4096), size + (ptr64 & 4095), MADV_DONTNEED) != -1; +#endif verify(HERE), ::mmap(pointer, size, +prot, MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0) != reinterpret_cast(-1); + verify(HERE), ::madvise(reinterpret_cast(ptr64 & -4096), size + (ptr64 & 4095), MADV_WILLNEED) != -1; #endif } @@ -178,7 +192,9 @@ namespace utils verify(HERE), m_file >= 0; verify(HERE), ::ftruncate(m_file, m_size) >= 0; #else - while ((m_file = ::shm_open("/rpcs3-mem1", O_RDWR | O_CREAT | O_EXCL, S_IWUSR | S_IRUSR)) == -1) + const std::string name = "/rpcs3-mem-" + std::to_string(reinterpret_cast(this)); + + while ((m_file = ::shm_open(name.c_str(), O_RDWR | O_CREAT | O_EXCL, S_IWUSR | S_IRUSR)) == -1) { if (errno == EMFILE) { @@ -188,7 +204,7 @@ namespace utils verify(HERE), errno == EEXIST; } - verify(HERE), ::shm_unlink("/rpcs3-mem1") >= 0; + verify(HERE), ::shm_unlink(name.c_str()) >= 0; verify(HERE), ::ftruncate(m_file, m_size) >= 0; #endif }