aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Phillips <david@sighup.nz>2018-06-18 20:27:41 +1200
committerDavid Phillips <david@sighup.nz>2018-06-18 20:27:41 +1200
commit48923aec615908412d00034a4bd69a2200921794 (patch)
treef5ff82e2aa9080908719b9e1e92c517cc767dde2
parent8bf20f5eaa5c65072011535c6aa9b1b73bbb85a0 (diff)
downloadsand-leek-48923aec615908412d00034a4bd69a2200921794.tar.xz
Refactor human-friendly unit algorithms
-rw-r--r--Makefile2
-rw-r--r--sand-leek.c80
-rw-r--r--unit_label.c23
-rw-r--r--unit_label.h6
4 files changed, 58 insertions, 53 deletions
diff --git a/Makefile b/Makefile
index 3db9b2f..b130d15 100644
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@ all: sand-leek
sand-leek.o: endian.h onion_base32.h key_update.h colour.h
-sand-leek: sand-leek.o onion_base32.o key_update.o
+sand-leek: sand-leek.o onion_base32.o key_update.o unit_label.o
$(CC) -o $@ $^ $(LDFLAGS)
clean:
diff --git a/sand-leek.c b/sand-leek.c
index 143430a..03c236b 100644
--- a/sand-leek.c
+++ b/sand-leek.c
@@ -17,6 +17,7 @@
#include "onion_base32.h"
#include "key_update.h"
#include "colour.h"
+#include "unit_label.h"
#define VERSION "1"
@@ -42,6 +43,30 @@ static int raw_len;
static char bitmask;
static volatile char working;
+static const struct unit_label khash_labels[] = {
+ {1000, "k" },
+ {1000, "M" },
+ {1000, "G" },
+ {1000, "T" },
+ {1000, "P" },
+ {1000, "E" },
+ { 0, NULL}
+};
+
+static const struct unit_label time_labels[] = {
+ { 60, "second" },
+ { 60, "minute" },
+ { 24, "hour" },
+ {365.25, "day" },
+ { 1000, "year" },
+ { 1000, "thousand year"},
+ { 1000, "million year" },
+ { 1000, "billion year" },
+ { 1000, "trillion year"},
+ { 0, NULL }
+};
+
+
/* "Bare" eprintf that does not change colour, apply prefix, etc.
* Only directs information to the appropriate stream */
#define eprintf_bare(...) \
@@ -296,28 +321,7 @@ monitor_progress(unsigned long volatile *khashes, int thread_count) {
total_khashes += khashes[i];
}
- /* compute approximate total hashes for this run and format it
- * nicely with a unit and everything */
- /* FIXME factor out and apply this to the current hashrate display */
- if (total_khashes > 1e15) {
- hashes_nice = total_khashes / 1e15;
- hashes_nice_unit = "E";
- } else if (total_khashes > 1e12) {
- hashes_nice = total_khashes / 1e12;
- hashes_nice_unit = "P";
- } else if (total_khashes > 1e9) {
- hashes_nice = total_khashes / 1e9;
- hashes_nice_unit = "T";
- } else if (total_khashes > 1e6) {
- hashes_nice = total_khashes / 1e6;
- hashes_nice_unit = "G";
- } else if (total_khashes > 1e3) {
- hashes_nice = total_khashes / 1e3;
- hashes_nice_unit = "M";
- } else {
- hashes_nice = total_khashes;
- hashes_nice_unit = "k";
- }
+ hashes_nice = make_unit_whatsit(khash_labels, &hashes_nice_unit, total_khashes);
/* compute timestamp */
clock_gettime(SL_CLOCK_SOURCE, &now);
@@ -330,36 +334,8 @@ monitor_progress(unsigned long volatile *khashes, int thread_count) {
remaining = (est_khashes/(total_khashes-last_total_khashes)) - delta;
}
- /* FIXME factor out */
- /* this was supposed to be temporary, why is it still here? */
remaining_abs = fabs(remaining);
- if (remaining_abs < 60) {
- remaining_unit = "second";
- } else if (remaining_abs < 60*60) {
- remaining = (remaining + 30) / 60;
- remaining_unit = "minute";
- } else if (remaining_abs < 60*60*24) {
- remaining = (remaining + 1800) / 3600;
- remaining_unit = "hour";
- } else if (remaining_abs < 60*60*24*365.25) {
- remaining = (remaining + 43200) / 86400;
- remaining_unit = "day";
- } else if (remaining_abs < 60*60*24*365.25*1e3) {
- remaining = (remaining + (60*60*24*365.25)/2) / (60*60*24*365.25);
- remaining_unit = "year";
- } else if (remaining_abs < 60*60*24*365.25*1e6) {
- remaining = (remaining + (60*60*24*365.25*1e3)/2) / (60*60*24*365.25*1e3);
- remaining_unit = "thousand year";
- } else if (remaining_abs < 60*60*24*365.25*1e9) {
- remaining = (remaining + (60*60*24*365.25*1e6)/2) / (60*60*24*365.25*1e6);
- remaining_unit = "million year";
- } else if (remaining_abs < 60*60*24*365.25*1e12) {
- remaining = (remaining + (60*60*24*365.25*1e9)/2) / (60*60*24*365.25*1e9);
- remaining_unit = "billion year";
- } else if (remaining_abs < 60*60*24*365.25*1e15) {
- remaining = (remaining + (60*60*24*365.25*1e12)/2) / (60*60*24*365.25*1e12);
- remaining_unit = "trillion year";
- }
+ remaining_abs = make_unit_whatsit(time_labels, &remaining_unit, remaining_abs);
#ifndef SAND_LEEK_DISABLE_COLOUR
if (!no_ansi_esc) {
@@ -372,7 +348,7 @@ monitor_progress(unsigned long volatile *khashes, int thread_count) {
hashes_nice, hashes_nice_unit, (hashes_nice >= 1000 ? " (!!)" : ""),
total_khashes - last_total_khashes,
(double)(total_khashes - last_total_khashes) / thread_count,
- fabs(remaining), remaining_unit, (fabs(remaining) == 1 ? "" : "s" ),
+ remaining_abs, remaining_unit, (remaining_abs == 1.0 ? "" : "s" ),
(remaining < 0 ? "overdue" : "left")
);
sleep(1);
diff --git a/unit_label.c b/unit_label.c
new file mode 100644
index 0000000..22043ee
--- /dev/null
+++ b/unit_label.c
@@ -0,0 +1,23 @@
+#include <unistd.h>
+
+struct unit_label {
+ /* number of times this unit fits into the immediate larger one */
+ double count;
+
+ /* label for the unit*/
+ char *label;
+};
+
+double make_unit_whatsit(const struct unit_label l[], char **unit, double value) {
+ size_t i = 0;
+
+ for (i = 0; l[i+1].label; i++) {
+ if (l[i].count > value) {
+ break;
+ }
+ value /= l[i].count;
+ }
+
+ *unit = l[i].label;
+ return value;
+}
diff --git a/unit_label.h b/unit_label.h
new file mode 100644
index 0000000..7fe1f2b
--- /dev/null
+++ b/unit_label.h
@@ -0,0 +1,6 @@
+struct unit_label {
+ double lower_bound;
+ char *label;
+};
+
+double make_unit_whatsit(const struct unit_label l[], char **unit, double value);