Create a cgminer specific gettimeofday wrapper that is always called with tz set to NULL and increases the resolution on windows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
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);