From c41d9e7bb65adf80be13a87bb09338c6e529ef81 Mon Sep 17 00:00:00 2001 From: Erik Mossberg Date: Sat, 21 Feb 2015 16:02:28 +0100 Subject: [PATCH] hashrate: format units based on the value size --- ccminer.cpp | 9 ++++----- miner.h | 1 + util.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/ccminer.cpp b/ccminer.cpp index dc97d37..196e958 100644 --- a/ccminer.cpp +++ b/ccminer.cpp @@ -1483,9 +1483,8 @@ static void *miner_thread(void *userdata) /* output */ if (!opt_quiet && loopcnt) { - sprintf(s, thr_hashrates[thr_id] >= 1e6 ? "%.0f" : "%.2f", - 1e-3 * thr_hashrates[thr_id]); - applog(LOG_INFO, "GPU #%d: %s, %s kH/s", + format_hashrate(thr_hashrates[thr_id], s); + applog(LOG_INFO, "GPU #%d: %s, %s", device_map[thr_id], device_name[device_map[thr_id]], s); } @@ -1497,8 +1496,8 @@ static void *miner_thread(void *userdata) hashrate += stats_get_speed(i, thr_hashrates[i]); pthread_mutex_unlock(&stats_lock); if (opt_benchmark) { - sprintf(s, hashrate >= 1e6 ? "%.0f" : "%.2f", hashrate / 1000.); - applog(LOG_NOTICE, "Total: %s kH/s", s); + format_hashrate(hashrate, s); + applog(LOG_NOTICE, "Total: %s", s); } // X-Mining-Hashrate diff --git a/miner.h b/miner.h index de3e63f..9c97ee6 100644 --- a/miner.h +++ b/miner.h @@ -517,6 +517,7 @@ extern uint32_t gpus_intensity[MAX_GPUS]; #define CL_WHT "\x1B[01;37m" /* white */ +extern void format_hashrate(double hashrate, char *output); extern void applog(int prio, const char *fmt, ...); void get_defconfig_path(char *out, size_t bufsize, char *argv0); extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass, diff --git a/util.cpp b/util.cpp index 05aebe6..0ae77ef 100644 --- a/util.cpp +++ b/util.cpp @@ -173,6 +173,60 @@ void get_defconfig_path(char *out, size_t bufsize, char *argv0) #endif } +void format_hashrate(double hashrate, char *output) +{ + char prefix; + + if (hashrate < 1e3) + prefix = ' '; + else if (hashrate < 1e6) + { + prefix = 'k'; + hashrate *= 1e-3; + } + else if (hashrate < 1e9) + { + prefix = 'M'; + hashrate *= 1e-6; + } + else if (hashrate < 1e12) + { + prefix = 'G'; + hashrate *= 1e-9; + } + else if (hashrate < 1e15) + { + prefix = 'T'; + hashrate *= 1e-12; + } + else if (hashrate < 1e18) + { + prefix = 'P'; + hashrate *= 1e-15; + } + else if (hashrate < 1e21) + { + prefix = 'E'; + hashrate *= 1e-18; + } + else if (hashrate < 1e24) + { + prefix = 'Z'; + hashrate *= 1e-21; + } + else + { + prefix = 'Y'; + hashrate *= 1e-24; + } + + sprintf( + output, + prefix == ' ' ? "%.0f%cH/s" : "%.2f %cH/s", + hashrate, prefix + ); +} + static void databuf_free(struct data_buffer *db) { if (!db)