Files
xmrig/src/backend/common/Hashrate.cpp

178 lines
4.5 KiB
C++
Raw Normal View History

2017-06-12 07:18:14 +03:00
/* 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 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
2019-03-16 00:44:15 +07:00
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
2017-06-12 07:18:14 +03:00
*
* 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/>.
*/
2018-04-17 10:29:37 +07:00
#include <assert.h>
2019-07-19 05:03:14 +07:00
#include <cmath>
2017-06-16 10:19:14 +03:00
#include <memory.h>
2017-11-17 12:59:46 +03:00
#include <stdio.h>
2017-06-12 07:18:14 +03:00
2019-07-19 02:24:37 +07:00
#include "backend/common/Hashrate.h"
#include "base/tools/Chrono.h"
2019-03-16 00:44:15 +07:00
#include "base/tools/Handle.h"
2019-07-19 02:24:37 +07:00
#include "rapidjson/document.h"
2017-06-12 07:18:14 +03:00
inline static const char *format(double h, char *buf, size_t size)
2017-06-16 10:19:14 +03:00
{
2019-07-19 05:03:14 +07:00
if (std::isnormal(h)) {
2017-06-16 10:19:14 +03:00
snprintf(buf, size, "%03.1f", h);
return buf;
}
return "n/a";
}
2019-07-17 01:28:42 +07:00
xmrig::Hashrate::Hashrate(size_t threads) :
2017-06-16 10:19:14 +03:00
m_highest(0.0),
2019-07-17 01:28:42 +07:00
m_threads(threads)
2017-06-12 07:18:14 +03:00
{
m_counts = new uint64_t*[threads];
m_timestamps = new uint64_t*[threads];
m_top = new uint32_t[threads];
2018-04-17 10:29:37 +07:00
for (size_t i = 0; i < threads; i++) {
m_counts[i] = new uint64_t[kBucketSize]();
m_timestamps[i] = new uint64_t[kBucketSize]();
m_top[i] = 0;
2017-06-12 07:18:14 +03:00
}
2019-07-17 01:28:42 +07:00
}
2017-06-16 11:08:10 +03:00
2019-07-17 01:28:42 +07:00
xmrig::Hashrate::~Hashrate()
{
for (size_t i = 0; i < m_threads; i++) {
delete [] m_counts[i];
delete [] m_timestamps[i];
2017-06-16 11:08:10 +03:00
}
2019-07-17 01:28:42 +07:00
delete [] m_counts;
delete [] m_timestamps;
delete [] m_top;
2017-06-12 07:18:14 +03:00
}
2019-07-17 01:28:42 +07:00
double xmrig::Hashrate::calc(size_t ms) const
2017-06-15 21:00:25 +03:00
{
2017-06-16 10:19:14 +03:00
double result = 0.0;
2017-06-15 21:00:25 +03:00
double data;
2018-04-17 10:29:37 +07:00
for (size_t i = 0; i < m_threads; ++i) {
2017-06-15 21:00:25 +03:00
data = calc(i, ms);
2019-07-19 05:03:14 +07:00
if (std::isnormal(data)) {
2017-06-15 21:00:25 +03:00
result += data;
}
}
return result;
}
2019-07-17 01:28:42 +07:00
double xmrig::Hashrate::calc(size_t threadId, size_t ms) const
2017-06-12 07:18:14 +03:00
{
2018-04-17 10:29:37 +07:00
assert(threadId < m_threads);
if (threadId >= m_threads) {
return nan("");
}
2018-04-17 10:29:37 +07:00
2017-06-12 07:18:14 +03:00
uint64_t earliestHashCount = 0;
uint64_t earliestStamp = 0;
uint64_t lastestStamp = 0;
uint64_t lastestHashCnt = 0;
bool haveFullSet = false;
for (size_t i = 1; i < kBucketSize; i++) {
const size_t idx = (m_top[threadId] - i) & kBucketMask;
if (m_timestamps[threadId][idx] == 0) {
break;
}
if (lastestStamp == 0) {
lastestStamp = m_timestamps[threadId][idx];
lastestHashCnt = m_counts[threadId][idx];
}
if (xmrig::Chrono::highResolutionMSecs() - m_timestamps[threadId][idx] > ms) {
2017-06-12 07:18:14 +03:00
haveFullSet = true;
break;
}
earliestStamp = m_timestamps[threadId][idx];
earliestHashCount = m_counts[threadId][idx];
}
if (!haveFullSet || earliestStamp == 0 || lastestStamp == 0) {
return nan("");
}
if (lastestStamp - earliestStamp == 0) {
return nan("");
}
const double hashes = static_cast<double>(lastestHashCnt - earliestHashCount);
const double time = static_cast<double>(lastestStamp - earliestStamp) / 1000.0;
2017-06-12 07:18:14 +03:00
return hashes / time;
}
2019-07-17 01:28:42 +07:00
void xmrig::Hashrate::add(size_t threadId, uint64_t count, uint64_t timestamp)
2017-06-12 07:18:14 +03:00
{
const size_t top = m_top[threadId];
m_counts[threadId][top] = count;
m_timestamps[threadId][top] = timestamp;
m_top[threadId] = (top + 1) & kBucketMask;
}
2017-06-16 10:19:14 +03:00
2019-07-17 01:28:42 +07:00
void xmrig::Hashrate::updateHighest()
2017-06-16 10:19:14 +03:00
{
2017-09-01 03:45:08 +03:00
double highest = calc(ShortInterval);
2019-07-19 05:03:14 +07:00
if (std::isnormal(highest) && highest > m_highest) {
2017-06-16 10:19:14 +03:00
m_highest = highest;
}
}
2017-06-16 11:08:10 +03:00
2019-07-17 01:28:42 +07:00
const char *xmrig::Hashrate::format(double h, char *buf, size_t size)
{
return ::format(h, buf, size);
}
2019-07-19 02:24:37 +07:00
rapidjson::Value xmrig::Hashrate::normalize(double d)
{
using namespace rapidjson;
if (!std::isnormal(d)) {
return Value(kNullType);
}
return Value(floor(d * 100.0) / 100.0);
}