aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-01-14 13:30:35 -0800
committerGarrett D'Amore <garrett@damore.org>2017-01-14 13:30:35 -0800
commit2eace190e7715e6c48f33897acf7537576f7a412 (patch)
treeb8c778ba1216dbda9d7b0eef42e3f6bc5ce4441a
parentf4ce5a285167e7656037096f77f04ab80a010453 (diff)
downloadnng-2eace190e7715e6c48f33897acf7537576f7a412.tar.gz
nng-2eace190e7715e6c48f33897acf7537576f7a412.tar.bz2
nng-2eace190e7715e6c48f33897acf7537576f7a412.zip
Use Windows tick clock instead of Performance counters.
Since we use the tick counter to sleep, we should use the same clock for validation. The problem is that the high performance tick counter on the CPU may be slightly out of agreement with the windows clock. Furthermore, the tick counter is probably lots faster to retrieve since it is already updated, and needn't be recalculated each time. (We should consider just switching to millisecond clock resolution internally as well. It turns out that I don't think that timers that are shorter than 1ms are very useful.)
-rw-r--r--src/platform/windows/win_clock.c13
-rw-r--r--src/platform/windows/win_thread.c2
-rw-r--r--tests/platform.c2
-rw-r--r--tests/sock.c43
4 files changed, 38 insertions, 22 deletions
diff --git a/src/platform/windows/win_clock.c b/src/platform/windows/win_clock.c
index 3309be24..bdb2f550 100644
--- a/src/platform/windows/win_clock.c
+++ b/src/platform/windows/win_clock.c
@@ -14,17 +14,8 @@
nni_time
nni_plat_clock(void)
{
- LARGE_INTEGER freq;
- LARGE_INTEGER count;
- double rate;
-
- QueryPerformanceFrequency(&freq);
- QueryPerformanceCounter(&count);
-
- // convert to ticks per us
- rate = (double) freq.QuadPart / 1000000.0;
-
- return ((nni_time) (count.QuadPart / rate));
+ // We are limited by the system clock, but that is ok.
+ return (GetTickCount64()*1000);
}
diff --git a/src/platform/windows/win_thread.c b/src/platform/windows/win_thread.c
index 631883fc..4d47f18e 100644
--- a/src/platform/windows/win_thread.c
+++ b/src/platform/windows/win_thread.c
@@ -95,7 +95,7 @@ nni_plat_cv_until(nni_plat_cv *cv, nni_time until)
msec = 0;
} else {
// times are in usec, but win32 wants millis
- msec = (until - now)/1000;
+ msec = ((until - now) + 999)/1000;
}
ok = SleepConditionVariableCS(&cv->cv, cv->cs, msec);
diff --git a/tests/platform.c b/tests/platform.c
index 7bf1d0d1..0ac881d6 100644
--- a/tests/platform.c
+++ b/tests/platform.c
@@ -19,7 +19,7 @@ uint64_t
getms(void)
{
#ifdef _WIN32
- return (GetTickCount()) ;
+ return (GetTickCount64()) ;
#else
static time_t epoch;
struct timeval tv;
diff --git a/tests/sock.c b/tests/sock.c
index acc4c437..8863023c 100644
--- a/tests/sock.c
+++ b/tests/sock.c
@@ -12,6 +12,34 @@
#include <string.h>
+#ifndef _WIN32
+#include <sys/time.h>
+#endif
+
+uint64_t
+getms(void)
+{
+#ifdef _WIN32
+ return (GetTickCount64()) ;
+#else
+ static time_t epoch;
+ struct timeval tv;
+
+ if (epoch == 0) {
+ epoch = time(NULL);
+ }
+ gettimeofday(&tv, NULL);
+
+ if (tv.tv_sec < epoch) {
+ // Broken clock.
+ // This will force all other timing tests to fail
+ return (0);
+ }
+ tv.tv_sec -= epoch;
+ return (((uint64_t)(tv.tv_sec ) * 1000) + (tv.tv_usec / 1000));
+#endif
+}
+
TestMain("Socket Operations", {
Convey("We are able to open a PAIR socket", {
int rv;
@@ -41,9 +69,7 @@ TestMain("Socket Operations", {
int64_t when = 500000;
uint64_t now;
- // We cheat to get access to the core's clock.
- extern uint64_t nni_clock(void);
- now = nni_clock();
+ now = getms();
rv = nng_setopt(sock, NNG_OPT_RCVTIMEO, &when,
sizeof (when));
@@ -51,8 +77,8 @@ TestMain("Socket Operations", {
rv = nng_recvmsg(sock, &msg, 0);
So(rv == NNG_ETIMEDOUT);
So(msg == NULL);
- So(nni_clock() >= (now + when));
- So(nni_clock() < (now + (when * 2)));
+ So(getms() >= (now + (when/1000)));
+ So(getms() < (now + ((when/1000) * 2)));
})
Convey("Recv nonblock with no pipes gives EAGAIN", {
@@ -70,16 +96,15 @@ TestMain("Socket Operations", {
// We cheat to get access to the core's clock.
So(nng_msg_alloc(&msg, 0) == 0);
So(msg != NULL);
- extern uint64_t nni_clock(void);
- now = nni_clock();
+ now = getms();
rv = nng_setopt(sock, NNG_OPT_SNDTIMEO, &when,
sizeof (when));
So(rv == 0);
rv = nng_sendmsg(sock, msg, 0);
So(rv == NNG_ETIMEDOUT);
- So(nni_clock() > (now + 500000));
- So(nni_clock() < (now + 1000000));
+ So(getms() >= (now + (when/1000)));
+ So(getms() < (now + ((when/1000) * 2)));
nng_msg_free(msg);
})