diff --git a/src/api/Api.cpp b/src/api/Api.cpp index 6f9991ee..3fff45b5 100644 --- a/src/api/Api.cpp +++ b/src/api/Api.cpp @@ -62,16 +62,6 @@ void Api::exec(const xmrig::HttpRequest &req, xmrig::HttpReply &reply) } -void Api::tick(const Hashrate *hashrate) -{ - if (!m_router) { - return; - } - - m_router->tick(hashrate); -} - - void Api::tick(const NetworkState &network) { if (!m_router) { diff --git a/src/api/Api.h b/src/api/Api.h index 43c7b17e..316bb0fa 100644 --- a/src/api/Api.h +++ b/src/api/Api.h @@ -47,7 +47,6 @@ public: static void release(); static void exec(const xmrig::HttpRequest &req, xmrig::HttpReply &reply); - static void tick(const Hashrate *hashrate); static void tick(const NetworkState &results); private: diff --git a/src/api/ApiRouter.cpp b/src/api/ApiRouter.cpp index 8206aa3e..08cd9bf6 100644 --- a/src/api/ApiRouter.cpp +++ b/src/api/ApiRouter.cpp @@ -47,6 +47,7 @@ #include "rapidjson/stringbuffer.h" #include "version.h" #include "workers/Hashrate.h" +#include "workers/Workers.h" extern "C" @@ -68,10 +69,6 @@ static inline double normalize(double d) ApiRouter::ApiRouter(xmrig::Controller *controller) : m_controller(controller) { - m_threads = controller->config()->threadsCount(); - m_hashrate = new double[m_threads * 3](); - - memset(m_totalHashrate, 0, sizeof(m_totalHashrate)); memset(m_workerId, 0, sizeof(m_workerId)); setWorkerId(controller->config()->apiWorkerId()); @@ -81,7 +78,6 @@ ApiRouter::ApiRouter(xmrig::Controller *controller) : ApiRouter::~ApiRouter() { - delete [] m_hashrate; } @@ -129,21 +125,6 @@ void ApiRouter::exec(const xmrig::HttpRequest &req, xmrig::HttpReply &reply) } -void ApiRouter::tick(const Hashrate *hashrate) -{ - for (int i = 0; i < m_threads; ++i) { - m_hashrate[i * 3] = hashrate->calc((size_t) i, Hashrate::ShortInterval); - m_hashrate[i * 3 + 1] = hashrate->calc((size_t) i, Hashrate::MediumInterval); - m_hashrate[i * 3 + 2] = hashrate->calc((size_t) i, Hashrate::LargeInterval); - } - - m_totalHashrate[0] = hashrate->calc(Hashrate::ShortInterval); - m_totalHashrate[1] = hashrate->calc(Hashrate::MediumInterval); - m_totalHashrate[2] = hashrate->calc(Hashrate::LargeInterval); - m_highestHashrate = hashrate->highest(); -} - - void ApiRouter::tick(const NetworkState &network) { m_network = network; @@ -225,21 +206,23 @@ void ApiRouter::getHashrate(rapidjson::Document &doc) const rapidjson::Value total(rapidjson::kArrayType); rapidjson::Value threads(rapidjson::kArrayType); - for (int i = 0; i < 3; ++i) { - total.PushBack(normalize(m_totalHashrate[i]), allocator); - } + const Hashrate *hr = Workers::hashrate(); - for (int i = 0; i < m_threads * 3; i += 3) { + total.PushBack(normalize(hr->calc(Hashrate::ShortInterval)), allocator); + total.PushBack(normalize(hr->calc(Hashrate::MediumInterval)), allocator); + total.PushBack(normalize(hr->calc(Hashrate::LargeInterval)), allocator); + + for (size_t i = 0; i < Workers::threads(); i++) { rapidjson::Value thread(rapidjson::kArrayType); - thread.PushBack(normalize(m_hashrate[i]), allocator); - thread.PushBack(normalize(m_hashrate[i + 1]), allocator); - thread.PushBack(normalize(m_hashrate[i + 2]), allocator); + thread.PushBack(normalize(hr->calc(i, Hashrate::ShortInterval)), allocator); + thread.PushBack(normalize(hr->calc(i, Hashrate::MediumInterval)), allocator); + thread.PushBack(normalize(hr->calc(i, Hashrate::LargeInterval)), allocator); threads.PushBack(thread, allocator); } - hashrate.AddMember("total", total, allocator); - hashrate.AddMember("highest", normalize(m_highestHashrate), allocator); + hashrate.AddMember("total", total, allocator); + hashrate.AddMember("highest", normalize(hr->highest()), allocator); hashrate.AddMember("threads", threads, allocator); doc.AddMember("hashrate", hashrate, allocator); } diff --git a/src/api/ApiRouter.h b/src/api/ApiRouter.h index e14f9e87..3ed458d4 100644 --- a/src/api/ApiRouter.h +++ b/src/api/ApiRouter.h @@ -49,7 +49,6 @@ public: void get(const xmrig::HttpRequest &req, xmrig::HttpReply &reply) const; void exec(const xmrig::HttpRequest &req, xmrig::HttpReply &reply); - void tick(const Hashrate *hashrate); void tick(const NetworkState &results); protected: @@ -69,10 +68,6 @@ private: char m_id[17]; char m_workerId[128]; - double *m_hashrate; - double m_highestHashrate; - double m_totalHashrate[3]; - int m_threads; NetworkState m_network; xmrig::Controller *m_controller; }; diff --git a/src/workers/Hashrate.cpp b/src/workers/Hashrate.cpp index ef06eb52..7d2e6a4c 100644 --- a/src/workers/Hashrate.cpp +++ b/src/workers/Hashrate.cpp @@ -22,6 +22,7 @@ */ +#include #include #include #include @@ -45,7 +46,7 @@ inline const char *format(double h, char* buf, size_t size) } -Hashrate::Hashrate(int threads, xmrig::Controller *controller) : +Hashrate::Hashrate(size_t threads, xmrig::Controller *controller) : m_highest(0.0), m_threads(threads), m_controller(controller) @@ -54,13 +55,10 @@ Hashrate::Hashrate(int threads, xmrig::Controller *controller) : m_timestamps = new uint64_t*[threads]; m_top = new uint32_t[threads]; - for (int i = 0; i < threads; i++) { - m_counts[i] = new uint64_t[kBucketSize]; - m_timestamps[i] = new uint64_t[kBucketSize]; - m_top[i] = 0; - - memset(m_counts[0], 0, sizeof(uint64_t) * kBucketSize); - memset(m_timestamps[0], 0, sizeof(uint64_t) * kBucketSize); + for (size_t i = 0; i < threads; i++) { + m_counts[i] = new uint64_t[kBucketSize](); + m_timestamps[i] = new uint64_t[kBucketSize](); + m_top[i] = 0; } const int printTime = controller->config()->printTime(); @@ -79,7 +77,7 @@ double Hashrate::calc(size_t ms) const double result = 0.0; double data; - for (int i = 0; i < m_threads; ++i) { + for (size_t i = 0; i < m_threads; ++i) { data = calc(i, ms); if (isnormal(data)) { result += data; @@ -92,6 +90,8 @@ double Hashrate::calc(size_t ms) const double Hashrate::calc(size_t threadId, size_t ms) const { + assert(threadId < m_threads); + using namespace std::chrono; const uint64_t now = time_point_cast(high_resolution_clock::now()).time_since_epoch().count(); diff --git a/src/workers/Hashrate.h b/src/workers/Hashrate.h index 5f4f0eaa..e79c757a 100644 --- a/src/workers/Hashrate.h +++ b/src/workers/Hashrate.h @@ -43,7 +43,7 @@ public: LargeInterval = 900000 }; - Hashrate(int threads, xmrig::Controller *controller); + Hashrate(size_t threads, xmrig::Controller *controller); 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); @@ -52,7 +52,7 @@ public: void updateHighest(); inline double highest() const { return m_highest; } - inline int threads() const { return m_threads; } + inline size_t threads() const { return m_threads; } private: static void onReport(uv_timer_t *handle); @@ -61,7 +61,7 @@ private: constexpr static size_t kBucketMask = kBucketSize - 1; double m_highest; - int m_threads; + size_t m_threads; uint32_t* m_top; uint64_t** m_counts; uint64_t** m_timestamps; diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index e26b2030..068c4258 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -243,10 +243,6 @@ void Workers::onTick(uv_timer_t *handle) if ((m_ticks++ & 0xF) == 0) { m_hashrate->updateHighest(); } - -# ifndef XMRIG_NO_API - Api::tick(m_hashrate); -# endif } diff --git a/src/workers/Workers.h b/src/workers/Workers.h index 81b3411a..0dce78b6 100644 --- a/src/workers/Workers.h +++ b/src/workers/Workers.h @@ -59,6 +59,8 @@ public: static inline bool isEnabled() { return m_enabled; } static inline bool isOutdated(uint64_t sequence) { return m_sequence.load(std::memory_order_relaxed) != sequence; } static inline bool isPaused() { return m_paused.load(std::memory_order_relaxed) == 1; } + static inline Hashrate *hashrate() { return m_hashrate; } + static inline size_t threads() { return m_status.threads; } static inline uint64_t sequence() { return m_sequence.load(std::memory_order_relaxed); } static inline void pause() { m_active = false; m_paused = 1; m_sequence++; } static inline void setListener(IJobResultListener *listener) { m_listener = listener; }