Skip to contentSkip to conversation

System time monotonicity

DISCUSSION John Levon 4 participants 10 messages Mar 26, 2007 – Apr 3, 2007

lore
Share & tools
lore

John Levon

author
#0 lore raw

It seems that VCPU system time isn't monotonic (using 3.0.4). It seems it might be correlated to when a VCPU is switched across real CPUs but I haven't conclusively proved that. But e.g.:

    {
        old = {
            time = {
                version = 0x4ec
                pad0 = 0xe8e0
                tsc_timestamp = 0x22cc8398b7194
                system_time = 0xe8e0345d8805
                tsc_to_system_mul = 0xd62c0083
                tsc_shift = '\377'
                pad1 = [ '\002', '\027', '\365' ]
            }
            result = 0xe8e0484568fa
            tsc = 0x22cc86921ab00
            cpu = 0
        }
        new = {
            time = {
                version = 0x4ee
                pad0 = 0
                tsc_timestamp = 0x22cc7db96cd29
                system_time = 0xe8e00d1031f3
                tsc_to_system_mul = 0xd62ae844
                tsc_shift = '\377'
                pad1 = [ '\357', '\002', '\365' ]
            }
            result = 0xe8e048456012
            tsc = 0x22cc869225443
            cpu = 0
        }
        delta = 0xfffffffffffff718
    }
Quoted 1 line

From what I can work out, time is supposed to be monotonic but I admit I

can't really understand the time code yet at least. I couldn't find any documentation on what to expect from system time. Any suggestions?

This seems to happen across all the hardware we've tried but this particular case is a Sun V20Z with two CPUs:

x86 (AuthenticAMD family 15 model 5 step 10 clock 2392 MHz)
AMD Opteron(tm) Processor 250

cheers,
john

#1 lore raw
Quoted 9 lines · John Levon
Quoted 1 line

From what I can work out, time is supposed to be monotonic but I admit I

can't really understand the time code yet at least. I couldn't find any documentation on what to expect from system time. Any suggestions?

This seems to happen across all the hardware we've tried but this particular case is a Sun V20Z with two CPUs:

x86 (AuthenticAMD family 15 model 5 step 10 clock 2392 MHz)
AMD Opteron(tm) Processor 250

Small backwards time deltas are possible from the current time code. You'll have to filter them out yourself if you can't deal with them. We could add extra code in Xen to stop this happening for any individual VCPU.

-- Keir

author
#2 lore raw
Quoted 7 lines · Keir Fraser
Quoted 7 lines
Quoted 1 line

From what I can work out, time is supposed to be monotonic but I admit I

can't really understand the time code yet at least. I couldn't find any documentation on what to expect from system time. Any suggestions?

This seems to happen across all the hardware we've tried but this particular case is a Sun V20Z with two CPUs:

Quoted 6 lines
Quoted 2 lines

x86 (AuthenticAMD family 15 model 5 step 10 clock 2392 MHz)
AMD Opteron(tm) Processor 250

Small backwards time deltas are possible from the current time code. You'll have to filter them out yourself if you can't deal with them. We could add extra code in Xen to stop this happening for any individual VCPU

Some instrumentation indicated that we had cross-VCPU jitter of significant deltas, ~18us at worst. Though the instrumentation wasn't completely reliable so that might not be accurate.

On real hardware we deal with any jitter via comparing against a regular clock on CPU0. This obviously won't work for Xen since we have no control over where VCPU0 actually lives. Presumably the Linux code (monotonic_clock() especially) must have something to handle it, but I can't see where...

regards
john

#3 lore raw
Quoted 7 lines · John Levon
Quoted 3 lines

Small backwards time deltas are possible from the current time code. You'll have to filter them out yourself if you can't deal with them. We could add extra code in Xen to stop this happening for any individual VCPU

Some instrumentation indicated that we had cross-VCPU jitter of significant deltas, ~18us at worst. Though the instrumentation wasn't completely reliable so that might not be accurate.

I should add that time synchronisation is currently broken in xen-unstable, and has been for about two weeks. I just checked in a patch (based on one from Jan Beulich) to fix this (changeset 14573:ba9d3fd4ee4b).

-- Keir

author
#4 lore raw
In reply to John Levon
Quoted 10 lines · John Levon
Quoted 10 lines
Quoted 9 lines
Quoted 1 line

From what I can work out, time is supposed to be monotonic but I admit I

can't really understand the time code yet at least. I couldn't find any documentation on what to expect from system time. Any suggestions?

This seems to happen across all the hardware we've tried but this particular case is a Sun V20Z with two CPUs:

x86 (AuthenticAMD family 15 model 5 step 10 clock 2392 MHz)
AMD Opteron(tm) Processor 250

Quoted 7 lines
Quoted 3 lines

Small backwards time deltas are possible from the current time code. You'll have to filter them out yourself if you can't deal with them. We could add extra code in Xen to stop this happening for any individual VCPU

Some instrumentation indicated that we had cross-VCPU jitter of significant deltas, ~18us at worst. Though the instrumentation wasn't completely reliable so that might not be accurate.

I've done some more testing, the problem appears to be significantly worse than you're claiming. I store the previously read value in the thread structure and breakpoint whenever it fails:

