From 5af169fd7b453c86cbfbba09099a641b4ac96e1d Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 14 Jun 2017 16:11:01 +0300 Subject: [PATCH] Fix linux build. --- cmake/FindUV.cmake | 2 +- src/App.cpp | 3 ++ src/App_unix.cpp | 66 ++++++++++++++++++++++++++++++++++++++++ src/Console.cpp | 4 +++ src/Cpu_unix.cpp | 35 +++++++++++++++++++++ src/Mem_unix.cpp | 56 ++++++++++++++++++++++++++++++++++ src/Options.cpp | 3 +- src/crypto/CryptoNight.h | 1 + src/net/JobResult.h | 1 + src/net/Network_unix.cpp | 50 ++++++++++++++++++++++++++++++ src/net/Network_win.cpp | 2 +- 11 files changed, 220 insertions(+), 3 deletions(-) create mode 100644 src/App_unix.cpp create mode 100644 src/net/Network_unix.cpp diff --git a/cmake/FindUV.cmake b/cmake/FindUV.cmake index c8a95e7..e3c22d2 100644 --- a/cmake/FindUV.cmake +++ b/cmake/FindUV.cmake @@ -1,5 +1,5 @@ find_path(UV_INCLUDE_DIR NAMES uv.h) -find_library(UV_LIBRARY NAMES libuv) +find_library(UV_LIBRARY NAMES uv libuv) set(UV_LIBRARIES ${UV_LIBRARY}) set(UV_INCLUDE_DIRS ${UV_INCLUDE_DIR}) diff --git a/src/App.cpp b/src/App.cpp index a92883c..8f6ed14 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -22,6 +22,7 @@ */ +#include #include @@ -92,6 +93,8 @@ int App::exec() free(m_network); free(m_options); + Mem::release(); + return r; } diff --git a/src/App_unix.cpp b/src/App_unix.cpp new file mode 100644 index 0000000..d001acb --- /dev/null +++ b/src/App_unix.cpp @@ -0,0 +1,66 @@ +/* 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 +#include +#include + + +#include "App.h" +#include "Console.h" +#include "Cpu.h" +#include "Options.h" + + +void App::background() +{ + if (m_options->affinity() != -1L) { + Cpu::setAffinity(-1, m_options->affinity()); + } + + if (!m_options->background()) { + return; + } + + int i = fork(); + if (i < 0) { + exit(1); + } + + if (i > 0) { + exit(0); + } + + i = setsid(); + + if (i < 0) { + LOG_ERR("setsid() failed (errno = %d)", errno); + } + + i = chdir("/"); + if (i < 0) { + LOG_ERR("chdir() failed (errno = %d)", errno); + } +} diff --git a/src/Console.cpp b/src/Console.cpp index b3b7ed3..5df744d 100644 --- a/src/Console.cpp +++ b/src/Console.cpp @@ -23,9 +23,13 @@ #include +#include +#include +#include #include + #ifdef WIN32 # include # include "3rdparty/winansi.h" diff --git a/src/Cpu_unix.cpp b/src/Cpu_unix.cpp index 8140cd5..1b7c6b0 100644 --- a/src/Cpu_unix.cpp +++ b/src/Cpu_unix.cpp @@ -22,3 +22,38 @@ */ +#include +#include +#include + + +#include "Cpu.h" + + +void Cpu::init() +{ +# ifdef XMRIG_NO_LIBCPUID + m_totalThreads = sysconf(_SC_NPROCESSORS_CONF); +# endif + + initCommon(); +} + + +void Cpu::setAffinity(int id, unsigned long mask) +{ + cpu_set_t set; + CPU_ZERO(&set); + + for (int i = 0; i < m_totalThreads; i++) { + if (mask & (1UL << i)) { + CPU_SET(i, &set); + } + } + + if (id == -1) { + sched_setaffinity(0, sizeof(&set), &set); + } else { + pthread_setaffinity_np(pthread_self(), sizeof(&set), &set); + } +} diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index a0690e7..231b48f 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -22,5 +22,61 @@ */ +#include +#include +#include + + #include "crypto/CryptoNight.h" #include "Mem.h" +#include "Options.h" +#include "Console.h" + + +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); + + m_flags |= HugepagesAvailable; + + m_memory = static_cast(mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, 0, 0)); + + if (m_memory == MAP_FAILED) { + m_memory = static_cast(_mm_malloc(size, 16)); + return true; + } + + m_flags |= HugepagesEnabled; + + if (madvise(m_memory, size, MADV_RANDOM | MADV_WILLNEED) != 0) { + LOG_ERR("madvise failed"); + } + + if (mlock(m_memory, size) == 0) { + m_flags |= Lock; + } + + return true; +} + + +void Mem::release() +{ + const int size = MEMORY * (m_threads + 1); + + if (m_flags & HugepagesEnabled) { + if (m_flags & Lock) { + munlock(m_memory, size); + } + + munmap(m_memory, size); + } + else { + _mm_free(m_memory); + } +} diff --git a/src/Options.cpp b/src/Options.cpp index c38f417..8a8a317 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -22,8 +22,9 @@ */ -#include #include +#include +#include #ifdef _MSC_VER diff --git a/src/crypto/CryptoNight.h b/src/crypto/CryptoNight.h index 2941273..64fc0fd 100644 --- a/src/crypto/CryptoNight.h +++ b/src/crypto/CryptoNight.h @@ -25,6 +25,7 @@ #define __CRYPTONIGHT_H__ +#include #include diff --git a/src/net/JobResult.h b/src/net/JobResult.h index 55ebdbd..87e3a64 100644 --- a/src/net/JobResult.h +++ b/src/net/JobResult.h @@ -25,6 +25,7 @@ #define __JOBRESULT_H__ +#include #include diff --git a/src/net/Network_unix.cpp b/src/net/Network_unix.cpp new file mode 100644 index 0000000..546d1b8 --- /dev/null +++ b/src/net/Network_unix.cpp @@ -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 . + */ + + +#include + + +#include "net/Network.h" +#include "version.h" + + +char *Network::userAgent() +{ + const size_t max = 128; + + char *buf = static_cast(malloc(max)); + int length = snprintf(buf, max, "%s/%s (Linux ", APP_NAME, APP_VERSION); + +# if defined(__x86_64__) + length += snprintf(buf + length, max - length, "x86_64) libuv/%s", uv_version_string()); +# else + length += snprintf(buf + length, max - length, "i686) libuv/%s", uv_version_string()); +# endif + +# ifdef __GNUC__ + length += snprintf(buf + length, max - length, " gcc/%d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__); +# endif + + return buf; +} diff --git a/src/net/Network_win.cpp b/src/net/Network_win.cpp index 15b358b..6ae5e32 100644 --- a/src/net/Network_win.cpp +++ b/src/net/Network_win.cpp @@ -64,7 +64,7 @@ char *Network::userAgent() # ifdef __GNUC__ length += snprintf(buf + length, max - length, " gcc/%d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__); -#elif _MSC_VER +# elif _MSC_VER length += snprintf(buf + length, max - length, " msvc/%d", MSVC_VERSION); # endif