diff --git a/src/base/base.cmake b/src/base/base.cmake index bc022c70c..8e43b6e08 100644 --- a/src/base/base.cmake +++ b/src/base/base.cmake @@ -98,7 +98,14 @@ elseif (APPLE) else() set(SOURCES_OS src/base/io/json/Json_unix.cpp - src/base/kernel//Platform_unix.cpp + src/base/kernel/Platform_unix.cpp + ) +endif() + + +if (WITH_HWLOC) + list(APPEND SOURCES_OS + src/base/kernel/Platform_hwloc.cpp ) endif() diff --git a/src/base/kernel/Platform.cpp b/src/base/kernel/Platform.cpp index a74f1978f..67c729236 100644 --- a/src/base/kernel/Platform.cpp +++ b/src/base/kernel/Platform.cpp @@ -23,7 +23,10 @@ */ -#include +#include "base/kernel/Platform.h" + + +#include #include @@ -33,13 +36,14 @@ #endif -#include "Platform.h" +namespace xmrig { + +String Platform::m_userAgent; + +} // namespace xmrig -xmrig::String Platform::m_userAgent; - - -void Platform::init(const char *userAgent) +void xmrig::Platform::init(const char *userAgent) { # ifdef XMRIG_FEATURE_TLS SSL_library_init(); diff --git a/src/base/kernel/Platform.h b/src/base/kernel/Platform.h index f3c2c719f..3f026f8bd 100644 --- a/src/base/kernel/Platform.h +++ b/src/base/kernel/Platform.h @@ -26,12 +26,15 @@ #define XMRIG_PLATFORM_H -#include +#include #include "base/tools/String.h" +namespace xmrig { + + class Platform { public: @@ -56,8 +59,11 @@ public: private: static char *createUserAgent(); - static xmrig::String m_userAgent; + static String m_userAgent; }; +} // namespace xmrig + + #endif /* XMRIG_PLATFORM_H */ diff --git a/src/base/kernel/Platform_hwloc.cpp b/src/base/kernel/Platform_hwloc.cpp new file mode 100644 index 000000000..f4b46ba1e --- /dev/null +++ b/src/base/kernel/Platform_hwloc.cpp @@ -0,0 +1,49 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "base/kernel/Platform.h" +#include "backend/cpu/platform/HwlocCpuInfo.h" +#include "backend/cpu/Cpu.h" + + +#include + + +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)); + + if (pu == nullptr) { + return false; + } + + if (hwloc_set_cpubind(cpu->topology(), pu->cpuset, HWLOC_CPUBIND_THREAD | HWLOC_CPUBIND_STRICT) >= 0) { + return true; + } + + return hwloc_set_cpubind(cpu->topology(), pu->cpuset, HWLOC_CPUBIND_THREAD) >= 0; +} diff --git a/src/base/kernel/Platform_mac.cpp b/src/base/kernel/Platform_mac.cpp index 4e4aa0ad1..146dd52d7 100644 --- a/src/base/kernel/Platform_mac.cpp +++ b/src/base/kernel/Platform_mac.cpp @@ -30,7 +30,7 @@ #include -#include "Platform.h" +#include "base/kernel/Platform.h" #include "version.h" #ifdef XMRIG_NVIDIA_PROJECT @@ -38,7 +38,7 @@ #endif -char *Platform::createUserAgent() +char *xmrig::Platform::createUserAgent() { constexpr const size_t max = 256; @@ -60,7 +60,8 @@ char *Platform::createUserAgent() } -bool Platform::setThreadAffinity(uint64_t cpu_id) +#ifndef XMRIG_FEATURE_HWLOC +bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id) { thread_port_t mach_thread; thread_affinity_policy_data_t policy = { static_cast(cpu_id) }; @@ -68,25 +69,26 @@ bool Platform::setThreadAffinity(uint64_t cpu_id) return thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY, (thread_policy_t)&policy, 1) == KERN_SUCCESS; } +#endif -uint32_t Platform::setTimerResolution(uint32_t resolution) +uint32_t xmrig::Platform::setTimerResolution(uint32_t resolution) { return resolution; } -void Platform::restoreTimerResolution() +void xmrig::Platform::restoreTimerResolution() { } -void Platform::setProcessPriority(int priority) +void xmrig::Platform::setProcessPriority(int priority) { } -void Platform::setThreadPriority(int priority) +void xmrig::Platform::setThreadPriority(int priority) { if (priority == -1) { return; diff --git a/src/base/kernel/Platform_unix.cpp b/src/base/kernel/Platform_unix.cpp index 3066630ae..bbba39f90 100644 --- a/src/base/kernel/Platform_unix.cpp +++ b/src/base/kernel/Platform_unix.cpp @@ -39,7 +39,7 @@ #include -#include "Platform.h" +#include "base/kernel/Platform.h" #include "version.h" #ifdef XMRIG_NVIDIA_PROJECT @@ -52,7 +52,7 @@ typedef cpuset_t cpu_set_t; #endif -char *Platform::createUserAgent() +char *xmrig::Platform::createUserAgent() { constexpr const size_t max = 256; @@ -84,7 +84,8 @@ char *Platform::createUserAgent() } -bool Platform::setThreadAffinity(uint64_t cpu_id) +#ifndef XMRIG_FEATURE_HWLOC +bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id) { cpu_set_t mn; CPU_ZERO(&mn); @@ -96,25 +97,26 @@ bool Platform::setThreadAffinity(uint64_t cpu_id) return sched_setaffinity(gettid(), sizeof(cpu_set_t), &mn) == 0; # endif } +#endif -uint32_t Platform::setTimerResolution(uint32_t resolution) +uint32_t xmrig::Platform::setTimerResolution(uint32_t resolution) { return resolution; } -void Platform::restoreTimerResolution() +void xmrig::Platform::restoreTimerResolution() { } -void Platform::setProcessPriority(int priority) +void xmrig::Platform::setProcessPriority(int priority) { } -void Platform::setThreadPriority(int priority) +void xmrig::Platform::setThreadPriority(int priority) { if (priority == -1) { return; diff --git a/src/base/kernel/Platform_win.cpp b/src/base/kernel/Platform_win.cpp index f2363cd0d..064c8352f 100644 --- a/src/base/kernel/Platform_win.cpp +++ b/src/base/kernel/Platform_win.cpp @@ -29,8 +29,8 @@ #include +#include "base/kernel/Platform.h" #include "base/io/log/Log.h" -#include "Platform.h" #include "version.h" @@ -51,10 +51,10 @@ static inline OSVERSIONINFOEX winOsVersion() HMODULE ntdll = GetModuleHandleW(L"ntdll.dll"); if (ntdll ) { - RtlGetVersionFunction pRtlGetVersion = reinterpret_cast(GetProcAddress(ntdll, "RtlGetVersion")); + auto pRtlGetVersion = reinterpret_cast(GetProcAddress(ntdll, "RtlGetVersion")); if (pRtlGetVersion) { - pRtlGetVersion((LPOSVERSIONINFO) &result); + pRtlGetVersion(reinterpret_cast(&result)); } } @@ -62,7 +62,7 @@ static inline OSVERSIONINFOEX winOsVersion() } -char *Platform::createUserAgent() +char *xmrig::Platform::createUserAgent() { const auto osver = winOsVersion(); constexpr const size_t max = 256; @@ -91,7 +91,8 @@ char *Platform::createUserAgent() } -bool Platform::setThreadAffinity(uint64_t cpu_id) +#ifndef XMRIG_FEATURE_HWLOC +bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id) { if (cpu_id >= 64) { LOG_ERR("Unable to set affinity. Windows supports only affinity up to 63."); @@ -99,9 +100,10 @@ bool Platform::setThreadAffinity(uint64_t cpu_id) return SetThreadAffinityMask(GetCurrentThread(), 1ULL << cpu_id) != 0; } +#endif -uint32_t Platform::setTimerResolution(uint32_t resolution) +uint32_t xmrig::Platform::setTimerResolution(uint32_t resolution) { # ifdef XMRIG_AMD_PROJECT TIMECAPS tc; @@ -119,7 +121,7 @@ uint32_t Platform::setTimerResolution(uint32_t resolution) } -void Platform::restoreTimerResolution() +void xmrig::Platform::restoreTimerResolution() { # ifdef XMRIG_AMD_PROJECT if (timerResolution) { @@ -129,7 +131,7 @@ void Platform::restoreTimerResolution() } -void Platform::setProcessPriority(int priority) +void xmrig::Platform::setProcessPriority(int priority) { if (priority == -1) { return; @@ -166,7 +168,7 @@ void Platform::setProcessPriority(int priority) } -void Platform::setThreadPriority(int priority) +void xmrig::Platform::setThreadPriority(int priority) { if (priority == -1) { return; diff --git a/src/crypto/common/VirtualMemory.cpp b/src/crypto/common/VirtualMemory.cpp index 56cf3f5f1..242f32a2c 100644 --- a/src/crypto/common/VirtualMemory.cpp +++ b/src/crypto/common/VirtualMemory.cpp @@ -46,8 +46,11 @@ 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 cpu = static_cast(Cpu::info()); + hwloc_obj_t pu = hwloc_get_pu_obj_by_os_index(cpu->topology(), static_cast(affinity)); + + char *buffer; + hwloc_bitmap_asprintf(&buffer, pu->cpuset); if (pu == nullptr || !cpu->membind(pu->nodeset)) { LOG_WARN("CPU #%02" PRId64 " warning: \"can't bind memory\"", affinity); diff --git a/src/crypto/common/VirtualMemory.h b/src/crypto/common/VirtualMemory.h index ac2f75dd8..88ca06836 100644 --- a/src/crypto/common/VirtualMemory.h +++ b/src/crypto/common/VirtualMemory.h @@ -28,8 +28,11 @@ #define XMRIG_VIRTUALMEMORY_H -#include -#include +#include "base/tools/Object.h" + + +#include +#include #include @@ -39,7 +42,9 @@ namespace xmrig { class VirtualMemory { public: - inline VirtualMemory() {} + XMRIG_DISABLE_COPY_MOVE(VirtualMemory) + + VirtualMemory() = default; VirtualMemory(size_t size, bool hugePages = true, size_t align = 64); ~VirtualMemory(); @@ -49,7 +54,7 @@ public: inline std::pair hugePages() const { - return std::pair(isHugePages() ? (align(size()) / 2097152) : 0, align(size()) / 2097152); + return { isHugePages() ? (align(size()) / 2097152) : 0, align(size()) / 2097152 }; } static uint32_t bindToNUMANode(int64_t affinity);