xmrig/src/workers/Workers.cpp

166 lines
4.1 KiB
C++
Raw Normal View History

2017-06-10 06:41:08 +00: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 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/>.
*/
2017-06-12 04:18:14 +00:00
#include <cmath>
#include "Console.h"
2017-06-11 12:32:15 +00:00
#include "interfaces/IJobResultListener.h"
2017-06-13 16:58:31 +00:00
#include "Mem.h"
#include "workers/DoubleWorker.h"
2017-06-10 06:41:08 +00:00
#include "workers/Handle.h"
#include "workers/SingleWorker.h"
2017-06-12 04:18:14 +00:00
#include "workers/Telemetry.h"
2017-06-10 06:41:08 +00:00
#include "workers/Workers.h"
2017-06-11 03:52:23 +00:00
2017-06-11 12:32:15 +00:00
IJobResultListener *Workers::m_listener = nullptr;
2017-06-11 03:52:23 +00:00
Job Workers::m_job;
std::atomic<int> Workers::m_paused;
std::atomic<uint64_t> Workers::m_sequence;
2017-06-11 07:58:46 +00:00
std::list<JobResult> Workers::m_queue;
2017-06-10 06:41:08 +00:00
std::vector<Handle*> Workers::m_workers;
2017-06-12 04:18:14 +00:00
Telemetry *Workers::m_telemetry = nullptr;
uint64_t Workers::m_ticks = 0;
2017-06-10 06:41:08 +00:00
uv_async_t Workers::m_async;
2017-06-12 13:19:07 +00:00
uv_mutex_t Workers::m_mutex;
uv_rwlock_t Workers::m_rwlock;
2017-06-12 04:18:14 +00:00
uv_timer_t Workers::m_timer;
2017-06-10 06:41:08 +00:00
2017-06-11 03:52:23 +00:00
Job Workers::job()
{
2017-06-12 13:19:07 +00:00
uv_rwlock_rdlock(&m_rwlock);
2017-06-11 03:52:23 +00:00
Job job = m_job;
2017-06-12 13:19:07 +00:00
uv_rwlock_rdunlock(&m_rwlock);
2017-06-11 03:52:23 +00:00
return std::move(job);
}
void Workers::setJob(const Job &job)
{
2017-06-12 13:19:07 +00:00
uv_rwlock_wrlock(&m_rwlock);
2017-06-11 03:52:23 +00:00
m_job = job;
2017-06-12 13:19:07 +00:00
uv_rwlock_wrunlock(&m_rwlock);
2017-06-11 03:52:23 +00:00
m_sequence++;
m_paused = 0;
}
2017-06-13 16:58:31 +00:00
void Workers::start(int64_t affinity, bool nicehash)
2017-06-10 06:41:08 +00:00
{
2017-06-13 16:58:31 +00:00
const int threads = Mem::threads();
2017-06-12 04:18:14 +00:00
m_telemetry = new Telemetry(threads);
2017-06-12 13:19:07 +00:00
uv_mutex_init(&m_mutex);
uv_rwlock_init(&m_rwlock);
2017-06-10 06:41:08 +00:00
2017-06-11 03:52:23 +00:00
m_sequence = 0;
m_paused = 1;
2017-06-10 06:41:08 +00:00
uv_async_init(uv_default_loop(), &m_async, Workers::onResult);
2017-06-12 04:18:14 +00:00
uv_timer_init(uv_default_loop(), &m_timer);
2017-06-12 13:19:07 +00:00
uv_timer_start(&m_timer, Workers::onTick, 500, 500);
2017-06-10 06:41:08 +00:00
for (int i = 0; i < threads; ++i) {
2017-06-11 07:58:46 +00:00
Handle *handle = new Handle(i, threads, affinity, nicehash);
2017-06-10 06:41:08 +00:00
m_workers.push_back(handle);
handle->start(Workers::onReady);
}
}
2017-06-11 07:58:46 +00:00
void Workers::submit(const JobResult &result)
2017-06-10 06:41:08 +00:00
{
2017-06-12 13:19:07 +00:00
uv_mutex_lock(&m_mutex);
2017-06-11 07:58:46 +00:00
m_queue.push_back(result);
2017-06-12 13:19:07 +00:00
uv_mutex_unlock(&m_mutex);
2017-06-11 07:58:46 +00:00
2017-06-10 06:41:08 +00:00
uv_async_send(&m_async);
}
2017-06-12 13:19:07 +00:00
void Workers::onReady(void *arg)
2017-06-10 06:41:08 +00:00
{
auto handle = static_cast<Handle*>(arg);
2017-06-13 16:58:31 +00:00
if (Mem::isDoubleHash()) {
handle->setWorker(new DoubleWorker(handle));
}
else {
handle->setWorker(new SingleWorker(handle));
}
handle->worker()->start();
2017-06-12 13:19:07 +00:00
}
2017-06-10 06:41:08 +00:00
2017-06-12 13:19:07 +00:00
void Workers::onResult(uv_async_t *handle)
{
std::list<JobResult> results;
uv_mutex_lock(&m_mutex);
while (!m_queue.empty()) {
results.push_back(std::move(m_queue.front()));
m_queue.pop_front();
}
uv_mutex_unlock(&m_mutex);
for (auto result : results) {
m_listener->onJobResult(result);
}
results.clear();
2017-06-10 06:41:08 +00:00
}
2017-06-12 13:19:07 +00:00
void Workers::onTick(uv_timer_t *handle)
2017-06-12 04:18:14 +00:00
{
for (Handle *handle : m_workers) {
m_telemetry->add(handle->threadId(), handle->worker()->hashCount(), handle->worker()->timestamp());
}
if ((m_ticks++ & 0xF) == 0) {
double hps = 0.0;
double telem;
bool normal = true;
for (Handle *handle : m_workers) {
telem = m_telemetry->calc(handle->threadId(), 2500);
if (!std::isnormal(telem)) {
normal = false;
break;
}
else {
hps += telem;
}
}
if (normal) {
LOG_NOTICE("%03.1f H/s", hps);
}
}
}