From 621aef78ae346e584204b8211fe2809ac5eb930b Mon Sep 17 00:00:00 2001 From: David Phillips Date: Fri, 5 May 2017 10:38:30 +1200 Subject: Refactor monitor loop to own function --- sand-leek.c | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) (limited to 'sand-leek.c') diff --git a/sand-leek.c b/sand-leek.c index 690da62..d199fcc 100644 --- a/sand-leek.c +++ b/sand-leek.c @@ -234,16 +234,38 @@ die_usage(const char *argv0) { exit(1); } +void +monitor_progress(unsigned long volatile *khash_count, int thread_count) { + int loops = 0; + int i = 0; + unsigned long khashes = 0; + + loops = 0; + while (sem_trywait(&working) && errno == EAGAIN) { + sleep(1); + loops++; + khashes = 0; + /* approximate hashes per second */ + for (i = 0; i < thread_count; i++) { + khashes += khash_count[i]; + } + fprintf(stderr, "Average rate: %.2f kH/s (%.2f kH/s/thread)\r", + (double)khashes / loops, + ((double)khashes / loops) / thread_count); + } + + /* line feed to finish off carriage return from hashrate fprintf */ + fputc('\n', stderr); +} + int main(int argc, char **argv) { int opt = '\0'; int thread_count = 1; - int loops = 0; int i = 0; size_t offset = 0; pthread_t *workers = NULL; unsigned long volatile *khash_count = NULL; - unsigned long khashes = 0; while ((opt = getopt(argc, argv, "t:s:")) != -1) { switch (opt) { @@ -297,23 +319,7 @@ main(int argc, char **argv) { } } - /* workers started; wait on one to finish */ - loops = 0; - while (sem_trywait(&working) && errno == EAGAIN) { - sleep(1); - loops++; - khashes = 0; - /* approximate hashes per second */ - for (i = 0; i < thread_count; i++) { - khashes += khash_count[i]; - } - fprintf(stderr, "Average rate: %.2f kH/s (%.2f kH/s/thread)\r", - (double)khashes / loops, - ((double)khashes / loops) / thread_count); - } - - /* line feed to finish off carriage return from hashrate fprintf */ - fputc('\n', stderr); + monitor_progress(khash_count, thread_count); for (i = 0; i < thread_count; i++) { pthread_join(workers[i], NULL); -- cgit v1.1