[2]> fffffffece9e6040::print kthread_t t_dhrtime t_dhrtime_tsc t_dhrtime_cpu t_dhrtime_vers t_dhrtime = 0x3de7923989c
t_dhrtime_tsc = 0x9414981ce0e
t_dhrtime_cpu = 0x2
t_dhrtime_vers = 0x2616

And the new values:
t_dhrtime = 0x3de7923088b
t_dhrtime_tsc = 0x9414982169b
t_dhrtime_cpu = 0x2
t_dhrtime_vers = 0x2618

[2]> 0x3de7923989c - 0x000003de7923088b=D

                36881

That's 36us. It doesn't even seem that we migrated across physical CPUs given the version field. So something seems significantly awry to me?

(I must admit I still don't quite understand why monotonic_clock() is called monotonic_clock() ...)

regards
john

#5 lore raw
In reply to John Levon
Quoted 2 lines

It seems that VCPU system time isn't monotonic (using 3.0.4). It seems it might be correlated to when a VCPU is switched across real CPUs but

I

Quoted 1 line

haven't conclusively proved that. But e.g.:

What output do you get when you hit 't' a few times on the xen serial console?

There's no guarantee that the system time calculated will be perfectly monotonic, but it should be very close. If the guest needs it to be monotonic, the time reading code should simply clamp the value that's read to ensure it always goes up. A little jitter at the microsecond granularity should be just fine.

On your system it appears to be a couple of microseconds out, which is on the high side of what we've observed. Normally you only see that kind of mismatch on systems with TSCs running off different crystals.

Ian

Quoted 23 lines
    {
        old = {
            time = {
                version = 0x4ec
                pad0 = 0xe8e0
                tsc_timestamp = 0x22cc8398b7194
                system_time = 0xe8e0345d8805
                tsc_to_system_mul = 0xd62c0083
                tsc_shift = '\377'
                pad1 = [ '\002', '\027', '\365' ]
            }
            result = 0xe8e0484568fa
            tsc = 0x22cc86921ab00
            cpu = 0
        }
        new = {
            time = {
                version = 0x4ee
                pad0 = 0
                tsc_timestamp = 0x22cc7db96cd29
                system_time = 0xe8e00d1031f3
                tsc_to_system_mul = 0xd62ae844
                tsc_shift = '\377'
Quoted 10 lines
                pad1 = [ '\357', '\002', '\365' ]
            }
            result = 0xe8e048456012
            tsc = 0x22cc869225443
            cpu = 0
        }
        delta = 0xfffffffffffff718
    }
Quoted 1 line

From what I can work out, time is supposed to be monotonic but I

admit

Quoted 2 lines

I
can't really understand the time code yet at least. I couldn't find

any

Quoted 15 lines

documentation on what to expect from system time. Any suggestions?

This seems to happen across all the hardware we've tried but this particular case is a Sun V20Z with two CPUs:

x86 (AuthenticAMD family 15 model 5 step 10 clock 2392 MHz)
AMD Opteron(tm) Processor 250

cheers,
john

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

#6 lore raw
Quoted 3 lines · Ian Pratt

On your system it appears to be a couple of microseconds out, which is on the high side of what we've observed. Normally you only see that kind of mismatch on systems with TSCs running off different crystals.

More likely a jittery chipset timer -- we've observed less-than-ideal stability from some chipset timers, which can throw us off a bit when independently sync'ing the TSCs (which each CPU does for its TSC independently every couple of seconds).

-- Keir

author
#7 lore raw
Quoted 10 lines · Keir Fraser
Quoted 3 lines · Ian Pratt

On your system it appears to be a couple of microseconds out, which is on the high side of what we've observed. Normally you only see that kind of mismatch on systems with TSCs running off different crystals.

More likely a jittery chipset timer -- we've observed less-than-ideal stability from some chipset timers, which can throw us off a bit when independently sync'ing the TSCs (which each CPU does for its TSC independently every couple of seconds).

And what about cross-VCPU monotonicity? It's called very frequently and having to fake monotonicity via a single variable across all CPUs would not be pleasant, unless I can think of something smarter...

regards
john

#8 lore raw
Quoted 1 line
Quoted 1 line

More likely a jittery chipset timer -- we've observed

less-than-ideal

Quoted 1 line
Quoted 1 line

stability from some chipset timers, which can throw us off a bit

when

Quoted 4 lines
Quoted 2 lines

independently sync'ing the TSCs (which each CPU does for its TSC independently every couple of seconds).

And what about cross-VCPU monotonicity? It's called very frequently

and

Quoted 1 line

having to fake monotonicity via a single variable across all CPUs

would

Quoted 1 line

not be pleasant, unless I can think of something smarter...

One LOCK'ed cmpxchg is still a lot quicker than reading the pm_timer or hpet...

Ian

#9 lore raw
In reply to John Levon
Quoted 8 lines · John Levon
Quoted 4 lines

More likely a jittery chipset timer -- we've observed less-than-ideal stability from some chipset timers, which can throw us off a bit when independently sync'ing the TSCs (which each CPU does for its TSC independently every couple of seconds).

And what about cross-VCPU monotonicity? It's called very frequently and having to fake monotonicity via a single variable across all CPUs would not be pleasant, unless I can think of something smarter...

Yet it is what you need to do if you want to absolutely guarantee monotonic time across all VCPUS. I don't think there's any way around that, but as Ian says it's still going to be cheaper than reading a chipset timer. I'd be inclined to settle for per-vcpu and per-OS-task monotonicity.

-- Keir