diff --git a/src/core/pinned.cu b/src/core/pinned.cu index 780d93fb..0927c6b1 100644 --- a/src/core/pinned.cu +++ b/src/core/pinned.cu @@ -224,9 +224,27 @@ void* reserve(uint64_t bytes, PageBacking& got, std::string& note, const std::st note = "MAP_HUGETLB unavailable (needed " + std::to_string(need) + " 2 MiB pages, vm.nr_hugepages=" + (have_pool ? std::to_string(pool) : std::string("?")) + "); using 4 KB pages"; } - void* p = mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + // No hugetlb mapping: a 2 MiB-aligned anonymous mapping with MADV_HUGEPAGE, so transparent huge pages back + // it where THP is "madvise" (the common distro default). The CPU expert pool streams whole experts out of + // this arena; with 4 KB pages every 3 MB expert costs ~750 TLB misses. + constexpr uint64_t kAlign = 2ull << 20; + const uint64_t padded = bytes + kAlign; + void* raw = mmap(nullptr, padded, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); got = PageBacking::NormalPages; - return p == MAP_FAILED ? nullptr : p; + if (raw == MAP_FAILED) return nullptr; + const uintptr_t start = (uintptr_t) raw; + const uintptr_t aligned = (start + kAlign - 1) & ~(uintptr_t) (kAlign - 1); + if (aligned > start) munmap(raw, aligned - start); + const uintptr_t end = aligned + bytes, raw_end = start + padded; + if (raw_end > end) munmap((void*) end, raw_end - end); + void* p = (void*) aligned; + if (std::getenv("STRATA_NO_LARGEPAGES") == nullptr && madvise(p, bytes, MADV_HUGEPAGE) == 0) { + const std::string four_k = "; using 4 KB pages"; + if (note.size() >= four_k.size() && note.compare(note.size() - four_k.size(), four_k.size(), four_k) == 0) + note.resize(note.size() - four_k.size()); + note += "; transparent huge pages requested (MADV_HUGEPAGE)"; + } + return p; #endif }