Skip to contentSkip to conversation

slirp: Use monotonic clock if available (v2)

PATCH In review Ed Swierk 2 participants 2 messages Jul 23, 2009 – Jul 25, 2009

lore
Share & tools
lore

Ed Swierk

author
#0 lore raw

PATCH 1/1 slirp: Use monotonic clock if available (v2)

Calling gettimeofday() to compute a time interval can cause problems if the system clock jumps forwards or backwards; replace updtime() with qemu_get_clock(rt_clock), which calls clock_gettime(CLOCK_MONOTONIC) if it is available.

Also remove some useless macros.

---

slirp/slirp.c +2 -25
index c68c6ab..3798780 100644
@@ -22,6 +22,7 @@
  * THE SOFTWARE.
  */
 #include "qemu-common.h"
+#include "qemu-timer.h"
 #include "qemu-char.h"
 #include "slirp.h"
 #include "hw/hw.h"
@@ -238,29 +239,6 @@ void slirp_cleanup(Slirp *slirp)
 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
 #define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
 
-/*
- * curtime kept to an accuracy of 1ms
- */
-#ifdef _WIN32
-static void updtime(void)
-{
-    struct _timeb tb;
-
-    _ftime(&tb);
-
-    curtime = tb.time * 1000 + tb.millitm;
-}
-#else
-static void updtime(void)
-{
-    struct timeval tv;
-
-    gettimeofday(&tv, NULL);
-
-    curtime = tv.tv_sec * 1000 + tv.tv_usec / 1000;
-}
-#endif
-
 void slirp_select_fill(int *pnfds,
                        fd_set *readfds, fd_set *writefds, fd_set *xfds)
 {
@@ -399,8 +377,7 @@ void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds,
     global_writefds = writefds;
     global_xfds = xfds;
 
-	/* Update time */
-	updtime();
+    curtime = qemu_get_clock(rt_clock);
 
     TAILQ_FOREACH(slirp, &slirp_instances, entry) {
 	/*
slirp/slirp.h +0 -4
index 22058cd..020412c 100644
@@ -108,10 +108,6 @@ typedef unsigned char u_int8_t;
 #include <arpa/inet.h>
 #endif
 
-#ifdef GETTIMEOFDAY_ONE_ARG
-#define gettimeofday(x, y) gettimeofday(x)
-#endif
-
 /* Systems lacking strdup() definition in <string.h>. */
 #if defined(ultrix)
 char *strdup(const char *);
slirp/slirp_config.h +0 -3
index ebaa99c..e977e77 100644
@@ -187,9 +187,6 @@
 #define NO_UNIX_SOCKETS
 #endif
 
-/* Define if gettimeofday only takes one argument */
-#undef GETTIMEOFDAY_ONE_ARG
-
 /* Define if you have revoke() */
 #undef HAVE_REVOKE
 
Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Ed Swierk

Calling gettimeofday() to compute a time interval can cause problems if the system clock jumps forwards or backwards; replace updtime() with qemu_get_clock(rt_clock), which calls clock_gettime(CLOCK_MONOTONIC) if it is available.

Also remove some useless macros.

Nice fix & cleanup.

Quoted 82 lines

---

slirp/slirp.c +2 -25
index c68c6ab..3798780 100644
@@ -22,6 +22,7 @@
  * THE SOFTWARE.
  */
 #include "qemu-common.h"
+#include "qemu-timer.h"
 #include "qemu-char.h"
 #include "slirp.h"
 #include "hw/hw.h"
@@ -238,29 +239,6 @@ void slirp_cleanup(Slirp *slirp)
 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
 #define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
 
-/*
- * curtime kept to an accuracy of 1ms
- */
-#ifdef _WIN32
-static void updtime(void)
-{
-    struct _timeb tb;
-
-    _ftime(&tb);
-
-    curtime = tb.time * 1000 + tb.millitm;
-}
-#else
-static void updtime(void)
-{
-    struct timeval tv;
-
-    gettimeofday(&tv, NULL);
-
-    curtime = tv.tv_sec * 1000 + tv.tv_usec / 1000;
-}
-#endif
-
 void slirp_select_fill(int *pnfds,
                        fd_set *readfds, fd_set *writefds, fd_set *xfds)
 {
@@ -399,8 +377,7 @@ void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds,
     global_writefds = writefds;
     global_xfds = xfds;
 
-	/* Update time */
-	updtime();
+    curtime = qemu_get_clock(rt_clock);
 
     TAILQ_FOREACH(slirp, &slirp_instances, entry) {
 	/*
slirp/slirp.h +0 -4
index 22058cd..020412c 100644
@@ -108,10 +108,6 @@ typedef unsigned char u_int8_t;
 #include <arpa/inet.h>
 #endif
 
-#ifdef GETTIMEOFDAY_ONE_ARG
-#define gettimeofday(x, y) gettimeofday(x)
-#endif
-
 /* Systems lacking strdup() definition in <string.h>. */
 #if defined(ultrix)
 char *strdup(const char *);
slirp/slirp_config.h +0 -3
index ebaa99c..e977e77 100644
@@ -187,9 +187,6 @@
 #define NO_UNIX_SOCKETS
 #endif
 
-/* Define if gettimeofday only takes one argument */
-#undef GETTIMEOFDAY_ONE_ARG
-
 /* Define if you have revoke() */
 #undef HAVE_REVOKE