|
| 1 | +// Copyright (C) 2018-2026 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | + |
| 5 | +#include "memory_prefetch.hpp" |
| 6 | + |
| 7 | +#include <condition_variable> |
| 8 | +#include <cstdint> |
| 9 | +#include <functional> |
| 10 | +#include <future> |
| 11 | +#include <list> |
| 12 | +#include <mutex> |
| 13 | +#include <new> |
| 14 | +#include <stdexcept> |
| 15 | +#include <thread> |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +#include "openvino/util/math_util.hpp" |
| 19 | +#include "openvino/util/memory.hpp" |
| 20 | + |
| 21 | +namespace ov::util { |
| 22 | + |
| 23 | +namespace { |
| 24 | + |
| 25 | +// FIFO job queue feeding the shared page-toucher pool: a small pool of long-lived worker threads |
| 26 | +// is reused across calls instead of spawning/joining threads per prefetch request. Shared by both |
| 27 | +// the Linux and Windows vm_prefetch_async() implementations (see submit_page_toucher_tasks() |
| 28 | +// below). |
| 29 | +class TaskQueue { |
| 30 | +public: |
| 31 | + void push(std::list<std::function<void()>>&& batch) noexcept { |
| 32 | + { |
| 33 | + std::lock_guard lock(m_mutex); |
| 34 | + m_queue.splice(m_queue.end(), batch); |
| 35 | + } |
| 36 | + m_cv.notify_all(); |
| 37 | + } |
| 38 | + |
| 39 | + // Blocks until a job is available, or returns false once the queue is stopped and drained. |
| 40 | + bool wait_and_pop(std::function<void()>& job) noexcept { |
| 41 | + std::unique_lock<std::mutex> lock(m_mutex); |
| 42 | + m_cv.wait(lock, [this] { |
| 43 | + return m_stop || !m_queue.empty(); |
| 44 | + }); |
| 45 | + if (m_queue.empty()) { |
| 46 | + return false; |
| 47 | + } |
| 48 | + job = std::move(m_queue.front()); |
| 49 | + m_queue.pop_front(); |
| 50 | + return true; |
| 51 | + } |
| 52 | + |
| 53 | + void stop() noexcept { |
| 54 | + { |
| 55 | + std::lock_guard<std::mutex> lock(m_mutex); |
| 56 | + m_stop = true; |
| 57 | + } |
| 58 | + m_cv.notify_all(); |
| 59 | + } |
| 60 | + |
| 61 | +private: |
| 62 | + std::mutex m_mutex; |
| 63 | + std::condition_variable m_cv; |
| 64 | + std::list<std::function<void()>> m_queue; |
| 65 | + bool m_stop = false; |
| 66 | +}; |
| 67 | + |
| 68 | +class ThreadPool { |
| 69 | +public: |
| 70 | + static ThreadPool& instance() { |
| 71 | + static ThreadPool pool; |
| 72 | + return pool; |
| 73 | + } |
| 74 | + |
| 75 | + ThreadPool(const ThreadPool&) = delete; |
| 76 | + ThreadPool& operator=(const ThreadPool&) = delete; |
| 77 | + ThreadPool(ThreadPool&&) = delete; |
| 78 | + ThreadPool& operator=(ThreadPool&&) = delete; |
| 79 | + |
| 80 | + std::vector<std::future<void>> submit(std::vector<std::function<void()>>&& jobs) { |
| 81 | + std::vector<std::future<void>> futures; |
| 82 | + futures.reserve(jobs.size()); |
| 83 | + std::list<std::function<void()>> pending; |
| 84 | + for (auto& job : jobs) { |
| 85 | + auto task = std::make_shared<std::packaged_task<void()>>(std::move(job)); |
| 86 | + futures.push_back(task->get_future()); |
| 87 | + pending.emplace_back([task]() { |
| 88 | + (*task)(); |
| 89 | + }); |
| 90 | + } |
| 91 | + m_queue.push(std::move(pending)); |
| 92 | + return futures; |
| 93 | + } |
| 94 | + |
| 95 | +private: |
| 96 | + ThreadPool() { |
| 97 | + const auto workers_count = |
| 98 | + std::max<size_t>(1, std::min<size_t>(max_prefetch_threads, std::thread::hardware_concurrency())); |
| 99 | + m_workers.reserve(workers_count); |
| 100 | + for (size_t i = 0; i < workers_count; ++i) { |
| 101 | + m_workers.emplace_back([this]() { |
| 102 | + worker_loop(); |
| 103 | + }); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + ~ThreadPool() { |
| 108 | + m_queue.stop(); |
| 109 | + for (auto& worker : m_workers) { |
| 110 | + if (worker.joinable()) { |
| 111 | + worker.join(); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + void worker_loop() noexcept { |
| 117 | + std::function<void()> job; |
| 118 | + while (m_queue.wait_and_pop(job)) { |
| 119 | + job(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + TaskQueue m_queue; |
| 124 | + std::vector<std::thread> m_workers; |
| 125 | +}; |
| 126 | + |
| 127 | +} // namespace |
| 128 | + |
| 129 | +std::vector<std::future<void>> submit_page_toucher_tasks(void* ptr, size_t size, size_t num_threads) noexcept { |
| 130 | + try { |
| 131 | + const auto page_size = static_cast<size_t>(get_system_page_size()); |
| 132 | + const auto chunk_size = |
| 133 | + std::max<size_t>(align_size_up(size / num_threads, page_size), default_parallel_io_min_chunk); |
| 134 | + |
| 135 | + std::vector<std::function<void()>> jobs; |
| 136 | + jobs.reserve(ceil_div(size, chunk_size)); |
| 137 | + |
| 138 | + for (auto first = reinterpret_cast<const uint8_t*>(ptr), last = first + size; first < last; |
| 139 | + first += chunk_size) { |
| 140 | + jobs.emplace_back(PageToucher{first, std::min(first + chunk_size, last), page_size}); |
| 141 | + } |
| 142 | + return ThreadPool::instance().submit(std::move(jobs)); |
| 143 | + } catch (const std::bad_alloc&) { |
| 144 | + // Job/future/packaged_task allocation failed under memory pressure. |
| 145 | + return {}; |
| 146 | + } catch (const std::length_error&) { |
| 147 | + // vector::reserve()'s requested capacity exceeded max_size() (e.g. a pathological |
| 148 | + // ptr/size/num_threads combination producing an absurd chunk count). |
| 149 | + return {}; |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +} // namespace ov::util |
0 commit comments