Added results statistics to API.

This commit is contained in:
XMRig 2017-09-01 08:02:56 +03:00
parent 1651b041de
commit 9e9cddedc5
21 changed files with 265 additions and 57 deletions

View file

@ -12,6 +12,7 @@ set(HEADERS
src/3rdparty/align.h
src/api/Api.h
src/api/ApiState.h
src/api/Results.h
src/App.h
src/Console.h
src/Cpu.h
@ -64,6 +65,7 @@ set(HEADERS_CRYPTO
set(SOURCES
src/api/Api.cpp
src/api/ApiState.cpp
src/api/Results.cpp
src/App.cpp
src/Console.cpp
src/log/ConsoleLog.cpp
@ -76,6 +78,7 @@ set(SOURCES
src/net/strategies/DonateStrategy.cpp
src/net/strategies/FailoverStrategy.cpp
src/net/strategies/SinglePoolStrategy.cpp
src/net/SubmitResult.cpp
src/net/Url.cpp
src/Options.cpp
src/Platform.cpp

View file

@ -79,3 +79,15 @@ void Api::tick(const Hashrate *hashrate)
m_state->tick(hashrate);
uv_mutex_unlock(&m_mutex);
}
void Api::tick(const Results &results)
{
if (!m_state) {
return;
}
uv_mutex_lock(&m_mutex);
m_state->tick(results);
uv_mutex_unlock(&m_mutex);
}

View file

@ -30,6 +30,7 @@
class ApiState;
class Hashrate;
class Results;
class Api
@ -40,6 +41,7 @@ public:
static const char *get(const char *url, size_t *size, int *status);
static void tick(const Hashrate *hashrate);
static void tick(const Results &results);
private:
static ApiState *m_state;

View file

@ -48,13 +48,13 @@ extern "C"
}
static inline double normalizeHs(double hashrate)
static inline double normalize(double d)
{
if (!std::isnormal(hashrate)) {
if (!std::isnormal(d)) {
return 0.0;
}
return std::floor(hashrate * 10.0) / 10.0;
return std::floor(d * 10.0) / 10.0;
}
@ -90,6 +90,8 @@ const char *ApiState::get(const char *url, size_t *size) const
getIdentify(reply);
getMiner(reply);
getHashrate(reply);
getResults(reply);
getConnection(reply);
return finalize(reply, size);
}
@ -98,15 +100,21 @@ const char *ApiState::get(const char *url, size_t *size) const
void ApiState::tick(const Hashrate *hashrate)
{
for (int i = 0; i < m_threads; ++i) {
m_hashrate[i * 3] = normalizeHs(hashrate->calc((size_t) i, Hashrate::ShortInterval));
m_hashrate[i * 3 + 1] = normalizeHs(hashrate->calc((size_t) i, Hashrate::MediumInterval));
m_hashrate[i * 3 + 2] = normalizeHs(hashrate->calc((size_t) i, Hashrate::LargeInterval));
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] = normalizeHs(hashrate->calc(Hashrate::ShortInterval));
m_totalHashrate[1] = normalizeHs(hashrate->calc(Hashrate::MediumInterval));
m_totalHashrate[2] = normalizeHs(hashrate->calc(Hashrate::LargeInterval));
m_highestHashrate = normalizeHs(hashrate->highest());
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 ApiState::tick(const Results &results)
{
m_results = results;
}
@ -144,6 +152,14 @@ void ApiState::genId()
}
void ApiState::getConnection(json_t *reply) const
{
json_t *connection = json_object();
json_object_set(reply, "connection", connection);
}
void ApiState::getHashrate(json_t *reply) const
{
json_t *hashrate = json_object();
@ -152,20 +168,20 @@ void ApiState::getHashrate(json_t *reply) const
json_object_set(reply, "hashrate", hashrate);
json_object_set(hashrate, "total", total);
json_object_set(hashrate, "highest", json_real(m_highestHashrate));
json_object_set(hashrate, "highest", json_real(normalize(m_highestHashrate)));
json_object_set(hashrate, "threads", threads);
for (int i = 0; i < m_threads * 3; i += 3) {
json_t *thread = json_array();
json_array_append(thread, json_real(m_hashrate[i]));
json_array_append(thread, json_real(m_hashrate[i + 1]));
json_array_append(thread, json_real(m_hashrate[i + 2]));
json_array_append(thread, json_real(normalize(m_hashrate[i])));
json_array_append(thread, json_real(normalize(m_hashrate[i + 1])));
json_array_append(thread, json_real(normalize(m_hashrate[i + 2])));
json_array_append(threads, thread);
}
for (int i = 0; i < 3; ++i) {
json_array_append(total, json_real(m_totalHashrate[i]));
json_array_append(total, json_real(normalize(m_totalHashrate[i])));
}
}
@ -193,3 +209,21 @@ void ApiState::getMiner(json_t *reply) const
json_object_set(cpu, "x64", json_boolean(Cpu::isX64()));
json_object_set(cpu, "sockets", json_integer(Cpu::sockets()));
}
void ApiState::getResults(json_t *reply) const
{
json_t *results = json_object();
json_t *best = json_array();
json_object_set(reply, "results", results);
json_object_set(results, "diff_current", json_integer(m_results.diff));
json_object_set(results, "shares_good", json_integer(m_results.accepted));
json_object_set(results, "shares_total", json_integer(m_results.accepted + m_results.rejected));
json_object_set(results, "hashes_total", json_integer(m_results.total));
json_object_set(results, "best", best);
for (size_t i = 0; i < m_results.topDiff.size(); ++i) {
json_array_append(best, json_integer(m_results.topDiff[i]));
}
}

View file

@ -25,6 +25,7 @@
#define __APISTATE_H__
#include "api/Results.h"
#include "jansson.h"
@ -39,13 +40,16 @@ public:
const char *get(const char *url, size_t *size) const;
void tick(const Hashrate *hashrate);
void tick(const Results &results);
private:
const char *finalize(json_t *reply, size_t *size) const;
void genId();
void getConnection(json_t *reply) const;
void getHashrate(json_t *reply) const;
void getIdentify(json_t *reply) const;
void getMiner(json_t *reply) const;
void getResults(json_t *reply) const;
char m_id[17];
char m_workerId[128];
@ -54,6 +58,7 @@ private:
double m_totalHashrate[3];
int m_threads;
mutable char m_buf[4096];
Results m_results;
};
#endif /* __APISTATE_H__ */

