Added real graceful exit.

This commit is contained in:
XMRig 2019-03-16 00:44:15 +07:00
parent 1e62943010
commit ba68fb6c53
41 changed files with 391 additions and 303 deletions

View file

@ -5,7 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/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
@ -29,6 +30,7 @@
#include <stdio.h>
#include "base/tools/Handle.h"
#include "common/log/Log.h"
#include "core/Config.h"
#include "core/Controller.h"
@ -49,6 +51,7 @@ inline static const char *format(double h, char *buf, size_t size)
Hashrate::Hashrate(size_t threads, xmrig::Controller *controller) :
m_highest(0.0),
m_threads(threads),
m_timer(nullptr),
m_controller(controller)
{
m_counts = new uint64_t*[threads];
@ -64,10 +67,11 @@ Hashrate::Hashrate(size_t threads, xmrig::Controller *controller) :
const int printTime = controller->config()->printTime();
if (printTime > 0) {
uv_timer_init(uv_default_loop(), &m_timer);
m_timer.data = this;
m_timer = new uv_timer_t;
uv_timer_init(uv_default_loop(), m_timer);
m_timer->data = this;
uv_timer_start(&m_timer, Hashrate::onReport, (printTime + 4) * 1000, printTime * 1000);
uv_timer_start(m_timer, Hashrate::onReport, (printTime + 4) * 1000, printTime * 1000);
}
}
@ -171,7 +175,8 @@ void Hashrate::print() const
void Hashrate::stop()
{
uv_timer_stop(&m_timer);
xmrig::Handle::close(m_timer);
m_timer = nullptr;
}

View file

@ -5,7 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/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
@ -21,8 +22,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __HASHRATE_H__
#define __HASHRATE_H__
#ifndef XMRIG_HASHRATE_H
#define XMRIG_HASHRATE_H
#include <stdint.h>
@ -67,9 +68,9 @@ private:
uint32_t* m_top;
uint64_t** m_counts;
uint64_t** m_timestamps;
uv_timer_t m_timer;
uv_timer_t *m_timer;
xmrig::Controller *m_controller;
};
#endif /* __HASHRATE_H__ */
#endif /* XMRIG_HASHRATE_H */

View file

