From a8a06b858c5acbcd2e415a24fb4b06f36f95cdc6 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 15 Aug 2017 00:58:29 +0300 Subject: [PATCH] Show CUDA version, fix Visual Studio warnings. --- CMakeLists.txt | 6 +-- cmake/flags.cmake | 13 ++--- res/app.rc | 2 +- src/3rdparty/jansson/dump.c | 4 ++ src/3rdparty/jansson/hashtable.c | 4 ++ src/3rdparty/jansson/load.c | 2 +- src/Cpu.h | 4 +- src/Cpu_stub.cpp | 4 +- src/Options.cpp | 6 ++- src/Summary.cpp | 7 +-- src/crypto/CryptoNight_p.h | 2 +- src/crypto/c_blake256.c | 2 +- src/interfaces/IStrategy.h | 1 + src/log/ConsoleLog.cpp | 8 ++- src/log/FileLog.cpp | 2 +- src/log/Log.h | 6 ++- src/net/Client.cpp | 59 ++++++++++++++++------- src/net/Client.h | 7 ++- src/net/Job.cpp | 2 +- src/net/Job.h | 6 +-- src/net/Network.cpp | 27 ++++++++++- src/net/Network.h | 9 +++- src/net/Network_win.cpp | 10 ++-- src/net/Url.cpp | 2 +- src/net/strategies/DonateStrategy.cpp | 6 +++ src/net/strategies/DonateStrategy.h | 1 + src/net/strategies/FailoverStrategy.cpp | 10 +++- src/net/strategies/FailoverStrategy.h | 1 + src/net/strategies/SinglePoolStrategy.cpp | 6 +++ src/net/strategies/SinglePoolStrategy.h | 1 + src/nvidia/cryptonight.h | 1 + src/nvidia/cuda_extra.cu | 10 ++++ src/workers/CudaWorker.cpp | 2 +- src/workers/Hashrate.cpp | 4 +- src/workers/Workers.cpp | 4 +- 35 files changed, 173 insertions(+), 68 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8f67a73..87b7c60 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.0) -project(xmrig) +project(xmrig-nvidia) option(WITH_AEON "CryptoNight-Lite support" ON) @@ -146,5 +146,5 @@ include_directories(${UV_INCLUDE_DIR}) add_subdirectory(src/3rdparty/jansson) -add_executable(xmrig ${HEADERS} ${SOURCES} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG}) -target_link_libraries(xmrig xmrig-cuda jansson ${UV_LIBRARIES} ${LIBS} ${EXTRA_LIBS} ${CPUID_LIB}) +add_executable(${PROJECT_NAME} ${HEADERS} ${SOURCES} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG}) +target_link_libraries(${PROJECT_NAME} xmrig-cuda jansson ${UV_LIBRARIES} ${LIBS} ${EXTRA_LIBS} ${CPUID_LIB}) diff --git a/cmake/flags.cmake b/cmake/flags.cmake index 445a214..ea155d9 100644 --- a/cmake/flags.cmake +++ b/cmake/flags.cmake @@ -1,10 +1,9 @@ if (CMAKE_CXX_COMPILER_ID MATCHES GNU) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -Wall -Wno-strict-aliasing") - 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_C_FLAGS "${CMAKE_C_FLAGS} -maes -Wall") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes -Wall -std=c++14 -fno-exceptions -fno-rtti") - 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_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -s") if (WIN32) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static") @@ -19,18 +18,16 @@ if (CMAKE_CXX_COMPILER_ID MATCHES GNU) elseif (CMAKE_CXX_COMPILER_ID MATCHES MSVC) - message(${CMAKE_C_FLAGS_RELEASE}) - message(${CMAKE_CXX_FLAGS_RELEASE}) - set(CMAKE_C_FLAGS_RELEASE "/MT /O2 /Ob2 /DNDEBUG") set(CMAKE_CXX_FLAGS_RELEASE "/MT /O2 /Ob2 /DNDEBUG") + add_definitions(/D_CRT_SECURE_NO_WARNINGS) + add_definitions(/D_CRT_NONSTDC_NO_WARNINGS) elseif (CMAKE_CXX_COMPILER_ID MATCHES Clang) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -Wall") - set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -funroll-loops -fmerge-all-constants") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes -Wall -std=c++14 -fno-exceptions -fno-rtti") - set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast -funroll-loops -fmerge-all-constants") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -s") endif() \ No newline at end of file diff --git a/res/app.rc b/res/app.rc index 800ce2d..aa41334 100644 --- a/res/app.rc +++ b/res/app.rc @@ -24,7 +24,7 @@ VS_VERSION_INFO VERSIONINFO VALUE "FileDescription", APP_DESC VALUE "FileVersion", APP_VERSION VALUE "LegalCopyright", APP_COPYRIGHT - VALUE "OriginalFilename", "xmrig.exe" + VALUE "OriginalFilename", "xmrig-nvidia.exe" VALUE "ProductName", APP_NAME VALUE "ProductVersion", APP_VERSION END diff --git a/src/3rdparty/jansson/dump.c b/src/3rdparty/jansson/dump.c index a23fabb..59b9b2c 100644 --- a/src/3rdparty/jansson/dump.c +++ b/src/3rdparty/jansson/dump.c @@ -5,6 +5,10 @@ * it under the terms of the MIT license. See LICENSE for details. */ +#ifdef _MSC_VER +#pragma warning(disable:4090) +#endif + #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif diff --git a/src/3rdparty/jansson/hashtable.c b/src/3rdparty/jansson/hashtable.c index c819319..dcc1687 100644 --- a/src/3rdparty/jansson/hashtable.c +++ b/src/3rdparty/jansson/hashtable.c @@ -5,6 +5,10 @@ * it under the terms of the MIT license. See LICENSE for details. */ +#ifdef _MSC_VER +#pragma warning(disable:4334) +#endif + #if HAVE_CONFIG_H #include #endif diff --git a/src/3rdparty/jansson/load.c b/src/3rdparty/jansson/load.c index c212489..6b3d1e2 100644 --- a/src/3rdparty/jansson/load.c +++ b/src/3rdparty/jansson/load.c @@ -1034,8 +1034,8 @@ json_t *json_loadf(FILE *input, size_t flags, json_error_t *error) static int fd_get_func(int *fd) { - uint8_t c; #ifdef HAVE_UNISTD_H + uint8_t c; if (read(*fd, &c, 1) == 1) return c; #endif diff --git a/src/Cpu.h b/src/Cpu.h index 8b7c364..9444274 100644 --- a/src/Cpu.h +++ b/src/Cpu.h @@ -41,8 +41,8 @@ public: static void init(); static void setAffinity(int id, uint64_t mask); - static inline bool hasAES() { return m_flags & AES; } - static inline bool isX64() { return m_flags & X86_64; } + static inline bool hasAES() { return (m_flags & AES) != 0; } + static inline bool isX64() { return (m_flags & X86_64) != 0; } static inline const char *brand() { return m_brand; } static inline int cores() { return m_totalCores; } static inline int l2() { return m_l2_cache; } diff --git a/src/Cpu_stub.cpp b/src/Cpu_stub.cpp index 0b9196e..6f45eb9 100644 --- a/src/Cpu_stub.cpp +++ b/src/Cpu_stub.cpp @@ -87,7 +87,7 @@ static inline bool has_aes_ni() int cpu_info[4] = { 0 }; cpuid(PROCESSOR_INFO, cpu_info); - return cpu_info[ECX_Reg] & bit_AES; + return (cpu_info[ECX_Reg] & bit_AES) != 0; } @@ -95,7 +95,7 @@ static inline bool has_bmi2() { int cpu_info[4] = { 0 }; cpuid(EXTENDED_FEATURES, cpu_info); - return cpu_info[EBX_Reg] & bit_BMI2; + return (cpu_info[EBX_Reg] & bit_BMI2) != 0; } diff --git a/src/Options.cpp b/src/Options.cpp index 778a051..bd51240 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -37,6 +37,7 @@ #include "Cpu.h" #include "donate.h" #include "net/Url.h" +#include "nvidia/cryptonight.h" #include "Options.h" #include "version.h" #include "workers/GpuThread.h" @@ -53,7 +54,6 @@ Options *Options::m_self = nullptr; static char const usage[] = "\ Usage: " APP_ID " [OPTIONS]\n\ Options:\n\ - -a, --algo=ALGO cryptonight (default) or cryptonight-lite\n\ -o, --url=URL URL of mining server\n\ -O, --userpass=U:P username:password pair for mining server\n\ -u, --user=USERNAME username for mining server\n\ @@ -71,7 +71,6 @@ Options:\n\ -S, --syslog use system log for output messages\n" # endif "\ - --safe safe adjust threads and av settings for current CPU\n\ --nicehash enable nicehash support\n\ --print-time=N print hashrate report every N seconds\n\ -h, --help display this help and exit\n\ @@ -599,6 +598,9 @@ void Options::showVersion() printf("\nlibuv/%s\n", uv_version_string()); printf("libjansson/%s\n", JANSSON_VERSION); + + const int cudaVersion = cuda_get_runtime_version(); + printf("CUDA/%d.%d\n", cudaVersion / 1000, cudaVersion % 100); } diff --git a/src/Summary.cpp b/src/Summary.cpp index 50efab9..07ec126 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -29,6 +29,7 @@ #include "Cpu.h" #include "log/Log.h" #include "net/Url.h" +#include "nvidia/cryptonight.h" #include "Options.h" #include "Summary.h" #include "version.h" @@ -49,9 +50,9 @@ static void print_versions() buf[0] = '\0'; # endif - - Log::i()->text(Options::i()->colors() ? "\x1B[01;32m * \x1B[01;37mVERSIONS: \x1B[01;36mXMRig/%s\x1B[01;37m libuv/%s%s" : " * VERSIONS: XMRig/%s libuv/%s%s", - APP_VERSION, uv_version_string(), buf); + const int cudaVersion = cuda_get_runtime_version(); + Log::i()->text(Options::i()->colors() ? "\x1B[01;32m * \x1B[01;37mVERSIONS: \x1B[01;36mXMRig/%s\x1B[01;37m libuv/%s CUDA/%d.%d%s" : " * VERSIONS: XMRig/%s libuv/%s CUDA/%d.%d%s", + APP_VERSION, uv_version_string(), cudaVersion / 1000, cudaVersion % 100, buf); } diff --git a/src/crypto/CryptoNight_p.h b/src/crypto/CryptoNight_p.h index 597fd20..7711727 100644 --- a/src/crypto/CryptoNight_p.h +++ b/src/crypto/CryptoNight_p.h @@ -311,7 +311,7 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) template inline void cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx) { - keccak(static_cast(input), size, ctx->state0, 200); + keccak(static_cast(input), (int) size, ctx->state0, 200); cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); diff --git a/src/crypto/c_blake256.c b/src/crypto/c_blake256.c index 0b484b9..00a84c2 100644 --- a/src/crypto/c_blake256.c +++ b/src/crypto/c_blake256.c @@ -148,7 +148,7 @@ void blake256_update(state *S, const uint8_t *data, uint64_t datalen) { if (datalen > 0) { memcpy((void *) (S->buf + left), (void *) data, datalen >> 3); - S->buflen = (left << 3) + datalen; + S->buflen = (left << 3) + (int) datalen; } else { S->buflen = 0; } diff --git a/src/interfaces/IStrategy.h b/src/interfaces/IStrategy.h index b7f5d65..660529e 100644 --- a/src/interfaces/IStrategy.h +++ b/src/interfaces/IStrategy.h @@ -41,6 +41,7 @@ public: virtual void connect() = 0; virtual void resume() = 0; virtual void stop() = 0; + virtual void tick(uint64_t now) = 0; }; diff --git a/src/log/ConsoleLog.cpp b/src/log/ConsoleLog.cpp index 51e8040..b641c0e 100644 --- a/src/log/ConsoleLog.cpp +++ b/src/log/ConsoleLog.cpp @@ -92,8 +92,7 @@ void ConsoleLog::message(int level, const char* fmt, va_list args) } } - const size_t len = 64 + strlen(fmt) + 2; - char *buf = new char[len]; + char *buf = new char[64 + strlen(fmt) + 2]; sprintf(buf, "[%d-%02d-%02d %02d:%02d:%02d]%s %s%s\n", stime.tm_year + 1900, @@ -113,8 +112,7 @@ void ConsoleLog::message(int level, const char* fmt, va_list args) void ConsoleLog::text(const char* fmt, va_list args) { - const int len = 64 + strlen(fmt) + 2; - char *buf = new char[len]; + char *buf = new char[64 + strlen(fmt) + 2]; sprintf(buf, "%s%s\n", fmt, m_colors ? Log::kCL_N : ""); @@ -129,7 +127,7 @@ void ConsoleLog::print(char *fmt, va_list args) uv_buf_t buf; buf.base = strdup(m_buf); - buf.len = strlen(buf.base); + buf.len = (ULONG) strlen(buf.base); uv_write_t *req = new uv_write_t; req->data = buf.base; diff --git a/src/log/FileLog.cpp b/src/log/FileLog.cpp index 224c600..9a8711a 100644 --- a/src/log/FileLog.cpp +++ b/src/log/FileLog.cpp @@ -88,7 +88,7 @@ void FileLog::onWrite(uv_fs_t *req) void FileLog::write(char *data, size_t size) { - uv_buf_t buf = uv_buf_init(data, size); + uv_buf_t buf = uv_buf_init(data, (unsigned int) size); uv_fs_t *req = static_cast(malloc(sizeof(uv_fs_t))); req->data = buf.base; diff --git a/src/log/Log.h b/src/log/Log.h index 16e94e0..e193125 100644 --- a/src/log/Log.h +++ b/src/log/Log.h @@ -77,10 +77,14 @@ private: #ifdef APP_DEBUG # define LOG_DEBUG(x, ...) Log::i()->message(Log::DEBUG, x, ##__VA_ARGS__) +#else +# define LOG_DEBUG(x, ...) +#endif + +#if defined(APP_DEBUG) || defined(APP_DEVEL) # define LOG_DEBUG_ERR(x, ...) Log::i()->message(Log::ERR, x, ##__VA_ARGS__) # define LOG_DEBUG_WARN(x, ...) Log::i()->message(Log::WARNING, x, ##__VA_ARGS__) #else -# define LOG_DEBUG(x, ...) # define LOG_DEBUG_ERR(x, ...) # define LOG_DEBUG_WARN(x, ...) #endif diff --git a/src/net/Client.cpp b/src/net/Client.cpp index bc44c7d..a129ddb 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -56,13 +56,14 @@ Client::Client(int id, const char *agent, IClientListener *listener) : m_failures(0), m_recvBufPos(0), m_state(UnconnectedState), + m_expire(0), m_stream(nullptr), m_socket(nullptr) { memset(m_ip, 0, sizeof(m_ip)); memset(&m_hints, 0, sizeof(m_hints)); - m_resolver.data = m_responseTimer.data = m_retriesTimer.data = m_keepAliveTimer.data = this; + m_resolver.data = this; m_hints.ai_family = PF_INET; m_hints.ai_socktype = SOCK_STREAM; @@ -71,10 +72,10 @@ Client::Client(int id, const char *agent, IClientListener *listener) : m_recvBuf.base = static_cast(malloc(kRecvBufSize)); m_recvBuf.len = kRecvBufSize; - auto loop = uv_default_loop(); - uv_timer_init(loop, &m_retriesTimer); - uv_timer_init(loop, &m_responseTimer); - uv_timer_init(loop, &m_keepAliveTimer); +# ifndef XMRIG_PROXY_PROJECT + m_keepAliveTimer.data = this; + uv_timer_init(uv_default_loop(), &m_keepAliveTimer); +# endif } @@ -93,12 +94,12 @@ Client::~Client() int64_t Client::send(char *data, size_t size) { LOG_DEBUG("[%s:%u] send (%d bytes): \"%s\"", m_url.host(), m_url.port(), size ? size : strlen(data), data); - if (state() != ConnectedState) { + if (state() != ConnectedState || !uv_is_writable(m_stream)) { LOG_DEBUG_ERR("[%s:%u] send failed, invalid state: %d", m_url.host(), m_url.port(), m_state); return -1; } - uv_buf_t buf = uv_buf_init(data, size ? size : strlen(data)); + uv_buf_t buf = uv_buf_init(data, (unsigned int) (size ? size : strlen(data))); uv_write_t *req = new uv_write_t; req->data = buf.base; @@ -108,8 +109,7 @@ int64_t Client::send(char *data, size_t size) delete req; }); - uv_timer_start(&m_responseTimer, [](uv_timer_t *handle) { getClient(handle->data)->close(); }, kResponseTimeout, 0); - + m_expire = uv_now(uv_default_loop()) + kResponseTimeout; return m_sequence++; } @@ -134,9 +134,11 @@ void Client::connect(const Url *url) void Client::disconnect() { +# ifndef XMRIG_PROXY_PROJECT uv_timer_stop(&m_keepAliveTimer); - uv_timer_stop(&m_responseTimer); - uv_timer_stop(&m_retriesTimer); +# endif + + m_expire = 0; m_failures = -1; close(); @@ -153,6 +155,24 @@ void Client::setUrl(const Url *url) } +void Client::tick(uint64_t now) +{ + if (m_expire == 0 || now < m_expire) { + return; + } + + if (m_state == ConnectedState) { + LOG_DEBUG_ERR("[%s:%u] timeout", m_url.host(), m_url.port()); + close(); + } + + + if (m_state == ConnectingState) { + connect(); + } +} + + int64_t Client::submit(const JobResult &result) { char *req = static_cast(malloc(345)); @@ -231,6 +251,7 @@ int Client::resolve(const char *host) { setState(HostLookupState); + m_expire = 0; m_recvBufPos = 0; if (m_failures == -1) { @@ -432,19 +453,20 @@ void Client::reconnect() { setState(ConnectingState); - uv_timer_stop(&m_responseTimer); +# ifndef XMRIG_PROXY_PROJECT if (m_url.isKeepAlive()) { uv_timer_stop(&m_keepAliveTimer); } +# endif if (m_failures == -1) { return m_listener->onClose(this, -1); } m_failures++; - m_listener->onClose(this, m_failures); + m_listener->onClose(this, (int) m_failures); - uv_timer_start(&m_retriesTimer, [](uv_timer_t *handle) { getClient(handle->data)->connect(); }, m_retryPause, 0); + m_expire = uv_now(uv_default_loop()) + m_retryPause; } @@ -462,12 +484,15 @@ void Client::setState(SocketState state) void Client::startTimeout() { - uv_timer_stop(&m_responseTimer); + m_expire = 0; + +# ifndef XMRIG_PROXY_PROJECT if (!m_url.isKeepAlive()) { return; } uv_timer_start(&m_keepAliveTimer, [](uv_timer_t *handle) { getClient(handle->data)->ping(); }, kKeepAliveTimeout, 0); +# endif } @@ -476,7 +501,7 @@ void Client::onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t auto client = getClient(handle->data); buf->base = &client->m_recvBuf.base[client->m_recvBufPos]; - buf->len = client->m_recvBuf.len - client->m_recvBufPos; + buf->len = client->m_recvBuf.len - (ULONG) client->m_recvBufPos; } @@ -523,7 +548,7 @@ void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) auto client = getClient(stream->data); if (nread < 0) { if (nread != UV_EOF && !client->m_quiet) { - LOG_ERR("[%s:%u] read error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror(nread)); + LOG_ERR("[%s:%u] read error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror((int) nread)); } return client->close();; diff --git a/src/net/Client.h b/src/net/Client.h index b04c2c3..b7eda32 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -62,6 +62,7 @@ public: void connect(const Url *url); void disconnect(); void setUrl(const Url *url); + void tick(uint64_t now); inline bool isReady() const { return m_state == ConnectedState && m_failures == 0; } inline const char *host() const { return m_url.host(); } @@ -112,14 +113,16 @@ private: SocketState m_state; static int64_t m_sequence; std::map m_results; + uint64_t m_expire; Url m_url; uv_buf_t m_recvBuf; uv_getaddrinfo_t m_resolver; uv_stream_t *m_stream; uv_tcp_t *m_socket; + +# ifndef XMRIG_PROXY_PROJECT uv_timer_t m_keepAliveTimer; - uv_timer_t m_responseTimer; - uv_timer_t m_retriesTimer; +# endif }; diff --git a/src/net/Job.cpp b/src/net/Job.cpp index 4929aaf..bce65e6 100644 --- a/src/net/Job.cpp +++ b/src/net/Job.cpp @@ -82,7 +82,7 @@ bool Job::setBlob(const char *blob) return false; } - if (!fromHex(blob, m_size * 2, m_blob)) { + if (!fromHex(blob, (int) m_size * 2, m_blob)) { return false; } diff --git a/src/net/Job.h b/src/net/Job.h index b723eb2..8616058 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -45,9 +45,9 @@ public: inline const uint32_t *nonce() const { return reinterpret_cast(m_blob + 39); } inline const uint8_t *blob() const { return m_blob; } inline int poolId() const { return m_poolId; } + inline size_t size() const { return m_size; } inline uint32_t *nonce() { return reinterpret_cast(m_blob + 39); } - inline uint32_t diff() const { return m_diff; } - inline uint32_t size() const { return m_size; } + inline uint32_t diff() const { return (uint32_t) m_diff; } inline uint64_t target() const { return m_target; } inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } @@ -68,7 +68,7 @@ private: int m_poolId; VAR_ALIGN(16, char m_id[64]); VAR_ALIGN(16, uint8_t m_blob[84]); // Max blob size is 84 (75 fixed + 9 variable), aligned to 96. https://github.com/xmrig/xmrig/issues/1 Thanks fireice-uk. - uint32_t m_size; + size_t m_size; uint64_t m_diff; uint64_t m_target; diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 86c5ee7..04ce2ae 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -21,6 +21,9 @@ * along with this program. If not, see . */ +#ifdef _MSC_VER +#pragma warning(disable:4244) +#endif #include #include @@ -39,7 +42,6 @@ Network::Network(const Options *options) : - m_donateActive(false), m_options(options), m_donate(nullptr), m_accepted(0), @@ -62,6 +64,11 @@ Network::Network(const Options *options) : if (m_options->donateLevel() > 0) { m_donate = new DonateStrategy(m_agent, this); } + + m_timer.data = this; + uv_timer_init(uv_default_loop(), &m_timer); + + uv_timer_start(&m_timer, Network::onTick, kTickInterval, kTickInterval); } @@ -164,3 +171,21 @@ void Network::setJob(Client *client, const Job &job) Workers::setJob(job); } + + +void Network::tick() +{ + const uint64_t now = uv_now(uv_default_loop()); + + m_strategy->tick(now); + + if (m_donate) { + m_donate->tick(now); + } +} + + +void Network::onTick(uv_timer_t *handle) +{ + static_cast(handle->data)->tick(); +} diff --git a/src/net/Network.h b/src/net/Network.h index 21046df..ba1982e 100644 --- a/src/net/Network.h +++ b/src/net/Network.h @@ -57,15 +57,20 @@ protected: void onResultAccepted(Client *client, int64_t seq, uint32_t diff, uint64_t ms, const char *error) override; private: - void setJob(Client *client, const Job &job); + constexpr static int kTickInterval = 1 * 1000; + + void setJob(Client *client, const Job &job); + void tick(); + + static void onTick(uv_timer_t *handle); - bool m_donateActive; char *m_agent; const Options *m_options; IStrategy *m_donate; IStrategy *m_strategy; uint64_t m_accepted; uint64_t m_rejected; + uv_timer_t m_timer; }; diff --git a/src/net/Network_win.cpp b/src/net/Network_win.cpp index 6ae5e32..3f3a515 100644 --- a/src/net/Network_win.cpp +++ b/src/net/Network_win.cpp @@ -27,6 +27,7 @@ #include "net/Network.h" +#include "nvidia/cryptonight.h" #include "version.h" @@ -51,15 +52,16 @@ static inline OSVERSIONINFOEX winOsVersion() char *Network::userAgent() { const auto osver = winOsVersion(); - const size_t max = 128; + const size_t max = 160; + const int cudaVersion = cuda_get_runtime_version(); - char *buf = static_cast(malloc(max)); + char *buf = new char[128]; int length = snprintf(buf, max, "%s/%s (Windows NT %lu.%lu", APP_NAME, APP_VERSION, osver.dwMajorVersion, osver.dwMinorVersion); # if defined(__x86_64__) || defined(_M_AMD64) - length += snprintf(buf + length, max - length, "; Win64; x64) libuv/%s", uv_version_string()); + length += snprintf(buf + length, max - length, "; Win64; x64) libuv/%s CUDA/%d.%d", uv_version_string(), cudaVersion / 1000, cudaVersion % 100); # else - length += snprintf(buf + length, max - length, ") libuv/%s", uv_version_string()); + length += snprintf(buf + length, max - length, ") libuv/%s CUDA/%d.%d", uv_version_string(), cudaVersion / 1000, cudaVersion % 100); # endif # ifdef __GNUC__ diff --git a/src/net/Url.cpp b/src/net/Url.cpp index 82b788d..a0024d2 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -121,7 +121,7 @@ bool Url::parse(const char *url) memcpy(m_host, base, size - 1); m_host[size - 1] = '\0'; - m_port = strtol(port, nullptr, 10); + m_port = (uint16_t) strtol(port, nullptr, 10); return true; } diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index d87e83d..c346801 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -69,6 +69,12 @@ void DonateStrategy::stop() } +void DonateStrategy::tick(uint64_t now) +{ + m_client->tick(now); +} + + void DonateStrategy::onClose(Client *client, int failures) { } diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h index 1c7597e..b54b0b1 100644 --- a/src/net/strategies/DonateStrategy.h +++ b/src/net/strategies/DonateStrategy.h @@ -49,6 +49,7 @@ public: int64_t submit(const JobResult &result) override; void connect() override; void stop() override; + void tick(uint64_t now) override; protected: void onClose(Client *client, int failures) override; diff --git a/src/net/strategies/FailoverStrategy.cpp b/src/net/strategies/FailoverStrategy.cpp index 380cf98..e25b8c5 100644 --- a/src/net/strategies/FailoverStrategy.cpp +++ b/src/net/strategies/FailoverStrategy.cpp @@ -74,6 +74,14 @@ void FailoverStrategy::stop() } +void FailoverStrategy::tick(uint64_t now) +{ + for (Client *client : m_pools) { + client->tick(now); + } +} + + void FailoverStrategy::onClose(Client *client, int failures) { if (failures == -1) { @@ -132,7 +140,7 @@ void FailoverStrategy::onResultAccepted(Client *client, int64_t seq, uint32_t di void FailoverStrategy::add(const Url *url, const char *agent) { - Client *client = new Client(m_pools.size(), agent, this); + Client *client = new Client((int) m_pools.size(), agent, this); client->setUrl(url); client->setRetryPause(Options::i()->retryPause() * 1000); diff --git a/src/net/strategies/FailoverStrategy.h b/src/net/strategies/FailoverStrategy.h index f0fa051..616a08d 100644 --- a/src/net/strategies/FailoverStrategy.h +++ b/src/net/strategies/FailoverStrategy.h @@ -49,6 +49,7 @@ public: void connect() override; void resume() override; void stop() override; + void tick(uint64_t now) override; protected: void onClose(Client *client, int failures) override; diff --git a/src/net/strategies/SinglePoolStrategy.cpp b/src/net/strategies/SinglePoolStrategy.cpp index b1a6941..f38405f 100644 --- a/src/net/strategies/SinglePoolStrategy.cpp +++ b/src/net/strategies/SinglePoolStrategy.cpp @@ -66,6 +66,12 @@ void SinglePoolStrategy::stop() } +void SinglePoolStrategy::tick(uint64_t now) +{ + m_client->tick(now); +} + + void SinglePoolStrategy::onClose(Client *client, int failures) { if (!isActive()) { diff --git a/src/net/strategies/SinglePoolStrategy.h b/src/net/strategies/SinglePoolStrategy.h index 51b1a88..c09d030 100644 --- a/src/net/strategies/SinglePoolStrategy.h +++ b/src/net/strategies/SinglePoolStrategy.h @@ -46,6 +46,7 @@ public: void connect() override; void resume() override; void stop() override; + void tick(uint64_t now) override; protected: void onClose(Client *client, int failures) override; diff --git a/src/nvidia/cryptonight.h b/src/nvidia/cryptonight.h index 23790f1..c5520c2 100644 --- a/src/nvidia/cryptonight.h +++ b/src/nvidia/cryptonight.h @@ -28,6 +28,7 @@ typedef struct { extern "C" { int cuda_get_devicecount(); +int cuda_get_runtime_version(); int cuda_get_deviceinfo(nvid_ctx *ctx); int cryptonight_extra_cpu_init(nvid_ctx *ctx); void cryptonight_extra_cpu_set_data( nvid_ctx* ctx, const void *data, uint32_t len); diff --git a/src/nvidia/cuda_extra.cu b/src/nvidia/cuda_extra.cu index a4b2f33..47ec7cf 100644 --- a/src/nvidia/cuda_extra.cu +++ b/src/nvidia/cuda_extra.cu @@ -261,6 +261,16 @@ extern "C" int cuda_get_devicecount() return 0; } +extern "C" int cuda_get_runtime_version() +{ + int runtimeVersion = 0; + if (cudaRuntimeGetVersion(&runtimeVersion) == cudaSuccess) { + return runtimeVersion; + } + + return 0; +} + extern "C" int cuda_get_deviceinfo(nvid_ctx* ctx) { cudaError_t err; diff --git a/src/workers/CudaWorker.cpp b/src/workers/CudaWorker.cpp index 14f993e..fdbc6fc 100644 --- a/src/workers/CudaWorker.cpp +++ b/src/workers/CudaWorker.cpp @@ -71,7 +71,7 @@ void CudaWorker::start() consumeJob(); } - cryptonight_extra_cpu_set_data(&m_ctx, m_job.blob(), m_job.size()); + cryptonight_extra_cpu_set_data(&m_ctx, m_job.blob(), (uint32_t) m_job.size()); while (!Workers::isOutdated(m_sequence)) { uint32_t foundNonce[10]; diff --git a/src/workers/Hashrate.cpp b/src/workers/Hashrate.cpp index 08ba87e..6a5639f 100644 --- a/src/workers/Hashrate.cpp +++ b/src/workers/Hashrate.cpp @@ -127,8 +127,8 @@ double Hashrate::calc(size_t threadId, size_t ms) const } double hashes, time; - hashes = lastestHashCnt - earliestHashCount; - time = lastestStamp - earliestStamp; + hashes = (double) lastestHashCnt - earliestHashCount; + time = (double) lastestStamp - earliestStamp; time /= 1000.0; return hashes / time; diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 496796a..a851a29 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -120,7 +120,7 @@ void Workers::setJob(const Job &job) void Workers::start(const std::vector &threads) { const size_t count = threads.size(); - m_hashrate = new Hashrate(count); + m_hashrate = new Hashrate((int) count); uv_mutex_init(&m_mutex); uv_rwlock_init(&m_rwlock); @@ -133,7 +133,7 @@ void Workers::start(const std::vector &threads) uv_timer_start(&m_timer, Workers::onTick, 500, 500); for (size_t i = 0; i < count; ++i) { - Handle *handle = new Handle(i, threads[i], count); + Handle *handle = new Handle((int) i, threads[i], (int) count); m_workers.push_back(handle); handle->start(Workers::onReady); }