Permalink
Browse files

Windows friendly replacement for gettimeofday

  • Loading branch information...
1 parent 574fe35 commit 5973b4c665c4a02d4bd5fce2370bd5cfaa821266 @TTimo TTimo committed Mar 31, 2013
Showing with 73 additions and 13 deletions.
  1. +38 −6 perf/local_thr.cpp
  2. +35 −7 perf/remote_thr.cpp
View
@@ -26,7 +26,12 @@
#include <string.h>
#include <time.h>
#include <limits.h>
+
+#include "platform.hpp"
+
+#ifndef ZMQ_HAVE_WINDOWS
#include <sys/time.h>
+#endif
#define ZMSG 1
#define DATA 0
@@ -41,9 +46,23 @@ struct US_TIMER{
/* Records the current timer state
*/
void tm_init( US_TIMER *t){
-
- if( gettimeofday( &t->time_now, NULL) < 0){ perror( "d_timer_init()");}
-
+#if defined ZMQ_HAVE_WINDOWS
+
+ // Get the high resolution counter's accuracy.
+ LARGE_INTEGER ticksPerSecond;
+ QueryPerformanceFrequency (&ticksPerSecond);
+
+ // What time is it?
+ LARGE_INTEGER tick;
+ if ( !QueryPerformanceCounter (&tick) ) { perror( "tm_init()" ); }
+
+ // Seconds
+ t->time_now.tv_sec = (long)( tick.QuadPart / ticksPerSecond.QuadPart );
+ // Microseconds
+ t->time_now.tv_usec = (long)( ( tick.QuadPart - t->time_now.tv_sec * ticksPerSecond.QuadPart ) * 1000000 / ticksPerSecond.QuadPart );
+#else
+ if( gettimeofday( &t->time_now, NULL) < 0){ perror( "tm_init()");}
+#endif
t->time_was = t->time_now;
}
@@ -54,9 +73,22 @@ void tm_init( US_TIMER *t){
float tm_secs( US_TIMER *t){
register float seconds;
-
- if( gettimeofday( &t->time_now, NULL) < 0){ perror( "d_timer_init()");}
-
+#if defined ZMQ_HAVE_WINDOWS
+ // Get the high resolution counter's accuracy.
+ LARGE_INTEGER ticksPerSecond;
+ QueryPerformanceFrequency (&ticksPerSecond);
+
+ // What time is it?
+ LARGE_INTEGER tick;
+ if ( !QueryPerformanceCounter (&tick) ) { perror( "tm_secs()" ); }
+
+ // Seconds
+ t->time_now.tv_sec = (long)( tick.QuadPart / ticksPerSecond.QuadPart );
+ // Microseconds
+ t->time_now.tv_usec = (long)( ( tick.QuadPart - t->time_now.tv_sec * ticksPerSecond.QuadPart ) * 1000000 / ticksPerSecond.QuadPart );
+#else
+ if( gettimeofday( &t->time_now, NULL) < 0){ perror( "tm_secs()");}
+#endif
seconds = ( ((float)( t->time_now.tv_sec - t->time_was.tv_sec)) +
(((float)( t->time_now.tv_usec - t->time_was.tv_usec)) / 1000000.0));
View
@@ -23,18 +23,18 @@
#include "../include/zmq_utils.h"
#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h>
#include <string.h>
#include <time.h>
#include <limits.h>
-#include <sys/time.h>
#include "platform.hpp"
#if defined ZMQ_HAVE_WINDOWS
#include <windows.h>
#include <process.h>
#else
+#include <unistd.h>
+#include <sys/time.h>
#include <pthread.h>
#endif
@@ -61,11 +61,25 @@ struct US_TIMER{
/* Records the current timer state
*/
void tm_init( US_TIMER *t){
+#if defined ZMQ_HAVE_WINDOWS
- if( gettimeofday( &t->time_now, NULL) < 0){ perror( "d_timer_init()");}
+ // Get the high resolution counter's accuracy.
+ LARGE_INTEGER ticksPerSecond;
+ QueryPerformanceFrequency (&ticksPerSecond);
- t->time_was = t->time_now;
+ // What time is it?
+ LARGE_INTEGER tick;
+ if ( !QueryPerformanceCounter (&tick) ) { perror( "tm_init()" ); }
+
+ // Seconds
+ t->time_now.tv_sec = (long)( tick.QuadPart / ticksPerSecond.QuadPart );
+ // Microseconds
+ t->time_now.tv_usec = (long)( ( tick.QuadPart - t->time_now.tv_sec * ticksPerSecond.QuadPart ) * 1000000 / ticksPerSecond.QuadPart );
+#else
+ if( gettimeofday( &t->time_now, NULL) < 0){ perror( "tm_init()");}
+#endif
+ t->time_was = t->time_now;
}
/* Returns the time passed in microsecond precision in seconds since last init
@@ -75,14 +89,28 @@ float tm_secs( US_TIMER *t){
register float seconds;
- if( gettimeofday( &t->time_now, NULL) < 0){ perror( "d_timer_init()");}
-
+#if defined ZMQ_HAVE_WINDOWS
+ // Get the high resolution counter's accuracy.
+ LARGE_INTEGER ticksPerSecond;
+ QueryPerformanceFrequency (&ticksPerSecond);
+
+ // What time is it?
+ LARGE_INTEGER tick;
+ if ( !QueryPerformanceCounter (&tick) ) { perror( "tm_secs()" ); }
+
+ // Seconds
+ t->time_now.tv_sec = (long)( tick.QuadPart / ticksPerSecond.QuadPart );
+ // Microseconds
+ t->time_now.tv_usec = (long)( ( tick.QuadPart - t->time_now.tv_sec * ticksPerSecond.QuadPart ) * 1000000 / ticksPerSecond.QuadPart );
+#else
+ if( gettimeofday( &t->time_now, NULL) < 0){ perror( "tm_secs()");}
+#endif
seconds = ( ((float)( t->time_now.tv_sec - t->time_was.tv_sec)) +
(((float)( t->time_now.tv_usec - t->time_was.tv_usec)) / 1000000.0));
t->time_was = t->time_now;
- return( seconds);
+ return seconds;
}
void my_free (void *data, void *hint)

3 comments on commit 5973b4c

Contributor

steve-o replied Apr 1, 2013

Why not just revert commit a22714d? I see no value reproducing the zmq::clock_t code here.

Contributor

TTimo replied Apr 1, 2013

I have no opinion about removing code that landed more than a month ago. I assume that if it's still there, it has some useful purpose. It seemed appropriate to do some simple fixes so it would compile on Windows. I am not sure what those two tools do.

Contributor

steve-o replied Apr 1, 2013

I'm waiting on that commit for feedback on what constitutes "better timing".

Please sign in to comment.