gettimeofday problem
For your eyes,
while reading this thread I wrote a sample program to test if the clock sometimes goes backward/forward.
I started my program while continuing reading threads on the linux-kernel archive. After about 90 minutes of continuous running I went back to the window running the program and surprise I saw this:
$ ./tloop Bump negative -4294967295 ^C Summary: ------- Min = 0 Max = 257845 Avg = 1 (6009092476/5521919279)
So it looks like the time changed somewhere of a value +/- 4,295 seconds.
kernel 2.4.18
$ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 8 model name : Pentium III (Coppermine) stepping : 1 cpu MHz : 602.566 cache size : 256 KB fdiv_bug : no hlt_bug : no f00f_bug : no coma_bug : no fpu : yes fpu_exception : yes cpuid level : 2 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 sep mtrr pge mca cmov pat pse36 mmx fxsr sse bogomips : 1202.58
--------------------- program tloop.c -------------------------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <signal.h>
typedef long long LL;
LL GetTime (void)
{
struct timeval tv;
LL retval;
gettimeofday (&tv, NULL);
retval = (tv.tv_sec * 1000000) + (tv.tv_usec);
return retval;
}
volatile int Break = 0;
void Trap (int sig)
{
Break = 1;
}
int main (void)
{
LL Now, Old;
LL Dt, Min=9999999, Max=0, Num=0, Tot=0;
Old = Now = GetTime ();
signal (SIGINT, Trap);
signal (SIGQUIT, Trap);
for ( ; Break==0 ; )
{
Now = GetTime(); if (Now < Old)
{
printf ("Bump negative %lld\n", (Now-Old));
}
else
{
Dt = Now-Old;
Min = (Dt < Min) ? Dt : Min;
Max = (Dt > Max) ? Dt : Max;
Tot += Dt;
Num += 1;
}
Old = Now;}
printf ("Summary:\n-------\n Min = %lld\n Max = %lld\n "
"Avg = %lld (%lld/%lld)\n", Min, Max, Tot/Num, Tot, Num); return 0;
}
Quoted 1 line · Christian Robert
gettimeofday (&tv, NULL);
How about checking the return value of the function call?
Brad
Signature · 1 line
http://conf.linux.org.au. 22-25Jan2003. Perth, Australia. Birds in Black.
Quoted 8 lines · Brad Hards
Quoted 1 line · Christian Robert
gettimeofday (&tv, NULL);How about checking the return value of the function call?
Brad
--
http://conf.linux.org.au. 22-25Jan2003. Perth, Australia. Birds in Black.
$ time ./tloop Bump negative -4294967295 Summary: ------- Min = 0 Max = 140068 Avg = 1 (4064861295/3825797418)
real 67m44.891s user 29m29.690s sys 27m53.130s
Same thing. Took about an hour before getting the negative bump.
Xtian.
---- modified GetTime() checking return value of gettimeofday() -----
LL GetTime (void)
{
struct timeval tv;
LL retval;
int rc;
while (0 != (rc = gettimeofday (&tv, NULL)))
printf ("Wow! gettimeofday () returned %d\n", rc); retval = (tv.tv_sec * 1000000) + (tv.tv_usec);
return retval;
}
Maybe this is the result of floating point rounding errors. Floating point is notorious for occaisional strange results. I suggest redoing the test program to keep all results in integer and seeing what happens...
john
Quoted 46 lines · Christian Robert
Quoted 8 lines · Brad Hards
Quoted 1 line · Christian Robert
gettimeofday (&tv, NULL);How about checking the return value of the function call?
Brad
--
http://conf.linux.org.au. 22-25Jan2003. Perth, Australia. Birds in Black.$ time ./tloop Bump negative -4294967295 Summary: ------- Min = 0 Max = 140068 Avg = 1 (4064861295/3825797418)real 67m44.891s user 29m29.690s sys 27m53.130sSame thing. Took about an hour before getting the negative bump.
Xtian.
---- modified
GetTime()checking return value ofgettimeofday()-----LL GetTime (void)
{
struct timeval tv;
LL retval;
int rc;while (0 != (rc = gettimeofday (&tv, NULL)))
printf ("Wow! gettimeofday () returned %d\n", rc);retval = (tv.tv_sec * 1000000) + (tv.tv_usec);
return retval;
}
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Quoted 7 lines · John Alvord
Maybe this is the result of floating point rounding errors. Floating point is notorious for occaisional strange results. I suggest redoing the test program to keep all results in integer and seeing what happens...
john
You were close.
Programming error on my part.
This modified subroutine does correct my test.
Notice the (LL) cast on 1000000
Computation was done on 32 bits integer then assign on the 64 bits integer.
sorry.
ps: It may help explain the other > 1 hour time jump I've seen in an
other thread.
LL GetTime (void)
{
struct timeval tv;
LL retval;
int rc;
while (0 != (rc = gettimeofday (&tv, NULL)))
printf ("Wow! gettimeofday () returned %d\n", rc);retval = (tv.tv_sec * (LL)1000000) + (tv.tv_usec);
return retval;
}
Quoted 7 lines · Christian Robert
Quoted 4 lines · John Alvord
Maybe this is the result of floating point rounding errors. Floating point is notorious for occaisional strange results. I suggest redoing the test program to keep all results in integer and seeing what happens...
You were close.
Programming error on my part.
What about comparing the struct timeval things directly? There is even a timercmp macro for that (well I noticed that in the manpage when I have olrady had the test written; the macro can only do sharp comparsions).
Something like this:
(I am now running it on three machines - Athlon 850, Pentium 1500 and dual Pentium III 500 - all seem to be OK so far)
#include<stdio.h>
#include<errno.h>
#include<sys/time.h>
#include<signal.h>
volatile int loop = 1;
void sigint(int foo) {
loop = 0;
}int main(void) {
unsigned long long cnt = 0, bcnt = 0, ecnt = 0;
struct timeval old, new = {0, 0};
signal(SIGINT, sigint);
while(loop && cnt < (1LLU<<54)) {
cnt++;
old = new;
if(gettimeofday(&new, NULL)) {
ecnt++;
printf("Error #%llu: count=%llu"
" error/count=0.%04llu errno=%i (%s)\n",
ecnt, cnt, (10000*ecnt)/cnt, errno,
sys_errlist[errno]);
continue;
}
if((new.tv_sec < old.tv_sec) || ((new.tv_sec == old.tv_sec) && (new.tv_usec < old.tv_usec))) {
bcnt++;
printf("Skew #%llu: count=%llu errors=%llu"
" skew/good count=0.%04llu, new=(%li,"
" %li) old=(%li, %li)\n", bcnt, cnt,
ecnt, (10000*bcnt)/(cnt-ecnt),
new.tv_sec, new.tv_usec, old.tv_sec,
old.tv_usec);
}
}
printf("Counted %llu, errors %llu (0.%04llu), skews %llu"
" (0.%04llu)\n", cnt, ecnt, (10000*ecnt)/cnt, bcnt,
(10000*bcnt)/(cnt-ecnt));
return 0;
}-------------------------------------------------------------------------------
Jan 'Bulb' Hudec <[email protected]>
There may be several distinct errors that people are observing. I wrote a program that makes 32 gettimeofday() calls in a tight loop so as to try to catch rare anomalies, and after running for about 24 hours, it just triggered a strange problem.
On 2.4.18-3 on a Pentium III, the following output shows a large jump in the tv_usec value, followed by an immediate return to the monotonically increasing sequence, but with a large sequence discontinuity.
So in this case the time is not so much "going backwards", but rather making a huge jump forwards and then returning to the original sequence. Hope this helps.
% ./test0035
Run: 1844080460; Timer went backwards!; dump:
1027095141 707896
1027095141 707896
1027095141 707897
1027095141 707897
1027095141 707898
1027095141 707898
1027095141 707899
1027095141 707899
1027095141 707900
1027095141 707900
1027095141 707901
1027095141 707901
1027095141 707902
1027095141 707902
1027095141 707903
1027095141 707903
1027095141 852234 <-- this is the anomaly
1027095141 712510 <-- sequence returns with 4607us discontinuity
1027095141 712510
1027095141 712511
1027095141 712511
1027095141 712512
1027095141 712512
1027095141 712513
1027095141 712513
1027095141 712514
1027095141 712514
1027095141 712515
1027095141 712515
1027095141 712516
1027095141 712516
1027095141 712517
% cat test0035.c
/* Test for timer weirdness.
* Amos Waterland <[email protected]>
* 15 July 2002
*/
#include <errno.h>
#include <error.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
int
main (int argc, char *argv[])
{
int result = 0;
{
const int ELEMS = 32;
struct timeval tm[ELEMS];
long j, i; while (1)
{
++j;
for (i = 0; i < ELEMS; i++)
{
gettimeofday (&(tm[i]), NULL);
}
for (i = 1; i < ELEMS; i++)
{
if ((long) tm[i].tv_sec > (long) tm[i - 1].tv_sec)
continue;
if ((long) tm[i].tv_sec < (long) tm[i - 1].tv_sec ||
(long) tm[i].tv_usec < (long) tm[i - 1].tv_usec)
{
printf ("\nRun: %li; Timer went backwards!; dump:\n", j);
for (i = 0; i < ELEMS; i++)
{
printf ("%li %li\n",
(long) tm[i].tv_sec, (long) tm[i].tv_usec);
}
}
}
}}
exit (result);
}
Quoted 60 lines · Jan Hudec
Quoted 7 lines · Christian Robert
Quoted 4 lines · John Alvord
Maybe this is the result of floating point rounding errors. Floating point is notorious for occaisional strange results. I suggest redoing the test program to keep all results in integer and seeing what happens...
You were close.
Programming error on my part.What about comparing the struct timeval things directly? There is even a timercmp macro for that (well I noticed that in the manpage when I have olrady had the test written; the macro can only do sharp comparsions).
Something like this:
(I am now running it on three machines - Athlon 850, Pentium 1500 and dual Pentium III 500 - all seem to be OK so far)#include<stdio.h>
#include<errno.h>
#include<sys/time.h>
#include<signal.h>volatile int loop = 1;
void sigint(int foo) { loop = 0; }int main(void) { unsigned long long cnt = 0, bcnt = 0, ecnt = 0; struct timeval old, new = {0, 0}; signal(SIGINT, sigint); while(loop && cnt < (1LLU<<54)) { cnt++; old = new; if(gettimeofday(&new, NULL)) { ecnt++; printf("Error #%llu: count=%llu" " error/count=0.%04llu errno=%i (%s)\n", ecnt, cnt, (10000*ecnt)/cnt, errno, sys_errlist[errno]); continue; } if((new.tv_sec < old.tv_sec) || ((new.tv_sec == old.tv_sec) && (new.tv_usec < old.tv_usec))) { bcnt++; printf("Skew #%llu: count=%llu errors=%llu" " skew/good count=0.%04llu, new=(%li," " %li) old=(%li, %li)\n", bcnt, cnt, ecnt, (10000*bcnt)/(cnt-ecnt), new.tv_sec, new.tv_usec, old.tv_sec, old.tv_usec); } } printf("Counted %llu, errors %llu (0.%04llu), skews %llu" " (0.%04llu)\n", cnt, ecnt, (10000*ecnt)/cnt, bcnt, (10000*bcnt)/(cnt-ecnt)); return 0; }
Quoted 7 lines · Brad Hards
Quoted 1 line · Christian Robert
gettimeofday (&tv, NULL);How about checking the return value of the function call?
Brad
Signature · 1 line
http://conf.linux.org.au. 22-25Jan2003. Perth, Australia. Birds in Black.
I think the only possible error returned would relate to the time-zone being invalid. The time-zone pointer being a NULL is valid so it isn't going to return EINVAL.
Cheers,
Dick Johnson
Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips).
Windows-2000/Professional isn't.
Quoted 6 lines · Richard B. Johnson
Quoted 5 lines · Brad Hards
Quoted 1 line · Christian Robert
gettimeofday (&tv, NULL);How about checking the return value of the function call?
Quoted 7 lines
Quoted 3 lines
Brad
--
http://conf.linux.org.au. 22-25Jan2003. Perth, Australia. Birds in Black.I think the only possible error returned would relate to the time-zone being invalid. The time-zone pointer being a NULL is valid so it isn't going to return EINVAL.
It was just a thought - it just seemed a reasonable thing to test for, since the call can fail.
I didn't check the lib code, so I imagined that their might be some glibc munging of the syscall output. man gettimeofday sez, inter alia:
EINVAL Timezone (or something else) is invalid.
EFAULT One of tv or tz pointed outside your accessible
address space.
As you pointed out, EINVAL doesn't seem too likely, and I can't see a pointer problem. So it looks like something else.
Brad
Signature · 1 line
http://conf.linux.org.au. 22-25Jan2003. Perth, Australia. Birds in Black.