OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[CORE] parallel population for mmaped buffers (#35870)
Alicja Miloszewskaa20c360cc100
diff --git a/src/common/util/include/openvino/util/mmap_object.hpp b/src/common/util/include/openvino/util/mmap_object.hppindex fbc37fe3724..ce0bc69e1c8 100644--- a/src/common/util/include/openvino/util/mmap_object.hpp+++ b/src/common/util/include/openvino/util/mmap_object.hpp@@ -45,6 +45,14 @@ public:virtual uint64_t get_id() const noexcept = 0;virtual ~MappedMemory() = default;virtual void hint_evict(size_t offset = 0, size_t size = auto_size) noexcept = 0;+ /**+ * @brief Hint that the given region of the mapping will be accessed soon.+ *+ * @param offset Offset within the mapping where prefetching starts.+ * @param size Number of bytes to prefetch. Defaults to the rest of the+ * mapping when set to auto_size.+ */+ virtual void hint_prefetch(size_t offset = 0, size_t size = auto_size) = 0;};/**diff --git a/src/common/util/src/os/lin/lin_mmap_object.cpp b/src/common/util/src/os/lin/lin_mmap_object.cppindex bf60c7b9bc3..76739faec45 100644--- a/src/common/util/src/os/lin/lin_mmap_object.cpp+++ b/src/common/util/src/os/lin/lin_mmap_object.cpp@@ -9,9 +9,11 @@#include <unistd.h>#include <algorithm>+#include <atomic>#include <cstring>#include <iostream>#include <sstream>+#include <thread>#include <tuple>#include "openvino/util/common_util.hpp"@@ -59,6 +61,53 @@ inline util::AlignedRegion make_madvise_region(const void* data, size_t mapping_}} // namespace util+namespace {+/**+ * @brief Touches memory pages in parallel to trigger page faults and populate the page cache.+ *+ * Spawns worker threads that each read one byte per page in their assigned range.+ * * No-op if the region length is below the prefault threshold. Below that threshold the overhead+ * of spawning threads exceeds the benefit.+ *+ * @param region Page-aligned memory region to prefault.+ * @param prefault_threshold Minimum region length in bytes to trigger parallel prefaulting (default 4 MiB).+ */+void populate_pages(const util::AlignedRegion& region, size_t prefault_threshold = 4 * 1024 * 1024) {+ if (region.m_length < prefault_threshold)+ return;++ const auto page = static_cast<size_t>(util::get_system_page_size());+ const size_t pages = (region.m_length + page - 1) / page;++ const size_t hw_threads = std::thread::hardware_concurrency();+ constexpr size_t min_chunk_size = 1 * 1024 * 1024; // 1 MiB per thread minimum+ constexpr size_t max_prefault_threads = 10;+ const size_t num_threads =+ std::min({hw_threads, pages, max_prefault_threads, std::max<size_t>(1, region.m_length / min_chunk_size)});++ std::vector<std::thread> threads;+ const auto base = reinterpret_cast<const uint8_t*>(region.m_address);++ for (size_t tid = 0; tid < num_threads; ++tid) {+ threads.emplace_back([&, tid] {+ const size_t begin_page = pages * tid / num_threads;+ const size_t end_page = pages * (tid + 1) / num_threads;+ volatile uint8_t local = 0; // prevents compiler from optimizing the loop away as a no-op++ for (size_t p = begin_page; p < end_page; ++p) {+ const size_t off = p * page;+ if (off < region.m_length) {+ local += base[off];+ }+ }+ });+ }+ for (auto& t : threads) {+ t.join();+ }+}+} // namespace+class HandleHolder {int m_handle = -1;void reset() noexcept {@@ -171,6 +220,18 @@ public:}}}++ void hint_prefetch(size_t offset, size_t size) override {+ if (m_data == nullptr || offset >= m_size) {+ return;+ }++ if (const auto region = util::make_madvise_region(m_data, m_size, offset, size); region.m_length > 0) {+ std::ignore = madvise(reinterpret_cast<void*>(region.m_address), region.m_length, MADV_SEQUENTIAL);+ std::ignore = madvise(reinterpret_cast<void*>(region.m_address), region.m_length, MADV_WILLNEED);+ ov::populate_pages(region);+ }+ }};std::shared_ptr<MappedMemory> load_mmap_object(const std::filesystem::path& path,diff --git a/src/common/util/src/os/win/win_mmap_object.cpp b/src/common/util/src/os/win/win_mmap_object.cppindex a35416410c5..beb9a0030aa 100644--- a/src/common/util/src/os/win/win_mmap_object.cpp+++ b/src/common/util/src/os/win/win_mmap_object.cpp@@ -302,6 +302,8 @@ public:void hint_evict(size_t offset, size_t size) noexcept override;+ void hint_prefetch(size_t /*offset*/, size_t /*size*/) override {}+private:/*** @brief Remaps a placeholder region by replacing it with a file-backed view.diff --git a/src/core/tests/mmap_object.cpp b/src/core/tests/mmap_object.cppindex af51ba317b2..ec4dfb3eadb 100644--- a/src/core/tests/mmap_object.cpp+++ b/src/core/tests/mmap_object.cpp@@ -9,6 +9,7 @@#include <algorithm>#include <filesystem>#include <iostream>+#include <numeric>#include <sstream>#include "common_test_utils/common_utils.hpp"@@ -349,4 +350,174 @@ TEST_F(HintEvictTest, evict_with_nonzero_offset_matches_original) {EXPECT_EQ(read_mapped(*mm), expected_slice);}+class HintPrefetchTest : public ::testing::Test {+protected:+ std::filesystem::path m_file_path;++ void TearDown() override {+ std::filesystem::remove(m_file_path);+ }++ static std::vector<uint8_t> read_mapped(MappedMemory& mm) {+ return {reinterpret_cast<uint8_t*>(mm.data()), reinterpret_cast<uint8_t*>(mm.data()) + mm.size()};+ }+};++TEST_F(HintPrefetchTest, parallel_prefault_whole_file) {+ m_file_path = std::filesystem::path(utils::generateTestFilePrefix() + "_prefault_test.bin");+ constexpr size_t file_size = 5 * 1024 * 1024; // 5 MiB (above 4 MiB threshold)+ std::vector<uint8_t> data(file_size);+ for (size_t i = 0; i < file_size; ++i)+ data[i] = static_cast<uint8_t>(i % 251);++ {+ std::ofstream f(m_file_path, std::ios::binary);+ f.write(reinterpret_cast<const char*>(data.data()), data.size());+ }++ {+ auto mapped = load_mmap_object(m_file_path);+ ASSERT_NE(mapped, nullptr);+ EXPECT_EQ(mapped->size(), file_size);++ EXPECT_NO_THROW(mapped->hint_prefetch());++ EXPECT_EQ(read_mapped(*mapped), data);+ }+}++TEST_F(HintPrefetchTest, parallel_prefault_partial_region) {+ m_file_path = std::filesystem::path(utils::generateTestFilePrefix() + "_prefault_partial.bin");+ constexpr size_t file_size = 8 * 1024 * 1024; // 8 MB+ constexpr size_t prefault_offset = 1 * 1024 * 1024;+ constexpr size_t prefault_size = 5 * 1024 * 1024;+ std::vector<uint8_t> data(file_size);+ for (size_t i = 0; i < file_size; ++i)+ data[i] = static_cast<uint8_t>(i % 251);++ {+ std::ofstream f(m_file_path, std::ios::binary);+ f.write(reinterpret_cast<const char*>(data.data()), data.size());+ }++ {+ auto mapped = load_mmap_object(m_file_path);+ ASSERT_NE(mapped, nullptr);++ EXPECT_NO_THROW(mapped->hint_prefetch(prefault_offset, prefault_size));++ EXPECT_EQ(read_mapped(*mapped), data);+ }+}++TEST_F(HintPrefetchTest, parallel_prefault_below_threshold_is_noop) {+ m_file_path = std::filesystem::path(utils::generateTestFilePrefix() + "_prefault_small.bin");+ constexpr size_t file_size = 1024; // 1 KB - below threshold+ std::vector<uint8_t> data(file_size);+ for (size_t i = 0; i < file_size; ++i)+ data[i] = static_cast<uint8_t>(i % 251);++ {+ std::ofstream f(m_file_path, std::ios::binary);+ f.write(reinterpret_cast<const char*>(data.data()), data.size());+ }++ {+ auto mapped = load_mmap_object(m_file_path);+ ASSERT_NE(mapped, nullptr);+ EXPECT_NO_THROW(mapped->hint_prefetch()); // no optimization+ EXPECT_EQ(read_mapped(*mapped), data);+ }+}++TEST_F(HintPrefetchTest, parallel_prefault_with_file_offset) {+ m_file_path = std::filesystem::path(utils::generateTestFilePrefix() + "_prefault_offset.bin");+ constexpr size_t file_size = 10 * 1024 * 1024; // 10 MB+ constexpr size_t map_offset = 2 * 1024 * 1024; // Map starting at 2 MB into file+ std::vector<uint8_t> data(file_size);+ for (size_t i = 0; i < file_size; ++i)+ data[i] = static_cast<uint8_t>(i % 251);++ {+ std::ofstream f(m_file_path, std::ios::binary);+ f.write(reinterpret_cast<const char*>(data.data()), data.size());+ }++ {+ auto mapped = load_mmap_object(m_file_path, map_offset);+ ASSERT_NE(mapped, nullptr);+ EXPECT_EQ(mapped->size(), file_size - map_offset);++ EXPECT_NO_THROW(mapped->hint_prefetch());++ EXPECT_EQ(read_mapped(*mapped), std::vector<uint8_t>(data.begin() + map_offset, data.end()));+ }+}++TEST_F(HintPrefetchTest, hint_prefetch_with_both_offsets) {+ m_file_path = std::filesystem::path(utils::generateTestFilePrefix() + "_prefault_both_offsets.bin");+ constexpr size_t file_size = 12 * 1024 * 1024; // 12 MB+ constexpr size_t map_offset = 2 * 1024 * 1024; // Map starting at 2 MB into file+ constexpr size_t pop_offset = 1 * 1024 * 1024; // Populate starting at 1 MB into mapping+ constexpr size_t pop_size = 5 * 1024 * 1024; // Populate 5 MB+ std::vector<uint8_t> data(file_size);+ for (size_t i = 0; i < file_size; ++i)+ data[i] = static_cast<uint8_t>(i % 251);++ {+ std::ofstream f(m_file_path, std::ios::binary);+ f.write(reinterpret_cast<const char*>(data.data()), data.size());+ }++ {+ auto mapped = load_mmap_object(m_file_path, map_offset);+ ASSERT_NE(mapped, nullptr);+ EXPECT_EQ(mapped->size(), file_size - map_offset);++ EXPECT_NO_THROW(mapped->hint_prefetch(pop_offset, pop_size));++ EXPECT_EQ(read_mapped(*mapped), std::vector<uint8_t>(data.begin() + map_offset, data.end()));+ }+}++// Investigates whether calling hint_prefetch(offset, size) and POSIX_FADV_SEQUENTIAL+// on a subregion of an already-cached file evicts pages *outside* that region+TEST_F(HintPrefetchTest, hint_prefetch_sequential_eviction_check) {+#ifndef __linux__+ GTEST_SKIP() << "utils::count_resident_pages is not implemented on this platform yet CVS-186579";+#endif+ constexpr size_t file_size = 128 * 1024 * 1024;++ constexpr size_t prefetch_offset = 80 * 1024 * 1024;+ constexpr size_t prefetch_size = 16 * 1024 * 1024;++ constexpr size_t prefix_mb = 64;+ constexpr size_t prefix_size = prefix_mb * 1024 * 1024;++ const size_t page = static_cast<size_t>(util::get_system_page_size());+ const size_t total_prefix_pages = prefix_size / page;++ m_file_path = std::filesystem::path(utils::generateTestFilePrefix() + "_file.bin");+ {+ std::vector<uint8_t> data(file_size);+ for (size_t i = 0; i < file_size; ++i)+ data[i] = static_cast<uint8_t>(i % 251);+ std::ofstream f(m_file_path, std::ios::binary);+ f.write(reinterpret_cast<const char*>(data.data()), data.size());+ }++ auto mapped = load_mmap_object(m_file_path);+ volatile char sink = 0;+ for (size_t i = 0; i < prefix_size; i += page) {+ sink += mapped->data()[i];+ }+ const size_t pages_before = utils::count_resident_pages(mapped->data(), prefix_size);+ ASSERT_EQ(pages_before, total_prefix_pages)+ << "Expected all " << total_prefix_pages << " prefix pages resident after warmup, but found " << pages_before;++ mapped->hint_prefetch(prefetch_offset, prefetch_size);+ const size_t pages_after = utils::count_resident_pages(mapped->data(), prefix_size);+ EXPECT_EQ(pages_after, pages_before) << "hint_prefetch evicted pages.";+}+} // namespace ov::testdiff --git a/src/core/tests/shared_buffer.cpp b/src/core/tests/shared_buffer.cppindex 3bfa6620891..89a26bd2b6c 100644--- a/src/core/tests/shared_buffer.cpp+++ b/src/core/tests/shared_buffer.cpp@@ -539,6 +539,8 @@ public:MOCK_METHOD(void, hint_evict_mock, (size_t offset, size_t size));+ MOCK_METHOD(void, hint_prefetch, (size_t offset, size_t size), (override));+private:std::vector<char> m_data;uint64_t m_id;diff --git a/src/tests/test_utils/common_test_utils/include/common_test_utils/common_utils.hpp b/src/tests/test_utils/common_test_utils/include/common_test_utils/common_utils.hppindex e166c9463aa..9d8b25c9410 100644--- a/src/tests/test_utils/common_test_utils/include/common_test_utils/common_utils.hpp+++ b/src/tests/test_utils/common_test_utils/include/common_test_utils/common_utils.hpp@@ -214,6 +214,9 @@ std::string generateTestFilePrefix();size_t getVmSizeInKB();size_t getVmRSSInKB();++size_t count_resident_pages(const void* data, size_t size);+} // namespace utils} // namespace test} // namespace ovdiff --git a/src/tests/test_utils/common_test_utils/src/common_utils.cpp b/src/tests/test_utils/common_test_utils/src/common_utils.cppindex 161ce2f4acd..9dfd043d820 100644--- a/src/tests/test_utils/common_test_utils/src/common_utils.cpp+++ b/src/tests/test_utils/common_test_utils/src/common_utils.cpp@@ -19,7 +19,10 @@# include "psapi.h"#else+# include <sys/mman.h> // mincore# include <unistd.h>++# include <cstdint>#endifnamespace ov {@@ -78,6 +81,10 @@ size_t getVmRSSInKB() {return getMemoryInfo().WorkingSetSize / 1024;}+size_t count_resident_pages(const void* data, size_t size) {+ throw std::runtime_error("count_resident_pages is not implemented on Windows");+}+#elsesize_t getSystemDataByName(char* name) {@@ -119,6 +126,29 @@ size_t getVmRSSInKB() {return getSystemDataByName(const_cast<char*>("VmRSS:"));}+// Returns the number of pages in [data, data+size) that are resident in the page cache.+// Uses mincore(2). Returns 0 if mincore fails (region not mapped or other error).+size_t count_resident_pages(const void* data, size_t size) {+ if (!data || size == 0)+ return 0;+ const size_t page = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));+ const auto base_addr = reinterpret_cast<uintptr_t>(data);+ const auto aligned = (base_addr / page) * page;+ const auto gap = base_addr - aligned;+ const size_t aligned_size = size + gap;+ const size_t num_pages = (aligned_size + page - 1) / page;+ std::vector<unsigned char> vec(num_pages, 0);+# ifdef __linux__+ if (mincore(reinterpret_cast<void*>(aligned), aligned_size, vec.data()) != 0)+# else+ if (mincore(reinterpret_cast<void*>(aligned), aligned_size, reinterpret_cast<char*>(vec.data())) != 0)+# endif+ return 0;+ return static_cast<size_t>(std::count_if(vec.begin(), vec.end(), [](unsigned char v) {+ return (v & 1) != 0;+ }));+}+#endif} // namespace utils} // namespace test