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] Set mmap object id properly (#34849)
Tomasz Jankowski1ac866450273
diff --git a/src/common/util/include/openvino/util/common_util.hpp b/src/common/util/include/openvino/util/common_util.hppindex 2a473a07c87..9c8265678e4 100644--- a/src/common/util/include/openvino/util/common_util.hpp+++ b/src/common/util/include/openvino/util/common_util.hpp@@ -94,6 +94,14 @@ constexpr uint64_t u64_hash_combine(uint64_t h, uint64_t k) {return h + 0xe6546b64;}+constexpr uint64_t u64_hash_combine(std::initializer_list<uint64_t>&& values) {+ uint64_t h = 0;+ for (uint64_t k : values) {+ h = u64_hash_combine(h, k);+ }+ return h;+}+/*** @brief trim from start (in place)* @param s - string to trimdiff --git a/src/common/util/src/os/lin/lin_mmap_object.cpp b/src/common/util/src/os/lin/lin_mmap_object.cppindex 62c63a52245..b54c88610f4 100644--- a/src/common/util/src/os/lin/lin_mmap_object.cpp+++ b/src/common/util/src/os/lin/lin_mmap_object.cpp@@ -81,14 +81,10 @@ public:" for mapping. Ensure that file exists and has appropriate permissions.");}set_from_fd(fd, offset, size);- m_id = util::u64_hash_combine(offset, size);- m_id = util::u64_hash_combine(std::hash<std::filesystem::path::string_type>{}(path.native()), m_id);+ m_id = util::u64_hash_combine({std::hash<std::filesystem::path::string_type>{}(path.native()), offset, size});}void set_from_fd(const int fd, const size_t offset, const size_t size) {- if (fd == -1) {- throw std::runtime_error("Invalid file descriptor provided for mapping.");- }m_handle = HandleHolder(fd);struct stat sb = {};@@ -112,6 +108,8 @@ public:}m_data = static_cast<char*>(m_mapped_view) + (offset - aligned_offset);}+ m_id =+ util::u64_hash_combine({static_cast<uint64_t>(sb.st_ino), static_cast<uint64_t>(sb.st_dev), offset, size});}uint64_t get_id() const noexcept override {@@ -140,6 +138,9 @@ std::shared_ptr<MappedMemory> load_mmap_object(const std::filesystem::path& path}std::shared_ptr<ov::MappedMemory> load_mmap_object_from_handle(FileHandle handle, size_t offset, size_t size) {+ if (handle == -1) {+ throw std::runtime_error("Invalid file descriptor provided for mapping.");+ }auto holder = std::make_shared<MapHolder>();holder->set_from_fd(handle, offset, size);return holder;diff --git a/src/common/util/src/os/win/win_mmap_object.cpp b/src/common/util/src/os/win/win_mmap_object.cppindex 6155e0f4f33..83fe412b042 100644--- a/src/common/util/src/os/win/win_mmap_object.cpp+++ b/src/common/util/src/os/win/win_mmap_object.cpp@@ -75,14 +75,15 @@ public:void set(const std::filesystem::path& path, size_t offset, size_t size) {// Note that file can't be changed (renamed/deleted) until it's unmapped. FILE_SHARE_DELETE flag allow// rename/deletion, but it doesn't work with FAT32 filesystem (works on NTFS)- auto h = ::CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);+ const auto h =+ ::CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);map(path, h, offset, size);- m_id = util::u64_hash_combine(offset, size);- m_id = util::u64_hash_combine(std::hash<std::filesystem::path::string_type>{}(path.native()), m_id);+ m_id = util::u64_hash_combine({std::hash<std::filesystem::path::string_type>{}(path.native()), offset, size});}void set_from_handle(HANDLE h, size_t offset, size_t size) {map("<external_handle>", h, offset, size);+ set_id(h, offset, size);}char* data() noexcept override {@@ -97,7 +98,20 @@ public:}private:- void map(const std::filesystem::path& path, HANDLE h, const size_t offset, const size_t size) {+ void set_id(const HANDLE h, const size_t offset, const size_t size) {+ if (FILE_ID_INFO info; GetFileInformationByHandleEx(h, FileIdInfo, &info, sizeof(info))) {+ static_assert(sizeof(info.FileId) == 16);+ uint64_t fid_l, fid_r;+ std::memcpy(&fid_l, &info.FileId, sizeof(fid_l));+ std::memcpy(&fid_r, reinterpret_cast<const char*>(&info.FileId) + sizeof(fid_l), sizeof(fid_r));+ m_id = util::u64_hash_combine({offset, size, info.VolumeSerialNumber, fid_l, fid_r});+ } else {+ throw std::runtime_error{"Cannot obtain file id info for handle " ++ std::to_string(reinterpret_cast<uint64_t>(h))};+ }+ }++ void map(const std::filesystem::path& path, const HANDLE h, const size_t offset, const size_t size) {if (h == INVALID_HANDLE_VALUE) {throw std::runtime_error("Can not open file " + util::path_to_string(path) +" for mapping. Ensure that file exists and has appropriate permissions");diff --git a/src/core/tests/mmap_object.cpp b/src/core/tests/mmap_object.cppindex 374bf44eec6..7b0ab5c62f4 100644--- a/src/core/tests/mmap_object.cpp+++ b/src/core/tests/mmap_object.cpp@@ -171,9 +171,6 @@ TEST_P(RangedMappingTest, compare_data) {TEST_P(RangedMappingTest, compare_id) {const auto& [regions, use_file_path] = GetParam();- if (!use_file_path) {- GTEST_SKIP(); // CVS-182260- }const auto& [offset_1, size_1, offset_2, size_2, file_size] = regions;std::filesystem::path other_file_path = utils::generateTestFilePrefix();@@ -189,14 +186,14 @@ TEST_P(RangedMappingTest, compare_id) {other_mm_2 = load_mmap_object(other_file_path, offset_2, size_2);mm_1_ = load_mmap_object(m_file_path, offset_1, size_1);} else {- const auto handle_1 = utils::open_ro_file(m_file_path);- const auto handle_2 = utils::open_ro_file(m_file_path);+ const auto handle = utils::open_ro_file(m_file_path);+ mm_1 = load_mmap_object_from_handle(handle, offset_1, size_1);+ mm_2 = load_mmap_object_from_handle(handle, offset_2, size_2);const auto other_handle = utils::open_ro_file(other_file_path);- mm_1 = load_mmap_object_from_handle(handle_1, offset_1, size_1);- mm_2 = load_mmap_object_from_handle(handle_2, offset_2, size_2);other_mm_1 = load_mmap_object_from_handle(other_handle, offset_1, size_1);other_mm_2 = load_mmap_object_from_handle(other_handle, offset_2, size_2);- mm_1_ = load_mmap_object_from_handle(handle_1, offset_1, size_1);+ const auto handle_ = utils::open_ro_file(m_file_path);+ mm_1_ = load_mmap_object_from_handle(handle_, offset_1, size_1);}ASSERT_NE(mm_1, nullptr);