From 426bc8f0c4eade47396a16c6ac59c245004d48fc Mon Sep 17 00:00:00 2001 From: SChernykh Date: Sat, 9 Nov 2019 17:29:12 +0100 Subject: [PATCH] Optimized hashrate calculation --- src/backend/common/Hashrate.cpp | 48 +++++++++++++-------------------- src/backend/common/Hashrate.h | 3 --- src/backend/common/Workers.cpp | 2 -- 3 files changed, 19 insertions(+), 34 deletions(-) diff --git a/src/backend/common/Hashrate.cpp b/src/backend/common/Hashrate.cpp index 1c9873e01..dedb74955 100644 --- a/src/backend/common/Hashrate.cpp +++ b/src/backend/common/Hashrate.cpp @@ -47,7 +47,6 @@ inline static const char *format(double h, char *buf, size_t size) xmrig::Hashrate::Hashrate(size_t threads) : - m_highest(0.0), m_threads(threads) { m_counts = new uint64_t*[threads]; @@ -100,30 +99,30 @@ double xmrig::Hashrate::calc(size_t threadId, size_t ms) const uint64_t earliestHashCount = 0; uint64_t earliestStamp = 0; - uint64_t lastestStamp = 0; - uint64_t lastestHashCnt = 0; bool haveFullSet = false; - for (size_t i = 1; i < kBucketSize; i++) { - const size_t idx = (m_top[threadId] - i) & kBucketMask; + const uint64_t timeStampLimit = xmrig::Chrono::highResolutionMSecs() - ms; + uint64_t* timestamps = m_timestamps[threadId]; + uint64_t* counts = m_counts[threadId]; - if (m_timestamps[threadId][idx] == 0) { + 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; } - - if (lastestStamp == 0) { - lastestStamp = m_timestamps[threadId][idx]; - lastestHashCnt = m_counts[threadId][idx]; - } - - if (xmrig::Chrono::highResolutionMSecs() - m_timestamps[threadId][idx] > ms) { - haveFullSet = true; - break; - } - - earliestStamp = m_timestamps[threadId][idx]; - earliestHashCount = m_counts[threadId][idx]; - } + idx = (idx - 1) & kBucketMask; + } while (idx != idx_start); if (!haveFullSet || earliestStamp == 0 || lastestStamp == 0) { return nan(""); @@ -150,15 +149,6 @@ void xmrig::Hashrate::add(size_t threadId, uint64_t count, uint64_t timestamp) } -void xmrig::Hashrate::updateHighest() -{ - double highest = calc(ShortInterval); - if (std::isnormal(highest) && highest > m_highest) { - m_highest = highest; - } -} - - const char *xmrig::Hashrate::format(double h, char *buf, size_t size) { return ::format(h, buf, size); diff --git a/src/backend/common/Hashrate.h b/src/backend/common/Hashrate.h index b9ca8430d..ba60d2adf 100644 --- a/src/backend/common/Hashrate.h +++ b/src/backend/common/Hashrate.h @@ -53,9 +53,7 @@ public: 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); - void updateHighest(); - inline double highest() const { return m_highest; } inline size_t threads() const { return m_threads; } static const char *format(double h, char *buf, size_t size); @@ -70,7 +68,6 @@ private: constexpr static size_t kBucketSize = 2 << 11; constexpr static size_t kBucketMask = kBucketSize - 1; - double m_highest; size_t m_threads; uint32_t* m_top; uint64_t** m_counts; diff --git a/src/backend/common/Workers.cpp b/src/backend/common/Workers.cpp index 319f2804c..8e195b66a 100644 --- a/src/backend/common/Workers.cpp +++ b/src/backend/common/Workers.cpp @@ -144,8 +144,6 @@ void xmrig::Workers::tick(uint64_t) d_ptr->hashrate->add(handle->id(), handle->worker()->hashCount(), handle->worker()->timestamp()); } - - d_ptr->hashrate->updateHighest(); }