Commit 3d7f824363dbae965f273ed0f88e626ece3b65e6

Con Kolivas 2013-04-21T19:02:44

Create a cgminer specific gettimeofday wrapper that is always called with tz set to NULL and increases the resolution on windows.

diff --git a/util.c b/util.c
index 0433d0b..131cbc8 100644
--- a/util.c
+++ b/util.c
@@ -258,7 +258,7 @@ static void last_nettime(struct timeval *last)
 static void set_nettime(void)
 {
 	wr_lock(&netacc_lock);
-	gettimeofday(&nettime, NULL);
+	cgtime(&nettime);
 	wr_unlock(&netacc_lock);
 }
 
@@ -382,7 +382,7 @@ json_t *json_rpc_call(CURL *curl, const char *url,
 			long long now_msecs, last_msecs;
 			struct timeval now, last;
 
-			gettimeofday(&now, NULL);
+			cgtime(&now);
 			last_nettime(&last);
 			now_msecs = (long long)now.tv_sec * 1000;
 			now_msecs += now.tv_usec / 1000;
@@ -842,6 +842,20 @@ void nmsleep(unsigned int msecs)
 #endif
 }
 
+/* This is a cgminer gettimeofday wrapper. Since we always call gettimeofday
+ * with tz set to NULL, and windows' default resolution is only 15ms, this
+ * gives us higher resolution times on windows. */
+void cgtime(struct timeval *tv)
+{
+#ifdef WIN32
+	timeBeginPeriod(1);
+#endif
+	gettimeofday(tv, NULL);
+#ifdef WIN32
+	timeEndPeriod(1);
+#endif
+}
+
 /* Returns the microseconds difference between end and start times as a double */
 double us_tdiff(struct timeval *end, struct timeval *start)
 {
@@ -1054,7 +1068,7 @@ char *recv_line(struct pool *pool)
 		enum recv_ret ret = RECV_OK;
 		struct timeval rstart, now;
 
-		gettimeofday(&rstart, NULL);
+		cgtime(&rstart);
 		if (!socket_full(pool, true)) {
 			applog(LOG_DEBUG, "Timed out waiting for data on socket_full");
 			goto out;
@@ -1079,7 +1093,7 @@ char *recv_line(struct pool *pool)
 			slen = strlen(s);
 			recalloc_sock(pool, slen);
 			strcat(pool->sockbuf, s);
-			gettimeofday(&now, NULL);
+			cgtime(&now);
 		} while (tdiff(&now, &rstart) < 60 && !strstr(pool->sockbuf, "\n"));
 		mutex_unlock(&pool->stratum_lock);
 
diff --git a/util.h b/util.h
index 9093b76..eeaa0b7 100644
--- a/util.h
+++ b/util.h
@@ -45,6 +45,7 @@
 struct pool;
 enum dev_reason;
 struct cgpu_info;
+void cgtime(struct timeval *tv);
 bool stratum_send(struct pool *pool, char *s, ssize_t len);
 bool sock_full(struct pool *pool);
 char *recv_line(struct pool *pool);