@ -35,7 +35,7 @@
template<size_t N>
MultiWorker<N>::MultiWorker(Handle *handle)
MultiWorker<N>::MultiWorker(ThreadHandle *handle)
: Worker(handle)
{
m_memory = Mem::create(m_ctx, m_thread->algorithm(), N);

View file

@ -40,7 +40,7 @@ template<size_t N>
class MultiWorker : public Worker
{
public:
MultiWorker(Handle *handle);
MultiWorker(ThreadHandle *handle);
~MultiWorker();
protected:

View file

@ -5,7 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/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
@ -22,10 +23,10 @@
*/
#include "workers/Handle.h"
#include "workers/ThreadHandle.h"
Handle::Handle(xmrig::IThread *config, uint32_t offset, size_t totalWays) :
ThreadHandle::ThreadHandle(xmrig::IThread *config, uint32_t offset, size_t totalWays) :
m_worker(nullptr),
m_totalWays(totalWays),
m_offset(offset),
@ -34,13 +35,13 @@ Handle::Handle(xmrig::IThread *config, uint32_t offset, size_t totalWays) :
}
void Handle::join()
void ThreadHandle::join()
{
uv_thread_join(&m_thread);
}
void Handle::start(void (*callback) (void *))
void ThreadHandle::start(void (*callback) (void *))
{
uv_thread_create(&m_thread, callback, this);
}

View file

@ -5,7 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/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
@ -21,8 +22,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __HANDLE_H__
#define __HANDLE_H__
#ifndef XMRIG_THREADHANDLE_H
#define XMRIG_THREADHANDLE_H
#include <assert.h>
@ -36,10 +37,10 @@
class IWorker;
class Handle
class ThreadHandle
{
public:
Handle(xmrig::IThread *config, uint32_t offset, size_t totalWays);
ThreadHandle(xmrig::IThread *config, uint32_t offset, size_t totalWays);
void join();
void start(void (*callback) (void *));
@ -59,4 +60,4 @@ private:
};
#endif /* __HANDLE_H__ */
#endif /* XMRIG_THREADHANDLE_H */

View file

@ -27,11 +27,11 @@
#include "common/cpu/Cpu.h"
#include "common/Platform.h"
#include "workers/CpuThread.h"
#include "workers/Handle.h"
#include "workers/ThreadHandle.h"
#include "workers/Worker.h"
Worker::Worker(Handle *handle) :
Worker::Worker(ThreadHandle *handle) :
m_id(handle->threadId()),
m_totalWays(handle->totalWays()),
m_offset(handle->offset()),

View file

@ -33,7 +33,7 @@
#include "Mem.h"
class Handle;
class ThreadHandle;
namespace xmrig {
@ -44,7 +44,7 @@ namespace xmrig {
class Worker : public IWorker
{
public:
Worker(Handle *handle);
Worker(ThreadHandle *handle);
inline const MemInfo &memory() const { return m_memory; }
inline size_t id() const override { return m_id; }

View file

@ -28,6 +28,7 @@
#include "api/Api.h"
#include "base/tools/Handle.h"
#include "common/log/Log.h"
#include "core/Config.h"
#include "core/Controller.h"
@ -36,9 +37,9 @@
#include "interfaces/IThread.h"
#include "Mem.h"
#include "rapidjson/document.h"
#include "workers/Handle.h"
#include "workers/Hashrate.h"
#include "workers/MultiWorker.h"
#include "workers/ThreadHandle.h"
#include "workers/Workers.h"
@ -51,12 +52,12 @@ Workers::LaunchStatus Workers::m_status;
std::atomic<int> Workers::m_paused;
std::atomic<uint64_t> Workers::m_sequence;
std::list<xmrig::JobResult> Workers::m_queue;
std::vector<Handle*> Workers::m_workers;
std::vector<ThreadHandle*> Workers::m_workers;
uint64_t Workers::m_ticks = 0;
uv_async_t Workers::m_async;
uv_async_t *Workers::m_async = nullptr;
uv_mutex_t Workers::m_mutex;
uv_rwlock_t Workers::m_rwlock;
uv_timer_t Workers::m_timer;
uv_timer_t *Workers::m_timer = nullptr;
xmrig::Controller *Workers::m_controller = nullptr;
@ -103,11 +104,11 @@ void Workers::printHashrate(bool detail)
char num2[8] = { 0 };
char num3[8] = { 0 };
Log::i()->text("%s| THREAD | AFFINITY | 10s H/s | 60s H/s | 15m H/s |", isColors ? "\x1B[1;37m" : "");
xmrig::Log::i()->text("%s| THREAD | AFFINITY | 10s H/s | 60s H/s | 15m H/s |", isColors ? "\x1B[1;37m" : "");
size_t i = 0;
for (const xmrig::IThread *thread : m_controller->config()->threads()) {
Log::i()->text("| %6zu | %8" PRId64 " | %7s | %7s | %7s |",
xmrig::Log::i()->text("| %6zu | %8" PRId64 " | %7s | %7s | %7s |",
thread->index(),
thread->affinity(),
Hashrate::format(m_hashrate->calc(thread->index(), Hashrate::ShortInterval), num1, sizeof num1),
@ -192,14 +193,17 @@ void Workers::start(xmrig::Controller *controller)
m_sequence = 1;
m_paused = 1;
uv_async_init(uv_default_loop(), &m_async, Workers::onResult);
uv_timer_init(uv_default_loop(), &m_timer);
uv_timer_start(&m_timer, Workers::onTick, 500, 500);
m_async = new uv_async_t;
uv_async_init(uv_default_loop(), m_async, Workers::onResult);
m_timer = new uv_timer_t;
uv_timer_init(uv_default_loop(), m_timer);
uv_timer_start(m_timer, Workers::onTick, 500, 500);
uint32_t offset = 0;
for (xmrig::IThread *thread : threads) {
Handle *handle = new Handle(thread, offset, m_status.ways);
ThreadHandle *handle = new ThreadHandle(thread, offset, m_status.ways);
offset += thread->multiway();
m_workers.push_back(handle);
@ -212,10 +216,10 @@ void Workers::start(xmrig::Controller *controller)
void Workers::stop()
{
uv_timer_stop(&m_timer);
xmrig::Handle::close(m_timer);
xmrig::Handle::close(m_async);
m_hashrate->stop();
uv_close(reinterpret_cast<uv_handle_t*>(&m_async), nullptr);
m_paused = 0;
m_sequence = 0;
@ -231,7 +235,7 @@ void Workers::submit(const xmrig::JobResult &result)
m_queue.push_back(result);
uv_mutex_unlock(&m_mutex);
uv_async_send(&m_async);
uv_async_send(m_async);
}
@ -257,7 +261,7 @@ void Workers::threadsSummary(rapidjson::Document &doc)
void Workers::onReady(void *arg)
{
auto handle = static_cast<Handle*>(arg);
auto handle = static_cast<ThreadHandle*>(arg);
IWorker *worker = nullptr;
@ -319,7 +323,7 @@ void Workers::onResult(uv_async_t *handle)
void Workers::onTick(uv_timer_t *handle)
{
for (Handle *handle : m_workers) {
for (ThreadHandle *handle : m_workers) {
if (!handle->worker()) {
return;
}

View file

@ -36,9 +36,9 @@
#include "rapidjson/fwd.h"
class Handle;
class Hashrate;
class IWorker;
class ThreadHandle;
namespace xmrig {
@ -109,12 +109,12 @@ private:
static std::atomic<int> m_paused;
static std::atomic<uint64_t> m_sequence;
static std::list<xmrig::JobResult> m_queue;
static std::vector<Handle*> m_workers;
static std::vector<ThreadHandle*> m_workers;
static uint64_t m_ticks;
static uv_async_t m_async;
static uv_async_t *m_async;
static uv_mutex_t m_mutex;
static uv_rwlock_t m_rwlock;
static uv_timer_t m_timer;
static uv_timer_t *m_timer;
static xmrig::Controller *m_controller;
};