public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* gettimeofday problem
@ 2002-06-24  8:55 Salvatore D'Angelo
       [not found] ` <200206240934.g5O9YL524660@budgie.cs.uwa.edu.au>
  0 siblings, 1 reply; 23+ messages in thread
From: Salvatore D'Angelo @ 2002-06-24  8:55 UTC (permalink / raw)
  To: linux-kernel

Hi,

I am writing a small piece of code that use the gettimeofday routine and 
I have noticed a very strange behaviour. If I call the routine two times 
in sequence I expect that the second value is greater than or equal to 
the first one, but it is not true.

please check the following code.

Sometimes happen that the string "Strange Behaviour" is printed with 
kernel 2.4.18.

I tried to find in the Linux Archive patches to solve this problem,  but 
I didn't find anything (there are emails that talk about gettimeofday, 
but probably they do not answer to my questions).

The same thing happen in Java using the System.currentTimeMillis() routine.

#include <fstream.h>
#include <sys/time.h>

// this routine calculate the current time returning its value in long 
long format.
long long currentTimeMillis() {
   long long t;
   struct timeval tv;

   gettimeofday(&tv, 0);

   t = tv.tv_sec;
   t = (t *1000) + (tv.tv_usec/1000);

   return t;
}

void main() {
   for (;;) {
       long long a = currentTimeMillis();
       long long b = currentTimeMillis();

       if (a>b) {
           cout << "Strange Behaviour" << endl;
       }
   }
}




^ permalink raw reply	[flat|nested] 23+ messages in thread
* Re: gettimeofday problem
@ 2002-06-25  0:37 Christian Robert
  2002-06-25  0:43 ` Brad Hards
  0 siblings, 1 reply; 23+ messages in thread
From: Christian Robert @ 2002-06-25  0:37 UTC (permalink / raw)
  To: linux-kernel

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;
}

^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2002-07-19 17:14 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-06-24  8:55 gettimeofday problem Salvatore D'Angelo
     [not found] ` <200206240934.g5O9YL524660@budgie.cs.uwa.edu.au>
2002-06-24 10:20   ` Salvatore D'Angelo
2002-06-24 12:46     ` Matti Aarnio
2002-06-24 13:57       ` Salvatore D'Angelo
2002-06-24 18:56         ` Karim Yaghmour
2002-06-26 11:01           ` Gabriel Paubert
2002-07-01  2:30         ` Pavel Machek
2002-07-01  2:29       ` Pavel Machek
2002-06-24 19:44     ` Richard B. Johnson
2002-06-24 23:34       ` Frank van de Pol
2002-06-25 11:42         ` Richard B. Johnson
2002-06-28 18:04           ` george anzinger
2002-06-25 13:36       ` Chris Friesen
2002-06-26 10:58         ` Gabriel Paubert
  -- strict thread matches above, loose matches on Subject: below --
2002-06-25  0:37 Christian Robert
2002-06-25  0:43 ` Brad Hards
2002-06-25  2:03   ` Christian Robert
2002-06-25  2:47     ` John Alvord
2002-06-25  9:17       ` Christian Robert
2002-06-25 10:00         ` Jan Hudec
2002-07-19 12:17           ` Amos Waterland
2002-06-25 11:45   ` Richard B. Johnson
2002-06-25 11:50     ` Brad Hards

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox