xmrig/src/workers/Workers.cpp

247 lines
5.4 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 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
2017-06-10 06:41:08 +00: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/>.
*/
2017-06-12 04:18:14 +00:00
#include <cmath>
#include <thread>
2017-06-12 04:18:14 +00:00
2017-09-01 00:45:08 +00:00
#include "api/Api.h"
#include "core/Config.h"
#include "core/Controller.h"
2017-06-11 12:32:15 +00:00
#include "interfaces/IJobResultListener.h"
#include "interfaces/IThread.h"
#include "log/Log.h"
2017-06-13 16:58:31 +00:00
#include "Mem.h"
2017-06-10 06:41:08 +00:00
#include "workers/Handle.h"
2017-06-15 18:00:25 +00:00
#include "workers/Hashrate.h"
#include "workers/MultiWorker.h"
2017-06-10 06:41:08 +00:00
#include "workers/Workers.h"
2017-06-11 03:52:23 +00:00
2017-07-10 18:42:28 +00:00
bool Workers::m_active = false;
bool Workers::m_enabled = true;
2017-06-15 18:00:25 +00:00
Hashrate *Workers::m_hashrate = nullptr;
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
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
2017-06-14 17:37:59 +00:00
return job;
2017-06-11 03:52:23 +00:00
}
2017-07-23 06:36:30 +00:00
void Workers::printHashrate(bool detail)
{
m_hashrate->print();
}
2017-07-10 18:42:28 +00:00
void Workers::setEnabled(bool enabled)
{
if (m_enabled == enabled) {
return;
}
m_enabled = enabled;
if (!m_active) {
return;
}
m_paused = enabled ? 0 : 1;
m_sequence++;
}
void Workers::setJob(const Job &job, bool donate)
2017-06-11 03:52:23 +00:00
{
2017-06-12 13:19:07 +00:00
uv_rwlock_wrlock(&m_rwlock);
2017-06-11 03:52:23 +00:00
m_job = job;
if (donate) {
m_job.setPoolId(-1);
}
2017-06-12 13:19:07 +00:00
uv_rwlock_wrunlock(&m_rwlock);
2017-06-11 03:52:23 +00:00
2017-07-10 18:42:28 +00:00
m_active = true;
if (!m_enabled) {
return;
}
2017-06-11 03:52:23 +00:00
m_sequence++;
m_paused = 0;
}
2018-04-02 20:27:44 +00:00
void Workers::start(xmrig::Controller *controller)
2017-06-10 06:41:08 +00:00
{
const std::vector<xmrig::IThread *> &threads = controller->config()->threads();
size_t totalWays = 0;
for (const xmrig::IThread *thread : threads) {
totalWays += thread->multiway();
}
m_hashrate = new Hashrate(threads.size(), controller);
2017-06-12 04:18:14 +00:00
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-07-19 01:28:59 +00:00
m_sequence = 1;
2017-06-11 03:52:23 +00:00
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
uint32_t offset = 0;
for (xmrig::IThread *thread : threads) {
Handle *handle = new Handle(thread, offset, totalWays);
offset += thread->multiway();
2017-06-10 06:41:08 +00:00
m_workers.push_back(handle);
handle->start(Workers::onReady);
}
}
2017-07-18 02:20:36 +00:00
void Workers::stop()
{
uv_timer_stop(&m_timer);
m_hashrate->stop();
uv_close(reinterpret_cast<uv_handle_t*>(&m_async), nullptr);
2017-07-23 06:36:30 +00:00
m_paused = 0;
2017-07-19 01:28:59 +00:00
m_sequence = 0;
for (size_t i = 0; i < m_workers.size(); ++i) {
m_workers[i]->join();
}
2017-07-18 02:20:36 +00:00
}
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);
IWorker *worker = nullptr;
switch (handle->config()->multiway()) {
case 1:
worker = new MultiWorker<1>(handle);
break;
case 2:
worker = new MultiWorker<2>(handle);
break;
case 3:
worker = new MultiWorker<3>(handle);
break;
case 4:
worker = new MultiWorker<4>(handle);
break;
case 5:
worker = new MultiWorker<5>(handle);
break;
default:
break;
2017-06-13 16:58:31 +00:00
}
handle->setWorker(worker);
if (!worker->selfTest()) {
LOG_ERR("thread %zu error: \"hash self-test failed\".", handle->worker()->id());
return;
}
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) {
2017-06-15 18:00:25 +00:00
if (!handle->worker()) {
return;
2017-06-14 17:37:59 +00:00
}
2017-06-15 18:00:25 +00:00
m_hashrate->add(handle->threadId(), handle->worker()->hashCount(), handle->worker()->timestamp());
2017-06-12 04:18:14 +00:00
}
if ((m_ticks++ & 0xF) == 0) {
2017-06-16 07:19:14 +00:00
m_hashrate->updateHighest();
2017-06-12 04:18:14 +00:00
}
2017-09-01 00:45:08 +00:00
2017-09-02 09:12:40 +00:00
# ifndef XMRIG_NO_API
2017-09-01 00:45:08 +00:00
Api::tick(m_hashrate);
2017-09-02 09:12:40 +00:00
# endif
2017-06-12 04:18:14 +00:00
}