From 1a3794f3e3a9364c0a7e87452042c9908ea491d8 Mon Sep 17 00:00:00 2001 From: mailchuck Date: Sat, 7 Nov 2015 18:39:55 +0100 Subject: [PATCH] C PoW updates - move to subdirectory - get rid of compile warnings on windows - get number of threads from affinity (Windows, Linux) or core count (BSD/OSX) --- src/{ => bitmsghash}/Makefile | 0 src/{ => bitmsghash}/bitmsghash.cpp | 58 ++++++++++++++++++++++++----- src/proofofwork.py | 4 +- 3 files changed, 50 insertions(+), 12 deletions(-) rename src/{ => bitmsghash}/Makefile (100%) rename src/{ => bitmsghash}/bitmsghash.cpp (63%) diff --git a/src/Makefile b/src/bitmsghash/Makefile similarity index 100% rename from src/Makefile rename to src/bitmsghash/Makefile diff --git a/src/bitmsghash.cpp b/src/bitmsghash/bitmsghash.cpp similarity index 63% rename from src/bitmsghash.cpp rename to src/bitmsghash/bitmsghash.cpp index d7dc9973..c0a40509 100644 --- a/src/bitmsghash.cpp +++ b/src/bitmsghash/bitmsghash.cpp @@ -19,27 +19,23 @@ #if defined(__GNUC__) #define EXPORT __attribute__ ((__visibility__("default"))) - #define UINT intptr_t -#elif defined(WIN32) +#elif defined(_WIN32) #define EXPORT __declspec(dllexport) - #define UINT unsigned int #endif #define ntohll(x) ( ( (uint64_t)(ntohl( (unsigned int)((x << 32) >> 32) )) << 32) | ntohl( ((unsigned int)(x >> 32)) ) ) unsigned long long max_val; unsigned char *initialHash; - - -int numthreads = 8; unsigned long long successval = 0; +unsigned int numthreads = 0; + #ifdef _WIN32 DWORD WINAPI threadfunc(LPVOID lpParameter) { - DWORD incamt = (DWORD)lpParameter; #else void * threadfunc(void* param) { - UINT incamt = (UINT)param; #endif + unsigned int incamt = *((unsigned int*)lpParameter); SHA512_CTX sha; unsigned char buf[HASH_SIZE + sizeof(uint64_t)] = { 0 }; unsigned char output[HASH_SIZE] = { 0 }; @@ -67,25 +63,66 @@ void * threadfunc(void* param) { return NULL; } +void getnumthreads() +{ +#ifdef _WIN32 + DWORD_PTR dwProcessAffinity, dwSystemAffinity; +#elif __linux__ + cpu_set_t dwProcessAffinity; +#else + int dwProcessAffinity = 0; + int32_t core_count = 0; +#endif + unsigned int len = sizeof(dwProcessAffinity); + if (numthreads > 0) + return; +#ifdef _WIN32 + GetProcessAffinityMask(GetCurrentProcess(), &dwProcessAffinity, &dwSystemAffinity); +#elif __linux__ + sched_getaffinity(0, len, &dwProcessAffinity); +#else + if (sysctlbyname("hw.logicalcpu", &core_count, &len, 0, 0)) + numthreads = core_count; +#endif + for (unsigned int i = 0; i < len * 8; i++) + if (dwProcessAffinity & (1i64 << i)) { + numthreads++; + printf("Detected core on: %u\n", i); + } + printf("Affinity: %lx\n", (unsigned long) dwProcessAffinity); + printf("Number of threads: %i\n", (int)numthreads); +} + extern "C" EXPORT unsigned long long BitmessagePOW(unsigned char * starthash, unsigned long long target) { successval = 0; max_val = target; + getnumthreads(); initialHash = (unsigned char *)starthash; # ifdef _WIN32 HANDLE* threads = (HANDLE*)calloc(sizeof(HANDLE), numthreads); # else pthread_t* threads = (pthread_t*)calloc(sizeof(pthread_t), numthreads); struct sched_param schparam; +# ifdef __linux__ schparam.sched_priority = 0; +# else + schparam.sched_priority = PTHREAD_MIN_PRIORITY; # endif +# endif + unsigned int *threaddata = (unsigned int *)calloc(sizeof(unsigned int), numthreads); for (UINT i = 0; i < numthreads; i++) { + threaddata[i] = i; # ifdef _WIN32 - threads[i] = CreateThread(NULL, 0, threadfunc, (LPVOID)i, 0, NULL); + threads[i] = CreateThread(NULL, 0, threadfunc, (LPVOID)&threaddata[i], 0, NULL); SetThreadPriority(threads[i], THREAD_PRIORITY_IDLE); # else - pthread_create(&threads[i], NULL, threadfunc, (void*)i); + pthread_create(&threads[i], NULL, threadfunc, (void*)&threaddata[i]); +# ifdef __linux__ pthread_setschedparam(threads[i], SCHED_IDLE, &schparam); +# else + pthread_setschedparam(threads[i], SCHED_RR, &schparam) +# endif # endif } # ifdef _WIN32 @@ -96,5 +133,6 @@ extern "C" EXPORT unsigned long long BitmessagePOW(unsigned char * starthash, un } # endif free(threads); + free(threaddata); return successval; } diff --git a/src/proofofwork.py b/src/proofofwork.py index 93f6f2cd..0aaf6487 100644 --- a/src/proofofwork.py +++ b/src/proofofwork.py @@ -18,12 +18,12 @@ if "win32" == sys.platform: else: bitmsglib = 'bitmsghash64.dll' try: - bso = ctypes.WinDLL(os.path.join(curdir, bitmsglib)) + bso = ctypes.WinDLL(os.path.join(curdir, "bitmsghash", bitmsglib)) except: bso = None else: try: - bso = ctypes.CDLL(os.path.join(curdir, bitmsglib)) + bso = ctypes.CDLL(os.path.join(curdir, "bitmsghash", bitmsglib)) except: bso = None if bso: