Optimized initialization.

This commit is contained in:
XMRig 2019-09-21 19:26:27 +07:00
parent 9da0cb2ad1
commit 2e49930b94
10 changed files with 98 additions and 92 deletions

View file

@ -24,7 +24,7 @@
*/
#include <stdlib.h>
#include <cstdlib>
#include <uv.h>
@ -42,18 +42,9 @@
#include "version.h"
xmrig::App::App(Process *process) :
m_console(nullptr),
m_signals(nullptr)
xmrig::App::App(Process *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()
{
if (!m_controller->isReady()) {
LOG_EMERG("no valid configuration found.");
return 2;
}
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());
@ -87,10 +92,10 @@ int xmrig::App::exec()
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());
return r;
return rc;
}

View file

@ -29,6 +29,7 @@
#include "base/kernel/interfaces/IConsoleListener.h"
#include "base/kernel/interfaces/ISignalListener.h"
#include "base/tools/Object.h"
namespace xmrig {
@ -44,6 +45,8 @@ class Signals;
class App : public IConsoleListener, public ISignalListener
{
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(App)
App(Process *process);
~App() override;
@ -54,12 +57,12 @@ protected:
void onSignal(int signum) override;
private:
void background();
bool background(int &rc);
void close();
Console *m_console;
Controller *m_controller;
Signals *m_signals;
Console *m_console = nullptr;
Controller *m_controller = nullptr;
Signals *m_signals = nullptr;
};

View file

@ -23,33 +23,36 @@
*/
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <cstdlib>
#include <csignal>
#include <cerrno>
#include <unistd.h>
#include "App.h"
#include "base/io/log/Log.h"
#include "core/config/Config.h"
#include "core/Controller.h"
void xmrig::App::background()
bool xmrig::App::background(int &rc)
{
signal(SIGPIPE, SIG_IGN);
if (!m_controller->config()->isBackground()) {
return;
if (!m_controller->isBackground()) {
return false;
}
int i = fork();
if (i < 0) {
exit(1);
rc = 1;
return true;
}
if (i > 0) {
exit(0);
rc = 0;
return true;
}
i = setsid();
@ -62,4 +65,6 @@ void xmrig::App::background()
if (i < 0) {
LOG_ERR("chdir() failed (errno = %d)", errno);
}
return false;
}

View file

@ -29,13 +29,12 @@
#include "App.h"
#include "core/Controller.h"
#include "core/config/Config.h"
void xmrig::App::background()
bool xmrig::App::background(int &)
{
if (!m_controller->config()->isBackground()) {
return;
if (!m_controller->isBackground()) {
return false;
}
HWND hcon = GetConsoleWindow();
@ -46,4 +45,6 @@ void xmrig::App::background()
CloseHandle(h);
FreeConsole();
}
return false;
}

View file

@ -23,7 +23,7 @@
*/
#include <assert.h>
#include <cassert>
#include <memory>
@ -64,15 +64,16 @@ static const char *kConfigPathV2 = "/2/config";
#endif
class xmrig::BasePrivate
namespace xmrig {
class BasePrivate
{
public:
inline BasePrivate(Process *process) :
api(nullptr),
config(nullptr),
process(process),
watcher(nullptr)
{}
XMRIG_DISABLE_COPY_MOVE_DEFAULT(BasePrivate)
inline BasePrivate(Process *process) : config(load(process)) {}
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;
ConfigTransform transform;
std::unique_ptr<Config> config;
transform.load(chain, process, transform);
ConfigTransform::load(chain, process, transform);
if (read(chain, config)) {
return config.release();
@ -122,29 +143,12 @@ public:
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)
: d_ptr(new BasePrivate(process))
{
@ -165,14 +169,6 @@ bool xmrig::Base::isReady() const
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
d_ptr->api = new Api(this);
d_ptr->api->addListener(this);
@ -184,7 +180,7 @@ int xmrig::Base::init()
Platform::setProcessPriority(config()->cpu().priority());
# endif
if (config()->isBackground()) {
if (isBackground()) {
Log::background = true;
}
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)
{
JsonReader reader(json);
@ -247,7 +249,7 @@ bool xmrig::Base::reload(const rapidjson::Value &json)
return false;
}
Config *config = new Config();
auto config = new Config();
if (!config->read(reader, d_ptr->config->fileName())) {
delete config;
@ -289,7 +291,7 @@ void xmrig::Base::onFileChanged(const String &fileName)
JsonChain chain;
chain.addFile(fileName);
Config *config = new Config();
auto config = new Config();
if (!config->read(chain, chain.fileName())) {
LOG_ERR("reloading failed");

View file

@ -29,6 +29,7 @@
#include "base/api/interfaces/IApiListener.h"
#include "base/kernel/interfaces/IConfigListener.h"
#include "base/kernel/interfaces/IWatcherListener.h"
#include "base/tools/Object.h"
#include "rapidjson/fwd.h"
@ -45,6 +46,8 @@ class Process;
class Base : public IWatcherListener, public IApiListener
{
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(Base)
Base(Process *process);
~Base() override;
@ -54,6 +57,7 @@ public:
virtual void stop();
Api *api() const;
bool isBackground() const;
bool reload(const rapidjson::Value &json);
Config *config() const;
void addListener(IBaseListener *listener);

View file

@ -23,8 +23,8 @@
*/
#include <ctime>
#include <uv.h>
#include <time.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
{
constexpr const size_t max = 520;

View file

@ -47,7 +47,6 @@ public:
# endif
Process(int argc, char **argv);
~Process();
String location(Location location, const char *fileName = nullptr) const;

View file

@ -23,7 +23,7 @@
*/
#include <assert.h>
#include <cassert>
#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()
{
Cpu::init();
const int rc = Base::init();
if (rc != 0) {
return rc;
}
Base::init();
m_network = new Network(this);
return 0;

View file

@ -27,6 +27,7 @@
#include "base/kernel/Base.h"
#include "base/tools/Object.h"
namespace xmrig {
@ -40,10 +41,11 @@ class Network;
class Controller : public Base
{
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(Controller)
Controller(Process *process);
~Controller() override;
bool isReady() const override;
int init() override;
void start() override;
void stop() override;