slirp: Use monotonic clock if available
PATCH 1/1 slirp: Use monotonic clock if available
Calling gettimeofday() to compute a time interval can cause problems if the system clock jumps forwards or backwards; use clock_gettime(CLOCK_MONOTONIC) instead if it is available.
Also remove some useless macros.
---
slirp/slirp.c +10 -5
@@ -258,25 +258,30 @@ void slirp_cleanup(Slirp *slirp)/* * curtime kept to an accuracy of 1ms */ -#ifdef _WIN32static void updtime (void ) { +#ifdef _WIN32struct _timeb tb;_ftime (&tb); curtime = tb.time *1000 + tb.millitm; -}#else -static void updtime (void ) -{ +#ifdef CLOCK_MONOTONIC +struct timespec tv; + +clock_gettime (CLOCK_MONOTONIC , &tv); + + curtime = tv.tv_sec *1000 + tv.tv_nsec /1000000 ; +#else struct timeval tv;gettimeofday (&tv,NULL ); curtime = tv.tv_sec *1000 + tv.tv_usec /1000 ; -}#endif +#endif +}void slirp_select_fill (int *pnfds,fd_set *readfds,fd_set *writefds,fd_set *xfds)
slirp/slirp.h +0 -4
@@ -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
@@ -190,9 +190,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
On Wed, 22 Jul 2009, Ed Swierk wrote:
[..snip..]
Quoted 8 lines
slirp/slirp.c +6
-2-static void updtime (void ) -{ +#ifdef CLOCK_MONOTONIC +struct timespec tv; + +clock_gettime (CLOCK_MONOTONIC , &tv); + + curtime = tv.tv_sec *1000 + tv.tv_nsec /1000000 ;
slirp/slirp.c void slirp_cleanup(Slirp *slirp) · line 284
Quoted 10 lines
slirp/slirp.c +3
-1+#else struct timeval tv;gettimeofday (&tv,NULL ); curtime = tv.tv_sec *1000 + tv.tv_usec /1000 ; -}#endif +#endif +}
This is wrong on many levels.
Suppose CLOCK_MONOTONIC(which is wrong _POSIX_MONOTONIC_CLOCK ought to be checked) is defined and you compile your binary on, say, Linux 2.6 and then try to run it on 2.4 (without any fancy RHEL patches for instance), things wouldn't work, what's worse they wouldn't work silently, giving no indication what's wrong all because nobody checks clock_gettime result for failures.
The CLOCK_MONOTONIC presence ought to be checked at runtime (see sysconf(3) and _POSIX_MONOTONIC_CLOCK)
[..snip..]
Signature · 1 line
mailto:[email protected]
Quoted 11 lines · malc
This is wrong on many levels.
Suppose
CLOCK_MONOTONIC(which is wrong _POSIX_MONOTONIC_CLOCK ought to be checked)is defined and you compile your binary on, say, Linux 2.6 and then try to run it on 2.4 (without any fancy RHEL patches for instance), things wouldn't work, what's worse they wouldn't work silently, giving no indication what's wrong all because nobody checksclock_gettimeresult for failures.The
CLOCK_MONOTONICpresence ought to be checked at runtime (see sysconf(3) and_POSIX_MONOTONIC_CLOCK)
OK, sounds like slirp should just call qemu_get_clock(rt_clock), which is supposed to deal with all these complications.
--Ed
Quoted 10 lines · Ed Swierk
Quoted 9 lines · malc
This is wrong on many levels.
Suppose
CLOCK_MONOTONIC(which is wrong _POSIX_MONOTONIC_CLOCK ought to be checked)is defined and you compile your binary on, say, Linux 2.6 and then try to run it on 2.4 (without any fancy RHEL patches for instance), things wouldn't work, what's worse they wouldn't work silently, giving no indication what's wrong all because nobody checksclock_gettimeresult for failures.
Quoted 5 lines
Quoted 2 lines
The
CLOCK_MONOTONICpresence ought to be checked at runtime (see sysconf(3) and_POSIX_MONOTONIC_CLOCK)OK, sounds like slirp should just call
qemu_get_clock(rt_clock), which is supposed to deal with all these complications.
Exactly.
Jan