Skip to content

Commit a8b28c0

Browse files
committed
Check the default huge page size for Linux
In Linux, we can read `/proc/meminfo` to get the huge page size.
1 parent 076f815 commit a8b28c0

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/os.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ terms of the MIT license. A copy of the license can be found in the file
3636
#include <unistd.h> // sysconf
3737
#if defined(__linux__)
3838
#include <features.h>
39+
#include <stdio.h> // sscanf, access
40+
#include <fcntl.h> // open
3941
#if defined(__GLIBC__)
4042
#include <linux/mman.h> // linux mmap flags
4143
#else
@@ -221,7 +223,31 @@ void _mi_os_init() {
221223
os_page_size = (size_t)result;
222224
os_alloc_granularity = os_page_size;
223225
}
226+
#if defined(__linux__)
227+
// try to get large page size from OS
228+
int fd = open("/proc/meminfo", O_RDONLY);
229+
if (fd != -1) {
230+
char buffer[4096];
231+
buffer[read(fd, buffer, sizeof(buffer) - 1)] = 0;
232+
close(fd);
233+
char* large_size = strstr(buffer, "Hugepagesize:");
234+
if (large_size == NULL) {
235+
// huge pages are not available
236+
large_os_page_size = 0;
237+
}
238+
else {
239+
large_size += 13; // this is the length of "Hugepagesize:"
240+
large_os_page_size = strtol(large_size, NULL, 0);
241+
large_os_page_size *= KiB;
242+
}
243+
}
244+
else {
245+
// or fall back
246+
large_os_page_size = 2*MiB;
247+
}
248+
#else
224249
large_os_page_size = 2*MiB; // TODO: can we query the OS for this?
250+
#endif
225251
}
226252
#endif
227253

@@ -1170,7 +1196,6 @@ static size_t mi_os_numa_node_countx(void) {
11701196
}
11711197
#elif defined(__linux__)
11721198
#include <sys/syscall.h> // getcpu
1173-
#include <stdio.h> // access
11741199

11751200
static size_t mi_os_numa_nodex(void) {
11761201
#ifdef SYS_getcpu

0 commit comments

Comments
 (0)