From 9125b6c2512bb5539424894c766ef178ded93fbd Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 15 Apr 2018 11:08:47 +0700 Subject: [PATCH] Rewrite memory allocation. --- CMakeLists.txt | 1 + src/App.cpp | 8 +--- src/Mem.cpp | 79 ++++++++++++--------------------- src/Mem.h | 35 +++++++-------- src/Mem_unix.cpp | 60 ++++++++++--------------- src/Mem_win.cpp | 66 +++++++++++++-------------- src/Summary.cpp | 9 ++-- src/api/ApiRouter.cpp | 2 +- src/common/utils/mm_malloc.h | 43 ++++++++++++++++++ src/crypto/CryptoNight.h | 3 +- src/crypto/CryptoNight_arm.h | 8 +--- src/crypto/CryptoNight_monero.h | 2 +- src/crypto/CryptoNight_x86.h | 38 ++++++++-------- src/interfaces/IThread.h | 20 ++++++--- src/workers/CpuThread.h | 13 +----- src/workers/MultiWorker.cpp | 26 +++++++---- src/workers/MultiWorker.h | 14 +++--- src/workers/Worker.cpp | 7 --- src/workers/Worker.h | 2 - 19 files changed, 212 insertions(+), 224 deletions(-) create mode 100644 src/common/utils/mm_malloc.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ecb65b22..c7017b01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ set(HEADERS src/common/config/ConfigWatcher.h src/common/Platform.h src/common/utils/c_str.h + src/common/utils/mm_malloc.h src/common/xmrig.h src/Console.h src/core/Config.cpp diff --git a/src/App.cpp b/src/App.cpp index 3d0cb3df..78d6b328 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -76,8 +76,6 @@ App::App(int argc, char **argv) : App::~App() { - Mem::release(); - uv_tty_reset_mode(); delete m_console; @@ -101,11 +99,7 @@ int App::exec() background(); - Mem::allocate(m_controller->config()->algorithm(), - m_controller->config()->threadsCount(), - m_controller->config()->isDoubleHash(), - m_controller->config()->isHugePages() - ); + Mem::init(m_controller->config()->isHugePages()); Summary::print(m_controller); diff --git a/src/Mem.cpp b/src/Mem.cpp index 9de79dbd..2efaac8a 100644 --- a/src/Mem.cpp +++ b/src/Mem.cpp @@ -23,70 +23,49 @@ */ -#include - - +#include "common/utils/mm_malloc.h" #include "crypto/CryptoNight.h" #include "crypto/CryptoNight_constants.h" #include "Mem.h" -bool Mem::m_doubleHash = false; -int Mem::m_algo = 0; -int Mem::m_flags = 0; -int Mem::m_threads = 0; -size_t Mem::m_offset = 0; -size_t Mem::m_size = 0; -alignas(16) uint8_t *Mem::m_memory = nullptr; +bool Mem::m_enabled = true; +int Mem::m_flags = 0; -cryptonight_ctx *Mem::create(int threadId) + +MemInfo Mem::create(cryptonight_ctx **ctx, xmrig::Algo algorithm, size_t count) { + using namespace xmrig; + + MemInfo info; + info.size = cn_select_memory(algorithm) * count; + # ifndef XMRIG_NO_AEON - if (m_algo == xmrig::CRYPTONIGHT_LITE) { - return createLite(threadId); - } + info.size += info.size % cn_select_memory(); # endif - const size_t size = m_algo == xmrig::CRYPTONIGHT_HEAVY ? xmrig::cn_select_memory() - : xmrig::cn_select_memory(); + info.pages = info.size / cn_select_memory(); - cryptonight_ctx *ctx = reinterpret_cast(&m_memory[size - sizeof(cryptonight_ctx) * (threadId + 1)]); + allocate(info, m_enabled); - const int ratio = m_doubleHash ? 2 : 1; - ctx->memory = &m_memory[size * (threadId * ratio + 1)]; + for (size_t i = 0; i < count; ++i) { + cryptonight_ctx *c = static_cast(_mm_malloc(sizeof(cryptonight_ctx), 16)); + c->memory = info.memory + (i * cn_select_memory(algorithm)); - return ctx; -} - - - -void *Mem::calloc(size_t num, size_t size) -{ - void *mem = &m_memory[m_offset]; - m_offset += (num * size); - - memset(mem, 0, num * size); - - return mem; -} - - -#ifndef XMRIG_NO_AEON -cryptonight_ctx *Mem::createLite(int threadId) { - cryptonight_ctx *ctx; - - if (!m_doubleHash) { - const size_t offset = MONERO_MEMORY * (threadId + 1); - - ctx = reinterpret_cast(&m_memory[offset + AEON_MEMORY]); - ctx->memory = &m_memory[offset]; - return ctx; + ctx[i] = c; } - ctx = reinterpret_cast(&m_memory[MONERO_MEMORY - sizeof(cryptonight_ctx) * (threadId + 1)]); - ctx->memory = &m_memory[MONERO_MEMORY * (threadId + 1)]; - - return ctx; + return info; } -#endif + + +void Mem::release(cryptonight_ctx **ctx, size_t count, MemInfo &info) +{ + release(info); + + for (size_t i = 0; i < count; ++i) { + _mm_free(ctx[i]); + } +} + diff --git a/src/Mem.h b/src/Mem.h index 06b470c8..6fd18fc1 100644 --- a/src/Mem.h +++ b/src/Mem.h @@ -36,6 +36,16 @@ struct cryptonight_ctx; +struct MemInfo +{ + alignas(16) uint8_t *memory; + + size_t hugePages; + size_t pages; + size_t size; +}; + + class Mem { public: @@ -45,29 +55,18 @@ public: Lock = 4 }; - static bool allocate(xmrig::Algo algo, int threads, bool doubleHash, bool enabled); - static cryptonight_ctx *create(int threadId); - static void *calloc(size_t num, size_t size); - static void release(); + static MemInfo create(cryptonight_ctx **ctx, xmrig::Algo algorithm, size_t count); + static void init(bool enabled); + static void release(cryptonight_ctx **ctx, size_t count, MemInfo &info); - static inline bool isDoubleHash() { return m_doubleHash; } static inline bool isHugepagesAvailable() { return (m_flags & HugepagesAvailable) != 0; } - static inline bool isHugepagesEnabled() { return (m_flags & HugepagesEnabled) != 0; } - static inline int flags() { return m_flags; } - static inline int threads() { return m_threads; } private: - static bool m_doubleHash; - static int m_algo; - static int m_flags; - static int m_threads; - static size_t m_offset; - static size_t m_size; - alignas(16) static uint8_t *m_memory; + static void allocate(MemInfo &info, bool enabled); + static void release(MemInfo &info); -# ifndef XMRIG_NO_AEON - static cryptonight_ctx *createLite(int threadId); -# endif + static int m_flags; + static bool m_enabled; }; diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index df7aaad2..550033c4 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -27,75 +27,63 @@ #include -#if defined(XMRIG_ARM) && !defined(__clang__) -# include "aligned_malloc.h" -#else -# include -#endif - - +#include "common/utils/mm_malloc.h" #include "common/xmrig.h" #include "crypto/CryptoNight.h" #include "log/Log.h" #include "Mem.h" -bool Mem::allocate(xmrig::Algo algo, int threads, bool doubleHash, bool enabled) +void Mem::init(bool enabled) { - m_algo = algo; - m_threads = threads; - m_doubleHash = doubleHash; + m_enabled = enabled; +} - const int ratio = (doubleHash && algo != xmrig::CRYPTONIGHT_LITE) ? 2 : 1; - m_size = MONERO_MEMORY * (threads * ratio + 1); - if (algo == xmrig::CRYPTONIGHT_HEAVY) { - m_size *= 2; - } +void Mem::allocate(MemInfo &info, bool enabled) +{ + info.hugePages = 0; if (!enabled) { - m_memory = static_cast(_mm_malloc(m_size, 16)); - return true; - } + info.memory = static_cast(_mm_malloc(info.size, 16)); - m_flags |= HugepagesAvailable; + return; + } # if defined(__APPLE__) - m_memory = static_cast(mmap(0, m_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, VM_FLAGS_SUPERPAGE_SIZE_2MB, 0)); + info.memory = static_cast(mmap(0, info.size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, VM_FLAGS_SUPERPAGE_SIZE_2MB, 0)); # elif defined(__FreeBSD__) - m_memory = static_cast(mmap(0, m_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED_SUPER | MAP_PREFAULT_READ, -1, 0)); + info.memory = static_cast(mmap(0, info.size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED_SUPER | MAP_PREFAULT_READ, -1, 0)); # else - m_memory = static_cast(mmap(0, m_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, 0, 0)); + info.memory = static_cast(mmap(0, info.size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, 0, 0)); # endif - if (m_memory == MAP_FAILED) { - m_memory = static_cast(_mm_malloc(m_size, 16)); - return true; + + if (info.memory == MAP_FAILED) { + return allocate(info, false);; } - m_flags |= HugepagesEnabled; + info.hugePages = info.pages; - if (madvise(m_memory, m_size, MADV_RANDOM | MADV_WILLNEED) != 0) { + if (madvise(info.memory, info.size, MADV_RANDOM | MADV_WILLNEED) != 0) { LOG_ERR("madvise failed"); } - if (mlock(m_memory, m_size) == 0) { + if (mlock(info.memory, info.size) == 0) { m_flags |= Lock; } - - return true; } -void Mem::release() +void Mem::release(MemInfo &info) { - if (m_flags & HugepagesEnabled) { + if (info.hugePages) { if (m_flags & Lock) { - munlock(m_memory, m_size); + munlock(info.memory, info.size); } - munmap(m_memory, m_size); + munmap(info.memory, info.size); } else { - _mm_free(m_memory); + _mm_free(info.memory); } } diff --git a/src/Mem_win.cpp b/src/Mem_win.cpp index 1f3066ea..d0b698f6 100644 --- a/src/Mem_win.cpp +++ b/src/Mem_win.cpp @@ -28,14 +28,11 @@ #include #include -#ifdef __GNUC__ -# include -#else -# include -#endif +#include "common/utils/mm_malloc.h" #include "common/xmrig.h" #include "crypto/CryptoNight.h" +#include "crypto/CryptoNight_constants.h" #include "log/Log.h" #include "Mem.h" @@ -145,46 +142,43 @@ static BOOL TrySetLockPagesPrivilege() { } -bool Mem::allocate(xmrig::Algo algo, int threads, bool doubleHash, bool enabled) +void Mem::init(bool enabled) { - m_algo = algo; - m_threads = threads; - m_doubleHash = doubleHash; + m_enabled = enabled; - const int ratio = (doubleHash && algo != xmrig::CRYPTONIGHT_LITE) ? 2 : 1; - m_size = MONERO_MEMORY * (threads * ratio + 1); - - if (algo == xmrig::CRYPTONIGHT_HEAVY) { - m_size *= 2; - } - - if (!enabled) { - m_memory = static_cast(_mm_malloc(m_size, 16)); - return true; - } - - if (TrySetLockPagesPrivilege()) { + if (enabled && TrySetLockPagesPrivilege()) { m_flags |= HugepagesAvailable; } - - m_memory = static_cast(VirtualAlloc(NULL, m_size, MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE)); - if (!m_memory) { - m_memory = static_cast(_mm_malloc(m_size, 16)); - } - else { - m_flags |= HugepagesEnabled; - } - - return true; } -void Mem::release() +void Mem::allocate(MemInfo &info, bool enabled) { - if (m_flags & HugepagesEnabled) { - VirtualFree(m_memory, 0, MEM_RELEASE); + info.hugePages = 0; + + if (!enabled) { + info.memory = static_cast(_mm_malloc(info.size, 16)); + + return; + } + + info.memory = static_cast(VirtualAlloc(nullptr, info.size, MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE)); + if (info.memory) { + info.hugePages = info.pages; + + return; + } + + allocate(info, false); +} + + +void Mem::release(MemInfo &info) +{ + if (info.hugePages) { + VirtualFree(info.memory, 0, MEM_RELEASE); } else { - _mm_free(m_memory); + _mm_free(info.memory); } } diff --git a/src/Summary.cpp b/src/Summary.cpp index 5acda5c2..a54214e4 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -58,14 +58,15 @@ static void print_versions(xmrig::Config *config) static void print_memory(xmrig::Config *config) { +# ifdef _WIN32 if (config->isColors()) { - Log::i()->text("\x1B[01;32m * \x1B[01;37mHUGE PAGES: %s, %s", - Mem::isHugepagesAvailable() ? "\x1B[01;32mavailable" : "\x1B[01;31munavailable", - Mem::isHugepagesEnabled() ? "\x1B[01;32menabled" : "\x1B[01;31mdisabled"); + Log::i()->text("\x1B[01;32m * \x1B[01;37mHUGE PAGES: %s", + Mem::isHugepagesAvailable() ? "\x1B[01;32mavailable" : "\x1B[01;31munavailable"); } else { - Log::i()->text(" * HUGE PAGES: %s, %s", Mem::isHugepagesAvailable() ? "available" : "unavailable", Mem::isHugepagesEnabled() ? "enabled" : "disabled"); + Log::i()->text(" * HUGE PAGES: %s", Mem::isHugepagesAvailable() ? "available" : "unavailable"); } +# endif } diff --git a/src/api/ApiRouter.cpp b/src/api/ApiRouter.cpp index cb7a8005..0893bc8b 100644 --- a/src/api/ApiRouter.cpp +++ b/src/api/ApiRouter.cpp @@ -267,7 +267,7 @@ void ApiRouter::getMiner(rapidjson::Document &doc) const doc.AddMember("ua", rapidjson::StringRef(Platform::userAgent()), allocator); doc.AddMember("cpu", cpu, allocator); doc.AddMember("algo", rapidjson::StringRef(m_controller->config()->algoName()), allocator); - doc.AddMember("hugepages", Mem::isHugepagesEnabled(), allocator); + doc.AddMember("hugepages", false, allocator); doc.AddMember("donate_level", m_controller->config()->donateLevel(), allocator); } diff --git a/src/common/utils/mm_malloc.h b/src/common/utils/mm_malloc.h new file mode 100644 index 00000000..30c721a3 --- /dev/null +++ b/src/common/utils/mm_malloc.h @@ -0,0 +1,43 @@ +/* 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 2016-2018 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 . + */ + +#ifndef __MM_MALLOC_PORTABLE_H__ +#define __MM_MALLOC_PORTABLE_H__ + + +#ifdef _WIN32 +# ifdef __GNUC__ +# include +# else +# include +# endif +#else +# if defined(XMRIG_ARM) && !defined(__clang__) +# include "aligned_malloc.h" +# else +# include +# endif +#endif + + +#endif /* __MM_MALLOC_PORTABLE_H__ */ diff --git a/src/crypto/CryptoNight.h b/src/crypto/CryptoNight.h index d0d61ae3..5a4a266d 100644 --- a/src/crypto/CryptoNight.h +++ b/src/crypto/CryptoNight.h @@ -40,8 +40,7 @@ struct cryptonight_ctx { - alignas(16) uint8_t state0[200]; - alignas(16) uint8_t state1[200]; + alignas(16) uint8_t state[200]; alignas(16) uint8_t* memory; }; diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index fd8b58ff..d9677d06 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -27,13 +27,7 @@ #define __CRYPTONIGHT_ARM_H__ -#if defined(XMRIG_ARM) && !defined(__clang__) -# include "aligned_malloc.h" -#else -# include -#endif - - +#include "common/utils/mm_malloc.h" #include "crypto/CryptoNight.h" #include "crypto/CryptoNight_constants.h" #include "crypto/CryptoNight_monero.h" diff --git a/src/crypto/CryptoNight_monero.h b/src/crypto/CryptoNight_monero.h index a667a3b3..a34f3ba8 100644 --- a/src/crypto/CryptoNight_monero.h +++ b/src/crypto/CryptoNight_monero.h @@ -32,7 +32,7 @@ uint64_t tweak1_2_##part = 0; \ if (VARIANT > 0) { \ tweak1_2_##part = (*reinterpret_cast(input + 35 + part * size) ^ \ - *(reinterpret_cast(ctx->state##part) + 24)); \ + *(reinterpret_cast(ctx[part]->state) + 24)); \ } #else # define VARIANT1_INIT(part) \ diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 417404a6..824dd99c 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -403,7 +403,7 @@ static inline void cryptonight_monero_tweak(uint64_t* mem_out, __m128i tmp) template -inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx *__restrict__ ctx) +inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); @@ -414,14 +414,14 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si return; } - keccak(input, (int) size, ctx->state0, 200); + keccak(input, (int) size, ctx[0]->state, 200); VARIANT1_INIT(0) - cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); + cn_explode_scratchpad((__m128i*) ctx[0]->state, (__m128i*) ctx[0]->memory); - const uint8_t* l0 = ctx->memory; - uint64_t* h0 = reinterpret_cast(ctx->state0); + const uint8_t* l0 = ctx[0]->memory; + uint64_t* h0 = reinterpret_cast(ctx[0]->state); uint64_t al0 = h0[0] ^ h0[4]; uint64_t ah0 = h0[1] ^ h0[5]; @@ -476,15 +476,15 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si } } - cn_implode_scratchpad((__m128i*) ctx->memory, (__m128i*) ctx->state0); + cn_implode_scratchpad((__m128i*) ctx[0]->memory, (__m128i*) ctx[0]->state); keccakf(h0, 24); - extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, output); + extra_hashes[ctx[0]->state[0] & 3](ctx[0]->state, 200, output); } template -inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { constexpr size_t MASK = xmrig::cn_select_mask(); constexpr size_t ITERATIONS = xmrig::cn_select_iter(); @@ -495,16 +495,16 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si return; } - keccak(input, (int) size, ctx->state0, 200); - keccak(input + size, (int) size, ctx->state1, 200); + keccak(input, (int) size, ctx[0]->state, 200); + keccak(input + size, (int) size, ctx[1]->state, 200); VARIANT1_INIT(0); VARIANT1_INIT(1); - const uint8_t* l0 = ctx->memory; - const uint8_t* l1 = ctx->memory + MEM; - uint64_t* h0 = reinterpret_cast(ctx->state0); - uint64_t* h1 = reinterpret_cast(ctx->state1); + const uint8_t* l0 = ctx[0]->memory; + const uint8_t* l1 = ctx[1]->memory; + uint64_t* h0 = reinterpret_cast(ctx[0]->state); + uint64_t* h1 = reinterpret_cast(ctx[1]->state); cn_explode_scratchpad((__m128i*) h0, (__m128i*) l0); cn_explode_scratchpad((__m128i*) h1, (__m128i*) l1); @@ -606,25 +606,25 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si keccakf(h0, 24); keccakf(h1, 24); - extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, output); - extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, output + 32); + extra_hashes[ctx[0]->state[0] & 3](ctx[0]->state, 200, output); + extra_hashes[ctx[1]->state[0] & 3](ctx[1]->state, 200, output + 32); } template -inline void cryptonight_triple_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +inline void cryptonight_triple_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { } template -inline void cryptonight_quad_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +inline void cryptonight_quad_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { } template -inline void cryptonight_penta_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +inline void cryptonight_penta_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx **__restrict__ ctx) { } diff --git a/src/interfaces/IThread.h b/src/interfaces/IThread.h index 5b3efa0d..f517ed18 100644 --- a/src/interfaces/IThread.h +++ b/src/interfaces/IThread.h @@ -43,14 +43,22 @@ public: CUDA }; + enum Multiway { + SingleWay = 1, + DoubleWay, + TripleWay, + QuadWay, + PentaWay + }; + virtual ~IThread() {} - virtual Algo algorithm() const = 0; - virtual int multiway() const = 0; - virtual int priority() const = 0; - virtual int64_t affinity() const = 0; - virtual size_t index() const = 0; - virtual Type type() const = 0; + virtual Algo algorithm() const = 0; + virtual int priority() const = 0; + virtual int64_t affinity() const = 0; + virtual Multiway multiway() const = 0; + virtual size_t index() const = 0; + virtual Type type() const = 0; # ifndef XMRIG_NO_API virtual rapidjson::Value toAPI(rapidjson::Document &doc) const = 0; diff --git a/src/workers/CpuThread.h b/src/workers/CpuThread.h index 9d1bc4e7..e14c79f1 100644 --- a/src/workers/CpuThread.h +++ b/src/workers/CpuThread.h @@ -38,15 +38,6 @@ namespace xmrig { class CpuThread : public IThread { public: - enum Multiway { - SingleWay = 1, - DoubleWay, - TripleWay, - QuadWay, - PentaWay - }; - - struct Data { inline Data() : valid(false), affinity(-1L), multiway(SingleWay) {} @@ -68,7 +59,7 @@ public: CpuThread(size_t index, Algo algorithm, AlgoVariant av, Multiway multiway, int64_t affinity, int priority, bool softAES, bool prefetch); ~CpuThread(); - typedef void (*cn_hash_fun)(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx); + typedef void (*cn_hash_fun)(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx **ctx); static cn_hash_fun fn(Algo algorithm, AlgoVariant av, Variant variant); static CpuThread *createFromAV(size_t index, Algo algorithm, AlgoVariant av, int64_t affinity, int priority); @@ -80,9 +71,9 @@ public: inline cn_hash_fun fn(Variant variant) const { return fn(m_algorithm, m_av, variant); } inline Algo algorithm() const override { return m_algorithm; } - inline int multiway() const override { return m_multiway; } inline int priority() const override { return m_priority; } inline int64_t affinity() const override { return m_affinity; } + inline Multiway multiway() const override { return m_multiway; } inline size_t index() const override { return m_index; } inline Type type() const override { return CPU; } diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index 0007b0cb..a75adecc 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -36,6 +36,14 @@ template MultiWorker::MultiWorker(Handle *handle) : Worker(handle) { + m_memory = Mem::create(m_ctx, m_thread->algorithm(), N); +} + + +template +MultiWorker::~MultiWorker() +{ + Mem::release(m_ctx, N, m_memory); } @@ -46,24 +54,24 @@ bool MultiWorker::selfTest() return false; } - m_thread->fn(xmrig::VARIANT_NONE)(test_input, 76, m_result.result, m_ctxLegacy); + m_thread->fn(xmrig::VARIANT_NONE)(test_input, 76, m_hash, m_ctx); - if (m_thread->algorithm() == xmrig::CRYPTONIGHT && memcmp(m_result.result, test_output_v0, 32) == 0) { - m_thread->fn(xmrig::VARIANT_V1)(test_input, 76, m_result.result, m_ctxLegacy); + if (m_thread->algorithm() == xmrig::CRYPTONIGHT && memcmp(m_hash, test_output_v0, sizeof m_hash) == 0) { + m_thread->fn(xmrig::VARIANT_V1)(test_input, 76, m_hash, m_ctx); - return memcmp(m_result.result, test_output_v1, 32) == 0; + return memcmp(m_hash, test_output_v1, sizeof m_hash) == 0; } # ifndef XMRIG_NO_AEON - if (m_thread->algorithm() == xmrig::CRYPTONIGHT_LITE && memcmp(m_result.result, test_output_v0_lite, 32) == 0) { - m_thread->fn(xmrig::VARIANT_V1)(test_input, 76, m_result.result, m_ctxLegacy); + if (m_thread->algorithm() == xmrig::CRYPTONIGHT_LITE && memcmp(m_hash, test_output_v0_lite, sizeof m_hash) == 0) { + m_thread->fn(xmrig::VARIANT_V1)(test_input, 76, m_hash, m_ctx); - return memcmp(m_result.result, test_output_v1_lite, 32) == 0; + return memcmp(m_hash, test_output_v1_lite, sizeof m_hash) == 0; } # endif # ifndef XMRIG_NO_SUMO - return m_thread->algorithm() == xmrig::CRYPTONIGHT_HEAVY && memcmp(m_result.result, test_output_heavy, 32) == 0; + return m_thread->algorithm() == xmrig::CRYPTONIGHT_HEAVY && memcmp(m_hash, test_output_heavy, sizeof m_hash) == 0; # else return false; # endif @@ -92,7 +100,7 @@ void MultiWorker::start() storeStats(); } - m_thread->fn(m_state.job.variant())(m_state.blob, m_state.job.size(), m_hash, m_ctxLegacy); + m_thread->fn(m_state.job.variant())(m_state.blob, m_state.job.size(), m_hash, m_ctx); for (size_t i = 0; i < N; ++i) { if (*reinterpret_cast(m_hash + (i * 32) + 24) < m_state.job.target()) { diff --git a/src/workers/MultiWorker.h b/src/workers/MultiWorker.h index d384b0ba..ba57f81e 100644 --- a/src/workers/MultiWorker.h +++ b/src/workers/MultiWorker.h @@ -26,6 +26,7 @@ #define __MULTIWORKER_H__ +#include "Mem.h" #include "net/Job.h" #include "net/JobResult.h" #include "workers/Worker.h" @@ -39,6 +40,7 @@ class MultiWorker : public Worker { public: MultiWorker(Handle *handle); + ~MultiWorker(); protected: bool selfTest() override; @@ -61,15 +63,11 @@ private: }; -// cryptonight_ctx *m_ctx[N]; - - uint8_t m_hash[N * 32]; - State m_state; + cryptonight_ctx *m_ctx[N]; + MemInfo m_memory; State m_pausedState; - - Job m_job; - Job m_paused; - JobResult m_result; + State m_state; + uint8_t m_hash[N * 32]; }; diff --git a/src/workers/Worker.cpp b/src/workers/Worker.cpp index e0ed846b..567b3e08 100644 --- a/src/workers/Worker.cpp +++ b/src/workers/Worker.cpp @@ -26,7 +26,6 @@ #include "common/Platform.h" #include "Cpu.h" -#include "Mem.h" #include "workers/CpuThread.h" #include "workers/Handle.h" #include "workers/Worker.h" @@ -47,12 +46,6 @@ Worker::Worker(Handle *handle) : } Platform::setThreadPriority(m_thread->priority()); - m_ctxLegacy = Mem::create(m_id); -} - - -Worker::~Worker() -{ } diff --git a/src/workers/Worker.h b/src/workers/Worker.h index 72b3beb2..0ae30530 100644 --- a/src/workers/Worker.h +++ b/src/workers/Worker.h @@ -45,7 +45,6 @@ class Worker : public IWorker { public: Worker(Handle *handle); - ~Worker(); inline size_t id() const override { return m_id; } inline uint64_t hashCount() const override { return m_hashCount.load(std::memory_order_relaxed); } @@ -57,7 +56,6 @@ protected: const size_t m_id; const size_t m_totalWays; const uint32_t m_offset; - cryptonight_ctx *m_ctxLegacy; std::atomic m_hashCount; std::atomic m_timestamp; uint64_t m_count;