diff --git a/CMakeLists.txt b/CMakeLists.txt index 2442a75a..e91fae3c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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") diff --git a/src/App.cpp b/src/App.cpp index b6a0720a..943690ec 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -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(arg); + IWorker *worker = new SingleWorker(handle); + worker->start(); + + return nullptr; +} diff --git a/src/App.h b/src/App.h index a6aef6fa..25d91890 100644 --- a/src/App.h +++ b/src/App.h @@ -25,8 +25,12 @@ #define __APP_H__ -class Options; +#include + + +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 m_workers; }; diff --git a/src/Mem.cpp b/src/Mem.cpp index f9554052..4437c673 100644 --- a/src/Mem.cpp +++ b/src/Mem.cpp @@ -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(&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(&m_memory[offset + MEMORY_LITE]); diff --git a/src/Mem.h b/src/Mem.h index ac89c363..9a438a17 100644 --- a/src/Mem.h +++ b/src/Mem.h @@ -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 }; diff --git a/src/Mem_win.cpp b/src/Mem_win.cpp index 18b54ba3..f9a7ab3d 100644 --- a/src/Mem_win.cpp +++ b/src/Mem_win.cpp @@ -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); diff --git a/src/interfaces/IWorker.h b/src/interfaces/IWorker.h new file mode 100644 index 00000000..f9010a2f --- /dev/null +++ b/src/interfaces/IWorker.h @@ -0,0 +1,37 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 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 . + */ + +#ifndef __IWORKER_H__ +#define __IWORKER_H__ + + +class IWorker +{ +public: + virtual ~IWorker() {} + + virtual void start() = 0; +}; + + +#endif // __IWORKER_H__ diff --git a/src/workers/Handle.cpp b/src/workers/Handle.cpp new file mode 100644 index 00000000..6c1f5553 --- /dev/null +++ b/src/workers/Handle.cpp @@ -0,0 +1,39 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 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 "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([]() {}); +} diff --git a/src/workers/Handle.h b/src/workers/Handle.h new file mode 100644 index 00000000..e697a618 --- /dev/null +++ b/src/workers/Handle.h @@ -0,0 +1,50 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 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 . + */ + +#ifndef __HANDLE_H__ +#define __HANDLE_H__ + + +#include + + +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__ */ diff --git a/src/workers/SingleWorker.cpp b/src/workers/SingleWorker.cpp new file mode 100644 index 00000000..d9a8f8a6 --- /dev/null +++ b/src/workers/SingleWorker.cpp @@ -0,0 +1,35 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 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 "Console.h" +#include "workers/SingleWorker.h" + + +SingleWorker::SingleWorker(Handle *handle) + : Worker(handle) +{ + LOG_WARN("SingleWorker %d", pthread_self()); +} diff --git a/src/workers/SingleWorker.h b/src/workers/SingleWorker.h new file mode 100644 index 00000000..830c6f39 --- /dev/null +++ b/src/workers/SingleWorker.h @@ -0,0 +1,41 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 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 . + */ + +#ifndef __SINGLEWORKER_H__ +#define __SINGLEWORKER_H__ + + +#include "workers/Worker.h" + + +class Handle; + + +class SingleWorker : public Worker +{ +public: + SingleWorker(Handle *handle); +}; + + +#endif /* __SINGLEWORKER_H__ */ diff --git a/src/workers/Worker.cpp b/src/workers/Worker.cpp new file mode 100644 index 00000000..3e42d1bd --- /dev/null +++ b/src/workers/Worker.cpp @@ -0,0 +1,48 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 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 "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() +{ + +} diff --git a/src/workers/Worker.h b/src/workers/Worker.h new file mode 100644 index 00000000..c24fa573 --- /dev/null +++ b/src/workers/Worker.h @@ -0,0 +1,50 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 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 . + */ + +#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__ */