Why time() and gettimeofday() can return different values

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi all,

The following code will exit (in a few seconds usually) - which I would
expect it not too. Since gettimeofday is called before time(). Obviously
this is compiled without optimization.

I traced this through to the different syscall's implementations of
time() and gettimeofday() in the kernel. 

They both use the same timer (xtime) but gettimeofday() calls
getnstimeofday() which calls timespec_add_ns(), which can modify the
seconds portion of timespec struct. I dont understand why
getnstimeofday() uses a loop and then adds on nanoseconds to the result?

Any insight greatly appreciated :-)

Adrian Cornish


static __always_inline void timespec_add_ns(struct timespec *a, u64 ns)
{
   a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns);
   a->tv_nsec = ns;
}


void do_gettimeofday(struct timeval *tv)
{
   struct timespec now;

   getnstimeofday(&now);
   tv->tv_sec = now.tv_sec;
   tv->tv_usec = now.tv_nsec/1000;
}

void getnstimeofday(struct timespec *ts)
{
   unsigned long seq;
   s64 nsecs;

   WARN_ON(timekeeping_suspended);

   do {
      seq = read_seqbegin(&xtime_lock);

      *ts = xtime;
      nsecs = timekeeping_get_ns();

      /* If arch requires, add in gettimeoffset() */
      nsecs += arch_gettimeoffset();

   } while (read_seqretry(&xtime_lock, seq));

   timespec_add_ns(ts, nsecs);
}

My example code
#include <iostream>
#include <sys/time.h>
#include <time.h>

int main(void)
{
   time_t t;
   timeval tv;
   size_t difference=0;
   for(;;)
   {
      gettimeofday(&tv, 0);
      t=time(0);
      if(t<tv.tv_sec)
      {
         std::cout << "Different " << ++difference << " times t=" << t
<< " tv.tv_sec=" << tv.tv_sec << std::endl;
         break;
      }
   }
   return 0;
}



--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ




[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux