Skip to contentSkip to conversation

s_time going backwards on same processor?

DISCUSSION Dan Magenheimer 3 participants 11 messages Jul 22, 2008 – Jul 26, 2008

lore
Share & tools
lore
#0 lore raw

Are there any conditions/events in a running Xen system that can cause two consecutive calls to get_s_time() *on the same processor* to return values such that the second one is smaller than the first (other than 64-bit wraparound)? I ask because I'm fairly sure that I observed this, and Xen system time on the same processor moved backwards about 40us (40000ns).

I *do* know that get_s_time() on different processors can have this behavior and I know it is possible for hvm_get_guest_time() to go backwards (timer_mode=0), but I thought s_time was monotonically non-decreasing on any given processor and that read_platform_stime() is also monotonically non-decreasing.

Does dom0 maybe have direct hardware access to the hardware platform timer that xen system time is dependent on?

Thanks,
Dan

===================================
Thanks... for the memory
I really could use more / My throughput's on the floor The balloon is flat / My swap disk's fat / I've OOM's in store Overcommitted so much
(with apologies to the late great Bob Hope)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw
Quoted 10 lines

From: Dan Magenheimer
Sent: 2008年7月23日 5:59

Are there any conditions/events in a running Xen system that can cause two consecutive calls to get_s_time() *on the same processor* to return values such that the second one is smaller than the first (other than 64-bit wraparound)? I ask because I'm fairly sure that I observed this, and Xen system time on the same processor moved backwards about 40us (40000ns).

Weird. Did you observe same phenomenon by putting your test code in any code path, or just on some specific point?

Does clocksource matters?

Did you enable any cpufreq or cpuidle feature?

Only observed on SMP Xen?

Thanks,
Kevin

#3 lore raw
In reply to Dan Magenheimer
Quoted 9 lines · Dan Magenheimer

I *do* know that get_s_time() on different processors can have this behavior and I know it is possible for hvm_get_guest_time() to go backwards (timer_mode=0), but I thought s_time was monotonically non-decreasing on any given processor and that read_platform_stime() is also monotonically non-decreasing.

Does dom0 maybe have direct hardware access to the hardware platform timer that xen system time is dependent on?

No matter what happens to the underlying platform timer, it should be impossible for Xen system time to go backwards on any given processor. The calibration function never sets the TSC and system timestamps for the next time record any earlier than current TSC value and current computed system time value. Hence it should be impossible for system time to be computed as earlier than that time record.

-- Keir

#2 lore raw
In reply to Tian, Kevin

Hi Kevin --

All good questions. I used a modified version of the skew measurement code I posted the other day and noticed it by accident. It could have been an artifact of my debugging code, but I have lost access to my test machine for a few days so I can't reproduce it right now. If I can reproduce it, I will test for some of your questions. However, I had previously tested on this machine for cpufreq changes and time stop/start calls and neither were present.

Dan

Quoted 23 lines

-----Original Message-----
From: Tian, Kevin [mailto:[email protected]]
Sent: Wednesday, July 23, 2008 12:16 AM
To: [email protected]; Xen-Devel (E-mail); Keir Fraser
Cc: Dave Winchell
Subject: RE: [Xen-devel] s_time going backwards on same processor?

Quoted 10 lines

From: Dan Magenheimer
Sent: 2008年7月23日 5:59

Are there any conditions/events in a running Xen system that can cause two consecutive calls to get_s_time() *on the same processor* to return values such that the second one is smaller than the first (other than 64-bit wraparound)? I ask because I'm fairly sure that I observed this, and Xen system time on the same processor moved backwards about 40us (40000ns).

Weird. Did you observe same phenomenon by putting your test code in any code path, or just on some specific point?

Quoted 9 lines

Does clocksource matters?

Did you enable any cpufreq or cpuidle feature?

Only observed on SMP Xen?

Thanks,
Kevin

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#4 lore raw
In reply to Keir Fraser
Quoted 10 lines

No matter what happens to the underlying platform timer, it should be impossible for Xen system time to go backwards on any given processor. The
calibration function never sets the TSC and system timestamps for the next
time record any earlier than current TSC value and current computed system
time value. Hence it should be impossible for system time to be computed as
earlier than that time record.

OK, that corresponds with my reading of the code. I just wanted to ensure there wasn't some subtle point I was missing.

When I again have access to my test machine, I will try to reproduce it and provide more information if I can.

