Bug 174401 - gettimeofday goes backward randomly with or without ntpd
Summary: gettimeofday goes backward randomly with or without ntpd
Keywords:
Status: CLOSED DUPLICATE of bug 174390
Alias: None
Product: Red Hat Enterprise Linux 4
Classification: Red Hat
Component: kernel
Version: 4.0
Hardware: i686
OS: Linux
medium
medium
Target Milestone: ---
: ---
Assignee: Brian Maly
QA Contact: Brian Brock
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2005-11-28 20:05 UTC by claudiu pop
Modified: 2007-11-30 22:07 UTC (History)
1 user (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2005-11-29 00:24:18 UTC
Target Upstream Version:
Embargoed:


Attachments (Terms of Use)

Description claudiu pop 2005-11-28 20:05:55 UTC
+++ This bug was initially created as a clone of Bug #170341 +++

Description of problem:
With the simple code shown below, gettimeofday() is going backward by a random
amount of time (between 10 us and 2 seconds).  

#include "stdio.h"
#include <sys/time.h>
#include <memory.h>
#include "errno.h"

typedef long long long64;

int main(int, char**)
{
  long64 issues = 0;
  long64 total = 0;
  while(1)
  {
    bool bError = false;
    timeval tv;
    memset(&tv, 0, sizeof(tv));
    if(::gettimeofday (&tv, NULL) == -1)
    {
       printf("returned error:%d\n", errno);
       bError = true;
       continue;
    }

    long64 ticks = tv.tv_sec*1000000ll+tv.tv_usec;
    memset(&tv, 0, sizeof(tv));
    if(::gettimeofday (&tv, NULL) == -1)
    {
       printf("returned error:%d\n", errno);
       bError = true;
       continue;
    }
    long64 ticks2 = tv.tv_sec*1000000ll+tv.tv_usec;
    if(ticks2 < ticks)
    {
      printf("Delta time:%lld us percentage:%f\n", ticks - ticks2,
100.*double(issues)/total);
      issues++;
    }
    total++;
  }
  return 1;
}


Version-Release number of selected component (if applicable):
kernel version: 2.6.9-11.ELsmp

How reproducible:
Always

Steps to Reproduce:
1.Compile and run the above code
2.Program will print "Delta time: xx us percentage: yy" when gettimeofday()
reports going backward
3.The frequency of error seems similar regardless if ntpd is running and
synchronized or not.
  

Actual Results:  
Delta time:200 us  percentage:0.000003
Delta time:375 us  percentage:0.000003
Delta time:910 us  percentage:0.000003
Delta time:177 us  percentage:0.000003
Delta time:177 us  percentage:0.000003
Delta time:177 us  percentage:0.000003
Delta time:139 us  percentage:0.000003
Delta time:177 us  percentage:0.000003
Delta time:841 us  percentage:0.000003
Delta time:179 us  percentage:0.000003
Delta time:199 us  percentage:0.000003
Delta time:178 us  percentage:0.000003
Delta time:177 us  percentage:0.000003
Delta time:552 us  percentage:0.000003
Delta time:176 us  percentage:0.000003
Delta time:176 us  percentage:0.000003
Delta time:84 us  percentage:0.000003
Delta time:258 us  percentage:0.000003
Delta time:436 us  percentage:0.000003
Delta time:726 us  percentage:0.000003
Delta time:60 us  percentage:0.000003
Delta time:585 us  percentage:0.000003
Delta time:758 us  percentage:0.000003
Delta time:717 us  percentage:0.000003
Delta time:546 us  percentage:0.000002
Delta time:372 us  percentage:0.000002
Delta time:444 us  percentage:0.000002
Delta time:30 us  percentage:0.000002
Delta time:858 us  percentage:0.000002
Delta time:392 us  percentage:0.000002
Delta time:571 us  percentage:0.000002
Delta time:759 us  percentage:0.000002
Delta time:746 us  percentage:0.000002
Delta time:99 us  percentage:0.000002
Delta time:274 us  percentage:0.000002
Delta time:760 us  percentage:0.000002
Delta time:156 us  percentage:0.000002
Delta time:759 us  percentage:0.000002
Delta time:754916 us  percentage:0.000002
Delta time:808 us  percentage:0.000002
Delta time:846 us  percentage:0.000002
Delta time:1788404 us  percentage:0.000002
Delta time:1787652 us  percentage:0.000002
Delta time:1396667 us  percentage:0.000002
Delta time:390090 us  percentage:0.000002
Delta time:1788633 us  percentage:0.000002
Delta time:858 us  percentage:0.000002
Delta time:877 us  percentage:0.000002
Delta time:860 us  percentage:0.000002
Delta time:860 us  percentage:0.000002
Delta time:858 us  percentage:0.000002
Delta time:858 us  percentage:0.000002
Delta time:494 us  percentage:0.000002
Delta time:673 us  percentage:0.000002
Delta time:909 us  percentage:0.000002
Delta time:438 us  percentage:0.000002
Delta time:1394161 us  percentage:0.000002
Delta time:758667 us  percentage:0.000002
Delta time:757164 us  percentage:0.000002
Delta time:758494 us  percentage:0.000002


Expected Results:  I should not see the "Delta time: xx yy" message

Additional info:

This problem happens on a dual-core bi-processor Opteron box.

>cat /proc/cpuinfo
processor       : 0
vendor_id       : AuthenticAMD
cpu family      : 15
model           : 33
model name      : 02/21
stepping        : 2
cpu MHz         : 1994.147
cache size      : 1024 KB
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 1
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov
pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext lm 3dnowext 3dnow pni
bogomips        : 3915.77

processor       : 1
vendor_id       : AuthenticAMD
cpu family      : 15
model           : 33
model name      : 02/21
stepping        : 2
cpu MHz         : 1994.147
cache size      : 1024 KB
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 1
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov
pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext lm 3dnowext 3dnow pni
bogomips        : 3981.31

processor       : 2
vendor_id       : AuthenticAMD
cpu family      : 15
model           : 33
model name      : 02/21
stepping        : 2
cpu MHz         : 1994.147
cache size      : 1024 KB
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 1
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov
pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext lm 3dnowext 3dnow pni
bogomips        : 3981.31

processor       : 3
vendor_id       : AuthenticAMD
cpu family      : 15
model           : 33
model name      : 02/21
stepping        : 2
cpu MHz         : 1994.147
cache size      : 1024 KB
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 1
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov
pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext lm 3dnowext 3dnow pni
bogomips        : 3981.31

I also tried to disable TSC but this doesn't work since the kernel is compiled
with CONFIG_X86_TSC.

>dmesg
Linux version 2.6.9-11.ELsmp (bhcompile.redhat.com) (gcc version
3.4.3 20050227 (Red Hat 3.4.3-22)) #1 SMP Fri May 20 18:26:27 EDT 2005
............
Kernel command line: ro root=LABEL=/ rhgb quiet notsc
notsc: Kernel compiled with CONFIG_X86_TSC, cannot disable TSC.
Initializing CPU#0
CPU 0 irqstacks, hard=c03db000 soft=c03bb000
PID hash table entries: 4096 (order: 12, 65536 bytes)
Detected 1994.147 MHz processor.
Using tsc for high-res timesource

Comment 1 Dave Jones 2005-11-28 20:15:02 UTC

*** This bug has been marked as a duplicate of 174390 ***

Comment 2 Ernie Petrides 2005-11-29 00:23:16 UTC
Reopening/reclosing to get off of kernel-maint list.

Comment 3 Ernie Petrides 2005-11-29 00:24:18 UTC

*** This bug has been marked as a duplicate of 174390 ***

Comment 4 Brian Maly 2006-08-04 16:25:06 UTC
TSC's between processors are not syncronized on AMD based systems. As a result
the kernel might read from different TSC's each time and introduce error into the
timekeeping math. Use "notsc" at boot to prevent this problem. This is a known
issue that will be fixed in future releases.


Note You need to log in before you can comment on or make changes to this bug.