From cdc29336f7f00fa8c7e4cf4596cb724d46ab2668 Mon Sep 17 00:00:00 2001 From: Tanguy Pruvot Date: Tue, 30 Sep 2014 09:37:11 +0200 Subject: [PATCH] stats: compute work difficulty from target --- cpu-miner.c | 30 +++++++++++++++++++++++++++--- miner.h | 26 ++++++++++++++++++++++++++ util.c | 2 +- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/cpu-miner.c b/cpu-miner.c index cc63e30..23196a1 100644 --- a/cpu-miner.c +++ b/cpu-miner.c @@ -370,6 +370,8 @@ struct work { uint64_t u64[1]; } noncerange; + double difficulty; + uint32_t scanned_from; uint32_t scanned_to; }; @@ -450,6 +452,27 @@ err_out: return false; } +/** + * Calculate the work difficulty as double + */ +static void calc_diff(struct work *work, int known) +{ + // sample for diff 32.53 : 00000007de5f0000 + const uint64_t diffone = 0xFFFF000000000000ull; + uint64_t *data64, d64; + char rtarget[32]; + + swab256(rtarget, work->target); + data64 = (uint64_t *)(rtarget + 3); /* todo: index (3) can be tuned here */ + d64 = swab64(*data64); + if (unlikely(!d64)) + d64 = 1; + work->difficulty = (double)diffone / d64; + if (opt_difficulty > 0.) { + work->difficulty /= opt_difficulty; + } +} + static int share_result(int result, const char *reason) { char s[345]; @@ -502,6 +525,7 @@ static bool submit_upstream_work(CURL *curl, struct work *work) applog(LOG_DEBUG, "DEBUG: stale work detected, discarding"); return true; } + calc_diff(work, 0); pthread_mutex_unlock(&g_work_lock); if (have_stratum) { @@ -1000,13 +1024,13 @@ static void *miner_thread(void *userdata) } if (memcmp(work.target, g_work.target, sizeof(work.target))) { + calc_diff(&g_work, 0); if (opt_debug) { uint64_t target64 = g_work.target[7] * 0x100000000ULL + g_work.target[6]; - applog(LOG_DEBUG, "job %s target change: %llx", g_work.job_id, target64); - applog_hash((uint8_t*) work.target); - applog_compare_hash((uint8_t*) g_work.target, (uint8_t*) work.target); + applog(LOG_DEBUG, "job %s target change: %llx (%.1f)", g_work.job_id, target64, g_work.difficulty); } memcpy(work.target, g_work.target, sizeof(work.target)); + work.difficulty = g_work.difficulty; (*nonceptr) = (0xffffffffUL / opt_n_threads) * thr_id; // 0 if single thr /* on new target, ignoring nonce, clear sent data (hashlog) */ if (memcmp(work.target, g_work.target, sizeof(work.target))) { diff --git a/miner.h b/miner.h index 664972b..62cff79 100644 --- a/miner.h +++ b/miner.h @@ -85,6 +85,8 @@ enum { #else #define bswap_32(x) ((((x) << 24) & 0xff000000u) | (((x) << 8) & 0x00ff0000u) \ | (((x) >> 8) & 0x0000ff00u) | (((x) >> 24) & 0x000000ffu)) +#define bswap_64(x) (((uint64_t) bswap_32((uint32_t)((x) & 0xffffffffu)) << 32) \ + | (uint64_t) bswap_32((uint32_t)((x) >> 32))) #endif static inline uint32_t swab32(uint32_t v) @@ -96,6 +98,30 @@ static inline uint32_t swab32(uint32_t v) #endif } +static inline uint64_t swab64(uint64_t v) +{ +#ifdef WANT_BUILTIN_BSWAP + return __builtin_bswap64(v); +#else + return bswap_64(v); +#endif +} + +static inline void swab256(void *dest_p, const void *src_p) +{ + uint32_t *dest = (uint32_t *) dest_p; + const uint32_t *src = (const uint32_t *) src_p; + + dest[0] = swab32(src[7]); + dest[1] = swab32(src[6]); + dest[2] = swab32(src[5]); + dest[3] = swab32(src[4]); + dest[4] = swab32(src[3]); + dest[5] = swab32(src[2]); + dest[6] = swab32(src[1]); + dest[7] = swab32(src[0]); +} + #ifdef HAVE_SYS_ENDIAN_H #include #endif diff --git a/util.c b/util.c index b7c7d37..4586ddd 100644 --- a/util.c +++ b/util.c @@ -388,7 +388,7 @@ json_t *json_rpc_call(CURL *curl, const char *url, upload_data.len = strlen(rpc_req); upload_data.pos = 0; sprintf(len_hdr, "Content-Length: %lu", (unsigned long) upload_data.len); - sprintf(hashrate_hdr, "X-Mining-Hashrate: %llu", global_hashrate); + sprintf(hashrate_hdr, "X-Mining-Hashrate: %llu", (unsigned long long) global_hashrate); headers = curl_slist_append(headers, "Content-Type: application/json"); headers = curl_slist_append(headers, len_hdr);