Skip to content

Commit b97dfcb

Browse files
committed
add hugepages cleanup on exit
1 parent 0704760 commit b97dfcb

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/llama-mmap.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,14 @@ static int file_name_offset = 0;
282282
struct llama_mmap::impl {
283283
#ifdef _POSIX_MAPPED_FILES
284284
std::vector<std::pair<size_t, size_t>> mapped_fragments;
285+
#ifdef GGML_NUMA_MIRROR
286+
struct numa_mapping {
287+
void* addr;
288+
size_t size;
289+
std::string path;
290+
};
291+
std::vector<numa_mapping> numa_mappings;
292+
#endif
285293

286294
impl(struct llama_file * file, size_t prefetch, bool numa) {
287295
#ifdef GGML_NUMA_MIRROR
@@ -346,11 +354,9 @@ struct llama_mmap::impl {
346354
if (is_new_mem[node]) {
347355
memset(mm, 0, GGML_MMAP_HUGEPAGESZ);
348356
}
349-
}
350-
if (node == 0) {
351-
addr = (void*)(GGML_MMAP_VIRTUAL_MEMORY_BASE_OFFSET + \
352-
node * GGML_MMAP_VIRTUAL_MEMORY_NUMA_INCREMENT + \
353-
base_address_offset);
357+
358+
// Store mapping info for cleanup
359+
numa_mappings.push_back({mm, GGML_MMAP_HUGEPAGESZ, std::string(path)});
354360
}
355361
}
356362
base_address_offset += i * GGML_MMAP_HUGEPAGESZ;
@@ -457,6 +463,19 @@ struct llama_mmap::impl {
457463
}
458464

459465
~impl() {
466+
#ifdef GGML_NUMA_MIRROR
467+
// Unmap all NUMA hugepage mappings
468+
for (const auto& mapping : numa_mappings) {
469+
if (munmap(mapping.addr, mapping.size)) {
470+
LLAMA_LOG_WARN("warning: failed to munmap NUMA hugepage: %s\n", strerror(errno));
471+
}
472+
// Delete the hugepage file
473+
if (unlink(mapping.path.c_str())) {
474+
LLAMA_LOG_WARN("warning: failed to unlink hugepage file %s: %s\n",
475+
mapping.path.c_str(), strerror(errno));
476+
}
477+
}
478+
#endif
460479
#ifndef GGML_NUMA_MIRROR
461480
for (const auto & frag : mapped_fragments) {
462481
if (munmap((char *) addr + frag.first, frag.second - frag.first)) {

0 commit comments

Comments
 (0)