diff --git a/src/base/base.cmake b/src/base/base.cmake index 5761a09b5..db26e2e98 100644 --- a/src/base/base.cmake +++ b/src/base/base.cmake @@ -132,6 +132,7 @@ set(SOURCES_BASE src/base/net/tools/LineReader.cpp src/base/net/tools/NetBuffer.cpp src/base/tools/Arguments.cpp + src/base/tools/Chrono.cpp src/base/tools/cryptonote/BlockTemplate.cpp src/base/tools/cryptonote/crypto-ops-data.c src/base/tools/cryptonote/crypto-ops.c diff --git a/src/base/tools/Chrono.cpp b/src/base/tools/Chrono.cpp new file mode 100644 index 000000000..5697472cd --- /dev/null +++ b/src/base/tools/Chrono.cpp @@ -0,0 +1,44 @@ +/* XMRig + * Copyright (c) 2018-2021 SChernykh + * Copyright (c) 2016-2021 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 "Chrono.h" + + +#ifdef XMRIG_OS_WIN +# include +#endif + + +namespace xmrig { + + +double Chrono::highResolutionMSecs() +{ +# ifdef XMRIG_OS_WIN + LARGE_INTEGER f, t; + QueryPerformanceFrequency(&f); + QueryPerformanceCounter(&t); + return static_cast(t.QuadPart) * 1e3 / f.QuadPart; +# else + using namespace std::chrono; + return static_cast(duration_cast(high_resolution_clock::now().time_since_epoch()).count()) / 1e6; +# endif +} + + +} /* namespace xmrig */ diff --git a/src/base/tools/Chrono.h b/src/base/tools/Chrono.h index 78da18c15..65c3d5ae4 100644 --- a/src/base/tools/Chrono.h +++ b/src/base/tools/Chrono.h @@ -29,12 +29,7 @@ namespace xmrig { class Chrono { public: - static inline uint64_t highResolutionMSecs() - { - using namespace std::chrono; - - return static_cast(time_point_cast(high_resolution_clock::now()).time_since_epoch().count()); - } + static double highResolutionMSecs(); static inline uint64_t steadyMSecs() diff --git a/src/crypto/ghostrider/ghostrider.cpp b/src/crypto/ghostrider/ghostrider.cpp index 0a526dd41..9f403d3aa 100644 --- a/src/crypto/ghostrider/ghostrider.cpp +++ b/src/crypto/ghostrider/ghostrider.cpp @@ -36,6 +36,7 @@ #include "base/io/log/Log.h" #include "base/io/log/Tags.h" +#include "base/tools/Chrono.h" #include "backend/cpu/Cpu.h" #include "crypto/cn/CnHash.h" #include "crypto/cn/CnCtx.h" @@ -44,7 +45,6 @@ #include #include -#include #include #ifdef XMRIG_FEATURE_HWLOC @@ -61,10 +61,6 @@ # include #endif -#ifdef XMRIG_OS_WIN -# include -#endif - #define CORE_HASH(i, x) static void h##i(const uint8_t* data, size_t size, uint8_t* output) \ { \ sph_##x##_context ctx; \ @@ -332,17 +328,6 @@ void benchmark() LOG_VERBOSE("%24s | N | Hashrate", "Algorithm"); LOG_VERBOSE("-------------------------|-----|-------------"); -# ifdef XMRIG_OS_WIN - LARGE_INTEGER timer_freq; - QueryPerformanceFrequency(&timer_freq); - auto measure_time = []() { LARGE_INTEGER t; QueryPerformanceCounter(&t); return t.QuadPart; }; - auto delta_time = [&timer_freq](LONGLONG t1, LONGLONG t2) { return static_cast(t2 - t1) / timer_freq.QuadPart; }; -# else - using namespace std::chrono; - auto measure_time = []() { return high_resolution_clock::now(); }; - auto delta_time = [](const high_resolution_clock::time_point& t1, const high_resolution_clock::time_point& t2) { return duration_cast(t2 - t1).count() / 1e9; }; -# endif - for (uint32_t algo = 0; algo < 6; ++algo) { for (uint64_t step : { 1, 2, 4}) { const size_t cur_scratchpad_size = cn_sizes[algo] * step; @@ -352,26 +337,26 @@ void benchmark() auto f = CnHash::fn(cn_hash[algo], av[step], Assembly::AUTO); - auto start_time = measure_time(); + double start_time = Chrono::highResolutionMSecs(); double min_dt = 1e10; for (uint32_t iter = 0;; ++iter) { - auto t1 = measure_time(); + double t1 = Chrono::highResolutionMSecs(); // Stop after 15 milliseconds, but only if at least 10 iterations were done - if ((iter >= 10) && (delta_time(start_time, t1) >= 0.015)) { + if ((iter >= 10) && (t1 - start_time >= 15.0)) { break; } f(buf, sizeof(buf), hash, ctx, 0); - const double dt = delta_time(t1, measure_time()); + const double dt = Chrono::highResolutionMSecs() - t1; if (dt < min_dt) { min_dt = dt; } } - const double hashrate = step / min_dt; + const double hashrate = step * 1e3 / min_dt; LOG_VERBOSE("%24s | %" PRIu64 "x1 | %.2f h/s", cn_names[algo], step, hashrate); if (hashrate > tune8MB[algo].hashrate) { @@ -401,14 +386,14 @@ void benchmark() auto f = CnHash::fn(cn_hash[algo], av[step], Assembly::AUTO); - auto start_time = measure_time(); + double start_time = Chrono::highResolutionMSecs(); double min_dt = 1e10; for (uint32_t iter = 0;; ++iter) { - auto t1 = measure_time(); + double t1 = Chrono::highResolutionMSecs(); // Stop after 30 milliseconds, but only if at least 10 iterations were done - if ((iter >= 10) && (delta_time(start_time, t1) >= 0.03)) { + if ((iter >= 10) && (t1 - start_time >= 30.0)) { break; } @@ -416,13 +401,13 @@ void benchmark() f(buf, sizeof(buf), hash, ctx, 0); helper->wait(); - const double dt = delta_time(t1, measure_time()); + const double dt = Chrono::highResolutionMSecs() - t1; if (dt < min_dt) { min_dt = dt; } } - const double hashrate = step * 2.0 / min_dt * 1.0075; + const double hashrate = step * 2e3 / min_dt * 1.0075; LOG_VERBOSE("%24s | %" PRIu64 "x2 | %.2f h/s", cn_names[algo], step, hashrate); if (hashrate > tune8MB[algo].hashrate) { diff --git a/src/crypto/randomx/aes_hash.cpp b/src/crypto/randomx/aes_hash.cpp index 2383dab41..ee5989e1f 100644 --- a/src/crypto/randomx/aes_hash.cpp +++ b/src/crypto/randomx/aes_hash.cpp @@ -382,7 +382,7 @@ void SelectSoftAESImpl(size_t threadsCount) double fast_speed = 0.0; for (size_t run = 0; run < 3; ++run) { for (size_t i = 0; i < impl.size(); ++i) { - const uint64_t t1 = xmrig::Chrono::highResolutionMSecs(); + const double t1 = xmrig::Chrono::highResolutionMSecs(); std::vector count(threadsCount, 0); std::vector threads; for (size_t t = 0; t < threadsCount; ++t) { @@ -401,7 +401,7 @@ void SelectSoftAESImpl(size_t threadsCount) threads[t].join(); total += count[t]; } - const uint64_t t2 = xmrig::Chrono::highResolutionMSecs(); + const double t2 = xmrig::Chrono::highResolutionMSecs(); const double speed = total * 1e3 / (t2 - t1); if (speed > fast_speed) { fast_idx = i;