Thanks,
Dan

#5 lore raw
In reply to Keir Fraser

OK, I am definitely recording stime going backwards observed with the attached patch. I have recorded dozens over a few hours. It appears to have no
obvious pattern between reports, but one thing is consistent: In all cases, t->tsc_scale.shift is -1. I'll try to run some more tests over the weekend (e.g. with different clocksources... this is with clocksource=hpet), but thought I'd report what I have seen. I'm running on a dual-core single socket ("Conroe").

Dan

P.S. If you try the patch, ensure you set
the boot parameter "measurestime".

Quoted 32 lines

-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Wednesday, July 23, 2008 1:06 AM
To: [email protected]; Xen-Devel (E-mail)
Cc: Dave Winchell
Subject: Re: s_time going backwards on same processor?

Quoted 9 lines · Dan Magenheimer

I *do* know that get_s_time() on different processors can have this behavior and I know it is possible for hvm_get_guest_time() to go backwards (timer_mode=0), but I thought s_time was monotonically non-decreasing on any given processor and that read_platform_stime() is also monotonically non-decreasing.

Does dom0 maybe have direct hardware access to the hardware platform timer that xen system time is dependent on?

No matter what happens to the underlying platform timer, it should be impossible for Xen system time to go backwards on any given processor. The
calibration function never sets the TSC and system timestamps for the next
time record any earlier than current TSC value and current computed system
time value. Hence it should be impossible for system time to be computed as
earlier than that time record.

Quoted 4 lines

-- Keir

xen/arch/x86/time.c +67 -0
diff -r 092a15aa2430 xen/arch/x86/time.c
@@ -708,6 +708,51 @@ static unsigned long get_cmos_time(void)
  * System Time
  ***************************************************************************/
 
+u64 max_stime_skew = 0;   /* record max intra-CPU stime skew */
+u64 stime_goes_backwards = 0;
+static DEFINE_SPINLOCK(time_skew_lock);
+
+int measure_stime = 0;
+boolean_param("measurestime", measure_stime);
+
+static inline void measure_stime_skew(s_time_t now, struct cpu_time *t, u64 tsc)
+{
+    long cur_procid;
+    static int ignore = 10000L;
+    static s_time_t last = 0;
+    static int last_procid;
+    int back = 0;
+
+    if ( ignore > 0 )
+    {
+        ignore--;  /* ignore for awhile to wait for calibrate */
+        return;
+    }
+    local_irq_disable();
+    spin_lock(&time_skew_lock);
+    cur_procid = smp_processor_id();
+    if ( last && last > now ) {
+        if ( last_procid != cur_procid ) {
+            if ( (last - now) > max_stime_skew )
+                max_stime_skew = last - now;
+        }
+        else
+        {
+            stime_goes_backwards++;
+            back++;
+        }
+    }
+    last = now;
+    last_procid = cur_procid;
+    spin_unlock(&time_skew_lock);
+    local_irq_enable();
+    if (back) 
+        printk("back: now=%"PRIu64" tsc=%"PRIu64", local_tsc_stamp=%"PRIu64""
+               " stime_local_stamp=%"PRIu64" shift=%d mul_frac=%"PRIu32"\n",
+            now, tsc, t->local_tsc_stamp, t->stime_local_stamp,
+            t->tsc_scale.shift, t->tsc_scale.mul_frac);
+}
+
 s_time_t get_s_time(void)
 {
     struct cpu_time *t = &this_cpu(cpu_time);
@@ -717,6 +762,8 @@ s_time_t get_s_time(void)
     rdtscll(tsc);
     delta = tsc - t->local_tsc_stamp;
     now = t->stime_local_stamp + scale_delta(delta, &t->tsc_scale);
+    if (measure_stime)
+        measure_stime_skew(now,t,tsc);
 
     return now;
 }
@@ -828,6 +875,10 @@ void do_settime(unsigned long secs, unsi
     rcu_read_unlock(&domlist_read_lock);
 }
 
