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] Fast read from files utils (#36379)
Pawel Raasz0127054b3d19
diff --git a/src/common/util/include/openvino/util/file_util.hpp b/src/common/util/include/openvino/util/file_util.hppindex 8dd3a124571..05a6a8e990f 100644--- a/src/common/util/include/openvino/util/file_util.hpp+++ b/src/common/util/include/openvino/util/file_util.hpp@@ -17,6 +17,16 @@#include "openvino/util/util.hpp"#include "openvino/util/wstring_convert_util.hpp"+namespace ov {+#ifdef _WIN32+// Windows uses HANDLE (void*) for file handles+using FileHandle = void*;+#else+// Linux/Unix uses int for file descriptors+using FileHandle = int;+#endif+} // namespace ov+namespace ov::util {inline std::filesystem::path library_prefix() {diff --git a/src/common/util/include/openvino/util/io.hpp b/src/common/util/include/openvino/util/io.hppnew file mode 100644index 00000000000..299b1e640f1--- /dev/null+++ b/src/common/util/include/openvino/util/io.hpp@@ -0,0 +1,65 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++/**+ * @brief Platform-agnostic I/O utilities.+ * @file io.hpp+ */++#pragma once++#include <cstddef>+#include <system_error>++#include "openvino/util/file_util.hpp"++namespace ov::util {++/**+ * @brief Populate a mapped memory region using the native async I/O backend (or synchronous fallback).+ *+ * Splits the target range into at most @p queue_depth chunks and populates them in parallel.+ * When the native async I/O ring is unavailable, a synchronous OS memory hint is issued instead.+ *+ * @param ptr Base address of the mapped region (as returned by the OS mapping call or @ref load_mmap_object).+ * @param size Number of bytes to populate.+ * @param offset Byte offset within the mapping to start from. Default: 0.+ * @param queue_depth Maximum number of parallel population operations. Default: 128.+ * @return @c true on success or after issuing the advisory hint.+ * @c false only if @p ptr is @c nullptr or @p size is @c 0.+ *+ * @code+ * auto mm = ov::util::load_mmap_object("model.bin");+ *+ * // Populate the entire mapping:+ * ov::util::io_populate_mmap(mm->data(), mm->size());+ * @endcode+ */+bool io_populate_mmap(void* ptr, size_t size, size_t offset = 0, size_t queue_depth = 128) noexcept;++/**+ * @brief Read a region from an open file directly into any destination buffer.+ *+ * Transfers [@p file_offset, @p file_offset + @p size) from @p handle into @p dst using the native async I/O backend+ * or the thread-pool fallback.+ *+ * The caller retains ownership of @p handle; it is not closed by this function.+ * If data is already in memory, use @c memcpy instead.+ *+ * The destination can be any writable buffer: heap memory, device memory, staging buffers, etc. No memory-mapped pages+ * are touched, so the resident-set size of any existing mapping of the same file is unchanged.+ *+ * @param handle Open, readable platform file handle.+ * @param dst Destination buffer. Must hold at least @p size bytes.+ * @param file_offset Byte offset within the file to start reading.+ * @param size Number of bytes to read.+ * @param queue_depth Submission-queue depth for the async I/O backend (ignored on the fallback path).+ * @return An empty @c std::error_code on success; a non-zero code describing the failure otherwise.+ */+std::error_code io_read_into(FileHandle handle,+ void* dst,+ size_t file_offset,+ size_t size,+ size_t queue_depth = 128) noexcept;+} // namespace ov::utildiff --git a/src/common/util/include/openvino/util/memory.hpp b/src/common/util/include/openvino/util/memory.hppindex d578af6c94d..82f23392cc2 100644--- a/src/common/util/include/openvino/util/memory.hpp+++ b/src/common/util/include/openvino/util/memory.hpp@@ -111,4 +111,17 @@ void vm_decommit(void* ptr, size_t size) noexcept;*/void vm_release(void* ptr, size_t size) noexcept;+/**+ * @brief Pre-fetch a committed VM range into physical memory.+ *+ * Works with both anonymous (@ref vm_commit) and file-backed (mmap) regions.+ *+ * @param ptr Base address of the range. Must be page-aligned.+ * @param size Number of bytes to pre-fetch. Must be a multiple of the system page size.+ * @param num_threads Strategy selector:+ * - @c 0 (default) → OS advisory hint (async, low overhead).+ * - @c N >= 1 → parallel touch with N threads (synchronous).+ */+void vm_prefetch(void* ptr, size_t size, size_t num_threads = 0) noexcept;+} // namespace ov::utildiff --git a/src/common/util/include/openvino/util/mmap_object.hpp b/src/common/util/include/openvino/util/mmap_object.hppindex ce0bc69e1c8..56e0567ac7f 100644--- a/src/common/util/include/openvino/util/mmap_object.hpp+++ b/src/common/util/include/openvino/util/mmap_object.hpp@@ -15,19 +15,14 @@#include <memory>#include <string>+#include "openvino/util/file_util.hpp"+namespace ov {namespace util {int64_t get_system_page_size();} // namespace util-#ifdef _WIN32-// Windows uses HANDLE (void*) for file handles-using FileHandle = void*;-#else-// Linux/Unix uses int for file descriptors-using FileHandle = int;-#endif/*** @brief Generic constant to indicate automatic size calculation is required.*/diff --git a/src/common/util/src/os/lin/lin_io.cpp b/src/common/util/src/os/lin/lin_io.cppnew file mode 100644index 00000000000..2f18c9d321b--- /dev/null+++ b/src/common/util/src/os/lin/lin_io.cpp@@ -0,0 +1,22 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "openvino/util/io.hpp"++namespace ov::util {++bool io_populate_mmap(void* /*ptr*/, size_t /*size*/, size_t /*offset*/, size_t /*queue_depth*/) noexcept {+ // CVS-186700+ return false;+}++std::error_code io_read_into(FileHandle /*handle*/,+ void* /*dst*/,+ size_t /*file_offset*/,+ size_t /*size*/,+ size_t /*queue_depth*/) noexcept {+ // CVS-186707+ return std::make_error_code(std::errc::function_not_supported);+}+} // namespace ov::utildiff --git a/src/common/util/src/os/lin/lin_memory.cpp b/src/common/util/src/os/lin/lin_memory.cppindex df92832c405..4cc7221323a 100644--- a/src/common/util/src/os/lin/lin_memory.cpp+++ b/src/common/util/src/os/lin/lin_memory.cpp@@ -4,17 +4,61 @@#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 {+namespace {++void madvise_hint(void* ptr, size_t size) noexcept {+ madvise(ptr, size, MADV_SEQUENTIAL);+ 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* aligned_alloc(size_t size, size_t alignment) noexcept {if (alignment == 0) {alignment = alignof(std::max_align_t);@@ -27,13 +71,14 @@ void aligned_free(void* ptr) noexcept {}void* vm_reserve(size_t size, std::error_code& ec) noexcept {- const auto p = mmap(nullptr, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);- if (p == MAP_FAILED) {+ void* result = mmap(nullptr, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);+ if (result != MAP_FAILED) {+ ec = {};+ } else {ec = std::error_code(errno, std::system_category());- return nullptr;+ result = nullptr;}- ec = {};- return p;+ return result;}void vm_commit(void* ptr, size_t size, std::error_code& ec) noexcept {@@ -62,4 +107,17 @@ void vm_release(void* ptr, size_t size) noexcept {std::ignore = munmap(ptr, size);}+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.+ populate_pages(ptr, size, num_threads);+ }+}+} // namespace ov::utildiff --git a/src/common/util/src/os/lin/lin_mmap_object.cpp b/src/common/util/src/os/lin/lin_mmap_object.cppindex 76739faec45..6ce1185a13c 100644--- a/src/common/util/src/os/lin/lin_mmap_object.cpp+++ b/src/common/util/src/os/lin/lin_mmap_object.cpp@@ -2,7 +2,6 @@// SPDX-License-Identifier: Apache-2.0//-#include <dlfcn.h>#include <fcntl.h>#include <sys/mman.h>#include <sys/stat.h>@@ -11,8 +10,6 @@#include <algorithm>#include <atomic>#include <cstring>-#include <iostream>-#include <sstream>#include <thread>#include <tuple>@@ -61,53 +58,6 @@ 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 {@@ -222,14 +172,12 @@ 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);+ constexpr size_t one_mb = 1024 * 1024;+ // Below 4 MiB the overhead of spawning threads exceeds the benefit; skip.+ if (const auto region = util::make_madvise_region(m_data, m_size, offset, size); region.m_length > 4 * one_mb) {+ const auto num_threads = std::min<size_t>(10, std::thread::hardware_concurrency());+ const auto aligned_size = util::align_size_up(region.m_length, util::get_system_page_size());+ util::vm_prefetch(reinterpret_cast<void*>(region.m_address), aligned_size, num_threads);}}};diff --git a/src/common/util/src/os/win/win_io.cpp b/src/common/util/src/os/win/win_io.cppnew file mode 100644index 00000000000..0e4d39127f5--- /dev/null+++ b/src/common/util/src/os/win/win_io.cpp@@ -0,0 +1,18 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "openvino/util/io.hpp"++namespace ov::util {++bool io_populate_mmap(void* /*ptr*/, size_t /*size*/, size_t /*offset*/, size_t /*queue_depth*/) noexcept {+ // CVS-186700+ return false;+}++std::error_code io_read_into(FileHandle /*handle*/, void* /*dst*/, size_t /*file_offset*/, size_t /*size*/, size_t /*queue_depth*/) noexcept {+ // CVS-186707+ return std::make_error_code(std::errc::function_not_supported);+}+} // namespace ov::utildiff --git a/src/common/util/src/os/win/win_memory.cpp b/src/common/util/src/os/win/win_memory.cppindex 8d03e1d7873..31743222666 100644--- a/src/common/util/src/os/win/win_memory.cpp+++ b/src/common/util/src/os/win/win_memory.cpp@@ -57,4 +57,16 @@ void vm_release(void* ptr, size_t) noexcept {std::ignore = VirtualFree(ptr, 0, MEM_RELEASE);}+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+ } else {+ // Option 2: parallel synchronous prefault & touch+ }+}+} // namespace ov::util