Optimized initialization.
This commit is contained in:
parent
9da0cb2ad1
commit
2e49930b94
10 changed files with 98 additions and 92 deletions
33
src/App.cpp
33
src/App.cpp
|
@ -24,7 +24,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <cstdlib>
|
||||||
#include <uv.h>
|
#include <uv.h>
|
||||||
|
|
||||||
|
|
||||||
|
@ -42,18 +42,9 @@
|
||||||
#include "version.h"
|
#include "version.h"
|
||||||
|
|
||||||
|
|
||||||
xmrig::App::App(Process *process) :
|
xmrig::App::App(Process *process)
|
||||||
m_console(nullptr),
|
|
||||||
m_signals(nullptr)
|
|
||||||
{
|
{
|
||||||
m_controller = new Controller(process);
|
m_controller = new Controller(process);
|
||||||
if (m_controller->init() != 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!m_controller->config()->isBackground()) {
|
|
||||||
m_console = new Console(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -68,12 +59,26 @@ xmrig::App::~App()
|
||||||
int xmrig::App::exec()
|
int xmrig::App::exec()
|
||||||
{
|
{
|
||||||
if (!m_controller->isReady()) {
|
if (!m_controller->isReady()) {
|
||||||
|
LOG_EMERG("no valid configuration found.");
|
||||||
|
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_signals = new Signals(this);
|
m_signals = new Signals(this);
|
||||||
|
|
||||||
background();
|
int rc = 0;
|
||||||
|
if (background(rc)) {
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = m_controller->init();
|
||||||
|
if (rc != 0) {
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!m_controller->isBackground()) {
|
||||||
|
m_console = new Console(this);
|
||||||
|
}
|
||||||
|
|
||||||
VirtualMemory::init(m_controller->config()->cpu().isHugePages());
|
VirtualMemory::init(m_controller->config()->cpu().isHugePages());
|
||||||
|
|
||||||
|
@ -87,10 +92,10 @@ int xmrig::App::exec()
|
||||||
|
|
||||||
m_controller->start();
|
m_controller->start();
|
||||||
|
|
||||||
const int r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
|
rc = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
|
||||||
uv_loop_close(uv_default_loop());
|
uv_loop_close(uv_default_loop());
|
||||||
|
|
||||||
return r;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
11
src/App.h
11
src/App.h
|
@ -29,6 +29,7 @@
|
||||||
|
|
||||||
#include "base/kernel/interfaces/IConsoleListener.h"
|
#include "base/kernel/interfaces/IConsoleListener.h"
|
||||||
#include "base/kernel/interfaces/ISignalListener.h"
|
#include "base/kernel/interfaces/ISignalListener.h"
|
||||||
|
#include "base/tools/Object.h"
|
||||||
|
|
||||||
|
|
||||||
namespace xmrig {
|
namespace xmrig {
|
||||||
|
@ -44,6 +45,8 @@ class Signals;
|
||||||
class App : public IConsoleListener, public ISignalListener
|
class App : public IConsoleListener, public ISignalListener
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
XMRIG_DISABLE_COPY_MOVE_DEFAULT(App)
|
||||||
|
|
||||||
App(Process *process);
|
App(Process *process);
|
||||||
~App() override;
|
~App() override;
|
||||||
|
|
||||||
|
@ -54,12 +57,12 @@ protected:
|
||||||
void onSignal(int signum) override;
|
void onSignal(int signum) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void background();
|
bool background(int &rc);
|
||||||
void close();
|
void close();
|
||||||
|
|
||||||
Console *m_console;
|
Console *m_console = nullptr;
|
||||||
Controller *m_controller;
|
Controller *m_controller = nullptr;
|
||||||
Signals *m_signals;
|
Signals *m_signals = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -23,33 +23,36 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <cstdlib>
|
||||||
#include <signal.h>
|
#include <csignal>
|
||||||
#include <errno.h>
|
#include <cerrno>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
|
||||||
#include "App.h"
|
#include "App.h"
|
||||||
#include "base/io/log/Log.h"
|
#include "base/io/log/Log.h"
|
||||||
#include "core/config/Config.h"
|
|
||||||
#include "core/Controller.h"
|
#include "core/Controller.h"
|
||||||
|
|
||||||
|
|
||||||
void xmrig::App::background()
|
bool xmrig::App::background(int &rc)
|
||||||
{
|
{
|
||||||
signal(SIGPIPE, SIG_IGN);
|
signal(SIGPIPE, SIG_IGN);
|
||||||
|
|
||||||
if (!m_controller->config()->isBackground()) {
|
if (!m_controller->isBackground()) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int i = fork();
|
int i = fork();
|
||||||
if (i < 0) {
|
if (i < 0) {
|
||||||
exit(1);
|
rc = 1;
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
exit(0);
|
rc = 0;
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
i = setsid();
|
i = setsid();
|
||||||
|
@ -62,4 +65,6 @@ void xmrig::App::background()
|
||||||
if (i < 0) {
|
if (i < 0) {
|
||||||
LOG_ERR("chdir() failed (errno = %d)", errno);
|
LOG_ERR("chdir() failed (errno = %d)", errno);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,13 +29,12 @@
|
||||||
|
|
||||||
#include "App.h"
|
#include "App.h"
|
||||||
#include "core/Controller.h"
|
#include "core/Controller.h"
|
||||||
#include "core/config/Config.h"
|
|
||||||
|
|
||||||
|
|
||||||
void xmrig::App::background()
|
bool xmrig::App::background(int &)
|
||||||
{
|
{
|
||||||
if (!m_controller->config()->isBackground()) {
|
if (!m_controller->isBackground()) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
HWND hcon = GetConsoleWindow();
|
HWND hcon = GetConsoleWindow();
|
||||||
|
@ -46,4 +45,6 @@ void xmrig::App::background()
|
||||||
CloseHandle(h);
|
CloseHandle(h);
|
||||||
FreeConsole();
|
FreeConsole();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <assert.h>
|
#include <cassert>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
|
||||||
|
@ -64,15 +64,16 @@ static const char *kConfigPathV2 = "/2/config";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
class xmrig::BasePrivate
|
namespace xmrig {
|
||||||
|
|
||||||
|
|
||||||
|
class BasePrivate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
inline BasePrivate(Process *process) :
|
XMRIG_DISABLE_COPY_MOVE_DEFAULT(BasePrivate)
|
||||||
api(nullptr),
|
|
||||||
config(nullptr),
|
|
||||||
process(process),
|
inline BasePrivate(Process *process) : config(load(process)) {}
|
||||||
watcher(nullptr)
|
|
||||||
{}
|
|
||||||
|
|
||||||
|
|
||||||
inline ~BasePrivate()
|
inline ~BasePrivate()
|
||||||
|
@ -94,13 +95,33 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline Config *load()
|
inline void replace(Config *newConfig)
|
||||||
|
{
|
||||||
|
Config *previousConfig = config;
|
||||||
|
config = newConfig;
|
||||||
|
|
||||||
|
for (IBaseListener *listener : listeners) {
|
||||||
|
listener->onConfigChanged(config, previousConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
delete previousConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Api *api = nullptr;
|
||||||
|
Config *config = nullptr;
|
||||||
|
std::vector<IBaseListener *> listeners;
|
||||||
|
Watcher *watcher = nullptr;
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
inline Config *load(Process *process)
|
||||||
{
|
{
|
||||||
JsonChain chain;
|
JsonChain chain;
|
||||||
ConfigTransform transform;
|
ConfigTransform transform;
|
||||||
std::unique_ptr<Config> config;
|
std::unique_ptr<Config> config;
|
||||||
|
|
||||||
transform.load(chain, process, transform);
|
ConfigTransform::load(chain, process, transform);
|
||||||
|
|
||||||
if (read(chain, config)) {
|
if (read(chain, config)) {
|
||||||
return config.release();
|
return config.release();
|
||||||
|
@ -122,29 +143,12 @@ public:
|
||||||
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline void replace(Config *newConfig)
|
|
||||||
{
|
|
||||||
Config *previousConfig = config;
|
|
||||||
config = newConfig;
|
|
||||||
|
|
||||||
for (IBaseListener *listener : listeners) {
|
|
||||||
listener->onConfigChanged(config, previousConfig);
|
|
||||||
}
|
|
||||||
|
|
||||||
delete previousConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Api *api;
|
|
||||||
Config *config;
|
|
||||||
Process *process;
|
|
||||||
std::vector<IBaseListener *> listeners;
|
|
||||||
Watcher *watcher;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace xmrig
|
||||||
|
|
||||||
|
|
||||||
xmrig::Base::Base(Process *process)
|
xmrig::Base::Base(Process *process)
|
||||||
: d_ptr(new BasePrivate(process))
|
: d_ptr(new BasePrivate(process))
|
||||||
{
|
{
|
||||||
|
@ -165,14 +169,6 @@ bool xmrig::Base::isReady() const
|
||||||
|
|
||||||
int xmrig::Base::init()
|
int xmrig::Base::init()
|
||||||
{
|
{
|
||||||
d_ptr->config = d_ptr->load();
|
|
||||||
|
|
||||||
if (!d_ptr->config) {
|
|
||||||
LOG_EMERG("No valid configuration found. Exiting.");
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
# ifdef XMRIG_FEATURE_API
|
# ifdef XMRIG_FEATURE_API
|
||||||
d_ptr->api = new Api(this);
|
d_ptr->api = new Api(this);
|
||||||
d_ptr->api->addListener(this);
|
d_ptr->api->addListener(this);
|
||||||
|
@ -184,7 +180,7 @@ int xmrig::Base::init()
|
||||||
Platform::setProcessPriority(config()->cpu().priority());
|
Platform::setProcessPriority(config()->cpu().priority());
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
if (config()->isBackground()) {
|
if (isBackground()) {
|
||||||
Log::background = true;
|
Log::background = true;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -240,6 +236,12 @@ xmrig::Api *xmrig::Base::api() const
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool xmrig::Base::isBackground() const
|
||||||
|
{
|
||||||
|
return d_ptr->config && d_ptr->config->isBackground();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool xmrig::Base::reload(const rapidjson::Value &json)
|
bool xmrig::Base::reload(const rapidjson::Value &json)
|
||||||
{
|
{
|
||||||
JsonReader reader(json);
|
JsonReader reader(json);
|
||||||
|
@ -247,7 +249,7 @@ bool xmrig::Base::reload(const rapidjson::Value &json)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Config *config = new Config();
|
auto config = new Config();
|
||||||
if (!config->read(reader, d_ptr->config->fileName())) {
|
if (!config->read(reader, d_ptr->config->fileName())) {
|
||||||
delete config;
|
delete config;
|
||||||
|
|
||||||
|
@ -289,7 +291,7 @@ void xmrig::Base::onFileChanged(const String &fileName)
|
||||||
JsonChain chain;
|
JsonChain chain;
|
||||||
chain.addFile(fileName);
|
chain.addFile(fileName);
|
||||||
|
|
||||||
Config *config = new Config();
|
auto config = new Config();
|
||||||
|
|
||||||
if (!config->read(chain, chain.fileName())) {
|
if (!config->read(chain, chain.fileName())) {
|
||||||
LOG_ERR("reloading failed");
|
LOG_ERR("reloading failed");
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
#include "base/api/interfaces/IApiListener.h"
|
#include "base/api/interfaces/IApiListener.h"
|
||||||
#include "base/kernel/interfaces/IConfigListener.h"
|
#include "base/kernel/interfaces/IConfigListener.h"
|
||||||
#include "base/kernel/interfaces/IWatcherListener.h"
|
#include "base/kernel/interfaces/IWatcherListener.h"
|
||||||
|
#include "base/tools/Object.h"
|
||||||
#include "rapidjson/fwd.h"
|
#include "rapidjson/fwd.h"
|
||||||
|
|
||||||
|
|
||||||
|
@ -45,6 +46,8 @@ class Process;
|
||||||
class Base : public IWatcherListener, public IApiListener
|
class Base : public IWatcherListener, public IApiListener
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
XMRIG_DISABLE_COPY_MOVE_DEFAULT(Base)
|
||||||
|
|
||||||
Base(Process *process);
|
Base(Process *process);
|
||||||
~Base() override;
|
~Base() override;
|
||||||
|
|
||||||
|
@ -54,6 +57,7 @@ public:
|
||||||
virtual void stop();
|
virtual void stop();
|
||||||
|
|
||||||
Api *api() const;
|
Api *api() const;
|
||||||
|
bool isBackground() const;
|
||||||
bool reload(const rapidjson::Value &json);
|
bool reload(const rapidjson::Value &json);
|
||||||
Config *config() const;
|
Config *config() const;
|
||||||
void addListener(IBaseListener *listener);
|
void addListener(IBaseListener *listener);
|
||||||
|
|
|
@ -23,8 +23,8 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <ctime>
|
||||||
#include <uv.h>
|
#include <uv.h>
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
|
|
||||||
#include "base/kernel/Process.h"
|
#include "base/kernel/Process.h"
|
||||||
|
@ -55,11 +55,6 @@ xmrig::Process::Process(int argc, char **argv) :
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
xmrig::Process::~Process()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
xmrig::String xmrig::Process::location(Location location, const char *fileName) const
|
xmrig::String xmrig::Process::location(Location location, const char *fileName) const
|
||||||
{
|
{
|
||||||
constexpr const size_t max = 520;
|
constexpr const size_t max = 520;
|
||||||
|
|
|
@ -47,7 +47,6 @@ public:
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
Process(int argc, char **argv);
|
Process(int argc, char **argv);
|
||||||
~Process();
|
|
||||||
|
|
||||||
String location(Location location, const char *fileName = nullptr) const;
|
String location(Location location, const char *fileName = nullptr) const;
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <assert.h>
|
#include <cassert>
|
||||||
|
|
||||||
|
|
||||||
#include "backend/cpu/Cpu.h"
|
#include "backend/cpu/Cpu.h"
|
||||||
|
@ -44,20 +44,10 @@ xmrig::Controller::~Controller()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool xmrig::Controller::isReady() const
|
|
||||||
{
|
|
||||||
return Base::isReady() && m_network;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int xmrig::Controller::init()
|
int xmrig::Controller::init()
|
||||||
{
|
{
|
||||||
Cpu::init();
|
Cpu::init();
|
||||||
|
Base::init();
|
||||||
const int rc = Base::init();
|
|
||||||
if (rc != 0) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_network = new Network(this);
|
m_network = new Network(this);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
|
|
||||||
|
|
||||||
#include "base/kernel/Base.h"
|
#include "base/kernel/Base.h"
|
||||||
|
#include "base/tools/Object.h"
|
||||||
|
|
||||||
|
|
||||||
namespace xmrig {
|
namespace xmrig {
|
||||||
|
@ -40,10 +41,11 @@ class Network;
|
||||||
class Controller : public Base
|
class Controller : public Base
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
XMRIG_DISABLE_COPY_MOVE_DEFAULT(Controller)
|
||||||
|
|
||||||
Controller(Process *process);
|
Controller(Process *process);
|
||||||
~Controller() override;
|
~Controller() override;
|
||||||
|
|
||||||
bool isReady() const override;
|
|
||||||
int init() override;
|
int init() override;
|
||||||
void start() override;
|
void start() override;
|
||||||
void stop() override;
|
void stop() override;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue