monotonically increasing Xen system time
(This is probably post-3.3, but feedback would be appreciated as I would hope it could go into 3.3.1 if it doesn't make 3.3.)
I've finally surrendered to the fact that intra-CPU stime skew can't be reduced down to the point where it can be ignored, at least on non-tsc-invariant boxes. It always seems to max out at least at several microseconds, and in some cases in tens of microseconds. This is probably a result of crystal oscillation drift, and perhaps the "beating" of the platform timer crystal vs the tsc crystal.
So the attached patch adds a get_s_time_mono() call that always returns a monotonically INcreasing (not just non-decreasing) stime. A stime_minstep is computed that guarantees that mono_stime can't increase faster than stime, even if all processors are pounding on stime in a loop. The result is the resolution for mono_stime. (On my dual-core box, it's 24ns... your mileage may vary.)
I want to use this in hvm_get_guest_time() (and thus for softtsc) but it may also be appropriate for at least some of the many uses of NOW() in Xen. If so, it might make sense that this should be the default get_s_time() and the current get_s_time() should be renamed get_local_s_time(). In any case, there are most likely other uses for it in Xen so I didn't want to build it just into hvm_get_guest_time().
(Note that init_xen_time() was moved down in __start_xen() because num_online_cpus() gave the wrong answer at its current position.)
===================================
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/arch/x86/setup.c +8 -8
@@ -944,14 +944,6 @@ void __init __start_xen(unsigned long mbsmp_prepare_cpus (max_cpus); -/* -* Initialise higher-level timer functions. We do this fairly late -* (post-SMP) because the time bases and scale factors need to be updated -* regularly, and SMP initialisation can cause a long delay with -* interrupts not yet enabled. -*/ -init_xen_time (); -initialize_keytable ();serial_init_postirq ();
@@ -976,6 +968,14 @@ void __init __start_xen(unsigned long mbprintk ("Brought up %ld CPUs\n" , (long )num_online_cpus ());smp_cpus_done (max_cpus); + +/* +* Initialise higher-level timer functions. We do this fairly late +* (post-SMP) because the time bases and scale factors need to be updated +* regularly, and SMP initialisation can cause a long delay with +* interrupts not yet enabled. +*/ +init_xen_time ();initialise_gdb ();/* could be moved earlier */
xen/arch/x86/time.c +40 -0
@@ -752,6 +752,9 @@ static unsigned long get_cmos_time(void)* System Time ***************************************************************************/ +u32 stime_minstep =0 ; +static DEFINE_SPINLOCK (stime_lock); +s_time_t get_s_time (void ) {struct cpu_time *t = &this_cpu (cpu_time);
@@ -763,6 +766,41 @@ s_time_t get_s_time(void) now = t->stime_local_stamp +scale_delta (delta, &t->tsc_scale);return now; +} + +/* monotonically increasing stime is required because of inter-CPU skew */ +s_time_t get_s_time_mono (void ) +{ +s_time_t now; +unsigned long flags; +static s_time_t last =0 ; + +spin_lock_irqsave (&stime_lock, flags); + now =get_s_time (); +if ( (int64_t )(now - last) >=0 ) + last = now; +else + now = (last += stime_minstep); +spin_unlock_irqrestore (&stime_lock, flags); + +return now; +} + +void init_stime_resolution (void ) +{ +u32 dif, minstep =0xffffffff ; +int i, ncpus =num_online_cpus (); +s_time_t t1, t2; + +for (i =0 ; i <10 ; i++) + { + t1 =get_s_time_mono (); + t2 =get_s_time_mono (); + dif = t2 - t1; +if (dif < minstep) + minstep = dif; + } + stime_minstep = minstep / ncpus; }static inline void version_update_begin (u32 *version)
@@ -1069,6 +1107,8 @@ int __init init_xen_time(void) stime_platform_stamp =0 ;init_platform_timer (); +init_stime_resolution (); +do_settime (get_cmos_time (),0 ,NOW ());local_irq_enable ();
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Xen itself doesn't in most cases care about a bit of skew across CPUs. So I think the cuurent get_s_time() is fine, and then build monotonicity on top where we want it (notably hvm_get_guest_time() as has already been done).
-- Keir
Quoted 40 lines · Dan Magenheimer
(This is probably post-3.3, but feedback would be appreciated as I would hope it could go into 3.3.1 if it doesn't make 3.3.)
I've finally surrendered to the fact that intra-CPU stime skew can't be reduced down to the point where it can be ignored, at least on non-tsc-invariant boxes. It always seems to max out at least at several microseconds, and in some cases in tens of microseconds. This is probably a result of crystal oscillation drift, and perhaps the "beating" of the platform timer crystal vs the tsc crystal.
So the attached patch adds a
get_s_time_mono()call that always returns a monotonically INcreasing (not just non-decreasing) stime. Astime_minstepis computed that guarantees thatmono_stimecan't increase faster than stime, even if all processors are pounding on stime in a loop. The result is the resolution formono_stime. (On my dual-core box, it's 24ns... your mileage may vary.)I want to use this in
hvm_get_guest_time()(and thus for softtsc) but it may also be appropriate for at least some of the many uses ofNOW()in Xen. If so, it might make sense that this should be the defaultget_s_time()and the currentget_s_time()should be renamedget_local_s_time(). In any case, there are most likely other uses for it in Xen so I didn't want to build it just intohvm_get_guest_time().(Note that
init_xen_time()was moved down in__start_xen()becausenum_online_cpus()gave the wrong answer at its current position.)Signed-off-by Dan Magenheimer <[email protected]>===================================
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)
Since my primary purpose is to guarantee monotonically INcreasing time to hvm_get_guest_time(), I am happy to resubmit the patch with only that fix provided (as the current solution only provides monotonically NON-DEcreasing time).
However, having spent far too much time looking at inter-CPU skew in the last few weeks, I have to argue. You say Xen doesn't care in *most* cases. I suspect that it does care in *some* cases and that there are one or more subtle race conditions that would only get tripped if skew is "very bad". Since it gets bad randomly and with very low frequency, and since on some machines "bad" may be "very bad", and since such races will be nearly impossible to isolate and debug... and since the increasing adoption of high-precision timers is setting higher expectations of resolution, I thought it might be wise to provide a solution that avoids the possibility of a skew-related bug. And then examine each use of NOW() to see whether it is really using it as a timestamp or as a "period from now on the current processor".
Thanks,
Dan
Quoted 62 lines
-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Monday, July 28, 2008 1:10 PM
To: [email protected]; Xen-Devel (E-mail)
Subject: Re: [PATCH?] monotonically increasing Xen system timeXen itself doesn't in most cases care about a bit of skew across CPUs. So I
think the cuurentget_s_time()is fine, and then build monotonicity on top
where we want it (notablyhvm_get_guest_time()as has already been done).-- Keir
Quoted 40 lines · Dan Magenheimer
(This is probably post-3.3, but feedback would be appreciated as I would hope it could go into 3.3.1 if it doesn't make 3.3.)
I've finally surrendered to the fact that intra-CPU stime skew can't be reduced down to the point where it can be ignored, at least on non-tsc-invariant boxes. It always seems to max out at least at several microseconds, and in some cases in tens of microseconds. This is probably a result of crystal oscillation drift, and perhaps the "beating" of the platform timer crystal vs the tsc crystal.
So the attached patch adds a
get_s_time_mono()call that always returns a monotonically INcreasing (not just non-decreasing) stime. Astime_minstepis computed that guarantees thatmono_stimecan't increase faster than stime, even if all processors are pounding on stime in a loop. The result is the resolution formono_stime. (On my dual-core box, it's 24ns... your mileage may vary.)I want to use this in
hvm_get_guest_time()(and thus for softtsc) but it may also be appropriate for at least some of the many uses ofNOW()in Xen. If so, it might make sense that this should be the defaultget_s_time()and the currentget_s_time()should be renamedget_local_s_time(). In any case, there are most likely other uses for it in Xen so I didn't want to build it just intohvm_get_guest_time().(Note that
init_xen_time()was moved down in__start_xen()becausenum_online_cpus()gave the wrong answer at its current position.)Signed-off-by Dan Magenheimer <[email protected]>===================================
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)
Quoted 1 line
Xen itself doesn't in most cases care about a bit of skew
Well, FYI, I did a survey of all the uses of NOW() in Xen and couldn't find ANY that would be sensitive to even tens of microseconds of skew across CPUs.
So, one point for Keir... as usual ;-)
Quoted 62 lines
-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Monday, July 28, 2008 1:10 PM
To: [email protected]; Xen-Devel (E-mail)
Subject: Re: [PATCH?] monotonically increasing Xen system timeXen itself doesn't in most cases care about a bit of skew across CPUs. So I
think the cuurentget_s_time()is fine, and then build monotonicity on top
where we want it (notablyhvm_get_guest_time()as has already been done).-- Keir
Quoted 40 lines · Dan Magenheimer
(This is probably post-3.3, but feedback would be appreciated as I would hope it could go into 3.3.1 if it doesn't make 3.3.)
I've finally surrendered to the fact that intra-CPU stime skew can't be reduced down to the point where it can be ignored, at least on non-tsc-invariant boxes. It always seems to max out at least at several microseconds, and in some cases in tens of microseconds. This is probably a result of crystal oscillation drift, and perhaps the "beating" of the platform timer crystal vs the tsc crystal.
So the attached patch adds a
get_s_time_mono()call that always returns a monotonically INcreasing (not just non-decreasing) stime. Astime_minstepis computed that guarantees thatmono_stimecan't increase faster than stime, even if all processors are pounding on stime in a loop. The result is the resolution formono_stime. (On my dual-core box, it's 24ns... your mileage may vary.)I want to use this in
hvm_get_guest_time()(and thus for softtsc) but it may also be appropriate for at least some of the many uses ofNOW()in Xen. If so, it might make sense that this should be the defaultget_s_time()and the currentget_s_time()should be renamedget_local_s_time(). In any case, there are most likely other uses for it in Xen so I didn't want to build it just intohvm_get_guest_time().(Note that
init_xen_time()was moved down in__start_xen()becausenum_online_cpus()gave the wrong answer at its current position.)Signed-off-by Dan Magenheimer <[email protected]>===================================
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)