47
src/api/Results.cpp Normal file
View file

@ -0,0 +1,47 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com>
*
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include "api/Results.h"
#include "net/SubmitResult.h"
void Results::add(const SubmitResult &result, const char *error)
{
if (error) {
rejected++;
return;
}
accepted++;
total += result.diff;
const size_t ln = topDiff.size() - 1;
if (result.actualDiff > topDiff[ln]) {
topDiff[ln] = result.actualDiff;
std::sort(topDiff.rbegin(), topDiff.rend());
}
}

54
src/api/Results.h Normal file
View file

@ -0,0 +1,54 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com>
*
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef __RESULTS_H__
#define __RESULTS_H__
#include <stdint.h>
#include <array>
class SubmitResult;
class Results
{
public:
inline Results() :
diff(0),
accepted(0),
rejected(0),
total(0)
{}
void add(const SubmitResult &result, const char *error);
std::array<uint64_t, 10> topDiff { { } };
uint32_t diff;
uint64_t accepted;
uint64_t rejected;
uint64_t total;
};
#endif /* __RESULTS_H__ */

View file

@ -30,6 +30,7 @@
class Client;
class Job;
class SubmitResult;
class IClientListener
@ -37,10 +38,10 @@ class IClientListener
public:
virtual ~IClientListener() {}
virtual void onClose(Client *client, int failures) = 0;
virtual void onJobReceived(Client *client, const Job &job) = 0;
virtual void onLoginSuccess(Client *client) = 0;
virtual void onResultAccepted(Client *client, int64_t seq, uint32_t diff, uint64_t ms, const char *error) = 0;
virtual void onClose(Client *client, int failures) = 0;
virtual void onJobReceived(Client *client, const Job &job) = 0;
virtual void onLoginSuccess(Client *client) = 0;
virtual void onResultAccepted(Client *client, const SubmitResult &result, const char *error) = 0;
};

View file

@ -31,6 +31,7 @@
class Client;
class IStrategy;
class Job;
class SubmitResult;
class IStrategyListener
@ -38,10 +39,10 @@ class IStrategyListener
public:
virtual ~IStrategyListener() {}
virtual void onActive(Client *client) = 0;
virtual void onJob(Client *client, const Job &job) = 0;
virtual void onPause(IStrategy *strategy) = 0;
virtual void onResultAccepted(Client *client, int64_t seq, uint32_t diff, uint64_t ms, const char *error) = 0;
virtual void onActive(Client *client) = 0;
virtual void onJob(Client *client, const Job &job) = 0;
virtual void onPause(IStrategy *strategy) = 0;
virtual void onResultAccepted(Client *client, const SubmitResult &result, const char *error) = 0;
};

View file

