Removed CPU specific code from Worker class.

This commit is contained in:
XMRig 2019-07-14 00:35:38 +07:00
parent ee434a5708
commit dff59fabc2
7 changed files with 39 additions and 44 deletions

View file

@ -23,36 +23,25 @@
* 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 <chrono>
#include "backend/common/Worker.h" #include "backend/common/Worker.h"
#include "backend/cpu/Cpu.h"
#include "base/kernel/Platform.h" #include "base/kernel/Platform.h"
#include "workers/CpuThreadLegacy.h" #include "base/tools/Chrono.h"
#include "workers/ThreadHandle.h"
Worker::Worker(ThreadHandle *handle) : xmrig::Worker::Worker(size_t id, int64_t affinity, int priority) :
m_id(handle->threadId()), m_id(id),
m_hashCount(0), m_hashCount(0),
m_timestamp(0), m_timestamp(0),
m_count(0), m_count(0)
m_thread(static_cast<xmrig::CpuThreadLegacy *>(handle->config()))
{ {
if (xmrig::Cpu::info()->threads() > 1 && m_thread->affinity() != -1L) { Platform::trySetThreadAffinity(affinity);
Platform::setThreadAffinity(m_thread->affinity()); Platform::setThreadPriority(priority);
}
Platform::setThreadPriority(m_thread->priority());
} }
void Worker::storeStats() void xmrig::Worker::storeStats()
{ {
using namespace std::chrono;
const uint64_t timestamp = time_point_cast<milliseconds>(high_resolution_clock::now()).time_since_epoch().count();
m_hashCount.store(m_count, std::memory_order_relaxed); m_hashCount.store(m_count, std::memory_order_relaxed);
m_timestamp.store(timestamp, std::memory_order_relaxed); m_timestamp.store(Chrono::highResolutionMSecs(), std::memory_order_relaxed);
} }

View file

@ -32,23 +32,16 @@
#include "backend/common/interfaces/IWorker.h" #include "backend/common/interfaces/IWorker.h"
#include "Mem.h"
class ThreadHandle;
namespace xmrig { namespace xmrig {
class CpuThreadLegacy;
}
class Worker : public IWorker class Worker : public IWorker
{ {
public: public:
Worker(ThreadHandle *handle); Worker(size_t id, int64_t affinity, int priority);
inline const MemInfo &memory() const { return m_memory; }
inline size_t id() const override { return m_id; } inline size_t id() const override { return m_id; }
inline uint64_t hashCount() const override { return m_hashCount.load(std::memory_order_relaxed); } inline uint64_t hashCount() const override { return m_hashCount.load(std::memory_order_relaxed); }
inline uint64_t timestamp() const override { return m_timestamp.load(std::memory_order_relaxed); } inline uint64_t timestamp() const override { return m_timestamp.load(std::memory_order_relaxed); }
@ -57,12 +50,13 @@ protected:
void storeStats(); void storeStats();
const size_t m_id; const size_t m_id;
MemInfo m_memory;
std::atomic<uint64_t> m_hashCount; std::atomic<uint64_t> m_hashCount;
std::atomic<uint64_t> m_timestamp; std::atomic<uint64_t> m_timestamp;
uint64_t m_count; uint64_t m_count;
xmrig::CpuThreadLegacy *m_thread;
}; };
} // namespace xmrig
#endif /* XMRIG_WORKER_H */ #endif /* XMRIG_WORKER_H */

View file

