Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/development/debugging-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,9 @@ Example:
```bash
gdb --args ~/llama.cpp/build-ci-debug/bin/test-tokenizer-0 "~/llama.cpp/tests/../models/ggml-vocab-llama-spm.gguf"
```

## Windows mapped-page retirement

On Windows, `test-mmap-win32` checks working-set residency with `QueryWorkingSetEx`. The fixture faults in file-backed pages, retires only complete pages within a fragment, and checks that live boundary pages remain resident and all file bytes remain readable. Windows retains the mapped view; this test checks residency retirement, not partial view unmapping.

With tests enabled, build the `test-mmap-win32` target and run `ctest --test-dir build -R '^test-mmap-win32$' --output-on-failure` (add `-C Release` for a multi-configuration generator).
44 changes: 30 additions & 14 deletions src/llama-mmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,18 @@ static llama_mmap::ranges ranges_complement(llama_mmap::ranges ranges, size_t li
#endif

struct llama_mmap::impl {
static void align_range(size_t * first, size_t * last, size_t page_size) {
size_t offset_in_page = *first & (page_size - 1);
size_t offset_to_page = offset_in_page == 0 ? 0 : page_size - offset_in_page;
*first += offset_to_page;

*last = *last & ~(page_size - 1);

if (*last <= *first) {
*last = *first;
}
}

#ifdef _POSIX_MAPPED_FILES
std::vector<std::pair<size_t, size_t>> mapped_fragments;

Expand Down Expand Up @@ -528,18 +540,6 @@ struct llama_mmap::impl {
mapped_fragments.emplace_back(0, file->size());
}

static void align_range(size_t * first, size_t * last, size_t page_size) {
size_t offset_in_page = *first & (page_size - 1);
size_t offset_to_page = offset_in_page == 0 ? 0 : page_size - offset_in_page;
*first += offset_to_page;

*last = *last & ~(page_size - 1);

if (*last <= *first) {
*last = *first;
}
}

void unmap_fragment(size_t first, size_t last) {
int page_size = sysconf(_SC_PAGESIZE);
align_range(&first, &last, page_size);
Expand Down Expand Up @@ -636,8 +636,24 @@ struct llama_mmap::impl {
}

void unmap_fragment(size_t first, size_t last) {
GGML_UNUSED(first);
GGML_UNUSED(last);
static const size_t page_size = [] {
SYSTEM_INFO info;
GetSystemInfo(&info);
return static_cast<size_t>(info.dwPageSize);
}();
align_range(&first, &last, page_size);
if (first == last) {
return;
}

// Windows cannot partially unmap a view. Unlocked pages leave the working set
// even when VirtualUnlock reports ERROR_NOT_LOCKED; boundary pages stay live.
if (!VirtualUnlock(static_cast<char *>(addr) + first, last - first)) {
const DWORD error = GetLastError();
if (error != ERROR_NOT_LOCKED) {
LLAMA_LOG_WARN("warning: VirtualUnlock failed: %s\n", llama_format_win_err(error).c_str());
}
}
}

~impl() {
Expand Down
8 changes: 8 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ llama_test(test-tokenizer-0 NAME test-tokenizer-0-qwen35 LABEL behavior
llama_test(test-tokenizer-0 NAME test-tokenizer-0-refact LABEL behavior ARGS ${PROJECT_SOURCE_DIR}/models/ggml-vocab-refact.gguf)
llama_test(test-tokenizer-0 NAME test-tokenizer-0-starcoder LABEL behavior ARGS ${PROJECT_SOURCE_DIR}/models/ggml-vocab-starcoder.gguf)

if (WIN32)
# Exercise the real internal mapping code without exporting its C++ types from llama.dll.
llama_build_and_test(test-mmap-win32.cpp ../src/llama-mmap.cpp ../src/llama-impl.cpp
LABEL behavior LINK ggml)
target_include_directories(test-mmap-win32 PRIVATE ${PROJECT_SOURCE_DIR}/src ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(test-mmap-win32 PRIVATE psapi)
endif()

if (NOT WIN32)
llama_test_cmd(
${CMAKE_CURRENT_SOURCE_DIR}/test-tokenizers-repo.sh
Expand Down
94 changes: 94 additions & 0 deletions tests/test-mmap-win32.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include "llama-mmap.h"

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <psapi.h>

#include <array>
#include <cstdio>
#include <cstring>
#include <exception>
#include <memory>
#include <stdexcept>
#include <vector>

static void require(bool value, const char * message) {
if (!value) {
throw std::runtime_error(message);
}
}

static std::array<bool, 4> resident_pages(void * base, size_t page_size) {
std::array<PSAPI_WORKING_SET_EX_INFORMATION, 4> entries{};
for (size_t i = 0; i < entries.size(); ++i) {
entries[i].VirtualAddress = static_cast<char *>(base) + i * page_size;
}
require(QueryWorkingSetEx(GetCurrentProcess(), entries.data(), sizeof(entries)) != 0,
"QueryWorkingSetEx failed");
std::array<bool, 4> resident{};
for (size_t i = 0; i < entries.size(); ++i) {
resident[i] = entries[i].VirtualAttributes.Valid != 0;
}
return resident;
}

int main() {
try {
SYSTEM_INFO info;
GetSystemInfo(&info);
const size_t page_size = info.dwPageSize;
std::vector<unsigned char> expected(8 * page_size);
for (size_t i = 0; i < expected.size(); ++i) {
expected[i] = static_cast<unsigned char>((i / page_size) * 17 + i % 251);
}
wchar_t directory[MAX_PATH], filename[MAX_PATH];
const DWORD path_size = GetTempPathW(MAX_PATH, directory);
require(path_size > 0 && path_size < MAX_PATH, "GetTempPathW failed");
require(GetTempFileNameW(directory, L"llm", 0, filename) != 0, "GetTempFileNameW failed");
FILE * raw = nullptr;
if (_wfopen_s(&raw, filename, L"w+b") != 0) {
DeleteFileW(filename);
throw std::runtime_error("opening fixture failed");
}
const auto close_temp = [&](FILE * fp) {
std::fclose(fp);
DeleteFileW(filename);
};
std::unique_ptr<FILE, decltype(close_temp)> temp(raw, close_temp);
require(std::fwrite(expected.data(), 1, expected.size(), temp.get()) == expected.size(),
"writing fixture failed");
require(std::fflush(temp.get()) == 0, "flushing fixture failed");
llama_file file(temp.get());
llama_mmap mapping(&file, 0);
const auto * bytes = static_cast<const volatile unsigned char *>(mapping.addr());
for (size_t i = 0; i < expected.size(); i += page_size) {
require(bytes[i] == expected[i], "initial mapped data differs");
}
require(resident_pages(mapping.addr(), page_size) == std::array<bool, 4>{true, true, true, true},
"fixture pages were not faulted in");

// A fragment containing no complete page must not evict adjacent tensor bytes.
mapping.unmap_fragment(page_size / 2, 3 * page_size / 4);
mapping.unmap_fragment(page_size, page_size);
require(resident_pages(mapping.addr(), page_size) == std::array<bool, 4>{true, true, true, true},
"empty/subpage fragment evicted live boundary data");

// Pages 0 and 3 contain live boundary bytes; only complete pages 1 and 2 retire.
mapping.unmap_fragment(page_size / 2, 3 * page_size + page_size / 2);
require(resident_pages(mapping.addr(), page_size) == std::array<bool, 4>{true, false, false, true},
"unused mapped pages remained resident or live boundary pages were evicted");
require(std::memcmp(mapping.addr(), expected.data(), expected.size()) == 0,
"retiring mapped pages changed file data or broke remapping");

std::puts("PASS: unused file pages retired; live boundary pages and full readback preserved");
return 0;
} catch (const std::exception & error) {
std::fprintf(stderr, "FAIL: %s\n", error.what());
return 1;
}
}