diff --git a/src/inference/src/os/cpu_map_info.hpp b/src/inference/src/os/cpu_map_info.hpp index 598990e7600..88606a846c3 100644 --- a/src/inference/src/os/cpu_map_info.hpp +++ b/src/inference/src/os/cpu_map_info.hpp @@ -148,7 +148,8 @@ void parse_processor_info_win(const char* base_ptr, #if defined(__APPLE__) /** - * @brief Parse processors infomation on Linux + * @brief Parse processors infomation on MacOS + * @param[in] system_info_table cpus information for this platform. * @param[out] _processors total number for processors in system. * @param[out] _numa_nodes total number for sockets in system * @param[out] _sockets total number for sockets in system @@ -156,7 +157,8 @@ void parse_processor_info_win(const char* base_ptr, * @param[out] _proc_type_table summary table of number of processors per type * @return */ -int parse_processor_info_macos(int& _processors, +int parse_processor_info_macos(const std::vector>& system_info_table, + int& _processors, int& _numa_nodes, int& _sockets, int& _cores, diff --git a/src/inference/src/os/mac/mac_system_conf.cpp b/src/inference/src/os/mac/mac_system_conf.cpp index cb31d3676f1..04b9a77c7d6 100644 --- a/src/inference/src/os/mac/mac_system_conf.cpp +++ b/src/inference/src/os/mac/mac_system_conf.cpp @@ -13,39 +13,72 @@ namespace ov { CPU::CPU() { - parse_processor_info_macos(_processors, _numa_nodes, _sockets, _cores, _proc_type_table); - _org_proc_type_table = _proc_type_table; + uint64_t output = 0; + size_t size = sizeof(output); + std::vector ctl_name_list = {"hw.ncpu", + "hw.physicalcpu", + "hw.optional.arm64", + "hw.perflevel0.physicalcpu", + "hw.perflevel1.physicalcpu"}; + std::vector> system_info_table; + + for (auto& row : ctl_name_list) { + if (sysctlbyname(row.c_str(), &output, &size, NULL, 0) >= 0) { + system_info_table.push_back(std::make_pair(row, output)); + } + } + + if (!parse_processor_info_macos(system_info_table, _processors, _numa_nodes, _sockets, _cores, _proc_type_table)) { + _org_proc_type_table = _proc_type_table; + } } -int parse_processor_info_macos(int& _processors, +int parse_processor_info_macos(const std::vector>& system_info_table, + int& _processors, int& _numa_nodes, int& _sockets, int& _cores, std::vector>& _proc_type_table) { - uint64_t output = 0; - size_t size = sizeof(output); - _processors = 0; _numa_nodes = 0; + _sockets = 0; _cores = 0; - if (sysctlbyname("hw.ncpu", &output, &size, NULL, 0) < 0) { + auto it = std::find_if(system_info_table.begin(), + system_info_table.end(), + [&](const std::pair& item) { + return item.first == "hw.ncpu"; + }); + + if (it == system_info_table.end()) { return -1; } else { - _processors = static_cast(output); + _processors = static_cast(it->second); } - if (sysctlbyname("hw.physicalcpu", &output, &size, NULL, 0) < 0) { + it = std::find_if(system_info_table.begin(), + system_info_table.end(), + [&](const std::pair& item) { + return item.first == "hw.physicalcpu"; + }); + + if (it == system_info_table.end()) { _processors = 0; return -1; } else { - _cores = static_cast(output); + _cores = static_cast(it->second); } _numa_nodes = 1; _sockets = 1; - if (sysctlbyname("hw.optional.arm64", &output, &size, NULL, 0) < 0) { + it = std::find_if(system_info_table.begin(), + system_info_table.end(), + [&](const std::pair& item) { + return item.first == "hw.optional.arm64"; + }); + + if (it == system_info_table.end()) { _proc_type_table.resize(1, std::vector(PROC_TYPE_TABLE_SIZE, 0)); _proc_type_table[0][ALL_PROC] = _processors; _proc_type_table[0][MAIN_CORE_PROC] = _cores; @@ -53,7 +86,13 @@ int parse_processor_info_macos(int& _processors, _proc_type_table[0][PROC_NUMA_NODE_ID] = 0; _proc_type_table[0][PROC_SOCKET_ID] = 0; } else { - if (sysctlbyname("hw.perflevel0.physicalcpu", &output, &size, NULL, 0) < 0) { + it = std::find_if(system_info_table.begin(), + system_info_table.end(), + [&](const std::pair& item) { + return item.first == "hw.perflevel0.physicalcpu"; + }); + + if (it == system_info_table.end()) { _processors = 0; _cores = 0; _numa_nodes = 0; @@ -62,15 +101,21 @@ int parse_processor_info_macos(int& _processors, } else { _proc_type_table.resize(1, std::vector(PROC_TYPE_TABLE_SIZE, 0)); _proc_type_table[0][ALL_PROC] = _processors; - _proc_type_table[0][MAIN_CORE_PROC] = output; + _proc_type_table[0][MAIN_CORE_PROC] = it->second; _proc_type_table[0][PROC_NUMA_NODE_ID] = 0; _proc_type_table[0][PROC_SOCKET_ID] = 0; } - if (sysctlbyname("hw.perflevel1.physicalcpu", &output, &size, NULL, 0) < 0) { + it = std::find_if(system_info_table.begin(), + system_info_table.end(), + [&](const std::pair& item) { + return item.first == "hw.perflevel1.physicalcpu"; + }); + + if (it == system_info_table.end()) { return 0; } else { - _proc_type_table[0][EFFICIENT_CORE_PROC] = output; + _proc_type_table[0][EFFICIENT_CORE_PROC] = it->second; } } diff --git a/src/inference/tests/unit/cpu_map_parser/parser_macos.cpp b/src/inference/tests/unit/cpu_map_parser/parser_macos.cpp new file mode 100644 index 00000000000..59fe8aef0ac --- /dev/null +++ b/src/inference/tests/unit/cpu_map_parser/parser_macos.cpp @@ -0,0 +1,84 @@ +// Copyright (C) 2018-2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include + +#include + +#include "ie_system_conf.h" +#include "os/cpu_map_info.hpp" + +using namespace testing; +using namespace ov; + +namespace { + +#ifdef __APPLE__ + +struct MacOSCpuMapTestCase { + int _processors; + int _numa_nodes; + int _sockets; + int _cores; + std::vector> _proc_type_table; + std::vector> system_info_table; +}; + +class MacOSCpuMapParserTests : public ov::test::TestsCommon, + public testing::WithParamInterface> { +public: + void SetUp() override { + const auto& test_data = std::get<0>(GetParam()); + + int test_processors = 0; + int test_numa_nodes = 0; + int test_sockets = 0; + int test_cores = 0; + std::vector> test_proc_type_table; + + ov::parse_processor_info_macos(test_data.system_info_table, + test_processors, + test_numa_nodes, + test_sockets, + test_cores, + test_proc_type_table); + + ASSERT_EQ(test_data._processors, test_processors); + ASSERT_EQ(test_data._numa_nodes, test_numa_nodes); + ASSERT_EQ(test_data._sockets, test_sockets); + ASSERT_EQ(test_data._cores, test_cores); + ASSERT_EQ(test_data._proc_type_table, test_proc_type_table); + } +}; + +MacOSCpuMapTestCase test_case_arm = { + 8, // param[expected out]: total 8 logcial processors on this simulated platform + 1, // param[expected out]: total 1 numa nodes on this simulated platform + 1, // param[expected out]: total 1 sockets on this simulated platform + 8, // param[expected out]: total 8 CPU cores on this simulated platform + {{8, 4, 4, 0, 0, 0}}, // param[expected out]: The proc_type_table of this simulated platform + { + {"hw.ncpu", 8}, + {"hw.physicalcpu", 8}, + {"hw.optional.arm64", 1}, + {"hw.perflevel0.physicalcpu", 4}, + {"hw.perflevel1.physicalcpu", 4}, + }, // param[in]: The system information table of this simulated platform +}; + +MacOSCpuMapTestCase test_case_x86 = { + 12, + 1, + 1, + 6, + {{12, 6, 0, 6, 0, 0}}, + {{"hw.ncpu", 12}, {"hw.physicalcpu", 6}}, +}; + +TEST_P(MacOSCpuMapParserTests, MacOS) {} + +INSTANTIATE_TEST_SUITE_P(CPUMap, MacOSCpuMapParserTests, testing::Values(test_case_arm, test_case_x86)); + +#endif +} // namespace