From c7e541d84f015882d6363972b1641a43337cac01 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 7 Jun 2023 00:32:09 +0700 Subject: [PATCH] Disallow direct use of HwlocCpuInfo class. --- src/backend/cpu/Cpu.h | 8 ++--- src/backend/cpu/interfaces/ICpuInfo.h | 18 +++++++++-- src/backend/cpu/platform/BasicCpuInfo.h | 11 +++---- src/backend/cpu/platform/HwlocCpuInfo.cpp | 7 ---- src/backend/cpu/platform/HwlocCpuInfo.h | 39 +++++++---------------- src/base/kernel/Platform_hwloc.cpp | 15 ++++----- src/crypto/common/VirtualMemory_hwloc.cpp | 11 +++---- src/crypto/ghostrider/ghostrider.cpp | 8 ++--- src/crypto/rx/RxConfig.cpp | 11 ++----- src/crypto/rx/RxConfig.h | 8 ++--- src/crypto/rx/RxNUMAStorage.cpp | 10 +++--- 11 files changed, 61 insertions(+), 85 deletions(-) diff --git a/src/backend/cpu/Cpu.h b/src/backend/cpu/Cpu.h index 186063d32..3067ebcda 100644 --- a/src/backend/cpu/Cpu.h +++ b/src/backend/cpu/Cpu.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2020 SChernykh - * Copyright (c) 2016-2020 XMRig , + * Copyright (c) 2018-2023 SChernykh + * Copyright (c) 2016-2023 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -37,7 +37,7 @@ public: }; -} /* namespace xmrig */ +} // namespace xmrig -#endif /* XMRIG_CPU_H */ +#endif // XMRIG_CPU_H diff --git a/src/backend/cpu/interfaces/ICpuInfo.h b/src/backend/cpu/interfaces/ICpuInfo.h index 387f319b1..e9d795cec 100644 --- a/src/backend/cpu/interfaces/ICpuInfo.h +++ b/src/backend/cpu/interfaces/ICpuInfo.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig + * Copyright (c) 2018-2023 SChernykh + * Copyright (c) 2016-2023 XMRig * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,6 +26,12 @@ #include "crypto/common/Assembly.h" +#ifdef XMRIG_FEATURE_HWLOC +using hwloc_const_bitmap_t = const struct hwloc_bitmap_s *; +using hwloc_topology_t = struct hwloc_topology *; +#endif + + namespace xmrig { @@ -116,10 +122,16 @@ public: virtual size_t threads() const = 0; virtual Vendor vendor() const = 0; virtual uint32_t model() const = 0; + +# ifdef XMRIG_FEATURE_HWLOC + virtual bool membind(hwloc_const_bitmap_t nodeset) = 0; + virtual const std::vector &nodeset() const = 0; + virtual hwloc_topology_t topology() const = 0; +# endif }; -} /* namespace xmrig */ +} // namespace xmrig #endif // XMRIG_CPUINFO_H diff --git a/src/backend/cpu/platform/BasicCpuInfo.h b/src/backend/cpu/platform/BasicCpuInfo.h index 9405fa751..f56d5425c 100644 --- a/src/backend/cpu/platform/BasicCpuInfo.h +++ b/src/backend/cpu/platform/BasicCpuInfo.h @@ -1,7 +1,7 @@ /* XMRig * Copyright (c) 2017-2019 XMR-Stak , - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig + * Copyright (c) 2018-2023 SChernykh + * Copyright (c) 2016-2023 XMRig * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -72,11 +72,10 @@ protected: # endif } -protected: Arch m_arch = ARCH_UNKNOWN; bool m_jccErratum = false; char m_brand[64 + 6]{}; - size_t m_threads; + size_t m_threads = 0; std::vector m_units; Vendor m_vendor = VENDOR_UNKNOWN; @@ -94,7 +93,7 @@ private: }; -} /* namespace xmrig */ +} // namespace xmrig -#endif /* XMRIG_BASICCPUINFO_H */ +#endif // XMRIG_BASICCPUINFO_H diff --git a/src/backend/cpu/platform/HwlocCpuInfo.cpp b/src/backend/cpu/platform/HwlocCpuInfo.cpp index 95753ae2e..f6137c31e 100644 --- a/src/backend/cpu/platform/HwlocCpuInfo.cpp +++ b/src/backend/cpu/platform/HwlocCpuInfo.cpp @@ -47,9 +47,6 @@ static inline int hwloc_obj_type_is_cache(hwloc_obj_type_t type) namespace xmrig { -uint32_t HwlocCpuInfo::m_features = 0; - - template static inline void findCache(hwloc_obj_t obj, unsigned min, unsigned max, func lambda) { @@ -172,10 +169,6 @@ xmrig::HwlocCpuInfo::HwlocCpuInfo() m_packages = countByType(m_topology, HWLOC_OBJ_PACKAGE); if (m_nodes > 1) { - if (hwloc_topology_get_support(m_topology)->membind->set_thisthread_membind) { - m_features |= SET_THISTHREAD_MEMBIND; - } - m_nodeset.reserve(m_nodes); hwloc_obj_t node = nullptr; diff --git a/src/backend/cpu/platform/HwlocCpuInfo.h b/src/backend/cpu/platform/HwlocCpuInfo.h index db8691840..a08ea33ef 100644 --- a/src/backend/cpu/platform/HwlocCpuInfo.h +++ b/src/backend/cpu/platform/HwlocCpuInfo.h @@ -21,12 +21,9 @@ #include "backend/cpu/platform/BasicCpuInfo.h" -#include "base/tools/Object.h" -using hwloc_const_bitmap_t = const struct hwloc_bitmap_s *; -using hwloc_obj_t = struct hwloc_obj *; -using hwloc_topology_t = struct hwloc_topology *; +using hwloc_obj_t = struct hwloc_obj *; namespace xmrig { @@ -37,39 +34,27 @@ class HwlocCpuInfo : public BasicCpuInfo public: XMRIG_DISABLE_COPY_MOVE(HwlocCpuInfo) - - enum Feature : uint32_t { - SET_THISTHREAD_MEMBIND = 1 - }; - - HwlocCpuInfo(); ~HwlocCpuInfo() override; - static inline bool hasFeature(Feature feature) { return m_features & feature; } - - inline const std::vector &nodeset() const { return m_nodeset; } - inline hwloc_topology_t topology() const { return m_topology; } - - bool membind(hwloc_const_bitmap_t nodeset); - protected: + bool membind(hwloc_const_bitmap_t nodeset) override; CpuThreads threads(const Algorithm &algorithm, uint32_t limit) const override; - 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]; } - inline size_t nodes() const override { return m_nodes; } - inline size_t packages() const override { return m_packages; } + inline const char *backend() const override { return m_backend; } + inline const std::vector &nodeset() const override { return m_nodeset; } + inline hwloc_topology_t topology() const override { return m_topology; } + 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: CpuThreads allThreads(const Algorithm &algorithm, uint32_t limit) const; void processTopLevelCache(hwloc_obj_t cache, const Algorithm &algorithm, CpuThreads &threads, size_t limit) const; void setThreads(size_t threads); - static uint32_t m_features; - char m_backend[20] = { 0 }; hwloc_topology_t m_topology = nullptr; size_t m_cache[5] = { 0 }; @@ -80,7 +65,7 @@ private: }; -} /* namespace xmrig */ +} // namespace xmrig -#endif /* XMRIG_HWLOCCPUINFO_H */ +#endif // XMRIG_HWLOCCPUINFO_H diff --git a/src/base/kernel/Platform_hwloc.cpp b/src/base/kernel/Platform_hwloc.cpp index 8d9d9f593..219c17424 100644 --- a/src/base/kernel/Platform_hwloc.cpp +++ b/src/base/kernel/Platform_hwloc.cpp @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2020 SChernykh - * Copyright (c) 2016-2020 XMRig , + * Copyright (c) 2018-2023 SChernykh + * Copyright (c) 2016-2023 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -16,9 +16,7 @@ * along with this program. If not, see . */ - #include "base/kernel/Platform.h" -#include "backend/cpu/platform/HwlocCpuInfo.h" #include "backend/cpu/Cpu.h" @@ -29,20 +27,21 @@ #ifndef XMRIG_OS_APPLE bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id) { - auto cpu = static_cast(Cpu::info()); - hwloc_obj_t pu = hwloc_get_pu_obj_by_os_index(cpu->topology(), static_cast(cpu_id)); + auto topology = Cpu::info()->topology(); + auto pu = hwloc_get_pu_obj_by_os_index(topology, static_cast(cpu_id)); if (pu == nullptr) { return false; } - if (hwloc_set_cpubind(cpu->topology(), pu->cpuset, HWLOC_CPUBIND_THREAD | HWLOC_CPUBIND_STRICT) >= 0) { + if (hwloc_set_cpubind(topology, pu->cpuset, HWLOC_CPUBIND_THREAD | HWLOC_CPUBIND_STRICT) >= 0) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); return true; } - const bool result = (hwloc_set_cpubind(cpu->topology(), pu->cpuset, HWLOC_CPUBIND_THREAD) >= 0); + const bool result = (hwloc_set_cpubind(topology, pu->cpuset, HWLOC_CPUBIND_THREAD) >= 0); std::this_thread::sleep_for(std::chrono::milliseconds(1)); + return result; } #endif diff --git a/src/crypto/common/VirtualMemory_hwloc.cpp b/src/crypto/common/VirtualMemory_hwloc.cpp index d11290081..21b0bd46c 100644 --- a/src/crypto/common/VirtualMemory_hwloc.cpp +++ b/src/crypto/common/VirtualMemory_hwloc.cpp @@ -1,8 +1,8 @@ /* XMRig * Copyright (c) 2018 Lee Clagett * Copyright (c) 2018-2019 tevador - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2023 SChernykh + * Copyright (c) 2016-2023 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,10 +18,8 @@ * along with this program. If not, see . */ - #include "crypto/common/VirtualMemory.h" #include "backend/cpu/Cpu.h" -#include "backend/cpu/platform/HwlocCpuInfo.h" #include "base/io/log/Log.h" @@ -34,10 +32,9 @@ uint32_t xmrig::VirtualMemory::bindToNUMANode(int64_t affinity) return 0; } - auto cpu = static_cast(Cpu::info()); - hwloc_obj_t pu = hwloc_get_pu_obj_by_os_index(cpu->topology(), static_cast(affinity)); + auto pu = hwloc_get_pu_obj_by_os_index(Cpu::info()->topology(), static_cast(affinity)); - if (pu == nullptr || !cpu->membind(pu->nodeset)) { + if (pu == nullptr || !Cpu::info()->membind(pu->nodeset)) { LOG_WARN("CPU #%02" PRId64 " warning: \"can't bind memory\"", affinity); return 0; diff --git a/src/crypto/ghostrider/ghostrider.cpp b/src/crypto/ghostrider/ghostrider.cpp index 79241bd3d..25bb44e74 100644 --- a/src/crypto/ghostrider/ghostrider.cpp +++ b/src/crypto/ghostrider/ghostrider.cpp @@ -48,7 +48,6 @@ #ifdef XMRIG_FEATURE_HWLOC # include "base/kernel/Platform.h" -# include "backend/cpu/platform/HwlocCpuInfo.h" # include # if HWLOC_API_VERSION < 0x20000 @@ -243,7 +242,7 @@ struct HelperThread void run() { if (hwloc_bitmap_weight(m_cpuSet) > 0) { - hwloc_topology_t topology = reinterpret_cast(Cpu::info())->topology(); + hwloc_topology_t topology = Cpu::info()->topology(); if (hwloc_set_cpubind(topology, m_cpuSet, HWLOC_CPUBIND_THREAD | HWLOC_CPUBIND_STRICT) < 0) { hwloc_set_cpubind(topology, m_cpuSet, HWLOC_CPUBIND_THREAD); } @@ -297,7 +296,7 @@ void benchmark() // Try to avoid CPU core 0 because many system threads use it and can interfere uint32_t thread_index1 = (Cpu::info()->threads() > 2) ? 2 : 0; - hwloc_topology_t topology = reinterpret_cast(Cpu::info())->topology(); + hwloc_topology_t topology = Cpu::info()->topology(); hwloc_obj_t pu = hwloc_get_pu_obj_by_os_index(topology, thread_index1); hwloc_obj_t pu2 = nullptr; hwloc_get_closest_objs(topology, pu, &pu2, 1); @@ -490,8 +489,7 @@ HelperThread* create_helper_thread(int64_t cpu_index, int priority, const std::v } if (cpu_index >= 0) { - hwloc_topology_t topology = reinterpret_cast(Cpu::info())->topology(); - hwloc_obj_t root = hwloc_get_root_obj(topology); + hwloc_obj_t root = hwloc_get_root_obj(Cpu::info()->topology()); bool is8MB = false; diff --git a/src/crypto/rx/RxConfig.cpp b/src/crypto/rx/RxConfig.cpp index cb50c4d1c..cd1969354 100644 --- a/src/crypto/rx/RxConfig.cpp +++ b/src/crypto/rx/RxConfig.cpp @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2023 SChernykh + * Copyright (c) 2016-2023 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,11 +22,6 @@ #include "base/io/json/Json.h" -#ifdef XMRIG_FEATURE_HWLOC -# include "backend/cpu/platform/HwlocCpuInfo.h" -#endif - - #include #include #include @@ -190,7 +185,7 @@ std::vector xmrig::RxConfig::nodeset() const return m_nodeset; } - return (m_numa && Cpu::info()->nodes() > 1) ? static_cast(Cpu::info())->nodeset() : std::vector(); + return (m_numa && Cpu::info()->nodes() > 1) ? Cpu::info()->nodeset() : std::vector(); } #endif diff --git a/src/crypto/rx/RxConfig.h b/src/crypto/rx/RxConfig.h index e4fb4f89c..e8662e4d3 100644 --- a/src/crypto/rx/RxConfig.h +++ b/src/crypto/rx/RxConfig.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2023 SChernykh + * Copyright (c) 2016-2023 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -123,7 +123,7 @@ private: }; -} /* namespace xmrig */ +} // namespace xmrig -#endif /* XMRIG_RXCONFIG_H */ +#endif // XMRIG_RXCONFIG_H diff --git a/src/crypto/rx/RxNUMAStorage.cpp b/src/crypto/rx/RxNUMAStorage.cpp index b6345a061..cc66d76e5 100644 --- a/src/crypto/rx/RxNUMAStorage.cpp +++ b/src/crypto/rx/RxNUMAStorage.cpp @@ -1,7 +1,7 @@ /* XMRig * Copyright (c) 2018-2019 tevador - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2023 SChernykh + * Copyright (c) 2016-2023 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,7 +19,6 @@ #include "crypto/rx/RxNUMAStorage.h" #include "backend/cpu/Cpu.h" -#include "backend/cpu/platform/HwlocCpuInfo.h" #include "base/io/log/Log.h" #include "base/io/log/Tags.h" #include "base/kernel/Platform.h" @@ -45,13 +44,12 @@ static std::mutex mutex; static bool bindToNUMANode(uint32_t nodeId) { - auto cpu = static_cast(Cpu::info()); - hwloc_obj_t node = hwloc_get_numanode_obj_by_os_index(cpu->topology(), nodeId); + auto node = hwloc_get_numanode_obj_by_os_index(Cpu::info()->topology(), nodeId); if (!node) { return false; } - if (cpu->membind(node->nodeset)) { + if (Cpu::info()->membind(node->nodeset)) { Platform::setThreadAffinity(static_cast(hwloc_bitmap_first(node->cpuset))); return true;