Job flow WIP.
This commit is contained in:
parent
bcef4b12ec
commit
3ad11685cc
14 changed files with 126 additions and 25 deletions
|
@ -25,8 +25,10 @@
|
|||
#include "workers/Handle.h"
|
||||
|
||||
|
||||
Handle::Handle(int id) :
|
||||
m_id(id),
|
||||
Handle::Handle(int threadId, int64_t affinity, bool nicehash) :
|
||||
m_nicehash(nicehash),
|
||||
m_threadId(threadId),
|
||||
m_affinity(affinity),
|
||||
m_worker(nullptr)
|
||||
{
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
|
||||
|
||||
#include <pthread.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
class IWorker;
|
||||
|
@ -34,14 +35,18 @@ class IWorker;
|
|||
class Handle
|
||||
{
|
||||
public:
|
||||
Handle(int id);
|
||||
Handle(int threadId, int64_t affinity, bool nicehash);
|
||||
void start(void *(*callback) (void *));
|
||||
|
||||
inline int id() const { return m_id; }
|
||||
inline bool nicehash() const { return m_nicehash; }
|
||||
inline int threadId() const { return m_threadId; }
|
||||
inline int64_t affinity() const { return m_affinity; }
|
||||
inline void setWorker(IWorker *worker) { m_worker = worker; }
|
||||
|
||||
private:
|
||||
int m_id;
|
||||
bool m_nicehash;
|
||||
int m_threadId;
|
||||
int64_t m_affinity;
|
||||
IWorker *m_worker;
|
||||
pthread_t m_thread;
|
||||
};
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
*/
|
||||
|
||||
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <pthread.h>
|
||||
|
||||
|
@ -40,5 +41,35 @@ SingleWorker::SingleWorker(Handle *handle)
|
|||
|
||||
void SingleWorker::start()
|
||||
{
|
||||
// Workers::submit();
|
||||
while (true) {
|
||||
if (Workers::isPaused()) {
|
||||
do {
|
||||
LOG_ERR("SLEEP WAIT FOR WORK");
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
}
|
||||
while (Workers::isPaused());
|
||||
|
||||
consumeJob();
|
||||
}
|
||||
|
||||
while (!Workers::isOutdated(m_sequence)) {
|
||||
LOG_ERR("WORK %lld %lld", Workers::sequence(), m_sequence);
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
|
||||
|
||||
sched_yield();
|
||||
}
|
||||
|
||||
consumeJob();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void SingleWorker::consumeJob()
|
||||
{
|
||||
m_job = Workers::job();
|
||||
m_sequence = Workers::sequence();
|
||||
|
||||
LOG_WARN("consumeJob");
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
#define __SINGLEWORKER_H__
|
||||
|
||||
|
||||
#include "net/Job.h"
|
||||
#include "workers/Worker.h"
|
||||
|
||||
|
||||
|
@ -37,6 +38,11 @@ public:
|
|||
SingleWorker(Handle *handle);
|
||||
|
||||
void start() override;
|
||||
|
||||
private:
|
||||
void consumeJob();
|
||||
|
||||
Job m_job;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -22,17 +22,23 @@
|
|||
*/
|
||||
|
||||
|
||||
#include "Cpu.h"
|
||||
#include "Mem.h"
|
||||
#include "workers/Handle.h"
|
||||
#include "workers/Worker.h"
|
||||
#include "Mem.h"
|
||||
|
||||
|
||||
Worker::Worker(Handle *handle) :
|
||||
m_nicehash(handle->nicehash()),
|
||||
m_handle(handle),
|
||||
m_id(handle->id())
|
||||
m_id(handle->threadId())
|
||||
{
|
||||
m_handle->setWorker(this);
|
||||
|
||||
if (Cpu::threads() > 1 && m_handle->affinity() != -1L) {
|
||||
Cpu::setAffinity(m_id, m_handle->affinity());
|
||||
}
|
||||
|
||||
m_ctx = Mem::create(m_id);
|
||||
}
|
||||
|
||||
|
|
|
@ -25,6 +25,9 @@
|
|||
#define __WORKER_H__
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
#include "interfaces/IWorker.h"
|
||||
|
||||
|
||||
|
@ -39,9 +42,11 @@ public:
|
|||
~Worker();
|
||||
|
||||
protected:
|
||||
bool m_nicehash;
|
||||
cryptonight_ctx *m_ctx;
|
||||
Handle *m_handle;
|
||||
int m_id;
|
||||
uint64_t m_sequence;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -21,27 +21,52 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
|
||||
#include "Console.h"
|
||||
#include "workers/Handle.h"
|
||||
#include "workers/SingleWorker.h"
|
||||
#include "workers/Workers.h"
|
||||
|
||||
|
||||
Job Workers::m_job;
|
||||
pthread_rwlock_t Workers::m_rwlock;
|
||||
std::atomic<int> Workers::m_paused;
|
||||
std::atomic<uint64_t> Workers::m_sequence;
|
||||
std::vector<Handle*> Workers::m_workers;
|
||||
uv_async_t Workers::m_async;
|
||||
|
||||
|
||||
void Workers::start(int threads)
|
||||
Job Workers::job()
|
||||
{
|
||||
pthread_rwlock_rdlock(&m_rwlock);
|
||||
Job job = m_job;
|
||||
pthread_rwlock_unlock(&m_rwlock);
|
||||
|
||||
return std::move(job);
|
||||
}
|
||||
|
||||
|
||||
void Workers::setJob(const Job &job)
|
||||
{
|
||||
pthread_rwlock_wrlock(&m_rwlock);
|
||||
m_job = job;
|
||||
pthread_rwlock_unlock(&m_rwlock);
|
||||
|
||||
m_sequence++;
|
||||
m_paused = 0;
|
||||
}
|
||||
|
||||
|
||||
void Workers::start(int threads, int64_t affinity, bool nicehash)
|
||||
{
|
||||
LOG_NOTICE("start %d", pthread_self());
|
||||
|
||||
m_sequence = 0;
|
||||
m_paused = 1;
|
||||
|
||||
uv_async_init(uv_default_loop(), &m_async, Workers::onResult);
|
||||
|
||||
for (int i = 0; i < threads; ++i) {
|
||||
Handle *handle = new Handle(i);
|
||||
Handle *handle = new Handle(i, affinity, nicehash);
|
||||
m_workers.push_back(handle);
|
||||
handle->start(Workers::onReady);
|
||||
}
|
||||
|
|
|
@ -25,8 +25,13 @@
|
|||
#define __WORKERS_H__
|
||||
|
||||
|
||||
#include <vector>
|
||||
#include <atomic>
|
||||
#include <pthread.h>
|
||||
#include <uv.h>
|
||||
#include <vector>
|
||||
|
||||
|
||||
#include "net/Job.h"
|
||||
|
||||
|
||||
class Handle;
|
||||
|
@ -35,13 +40,24 @@ class Handle;
|
|||
class Workers
|
||||
{
|
||||
public:
|
||||
static void start(int threads);
|
||||
static Job job();
|
||||
static void setJob(const Job &job);
|
||||
static void start(int threads, int64_t affinity, bool nicehash);
|
||||
static void submit();
|
||||
|
||||
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 uint64_t sequence() { return m_sequence.load(std::memory_order_relaxed); }
|
||||
static inline void pause() { m_paused = 1; }
|
||||
|
||||
private:
|
||||
static void *onReady(void *arg);
|
||||
static void onResult(uv_async_t *handle);
|
||||
|
||||
static Job m_job;
|
||||
static pthread_rwlock_t m_rwlock;
|
||||
static std::atomic<int> m_paused;
|
||||
static std::atomic<uint64_t> m_sequence;
|
||||
static std::vector<Handle*> m_workers;
|
||||
static uv_async_t m_async;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue