diff --git a/src/main.cpp b/src/main.cpp index 26d981a..f463aa0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -36,7 +36,8 @@ static void usage() "--loglevel Verbosity of the log, integer number between 0 and %d\n" "--config Name of the p2pool config file\n" "--data-api Path to the p2pool JSON data (use it in tandem with an external web-server)\n" - "--stratum-api Enable /local/ path in api path for Stratum Server statistics\n" + "--local-api Enable /local/ path in api path for Stratum Server and built-in miner statistics\n" + "--stratum-api An alias for --local-api\n" "--no-cache Disable p2pool.cache\n" "--no-color Disable colors in console output\n" "--no-randomx Disable internal RandomX hasher: p2pool will use RPC calls to monerod to check PoW hashes\n" diff --git a/src/miner.cpp b/src/miner.cpp index 1dab226..6416e61 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -22,6 +22,8 @@ #include "block_template.h" #include "pow_hash.h" #include "randomx.h" +#include "params.h" +#include "p2pool_api.h" #include static constexpr char log_category_prefix[] = "Miner "; @@ -34,9 +36,11 @@ Miner::Miner(p2pool* pool, uint32_t threads) : m_pool(pool) , m_threads(threads) , m_stopped(false) + , m_startTimestamp(high_resolution_clock::now()) , m_nonce(0) - , m_nonceTimestamp(high_resolution_clock::now()) + , m_nonceTimestamp(m_startTimestamp) , m_extraNonce(0xF19E3779U) + , m_totalHashes(0) , m_job{} , m_jobIndex(0) { @@ -85,9 +89,31 @@ void Miner::on_block(const BlockTemplate& block) Job& j = m_job[next_index]; hash seed; j.m_blobSize = block.get_hashing_blob(m_extraNonce, j.m_blob, j.m_height, j.m_diff, j.m_sidechainDiff, seed, j.m_nonceOffset, j.m_templateId); - m_nonce.exchange(0); + + const uint32_t hash_count = 0 - m_nonce.exchange(0); m_jobIndex = next_index; - m_nonceTimestamp = high_resolution_clock::now(); + + const auto cur_ts = high_resolution_clock::now(); + const double dt = static_cast(duration_cast(cur_ts - m_nonceTimestamp).count()) / 1e9; + + m_nonceTimestamp = cur_ts; + m_totalHashes += hash_count; + + if (m_pool->api() && m_pool->params().m_localStats) { + m_pool->api()->set(p2pool_api::Category::LOCAL, "miner", + [cur_ts, hash_count, dt, this](log::Stream& s) + { + const uint64_t hr = (dt > 0.0) ? static_cast(hash_count / dt) : 0; + const double time_running = static_cast(duration_cast(cur_ts - m_startTimestamp).count()) / 1e3; + + s << "{\"current_hashrate\":" << hr + << ",\"total_hashes\":" << m_totalHashes + << ",\"time_running\":" << time_running + << ",\"shares_found\":" << m_sharesFound.load() + << ",\"threads\":" << m_threads + << "}"; + }); + } } void Miner::run(void* data) @@ -173,6 +199,7 @@ void Miner::run(WorkerData* data) if (j.m_sidechainDiff.check_pow(h)) { LOGINFO(0, log::Green() << "SHARE FOUND: mainchain height " << j.m_height << ", diff " << j.m_sidechainDiff << ", worker thread " << data->m_index << '/' << data->m_count); m_pool->submit_sidechain_block(j.m_templateId, j.m_nonce, j.m_extraNonce); + ++m_sharesFound; } std::this_thread::yield(); diff --git a/src/miner.h b/src/miner.h index 3317b00..fd418c5 100644 --- a/src/miner.h +++ b/src/miner.h @@ -51,10 +51,15 @@ private: std::vector m_minerThreads; volatile bool m_stopped; + std::chrono::time_point m_startTimestamp; + std::atomic m_nonce; std::chrono::time_point m_nonceTimestamp; const uint32_t m_extraNonce; + uint64_t m_totalHashes; + std::atomic m_sharesFound; + struct Job { uint8_t m_blob[128] = {}; diff --git a/src/params.cpp b/src/params.cpp index 15e9a3d..95cd28c 100644 --- a/src/params.cpp +++ b/src/params.cpp @@ -70,7 +70,7 @@ Params::Params(int argc, char* argv[]) m_apiPath = argv[++i]; } - if (strcmp(argv[i], "--stratum-api") == 0) { + if ((strcmp(argv[i], "--local-api") == 0) || (strcmp(argv[i], "--stratum-api") == 0)) { m_localStats = true; }