From 63104af8e9eebd03f9947da3319eecc993704c66 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Wed, 25 Aug 2021 17:47:28 +0300 Subject: [PATCH] vm_native: detect overcommit support on FreeBSD and other OS Used in sparse memory initialization code. --- rpcs3/util/vm_native.cpp | 42 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/rpcs3/util/vm_native.cpp b/rpcs3/util/vm_native.cpp index 82f05ef79b..fa9c386ab9 100644 --- a/rpcs3/util/vm_native.cpp +++ b/rpcs3/util/vm_native.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #endif #ifdef __linux__ @@ -437,7 +438,6 @@ namespace utils m_handle = ensure(::CreateFileMappingW(f.get_handle(), nullptr, PAGE_READWRITE, 0, 0, nullptr)); #else - // TODO: check overcommit configuration of other supported platforms to bypass rpcs3_vm creation #ifdef __linux__ if (const char c = fs::file("/proc/sys/vm/overcommit_memory").read(); c == '0' || c == '1') { @@ -451,6 +451,46 @@ namespace utils { fprintf(stderr, "Reading /proc/sys/vm/overcommit_memory: %c", c); } +#else + int vm_overcommit = 0; + auto vm_sz = sizeof(int); + +#if defined(__NetBSD__) || defined(__APPLE__) + // Always ON + vm_overcommit = 0; +#elif defined(__FreeBSD__) && defined(VM_OVERCOMMIT) + int mib[2]{CTL_VM, VM_OVERCOMMIT}; + if (::sysctl(mib, 2, &vm_overcommit, &vm_sz, NULL, 0) != 0) + vm_overcommit = -1; +#elif defined(__FreeBSD__) || defined(__DragonFly__) + if (::sysctlbyname("vm.overcommit", &vm_overcommit, &vm_sz, NULL, 0) != 0) + vm_overcommit = -1; +#else + vm_overcommit = -1; +#endif + + if ((vm_overcommit & 3) == 0) + { +#ifdef SHM_ANON + ensure(::shm_open(SHM_ANON, O_RDWR, S_IWUSR | S_IRUSR) == 0); +#else + const std::string name = "/rpcs3-mem2-" + 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) + { + fmt::throw_exception("Too many open files. Raise the limit and try again."); + } + + ensure(errno == EEXIST); + } + + ensure(::shm_unlink(name.c_str()) >= 0); +#endif + ensure(::ftruncate(m_file, m_size) >= 0); + return; + } #endif if (!storage.empty())