@ -194,7 +194,7 @@ int64_t Client::submit(const JobResult &result)
snprintf(req, 345, "{\"id\":%" PRIu64 ",\"jsonrpc\":\"2.0\",\"method\":\"submit\",\"params\":{\"id\":\"%s\",\"job_id\":\"%s\",\"nonce\":\"%s\",\"result\":\"%s\"}}\n",
m_sequence, m_rpcId, result.jobId, nonce, data);
m_results[m_sequence] = SubmitResult(m_sequence, result.diff);
m_results[m_sequence] = SubmitResult(m_sequence, result.diff, result.actualDiff());
return send(req);
}
@ -420,7 +420,8 @@ void Client::parseResponse(int64_t id, const json_t *result, const json_t *error
auto it = m_results.find(id);
if (it != m_results.end()) {
m_listener->onResultAccepted(this, it->second.seq, it->second.diff, it->second.elapsed(), message);
it->second.done();
m_listener->onResultAccepted(this, it->second, message);
m_results.erase(it);
}
else if (!m_quiet) {
@ -456,7 +457,8 @@ void Client::parseResponse(int64_t id, const json_t *result, const json_t *error
auto it = m_results.find(id);
if (it != m_results.end()) {
m_listener->onResultAccepted(this, it->second.seq, it->second.diff, it->second.elapsed(), nullptr);
it->second.done();
m_listener->onResultAccepted(this, it->second, nullptr);
m_results.erase(it);
}
}

View file

@ -61,6 +61,12 @@ public:
}
inline uint64_t actualDiff() const
{
return Job::toDiff(reinterpret_cast<const uint64_t*>(result)[3]);
}
char jobId[64];
int poolId;
uint32_t diff;

View file

@ -30,12 +30,14 @@
#include <time.h>
#include "api/Api.h"
#include "log/Log.h"
#include "net/Client.h"
#include "net/Network.h"
#include "net/strategies/DonateStrategy.h"
#include "net/strategies/FailoverStrategy.h"
#include "net/strategies/SinglePoolStrategy.h"
#include "net/SubmitResult.h"
#include "net/Url.h"
#include "Options.h"
#include "Platform.h"
@ -44,9 +46,7 @@
Network::Network(const Options *options) :
m_options(options),
m_donate(nullptr),
m_accepted(0),
m_rejected(0)
m_donate(nullptr)
{
srand(time(0) ^ (uintptr_t) this);
@ -139,21 +139,19 @@ void Network::onPause(IStrategy *strategy)
}
void Network::onResultAccepted(Client *client, int64_t seq, uint32_t diff, uint64_t ms, const char *error)
void Network::onResultAccepted(Client *client, const SubmitResult &result, const char *error)
{
if (error) {
m_rejected++;
m_results.add(result, error);
if (error) {
LOG_INFO(m_options->colors() ? "\x1B[01;31mrejected\x1B[0m (%" PRId64 "/%" PRId64 ") diff \x1B[01;37m%u\x1B[0m \x1B[31m\"%s\"\x1B[0m \x1B[01;30m(%" PRIu64 " ms)"
: "rejected (%" PRId64 "/%" PRId64 ") diff %u \"%s\" (%" PRIu64 " ms)",
m_accepted, m_rejected, diff, error, ms);
m_results.accepted, m_results.rejected, result.diff, error, result.elapsed);
}
else {
m_accepted++;
LOG_INFO(m_options->colors() ? "\x1B[01;32maccepted\x1B[0m (%" PRId64 "/%" PRId64 ") diff \x1B[01;37m%u\x1B[0m \x1B[01;30m(%" PRIu64 " ms)"
: "accepted (%" PRId64 "/%" PRId64 ") diff %u (%" PRIu64 " ms)",
m_accepted, m_rejected, diff, ms);
m_results.accepted, m_results.rejected, result.diff, result.elapsed);
}
}
@ -162,12 +160,12 @@ void Network::setJob(Client *client, const Job &job)
{
if (m_options->colors()) {
LOG_INFO("\x1B[01;35mnew job\x1B[0m from \x1B[01;37m%s:%d\x1B[0m diff \x1B[01;37m%d", client->host(), client->port(), job.diff());
}
else {
LOG_INFO("new job from %s:%d diff %d", client->host(), client->port(), job.diff());
}
m_results.diff = job.diff();
Workers::setJob(job);
}
@ -181,6 +179,8 @@ void Network::tick()
if (m_donate) {
m_donate->tick(now);
}
Api::tick(m_results);
}

View file

