Add class JobResult.
This commit is contained in:
parent
3ad11685cc
commit
a0a8711dab
12 changed files with 112 additions and 22 deletions
|
@ -25,9 +25,10 @@
|
|||
#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_threadId(threadId),
|
||||
m_threads(threads),
|
||||
m_affinity(affinity),
|
||||
m_worker(nullptr)
|
||||
{
|
||||
|
|
|
@ -35,17 +35,19 @@ class IWorker;
|
|||
class Handle
|
||||
{
|
||||
public:
|
||||
Handle(int threadId, int64_t affinity, bool nicehash);
|
||||
Handle(int threadId, int threads, int64_t affinity, bool nicehash);
|
||||
void start(void *(*callback) (void *));
|
||||
|
||||
inline bool nicehash() const { return m_nicehash; }
|
||||
inline int threadId() const { return m_threadId; }
|
||||
inline int threads() const { return m_threads; }
|
||||
inline int64_t affinity() const { return m_affinity; }
|
||||
inline void setWorker(IWorker *worker) { m_worker = worker; }
|
||||
|
||||
private:
|
||||
bool m_nicehash;
|
||||
int m_threadId;
|
||||
int m_threads;
|
||||
int64_t m_affinity;
|
||||
IWorker *m_worker;
|
||||
pthread_t m_thread;
|
||||
|
|
|
@ -24,10 +24,10 @@
|
|||
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <pthread.h>
|
||||
|
||||
|
||||
#include "Console.h"
|
||||
#include "crypto/CryptoNight.h"
|
||||
#include "workers/SingleWorker.h"
|
||||
#include "workers/Workers.h"
|
||||
|
||||
|
@ -35,7 +35,6 @@
|
|||
SingleWorker::SingleWorker(Handle *handle)
|
||||
: Worker(handle)
|
||||
{
|
||||
LOG_WARN("SingleWorker %d", pthread_self());
|
||||
}
|
||||
|
||||
|
||||
|
@ -44,7 +43,6 @@ void SingleWorker::start()
|
|||
while (true) {
|
||||
if (Workers::isPaused()) {
|
||||
do {
|
||||
LOG_ERR("SLEEP WAIT FOR WORK");
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
}
|
||||
while (Workers::isPaused());
|
||||
|
@ -53,9 +51,12 @@ void SingleWorker::start()
|
|||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
@ -71,5 +72,13 @@ void SingleWorker::consumeJob()
|
|||
m_job = Workers::job();
|
||||
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/JobResult.h"
|
||||
#include "workers/Worker.h"
|
||||
|
||||
|
||||
|
@ -43,6 +44,7 @@ private:
|
|||
void consumeJob();
|
||||
|
||||
Job m_job;
|
||||
JobResult m_result;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -30,13 +30,15 @@
|
|||
|
||||
Worker::Worker(Handle *handle) :
|
||||
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) {
|
||||
Cpu::setAffinity(m_id, m_handle->affinity());
|
||||
if (Cpu::threads() > 1 && handle->affinity() != -1L) {
|
||||
Cpu::setAffinity(m_id, handle->affinity());
|
||||
}
|
||||
|
||||
m_ctx = Mem::create(m_id);
|
||||
|
|
|
@ -44,8 +44,9 @@ public:
|
|||
protected:
|
||||
bool m_nicehash;
|
||||
cryptonight_ctx *m_ctx;
|
||||
Handle *m_handle;
|
||||
int m_id;
|
||||
int m_threads;
|
||||
uint64_t m_count;
|
||||
uint64_t m_sequence;
|
||||
};
|
||||
|
||||
|
|
|
@ -21,16 +21,17 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "Console.h"
|
||||
#include "workers/Handle.h"
|
||||
#include "workers/SingleWorker.h"
|
||||
#include "workers/Workers.h"
|
||||
|
||||
|
||||
Job Workers::m_job;
|
||||
pthread_mutex_t Workers::m_mutex;
|
||||
pthread_rwlock_t Workers::m_rwlock;
|
||||
std::atomic<int> Workers::m_paused;
|
||||
std::atomic<uint64_t> Workers::m_sequence;
|
||||
std::list<JobResult> Workers::m_queue;
|
||||
std::vector<Handle*> Workers::m_workers;
|
||||
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)
|
||||
{
|
||||
LOG_NOTICE("start %d", pthread_self());
|
||||
pthread_mutex_init(&m_mutex, nullptr);
|
||||
pthread_rwlock_init(&m_rwlock, nullptr);
|
||||
|
||||
m_sequence = 0;
|
||||
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);
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -93,4 +99,12 @@ void *Workers::onReady(void *arg)
|
|||
|
||||
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 <list>
|
||||
#include <pthread.h>
|
||||
#include <uv.h>
|
||||
#include <vector>
|
||||
|
||||
|
||||
#include "net/Job.h"
|
||||
#include "net/JobResult.h"
|
||||
|
||||
|
||||
class Handle;
|
||||
|
@ -43,7 +44,7 @@ public:
|
|||
static Job job();
|
||||
static void setJob(const Job &job);
|
||||
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 isPaused() { return m_paused.load(std::memory_order_relaxed) == 1; }
|
||||
|
@ -55,9 +56,11 @@ private:
|
|||
static void onResult(uv_async_t *handle);
|
||||
|
||||
static Job m_job;
|
||||
static pthread_mutex_t m_mutex;
|
||||
static pthread_rwlock_t m_rwlock;
|
||||
static std::atomic<int> m_paused;
|
||||
static std::atomic<uint64_t> m_sequence;
|
||||
static std::list<JobResult> m_queue;
|
||||
static std::vector<Handle*> m_workers;
|
||||
static uv_async_t m_async;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue