Skip to content

Commit

Permalink
[refactoring] Remove get_sys_info duplication in tools/ (#1080)
Browse files Browse the repository at this point in the history
  • Loading branch information
mzhukova authored Nov 19, 2024
1 parent 80605bf commit c77eafa
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 114 deletions.
23 changes: 1 addition & 22 deletions tools/benchmarks/include/details/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,8 @@ using registry_t = std::vector<registry_call_t>;
registry_t& get_registry();

//
// System information utils
// Device utils
//
struct accel_info_t {
size_t total_devices = 0;
std::map<int, size_t> devices_per_numa;
};

struct extended_info_t {
std::string host_name;
std::string kernel;
std::uint32_t cpu_model = 0U;
std::string cpu_model_name;
std::uint32_t cpu_stepping = 0U;
std::uint32_t cpu_microcode = 0U;
std::uint32_t cpu_logical_cores = 0U;
std::uint32_t cpu_physical_cores = 0U;
std::uint32_t cpu_sockets = 0U;
std::uint32_t cpu_physical_per_socket = 0U;
accel_info_t accelerators;
};

const extended_info_t& get_sys_info();

std::uint32_t get_number_of_devices_matching_numa_policy(std::uint32_t user_specified_numa_id) noexcept;

constexpr std::uint64_t submitRetryWaitNs = 0U;
Expand Down
92 changes: 6 additions & 86 deletions tools/benchmarks/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
#include "test_hw_device.hpp"
#include "test_hw_dispatcher.hpp"

// tool_common
#include "system_info.hpp"

#if defined(__linux__)
#include <sys/utsname.h>
#endif
#include <cstdarg>
#include <fstream>
#include <iostream>
#include <memory>
#include <mutex>
#include <regex>
Expand Down Expand Up @@ -78,91 +82,6 @@ std::uint32_t get_number_of_devices_matching_numa_policy(std::uint32_t user_spec
return counter;
}

static inline accel_info_t& get_accels_info() noexcept {
static accel_info_t info;

static auto& disp = qpl::test::hw_dispatcher::get_instance();

for (auto& device : disp) {
if (info.devices_per_numa.find(device.numa_id()) == info.devices_per_numa.end())
info.devices_per_numa[device.numa_id()] = 1;
else
info.devices_per_numa[device.numa_id()]++;
}

info.total_devices = disp.device_count();

return info;
}

const extended_info_t& get_sys_info() {
static extended_info_t info;
static bool is_setup {false};
static std::mutex guard;

guard.lock();
if (!is_setup) {
#if defined(__linux__)
utsname uname_buf;
uname(&uname_buf);
info.host_name = uname_buf.nodename;
info.kernel = uname_buf.release;

std::ifstream info_file("/proc/cpuinfo");
if (!info_file.is_open()) {
guard.unlock();
throw std::runtime_error("Failed to open /proc/cpuinfo");
}
std::string line;
while (std::getline(info_file, line)) {
if (line.empty()) continue;
auto del_index = line.find(':');
if (del_index == std::string::npos) continue;
auto key = line.substr(0, del_index);
auto val = line.substr(del_index + 1);
trim(key);
trim(val);

if (key == "processor")
info.cpu_logical_cores++;
else if (key == "physical id")
info.cpu_sockets = std::max(info.cpu_sockets, (std::uint32_t)atoi(val.c_str()) + 1);
else if (!info.cpu_physical_per_socket && key == "cpu cores")
info.cpu_physical_per_socket = std::max(info.cpu_physical_per_socket, (std::uint32_t)atoi(val.c_str()));
else if (info.cpu_model_name.empty() && key == "model name")
info.cpu_model_name = val;
else if (!info.cpu_model && key == "model")
info.cpu_model = atoi(val.c_str());
else if (!info.cpu_microcode && key == "microcode")
info.cpu_microcode = strtol(val.c_str(), NULL, 16);
else if (!info.cpu_stepping && key == "stepping")
info.cpu_stepping = atoi(val.c_str());
}
info.cpu_physical_cores = info.cpu_physical_per_socket * info.cpu_sockets;

info.accelerators = get_accels_info();

/* Benchmarks output for system configuration details */
printf("Host Name: %s\n", info.host_name.c_str());
printf("Kernel: %s\n", info.kernel.c_str());
printf("CPU: %s (%d)\n", info.cpu_model_name.c_str(), info.cpu_model);
printf(" Microcode: 0x%x\n", info.cpu_microcode);
printf(" Stepping: %d\n", info.cpu_stepping);
printf(" Logical Cores: %d\n", info.cpu_logical_cores);
printf(" Physical Cores: %d\n", info.cpu_physical_cores);
printf(" Cores per Socket: %d\n", info.cpu_physical_per_socket);
printf(" Sockets: %d\n", info.cpu_sockets);
printf("Accelerators: %ld\n", info.accelerators.total_devices);
for (auto& it : info.accelerators.devices_per_numa) {
printf(" On NUMA %d: %ld\n", it.first, it.second);
}
#endif
is_setup = true;
}
guard.unlock();

return info;
}
} // namespace bench::details

