From a6a0995d547f050a9984644e0a70f545b71f1d39 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 23 Jul 2019 14:19:41 +0700 Subject: [PATCH] Better method to get hwloc version. --- src/backend/cpu/Cpu.cpp | 14 +------------- src/backend/cpu/Cpu.h | 1 - src/backend/cpu/interfaces/ICpuInfo.h | 1 + src/backend/cpu/platform/AdvancedCpuInfo.cpp | 4 +++- src/backend/cpu/platform/AdvancedCpuInfo.h | 4 +++- src/backend/cpu/platform/BasicCpuInfo.cpp | 6 ++++++ src/backend/cpu/platform/BasicCpuInfo.h | 1 + src/backend/cpu/platform/BasicCpuInfo_arm.cpp | 8 +++++++- src/backend/cpu/platform/HwlocCpuInfo.cpp | 10 +++++++++- src/backend/cpu/platform/HwlocCpuInfo.h | 2 ++ src/base/kernel/config/BaseConfig.cpp | 13 ++----------- 11 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/backend/cpu/Cpu.cpp b/src/backend/cpu/Cpu.cpp index d32b2d8e9..886ad15cd 100644 --- a/src/backend/cpu/Cpu.cpp +++ b/src/backend/cpu/Cpu.cpp @@ -42,18 +42,6 @@ static xmrig::ICpuInfo *cpuInfo = nullptr; -const char *xmrig::Cpu::backend() -{ -# if defined(XMRIG_FEATURE_HWLOC) - return "hwloc"; -# elif defined(XMRIG_FEATURE_LIBCPUID) - return "libcpuid"; -# else - return "basic"; -# endif -} - - xmrig::ICpuInfo *xmrig::Cpu::info() { assert(cpuInfo != nullptr); @@ -82,7 +70,7 @@ rapidjson::Value xmrig::Cpu::toJSON(rapidjson::Document &doc) cpu.AddMember("threads", static_cast(i->threads()), allocator); cpu.AddMember("packages", static_cast(i->packages()), allocator); cpu.AddMember("nodes", static_cast(i->nodes()), allocator); - cpu.AddMember("backend", StringRef(backend()), allocator); + cpu.AddMember("backend", StringRef(i->backend()), allocator); return cpu; } diff --git a/src/backend/cpu/Cpu.h b/src/backend/cpu/Cpu.h index 4638b568c..bece97d3a 100644 --- a/src/backend/cpu/Cpu.h +++ b/src/backend/cpu/Cpu.h @@ -35,7 +35,6 @@ namespace xmrig { class Cpu { public: - static const char *backend(); static ICpuInfo *info(); static rapidjson::Value toJSON(rapidjson::Document &doc); static void init(); diff --git a/src/backend/cpu/interfaces/ICpuInfo.h b/src/backend/cpu/interfaces/ICpuInfo.h index 7faa7187f..daaa39c30 100644 --- a/src/backend/cpu/interfaces/ICpuInfo.h +++ b/src/backend/cpu/interfaces/ICpuInfo.h @@ -48,6 +48,7 @@ public: virtual Assembly::Id assembly() const = 0; virtual bool hasAES() const = 0; virtual bool hasAVX2() const = 0; + virtual const char *backend() const = 0; virtual const char *brand() const = 0; virtual CpuThreads threads(const Algorithm &algorithm) const = 0; virtual size_t cores() const = 0; diff --git a/src/backend/cpu/platform/AdvancedCpuInfo.cpp b/src/backend/cpu/platform/AdvancedCpuInfo.cpp index b5f2dfd5e..f8871d3c1 100644 --- a/src/backend/cpu/platform/AdvancedCpuInfo.cpp +++ b/src/backend/cpu/platform/AdvancedCpuInfo.cpp @@ -24,11 +24,12 @@ #include #include -#include #include +#include #include +#include "3rdparty/libcpuid/libcpuid.h" #include "backend/cpu/platform/AdvancedCpuInfo.h" @@ -66,6 +67,7 @@ xmrig::AdvancedCpuInfo::AdvancedCpuInfo() : cpu_identify(&raw, &data); cpu_brand_string(m_brand, data.brand_str); + snprintf(m_backend, sizeof m_backend, "libcpuid/%s", cpuid_lib_version()); m_threads = static_cast(data.total_logical_cpus); m_packages = std::max(threads() / static_cast(data.num_logical_cpus), 1); diff --git a/src/backend/cpu/platform/AdvancedCpuInfo.h b/src/backend/cpu/platform/AdvancedCpuInfo.h index e82258443..51b84c9fd 100644 --- a/src/backend/cpu/platform/AdvancedCpuInfo.h +++ b/src/backend/cpu/platform/AdvancedCpuInfo.h @@ -43,6 +43,7 @@ protected: inline Assembly::Id assembly() const override { return m_assembly; } inline bool hasAES() const override { return m_aes; } inline bool hasAVX2() const override { return m_avx2; } + inline const char *backend() const override { return m_backend; } inline const char *brand() const override { return m_brand; } inline size_t cores() const override { return m_cores; } inline size_t L2() const override { return m_L2; } @@ -56,7 +57,8 @@ private: bool m_aes = false; bool m_avx2 = false; bool m_L2_exclusive = false; - char m_brand[64]; + char m_backend[32]; + char m_brand[64 + 5]; size_t m_cores = 0; size_t m_L2 = 0; size_t m_L3 = 0; diff --git a/src/backend/cpu/platform/BasicCpuInfo.cpp b/src/backend/cpu/platform/BasicCpuInfo.cpp index 1ceefecc4..4bbc8ba00 100644 --- a/src/backend/cpu/platform/BasicCpuInfo.cpp +++ b/src/backend/cpu/platform/BasicCpuInfo.cpp @@ -174,6 +174,12 @@ xmrig::BasicCpuInfo::BasicCpuInfo() : } +const char *xmrig::BasicCpuInfo::backend() const +{ + return "basic"; +} + + xmrig::CpuThreads xmrig::BasicCpuInfo::threads(const Algorithm &algorithm) const { if (threads() == 1) { diff --git a/src/backend/cpu/platform/BasicCpuInfo.h b/src/backend/cpu/platform/BasicCpuInfo.h index b05ed9cc3..8c5485596 100644 --- a/src/backend/cpu/platform/BasicCpuInfo.h +++ b/src/backend/cpu/platform/BasicCpuInfo.h @@ -38,6 +38,7 @@ public: BasicCpuInfo(); protected: + const char *backend() const override; CpuThreads threads(const Algorithm &algorithm) const override; inline Assembly::Id assembly() const override { return m_assembly; } diff --git a/src/backend/cpu/platform/BasicCpuInfo_arm.cpp b/src/backend/cpu/platform/BasicCpuInfo_arm.cpp index 3d733535a..1e6c3cb72 100644 --- a/src/backend/cpu/platform/BasicCpuInfo_arm.cpp +++ b/src/backend/cpu/platform/BasicCpuInfo_arm.cpp @@ -57,7 +57,13 @@ xmrig::BasicCpuInfo::BasicCpuInfo() : } -xmrig::CpuThreads xmrig::BasicCpuInfo::threads(const Algorithm &algorithm) const +const char *xmrig::BasicCpuInfo::backend() const +{ + return "basic_arm"; +} + + +xmrig::CpuThreads xmrig::BasicCpuInfo::threads(const Algorithm &) const { return CpuThreads(threads()); } diff --git a/src/backend/cpu/platform/HwlocCpuInfo.cpp b/src/backend/cpu/platform/HwlocCpuInfo.cpp index b12a30108..156f3ae44 100644 --- a/src/backend/cpu/platform/HwlocCpuInfo.cpp +++ b/src/backend/cpu/platform/HwlocCpuInfo.cpp @@ -71,6 +71,7 @@ inline size_t countByType(hwloc_topology_t topology, hwloc_obj_type_t type) xmrig::HwlocCpuInfo::HwlocCpuInfo() : BasicCpuInfo(), + m_backend(), m_cache() { m_threads = 0; @@ -79,7 +80,14 @@ xmrig::HwlocCpuInfo::HwlocCpuInfo() : BasicCpuInfo(), hwloc_topology_init(&topology); hwloc_topology_load(topology); - findCache(hwloc_get_root_obj(topology), [this](hwloc_obj_t found) { this->m_cache[found->attr->cache.depth] += found->attr->cache.size; }); + hwloc_obj_t root = hwloc_get_root_obj(topology); + snprintf(m_backend, sizeof m_backend, "hwloc/%s", hwloc_obj_get_info_by_name(root, "hwlocVersion")); + + findCache(root, [this](hwloc_obj_t found) { + const unsigned depth = found->attr->cache.depth; + + this->m_cache[depth] += found->attr->cache.size; + }); m_threads = countByType(topology, HWLOC_OBJ_PU); m_cores = countByType(topology, HWLOC_OBJ_CORE); diff --git a/src/backend/cpu/platform/HwlocCpuInfo.h b/src/backend/cpu/platform/HwlocCpuInfo.h index 8d626a8a5..a67680665 100644 --- a/src/backend/cpu/platform/HwlocCpuInfo.h +++ b/src/backend/cpu/platform/HwlocCpuInfo.h @@ -38,6 +38,7 @@ public: HwlocCpuInfo(); protected: + inline const char *backend() const override { return m_backend; } inline size_t cores() const override { return m_cores; } inline size_t L2() const override { return m_cache[2]; } inline size_t L3() const override { return m_cache[3]; } @@ -45,6 +46,7 @@ protected: inline size_t packages() const override { return m_packages; } private: + char m_backend[20]; size_t m_cache[5]; size_t m_cores = 0; size_t m_nodes = 0; diff --git a/src/base/kernel/config/BaseConfig.cpp b/src/base/kernel/config/BaseConfig.cpp index c89b032a0..4600a1f4b 100644 --- a/src/base/kernel/config/BaseConfig.cpp +++ b/src/base/kernel/config/BaseConfig.cpp @@ -35,7 +35,7 @@ #endif #ifdef XMRIG_FEATURE_HWLOC -# include +# include "backend/cpu/Cpu.h" #endif #ifdef XMRIG_AMD_PROJECT @@ -110,17 +110,8 @@ void xmrig::BaseConfig::printVersions() } # endif - # if defined(XMRIG_FEATURE_HWLOC) -# if defined(HWLOC_VERSION) - snprintf(buf, sizeof buf, "hwloc/%s ", HWLOC_VERSION); -# elif HWLOC_API_VERSION >= 0x20000 - snprintf(buf, sizeof buf, "hwloc/2 "); -# else - snprintf(buf, sizeof buf, "hwloc/1 "); -# endif - - libs += buf; + libs += Cpu::info()->backend(); # endif Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13slibuv/%s %s"), "LIBS", uv_version_string(), libs.c_str());