From 2876702ea26d29454728877e76e9ce585bb69113 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 25 Jul 2019 11:24:27 +0700 Subject: [PATCH] Added cmake option HWLOC_DEBUG. --- CMakeLists.txt | 4 +- src/backend/cpu/cpu.cmake | 4 ++ src/backend/cpu/platform/BasicCpuInfo.cpp | 2 +- src/backend/cpu/platform/BasicCpuInfo.h | 2 +- src/backend/cpu/platform/HwlocCpuInfo.cpp | 57 ++++++++++++++++------- src/backend/cpu/platform/HwlocCpuInfo.h | 3 ++ 6 files changed, 53 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fd2608b3..5ef60842 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,9 +12,11 @@ option(WITH_HTTP "HTTP protocol support (client/server)" ON) option(WITH_DEBUG_LOG "Enable debug log output" OFF) option(WITH_TLS "Enable OpenSSL support" ON) option(WITH_ASM "Enable ASM PoW implementations" ON) +option(WITH_EMBEDDED_CONFIG "Enable internal embedded JSON config" OFF) + option(BUILD_STATIC "Build static binary" OFF) option(ARM_TARGET "Force use specific ARM target 8 or 7" 0) -option(WITH_EMBEDDED_CONFIG "Enable internal embedded JSON config" OFF) +option(HWLOC_DEBUG "Enable hwloc debug helpers and log" OFF) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") diff --git a/src/backend/cpu/cpu.cmake b/src/backend/cpu/cpu.cmake index dab30310..b685d7e4 100644 --- a/src/backend/cpu/cpu.cmake +++ b/src/backend/cpu/cpu.cmake @@ -28,6 +28,10 @@ if (WITH_HWLOC) remove_definitions(/DXMRIG_FEATURE_LIBCPUID) add_definitions(/DXMRIG_FEATURE_HWLOC) + if (HWLOC_DEBUG) + add_definitions(/DXMRIG_HWLOC_DEBUG) + endif() + set(CPUID_LIB "") set(SOURCES_CPUID src/backend/cpu/platform/BasicCpuInfo.cpp diff --git a/src/backend/cpu/platform/BasicCpuInfo.cpp b/src/backend/cpu/platform/BasicCpuInfo.cpp index 4bbc8ba0..2b63edba 100644 --- a/src/backend/cpu/platform/BasicCpuInfo.cpp +++ b/src/backend/cpu/platform/BasicCpuInfo.cpp @@ -141,10 +141,10 @@ static inline bool has_avx2() xmrig::BasicCpuInfo::BasicCpuInfo() : + m_brand(), m_threads(std::thread::hardware_concurrency()), m_assembly(Assembly::NONE), m_aes(has_aes_ni()), - m_brand(), m_avx2(has_avx2()) { cpu_brand_string(m_brand); diff --git a/src/backend/cpu/platform/BasicCpuInfo.h b/src/backend/cpu/platform/BasicCpuInfo.h index 8c548559..6cf25714 100644 --- a/src/backend/cpu/platform/BasicCpuInfo.h +++ b/src/backend/cpu/platform/BasicCpuInfo.h @@ -53,12 +53,12 @@ protected: inline size_t threads() const override { return m_threads; } protected: + char m_brand[64 + 6]; size_t m_threads; private: Assembly m_assembly; bool m_aes; - char m_brand[64 + 6]; const bool m_avx2; }; diff --git a/src/backend/cpu/platform/HwlocCpuInfo.cpp b/src/backend/cpu/platform/HwlocCpuInfo.cpp index cdfbdc59..13ff1ee6 100644 --- a/src/backend/cpu/platform/HwlocCpuInfo.cpp +++ b/src/backend/cpu/platform/HwlocCpuInfo.cpp @@ -23,6 +23,12 @@ */ +#ifdef XMRIG_HWLOC_DEBUG +# include +#endif + + +#include #include @@ -107,21 +113,46 @@ xmrig::HwlocCpuInfo::HwlocCpuInfo() : BasicCpuInfo(), { m_threads = 0; - hwloc_topology_t topology; - hwloc_topology_init(&topology); - hwloc_topology_load(topology); + hwloc_topology_init(&m_topology); + hwloc_topology_load(m_topology); - hwloc_obj_t root = hwloc_get_root_obj(topology); +# ifdef XMRIG_HWLOC_DEBUG +# if defined(UV_VERSION_HEX) && UV_VERSION_HEX >= 0x010c00 + { + char env[520] = { 0 }; + size_t size = sizeof(env); + + if (uv_os_getenv("HWLOC_XMLFILE", env, &size) == 0) { + printf("use HWLOC XML file: \"%s\"\n", env); + } + } +# endif + + std::vector packages; + findByType(hwloc_get_root_obj(m_topology), HWLOC_OBJ_PACKAGE, [&packages](hwloc_obj_t found) { packages.emplace_back(found); }); + if (packages.size()) { + const char *value = hwloc_obj_get_info_by_name(packages[0], "CPUModel"); + if (value) { + strncpy(m_brand, value, 64); + } + } +# endif + + hwloc_obj_t root = hwloc_get_root_obj(m_topology); snprintf(m_backend, sizeof m_backend, "hwloc/%s", hwloc_obj_get_info_by_name(root, "hwlocVersion")); findCache(root, 2, 3, [this](hwloc_obj_t found) { this->m_cache[found->attr->cache.depth] += found->attr->cache.size; }); - m_threads = countByType(topology, HWLOC_OBJ_PU); - m_cores = countByType(topology, HWLOC_OBJ_CORE); - m_nodes = countByType(topology, HWLOC_OBJ_NUMANODE); - m_packages = countByType(topology, HWLOC_OBJ_PACKAGE); + m_threads = countByType(m_topology, HWLOC_OBJ_PU); + m_cores = countByType(m_topology, HWLOC_OBJ_CORE); + m_nodes = std::max(countByType(m_topology, HWLOC_OBJ_NUMANODE), 1); + m_packages = countByType(m_topology, HWLOC_OBJ_PACKAGE); +} - hwloc_topology_destroy(topology); + +xmrig::HwlocCpuInfo::~HwlocCpuInfo() +{ + hwloc_topology_destroy(m_topology); } @@ -131,10 +162,6 @@ xmrig::CpuThreads xmrig::HwlocCpuInfo::threads(const Algorithm &algorithm) const return BasicCpuInfo::threads(algorithm); } - hwloc_topology_t topology; - hwloc_topology_init(&topology); - hwloc_topology_load(topology); - const unsigned depth = L3() > 0 ? 3 : 2; CpuThreads threads; @@ -143,14 +170,12 @@ xmrig::CpuThreads xmrig::HwlocCpuInfo::threads(const Algorithm &algorithm) const std::vector caches; caches.reserve(16); - findCache(hwloc_get_root_obj(topology), depth, depth, [&caches](hwloc_obj_t found) { caches.emplace_back(found); }); + findCache(hwloc_get_root_obj(m_topology), depth, depth, [&caches](hwloc_obj_t found) { caches.emplace_back(found); }); for (hwloc_obj_t cache : caches) { processTopLevelCache(cache, algorithm, threads); } - hwloc_topology_destroy(topology); - return threads; } diff --git a/src/backend/cpu/platform/HwlocCpuInfo.h b/src/backend/cpu/platform/HwlocCpuInfo.h index a6cd6b55..4ba0f2fb 100644 --- a/src/backend/cpu/platform/HwlocCpuInfo.h +++ b/src/backend/cpu/platform/HwlocCpuInfo.h @@ -30,6 +30,7 @@ typedef struct hwloc_obj *hwloc_obj_t; +typedef struct hwloc_topology *hwloc_topology_t; namespace xmrig { @@ -39,6 +40,7 @@ class HwlocCpuInfo : public BasicCpuInfo { public: HwlocCpuInfo(); + ~HwlocCpuInfo() override; protected: CpuThreads threads(const Algorithm &algorithm) const override; @@ -54,6 +56,7 @@ private: void processTopLevelCache(hwloc_obj_t obj, const Algorithm &algorithm, CpuThreads &threads) const; char m_backend[20]; + hwloc_topology_t m_topology; size_t m_cache[5]; size_t m_cores = 0; size_t m_nodes = 0;