pinned: back the Linux expert arena with transparent huge pages
Without a hugetlb pool the Linux arena fell back to a plain anonymous mapping with 4 KB pages. The CPU expert pool streams whole experts (about 3 MB each for the 4-5 bit GGUFs) out of that arena, so every expert costs ~750 TLB misses. Map the fallback 2 MiB-aligned and madvise(MADV_HUGEPAGE), so THP in "madvise" mode (the common distro default) backs it. STRATA_NO_LARGEPAGES=1 keeps 4 KB pages, as on the hugetlb path; the log note says which one it got.
This commit is contained in:
1 parent
4175cbcb5b
commit
8ca4e160c2
1 file changed
+20
-2
+20
-2
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user