@ -29,6 +29,7 @@
#include <uv.h>
#include "api/Results.h"
#include "interfaces/IJobResultListener.h"
#include "interfaces/IStrategyListener.h"
@ -52,7 +53,7 @@ protected:
void onJob(Client *client, const Job &job) override;
void onJobResult(const JobResult &result) override;
void onPause(IStrategy *strategy) override;
void onResultAccepted(Client *client, int64_t seq, uint32_t diff, uint64_t ms, const char *error) override;
void onResultAccepted(Client *client, const SubmitResult &result, const char *error) override;
private:
constexpr static int kTickInterval = 1 * 1000;
@ -65,8 +66,7 @@ private:
const Options *m_options;
IStrategy *m_donate;
IStrategy *m_strategy;
uint64_t m_accepted;
uint64_t m_rejected;
Results m_results;
uv_timer_t m_timer;
};

44
src/net/SubmitResult.cpp Normal file
View file

@ -0,0 +1,44 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com>
*
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <uv.h>
#include "net/SubmitResult.h"
SubmitResult::SubmitResult(int64_t seq, uint32_t diff, uint64_t actualDiff) :
seq(seq),
diff(diff),
actualDiff(actualDiff),
elapsed(0)
{
start = uv_hrtime();
}
void SubmitResult::done()
{
elapsed = (uv_hrtime() - start) / 1000000;
}

View file

@ -31,18 +31,15 @@
class SubmitResult
{
public:
inline SubmitResult() : seq(0), diff(0), start(0) {}
inline SubmitResult(int64_t seq, uint32_t diff) :
seq(seq),
diff(diff)
{
start = uv_hrtime();
}
inline SubmitResult() : seq(0), diff(0), actualDiff(0), elapsed(0), start(0) {}
SubmitResult(int64_t seq, uint32_t diff, uint64_t actualDiff);
inline uint64_t elapsed() const { return (uv_hrtime() - start) / 1000000; }
void done();
int64_t seq;
uint32_t diff;
uint64_t actualDiff;
uint64_t elapsed;
uint64_t start;
};

View file

@ -111,9 +111,9 @@ void DonateStrategy::onLoginSuccess(Client *client)
}
void DonateStrategy::onResultAccepted(Client *client, int64_t seq, uint32_t diff, uint64_t ms, const char *error)
void DonateStrategy::onResultAccepted(Client *client, const SubmitResult &result, const char *error)
{
m_listener->onResultAccepted(client, seq, diff, ms, error);
m_listener->onResultAccepted(client, result, error);
}

View file

@ -55,7 +55,7 @@ protected:
void onClose(Client *client, int failures) override;
void onJobReceived(Client *client, const Job &job) override;
void onLoginSuccess(Client *client) override;
void onResultAccepted(Client *client, int64_t seq, uint32_t diff, uint64_t ms, const char *error) override;
void onResultAccepted(Client *client, const SubmitResult &result, const char *error) override;
private:
void idle();

View file

@ -132,9 +132,9 @@ void FailoverStrategy::onLoginSuccess(Client *client)
}
void FailoverStrategy::onResultAccepted(Client *client, int64_t seq, uint32_t diff, uint64_t ms, const char *error)
void FailoverStrategy::onResultAccepted(Client *client, const SubmitResult &result, const char *error)
{
m_listener->onResultAccepted(client, seq, diff, ms, error);
m_listener->onResultAccepted(client, result, error);
}

View file

@ -55,7 +55,7 @@ protected:
void onClose(Client *client, int failures) override;
void onJobReceived(Client *client, const Job &job) override;
void onLoginSuccess(Client *client) override;
void onResultAccepted(Client *client, int64_t seq, uint32_t diff, uint64_t ms, const char *error) override;
void onResultAccepted(Client *client, const SubmitResult &result, const char *error) override;
private:
void add(const Url *url, const char *agent);

View file

@ -96,7 +96,7 @@ void SinglePoolStrategy::onLoginSuccess(Client *client)
}
void SinglePoolStrategy::onResultAccepted(Client *client, int64_t seq, uint32_t diff, uint64_t ms, const char *error)
void SinglePoolStrategy::onResultAccepted(Client *client, const SubmitResult &result, const char *error)
{
m_listener->onResultAccepted(client, seq, diff, ms, error);
m_listener->onResultAccepted(client, result, error);
}

View file

@ -52,7 +52,7 @@ protected:
void onClose(Client *client, int failures) override;
void onJobReceived(Client *client, const Job &job) override;
void onLoginSuccess(Client *client) override;
void onResultAccepted(Client *client, int64_t seq, uint32_t diff, uint64_t ms, const char *error) override;
void onResultAccepted(Client *client, const SubmitResult &result, const char *error) override;
private:
bool m_active;