From f05a3284743d01e1081e5ae66284950947b203dd Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 15 Aug 2017 08:19:55 +0300 Subject: [PATCH] Added --cpu-priority option. --- src/App.cpp | 3 +- src/Options.cpp | 11 ++++++ src/Options.h | 2 ++ src/Platform.h | 2 ++ src/Platform_mac.cpp | 12 +++++++ src/Platform_unix.cpp | 12 +++++++ src/Platform_win.cpp | 74 +++++++++++++++++++++++++++++++++++++++++ src/config.json | 1 + src/net/Job.h | 1 + src/net/JobResult.h | 9 +++++ src/workers/Handle.cpp | 3 +- src/workers/Handle.h | 4 ++- src/workers/Worker.cpp | 2 ++ src/workers/Workers.cpp | 4 +-- src/workers/Workers.h | 2 +- 15 files changed, 136 insertions(+), 6 deletions(-) diff --git a/src/App.cpp b/src/App.cpp index e4af60bf..28e61df3 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -82,6 +82,7 @@ App::App(int argc, char **argv) : # endif Platform::init(); + Platform::setProcessPriority(m_options->priority()); m_network = new Network(m_options); @@ -115,7 +116,7 @@ int App::exec() Mem::allocate(m_options->algo(), m_options->threads(), m_options->doubleHash()); Summary::print(); - Workers::start(m_options->affinity()); + Workers::start(m_options->affinity(), m_options->priority()); m_network->connect(); diff --git a/src/Options.cpp b/src/Options.cpp index e79231d5..832ed09a 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -63,6 +63,7 @@ Options:\n\ -r, --retries=N number of times to retry before switch to backup server (default: 5)\n\ -R, --retry-pause=N time to pause between retries (default: 5)\n\ --cpu-affinity set process affinity to CPU core(s), mask 0x3 for cores 0 and 1\n\ + --cpu-priority set process priority (0 idle, 2 normal to 5 highest)\n\ --no-color disable colored output\n\ --donate-level=N donate level, default 5%% (5 minutes in 100 minutes)\n\ -B, --background run the miner in the background\n\ @@ -91,6 +92,7 @@ static struct option const options[] = { { "background", 0, nullptr, 'B' }, { "config", 1, nullptr, 'c' }, { "cpu-affinity", 1, nullptr, 1020 }, + { "cpu-priority", 1, nullptr, 1021 }, { "donate-level", 1, nullptr, 1003 }, { "help", 0, nullptr, 'h' }, { "keepalive", 0, nullptr ,'k' }, @@ -118,6 +120,7 @@ static struct option const config_options[] = { { "av", 1, nullptr, 'v' }, { "background", 0, nullptr, 'B' }, { "cpu-affinity", 1, nullptr, 1020 }, + { "cpu-priority", 1, nullptr, 1021 }, { "donate-level", 1, nullptr, 1003 }, { "log-file", 1, nullptr, 'l' }, { "max-cpu-usage", 1, nullptr, 1004 }, @@ -211,6 +214,7 @@ Options::Options(int argc, char **argv) : m_donateLevel(kDonateLevel), m_maxCpuUsage(75), m_printTime(60), + m_priority(-1), m_retries(5), m_retryPause(5), m_threads(0), @@ -326,6 +330,7 @@ bool Options::parseArg(int key, const char *arg) case 1003: /* --donate-level */ case 1004: /* --max-cpu-usage */ case 1007: /* --print-time */ + case 1021: /* --cpu-priority */ return parseArg(key, strtol(arg, nullptr, 10)); case 'B': /* --background */ @@ -434,6 +439,12 @@ bool Options::parseArg(int key, uint64_t arg) } break; + case 1021: /* --cpu-priority */ + if (arg <= 5) { + m_priority = (int) arg; + } + break; + default: break; } diff --git a/src/Options.h b/src/Options.h index 3735fca0..0e57100e 100644 --- a/src/Options.h +++ b/src/Options.h @@ -64,6 +64,7 @@ public: inline int algoVariant() const { return m_algoVariant; } inline int donateLevel() const { return m_donateLevel; } inline int printTime() const { return m_printTime; } + inline int priority() const { return m_priority; } inline int retries() const { return m_retries; } inline int retryPause() const { return m_retryPause; } inline int threads() const { return m_threads; } @@ -109,6 +110,7 @@ private: int m_donateLevel; int m_maxCpuUsage; int m_printTime; + int m_priority; int m_retries; int m_retryPause; int m_threads; diff --git a/src/Platform.h b/src/Platform.h index cc798fca..cc6fd879 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -30,6 +30,8 @@ class Platform public: static void init(); static void release(); + static void setProcessPriority(int priority); + static void setThreadPriority(int priority); static inline const char *userAgent() { return m_userAgent; } diff --git a/src/Platform_mac.cpp b/src/Platform_mac.cpp index a25d1ac1..456e6f74 100644 --- a/src/Platform_mac.cpp +++ b/src/Platform_mac.cpp @@ -52,3 +52,15 @@ void Platform::release() delete [] m_userAgent; } + +void Platform::setProcessPriority(int priority) +{ + +} + + +void Platform::setThreadPriority(int priority) +{ + +} + diff --git a/src/Platform_unix.cpp b/src/Platform_unix.cpp index 6da3a197..861fe873 100644 --- a/src/Platform_unix.cpp +++ b/src/Platform_unix.cpp @@ -62,3 +62,15 @@ void Platform::release() delete [] m_userAgent; } + +void Platform::setProcessPriority(int priority) +{ + +} + + + +void Platform::setThreadPriority(int priority) +{ + +} diff --git a/src/Platform_win.cpp b/src/Platform_win.cpp index 7fbe0c8c..7943b662 100644 --- a/src/Platform_win.cpp +++ b/src/Platform_win.cpp @@ -84,3 +84,77 @@ void Platform::release() delete [] m_userAgent; } + +void Platform::setProcessPriority(int priority) +{ + if (priority == -1) { + return; + } + + DWORD prio = IDLE_PRIORITY_CLASS; + switch (priority) + { + case 1: + prio = BELOW_NORMAL_PRIORITY_CLASS; + break; + + case 2: + prio = NORMAL_PRIORITY_CLASS; + break; + + case 3: + prio = ABOVE_NORMAL_PRIORITY_CLASS; + break; + + case 4: + prio = HIGH_PRIORITY_CLASS; + break; + + case 5: + prio = REALTIME_PRIORITY_CLASS; + + default: + break; + } + + SetPriorityClass(GetCurrentProcess(), prio); +} + + + +void Platform::setThreadPriority(int priority) +{ + if (priority == -1) { + return; + } + + int prio = THREAD_PRIORITY_IDLE; + switch (priority) + { + case 1: + prio = THREAD_PRIORITY_BELOW_NORMAL; + break; + + case 2: + prio = THREAD_PRIORITY_NORMAL; + break; + + case 3: + prio = THREAD_PRIORITY_ABOVE_NORMAL; + break; + + case 4: + prio = THREAD_PRIORITY_HIGHEST; + break; + + case 5: + prio = THREAD_PRIORITY_TIME_CRITICAL; + break; + + default: + break; + } + + SetThreadPriority(GetCurrentThread(), prio); +} + diff --git a/src/config.json b/src/config.json index a0ce1a3b..afc2936b 100644 --- a/src/config.json +++ b/src/config.json @@ -4,6 +4,7 @@ "background": false, "colors": true, "cpu-affinity": null, + "cpu-priority": null, "donate-level": 5, "log-file": null, "max-cpu-usage": 75, diff --git a/src/net/Job.h b/src/net/Job.h index 1b2f732a..86160584 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -42,6 +42,7 @@ public: inline bool isNicehash() const { return m_nicehash; } inline bool isValid() const { return m_size > 0 && m_diff > 0; } inline const char *id() const { return m_id; } + inline const uint32_t *nonce() const { return reinterpret_cast(m_blob + 39); } inline const uint8_t *blob() const { return m_blob; } inline int poolId() const { return m_poolId; } inline size_t size() const { return m_size; } diff --git a/src/net/JobResult.h b/src/net/JobResult.h index 9ced3cac..de3b17ad 100644 --- a/src/net/JobResult.h +++ b/src/net/JobResult.h @@ -43,6 +43,15 @@ public: } + inline JobResult(const Job &job) : poolId(0), diff(0), nonce(0) + { + memcpy(jobId, job.id(), sizeof(jobId)); + poolId = job.poolId(); + diff = job.diff(); + nonce = *job.nonce(); + } + + inline JobResult &operator=(const Job &job) { memcpy(jobId, job.id(), sizeof(jobId)); poolId = job.poolId(); diff --git a/src/workers/Handle.cpp b/src/workers/Handle.cpp index 8b748cd3..c461cee7 100644 --- a/src/workers/Handle.cpp +++ b/src/workers/Handle.cpp @@ -25,7 +25,8 @@ #include "workers/Handle.h" -Handle::Handle(int threadId, int threads, int64_t affinity) : +Handle::Handle(int threadId, int threads, int64_t affinity, int priority) : + m_priority(priority), m_threadId(threadId), m_threads(threads), m_affinity(affinity), diff --git a/src/workers/Handle.h b/src/workers/Handle.h index a663fbe9..9faae0d0 100644 --- a/src/workers/Handle.h +++ b/src/workers/Handle.h @@ -35,10 +35,11 @@ class IWorker; class Handle { public: - Handle(int threadId, int threads, int64_t affinity); + Handle(int threadId, int threads, int64_t affinity, int priority); void join(); void start(void (*callback) (void *)); + inline int priority() const { return m_priority; } inline int threadId() const { return m_threadId; } inline int threads() const { return m_threads; } inline int64_t affinity() const { return m_affinity; } @@ -46,6 +47,7 @@ public: inline void setWorker(IWorker *worker) { m_worker = worker; } private: + int m_priority; int m_threadId; int m_threads; int64_t m_affinity; diff --git a/src/workers/Worker.cpp b/src/workers/Worker.cpp index 583c1d45..02646ced 100644 --- a/src/workers/Worker.cpp +++ b/src/workers/Worker.cpp @@ -26,6 +26,7 @@ #include "Cpu.h" #include "Mem.h" +#include "Platform.h" #include "workers/Handle.h" #include "workers/Worker.h" @@ -42,6 +43,7 @@ Worker::Worker(Handle *handle) : Cpu::setAffinity(m_id, handle->affinity()); } + Platform::setThreadPriority(handle->priority()); m_ctx = Mem::create(m_id); } diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index b056c826..e51f5d22 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -98,7 +98,7 @@ void Workers::setJob(const Job &job) } -void Workers::start(int64_t affinity) +void Workers::start(int64_t affinity, int priority) { const int threads = Mem::threads(); m_hashrate = new Hashrate(threads); @@ -114,7 +114,7 @@ void Workers::start(int64_t affinity) uv_timer_start(&m_timer, Workers::onTick, 500, 500); for (int i = 0; i < threads; ++i) { - Handle *handle = new Handle(i, threads, affinity); + Handle *handle = new Handle(i, threads, affinity, priority); m_workers.push_back(handle); handle->start(Workers::onReady); } diff --git a/src/workers/Workers.h b/src/workers/Workers.h index 0caf22a2..e76d0a62 100644 --- a/src/workers/Workers.h +++ b/src/workers/Workers.h @@ -46,7 +46,7 @@ public: static void printHashrate(bool detail); static void setEnabled(bool enabled); static void setJob(const Job &job); - static void start(int64_t affinity); + static void start(int64_t affinity, int priority); static void stop(); static void submit(const JobResult &result);