Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[Core] Add parallel mapping population on Windows (#36586)

Alicja Miloszewskaf8fa4c196b84
diff --git a/src/common/util/CMakeLists.txt b/src/common/util/CMakeLists.txt
index c73bdf489bd..da7305dd89b 100644
--- a/src/common/util/CMakeLists.txt
+++ b/src/common/util/CMakeLists.txt
@@ -69,6 +69,7 @@ target_sources(${TARGET_NAME}
${CMAKE_CURRENT_SOURCE_DIR}/src/file_path.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/file_util.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/log.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/memory.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/native_stream.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/native_streambuf.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/parallel_read_streambuf.cpp
diff --git a/src/common/util/include/openvino/util/memory.hpp b/src/common/util/include/openvino/util/memory.hpp
index 7bf3cdbdedd..351ca2df028 100644
--- a/src/common/util/include/openvino/util/memory.hpp
+++ b/src/common/util/include/openvino/util/memory.hpp
@@ -12,6 +12,9 @@
namespace ov::util {
+/** @brief One mebibyte (1024 * 1024 bytes). */
+inline constexpr size_t one_mib = 1024 * 1024;
+
/** @brief Minimum guaranteed page alignment on all supported platforms (x86, ARM, RISC-V). */
inline constexpr size_t min_page_alignment = 4096;
diff --git a/src/common/util/src/memory.cpp b/src/common/util/src/memory.cpp
new file mode 100644
index 00000000000..8ad385947e0
--- /dev/null
+++ b/src/common/util/src/memory.cpp
@@ -0,0 +1,57 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/util/memory.hpp"
+
+#include <algorithm>
+#include <cassert>
+#include <cstdint>
+#include <thread>
+#include <vector>
+
+#include "openvino/util/common_util.hpp"
+#include "openvino/util/mmap_object.hpp"
+
+namespace ov::util {
+void populate_pages(void* ptr, size_t size, size_t num_threads) noexcept;
+
+namespace {
+
+/**
+ * @brief Functor that touches one byte per page over [m_begin, m_end) to force the pages resident.
+ *
+ * The volatile accumulator prevents the compiler from optimizing the read loop away.
+ */
+struct PageToucher {
+ const uint8_t* m_begin;
+ const uint8_t* m_end;
+ const size_t m_page_size;
+
+ void operator()() const noexcept {
+ volatile uint8_t local = 0; // prevents the compiler from optimizing the loop away
+ for (auto begin = m_begin; begin < m_end; begin += m_page_size) {
+ local += *begin;
+ }
+ }
+};
+
+} // namespace
+
+void populate_pages(void* ptr, size_t size, size_t num_threads) noexcept {
+ // ptr and size are guaranteed page-aligned by vm_prefetch's precondition.
+ const auto page_size = static_cast<size_t>(get_system_page_size());
+ const auto chunk_size = std::max<size_t>(align_size_up(size / num_threads, page_size), one_mib);
+
+ std::vector<std::thread> threads;
+ threads.reserve(ceil_div(size, chunk_size));
+
+ for (auto first = reinterpret_cast<const uint8_t*>(ptr), last = first + size; first < last; first += chunk_size) {
+ threads.emplace_back(PageToucher{first, std::min(first + chunk_size, last), page_size});
+ }
+ for (auto& t : threads) {
+ t.join();
+ }
+}
+
+} // namespace ov::util
diff --git a/src/common/util/src/os/lin/lin_memory.cpp b/src/common/util/src/os/lin/lin_memory.cpp
index 4cc7221323a..6ad74c2c6d7 100644
--- a/src/common/util/src/os/lin/lin_memory.cpp
+++ b/src/common/util/src/os/lin/lin_memory.cpp
@@ -4,20 +4,14 @@
#include <sys/mman.h>
-#include <algorithm>
#include <cassert>
#include <cerrno>
#include <cstddef>
-#include <cstdint>
#include <cstdlib>
#include <cstring>
-#include <thread>
#include <tuple>
-#include <vector>
-#include "openvino/util/common_util.hpp"
#include "openvino/util/memory.hpp"
-#include "openvino/util/mmap_object.hpp"
namespace ov::util {
@@ -28,37 +22,10 @@ void madvise_hint(void* ptr, size_t size) noexcept {
madvise(ptr, size, MADV_WILLNEED);
}
-struct PageToucher {
- const uint8_t* m_begin;
- const uint8_t* m_end;
- const size_t m_page_size;
-
- void operator()() const noexcept {
- volatile uint8_t local = 0; // prevents the compiler from optimizing the loop away
- for (auto begin = m_begin; begin < m_end; begin += m_page_size) {
- local += *begin;
- }
- }
-};
-
-void populate_pages(void* ptr, size_t size, size_t num_threads) noexcept {
- // ptr and size are guaranteed page-aligned by vm_prefetch's precondition.
- const auto page_size = static_cast<size_t>(get_system_page_size());
- const auto chunk_size = std::max<size_t>(align_size_up(size / num_threads, page_size), 1024 * 1024);
-
- std::vector<std::thread> threads;
- threads.reserve(ceil_div(size, chunk_size));
-
- for (auto first = reinterpret_cast<const uint8_t*>(ptr), last = first + size; first < last; first += chunk_size) {
- threads.emplace_back(PageToucher{first, std::min(first + chunk_size, last), page_size});
- }
- for (auto& t : threads) {
- t.join();
- }
-}
-
} // namespace
+void populate_pages(void* ptr, size_t size, size_t num_threads) noexcept;
+
void* aligned_alloc(size_t size, size_t alignment) noexcept {
if (alignment == 0) {
alignment = alignof(std::max_align_t);
@@ -109,13 +76,10 @@ void vm_release(void* ptr, size_t size) noexcept {
void vm_prefetch(void* ptr, size_t size, size_t num_threads) noexcept {
assert(ptr != nullptr && size > 0);
- // assert if region is not mmap-backed.
-
if (num_threads == 0) {
- // Option 1: OS advisory hints — async, low overhead.
madvise_hint(ptr, size);
} else {
- // Option 2: parallel synchronous touch — blocks until every page is resident.
+ // blocks until every page has been faulted in.
populate_pages(ptr, size, num_threads);
}
}
diff --git a/src/common/util/src/os/win/win_memory.cpp b/src/common/util/src/os/win/win_memory.cpp
index 31743222666..f0b5d7cdd68 100644
--- a/src/common/util/src/os/win/win_memory.cpp
+++ b/src/common/util/src/os/win/win_memory.cpp
@@ -18,6 +18,8 @@
namespace ov::util {
+void populate_pages(void* ptr, size_t size, size_t num_threads) noexcept;
+
void* aligned_alloc(size_t size, size_t alignment) noexcept {
if (alignment == 0) {
alignment = alignof(std::max_align_t);
@@ -59,13 +61,12 @@ void vm_release(void* ptr, size_t) noexcept {
void vm_prefetch(void* ptr, size_t size, size_t num_threads) noexcept {
assert(ptr != nullptr && size > 0);
- // CVS-186579
- // assert if region is not mmap-baked.
-
if (num_threads == 0) {
- // Option 1: OS advisory hints
+ WIN32_MEMORY_RANGE_ENTRY entry{ptr, size};
+ ::PrefetchVirtualMemory(::GetCurrentProcess(), 1, &entry, 0);
} else {
- // Option 2: parallel synchronous prefault & touch
+ // blocks until every page has been faulted in.
+ populate_pages(ptr, size, num_threads);
}
}
diff --git a/src/common/util/src/os/win/win_mmap_object.cpp b/src/common/util/src/os/win/win_mmap_object.cpp
index 6cb7cf23437..12f854d89a0 100644
--- a/src/common/util/src/os/win/win_mmap_object.cpp
+++ b/src/common/util/src/os/win/win_mmap_object.cpp
@@ -8,6 +8,7 @@
#include <mutex>
#include <shared_mutex>
#include <stdexcept>
+#include <thread>
#include <vector>
#include "openvino/util/common_util.hpp"
@@ -302,7 +303,7 @@ public:
void hint_evict(size_t offset, size_t size) noexcept override;
- void hint_prefetch(size_t /*offset*/, size_t /*size*/) override {}
+ void hint_prefetch(size_t offset, size_t size) override;
private:
/**
@@ -743,6 +744,33 @@ bool MapHolder::try_remap_slot(uintptr_t fault_addr) {
return remap_placeholder(proc, placeholder_base, placeholder_size);
}
+namespace {
+
+// Clamps [offset, offset + size) to [0, mapping_size) and page-aligns the result.
+// Returns an empty region (m_length == 0) for a null/empty mapping, an offset at or past the end,
+// or a sub-page request.
+util::AlignedRegion clamp_align_region(const void* data, size_t mapping_size, size_t offset, size_t size) noexcept {
+ const auto page_size = static_cast<size_t>(util::get_system_page_size());
+ if (data == nullptr || mapping_size == 0 || offset >= mapping_size || size < page_size) {
+ return {};
+ }
+ const auto available = mapping_size - offset;
+ const auto raw_len = (size == auto_size) ? available : std::min(size, available);
+ return util::align_region(reinterpret_cast<uintptr_t>(data) + offset, raw_len, page_size);
+}
+
+} // namespace
+
+void MapHolder::hint_prefetch(size_t offset, size_t size) {
+ // Below 4 MiB the overhead of spawning threads exceeds the benefit; skip.
+ if (const auto region = clamp_align_region(m_data, m_size, offset, size); region.m_length > 4 * util::one_mib) {
+ const auto num_threads = std::min<size_t>(10, std::thread::hardware_concurrency());
+ const auto aligned_size =
+ util::align_size_up(region.m_length, static_cast<size_t>(util::get_system_page_size()));
+ util::vm_prefetch(reinterpret_cast<void*>(region.m_address), aligned_size, num_threads);
+ }
+}
+
std::pair<char*, char*> MapHolder::compute_evict_range(size_t offset, size_t size) const noexcept {
// Require placeholder path, API availability, and a live VEH (eviction without VEH
// would leave inaccessible placeholders on the next access).
diff --git a/src/core/tests/file_load_benchmark.cpp b/src/core/tests/file_load_benchmark.cpp
index a7aa95f48f3..3c983276d79 100644
--- a/src/core/tests/file_load_benchmark.cpp
+++ b/src/core/tests/file_load_benchmark.cpp
@@ -3,7 +3,6 @@
//
#include <gtest/gtest.h>
-#include <unistd.h>
#include <algorithm>
#include <cerrno>
@@ -17,15 +16,23 @@
#include <iostream>
#include <numeric>
#include <string>
+#include <string_view>
+#include <thread>
#include <tuple>
#include <vector>
#include "openvino/util/file_util.hpp"
+#include "openvino/util/memory.hpp"
#include "openvino/util/mmap_object.hpp"
#ifdef __linux__
# include <fcntl.h>
# include <sys/mman.h>
+# include <unistd.h>
+#elif defined(_WIN32)
+# define WIN32_LEAN_AND_MEAN
+# define NOMINMAX
+# include <windows.h>
#endif
#include "common_test_utils/common_utils.hpp"
@@ -34,27 +41,53 @@
namespace ov::test {
namespace {
-const size_t page_size = static_cast<size_t>(::sysconf(_SC_PAGESIZE));
+const size_t page_size = static_cast<size_t>(util::get_system_page_size());
+
+#ifdef __linux__
+// mlock forces every page resident before returning; munlock releases the pin without evicting.
+// Bounded by RLIMIT_MEMLOCK -- no limit on a privileged process.
+void ensure_memory_resident(const std::shared_ptr<ov::MappedMemory>& mapped) {
+ ASSERT_EQ(::mlock(mapped->data(), mapped->size()), 0)
+ << "mlock failed (errno=" << errno << "); check RLIMIT_MEMLOCK";
+ ::munlock(mapped->data(), mapped->size());
+}
+
+#else
+// VirtualLock forces pages resident; VirtualUnlock releases the pin without evicting.
+// The lock is bounded by the working-set quota, so grow it to cover the region first.
+void ensure_memory_resident(const std::shared_ptr<ov::MappedMemory>& mapped) {
+ const size_t need = mapped->size() + 64 * util::one_mib; // headroom for code/stack
+ SIZE_T min_ws = 0, max_ws = 0;
+ if (GetProcessWorkingSetSize(GetCurrentProcess(), &min_ws, &max_ws) && max_ws < need) {
+ SetProcessWorkingSetSize(GetCurrentProcess(), need, need);
+ }
+ ASSERT_NE(VirtualLock(mapped->data(), mapped->size()), 0)
+ << "VirtualLock failed (GetLastError=" << GetLastError() << ")";
+ VirtualUnlock(mapped->data(), mapped->size());
+}
+#endif
struct TestFile {
- size_t size_mb;
- size_t size_bytes;
+ size_t size_mib;
std::filesystem::path path;
+
+ size_t size_bytes() const {
+ return size_mib * util::one_mib;
+ }
};
std::filesystem::path generate_test_file(const TestFile& tf) {
- auto path = std::filesystem::path("test_file" + std::to_string(tf.size_mb) + "mb.bin");
+ auto path = std::filesystem::path("test_file" + std::to_string(tf.size_mib) + "mib.bin");
- if (util::file_exists(path) && std::filesystem::file_size(path) == tf.size_bytes) {
- std::cout << "Test file already exists: " << path << ", skipping generation." << std::endl;
+ if (util::file_exists(path) && std::filesystem::file_size(path) == tf.size_bytes()) {
return path;
}
- std::vector<uint8_t> chunk(1024 * 1024);
+ std::vector<uint8_t> chunk(util::one_mib);
for (size_t i = 0; i < chunk.size(); ++i)
chunk[i] = static_cast<uint8_t>(i % 251);
std::ofstream f(path, std::ios::binary);
- for (size_t written = 0; written < tf.size_bytes; written += chunk.size()) {
- const auto to_write = std::min(chunk.size(), tf.size_bytes - written);
+ for (size_t written = 0; written < tf.size_bytes(); written += chunk.size()) {
+ const auto to_write = std::min(chunk.size(), tf.size_bytes() - written);
f.write(reinterpret_cast<const char*>(chunk.data()), static_cast<std::streamsize>(to_write));
}
return path;
@@ -68,6 +101,15 @@ long long measure_ms(const std::function<void()>& fn) {
}
void evict_cache(const std::filesystem::path& path, size_t file_size) {
+ static bool warned = false;
+ auto warn_once = [](std::string_view msg) {
+ if (!warned) {
+ std::cout << "[WARNING] " << msg << " Results may be unreliable." << std::endl;
+ warned = true;
+ }
+ };
+
+#ifdef __linux__
// Prefer /proc/sys/vm/drop_caches (requires root / CAP_SYS_ADMIN, available when the container
// is started with --privileged). Writing "3" flushes the host page
// cache, dentries, and inodes — the only fully reliable way to guarantee a cold-cache run.
@@ -79,13 +121,7 @@ void evict_cache(const std::filesystem::path& path, size_t file_size) {
return;
}
- static bool warned = false;
- if (!warned) {
- std::cout << "[WARNING] No access to /proc/sys/vm/drop_caches, falling back to "
- "posix_fadvise(DONTNEED). Results may be unreliable (kernel can ignore the hint)."
- << std::endl;
- warned = true;
- }
+ warn_once("No access to /proc/sys/vm/drop_caches, falling back to posix_fadvise(DONTNEED).");
// Fallback: best-effort fadvise.
int fd = ::open(path.c_str(), O_RDONLY);
@@ -93,6 +129,45 @@ void evict_cache(const std::filesystem::path& path, size_t file_size) {
posix_fadvise(fd, 0, static_cast<off_t>(file_size), POSIX_FADV_DONTNEED);
::close(fd);
}
+#elif defined(_WIN32)
+ // Windows moves evicted file pages to the standby list ("Cached" in Task Manager). Purging it is
+ // the equivalent of `drop_caches 1`: NtSetSystemInformation(SystemMemoryListInformation,
+ // MemoryPurgeStandbyList) clears it. Requires SeProfileSingleProcessPrivilege (run elevated).
+ // Both mmap and read paths start cold once the standby list is empty.
+ (void)path;
+ (void)file_size;
+
+ enum SYSTEM_MEMORY_LIST_COMMAND { MemoryPurgeStandbyList = 4 };
+ constexpr int SystemMemoryListInformation = 80;
+ using NtSetSystemInformation_t = LONG(WINAPI*)(int, PVOID, ULONG);
+
+ // Acquire the privilege required to purge the standby list.
+ HANDLE token = nullptr;
+ if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token)) {
+ TOKEN_PRIVILEGES tp{};
+ tp.PrivilegeCount = 1;
+ tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
+ LookupPrivilegeValue(nullptr, SE_PROF_SINGLE_PROCESS_NAME, &tp.Privileges[0].Luid);
+ AdjustTokenPrivileges(token, FALSE, &tp, sizeof(tp), nullptr, nullptr);
+ CloseHandle(token);
+ }
+
+ auto ntdll = GetModuleHandleW(L"ntdll.dll");
+ auto nt_set =
+ ntdll ? reinterpret_cast<NtSetSystemInformation_t>(GetProcAddress(ntdll, "NtSetSystemInformation")) : nullptr;
+ if (!nt_set) {
+ warn_once("NtSetSystemInformation unavailable; cannot purge standby list.");
+ return;
+ }
+ int command = MemoryPurgeStandbyList;
+ if (nt_set(SystemMemoryListInformation, &command, sizeof(command)) != 0) {
+ warn_once("Standby-list purge failed (run elevated for cold-cache benchmarking).");
+ }
+#else
+ (void)path;
+ (void)file_size;
+ warn_once("No cache eviction strategy available on this platform.");
+#endif
}
long long bench(const std::function<void()>& fn,
@@ -112,42 +187,34 @@ long long bench(const std::function<void()>& fn,
return total / measured_runs;
}
-double throughput_mbs(size_t size_mb, long long ms) {
+double throughput_mibs(size_t size_mib, long long ms) {
if (ms <= 0)
return 0.0;
- return static_cast<double>(size_mb) * 1000.0 / static_cast<double>(ms);
+ return static_cast<double>(size_mib) * 1000.0 / static_cast<double>(ms);
}
namespace strategy {
-// mlock forces every page resident before returning; munlock releases the pin without evicting.
-// Bounded by RLIMIT_MEMLOCK -- no limit on a privileged process.
-void mlock_munlock(const std::shared_ptr<ov::MappedMemory>& mapped) {
- ASSERT_EQ(::mlock(mapped->data(), mapped->size()), 0)
- << "mlock failed (errno=" << errno << "); check RLIMIT_MEMLOCK";
- ::munlock(mapped->data(), mapped->size());
-}
-
// Note: the mmap destructor (munmap + close) runs inside the timed window;
-void mmap_prefetch_mlock(const std::filesystem::path& path, size_t /*file_size*/) {
+void sync_vm_prefetch_mem_lock(const std::filesystem::path& path, size_t /*file_size*/) {
auto mapped = load_mmap_object(path);
- mapped->hint_prefetch(); // synchronous for regions > 4 MiB (parallel touch + join)
- mlock_munlock(mapped); // should be near no-op and just lock/unlock resident pages
+ util::vm_prefetch(mapped->data(), mapped->size(), std::thread::hardware_concurrency());
+ ensure_memory_resident(mapped); // should be near no-op and just lock/unlock resident pages
}
-void mmap_touch_mlock(const std::filesystem::path& path, size_t /*file_size*/) {
+void loop_touch_mem_lock(const std::filesystem::path& path, size_t /*file_size*/) {
auto mapped = load_mmap_object(path);
volatile uint8_t sink = 0;
for (auto first = mapped->data(), last = first + mapped->size(); first < last; first += page_size) {
sink += *first;
}
- mlock_munlock(mapped); // should be near no-op and just lock/unlock resident pages
+ ensure_memory_resident(mapped); // should be near no-op and just lock/unlock resident pages
}
-void mmap_prefetch_then_memcpy(const std::filesystem::path& path, size_t file_size) {
+void parallel_loop_sync_then_memcpy(const std::filesystem::path& path, size_t file_size) {
auto mapped = load_mmap_object(path);
- mapped->hint_prefetch();
- constexpr size_t chunk_size = 128 * 1024 * 1024; // 128 MB chunks
+ util::vm_prefetch(mapped->data(), mapped->size(), std::thread::hardware_concurrency());
+ constexpr size_t chunk_size = 128 * util::one_mib;
std::vector<char> buffer(std::min(chunk_size, file_size));
volatile char sink = 0;
for (size_t offset = 0; offset < file_size; offset += chunk_size) {
@@ -159,7 +226,7 @@ void mmap_prefetch_then_memcpy(const std::filesystem::path& path, size_t file_si
void mmap_then_memcpy(const std::filesystem::path& path, size_t file_size) {
auto mapped = load_mmap_object(path);
- constexpr size_t chunk_size = 128 * 1024 * 1024; // 128 MB chunks
+ constexpr size_t chunk_size = 128 * util::one_mib;
std::vector<char> buffer(std::min(chunk_size, file_size));
volatile char sink = 0;
for (size_t offset = 0; offset < file_size; offset += chunk_size) {
@@ -185,7 +252,7 @@ void mmap_prefetch_then_memcpy_partial(const std::filesystem::path& path,
auto mapped = load_mmap_object(path);
mapped->hint_prefetch(offset, size);
const auto total_copy_size = std::min(size, mapped->size() - offset);
- constexpr size_t chunk_size = 128 * 1024 * 1024; // 128 MB chunks
+ constexpr size_t chunk_size = 128 * util::one_mib;
std::vector<char> buffer(std::min(chunk_size, total_copy_size));
volatile char sink = 0;
for (size_t i = 0; i < total_copy_size; i += chunk_size) {
@@ -205,22 +272,22 @@ void mmap_prefetch_then_memcpy_partial(const std::filesystem::path& path,
class FileLoadBenchmark : public ::testing::Test {};
TEST_F(FileLoadBenchmark, strategies_read_memcpy) {
- const std::vector<size_t> sizes_mb = {10, 100, 500, 1000};
+ const std::vector<size_t> sizes_mib = {10, 100, 500, 1000};
constexpr int warmup = 0;
constexpr int runs = 3;
// Generate all test files
std::vector<TestFile> files;
- for (size_t mb : sizes_mb) {
- TestFile tf{mb, mb * 1024 * 1024, {}};
+ for (size_t mib : sizes_mib) {
+ TestFile tf{mib, {}};
tf.path = generate_test_file(tf);
- evict_cache(tf.path, tf.size_bytes);
+ evict_cache(tf.path, tf.size_bytes());
files.push_back(tf);
}
// Collect results: [file_idx] -> {mmap_prefetch_memcpy, mmap_memcpy, ifstream}
struct Row {
- size_t mb;
+ size_t mib;
long long t_hint_prefetch;
long long t_no_prefault;
long long t_ifstream;
@@ -229,70 +296,59 @@ TEST_F(FileLoadBenchmark, strategies_read_memcpy) {
for (const auto& tf : files) {
Row r{};
- r.mb = tf.size_mb;
+ r.mib = tf.size_mib;
r.t_ifstream = bench(
[&]() {
- strategy::ifstream_read(tf.path, tf.size_bytes);
+ strategy::ifstream_read(tf.path, tf.size_bytes());
},
tf.path,
- tf.size_bytes,
+ tf.size_bytes(),
warmup,
runs);
r.t_no_prefault = bench(
[&]() {
- strategy::mmap_then_memcpy(tf.path, tf.size_bytes);
+ strategy::mmap_then_memcpy(tf.path, tf.size_bytes());
},
tf.path,
- tf.size_bytes,
+ tf.size_bytes(),
warmup,
runs);
r.t_hint_prefetch = bench(
[&]() {
- strategy::mmap_prefetch_then_memcpy(tf.path, tf.size_bytes);
+ strategy::parallel_loop_sync_then_memcpy(tf.path, tf.size_bytes());
},
tf.path,
- tf.size_bytes,
+ tf.size_bytes(),
warmup,
runs);
results.push_back(r);
}
printf("\n--- Latency (ms, mean of %d runs, cold cache) ---\n", runs);
- printf("%-10s | %17s | %13s | %13s\n", "Size (MB)", "prefetch+memcpy", "mmap+memcpy", "ifstream");
- printf("%-10s-|-%17s-|-%13s-|-%13s\n", "----------", "-----------------", "-------------", "-------------");
+ printf("%-10s | %18s | %13s | %13s\n", "Size (MiB)", "parallel loop sync", "default mmap", "ifstream");
+ printf("%-10s-|-%18s-|-%13s-|-%13s\n", "----------", "------------------", "-------------", "-------------");
for (const auto& r : results) {
- printf("%-10zu | %14lld ms | %10lld ms | %10lld ms\n", r.mb, r.t_hint_prefetch, r.t_no_prefault, r.t_ifstream);
- }
-
- printf("\n--- Throughput (MB/s) ---\n");
- printf("%-10s | %17s | %13s | %13s\n", "Size (MB)", "prefetch+memcpy", "mmap+memcpy", "ifstream");
- printf("%-10s-|-%17s-|-%13s-|-%13s\n", "----------", "-----------------", "-------------", "-------------");
- for (const auto& r : results) {
- printf("%-10zu | %12.0f MB/s | %8.0f MB/s | %8.0f MB/s\n",
- r.mb,
- throughput_mbs(r.mb, r.t_hint_prefetch),
- throughput_mbs(r.mb, r.t_no_prefault),
- throughput_mbs(r.mb, r.t_ifstream));
+ printf("%-10zu | %15lld ms | %10lld ms | %10lld ms\n", r.mib, r.t_hint_prefetch, r.t_no_prefault, r.t_ifstream);
}
}
-TEST_F(FileLoadBenchmark, strategies_mlock) {
- const std::vector<size_t> sizes_mb = {10, 100, 500, 1000};
+TEST_F(FileLoadBenchmark, test_speed_load_data_into_mmap_region) {
+ const std::vector<size_t> sizes_mib = {10, 100, 500, 1000};
constexpr int warmup = 0;
constexpr int runs = 3;
// Generate all test files
std::vector<TestFile> files;
- for (size_t mb : sizes_mb) {
- TestFile tf{mb, mb * 1024 * 1024, {}};
+ for (size_t mib : sizes_mib) {
+ TestFile tf{mib, {}};
tf.path = generate_test_file(tf);
- evict_cache(tf.path, tf.size_bytes);
+ evict_cache(tf.path, tf.size_bytes());
files.push_back(tf);
}
- // Collect results: [file_idx] -> {mmap_prefetch_mlock, mmap_touch_mlock}
+ // Collect results: [file_idx] -> {mmap_vm_prefetch_mem_lock, loop_touch_mem_lock}
struct Row {
- size_t mb;
+ size_t mib;
long long t_prefetch_mlock;
long long t_mlock;
};
@@ -300,70 +356,72 @@ TEST_F(FileLoadBenchmark, strategies_mlock) {
for (const auto& tf : files) {
Row r{};
- r.mb = tf.size_mb;
+ r.mib = tf.size_mib;
r.t_mlock = bench(
[&]() {
- strategy::mmap_touch_mlock(tf.path, tf.size_bytes);
+ strategy::loop_touch_mem_lock(tf.path, tf.size_bytes());
},
tf.path,
- tf.size_bytes,
+ tf.size_bytes(),
warmup,
runs);
r.t_prefetch_mlock = bench(
[&]() {
- strategy::mmap_prefetch_mlock(tf.path, tf.size_bytes);
+ strategy::sync_vm_prefetch_mem_lock(tf.path, tf.size_bytes());
},
tf.path,
- tf.size_bytes,
+ tf.size_bytes(),
warmup,
runs);
results.push_back(r);
}
printf("\n--- Latency (ms, mean of %d runs, cold cache) ---\n", runs);
- printf("%-10s | %17s | %13s\n", "Size (MB)", "prefetch+mlock", "mmap+mlock");
- printf("%-10s-|-%17s-|-%13s\n", "----------", "-----------------", "-------------");
+ printf("%-10s | %18s | %13s\n", "Size (MiB)", "parallel loop sync", "loop touch");
+ printf("%-10s-|-%18s-|-%13s\n", "----------", "------------------", "-------------");
for (const auto& r : results) {
- printf("%-10zu | %14lld ms | %10lld ms\n", r.mb, r.t_prefetch_mlock, r.t_mlock);
+ printf("%-10zu | %15lld ms | %10lld ms\n", r.mib, r.t_prefetch_mlock, r.t_mlock);
}
- printf("\n--- Throughput (MB/s) ---\n");
- printf("%-10s | %17s | %13s\n", "Size (MB)", "prefetch+mlock", "mmap+mlock");
- printf("%-10s-|-%17s-|-%13s\n", "----------", "-----------------", "-------------");
+ printf("\n--- Throughput (MiB/s) ---\n");
+ printf("%-10s | %18s | %13s\n", "Size (MiB)", "parallel loop sync", "loop touch");
+ printf("%-10s-|-%18s-|-%13s\n", "----------", "------------------", "-------------");
+
for (const auto& r : results) {
- printf("%-10zu | %12.0f MB/s | %8.0f MB/s\n",
- r.mb,
- throughput_mbs(r.mb, r.t_prefetch_mlock),
- throughput_mbs(r.mb, r.t_mlock));
+ printf("%-10zu | %13.0f MiB/s | %8.0f MiB/s\n",
+ r.mib,
+ throughput_mibs(r.mib, r.t_prefetch_mlock),
+ throughput_mibs(r.mib, r.t_mlock));
}
}
TEST_F(FileLoadBenchmark, hint_prefetch_with_offset_table) {
- constexpr size_t file_size_mb = 1200;
+ constexpr size_t file_size_mib = 1200;
constexpr int warmup = 0;
constexpr int runs = 3;
- TestFile tf{file_size_mb, file_size_mb * 1024 * 1024, {}};
+ TestFile tf{file_size_mib, {}};
tf.path = generate_test_file(tf);
- evict_cache(tf.path, tf.size_bytes); // Flush dirty pages from file generation
+ evict_cache(tf.path, tf.size_bytes()); // Flush dirty pages from file generation
- const std::vector<size_t> offsets_mb = {0, 1, 17, 500, 700, 800};
- const std::vector<size_t> region_sizes_mb = {10, 100, 500};
+ const std::vector<size_t> offsets_mib = {0, 1, 17, 500, 700, 800};
+ const std::vector<size_t> region_sizes_mib = {10, 100, 500};
// Pre-compute all results[size_idx][offset_idx]; -1 means "exceeds"
- std::vector<std::vector<long long>> results(region_sizes_mb.size(), std::vector<long long>(offsets_mb.size(), -1));
- for (size_t si = 0; si < region_sizes_mb.size(); ++si) {
- for (size_t oi = 0; oi < offsets_mb.size(); ++oi) {
- const size_t off_bytes = offsets_mb[oi] * 1024 * 1024;
- const size_t sz_bytes = region_sizes_mb[si] * 1024 * 1024;
- if (off_bytes + sz_bytes > tf.size_bytes)
+ std::vector<std::vector<long long>> results(region_sizes_mib.size(),
+ std::vector<long long>(offsets_mib.size(), -1));
+ for (size_t si = 0; si < region_sizes_mib.size(); ++si) {
+ for (size_t oi = 0; oi < offsets_mib.size(); ++oi) {
+ const size_t off_bytes = offsets_mib[oi] * util::one_mib;
+ const size_t sz_bytes = region_sizes_mib[si] * util::one_mib;
+ if (off_bytes + sz_bytes > tf.size_bytes())
continue;
results[si][oi] = bench(
[&]() {
- strategy::mmap_prefetch_then_memcpy_partial(tf.path, tf.size_bytes, off_bytes, sz_bytes);
+ strategy::mmap_prefetch_then_memcpy_partial(tf.path, tf.size_bytes(), off_bytes, sz_bytes);
},
tf.path,
- tf.size_bytes,
+ tf.size_bytes(),
warmup,
runs);
}
@@ -372,13 +430,13 @@ TEST_F(FileLoadBenchmark, hint_prefetch_with_offset_table) {
// Print: sizes as rows, offsets as columns
printf("\n--- partial prefault: hint_prefetch with offset ---\n");
printf(" %-14s", "size \\ offset");
- for (size_t off_mb : offsets_mb)
- printf(" | %7zu MB", off_mb);
- printf("\n %s\n", std::string(14 + offsets_mb.size() * 14, '-').c_str());
+ for (size_t off_mib : offsets_mib)
+ printf(" | %7zu MiB", off_mib);
+ printf("\n %s\n", std::string(14 + offsets_mib.size() * 14, '-').c_str());
- for (size_t si = 0; si < region_sizes_mb.size(); ++si) {
- printf(" %-14zu", region_sizes_mb[si]);
- for (size_t oi = 0; oi < offsets_mb.size(); ++oi) {
+ for (size_t si = 0; si < region_sizes_mib.size(); ++si) {
+ printf(" %-14zu", region_sizes_mib[si]);
+ for (size_t oi = 0; oi < offsets_mib.size(); ++oi) {
if (results[si][oi] < 0)
printf(" | (exceeds)");
else
diff --git a/src/tests/test_utils/common_test_utils/tests/memory_test.cpp b/src/tests/test_utils/common_test_utils/tests/memory_test.cpp
index 1db1ae773cc..461ded65cf4 100644
--- a/src/tests/test_utils/common_test_utils/tests/memory_test.cpp
+++ b/src/tests/test_utils/common_test_utils/tests/memory_test.cpp
@@ -5,8 +5,14 @@
#include <gtest/gtest.h>
#include <cstdint>
+#include <filesystem>
+#include <fstream>
+#include <system_error>
+#include <vector>
+#include "common_test_utils/common_utils.hpp"
#include "openvino/util/memory.hpp"
+#include "openvino/util/mmap_object.hpp"
namespace ov::test {
@@ -147,4 +153,40 @@ TEST_F(AlignedAllocTest, free_nullptr_is_noop) {
EXPECT_NO_FATAL_FAILURE(util::aligned_free(nullptr));
}
+class VmPrefetchMappedFileTest : public testing::TestWithParam<size_t> {
+protected:
+ std::filesystem::path m_file_path;
+
+ void TearDown() override {
+ std::filesystem::remove(m_file_path);
+ }
+};
+
+TEST_P(VmPrefetchMappedFileTest, prefetch_faults_in_mapped_file_and_preserves_data) {
+ const size_t num_threads = GetParam();
+ const size_t size = 64 * util::min_page_alignment;
+
+ std::vector<uint8_t> expected(size);
+ for (size_t i = 0; i < size; ++i)
+ expected[i] = static_cast<uint8_t>(i % 251);
+
+ m_file_path = utils::generateTestFilePrefix() + "_vm_prefetch.bin";
+ {
+ std::ofstream f(m_file_path, std::ios::binary);
+ f.write(reinterpret_cast<const char*>(expected.data()), expected.size());
+ }
+
+ auto mapped = load_mmap_object(m_file_path);
+ ASSERT_NE(mapped, nullptr);
+ ASSERT_EQ(mapped->size(), size);
+
+ EXPECT_NO_FATAL_FAILURE(util::vm_prefetch(mapped->data(), mapped->size(), num_threads));
+
+ const std::vector<uint8_t> actual(reinterpret_cast<uint8_t*>(mapped->data()),
+ reinterpret_cast<uint8_t*>(mapped->data()) + mapped->size());
+ EXPECT_EQ(actual, expected);
+}
+
+INSTANTIATE_TEST_SUITE_P(NumThreads, VmPrefetchMappedFileTest, testing::Values(0u, 5u, 10u));
+
} // namespace ov::test