diff --git a/CMakeLists.txt b/CMakeLists.txt index 5b095f54..86edd95c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -168,7 +168,7 @@ else() endif() endif() -add_definitions(-DXMRIG_MINER_PROJECT) +add_definitions(-DXMRIG_MINER_PROJECT -DXMRIG_JSON_SINGLE_LINE_ARRAY) add_definitions(-D__STDC_FORMAT_MACROS -DUNICODE) find_package(UV REQUIRED) diff --git a/cmake/randomx.cmake b/cmake/randomx.cmake index 99de2632..9b6f76bc 100644 --- a/cmake/randomx.cmake +++ b/cmake/randomx.cmake @@ -108,6 +108,13 @@ if (WITH_RANDOMX) remove_definitions(/DXMRIG_FIX_RYZEN) message("-- WITH_MSR=OFF") endif() + + if (WITH_PROFILING) + add_definitions(/DXMRIG_FEATURE_PROFILING) + + list(APPEND HEADERS_CRYPTO src/crypto/rx/Profiler.h) + list(APPEND SOURCES_CRYPTO src/crypto/rx/Profiler.cpp) + endif() else() remove_definitions(/DXMRIG_ALGO_RANDOMX) endif() diff --git a/src/App.cpp b/src/App.cpp index 1d1ec3e9..39ec0d62 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -43,17 +43,13 @@ xmrig::App::App(Process *process) { - m_controller = new Controller(process); + m_controller = std::make_shared(process); } xmrig::App::~App() { Cpu::release(); - - delete m_signals; - delete m_console; - delete m_controller; } @@ -65,7 +61,7 @@ int xmrig::App::exec() return 2; } - m_signals = new Signals(this); + m_signals = std::make_shared(this); int rc = 0; if (background(rc)) { @@ -78,10 +74,10 @@ int xmrig::App::exec() } if (!m_controller->isBackground()) { - m_console = new Console(this); + m_console = std::make_shared(this); } - Summary::print(m_controller); + Summary::print(m_controller.get()); if (m_controller->config()->isDryRun()) { LOG_NOTICE("%s " WHITE_BOLD("OK"), Tags::config()); @@ -115,32 +111,20 @@ void xmrig::App::onSignal(int signum) switch (signum) { case SIGHUP: - LOG_WARN("%s " YELLOW("SIGHUP received, exiting"), Tags::signal()); - break; - case SIGTERM: - LOG_WARN("%s " YELLOW("SIGTERM received, exiting"), Tags::signal()); - break; - case SIGINT: - LOG_WARN("%s " YELLOW("SIGINT received, exiting"), Tags::signal()); - break; + return close(); default: - return; + break; } - - close(); } void xmrig::App::close() { - m_signals->stop(); - - if (m_console) { - m_console->stop(); - } + m_signals.reset(); + m_console.reset(); m_controller->stop(); diff --git a/src/App.h b/src/App.h index 122e8fb6..962baead 100644 --- a/src/App.h +++ b/src/App.h @@ -32,6 +32,9 @@ #include "base/tools/Object.h" +#include + + namespace xmrig { @@ -60,9 +63,9 @@ private: bool background(int &rc); void close(); - Console *m_console = nullptr; - Controller *m_controller = nullptr; - Signals *m_signals = nullptr; + std::shared_ptr m_console; + std::shared_ptr m_controller; + std::shared_ptr m_signals; }; diff --git a/src/App_unix.cpp b/src/App_unix.cpp index ae2905db..b0b80079 100644 --- a/src/App_unix.cpp +++ b/src/App_unix.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 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 @@ -36,8 +36,6 @@ bool xmrig::App::background(int &rc) { - signal(SIGPIPE, SIG_IGN); - if (!m_controller->isBackground()) { return false; } diff --git a/src/Summary.cpp b/src/Summary.cpp index a026c938..e96a222d 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -160,11 +160,11 @@ static void print_threads(Config *config) static void print_commands(Config *) { if (Log::isColors()) { - Log::print(GREEN_BOLD(" * ") WHITE_BOLD("COMMANDS ") MAGENTA_BG(WHITE_BOLD_S "h") WHITE_BOLD("ashrate, ") - MAGENTA_BG(WHITE_BOLD_S "p") WHITE_BOLD("ause, ") - MAGENTA_BG(WHITE_BOLD_S "r") WHITE_BOLD("esume, ") + Log::print(GREEN_BOLD(" * ") WHITE_BOLD("COMMANDS ") MAGENTA_BG_BOLD("h") WHITE_BOLD("ashrate, ") + MAGENTA_BG_BOLD("p") WHITE_BOLD("ause, ") + MAGENTA_BG_BOLD("r") WHITE_BOLD("esume, ") WHITE_BOLD("re") MAGENTA_BG(WHITE_BOLD_S "s") WHITE_BOLD("ults, ") - MAGENTA_BG(WHITE_BOLD_S "c") WHITE_BOLD("onnection") + MAGENTA_BG_BOLD("c") WHITE_BOLD("onnection") ); } else { diff --git a/src/backend/common/GpuWorker.cpp b/src/backend/common/GpuWorker.cpp new file mode 100644 index 00000000..5a9c3d13 --- /dev/null +++ b/src/backend/common/GpuWorker.cpp @@ -0,0 +1,52 @@ +/* XMRig + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 "backend/common/GpuWorker.h" +#include "base/tools/Chrono.h" + + +xmrig::GpuWorker::GpuWorker(size_t id, int64_t affinity, int priority, uint32_t deviceIndex) : Worker(id, affinity, priority), + m_deviceIndex(deviceIndex) +{ +} + + +void xmrig::GpuWorker::storeStats() +{ + // Get index which is unused now + const uint32_t index = m_index.load(std::memory_order_relaxed) ^ 1; + + // Fill in the data for that index + m_hashCount[index] = m_count; + m_timestamp[index] = Chrono::steadyMSecs(); + + // Switch to that index + // All data will be in memory by the time it completes thanks to std::memory_order_seq_cst + m_index.fetch_xor(1, std::memory_order_seq_cst); +} + + +void xmrig::GpuWorker::hashrateData(uint64_t &hashCount, uint64_t &timeStamp, uint64_t &rawHashes) const +{ + const uint32_t index = m_index.load(std::memory_order_relaxed); + + rawHashes = m_hashrateData.interpolate(timeStamp); + hashCount = m_hashCount[index]; + timeStamp = m_timestamp[index]; +} diff --git a/src/backend/common/GpuWorker.h b/src/backend/common/GpuWorker.h new file mode 100644 index 00000000..3c828e52 --- /dev/null +++ b/src/backend/common/GpuWorker.h @@ -0,0 +1,58 @@ +/* XMRig + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 XMRIG_GPUWORKER_H +#define XMRIG_GPUWORKER_H + + +#include + + +#include "backend/common/HashrateInterpolator.h" +#include "backend/common/Worker.h" + + +namespace xmrig { + + +class GpuWorker : public Worker +{ +public: + GpuWorker(size_t id, int64_t affinity, int priority, uint32_t m_deviceIndex); + +protected: + inline const VirtualMemory *memory() const override { return nullptr; } + inline uint32_t deviceIndex() const { return m_deviceIndex; } + + void hashrateData(uint64_t &hashCount, uint64_t &timeStamp, uint64_t &rawHashes) const override; + +protected: + void storeStats(); + + const uint32_t m_deviceIndex; + HashrateInterpolator m_hashrateData; + std::atomic m_index = {}; + uint64_t m_hashCount[2] = {}; + uint64_t m_timestamp[2] = {}; +}; + + +} // namespace xmrig + + +#endif /* XMRIG_GPUWORKER_H */ diff --git a/src/backend/common/Hashrate.cpp b/src/backend/common/Hashrate.cpp index aa4d80c7..4038e34b 100644 --- a/src/backend/common/Hashrate.cpp +++ b/src/backend/common/Hashrate.cpp @@ -1,12 +1,7 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2017-2018 XMR-Stak , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -24,7 +19,6 @@ #include -#include #include #include @@ -75,72 +69,6 @@ xmrig::Hashrate::~Hashrate() } -double xmrig::Hashrate::calc(size_t ms) const -{ - const double data = calc(0, ms); - return std::isnormal(data) ? data : 0.0; -} - - -double xmrig::Hashrate::calc(size_t threadId, size_t ms) const -{ - assert(threadId < m_threads); - if (threadId >= m_threads) { - return nan(""); - } - - uint64_t earliestHashCount = 0; - uint64_t earliestStamp = 0; - bool haveFullSet = false; - - const uint64_t timeStampLimit = xmrig::Chrono::steadyMSecs() - ms; - uint64_t* timestamps = m_timestamps[threadId]; - uint64_t* counts = m_counts[threadId]; - - const size_t idx_start = (m_top[threadId] - 1) & kBucketMask; - size_t idx = idx_start; - - uint64_t lastestStamp = timestamps[idx]; - uint64_t lastestHashCnt = counts[idx]; - - do { - if (timestamps[idx] < timeStampLimit) { - haveFullSet = (timestamps[idx] != 0); - if (idx != idx_start) { - idx = (idx + 1) & kBucketMask; - earliestStamp = timestamps[idx]; - earliestHashCount = counts[idx]; - } - break; - } - idx = (idx - 1) & kBucketMask; - } while (idx != idx_start); - - if (!haveFullSet || earliestStamp == 0 || lastestStamp == 0) { - return nan(""); - } - - if (lastestStamp - earliestStamp == 0) { - return nan(""); - } - - const auto hashes = static_cast(lastestHashCnt - earliestHashCount); - const auto time = static_cast(lastestStamp - earliestStamp) / 1000.0; - - return hashes / time; -} - - -void xmrig::Hashrate::add(size_t threadId, uint64_t count, uint64_t timestamp) -{ - const size_t top = m_top[threadId]; - m_counts[threadId][top] = count; - m_timestamps[threadId][top] = timestamp; - - m_top[threadId] = (top + 1) & kBucketMask; -} - - const char *xmrig::Hashrate::format(double h, char *buf, size_t size) { return ::format(h, buf, size); @@ -174,10 +102,69 @@ rapidjson::Value xmrig::Hashrate::toJSON(size_t threadId, rapidjson::Document &d auto &allocator = doc.GetAllocator(); Value out(kArrayType); - out.PushBack(normalize(calc(threadId + 1, ShortInterval)), allocator); - out.PushBack(normalize(calc(threadId + 1, MediumInterval)), allocator); - out.PushBack(normalize(calc(threadId + 1, LargeInterval)), allocator); + out.PushBack(normalize(calc(threadId, ShortInterval)), allocator); + out.PushBack(normalize(calc(threadId, MediumInterval)), allocator); + out.PushBack(normalize(calc(threadId, LargeInterval)), allocator); return out; } #endif + + +double xmrig::Hashrate::hashrate(size_t index, size_t ms) const +{ + assert(index < m_threads); + if (index >= m_threads) { + return nan(""); + } + + uint64_t earliestHashCount = 0; + uint64_t earliestStamp = 0; + bool haveFullSet = false; + + const uint64_t timeStampLimit = xmrig::Chrono::steadyMSecs() - ms; + uint64_t* timestamps = m_timestamps[index]; + uint64_t* counts = m_counts[index]; + + const size_t idx_start = (m_top[index] - 1) & kBucketMask; + size_t idx = idx_start; + + uint64_t lastestStamp = timestamps[idx]; + uint64_t lastestHashCnt = counts[idx]; + + do { + if (timestamps[idx] < timeStampLimit) { + haveFullSet = (timestamps[idx] != 0); + if (idx != idx_start) { + idx = (idx + 1) & kBucketMask; + earliestStamp = timestamps[idx]; + earliestHashCount = counts[idx]; + } + break; + } + idx = (idx - 1) & kBucketMask; + } while (idx != idx_start); + + if (!haveFullSet || earliestStamp == 0 || lastestStamp == 0) { + return nan(""); + } + + if (lastestStamp - earliestStamp == 0) { + return nan(""); + } + + const auto hashes = static_cast(lastestHashCnt - earliestHashCount); + const auto time = static_cast(lastestStamp - earliestStamp) / 1000.0; + + return hashes / time; +} + + +void xmrig::Hashrate::addData(size_t index, uint64_t count, uint64_t timestamp) +{ + const size_t top = m_top[index]; + m_counts[index][top] = count; + m_timestamps[index][top] = timestamp; + + m_top[index] = (top + 1) & kBucketMask; +} diff --git a/src/backend/common/Hashrate.h b/src/backend/common/Hashrate.h index 59e1afe1..ad278cfa 100644 --- a/src/backend/common/Hashrate.h +++ b/src/backend/common/Hashrate.h @@ -1,12 +1,7 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2017-2018 XMR-Stak , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 +21,7 @@ #define XMRIG_HASHRATE_H +#include #include #include @@ -42,7 +38,7 @@ class Hashrate public: XMRIG_DISABLE_COPY_MOVE_DEFAULT(Hashrate) - enum Intervals { + enum Intervals : size_t { ShortInterval = 10000, MediumInterval = 60000, LargeInterval = 900000 @@ -50,11 +46,12 @@ public: Hashrate(size_t threads); ~Hashrate(); - double calc(size_t ms) const; - double calc(size_t threadId, size_t ms) const; - void add(size_t threadId, uint64_t count, uint64_t timestamp); - inline size_t threads() const { return m_threads; } + inline double calc(size_t ms) const { const double data = hashrate(0U, ms); return std::isnormal(data) ? data : 0.0; } + inline double calc(size_t threadId, size_t ms) const { return hashrate(threadId + 1, ms); } + inline size_t threads() const { return m_threads > 0U ? m_threads - 1U : 0U; } + inline void add(size_t threadId, uint64_t count, uint64_t timestamp) { addData(threadId + 1U, count, timestamp); } + inline void add(uint64_t count, uint64_t timestamp) { addData(0U, count, timestamp); } static const char *format(double h, char *buf, size_t size); static rapidjson::Value normalize(double d); @@ -65,6 +62,9 @@ public: # endif private: + double hashrate(size_t index, size_t ms) const; + void addData(size_t index, uint64_t count, uint64_t timestamp); + constexpr static size_t kBucketSize = 2 << 11; constexpr static size_t kBucketMask = kBucketSize - 1; diff --git a/src/backend/common/HashrateInterpolator.cpp b/src/backend/common/HashrateInterpolator.cpp index 2edda6a6..0ac11514 100644 --- a/src/backend/common/HashrateInterpolator.cpp +++ b/src/backend/common/HashrateInterpolator.cpp @@ -1,10 +1,4 @@ /* 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-2020 SChernykh * Copyright 2016-2020 XMRig , * diff --git a/src/backend/common/HashrateInterpolator.h b/src/backend/common/HashrateInterpolator.h index b4c7b8c7..65e62443 100644 --- a/src/backend/common/HashrateInterpolator.h +++ b/src/backend/common/HashrateInterpolator.h @@ -1,10 +1,4 @@ /* 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-2020 SChernykh * Copyright 2016-2020 XMRig , * @@ -47,7 +41,7 @@ public: private: // Buffer of hashrate counters, used for linear interpolation of past data mutable std::mutex m_lock; - std::deque> m_data; + std::deque > m_data; }; diff --git a/src/backend/common/Worker.cpp b/src/backend/common/Worker.cpp index cf244ab3..b24ac280 100644 --- a/src/backend/common/Worker.cpp +++ b/src/backend/common/Worker.cpp @@ -1,13 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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,7 +19,6 @@ #include "backend/common/Worker.h" #include "base/kernel/Platform.h" -#include "base/tools/Chrono.h" #include "crypto/common/VirtualMemory.h" @@ -39,27 +31,3 @@ xmrig::Worker::Worker(size_t id, int64_t affinity, int priority) : Platform::trySetThreadAffinity(affinity); Platform::setThreadPriority(priority); } - - -void xmrig::Worker::storeStats() -{ - // Get index which is unused now - const uint32_t index = m_index.load(std::memory_order_relaxed) ^ 1; - - // Fill in the data for that index - m_hashCount[index] = m_count; - m_timestamp[index] = Chrono::steadyMSecs(); - - // Switch to that index - // All data will be in memory by the time it completes thanks to std::memory_order_seq_cst - m_index.fetch_xor(1, std::memory_order_seq_cst); -} - - -void xmrig::Worker::getHashrateData(uint64_t& hashCount, uint64_t& timeStamp) const -{ - const uint32_t index = m_index.load(std::memory_order_relaxed); - - hashCount = m_hashCount[index]; - timeStamp = m_timestamp[index]; -} diff --git a/src/backend/common/Worker.h b/src/backend/common/Worker.h index 23c592b9..f4c12ea5 100644 --- a/src/backend/common/Worker.h +++ b/src/backend/common/Worker.h @@ -1,13 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -27,9 +20,6 @@ #define XMRIG_WORKER_H -#include - - #include "backend/common/interfaces/IWorker.h" @@ -41,23 +31,17 @@ class Worker : public IWorker public: Worker(size_t id, int64_t affinity, int priority); - inline const VirtualMemory *memory() const override { return nullptr; } - inline size_t id() const override { return m_id; } - inline uint64_t rawHashes() const override { return m_count; } - inline void jobEarlyNotification(const Job&) override {} - - void getHashrateData(uint64_t& hashCount, uint64_t& timeStamp) const override; - protected: - void storeStats(); + inline int64_t affinity() const { return m_affinity; } + inline size_t id() const override { return m_id; } + inline uint32_t node() const { return m_node; } + uint64_t m_count = 0; + +private: const int64_t m_affinity; const size_t m_id; - std::atomic m_index = {}; uint32_t m_node = 0; - uint64_t m_count = 0; - uint64_t m_hashCount[2] = {}; - uint64_t m_timestamp[2] = {}; }; diff --git a/src/backend/common/Workers.cpp b/src/backend/common/Workers.cpp index 16265b0a..f015b5f7 100644 --- a/src/backend/common/Workers.cpp +++ b/src/backend/common/Workers.cpp @@ -1,13 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -24,16 +17,13 @@ */ +#include "backend/common/Workers.h" #include "backend/common/Hashrate.h" #include "backend/common/interfaces/IBackend.h" -#include "backend/common/Workers.h" #include "backend/cpu/CpuWorker.h" #include "base/io/log/Log.h" #include "base/io/log/Tags.h" -#include "base/net/stratum/Pool.h" #include "base/tools/Chrono.h" -#include "base/tools/Object.h" -#include "core/Miner.h" #ifdef XMRIG_FEATURE_OPENCL @@ -59,7 +49,6 @@ class WorkersPrivate public: XMRIG_DISABLE_COPY_MOVE(WorkersPrivate) - WorkersPrivate() = default; ~WorkersPrivate() = default; @@ -87,20 +76,6 @@ xmrig::Workers::~Workers() } -template -static void getHashrateData(xmrig::IWorker* worker, uint64_t& hashCount, uint64_t& timeStamp) -{ - worker->getHashrateData(hashCount, timeStamp); -} - - -template<> -void getHashrateData(xmrig::IWorker* worker, uint64_t& hashCount, uint64_t&) -{ - hashCount = worker->rawHashes(); -} - - template bool xmrig::Workers::tick(uint64_t) { @@ -111,33 +86,32 @@ bool xmrig::Workers::tick(uint64_t) uint64_t ts = Chrono::steadyMSecs(); bool totalAvailable = true; uint64_t totalHashCount = 0; + uint64_t hashCount = 0; + uint64_t rawHashes = 0; for (Thread *handle : m_workers) { IWorker *worker = handle->worker(); if (worker) { - uint64_t hashCount; - getHashrateData(worker, hashCount, ts); - d_ptr->hashrate->add(handle->id() + 1, hashCount, ts); + worker->hashrateData(hashCount, ts, rawHashes); + d_ptr->hashrate->add(handle->id(), hashCount, ts); - const uint64_t n = worker->rawHashes(); - if (n == 0) { + if (rawHashes == 0) { totalAvailable = false; } - totalHashCount += n; + + totalHashCount += rawHashes; } } if (totalAvailable) { - d_ptr->hashrate->add(0, totalHashCount, Chrono::steadyMSecs()); + d_ptr->hashrate->add(totalHashCount, Chrono::steadyMSecs()); } # ifdef XMRIG_FEATURE_BENCHMARK - if (d_ptr->benchmark && d_ptr->benchmark->finish(totalHashCount)) { - return false; - } -# endif - + return !d_ptr->benchmark || !d_ptr->benchmark->finish(totalHashCount); +# else return true; +# endif } @@ -158,14 +132,19 @@ void xmrig::Workers::setBackend(IBackend *backend) template void xmrig::Workers::stop() { +# ifdef XMRIG_MINER_PROJECT Nonce::stop(T::backend()); +# endif for (Thread *worker : m_workers) { delete worker; } m_workers.clear(); + +# ifdef XMRIG_MINER_PROJECT Nonce::touch(T::backend()); +# endif d_ptr->hashrate.reset(); } @@ -226,7 +205,10 @@ void xmrig::Workers::start(const std::vector &data, bool sleep) } d_ptr->hashrate = std::make_shared(m_workers.size()); + +# ifdef XMRIG_MINER_PROJECT Nonce::touch(T::backend()); +# endif for (auto worker : m_workers) { worker->start(Workers::onReady); @@ -247,6 +229,7 @@ namespace xmrig { template<> xmrig::IWorker *xmrig::Workers::create(Thread *handle) { +# ifdef XMRIG_MINER_PROJECT switch (handle->config().intensity) { case 1: return new CpuWorker<1>(handle->id(), handle->config()); @@ -265,6 +248,11 @@ xmrig::IWorker *xmrig::Workers::create(Thread *han } return nullptr; +# else + assert(handle->config().intensity == 1); + + return new CpuWorker<1>(handle->id(), handle->config()); +# endif } diff --git a/src/backend/common/Workers.h b/src/backend/common/Workers.h index 5cdc78a2..1313384c 100644 --- a/src/backend/common/Workers.h +++ b/src/backend/common/Workers.h @@ -1,13 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -44,10 +37,9 @@ namespace xmrig { +class Benchmark; class Hashrate; class WorkersPrivate; -class Job; -class Benchmark; template @@ -63,7 +55,7 @@ public: bool tick(uint64_t ticks); const Hashrate *hashrate() const; - void jobEarlyNotification(const Job&); + void jobEarlyNotification(const Job &job); void setBackend(IBackend *backend); void stop(); @@ -83,7 +75,7 @@ private: template -void xmrig::Workers::jobEarlyNotification(const Job& job) +void xmrig::Workers::jobEarlyNotification(const Job &job) { for (Thread* t : m_workers) { if (t->worker()) { diff --git a/src/backend/common/common.cmake b/src/backend/common/common.cmake index 95c72440..52a5ee6f 100644 --- a/src/backend/common/common.cmake +++ b/src/backend/common/common.cmake @@ -1,6 +1,5 @@ set(HEADERS_BACKEND_COMMON src/backend/common/Hashrate.h - src/backend/common/HashrateInterpolator.h src/backend/common/Tags.h src/backend/common/interfaces/IBackend.h src/backend/common/interfaces/IRxListener.h @@ -16,7 +15,6 @@ set(HEADERS_BACKEND_COMMON set(SOURCES_BACKEND_COMMON src/backend/common/Hashrate.cpp - src/backend/common/HashrateInterpolator.cpp src/backend/common/Threads.cpp src/backend/common/Worker.cpp src/backend/common/Workers.cpp @@ -35,3 +33,16 @@ if (WITH_RANDOMX AND WITH_BENCHMARK) src/backend/common/benchmark/BenchState.cpp ) endif() + + +if (WITH_OPENCL OR WITH_CUDA) + list(APPEND HEADERS_BACKEND_COMMON + src/backend/common/HashrateInterpolator.h + src/backend/common/GpuWorker.h + ) + + list(APPEND SOURCES_BACKEND_COMMON + src/backend/common/HashrateInterpolator.cpp + src/backend/common/GpuWorker.cpp + ) +endif() diff --git a/src/backend/common/interfaces/IBackend.h b/src/backend/common/interfaces/IBackend.h index c07ee49a..53e8e0f1 100644 --- a/src/backend/common/interfaces/IBackend.h +++ b/src/backend/common/interfaces/IBackend.h @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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,10 +20,11 @@ #define XMRIG_IBACKEND_H -#include - - #include "3rdparty/rapidjson/fwd.h" +#include "base/tools/Object.h" + + +#include namespace xmrig { @@ -47,10 +42,14 @@ class String; class IBackend { public: + XMRIG_DISABLE_COPY_MOVE(IBackend) + + IBackend() = default; virtual ~IBackend() = default; virtual bool isEnabled() const = 0; virtual bool isEnabled(const Algorithm &algorithm) const = 0; + virtual bool tick(uint64_t ticks) = 0; virtual const Hashrate *hashrate() const = 0; virtual const String &profileName() const = 0; virtual const String &type() const = 0; @@ -61,7 +60,6 @@ public: virtual void setJob(const Job &job) = 0; virtual void start(IWorker *worker, bool ready) = 0; virtual void stop() = 0; - virtual bool tick(uint64_t ticks) = 0; # ifdef XMRIG_FEATURE_API virtual rapidjson::Value toJSON(rapidjson::Document &doc) const = 0; diff --git a/src/backend/common/interfaces/IMemoryPool.h b/src/backend/common/interfaces/IMemoryPool.h index 44ff2495..10d27b50 100644 --- a/src/backend/common/interfaces/IMemoryPool.h +++ b/src/backend/common/interfaces/IMemoryPool.h @@ -1,14 +1,6 @@ /* 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 2018-2019 tevador - * Copyright 2016-2019 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -28,6 +20,9 @@ #define XMRIG_IMEMORYPOOL_H +#include "base/tools/Object.h" + + #include #include @@ -38,7 +33,10 @@ namespace xmrig { class IMemoryPool { public: - virtual ~IMemoryPool() = default; + XMRIG_DISABLE_COPY_MOVE(IMemoryPool) + + IMemoryPool() = default; + virtual ~IMemoryPool() = default; virtual bool isHugePages(uint32_t node) const = 0; virtual uint8_t *get(size_t size, uint32_t node) = 0; diff --git a/src/backend/common/interfaces/IRxListener.h b/src/backend/common/interfaces/IRxListener.h index b4dde9e5..11adb0d4 100644 --- a/src/backend/common/interfaces/IRxListener.h +++ b/src/backend/common/interfaces/IRxListener.h @@ -1,10 +1,7 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2016-2018 XMRig + * Copyright (c) 2018-2019 tevador + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -24,13 +21,19 @@ #define XMRIG_IRXLISTENER_H +#include "base/tools/Object.h" + + namespace xmrig { class IRxListener { public: - virtual ~IRxListener() = default; + XMRIG_DISABLE_COPY_MOVE(IRxListener) + + IRxListener() = default; + virtual ~IRxListener() = default; # ifdef XMRIG_ALGO_RANDOMX virtual void onDatasetReady() = 0; diff --git a/src/backend/common/interfaces/IRxStorage.h b/src/backend/common/interfaces/IRxStorage.h index ff447044..ab287a76 100644 --- a/src/backend/common/interfaces/IRxStorage.h +++ b/src/backend/common/interfaces/IRxStorage.h @@ -1,10 +1,7 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2016-2018 XMRig + * Copyright (c) 2018-2019 tevador + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -24,8 +21,9 @@ #define XMRIG_IRXSTORAGE_H -#include "crypto/rx/RxConfig.h" +#include "base/tools/Object.h" #include "crypto/common/HugePagesInfo.h" +#include "crypto/rx/RxConfig.h" #include @@ -43,7 +41,10 @@ class RxSeed; class IRxStorage { public: - virtual ~IRxStorage() = default; + XMRIG_DISABLE_COPY_MOVE(IRxStorage) + + IRxStorage() = default; + virtual ~IRxStorage() = default; virtual bool isAllocated() const = 0; virtual HugePagesInfo hugePages() const = 0; diff --git a/src/backend/common/interfaces/IWorker.h b/src/backend/common/interfaces/IWorker.h index f528469a..7ed6ff00 100644 --- a/src/backend/common/interfaces/IWorker.h +++ b/src/backend/common/interfaces/IWorker.h @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -36,8 +30,8 @@ namespace xmrig { -class VirtualMemory; class Job; +class VirtualMemory; class IWorker @@ -48,14 +42,13 @@ public: IWorker() = default; virtual ~IWorker() = default; - virtual bool selfTest() = 0; - virtual const VirtualMemory *memory() const = 0; - virtual size_t id() const = 0; - virtual size_t intensity() const = 0; - virtual uint64_t rawHashes() const = 0; - virtual void getHashrateData(uint64_t&, uint64_t&) const = 0; - virtual void start() = 0; - virtual void jobEarlyNotification(const Job&) = 0; + virtual bool selfTest() = 0; + virtual const VirtualMemory *memory() const = 0; + virtual size_t id() const = 0; + virtual size_t intensity() const = 0; + virtual void hashrateData(uint64_t &hashCount, uint64_t &timeStamp, uint64_t &rawHashes) const = 0; + virtual void jobEarlyNotification(const Job &job) = 0; + virtual void start() = 0; }; diff --git a/src/backend/cpu/CpuBackend.cpp b/src/backend/cpu/CpuBackend.cpp index fc6b7e74..1e47a6c5 100644 --- a/src/backend/cpu/CpuBackend.cpp +++ b/src/backend/cpu/CpuBackend.cpp @@ -266,6 +266,12 @@ bool xmrig::CpuBackend::isEnabled(const Algorithm &algorithm) const } +bool xmrig::CpuBackend::tick(uint64_t ticks) +{ + return d_ptr->workers.tick(ticks); +} + + const xmrig::Hashrate *xmrig::CpuBackend::hashrate() const { return d_ptr->workers.hashrate(); @@ -316,9 +322,9 @@ void xmrig::CpuBackend::printHashrate(bool details) Log::print("| %8zu | %8" PRId64 " | %7s | %7s | %7s |", i, data.affinity, - Hashrate::format(hashrate()->calc(i + 1, Hashrate::ShortInterval), num, sizeof num / 3), - Hashrate::format(hashrate()->calc(i + 1, Hashrate::MediumInterval), num + 8, sizeof num / 3), - Hashrate::format(hashrate()->calc(i + 1, Hashrate::LargeInterval), num + 8 * 2, sizeof num / 3) + Hashrate::format(hashrate()->calc(i, Hashrate::ShortInterval), num, sizeof num / 3), + Hashrate::format(hashrate()->calc(i, Hashrate::MediumInterval), num + 8, sizeof num / 3), + Hashrate::format(hashrate()->calc(i, Hashrate::LargeInterval), num + 8 * 2, sizeof num / 3) ); i++; @@ -405,12 +411,6 @@ void xmrig::CpuBackend::stop() } -bool xmrig::CpuBackend::tick(uint64_t ticks) -{ - return d_ptr->workers.tick(ticks); -} - - #ifdef XMRIG_FEATURE_API rapidjson::Value xmrig::CpuBackend::toJSON(rapidjson::Document &doc) const { diff --git a/src/backend/cpu/CpuBackend.h b/src/backend/cpu/CpuBackend.h index 5be933c3..2f697b8b 100644 --- a/src/backend/cpu/CpuBackend.h +++ b/src/backend/cpu/CpuBackend.h @@ -54,6 +54,7 @@ protected: bool isEnabled() const override; bool isEnabled(const Algorithm &algorithm) const override; + bool tick(uint64_t ticks) override; const Hashrate *hashrate() const override; const String &profileName() const override; const String &type() const override; @@ -63,7 +64,6 @@ protected: void setJob(const Job &job) override; void start(IWorker *worker, bool ready) override; void stop() override; - bool tick(uint64_t ticks) override; # ifdef XMRIG_FEATURE_API rapidjson::Value toJSON(rapidjson::Document &doc) const override; diff --git a/src/backend/cpu/CpuWorker.cpp b/src/backend/cpu/CpuWorker.cpp index 778f8e46..f55d153c 100644 --- a/src/backend/cpu/CpuWorker.cpp +++ b/src/backend/cpu/CpuWorker.cpp @@ -1,13 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -80,7 +73,7 @@ xmrig::CpuWorker::CpuWorker(size_t id, const CpuLaunchData &data) : m_threads(data.threads), m_ctx() { - m_memory = new VirtualMemory(m_algorithm.l3() * N, data.hugePages, false, true, m_node); + m_memory = new VirtualMemory(m_algorithm.l3() * N, data.hugePages, false, true, node()); } @@ -100,7 +93,7 @@ xmrig::CpuWorker::~CpuWorker() template void xmrig::CpuWorker::allocateRandomX_VM() { - RxDataset *dataset = Rx::dataset(m_job.currentJob(), m_node); + RxDataset *dataset = Rx::dataset(m_job.currentJob(), node()); while (dataset == nullptr) { std::this_thread::sleep_for(std::chrono::milliseconds(200)); @@ -109,13 +102,13 @@ void xmrig::CpuWorker::allocateRandomX_VM() return; } - dataset = Rx::dataset(m_job.currentJob(), m_node); + dataset = Rx::dataset(m_job.currentJob(), node()); } if (!m_vm) { // Try to allocate scratchpad from dataset's 1 GB huge pages, if normal huge pages are not available uint8_t* scratchpad = m_memory->isHugePages() ? m_memory->scratchpad() : dataset->tryAllocateScrathpad(); - m_vm = RxVm::create(dataset, scratchpad ? scratchpad : m_memory->scratchpad(), !m_hwAES, m_assembly, m_node); + m_vm = RxVm::create(dataset, scratchpad ? scratchpad : m_memory->scratchpad(), !m_hwAES, m_assembly, node()); } } #endif @@ -189,6 +182,14 @@ bool xmrig::CpuWorker::selfTest() } +template +void xmrig::CpuWorker::hashrateData(uint64_t &hashCount, uint64_t &, uint64_t &rawHashes) const +{ + hashCount = m_count; + rawHashes = m_count; +} + + template void xmrig::CpuWorker::start() { diff --git a/src/backend/cpu/CpuWorker.h b/src/backend/cpu/CpuWorker.h index 06c92fa4..785763af 100644 --- a/src/backend/cpu/CpuWorker.h +++ b/src/backend/cpu/CpuWorker.h @@ -1,13 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -56,10 +49,12 @@ public: protected: bool selfTest() override; + void hashrateData(uint64_t &hashCount, uint64_t &timeStamp, uint64_t &rawHashes) const override; void start() override; - inline const VirtualMemory *memory() const override { return m_memory; } - inline size_t intensity() const override { return N; } + inline const VirtualMemory *memory() const override { return m_memory; } + inline size_t intensity() const override { return N; } + inline void jobEarlyNotification(const Job&) override {} private: inline cn_hash_fun fn(const Algorithm &algorithm) const { return CnHash::fn(algorithm, m_av, m_assembly); } diff --git a/src/backend/cpu/interfaces/ICpuInfo.h b/src/backend/cpu/interfaces/ICpuInfo.h index 9270ca09..7464781a 100644 --- a/src/backend/cpu/interfaces/ICpuInfo.h +++ b/src/backend/cpu/interfaces/ICpuInfo.h @@ -1,12 +1,6 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2019 XMR-Stak , - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 diff --git a/src/backend/cpu/platform/HwlocCpuInfo.cpp b/src/backend/cpu/platform/HwlocCpuInfo.cpp index 0d587332..6f6cb4b7 100644 --- a/src/backend/cpu/platform/HwlocCpuInfo.cpp +++ b/src/backend/cpu/platform/HwlocCpuInfo.cpp @@ -1,12 +1,6 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2019 XMR-Stak , - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 diff --git a/src/backend/cpu/platform/HwlocCpuInfo.h b/src/backend/cpu/platform/HwlocCpuInfo.h index eed3ae8b..1a24f425 100644 --- a/src/backend/cpu/platform/HwlocCpuInfo.h +++ b/src/backend/cpu/platform/HwlocCpuInfo.h @@ -1,12 +1,6 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2019 XMR-Stak , - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 diff --git a/src/backend/cuda/CudaBackend.cpp b/src/backend/cuda/CudaBackend.cpp index ab5b22e5..59eaab1f 100644 --- a/src/backend/cuda/CudaBackend.cpp +++ b/src/backend/cuda/CudaBackend.cpp @@ -409,9 +409,9 @@ void xmrig::CudaBackend::printHashrate(bool details) Log::print("| %8zu | %8" PRId64 " | %8s | %8s | %8s |" CYAN_BOLD(" #%u") YELLOW(" %s") GREEN(" %s"), i, data.thread.affinity(), - Hashrate::format(hashrate()->calc(i + 1, Hashrate::ShortInterval) * scale, num, sizeof num / 3), - Hashrate::format(hashrate()->calc(i + 1, Hashrate::MediumInterval) * scale, num + 16, sizeof num / 3), - Hashrate::format(hashrate()->calc(i + 1, Hashrate::LargeInterval) * scale, num + 16 * 2, sizeof num / 3), + Hashrate::format(hashrate()->calc(i, Hashrate::ShortInterval) * scale, num, sizeof num / 3), + Hashrate::format(hashrate()->calc(i, Hashrate::MediumInterval) * scale, num + 16, sizeof num / 3), + Hashrate::format(hashrate()->calc(i, Hashrate::LargeInterval) * scale, num + 16 * 2, sizeof num / 3), data.device.index(), data.device.topology().toString().data(), data.device.name().data() diff --git a/src/backend/cuda/CudaWorker.cpp b/src/backend/cuda/CudaWorker.cpp index 7bb8844c..3585308f 100644 --- a/src/backend/cuda/CudaWorker.cpp +++ b/src/backend/cuda/CudaWorker.cpp @@ -1,13 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -61,7 +54,7 @@ static constexpr uint32_t kReserveCount = 32768; std::atomic CudaWorker::ready; -static inline bool isReady() { return !Nonce::isPaused() && CudaWorker::ready; } +static inline bool isReady() { return !Nonce::isPaused() && CudaWorker::ready; } } // namespace xmrig @@ -69,10 +62,9 @@ static inline bool isReady() { return !Nonce::isPaused() xmrig::CudaWorker::CudaWorker(size_t id, const CudaLaunchData &data) : - Worker(id, data.thread.affinity(), -1), + GpuWorker(id, data.thread.affinity(), -1, data.device.index()), m_algorithm(data.algorithm), - m_miner(data.miner), - m_deviceIndex(data.device.index()) + m_miner(data.miner) { switch (m_algorithm.family()) { case Algorithm::RANDOM_X: @@ -119,13 +111,7 @@ xmrig::CudaWorker::~CudaWorker() } -uint64_t xmrig::CudaWorker::rawHashes() const -{ - return m_hashrateData.interpolate(Chrono::steadyMSecs()); -} - - -void xmrig::CudaWorker::jobEarlyNotification(const Job& job) +void xmrig::CudaWorker::jobEarlyNotification(const Job &job) { if (m_runner) { m_runner->jobEarlyNotification(job); @@ -213,5 +199,5 @@ void xmrig::CudaWorker::storeStats() const uint64_t timeStamp = Chrono::steadyMSecs(); m_hashrateData.addDataPoint(m_count, timeStamp); - Worker::storeStats(); + GpuWorker::storeStats(); } diff --git a/src/backend/cuda/CudaWorker.h b/src/backend/cuda/CudaWorker.h index e82e3425..24cf1e65 100644 --- a/src/backend/cuda/CudaWorker.h +++ b/src/backend/cuda/CudaWorker.h @@ -1,13 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -27,8 +20,7 @@ #define XMRIG_CUDAWORKER_H -#include "backend/common/HashrateInterpolator.h" -#include "backend/common/Worker.h" +#include "backend/common/GpuWorker.h" #include "backend/common/WorkerJob.h" #include "backend/cuda/CudaLaunchData.h" #include "base/tools/Object.h" @@ -41,7 +33,7 @@ namespace xmrig { class ICudaRunner; -class CudaWorker : public Worker +class CudaWorker : public GpuWorker { public: XMRIG_DISABLE_COPY_MOVE_DEFAULT(CudaWorker) @@ -50,8 +42,7 @@ public: ~CudaWorker() override; - uint64_t rawHashes() const override; - void jobEarlyNotification(const Job&) override; + void jobEarlyNotification(const Job &job) override; static std::atomic ready; @@ -68,9 +59,6 @@ private: const Miner *m_miner; ICudaRunner *m_runner = nullptr; WorkerJob<1> m_job; - uint32_t m_deviceIndex; - - HashrateInterpolator m_hashrateData; }; diff --git a/src/backend/opencl/OclBackend.cpp b/src/backend/opencl/OclBackend.cpp index 7b99700b..d0d04f37 100644 --- a/src/backend/opencl/OclBackend.cpp +++ b/src/backend/opencl/OclBackend.cpp @@ -385,9 +385,9 @@ void xmrig::OclBackend::printHashrate(bool details) Log::print("| %8zu | %8" PRId64 " | %8s | %8s | %8s |" CYAN_BOLD(" #%u") YELLOW(" %s") " %s", i, data.affinity, - Hashrate::format(hashrate()->calc(i + 1, Hashrate::ShortInterval) * scale, num, sizeof num / 3), - Hashrate::format(hashrate()->calc(i + 1, Hashrate::MediumInterval) * scale, num + 16, sizeof num / 3), - Hashrate::format(hashrate()->calc(i + 1, Hashrate::LargeInterval) * scale, num + 16 * 2, sizeof num / 3), + Hashrate::format(hashrate()->calc(i, Hashrate::ShortInterval) * scale, num, sizeof num / 3), + Hashrate::format(hashrate()->calc(i, Hashrate::MediumInterval) * scale, num + 16, sizeof num / 3), + Hashrate::format(hashrate()->calc(i, Hashrate::LargeInterval) * scale, num + 16 * 2, sizeof num / 3), data.device.index(), data.device.topology().toString().data(), data.device.printableName().data() diff --git a/src/backend/opencl/OclWorker.cpp b/src/backend/opencl/OclWorker.cpp index ac493ab5..ee5c4a04 100644 --- a/src/backend/opencl/OclWorker.cpp +++ b/src/backend/opencl/OclWorker.cpp @@ -1,13 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -59,7 +52,7 @@ namespace xmrig { std::atomic OclWorker::ready; -static inline bool isReady() { return !Nonce::isPaused() && OclWorker::ready; } +static inline bool isReady() { return !Nonce::isPaused() && OclWorker::ready; } static inline void printError(size_t id, const char *error) @@ -73,11 +66,10 @@ static inline void printError(size_t id, const char *error) xmrig::OclWorker::OclWorker(size_t id, const OclLaunchData &data) : - Worker(id, data.affinity, -1), + GpuWorker(id, data.affinity, -1, data.device.index()), m_algorithm(data.algorithm), m_miner(data.miner), - m_sharedData(OclSharedState::get(data.device.index())), - m_deviceIndex(data.device.index()) + m_sharedData(OclSharedState::get(data.device.index())) { switch (m_algorithm.family()) { case Algorithm::RANDOM_X: @@ -137,13 +129,7 @@ xmrig::OclWorker::~OclWorker() } -uint64_t xmrig::OclWorker::rawHashes() const -{ - return m_hashrateData.interpolate(Chrono::steadyMSecs()); -} - - -void xmrig::OclWorker::jobEarlyNotification(const Job& job) +void xmrig::OclWorker::jobEarlyNotification(const Job &job) { if (m_runner) { m_runner->jobEarlyNotification(job); @@ -180,7 +166,7 @@ void xmrig::OclWorker::start() break; } - m_sharedData.resumeDelay(m_id); + m_sharedData.resumeDelay(id()); if (!consumeJob()) { return; @@ -188,7 +174,7 @@ void xmrig::OclWorker::start() } while (!Nonce::isOutdated(Nonce::OPENCL, m_job.sequence())) { - m_sharedData.adjustDelay(m_id); + m_sharedData.adjustDelay(id()); const uint64_t t = Chrono::steadyMSecs(); @@ -254,5 +240,5 @@ void xmrig::OclWorker::storeStats(uint64_t t) m_sharedData.setRunTime(timeStamp - t); - Worker::storeStats(); + GpuWorker::storeStats(); } diff --git a/src/backend/opencl/OclWorker.h b/src/backend/opencl/OclWorker.h index 403a0765..4dfe2a77 100644 --- a/src/backend/opencl/OclWorker.h +++ b/src/backend/opencl/OclWorker.h @@ -1,13 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -27,8 +20,7 @@ #define XMRIG_OCLWORKER_H -#include "backend/common/HashrateInterpolator.h" -#include "backend/common/Worker.h" +#include "backend/common/GpuWorker.h" #include "backend/common/WorkerJob.h" #include "backend/opencl/OclLaunchData.h" #include "base/tools/Object.h" @@ -42,7 +34,7 @@ class IOclRunner; class Job; -class OclWorker : public Worker +class OclWorker : public GpuWorker { public: XMRIG_DISABLE_COPY_MOVE_DEFAULT(OclWorker) @@ -51,8 +43,7 @@ public: ~OclWorker() override; - uint64_t rawHashes() const override; - void jobEarlyNotification(const Job&) override; + void jobEarlyNotification(const Job &job) override; static std::atomic ready; @@ -70,9 +61,6 @@ private: IOclRunner *m_runner = nullptr; OclSharedData &m_sharedData; WorkerJob<1> m_job; - uint32_t m_deviceIndex; - - HashrateInterpolator m_hashrateData; }; diff --git a/src/base/api/Api.cpp b/src/base/api/Api.cpp index 85c9dd60..4baebbf3 100644 --- a/src/base/api/Api.cpp +++ b/src/base/api/Api.cpp @@ -34,8 +34,8 @@ #include "base/io/Env.h" #include "base/io/json/Json.h" #include "base/kernel/Base.h" -#include "base/tools/Buffer.h" #include "base/tools/Chrono.h" +#include "base/tools/Cvt.h" #include "core/config/Config.h" #include "core/Controller.h" #include "version.h" @@ -222,13 +222,13 @@ void xmrig::Api::genId(const String &id) const size_t inSize = (sizeof(APP_KIND) - 1) + addrSize + sizeof(uint16_t); const auto port = static_cast(m_base->config()->http().port()); - auto*input = new uint8_t[inSize](); + auto *input = new uint8_t[inSize](); memcpy(input, &port, sizeof(uint16_t)); memcpy(input + sizeof(uint16_t), interfaces[i].phys_addr, addrSize); memcpy(input + sizeof(uint16_t) + addrSize, APP_KIND, (sizeof(APP_KIND) - 1)); keccak(input, inSize, hash); - Buffer::toHex(hash, 8, m_id); + Cvt::toHex(m_id, sizeof(m_id), hash, 8); delete [] input; break; diff --git a/src/base/base.cmake b/src/base/base.cmake index 04506927..cd6ee88b 100644 --- a/src/base/base.cmake +++ b/src/base/base.cmake @@ -22,6 +22,7 @@ set(HEADERS_BASE src/base/kernel/config/BaseConfig.h src/base/kernel/config/BaseTransform.h src/base/kernel/config/Title.h + src/base/kernel/constants.h src/base/kernel/Entry.h src/base/kernel/interfaces/IAsyncListener.h src/base/kernel/interfaces/IBaseListener.h @@ -66,6 +67,7 @@ set(HEADERS_BASE src/base/tools/Baton.h src/base/tools/Buffer.h src/base/tools/Chrono.h + src/base/tools/Cvt.h src/base/tools/Handle.h src/base/tools/String.h src/base/tools/Timer.h @@ -113,7 +115,7 @@ set(SOURCES_BASE src/base/net/tools/LineReader.cpp src/base/net/tools/NetBuffer.cpp src/base/tools/Arguments.cpp - src/base/tools/Buffer.cpp + src/base/tools/Cvt.cpp src/base/tools/String.cpp src/base/tools/Timer.cpp ) @@ -230,13 +232,6 @@ if (WITH_KAWPOW) ) endif() -if (WITH_PROFILING) - add_definitions(/DXMRIG_FEATURE_PROFILING) - - list(APPEND HEADERS_BASE src/base/tools/Profiler.h) - list(APPEND SOURCES_BASE src/base/tools/Profiler.cpp) -endif() - if (WITH_RANDOMX AND WITH_BENCHMARK) add_definitions(/DXMRIG_FEATURE_BENCHMARK) diff --git a/src/base/io/Console.cpp b/src/base/io/Console.cpp index bba73035..5af7e4a4 100644 --- a/src/base/io/Console.cpp +++ b/src/base/io/Console.cpp @@ -1,12 +1,6 @@ /* 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-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -50,20 +44,9 @@ xmrig::Console::Console(IConsoleListener *listener) xmrig::Console::~Console() { - stop(); -} - - -void xmrig::Console::stop() -{ - if (!m_tty) { - return; - } - uv_tty_reset_mode(); Handle::close(m_tty); - m_tty = nullptr; } diff --git a/src/base/io/Console.h b/src/base/io/Console.h index 0a075348..65523b94 100644 --- a/src/base/io/Console.h +++ b/src/base/io/Console.h @@ -1,12 +1,6 @@ /* 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-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -29,7 +23,16 @@ #include "base/tools/Object.h" -#include +using uv_buf_t = struct uv_buf_t; +using uv_handle_t = struct uv_handle_s; +using uv_stream_t = struct uv_stream_s; +using uv_tty_t = struct uv_tty_s; + +#ifdef XMRIG_OS_WIN +using ssize_t = intptr_t; +#else +# include +#endif namespace xmrig { @@ -46,8 +49,6 @@ public: Console(IConsoleListener *listener); ~Console(); - void stop(); - private: bool isSupported() const; diff --git a/src/base/io/Signals.cpp b/src/base/io/Signals.cpp index 00ec8c17..dfe4a89b 100644 --- a/src/base/io/Signals.cpp +++ b/src/base/io/Signals.cpp @@ -1,12 +1,6 @@ /* 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-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -23,23 +17,30 @@ */ -#include - - -#include "base/io/Signals.h" #include "base/kernel/interfaces/ISignalListener.h" +#include "base/io/log/Log.h" +#include "base/io/log/Tags.h" +#include "base/io/Signals.h" #include "base/tools/Handle.h" +#ifdef SIGUSR1 +static const int signums[xmrig::Signals::kSignalsCount] = { SIGHUP, SIGINT, SIGTERM, SIGUSR1 }; +#else static const int signums[xmrig::Signals::kSignalsCount] = { SIGHUP, SIGINT, SIGTERM }; +#endif xmrig::Signals::Signals(ISignalListener *listener) : m_listener(listener) { +# ifndef XMRIG_OS_WIN + signal(SIGPIPE, SIG_IGN); +# endif + for (size_t i = 0; i < kSignalsCount; ++i) { - uv_signal_t *signal = new uv_signal_t; - signal->data = this; + auto signal = new uv_signal_t; + signal->data = this; m_signals[i] = signal; @@ -51,24 +52,37 @@ xmrig::Signals::Signals(ISignalListener *listener) xmrig::Signals::~Signals() { - stop(); -} - - -void xmrig::Signals::stop() -{ - if (!m_signals[0]) { - return; - } - - for (size_t i = 0; i < kSignalsCount; ++i) { - Handle::close(m_signals[i]); - m_signals[i] = nullptr; + for (auto signal : m_signals) { + Handle::close(signal); } } void xmrig::Signals::onSignal(uv_signal_t *handle, int signum) { + switch (signum) + { + case SIGHUP: + LOG_WARN("%s " YELLOW("SIGHUP received, exiting"), Tags::signal()); + break; + + case SIGTERM: + LOG_WARN("%s " YELLOW("SIGTERM received, exiting"), Tags::signal()); + break; + + case SIGINT: + LOG_WARN("%s " YELLOW("SIGINT received, exiting"), Tags::signal()); + break; + +# ifdef SIGUSR1 + case SIGUSR1: + LOG_V5("%s " WHITE_BOLD("SIGUSR1 received"), Tags::signal()); + break; +# endif + + default: + break; + } + static_cast(handle->data)->m_listener->onSignal(signum); } diff --git a/src/base/io/Signals.h b/src/base/io/Signals.h index 9b4a870a..56be5889 100644 --- a/src/base/io/Signals.h +++ b/src/base/io/Signals.h @@ -1,12 +1,6 @@ /* 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-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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,10 +20,14 @@ #define XMRIG_SIGNALS_H -#include +#include "base/tools/Object.h" -typedef struct uv_signal_s uv_signal_t; +#include +#include + + +using uv_signal_t = struct uv_signal_s; namespace xmrig { @@ -41,20 +39,24 @@ class ISignalListener; class Signals { public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(Signals) + +# ifdef SIGUSR1 + constexpr static const size_t kSignalsCount = 4; +# else constexpr static const size_t kSignalsCount = 3; +# endif Signals(ISignalListener *listener); ~Signals(); - void stop(); - private: void close(int signum); static void onSignal(uv_signal_t *handle, int signum); ISignalListener *m_listener; - uv_signal_t *m_signals[kSignalsCount]; + uv_signal_t *m_signals[kSignalsCount]{}; }; diff --git a/src/base/io/json/Json.cpp b/src/base/io/json/Json.cpp index a9be7dc3..99a087e9 100644 --- a/src/base/io/json/Json.cpp +++ b/src/base/io/json/Json.cpp @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -29,6 +23,7 @@ #include #include +#include namespace xmrig { @@ -119,6 +114,21 @@ const rapidjson::Value &xmrig::Json::getValue(const rapidjson::Value &obj, const } +double xmrig::Json::getDouble(const rapidjson::Value &obj, const char *key, double defaultValue) +{ + if (isEmpty(obj)) { + return defaultValue; + } + + auto i = obj.FindMember(key); + if (i != obj.MemberEnd() && (i->value.IsDouble() || i->value.IsLosslessDouble())) { + return i->value.GetDouble(); + } + + return defaultValue; +} + + int xmrig::Json::getInt(const rapidjson::Value &obj, const char *key, int defaultValue) { if (isEmpty(obj)) { @@ -149,6 +159,25 @@ int64_t xmrig::Json::getInt64(const rapidjson::Value &obj, const char *key, int6 } +xmrig::String xmrig::Json::getString(const rapidjson::Value &obj, const char *key, size_t maxSize) +{ + if (isEmpty(obj)) { + return {}; + } + + auto i = obj.FindMember(key); + if (i == obj.MemberEnd() || !i->value.IsString()) { + return {}; + } + + if (maxSize == 0 || i->value.GetStringLength() <= maxSize) { + return i->value.GetString(); + } + + return { i->value.GetString(), maxSize }; +} + + uint64_t xmrig::Json::getUint64(const rapidjson::Value &obj, const char *key, uint64_t defaultValue) { if (isEmpty(obj)) { @@ -222,6 +251,11 @@ bool xmrig::Json::convertOffset(std::istream &ifs, size_t offset, size_t &line, } +xmrig::JsonReader::JsonReader() : + m_obj(kNullValue) +{} + + bool xmrig::JsonReader::isEmpty() const { return Json::isEmpty(m_obj); diff --git a/src/base/io/json/Json.h b/src/base/io/json/Json.h index 683eb308..e5d0fcc6 100644 --- a/src/base/io/json/Json.h +++ b/src/base/io/json/Json.h @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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,13 +20,10 @@ #define XMRIG_JSON_H -#include "3rdparty/rapidjson/fwd.h" #include "base/kernel/interfaces/IJsonReader.h" #include -#include -#include namespace xmrig { @@ -47,8 +38,10 @@ public: static const rapidjson::Value &getArray(const rapidjson::Value &obj, const char *key); static const rapidjson::Value &getObject(const rapidjson::Value &obj, const char *key); static const rapidjson::Value &getValue(const rapidjson::Value &obj, const char *key); + static double getDouble(const rapidjson::Value &obj, const char *key, double defaultValue = 0); static int getInt(const rapidjson::Value &obj, const char *key, int defaultValue = 0); static int64_t getInt64(const rapidjson::Value &obj, const char *key, int64_t defaultValue = 0); + static String getString(const rapidjson::Value &obj, const char *key, size_t maxSize); static uint64_t getUint64(const rapidjson::Value &obj, const char *key, uint64_t defaultValue = 0); static unsigned getUint(const rapidjson::Value &obj, const char *key, unsigned defaultValue = 0); @@ -66,6 +59,7 @@ private: class JsonReader : public IJsonReader { public: + JsonReader(); inline JsonReader(const rapidjson::Value &obj) : m_obj(obj) {} inline bool getBool(const char *key, bool defaultValue = false) const override { return Json::getBool(m_obj, key, defaultValue); } @@ -73,8 +67,11 @@ public: inline const rapidjson::Value &getArray(const char *key) const override { return Json::getArray(m_obj, key); } inline const rapidjson::Value &getObject(const char *key) const override { return Json::getObject(m_obj, key); } inline const rapidjson::Value &getValue(const char *key) const override { return Json::getValue(m_obj, key); } + inline const rapidjson::Value &object() const override { return m_obj; } + inline double getDouble(const char *key, double defaultValue = 0) const override { return Json::getDouble(m_obj, key, defaultValue); } inline int getInt(const char *key, int defaultValue = 0) const override { return Json::getInt(m_obj, key, defaultValue); } inline int64_t getInt64(const char *key, int64_t defaultValue = 0) const override { return Json::getInt64(m_obj, key, defaultValue); } + inline String getString(const char *key, size_t maxSize) const override { return Json::getString(m_obj, key, maxSize); } inline uint64_t getUint64(const char *key, uint64_t defaultValue = 0) const override { return Json::getUint64(m_obj, key, defaultValue); } inline unsigned getUint(const char *key, unsigned defaultValue = 0) const override { return Json::getUint(m_obj, key, defaultValue); } diff --git a/src/base/io/json/JsonChain.cpp b/src/base/io/json/JsonChain.cpp index dff619e8..0a4a1857 100644 --- a/src/base/io/json/JsonChain.cpp +++ b/src/base/io/json/JsonChain.cpp @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -181,6 +175,28 @@ const rapidjson::Value &xmrig::JsonChain::getValue(const char *key) const } + +const rapidjson::Value &xmrig::JsonChain::object() const +{ + assert(false); + + return m_chain.back(); +} + + +double xmrig::JsonChain::getDouble(const char *key, double defaultValue) const +{ + for (auto it = m_chain.rbegin(); it != m_chain.rend(); ++it) { + auto i = it->FindMember(key); + if (i != it->MemberEnd() && (i->value.IsDouble() || i->value.IsLosslessDouble())) { + return i->value.GetDouble(); + } + } + + return defaultValue; +} + + int xmrig::JsonChain::getInt(const char *key, int defaultValue) const { for (auto it = m_chain.rbegin(); it != m_chain.rend(); ++it) { @@ -207,6 +223,24 @@ int64_t xmrig::JsonChain::getInt64(const char *key, int64_t defaultValue) const } + +xmrig::String xmrig::JsonChain::getString(const char *key, size_t maxSize) const +{ + for (auto it = m_chain.rbegin(); it != m_chain.rend(); ++it) { + auto i = it->FindMember(key); + if (i != it->MemberEnd() && i->value.IsString()) { + if (maxSize == 0 || i->value.GetStringLength() <= maxSize) { + return i->value.GetString(); + } + + return { i->value.GetString(), maxSize }; + } + } + + return {}; +} + + uint64_t xmrig::JsonChain::getUint64(const char *key, uint64_t defaultValue) const { for (auto it = m_chain.rbegin(); it != m_chain.rend(); ++it) { diff --git a/src/base/io/json/JsonChain.h b/src/base/io/json/JsonChain.h index 90f30742..d7fc2f05 100644 --- a/src/base/io/json/JsonChain.h +++ b/src/base/io/json/JsonChain.h @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -59,8 +53,11 @@ protected: const rapidjson::Value &getArray(const char *key) const override; const rapidjson::Value &getObject(const char *key) const override; const rapidjson::Value &getValue(const char *key) const override; + const rapidjson::Value &object() const override; + double getDouble(const char *key, double defaultValue = 0) const override; int getInt(const char *key, int defaultValue = 0) const override; int64_t getInt64(const char *key, int64_t defaultValue = 0) const override; + String getString(const char *key, size_t maxSize) const override; uint64_t getUint64(const char *key, uint64_t defaultValue = 0) const override; unsigned getUint(const char *key, unsigned defaultValue = 0) const override; diff --git a/src/base/io/json/JsonRequest.cpp b/src/base/io/json/JsonRequest.cpp index cacbdc80..2bfdda00 100644 --- a/src/base/io/json/JsonRequest.cpp +++ b/src/base/io/json/JsonRequest.cpp @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -30,16 +24,33 @@ namespace xmrig { -static const char *k2_0 = "2.0"; -static const char *kId = "id"; -static const char *kJsonRPC = "jsonrpc"; -static const char *kMethod = "method"; -const char *JsonRequest::kParams = "params"; +const char *JsonRequest::k2_0 = "2.0"; +const char *JsonRequest::kId = "id"; +const char *JsonRequest::kJsonRPC = "jsonrpc"; +const char *JsonRequest::kMethod = "method"; +const char *JsonRequest::kOK = "OK"; +const char *JsonRequest::kParams = "params"; +const char *JsonRequest::kResult = "result"; +const char *JsonRequest::kStatus = "status"; + +const char *JsonRequest::kParseError = "parse error"; +const char *JsonRequest::kInvalidRequest = "invalid request"; +const char *JsonRequest::kMethodNotFound = "method not found"; +const char *JsonRequest::kInvalidParams = "invalid params"; +const char *JsonRequest::kInternalError = "internal error"; + +static uint64_t nextId = 0; } // namespace xmrig +rapidjson::Document xmrig::JsonRequest::create(const char *method) +{ + return create(++nextId, method); +} + + rapidjson::Document xmrig::JsonRequest::create(int64_t id, const char *method) { using namespace rapidjson; @@ -54,7 +65,13 @@ rapidjson::Document xmrig::JsonRequest::create(int64_t id, const char *method) } -void xmrig::JsonRequest::create(rapidjson::Document &doc, int64_t id, const char *method, rapidjson::Value ¶ms) +uint64_t xmrig::JsonRequest::create(rapidjson::Document &doc, const char *method, rapidjson::Value ¶ms) +{ + return create(doc, ++nextId, method, params); +} + + +uint64_t xmrig::JsonRequest::create(rapidjson::Document &doc, int64_t id, const char *method, rapidjson::Value ¶ms) { using namespace rapidjson; auto &allocator = doc.GetAllocator(); @@ -63,4 +80,6 @@ void xmrig::JsonRequest::create(rapidjson::Document &doc, int64_t id, const char doc.AddMember(StringRef(kJsonRPC), StringRef(k2_0), allocator); doc.AddMember(StringRef(kMethod), StringRef(method), allocator); doc.AddMember(StringRef(kParams), params, allocator); + + return id; } diff --git a/src/base/io/json/JsonRequest.h b/src/base/io/json/JsonRequest.h index 88dbbad6..21451a43 100644 --- a/src/base/io/json/JsonRequest.h +++ b/src/base/io/json/JsonRequest.h @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -35,10 +29,31 @@ namespace xmrig { class JsonRequest { public: + static const char *k2_0; + static const char *kId; + static const char *kJsonRPC; + static const char *kMethod; + static const char *kOK; static const char *kParams; + static const char *kResult; + static const char *kStatus; + static const char *kParseError; + static const char *kInvalidRequest; + static const char *kMethodNotFound; + static const char *kInvalidParams; + static const char *kInternalError; + + constexpr static int kParseErrorCode = -32700; + constexpr static int kInvalidRequestCode = -32600; + constexpr static int kMethodNotFoundCode = -32601; + constexpr static int kInvalidParamsCode = -32602; + constexpr static int kInternalErrorCode = -32603; + + static rapidjson::Document create(const char *method); static rapidjson::Document create(int64_t id, const char *method); - static void create(rapidjson::Document &doc, int64_t id, const char *method, rapidjson::Value ¶ms); + static uint64_t create(rapidjson::Document &doc, const char *method, rapidjson::Value ¶ms); + static uint64_t create(rapidjson::Document &doc, int64_t id, const char *method, rapidjson::Value ¶ms); }; diff --git a/src/base/io/json/Json_unix.cpp b/src/base/io/json/Json_unix.cpp index 328dda34..d2a2f351 100644 --- a/src/base/io/json/Json_unix.cpp +++ b/src/base/io/json/Json_unix.cpp @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -43,7 +37,7 @@ bool xmrig::Json::get(const char *fileName, rapidjson::Document &doc) rapidjson::IStreamWrapper isw(ifs); doc.ParseStream(isw); - return !doc.HasParseError() && doc.IsObject(); + return !doc.HasParseError() && (doc.IsObject() || doc.IsArray()); } @@ -56,7 +50,10 @@ bool xmrig::Json::save(const char *fileName, const rapidjson::Document &doc) rapidjson::OStreamWrapper osw(ofs); rapidjson::PrettyWriter writer(osw); + +# ifdef XMRIG_JSON_SINGLE_LINE_ARRAY writer.SetFormatOptions(rapidjson::kFormatSingleLineArray); +# endif doc.Accept(writer); diff --git a/src/base/io/json/Json_win.cpp b/src/base/io/json/Json_win.cpp index 87c79c24..cd7cf584 100644 --- a/src/base/io/json/Json_win.cpp +++ b/src/base/io/json/Json_win.cpp @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -96,7 +90,7 @@ bool xmrig::Json::get(const char *fileName, rapidjson::Document &doc) IStreamWrapper isw(ifs); doc.ParseStream(isw); - return !doc.HasParseError() && doc.IsObject(); + return !doc.HasParseError() && (doc.IsObject() || doc.IsArray()); } @@ -127,7 +121,10 @@ bool xmrig::Json::save(const char *fileName, const rapidjson::Document &doc) OStreamWrapper osw(ofs); PrettyWriter writer(osw); + +# ifdef XMRIG_JSON_SINGLE_LINE_ARRAY writer.SetFormatOptions(kFormatSingleLineArray); +# endif doc.Accept(writer); diff --git a/src/base/io/log/Log.cpp b/src/base/io/log/Log.cpp index 2c2b2d14..e144858a 100644 --- a/src/base/io/log/Log.cpp +++ b/src/base/io/log/Log.cpp @@ -1,13 +1,7 @@ /* 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 2019 Spudz76 - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2019 Spudz76 + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -31,6 +25,7 @@ #include +#include #include #include #include @@ -76,7 +71,7 @@ public: inline ~LogPrivate() { - for (ILogBackend *backend : m_backends) { + for (auto backend : m_backends) { delete backend; } } @@ -96,7 +91,7 @@ public: return; } - timestamp(level, size, offset); + const uint64_t ts = timestamp(level, size, offset); color(level, size); const int rc = vsnprintf(m_buf + size, sizeof (m_buf) - offset - 32, fmt, args); @@ -114,9 +109,9 @@ public: } if (!m_backends.empty()) { - for (ILogBackend *backend : m_backends) { - backend->print(level, m_buf, offset, size, true); - backend->print(level, txt.c_str(), offset ? (offset - 11) : 0, txt.size(), false); + for (auto backend : m_backends) { + backend->print(ts, level, m_buf, offset, size, true); + backend->print(ts, level, txt.c_str(), offset ? (offset - 11) : 0, txt.size(), false); } } else { @@ -127,14 +122,15 @@ public: private: - inline void timestamp(Log::Level level, size_t &size, size_t &offset) + inline uint64_t timestamp(Log::Level level, size_t &size, size_t &offset) { + const uint64_t ms = Chrono::currentMSecsSinceEpoch(); + if (level == Log::NONE) { - return; + return ms; } - const uint64_t ms = Chrono::currentMSecsSinceEpoch(); - time_t now = ms / 1000; + time_t now = ms / 1000; tm stime{}; # ifdef _WIN32 @@ -156,6 +152,8 @@ private: if (rc > 0) { size = offset = static_cast(rc); } + + return ms; } @@ -195,10 +193,10 @@ private: }; -bool Log::m_background = false; -bool Log::m_colors = true; -LogPrivate *Log::d = new LogPrivate(); -uint32_t Log::m_verbose = 0; +bool Log::m_background = false; +bool Log::m_colors = true; +LogPrivate *Log::d = nullptr; +uint32_t Log::m_verbose = 0; } /* namespace xmrig */ @@ -207,6 +205,8 @@ uint32_t Log::m_verbose = 0; void xmrig::Log::add(ILogBackend *backend) { + assert(d != nullptr); + if (d) { d->add(backend); } @@ -220,6 +220,12 @@ void xmrig::Log::destroy() } +void xmrig::Log::init() +{ + d = new LogPrivate(); +} + + void xmrig::Log::print(const char *fmt, ...) { if (!d) { diff --git a/src/base/io/log/Log.h b/src/base/io/log/Log.h index 73d4e53f..6da2e892 100644 --- a/src/base/io/log/Log.h +++ b/src/base/io/log/Log.h @@ -1,13 +1,7 @@ /* 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 2019 Spudz76 - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2019 Spudz76 + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -57,6 +51,7 @@ public: static void add(ILogBackend *backend); static void destroy(); + static void init(); static void print(const char *fmt, ...); static void print(Level level, const char *fmt, ...); @@ -71,9 +66,8 @@ public: private: static bool m_background; static bool m_colors; - static uint32_t m_verbose; - static LogPrivate *d; + static uint32_t m_verbose; }; diff --git a/src/base/io/log/backends/ConsoleLog.cpp b/src/base/io/log/backends/ConsoleLog.cpp index 29103a54..cc3081fd 100644 --- a/src/base/io/log/backends/ConsoleLog.cpp +++ b/src/base/io/log/backends/ConsoleLog.cpp @@ -1,13 +1,7 @@ /* 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 2019 Spudz76 - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2019 Spudz76 + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -24,14 +18,13 @@ */ -#include - - #include "base/io/log/backends/ConsoleLog.h" #include "base/io/log/Log.h" #include "base/kernel/config/Title.h" #include "base/tools/Handle.h" -#include "version.h" + + +#include xmrig::ConsoleLog::ConsoleLog(const Title &title) @@ -75,7 +68,7 @@ xmrig::ConsoleLog::~ConsoleLog() } -void xmrig::ConsoleLog::print(int, const char *line, size_t, size_t size, bool colors) +void xmrig::ConsoleLog::print(uint64_t, int, const char *line, size_t, size_t size, bool colors) { if (!m_tty || Log::isColors() != colors) { return; diff --git a/src/base/io/log/backends/ConsoleLog.h b/src/base/io/log/backends/ConsoleLog.h index 7f365a15..01917763 100644 --- a/src/base/io/log/backends/ConsoleLog.h +++ b/src/base/io/log/backends/ConsoleLog.h @@ -1,13 +1,7 @@ /* 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 2019 Spudz76 - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2019 Spudz76 + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -50,7 +44,7 @@ public: ~ConsoleLog() override; protected: - void print(int level, const char *line, size_t offset, size_t size, bool colors) override; + void print(uint64_t timestamp, int level, const char *line, size_t offset, size_t size, bool colors) override; private: bool isSupported() const; diff --git a/src/base/io/log/backends/FileLog.cpp b/src/base/io/log/backends/FileLog.cpp index 2d52d812..51e5b1f7 100644 --- a/src/base/io/log/backends/FileLog.cpp +++ b/src/base/io/log/backends/FileLog.cpp @@ -1,13 +1,7 @@ /* 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 2019 Spudz76 - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2019 Spudz76 + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 +31,7 @@ xmrig::FileLog::FileLog(const char *fileName) : } -void xmrig::FileLog::print(int, const char *line, size_t, size_t size, bool colors) +void xmrig::FileLog::print(uint64_t, int, const char *line, size_t, size_t size, bool colors) { if (!m_writer.isOpen() || colors) { return; diff --git a/src/base/io/log/backends/FileLog.h b/src/base/io/log/backends/FileLog.h index 95e9881a..c52cefd9 100644 --- a/src/base/io/log/backends/FileLog.h +++ b/src/base/io/log/backends/FileLog.h @@ -1,13 +1,7 @@ /* 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 2019 Spudz76 - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2019 Spudz76 + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -40,7 +34,7 @@ public: FileLog(const char *fileName); protected: - void print(int level, const char *line, size_t offset, size_t size, bool colors) override; + void print(uint64_t timestamp, int level, const char *line, size_t offset, size_t size, bool colors) override; private: FileLogWriter m_writer; diff --git a/src/base/io/log/backends/SysLog.cpp b/src/base/io/log/backends/SysLog.cpp index e66f2e35..bd333908 100644 --- a/src/base/io/log/backends/SysLog.cpp +++ b/src/base/io/log/backends/SysLog.cpp @@ -1,13 +1,7 @@ /* 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 2019 Spudz76 - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright (c) 2019 Spudz76 + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -43,7 +37,7 @@ xmrig::SysLog::~SysLog() } -void xmrig::SysLog::print(int level, const char *line, size_t offset, size_t, bool colors) +void xmrig::SysLog::print(uint64_t, int level, const char *line, size_t offset, size_t, bool colors) { if (colors) { return; diff --git a/src/base/io/log/backends/SysLog.h b/src/base/io/log/backends/SysLog.h index d131784b..447ad006 100644 --- a/src/base/io/log/backends/SysLog.h +++ b/src/base/io/log/backends/SysLog.h @@ -1,13 +1,7 @@ /* 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 2019 Spudz76 - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright (c) 2019 Spudz76 + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -36,11 +30,13 @@ namespace xmrig { class SysLog : public ILogBackend { public: + XMRIG_DISABLE_COPY_MOVE(SysLog) + SysLog(); ~SysLog() override; protected: - void print(int level, const char *line, size_t offset, size_t size, bool colors) override; + void print(uint64_t timestamp, int level, const char *line, size_t offset, size_t size, bool colors) override; }; diff --git a/src/base/kernel/Base.cpp b/src/base/kernel/Base.cpp index 1e5019ca..24736193 100644 --- a/src/base/kernel/Base.cpp +++ b/src/base/kernel/Base.cpp @@ -166,6 +166,7 @@ private: xmrig::Base::Base(Process *process) : d_ptr(new BasePrivate(process)) { + Log::init(); } diff --git a/src/base/kernel/config/Title.cpp b/src/base/kernel/config/Title.cpp index 1e9c3ce4..030aa727 100644 --- a/src/base/kernel/config/Title.cpp +++ b/src/base/kernel/config/Title.cpp @@ -1,6 +1,6 @@ /* XMRig - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 diff --git a/src/base/kernel/config/Title.h b/src/base/kernel/config/Title.h index 8cf73f28..452869dc 100644 --- a/src/base/kernel/config/Title.h +++ b/src/base/kernel/config/Title.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 diff --git a/src/base/kernel/constants.h b/src/base/kernel/constants.h new file mode 100644 index 00000000..a76cee9a --- /dev/null +++ b/src/base/kernel/constants.h @@ -0,0 +1,31 @@ +/* XMRig + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 XMRIG_CONSTANTS_H +#define XMRIG_CONSTANTS_H + + +#include +#include + + +constexpr size_t XMRIG_NET_BUFFER_CHUNK_SIZE = 64 * 1024; +constexpr size_t XMRIG_NET_BUFFER_INIT_CHUNKS = 4; + + +#endif /* XMRIG_CONSTANTS_H */ diff --git a/src/base/kernel/interfaces/IJsonReader.h b/src/base/kernel/interfaces/IJsonReader.h index b545514d..044a291c 100644 --- a/src/base/kernel/interfaces/IJsonReader.h +++ b/src/base/kernel/interfaces/IJsonReader.h @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -27,6 +21,8 @@ #include "3rdparty/rapidjson/fwd.h" +#include "base/tools/Object.h" +#include "base/tools/String.h" namespace xmrig { @@ -35,7 +31,10 @@ namespace xmrig { class IJsonReader { public: - virtual ~IJsonReader() = default; + XMRIG_DISABLE_COPY_MOVE(IJsonReader) + + IJsonReader() = default; + virtual ~IJsonReader() = default; virtual bool getBool(const char *key, bool defaultValue = false) const = 0; virtual bool isEmpty() const = 0; @@ -43,8 +42,11 @@ public: virtual const rapidjson::Value &getArray(const char *key) const = 0; virtual const rapidjson::Value &getObject(const char *key) const = 0; virtual const rapidjson::Value &getValue(const char *key) const = 0; + virtual const rapidjson::Value &object() const = 0; + virtual double getDouble(const char *key, double defaultValue = 0) const = 0; virtual int getInt(const char *key, int defaultValue = 0) const = 0; virtual int64_t getInt64(const char *key, int64_t defaultValue = 0) const = 0; + virtual String getString(const char *key, size_t maxSize) const = 0; virtual uint64_t getUint64(const char *key, uint64_t defaultValue = 0) const = 0; virtual unsigned getUint(const char *key, unsigned defaultValue = 0) const = 0; }; diff --git a/src/base/kernel/interfaces/ILogBackend.h b/src/base/kernel/interfaces/ILogBackend.h index ef18da88..88137fd1 100644 --- a/src/base/kernel/interfaces/ILogBackend.h +++ b/src/base/kernel/interfaces/ILogBackend.h @@ -1,13 +1,6 @@ /* 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 2019 Spudz76 - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -27,8 +20,11 @@ #define XMRIG_ILOGBACKEND_H -#include -#include +#include "base/tools/Object.h" + + +#include +#include namespace xmrig { @@ -37,9 +33,12 @@ namespace xmrig { class ILogBackend { public: - virtual ~ILogBackend() = default; + XMRIG_DISABLE_COPY_MOVE(ILogBackend) - virtual void print(int level, const char *line, size_t offset, size_t size, bool colors) = 0; + ILogBackend() = default; + virtual ~ILogBackend() = default; + + virtual void print(uint64_t timestamp, int level, const char *line, size_t offset, size_t size, bool colors) = 0; }; diff --git a/src/base/net/dns/Dns.cpp b/src/base/net/dns/Dns.cpp index 6246421c..ef50b20d 100644 --- a/src/base/net/dns/Dns.cpp +++ b/src/base/net/dns/Dns.cpp @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 diff --git a/src/base/net/dns/Dns.h b/src/base/net/dns/Dns.h index 068ab80f..86f90145 100644 --- a/src/base/net/dns/Dns.h +++ b/src/base/net/dns/Dns.h @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 diff --git a/src/base/net/dns/DnsRecord.cpp b/src/base/net/dns/DnsRecord.cpp index e722ad6a..1667c715 100644 --- a/src/base/net/dns/DnsRecord.cpp +++ b/src/base/net/dns/DnsRecord.cpp @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 diff --git a/src/base/net/dns/DnsRecord.h b/src/base/net/dns/DnsRecord.h index 0b572df8..cf6c2598 100644 --- a/src/base/net/dns/DnsRecord.h +++ b/src/base/net/dns/DnsRecord.h @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 diff --git a/src/base/net/http/Fetch.cpp b/src/base/net/http/Fetch.cpp index 4d18b71d..84ff715c 100644 --- a/src/base/net/http/Fetch.cpp +++ b/src/base/net/http/Fetch.cpp @@ -1,6 +1,6 @@ /* XMRig - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -95,7 +95,7 @@ void xmrig::FetchRequest::setBody(const rapidjson::Value &value) } -void xmrig::fetch(FetchRequest &&req, const std::weak_ptr &listener, int type) +void xmrig::fetch(const char *tag, FetchRequest &&req, const std::weak_ptr &listener, int type, uint64_t rpcId) { # ifdef APP_DEBUG LOG_DEBUG(CYAN("http%s://%s:%u ") MAGENTA_BOLD("\"%s %s\"") BLACK_BOLD(" body: ") CYAN_BOLD("%zu") BLACK_BOLD(" bytes"), @@ -109,14 +109,15 @@ void xmrig::fetch(FetchRequest &&req, const std::weak_ptr &listen HttpClient *client; # ifdef XMRIG_FEATURE_TLS if (req.tls) { - client = new HttpsClient(std::move(req), listener); + client = new HttpsClient(tag, std::move(req), listener); } else # endif { - client = new HttpClient(std::move(req), listener); + client = new HttpClient(tag, std::move(req), listener); } client->userType = type; + client->rpcId = rpcId; client->connect(); } diff --git a/src/base/net/http/Fetch.h b/src/base/net/http/Fetch.h index b6fbf487..bdab46ba 100644 --- a/src/base/net/http/Fetch.h +++ b/src/base/net/http/Fetch.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -58,10 +58,11 @@ public: String host; String path; uint16_t port = 0; + uint64_t timeout = 0; }; -void fetch(FetchRequest &&req, const std::weak_ptr &listener, int type = 0); +void fetch(const char *tag, FetchRequest &&req, const std::weak_ptr &listener, int type = 0, uint64_t rpcId = 0); } // namespace xmrig diff --git a/src/base/net/http/HttpClient.cpp b/src/base/net/http/HttpClient.cpp index 92ca6fac..320f8ee4 100644 --- a/src/base/net/http/HttpClient.cpp +++ b/src/base/net/http/HttpClient.cpp @@ -1,13 +1,7 @@ /* 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 2014-2019 heapwolf - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2014-2019 heapwolf + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -30,6 +24,7 @@ #include "base/kernel/Platform.h" #include "base/net/dns/Dns.h" #include "base/net/tools/NetBuffer.h" +#include "base/tools/Timer.h" #include @@ -44,21 +39,20 @@ static const char *kCRLF = "\r\n"; } // namespace xmrig -xmrig::HttpClient::HttpClient(FetchRequest &&req, const std::weak_ptr &listener) : +xmrig::HttpClient::HttpClient(const char *tag, FetchRequest &&req, const std::weak_ptr &listener) : HttpContext(HTTP_RESPONSE, listener), + m_tag(tag), m_req(std::move(req)) { method = m_req.method; url = std::move(m_req.path); body = std::move(m_req.body); headers = std::move(m_req.headers); - m_dns = new Dns(this); -} + m_dns = std::make_shared(this); - -xmrig::HttpClient::~HttpClient() -{ - delete m_dns; + if (m_req.timeout) { + m_timer = std::make_shared(this, m_req.timeout, 0); + } } @@ -74,7 +68,7 @@ void xmrig::HttpClient::onResolved(const Dns &dns, int status) if (status < 0 && dns.isEmpty()) { if (!isQuiet()) { - LOG_ERR("[%s:%d] DNS error: \"%s\"", dns.host().data(), port(), uv_strerror(status)); + LOG_ERR("%s " RED("DNS error: ") RED_BOLD("\"%s\""), tag(), uv_strerror(status)); } return; @@ -91,6 +85,12 @@ void xmrig::HttpClient::onResolved(const Dns &dns, int status) } +void xmrig::HttpClient::onTimer(const Timer *) +{ + close(UV_ETIMEDOUT); +} + + void xmrig::HttpClient::handshake() { headers.insert({ "Host", host() }); @@ -135,8 +135,12 @@ void xmrig::HttpClient::onConnect(uv_connect_t *req, int status) } if (status < 0) { + if (status == UV_ECANCELED) { + status = UV_ETIMEDOUT; + } + if (!client->isQuiet()) { - LOG_ERR("[%s:%d] connect error: \"%s\"", client->m_dns->host().data(), client->port(), uv_strerror(status)); + LOG_ERR("%s " RED("connect error: ") RED_BOLD("\"%s\""), client->tag(), uv_strerror(status)); } return client->close(status); @@ -151,7 +155,7 @@ void xmrig::HttpClient::onConnect(uv_connect_t *req, int status) client->read(buf->base, static_cast(nread)); } else { if (!client->isQuiet() && nread != UV_EOF) { - LOG_ERR("[%s:%d] read error: \"%s\"", client->m_dns->host().data(), client->port(), uv_strerror(static_cast(nread))); + LOG_ERR("%s " RED("read error: ") RED_BOLD("\"%s\""), client->tag(), uv_strerror(static_cast(nread))); } client->close(static_cast(nread)); diff --git a/src/base/net/http/HttpClient.h b/src/base/net/http/HttpClient.h index acfe15e2..2b9a314d 100644 --- a/src/base/net/http/HttpClient.h +++ b/src/base/net/http/HttpClient.h @@ -1,13 +1,7 @@ /* 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 2014-2019 heapwolf - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2014-2019 heapwolf + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -29,6 +23,7 @@ #include "base/kernel/interfaces/IDnsListener.h" +#include "base/kernel/interfaces/ITimerListener.h" #include "base/net/http/Fetch.h" #include "base/net/http/HttpContext.h" #include "base/tools/Object.h" @@ -40,22 +35,24 @@ namespace xmrig { class String; -class HttpClient : public HttpContext, public IDnsListener +class HttpClient : public HttpContext, public IDnsListener, public ITimerListener { public: XMRIG_DISABLE_COPY_MOVE_DEFAULT(HttpClient); - HttpClient(FetchRequest &&req, const std::weak_ptr &listener); - ~HttpClient() override; + HttpClient(const char *tag, FetchRequest &&req, const std::weak_ptr &listener); + ~HttpClient() override = default; inline bool isQuiet() const { return m_req.quiet; } inline const char *host() const override { return m_req.host; } + inline const char *tag() const { return m_tag; } inline uint16_t port() const override { return m_req.port; } bool connect(); protected: void onResolved(const Dns &dns, int status) override; + void onTimer(const Timer *timer) override; virtual void handshake(); virtual void read(const char *data, size_t size); @@ -66,8 +63,10 @@ protected: private: static void onConnect(uv_connect_t *req, int status); - Dns *m_dns; + const char *m_tag; FetchRequest m_req; + std::shared_ptr m_dns; + std::shared_ptr m_timer; }; diff --git a/src/base/net/http/HttpContext.cpp b/src/base/net/http/HttpContext.cpp index b348d7e4..52739e4d 100644 --- a/src/base/net/http/HttpContext.cpp +++ b/src/base/net/http/HttpContext.cpp @@ -1,13 +1,7 @@ /* 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 2014-2019 heapwolf - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2014-2019 heapwolf + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -160,6 +154,10 @@ uint64_t xmrig::HttpContext::elapsed() const void xmrig::HttpContext::close(int status) { + if (!get(id())) { + return; + } + auto listener = httpListener(); if (status < 0 && listener) { @@ -262,7 +260,7 @@ void xmrig::HttpContext::attach(http_parser_settings *settings) settings->on_body = [](http_parser *parser, const char *at, size_t len) -> int { - static_cast(parser->data)->body += std::string(at, len); + static_cast(parser->data)->body.append(at, len); return 0; }; diff --git a/src/base/net/http/HttpContext.h b/src/base/net/http/HttpContext.h index a7554679..4202bfaf 100644 --- a/src/base/net/http/HttpContext.h +++ b/src/base/net/http/HttpContext.h @@ -1,13 +1,7 @@ /* 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 2014-2019 heapwolf - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2014-2019 heapwolf + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 diff --git a/src/base/net/http/HttpListener.cpp b/src/base/net/http/HttpListener.cpp index 077184fc..ba9130ed 100644 --- a/src/base/net/http/HttpListener.cpp +++ b/src/base/net/http/HttpListener.cpp @@ -1,6 +1,6 @@ /* XMRig - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 diff --git a/src/base/net/http/HttpListener.h b/src/base/net/http/HttpListener.h index 4f982c4d..0e726421 100644 --- a/src/base/net/http/HttpListener.h +++ b/src/base/net/http/HttpListener.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -29,20 +29,13 @@ namespace xmrig { class HttpListener : public IHttpListener { public: - inline HttpListener(IHttpListener *listener, const char *tag = nullptr) : -# ifdef APP_DEBUG - m_tag(tag), -# endif - m_listener(listener) - {} + inline HttpListener(IHttpListener *listener, const char *tag = nullptr) : m_tag(tag), m_listener(listener) {} protected: void onHttpData(const HttpData &data) override; private: -# ifdef APP_DEBUG const char *m_tag; -# endif IHttpListener *m_listener; }; diff --git a/src/base/net/https/HttpsClient.cpp b/src/base/net/https/HttpsClient.cpp index 28b5a5ef..8b414917 100644 --- a/src/base/net/https/HttpsClient.cpp +++ b/src/base/net/https/HttpsClient.cpp @@ -1,13 +1,7 @@ /* 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 2014-2019 heapwolf - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2014-2019 heapwolf + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -31,7 +25,7 @@ #include "base/net/https/HttpsClient.h" #include "base/io/log/Log.h" -#include "base/tools/Buffer.h" +#include "base/tools/Cvt.h" #ifdef _MSC_VER @@ -39,8 +33,8 @@ #endif -xmrig::HttpsClient::HttpsClient(FetchRequest &&req, const std::weak_ptr &listener) : - HttpClient(std::move(req), listener) +xmrig::HttpsClient::HttpsClient(const char *tag, FetchRequest &&req, const std::weak_ptr &listener) : + HttpClient(tag, std::move(req), listener) { m_ctx = SSL_CTX_new(SSLv23_method()); assert(m_ctx != nullptr); @@ -182,7 +176,7 @@ bool xmrig::HttpsClient::verifyFingerprint(X509 *cert) return false; } - Buffer::toHex(md, 32, m_fingerprint); + Cvt::toHex(m_fingerprint, sizeof(m_fingerprint), md, 32); return req().fingerprint.isNull() || strncasecmp(m_fingerprint, req().fingerprint.data(), 64) == 0; } diff --git a/src/base/net/https/HttpsClient.h b/src/base/net/https/HttpsClient.h index eeeec747..bd0c65ff 100644 --- a/src/base/net/https/HttpsClient.h +++ b/src/base/net/https/HttpsClient.h @@ -1,13 +1,7 @@ /* 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 2014-2019 heapwolf - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2014-2019 heapwolf + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -46,7 +40,7 @@ class HttpsClient : public HttpClient public: XMRIG_DISABLE_COPY_MOVE_DEFAULT(HttpsClient) - HttpsClient(FetchRequest &&req, const std::weak_ptr &listener); + HttpsClient(const char *tag, FetchRequest &&req, const std::weak_ptr &listener); ~HttpsClient() override; const char *tlsFingerprint() const override; diff --git a/src/base/net/stratum/Client.cpp b/src/base/net/stratum/Client.cpp index 018f7a66..5327867e 100644 --- a/src/base/net/stratum/Client.cpp +++ b/src/base/net/stratum/Client.cpp @@ -50,7 +50,7 @@ #include "base/net/dns/Dns.h" #include "base/net/stratum/Socks5.h" #include "base/net/tools/NetBuffer.h" -#include "base/tools/Buffer.h" +#include "base/tools/Cvt.h" #include "base/tools/Chrono.h" #include "net/JobResult.h" @@ -202,11 +202,8 @@ int64_t xmrig::Client::submit(const JobResult &result) char *nonce = m_sendBuf.data(); char *data = m_sendBuf.data() + 16; - Buffer::toHex(reinterpret_cast(&result.nonce), 4, nonce); - nonce[8] = '\0'; - - Buffer::toHex(result.result(), 32, data); - data[64] = '\0'; + Cvt::toHex(nonce, sizeof(uint32_t) * 2 + 1, reinterpret_cast(&result.nonce), sizeof(uint32_t)); + Cvt::toHex(data, 65, result.result(), 32); # endif Document doc(kObjectType); diff --git a/src/base/net/stratum/DaemonClient.cpp b/src/base/net/stratum/DaemonClient.cpp index a8986747..8b4d0fb6 100644 --- a/src/base/net/stratum/DaemonClient.cpp +++ b/src/base/net/stratum/DaemonClient.cpp @@ -36,7 +36,7 @@ #include "base/net/http/HttpData.h" #include "base/net/http/HttpListener.h" #include "base/net/stratum/SubmitResult.h" -#include "base/tools/Buffer.h" +#include "base/tools/Cvt.h" #include "base/tools/Timer.h" #include "net/JobResult.h" @@ -54,7 +54,7 @@ static const char *kHash = "hash"; static const char *kHeight = "height"; static const char *kJsonRPC = "/json_rpc"; -static const size_t BlobReserveSize = 8; +static constexpr size_t kBlobReserveSize = 8; } @@ -104,7 +104,7 @@ int64_t xmrig::DaemonClient::submit(const JobResult &result) # ifdef XMRIG_PROXY_PROJECT memcpy(data + 78, result.nonce, 8); # else - Buffer::toHex(reinterpret_cast(&result.nonce), 4, data + 78); + Cvt::toHex(data + 78, 9, reinterpret_cast(&result.nonce), 4); # endif using namespace rapidjson; @@ -227,7 +227,7 @@ bool xmrig::DaemonClient::parseJob(const rapidjson::Value ¶ms, int *code) m_blockhashingblob = Json::getString(params, "blockhashing_blob"); if (m_apiVersion == API_DERO) { const uint64_t offset = Json::getUint64(params, "reserved_offset"); - Buffer::toHex(Buffer::randomBytes(BlobReserveSize).data(), BlobReserveSize, m_blockhashingblob.data() + offset * 2); + Cvt::toHex(m_blockhashingblob.data() + offset * 2, kBlobReserveSize * 2 + 1, Cvt::randomBytes(kBlobReserveSize).data(), kBlobReserveSize); } if (blocktemplate.isNull() || !job.setBlob(m_blockhashingblob)) { @@ -315,10 +315,10 @@ int64_t xmrig::DaemonClient::getBlockTemplate() Value params(kObjectType); params.AddMember("wallet_address", m_user.toJSON(), allocator); if (m_apiVersion == API_DERO) { - params.AddMember("reserve_size", static_cast(BlobReserveSize), allocator); + params.AddMember("reserve_size", static_cast(kBlobReserveSize), allocator); } else { - params.AddMember("extra_nonce", Buffer::randomBytes(BlobReserveSize).toHex().toJSON(doc), allocator); + params.AddMember("extra_nonce", Cvt::toHex(Cvt::randomBytes(kBlobReserveSize)).toJSON(doc), allocator); } JsonRequest::create(doc, m_sequence, "getblocktemplate", params); @@ -330,7 +330,7 @@ int64_t xmrig::DaemonClient::getBlockTemplate() int64_t xmrig::DaemonClient::rpcSend(const rapidjson::Document &doc) { FetchRequest req(HTTP_POST, m_pool.host(), m_pool.port(), kJsonRPC, doc, m_pool.isTLS(), isQuiet()); - fetch(std::move(req), m_httpListener); + fetch(tag(), std::move(req), m_httpListener); return m_sequence++; } @@ -357,7 +357,7 @@ void xmrig::DaemonClient::retry() void xmrig::DaemonClient::send(const char *path) { FetchRequest req(HTTP_GET, m_pool.host(), m_pool.port(), path, m_pool.isTLS(), isQuiet()); - fetch(std::move(req), m_httpListener); + fetch(tag(), std::move(req), m_httpListener); } diff --git a/src/base/net/stratum/Job.cpp b/src/base/net/stratum/Job.cpp index 20345414..9075e4b2 100644 --- a/src/base/net/stratum/Job.cpp +++ b/src/base/net/stratum/Job.cpp @@ -31,6 +31,7 @@ #include "base/net/stratum/Job.h" #include "base/tools/Buffer.h" +#include "base/tools/Cvt.h" xmrig::Job::Job(bool nicehash, const Algorithm &algorithm, const String &clientId) : @@ -63,7 +64,7 @@ bool xmrig::Job::setBlob(const char *blob) return false; } - if (!Buffer::fromHex(blob, m_size * 2, m_blob)) { + if (!Cvt::fromHex(m_blob, sizeof(m_blob), blob, m_size * 2)) { return false; } @@ -90,9 +91,9 @@ bool xmrig::Job::setSeedHash(const char *hash) m_rawSeedHash = hash; # endif - m_seed = Buffer::fromHex(hash, kMaxSeedSize * 2); + m_seed = Cvt::fromHex(hash, kMaxSeedSize * 2); - return !m_seed.isEmpty(); + return !m_seed.empty(); } @@ -102,27 +103,14 @@ bool xmrig::Job::setTarget(const char *target) return false; } - const size_t len = strlen(target); + const auto raw = Cvt::fromHex(target, strlen(target)); + const size_t size = raw.size(); - if (len <= 8) { - uint32_t tmp = 0; - char str[8]; - memcpy(str, target, len); - - if (!Buffer::fromHex(str, 8, reinterpret_cast(&tmp)) || tmp == 0) { - return false; - } - - m_target = 0xFFFFFFFFFFFFFFFFULL / (0xFFFFFFFFULL / static_cast(tmp)); + if (size == 4) { + m_target = 0xFFFFFFFFFFFFFFFFULL / (0xFFFFFFFFULL / uint64_t(*reinterpret_cast(raw.data()))); } - else if (len <= 16) { - m_target = 0; - char str[16]; - memcpy(str, target, len); - - if (!Buffer::fromHex(str, 16, reinterpret_cast(&m_target)) || m_target == 0) { - return false; - } + else if (size == 8) { + m_target = *reinterpret_cast(raw.data()); } else { return false; diff --git a/src/base/net/stratum/SelfSelectClient.cpp b/src/base/net/stratum/SelfSelectClient.cpp index 8ad48b65..fc4cea6e 100644 --- a/src/base/net/stratum/SelfSelectClient.cpp +++ b/src/base/net/stratum/SelfSelectClient.cpp @@ -154,7 +154,7 @@ void xmrig::SelfSelectClient::getBlockTemplate() JsonRequest::create(doc, m_sequence++, "getblocktemplate", params); FetchRequest req(HTTP_POST, pool().daemon().host(), pool().daemon().port(), "/json_rpc", doc, pool().daemon().isTLS(), isQuiet()); - fetch(std::move(req), m_httpListener); + fetch(tag(), std::move(req), m_httpListener); } diff --git a/src/base/net/stratum/Tls.cpp b/src/base/net/stratum/Tls.cpp index 5915fad9..a4ccd853 100644 --- a/src/base/net/stratum/Tls.cpp +++ b/src/base/net/stratum/Tls.cpp @@ -27,7 +27,7 @@ #include "base/net/stratum/Tls.h" #include "base/io/log/Log.h" #include "base/net/stratum/Client.h" -#include "base/tools/Buffer.h" +#include "base/tools/Cvt.h" #ifdef _MSC_VER @@ -183,7 +183,7 @@ bool xmrig::Client::Tls::verifyFingerprint(X509 *cert) return false; } - Buffer::toHex(md, 32, m_fingerprint); + Cvt::toHex(m_fingerprint, sizeof(m_fingerprint), md, 32); const char *fingerprint = m_client->m_pool.fingerprint(); return fingerprint == nullptr || strncasecmp(m_fingerprint, fingerprint, 64) == 0; diff --git a/src/base/net/stratum/benchmark/BenchClient.cpp b/src/base/net/stratum/benchmark/BenchClient.cpp index 003eeca7..beac2f19 100644 --- a/src/base/net/stratum/benchmark/BenchClient.cpp +++ b/src/base/net/stratum/benchmark/BenchClient.cpp @@ -31,6 +31,7 @@ #include "base/net/http/HttpData.h" #include "base/net/http/HttpListener.h" #include "base/net/stratum/benchmark/BenchConfig.h" +#include "base/tools/Cvt.h" #include "version.h" @@ -217,7 +218,7 @@ bool xmrig::BenchClient::setSeed(const char *seed) return false; } - if (!Buffer::fromHex(seed, size * 2, m_job.blob())) { + if (!Cvt::fromHex(m_job.blob(), m_job.size(), seed, size * 2)) { return false; } @@ -321,7 +322,7 @@ void xmrig::BenchClient::send(Request request) case GET_BENCH: { FetchRequest req(HTTP_GET, m_ip, BenchConfig::kApiPort, fmt::format("/1/benchmark/{}", m_job.id()).c_str(), BenchConfig::kApiTLS, true); - fetch(std::move(req), m_httpListener); + fetch(tag(), std::move(req), m_httpListener); } break; @@ -335,7 +336,7 @@ void xmrig::BenchClient::send(Request request) doc.AddMember("cpu", Cpu::toJSON(doc), allocator); FetchRequest req(HTTP_POST, m_ip, BenchConfig::kApiPort, "/1/benchmark", doc, BenchConfig::kApiTLS, true); - fetch(std::move(req), m_httpListener); + fetch(tag(), std::move(req), m_httpListener); } break; @@ -374,6 +375,6 @@ void xmrig::BenchClient::update(const rapidjson::Value &body) FetchRequest req(HTTP_PATCH, m_ip, BenchConfig::kApiPort, fmt::format("/1/benchmark/{}", m_job.id()).c_str(), body, BenchConfig::kApiTLS, true); req.headers.insert({ "Authorization", fmt::format("Bearer {}", m_token)}); - fetch(std::move(req), m_httpListener); + fetch(tag(), std::move(req), m_httpListener); } #endif diff --git a/src/base/net/tls/ServerTls.cpp b/src/base/net/tls/ServerTls.cpp index e76d5862..abae315d 100644 --- a/src/base/net/tls/ServerTls.cpp +++ b/src/base/net/tls/ServerTls.cpp @@ -1,6 +1,6 @@ /* XMRig - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -20,6 +20,7 @@ #include "base/net/tls/ServerTls.h" +#include #include #include #include @@ -39,11 +40,23 @@ xmrig::ServerTls::~ServerTls() } +bool xmrig::ServerTls::isHTTP(const char *data, size_t size) +{ + assert(size > 0); + + static const char test[6] = "GET /"; + + return size > 0 && memcmp(data, test, std::min(size, sizeof(test) - 1)) == 0; +} + + bool xmrig::ServerTls::isTLS(const char *data, size_t size) { + assert(size > 0); + static const uint8_t test[3] = { 0x16, 0x03, 0x01 }; - return size >= sizeof(test) && memcmp(data, test, sizeof(test)) == 0; + return size > 0 && memcmp(data, test, std::min(size, sizeof(test))) == 0; } diff --git a/src/base/net/tls/ServerTls.h b/src/base/net/tls/ServerTls.h index 892bed2b..20337eb6 100644 --- a/src/base/net/tls/ServerTls.h +++ b/src/base/net/tls/ServerTls.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -41,6 +41,7 @@ public: ServerTls(SSL_CTX *ctx); virtual ~ServerTls(); + static bool isHTTP(const char *data, size_t size); static bool isTLS(const char *data, size_t size); bool send(const char *data, size_t size); diff --git a/src/base/net/tools/LineReader.cpp b/src/base/net/tools/LineReader.cpp index 3ad5c373..aa2897fc 100644 --- a/src/base/net/tools/LineReader.cpp +++ b/src/base/net/tools/LineReader.cpp @@ -19,8 +19,9 @@ #include "base/net/tools/LineReader.h" -#include "base/net/tools/NetBuffer.h" +#include "base/kernel/constants.h" #include "base/kernel/interfaces/ILineListener.h" +#include "base/net/tools/NetBuffer.h" #include #include @@ -55,7 +56,7 @@ void xmrig::LineReader::reset() void xmrig::LineReader::add(const char *data, size_t size) { - if (size > NetBuffer::kChunkSize - m_pos) { + if (size > XMRIG_NET_BUFFER_CHUNK_SIZE - m_pos) { // it breakes correctness silently for long lines return; } diff --git a/src/base/net/tools/MemPool.h b/src/base/net/tools/MemPool.h index 3b8a8ef4..e7cf3a19 100644 --- a/src/base/net/tools/MemPool.h +++ b/src/base/net/tools/MemPool.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -20,10 +20,10 @@ #define XMRIG_MEMPOOL_H -#include -#include #include #include +#include +#include namespace xmrig { diff --git a/src/base/net/tools/NetBuffer.cpp b/src/base/net/tools/NetBuffer.cpp index 3303d971..8931f06e 100644 --- a/src/base/net/tools/NetBuffer.cpp +++ b/src/base/net/tools/NetBuffer.cpp @@ -1,6 +1,6 @@ /* XMRig - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -17,8 +17,9 @@ */ -#include "base/net/tools/MemPool.h" #include "base/net/tools/NetBuffer.h" +#include "base/kernel/constants.h" +#include "base/net/tools/MemPool.h" #include @@ -28,14 +29,13 @@ namespace xmrig { -static constexpr size_t kInitSize = 4; -static MemPool *pool = nullptr; +static MemPool *pool = nullptr; -inline MemPool *getPool() +inline MemPool *getPool() { if (!pool) { - pool = new MemPool(); + pool = new MemPool(); } return pool; @@ -67,17 +67,25 @@ void xmrig::NetBuffer::destroy() void xmrig::NetBuffer::onAlloc(uv_handle_t *, size_t, uv_buf_t *buf) { buf->base = getPool()->allocate(); - buf->len = kChunkSize; + buf->len = XMRIG_NET_BUFFER_CHUNK_SIZE; } void xmrig::NetBuffer::release(const char *buf) { + if (buf == nullptr) { + return; + } + getPool()->deallocate(buf); } void xmrig::NetBuffer::release(const uv_buf_t *buf) { + if (buf->base == nullptr) { + return; + } + getPool()->deallocate(buf->base); } diff --git a/src/base/net/tools/NetBuffer.h b/src/base/net/tools/NetBuffer.h index 14255bb0..a7a2ee05 100644 --- a/src/base/net/tools/NetBuffer.h +++ b/src/base/net/tools/NetBuffer.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -33,8 +33,6 @@ namespace xmrig { class NetBuffer { public: - static constexpr size_t kChunkSize = 16 * 1024; - static char *allocate(); static void destroy(); static void onAlloc(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf); diff --git a/src/base/tools/Buffer.cpp b/src/base/tools/Buffer.cpp deleted file mode 100644 index e260b7a5..00000000 --- a/src/base/tools/Buffer.cpp +++ /dev/null @@ -1,231 +0,0 @@ -/* 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-2020 SChernykh - * Copyright 2016-2020 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/tools/Buffer.h" - - -#include - - -namespace xmrig { - - -static std::random_device randomDevice; -static std::mt19937 randomEngine(randomDevice()); - - -} // namespace xmrig - - -static inline uint8_t hf_hex2bin(uint8_t c, bool &err) -{ - if (c >= '0' && c <= '9') { - return c - '0'; - } - - if (c >= 'a' && c <= 'f') { - return c - 'a' + 0xA; - } - - if (c >= 'A' && c <= 'F') { - return c - 'A' + 0xA; - } - - err = true; - return 0; -} - - -static inline uint8_t hf_bin2hex(uint8_t c) -{ - if (c <= 0x9) { - return '0' + c; - } - - return 'a' - 0xA + c; -} - - -xmrig::Buffer::Buffer(Buffer &&other) noexcept : - m_data(other.m_data), - m_size(other.m_size) -{ - other.m_data = nullptr; - other.m_size = 0; -} - - -xmrig::Buffer::Buffer(const Buffer &other) -{ - copy(other.data(), other.size()); -} - - -xmrig::Buffer::Buffer(const char *data, size_t size) -{ - copy(data, size); -} - - -xmrig::Buffer::Buffer(size_t size) : - m_size(size) -{ - if (size > 0) { - m_data = new char[size](); - } -} - - -xmrig::Buffer::~Buffer() -{ - delete [] m_data; -} - - -void xmrig::Buffer::from(const char *data, size_t size) -{ - if (m_size > 0) { - if (m_size == size) { - memcpy(m_data, data, m_size); - - return; - } - - delete [] m_data; - } - - copy(data, size); -} - - -xmrig::Buffer xmrig::Buffer::allocUnsafe(size_t size) -{ - if (size == 0) { - return {}; - } - - Buffer buf; - buf.m_size = size; - buf.m_data = new char[size]; - - return buf; -} - - -xmrig::Buffer xmrig::Buffer::randomBytes(const size_t size) -{ - Buffer buf(size); - std::uniform_int_distribution<> dis(0, 255); - - for (size_t i = 0; i < size; ++i) { - buf.m_data[i] = static_cast(dis(randomEngine)); - } - - return buf; -} - - -bool xmrig::Buffer::fromHex(const uint8_t *in, size_t size, uint8_t *out) -{ - bool error = false; - for (size_t i = 0; i < size; i += 2) { - out[i / 2] = static_cast((hf_hex2bin(in[i], error) << 4) | hf_hex2bin(in[i + 1], error)); - - if (error) { - return false; - } - } - - return true; -} - - -xmrig::Buffer xmrig::Buffer::fromHex(const char *data, size_t size) -{ - if (data == nullptr || size % 2 != 0) { - return {}; - } - - Buffer buf(size / 2); - if (!fromHex(data, size, buf.data())) { - return {}; - } - - return buf; -} - - -void xmrig::Buffer::toHex(const uint8_t *in, size_t size, uint8_t *out) -{ - for (size_t i = 0; i < size; i++) { - out[i * 2] = hf_bin2hex((in[i] & 0xF0) >> 4); - out[i * 2 + 1] = hf_bin2hex(in[i] & 0x0F); - } -} - - -xmrig::String xmrig::Buffer::toHex() const -{ - if (m_size == 0) { - return String(); - } - - char *buf = new char[m_size * 2 + 1]; - buf[m_size * 2] = '\0'; - - toHex(m_data, m_size, buf); - - return String(buf); -} - - -void xmrig::Buffer::copy(const char *data, size_t size) -{ - if (size == 0) { - m_data = nullptr; - m_size = 0; - - return; - } - - m_data = new char[size]; - m_size = size; - - memcpy(m_data, data, m_size); -} - - -void xmrig::Buffer::move(Buffer &&other) -{ - if (m_size > 0) { - delete [] m_data; - } - - m_data = other.m_data; - m_size = other.m_size; - - other.m_data = nullptr; - other.m_size = 0; -} diff --git a/src/base/tools/Buffer.h b/src/base/tools/Buffer.h index 33b186c0..91c98e64 100644 --- a/src/base/tools/Buffer.h +++ b/src/base/tools/Buffer.h @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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,67 +20,14 @@ #define XMRIG_BUFFER_H -#include "base/tools/String.h" +#include +#include namespace xmrig { -class Buffer -{ -public: - Buffer() = default; - Buffer(Buffer &&other) noexcept; - Buffer(const Buffer &other); - Buffer(const char *data, size_t size); - Buffer(size_t size); - ~Buffer(); - - - inline bool isEmpty() const { return size() == 0; } - inline bool isEqual(const Buffer &other) const { return m_size == other.m_size && (m_size == 0 || memcmp(m_data, other.m_data, m_size) == 0); } - inline char *data() { return m_data; } - inline const char *data() const { return m_data; } - inline size_t size() const { return m_size; } - inline void from(const Buffer &other) { from(other.data(), other.size()); } - - - void from(const char *data, size_t size); - - - inline bool operator!=(const Buffer &other) const { return !isEqual(other); } - inline bool operator==(const Buffer &other) const { return isEqual(other); } - inline Buffer &operator=(Buffer &&other) noexcept { move(std::move(other)); return *this; } - inline Buffer &operator=(const Buffer &other) { from(other); return *this; } - - - static Buffer allocUnsafe(size_t size); - static Buffer randomBytes(const size_t size); - - static inline Buffer alloc(size_t size) { return Buffer(size); } - - - inline static bool fromHex(const char *in, size_t size, char *out) { return fromHex(reinterpret_cast(in), size, reinterpret_cast(out)); } - inline static bool fromHex(const char *in, size_t size, uint8_t *out) { return fromHex(reinterpret_cast(in), size, out); } - inline static Buffer fromHex(const char *data) { return fromHex(data, strlen(data)); } - inline static Buffer fromHex(const String &str) { return fromHex(str.data(), str.size()); } - inline static String toHex(const char *in, size_t size) { return Buffer(in, size).toHex(); } - inline static String toHex(const uint8_t *in, size_t size) { return Buffer(reinterpret_cast(in), size).toHex(); } - inline static void toHex(const char *in, size_t size, char *out) { return toHex(reinterpret_cast(in), size, reinterpret_cast(out)); } - inline static void toHex(const uint8_t *in, size_t size, char *out) { return toHex(in, size, reinterpret_cast(out)); } - - static bool fromHex(const uint8_t *in, size_t size, uint8_t *out); - static Buffer fromHex(const char *data, size_t size); - static void toHex(const uint8_t *in, size_t size, uint8_t *out); - String toHex() const; - -private: - void copy(const char *data, size_t size); - void move(Buffer &&other); - - char *m_data = nullptr; - size_t m_size = 0; -}; +using Buffer = std::vector; } /* namespace xmrig */ diff --git a/src/base/tools/Cvt.cpp b/src/base/tools/Cvt.cpp new file mode 100644 index 00000000..c80c022c --- /dev/null +++ b/src/base/tools/Cvt.cpp @@ -0,0 +1,275 @@ +/* XMRig + * Copyright (c) 2013-2020 Frank Denis + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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/tools/Cvt.h" +#include "3rdparty/rapidjson/document.h" + + +#include +#include + + +#ifdef XMRIG_SODIUM +# include +#endif + + +namespace xmrig { + + +#ifndef XMRIG_SODIUM +static std::random_device randomDevice; +static std::mt19937 randomEngine(randomDevice()); + + +static char *cvt_bin2hex(char *const hex, const size_t hex_maxlen, const unsigned char *const bin, const size_t bin_len) +{ + size_t i = 0U; + unsigned int x; + int b; + int c; + + if (bin_len >= SIZE_MAX / 2 || hex_maxlen <= bin_len * 2U) { + return nullptr; /* LCOV_EXCL_LINE */ + } + + while (i < bin_len) { + c = bin[i] & 0xf; + b = bin[i] >> 4; + x = (unsigned char) (87U + c + (((c - 10U) >> 8) & ~38U)) << 8 | + (unsigned char) (87U + b + (((b - 10U) >> 8) & ~38U)); + hex[i * 2U] = (char) x; + x >>= 8; + hex[i * 2U + 1U] = (char) x; + i++; + } + hex[i * 2U] = 0U; + + return hex; +} + + +static int cvt_hex2bin(unsigned char *const bin, const size_t bin_maxlen, const char *const hex, const size_t hex_len, const char *const ignore, size_t *const bin_len, const char **const hex_end) +{ + size_t bin_pos = 0U; + size_t hex_pos = 0U; + int ret = 0; + unsigned char c; + unsigned char c_acc = 0U; + unsigned char c_alpha0; + unsigned char c_alpha; + unsigned char c_num0; + unsigned char c_num; + unsigned char c_val; + unsigned char state = 0U; + + while (hex_pos < hex_len) { + c = (unsigned char) hex[hex_pos]; + c_num = c ^ 48U; + c_num0 = (c_num - 10U) >> 8; + c_alpha = (c & ~32U) - 55U; + c_alpha0 = ((c_alpha - 10U) ^ (c_alpha - 16U)) >> 8; + + if ((c_num0 | c_alpha0) == 0U) { + if (ignore != nullptr && state == 0U && strchr(ignore, c) != nullptr) { + hex_pos++; + continue; + } + break; + } + + c_val = (c_num0 & c_num) | (c_alpha0 & c_alpha); + + if (bin_pos >= bin_maxlen) { + ret = -1; + errno = ERANGE; + break; + } + + if (state == 0U) { + c_acc = c_val * 16U; + } else { + bin[bin_pos++] = c_acc | c_val; + } + + state = ~state; + hex_pos++; + } + + if (state != 0U) { + hex_pos--; + errno = EINVAL; + ret = -1; + } + + if (ret != 0) { + bin_pos = 0U; + } + + if (hex_end != nullptr) { + *hex_end = &hex[hex_pos]; + } else if (hex_pos != hex_len) { + errno = EINVAL; + ret = -1; + } + + if (bin_len != nullptr) { + *bin_len = bin_pos; + } + + return ret; +} + +#define sodium_bin2hex cvt_bin2hex +#define sodium_hex2bin cvt_hex2bin +#endif + + +template +inline bool fromHexImpl(T &buf, const char *in, size_t size) +{ + assert(in != nullptr && size > 0); + if (in == nullptr || size == 0) { + return false; + } + + buf.resize(size / 2); + + return sodium_hex2bin(reinterpret_cast(&buf.front()), buf.size(), in, size, nullptr, nullptr, nullptr) == 0; +} + + +} // namespace xmrig + + +bool xmrig::Cvt::fromHex(Buffer &buf, const char *in, size_t size) +{ + return fromHexImpl(buf, in, size); +} + + +bool xmrig::Cvt::fromHex(Buffer &buf, const rapidjson::Value &value) +{ + if (!value.IsString()) { + return false; + } + + return fromHexImpl(buf, value.GetString(), value.GetStringLength()); +} + + +bool xmrig::Cvt::fromHex(std::string &buf, const char *in, size_t size) +{ + return fromHexImpl(buf, in, size); +} + + +bool xmrig::Cvt::fromHex(uint8_t *bin, size_t bin_maxlen, const char *hex, size_t hex_len) +{ + assert(hex != nullptr && hex_len > 0); + if (hex == nullptr || hex_len == 0) { + return false; + } + + return sodium_hex2bin(bin, bin_maxlen, hex, hex_len, nullptr, nullptr, nullptr) == 0; +} + + +bool xmrig::Cvt::fromHex(uint8_t *bin, size_t max, const rapidjson::Value &value) +{ + if (!value.IsString()) { + return false; + } + + return fromHex(bin, max, value.GetString(), value.GetStringLength()); +} + + +xmrig::Buffer xmrig::Cvt::fromHex(const char *in, size_t size) +{ + Buffer buf; + if (!fromHex(buf, in, size)) { + return {}; + } + + return buf; +} + + +bool xmrig::Cvt::toHex(char *hex, size_t hex_maxlen, const uint8_t *bin, size_t bin_len) +{ + return sodium_bin2hex(hex, hex_maxlen, bin, bin_len) != nullptr; +} + + +xmrig::Buffer xmrig::Cvt::randomBytes(const size_t size) +{ + Buffer buf(size); + +# ifndef XMRIG_SODIUM + std::uniform_int_distribution<> dis(0, 255); + + for (size_t i = 0; i < size; ++i) { + buf[i] = static_cast(dis(randomEngine)); + } +# else + randombytes_buf(buf.data(), size); +# endif + + return buf; +} + + +rapidjson::Value xmrig::Cvt::toHex(const Buffer &data, rapidjson::Document &doc) +{ + return toHex(data.data(), data.size(), doc); +} + + +rapidjson::Value xmrig::Cvt::toHex(const std::string &data, rapidjson::Document &doc) +{ + return toHex(reinterpret_cast(data.data()), data.size(), doc); +} + + +rapidjson::Value xmrig::Cvt::toHex(const uint8_t *in, size_t size, rapidjson::Document &doc) +{ + return toHex(in, size).toJSON(doc); +} + + +xmrig::String xmrig::Cvt::toHex(const uint8_t *in, size_t size) +{ + assert(in != nullptr && size > 0); + if (in == nullptr || size == 0) { + return {}; + } + + const size_t hex_maxlen = size * 2 + 1; + + char *buf = new char[hex_maxlen]; + if (!toHex(buf, hex_maxlen, in, size)) { + delete [] buf; + + return {}; + } + + return buf; +} diff --git a/src/base/tools/Cvt.h b/src/base/tools/Cvt.h new file mode 100644 index 00000000..8b67b146 --- /dev/null +++ b/src/base/tools/Cvt.h @@ -0,0 +1,61 @@ +/* XMRig + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 XMRIG_CVT_H +#define XMRIG_CVT_H + + +#include "3rdparty/rapidjson/fwd.h" +#include "base/tools/Buffer.h" +#include "base/tools/String.h" + + +#include + + +namespace xmrig { + + +class Cvt +{ +public: + inline static bool fromHex(Buffer &buf, const String &hex) { return fromHex(buf, hex.data(), hex.size()); } + inline static Buffer fromHex(const std::string &hex) { return fromHex(hex.data(), hex.size()); } + inline static Buffer fromHex(const String &hex) { return fromHex(hex.data(), hex.size()); } + inline static String toHex(const Buffer &data) { return toHex(data.data(), data.size()); } + inline static String toHex(const std::string &data) { return toHex(reinterpret_cast(data.data()), data.size()); } + + static bool fromHex(Buffer &buf, const char *in, size_t size); + static bool fromHex(Buffer &buf, const rapidjson::Value &value); + static bool fromHex(std::string &buf, const char *in, size_t size); + static bool fromHex(uint8_t *bin, size_t bin_maxlen, const char *hex, size_t hex_len); + static bool fromHex(uint8_t *bin, size_t bin_maxlen, const rapidjson::Value &value); + static bool toHex(char *hex, size_t hex_maxlen, const uint8_t *bin, size_t bin_len); + static Buffer fromHex(const char *in, size_t size); + static Buffer randomBytes(size_t size); + static rapidjson::Value toHex(const Buffer &data, rapidjson::Document &doc); + static rapidjson::Value toHex(const std::string &data, rapidjson::Document &doc); + static rapidjson::Value toHex(const uint8_t *in, size_t size, rapidjson::Document &doc); + static String toHex(const uint8_t *in, size_t size); +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_CVT_H */ diff --git a/src/core/Miner.cpp b/src/core/Miner.cpp index 67a93488..9c96d1e3 100644 --- a/src/core/Miner.cpp +++ b/src/core/Miner.cpp @@ -38,12 +38,10 @@ #include "base/kernel/Platform.h" #include "base/net/stratum/Job.h" #include "base/tools/Object.h" -#include "base/tools/Profiler.h" #include "base/tools/Timer.h" #include "core/config/Config.h" #include "core/Controller.h" #include "crypto/common/Nonce.h" -#include "crypto/rx/Rx.h" #include "version.h" @@ -64,6 +62,8 @@ #ifdef XMRIG_ALGO_RANDOMX +# include "crypto/rx/Profiler.h" +# include "crypto/rx/Rx.h" # include "crypto/rx/RxConfig.h" #endif @@ -203,7 +203,7 @@ public: continue; } - for (size_t i = 1; i < hr->threads(); i++) { + for (size_t i = 0; i < hr->threads(); i++) { Value thread(kArrayType); thread.PushBack(Hashrate::normalize(hr->calc(i, Hashrate::ShortInterval)), allocator); thread.PushBack(Hashrate::normalize(hr->calc(i, Hashrate::MediumInterval)), allocator); diff --git a/src/crypto/common/Assembly.cpp b/src/crypto/common/Assembly.cpp index 5b7f8959..eb6b8537 100644 --- a/src/crypto/common/Assembly.cpp +++ b/src/crypto/common/Assembly.cpp @@ -1,13 +1,6 @@ /* 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 SChernykh - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -24,8 +17,8 @@ */ -#include -#include +#include +#include #ifdef _MSC_VER diff --git a/src/crypto/common/Assembly.h b/src/crypto/common/Assembly.h index 803ea716..8d7ba43c 100644 --- a/src/crypto/common/Assembly.h +++ b/src/crypto/common/Assembly.h @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -57,6 +51,7 @@ public: rapidjson::Value toJSON() const; inline bool isEqual(const Assembly &other) const { return m_id == other.m_id; } + inline Id id() const { return m_id; } inline bool operator!=(Assembly::Id id) const { return m_id != id; } inline bool operator!=(const Assembly &other) const { return !isEqual(other); } diff --git a/src/crypto/randomx/aes_hash.cpp b/src/crypto/randomx/aes_hash.cpp index 6697a4ed..4d0ee7a2 100644 --- a/src/crypto/randomx/aes_hash.cpp +++ b/src/crypto/randomx/aes_hash.cpp @@ -30,10 +30,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include "crypto/randomx/aes_hash.hpp" -#include "crypto/randomx/soft_aes.h" -#include "crypto/randomx/randomx.h" #include "base/tools/Chrono.h" -#include "base/tools/Profiler.h" +#include "crypto/randomx/randomx.h" +#include "crypto/randomx/soft_aes.h" +#include "crypto/rx/Profiler.h" #define AES_HASH_1R_STATE0 0xd7983aad, 0xcc82db47, 0x9fa856de, 0x92b52c0d #define AES_HASH_1R_STATE1 0xace78057, 0xf59e125a, 0x15c7b798, 0x338d996e diff --git a/src/crypto/randomx/jit_compiler_x86.cpp b/src/crypto/randomx/jit_compiler_x86.cpp index 9753701e..4d5d0ee1 100644 --- a/src/crypto/randomx/jit_compiler_x86.cpp +++ b/src/crypto/randomx/jit_compiler_x86.cpp @@ -31,13 +31,13 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include "crypto/randomx/jit_compiler_x86.hpp" +#include "backend/cpu/Cpu.h" #include "crypto/randomx/jit_compiler_x86_static.hpp" -#include "crypto/randomx/superscalar.hpp" #include "crypto/randomx/program.hpp" #include "crypto/randomx/reciprocal.h" +#include "crypto/randomx/superscalar.hpp" #include "crypto/randomx/virtual_memory.hpp" -#include "base/tools/Profiler.h" -#include "backend/cpu/Cpu.h" +#include "crypto/rx/Profiler.h" #ifdef XMRIG_FIX_RYZEN # include "crypto/rx/Rx.h" diff --git a/src/crypto/randomx/randomx.cpp b/src/crypto/randomx/randomx.cpp index 9dd4aeee..28b9f39a 100644 --- a/src/crypto/randomx/randomx.cpp +++ b/src/crypto/randomx/randomx.cpp @@ -47,7 +47,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include -#include "base/tools/Profiler.h" +#include "crypto/rx/Profiler.h" RandomX_ConfigurationWownero::RandomX_ConfigurationWownero() { diff --git a/src/crypto/randomx/virtual_machine.cpp b/src/crypto/randomx/virtual_machine.cpp index 4a6990b2..43e19939 100644 --- a/src/crypto/randomx/virtual_machine.cpp +++ b/src/crypto/randomx/virtual_machine.cpp @@ -30,13 +30,13 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include "crypto/randomx/virtual_machine.hpp" -#include "crypto/randomx/common.hpp" #include "crypto/randomx/aes_hash.hpp" -#include "crypto/randomx/blake2/blake2.h" -#include "crypto/randomx/intrin_portable.h" #include "crypto/randomx/allocator.hpp" +#include "crypto/randomx/blake2/blake2.h" +#include "crypto/randomx/common.hpp" +#include "crypto/randomx/intrin_portable.h" #include "crypto/randomx/soft_aes.h" -#include "base/tools/Profiler.h" +#include "crypto/rx/Profiler.h" randomx_vm::~randomx_vm() { diff --git a/src/crypto/randomx/vm_compiled.cpp b/src/crypto/randomx/vm_compiled.cpp index c32034b3..c40247d9 100644 --- a/src/crypto/randomx/vm_compiled.cpp +++ b/src/crypto/randomx/vm_compiled.cpp @@ -28,7 +28,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "crypto/randomx/vm_compiled.hpp" #include "crypto/randomx/common.hpp" -#include "base/tools/Profiler.h" +#include "crypto/rx/Profiler.h" namespace randomx { diff --git a/src/base/tools/Profiler.cpp b/src/crypto/rx/Profiler.cpp similarity index 93% rename from src/base/tools/Profiler.cpp rename to src/crypto/rx/Profiler.cpp index ac2a6d2c..9cfe72a0 100644 --- a/src/base/tools/Profiler.cpp +++ b/src/crypto/rx/Profiler.cpp @@ -1,6 +1,6 @@ /* XMRig - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -17,9 +17,11 @@ */ -#include "base/tools/Profiler.h" +#include "crypto/rx/Profiler.h" #include "base/io/log/Log.h" #include "base/io/log/Tags.h" + + #include #include #include diff --git a/src/base/tools/Profiler.h b/src/crypto/rx/Profiler.h similarity index 94% rename from src/base/tools/Profiler.h rename to src/crypto/rx/Profiler.h index ae3470f8..ca7d29db 100644 --- a/src/base/tools/Profiler.h +++ b/src/crypto/rx/Profiler.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 diff --git a/src/crypto/rx/Rx.cpp b/src/crypto/rx/Rx.cpp index 2fffb0e6..ea671d01 100644 --- a/src/crypto/rx/Rx.cpp +++ b/src/crypto/rx/Rx.cpp @@ -1,14 +1,7 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2019 XMR-Stak , - * Copyright 2018 Lee Clagett - * Copyright 2018-2019 tevador - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2019 tevador + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 diff --git a/src/crypto/rx/Rx.h b/src/crypto/rx/Rx.h index 1c452e8b..42bd0c15 100644 --- a/src/crypto/rx/Rx.h +++ b/src/crypto/rx/Rx.h @@ -1,14 +1,7 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2019 XMR-Stak , - * Copyright 2018 Lee Clagett - * Copyright 2018-2019 tevador - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2019 tevador + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 diff --git a/src/crypto/rx/RxAlgo.cpp b/src/crypto/rx/RxAlgo.cpp index 91d2c195..b7d2b083 100644 --- a/src/crypto/rx/RxAlgo.cpp +++ b/src/crypto/rx/RxAlgo.cpp @@ -1,14 +1,7 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2019 XMR-Stak , - * Copyright 2018 Lee Clagett - * Copyright 2018-2019 tevador - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright (c) 2018-2019 tevador + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 diff --git a/src/crypto/rx/RxAlgo.h b/src/crypto/rx/RxAlgo.h index 81c4687c..0cbbbd35 100644 --- a/src/crypto/rx/RxAlgo.h +++ b/src/crypto/rx/RxAlgo.h @@ -1,14 +1,7 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2019 XMR-Stak , - * Copyright 2018 Lee Clagett - * Copyright 2018-2019 tevador - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2019 tevador + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 diff --git a/src/crypto/rx/RxBasicStorage.cpp b/src/crypto/rx/RxBasicStorage.cpp index 7f389666..eacb6295 100644 --- a/src/crypto/rx/RxBasicStorage.cpp +++ b/src/crypto/rx/RxBasicStorage.cpp @@ -1,14 +1,7 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2019 XMR-Stak , - * Copyright 2018 Lee Clagett - * Copyright 2018-2019 tevador - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2019 tevador + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -30,7 +23,6 @@ #include "base/io/log/Log.h" #include "base/io/log/Tags.h" #include "base/tools/Chrono.h" -#include "base/tools/Object.h" #include "crypto/rx/RxAlgo.h" #include "crypto/rx/RxCache.h" #include "crypto/rx/RxDataset.h" diff --git a/src/crypto/rx/RxBasicStorage.h b/src/crypto/rx/RxBasicStorage.h index c689df83..463b479e 100644 --- a/src/crypto/rx/RxBasicStorage.h +++ b/src/crypto/rx/RxBasicStorage.h @@ -1,14 +1,7 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2019 XMR-Stak , - * Copyright 2018 Lee Clagett - * Copyright 2018-2019 tevador - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2019 tevador + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -29,7 +22,6 @@ #include "backend/common/interfaces/IRxStorage.h" -#include "base/tools/Object.h" namespace xmrig diff --git a/src/crypto/rx/RxConfig.cpp b/src/crypto/rx/RxConfig.cpp index c501e712..d9f05d4e 100644 --- a/src/crypto/rx/RxConfig.cpp +++ b/src/crypto/rx/RxConfig.cpp @@ -122,7 +122,7 @@ bool xmrig::RxConfig::read(const rapidjson::Value &value) } # endif - const uint32_t mode = static_cast(Json::getInt(value, kScratchpadPrefetchMode, static_cast(m_scratchpadPrefetchMode))); + const auto mode = static_cast(Json::getInt(value, kScratchpadPrefetchMode, static_cast(m_scratchpadPrefetchMode))); if (mode < ScratchpadPrefetchMax) { m_scratchpadPrefetchMode = static_cast(mode); } diff --git a/src/crypto/rx/RxDataset.cpp b/src/crypto/rx/RxDataset.cpp index c207d6f4..410a2f34 100644 --- a/src/crypto/rx/RxDataset.cpp +++ b/src/crypto/rx/RxDataset.cpp @@ -1,14 +1,7 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2019 XMR-Stak , - * Copyright 2018 Lee Clagett - * Copyright 2018-2019 tevador - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2019 tevador + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -30,6 +23,7 @@ #include "base/io/log/Tags.h" #include "base/kernel/Platform.h" #include "crypto/common/VirtualMemory.h" +#include "crypto/randomx/randomx.h" #include "crypto/rx/RxAlgo.h" #include "crypto/rx/RxCache.h" @@ -162,6 +156,22 @@ size_t xmrig::RxDataset::size(bool cache) const } +uint8_t *xmrig::RxDataset::tryAllocateScrathpad() +{ + auto p = reinterpret_cast(raw()); + if (!p) { + return nullptr; + } + + const size_t offset = m_scratchpadOffset.fetch_add(RANDOMX_SCRATCHPAD_L3_MAX_SIZE); + if (offset + RANDOMX_SCRATCHPAD_L3_MAX_SIZE > m_scratchpadLimit) { + return nullptr; + } + + return p + offset; +} + + void *xmrig::RxDataset::raw() const { return m_dataset ? randomx_get_dataset_memory(m_dataset) : nullptr; @@ -208,19 +218,3 @@ void xmrig::RxDataset::allocate(bool hugePages, bool oneGbPages) } # endif } - - -uint8_t* xmrig::RxDataset::tryAllocateScrathpad() -{ - uint8_t* p = reinterpret_cast(raw()); - if (!p) { - return nullptr; - } - - const size_t offset = m_scratchpadOffset.fetch_add(RANDOMX_SCRATCHPAD_L3_MAX_SIZE); - if (offset + RANDOMX_SCRATCHPAD_L3_MAX_SIZE > m_scratchpadLimit) { - return nullptr; - } - - return p + offset; -} diff --git a/src/crypto/rx/RxDataset.h b/src/crypto/rx/RxDataset.h index 798ec996..1621cae1 100644 --- a/src/crypto/rx/RxDataset.h +++ b/src/crypto/rx/RxDataset.h @@ -1,14 +1,7 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2019 XMR-Stak , - * Copyright 2018 Lee Clagett - * Copyright 2018-2019 tevador - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2019 tevador + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -28,12 +21,10 @@ #define XMRIG_RX_DATASET_H -#include "base/crypto/Algorithm.h" #include "base/tools/Buffer.h" #include "base/tools/Object.h" #include "crypto/common/HugePagesInfo.h" #include "crypto/randomx/configuration.h" -#include "crypto/randomx/randomx.h" #include "crypto/rx/RxConfig.h" #include @@ -68,11 +59,10 @@ public: bool isOneGbPages() const; HugePagesInfo hugePages(bool cache = true) const; size_t size(bool cache = true) const; + uint8_t *tryAllocateScrathpad(); void *raw() const; void setRaw(const void *raw); - uint8_t *tryAllocateScrathpad(); - static inline constexpr size_t maxSize() { return RANDOMX_DATASET_MAX_SIZE; } private: @@ -82,10 +72,9 @@ private: const uint32_t m_node; randomx_dataset *m_dataset = nullptr; RxCache *m_cache = nullptr; - VirtualMemory *m_memory = nullptr; - - std::atomic m_scratchpadOffset; size_t m_scratchpadLimit = 0; + std::atomic m_scratchpadOffset{}; + VirtualMemory *m_memory = nullptr; }; diff --git a/src/crypto/rx/RxNUMAStorage.cpp b/src/crypto/rx/RxNUMAStorage.cpp index 4b4ab4d6..6bd5627f 100644 --- a/src/crypto/rx/RxNUMAStorage.cpp +++ b/src/crypto/rx/RxNUMAStorage.cpp @@ -1,14 +1,7 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2019 XMR-Stak , - * Copyright 2018 Lee Clagett - * Copyright 2018-2019 tevador - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2019 tevador + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -32,7 +25,6 @@ #include "base/io/log/Tags.h" #include "base/kernel/Platform.h" #include "base/tools/Chrono.h" -#include "base/tools/Object.h" #include "crypto/rx/RxAlgo.h" #include "crypto/rx/RxCache.h" #include "crypto/rx/RxDataset.h" diff --git a/src/crypto/rx/RxNUMAStorage.h b/src/crypto/rx/RxNUMAStorage.h index ef0d6431..29ec5fb0 100644 --- a/src/crypto/rx/RxNUMAStorage.h +++ b/src/crypto/rx/RxNUMAStorage.h @@ -1,14 +1,7 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2019 XMR-Stak , - * Copyright 2018 Lee Clagett - * Copyright 2018-2019 tevador - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2019 tevador + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -29,7 +22,6 @@ #include "backend/common/interfaces/IRxStorage.h" -#include "base/tools/Object.h" #include diff --git a/src/crypto/rx/RxQueue.cpp b/src/crypto/rx/RxQueue.cpp index 8badab50..cbbac070 100644 --- a/src/crypto/rx/RxQueue.cpp +++ b/src/crypto/rx/RxQueue.cpp @@ -30,6 +30,7 @@ #include "base/io/Async.h" #include "base/io/log/Log.h" #include "base/io/log/Tags.h" +#include "base/tools/Cvt.h" #include "crypto/rx/RxBasicStorage.h" @@ -149,7 +150,7 @@ void xmrig::RxQueue::backgroundInit() item.nodeset.size() > 1 ? "s" : "", item.seed.algorithm().shortName(), item.threads, - Buffer::toHex(item.seed.data().data(), 8).data() + Cvt::toHex(item.seed.data().data(), 8).data() ); m_storage->init(item.seed, item.threads, item.hugePages, item.oneGbPages, item.mode, item.priority); diff --git a/src/crypto/rx/RxVm.cpp b/src/crypto/rx/RxVm.cpp index aa2217fc..f4b37375 100644 --- a/src/crypto/rx/RxVm.cpp +++ b/src/crypto/rx/RxVm.cpp @@ -1,14 +1,7 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2019 XMR-Stak , - * Copyright 2018 Lee Clagett - * Copyright 2018-2019 tevador - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2019 tevador + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 +19,7 @@ #include "crypto/randomx/randomx.h" +#include "backend/cpu/Cpu.h" #include "crypto/rx/RxCache.h" #include "crypto/rx/RxDataset.h" #include "crypto/rx/RxVm.h" @@ -36,7 +30,7 @@ extern "C" uint32_t rx_blake2b_use_sse41; #endif -randomx_vm* xmrig::RxVm::create(RxDataset *dataset, uint8_t *scratchpad, bool softAes, xmrig::Assembly assembly, uint32_t node) +randomx_vm *xmrig::RxVm::create(RxDataset *dataset, uint8_t *scratchpad, bool softAes, const Assembly &assembly, uint32_t node) { int flags = 0; @@ -52,11 +46,8 @@ randomx_vm* xmrig::RxVm::create(RxDataset *dataset, uint8_t *scratchpad, bool so flags |= RANDOMX_FLAG_JIT; } - if (assembly == Assembly::AUTO) { - assembly = Cpu::info()->assembly(); - } - - if ((assembly == Assembly::RYZEN) || (assembly == Assembly::BULLDOZER)) { + const auto asmId = assembly == Assembly::AUTO ? Cpu::info()->assembly() : assembly.id(); + if ((asmId == Assembly::RYZEN) || (asmId == Assembly::BULLDOZER)) { flags |= RANDOMX_FLAG_AMD; } diff --git a/src/crypto/rx/RxVm.h b/src/crypto/rx/RxVm.h index 6397d726..424da209 100644 --- a/src/crypto/rx/RxVm.h +++ b/src/crypto/rx/RxVm.h @@ -1,14 +1,7 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2019 XMR-Stak , - * Copyright 2018 Lee Clagett - * Copyright 2018-2019 tevador - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2019 tevador + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -28,10 +21,6 @@ #define XMRIG_RX_VM_H -#include "base/tools/Object.h" -#include "backend/cpu/Cpu.h" - - #include @@ -42,16 +31,15 @@ namespace xmrig { +class Assembly; class RxDataset; class RxVm { public: - XMRIG_DISABLE_COPY_MOVE_DEFAULT(RxVm); - - static randomx_vm* create(RxDataset *dataset, uint8_t *scratchpad, bool softAes, xmrig::Assembly assembly, uint32_t node); - static void destroy(randomx_vm* vm); + static randomx_vm *create(RxDataset *dataset, uint8_t *scratchpad, bool softAes, const Assembly &assembly, uint32_t node); + static void destroy(randomx_vm *vm); }; diff --git a/src/crypto/rx/Rx_linux.cpp b/src/crypto/rx/Rx_linux.cpp index 62ce936a..1525f99d 100644 --- a/src/crypto/rx/Rx_linux.cpp +++ b/src/crypto/rx/Rx_linux.cpp @@ -1,16 +1,9 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2019 XMR-Stak , - * Copyright 2018 Lee Clagett - * Copyright 2018-2019 tevador - * Copyright 2000 Transmeta Corporation - * Copyright 2004-2008 H. Peter Anvin - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2019 tevador + * Copyright (c) 2000 Transmeta Corporation + * Copyright (c) 2004-2008 H. Peter Anvin + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 diff --git a/src/crypto/rx/Rx_win.cpp b/src/crypto/rx/Rx_win.cpp index b32c3dae..77e7c9b4 100644 --- a/src/crypto/rx/Rx_win.cpp +++ b/src/crypto/rx/Rx_win.cpp @@ -1,17 +1,10 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2019 XMR-Stak , - * Copyright 2018 Lee Clagett - * Copyright 2018-2019 tevador - * Copyright 2000 Transmeta Corporation - * Copyright 2004-2008 H. Peter Anvin - * Copyright 2007-2009 hiyohiyo , - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2019 tevador + * Copyright (c) 2000 Transmeta Corporation + * Copyright (c) 2004-2008 H. Peter Anvin + * Copyright (c) 2007-2009 hiyohiyo , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index f8a391d9..28a1b1cd 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -37,6 +37,7 @@ #include "base/net/stratum/strategies/FailoverStrategy.h" #include "base/net/stratum/strategies/SinglePoolStrategy.h" #include "base/tools/Buffer.h" +#include "base/tools/Cvt.h" #include "base/tools/Timer.h" #include "core/config/Config.h" #include "core/Controller.h" @@ -65,9 +66,9 @@ xmrig::DonateStrategy::DonateStrategy(Controller *controller, IStrategyListener { uint8_t hash[200]; - const String &user = controller->config()->pools().data().front().user(); + const auto &user = controller->config()->pools().data().front().user(); keccak(reinterpret_cast(user.data()), user.size(), hash); - Buffer::toHex(hash, 32, m_userId); + Cvt::toHex(m_userId, sizeof(m_userId), hash, 32); # ifdef XMRIG_ALGO_KAWPOW constexpr Pool::Mode mode = Pool::MODE_AUTO_ETH; diff --git a/src/version.h b/src/version.h index 11fed710..77ad0987 100644 --- a/src/version.h +++ b/src/version.h @@ -28,15 +28,15 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig miner" -#define APP_VERSION "6.6.3-dev" +#define APP_VERSION "6.7.0-dev" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2020 xmrig.com" #define APP_KIND "miner" #define APP_VER_MAJOR 6 -#define APP_VER_MINOR 6 -#define APP_VER_PATCH 3 +#define APP_VER_MINOR 7 +#define APP_VER_PATCH 0 #ifdef _MSC_VER # if (_MSC_VER >= 1920)