Add class JobResult.
This commit is contained in:
parent
3ad11685cc
commit
a0a8711dab
12 changed files with 112 additions and 22 deletions
|
@ -13,6 +13,7 @@ set(HEADERS
|
||||||
src/Mem.h
|
src/Mem.h
|
||||||
src/net/Client.h
|
src/net/Client.h
|
||||||
src/net/Job.h
|
src/net/Job.h
|
||||||
|
src/net/JobResult.h
|
||||||
src/net/Network.h
|
src/net/Network.h
|
||||||
src/net/Url.h
|
src/net/Url.h
|
||||||
src/Options.h
|
src/Options.h
|
||||||
|
@ -84,7 +85,7 @@ endif()
|
||||||
|
|
||||||
add_definitions(/D_GNU_SOURCE)
|
add_definitions(/D_GNU_SOURCE)
|
||||||
add_definitions(/DUNICODE)
|
add_definitions(/DUNICODE)
|
||||||
#add_definitions(/DAPP_DEBUG)
|
add_definitions(/DAPP_DEBUG)
|
||||||
|
|
||||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
|
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
|
||||||
|
|
||||||
|
@ -99,7 +100,7 @@ set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -funroll-loops -fvari
|
||||||
|
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes -Wall -fno-exceptions -fno-rtti")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes -Wall -fno-exceptions -fno-rtti")
|
||||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast -s -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2")
|
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast -s -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2")
|
||||||
#set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -gdwarf-2")
|
#set(CMAKE_C_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -gdwarf-2")
|
||||||
#set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -fprofile-generate")
|
#set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -fprofile-generate")
|
||||||
#set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -fprofile-use -fprofile-correction")
|
#set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -fprofile-use -fprofile-correction")
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,8 @@
|
||||||
#include "crypto/CryptoNight.h"
|
#include "crypto/CryptoNight.h"
|
||||||
#include "crypto/CryptoNight_p.h"
|
#include "crypto/CryptoNight_p.h"
|
||||||
#include "crypto/CryptoNight_test.h"
|
#include "crypto/CryptoNight_test.h"
|
||||||
|
#include "net/Job.h"
|
||||||
|
#include "net/JobResult.h"
|
||||||
#include "Options.h"
|
#include "Options.h"
|
||||||
|
|
||||||
|
|
||||||
|
@ -91,6 +93,14 @@ void (*cryptonight_variations[4])(const void *input, size_t size, void *output,
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
bool CryptoNight::hash(const Job &job, JobResult &result, cryptonight_ctx *ctx)
|
||||||
|
{
|
||||||
|
cryptonight_hash_ctx(job.blob(), job.size(), result.result, ctx);
|
||||||
|
|
||||||
|
return *reinterpret_cast<uint64_t*>(result.result + 24) < job.target();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool CryptoNight::init(int algo, int variant)
|
bool CryptoNight::init(int algo, int variant)
|
||||||
{
|
{
|
||||||
if (variant < 1 || variant > 4) {
|
if (variant < 1 || variant > 4) {
|
||||||
|
|
|
@ -39,9 +39,14 @@ struct cryptonight_ctx {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class Job;
|
||||||
|
class JobResult;
|
||||||
|
|
||||||
|
|
||||||
class CryptoNight
|
class CryptoNight
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
static bool hash(const Job &job, JobResult &result, cryptonight_ctx *ctx);
|
||||||
static bool init(int algo, int variant);
|
static bool init(int algo, int variant);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
40
src/net/JobResult.h
Normal file
40
src/net/JobResult.h
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
/* 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 __JOBRESULT_H__
|
||||||
|
#define __JOBRESULT_H__
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
|
||||||
|
class JobResult
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
char jobId[64];
|
||||||
|
int poolId;
|
||||||
|
uint32_t nonce;
|
||||||
|
uint8_t result[32];
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* __JOBRESULT_H__ */
|
|
@ -25,9 +25,10 @@
|
||||||
#include "workers/Handle.h"
|
#include "workers/Handle.h"
|
||||||
|
|
||||||
|
|
||||||
Handle::Handle(int threadId, int64_t affinity, bool nicehash) :
|
Handle::Handle(int threadId, int threads, int64_t affinity, bool nicehash) :
|
||||||
m_nicehash(nicehash),
|
m_nicehash(nicehash),
|
||||||
m_threadId(threadId),
|
m_threadId(threadId),
|
||||||
|
m_threads(threads),
|
||||||
m_affinity(affinity),
|
m_affinity(affinity),
|
||||||
m_worker(nullptr)
|
m_worker(nullptr)
|
||||||
{
|
{
|
||||||
|
|
|
@ -35,17 +35,19 @@ class IWorker;
|
||||||
class Handle
|
class Handle
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Handle(int threadId, int64_t affinity, bool nicehash);
|
Handle(int threadId, int threads, int64_t affinity, bool nicehash);
|
||||||
void start(void *(*callback) (void *));
|
void start(void *(*callback) (void *));
|
||||||
|
|
||||||
inline bool nicehash() const { return m_nicehash; }
|
inline bool nicehash() const { return m_nicehash; }
|
||||||
inline int threadId() const { return m_threadId; }
|
inline int threadId() const { return m_threadId; }
|
||||||
|
inline int threads() const { return m_threads; }
|
||||||
inline int64_t affinity() const { return m_affinity; }
|
inline int64_t affinity() const { return m_affinity; }
|
||||||
inline void setWorker(IWorker *worker) { m_worker = worker; }
|
inline void setWorker(IWorker *worker) { m_worker = worker; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_nicehash;
|
bool m_nicehash;
|
||||||
int m_threadId;
|
int m_threadId;
|
||||||
|
int m_threads;
|
||||||
int64_t m_affinity;
|
int64_t m_affinity;
|
||||||
IWorker *m_worker;
|
IWorker *m_worker;
|
||||||
pthread_t m_thread;
|
pthread_t m_thread;
|
||||||
|
|
|
@ -24,10 +24,10 @@
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
|
|
||||||
#include "Console.h"
|
#include "Console.h"
|
||||||
|
#include "crypto/CryptoNight.h"
|
||||||
#include "workers/SingleWorker.h"
|
#include "workers/SingleWorker.h"
|
||||||
#include "workers/Workers.h"
|
#include "workers/Workers.h"
|
||||||
|
|
||||||
|
@ -35,7 +35,6 @@
|
||||||
SingleWorker::SingleWorker(Handle *handle)
|
SingleWorker::SingleWorker(Handle *handle)
|
||||||
: Worker(handle)
|
: Worker(handle)
|
||||||
{
|
{
|
||||||
LOG_WARN("SingleWorker %d", pthread_self());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -44,7 +43,6 @@ void SingleWorker::start()
|
||||||
while (true) {
|
while (true) {
|
||||||
if (Workers::isPaused()) {
|
if (Workers::isPaused()) {
|
||||||
do {
|
do {
|
||||||
LOG_ERR("SLEEP WAIT FOR WORK");
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||||
}
|
}
|
||||||
while (Workers::isPaused());
|
while (Workers::isPaused());
|
||||||
|
@ -53,9 +51,12 @@ void SingleWorker::start()
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!Workers::isOutdated(m_sequence)) {
|
while (!Workers::isOutdated(m_sequence)) {
|
||||||
LOG_ERR("WORK %lld %lld", Workers::sequence(), m_sequence);
|
m_count++;
|
||||||
|
*m_job.nonce() = ++m_result.nonce;
|
||||||
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
|
if (CryptoNight::hash(m_job, m_result, m_ctx)) {
|
||||||
|
Workers::submit(m_result);
|
||||||
|
}
|
||||||
|
|
||||||
sched_yield();
|
sched_yield();
|
||||||
}
|
}
|
||||||
|
@ -71,5 +72,13 @@ void SingleWorker::consumeJob()
|
||||||
m_job = Workers::job();
|
m_job = Workers::job();
|
||||||
m_sequence = Workers::sequence();
|
m_sequence = Workers::sequence();
|
||||||
|
|
||||||
LOG_WARN("consumeJob");
|
memcpy(m_result.jobId, m_job.id(), sizeof(m_result.jobId));
|
||||||
|
m_result.poolId = m_job.poolId();
|
||||||
|
|
||||||
|
if (m_nicehash) {
|
||||||
|
m_result.nonce = (*m_job.nonce() & 0xff000000U) + (0xffffffU / m_threads * m_id);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
m_result.nonce = 0xffffffffU / m_threads * m_id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
|
|
||||||
|
|
||||||
#include "net/Job.h"
|
#include "net/Job.h"
|
||||||
|
#include "net/JobResult.h"
|
||||||
#include "workers/Worker.h"
|
#include "workers/Worker.h"
|
||||||
|
|
||||||
|
|
||||||
|
@ -43,6 +44,7 @@ private:
|
||||||
void consumeJob();
|
void consumeJob();
|
||||||
|
|
||||||
Job m_job;
|
Job m_job;
|
||||||
|
JobResult m_result;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -30,13 +30,15 @@
|
||||||
|
|
||||||
Worker::Worker(Handle *handle) :
|
Worker::Worker(Handle *handle) :
|
||||||
m_nicehash(handle->nicehash()),
|
m_nicehash(handle->nicehash()),
|
||||||
m_handle(handle),
|
m_id(handle->threadId()),
|
||||||
m_id(handle->threadId())
|
m_threads(handle->threads()),
|
||||||
|
m_count(0),
|
||||||
|
m_sequence(0)
|
||||||
{
|
{
|
||||||
m_handle->setWorker(this);
|
handle->setWorker(this);
|
||||||
|
|
||||||
if (Cpu::threads() > 1 && m_handle->affinity() != -1L) {
|
if (Cpu::threads() > 1 && handle->affinity() != -1L) {
|
||||||
Cpu::setAffinity(m_id, m_handle->affinity());
|
Cpu::setAffinity(m_id, handle->affinity());
|
||||||
}
|
}
|
||||||
|
|
||||||
m_ctx = Mem::create(m_id);
|
m_ctx = Mem::create(m_id);
|
||||||
|
|
|
@ -44,8 +44,9 @@ public:
|
||||||
protected:
|
protected:
|
||||||
bool m_nicehash;
|
bool m_nicehash;
|
||||||
cryptonight_ctx *m_ctx;
|
cryptonight_ctx *m_ctx;
|
||||||
Handle *m_handle;
|
|
||||||
int m_id;
|
int m_id;
|
||||||
|
int m_threads;
|
||||||
|
uint64_t m_count;
|
||||||
uint64_t m_sequence;
|
uint64_t m_sequence;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -21,16 +21,17 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "Console.h"
|
|
||||||
#include "workers/Handle.h"
|
#include "workers/Handle.h"
|
||||||
#include "workers/SingleWorker.h"
|
#include "workers/SingleWorker.h"
|
||||||
#include "workers/Workers.h"
|
#include "workers/Workers.h"
|
||||||
|
|
||||||
|
|
||||||
Job Workers::m_job;
|
Job Workers::m_job;
|
||||||
|
pthread_mutex_t Workers::m_mutex;
|
||||||
pthread_rwlock_t Workers::m_rwlock;
|
pthread_rwlock_t Workers::m_rwlock;
|
||||||
std::atomic<int> Workers::m_paused;
|
std::atomic<int> Workers::m_paused;
|
||||||
std::atomic<uint64_t> Workers::m_sequence;
|
std::atomic<uint64_t> Workers::m_sequence;
|
||||||
|
std::list<JobResult> Workers::m_queue;
|
||||||
std::vector<Handle*> Workers::m_workers;
|
std::vector<Handle*> Workers::m_workers;
|
||||||
uv_async_t Workers::m_async;
|
uv_async_t Workers::m_async;
|
||||||
|
|
||||||
|
@ -58,7 +59,8 @@ void Workers::setJob(const Job &job)
|
||||||
|
|
||||||
void Workers::start(int threads, int64_t affinity, bool nicehash)
|
void Workers::start(int threads, int64_t affinity, bool nicehash)
|
||||||
{
|
{
|
||||||
LOG_NOTICE("start %d", pthread_self());
|
pthread_mutex_init(&m_mutex, nullptr);
|
||||||
|
pthread_rwlock_init(&m_rwlock, nullptr);
|
||||||
|
|
||||||
m_sequence = 0;
|
m_sequence = 0;
|
||||||
m_paused = 1;
|
m_paused = 1;
|
||||||
|
@ -66,15 +68,19 @@ void Workers::start(int threads, int64_t affinity, bool nicehash)
|
||||||
uv_async_init(uv_default_loop(), &m_async, Workers::onResult);
|
uv_async_init(uv_default_loop(), &m_async, Workers::onResult);
|
||||||
|
|
||||||
for (int i = 0; i < threads; ++i) {
|
for (int i = 0; i < threads; ++i) {
|
||||||
Handle *handle = new Handle(i, affinity, nicehash);
|
Handle *handle = new Handle(i, threads, affinity, nicehash);
|
||||||
m_workers.push_back(handle);
|
m_workers.push_back(handle);
|
||||||
handle->start(Workers::onReady);
|
handle->start(Workers::onReady);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Workers::submit()
|
void Workers::submit(const JobResult &result)
|
||||||
{
|
{
|
||||||
|
pthread_mutex_lock(&m_mutex);
|
||||||
|
m_queue.push_back(result);
|
||||||
|
pthread_mutex_unlock(&m_mutex);
|
||||||
|
|
||||||
uv_async_send(&m_async);
|
uv_async_send(&m_async);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,4 +99,12 @@ void *Workers::onReady(void *arg)
|
||||||
|
|
||||||
void Workers::onResult(uv_async_t *handle)
|
void Workers::onResult(uv_async_t *handle)
|
||||||
{
|
{
|
||||||
|
std::list<JobResult> results;
|
||||||
|
|
||||||
|
pthread_mutex_lock(&m_mutex);
|
||||||
|
while (!m_queue.empty()) {
|
||||||
|
results.push_back(std::move(m_queue.front()));
|
||||||
|
m_queue.pop_front();
|
||||||
|
}
|
||||||
|
pthread_mutex_unlock(&m_mutex);
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,12 +26,13 @@
|
||||||
|
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#include <list>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <uv.h>
|
#include <uv.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
#include "net/Job.h"
|
#include "net/Job.h"
|
||||||
|
#include "net/JobResult.h"
|
||||||
|
|
||||||
|
|
||||||
class Handle;
|
class Handle;
|
||||||
|
@ -43,7 +44,7 @@ public:
|
||||||
static Job job();
|
static Job job();
|
||||||
static void setJob(const Job &job);
|
static void setJob(const Job &job);
|
||||||
static void start(int threads, int64_t affinity, bool nicehash);
|
static void start(int threads, int64_t affinity, bool nicehash);
|
||||||
static void submit();
|
static void submit(const JobResult &result);
|
||||||
|
|
||||||
static inline bool isOutdated(uint64_t sequence) { return m_sequence.load(std::memory_order_relaxed) != sequence; }
|
static inline bool isOutdated(uint64_t sequence) { return m_sequence.load(std::memory_order_relaxed) != sequence; }
|
||||||
static inline bool isPaused() { return m_paused.load(std::memory_order_relaxed) == 1; }
|
static inline bool isPaused() { return m_paused.load(std::memory_order_relaxed) == 1; }
|
||||||
|
@ -55,9 +56,11 @@ private:
|
||||||
static void onResult(uv_async_t *handle);
|
static void onResult(uv_async_t *handle);
|
||||||
|
|
||||||
static Job m_job;
|
static Job m_job;
|
||||||
|
static pthread_mutex_t m_mutex;
|
||||||
static pthread_rwlock_t m_rwlock;
|
static pthread_rwlock_t m_rwlock;
|
||||||
static std::atomic<int> m_paused;
|
static std::atomic<int> m_paused;
|
||||||
static std::atomic<uint64_t> m_sequence;
|
static std::atomic<uint64_t> m_sequence;
|
||||||
|
static std::list<JobResult> m_queue;
|
||||||
static std::vector<Handle*> m_workers;
|
static std::vector<Handle*> m_workers;
|
||||||
static uv_async_t m_async;
|
static uv_async_t m_async;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue