mirror of
https://github.com/microsoft/mimalloc.git
synced 2025-05-20 14:09:32 +03:00
Check the default huge page size for Linux
In Linux, we can read `/proc/meminfo` to get the huge page size.
This commit is contained in:
parent
076f815cec
commit
4850b55440
1 changed files with 27 additions and 1 deletions
28
src/os.c
28
src/os.c
|
@ -36,6 +36,9 @@ terms of the MIT license. A copy of the license can be found in the file
|
|||
#include <unistd.h> // sysconf
|
||||
#if defined(__linux__)
|
||||
#include <features.h>
|
||||
#include <stdio.h> // sscanf, access
|
||||
#include <fcntl.h> // open
|
||||
#include <stdlib.h> // strtol
|
||||
#if defined(__GLIBC__)
|
||||
#include <linux/mman.h> // 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 <sys/syscall.h> // getcpu
|
||||
#include <stdio.h> // access
|
||||
|
||||
static size_t mi_os_numa_nodex(void) {
|
||||
#ifdef SYS_getcpu
|
||||
|
|
Loading…
Add table
Reference in a new issue