From b27fc6fd5df9dea03ff4b234d2d2b8b040f685b8 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 23 Jul 2019 07:12:56 +0700 Subject: [PATCH] hwloc used for CPU information. --- src/Summary.cpp | 29 +++++++--- src/backend/cpu/platform/AdvancedCpuInfo.cpp | 4 +- src/backend/cpu/platform/HwlocCpuInfo.cpp | 58 +++++++++++++++++++- src/backend/cpu/platform/HwlocCpuInfo.h | 13 +++++ 4 files changed, 94 insertions(+), 10 deletions(-) diff --git a/src/Summary.cpp b/src/Summary.cpp index fec7c65cf..62d226197 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -67,17 +67,30 @@ static void print_memory(xmrig::Config *) { static void print_cpu(xmrig::Config *) { using namespace xmrig; + const ICpuInfo *info = Cpu::info(); - Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13s%s (%d)") " %sx64 %sAES %sAVX2", + Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13s%s (%zu/%zu)") " %sx64 %sAES %sAVX2", "CPU", - Cpu::info()->brand(), - Cpu::info()->packages(), - Cpu::info()->isX64() ? GREEN_BOLD_S : RED_BOLD_S "-", - Cpu::info()->hasAES() ? GREEN_BOLD_S : RED_BOLD_S "-", - Cpu::info()->hasAVX2() ? GREEN_BOLD_S : RED_BOLD_S "-" + info->brand(), + info->packages(), + info->nodes(), + info->isX64() ? GREEN_BOLD_S : RED_BOLD_S "-", + info->hasAES() ? GREEN_BOLD_S : RED_BOLD_S "-", + info->hasAVX2() ? GREEN_BOLD_S : RED_BOLD_S "-" + ); +# if defined(XMRIG_FEATURE_LIBCPUID) || defined (XMRIG_FEATURE_HWLOC) + Log::print(WHITE_BOLD(" %-13s") BLACK_BOLD("L2:") WHITE_BOLD("%.1f MB") BLACK_BOLD(" L3:") WHITE_BOLD("%.1f MB") BLACK_BOLD(" cores:") CYAN_BOLD("%zu") BLACK_BOLD(" threads:") CYAN_BOLD("%zu"), + "", + info->L2() / 1048576.0, + info->L3() / 1048576.0, + info->cores(), + info->threads() + ); +# else + Log::print(WHITE_BOLD(" %-13s") BLACK_BOLD("threads:") CYAN_BOLD("%zu"), + "", + info->threads() ); -# ifdef XMRIG_FEATURE_LIBCPUID - Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13s%.1f MB/%.1f MB"), "CPU L2/L3", Cpu::info()->L2() / 1024.0, Cpu::info()->L3() / 1024.0); # endif } diff --git a/src/backend/cpu/platform/AdvancedCpuInfo.cpp b/src/backend/cpu/platform/AdvancedCpuInfo.cpp index cd44cc2e0..b5f2dfd5e 100644 --- a/src/backend/cpu/platform/AdvancedCpuInfo.cpp +++ b/src/backend/cpu/platform/AdvancedCpuInfo.cpp @@ -89,6 +89,9 @@ xmrig::AdvancedCpuInfo::AdvancedCpuInfo() : m_L2 = data.l2_cache > 0 ? l2 * cores() * m_packages : 0; } + m_L2 *= 1024; + m_L3 *= 1024; + if (data.flags[CPU_FEATURE_AES]) { m_aes = true; @@ -127,7 +130,6 @@ xmrig::CpuThreads xmrig::AdvancedCpuInfo::threads(const Algorithm &algorithm) co } if (cache) { - cache *= 1024; const size_t memory = algorithm.memory(); assert(memory > 0); diff --git a/src/backend/cpu/platform/HwlocCpuInfo.cpp b/src/backend/cpu/platform/HwlocCpuInfo.cpp index ef518b334..b12a30108 100644 --- a/src/backend/cpu/platform/HwlocCpuInfo.cpp +++ b/src/backend/cpu/platform/HwlocCpuInfo.cpp @@ -29,6 +29,62 @@ #include "backend/cpu/platform/HwlocCpuInfo.h" -xmrig::HwlocCpuInfo::HwlocCpuInfo() : BasicCpuInfo() +namespace xmrig { + + +inline bool isCacheObject(hwloc_obj_t obj) { +# if HWLOC_API_VERSION >= 0x20000 + return hwloc_obj_type_is_cache(obj->type); +# else + return obj->type == HWLOC_OBJ_CACHE; +# endif +} + + +template +inline void findCache(hwloc_obj_t obj, func lambda) +{ + for (size_t i = 0; i < obj->arity; i++) { + if (isCacheObject(obj->children[i])) { + if (obj->children[i]->attr->cache.depth < 2) { + continue; + } + + lambda(obj->children[i]); + } + + findCache(obj->children[i], lambda); + } +} + + +inline size_t countByType(hwloc_topology_t topology, hwloc_obj_type_t type) +{ + const int count = hwloc_get_nbobjs_by_type(topology, type); + + return count > 0 ? static_cast(count) : 0; +} + + +} // namespace xmrig + + +xmrig::HwlocCpuInfo::HwlocCpuInfo() : BasicCpuInfo(), + m_cache() +{ + m_threads = 0; + + hwloc_topology_t topology; + 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; }); + + 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); + + hwloc_topology_destroy(topology); } diff --git a/src/backend/cpu/platform/HwlocCpuInfo.h b/src/backend/cpu/platform/HwlocCpuInfo.h index 0b09bf88f..8d626a8a5 100644 --- a/src/backend/cpu/platform/HwlocCpuInfo.h +++ b/src/backend/cpu/platform/HwlocCpuInfo.h @@ -36,6 +36,19 @@ class HwlocCpuInfo : public BasicCpuInfo { public: HwlocCpuInfo(); + +protected: + 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]; } + inline size_t nodes() const override { return m_nodes; } + inline size_t packages() const override { return m_packages; } + +private: + size_t m_cache[5]; + size_t m_cores = 0; + size_t m_nodes = 0; + size_t m_packages = 0; };