Commit 44b9cf50e3f951ca66d20d899cda63a2c6aa230f

Con Kolivas 2013-09-26T12:11:25

Limit the duration we wait for reads in BF1 based on time already elapsed to account for other delays such as work restart messages or out of work.

diff --git a/driver-bitfury.c b/driver-bitfury.c
index 84ac35a..b0b41a1 100644
--- a/driver-bitfury.c
+++ b/driver-bitfury.c
@@ -189,8 +189,10 @@ static int64_t bitfury_scanhash(struct thr_info *thr, struct work *work,
 {
 	struct cgpu_info *bitfury = thr->cgpu;
 	struct bitfury_info *info = bitfury->device_data;
+	struct timeval tv_now;
 	int amount, i;
 	char buf[45];
+	int ms_diff;
 
 	buf[0] = 'W';
 	memcpy(buf + 1, work->midstate, 32);
@@ -199,14 +201,23 @@ static int64_t bitfury_scanhash(struct thr_info *thr, struct work *work,
 	/* New results may spill out from the latest work, making us drop out
 	 * too early so read whatever we get for the first half nonce and then
 	 * look for the results to prev work. */
-	usb_read_timeout(bitfury, info->buf, 512, &amount, 600, C_BF1_GETRES);
-	info->tot += amount;
+	cgtime(&tv_now);
+	ms_diff = 600 - ms_tdiff(&tv_now, &info->tv_start);
+	if (ms_diff > 0) {
+		usb_read_timeout(bitfury, info->buf, 512, &amount, ms_diff, C_BF1_GETRES);
+		info->tot += amount;
+	}
+
 	if (unlikely(thr->work_restart))
 		goto cascade;
 
 	/* Now look for the bulk of the previous work results, they will come
 	 * in a batch following the first data. */
-	usb_read_once_timeout(bitfury, info->buf + info->tot, 7, &amount, 1000, C_BF1_GETRES);
+	cgtime(&tv_now);
+	ms_diff = BF1WAIT - ms_tdiff(&tv_now, &info->tv_start);
+	if (unlikely(ms_diff < 10))
+		ms_diff = 10;
+	usb_read_once_timeout(bitfury, info->buf + info->tot, 7, &amount, ms_diff, C_BF1_GETRES);
 	info->tot += amount;
 	while (amount) {
 		usb_read_once_timeout(bitfury, info->buf + info->tot, 512, &amount, 10, C_BF1_GETRES);
@@ -218,6 +229,7 @@ static int64_t bitfury_scanhash(struct thr_info *thr, struct work *work,
 
 	/* Send work */
 	usb_write(bitfury, buf, 45, &amount, C_BF1_REQWORK);
+	cgtime(&info->tv_start);
 	/* Get response acknowledging work */
 	usb_read(bitfury, buf, 7, &amount, C_BF1_GETWORK);
 
diff --git a/driver-bitfury.h b/driver-bitfury.h
index 4cc1300..db13323 100644
--- a/driver-bitfury.h
+++ b/driver-bitfury.h
@@ -23,6 +23,7 @@ struct bitfury_info {
 	char buf[512];
 	int tot;
 	int nonces;
+	struct timeval tv_start;
 };
 
 #endif /* BITFURY_H */
diff --git a/util.c b/util.c
index c0feaaf..ecece48 100644
--- a/util.c
+++ b/util.c
@@ -1074,6 +1074,12 @@ double us_tdiff(struct timeval *end, struct timeval *start)
 	return end->tv_sec * 1000000 + end->tv_usec - start->tv_sec * 1000000 - start->tv_usec;
 }
 
+/* Returns the milliseconds difference between end and start times */
+int ms_tdiff(struct timeval *end, struct timeval *start)
+{
+	return end->tv_sec * 1000 + end->tv_usec / 1000 - start->tv_sec * 1000 - start->tv_usec / 1000;
+}
+
 /* Returns the seconds difference between end and start times as a double */
 double tdiff(struct timeval *end, struct timeval *start)
 {
diff --git a/util.h b/util.h
index 96157fa..5a3d968 100644
--- a/util.h
+++ b/util.h
@@ -98,6 +98,7 @@ void cgsleep_us_r(cgtimer_t *ts_start, int64_t us);
 int cgtimer_to_ms(cgtimer_t *cgt);
 void cgtimer_sub(cgtimer_t *a, cgtimer_t *b, cgtimer_t *res);
 double us_tdiff(struct timeval *end, struct timeval *start);
+int ms_tdiff(struct timeval *end, struct timeval *start);
 double tdiff(struct timeval *end, struct timeval *start);
 bool stratum_send(struct pool *pool, char *s, ssize_t len);
 bool sock_full(struct pool *pool);