@@ -97,6 +97,11 @@ static size_t os_alloc_granularity = 4096;
97
97
// if non-zero, use large page allocation
98
98
static size_t large_os_page_size = 0 ;
99
99
100
+ #if defined(MADV_HUGEPAGE )
101
+ // only linux supports the THP's notion.
102
+ static bool os_transparent_huge_pages = false;
103
+ #endif
104
+
100
105
// is memory overcommit allowed?
101
106
// set dynamically in _mi_os_init (and if true we use MAP_NORESERVE)
102
107
static bool os_overcommit = true;
@@ -237,13 +242,27 @@ void _mi_os_init() {
237
242
238
243
#else // generic unix
239
244
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
+
240
259
static void os_detect_overcommit (void ) {
241
260
#if defined(__linux__ )
242
261
int fd = open ("/proc/sys/vm/overcommit_memory" , O_RDONLY );
243
- if (fd < 0 ) return ;
262
+ if (fd < 0 ) return ;
244
263
char buf [128 ];
245
264
ssize_t nread = read (fd , & buf , sizeof (buf ));
246
- close (fd );
265
+ close (fd );
247
266
// <https://www.kernel.org/doc/Documentation/vm/overcommit-accounting>
248
267
// 0: heuristic overcommit, 1: always overcommit, 2: never overcommit (ignore NORESERVE)
249
268
if (nread >= 1 ) {
@@ -269,6 +288,7 @@ void _mi_os_init() {
269
288
}
270
289
large_os_page_size = 2 * MI_MiB ; // TODO: can we query the OS for this?
271
290
os_detect_overcommit ();
291
+ os_detect_transparent_huge_pages ();
272
292
}
273
293
#endif
274
294
@@ -542,7 +562,7 @@ static void* mi_unix_mmap(void* addr, size_t size, size_t try_alignment, int pro
542
562
// in that case -- in particular for our large regions (in `memory.c`).
543
563
// However, some systems only allow THP if called with explicit `madvise`, so
544
564
// 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 )) {
546
566
if (madvise (p , size , MADV_HUGEPAGE ) == 0 ) {
547
567
* is_large = true; // possibly
548
568
};
0 commit comments