Moved CCClient to seperate thrad and process ControlCommands async
This commit is contained in:
parent
a5f81b8ae4
commit
b18842308f
6 changed files with 90 additions and 39 deletions
30
src/App.cpp
30
src/App.cpp
|
@ -25,6 +25,7 @@
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <uv.h>
|
#include <uv.h>
|
||||||
|
#include <cc/ControlCommand.h>
|
||||||
|
|
||||||
#include "api/Api.h"
|
#include "api/Api.h"
|
||||||
#include "App.h"
|
#include "App.h"
|
||||||
|
@ -94,7 +95,6 @@ App::App(int argc, char **argv) :
|
||||||
uv_signal_init(uv_default_loop(), &m_signal);
|
uv_signal_init(uv_default_loop(), &m_signal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
App::~App()
|
App::~App()
|
||||||
{
|
{
|
||||||
delete m_network;
|
delete m_network;
|
||||||
|
@ -148,7 +148,9 @@ int App::start()
|
||||||
|
|
||||||
# ifndef XMRIG_NO_CC
|
# ifndef XMRIG_NO_CC
|
||||||
if (m_options->ccHost() && m_options->ccPort() > 0) {
|
if (m_options->ccHost() && m_options->ccPort() > 0) {
|
||||||
m_ccclient = new CCClient(m_options);
|
uv_async_init(uv_default_loop(), &m_async, App::onCommandReceived);
|
||||||
|
|
||||||
|
m_ccclient = new CCClient(m_options, &m_async);
|
||||||
} else {
|
} else {
|
||||||
LOG_WARN("Please configure CC-Url and restart. CC feature is now deactivated.");
|
LOG_WARN("Please configure CC-Url and restart. CC feature is now deactivated.");
|
||||||
}
|
}
|
||||||
|
@ -219,7 +221,7 @@ void App::shutdown()
|
||||||
m_self->stop(false);
|
m_self->stop(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void App::onSignal(uv_signal_t *handle, int signum)
|
void App::onSignal(uv_signal_t* handle, int signum)
|
||||||
{
|
{
|
||||||
switch (signum)
|
switch (signum)
|
||||||
{
|
{
|
||||||
|
@ -242,3 +244,25 @@ void App::onSignal(uv_signal_t *handle, int signum)
|
||||||
uv_signal_stop(handle);
|
uv_signal_stop(handle);
|
||||||
App::shutdown();
|
App::shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void App::onCommandReceived(uv_async_t* async)
|
||||||
|
{
|
||||||
|
if (async->data) {
|
||||||
|
auto command = reinterpret_cast<ControlCommand::Command &> (async->data);
|
||||||
|
switch (command) {
|
||||||
|
case ControlCommand::START:
|
||||||
|
Workers::setEnabled(true);
|
||||||
|
break;
|
||||||
|
case ControlCommand::STOP:
|
||||||
|
Workers::setEnabled(false);
|
||||||
|
break;
|
||||||
|
case ControlCommand::UPDATE_CONFIG:;
|
||||||
|
case ControlCommand::RESTART:
|
||||||
|
App::restart();
|
||||||
|
break;
|
||||||
|
case ControlCommand::SHUTDOWN:
|
||||||
|
App::shutdown();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -55,7 +55,8 @@ private:
|
||||||
void background();
|
void background();
|
||||||
void stop(bool restart);
|
void stop(bool restart);
|
||||||
|
|
||||||
static void onSignal(uv_signal_t *handle, int signum);
|
static void onSignal(uv_signal_t* handle, int signum);
|
||||||
|
static void onCommandReceived(uv_async_t* handle);
|
||||||
|
|
||||||
static App *m_self;
|
static App *m_self;
|
||||||
|
|
||||||
|
@ -67,6 +68,7 @@ private:
|
||||||
Options *m_options;
|
Options *m_options;
|
||||||
CCClient *m_ccclient;
|
CCClient *m_ccclient;
|
||||||
uv_signal_t m_signal;
|
uv_signal_t m_signal;
|
||||||
|
uv_async_t m_async;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -48,8 +48,9 @@
|
||||||
CCClient *CCClient::m_self = nullptr;
|
CCClient *CCClient::m_self = nullptr;
|
||||||
uv_mutex_t CCClient::m_mutex;
|
uv_mutex_t CCClient::m_mutex;
|
||||||
|
|
||||||
CCClient::CCClient(const Options *options)
|
CCClient::CCClient(Options* options, uv_async_t* async)
|
||||||
: m_options(options)
|
: m_options(options),
|
||||||
|
m_async(async)
|
||||||
{
|
{
|
||||||
uv_mutex_init(&m_mutex);
|
uv_mutex_init(&m_mutex);
|
||||||
|
|
||||||
|
@ -88,10 +89,7 @@ CCClient::CCClient(const Options *options)
|
||||||
m_authorization = std::string("Bearer ") + m_self->m_options->ccToken();
|
m_authorization = std::string("Bearer ") + m_self->m_options->ccToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
uv_timer_init(uv_default_loop(), &m_timer);
|
uv_thread_create(&m_thread, CCClient::onThreadStarted, this);
|
||||||
uv_timer_start(&m_timer, CCClient::onReport,
|
|
||||||
static_cast<uint64_t>(m_options->ccUpdateInterval() * 1000),
|
|
||||||
static_cast<uint64_t>(m_options->ccUpdateInterval() * 1000));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CCClient::~CCClient()
|
CCClient::~CCClient()
|
||||||
|
@ -102,32 +100,33 @@ CCClient::~CCClient()
|
||||||
|
|
||||||
void CCClient::updateHashrate(const Hashrate *hashrate)
|
void CCClient::updateHashrate(const Hashrate *hashrate)
|
||||||
{
|
{
|
||||||
uv_mutex_lock(&m_mutex);
|
|
||||||
|
|
||||||
if (m_self) {
|
if (m_self) {
|
||||||
|
uv_mutex_lock(&m_mutex);
|
||||||
|
|
||||||
m_self->m_clientStatus.setHashrateShort(hashrate->calc(Hashrate::ShortInterval));
|
m_self->m_clientStatus.setHashrateShort(hashrate->calc(Hashrate::ShortInterval));
|
||||||
m_self->m_clientStatus.setHashrateMedium(hashrate->calc(Hashrate::MediumInterval));
|
m_self->m_clientStatus.setHashrateMedium(hashrate->calc(Hashrate::MediumInterval));
|
||||||
m_self->m_clientStatus.setHashrateLong(hashrate->calc(Hashrate::LargeInterval));
|
m_self->m_clientStatus.setHashrateLong(hashrate->calc(Hashrate::LargeInterval));
|
||||||
m_self->m_clientStatus.setHashrateHighest(hashrate->highest());
|
m_self->m_clientStatus.setHashrateHighest(hashrate->highest());
|
||||||
}
|
|
||||||
|
|
||||||
uv_mutex_unlock(&m_mutex);
|
uv_mutex_unlock(&m_mutex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void CCClient::updateNetworkState(const NetworkState &network)
|
void CCClient::updateNetworkState(const NetworkState &network)
|
||||||
{
|
{
|
||||||
uv_mutex_lock(&m_mutex);
|
|
||||||
|
|
||||||
if (m_self) {
|
if (m_self) {
|
||||||
|
uv_mutex_lock(&m_mutex);
|
||||||
|
|
||||||
m_self->m_clientStatus.setCurrentStatus(Workers::isEnabled() ? ClientStatus::RUNNING : ClientStatus::PAUSED);
|
m_self->m_clientStatus.setCurrentStatus(Workers::isEnabled() ? ClientStatus::RUNNING : ClientStatus::PAUSED);
|
||||||
m_self->m_clientStatus.setCurrentPool(network.pool);
|
m_self->m_clientStatus.setCurrentPool(network.pool);
|
||||||
m_self->m_clientStatus.setSharesGood(network.accepted);
|
m_self->m_clientStatus.setSharesGood(network.accepted);
|
||||||
m_self->m_clientStatus.setSharesTotal(network.accepted + network.rejected);
|
m_self->m_clientStatus.setSharesTotal(network.accepted + network.rejected);
|
||||||
m_self->m_clientStatus.setHashesTotal(network.total);
|
m_self->m_clientStatus.setHashesTotal(network.total);
|
||||||
m_self->m_clientStatus.setAvgTime(network.avgTime());
|
m_self->m_clientStatus.setAvgTime(network.avgTime());
|
||||||
|
|
||||||
|
uv_mutex_unlock(&m_mutex);
|
||||||
}
|
}
|
||||||
uv_mutex_unlock(&m_mutex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CCClient::publishClientStatusReport()
|
void CCClient::publishClientStatusReport()
|
||||||
|
@ -148,23 +147,22 @@ void CCClient::publishClientStatusReport()
|
||||||
if (controlCommand.getCommand() == ControlCommand::START) {
|
if (controlCommand.getCommand() == ControlCommand::START) {
|
||||||
if (!Workers::isEnabled()) {
|
if (!Workers::isEnabled()) {
|
||||||
LOG_WARN("[CC-Client] Command: START received -> resume");
|
LOG_WARN("[CC-Client] Command: START received -> resume");
|
||||||
Workers::setEnabled(true);
|
|
||||||
}
|
}
|
||||||
} else if (controlCommand.getCommand() == ControlCommand::STOP) {
|
} else if (controlCommand.getCommand() == ControlCommand::STOP) {
|
||||||
if (Workers::isEnabled()) {
|
if (Workers::isEnabled()) {
|
||||||
LOG_WARN("[CC-Client] Command: STOP received -> pause");
|
LOG_WARN("[CC-Client] Command: STOP received -> pause");
|
||||||
Workers::setEnabled(false);
|
|
||||||
}
|
}
|
||||||
} else if (controlCommand.getCommand() == ControlCommand::UPDATE_CONFIG) {
|
} else if (controlCommand.getCommand() == ControlCommand::UPDATE_CONFIG) {
|
||||||
LOG_WARN("[CC-Client] Command: UPDATE_CONFIG received -> update config");
|
LOG_WARN("[CC-Client] Command: UPDATE_CONFIG received -> update config");
|
||||||
updateConfig();
|
updateConfig();
|
||||||
} else if (controlCommand.getCommand() == ControlCommand::RESTART) {
|
} else if (controlCommand.getCommand() == ControlCommand::RESTART) {
|
||||||
LOG_WARN("[CC-Client] Command: RESTART received -> restart");
|
LOG_WARN("[CC-Client] Command: RESTART received -> restart");
|
||||||
App::restart();
|
|
||||||
} else if (controlCommand.getCommand() == ControlCommand::SHUTDOWN) {
|
} else if (controlCommand.getCommand() == ControlCommand::SHUTDOWN) {
|
||||||
LOG_WARN("[CC-Client] Command: SHUTDOWN received -> shutdown");
|
LOG_WARN("[CC-Client] Command: SHUTDOWN received -> shutdown");
|
||||||
App::shutdown();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_self->m_async->data = reinterpret_cast<void *>(controlCommand.getCommand());
|
||||||
|
uv_async_send(m_self->m_async);
|
||||||
} else {
|
} else {
|
||||||
LOG_ERR("[CC-Client] Unknown command received from CC Server.");
|
LOG_ERR("[CC-Client] Unknown command received from CC Server.");
|
||||||
}
|
}
|
||||||
|
@ -196,8 +194,7 @@ void CCClient::updateConfig()
|
||||||
clientConfigFile << buffer.GetString();
|
clientConfigFile << buffer.GetString();
|
||||||
clientConfigFile.close();
|
clientConfigFile.close();
|
||||||
|
|
||||||
LOG_WARN("[CC-Client] Config updated. -> restart");
|
LOG_WARN("[CC-Client] Config updated. -> trigger restart");
|
||||||
App::restart();
|
|
||||||
} else {
|
} else {
|
||||||
LOG_ERR("[CC-Client] Not able to store client config to file %s.", m_self->m_options->configFile());
|
LOG_ERR("[CC-Client] Not able to store client config to file %s.", m_self->m_options->configFile());
|
||||||
}
|
}
|
||||||
|
@ -214,8 +211,8 @@ std::shared_ptr<httplib::Response> CCClient::performRequest(const std::string& r
|
||||||
httplib::Client cli(m_self->m_options->ccHost(), m_self->m_options->ccPort());
|
httplib::Client cli(m_self->m_options->ccHost(), m_self->m_options->ccPort());
|
||||||
|
|
||||||
httplib::Request req;
|
httplib::Request req;
|
||||||
req.method = operation.c_str();
|
req.method = operation;
|
||||||
req.path = requestUrl.c_str();
|
req.path = requestUrl;
|
||||||
req.set_header("Host", "");
|
req.set_header("Host", "");
|
||||||
req.set_header("Accept", "*/*");
|
req.set_header("Accept", "*/*");
|
||||||
req.set_header("User-Agent", Platform::userAgent());
|
req.set_header("User-Agent", Platform::userAgent());
|
||||||
|
@ -227,7 +224,7 @@ std::shared_ptr<httplib::Response> CCClient::performRequest(const std::string& r
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!requestBuffer.empty()) {
|
if (!requestBuffer.empty()) {
|
||||||
req.body = requestBuffer.c_str();
|
req.body = requestBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto res = std::make_shared<httplib::Response>();
|
auto res = std::make_shared<httplib::Response>();
|
||||||
|
@ -235,6 +232,18 @@ std::shared_ptr<httplib::Response> CCClient::performRequest(const std::string& r
|
||||||
return cli.send(req, *res) ? res : nullptr;
|
return cli.send(req, *res) ? res : nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CCClient::onThreadStarted(void* handle)
|
||||||
|
{
|
||||||
|
uv_loop_init(&m_self->m_client_loop);
|
||||||
|
|
||||||
|
uv_timer_init(&m_self->m_client_loop, &m_self->m_timer);
|
||||||
|
uv_timer_start(&m_self->m_timer, CCClient::onReport,
|
||||||
|
static_cast<uint64_t>(m_self->m_options->ccUpdateInterval() * 1000),
|
||||||
|
static_cast<uint64_t>(m_self->m_options->ccUpdateInterval() * 1000));
|
||||||
|
|
||||||
|
uv_run(&m_self->m_client_loop, UV_RUN_DEFAULT);
|
||||||
|
}
|
||||||
|
|
||||||
void CCClient::onReport(uv_timer_t *handle)
|
void CCClient::onReport(uv_timer_t *handle)
|
||||||
{
|
{
|
||||||
if (m_self) {
|
if (m_self) {
|
||||||
|
|
|
@ -38,7 +38,7 @@ class NetworkState;
|
||||||
class CCClient
|
class CCClient
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CCClient(const Options *options);
|
CCClient(Options *options, uv_async_t* async);
|
||||||
~CCClient();
|
~CCClient();
|
||||||
|
|
||||||
static void updateHashrate(const Hashrate *hashrate);
|
static void updateHashrate(const Hashrate *hashrate);
|
||||||
|
@ -52,9 +52,10 @@ private:
|
||||||
const std::string& requestBuffer,
|
const std::string& requestBuffer,
|
||||||
const std::string& operation);
|
const std::string& operation);
|
||||||
|
|
||||||
|
static void onThreadStarted(void *handle);
|
||||||
static void onReport(uv_timer_t *handle);
|
static void onReport(uv_timer_t *handle);
|
||||||
|
|
||||||
const Options *m_options;
|
const Options* m_options;
|
||||||
|
|
||||||
static CCClient* m_self;
|
static CCClient* m_self;
|
||||||
static uv_mutex_t m_mutex;
|
static uv_mutex_t m_mutex;
|
||||||
|
@ -63,7 +64,10 @@ private:
|
||||||
|
|
||||||
std::string m_authorization;
|
std::string m_authorization;
|
||||||
|
|
||||||
|
uv_async_t* m_async;
|
||||||
uv_timer_t m_timer;
|
uv_timer_t m_timer;
|
||||||
|
uv_loop_t m_client_loop;
|
||||||
|
uv_thread_t m_thread;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -32,12 +32,24 @@
|
||||||
#include "ClientStatus.h"
|
#include "ClientStatus.h"
|
||||||
|
|
||||||
ClientStatus::ClientStatus()
|
ClientStatus::ClientStatus()
|
||||||
: m_hashrateShort(0),
|
: m_currentStatus(Status::PAUSED),
|
||||||
|
m_hasHugepages(false),
|
||||||
|
m_isHugepagesEnabled(false),
|
||||||
|
m_isDoubleHashMode(false),
|
||||||
|
m_isCpuX64(false),
|
||||||
|
m_hasCpuAES(false),
|
||||||
|
m_hashrateShort(0),
|
||||||
m_hashrateMedium(0),
|
m_hashrateMedium(0),
|
||||||
m_hashrateLong(0),
|
m_hashrateLong(0),
|
||||||
|
m_hashrateHighest(0),
|
||||||
|
m_currentThreads(0),
|
||||||
|
m_cpuCores(0),
|
||||||
|
m_cpuL2(0),
|
||||||
|
m_cpuL3(0),
|
||||||
m_sharesGood(0),
|
m_sharesGood(0),
|
||||||
m_sharesTotal(0),
|
m_sharesTotal(0),
|
||||||
m_hashesTotal(0),
|
m_hashesTotal(0),
|
||||||
|
m_avgTime(0),
|
||||||
m_lastStatusUpdate(0)
|
m_lastStatusUpdate(0)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,14 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "rapidjson/document.h"
|
#include "rapidjson/document.h"
|
||||||
|
|
||||||
|
static const char* command_str[5] = {
|
||||||
|
"START",
|
||||||
|
"STOP",
|
||||||
|
"UPDATE_CONFIG",
|
||||||
|
"RESTART",
|
||||||
|
"SHUTDOWN"
|
||||||
|
};
|
||||||
|
|
||||||
class ControlCommand
|
class ControlCommand
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -43,12 +51,12 @@ public:
|
||||||
ControlCommand();
|
ControlCommand();
|
||||||
explicit ControlCommand(Command command);
|
explicit ControlCommand(Command command);
|
||||||
|
|
||||||
inline const char *toString (Command command)
|
static inline const char *toString (Command command)
|
||||||
{
|
{
|
||||||
return command_str[static_cast<int>(command)];
|
return command_str[static_cast<int>(command)];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Command toCommand (const char *command)
|
static inline Command toCommand (const char *command)
|
||||||
{
|
{
|
||||||
const int n = sizeof(command_str) / sizeof(command_str[0]);
|
const int n = sizeof(command_str) / sizeof(command_str[0]);
|
||||||
for (int i = 0; i < n; ++i)
|
for (int i = 0; i < n; ++i)
|
||||||
|
@ -69,14 +77,6 @@ public:
|
||||||
bool isOneTimeCommand() const;
|
bool isOneTimeCommand() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const char* command_str[5] = {
|
|
||||||
"START",
|
|
||||||
"STOP",
|
|
||||||
"UPDATE_CONFIG",
|
|
||||||
"RESTART",
|
|
||||||
"SHUTDOWN"
|
|
||||||
};
|
|
||||||
|
|
||||||
Command m_command;
|
Command m_command;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue