Add smoke test for CPU map on MacOS (#18753)

This commit is contained in:
Wanglei Shen 2023-08-31 23:31:21 +08:00 committed by GitHub
parent d3ac00c6d2
commit 0651d57c33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 148 additions and 17 deletions

View File

@ -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<std::pair<std::string, uint64_t>>& system_info_table,
int& _processors,
int& _numa_nodes,
int& _sockets,
int& _cores,

View File

@ -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<std::string> ctl_name_list = {"hw.ncpu",
"hw.physicalcpu",
"hw.optional.arm64",
"hw.perflevel0.physicalcpu",
"hw.perflevel1.physicalcpu"};
std::vector<std::pair<std::string, uint64_t>> 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<std::pair<std::string, uint64_t>>& system_info_table,
int& _processors,
int& _numa_nodes,
int& _sockets,
int& _cores,
std::vector<std::vector<int>>& _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<std::string, uint64_t>& item) {
return item.first == "hw.ncpu";
});
if (it == system_info_table.end()) {
return -1;
} else {
_processors = static_cast<int>(output);
_processors = static_cast<int>(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<std::string, uint64_t>& item) {
return item.first == "hw.physicalcpu";
});
if (it == system_info_table.end()) {
_processors = 0;
return -1;
} else {
_cores = static_cast<int>(output);
_cores = static_cast<int>(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<std::string, uint64_t>& item) {
return item.first == "hw.optional.arm64";
});
if (it == system_info_table.end()) {
_proc_type_table.resize(1, std::vector<int>(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<std::string, uint64_t>& 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<int>(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<std::string, uint64_t>& 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;
}
}

View File

@ -0,0 +1,84 @@
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include <gtest/gtest.h>
#include <common_test_utils/test_common.hpp>
#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<std::vector<int>> _proc_type_table;
std::vector<std::pair<std::string, uint64_t>> system_info_table;
};
class MacOSCpuMapParserTests : public ov::test::TestsCommon,
public testing::WithParamInterface<std::tuple<MacOSCpuMapTestCase>> {
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<std::vector<int>> 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