@ -34,6 +34,7 @@
#include "crypto/rx/RxVm.h" #include "crypto/rx/RxVm.h"
#include "net/JobResults.h" #include "net/JobResults.h"
#include "workers/CpuThreadLegacy.h" #include "workers/CpuThreadLegacy.h"
#include "workers/ThreadHandle.h"
#include "workers/Workers.h" #include "workers/Workers.h"
@ -45,8 +46,9 @@ static constexpr uint32_t kReserveCount = 4096;
template<size_t N> template<size_t N>
xmrig::CpuWorker<N>::CpuWorker(ThreadHandle *handle) xmrig::CpuWorker<N>::CpuWorker(ThreadHandle *handle) :
: Worker(handle) Worker(handle->threadId(), handle->config()->affinity(), handle->config()->priority()),
m_thread(static_cast<xmrig::CpuThreadLegacy *>(handle->config()))
{ {
if (m_thread->algorithm().family() != Algorithm::RANDOM_X) { if (m_thread->algorithm().family() != Algorithm::RANDOM_X) {
m_memory = Mem::create(m_ctx, m_thread->algorithm(), N); m_memory = Mem::create(m_ctx, m_thread->algorithm(), N);

View file

@ -34,9 +34,13 @@
#include "backend/common/Worker.h" #include "backend/common/Worker.h"
class ThreadHandle;
namespace xmrig { namespace xmrig {
class CpuThreadLegacy;
class RxVm; class RxVm;
@ -47,6 +51,8 @@ public:
CpuWorker(ThreadHandle *handle); CpuWorker(ThreadHandle *handle);
~CpuWorker() override; ~CpuWorker() override;
inline const MemInfo &memory() const { return m_memory; }
protected: protected:
bool selfTest() override; bool selfTest() override;
void start() override; void start() override;
@ -60,9 +66,10 @@ private:
bool verify2(const Algorithm &algorithm, const uint8_t *referenceValue); bool verify2(const Algorithm &algorithm, const uint8_t *referenceValue);
void consumeJob(); void consumeJob();
CpuThreadLegacy *m_thread;
cryptonight_ctx *m_ctx[N]; cryptonight_ctx *m_ctx[N];
MemInfo m_memory;
uint8_t m_hash[N * 32]; uint8_t m_hash[N * 32];
WorkerJob<N> m_job; WorkerJob<N> m_job;
# ifdef XMRIG_ALGO_RANDOMX # ifdef XMRIG_ALGO_RANDOMX

View file

@ -35,6 +35,14 @@ namespace xmrig {
class Chrono class Chrono
{ {
public: public:
static inline uint64_t highResolutionMSecs()
{
using namespace std::chrono;
return static_cast<uint64_t>(time_point_cast<milliseconds>(high_resolution_clock::now()).time_since_epoch().count());
}
static inline uint64_t steadyMSecs() static inline uint64_t steadyMSecs()
{ {
using namespace std::chrono; using namespace std::chrono;

View file

@ -24,13 +24,13 @@
#include <assert.h> #include <assert.h>
#include <chrono>
#include <math.h> #include <math.h>
#include <memory.h> #include <memory.h>
#include <stdio.h> #include <stdio.h>
#include "base/io/log/Log.h" #include "base/io/log/Log.h"
#include "base/tools/Chrono.h"
#include "base/tools/Handle.h" #include "base/tools/Handle.h"
#include "core/config/Config.h" #include "core/config/Config.h"
#include "core/Controller.h" #include "core/Controller.h"
@ -98,9 +98,6 @@ double Hashrate::calc(size_t threadId, size_t ms) const
return nan(""); return nan("");
} }
using namespace std::chrono;
const uint64_t now = time_point_cast<milliseconds>(high_resolution_clock::now()).time_since_epoch().count();
uint64_t earliestHashCount = 0; uint64_t earliestHashCount = 0;
uint64_t earliestStamp = 0; uint64_t earliestStamp = 0;
uint64_t lastestStamp = 0; uint64_t lastestStamp = 0;
@ -119,7 +116,7 @@ double Hashrate::calc(size_t threadId, size_t ms) const
lastestHashCnt = m_counts[threadId][idx]; lastestHashCnt = m_counts[threadId][idx];
} }
if (now - m_timestamps[threadId][idx] > ms) { if (xmrig::Chrono::highResolutionMSecs() - m_timestamps[threadId][idx] > ms) {
haveFullSet = true; haveFullSet = true;
break; break;
} }
@ -136,10 +133,8 @@ double Hashrate::calc(size_t threadId, size_t ms) const
return nan(""); return nan("");
} }
double hashes, time; const double hashes = static_cast<double>(lastestHashCnt - earliestHashCount);
hashes = (double) lastestHashCnt - earliestHashCount; const double time = static_cast<double>(lastestStamp - earliestStamp) / 1000.0;
time = (double) lastestStamp - earliestStamp;
time /= 1000.0;
return hashes / time; return hashes / time;
} }

View file

@ -299,12 +299,12 @@ void Workers::onTick(uv_timer_t *)
void Workers::start(IWorker *worker) void Workers::start(IWorker *worker)
{ {
const Worker *w = static_cast<const Worker *>(worker); // const Worker *w = static_cast<const Worker *>(worker);
uv_mutex_lock(&m_mutex); uv_mutex_lock(&m_mutex);
m_status.started++; m_status.started++;
m_status.pages += w->memory().pages; // m_status.pages += w->memory().pages;
m_status.hugePages += w->memory().hugePages; // m_status.hugePages += w->memory().hugePages;
if (m_status.started == m_status.threads) { if (m_status.started == m_status.threads) {
const double percent = (double) m_status.hugePages / m_status.pages * 100.0; const double percent = (double) m_status.hugePages / m_status.pages * 100.0;