+u64 stime_skew_max = 0;
+u32 stime_skew_count = 0;
+u64 stime_skew_sum = 0;
+
 static void local_time_calibration(void *unused)
 {
     struct cpu_time *t = &this_cpu(cpu_time);
@@ -861,6 +912,10 @@ static void local_time_calibration(void 
     /* The overall calibration scale multiplier. */
     u32 calibration_mul_frac;
 
+    /* record skew vs platform counter */
+    static int skip_max_skew_calc = 100;
+    u64 curr_stime_skew;
+
     prev_tsc          = t->local_tsc_stamp;
     prev_local_stime  = t->stime_local_stamp;
     prev_master_stime = t->stime_master_stamp;
@@ -875,6 +930,18 @@ static void local_time_calibration(void 
     rdtscll(curr_tsc);
     local_irq_enable();
 
+    if ( skip_max_skew_calc > 0)
+        skip_max_skew_calc--;  /* allow calibration to converge */
+    else
+    {
+        stime_skew_count++;
+        curr_stime_skew = curr_master_stime - curr_local_stime;
+        if ( (s64) curr_stime_skew < 0 )
+            curr_stime_skew = - curr_stime_skew;
+        if ( curr_stime_skew > stime_skew_max )
+            stime_skew_max = curr_stime_skew;
+        stime_skew_sum += curr_stime_skew;
+    }
 #if 0
     printk("PRE%d: tsc=%"PRIu64" stime=%"PRIu64" master=%"PRIu64"\n",
            smp_processor_id(), prev_tsc, prev_local_stime, prev_master_stime);
xen/common/keyhandler.c +28 -3
diff -r 092a15aa2430 xen/common/keyhandler.c
@@ -246,11 +246,26 @@ static void read_clocks_slave(void *unus
     cpu_clear(cpu, read_clocks_cpumask);
 }
 
+/*
+ * Print collected skew information; three different measures because:
+ *  1) Sync'ed skew is only sampled manually (e.g. xm debug-key t) but
+ *     measures precisely what we want
+ *  2) max_stime_skew is sampled in get_s_time at high frequency but 
+ *     max capture requires near simultaneous access by different processors
+ *     (and the code is ugly so Keir will probably remove ;-)
+ *  3) stime_skew_max is recorded at n Hz in local_time_calibration but
+ *     only records positive skew from platform timer (n = #CPUs)
+ * NOTE: HVM's with timer_mode==0 intentionally cause intra-CPU skew
+ * Also, ntpd may affect results(?) so should be turned off
+ */
 static void read_clocks(unsigned char key)
 {
     unsigned int cpu = smp_processor_id(), min_cpu, max_cpu;
     u64 min, max, dif, difus;
+    static u64 sumdif = 0, maxdif = 0, count = 0;
     static DEFINE_SPINLOCK(lock);
+    extern u64 max_stime_skew, stime_goes_backwards;
+    extern u64 stime_skew_max, stime_skew_count, stime_skew_sum;
 
     spin_lock(&lock);
 
@@ -280,10 +295,20 @@ static void read_clocks(unsigned char ke
     spin_unlock(&lock);
 
     dif = difus = max - min;
+    if ( dif > maxdif )
+        maxdif = dif;
+    count++;
+    sumdif += dif;
     do_div(difus, 1000);
-    printk("Min = %"PRIu64" ; Max = %"PRIu64" ; Diff = %"PRIu64
-           " (%"PRIu64" microseconds)\n",
-           min, max, dif, difus);
+    printk("Synced skew: max=%"PRIu64"ns;avg=%"PRIu64"ns "
+           "cnt=%"PRIu64" cur=%"PRIu64"\n",
+           maxdif, sumdif/count, count, dif);
+    printk("Max recorded stime skew: %"PRIu64"ns backwards=%"PRIu64"\n",
+           max_stime_skew, stime_goes_backwards);
+    if ( !stime_skew_count )
+        stime_skew_count = 1; /* shouldn't happen but avoid divide by zero */
+    printk("Platform skew: max=%"PRIu64"ns; avg=%"PRIu64"ns; cnt=%"PRIu64"\n",
+           stime_skew_max, stime_skew_sum/stime_skew_count, stime_skew_count);
 }
 
 extern void dump_runq(unsigned char key);

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#6 lore raw

In a quick glimpse, your measure_stime_skew has interrupt enabled absolutely at exit, instead of restoring previous setting. Would it be a trouble-maker? I have no latest code at hand, but at least for what I can check:

in local_time_calibration:

    local_irq_disable();
    curr_master_stime = read_platform_stime();
    curr_local_stime  = get_s_time();
    rdtscll(curr_tsc);
    local_irq_enable();

with your patch, interrupt is enabled after get_s_time, which then may have curr_tsc read out from a different time point...

Thanks,
Kevin

Quoted 56 lines

From: Dan Magenheimer [mailto:[email protected]]
Sent: 2008年7月26日 9:47

OK, I am definitely recording stime going backwards observed with the attached patch. I have recorded dozens over a few hours. It appears to have no
obvious pattern between reports, but one thing is consistent: In all cases, t->tsc_scale.shift is -1. I'll try to run some more tests over the weekend (e.g. with different clocksources... this is with clocksource=hpet), but thought I'd report what I have seen. I'm running on a dual-core single socket ("Conroe").

Dan

P.S. If you try the patch, ensure you set
the boot parameter "measurestime".

Quoted 36 lines

-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Wednesday, July 23, 2008 1:06 AM
To: [email protected]; Xen-Devel (E-mail)
Cc: Dave Winchell
Subject: Re: s_time going backwards on same processor?

Quoted 9 lines · Dan Magenheimer

I *do* know that get_s_time() on different processors can have this behavior and I know it is possible for hvm_get_guest_time() to go backwards (timer_mode=0), but I thought s_time was monotonically non-decreasing on any given processor and that read_platform_stime() is also monotonically non-decreasing.

Does dom0 maybe have direct hardware access to the hardware platform timer that xen system time is dependent on?

No matter what happens to the underlying platform timer, it should be impossible for Xen system time to go backwards on any given processor. The
calibration function never sets the TSC and system timestamps for the next
time record any earlier than current TSC value and current computed system
time value. Hence it should be impossible for system time to be computed as
earlier than that time record.

-- Keir

#7 lore raw

The code should be using spin_lock_irqsave/spin_unlock_irqrestore.

As it is it's incorrect and could cause odd behaviour. I don't know whether that would extend to seeing time goes backwards as often as Dan reports, but obviously the test has to be re-run.

-- Keir

Quoted 75 lines · Kevin

In a quick glimpse, your measure_stime_skew has interrupt enabled absolutely at exit, instead of restoring previous setting. Would it be a trouble-maker? I have no latest code at hand, but at least for what I can check:

in local_time_calibration:

    local_irq_disable();
    curr_master_stime = read_platform_stime();
    curr_local_stime  = get_s_time();
    rdtscll(curr_tsc);
    local_irq_enable();

with your patch, interrupt is enabled after get_s_time, which then may have curr_tsc read out from a different time point...

Thanks,
Kevin

Quoted 56 lines

From: Dan Magenheimer [mailto:[email protected]]
Sent: 2008年7月26日 9:47

OK, I am definitely recording stime going backwards observed with the attached patch. I have recorded dozens over a few hours. It appears to have no
obvious pattern between reports, but one thing is consistent: In all cases, t->tsc_scale.shift is -1. I'll try to run some more tests over the weekend (e.g. with different clocksources... this is with clocksource=hpet), but thought I'd report what I have seen. I'm running on a dual-core single socket ("Conroe").

Dan

P.S. If you try the patch, ensure you set
the boot parameter "measurestime".

Quoted 36 lines

-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Wednesday, July 23, 2008 1:06 AM
To: [email protected]; Xen-Devel (E-mail)
Cc: Dave Winchell
Subject: Re: s_time going backwards on same processor?

Quoted 9 lines · Dan Magenheimer

I *do* know that get_s_time() on different processors can have this behavior and I know it is possible for hvm_get_guest_time() to go backwards (timer_mode=0), but I thought s_time was monotonically non-decreasing on any given processor and that read_platform_stime() is also monotonically non-decreasing.

Does dom0 maybe have direct hardware access to the hardware platform timer that xen system time is dependent on?

No matter what happens to the underlying platform timer, it should be impossible for Xen system time to go backwards on any given processor. The
calibration function never sets the TSC and system timestamps for the next
time record any earlier than current TSC value and current computed system
time value. Hence it should be impossible for system time to be computed as
earlier than that time record.

-- Keir

#8 lore raw

Thanks Kevin for catching that. I fixed it using spin_lock_irqsave and spin_unlock_irqrestore and have already seen stime going backwards... I haven't retested all the clocksources and smp. but it doesn't appear that the incorrect enablement of interrupts was the problem.

It's interesting that the problem occurs even with one processor. Hopefully that will make it easier to debug.

Updated debug patch attached.

Thanks,
Dan

Quoted 76 lines

-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Saturday, July 26, 2008 12:51 AM
To: Tian, Kevin; [email protected]; Xen-Devel (E-mail)
Cc: Dave Winchell
Subject: Re: s_time going backwards on same processor?

The code should be using spin_lock_irqsave/spin_unlock_irqrestore.

As it is it's incorrect and could cause odd behaviour. I don't know whether
that would extend to seeing time goes backwards as often as Dan reports, but
obviously the test has to be re-run.

-- Keir

Quoted 56 lines · Kevin

In a quick glimpse, your measure_stime_skew has interrupt enabled absolutely at exit, instead of restoring previous setting. Would it be a trouble-maker? I have no latest code at hand, but at least for what I can check:

in local_time_calibration:

    local_irq_disable();
    curr_master_stime = read_platform_stime();
    curr_local_stime  = get_s_time();
    rdtscll(curr_tsc);
    local_irq_enable();

with your patch, interrupt is enabled after get_s_time, which then may have curr_tsc read out from a different time point...

Thanks,
Kevin

Quoted 37 lines

From: Dan Magenheimer [mailto:[email protected]]
Sent: 2008年7月26日 9:47

OK, I am definitely recording stime going backwards observed with the attached patch. I have recorded dozens over a few hours. It appears to have no
obvious pattern between reports, but one thing is consistent: In all cases, t->tsc_scale.shift is -1. I'll try to run some more tests over the weekend (e.g. with different clocksources... this is with clocksource=hpet), but thought I'd report what I have seen. I'm running on a dual-core single socket ("Conroe").

Dan

P.S. If you try the patch, ensure you set
the boot parameter "measurestime".

Quoted 18 lines

-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Wednesday, July 23, 2008 1:06 AM
To: [email protected]; Xen-Devel (E-mail)
Cc: Dave Winchell
Subject: Re: s_time going backwards on same processor?

Quoted 7 lines · Dan Magenheimer

I *do* know that get_s_time() on different processors can have this behavior and I know it is possible for hvm_get_guest_time() to go backwards (timer_mode=0), but I thought s_time was monotonically non-decreasing on any given processor and that read_platform_stime() is also monotonically non-decreasing.

Quoted 23 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

Does dom0 maybe have direct hardware access to the hardware platform timer that xen system time is dependent on?

No matter what happens to the underlying platform timer,

it should be

Quoted 15 lines
Quoted 15 lines
Quoted 14 lines

impossible for Xen system time to go backwards on any given processor. The
calibration function never sets the TSC and system timestamps for the next
time record any earlier than current TSC value and current computed system
time value. Hence it should be impossible for system time to be computed as
earlier than that time record.

-- Keir

xen/arch/x86/time.c +67 -0
diff -r 092a15aa2430 xen/arch/x86/time.c
@@ -30,6 +30,7 @@
 #include <asm/div64.h>
 #include <asm/hpet.h>
 #include <io_ports.h>
+#include <xen/spinlock.h>
 
 /* opt_clocksource: Force clocksource to one of: pit, hpet, cyclone, acpi. */
 static char opt_clocksource[10];
@@ -708,6 +709,50 @@ static unsigned long get_cmos_time(void)
  * System Time
  ***************************************************************************/
 
+u64 max_stime_skew = 0;   /* record max intra-CPU stime skew */
+u64 stime_goes_backwards = 0;
+static DEFINE_SPINLOCK(time_skew_lock);
+
+int measure_stime = 0;
+boolean_param("measurestime", measure_stime);
+
+static inline void measure_stime_skew(s_time_t now, struct cpu_time *t, u64 tsc)
+{
+    long cur_procid;
+    static int ignore = 10000L;
+    static s_time_t last = 0;
+    static int last_procid;
+    int back = 0;
+    unsigned long flags;
+
+    if ( ignore > 0 )
+    {
+        ignore--;  /* ignore for awhile to wait for calibrate */
+        return;
+    }
+    spin_lock_irqsave(&time_skew_lock,flags);
+    cur_procid = smp_processor_id();
+    if ( last && last > now ) {
+        if ( last_procid != cur_procid ) {
+            if ( (last - now) > max_stime_skew )
+                max_stime_skew = last - now;
+        }
+        else
+        {
+            stime_goes_backwards++;
+            back++;
+        }
+    }
+    last = now;
+    last_procid = cur_procid;
+    spin_unlock_irqrestore(&time_skew_lock,flags);
+    if (back) 
+        printk("back:, now=%"PRIu64" tsc=%"PRIu64", local_tsc_stamp=%"PRIu64""
+               " stime_local_stamp=%"PRIu64" shift=%d mul_frac=%"PRIu32"\n",
+            now, tsc, t->local_tsc_stamp, t->stime_local_stamp,
+            t->tsc_scale.shift, t->tsc_scale.mul_frac);
+}
+
 s_time_t get_s_time(void)
 {
     struct cpu_time *t = &this_cpu(cpu_time);
@@ -717,6 +762,8 @@ s_time_t get_s_time(void)
     rdtscll(tsc);
     delta = tsc - t->local_tsc_stamp;
     now = t->stime_local_stamp + scale_delta(delta, &t->tsc_scale);
+    if (measure_stime)
+        measure_stime_skew(now,t,tsc);
 
     return now;
 }
@@ -828,6 +875,10 @@ void do_settime(unsigned long secs, unsi
     rcu_read_unlock(&domlist_read_lock);
 }
 
+u64 stime_skew_max = 0;
+u32 stime_skew_count = 0;
+u64 stime_skew_sum = 0;
+
 static void local_time_calibration(void *unused)
 {
     struct cpu_time *t = &this_cpu(cpu_time);
@@ -861,6 +912,10 @@ static void local_time_calibration(void 
     /* The overall calibration scale multiplier. */
     u32 calibration_mul_frac;
 
+    /* record skew vs platform counter */
+    static int skip_max_skew_calc = 100;
+    u64 curr_stime_skew;
+
     prev_tsc          = t->local_tsc_stamp;
     prev_local_stime  = t->stime_local_stamp;
     prev_master_stime = t->stime_master_stamp;
@@ -875,6 +930,18 @@ static void local_time_calibration(void 
     rdtscll(curr_tsc);
     local_irq_enable();
 
+    if ( skip_max_skew_calc > 0)
+        skip_max_skew_calc--;  /* allow calibration to converge */
+    else
+    {
+        stime_skew_count++;
+        curr_stime_skew = curr_master_stime - curr_local_stime;
+        if ( (s64) curr_stime_skew < 0 )
+            curr_stime_skew = - curr_stime_skew;
+        if ( curr_stime_skew > stime_skew_max )
+            stime_skew_max = curr_stime_skew;
+        stime_skew_sum += curr_stime_skew;
+    }
 #if 0
     printk("PRE%d: tsc=%"PRIu64" stime=%"PRIu64" master=%"PRIu64"\n",
            smp_processor_id(), prev_tsc, prev_local_stime, prev_master_stime);
xen/common/keyhandler.c +28 -3
diff -r 092a15aa2430 xen/common/keyhandler.c
@@ -246,11 +246,26 @@ static void read_clocks_slave(void *unus
     cpu_clear(cpu, read_clocks_cpumask);
 }
 
+/*
+ * Print collected skew information; three different measures because:
+ *  1) Sync'ed skew is only sampled manually (e.g. xm debug-key t) but
+ *     measures precisely what we want
+ *  2) max_stime_skew is sampled in get_s_time at high frequency but 
+ *     max capture requires near simultaneous access by different processors
+ *     (and the code is ugly so Keir will probably remove ;-)
+ *  3) stime_skew_max is recorded at n Hz in local_time_calibration but
+ *     only records positive skew from platform timer (n = #CPUs)
+ * NOTE: HVM's with timer_mode==0 intentionally cause intra-CPU skew
+ * Also, ntpd may affect results(?) so should be turned off
+ */
 static void read_clocks(unsigned char key)
 {
     unsigned int cpu = smp_processor_id(), min_cpu, max_cpu;
     u64 min, max, dif, difus;
+    static u64 sumdif = 0, maxdif = 0, count = 0;
     static DEFINE_SPINLOCK(lock);
+    extern u64 max_stime_skew, stime_goes_backwards;
+    extern u64 stime_skew_max, stime_skew_count, stime_skew_sum;
 
     spin_lock(&lock);
 
@@ -280,10 +295,20 @@ static void read_clocks(unsigned char ke
     spin_unlock(&lock);
 
     dif = difus = max - min;
+    if ( dif > maxdif )
+        maxdif = dif;
+    count++;
+    sumdif += dif;
     do_div(difus, 1000);
-    printk("Min = %"PRIu64" ; Max = %"PRIu64" ; Diff = %"PRIu64
-           " (%"PRIu64" microseconds)\n",
-           min, max, dif, difus);
+    printk("Synced skew: max=%"PRIu64"ns;avg=%"PRIu64"ns "
+           "cnt=%"PRIu64" cur=%"PRIu64"\n",
+           maxdif, sumdif/count, count, dif);
+    printk("Max recorded stime skew: %"PRIu64"ns backwards=%"PRIu64"\n",
+           max_stime_skew, stime_goes_backwards);
+    if ( !stime_skew_count )
+        stime_skew_count = 1; /* shouldn't happen but avoid divide by zero */
+    printk("Platform skew: max=%"PRIu64"ns; avg=%"PRIu64"ns; cnt=%"PRIu64"\n",
+           stime_skew_max, stime_skew_sum/stime_skew_count, stime_skew_count);
 }
 
 extern void dump_runq(unsigned char key);

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#9 lore raw

Which clocksource was this with? It would be worth saving the previous tsc and struct cpu_time values so you can print those out when you see s_time goes backwards. It'll make it much easier to see what actually went 'backwards' -- also we could run through the 'prev' and 'now' s_time calculation arithmetic manually to see if any of the arithmetic is at fault.

-- Keir

Quoted 113 lines · Dan Magenheimer

Thanks Kevin for catching that. I fixed it using spin_lock_irqsave and spin_unlock_irqrestore and have already seen stime going backwards... I haven't retested all the clocksources and smp. but it doesn't appear that the incorrect enablement of interrupts was the problem.

It's interesting that the problem occurs even with one processor. Hopefully that will make it easier to debug.

Updated debug patch attached.

Thanks,
Dan

Quoted 99 lines

-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Saturday, July 26, 2008 12:51 AM
To: Tian, Kevin; [email protected]; Xen-Devel (E-mail)
Cc: Dave Winchell
Subject: Re: s_time going backwards on same processor?

The code should be using spin_lock_irqsave/spin_unlock_irqrestore.

As it is it's incorrect and could cause odd behaviour. I don't know whether
that would extend to seeing time goes backwards as often as Dan reports, but
obviously the test has to be re-run.

-- Keir

Quoted 13 lines · Kevin

In a quick glimpse, your measure_stime_skew has interrupt enabled absolutely at exit, instead of restoring previous setting. Would it be a trouble-maker? I have no latest code at hand, but at least for what I can check:

in local_time_calibration:

    local_irq_disable();
    curr_master_stime = read_platform_stime();
    curr_local_stime  = get_s_time();
    rdtscll(curr_tsc);
    local_irq_enable();
Quoted 47 lines

with your patch, interrupt is enabled after get_s_time, which then may have curr_tsc read out from a different time point...

Thanks,
Kevin

Quoted 41 lines

From: Dan Magenheimer [mailto:[email protected]]
Sent: 2008年7月26日 9:47

OK, I am definitely recording stime going backwards observed with the attached patch. I have recorded dozens over a few hours. It appears to have no
obvious pattern between reports, but one thing is consistent: In all cases, t->tsc_scale.shift is -1. I'll try to run some more tests over the weekend (e.g. with different clocksources... this is with clocksource=hpet), but thought I'd report what I have seen. I'm running on a dual-core single socket ("Conroe").

Dan

P.S. If you try the patch, ensure you set
the boot parameter "measurestime".

Quoted 22 lines

-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Wednesday, July 23, 2008 1:06 AM
To: [email protected]; Xen-Devel (E-mail)
Cc: Dave Winchell
Subject: Re: s_time going backwards on same processor?

Quoted 9 lines · Dan Magenheimer

I *do* know that get_s_time() on different processors can have this behavior and I know it is possible for hvm_get_guest_time() to go backwards (timer_mode=0), but I thought s_time was monotonically non-decreasing on any given processor and that read_platform_stime() is also monotonically non-decreasing.

Does dom0 maybe have direct hardware access to the hardware platform timer that xen system time is dependent on?

No matter what happens to the underlying platform timer,

it should be

Quoted 15 lines
Quoted 15 lines
Quoted 14 lines

impossible for Xen system time to go backwards on any given processor. The
calibration function never sets the TSC and system timestamps for the next
time record any earlier than current TSC value and current computed system
time value. Hence it should be impossible for system time to be computed as
earlier than that time record.

-- Keir

#10 lore raw

Well, the good news is I'm now convinced that stime is not going backwards... it was a false alarm. The bad news is that skew is still 3x-4x worse than I had previously thought.

The backwards stime was an artifact of the debug code. I needed to put the spin_lock_irqsave/spin_unlock_irqrestore pair around the ENTIRE call to get_s_time... I was apparently measuring nested get_s_time calls (get_s_time interrupted by an interrupt that calls get_s_time) and the measurement code was seeing the unwinding of the interrupt stack. Sorry for the noise.

For the bad skew, I'll look at some of the ideas proposed in the other ("Xen system skew MUCH worse than tsc skew") thread.

Quoted 18 lines

-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Saturday, July 26, 2008 9:14 AM
To: [email protected]; Tian, Kevin; Xen-Devel (E-mail)
Cc: Dave Winchell
Subject: Re: s_time going backwards on same processor?

Which clocksource was this with? It would be worth saving the previous tsc
and struct cpu_time values so you can print those out when you see s_time
goes backwards. It'll make it much easier to see what actually went 'backwards' -- also we could run through the 'prev' and 'now' s_time calculation arithmetic manually to see if any of the arithmetic is at fault.

Quoted 121 lines

-- Keir

Quoted 113 lines · Dan Magenheimer

Thanks Kevin for catching that. I fixed it using spin_lock_irqsave and spin_unlock_irqrestore and have already seen stime going backwards... I haven't retested all the clocksources and smp. but it doesn't appear that the incorrect enablement of interrupts was the problem.

It's interesting that the problem occurs even with one processor. Hopefully that will make it easier to debug.

Updated debug patch attached.

Thanks,
Dan

Quoted 99 lines

-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Saturday, July 26, 2008 12:51 AM
To: Tian, Kevin; [email protected]; Xen-Devel (E-mail)
Cc: Dave Winchell
Subject: Re: s_time going backwards on same processor?

The code should be using spin_lock_irqsave/spin_unlock_irqrestore.

As it is it's incorrect and could cause odd behaviour. I don't know whether
that would extend to seeing time goes backwards as often as Dan reports, but
obviously the test has to be re-run.

-- Keir

Quoted 13 lines · Kevin

In a quick glimpse, your measure_stime_skew has interrupt enabled absolutely at exit, instead of restoring previous setting. Would it be a trouble-maker? I have no latest code at hand, but at least for what I can check:

in local_time_calibration:

    local_irq_disable();
    curr_master_stime = read_platform_stime();
    curr_local_stime  = get_s_time();
    rdtscll(curr_tsc);
    local_irq_enable();
Quoted 47 lines

with your patch, interrupt is enabled after get_s_time, which then may have curr_tsc read out from a different time point...

Thanks,
Kevin

Quoted 41 lines

From: Dan Magenheimer [mailto:[email protected]]
Sent: 2008年7月26日 9:47

OK, I am definitely recording stime going backwards observed with the attached patch. I have recorded dozens over a few hours. It appears to have no
obvious pattern between reports, but one thing is consistent: In all cases, t->tsc_scale.shift is -1. I'll try to run some more tests over the weekend (e.g. with different clocksources... this is with clocksource=hpet), but thought I'd report what I have seen. I'm running on a dual-core single socket ("Conroe").

Dan

P.S. If you try the patch, ensure you set
the boot parameter "measurestime".

Quoted 22 lines

-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Wednesday, July 23, 2008 1:06 AM
To: [email protected]; Xen-Devel (E-mail)
Cc: Dave Winchell
Subject: Re: s_time going backwards on same processor?

Quoted 9 lines · Dan Magenheimer

I *do* know that get_s_time() on different processors can have this behavior and I know it is possible for hvm_get_guest_time() to go backwards (timer_mode=0), but I thought s_time was monotonically non-decreasing on any given processor and that read_platform_stime() is also monotonically non-decreasing.

Does dom0 maybe have direct hardware access to the hardware platform timer that xen system time is dependent on?

No matter what happens to the underlying platform timer,

it should be

Quoted 15 lines
Quoted 15 lines
Quoted 14 lines

impossible for Xen system time to go backwards on any given processor. The
calibration function never sets the TSC and system timestamps for the next
time record any earlier than current TSC value and current computed system
time value. Hence it should be impossible for system time to be computed as
earlier than that time record.

-- Keir

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel