Add IWorker, Handle, SingleWorker, Worker classes.

This commit is contained in:
XMRig 2017-06-10 07:05:00 +03:00
parent b772349f69
commit 29aa466023
13 changed files with 358 additions and 12 deletions

View file

@ -9,6 +9,7 @@ set(HEADERS
src/Console.h
src/Cpu.h
src/interfaces/IClientListener.h
src/interfaces/IWorker.h
src/Mem.h
src/net/Client.h
src/net/Job.h
@ -16,6 +17,9 @@ set(HEADERS
src/net/Url.h
src/Options.h
src/Summary.h
src/workers/Handle.h
src/workers/SingleWorker.h
src/workers/Worker.h
src/version.h
)
@ -43,6 +47,9 @@ set(SOURCES
src/net/Url.cpp
src/Options.cpp
src/Summary.cpp
src/workers/Handle.cpp
src/workers/SingleWorker.cpp
src/workers/Worker.cpp
src/xmrig.cpp
)
@ -85,11 +92,11 @@ if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
set(CMAKE_BUILD_TYPE Release)
endif()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -Wall")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -Wall -fno-exceptions")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes -Wall -fno-exceptions")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast -s -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2")
#set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -gdwarf-2")
#set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -fprofile-generate")
#set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -fprofile-use -fprofile-correction")

View file

@ -35,6 +35,8 @@
#include "Options.h"
#include "Summary.h"
#include "version.h"
#include "workers/Handle.h"
#include "workers/SingleWorker.h"
@ -45,6 +47,8 @@ App::App(int argc, char **argv)
m_options = Options::parse(argc, argv);
m_network = new Network(m_options);
}
@ -71,6 +75,8 @@ App::exec()
Mem::allocate(m_options->algo(), m_options->threads(), m_options->doubleHash());
Summary::print();
startWorders();
m_network->connect();
const int r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
@ -78,3 +84,23 @@ App::exec()
return r;
}
void App::startWorders()
{
for (int i = 0; i < m_options->threads(); ++i) {
Handle *handle = new Handle(i);
m_workers.push_back(handle);
handle->start(App::onWorkerStarted);
}
}
void *App::onWorkerStarted(void *arg)
{
auto handle = static_cast<Handle*>(arg);
IWorker *worker = new SingleWorker(handle);
worker->start();
return nullptr;
}

View file

@ -25,8 +25,12 @@
#define __APP_H__
class Options;
#include <vector>
class Handle;
class Network;
class Options;
class App
@ -38,8 +42,13 @@ public:
int exec();
private:
Options *m_options;
void startWorders();
static void* onWorkerStarted(void *arg);
Network *m_network;
Options *m_options;
std::vector<Handle*> m_workers;
};

View file