//
Expand Down Expand Up @@ -362,7 +281,8 @@ int main(int argc, char** argv) //NOLINT(bugprone-exception-escape)
if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1;

// Retrieve system information
bench::details::get_sys_info();
auto& sys_info = qpl::test::get_sys_info();
std::cout << sys_info;

// Initialize accelerator hardware if enabled
if (!bench::cmd::FLAGS_no_hw) bench::details::init_hw();
Expand Down
6 changes: 6 additions & 0 deletions tools/tests/functional/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

#include <algorithm>

// tool_common
#include "system_info.hpp"

namespace qpl::test {

static inline void show_help() {
Expand Down Expand Up @@ -187,6 +190,9 @@ int main(int argc, char* argv[]) { //NOLINT(bugprone-exception-escape)

environment::GetInstance().Initialize(arguments_list);

const qpl::test::extended_info_t& info = qpl::test::get_sys_info();
std::cout << info;

int init_with_fork_status = 0;
#if defined(__linux__)
auto execution_path = environment::GetInstance().GetExecutionPath();
Expand Down
52 changes: 46 additions & 6 deletions tools/utils/common/system_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,32 @@
#ifndef QPL_TOOLS_UTILS_COMMON_SYSTEM_INFO_HPP_
#define QPL_TOOLS_UTILS_COMMON_SYSTEM_INFO_HPP_

#include <algorithm> // std::find_if
#include <cctype> // std::isspace, std::isdigit
#include <fstream> // std::ifstream
#include <mutex> // std::mutex
#include <string> // std::string, std::getline, substr, etc.
#include <vector> // std::vector
#include <algorithm>
#include <cctype>
#include <fstream>
#include <map>
#include <mutex>
#include <string>
#include <vector>

#if defined(__linux__)
#include <sys/utsname.h>
#include <x86intrin.h>
#endif

// tool_hw_dispatcher
#include "test_hw_dispatcher.hpp"

namespace qpl::test {

/**
* @brief Structure to keep information about accelerators.
*/
struct accel_info_t {
size_t total_devices = 0;
std::map<int, size_t> devices_per_numa;
};

/**
* @brief Structure to keep system information not including accelerators.
*
Expand All @@ -39,6 +51,7 @@ struct extended_info_t {
std::uint32_t cpu_sockets = 1U;
std::uint32_t cpu_physical_per_socket = 1U;
std::uint32_t cpu_numa_nodes = 1U;
accel_info_t accelerators;
};

static void trim(std::string& str) {
Expand Down Expand Up @@ -112,6 +125,10 @@ static std::ostream& operator<<(std::ostream& os, const extended_info_t& info) {
os << " Cores per Socket: " << info.cpu_physical_per_socket << "\n";
os << " Sockets: " << info.cpu_sockets << "\n";
os << " NUMA Nodes: " << info.cpu_numa_nodes << "\n";
os << " Accelerators: " << info.accelerators.total_devices << "\n";
for (auto& it : info.accelerators.devices_per_numa) {
os << " On NUMA " << it.first << ": " << it.second << "\n";
}

return os;
}
Expand All @@ -135,6 +152,28 @@ static uint32_t get_total_numa_nodes() {
return total_nodes;
}

/*
* @brief Structure to keep information about accelerators.
*/
static inline accel_info_t& get_accels_info() noexcept {
static accel_info_t info;

#if defined(__linux__)
static auto& disp = qpl::test::hw_dispatcher::get_instance();

for (auto& device : disp) {
if (info.devices_per_numa.find(device.numa_id()) == info.devices_per_numa.end())
info.devices_per_numa[device.numa_id()] = 1;
else
info.devices_per_numa[device.numa_id()]++;
}

info.total_devices = disp.device_count();
#endif

return info;
}

/**
* @note Implementation is borrowed from Benchmarks Framework.
* A copy is stored since it is not desirable to introduce additional dependencies for benchmarks executable.
Expand Down Expand Up @@ -194,6 +233,7 @@ static const extended_info_t& get_sys_info() {
guard.unlock();

info.cpu_numa_nodes = get_total_numa_nodes();
info.accelerators = get_accels_info();

return info;
}
Expand Down

0 comments on commit c77eafa

Please sign in to comment.