Skip to content

Commit eccbff7

Browse files
committed
linux, checking THP current system settings.
1 parent 36edfbc commit eccbff7

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/os.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ static size_t os_alloc_granularity = 4096;
9797
// if non-zero, use large page allocation
9898
static size_t large_os_page_size = 0;
9999

100+
#if defined(MADV_HUGEPAGE)
101+
// only linux supports the THP's notion.
102+
static bool os_transparent_huge_pages = false;
103+
#endif
104+
100105
// is memory overcommit allowed?
101106
// set dynamically in _mi_os_init (and if true we use MAP_NORESERVE)
102107
static bool os_overcommit = true;
@@ -237,13 +242,27 @@ void _mi_os_init() {
237242

238243
#else // generic unix
239244

245+
static void os_detect_transparent_huge_pages(void) {
246+
#if defined(MADV_HUGEPAGE)
247+
int fd = open("/sys/kernel/mm/transparent_hugepage/enabled", O_RDONLY);
248+
if (fd < 0) return;
249+
char buf[128];
250+
ssize_t nread = read(fd, &buf, sizeof(buf));
251+
close(fd);
252+
if (nread >= 1) {
253+
// in some configurations, it can occur (e.g. embedded)
254+
if (!strstr(buf, "[never]")) os_transparent_huge_pages = true;
255+
}
256+
#endif
257+
}
258+
240259
static void os_detect_overcommit(void) {
241260
#if defined(__linux__)
242261
int fd = open("/proc/sys/vm/overcommit_memory", O_RDONLY);
243-
if (fd < 0) return;
262+
if (fd < 0) return;
244263
char buf[128];
245264
ssize_t nread = read(fd, &buf, sizeof(buf));
246-
close(fd);
265+
close(fd);
247266
// <https://www.kernel.org/doc/Documentation/vm/overcommit-accounting>
248267
// 0: heuristic overcommit, 1: always overcommit, 2: never overcommit (ignore NORESERVE)
249268
if (nread >= 1) {
@@ -269,6 +288,7 @@ void _mi_os_init() {
269288
}
270289
large_os_page_size = 2*MI_MiB; // TODO: can we query the OS for this?
271290
os_detect_overcommit();
291+
os_detect_transparent_huge_pages();
272292
}
273293
#endif
274294

@@ -542,7 +562,7 @@ static void* mi_unix_mmap(void* addr, size_t size, size_t try_alignment, int pro
542562
// in that case -- in particular for our large regions (in `memory.c`).
543563
// However, some systems only allow THP if called with explicit `madvise`, so
544564
// when large OS pages are enabled for mimalloc, we call `madvise` anyways.
545-
if (allow_large && use_large_os_page(size, try_alignment)) {
565+
if (os_transparent_huge_pages && allow_large && use_large_os_page(size, try_alignment)) {
546566
if (madvise(p, size, MADV_HUGEPAGE) == 0) {
547567
*is_large = true; // possibly
548568
};

0 commit comments

Comments
 (0)