Commit 953ecd9e6a3fff4c26d25197bcd90848c948fb91

Paul Sheppard 2012-07-08T23:25:02

Change timeouts to time-vals for accuracy.

diff --git a/driver-bitforce.c b/driver-bitforce.c
index 276ffcf..9231583 100644
--- a/driver-bitforce.c
+++ b/driver-bitforce.c
@@ -21,11 +21,14 @@
 #include "miner.h"
 
 #define BITFORCE_SLEEP_MS 500
-#define BITFORCE_TIMEOUT_MS 10000
-#define BITFORCE_LONG_TIMEOUT_MS 15000
+#define BITFORCE_TIMEOUT_S 7
+#define BITFORCE_TIMEOUT_MS (BITFORCE_TIMEOUT_S * 1000)
+#define BITFORCE_LONG_TIMEOUT_S 15
+#define BITFORCE_LONG_TIMEOUT_MS (BITFORCE_LONG_TIMEOUT_S * 1000)
 #define BITFORCE_CHECK_INTERVAL_MS 10
 #define WORK_CHECK_INTERVAL_MS 50
 #define MAX_START_DELAY_US 100000
+#define tv_to_ms(tval) (tval.tv_sec * 1000 + tval.tv_usec / 1000)
 
 struct device_api bitforce_api;
 
@@ -332,6 +335,7 @@ re_send:
 		return false;
 	}
 
+	gettimeofday(&bitforce->work_start_tv, NULL);
 	return true;
 }
 
@@ -340,6 +344,8 @@ static uint64_t bitforce_get_result(struct thr_info *thr, struct work *work)
 	struct cgpu_info *bitforce = thr->cgpu;
 	int fdDev = bitforce->device_fd;
 	unsigned int delay_time_ms;
+	struct timeval elapsed;
+	struct timeval now;
 	char pdevbuf[0x100];
 	char *pnoncebuf;
 	uint32_t nonce;
@@ -348,7 +354,7 @@ static uint64_t bitforce_get_result(struct thr_info *thr, struct work *work)
 	if (!fdDev)
 		return 0;
 
-	while (bitforce->wait_ms < BITFORCE_LONG_TIMEOUT_MS) {
+	while (1) {
 		if (unlikely(thr->work_restart))
 			return 1;
 
@@ -357,6 +363,15 @@ static uint64_t bitforce_get_result(struct thr_info *thr, struct work *work)
 		BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
 		mutex_unlock(&bitforce->device_mutex);
 
+		gettimeofday(&now, NULL);
+		timersub(&now, &bitforce->work_start_tv, &elapsed);
+
+		if (elapsed.tv_sec >= BITFORCE_LONG_TIMEOUT_S) {
+			applog(LOG_ERR, "BFL%i: took %dms - longer than %dms", bitforce->device_id,
+				tv_to_ms(elapsed), BITFORCE_LONG_TIMEOUT_MS);
+			return 0;
+		}
+
 		if (pdevbuf[0] && strncasecmp(pdevbuf, "B", 1)) /* BFL does not respond during throttling */
 			break;
 
@@ -366,9 +381,9 @@ static uint64_t bitforce_get_result(struct thr_info *thr, struct work *work)
 		bitforce->wait_ms += delay_time_ms;
 	}
 
-	if (bitforce->wait_ms >= BITFORCE_TIMEOUT_MS) {
+	if (elapsed.tv_sec > BITFORCE_TIMEOUT_S) {
 		applog(LOG_ERR, "BFL%i: took %dms - longer than %dms", bitforce->device_id,
-		       bitforce->wait_ms, BITFORCE_TIMEOUT_MS);
+			tv_to_ms(elapsed), BITFORCE_TIMEOUT_MS);
 		bitforce->device_last_not_well = time(NULL);
 		bitforce->device_not_well_reason = REASON_DEV_OVER_HEAT;
 		bitforce->dev_over_heat_count++;
diff --git a/miner.h b/miner.h
index 50a1ca2..4acb8f9 100644
--- a/miner.h
+++ b/miner.h
@@ -320,6 +320,7 @@ struct cgpu_info {
 		int device_fd;
 	};
 #ifdef USE_BITFORCE
+	struct timeval work_start_tv;
 	unsigned int wait_ms;
 	unsigned int sleep_ms;
 	uint32_t nonces;