2017-06-10 04:05:00 +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>
|
2018-03-06 10:06:07 +00:00
|
|
|
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
|
|
|
* Copyright 2018 Lee Clagett <https://github.com/vtnerd>
|
|
|
|
* Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
2017-06-10 04:05:00 +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-10 06:41:08 +00:00
|
|
|
#include <thread>
|
2017-06-10 04:05:00 +00:00
|
|
|
|
2017-06-10 06:41:08 +00:00
|
|
|
|
2018-04-03 07:51:05 +00:00
|
|
|
#include "crypto/CryptoNight_test.h"
|
2018-04-02 19:55:28 +00:00
|
|
|
#include "workers/CpuThread.h"
|
2018-04-14 15:14:57 +00:00
|
|
|
#include "workers/MultiWorker.h"
|
2017-06-10 06:41:08 +00:00
|
|
|
#include "workers/Workers.h"
|
2017-06-10 04:05:00 +00:00
|
|
|
|
|
|
|
|
2018-04-14 15:14:57 +00:00
|
|
|
template<size_t N>
|
|
|
|
MultiWorker<N>::MultiWorker(Handle *handle)
|
2017-06-10 04:05:00 +00:00
|
|
|
: Worker(handle)
|
|
|
|
{
|
2018-04-15 04:08:47 +00:00
|
|
|
m_memory = Mem::create(m_ctx, m_thread->algorithm(), N);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<size_t N>
|
|
|
|
MultiWorker<N>::~MultiWorker()
|
|
|
|
{
|
|
|
|
Mem::release(m_ctx, N, m_memory);
|
2017-06-10 04:05:00 +00:00
|
|
|
}
|
2017-06-10 06:41:08 +00:00
|
|
|
|
|
|
|
|
2018-04-14 15:14:57 +00:00
|
|
|
template<size_t N>
|
|
|
|
bool MultiWorker<N>::selfTest()
|
2017-06-10 06:41:08 +00:00
|
|
|
{
|
2018-07-09 09:54:39 +00:00
|
|
|
using namespace xmrig;
|
|
|
|
|
|
|
|
if (m_thread->algorithm() == CRYPTONIGHT) {
|
|
|
|
return verify(VARIANT_0, test_output_v0) &&
|
|
|
|
verify(VARIANT_1, test_output_v1) &&
|
|
|
|
verify(VARIANT_XTL, test_output_xtl) &&
|
|
|
|
verify(VARIANT_MSR, test_output_msr) &&
|
|
|
|
verify(VARIANT_XAO, test_output_xao) &&
|
|
|
|
verify(VARIANT_RTO, test_output_rto);
|
2018-04-14 15:14:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# ifndef XMRIG_NO_AEON
|
2018-07-09 09:54:39 +00:00
|
|
|
if (m_thread->algorithm() == CRYPTONIGHT_LITE) {
|
|
|
|
return verify(VARIANT_0, test_output_v0_lite) &&
|
|
|
|
verify(VARIANT_1, test_output_v1_lite);
|
2018-04-14 15:14:57 +00:00
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifndef XMRIG_NO_SUMO
|
2018-07-09 09:54:39 +00:00
|
|
|
if (m_thread->algorithm() == CRYPTONIGHT_HEAVY) {
|
|
|
|
return verify(VARIANT_0, test_output_v0_heavy) &&
|
|
|
|
verify(VARIANT_XHV, test_output_xhv_heavy);
|
2018-06-11 05:00:59 +00:00
|
|
|
}
|
2018-04-14 15:14:57 +00:00
|
|
|
# endif
|
2018-06-11 05:00:59 +00:00
|
|
|
|
|
|
|
return false;
|
2018-04-14 15:14:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<size_t N>
|
|
|
|
void MultiWorker<N>::start()
|
|
|
|
{
|
2017-07-19 01:28:59 +00:00
|
|
|
while (Workers::sequence() > 0) {
|
2017-06-11 03:52:23 +00:00
|
|
|
if (Workers::isPaused()) {
|
|
|
|
do {
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
|
|
|
}
|
|
|
|
while (Workers::isPaused());
|
|
|
|
|
2017-07-23 06:36:30 +00:00
|
|
|
if (Workers::sequence() == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-06-11 03:52:23 +00:00
|
|
|
consumeJob();
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!Workers::isOutdated(m_sequence)) {
|
2018-04-14 15:14:57 +00:00
|
|
|
if ((m_count & 0x7) == 0) {
|
2017-06-12 04:18:14 +00:00
|
|
|
storeStats();
|
|
|
|
}
|
|
|
|
|
2018-04-15 04:08:47 +00:00
|
|
|
m_thread->fn(m_state.job.variant())(m_state.blob, m_state.job.size(), m_hash, m_ctx);
|
2017-06-11 03:52:23 +00:00
|
|
|
|
2018-04-14 15:14:57 +00:00
|
|
|
for (size_t i = 0; i < N; ++i) {
|
|
|
|
if (*reinterpret_cast<uint64_t*>(m_hash + (i * 32) + 24) < m_state.job.target()) {
|
2018-04-26 16:27:53 +00:00
|
|
|
Workers::submit(JobResult(m_state.job.poolId(), m_state.job.id(), *nonce(i), m_hash + (i * 32), m_state.job.diff(), m_state.job.algorithm()));
|
2018-04-14 15:14:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*nonce(i) += 1;
|
2017-06-11 07:58:46 +00:00
|
|
|
}
|
2017-06-11 03:52:23 +00:00
|
|
|
|
2018-04-14 15:14:57 +00:00
|
|
|
m_count += N;
|
|
|
|
|
2017-06-13 16:58:31 +00:00
|
|
|
std::this_thread::yield();
|
2017-06-11 03:52:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
consumeJob();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-14 15:14:57 +00:00
|
|
|
template<size_t N>
|
|
|
|
bool MultiWorker<N>::resume(const Job &job)
|
2017-07-03 00:34:23 +00:00
|
|
|
{
|
2018-04-14 15:14:57 +00:00
|
|
|
if (m_state.job.poolId() == -1 && job.poolId() >= 0 && job.id() == m_pausedState.job.id()) {
|
|
|
|
m_state = m_pausedState;
|
2017-07-03 00:34:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-11 03:52:23 +00:00
|
|
|
|
2018-06-10 12:48:34 +00:00
|
|
|
template<size_t N>
|
|
|
|
bool MultiWorker<N>::verify(xmrig::Variant variant, const uint8_t *referenceValue)
|
|
|
|
{
|
|
|
|
|
|
|
|
xmrig::CpuThread::cn_hash_fun func = m_thread->fn(variant);
|
|
|
|
if (!func) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
func(test_input, 76, m_hash, m_ctx);
|
|
|
|
return memcmp(m_hash, referenceValue, sizeof m_hash) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-14 15:14:57 +00:00
|
|
|
template<size_t N>
|
|
|
|
void MultiWorker<N>::consumeJob()
|
2017-06-11 03:52:23 +00:00
|
|
|
{
|
2017-07-03 00:34:23 +00:00
|
|
|
Job job = Workers::job();
|
2017-06-11 03:52:23 +00:00
|
|
|
m_sequence = Workers::sequence();
|
2018-04-14 15:14:57 +00:00
|
|
|
if (m_state.job == job) {
|
2017-07-10 18:42:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-06-11 03:52:23 +00:00
|
|
|
|
2017-07-03 00:34:23 +00:00
|
|
|
save(job);
|
|
|
|
|
|
|
|
if (resume(job)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-14 15:14:57 +00:00
|
|
|
m_state.job = job;
|
2017-06-11 07:58:46 +00:00
|
|
|
|
2018-04-14 15:14:57 +00:00
|
|
|
const size_t size = m_state.job.size();
|
|
|
|
memcpy(m_state.blob, m_state.job.blob(), m_state.job.size());
|
|
|
|
|
|
|
|
if (N > 1) {
|
|
|
|
for (size_t i = 1; i < N; ++i) {
|
|
|
|
memcpy(m_state.blob + (i * size), m_state.blob, size);
|
|
|
|
}
|
2017-06-11 07:58:46 +00:00
|
|
|
}
|
2018-04-14 15:14:57 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < N; ++i) {
|
|
|
|
if (m_state.job.isNicehash()) {
|
|
|
|
*nonce(i) = (*nonce(i) & 0xff000000U) + (0xffffffU / m_totalWays * (m_offset + i));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*nonce(i) = 0xffffffffU / m_totalWays * (m_offset + i);
|
|
|
|
}
|
2017-06-11 07:58:46 +00:00
|
|
|
}
|
2017-06-10 06:41:08 +00:00
|
|
|
}
|
2017-07-03 00:34:23 +00:00
|
|
|
|
|
|
|
|
2018-04-14 15:14:57 +00:00
|
|
|
template<size_t N>
|
|
|
|
void MultiWorker<N>::save(const Job &job)
|
2017-07-03 00:34:23 +00:00
|
|
|
{
|
2018-04-14 15:14:57 +00:00
|
|
|
if (job.poolId() == -1 && m_state.job.poolId() >= 0) {
|
|
|
|
m_pausedState = m_state;
|
2017-07-03 00:34:23 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-14 15:14:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
template class MultiWorker<1>;
|
|
|
|
template class MultiWorker<2>;
|
|
|
|
template class MultiWorker<3>;
|
|
|
|
template class MultiWorker<4>;
|
|
|
|
template class MultiWorker<5>;
|