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

@ -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__ */