From e40a7a720c84b709dbb49e9bdbb647acb3e43af6 Mon Sep 17 00:00:00 2001 From: Tanguy Pruvot Date: Thu, 13 Nov 2014 14:46:16 +0100 Subject: [PATCH] ccminer: rename main file and switch to C++ There was a different behavior on linux and visual studio That was making it hard to link functions correctly That remove some ifdef / extern "C" requirements note about x86 releases, x86 nvml.dll is not installed on Windows x64! --- Makefile.am | 3 +- api.cpp | 18 +++---- cpu-miner.c => ccminer.cpp | 30 +++-------- ccminer.vcxproj | 10 ++-- ccminer.vcxproj.filters | 4 +- configure.ac | 2 +- cuda.cpp | 8 +-- hashlog.cpp | 12 ++--- nvml.cpp | 105 ++++++++++++++++--------------------- stats.cpp | 3 +- util.c => util.cpp | 4 +- 11 files changed, 82 insertions(+), 117 deletions(-) rename cpu-miner.c => ccminer.cpp (99%) rename util.c => util.cpp (99%) diff --git a/Makefile.am b/Makefile.am index 2f8c418..f2af2d1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -17,7 +17,8 @@ bin_PROGRAMS = ccminer ccminer_SOURCES = elist.h miner.h compat.h \ compat/inttypes.h compat/stdbool.h compat/unistd.h \ compat/sys/time.h compat/getopt/getopt.h \ - cpu-miner.c util.c crc32.c hefty1.c scrypt.c \ + crc32.c hefty1.c scrypt.c \ + ccminer.cpp util.cpp \ api.cpp hashlog.cpp stats.cpp cuda.cpp \ heavy/heavy.cu \ heavy/cuda_blake512.cu heavy/cuda_blake512.h \ diff --git a/api.cpp b/api.cpp index fa716ba..ee26ef1 100644 --- a/api.cpp +++ b/api.cpp @@ -114,8 +114,6 @@ extern uint32_t rejected_count; static void gpustatus(int thr_id) { char buf[MYBUFSIZ]; - float gt; - int gp, gf; if (thr_id >= 0 && thr_id < gpu_threads) { struct cgpu_info *cgpu = &thr_info[thr_id].gpu; @@ -125,16 +123,12 @@ static void gpustatus(int thr_id) #ifdef USE_WRAPNVML // todo if (1 || cgpu->has_monitoring) { - gf = gpu_fanpercent(cgpu); - gt = gpu_temp(cgpu); - gp = gpu_power(cgpu); - // gpu_clock(cgpu); + cgpu->gpu_temp = gpu_temp(cgpu); + cgpu->gpu_fan = gpu_fanpercent(cgpu); + cgpu->gpu_power = gpu_power(cgpu); + //cgpu->gpu_clock = gpu_clock(cgpu); } - else #endif - { - gt = 0.0; gf = gp = 0; - } // todo: can be 0 if set by algo (auto) if (opt_intensity == 0 && opt_work_size) { @@ -155,7 +149,7 @@ static void gpustatus(int thr_id) sprintf(buf, "GPU=%d;TEMP=%.1f;FAN=%d;POWER=%d;KHS=%.2f;" "HWF=%d;I=%d|", - thr_id, gt, gf, gp, cgpu->khashes, + thr_id, cgpu->gpu_temp, cgpu->gpu_fan, cgpu->gpu_power, cgpu->khashes, cgpu->hw_errors, cgpu->intensity); strcat(buffer, buf); @@ -174,7 +168,7 @@ static char *getsummary(char *params) *buffer = '\0'; sprintf(buffer, "NAME=%s;VER=%s;API=%s;" - "ALGO=%s;KHS=%.2f;ACC=%d;REJ=%d;ACCMN=%.3f;UPTIME=%.1f|", + "ALGO=%s;KHS=%.2f;ACC=%d;REJ=%d;ACCMN=%.3f;UPTIME=%.0f|", PACKAGE_NAME, PACKAGE_VERSION, APIVERSION, algo, (double)global_hashrate / 1000.0, accepted_count, rejected_count, diff --git a/cpu-miner.c b/ccminer.cpp similarity index 99% rename from cpu-miner.c rename to ccminer.cpp index f2ff544..0164067 100644 --- a/cpu-miner.c +++ b/ccminer.cpp @@ -9,7 +9,6 @@ */ #include "cpuminer-config.h" -#define _GNU_SOURCE #include #include @@ -56,21 +55,15 @@ BOOL WINAPI ConsoleHandler(DWORD); #define HEAVYCOIN_BLKHDR_SZ 84 #define MNR_BLKHDR_SZ 80 -// from cuda.cu -#ifdef __cplusplus -extern "C" -{ -#endif +// from cuda.cpp int cuda_num_devices(); void cuda_devicenames(); void cuda_devicereset(); int cuda_finddevice(char *name); -#ifdef __cplusplus -} -#endif #ifdef USE_WRAPNVML #include "nvml.h" +wrap_nvml_handle *hnvml = NULL; #endif #ifdef __linux /* Linux specific policy and affinity management */ @@ -247,10 +240,6 @@ uint32_t opt_work_size = 0; /* default */ char *opt_api_allow = "127.0.0.1"; /* 0.0.0.0 for all ips */ int opt_api_listen = 4068; /* 0 to disable */ -#ifdef USE_WRAPNVML -wrap_nvml_handle *nvmlh = NULL; -#endif - #ifdef HAVE_GETOPT_LONG #include #else @@ -429,8 +418,8 @@ void proper_exit(int reason) timeEndPeriod(1); // else never executed #endif #ifdef USE_WRAPNVML - if (nvmlh) - wrap_nvml_destroy(nvmlh); + if (hnvml) + wrap_nvml_destroy(hnvml); #endif exit(reason); } @@ -463,11 +452,11 @@ static bool work_decode(const json_t *val, struct work *work) if (unlikely(!jobj_binary(val, "data", work->data, sizeof(work->data)))) { applog(LOG_ERR, "JSON inval data"); - goto err_out; + return false; } if (unlikely(!jobj_binary(val, "target", work->target, sizeof(work->target)))) { applog(LOG_ERR, "JSON inval target"); - goto err_out; + return false; } if (opt_algo == ALGO_HEAVY) { if (unlikely(!jobj_binary(val, "maxvote", &work->maxvote, sizeof(work->maxvote)))) { @@ -494,9 +483,6 @@ static bool work_decode(const json_t *val, struct work *work) cbin2hex(work->job_id, (const char*)&work->data[17], 4); return true; - -err_out: - return false; } /** @@ -2140,8 +2126,8 @@ int main(int argc, char *argv[]) } #ifdef USE_WRAPNVML - nvmlh = wrap_nvml_create(); - if (nvmlh) { + hnvml = wrap_nvml_create(); + if (hnvml) { // todo: link threads info gpu applog(LOG_INFO, "NVML GPU monitoring enabled."); } else { diff --git a/ccminer.vcxproj b/ccminer.vcxproj index f14e5bc..b964bf9 100644 --- a/ccminer.vcxproj +++ b/ccminer.vcxproj @@ -230,18 +230,18 @@ - - /Tp %(AdditionalOptions) + false Full - + + @@ -274,10 +274,6 @@ - - /Tp %(AdditionalOptions) - Full - diff --git a/ccminer.vcxproj.filters b/ccminer.vcxproj.filters index 51cb1de..8a819a6 100644 --- a/ccminer.vcxproj.filters +++ b/ccminer.vcxproj.filters @@ -93,10 +93,10 @@ Source Files\gettimeofday - + Source Files - + Source Files diff --git a/configure.ac b/configure.ac index e6418ed..1ebfd65 100644 --- a/configure.ac +++ b/configure.ac @@ -2,7 +2,7 @@ AC_INIT([ccminer], [1.4.9]) AC_PREREQ([2.59c]) AC_CANONICAL_SYSTEM -AC_CONFIG_SRCDIR([cpu-miner.c]) +AC_CONFIG_SRCDIR([ccminer.cpp]) AM_INIT_AUTOMAKE([foreign subdir-objects]) AC_CONFIG_HEADERS([cpuminer-config.h]) diff --git a/cuda.cpp b/cuda.cpp index 6d03f66..1a7fc38 100644 --- a/cuda.cpp +++ b/cuda.cpp @@ -27,7 +27,7 @@ extern int device_map[8]; extern int device_sm[8]; // CUDA Devices on the System -extern "C" int cuda_num_devices() +int cuda_num_devices() { int version; cudaError_t err = cudaDriverGetVersion(&version); @@ -54,7 +54,7 @@ extern "C" int cuda_num_devices() return GPU_N; } -extern "C" void cuda_devicenames() +void cuda_devicenames() { cudaError_t err; int GPU_N; @@ -76,7 +76,7 @@ extern "C" void cuda_devicenames() } // Can't be called directly in cpu-miner.c -extern "C" void cuda_devicereset() +void cuda_devicereset() { cudaDeviceReset(); } @@ -103,7 +103,7 @@ static bool substringsearch(const char *haystack, const char *needle, int &match } // CUDA Gerät nach Namen finden (gibt Geräte-Index zurück oder -1) -extern "C" int cuda_finddevice(char *name) +int cuda_finddevice(char *name) { int num = cuda_num_devices(); int match = 0; diff --git a/hashlog.cpp b/hashlog.cpp index 025ba14..f52af1d 100644 --- a/hashlog.cpp +++ b/hashlog.cpp @@ -63,8 +63,7 @@ extern "C" uint32_t hashlog_already_submittted(char* jobid, uint32_t nonce) extern "C" void hashlog_remember_submit(char* jobid, uint32_t nonce, uint32_t scanned_from) { uint64_t njobid = hextouint(jobid); - uint64_t keyall = (njobid << 32); - uint64_t key = keyall + nonce; + uint64_t key = (njobid << 32) + nonce; hashlog_data data; memset(&data, 0, sizeof(data)); @@ -80,12 +79,12 @@ extern "C" void hashlog_remember_submit(char* jobid, uint32_t nonce, uint32_t sc extern "C" void hashlog_remember_scan_range(char* jobid, uint32_t scanned_from, uint32_t scanned_to) { uint64_t njobid = hextouint(jobid); - uint64_t keyall = (njobid << 32); + uint64_t key = (njobid << 32); uint64_t range = hashlog_get_scan_range(jobid); hashlog_data data; // global scan range of a job - data = tlastshares[keyall]; + data = tlastshares[key]; if (range == 0) { memset(&data, 0, sizeof(data)); } else { @@ -110,7 +109,7 @@ extern "C" void hashlog_remember_scan_range(char* jobid, uint32_t scanned_from, data.tm_upd = (uint32_t) time(NULL); - tlastshares[keyall] = data; + tlastshares[key] = data; /* applog(LOG_BLUE, "job %s range : %x %x -> %x %x", jobid, scanned_from, scanned_to, data.scanned_from, data.scanned_to); */ } @@ -124,13 +123,14 @@ extern "C" uint64_t hashlog_get_scan_range(char* jobid) uint64_t ret = 0; uint64_t njobid = hextouint(jobid); uint64_t keypfx = (njobid << 32); + uint64_t keymsk = (0xffffffffULL << 32); hashlog_data data; data.scanned_from = 0; data.scanned_to = 0; std::map::iterator i = tlastshares.begin(); while (i != tlastshares.end()) { - if ((keypfx & i->first) == keypfx && i->second.scanned_to > ret) { + if ((keymsk & i->first) == keypfx && i->second.scanned_to > ret) { if (i->second.scanned_to > data.scanned_to) data.scanned_to = i->second.scanned_to; if (i->second.scanned_from < data.scanned_from || data.scanned_from == 0) diff --git a/nvml.cpp b/nvml.cpp index 6700567..5663c53 100644 --- a/nvml.cpp +++ b/nvml.cpp @@ -19,7 +19,7 @@ #include #include -#include +#include #ifndef _MSC_VER #include #endif @@ -28,13 +28,21 @@ #include "cuda_runtime.h" #include "nvml.h" +extern wrap_nvml_handle *hnvml; +extern int device_map[8]; + /* * Wrappers to emulate dlopen() on other systems like Windows */ #if defined(_MSC_VER) || defined(_WIN32) || defined(_WIN64) #include static void *wrap_dlopen(const char *filename) { - return (void *)LoadLibrary(filename); + HMODULE h = LoadLibrary(filename); + if (!h && opt_debug) { + applog(LOG_DEBUG, "dlopen(%d): failed to load %s", + GetLastError(), filename); + } + return (void*)h; } static void *wrap_dlsym(void *h, const char *sym) { return (void *)GetProcAddress((HINSTANCE)h, sym); @@ -46,9 +54,16 @@ #else /* assume we can use dlopen itself... */ #include + #include static void *wrap_dlopen(const char *filename) { - return dlopen(filename, RTLD_NOW); + void *h = dlopen(filename, RTLD_NOW); + if (h == NULL && opt_debug) { + applog(LOG_DEBUG, "dlopen(%d): failed to load %s", + errno, filename); + } + return (void*)h; } + static void *wrap_dlsym(void *h, const char *sym) { return dlsym(h, sym); } @@ -57,7 +72,7 @@ } #endif -#if defined(__cplusplus) +#ifdef __cplusplus extern "C" { #endif @@ -66,51 +81,27 @@ wrap_nvml_handle * wrap_nvml_create() int i=0; wrap_nvml_handle *nvmlh = NULL; - /* - * We use hard-coded library installation locations for the time being... - * No idea where or if libnvidia-ml.so is installed on MacOS X, a - * deep scouring of the filesystem on one of the Mac CUDA build boxes - * I used turned up nothing, so for now it's not going to work on OSX. - */ -#if defined(_WIN64) - /* 64-bit Windows */ -#define libnvidia_ml "%PROGRAMFILES%/NVIDIA Corporation/NVSMI/nvml.dll" -#elif defined(_WIN32) || defined(_MSC_VER) - /* 32-bit Windows */ -#define libnvidia_ml "%PROGRAMFILES%/NVIDIA Corporation/NVSMI/nvml.dll" -#elif defined(__linux) && (defined(__i386__) || defined(__ARM_ARCH_7A__)) - /* 32-bit linux assumed */ -#define libnvidia_ml "/usr/lib32/libnvidia-ml.so" -#elif defined(__linux) - /* 64-bit linux assumed */ -#define libnvidia_ml "/usr/lib/libnvidia-ml.so" +#if defined(WIN32) + /* Windows (do not use slashes, else ExpandEnvironmentStrings will mix them) */ +#define libnvidia_ml "%PROGRAMFILES%\\NVIDIA Corporation\\NVSMI\\nvml.dll" #else -#error "Unrecognized platform: need NVML DLL path for this platform..." + /* linux assumed */ +#define libnvidia_ml "libnvidia-ml.so" #endif -#if WIN32 char tmp[512]; - ExpandEnvironmentStringsA(libnvidia_ml, tmp, sizeof(tmp)); +#ifdef WIN32 + ExpandEnvironmentStrings(libnvidia_ml, tmp, sizeof(tmp)); #else - char tmp[512] = libnvidia_ml; + strcpy(tmp, libnvidia_ml); #endif void *nvml_dll = wrap_dlopen(tmp); if (nvml_dll == NULL) { #ifdef WIN32 - char lib[] = "nvml.dll"; -#else - char lib[64] = { '\0' }; - snprintf(lib, sizeof(lib), "%s", basename(tmp)); - /* try dlopen without path, here /usr/lib/nvidia-340/libnvidia-ml.so */ + nvml_dll = wrap_dlopen("nvml.dll"); + if (nvml_dll == NULL) #endif - nvml_dll = wrap_dlopen(lib); - if (opt_debug) - applog(LOG_DEBUG, "dlopen: %s=%p", lib, nvml_dll); - } - if (nvml_dll == NULL) { - if (opt_debug) - applog(LOG_DEBUG, "dlopen(%d): failed to load %s", errno, tmp); return NULL; } @@ -120,9 +111,10 @@ wrap_nvml_handle * wrap_nvml_create() nvmlh->nvmlInit = (wrap_nvmlReturn_t (*)(void)) wrap_dlsym(nvmlh->nvml_dll, "nvmlInit_v2"); - if (!nvmlh->nvmlInit) + if (!nvmlh->nvmlInit) { nvmlh->nvmlInit = (wrap_nvmlReturn_t (*)(void)) wrap_dlsym(nvmlh->nvml_dll, "nvmlInit"); + } nvmlh->nvmlDeviceGetCount = (wrap_nvmlReturn_t (*)(int *)) wrap_dlsym(nvmlh->nvml_dll, "nvmlDeviceGetCount_v2"); nvmlh->nvmlDeviceGetHandleByIndex = (wrap_nvmlReturn_t (*)(int, wrap_nvmlDevice_t *)) @@ -153,11 +145,10 @@ wrap_nvml_handle * wrap_nvml_create() nvmlh->nvmlDeviceGetPciInfo == NULL || nvmlh->nvmlDeviceGetName == NULL || nvmlh->nvmlDeviceGetTemperature == NULL || - nvmlh->nvmlDeviceGetFanSpeed == NULL || - nvmlh->nvmlDeviceGetPowerUsage == NULL) + nvmlh->nvmlDeviceGetFanSpeed == NULL) { if (opt_debug) - applog(LOG_DEBUG, "Failed to obtain all required NVML function pointers"); + applog(LOG_DEBUG, "Failed to obtain required NVML function pointers"); wrap_dlclose(nvmlh->nvml_dll); free(nvmlh); return NULL; @@ -342,14 +333,11 @@ int wrap_nvml_destroy(wrap_nvml_handle *nvmlh) /* api functions */ -extern wrap_nvml_handle *nvmlh; -extern int device_map[8]; - unsigned int gpu_fanpercent(struct cgpu_info *gpu) { unsigned int pct = 0; - if (nvmlh) { - wrap_nvml_get_fanpcnt(nvmlh, device_map[gpu->thr_id], &pct); + if (hnvml) { + wrap_nvml_get_fanpcnt(hnvml, device_map[gpu->thr_id], &pct); } return pct; } @@ -357,9 +345,9 @@ unsigned int gpu_fanpercent(struct cgpu_info *gpu) double gpu_temp(struct cgpu_info *gpu) { double tc = 0.0; - if (nvmlh) { + if (hnvml) { unsigned int tmp = 0; - wrap_nvml_get_tempC(nvmlh, device_map[gpu->thr_id], &tmp); + wrap_nvml_get_tempC(hnvml, device_map[gpu->thr_id], &tmp); tc = (double) tmp; } return tc; @@ -368,8 +356,8 @@ double gpu_temp(struct cgpu_info *gpu) unsigned int gpu_clock(struct cgpu_info *gpu) { unsigned int freq = 0; - if (nvmlh) { - wrap_nvml_get_clock(nvmlh, device_map[gpu->thr_id], NVML_CLOCK_SM, &freq); + if (hnvml) { + wrap_nvml_get_clock(hnvml, device_map[gpu->thr_id], NVML_CLOCK_SM, &freq); } return freq; } @@ -377,8 +365,8 @@ unsigned int gpu_clock(struct cgpu_info *gpu) unsigned int gpu_power(struct cgpu_info *gpu) { unsigned int mw = 0; - if (nvmlh) { - wrap_nvml_get_power_usage(nvmlh, device_map[gpu->thr_id], &mw); + if (hnvml) { + wrap_nvml_get_power_usage(hnvml, device_map[gpu->thr_id], &mw); } return mw; } @@ -386,9 +374,8 @@ unsigned int gpu_power(struct cgpu_info *gpu) int gpu_pstate(struct cgpu_info *gpu) { int pstate = 0; - if (nvmlh) { - wrap_nvml_get_pstate(nvmlh, device_map[gpu->thr_id], &pstate); - //gpu->gpu_pstate = pstate; + if (hnvml) { + wrap_nvml_get_pstate(hnvml, device_map[gpu->thr_id], &pstate); } return pstate; } @@ -433,7 +420,7 @@ int gpu_pstate(struct cgpu_info *gpu) * nvmlDeviceGetFanSpeed nvmlDeviceGetGpuOperationMode nvmlDeviceGetHandleByIndex - nvmlDeviceGetHandleByIndex_v2 +* nvmlDeviceGetHandleByIndex_v2 nvmlDeviceGetHandleByPciBusId nvmlDeviceGetHandleByPciBusId_v2 nvmlDeviceGetHandleBySerial @@ -450,7 +437,7 @@ int gpu_pstate(struct cgpu_info *gpu) nvmlDeviceGetMinorNumber nvmlDeviceGetMultiGpuBoard nvmlDeviceGetName - nvmlDeviceGetPciInfo +* nvmlDeviceGetPciInfo nvmlDeviceGetPciInfo_v2 * nvmlDeviceGetPerformanceState nvmlDeviceGetPersistenceMode @@ -476,4 +463,4 @@ int gpu_pstate(struct cgpu_info *gpu) nvmlDeviceGetVbiosVersion nvmlDeviceGetViolationStatus -*/ \ No newline at end of file +*/ diff --git a/stats.cpp b/stats.cpp index 043eead..fd4c772 100644 --- a/stats.cpp +++ b/stats.cpp @@ -72,13 +72,14 @@ extern "C" double stats_get_speed(int thr_id) { uint64_t thr = (0xff && thr_id); uint64_t keypfx = (thr << 56); + uint64_t keymsk = (0xffULL << 56); double speed = 0.0; int records = 0; std::map::reverse_iterator i = tlastscans.rbegin(); while (i != tlastscans.rend() && records < opt_statsavg) { if (!i->second.ignored) - if (thr_id == -1 || (keypfx & i->first) == keypfx) { + if (thr_id == -1 || (keymsk & i->first) == keypfx) { if (i->second.hashcount > 1000) { speed += i->second.hashrate; records++; diff --git a/util.c b/util.cpp similarity index 99% rename from util.c rename to util.cpp index f1dd63c..245f2cc 100644 --- a/util.c +++ b/util.cpp @@ -90,7 +90,7 @@ void applog(int prio, const char *fmt, ...) va_copy(ap2, ap); len = vsnprintf(NULL, 0, fmt, ap2) + 1; va_end(ap2); - buf = alloca(len); + buf = (char*) alloca(len); if (vsnprintf(buf, len, fmt, ap) >= 0) syslog(prio, "%s", buf); } @@ -1473,7 +1473,7 @@ extern void applog_compare_hash(uchar *hash, uchar *hash2) char s[256] = ""; int len = 0; for (int i=0; i < 32; i += 4) { - char *color = memcmp(hash+i, hash2+i, 4) ? CL_WHT : CL_GRY; + const char *color = memcmp(hash+i, hash2+i, 4) ? CL_WHT : CL_GRY; len += sprintf(s+len, "%s%02x%02x%02x%02x " CL_GRY, color, hash[i], hash[i+1], hash[i+2], hash[i+3]); s[len] = '\0';