From 4850b554400011f8102d071875f032aabc8ea777 Mon Sep 17 00:00:00 2001 From: hank Date: Sun, 27 Jun 2021 23:01:57 +0800 Subject: [PATCH] Check the default huge page size for Linux In Linux, we can read `/proc/meminfo` to get the huge page size. --- src/os.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/os.c b/src/os.c index 85415232d..661537010 100644 --- a/src/os.c +++ b/src/os.c @@ -36,6 +36,9 @@ terms of the MIT license. A copy of the license can be found in the file #include // sysconf #if defined(__linux__) #include +#include // sscanf, access +#include // open +#include // strtol #if defined(__GLIBC__) #include // linux mmap flags #else @@ -221,7 +224,31 @@ void _mi_os_init() { os_page_size = (size_t)result; os_alloc_granularity = os_page_size; } +#if defined(__linux__) + // try to get large page size from OS + int fd = open("/proc/meminfo", O_RDONLY); + if (fd != -1) { + char buffer[4096]; + buffer[read(fd, buffer, sizeof(buffer) - 1)] = 0; + close(fd); + char* large_size = strstr(buffer, "Hugepagesize:"); + if (large_size == NULL) { + // huge pages are not available + large_os_page_size = 0; + } + else { + large_size += 13; // this is the length of "Hugepagesize:" + large_os_page_size = strtol(large_size, NULL, 0); + large_os_page_size *= KiB; + } + } + else { + // or fall back + large_os_page_size = 2*MiB; + } +#else large_os_page_size = 2*MiB; // TODO: can we query the OS for this? +#endif } #endif @@ -1170,7 +1197,6 @@ static size_t mi_os_numa_node_countx(void) { } #elif defined(__linux__) #include // getcpu -#include // access static size_t mi_os_numa_nodex(void) { #ifdef SYS_getcpu