From ba68fb6c538b3a86592023a823f2010f5637cad7 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 16 Mar 2019 00:44:15 +0700 Subject: [PATCH] Added real graceful exit. --- CMakeLists.txt | 5 +- src/App.cpp | 12 ++- src/App.h | 2 +- src/Summary.cpp | 16 ++-- src/base/kernel/Signals.cpp | 19 ++++- src/base/kernel/Signals.h | 2 + src/base/net/dns/Dns.cpp | 4 +- src/base/tools/Handle.cpp | 79 -------------------- src/base/tools/Handle.h | 68 ++++++++++++++--- src/common/Console.cpp | 35 ++++++--- src/common/Console.h | 21 ++++-- src/common/api/Httpd.cpp | 26 +++++-- src/common/api/Httpd.h | 12 +-- src/common/interfaces/IConsoleListener.h | 19 +++-- src/common/interfaces/ILogBackend.h | 17 +++-- src/common/log/BasicLog.cpp | 11 +-- src/common/log/BasicLog.h | 14 ++-- src/common/log/ConsoleLog.cpp | 41 +++++----- src/common/log/ConsoleLog.h | 22 +++--- src/common/log/FileLog.cpp | 22 +++--- src/common/log/FileLog.h | 18 +++-- src/common/log/Log.cpp | 18 +++-- src/common/log/Log.h | 20 +++-- src/common/log/SysLog.cpp | 9 ++- src/common/log/SysLog.h | 16 +++- src/core/Controller.cpp | 15 +++- src/core/Controller.h | 1 + src/net/Network.cpp | 28 ++++--- src/net/Network.h | 3 +- src/net/strategies/DonateStrategy.cpp | 13 ++-- src/net/strategies/DonateStrategy.h | 2 +- src/workers/Hashrate.cpp | 15 ++-- src/workers/Hashrate.h | 11 +-- src/workers/MultiWorker.cpp | 2 +- src/workers/MultiWorker.h | 2 +- src/workers/{Handle.cpp => ThreadHandle.cpp} | 11 +-- src/workers/{Handle.h => ThreadHandle.h} | 13 ++-- src/workers/Worker.cpp | 4 +- src/workers/Worker.h | 4 +- src/workers/Workers.cpp | 34 +++++---- src/workers/Workers.h | 8 +- 41 files changed, 391 insertions(+), 303 deletions(-) delete mode 100644 src/base/tools/Handle.cpp rename src/workers/{Handle.cpp => ThreadHandle.cpp} (79%) rename src/workers/{Handle.h => ThreadHandle.h} (85%) diff --git a/CMakeLists.txt b/CMakeLists.txt index f0d2e64f..1f87a85d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,7 +84,7 @@ set(HEADERS src/Summary.h src/version.h src/workers/CpuThread.h - src/workers/Handle.h + src/workers/ThreadHandle.h src/workers/Hashrate.h src/workers/MultiWorker.h src/workers/Worker.h @@ -131,7 +131,6 @@ set(SOURCES src/base/net/stratum/strategies/SinglePoolStrategy.cpp src/base/tools/Arguments.cpp src/base/tools/Buffer.cpp - src/base/tools/Handle.cpp src/base/tools/String.cpp src/common/config/CommonConfig.cpp src/common/config/ConfigLoader.cpp @@ -151,7 +150,7 @@ set(SOURCES src/net/strategies/DonateStrategy.cpp src/Summary.cpp src/workers/CpuThread.cpp - src/workers/Handle.cpp + src/workers/ThreadHandle.cpp src/workers/Hashrate.cpp src/workers/MultiWorker.cpp src/workers/Worker.cpp diff --git a/src/App.cpp b/src/App.cpp index e75766ac..62dd3814 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -187,8 +187,14 @@ void xmrig::App::onSignal(int signum) void xmrig::App::close() { - m_controller->network()->stop(); - Workers::stop(); +# ifndef XMRIG_NO_HTTPD + m_httpd->stop(); +# endif - uv_stop(uv_default_loop()); + m_signals->stop(); + m_console->stop(); + m_controller->stop(); + + Workers::stop(); + Log::release(); } diff --git a/src/App.h b/src/App.h index fc944967..02cc61a2 100644 --- a/src/App.h +++ b/src/App.h @@ -31,13 +31,13 @@ #include "common/interfaces/IConsoleListener.h" -class Console; class Httpd; namespace xmrig { +class Console; class Controller; class Network; class Process; diff --git a/src/Summary.cpp b/src/Summary.cpp index 7a52d1b1..61638019 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -59,11 +59,11 @@ inline static const char *asmName(xmrig::Assembly assembly, bool colors) static void print_memory(xmrig::Config *config) { # ifdef _WIN32 if (config->isColors()) { - Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("%-13s") "%s", + xmrig::Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("%-13s") "%s", "HUGE PAGES", Mem::isHugepagesAvailable() ? "\x1B[1;32mavailable" : "\x1B[01;31munavailable"); } else { - Log::i()->text(" * %-13s%s", "HUGE PAGES", Mem::isHugepagesAvailable() ? "available" : "unavailable"); + xmrig::Log::i()->text(" * %-13s%s", "HUGE PAGES", Mem::isHugepagesAvailable() ? "available" : "unavailable"); } # endif } @@ -108,7 +108,7 @@ static void print_threads(xmrig::Config *config) snprintf(buf, sizeof buf, ", affinity=0x%" PRIX64, config->affinity()); } - Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN_BOLD("%d") WHITE_BOLD(", %s, av=%d, %sdonate=%d%%") WHITE_BOLD("%s") + xmrig::Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN_BOLD("%d") WHITE_BOLD(", %s, av=%d, %sdonate=%d%%") WHITE_BOLD("%s") : " * %-13s%d, %s, av=%d, %sdonate=%d%%%s", "THREADS", config->threadsCount(), @@ -119,7 +119,7 @@ static void print_threads(xmrig::Config *config) buf); } else { - Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN_BOLD("%d") WHITE_BOLD(", %s, %sdonate=%d%%") + xmrig::Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN_BOLD("%d") WHITE_BOLD(", %s, %sdonate=%d%%") : " * %-13s%d, %s, %sdonate=%d%%", "THREADS", config->threadsCount(), @@ -132,11 +132,11 @@ static void print_threads(xmrig::Config *config) if (config->assembly() == xmrig::ASM_AUTO) { const xmrig::Assembly assembly = xmrig::Cpu::info()->assembly(); - Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13sauto:%s") + xmrig::Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13sauto:%s") : " * %-13sauto:%s", "ASSEMBLY", asmName(assembly, config->isColors())); } else { - Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13s%s") : " * %-13s%s", "ASSEMBLY", asmName(config->assembly(), config->isColors())); + xmrig::Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13s%s") : " * %-13s%s", "ASSEMBLY", asmName(config->assembly(), config->isColors())); } # endif } @@ -145,12 +145,12 @@ static void print_threads(xmrig::Config *config) static void print_commands(xmrig::Config *config) { if (config->isColors()) { - Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("COMMANDS ") MAGENTA_BOLD("h") WHITE_BOLD("ashrate, ") + xmrig::Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("COMMANDS ") MAGENTA_BOLD("h") WHITE_BOLD("ashrate, ") MAGENTA_BOLD("p") WHITE_BOLD("ause, ") MAGENTA_BOLD("r") WHITE_BOLD("esume")); } else { - Log::i()->text(" * COMMANDS 'h' hashrate, 'p' pause, 'r' resume"); + xmrig::Log::i()->text(" * COMMANDS 'h' hashrate, 'p' pause, 'r' resume"); } } diff --git a/src/base/kernel/Signals.cpp b/src/base/kernel/Signals.cpp index a37c49c6..87825b83 100644 --- a/src/base/kernel/Signals.cpp +++ b/src/base/kernel/Signals.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018 SChernykh - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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 @@ -44,20 +44,31 @@ xmrig::Signals::Signals(ISignalListener *listener) m_signals[i] = signal; uv_signal_init(uv_default_loop(), signal); - uv_signal_start(signal, xmrig::Signals::onSignal, signums[i]); + uv_signal_start(signal, Signals::onSignal, signums[i]); } } xmrig::Signals::~Signals() { + stop(); +} + + +void xmrig::Signals::stop() +{ + if (!m_signals[0]) { + return; + } + for (size_t i = 0; i < kSignalsCount; ++i) { Handle::close(m_signals[i]); + m_signals[i] = nullptr; } } void xmrig::Signals::onSignal(uv_signal_t *handle, int signum) { - static_cast(handle->data)->m_listener->onSignal(signum); + static_cast(handle->data)->m_listener->onSignal(signum); } diff --git a/src/base/kernel/Signals.h b/src/base/kernel/Signals.h index 8ef4a4bb..9b4a870a 100644 --- a/src/base/kernel/Signals.h +++ b/src/base/kernel/Signals.h @@ -46,6 +46,8 @@ public: Signals(ISignalListener *listener); ~Signals(); + void stop(); + private: void close(int signum); diff --git a/src/base/net/dns/Dns.cpp b/src/base/net/dns/Dns.cpp index 1e285fb4..f2402124 100644 --- a/src/base/net/dns/Dns.cpp +++ b/src/base/net/dns/Dns.cpp @@ -52,9 +52,9 @@ xmrig::Dns::Dns(IDnsListener *listener) : xmrig::Dns::~Dns() { - Handle::close(m_resolver); - m_storage.release(m_key); + + Handle::close(m_resolver); } diff --git a/src/base/tools/Handle.cpp b/src/base/tools/Handle.cpp deleted file mode 100644 index d486ab39..00000000 --- a/src/base/tools/Handle.cpp +++ /dev/null @@ -1,79 +0,0 @@ -/* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , - * - * 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 - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -#include - - -#include "base/tools/Handle.h" - - -void xmrig::Handle::close(uv_fs_event_t *handle) -{ - if (handle) { - uv_fs_event_stop(handle); - close(reinterpret_cast(handle)); - } -} - - -void xmrig::Handle::close(uv_getaddrinfo_t *handle) -{ - if (handle) { - uv_cancel(reinterpret_cast(handle)); - close(reinterpret_cast(handle)); - } -} - - -void xmrig::Handle::close(uv_handle_t *handle) -{ - uv_close(handle, [](uv_handle_t *handle) { delete handle; }); -} - - -void xmrig::Handle::close(uv_signal_t *handle) -{ - if (handle) { - uv_signal_stop(handle); - close(reinterpret_cast(handle)); - } -} - - -void xmrig::Handle::close(uv_tcp_t *handle) -{ - if (handle) { - close(reinterpret_cast(handle)); - } -} - - -void xmrig::Handle::close(uv_timer_s *handle) -{ - if (handle) { - uv_timer_stop(handle); - close(reinterpret_cast(handle)); - } -} diff --git a/src/base/tools/Handle.h b/src/base/tools/Handle.h index 547c9202..980d4500 100644 --- a/src/base/tools/Handle.h +++ b/src/base/tools/Handle.h @@ -26,12 +26,7 @@ #define XMRIG_HANDLE_H -typedef struct uv_fs_event_s uv_fs_event_t; -typedef struct uv_getaddrinfo_s uv_getaddrinfo_t; -typedef struct uv_handle_s uv_handle_t; -typedef struct uv_signal_s uv_signal_t; -typedef struct uv_tcp_s uv_tcp_t; -typedef struct uv_timer_s uv_timer_t; +#include namespace xmrig { @@ -40,15 +35,64 @@ namespace xmrig { class Handle { public: - static void close(uv_fs_event_t *handle); - static void close(uv_getaddrinfo_t *handle); - static void close(uv_handle_t *handle); - static void close(uv_signal_t *handle); - static void close(uv_tcp_t *handle); - static void close(uv_timer_t *handle); + template + static inline void close(T handle) + { + if (handle) { + deleteLater(handle); + } + } + + + template + static inline void deleteLater(T handle) + { + uv_close(reinterpret_cast(handle), [](uv_handle_t *handle) { delete handle; }); + } }; +template<> +inline void Handle::close(uv_timer_t *handle) +{ + if (handle) { + uv_timer_stop(handle); + deleteLater(handle); + } +} + + +template<> +inline void Handle::close(uv_signal_t *handle) +{ + if (handle) { + uv_signal_stop(handle); + deleteLater(handle); + } +} + + +template<> +inline void Handle::close(uv_getaddrinfo_t *handle) +{ + if (handle) { + uv_cancel(reinterpret_cast(handle)); + + delete handle; + } +} + + +template<> +inline void Handle::close(uv_fs_event_t *handle) +{ + if (handle) { + uv_fs_event_stop(handle); + deleteLater(handle); + } +} + + } /* namespace xmrig */ diff --git a/src/common/Console.cpp b/src/common/Console.cpp index 350fb139..8e5d7285 100644 --- a/src/common/Console.cpp +++ b/src/common/Console.cpp @@ -5,7 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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,26 +23,42 @@ */ +#include "base/tools/Handle.h" #include "common/Console.h" #include "interfaces/IConsoleListener.h" -Console::Console(IConsoleListener *listener) +xmrig::Console::Console(IConsoleListener *listener) : m_listener(listener) { - m_tty.data = this; - uv_tty_init(uv_default_loop(), &m_tty, 0, 1); + m_tty = new uv_tty_t; - if (!uv_is_readable(reinterpret_cast(&m_tty))) { + m_tty->data = this; + uv_tty_init(uv_default_loop(), m_tty, 0, 1); + + if (!uv_is_readable(reinterpret_cast(m_tty))) { return; } - uv_tty_set_mode(&m_tty, UV_TTY_MODE_RAW); - uv_read_start(reinterpret_cast(&m_tty), Console::onAllocBuffer, Console::onRead); + uv_tty_set_mode(m_tty, UV_TTY_MODE_RAW); + uv_read_start(reinterpret_cast(m_tty), Console::onAllocBuffer, Console::onRead); } -void Console::onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) +xmrig::Console::~Console() +{ + stop(); +} + + +void xmrig::Console::stop() +{ + Handle::close(m_tty); + m_tty = nullptr; +} + + +void xmrig::Console::onAllocBuffer(uv_handle_t *handle, size_t, uv_buf_t *buf) { auto console = static_cast(handle->data); buf->len = 1; @@ -49,7 +66,7 @@ void Console::onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t } -void Console::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) +void xmrig::Console::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) { if (nread < 0) { return uv_close(reinterpret_cast(stream), nullptr); diff --git a/src/common/Console.h b/src/common/Console.h index 7f2e3cc9..c0a36ec4 100644 --- a/src/common/Console.h +++ b/src/common/Console.h @@ -5,7 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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,13 +22,17 @@ * along with this program. If not, see . */ -#ifndef __CONSOLE_H__ -#define __CONSOLE_H__ +#ifndef XMRIG_CONSOLE_H +#define XMRIG_CONSOLE_H #include + +namespace xmrig { + + class IConsoleListener; @@ -35,6 +40,9 @@ class Console { public: Console(IConsoleListener *listener); + ~Console(); + + void stop(); private: static void onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf); @@ -42,8 +50,11 @@ private: char m_buf[1]; IConsoleListener *m_listener; - uv_tty_t m_tty; + uv_tty_t *m_tty; }; -#endif /* __CONSOLE_H__ */ +} /* namespace xmrig */ + + +#endif /* XMRIG_CONSOLE_H */ diff --git a/src/common/api/Httpd.cpp b/src/common/api/Httpd.cpp index eb6a4ba6..cca2b056 100644 --- a/src/common/api/Httpd.cpp +++ b/src/common/api/Httpd.cpp @@ -5,7 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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 @@ -27,6 +28,7 @@ #include "api/Api.h" +#include "base/tools/Handle.h" #include "common/api/Httpd.h" #include "common/api/HttpReply.h" #include "common/api/HttpRequest.h" @@ -41,14 +43,15 @@ Httpd::Httpd(int port, const char *accessToken, bool IPv6, bool restricted) : m_port(port), m_daemon(nullptr) { - 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; } Httpd::~Httpd() { - uv_timer_stop(&m_timer); + stop(); if (m_daemon) { MHD_stop_daemon(m_daemon); @@ -82,15 +85,22 @@ bool Httpd::start() } # if MHD_VERSION >= 0x00093900 - uv_timer_start(&m_timer, Httpd::onTimer, kIdleInterval, kIdleInterval); + uv_timer_start(m_timer, Httpd::onTimer, kIdleInterval, kIdleInterval); # else - uv_timer_start(&m_timer, Httpd::onTimer, kActiveInterval, kActiveInterval); + uv_timer_start(m_timer, Httpd::onTimer, kActiveInterval, kActiveInterval); # endif return true; } +void Httpd::stop() +{ + xmrig::Handle::close(m_timer); + m_timer = nullptr; +} + + int Httpd::process(xmrig::HttpRequest &req) { xmrig::HttpReply reply; @@ -115,11 +125,11 @@ void Httpd::run() # if MHD_VERSION >= 0x00093900 const MHD_DaemonInfo *info = MHD_get_daemon_info(m_daemon, MHD_DAEMON_INFO_CURRENT_CONNECTIONS); if (m_idle && info->num_connections) { - uv_timer_set_repeat(&m_timer, kActiveInterval); + uv_timer_set_repeat(m_timer, kActiveInterval); m_idle = false; } else if (!m_idle && !info->num_connections) { - uv_timer_set_repeat(&m_timer, kIdleInterval); + uv_timer_set_repeat(m_timer, kIdleInterval); m_idle = true; } # endif diff --git a/src/common/api/Httpd.h b/src/common/api/Httpd.h index adec1d71..82ca1541 100644 --- a/src/common/api/Httpd.h +++ b/src/common/api/Httpd.h @@ -5,7 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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 . */ -#ifndef __HTTPD_H__ -#define __HTTPD_H__ +#ifndef XMRIG_HTTPD_H +#define XMRIG_HTTPD_H #include @@ -47,6 +48,7 @@ public: Httpd(int port, const char *accessToken, bool IPv6, bool restricted); ~Httpd(); bool start(); + void stop(); private: constexpr static const int kIdleInterval = 200; @@ -64,7 +66,7 @@ private: const char *m_accessToken; const int m_port; MHD_Daemon *m_daemon; - uv_timer_t m_timer; + uv_timer_t *m_timer; }; -#endif /* __HTTPD_H__ */ +#endif /* XMRIG_HTTPD_H */ diff --git a/src/common/interfaces/IConsoleListener.h b/src/common/interfaces/IConsoleListener.h index 723f87df..88972f05 100644 --- a/src/common/interfaces/IConsoleListener.h +++ b/src/common/interfaces/IConsoleListener.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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,17 +22,23 @@ * along with this program. If not, see . */ -#ifndef __ICONSOLELISTENER_H__ -#define __ICONSOLELISTENER_H__ +#ifndef XMRIG_ICONSOLELISTENER_H +#define XMRIG_ICONSOLELISTENER_H + + +namespace xmrig { class IConsoleListener { public: - virtual ~IConsoleListener() {} + virtual ~IConsoleListener() = default; virtual void onConsoleCommand(char command) = 0; }; -#endif // __ICONSOLELISTENER_H__ +} /* namespace xmrig */ + + +#endif // XMRIG_ICONSOLELISTENER_H diff --git a/src/common/interfaces/ILogBackend.h b/src/common/interfaces/ILogBackend.h index 85a04e93..2299fc67 100644 --- a/src/common/interfaces/ILogBackend.h +++ b/src/common/interfaces/ILogBackend.h @@ -5,7 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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,14 +22,17 @@ * along with this program. If not, see . */ -#ifndef __ILOGBACKEND_H__ -#define __ILOGBACKEND_H__ +#ifndef XMRIG_ILOGBACKEND_H +#define XMRIG_ILOGBACKEND_H #include #include +namespace xmrig { + + class ILogBackend { public: @@ -46,11 +50,14 @@ public: constexpr static const size_t kBufferSize = 512; # endif - virtual ~ILogBackend() {} + virtual ~ILogBackend() = default; virtual void message(Level level, const char* fmt, va_list args) = 0; virtual void text(const char* fmt, va_list args) = 0; }; -#endif // __ILOGBACKEND_H__ +} /* namespace xmrig */ + + +#endif // XMRIG_ILOGBACKEND_H diff --git a/src/common/log/BasicLog.cpp b/src/common/log/BasicLog.cpp index cb4defcd..b048dd6e 100644 --- a/src/common/log/BasicLog.cpp +++ b/src/common/log/BasicLog.cpp @@ -5,7 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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 @@ -38,12 +39,12 @@ #include "common/log/Log.h" -BasicLog::BasicLog() +xmrig::BasicLog::BasicLog() { } -void BasicLog::message(Level level, const char* fmt, va_list args) +void xmrig::BasicLog::message(Level level, const char* fmt, va_list args) { time_t now = time(nullptr); tm stime; @@ -70,7 +71,7 @@ void BasicLog::message(Level level, const char* fmt, va_list args) } -void BasicLog::text(const char* fmt, va_list args) +void xmrig::BasicLog::text(const char* fmt, va_list args) { snprintf(m_fmt, sizeof(m_fmt) - 1, "%s%s", fmt, Log::endl(false)); @@ -78,7 +79,7 @@ void BasicLog::text(const char* fmt, va_list args) } -void BasicLog::print(va_list args) +void xmrig::BasicLog::print(va_list args) { if (vsnprintf(m_buf, sizeof(m_buf) - 1, m_fmt, args) <= 0) { return; diff --git a/src/common/log/BasicLog.h b/src/common/log/BasicLog.h index 523538e9..bddbe07c 100644 --- a/src/common/log/BasicLog.h +++ b/src/common/log/BasicLog.h @@ -5,7 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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 . */ -#ifndef __BASICLOG_H__ -#define __BASICLOG_H__ +#ifndef XMRIG_BASICLOG_H +#define XMRIG_BASICLOG_H #include @@ -32,8 +33,6 @@ namespace xmrig { - class Controller; -} class BasicLog : public ILogBackend @@ -52,4 +51,7 @@ private: char m_fmt[256]; }; -#endif /* __BASICLOG_H__ */ + +} /* namespace xmrig */ + +#endif /* XMRIG_BASICLOG_H */ diff --git a/src/common/log/ConsoleLog.cpp b/src/common/log/ConsoleLog.cpp index b10812a6..e0ac01ea 100644 --- a/src/common/log/ConsoleLog.cpp +++ b/src/common/log/ConsoleLog.cpp @@ -5,7 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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 @@ -34,24 +35,24 @@ #endif +#include "base/tools/Handle.h" #include "common/log/ConsoleLog.h" #include "common/log/Log.h" -#include "core/Config.h" -#include "core/Controller.h" -ConsoleLog::ConsoleLog(xmrig::Controller *controller) : - m_stream(nullptr), - m_controller(controller) +xmrig::ConsoleLog::ConsoleLog() : + m_stream(nullptr) { - if (uv_tty_init(uv_default_loop(), &m_tty, 1, 0) < 0) { + m_tty = new uv_tty_t; + + if (uv_tty_init(uv_default_loop(), m_tty, 1, 0) < 0) { Log::colors = false; return; } - uv_tty_set_mode(&m_tty, UV_TTY_MODE_NORMAL); + uv_tty_set_mode(m_tty, UV_TTY_MODE_NORMAL); m_uvBuf.base = m_buf; - m_stream = reinterpret_cast(&m_tty); + m_stream = reinterpret_cast(m_tty); # ifdef WIN32 HANDLE handle = GetStdHandle(STD_INPUT_HANDLE); @@ -66,7 +67,13 @@ ConsoleLog::ConsoleLog(xmrig::Controller *controller) : } -void ConsoleLog::message(Level level, const char* fmt, va_list args) +xmrig::ConsoleLog::~ConsoleLog() +{ + Handle::close(m_tty); +} + + +void xmrig::ConsoleLog::message(Level level, const char* fmt, va_list args) { time_t now = time(nullptr); tm stime; @@ -77,8 +84,6 @@ void ConsoleLog::message(Level level, const char* fmt, va_list args) localtime_r(&now, &stime); # endif - const bool isColors = m_controller->config()->isColors(); - snprintf(m_fmt, sizeof(m_fmt) - 1, "[%d-%02d-%02d %02d:%02d:%02d]%s %s%s", stime.tm_year + 1900, stime.tm_mon + 1, @@ -86,24 +91,24 @@ void ConsoleLog::message(Level level, const char* fmt, va_list args) stime.tm_hour, stime.tm_min, stime.tm_sec, - Log::colorByLevel(level, isColors), + Log::colorByLevel(level, Log::colors), fmt, - Log::endl(isColors) + Log::endl(Log::colors) ); print(args); } -void ConsoleLog::text(const char* fmt, va_list args) +void xmrig::ConsoleLog::text(const char* fmt, va_list args) { - snprintf(m_fmt, sizeof(m_fmt) - 1, "%s%s", fmt, Log::endl(m_controller->config()->isColors())); + snprintf(m_fmt, sizeof(m_fmt) - 1, "%s%s", fmt, Log::endl(Log::colors)); print(args); } -bool ConsoleLog::isWritable() const +bool xmrig::ConsoleLog::isWritable() const { if (!m_stream || uv_is_writable(m_stream) != 1) { return false; @@ -114,7 +119,7 @@ bool ConsoleLog::isWritable() const } -void ConsoleLog::print(va_list args) +void xmrig::ConsoleLog::print(va_list args) { m_uvBuf.len = vsnprintf(m_buf, sizeof(m_buf) - 1, m_fmt, args); if (m_uvBuf.len <= 0) { diff --git a/src/common/log/ConsoleLog.h b/src/common/log/ConsoleLog.h index bac09a53..73a701e6 100644 --- a/src/common/log/ConsoleLog.h +++ b/src/common/log/ConsoleLog.h @@ -5,7 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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 . */ -#ifndef __CONSOLELOG_H__ -#define __CONSOLELOG_H__ +#ifndef XMRIG_CONSOLELOG_H +#define XMRIG_CONSOLELOG_H #include @@ -32,15 +33,15 @@ namespace xmrig { - class Controller; -} class ConsoleLog : public ILogBackend { public: - ConsoleLog(xmrig::Controller *controller); + ConsoleLog(); + ~ConsoleLog() override; +protected: void message(Level level, const char *fmt, va_list args) override; void text(const char *fmt, va_list args) override; @@ -52,8 +53,11 @@ private: char m_fmt[256]; uv_buf_t m_uvBuf; uv_stream_t *m_stream; - uv_tty_t m_tty; - xmrig::Controller *m_controller; + uv_tty_t *m_tty; }; -#endif /* __CONSOLELOG_H__ */ + +} /* namespace xmrig */ + + +#endif /* XMRIG_CONSOLELOG_H */ diff --git a/src/common/log/FileLog.cpp b/src/common/log/FileLog.cpp index 9134c7c7..71eeb18c 100644 --- a/src/common/log/FileLog.cpp +++ b/src/common/log/FileLog.cpp @@ -5,7 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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 @@ -31,12 +32,9 @@ #include "common/log/FileLog.h" #include "common/log/Log.h" -#include "core/Config.h" -#include "core/Controller.h" -FileLog::FileLog(xmrig::Controller *controller, const char *fileName) : - m_controller(controller) +xmrig::FileLog::FileLog(const char *fileName) { uv_fs_t req; m_file = uv_fs_open(uv_default_loop(), &req, fileName, O_CREAT | O_APPEND | O_WRONLY, 0644, nullptr); @@ -44,7 +42,7 @@ FileLog::FileLog(xmrig::Controller *controller, const char *fileName) : } -void FileLog::message(Level level, const char* fmt, va_list args) +void xmrig::FileLog::message(Level level, const char* fmt, va_list args) { if (m_file < 0) { return; @@ -59,8 +57,6 @@ void FileLog::message(Level level, const char* fmt, va_list args) localtime_r(&now, &stime); # endif - const bool isColors = m_controller->config()->isColors(); - snprintf(m_fmt, sizeof(m_fmt) - 1, "[%d-%02d-%02d %02d:%02d:%02d]%s %s%s", stime.tm_year + 1900, stime.tm_mon + 1, @@ -68,9 +64,9 @@ void FileLog::message(Level level, const char* fmt, va_list args) stime.tm_hour, stime.tm_min, stime.tm_sec, - Log::colorByLevel(level, isColors), + Log::colorByLevel(level, Log::colors), fmt, - Log::endl(isColors) + Log::endl(Log::colors) ); char *buf = new char[kBufferSize]; @@ -80,13 +76,13 @@ void FileLog::message(Level level, const char* fmt, va_list args) } -void FileLog::text(const char* fmt, va_list args) +void xmrig::FileLog::text(const char* fmt, va_list args) { message(INFO, fmt, args); } -void FileLog::onWrite(uv_fs_t *req) +void xmrig::FileLog::onWrite(uv_fs_t *req) { delete [] static_cast(req->data); @@ -95,7 +91,7 @@ void FileLog::onWrite(uv_fs_t *req) } -void FileLog::write(char *data, size_t size) +void xmrig::FileLog::write(char *data, size_t size) { uv_buf_t buf = uv_buf_init(data, (unsigned int) size); uv_fs_t *req = new uv_fs_t; diff --git a/src/common/log/FileLog.h b/src/common/log/FileLog.h index 8a58d4e4..da80acb2 100644 --- a/src/common/log/FileLog.h +++ b/src/common/log/FileLog.h @@ -5,7 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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 . */ -#ifndef __FILELOG_H__ -#define __FILELOG_H__ +#ifndef XMRIG_FILELOG_H +#define XMRIG_FILELOG_H #include @@ -32,14 +33,12 @@ namespace xmrig { - class Controller; -} class FileLog : public ILogBackend { public: - FileLog(xmrig::Controller *controller, const char *fileName); + FileLog(const char *fileName); void message(Level level, const char* fmt, va_list args) override; void text(const char* fmt, va_list args) override; @@ -51,7 +50,10 @@ private: char m_fmt[256]; int m_file; - xmrig::Controller *m_controller; }; -#endif /* __FILELOG_H__ */ + +} /* namespace xmrig */ + + +#endif /* XMRIG_FILELOG_H */ diff --git a/src/common/log/Log.cpp b/src/common/log/Log.cpp index 07e4f299..4ef1e14a 100644 --- a/src/common/log/Log.cpp +++ b/src/common/log/Log.cpp @@ -35,6 +35,8 @@ #include "common/log/Log.h" +namespace xmrig { + Log *Log::m_self = nullptr; bool Log::colors = true; @@ -51,8 +53,10 @@ static const char *color[5] = { # endif }; +} /* namespace xmrig */ -void Log::message(ILogBackend::Level level, const char* fmt, ...) + +void xmrig::Log::message(ILogBackend::Level level, const char* fmt, ...) { uv_mutex_lock(&m_mutex); @@ -72,7 +76,7 @@ void Log::message(ILogBackend::Level level, const char* fmt, ...) } -void Log::text(const char* fmt, ...) +void xmrig::Log::text(const char* fmt, ...) { uv_mutex_lock(&m_mutex); @@ -92,7 +96,7 @@ void Log::text(const char* fmt, ...) } -const char *Log::colorByLevel(ILogBackend::Level level, bool isColors) +const char *xmrig::Log::colorByLevel(ILogBackend::Level level, bool isColors) { if (!isColors) { return ""; @@ -102,7 +106,7 @@ const char *Log::colorByLevel(ILogBackend::Level level, bool isColors) } -const char *Log::endl(bool isColors) +const char *xmrig::Log::endl(bool isColors) { # ifdef _WIN32 return isColors ? "\x1B[0m\r\n" : "\r\n"; @@ -112,7 +116,7 @@ const char *Log::endl(bool isColors) } -void Log::defaultInit() +void xmrig::Log::defaultInit() { m_self = new Log(); @@ -120,8 +124,10 @@ void Log::defaultInit() } -Log::~Log() +xmrig::Log::~Log() { + m_self = nullptr; + for (auto backend : m_backends) { delete backend; } diff --git a/src/common/log/Log.h b/src/common/log/Log.h index c32edddd..9a0ba2a9 100644 --- a/src/common/log/Log.h +++ b/src/common/log/Log.h @@ -34,6 +34,9 @@ #include "common/interfaces/ILogBackend.h" +namespace xmrig { + + class Log { public: @@ -68,6 +71,9 @@ private: }; +} /* namespace xmrig */ + + #define RED_BOLD(x) "\x1B[1;31m" x "\x1B[0m" #define RED(x) "\x1B[0;31m" x "\x1B[0m" #define GREEN_BOLD(x) "\x1B[1;32m" x "\x1B[0m" @@ -83,20 +89,20 @@ private: #define GRAY(x) "\x1B[1;30m" x "\x1B[0m" -#define LOG_ERR(x, ...) Log::i()->message(ILogBackend::ERR, x, ##__VA_ARGS__) -#define LOG_WARN(x, ...) Log::i()->message(ILogBackend::WARNING, x, ##__VA_ARGS__) -#define LOG_NOTICE(x, ...) Log::i()->message(ILogBackend::NOTICE, x, ##__VA_ARGS__) -#define LOG_INFO(x, ...) Log::i()->message(ILogBackend::INFO, x, ##__VA_ARGS__) +#define LOG_ERR(x, ...) xmrig::Log::i()->message(xmrig::ILogBackend::ERR, x, ##__VA_ARGS__) +#define LOG_WARN(x, ...) xmrig::Log::i()->message(xmrig::ILogBackend::WARNING, x, ##__VA_ARGS__) +#define LOG_NOTICE(x, ...) xmrig::Log::i()->message(xmrig::ILogBackend::NOTICE, x, ##__VA_ARGS__) +#define LOG_INFO(x, ...) xmrig::Log::i()->message(xmrig::ILogBackend::INFO, x, ##__VA_ARGS__) #ifdef APP_DEBUG -# define LOG_DEBUG(x, ...) Log::i()->message(ILogBackend::DEBUG, x, ##__VA_ARGS__) +# define LOG_DEBUG(x, ...) xmrig::Log::i()->message(xmrig::ILogBackend::DEBUG, x, ##__VA_ARGS__) #else # define LOG_DEBUG(x, ...) #endif #if defined(APP_DEBUG) || defined(APP_DEVEL) -# define LOG_DEBUG_ERR(x, ...) Log::i()->message(ILogBackend::ERR, x, ##__VA_ARGS__) -# define LOG_DEBUG_WARN(x, ...) Log::i()->message(ILogBackend::WARNING, x, ##__VA_ARGS__) +# define LOG_DEBUG_ERR(x, ...) xmrig::Log::i()->message(xmrig::ILogBackend::ERR, x, ##__VA_ARGS__) +# define LOG_DEBUG_WARN(x, ...) xmrig::Log::i()->message(xmrig::ILogBackend::WARNING, x, ##__VA_ARGS__) #else # define LOG_DEBUG_ERR(x, ...) # define LOG_DEBUG_WARN(x, ...) diff --git a/src/common/log/SysLog.cpp b/src/common/log/SysLog.cpp index bcb96394..1a8ffbd2 100644 --- a/src/common/log/SysLog.cpp +++ b/src/common/log/SysLog.cpp @@ -5,7 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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,19 +30,19 @@ #include "version.h" -SysLog::SysLog() +xmrig::SysLog::SysLog() { openlog(APP_ID, LOG_PID, LOG_USER); } -void SysLog::message(Level level, const char *fmt, va_list args) +void xmrig::SysLog::message(Level level, const char *fmt, va_list args) { vsyslog(static_cast(level), fmt, args); } -void SysLog::text(const char *fmt, va_list args) +void xmrig::SysLog::text(const char *fmt, va_list args) { vsyslog(LOG_INFO, fmt, args); } diff --git a/src/common/log/SysLog.h b/src/common/log/SysLog.h index 5cfeefcd..3e64cf8b 100644 --- a/src/common/log/SysLog.h +++ b/src/common/log/SysLog.h @@ -5,7 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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,13 +22,16 @@ * along with this program. If not, see . */ -#ifndef __SYSLOG_H__ -#define __SYSLOG_H__ +#ifndef XMRIG_SYSLOG_H +#define XMRIG_SYSLOG_H #include "common/interfaces/ILogBackend.h" +namespace xmrig { + + class SysLog : public ILogBackend { public: @@ -37,4 +41,8 @@ public: void text(const char *fmt, va_list args) override; }; -#endif /* __SYSLOG_BACKEND_H__ */ + +} /* namespace xmrig */ + + +#endif /* XMRIG_SYSLOG_H */ diff --git a/src/core/Controller.cpp b/src/core/Controller.cpp index 7639bfaa..cfd132dd 100644 --- a/src/core/Controller.cpp +++ b/src/core/Controller.cpp @@ -75,8 +75,6 @@ xmrig::Controller::Controller(Process *process) xmrig::Controller::~Controller() { - ConfigLoader::release(); - delete d_ptr; } @@ -109,11 +107,11 @@ int xmrig::Controller::init() Platform::setProcessPriority(d_ptr->config->priority()); if (!config()->isBackground()) { - Log::add(new ConsoleLog(this)); + Log::add(new ConsoleLog()); } if (config()->logFile()) { - Log::add(new FileLog(this, config()->logFile())); + Log::add(new FileLog(config()->logFile())); } # ifdef HAVE_SYSLOG_H @@ -166,3 +164,12 @@ void xmrig::Controller::onNewConfig(IConfig *config) delete previousConfig; } + + +void xmrig::Controller::stop() +{ + ConfigLoader::release(); + + delete d_ptr->network; + d_ptr->network = nullptr; +} diff --git a/src/core/Controller.h b/src/core/Controller.h index 8d2f2674..7c74b53c 100644 --- a/src/core/Controller.h +++ b/src/core/Controller.h @@ -54,6 +54,7 @@ public: Network *network() const; void addListener(IControllerListener *listener); void save(); + void stop(); protected: void onNewConfig(IConfig *config) override; diff --git a/src/net/Network.cpp b/src/net/Network.cpp index b8f4f05e..efadb365 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -35,6 +35,7 @@ #include "base/net/stratum/Client.h" #include "base/net/stratum/SubmitResult.h" #include "base/tools/Chrono.h" +#include "base/tools/Handle.h" #include "common/log/Log.h" #include "core/Config.h" #include "core/Controller.h" @@ -44,7 +45,8 @@ xmrig::Network::Network(Controller *controller) : - m_donate(nullptr) + m_donate(nullptr), + m_timer(nullptr) { Workers::setListener(this); controller->addListener(this); @@ -56,15 +58,21 @@ xmrig::Network::Network(Controller *controller) : m_donate = new DonateStrategy(controller->config()->donateLevel(), pools.data().front().user(), controller->config()->algorithm().algo(), this); } - m_timer.data = this; - uv_timer_init(uv_default_loop(), &m_timer); - - uv_timer_start(&m_timer, Network::onTick, kTickInterval, kTickInterval); + m_timer = new uv_timer_t; + m_timer->data = this; + uv_timer_init(uv_default_loop(), m_timer); + uv_timer_start(m_timer, Network::onTick, kTickInterval, kTickInterval); } xmrig::Network::~Network() { + Handle::close(m_timer); + + if (m_donate) { + delete m_donate; + } + delete m_strategy; } @@ -75,16 +83,6 @@ void xmrig::Network::connect() } -void xmrig::Network::stop() -{ - if (m_donate) { - m_donate->stop(); - } - - m_strategy->stop(); -} - - void xmrig::Network::onActive(IStrategy *strategy, Client *client) { if (m_donate && m_donate == strategy) { diff --git a/src/net/Network.h b/src/net/Network.h index 8796045a..e83e60e2 100644 --- a/src/net/Network.h +++ b/src/net/Network.h @@ -50,7 +50,6 @@ public: ~Network() override; void connect(); - void stop(); protected: void onActive(IStrategy *strategy, Client *client) override; @@ -72,7 +71,7 @@ private: IStrategy *m_donate; IStrategy *m_strategy; NetworkState m_state; - uv_timer_t m_timer; + uv_timer_t *m_timer; }; diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index ba544c4b..7d08fb33 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -28,6 +28,7 @@ #include "base/net/stratum/strategies/FailoverStrategy.h" #include "base/net/stratum/strategies/SinglePoolStrategy.h" #include "base/tools/Buffer.h" +#include "base/tools/Handle.h" #include "common/crypto/keccak.h" #include "common/Platform.h" #include "common/xmrig.h" @@ -71,8 +72,9 @@ xmrig::DonateStrategy::DonateStrategy(int level, const char *user, Algo algo, IS m_strategy = new SinglePoolStrategy(m_pools.front(), 1, 2, this, true); } - m_timer.data = this; - uv_timer_init(uv_default_loop(), &m_timer); + m_timer = new uv_timer_t; + m_timer->data = this; + uv_timer_init(uv_default_loop(), m_timer); idle(m_idleTime * randomf(0.5, 1.5)); } @@ -80,6 +82,7 @@ xmrig::DonateStrategy::DonateStrategy(int level, const char *user, Algo algo, IS xmrig::DonateStrategy::~DonateStrategy() { + Handle::close(m_timer); delete m_strategy; } @@ -104,7 +107,7 @@ void xmrig::DonateStrategy::setAlgo(const xmrig::Algorithm &algo) void xmrig::DonateStrategy::stop() { - uv_timer_stop(&m_timer); + uv_timer_stop(m_timer); m_strategy->stop(); } @@ -125,7 +128,7 @@ void xmrig::DonateStrategy::tick(uint64_t now) void xmrig::DonateStrategy::onActive(IStrategy *strategy, Client *client) { if (!isActive()) { - uv_timer_start(&m_timer, DonateStrategy::onTimer, m_donateTime, 0); + uv_timer_start(m_timer, DonateStrategy::onTimer, m_donateTime, 0); } m_active = true; @@ -154,7 +157,7 @@ void xmrig::DonateStrategy::onResultAccepted(IStrategy *strategy, Client *client void xmrig::DonateStrategy::idle(uint64_t timeout) { - uv_timer_start(&m_timer, DonateStrategy::onTimer, timeout, 0); + uv_timer_start(m_timer, DonateStrategy::onTimer, timeout, 0); } diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h index 8c350097..ab898332 100644 --- a/src/net/strategies/DonateStrategy.h +++ b/src/net/strategies/DonateStrategy.h @@ -79,7 +79,7 @@ private: std::vector m_pools; uint64_t m_now; uint64_t m_stop; - uv_timer_t m_timer; + uv_timer_t *m_timer; }; diff --git a/src/workers/Hashrate.cpp b/src/workers/Hashrate.cpp index 2a750318..591b719a 100644 --- a/src/workers/Hashrate.cpp +++ b/src/workers/Hashrate.cpp @@ -5,7 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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 +#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; } diff --git a/src/workers/Hashrate.h b/src/workers/Hashrate.h index e766f117..673c6a51 100644 --- a/src/workers/Hashrate.h +++ b/src/workers/Hashrate.h @@ -5,7 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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 . */ -#ifndef __HASHRATE_H__ -#define __HASHRATE_H__ +#ifndef XMRIG_HASHRATE_H +#define XMRIG_HASHRATE_H #include @@ -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 */ diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index 02eec378..b3c4882b 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -35,7 +35,7 @@ template -MultiWorker::MultiWorker(Handle *handle) +MultiWorker::MultiWorker(ThreadHandle *handle) : Worker(handle) { m_memory = Mem::create(m_ctx, m_thread->algorithm(), N); diff --git a/src/workers/MultiWorker.h b/src/workers/MultiWorker.h index 525eee18..99d37e44 100644 --- a/src/workers/MultiWorker.h +++ b/src/workers/MultiWorker.h @@ -40,7 +40,7 @@ template class MultiWorker : public Worker { public: - MultiWorker(Handle *handle); + MultiWorker(ThreadHandle *handle); ~MultiWorker(); protected: diff --git a/src/workers/Handle.cpp b/src/workers/ThreadHandle.cpp similarity index 79% rename from src/workers/Handle.cpp rename to src/workers/ThreadHandle.cpp index d42ea368..43ff950c 100644 --- a/src/workers/Handle.cpp +++ b/src/workers/ThreadHandle.cpp @@ -5,7 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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); } diff --git a/src/workers/Handle.h b/src/workers/ThreadHandle.h similarity index 85% rename from src/workers/Handle.h rename to src/workers/ThreadHandle.h index 4bb899f9..f3e09ce5 100644 --- a/src/workers/Handle.h +++ b/src/workers/ThreadHandle.h @@ -5,7 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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 . */ -#ifndef __HANDLE_H__ -#define __HANDLE_H__ +#ifndef XMRIG_THREADHANDLE_H +#define XMRIG_THREADHANDLE_H #include @@ -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 */ diff --git a/src/workers/Worker.cpp b/src/workers/Worker.cpp index c569908c..234e7bfd 100644 --- a/src/workers/Worker.cpp +++ b/src/workers/Worker.cpp @@ -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()), diff --git a/src/workers/Worker.h b/src/workers/Worker.h index 73e25033..4710bcc5 100644 --- a/src/workers/Worker.h +++ b/src/workers/Worker.h @@ -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; } diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index f718a52c..5426762e 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -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 Workers::m_paused; std::atomic Workers::m_sequence; std::list Workers::m_queue; -std::vector Workers::m_workers; +std::vector 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(&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(arg); + auto handle = static_cast(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; } diff --git a/src/workers/Workers.h b/src/workers/Workers.h index 0760c98f..23b0a27c 100644 --- a/src/workers/Workers.h +++ b/src/workers/Workers.h @@ -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 m_paused; static std::atomic m_sequence; static std::list m_queue; - static std::vector m_workers; + static std::vector 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; };