Commit 923b9e0d86d54d5d36b8a5c53b1b45d6d89d13c1

Kano 2012-09-18T11:19:57

Calculate work difficulty for each getwork and display with WorkTime debug

diff --git a/README b/README
index 36de306..1591d66 100644
--- a/README
+++ b/README
@@ -477,12 +477,13 @@ and pipe the output directly to that command.
 The WorkTime details 'debug' option adds details on the end of each line
 displayed for Accepted or Rejected work done. An example would be:
 
- <-00000059.ed4834a3 M:X G:17:02:38:0.405 C:1.855 (2.995) W:3.440 (0.000) S:0.461 R:17:02:47
+ <-00000059.ed4834a3 M:X D:1.0 G:17:02:38:0.405 C:1.855 (2.995) W:3.440 (0.000) S:0.461 R:17:02:47
 
 The first 2 hex codes are the previous block hash, the rest are reported in
 seconds unless stated otherwise:
 The previous hash is followed by the getwork mode used M:X where X is one of
 P:Pool, T:Test Pool, L:LP or B:Benchmark,
+then D:d.ddd is the difficulty required to get a share from the work,
 then G:hh:mm:ss:n.nnn, which is when the getwork or LP was sent to the pool and
 the n.nnn is how long it took to reply,
 followed by 'O' on it's own if it is an original getwork, or 'C:n.nnn' if it was
diff --git a/cgminer.c b/cgminer.c
index 2b1a210..259a04d 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -1833,6 +1833,7 @@ static bool submit_upstream_work(const struct work *work, CURL *curl, bool resub
 			double work_to_submit = tdiff(&tv_submit,
 							(struct timeval *)&(work->tv_work_found));
 			double submit_time = tdiff(&tv_submit_reply, &tv_submit);
+			int diffplaces = 3;
 
 			tm = localtime(&(work->tv_getwork.tv_sec));
 			memcpy(&tm_getwork, tm, sizeof(struct tm));
@@ -1847,10 +1848,14 @@ static bool submit_upstream_work(const struct work *work, CURL *curl, bool resub
 			else
 				strcpy(workclone, "O");
 
-			sprintf(worktime, " <-%08lx.%08lx M:%c G:%02d:%02d:%02d:%1.3f %s (%1.3f) W:%1.3f (%1.3f) S:%1.3f R:%02d:%02d:%02d",
+			if (work->work_difficulty < 1)
+				diffplaces = 6;
+
+			sprintf(worktime, " <-%08lx.%08lx M:%c D:%1.*f G:%02d:%02d:%02d:%1.3f %s (%1.3f) W:%1.3f (%1.3f) S:%1.3f R:%02d:%02d:%02d",
 				(unsigned long)swab32(*(uint32_t *)&(work->data[28])),
 				(unsigned long)swab32(*(uint32_t *)&(work->data[24])),
-				work->getwork_mode, tm_getwork.tm_hour, tm_getwork.tm_min,
+				work->getwork_mode, diffplaces, work->work_difficulty,
+				tm_getwork.tm_hour, tm_getwork.tm_min,
 				tm_getwork.tm_sec, getwork_time, workclone,
 				getwork_to_work, work_time, work_to_submit, submit_time,
 				tm_submit_reply.tm_hour, tm_submit_reply.tm_min,
@@ -2026,6 +2031,25 @@ static inline struct pool *select_pool(bool lagging)
 	return pool;
 }
 
+static double DIFFEXACTONE = 26959946667150639794667015087019630673637144422540572481103610249216.0;
+
+/*
+ * Calculate the work share difficulty
+ */
+static void calc_diff(struct work *work)
+{
+	double targ;
+	int i;
+
+	targ = 0;
+	for (i = 31; i >= 0; i--) {
+		targ *= 256;
+		targ += work->target[i];
+	}
+
+	work->work_difficulty = DIFFEXACTONE / (targ ? : DIFFEXACTONE);
+}
+
 static void get_benchmark_work(struct work *work)
 {
 	// Use a random work block pulled from a pool
@@ -2041,6 +2065,7 @@ static void get_benchmark_work(struct work *work)
 	gettimeofday(&(work->tv_getwork), NULL);
 	memcpy(&(work->tv_getwork_reply), &(work->tv_getwork), sizeof(struct timeval));
 	work->getwork_mode = GETWORK_MODE_BENCHMARK;
+	calc_diff(work);
 }
 
 static bool get_upstream_work(struct work *work, CURL *curl)
@@ -2088,6 +2113,7 @@ static bool get_upstream_work(struct work *work, CURL *curl)
 	work->pool = pool;
 	work->longpoll = false;
 	work->getwork_mode = GETWORK_MODE_POOL;
+	calc_diff(work);
 	total_getworks++;
 	pool->getwork_requested++;
 
@@ -3897,6 +3923,7 @@ static bool pool_active(struct pool *pool, bool pinging)
 			memcpy(&(work->tv_getwork), &tv_getwork, sizeof(struct timeval));
 			memcpy(&(work->tv_getwork_reply), &tv_getwork_reply, sizeof(struct timeval));
 			work->getwork_mode = GETWORK_MODE_TESTPOOL;
+			calc_diff(work);
 			applog(LOG_DEBUG, "Pushing pooltest work to base pool");
 
 			tq_push(thr_info[stage_thr_id].q, work);
@@ -4519,6 +4546,7 @@ static void convert_to_work(json_t *val, int rolltime, struct pool *pool, struct
 	memcpy(&(work->tv_getwork), tv_lp, sizeof(struct timeval));
 	memcpy(&(work->tv_getwork_reply), tv_lp_reply, sizeof(struct timeval));
 	work->getwork_mode = GETWORK_MODE_LP;
+	calc_diff(work);
 
 	if (pool->enabled == POOL_REJECTING)
 		work->mandatory = true;
diff --git a/miner.h b/miner.h
index 74cc001..f3e9303 100644
--- a/miner.h
+++ b/miner.h
@@ -833,7 +833,9 @@ struct work {
 
 	unsigned int	work_block;
 	int		id;
-	UT_hash_handle hh;
+	UT_hash_handle	hh;
+
+	double		work_difficulty;
 
 	struct timeval	tv_getwork;
 	struct timeval	tv_getwork_reply;