@ -38,17 +38,17 @@ size_t Mem::m_offset = 0;
uint8_t *Mem::m_memory = nullptr;
cryptonight_ctx *Mem::create(int algo, int threadId, bool doubleHash)
cryptonight_ctx *Mem::create(int threadId)
{
# ifndef XMRIG_NO_AEON
if (algo == Options::ALGO_CRYPTONIGHT_LITE) {
return createLite(threadId, doubleHash);
if (m_algo == Options::ALGO_CRYPTONIGHT_LITE) {
return createLite(threadId);
}
# endif
cryptonight_ctx *ctx = reinterpret_cast<cryptonight_ctx *>(&m_memory[MEMORY - sizeof(cryptonight_ctx) * (threadId + 1)]);
const int ratio = doubleHash ? 2 : 1;
const int ratio = m_doubleHash ? 2 : 1;
ctx->memory = &m_memory[MEMORY * (threadId * ratio + 1)];
return ctx;
@ -68,10 +68,10 @@ void *Mem::calloc(size_t num, size_t size)
#ifndef XMRIG_NO_AEON
cryptonight_ctx *Mem::createLite(int threadId, bool doubleHash) {
cryptonight_ctx *Mem::createLite(int threadId) {
cryptonight_ctx *ctx;
if (!doubleHash) {
if (!m_doubleHash) {
const size_t offset = MEMORY * (threadId + 1);
ctx = reinterpret_cast<cryptonight_ctx *>(&m_memory[offset + MEMORY_LITE]);

View file

@ -41,7 +41,7 @@ public:
};
static bool allocate(int algo, int threads, bool doubleHash);
static cryptonight_ctx *create(int algo, int threadId, bool doubleHash);
static cryptonight_ctx *create(int threadId);
static void *calloc(size_t num, size_t size);
static void release();
@ -58,7 +58,7 @@ private:
static uint8_t *m_memory __attribute__((aligned(16)));
# ifndef XMRIG_NO_AEON
static cryptonight_ctx *createLite(int threadId, bool doubleHash);
static cryptonight_ctx *createLite(int threadId);
# endif
};

View file

@ -143,6 +143,10 @@ static BOOL TrySetLockPagesPrivilege() {
bool Mem::allocate(int algo, int threads, bool doubleHash)
{
m_algo = algo;
m_threads = threads;
m_doubleHash = doubleHash;
const int ratio = (doubleHash && algo != Options::ALGO_CRYPTONIGHT_LITE) ? 2 : 1;
const size_t size = MEMORY * (threads * ratio + 1);

37
src/interfaces/IWorker.h Normal file
View file

@ -0,0 +1,37 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 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
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef __IWORKER_H__
#define __IWORKER_H__
class IWorker
{
public:
virtual ~IWorker() {}
virtual void start() = 0;
};
#endif // __IWORKER_H__

39
src/workers/Handle.cpp Normal file
View file

@ -0,0 +1,39 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 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
* 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 <http://www.gnu.org/licenses/>.
*/
#include "workers/Handle.h"
Handle::Handle(int id) :
m_id(id),
m_worker(nullptr)
{
}
void Handle::start(void *(*callback) (void *))
{
pthread_create(&m_thread, nullptr, callback, this);
// m_thread = std::thread([]() {});
}

50
src/workers/Handle.h Normal file
View file

@ -0,0 +1,50 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 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
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef __HANDLE_H__
#define __HANDLE_H__
#include <pthread.h>
class IWorker;
class Handle
{
public:
Handle(int id);
void start(void *(*callback) (void *));
inline int id() const { return m_id; }
inline void setWorker(IWorker *worker) { m_worker = worker; }
private:
int m_id;
IWorker *m_worker;
pthread_t m_thread;
};
#endif /* __HANDLE_H__ */

View file

@ -0,0 +1,35 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 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
* 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 <http://www.gnu.org/licenses/>.
*/
#include <pthread.h>
#include "Console.h"
#include "workers/SingleWorker.h"
SingleWorker::SingleWorker(Handle *handle)
: Worker(handle)
{
LOG_WARN("SingleWorker %d", pthread_self());
}

View file

@ -0,0 +1,41 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 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
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef __SINGLEWORKER_H__
#define __SINGLEWORKER_H__
#include "workers/Worker.h"
class Handle;
class SingleWorker : public Worker
{
public:
SingleWorker(Handle *handle);
};
#endif /* __SINGLEWORKER_H__ */

48
src/workers/Worker.cpp Normal file
View file

@ -0,0 +1,48 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 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
* 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 <http://www.gnu.org/licenses/>.
*/
#include "workers/Handle.h"
#include "workers/Worker.h"
#include "Mem.h"
Worker::Worker(Handle *handle) :
m_handle(handle),
m_id(handle->id())
{
m_handle->setWorker(this);
m_ctx = Mem::create(m_id);
}
Worker::~Worker()
{
}
void Worker::start()
{
}

50
src/workers/Worker.h Normal file
View file

@ -0,0 +1,50 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 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
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef __WORKER_H__
#define __WORKER_H__
#include "interfaces/IWorker.h"
struct cryptonight_ctx;
class Handle;
class Worker : public IWorker
{
public:
Worker(Handle *handle);
~Worker();
void start() override;
protected:
cryptonight_ctx *m_ctx;
Handle *m_handle;
int m_id;
};
#endif /* __WORKER_H__ */