Skip to content

Commit

Permalink
Check the default huge page size for Linux
Browse files Browse the repository at this point in the history
In Linux, we can read `/proc/meminfo` to get the huge page size.
  • Loading branch information
hankluo6 committed Jun 27, 2021
1 parent 076f815 commit 4850b55
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/os.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4850b55

Please sign in to comment.