From c9730faa493eb4f95d69bc405727e406a7c6a485 Mon Sep 17 00:00:00 2001
From: SChernykh <sergey.v.chernykh@gmail.com>
Date: Thu, 28 May 2020 22:23:31 +0200
Subject: [PATCH] Hashrate display fixes

---
 src/backend/cuda/CudaBackend.cpp  | 53 +++++++++++++++++++++++--------
 src/backend/opencl/OclBackend.cpp | 51 ++++++++++++++++++++++-------
 2 files changed, 79 insertions(+), 25 deletions(-)

diff --git a/src/backend/cuda/CudaBackend.cpp b/src/backend/cuda/CudaBackend.cpp
index 55441c62b..565472d90 100644
--- a/src/backend/cuda/CudaBackend.cpp
+++ b/src/backend/cuda/CudaBackend.cpp
@@ -52,6 +52,12 @@
 #endif
 
 
+#ifdef XMRIG_ALGO_KAWPOW
+#   include "crypto/kawpow/KPCache.h"
+#   include "crypto/kawpow/KPHash.h"
+#endif
+
+
 #ifdef XMRIG_FEATURE_API
 #   include "base/api/interfaces/IApiRequest.h"
 #endif
@@ -205,7 +211,7 @@ public:
     }
 
 
-    inline void start(const Job &)
+    inline void start(const Job &job)
     {
         LOG_INFO("%s use profile " BLUE_BG(WHITE_BOLD_S " %s ") WHITE_BOLD_S " (" CYAN_BOLD("%zu") WHITE_BOLD(" thread%s)") " scratchpad " CYAN_BOLD("%zu KB"),
                  Tags::nvidia(),
@@ -227,6 +233,15 @@ public:
 
         size_t i = 0;
         for (const auto &data : threads) {
+            size_t mem_used = (data.thread.threads() * data.thread.blocks()) * algo_l3 / oneMiB;
+
+#           ifdef XMRIG_ALGO_KAWPOW
+            if (algo.family() == Algorithm::KAWPOW) {
+                const uint32_t epoch = job.height() / KPHash::EPOCH_LENGTH;
+                mem_used = (KPCache::cache_size(epoch) + KPCache::dag_size(epoch)) / oneMiB;
+            }
+#           endif
+
             Log::print("|" CYAN_BOLD("%3zu") " |" CYAN_BOLD("%4u") " |" YELLOW(" %7s") " |" CYAN_BOLD("%10d") " |" CYAN_BOLD("%8d") " |"
                        CYAN_BOLD("%7d") " |" CYAN_BOLD("%3d") " |" CYAN_BOLD("%4d") " |" CYAN("%7zu") " | " GREEN("%s"),
                        i,
@@ -237,7 +252,7 @@ public:
                        data.thread.blocks(),
                        data.thread.bfactor(),
                        data.thread.bsleep(),
-                       (data.thread.threads() * data.thread.blocks()) * algo_l3 / oneMiB,
+                       mem_used,
                        data.device.name().data()
                        );
 
@@ -368,18 +383,30 @@ void xmrig::CudaBackend::printHashrate(bool details)
         return;
     }
 
-    char num[8 * 3] = { 0 };
+    char num[16 * 3] = { 0 };
 
-    Log::print(WHITE_BOLD_S "|   CUDA # | AFFINITY | 10s H/s | 60s H/s | 15m H/s |");
+    const double hashrate_short  = hashrate()->calc(Hashrate::ShortInterval);
+    const double hashrate_medium = hashrate()->calc(Hashrate::MediumInterval);
+    const double hashrate_large  = hashrate()->calc(Hashrate::LargeInterval);
+
+    double scale = 1.0;
+    const char* h = " H/s";
+
+    if ((hashrate_short >= 1e6) || (hashrate_medium >= 1e6) || (hashrate_large >= 1e6)) {
+        scale = 1e-6;
+        h = "MH/s";
+    }
+
+    Log::print(WHITE_BOLD_S "|   CUDA # | AFFINITY | 10s %s | 60s %s | 15m %s |", h, h, h);
 
     size_t i = 0;
-    for (const auto &data : d_ptr->threads) {
-         Log::print("| %8zu | %8" PRId64 " | %7s | %7s | %7s |" CYAN_BOLD(" #%u") YELLOW(" %s") GREEN(" %s"),
+    for (const auto& data : d_ptr->threads) {
+         Log::print("| %8zu | %8" PRId64 " | %8s | %8s | %8s |" CYAN_BOLD(" #%u") YELLOW(" %s") GREEN(" %s"),
                     i,
                     data.thread.affinity(),
-                    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),
+                    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()
@@ -388,10 +415,10 @@ void xmrig::CudaBackend::printHashrate(bool details)
          i++;
     }
 
-    Log::print(WHITE_BOLD_S "|        - |        - | %7s | %7s | %7s |",
-               Hashrate::format(hashrate()->calc(Hashrate::ShortInterval),  num,         sizeof num / 3),
-               Hashrate::format(hashrate()->calc(Hashrate::MediumInterval), num + 8,     sizeof num / 3),
-               Hashrate::format(hashrate()->calc(Hashrate::LargeInterval),  num + 8 * 2, sizeof num / 3)
+    Log::print(WHITE_BOLD_S "|        - |        - | %8s | %8s | %8s |",
+               Hashrate::format(hashrate()->calc(Hashrate::ShortInterval)  * scale, num,          sizeof num / 3),
+               Hashrate::format(hashrate()->calc(Hashrate::MediumInterval) * scale, num + 16,     sizeof num / 3),
+               Hashrate::format(hashrate()->calc(Hashrate::LargeInterval)  * scale, num + 16 * 2, sizeof num / 3)
                );
 }
 
diff --git a/src/backend/opencl/OclBackend.cpp b/src/backend/opencl/OclBackend.cpp
index 9a11ac5d5..049ab95fb 100644
--- a/src/backend/opencl/OclBackend.cpp
+++ b/src/backend/opencl/OclBackend.cpp
@@ -49,6 +49,12 @@
 #include "core/Controller.h"
 
 
+#ifdef XMRIG_ALGO_KAWPOW
+#   include "crypto/kawpow/KPCache.h"
+#   include "crypto/kawpow/KPHash.h"
+#endif
+
+
 #ifdef XMRIG_FEATURE_API
 #   include "base/api/interfaces/IApiRequest.h"
 #endif
@@ -213,6 +219,15 @@ public:
 
         size_t i = 0;
         for (const auto &data : threads) {
+            size_t mem_used = data.thread.intensity() * algo_l3 / oneMiB;
+
+#           ifdef XMRIG_ALGO_KAWPOW
+            if (algo.family() == Algorithm::KAWPOW) {
+                const uint32_t epoch = job.height() / KPHash::EPOCH_LENGTH;
+                mem_used = (KPCache::cache_size(epoch) + KPCache::dag_size(epoch)) / oneMiB;
+            }
+#           endif
+
             Log::print("|" CYAN_BOLD("%3zu") " |" CYAN_BOLD("%4u") " |" YELLOW(" %7s") " |" CYAN_BOLD("%10u") " |" CYAN_BOLD("%6u") " |"
                        CYAN("%7zu") " | %s",
                        i,
@@ -220,7 +235,7 @@ public:
                        data.device.topology().toString().data(),
                        data.thread.intensity(),
                        data.thread.worksize(),
-                       data.thread.intensity() * algo_l3 / oneMiB,
+                       mem_used,
                        data.device.printableName().data()
                        );
 
@@ -346,18 +361,30 @@ void xmrig::OclBackend::printHashrate(bool details)
         return;
     }
 
-    char num[8 * 3] = { 0 };
+    char num[16 * 3] = { 0 };
 
-    Log::print(WHITE_BOLD_S "| OPENCL # | AFFINITY | 10s H/s | 60s H/s | 15m H/s |");
+    const double hashrate_short  = hashrate()->calc(Hashrate::ShortInterval);
+    const double hashrate_medium = hashrate()->calc(Hashrate::MediumInterval);
+    const double hashrate_large  = hashrate()->calc(Hashrate::LargeInterval);
+
+    double scale = 1.0;
+    const char* h = " H/s";
+
+    if ((hashrate_short >= 1e6) || (hashrate_medium >= 1e6) || (hashrate_large >= 1e6)) {
+        scale = 1e-6;
+        h = "MH/s";
+    }
+
+    Log::print(WHITE_BOLD_S "| OPENCL # | AFFINITY | 10s %s | 60s %s | 15m %s |", h, h, h);
 
     size_t i = 0;
-    for (const auto &data : d_ptr->threads) {
-         Log::print("| %8zu | %8" PRId64 " | %7s | %7s | %7s |" CYAN_BOLD(" #%u") YELLOW(" %s") " %s",
+    for (const auto& data : d_ptr->threads) {
+         Log::print("| %8zu | %8" PRId64 " | %8s | %8s | %8s |" CYAN_BOLD(" #%u") YELLOW(" %s") " %s",
                     i,
                     data.affinity,
-                    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),
+                    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()
@@ -366,10 +393,10 @@ void xmrig::OclBackend::printHashrate(bool details)
          i++;
     }
 
-    Log::print(WHITE_BOLD_S "|        - |        - | %7s | %7s | %7s |",
-               Hashrate::format(hashrate()->calc(Hashrate::ShortInterval),  num,         sizeof num / 3),
-               Hashrate::format(hashrate()->calc(Hashrate::MediumInterval), num + 8,     sizeof num / 3),
-               Hashrate::format(hashrate()->calc(Hashrate::LargeInterval),  num + 8 * 2, sizeof num / 3)
+    Log::print(WHITE_BOLD_S "|        - |        - | %8s | %8s | %8s |",
+               Hashrate::format(hashrate()->calc(Hashrate::ShortInterval)  * scale, num,          sizeof num / 3),
+               Hashrate::format(hashrate()->calc(Hashrate::MediumInterval) * scale, num + 16,     sizeof num / 3),
+               Hashrate::format(hashrate()->calc(Hashrate::LargeInterval)  * scale, num + 16 * 2, sizeof num / 3)
                );
 }