Skip to contentSkip to conversation

Improve hpet accuracy

PATCH In review Ben Guthro 5 participants 39 messages Jun 5, 2008 – Jun 12, 2008

lore
Share & tools
lore

Ben Guthro

author
#0 lore raw

cover Improve hpet accuracy

1. Introduction

This patch improves the hpet based guest clock in terms of drift and monotonicity. Prior to this work the drift with hpet was greater than 2%, far above the .05% limit for ntp to synchronize. With this code, the drift ranges from .001% to .0033% depending on guest and physical platform.

Using hpet allows guest operating systems to provide monotonic time to their applications. Time sources other than hpet are not monotonic because of their reliance on tsc, which is not synchronized across physical processors.

Windows 2k864 and many Linux guests are supported with two policies, one for guests that handle missed clock interrupts and the other for guests that require the correct number of interrupts.

Guests may use hpet for the timing source even if the physical platform has no visible hpet. Migration is supported between physical machines which differ in physical hpet visibility.

Most of the changes are in hpet.c. Two general facilities are added to track interrupt progress. The ideas here and the facilities would be useful in vpt.c, for other time sources, though no attempt is made here to improve vpt.c.

The following sections discuss hpet dependencies, interrupt delivery policies, live migration, test results, and relation to recent work with monotonic time.

2. Virtual Hpet dependencies

The virtual hpet depends on the ability to read the physical or simulated (see discussion below) hpet. For timekeeping, the virtual hpet also depends on two new interrupt notification facilities to implement its policies for interrupt delivery.

2.1. Two modes of low-level hpet main counter reads.

In this implementation, the virtual hpet reads with read_64_main_counter(), exported by time.c, either the real physical hpet main counter register directly or a "simulated" hpet main counter.

The simulated mode uses a monotonic version of get_s_time() (NOW()), where the last time value is returned whenever the current time value is less than the last time value. In simulated mode, since it is layered on s_time, the underlying hardware can be hpet or some other device. The frequency of the main counter in simulated mode is the same as the standard physical hpet frequency, allowing live migration between nodes that are configured differently.

If the physical platform does not have an hpet device, or if xen is configured not to use the device, then the simulated method is used. If there is a physical hpet device, and xen has initialized it, then either simulated or physical mode can be used. This is governed by a boot time option, hpet-avoid. Setting this option to 1 gives the simulated mode and 0 the physical mode. The default is physical mode.

A disadvantage of the physical mode is that may take longer to read the device than in simulated mode. On some platforms the cost is about the same (less than 250 nsec) for physical and simulated modes, while on others physical cost is much higher than simulated. A disadvantage of the simulated mode is that it can return the same value
for the counter in consecutive calls.

2.2. Interrupt notification facilities.

Two interrupt notification facilities are introduced, one is hvm_isa_irq_assert_cb() and the other hvm_register_intr_en_notif().

The vhpet uses hvm_isa_irq_assert_cb to deliver interrupts to the vioapic. hvm_isa_irq_assert_cb allows a callback to be passed along to vioapic_deliver() and this callback is called with a mask of the vcpus which will get the interrupt. This callback is made before any vcpus receive an interrupt.

Vhpet uses hvm_register_intr_en_notif() to register a handler for a particular vector that will be called when that vector is injected in [vmx,svm]_intr_assist() and also when the guest finishes handling the interrupt. Here finished is defined as the point when the guest re-enables interrupts or lowers the tpr value. EOI is not used as the end of interrupt as this is sometimes returned before the interrupt handler has done its work. A flag is passed to the handler indicating whether this is the injection point (post = 1) or the interrupt finished (post = 0) point. The need for the finished point callback is discussed in the missed ticks policy section.

To prevent a possible early trigger of the finished callback, intr_en_notif logic has a two stage arm, the first at injection (hvm_intr_en_notif_arm()) and the second when interrupts are seen to be disabled (hvm_intr_en_notif_disarm()). Once fully armed, re-enabling interrupts will cause hvm_intr_en_notif_disarm() to make the end of interrupt callback. hvm_intr_en_notif_arm() and hvm_intr_en_notif_disarm() are called by [vmx,svm]_intr_assist().

3. Interrupt delivery policies

The existing hpet interrupt delivery is preserved. This includes vcpu round robin delivery used by Linux and broadcast delivery used by Windows.

There are two policies for interrupt delivery, one for Windows 2k8-64 and the other for Linux. The Linux policy takes advantage of the (guest) Linux missed tick and offset calculations and does not attempt to deliver the right number of interrupts. The Windows policy delivers the correct number of interrupts, even if sometimes much closer to each other than the period. The policies are similar to those in vpt.c, though there are some important differences.

Policies are selected with an HVMOP_set_param hypercall with index HVM_PARAM_TIMER_MODE. Two new values are added, HVM_HPET_guest_computes_missed_ticks and HVM_HPET_guest_does_not_compute_missed_ticks. The reason that two new ones are added is that in some guests (32bit Linux) a no-missed policy is needed for clock sources other than hpet and a missed ticks policy for hpet. It was felt that there would be less confusion by simply introducing the two hpet policies.

3.1. The missed ticks policy

The Linux clock interrupt handler for hpet calculates missed ticks and offset using the hpet main counter. The algorithm works well when the time since the last interrupt is greater than or equal to a period and poorly otherwise.

The missed ticks policy ensures that no two clock interrupts are delivered to the guest at a time interval less than a period. A time stamp (hpet main counter value) is recorded (by a callback registered with hvm_register_intr_en_notif) when Linux finishes handling the clock interrupt. Then, ensuing interrupts are delivered to the vioapic only if the current main counter value is a period greater than when the last interrupt was handled.

Tests showed a significant improvement in clock drift with end of interrupt time stamps versus beginning of interrupt[1]. It is believed that the reason for the improvement is that the clock interrupt handler goes for a spinlock and can be therefore delayed in its processing. Furthermore, the main counter is read by the guest under the lock. The net effect is that if we time stamp injection, we can get the difference in time between successive interrupt handler lock acquisitions to be less than the period.

3.2. The no-missed ticks policy

Windows 2k864 keeps very poor time with the missed ticks policy. So the no-missed ticks policy was developed. In the no-missed ticks policy we deliver the correct number of interrupts, even if they are spaced less than a period apart (when catching up).

Windows 2k864 uses a broadcast mode in the interrupt routing such that
all vcpus get the clock interrupt. The best Windows drift performance was achieved when the policy code ensured that all the previous interrupts (on the various vcpus) had been injected before injecting the next interrupt to the vioapic..

The policy code works as follows. It uses the hvm_isa_irq_assert_cb() to record the vcpus to be interrupted in h->hpet.pending_mask. Then, in the callback registered with hvm_register_intr_en_notif() at post=1 time it clears the current vcpu in the pending_mask. When the pending_mask is clear it decrements hpet.intr_pending_nr and if intr_pending_nr is still non-zero posts another interrupt to the ioapic with hvm_isa_irq_assert_cb(). Intr_pending_nr is incremented in hpet_route_decision_not_missed_ticks().

The missed ticks policy intr_en_notif callback also uses the pending_mask method. So even though Linux does not broadcast its interrupts, the code could handle it if it did.
In this case the end of interrupt time stamp is made when the pending_mask is clear.

4. Live Migration

Live migration with hpet preserves the current offset of the guest clock with respect to ntp. This is accomplished by migrating all of the state in the h->hpet data structure in the usual way. The hp->mc_offset is recalculated on the receiving node so that the guest sees a continuous hpet main counter.

Code as been added to xc_domain_save.c to send a small message after the
domain context is sent. The contents of the message is the physical tsc timestamp, last_tsc, read just before the message is sent. When the last_tsc message is received in xc_domain_restore.c, another physical tsc timestamp, cur_tsc, is read. The two timestamps are loaded into the domain structure as last_tsc_sender and first_tsc_receiver with hypercalls. Then xc_domain_hvm_setcontext is called so that hpet_load has access to these time stamps. Hpet_load uses the timestamps to account for the time spent saving and loading the domain context. With this technique, the only neglected time is the time spent sending a small network message.

5. Test Results

Some recent test results are:

5.1 Linux 4u664 and Windows 2k864 load test.
Duration: 70 hours.
Test date: 6/2/08
Loads: usex -b48 on Linux; burn-in on Windows Guest vcpus: 8 for Linux; 2 for Windows
Hardware: 8 physical cpu AMD
Clock drift : Linux: .0012% Windows: .009%

5.2 Linux 4u664, Linux 4u464 , and Windows 2k864 no-load test
Duration: 23 hours.
Test date: 6/3/08
Loads: none
Guest vcpus: 8 for each Linux; 2 for Windows
Hardware: 4 physical cpu AMD
Clock drift : Linux: .033% Windows: .019%

6. Relation to recent work in xen-unstable

There is a similarity between hvm_get_guest_time() in xen-unstable and read_64_main_counter() in this code. However, read_64_main_counter() is more tuned to the needs of hpet.c. It has no "set" operation, only the get. It isolates the mode, physical or simulated, in read_64_main_counter() itself. It uses no vcpu or domain state as it is a physical entity, in either mode. And it provides a real physical mode for every read for those applications that desire this.

7. Conclusion

The virtual hpet is improved by this patch in terms of accuracy and monotonicity. Tests performed to date verify this and more testing is under way.

8. Future Work

Testing with Windows Vista will be performed soon. The reason for accuracy variations on different platforms using the physical hpet device will be investigated. Additional overhead measurements on simulated vs physical hpet mode will be made.

Footnotes:

1. I don't recall the accuracy improvement with end of interrupt stamping, but it was significant, perhaps better than two to one improvement. It would be a very simple matter to re-measure the improvement as the facility can call back at injection time as well.

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

#1 lore raw

Are these patches needed now the timers are built on Xen system time rather than host TSC? Dan has reported much better time-keeping with his patch checked in, and it¹s for sure a lot less invasive than this patchset.

-- Keir

Quoted 292 lines · Ben Guthro

1. Introduction

This patch improves the hpet based guest clock in terms of drift and monotonicity.
Prior to this work the drift with hpet was greater than 2%, far above the .05% limit
for ntp to synchronize. With this code, the drift ranges from .001% to .0033% depending
on guest and physical platform.

Using hpet allows guest operating systems to provide monotonic time to their applications. Time sources other than hpet are not monotonic because of their reliance on tsc, which is not synchronized across physical processors.

Windows 2k864 and many Linux guests are supported with two policies, one for guests
that handle missed clock interrupts and the other for guests that require the correct number of interrupts.

Guests may use hpet for the timing source even if the physical platform has no visible
hpet. Migration is supported between physical machines which differ in physical
hpet visibility.

Most of the changes are in hpet.c. Two general facilities are added to track interrupt
progress. The ideas here and the facilities would be useful in vpt.c, for other time
sources, though no attempt is made here to improve vpt.c.

The following sections discuss hpet dependencies, interrupt delivery policies, live migration,
test results, and relation to recent work with monotonic time.

2. Virtual Hpet dependencies

The virtual hpet depends on the ability to read the physical or simulated (see discussion below) hpet. For timekeeping, the virtual hpet also depends on two new interrupt notification facilities to implement its policies for interrupt delivery.

2.1. Two modes of low-level hpet main counter reads.

In this implementation, the virtual hpet reads with read_64_main_counter(), exported by
time.c, either the real physical hpet main counter register directly or a "simulated"
hpet main counter.

The simulated mode uses a monotonic version of get_s_time() (NOW()), where the last
time value is returned whenever the current time value is less than the last time
value. In simulated mode, since it is layered on s_time, the underlying hardware
can be hpet or some other device. The frequency of the main counter in simulated
mode is the same as the standard physical hpet frequency, allowing live migration
between nodes that are configured differently.

If the physical platform does not have an hpet device, or if xen is configured not
to use the device, then the simulated method is used. If there is a physical hpet device,
and xen has initialized it, then either simulated or physical mode can be used.
This is governed by a boot time option, hpet-avoid. Setting this option to 1 gives the
simulated mode and 0 the physical mode. The default is physical mode.

A disadvantage of the physical mode is that may take longer to read the device than in simulated mode. On some platforms the cost is about the same (less than 250 nsec) for
physical and simulated modes, while on others physical cost is much higher than simulated.
A disadvantage of the simulated mode is that it can return the same value for the counter in consecutive calls.

2.2. Interrupt notification facilities.

Two interrupt notification facilities are introduced, one is hvm_isa_irq_assert_cb()
and the other hvm_register_intr_en_notif().

The vhpet uses hvm_isa_irq_assert_cb to deliver interrupts to the vioapic. hvm_isa_irq_assert_cb allows a callback to be passed along to vioapic_deliver()
and this callback is called with a mask of the vcpus which will get the interrupt. This callback is made before any vcpus receive an interrupt.

Vhpet uses hvm_register_intr_en_notif() to register a handler for a particular vector that will be called when that vector is injected in [vmx,svm]_intr_assist()
and also when the guest finishes handling the interrupt. Here finished is defined
as the point when the guest re-enables interrupts or lowers the tpr value. EOI is not used as the end of interrupt as this is sometimes returned before the interrupt handler has done its work. A flag is passed to the handler indicating
whether this is the injection point (post = 1) or the interrupt finished (post = 0) point.
The need for the finished point callback is discussed in the missed ticks policy section.

To prevent a possible early trigger of the finished callback, intr_en_notif logic
has a two stage arm, the first at injection (hvm_intr_en_notif_arm()) and the second when
interrupts are seen to be disabled (hvm_intr_en_notif_disarm()). Once fully armed, re-enabling
interrupts will cause hvm_intr_en_notif_disarm() to make the end of interrupt callback. hvm_intr_en_notif_arm() and hvm_intr_en_notif_disarm() are called by [vmx,svm]_intr_assist().

3. Interrupt delivery policies

The existing hpet interrupt delivery is preserved. This includes vcpu round robin delivery used by Linux and broadcast delivery used by Windows.

There are two policies for interrupt delivery, one for Windows 2k8-64 and the other
for Linux. The Linux policy takes advantage of the (guest) Linux missed tick and offset
calculations and does not attempt to deliver the right number of interrupts. The Windows policy delivers the correct number of interrupts, even if sometimes much
closer to each other than the period. The policies are similar to those in vpt.c, though
there are some important differences.

Policies are selected with an HVMOP_set_param hypercall with index HVM_PARAM_TIMER_MODE.
Two new values are added, HVM_HPET_guest_computes_missed_ticks and HVM_HPET_guest_does_not_compute_missed_ticks. The reason that two new ones are added is that
in some guests (32bit Linux) a no-missed policy is needed for clock sources other than hpet
and a missed ticks policy for hpet. It was felt that there would be less confusion by simply
introducing the two hpet policies.

3.1. The missed ticks policy

The Linux clock interrupt handler for hpet calculates missed ticks and offset using the hpet
main counter. The algorithm works well when the time since the last interrupt is greater than
or equal to a period and poorly otherwise.

The missed ticks policy ensures that no two clock interrupts are delivered to the guest at
a time interval less than a period. A time stamp (hpet main counter value) is recorded (by a
callback registered with hvm_register_intr_en_notif) when Linux finishes handling the clock
interrupt. Then, ensuing interrupts are delivered to the vioapic only if the current main
counter value is a period greater than when the last interrupt was handled.

Tests showed a significant improvement in clock drift with end of interrupt time stamps
versus beginning of interrupt[1]. It is believed that the reason for the improvement
is that the clock interrupt handler goes for a spinlock and can be therefore delayed in its
processing. Furthermore, the main counter is read by the guest under the lock. The net
effect is that if we time stamp injection, we can get the difference in time between successive interrupt handler lock acquisitions to be less than the period.

3.2. The no-missed ticks policy

Windows 2k864 keeps very poor time with the missed ticks policy. So the no-missed ticks policy
was developed. In the no-missed ticks policy we deliver the correct number of interrupts,
even if they are spaced less than a period apart (when catching up).

Windows 2k864 uses a broadcast mode in the interrupt routing such that all vcpus get the clock interrupt. The best Windows drift performance was achieved when the
policy code ensured that all the previous interrupts (on the various vcpus) had been injected
before injecting the next interrupt to the vioapic..

The policy code works as follows. It uses the hvm_isa_irq_assert_cb() to record
the vcpus to be interrupted in h->hpet.pending_mask. Then, in the callback registered
with hvm_register_intr_en_notif() at post=1 time it clears the current vcpu in the pending_mask.
When the pending_mask is clear it decrements hpet.intr_pending_nr and if intr_pending_nr is still
non-zero posts another interrupt to the ioapic with hvm_isa_irq_assert_cb(). Intr_pending_nr is incremented in hpet_route_decision_not_missed_ticks().

The missed ticks policy intr_en_notif callback also uses the pending_mask method. So even though
Linux does not broadcast its interrupts, the code could handle it if it did. In this case the end of interrupt time stamp is made when the pending_mask is clear.

4. Live Migration

Live migration with hpet preserves the current offset of the guest clock with respect
to ntp. This is accomplished by migrating all of the state in the h->hpet data structure
in the usual way. The hp->mc_offset is recalculated on the receiving node so that the
guest sees a continuous hpet main counter.

Code as been added to xc_domain_save.c to send a small message after the domain context is sent. The contents of the message is the physical tsc timestamp, last_tsc,
read just before the message is sent. When the last_tsc message is received in xc_domain_restore.c,
another physical tsc timestamp, cur_tsc, is read. The two timestamps are loaded into the domain
structure as last_tsc_sender and first_tsc_receiver with hypercalls. Then xc_domain_hvm_setcontext
is called so that hpet_load has access to these time stamps. Hpet_load uses the timestamps
to account for the time spent saving and loading the domain context. With this technique,
the only neglected time is the time spent sending a small network message.

5. Test Results

Some recent test results are:

5.1 Linux 4u664 and Windows 2k864 load test.
Duration: 70 hours.
Test date: 6/2/08
Loads: usex -b48 on Linux; burn-in on Windows Guest vcpus: 8 for Linux; 2 for Windows
Hardware: 8 physical cpu AMD
Clock drift : Linux: .0012% Windows: .009%

5.2 Linux 4u664, Linux 4u464 , and Windows 2k864 no-load test
Duration: 23 hours.
Test date: 6/3/08
Loads: none
Guest vcpus: 8 for each Linux; 2 for Windows
Hardware: 4 physical cpu AMD
Clock drift : Linux: .033% Windows: .019%

6. Relation to recent work in xen-unstable

There is a similarity between hvm_get_guest_time() in xen-unstable and read_64_main_counter()
in this code. However, read_64_main_counter() is more tuned to the needs of hpet.c. It has no
"set" operation, only the get. It isolates the mode, physical or simulated, in read_64_main_counter()
itself. It uses no vcpu or domain state as it is a physical entity, in either mode. And it provides a real
physical mode for every read for those applications that desire this.

7. Conclusion

The virtual hpet is improved by this patch in terms of accuracy and monotonicity.
Tests performed to date verify this and more testing is under way.

8. Future Work

Testing with Windows Vista will be performed soon. The reason for accuracy variations
on different platforms using the physical hpet device will be investigated. Additional overhead measurements on simulated vs physical hpet mode will be made.

Footnotes:

1. I don't recall the accuracy improvement with end of interrupt stamping, but it was
significant, perhaps better than two to one improvement. It would be a very simple matter
to re-measure the improvement as the facility can call back at injection time as well.

<mailto:[email protected]>

Quoted 8 lines

<mailto:[email protected]>

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

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

#2 lore raw

Keir,

I think the changes are required. We'll run some tests today today so that we have some data to talk about.

-Dave

-----Original Message-----
From: [email protected] on behalf of Keir Fraser
Sent: Fri 6/6/2008 4:58 AM
To: Ben Guthro; xen-devel
Cc: [email protected]
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Are these patches needed now the timers are built on Xen system time rather than host TSC? Dan has reported much better time-keeping with his patch checked in, and it¹s for sure a lot less invasive than this patchset.

-- Keir

Quoted 292 lines · Ben Guthro

1. Introduction

This patch improves the hpet based guest clock in terms of drift and monotonicity.
Prior to this work the drift with hpet was greater than 2%, far above the .05% limit
for ntp to synchronize. With this code, the drift ranges from .001% to .0033% depending
on guest and physical platform.

Using hpet allows guest operating systems to provide monotonic time to their applications. Time sources other than hpet are not monotonic because of their reliance on tsc, which is not synchronized across physical processors.

Windows 2k864 and many Linux guests are supported with two policies, one for guests
that handle missed clock interrupts and the other for guests that require the correct number of interrupts.

Guests may use hpet for the timing source even if the physical platform has no visible
hpet. Migration is supported between physical machines which differ in physical
hpet visibility.

Most of the changes are in hpet.c. Two general facilities are added to track interrupt
progress. The ideas here and the facilities would be useful in vpt.c, for other time
sources, though no attempt is made here to improve vpt.c.

The following sections discuss hpet dependencies, interrupt delivery policies, live migration,
test results, and relation to recent work with monotonic time.

2. Virtual Hpet dependencies

The virtual hpet depends on the ability to read the physical or simulated (see discussion below) hpet. For timekeeping, the virtual hpet also depends on two new interrupt notification facilities to implement its policies for interrupt delivery.

2.1. Two modes of low-level hpet main counter reads.

In this implementation, the virtual hpet reads with read_64_main_counter(), exported by
time.c, either the real physical hpet main counter register directly or a "simulated"
hpet main counter.

The simulated mode uses a monotonic version of get_s_time() (NOW()), where the last
time value is returned whenever the current time value is less than the last time
value. In simulated mode, since it is layered on s_time, the underlying hardware
can be hpet or some other device. The frequency of the main counter in simulated
mode is the same as the standard physical hpet frequency, allowing live migration
between nodes that are configured differently.

If the physical platform does not have an hpet device, or if xen is configured not
to use the device, then the simulated method is used. If there is a physical hpet device,
and xen has initialized it, then either simulated or physical mode can be used.
This is governed by a boot time option, hpet-avoid. Setting this option to 1 gives the
simulated mode and 0 the physical mode. The default is physical mode.

A disadvantage of the physical mode is that may take longer to read the device than in simulated mode. On some platforms the cost is about the same (less than 250 nsec) for
physical and simulated modes, while on others physical cost is much higher than simulated.
A disadvantage of the simulated mode is that it can return the same value for the counter in consecutive calls.

2.2. Interrupt notification facilities.

Two interrupt notification facilities are introduced, one is hvm_isa_irq_assert_cb()
and the other hvm_register_intr_en_notif().

The vhpet uses hvm_isa_irq_assert_cb to deliver interrupts to the vioapic. hvm_isa_irq_assert_cb allows a callback to be passed along to vioapic_deliver()
and this callback is called with a mask of the vcpus which will get the interrupt. This callback is made before any vcpus receive an interrupt.

Vhpet uses hvm_register_intr_en_notif() to register a handler for a particular vector that will be called when that vector is injected in [vmx,svm]_intr_assist()
and also when the guest finishes handling the interrupt. Here finished is defined
as the point when the guest re-enables interrupts or lowers the tpr value. EOI is not used as the end of interrupt as this is sometimes returned before the interrupt handler has done its work. A flag is passed to the handler indicating
whether this is the injection point (post = 1) or the interrupt finished (post = 0) point.
The need for the finished point callback is discussed in the missed ticks policy section.

To prevent a possible early trigger of the finished callback, intr_en_notif logic
has a two stage arm, the first at injection (hvm_intr_en_notif_arm()) and the second when
interrupts are seen to be disabled (hvm_intr_en_notif_disarm()). Once fully armed, re-enabling
interrupts will cause hvm_intr_en_notif_disarm() to make the end of interrupt callback. hvm_intr_en_notif_arm() and hvm_intr_en_notif_disarm() are called by [vmx,svm]_intr_assist().

3. Interrupt delivery policies

The existing hpet interrupt delivery is preserved. This includes vcpu round robin delivery used by Linux and broadcast delivery used by Windows.

There are two policies for interrupt delivery, one for Windows 2k8-64 and the other
for Linux. The Linux policy takes advantage of the (guest) Linux missed tick and offset
calculations and does not attempt to deliver the right number of interrupts. The Windows policy delivers the correct number of interrupts, even if sometimes much
closer to each other than the period. The policies are similar to those in vpt.c, though
there are some important differences.

Policies are selected with an HVMOP_set_param hypercall with index HVM_PARAM_TIMER_MODE.
Two new values are added, HVM_HPET_guest_computes_missed_ticks and HVM_HPET_guest_does_not_compute_missed_ticks. The reason that two new ones are added is that
in some guests (32bit Linux) a no-missed policy is needed for clock sources other than hpet
and a missed ticks policy for hpet. It was felt that there would be less confusion by simply
introducing the two hpet policies.

3.1. The missed ticks policy

The Linux clock interrupt handler for hpet calculates missed ticks and offset using the hpet
main counter. The algorithm works well when the time since the last interrupt is greater than
or equal to a period and poorly otherwise.

The missed ticks policy ensures that no two clock interrupts are delivered to the guest at
a time interval less than a period. A time stamp (hpet main counter value) is recorded (by a
callback registered with hvm_register_intr_en_notif) when Linux finishes handling the clock
interrupt. Then, ensuing interrupts are delivered to the vioapic only if the current main
counter value is a period greater than when the last interrupt was handled.

Tests showed a significant improvement in clock drift with end of interrupt time stamps
versus beginning of interrupt[1]. It is believed that the reason for the improvement
is that the clock interrupt handler goes for a spinlock and can be therefore delayed in its
processing. Furthermore, the main counter is read by the guest under the lock. The net
effect is that if we time stamp injection, we can get the difference in time between successive interrupt handler lock acquisitions to be less than the period.

3.2. The no-missed ticks policy

Windows 2k864 keeps very poor time with the missed ticks policy. So the no-missed ticks policy
was developed. In the no-missed ticks policy we deliver the correct number of interrupts,
even if they are spaced less than a period apart (when catching up).

Windows 2k864 uses a broadcast mode in the interrupt routing such that all vcpus get the clock interrupt. The best Windows drift performance was achieved when the
policy code ensured that all the previous interrupts (on the various vcpus) had been injected
before injecting the next interrupt to the vioapic..

The policy code works as follows. It uses the hvm_isa_irq_assert_cb() to record
the vcpus to be interrupted in h->hpet.pending_mask. Then, in the callback registered
with hvm_register_intr_en_notif() at post=1 time it clears the current vcpu in the pending_mask.
When the pending_mask is clear it decrements hpet.intr_pending_nr and if intr_pending_nr is still
non-zero posts another interrupt to the ioapic with hvm_isa_irq_assert_cb(). Intr_pending_nr is incremented in hpet_route_decision_not_missed_ticks().

The missed ticks policy intr_en_notif callback also uses the pending_mask method. So even though
Linux does not broadcast its interrupts, the code could handle it if it did. In this case the end of interrupt time stamp is made when the pending_mask is clear.

4. Live Migration

Live migration with hpet preserves the current offset of the guest clock with respect
to ntp. This is accomplished by migrating all of the state in the h->hpet data structure
in the usual way. The hp->mc_offset is recalculated on the receiving node so that the
guest sees a continuous hpet main counter.

Code as been added to xc_domain_save.c to send a small message after the domain context is sent. The contents of the message is the physical tsc timestamp, last_tsc,
read just before the message is sent. When the last_tsc message is received in xc_domain_restore.c,
another physical tsc timestamp, cur_tsc, is read. The two timestamps are loaded into the domain
structure as last_tsc_sender and first_tsc_receiver with hypercalls. Then xc_domain_hvm_setcontext
is called so that hpet_load has access to these time stamps. Hpet_load uses the timestamps
to account for the time spent saving and loading the domain context. With this technique,
the only neglected time is the time spent sending a small network message.

5. Test Results

Some recent test results are:

5.1 Linux 4u664 and Windows 2k864 load test.
Duration: 70 hours.
Test date: 6/2/08
Loads: usex -b48 on Linux; burn-in on Windows Guest vcpus: 8 for Linux; 2 for Windows
Hardware: 8 physical cpu AMD
Clock drift : Linux: .0012% Windows: .009%

5.2 Linux 4u664, Linux 4u464 , and Windows 2k864 no-load test
Duration: 23 hours.
Test date: 6/3/08
Loads: none
Guest vcpus: 8 for each Linux; 2 for Windows
Hardware: 4 physical cpu AMD
Clock drift : Linux: .033% Windows: .019%

6. Relation to recent work in xen-unstable

There is a similarity between hvm_get_guest_time() in xen-unstable and read_64_main_counter()
in this code. However, read_64_main_counter() is more tuned to the needs of hpet.c. It has no
"set" operation, only the get. It isolates the mode, physical or simulated, in read_64_main_counter()
itself. It uses no vcpu or domain state as it is a physical entity, in either mode. And it provides a real
physical mode for every read for those applications that desire this.

7. Conclusion

The virtual hpet is improved by this patch in terms of accuracy and monotonicity.
Tests performed to date verify this and more testing is under way.

8. Future Work

Testing with Windows Vista will be performed soon. The reason for accuracy variations
on different platforms using the physical hpet device will be investigated. Additional overhead measurements on simulated vs physical hpet mode will be made.

Footnotes:

1. I don't recall the accuracy improvement with end of interrupt stamping, but it was
significant, perhaps better than two to one improvement. It would be a very simple matter
to re-measure the improvement as the facility can call back at injection time as well.

<mailto:[email protected]>

Quoted 8 lines

<mailto:[email protected]>

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

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

#3 lore raw
In reply to Dave Winchell

RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracyHi Dave and Ben --

When running tests on xen-unstable (without your patch), please ensure that hpet=1 is set in the hvm config and also I think that when hpet is the clocksource on RHEL4-32, the clock IS resilient to missed ticks so timer_mode should be 2 (vs when pit is the clocksource on RHEL4-32, all clock ticks must be delivered and so timer_mode should be 0).

Per http://lists.xensource.com/archives/html/xen-devel/2008-06/msg00098.html it's my intent to clean this up, but I won't get to it until next week.

Thanks,
Dan
-----Original Message-----
From: [email protected] [mailto:[email protected]]On Behalf Of Dave Winchell
Sent: Friday, June 06, 2008 4:46 AM
To: Keir Fraser; Ben Guthro; xen-devel
Cc: [email protected]; Dave Winchell
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Keir,

I think the changes are required. We'll run some tests today today so that we have some data to talk about.

-Dave

-----Original Message-----
From: [email protected] on behalf of Keir Fraser
Sent: Fri 6/6/2008 4:58 AM
To: Ben Guthro; xen-devel
Cc: [email protected]
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Are these patches needed now the timers are built on Xen system time rather than host TSC? Dan has reported much better time-keeping with his patch checked in, and it¹s for sure a lot less invasive than this patchset.

-- Keir

Quoted 292 lines · Ben Guthro

1. Introduction

This patch improves the hpet based guest clock in terms of drift and monotonicity.
Prior to this work the drift with hpet was greater than 2%, far above the .05% limit
for ntp to synchronize. With this code, the drift ranges from .001% to .0033% depending
on guest and physical platform.

Using hpet allows guest operating systems to provide monotonic time to their applications. Time sources other than hpet are not monotonic because of their reliance on tsc, which is not synchronized across physical processors.

Windows 2k864 and many Linux guests are supported with two policies, one for guests
that handle missed clock interrupts and the other for guests that require the correct number of interrupts.

Guests may use hpet for the timing source even if the physical platform has no visible
hpet. Migration is supported between physical machines which differ in physical
hpet visibility.

Most of the changes are in hpet.c. Two general facilities are added to track interrupt
progress. The ideas here and the facilities would be useful in vpt.c, for other time
sources, though no attempt is made here to improve vpt.c.

The following sections discuss hpet dependencies, interrupt delivery policies, live migration,
test results, and relation to recent work with monotonic time.

2. Virtual Hpet dependencies

The virtual hpet depends on the ability to read the physical or simulated (see discussion below) hpet. For timekeeping, the virtual hpet also depends on two new interrupt notification facilities to implement its policies for interrupt delivery.

2.1. Two modes of low-level hpet main counter reads.

In this implementation, the virtual hpet reads with read_64_main_counter(), exported by
time.c, either the real physical hpet main counter register directly or a "simulated"
hpet main counter.

The simulated mode uses a monotonic version of get_s_time() (NOW()), where the last
time value is returned whenever the current time value is less than the last time
value. In simulated mode, since it is layered on s_time, the underlying hardware
can be hpet or some other device. The frequency of the main counter in simulated
mode is the same as the standard physical hpet frequency, allowing live migration
between nodes that are configured differently.

If the physical platform does not have an hpet device, or if xen is configured not
to use the device, then the simulated method is used. If there is a physical hpet device,
and xen has initialized it, then either simulated or physical mode can be used.
This is governed by a boot time option, hpet-avoid. Setting this option to 1 gives the
simulated mode and 0 the physical mode. The default is physical mode.

A disadvantage of the physical mode is that may take longer to read the device than in simulated mode. On some platforms the cost is about the same (less than 250 nsec) for
physical and simulated modes, while on others physical cost is much higher than simulated.
A disadvantage of the simulated mode is that it can return the same value for the counter in consecutive calls.

2.2. Interrupt notification facilities.

Two interrupt notification facilities are introduced, one is hvm_isa_irq_assert_cb()
and the other hvm_register_intr_en_notif().

The vhpet uses hvm_isa_irq_assert_cb to deliver interrupts to the vioapic. hvm_isa_irq_assert_cb allows a callback to be passed along to vioapic_deliver()
and this callback is called with a mask of the vcpus which will get the interrupt. This callback is made before any vcpus receive an interrupt.

Vhpet uses hvm_register_intr_en_notif() to register a handler for a particular vector that will be called when that vector is injected in [vmx,svm]_intr_assist()
and also when the guest finishes handling the interrupt. Here finished is defined
as the point when the guest re-enables interrupts or lowers the tpr value. EOI is not used as the end of interrupt as this is sometimes returned before the interrupt handler has done its work. A flag is passed to the handler indicating
whether this is the injection point (post = 1) or the interrupt finished (post = 0) point.
The need for the finished point callback is discussed in the missed ticks policy section.

To prevent a possible early trigger of the finished callback, intr_en_notif logic
has a two stage arm, the first at injection (hvm_intr_en_notif_arm()) and the second when
interrupts are seen to be disabled (hvm_intr_en_notif_disarm()). Once fully armed, re-enabling
interrupts will cause hvm_intr_en_notif_disarm() to make the end of interrupt callback. hvm_intr_en_notif_arm() and hvm_intr_en_notif_disarm() are called by [vmx,svm]_intr_assist().

3. Interrupt delivery policies

The existing hpet interrupt delivery is preserved. This includes vcpu round robin delivery used by Linux and broadcast delivery used by Windows.

There are two policies for interrupt delivery, one for Windows 2k8-64 and the other
for Linux. The Linux policy takes advantage of the (guest) Linux missed tick and offset
calculations and does not attempt to deliver the right number of interrupts. The Windows policy delivers the correct number of interrupts, even if sometimes much
closer to each other than the period. The policies are similar to those in vpt.c, though
there are some important differences.

Policies are selected with an HVMOP_set_param hypercall with index HVM_PARAM_TIMER_MODE.
Two new values are added, HVM_HPET_guest_computes_missed_ticks and HVM_HPET_guest_does_not_compute_missed_ticks. The reason that two new ones are added is that
in some guests (32bit Linux) a no-missed policy is needed for clock sources other than hpet
and a missed ticks policy for hpet. It was felt that there would be less confusion by simply
introducing the two hpet policies.

3.1. The missed ticks policy

The Linux clock interrupt handler for hpet calculates missed ticks and offset using the hpet
main counter. The algorithm works well when the time since the last interrupt is greater than
or equal to a period and poorly otherwise.

The missed ticks policy ensures that no two clock interrupts are delivered to the guest at
a time interval less than a period. A time stamp (hpet main counter value) is recorded (by a
callback registered with hvm_register_intr_en_notif) when Linux finishes handling the clock
interrupt. Then, ensuing interrupts are delivered to the vioapic only if the current main
counter value is a period greater than when the last interrupt was handled.

Tests showed a significant improvement in clock drift with end of interrupt time stamps
versus beginning of interrupt[1]. It is believed that the reason for the improvement
is that the clock interrupt handler goes for a spinlock and can be therefore delayed in its
processing. Furthermore, the main counter is read by the guest under the lock. The net
effect is that if we time stamp injection, we can get the difference in time between successive interrupt handler lock acquisitions to be less than the period.

3.2. The no-missed ticks policy

Windows 2k864 keeps very poor time with the missed ticks policy. So the no-missed ticks policy
was developed. In the no-missed ticks policy we deliver the correct number of interrupts,
even if they are spaced less than a period apart (when catching up).

Windows 2k864 uses a broadcast mode in the interrupt routing such that all vcpus get the clock interrupt. The best Windows drift performance was achieved when the
policy code ensured that all the previous interrupts (on the various vcpus) had been injected
before injecting the next interrupt to the vioapic..

The policy code works as follows. It uses the hvm_isa_irq_assert_cb() to record
the vcpus to be interrupted in h->hpet.pending_mask. Then, in the callback registered
with hvm_register_intr_en_notif() at post=1 time it clears the current vcpu in the pending_mask.
When the pending_mask is clear it decrements hpet.intr_pending_nr and if intr_pending_nr is still
non-zero posts another interrupt to the ioapic with hvm_isa_irq_assert_cb(). Intr_pending_nr is incremented in hpet_route_decision_not_missed_ticks().

The missed ticks policy intr_en_notif callback also uses the pending_mask method. So even though
Linux does not broadcast its interrupts, the code could handle it if it did. In this case the end of interrupt time stamp is made when the pending_mask is clear.

4. Live Migration

Live migration with hpet preserves the current offset of the guest clock with respect
to ntp. This is accomplished by migrating all of the state in the h->hpet data structure
in the usual way. The hp->mc_offset is recalculated on the receiving node so that the
guest sees a continuous hpet main counter.

Code as been added to xc_domain_save.c to send a small message after the domain context is sent. The contents of the message is the physical tsc timestamp, last_tsc,
read just before the message is sent. When the last_tsc message is received in xc_domain_restore.c,
another physical tsc timestamp, cur_tsc, is read. The two timestamps are loaded into the domain
structure as last_tsc_sender and first_tsc_receiver with hypercalls. Then xc_domain_hvm_setcontext
is called so that hpet_load has access to these time stamps. Hpet_load uses the timestamps
to account for the time spent saving and loading the domain context. With this technique,
the only neglected time is the time spent sending a small network message.

5. Test Results

Some recent test results are:

5.1 Linux 4u664 and Windows 2k864 load test.
Duration: 70 hours.
Test date: 6/2/08
Loads: usex -b48 on Linux; burn-in on Windows Guest vcpus: 8 for Linux; 2 for Windows
Hardware: 8 physical cpu AMD
Clock drift : Linux: .0012% Windows: .009%

5.2 Linux 4u664, Linux 4u464 , and Windows 2k864 no-load test
Duration: 23 hours.
Test date: 6/3/08
Loads: none
Guest vcpus: 8 for each Linux; 2 for Windows
Hardware: 4 physical cpu AMD
Clock drift : Linux: .033% Windows: .019%

6. Relation to recent work in xen-unstable

There is a similarity between hvm_get_guest_time() in xen-unstable and read_64_main_counter()
in this code. However, read_64_main_counter() is more tuned to the needs of hpet.c. It has no
"set" operation, only the get. It isolates the mode, physical or simulated, in read_64_main_counter()
itself. It uses no vcpu or domain state as it is a physical entity, in either mode. And it provides a real
physical mode for every read for those applications that desire this.

7. Conclusion

The virtual hpet is improved by this patch in terms of accuracy and monotonicity.
Tests performed to date verify this and more testing is under way.

8. Future Work

Testing with Windows Vista will be performed soon. The reason for accuracy variations
on different platforms using the physical hpet device will be investigated. Additional overhead measurements on simulated vs physical hpet mode will be made.

Footnotes:

1. I don't recall the accuracy improvement with end of interrupt stamping, but it was
significant, perhaps better than two to one improvement. It would be a very simple matter
to re-measure the improvement as the facility can call back at injection time as well.

<mailto:[email protected]>

Quoted 8 lines

<mailto:[email protected]>

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

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

#4 lore raw
In reply to Dan Magenheimer

Hi Dan,

I am running with hpet=1 and timer_mode=2. I don't see where timer_mode is checked for hpet timekeeping but I set it nevertheless.

thanks,
Dave

Quoted 449 lines · Dan Magenheimer

Hi Dave and Ben --

When running tests on xen-unstable (without your patch), please ensure that hpet=1 is set in the hvm config and also I think that when hpet is the clocksource on RHEL4-32, the clock IS resilient to missed ticks so timer_mode should be 2 (vs when pit is the clocksource on RHEL4-32, all clock ticks must be delivered and so timer_mode should be 0).

Per http://lists.xensource.com/archives/html/xen-devel/2008-06/msg00098.html it's my intent to clean this up, but I won't get to it until next week.

Thanks,
Dan

-----Original Message-----
*From:* [email protected] [mailto:[email protected]]*On Behalf Of *Dave Winchell
*Sent:* Friday, June 06, 2008 4:46 AM
*To:* Keir Fraser; Ben Guthro; xen-devel
*Cc:* [email protected]; Dave Winchell *Subject:* RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Keir,

I think the changes are required. We'll run some tests today today so that we have some data to talk about.

-Dave

-----Original Message-----
From: [email protected] on behalf of Keir Fraser
Sent: Fri 6/6/2008 4:58 AM
To: Ben Guthro; xen-devel
Cc: [email protected]
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Are these patches needed now the timers are built on Xen system time rather
than host TSC? Dan has reported much better time-keeping with his patch
checked in, and it¹s for sure a lot less invasive than this patchset.

-- Keir

On 5/6/08 15:59, "Ben Guthro" <[email protected]> wrote:

>
> 1. Introduction
>
> This patch improves the hpet based guest clock in terms of drift and > monotonicity.
> Prior to this work the drift with hpet was greater than 2%, far above the .05%
> limit
> for ntp to synchronize. With this code, the drift ranges from .001% to .0033%
> depending
> on guest and physical platform.
>
> Using hpet allows guest operating systems to provide monotonic time to their
> applications. Time sources other than hpet are not monotonic because > of their reliance on tsc, which is not synchronized across physical > processors.
>
> Windows 2k864 and many Linux guests are supported with two policies, one for
> guests
> that handle missed clock interrupts and the other for guests that require the
> correct number of interrupts.
>
> Guests may use hpet for the timing source even if the physical platform has no
> visible
> hpet. Migration is supported between physical machines which differ in
> physical
> hpet visibility.
>
> Most of the changes are in hpet.c. Two general facilities are added to track
> interrupt
> progress. The ideas here and the facilities would be useful in vpt.c, for
> other time
> sources, though no attempt is made here to improve vpt.c. >
> The following sections discuss hpet dependencies, interrupt delivery policies,
> live migration,
> test results, and relation to recent work with monotonic time. >
>
> 2. Virtual Hpet dependencies
>
> The virtual hpet depends on the ability to read the physical or simulated
> (see discussion below) hpet. For timekeeping, the virtual hpet also depends
> on two new interrupt notification facilities to implement its policies for
> interrupt delivery.
>
> 2.1. Two modes of low-level hpet main counter reads.
>
> In this implementation, the virtual hpet reads with read_64_main_counter(),
> exported by
> time.c, either the real physical hpet main counter register directly or a
> "simulated"
> hpet main counter.
>
> The simulated mode uses a monotonic version of get_s_time() (NOW()), where the
> last
> time value is returned whenever the current time value is less than the last
> time
> value. In simulated mode, since it is layered on s_time, the underlying
> hardware
> can be hpet or some other device. The frequency of the main counter in
> simulated
> mode is the same as the standard physical hpet frequency, allowing live
> migration
> between nodes that are configured differently.
>
> If the physical platform does not have an hpet device, or if xen is configured
> not
> to use the device, then the simulated method is used. If there is a physical
> hpet device,
> and xen has initialized it, then either simulated or physical mode can be
> used.
> This is governed by a boot time option, hpet-avoid. Setting this option to 1
> gives the
> simulated mode and 0 the physical mode. The default is physical mode.
>
> A disadvantage of the physical mode is that may take longer to read the device
> than in simulated mode. On some platforms the cost is about the same (less
> than 250 nsec) for
> physical and simulated modes, while on others physical cost is much higher
> than simulated.
> A disadvantage of the simulated mode is that it can return the same value
> for the counter in consecutive calls.
>
> 2.2. Interrupt notification facilities.
>
> Two interrupt notification facilities are introduced, one is > hvm_isa_irq_assert_cb()
> and the other hvm_register_intr_en_notif().
>
> The vhpet uses hvm_isa_irq_assert_cb to deliver interrupts to the vioapic.
> hvm_isa_irq_assert_cb allows a callback to be passed along to > vioapic_deliver()
> and this callback is called with a mask of the vcpus which will get the
> interrupt. This callback is made before any vcpus receive an interrupt.
>
> Vhpet uses hvm_register_intr_en_notif() to register a handler for a particular
> vector that will be called when that vector is injected in > [vmx,svm]_intr_assist()
> and also when the guest finishes handling the interrupt. Here finished is
> defined
> as the point when the guest re-enables interrupts or lowers the tpr value.
> EOI is not used as the end of interrupt as this is sometimes returned before
> the interrupt handler has done its work. A flag is passed to the handler
> indicating
> whether this is the injection point (post = 1) or the interrupt finished (post
> = 0) point.
> The need for the finished point callback is discussed in the missed ticks
> policy section.
>
> To prevent a possible early trigger of the finished callback, intr_en_notif
> logic
> has a two stage arm, the first at injection (hvm_intr_en_notif_arm()) and the
> second when
> interrupts are seen to be disabled (hvm_intr_en_notif_disarm()). Once fully
> armed, re-enabling
> interrupts will cause hvm_intr_en_notif_disarm() to make the end of interrupt
> callback. hvm_intr_en_notif_arm() and hvm_intr_en_notif_disarm() are called by
> [vmx,svm]_intr_assist().
>
> 3. Interrupt delivery policies
>
> The existing hpet interrupt delivery is preserved. This includes > vcpu round robin delivery used by Linux and broadcast delivery used by
> Windows.
>
> There are two policies for interrupt delivery, one for Windows 2k8-64 and the
> other
> for Linux. The Linux policy takes advantage of the (guest) Linux missed tick
> and offset
> calculations and does not attempt to deliver the right number of interrupts.
> The Windows policy delivers the correct number of interrupts, even if
> sometimes much
> closer to each other than the period. The policies are similar to those in
> vpt.c, though
> there are some important differences.
>
> Policies are selected with an HVMOP_set_param hypercall with index > HVM_PARAM_TIMER_MODE.
> Two new values are added, HVM_HPET_guest_computes_missed_ticks and > HVM_HPET_guest_does_not_compute_missed_ticks. The reason that two new ones
> are added is that
> in some guests (32bit Linux) a no-missed policy is needed for clock sources
> other than hpet
> and a missed ticks policy for hpet. It was felt that there would be less
> confusion by simply
> introducing the two hpet policies.
>
> 3.1. The missed ticks policy
>
> The Linux clock interrupt handler for hpet calculates missed ticks and offset
> using the hpet
> main counter. The algorithm works well when the time since the last interrupt
> is greater than
> or equal to a period and poorly otherwise.
>
> The missed ticks policy ensures that no two clock interrupts are delivered to
> the guest at
> a time interval less than a period. A time stamp (hpet main counter value) is
> recorded (by a
> callback registered with hvm_register_intr_en_notif) when Linux finishes
> handling the clock
> interrupt. Then, ensuing interrupts are delivered to the vioapic only if the
> current main
> counter value is a period greater than when the last interrupt was handled.
>
> Tests showed a significant improvement in clock drift with end of interrupt
> time stamps
> versus beginning of interrupt[1]. It is believed that the reason for the
> improvement
> is that the clock interrupt handler goes for a spinlock and can be therefore
> delayed in its
> processing. Furthermore, the main counter is read by the guest under the lock.
> The net
> effect is that if we time stamp injection, we can get the difference in time
> between successive interrupt handler lock acquisitions to be less than the
> period.
>
> 3.2. The no-missed ticks policy
>
> Windows 2k864 keeps very poor time with the missed ticks policy. So the
> no-missed ticks policy
> was developed. In the no-missed ticks policy we deliver the correct number of
> interrupts,
> even if they are spaced less than a period apart (when catching up). >
> Windows 2k864 uses a broadcast mode in the interrupt routing such that
> all vcpus get the clock interrupt. The best Windows drift performance was
> achieved when the
> policy code ensured that all the previous interrupts (on the various vcpus)
> had been injected
> before injecting the next interrupt to the vioapic..
>
> The policy code works as follows. It uses the hvm_isa_irq_assert_cb() to
> record
> the vcpus to be interrupted in h->hpet.pending_mask. Then, in the callback
> registered
> with hvm_register_intr_en_notif() at post=1 time it clears the current vcpu in
> the pending_mask.
> When the pending_mask is clear it decrements hpet.intr_pending_nr and if
> intr_pending_nr is still
> non-zero posts another interrupt to the ioapic with hvm_isa_irq_assert_cb().
> Intr_pending_nr is incremented in hpet_route_decision_not_missed_ticks().
>
> The missed ticks policy intr_en_notif callback also uses the pending_mask
> method. So even though
> Linux does not broadcast its interrupts, the code could handle it if it did.
> In this case the end of interrupt time stamp is made when the pending_mask is
> clear.
>
> 4. Live Migration
>
> Live migration with hpet preserves the current offset of the guest clock with
> respect
> to ntp. This is accomplished by migrating all of the state in the h->hpet data
> structure
> in the usual way. The hp->mc_offset is recalculated on the receiving node so
> that the
> guest sees a continuous hpet main counter.
>
> Code as been added to xc_domain_save.c to send a small message after the
> domain context is sent. The contents of the message is the physical tsc
> timestamp, last_tsc,
> read just before the message is sent. When the last_tsc message is received in
> xc_domain_restore.c,
> another physical tsc timestamp, cur_tsc, is read. The two timestamps are
> loaded into the domain
> structure as last_tsc_sender and first_tsc_receiver with hypercalls. Then
> xc_domain_hvm_setcontext
> is called so that hpet_load has access to these time stamps. Hpet_load uses
> the timestamps
> to account for the time spent saving and loading the domain context. With this
> technique,
> the only neglected time is the time spent sending a small network message.
>
> 5. Test Results
>
> Some recent test results are:
>
> 5.1 Linux 4u664 and Windows 2k864 load test.
> Duration: 70 hours.
> Test date: 6/2/08
> Loads: usex -b48 on Linux; burn-in on Windows
> Guest vcpus: 8 for Linux; 2 for Windows
> Hardware: 8 physical cpu AMD
> Clock drift : Linux: .0012% Windows: .009%
>
> 5.2 Linux 4u664, Linux 4u464 , and Windows 2k864 no-load test
> Duration: 23 hours.
> Test date: 6/3/08
> Loads: none
> Guest vcpus: 8 for each Linux; 2 for Windows
> Hardware: 4 physical cpu AMD
> Clock drift : Linux: .033% Windows: .019%
>
> 6. Relation to recent work in xen-unstable
>
> There is a similarity between hvm_get_guest_time() in xen-unstable and
> read_64_main_counter()
> in this code. However, read_64_main_counter() is more tuned to the needs of
> hpet.c. It has no
> "set" operation, only the get. It isolates the mode, physical or simulated, in
> read_64_main_counter()
> itself. It uses no vcpu or domain state as it is a physical entity, in either
> mode. And it provides a real
> physical mode for every read for those applications that desire this.
>
> 7. Conclusion
>
> The virtual hpet is improved by this patch in terms of accuracy and > monotonicity.
> Tests performed to date verify this and more testing is under way. >
> 8. Future Work
>
> Testing with Windows Vista will be performed soon. The reason for accuracy
> variations
> on different platforms using the physical hpet device will be investigated.
> Additional overhead measurements on simulated vs physical hpet mode will be
> made.
>
> Footnotes:
>
> 1. I don't recall the accuracy improvement with end of interrupt stamping, but
> it was
> significant, perhaps better than two to one improvement. It would be a very
> simple matter
> to re-measure the improvement as the facility can call back at injection time
> as well.
>
>
> Signed-off-by: Dave Winchell <[email protected]> > <mailto:[email protected]>
> Signed-off-by: Ben Guthro <[email protected]>
> <mailto:[email protected]>
>
>
> _______________________________________________
> Xen-devel mailing list
> [email protected]
> http://lists.xensource.com/xen-devel

#5 lore raw
In reply to Dan Magenheimer

Dan, Keir:

Preliminary tests results indicate an error of .1% for Linux 64 bit guests configured for hpet with xen-unstable as is. As we have discussed many times, the ntp requirement is .05%. Tests on the patch we just submitted for hpet have indicated errors of .0012%
on this platform under similar test conditions and .03% on other platforms.

Windows vista64 has an error of 11% using hpet with the xen-unstable bits. In an overnight test with our hpet patch, the Windows vista error was .008%.

The tests are with two or three guests on a physical node, all under load, and with the ratio of vcpus to phys cpus > 1.

I will continue to run tests over the next few days.

thanks,
Dave

Quoted 449 lines · Dan Magenheimer

Hi Dave and Ben --

When running tests on xen-unstable (without your patch), please ensure that hpet=1 is set in the hvm config and also I think that when hpet is the clocksource on RHEL4-32, the clock IS resilient to missed ticks so timer_mode should be 2 (vs when pit is the clocksource on RHEL4-32, all clock ticks must be delivered and so timer_mode should be 0).

Per http://lists.xensource.com/archives/html/xen-devel/2008-06/msg00098.html it's my intent to clean this up, but I won't get to it until next week.

Thanks,
Dan

-----Original Message-----
*From:* [email protected] [mailto:[email protected]]*On Behalf Of *Dave Winchell
*Sent:* Friday, June 06, 2008 4:46 AM
*To:* Keir Fraser; Ben Guthro; xen-devel
*Cc:* [email protected]; Dave Winchell *Subject:* RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Keir,

I think the changes are required. We'll run some tests today today so that we have some data to talk about.

-Dave

-----Original Message-----
From: [email protected] on behalf of Keir Fraser
Sent: Fri 6/6/2008 4:58 AM
To: Ben Guthro; xen-devel
Cc: [email protected]
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Are these patches needed now the timers are built on Xen system time rather
than host TSC? Dan has reported much better time-keeping with his patch
checked in, and it¹s for sure a lot less invasive than this patchset.

-- Keir

On 5/6/08 15:59, "Ben Guthro" <[email protected]> wrote:

>
> 1. Introduction
>
> This patch improves the hpet based guest clock in terms of drift and > monotonicity.
> Prior to this work the drift with hpet was greater than 2%, far above the .05%
> limit
> for ntp to synchronize. With this code, the drift ranges from .001% to .0033%
> depending
> on guest and physical platform.
>
> Using hpet allows guest operating systems to provide monotonic time to their
> applications. Time sources other than hpet are not monotonic because > of their reliance on tsc, which is not synchronized across physical > processors.
>
> Windows 2k864 and many Linux guests are supported with two policies, one for
> guests
> that handle missed clock interrupts and the other for guests that require the
> correct number of interrupts.
>
> Guests may use hpet for the timing source even if the physical platform has no
> visible
> hpet. Migration is supported between physical machines which differ in
> physical
> hpet visibility.
>
> Most of the changes are in hpet.c. Two general facilities are added to track
> interrupt
> progress. The ideas here and the facilities would be useful in vpt.c, for
> other time
> sources, though no attempt is made here to improve vpt.c. >
> The following sections discuss hpet dependencies, interrupt delivery policies,
> live migration,
> test results, and relation to recent work with monotonic time. >
>
> 2. Virtual Hpet dependencies
>
> The virtual hpet depends on the ability to read the physical or simulated
> (see discussion below) hpet. For timekeeping, the virtual hpet also depends
> on two new interrupt notification facilities to implement its policies for
> interrupt delivery.
>
> 2.1. Two modes of low-level hpet main counter reads.
>
> In this implementation, the virtual hpet reads with read_64_main_counter(),
> exported by
> time.c, either the real physical hpet main counter register directly or a
> "simulated"
> hpet main counter.
>
> The simulated mode uses a monotonic version of get_s_time() (NOW()), where the
> last
> time value is returned whenever the current time value is less than the last
> time
> value. In simulated mode, since it is layered on s_time, the underlying
> hardware
> can be hpet or some other device. The frequency of the main counter in
> simulated
> mode is the same as the standard physical hpet frequency, allowing live
> migration
> between nodes that are configured differently.
>
> If the physical platform does not have an hpet device, or if xen is configured
> not
> to use the device, then the simulated method is used. If there is a physical
> hpet device,
> and xen has initialized it, then either simulated or physical mode can be
> used.
> This is governed by a boot time option, hpet-avoid. Setting this option to 1
> gives the
> simulated mode and 0 the physical mode. The default is physical mode.
>
> A disadvantage of the physical mode is that may take longer to read the device
> than in simulated mode. On some platforms the cost is about the same (less
> than 250 nsec) for
> physical and simulated modes, while on others physical cost is much higher
> than simulated.
> A disadvantage of the simulated mode is that it can return the same value
> for the counter in consecutive calls.
>
> 2.2. Interrupt notification facilities.
>
> Two interrupt notification facilities are introduced, one is > hvm_isa_irq_assert_cb()
> and the other hvm_register_intr_en_notif().
>
> The vhpet uses hvm_isa_irq_assert_cb to deliver interrupts to the vioapic.
> hvm_isa_irq_assert_cb allows a callback to be passed along to > vioapic_deliver()
> and this callback is called with a mask of the vcpus which will get the
> interrupt. This callback is made before any vcpus receive an interrupt.
>
> Vhpet uses hvm_register_intr_en_notif() to register a handler for a particular
> vector that will be called when that vector is injected in > [vmx,svm]_intr_assist()
> and also when the guest finishes handling the interrupt. Here finished is
> defined
> as the point when the guest re-enables interrupts or lowers the tpr value.
> EOI is not used as the end of interrupt as this is sometimes returned before
> the interrupt handler has done its work. A flag is passed to the handler
> indicating
> whether this is the injection point (post = 1) or the interrupt finished (post
> = 0) point.
> The need for the finished point callback is discussed in the missed ticks
> policy section.
>
> To prevent a possible early trigger of the finished callback, intr_en_notif
> logic
> has a two stage arm, the first at injection (hvm_intr_en_notif_arm()) and the
> second when
> interrupts are seen to be disabled (hvm_intr_en_notif_disarm()). Once fully
> armed, re-enabling
> interrupts will cause hvm_intr_en_notif_disarm() to make the end of interrupt
> callback. hvm_intr_en_notif_arm() and hvm_intr_en_notif_disarm() are called by
> [vmx,svm]_intr_assist().
>
> 3. Interrupt delivery policies
>
> The existing hpet interrupt delivery is preserved. This includes > vcpu round robin delivery used by Linux and broadcast delivery used by
> Windows.
>
> There are two policies for interrupt delivery, one for Windows 2k8-64 and the
> other
> for Linux. The Linux policy takes advantage of the (guest) Linux missed tick
> and offset
> calculations and does not attempt to deliver the right number of interrupts.
> The Windows policy delivers the correct number of interrupts, even if
> sometimes much
> closer to each other than the period. The policies are similar to those in
> vpt.c, though
> there are some important differences.
>
> Policies are selected with an HVMOP_set_param hypercall with index > HVM_PARAM_TIMER_MODE.
> Two new values are added, HVM_HPET_guest_computes_missed_ticks and > HVM_HPET_guest_does_not_compute_missed_ticks. The reason that two new ones
> are added is that
> in some guests (32bit Linux) a no-missed policy is needed for clock sources
> other than hpet
> and a missed ticks policy for hpet. It was felt that there would be less
> confusion by simply
> introducing the two hpet policies.
>
> 3.1. The missed ticks policy
>
> The Linux clock interrupt handler for hpet calculates missed ticks and offset
> using the hpet
> main counter. The algorithm works well when the time since the last interrupt
> is greater than
> or equal to a period and poorly otherwise.
>
> The missed ticks policy ensures that no two clock interrupts are delivered to
> the guest at
> a time interval less than a period. A time stamp (hpet main counter value) is
> recorded (by a
> callback registered with hvm_register_intr_en_notif) when Linux finishes
> handling the clock
> interrupt. Then, ensuing interrupts are delivered to the vioapic only if the
> current main
> counter value is a period greater than when the last interrupt was handled.
>
> Tests showed a significant improvement in clock drift with end of interrupt
> time stamps
> versus beginning of interrupt[1]. It is believed that the reason for the
> improvement
> is that the clock interrupt handler goes for a spinlock and can be therefore
> delayed in its
> processing. Furthermore, the main counter is read by the guest under the lock.
> The net
> effect is that if we time stamp injection, we can get the difference in time
> between successive interrupt handler lock acquisitions to be less than the
> period.
>
> 3.2. The no-missed ticks policy
>
> Windows 2k864 keeps very poor time with the missed ticks policy. So the
> no-missed ticks policy
> was developed. In the no-missed ticks policy we deliver the correct number of
> interrupts,
> even if they are spaced less than a period apart (when catching up). >
> Windows 2k864 uses a broadcast mode in the interrupt routing such that
> all vcpus get the clock interrupt. The best Windows drift performance was
> achieved when the
> policy code ensured that all the previous interrupts (on the various vcpus)
> had been injected
> before injecting the next interrupt to the vioapic..
>
> The policy code works as follows. It uses the hvm_isa_irq_assert_cb() to
> record
> the vcpus to be interrupted in h->hpet.pending_mask. Then, in the callback
> registered
> with hvm_register_intr_en_notif() at post=1 time it clears the current vcpu in
> the pending_mask.
> When the pending_mask is clear it decrements hpet.intr_pending_nr and if
> intr_pending_nr is still
> non-zero posts another interrupt to the ioapic with hvm_isa_irq_assert_cb().
> Intr_pending_nr is incremented in hpet_route_decision_not_missed_ticks().
>
> The missed ticks policy intr_en_notif callback also uses the pending_mask
> method. So even though
> Linux does not broadcast its interrupts, the code could handle it if it did.
> In this case the end of interrupt time stamp is made when the pending_mask is
> clear.
>
> 4. Live Migration
>
> Live migration with hpet preserves the current offset of the guest clock with
> respect
> to ntp. This is accomplished by migrating all of the state in the h->hpet data
> structure
> in the usual way. The hp->mc_offset is recalculated on the receiving node so
> that the
> guest sees a continuous hpet main counter.
>
> Code as been added to xc_domain_save.c to send a small message after the
> domain context is sent. The contents of the message is the physical tsc
> timestamp, last_tsc,
> read just before the message is sent. When the last_tsc message is received in
> xc_domain_restore.c,
> another physical tsc timestamp, cur_tsc, is read. The two timestamps are
> loaded into the domain
> structure as last_tsc_sender and first_tsc_receiver with hypercalls. Then
> xc_domain_hvm_setcontext
> is called so that hpet_load has access to these time stamps. Hpet_load uses
> the timestamps
> to account for the time spent saving and loading the domain context. With this
> technique,
> the only neglected time is the time spent sending a small network message.
>
> 5. Test Results
>
> Some recent test results are:
>
> 5.1 Linux 4u664 and Windows 2k864 load test.
> Duration: 70 hours.
> Test date: 6/2/08
> Loads: usex -b48 on Linux; burn-in on Windows
> Guest vcpus: 8 for Linux; 2 for Windows
> Hardware: 8 physical cpu AMD
> Clock drift : Linux: .0012% Windows: .009%
>
> 5.2 Linux 4u664, Linux 4u464 , and Windows 2k864 no-load test
> Duration: 23 hours.
> Test date: 6/3/08
> Loads: none
> Guest vcpus: 8 for each Linux; 2 for Windows
> Hardware: 4 physical cpu AMD
> Clock drift : Linux: .033% Windows: .019%
>
> 6. Relation to recent work in xen-unstable
>
> There is a similarity between hvm_get_guest_time() in xen-unstable and
> read_64_main_counter()
> in this code. However, read_64_main_counter() is more tuned to the needs of
> hpet.c. It has no
> "set" operation, only the get. It isolates the mode, physical or simulated, in
> read_64_main_counter()
> itself. It uses no vcpu or domain state as it is a physical entity, in either
> mode. And it provides a real
> physical mode for every read for those applications that desire this.
>
> 7. Conclusion
>
> The virtual hpet is improved by this patch in terms of accuracy and > monotonicity.
> Tests performed to date verify this and more testing is under way. >
> 8. Future Work
>
> Testing with Windows Vista will be performed soon. The reason for accuracy
> variations
> on different platforms using the physical hpet device will be investigated.
> Additional overhead measurements on simulated vs physical hpet mode will be
> made.
>
> Footnotes:
>
> 1. I don't recall the accuracy improvement with end of interrupt stamping, but
> it was
> significant, perhaps better than two to one improvement. It would be a very
> simple matter
> to re-measure the improvement as the facility can call back at injection time
> as well.
>
>
> Signed-off-by: Dave Winchell <[email protected]> > <mailto:[email protected]>
> Signed-off-by: Ben Guthro <[email protected]>
> <mailto:[email protected]>
>
>
> _______________________________________________
> Xen-devel mailing list
> [email protected]
> http://lists.xensource.com/xen-devel

#6 lore raw

Dave --

Thanks much for posting the preliminary results!

While I am fully supportive of offering hardware hpet as an option for hvm guests (let's call it hwhpet=1 for shorthand), I am very surprised by your preliminary results; the most obvious conclusion is that Xen system time is losing time at the rate of 1000 PPM though its possible there's a bug somewhere else in the "time stack". Your Windows result is jaw-dropping and inexplicable, though I have to admit ignorance of how Windows manages time.

I think with my recent patch and hpet=1 (essentially the same as your emulated hpet), hvm guest time should track Xen system time. I wonder if domain0 (which if I understand correctly is directly using Xen system time) is also seeing an error of .1%? Also I wonder for the skew you are seeing (in both hvm guests and domain0) is time moving too fast or two slow?

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0. And if for some reason Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

One more thought... do you know the accuracy of the TSC crystals on your test systems? I posted a patch awhile ago that was intended to test that, though I guess it was only testing skew of different TSCs on the same system, not TSCs against an external time source.

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Thanks,
Dan

Quoted 539 lines

-----Original Message-----
From: Dave Winchell [mailto:[email protected]]
Sent: Friday, June 06, 2008 1:33 PM
To: [email protected]; Keir Fraser
Cc: Ben Guthro; xen-devel; Dave Winchell
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Dan, Keir:

Preliminary tests results indicate an error of .1% for Linux 64 bit guests configured
for hpet with xen-unstable as is. As we have discussed many times, the ntp requirement is .05%.
Tests on the patch we just submitted for hpet have indicated errors of .0012%
on this platform under similar test conditions and .03% on other platforms.

Windows vista64 has an error of 11% using hpet with the xen-unstable bits.
In an overnight test with our hpet patch, the Windows vista error was .008%.

The tests are with two or three guests on a physical node, all under load, and with
the ratio of vcpus to phys cpus > 1.

I will continue to run tests over the next few days.

thanks,
Dave

Quoted 3 lines · Dan Magenheimer

Hi Dave and Ben --

When running tests on xen-unstable (without your patch),

please ensure

Quoted 2 lines

that hpet=1 is set in the hvm config and also I think that when hpet is the clocksource on RHEL4-32, the clock IS resilient to

missed ticks

Quoted 1 line

so timer_mode should be 2 (vs when pit is the clocksource

on RHEL4-32,

Quoted 4 lines

all clock ticks must be delivered and so timer_mode should be 0).

Per

http://lists.xensource.com/archives/html/xen-devel/2008-06/msg
00098.html it's

Quoted 8 lines

my intent to clean this up, but I won't get to it until next week.

Thanks,
Dan

    -----Original Message-----
    *From:* [email protected]
    [mailto:[email protected]]*On 

Behalf Of *Dave

Quoted 9 lines

Winchell
*Sent:* Friday, June 06, 2008 4:46 AM
*To:* Keir Fraser; Ben Guthro; xen-devel
*Cc:* [email protected]; Dave Winchell *Subject:* RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Keir,

I think the changes are required. We'll run some tests

today today so

Quoted 7 lines

that we have some data to talk about.

-Dave

-----Original Message-----
From: [email protected] on behalf

of Keir Fraser

Quoted 8 lines

Sent: Fri 6/6/2008 4:58 AM
To: Ben Guthro; xen-devel
Cc: [email protected]
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Are these patches needed now the timers are built on Xen system time rather
than host TSC? Dan has reported much better

time-keeping with his

Quoted 2 lines

patch
checked in, and it¹s for sure a lot less invasive than

this patchset.

Quoted 10 lines

     -- Keir

    On 5/6/08 15:59, "Ben Guthro" <[email protected]> wrote:

    >
    > 1. Introduction
    >
    > This patch improves the hpet based guest clock in 

terms of drift and

Quoted 2 lines
Quoted 2 lines

monotonicity.
Prior to this work the drift with hpet was greater

than 2%, far

Quoted 10 lines

above the .05%
> limit
> for ntp to synchronize. With this code, the drift ranges from .001% to .0033%
> depending
> on guest and physical platform.
>
> Using hpet allows guest operating systems to provide monotonic time to their
> applications. Time sources other than hpet are not

monotonic because

Quoted 1 line
Quoted 1 line

of their reliance on tsc, which is not synchronized

across physical

Quoted 31 lines
Quoted 3 lines

processors.

Windows 2k864 and many Linux guests are supported with two

policies, one for
> guests
> that handle missed clock interrupts and the other for guests that require the
> correct number of interrupts.
>
> Guests may use hpet for the timing source even if the physical platform has no
> visible
> hpet. Migration is supported between physical machines which differ in
> physical
> hpet visibility.
>
> Most of the changes are in hpet.c. Two general facilities are added to track
> interrupt
> progress. The ideas here and the facilities would be useful in vpt.c, for
> other time
> sources, though no attempt is made here to improve vpt.c. >
> The following sections discuss hpet dependencies, interrupt delivery policies,
> live migration,
> test results, and relation to recent work with monotonic time. >
>

Quoted 3 lines

> 2. Virtual Hpet dependencies
>
> The virtual hpet depends on the ability to read the

physical or

Quoted 2 lines

simulated
> (see discussion below) hpet. For timekeeping, the

virtual hpet

Quoted 32 lines

also depends
> on two new interrupt notification facilities to implement its policies for
> interrupt delivery.
>
> 2.1. Two modes of low-level hpet main counter reads. >
> In this implementation, the virtual hpet reads with read_64_main_counter(),
> exported by
> time.c, either the real physical hpet main counter register directly or a
> "simulated"
> hpet main counter.
>
> The simulated mode uses a monotonic version of get_s_time() (NOW()), where the
> last
> time value is returned whenever the current time value is less than the last
> time
> value. In simulated mode, since it is layered on s_time, the underlying
> hardware
> can be hpet or some other device. The frequency of the main counter in
> simulated
> mode is the same as the standard physical hpet frequency, allowing live
> migration
> between nodes that are configured differently.
>

Quoted 1 line

> If the physical platform does not have an hpet

device, or if xen

Quoted 9 lines

is configured
> not
> to use the device, then the simulated method is used. If there is a physical
> hpet device,
> and xen has initialized it, then either simulated or physical mode can be
> used.
> This is governed by a boot time option, hpet-avoid.

Setting this

Quoted 3 lines

option to 1
> gives the
> simulated mode and 0 the physical mode. The default

is physical

Quoted 5 lines

mode.
>
> A disadvantage of the physical mode is that may take longer to read the device
> than in simulated mode. On some platforms the cost is

about the

Quoted 15 lines

same (less
> than 250 nsec) for
> physical and simulated modes, while on others physical cost is much higher
> than simulated.
> A disadvantage of the simulated mode is that it can return the same value
> for the counter in consecutive calls.
>
> 2.2. Interrupt notification facilities.
>
> Two interrupt notification facilities are introduced, one is > hvm_isa_irq_assert_cb()
> and the other hvm_register_intr_en_notif().
>

Quoted 5 lines

> The vhpet uses hvm_isa_irq_assert_cb to deliver interrupts to the vioapic.
> hvm_isa_irq_assert_cb allows a callback to be passed along to > vioapic_deliver()
> and this callback is called with a mask of the vcpus

which will

Quoted 4 lines

get the
> interrupt. This callback is made before any vcpus receive an interrupt.
>

Quoted 8 lines

> Vhpet uses hvm_register_intr_en_notif() to register a handler for a particular
> vector that will be called when that vector is injected in > [vmx,svm]_intr_assist()
> and also when the guest finishes handling the interrupt. Here finished is
> defined
> as the point when the guest re-enables interrupts or

lowers the

Quoted 4 lines

tpr value.
> EOI is not used as the end of interrupt as this is sometimes returned before
> the interrupt handler has done its work. A flag is

passed to the

Quoted 3 lines

handler
> indicating
> whether this is the injection point (post = 1) or the

interrupt

Quoted 6 lines

finished (post
> = 0) point.
> The need for the finished point callback is discussed in the missed ticks
> policy section.
>

Quoted 7 lines

> To prevent a possible early trigger of the finished callback, intr_en_notif
> logic
> has a two stage arm, the first at injection (hvm_intr_en_notif_arm()) and the
> second when
> interrupts are seen to be disabled

(hvm_intr_en_notif_disarm()).

Quoted 3 lines

Once fully
> armed, re-enabling
> interrupts will cause hvm_intr_en_notif_disarm() to

make the end

Quoted 2 lines

of interrupt
> callback. hvm_intr_en_notif_arm() and

hvm_intr_en_notif_disarm()

Quoted 6 lines

are called by
> [vmx,svm]_intr_assist().
>
> 3. Interrupt delivery policies
>
> The existing hpet interrupt delivery is preserved.

This includes

Quoted 8 lines
Quoted 1 line

vcpu round robin delivery used by Linux and broadcast delivery

used by
> Windows.
>
> There are two policies for interrupt delivery, one for Windows 2k8-64 and the
> other
> for Linux. The Linux policy takes advantage of the

(guest) Linux

Quoted 3 lines

missed tick
> and offset
> calculations and does not attempt to deliver the

right number of

Quoted 10 lines

interrupts.
> The Windows policy delivers the correct number of interrupts, even if
> sometimes much
> closer to each other than the period. The policies are similar to those in
> vpt.c, though
> there are some important differences.
>
> Policies are selected with an HVMOP_set_param

hypercall with index

Quoted 2 lines
Quoted 2 lines

HVM_PARAM_TIMER_MODE.
Two new values are added,

HVM_HPET_guest_computes_missed_ticks and

Quoted 7 lines
Quoted 1 line

HVM_HPET_guest_does_not_compute_missed_ticks. The reason that

two new ones
> are added is that
> in some guests (32bit Linux) a no-missed policy is needed for clock sources
> other than hpet
> and a missed ticks policy for hpet. It was felt that

there would

Quoted 14 lines

be less
> confusion by simply
> introducing the two hpet policies.
>
> 3.1. The missed ticks policy
>
> The Linux clock interrupt handler for hpet calculates missed ticks and offset
> using the hpet
> main counter. The algorithm works well when the time since the last interrupt
> is greater than
> or equal to a period and poorly otherwise.
>

Quoted 1 line

> The missed ticks policy ensures that no two clock

interrupts are

Quoted 6 lines

delivered to
> the guest at
> a time interval less than a period. A time stamp (hpet main counter value) is
> recorded (by a
> callback registered with hvm_register_intr_en_notif)

when Linux

Quoted 3 lines

finishes
> handling the clock
> interrupt. Then, ensuing interrupts are delivered to

the vioapic

Quoted 9 lines

only if the
> current main
> counter value is a period greater than when the last interrupt was handled.
>
> Tests showed a significant improvement in clock drift with end of interrupt
> time stamps
> versus beginning of interrupt[1]. It is believed that

the reason

Quoted 3 lines

for the
> improvement
> is that the clock interrupt handler goes for a

spinlock and can

Quoted 11 lines

be therefore
> delayed in its
> processing. Furthermore, the main counter is read by the guest under the lock.
> The net
> effect is that if we time stamp injection, we can get the difference in time
> between successive interrupt handler lock acquisitions to be less than the
> period.
>

Quoted 3 lines

> 3.2. The no-missed ticks policy
>
> Windows 2k864 keeps very poor time with the missed

ticks policy.

Quoted 6 lines

So the
> no-missed ticks policy
> was developed. In the no-missed ticks policy we deliver the correct number of
> interrupts,
> even if they are spaced less than a period apart

(when catching up).

Quoted 50 lines
Quoted 2 lines

Windows 2k864 uses a broadcast mode in the interrupt routing

such that
> all vcpus get the clock interrupt. The best Windows drift performance was
> achieved when the
> policy code ensured that all the previous interrupts (on the various vcpus)
> had been injected
> before injecting the next interrupt to the vioapic.. >
> The policy code works as follows. It uses the hvm_isa_irq_assert_cb() to
> record
> the vcpus to be interrupted in h->hpet.pending_mask. Then, in the callback
> registered
> with hvm_register_intr_en_notif() at post=1 time it clears the current vcpu in
> the pending_mask.
> When the pending_mask is clear it decrements hpet.intr_pending_nr and if
> intr_pending_nr is still
> non-zero posts another interrupt to the ioapic with hvm_isa_irq_assert_cb().
> Intr_pending_nr is incremented in hpet_route_decision_not_missed_ticks().
>
> The missed ticks policy intr_en_notif callback also uses the pending_mask
> method. So even though
> Linux does not broadcast its interrupts, the code could handle it if it did.
> In this case the end of interrupt time stamp is made when the pending_mask is
> clear.
>
> 4. Live Migration
>
> Live migration with hpet preserves the current offset of the guest clock with
> respect
> to ntp. This is accomplished by migrating all of the state in the h->hpet data
> structure
> in the usual way. The hp->mc_offset is recalculated on the receiving node so
> that the
> guest sees a continuous hpet main counter.
>

Quoted 6 lines

> Code as been added to xc_domain_save.c to send a small message after the
> domain context is sent. The contents of the message is the physical tsc
> timestamp, last_tsc,
> read just before the message is sent. When the

last_tsc message

Quoted 39 lines

is received in
> xc_domain_restore.c,
> another physical tsc timestamp, cur_tsc, is read. The two timestamps are
> loaded into the domain
> structure as last_tsc_sender and first_tsc_receiver with hypercalls. Then
> xc_domain_hvm_setcontext
> is called so that hpet_load has access to these time stamps. Hpet_load uses
> the timestamps
> to account for the time spent saving and loading the domain context. With this
> technique,
> the only neglected time is the time spent sending a small network message.
>
> 5. Test Results
>
> Some recent test results are:
>
> 5.1 Linux 4u664 and Windows 2k864 load test.
> Duration: 70 hours.
> Test date: 6/2/08
> Loads: usex -b48 on Linux; burn-in on Windows
> Guest vcpus: 8 for Linux; 2 for Windows
> Hardware: 8 physical cpu AMD
> Clock drift : Linux: .0012% Windows: .009%
>
> 5.2 Linux 4u664, Linux 4u464 , and Windows 2k864 no-load test
> Duration: 23 hours.
> Test date: 6/3/08
> Loads: none
> Guest vcpus: 8 for each Linux; 2 for Windows
> Hardware: 4 physical cpu AMD
> Clock drift : Linux: .033% Windows: .019%
>
> 6. Relation to recent work in xen-unstable
>

Quoted 7 lines

> There is a similarity between hvm_get_guest_time() in xen-unstable and
> read_64_main_counter()
> in this code. However, read_64_main_counter() is more tuned to the needs of
> hpet.c. It has no
> "set" operation, only the get. It isolates the mode,

physical or

Quoted 6 lines

simulated, in
> read_64_main_counter()
> itself. It uses no vcpu or domain state as it is a physical entity, in either
> mode. And it provides a real
> physical mode for every read for those applications

that desire

Quoted 5 lines

this.
>
> 7. Conclusion
>
> The virtual hpet is improved by this patch in terms

of accuracy and

Quoted 2 lines
Quoted 2 lines

monotonicity.
Tests performed to date verify this and more testing

is under way.

Quoted 12 lines
Quoted 4 lines

8. Future Work

Testing with Windows Vista will be performed soon. The reason

for accuracy
> variations
> on different platforms using the physical hpet device will be investigated.
> Additional overhead measurements on simulated vs physical hpet mode will be
> made.
>

Quoted 3 lines

> Footnotes:
>
> 1. I don't recall the accuracy improvement with end

of interrupt

Quoted 23 lines

stamping, but
> it was
> significant, perhaps better than two to one improvement. It would be a very
> simple matter
> to re-measure the improvement as the facility can call back at injection time
> as well.
>
>
> Signed-off-by: Dave Winchell <[email protected]> > <mailto:[email protected]>
> Signed-off-by: Ben Guthro <[email protected]> > <mailto:[email protected]>
>
>
> _______________________________________________
> Xen-devel mailing list
> [email protected]
> http://lists.xensource.com/xen-devel

#7 lore raw
Quoted 8 lines · Dan Magenheimer

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0. And if for some reason Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

Yes, this would be a sensible extra timer_mode: have hvm_get_guest_time() call to the platform time read function, and bypass TSC altogether. This would be cleaner than having only the vHPET code punch through to the physical HPET: instead we have the boot-time chosen platform timesource used by all virtual timers.

Quoted 2 lines

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Possibly there are bugs in the hpet device model which are fixed by Dave's patch. If this is actually the case, it would be nice to break those out as separate patches, as I think an 11% drift must largely be due to device-model bugs rather than relatively insignificant differences between hvm_get_guest_time() and physical HPET main counter.

-- Keir

#8 lore raw
Quoted 5 lines

Possibly there are bugs in the hpet device model which are fixed by Dave's patch. If this is actually the case, it would be nice to break those out as separate patches, as I think an 11% drift must largely be due to device-model bugs rather than relatively insignificant differences between hvm_get_guest_time() and physical HPET main counter.

Hi Keir,

I tried an experiment on Friday where I short circuited the missed ticks policy code in the hpet.c patch, but used the physical hpet each access. The result for Linux was a drift of .1%, same as the xen-unstable bits.

Conversely I get very good drift numbers, i.e., under .03%, when using the missed ticks policy code and running in simulated mode (layered on stime) when stime uses hpet.

So clearly, the improvement from .1% to .03% is due to the policy code. I haven't run the short circuit test with the windows policy but I can do that on Monday.

Note: For Windows and Linux I get < .03% drift using the policy code and running simulated mode whether stime is using hpet or some other device.

regards,
Dave

-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Fri 6/6/2008 6:34 PM
To: [email protected]; Dave Winchell
Cc: Ben Guthro; xen-devel
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 8 lines · Dan Magenheimer

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0. And if for some reason Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

Yes, this would be a sensible extra timer_mode: have hvm_get_guest_time() call to the platform time read function, and bypass TSC altogether. This would be cleaner than having only the vHPET code punch through to the physical HPET: instead we have the boot-time chosen platform timesource used by all virtual timers.

Quoted 2 lines

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Possibly there are bugs in the hpet device model which are fixed by Dave's patch. If this is actually the case, it would be nice to break those out as separate patches, as I think an 11% drift must largely be due to device-model bugs rather than relatively insignificant differences between hvm_get_guest_time() and physical HPET main counter.

-- Keir

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

#11 lore raw
In reply to Dan Magenheimer

Hi Dan,

Quoted 7 lines

While I am fully supportive of offering hardware hpet as an option for hvm guests (let's call it hwhpet=1 for shorthand), I am very surprised by your preliminary results; the most obvious conclusion is that Xen system time is losing time at the rate of 1000 PPM though its possible there's a bug somewhere else in the "time stack". Your Windows result is jaw-dropping and inexplicable, though I have to admit ignorance of how Windows manages time.

I think xen system time is fine. You have to add the interrupt delivery policies decribed in the write-up for the patch to get accurate timekeeping in the guest.

The windows policy is obvious and results in a large improvement in accuracy. The Linux policy is more subtle, but is required to go from .1% to .03%.

Quoted 6 lines

I think with my recent patch and hpet=1 (essentially the same as your emulated hpet), hvm guest time should track Xen system time. I wonder if domain0 (which if I understand correctly is directly using Xen system time) is also seeing an error of .1%? Also I wonder for the skew you are seeing (in both hvm guests and domain0) is time moving too fast or two slow?

I don't recall the direction. I can look it up in my notes at work tomorrow.

Quoted 4 lines

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0.

Our patch is accurate to < .03% using the physical hpet mode or the simulated mode.

Quoted 5 lines

And if for some reason
Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

In our experience, Xen system time is accurate enough now.

Quoted 5 lines

One more thought... do you know the accuracy of the TSC crystals on your test systems? I posted a patch awhile ago that was intended to test that, though I guess it was only testing skew of different TSCs on the same system, not TSCs against an external time source.

I do not know the tsc accuracy.

Quoted 2 lines

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Regards,
Dave

-----Original Message-----
From: Dan Magenheimer [mailto:[email protected]]
Sent: Fri 6/6/2008 4:29 PM
To: Dave Winchell; Keir Fraser
Cc: Ben Guthro; xen-devel
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Dave --

Thanks much for posting the preliminary results!

While I am fully supportive of offering hardware hpet as an option for hvm guests (let's call it hwhpet=1 for shorthand), I am very surprised by your preliminary results; the most obvious conclusion is that Xen system time is losing time at the rate of 1000 PPM though its possible there's a bug somewhere else in the "time stack". Your Windows result is jaw-dropping and inexplicable, though I have to admit ignorance of how Windows manages time.

I think with my recent patch and hpet=1 (essentially the same as your emulated hpet), hvm guest time should track Xen system time. I wonder if domain0 (which if I understand correctly is directly using Xen system time) is also seeing an error of .1%? Also I wonder for the skew you are seeing (in both hvm guests and domain0) is time moving too fast or two slow?

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0. And if for some reason Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

One more thought... do you know the accuracy of the TSC crystals on your test systems? I posted a patch awhile ago that was intended to test that, though I guess it was only testing skew of different TSCs on the same system, not TSCs against an external time source.

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Thanks,
Dan

Quoted 510 lines

-----Original Message-----
From: Dave Winchell [mailto:[email protected]]
Sent: Friday, June 06, 2008 1:33 PM
To: [email protected]; Keir Fraser
Cc: Ben Guthro; xen-devel; Dave Winchell
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Dan, Keir:

Preliminary tests results indicate an error of .1% for Linux 64 bit guests configured
for hpet with xen-unstable as is. As we have discussed many times, the ntp requirement is .05%.
Tests on the patch we just submitted for hpet have indicated errors of .0012%
on this platform under similar test conditions and .03% on other platforms.

Windows vista64 has an error of 11% using hpet with the xen-unstable bits.
In an overnight test with our hpet patch, the Windows vista error was .008%.

The tests are with two or three guests on a physical node, all under load, and with
the ratio of vcpus to phys cpus > 1.

I will continue to run tests over the next few days.

thanks,
Dave

Quoted 3 lines · Dan Magenheimer

Hi Dave and Ben --

When running tests on xen-unstable (without your patch),

please ensure

Quoted 2 lines

that hpet=1 is set in the hvm config and also I think that when hpet is the clocksource on RHEL4-32, the clock IS resilient to

missed ticks

Quoted 1 line

so timer_mode should be 2 (vs when pit is the clocksource

on RHEL4-32,

Quoted 4 lines

all clock ticks must be delivered and so timer_mode should be 0).

Per

http://lists.xensource.com/archives/html/xen-devel/2008-06/msg
00098.html it's

Quoted 8 lines

my intent to clean this up, but I won't get to it until next week.

Thanks,
Dan

    -----Original Message-----
    *From:* [email protected]
    [mailto:[email protected]]*On 

Behalf Of *Dave

Quoted 9 lines

Winchell
*Sent:* Friday, June 06, 2008 4:46 AM
*To:* Keir Fraser; Ben Guthro; xen-devel
*Cc:* [email protected]; Dave Winchell *Subject:* RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Keir,

I think the changes are required. We'll run some tests

today today so

Quoted 7 lines

that we have some data to talk about.

-Dave

-----Original Message-----
From: [email protected] on behalf

of Keir Fraser

Quoted 8 lines

Sent: Fri 6/6/2008 4:58 AM
To: Ben Guthro; xen-devel
Cc: [email protected]
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Are these patches needed now the timers are built on Xen system time rather
than host TSC? Dan has reported much better

time-keeping with his

Quoted 2 lines

patch
checked in, and it¹s for sure a lot less invasive than

this patchset.

Quoted 10 lines

     -- Keir

    On 5/6/08 15:59, "Ben Guthro" <[email protected]> wrote:

    >
    > 1. Introduction
    >
    > This patch improves the hpet based guest clock in 

terms of drift and

Quoted 2 lines
Quoted 2 lines

monotonicity.
Prior to this work the drift with hpet was greater

than 2%, far

Quoted 10 lines

above the .05%
> limit
> for ntp to synchronize. With this code, the drift ranges from .001% to .0033%
> depending
> on guest and physical platform.
>
> Using hpet allows guest operating systems to provide monotonic time to their
> applications. Time sources other than hpet are not

monotonic because

Quoted 1 line
Quoted 1 line

of their reliance on tsc, which is not synchronized

across physical

Quoted 31 lines
Quoted 3 lines

processors.

Windows 2k864 and many Linux guests are supported with two

policies, one for
> guests
> that handle missed clock interrupts and the other for guests that require the
> correct number of interrupts.
>
> Guests may use hpet for the timing source even if the physical platform has no
> visible
> hpet. Migration is supported between physical machines which differ in
> physical
> hpet visibility.
>
> Most of the changes are in hpet.c. Two general facilities are added to track
> interrupt
> progress. The ideas here and the facilities would be useful in vpt.c, for
> other time
> sources, though no attempt is made here to improve vpt.c. >
> The following sections discuss hpet dependencies, interrupt delivery policies,
> live migration,
> test results, and relation to recent work with monotonic time. >
>

Quoted 3 lines

> 2. Virtual Hpet dependencies
>
> The virtual hpet depends on the ability to read the

physical or

Quoted 2 lines

simulated
> (see discussion below) hpet. For timekeeping, the

virtual hpet

Quoted 32 lines

also depends
> on two new interrupt notification facilities to implement its policies for
> interrupt delivery.
>
> 2.1. Two modes of low-level hpet main counter reads. >
> In this implementation, the virtual hpet reads with read_64_main_counter(),
> exported by
> time.c, either the real physical hpet main counter register directly or a
> "simulated"
> hpet main counter.
>
> The simulated mode uses a monotonic version of get_s_time() (NOW()), where the
> last
> time value is returned whenever the current time value is less than the last
> time
> value. In simulated mode, since it is layered on s_time, the underlying
> hardware
> can be hpet or some other device. The frequency of the main counter in
> simulated
> mode is the same as the standard physical hpet frequency, allowing live
> migration
> between nodes that are configured differently.
>

Quoted 1 line

> If the physical platform does not have an hpet

device, or if xen

Quoted 9 lines

is configured
> not
> to use the device, then the simulated method is used. If there is a physical
> hpet device,
> and xen has initialized it, then either simulated or physical mode can be
> used.
> This is governed by a boot time option, hpet-avoid.

Setting this

Quoted 3 lines

option to 1
> gives the
> simulated mode and 0 the physical mode. The default

is physical

Quoted 5 lines

mode.
>
> A disadvantage of the physical mode is that may take longer to read the device
> than in simulated mode. On some platforms the cost is

about the

Quoted 15 lines

same (less
> than 250 nsec) for
> physical and simulated modes, while on others physical cost is much higher
> than simulated.
> A disadvantage of the simulated mode is that it can return the same value
> for the counter in consecutive calls.
>
> 2.2. Interrupt notification facilities.
>
> Two interrupt notification facilities are introduced, one is > hvm_isa_irq_assert_cb()
> and the other hvm_register_intr_en_notif().
>

Quoted 5 lines

> The vhpet uses hvm_isa_irq_assert_cb to deliver interrupts to the vioapic.
> hvm_isa_irq_assert_cb allows a callback to be passed along to > vioapic_deliver()
> and this callback is called with a mask of the vcpus

which will

Quoted 4 lines

get the
> interrupt. This callback is made before any vcpus receive an interrupt.
>

Quoted 8 lines

> Vhpet uses hvm_register_intr_en_notif() to register a handler for a particular
> vector that will be called when that vector is injected in > [vmx,svm]_intr_assist()
> and also when the guest finishes handling the interrupt. Here finished is
> defined
> as the point when the guest re-enables interrupts or

lowers the

Quoted 4 lines

tpr value.
> EOI is not used as the end of interrupt as this is sometimes returned before
> the interrupt handler has done its work. A flag is

passed to the

Quoted 3 lines

handler
> indicating
> whether this is the injection point (post = 1) or the

interrupt

Quoted 6 lines

finished (post
> = 0) point.
> The need for the finished point callback is discussed in the missed ticks
> policy section.
>

Quoted 7 lines

> To prevent a possible early trigger of the finished callback, intr_en_notif
> logic
> has a two stage arm, the first at injection (hvm_intr_en_notif_arm()) and the
> second when
> interrupts are seen to be disabled

(hvm_intr_en_notif_disarm()).

Quoted 3 lines

Once fully
> armed, re-enabling
> interrupts will cause hvm_intr_en_notif_disarm() to

make the end

Quoted 2 lines

of interrupt
> callback. hvm_intr_en_notif_arm() and

hvm_intr_en_notif_disarm()

Quoted 6 lines

are called by
> [vmx,svm]_intr_assist().
>
> 3. Interrupt delivery policies
>
> The existing hpet interrupt delivery is preserved.

This includes

Quoted 8 lines
Quoted 1 line

vcpu round robin delivery used by Linux and broadcast delivery

used by
> Windows.
>
> There are two policies for interrupt delivery, one for Windows 2k8-64 and the
> other
> for Linux. The Linux policy takes advantage of the

(guest) Linux

Quoted 3 lines

missed tick
> and offset
> calculations and does not attempt to deliver the

right number of

Quoted 10 lines

interrupts.
> The Windows policy delivers the correct number of interrupts, even if
> sometimes much
> closer to each other than the period. The policies are similar to those in
> vpt.c, though
> there are some important differences.
>
> Policies are selected with an HVMOP_set_param

hypercall with index

Quoted 2 lines
Quoted 2 lines

HVM_PARAM_TIMER_MODE.
Two new values are added,

HVM_HPET_guest_computes_missed_ticks and

Quoted 7 lines
Quoted 1 line

HVM_HPET_guest_does_not_compute_missed_ticks. The reason that

two new ones
> are added is that
> in some guests (32bit Linux) a no-missed policy is needed for clock sources
> other than hpet
> and a missed ticks policy for hpet. It was felt that

there would

Quoted 14 lines

be less
> confusion by simply
> introducing the two hpet policies.
>
> 3.1. The missed ticks policy
>
> The Linux clock interrupt handler for hpet calculates missed ticks and offset
> using the hpet
> main counter. The algorithm works well when the time since the last interrupt
> is greater than
> or equal to a period and poorly otherwise.
>

Quoted 1 line

> The missed ticks policy ensures that no two clock

interrupts are

Quoted 6 lines

delivered to
> the guest at
> a time interval less than a period. A time stamp (hpet main counter value) is
> recorded (by a
> callback registered with hvm_register_intr_en_notif)

when Linux

Quoted 3 lines

finishes
> handling the clock
> interrupt. Then, ensuing interrupts are delivered to

the vioapic

Quoted 9 lines

only if the
> current main
> counter value is a period greater than when the last interrupt was handled.
>
> Tests showed a significant improvement in clock drift with end of interrupt
> time stamps
> versus beginning of interrupt[1]. It is believed that

the reason

Quoted 3 lines

for the
> improvement
> is that the clock interrupt handler goes for a

spinlock and can

Quoted 11 lines

be therefore
> delayed in its
> processing. Furthermore, the main counter is read by the guest under the lock.
> The net
> effect is that if we time stamp injection, we can get the difference in time
> between successive interrupt handler lock acquisitions to be less than the
> period.
>

Quoted 3 lines

> 3.2. The no-missed ticks policy
>
> Windows 2k864 keeps very poor time with the missed

ticks policy.

Quoted 6 lines

So the
> no-missed ticks policy
> was developed. In the no-missed ticks policy we deliver the correct number of
> interrupts,
> even if they are spaced less than a period apart

(when catching up).

Quoted 50 lines
Quoted 2 lines

Windows 2k864 uses a broadcast mode in the interrupt routing

such that
> all vcpus get the clock interrupt. The best Windows drift performance was
> achieved when the
> policy code ensured that all the previous interrupts (on the various vcpus)
> had been injected
> before injecting the next interrupt to the vioapic.. >
> The policy code works as follows. It uses the hvm_isa_irq_assert_cb() to
> record
> the vcpus to be interrupted in h->hpet.pending_mask. Then, in the callback
> registered
> with hvm_register_intr_en_notif() at post=1 time it clears the current vcpu in
> the pending_mask.
> When the pending_mask is clear it decrements hpet.intr_pending_nr and if
> intr_pending_nr is still
> non-zero posts another interrupt to the ioapic with hvm_isa_irq_assert_cb().
> Intr_pending_nr is incremented in hpet_route_decision_not_missed_ticks().
>
> The missed ticks policy intr_en_notif callback also uses the pending_mask
> method. So even though
> Linux does not broadcast its interrupts, the code could handle it if it did.
> In this case the end of interrupt time stamp is made when the pending_mask is
> clear.
>
> 4. Live Migration
>
> Live migration with hpet preserves the current offset of the guest clock with
> respect
> to ntp. This is accomplished by migrating all of the state in the h->hpet data
> structure
> in the usual way. The hp->mc_offset is recalculated on the receiving node so
> that the
> guest sees a continuous hpet main counter.
>

Quoted 6 lines

> Code as been added to xc_domain_save.c to send a small message after the
> domain context is sent. The contents of the message is the physical tsc
> timestamp, last_tsc,
> read just before the message is sent. When the

last_tsc message

Quoted 39 lines

is received in
> xc_domain_restore.c,
> another physical tsc timestamp, cur_tsc, is read. The two timestamps are
> loaded into the domain
> structure as last_tsc_sender and first_tsc_receiver with hypercalls. Then
> xc_domain_hvm_setcontext
> is called so that hpet_load has access to these time stamps. Hpet_load uses
> the timestamps
> to account for the time spent saving and loading the domain context. With this
> technique,
> the only neglected time is the time spent sending a small network message.
>
> 5. Test Results
>
> Some recent test results are:
>
> 5.1 Linux 4u664 and Windows 2k864 load test.
> Duration: 70 hours.
> Test date: 6/2/08
> Loads: usex -b48 on Linux; burn-in on Windows
> Guest vcpus: 8 for Linux; 2 for Windows
> Hardware: 8 physical cpu AMD
> Clock drift : Linux: .0012% Windows: .009%
>
> 5.2 Linux 4u664, Linux 4u464 , and Windows 2k864 no-load test
> Duration: 23 hours.
> Test date: 6/3/08
> Loads: none
> Guest vcpus: 8 for each Linux; 2 for Windows
> Hardware: 4 physical cpu AMD
> Clock drift : Linux: .033% Windows: .019%
>
> 6. Relation to recent work in xen-unstable
>

Quoted 7 lines

> There is a similarity between hvm_get_guest_time() in xen-unstable and
> read_64_main_counter()
> in this code. However, read_64_main_counter() is more tuned to the needs of
> hpet.c. It has no
> "set" operation, only the get. It isolates the mode,

physical or

Quoted 6 lines

simulated, in
> read_64_main_counter()
> itself. It uses no vcpu or domain state as it is a physical entity, in either
> mode. And it provides a real
> physical mode for every read for those applications

that desire

Quoted 5 lines

this.
>
> 7. Conclusion
>
> The virtual hpet is improved by this patch in terms

of accuracy and

Quoted 2 lines
Quoted 2 lines

monotonicity.
Tests performed to date verify this and more testing

is under way.

Quoted 12 lines
Quoted 4 lines

8. Future Work

Testing with Windows Vista will be performed soon. The reason

for accuracy
> variations
> on different platforms using the physical hpet device will be investigated.
> Additional overhead measurements on simulated vs physical hpet mode will be
> made.
>

Quoted 29 lines
Quoted 3 lines
Quoted 3 lines

Footnotes:

1. I don't recall the accuracy improvement with end

of interrupt

Quoted 23 lines

stamping, but
> it was
> significant, perhaps better than two to one improvement. It would be a very
> simple matter
> to re-measure the improvement as the facility can call back at injection time
> as well.
>
>
> Signed-off-by: Dave Winchell <[email protected]> > <mailto:[email protected]>
> Signed-off-by: Ben Guthro <[email protected]> > <mailto:[email protected]>
>
>
> _______________________________________________
> Xen-devel mailing list
> [email protected]
> http://lists.xensource.com/xen-devel

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

#12 lore raw

RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracyHi Dave --

Thanks for the additional explanation.

Could you please be very precise, when you say "Linux", as to what you are (and are not) testing? Specifically:
1) kernel version number and/or distro info
2) 32 vs 64
3) kernel parameters specified
4) config file parameters
5) relevant CPU info that may be passed through by Xen to hvm guests (e.g. whether "tsc is synchronized")
6) relevant xen boot parameters (if any)

As we've seen, different combinations of the above can yield very different test results. We'd like to confirm your tests, but if we can avoid unnecessary additional iterations (due to mismatches on the above), that would be helpful.

Our testing goal is to ensure that there is at least one known good combination of parameters for each of RHEL3, RHEL4, and RHEL5 (both 32 and 64) and that works on both tsc-synchronized and tsc-unsynchronized Intel and AMD boxes. And hopefully that works with and without a real physical hpet available.

We don't have a good test environment for Windows time, but if you can provide the same test configuration detail, we may be able to do some testing.

Thanks,
Dan

-----Original Message-----
From: Dave Winchell [mailto:[email protected]]
Sent: Sunday, June 08, 2008 2:32 PM
To: [email protected]; Keir Fraser
Cc: Ben Guthro; xen-devel; Dave Winchell
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Hi Dan,

Quoted 7 lines

While I am fully supportive of offering hardware hpet as an option for hvm guests (let's call it hwhpet=1 for shorthand), I am very surprised by your preliminary results; the most obvious conclusion is that Xen system time is losing time at the rate of 1000 PPM though its possible there's a bug somewhere else in the "time stack". Your Windows result is jaw-dropping and inexplicable, though I have to admit ignorance of how Windows manages time.

I think xen system time is fine. You have to add the interrupt delivery policies decribed in the write-up for the patch to get accurate timekeeping in the guest.

The windows policy is obvious and results in a large improvement in accuracy. The Linux policy is more subtle, but is required to go from .1% to .03%.

Quoted 6 lines

I think with my recent patch and hpet=1 (essentially the same as your emulated hpet), hvm guest time should track Xen system time. I wonder if domain0 (which if I understand correctly is directly using Xen system time) is also seeing an error of .1%? Also I wonder for the skew you are seeing (in both hvm guests and domain0) is time moving too fast or two slow?

I don't recall the direction. I can look it up in my notes at work tomorrow.

Quoted 4 lines

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0.

Our patch is accurate to < .03% using the physical hpet mode or the simulated mode.

Quoted 5 lines

And if for some reason
Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

In our experience, Xen system time is accurate enough now.

Quoted 5 lines

One more thought... do you know the accuracy of the TSC crystals on your test systems? I posted a patch awhile ago that was intended to test that, though I guess it was only testing skew of different TSCs on the same system, not TSCs against an external time source.

I do not know the tsc accuracy.

Quoted 2 lines

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Regards,
Dave

-----Original Message-----
From: Dan Magenheimer [mailto:[email protected]]
Sent: Fri 6/6/2008 4:29 PM
To: Dave Winchell; Keir Fraser
Cc: Ben Guthro; xen-devel
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Dave --

Thanks much for posting the preliminary results!

While I am fully supportive of offering hardware hpet as an option for hvm guests (let's call it hwhpet=1 for shorthand), I am very surprised by your preliminary results; the most obvious conclusion is that Xen system time is losing time at the rate of 1000 PPM though its possible there's a bug somewhere else in the "time stack". Your Windows result is jaw-dropping and inexplicable, though I have to admit ignorance of how Windows manages time.

I think with my recent patch and hpet=1 (essentially the same as your emulated hpet), hvm guest time should track Xen system time. I wonder if domain0 (which if I understand correctly is directly using Xen system time) is also seeing an error of .1%? Also I wonder for the skew you are seeing (in both hvm guests and domain0) is time moving too fast or two slow?

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0. And if for some reason Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

One more thought... do you know the accuracy of the TSC crystals on your test systems? I posted a patch awhile ago that was intended to test that, though I guess it was only testing skew of different TSCs on the same system, not TSCs against an external time source.

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Thanks,
Dan

Quoted 510 lines

-----Original Message-----
From: Dave Winchell [mailto:[email protected]]
Sent: Friday, June 06, 2008 1:33 PM
To: [email protected]; Keir Fraser
Cc: Ben Guthro; xen-devel; Dave Winchell
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Dan, Keir:

Preliminary tests results indicate an error of .1% for Linux 64 bit guests configured
for hpet with xen-unstable as is. As we have discussed many times, the ntp requirement is .05%.
Tests on the patch we just submitted for hpet have indicated errors of .0012%
on this platform under similar test conditions and .03% on other platforms.

Windows vista64 has an error of 11% using hpet with the xen-unstable bits.
In an overnight test with our hpet patch, the Windows vista error was .008%.

The tests are with two or three guests on a physical node, all under load, and with
the ratio of vcpus to phys cpus > 1.

I will continue to run tests over the next few days.

thanks,
Dave

Quoted 3 lines · Dan Magenheimer

Hi Dave and Ben --

When running tests on xen-unstable (without your patch),

please ensure

Quoted 2 lines

that hpet=1 is set in the hvm config and also I think that when hpet is the clocksource on RHEL4-32, the clock IS resilient to

missed ticks

Quoted 1 line

so timer_mode should be 2 (vs when pit is the clocksource

on RHEL4-32,

Quoted 4 lines

all clock ticks must be delivered and so timer_mode should be 0).

Per

http://lists.xensource.com/archives/html/xen-devel/2008-06/msg
00098.html it's

Quoted 8 lines

my intent to clean this up, but I won't get to it until next week.

Thanks,
Dan

    -----Original Message-----
    *From:* [email protected]
    [mailto:[email protected]]*On

Behalf Of *Dave

Quoted 9 lines

Winchell
*Sent:* Friday, June 06, 2008 4:46 AM
*To:* Keir Fraser; Ben Guthro; xen-devel
*Cc:* [email protected]; Dave Winchell *Subject:* RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Keir,

I think the changes are required. We'll run some tests

today today so

Quoted 7 lines

that we have some data to talk about.

-Dave

-----Original Message-----
From: [email protected] on behalf

of Keir Fraser

Quoted 8 lines

Sent: Fri 6/6/2008 4:58 AM
To: Ben Guthro; xen-devel
Cc: [email protected]
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Are these patches needed now the timers are built on Xen system time rather
than host TSC? Dan has reported much better

time-keeping with his

Quoted 2 lines

patch
checked in, and it¹s for sure a lot less invasive than

this patchset.

Quoted 10 lines

     -- Keir

    On 5/6/08 15:59, "Ben Guthro" <[email protected]> wrote:

    >
    > 1. Introduction
    >
    > This patch improves the hpet based guest clock in

terms of drift and

Quoted 2 lines
Quoted 2 lines

monotonicity.
Prior to this work the drift with hpet was greater

than 2%, far

Quoted 10 lines

above the .05%
> limit
> for ntp to synchronize. With this code, the drift ranges from .001% to .0033%
> depending
> on guest and physical platform.
>
> Using hpet allows guest operating systems to provide monotonic time to their
> applications. Time sources other than hpet are not

monotonic because

Quoted 1 line
Quoted 1 line

of their reliance on tsc, which is not synchronized

across physical

Quoted 31 lines
Quoted 3 lines

processors.

Windows 2k864 and many Linux guests are supported with two

policies, one for
> guests
> that handle missed clock interrupts and the other for guests that require the
> correct number of interrupts.
>
> Guests may use hpet for the timing source even if the physical platform has no
> visible
> hpet. Migration is supported between physical machines which differ in
> physical
> hpet visibility.
>
> Most of the changes are in hpet.c. Two general facilities are added to track
> interrupt
> progress. The ideas here and the facilities would be useful in vpt.c, for
> other time
> sources, though no attempt is made here to improve vpt.c. >
> The following sections discuss hpet dependencies, interrupt delivery policies,
> live migration,
> test results, and relation to recent work with monotonic time. >
>

Quoted 3 lines

> 2. Virtual Hpet dependencies
>
> The virtual hpet depends on the ability to read the

physical or

Quoted 2 lines

simulated
> (see discussion below) hpet. For timekeeping, the

virtual hpet

Quoted 32 lines

also depends
> on two new interrupt notification facilities to implement its policies for
> interrupt delivery.
>
> 2.1. Two modes of low-level hpet main counter reads. >
> In this implementation, the virtual hpet reads with read_64_main_counter(),
> exported by
> time.c, either the real physical hpet main counter register directly or a
> "simulated"
> hpet main counter.
>
> The simulated mode uses a monotonic version of get_s_time() (NOW()), where the
> last
> time value is returned whenever the current time value is less than the last
> time
> value. In simulated mode, since it is layered on s_time, the underlying
> hardware
> can be hpet or some other device. The frequency of the main counter in
> simulated
> mode is the same as the standard physical hpet frequency, allowing live
> migration
> between nodes that are configured differently.
>

Quoted 1 line

> If the physical platform does not have an hpet

device, or if xen

Quoted 9 lines

is configured
> not
> to use the device, then the simulated method is used. If there is a physical
> hpet device,
> and xen has initialized it, then either simulated or physical mode can be
> used.
> This is governed by a boot time option, hpet-avoid.

Setting this

Quoted 3 lines

option to 1
> gives the
> simulated mode and 0 the physical mode. The default

is physical

Quoted 5 lines

mode.
>
> A disadvantage of the physical mode is that may take longer to read the device
> than in simulated mode. On some platforms the cost is

about the

Quoted 15 lines

same (less
> than 250 nsec) for
> physical and simulated modes, while on others physical cost is much higher
> than simulated.
> A disadvantage of the simulated mode is that it can return the same value
> for the counter in consecutive calls.
>
> 2.2. Interrupt notification facilities.
>
> Two interrupt notification facilities are introduced, one is > hvm_isa_irq_assert_cb()
> and the other hvm_register_intr_en_notif().
>

Quoted 5 lines

> The vhpet uses hvm_isa_irq_assert_cb to deliver interrupts to the vioapic.
> hvm_isa_irq_assert_cb allows a callback to be passed along to > vioapic_deliver()
> and this callback is called with a mask of the vcpus

which will

Quoted 4 lines

get the
> interrupt. This callback is made before any vcpus receive an interrupt.
>

Quoted 8 lines

> Vhpet uses hvm_register_intr_en_notif() to register a handler for a particular
> vector that will be called when that vector is injected in > [vmx,svm]_intr_assist()
> and also when the guest finishes handling the interrupt. Here finished is
> defined
> as the point when the guest re-enables interrupts or

lowers the

Quoted 4 lines

tpr value.
> EOI is not used as the end of interrupt as this is sometimes returned before
> the interrupt handler has done its work. A flag is

passed to the

Quoted 3 lines

handler
> indicating
> whether this is the injection point (post = 1) or the

interrupt

Quoted 6 lines

finished (post
> = 0) point.
> The need for the finished point callback is discussed in the missed ticks
> policy section.
>

Quoted 7 lines

> To prevent a possible early trigger of the finished callback, intr_en_notif
> logic
> has a two stage arm, the first at injection (hvm_intr_en_notif_arm()) and the
> second when
> interrupts are seen to be disabled

(hvm_intr_en_notif_disarm()).

Quoted 3 lines

Once fully
> armed, re-enabling
> interrupts will cause hvm_intr_en_notif_disarm() to

make the end

Quoted 2 lines

of interrupt
> callback. hvm_intr_en_notif_arm() and

hvm_intr_en_notif_disarm()

Quoted 6 lines

are called by
> [vmx,svm]_intr_assist().
>
> 3. Interrupt delivery policies
>
> The existing hpet interrupt delivery is preserved.

This includes

Quoted 8 lines
Quoted 1 line

vcpu round robin delivery used by Linux and broadcast delivery

used by
> Windows.
>
> There are two policies for interrupt delivery, one for Windows 2k8-64 and the
> other
> for Linux. The Linux policy takes advantage of the

(guest) Linux

Quoted 3 lines

missed tick
> and offset
> calculations and does not attempt to deliver the

right number of

Quoted 10 lines

interrupts.
> The Windows policy delivers the correct number of interrupts, even if
> sometimes much
> closer to each other than the period. The policies are similar to those in
> vpt.c, though
> there are some important differences.
>
> Policies are selected with an HVMOP_set_param

hypercall with index

Quoted 2 lines
Quoted 2 lines

HVM_PARAM_TIMER_MODE.
Two new values are added,

HVM_HPET_guest_computes_missed_ticks and

Quoted 7 lines
Quoted 1 line

HVM_HPET_guest_does_not_compute_missed_ticks. The reason that

two new ones
> are added is that
> in some guests (32bit Linux) a no-missed policy is needed for clock sources
> other than hpet
> and a missed ticks policy for hpet. It was felt that

there would

Quoted 14 lines

be less
> confusion by simply
> introducing the two hpet policies.
>
> 3.1. The missed ticks policy
>
> The Linux clock interrupt handler for hpet calculates missed ticks and offset
> using the hpet
> main counter. The algorithm works well when the time since the last interrupt
> is greater than
> or equal to a period and poorly otherwise.
>

Quoted 1 line

> The missed ticks policy ensures that no two clock

interrupts are

Quoted 6 lines

delivered to
> the guest at
> a time interval less than a period. A time stamp (hpet main counter value) is
> recorded (by a
> callback registered with hvm_register_intr_en_notif)

when Linux

Quoted 3 lines

finishes
> handling the clock
> interrupt. Then, ensuing interrupts are delivered to

the vioapic

Quoted 9 lines

only if the
> current main
> counter value is a period greater than when the last interrupt was handled.
>
> Tests showed a significant improvement in clock drift with end of interrupt
> time stamps
> versus beginning of interrupt[1]. It is believed that

the reason

Quoted 3 lines

for the
> improvement
> is that the clock interrupt handler goes for a

spinlock and can

Quoted 11 lines

be therefore
> delayed in its
> processing. Furthermore, the main counter is read by the guest under the lock.
> The net
> effect is that if we time stamp injection, we can get the difference in time
> between successive interrupt handler lock acquisitions to be less than the
> period.
>

Quoted 3 lines

> 3.2. The no-missed ticks policy
>
> Windows 2k864 keeps very poor time with the missed

ticks policy.

Quoted 6 lines

So the
> no-missed ticks policy
> was developed. In the no-missed ticks policy we deliver the correct number of
> interrupts,
> even if they are spaced less than a period apart

(when catching up).

Quoted 50 lines
Quoted 2 lines

Windows 2k864 uses a broadcast mode in the interrupt routing

such that
> all vcpus get the clock interrupt. The best Windows drift performance was
> achieved when the
> policy code ensured that all the previous interrupts (on the various vcpus)
> had been injected
> before injecting the next interrupt to the vioapic.. >
> The policy code works as follows. It uses the hvm_isa_irq_assert_cb() to
> record
> the vcpus to be interrupted in h->hpet.pending_mask. Then, in the callback
> registered
> with hvm_register_intr_en_notif() at post=1 time it clears the current vcpu in
> the pending_mask.
> When the pending_mask is clear it decrements hpet.intr_pending_nr and if
> intr_pending_nr is still
> non-zero posts another interrupt to the ioapic with hvm_isa_irq_assert_cb().
> Intr_pending_nr is incremented in hpet_route_decision_not_missed_ticks().
>
> The missed ticks policy intr_en_notif callback also uses the pending_mask
> method. So even though
> Linux does not broadcast its interrupts, the code could handle it if it did.
> In this case the end of interrupt time stamp is made when the pending_mask is
> clear.
>
> 4. Live Migration
>
> Live migration with hpet preserves the current offset of the guest clock with
> respect
> to ntp. This is accomplished by migrating all of the state in the h->hpet data
> structure
> in the usual way. The hp->mc_offset is recalculated on the receiving node so
> that the
> guest sees a continuous hpet main counter.
>

Quoted 6 lines

> Code as been added to xc_domain_save.c to send a small message after the
> domain context is sent. The contents of the message is the physical tsc
> timestamp, last_tsc,
> read just before the message is sent. When the

last_tsc message

Quoted 39 lines

is received in
> xc_domain_restore.c,
> another physical tsc timestamp, cur_tsc, is read. The two timestamps are
> loaded into the domain
> structure as last_tsc_sender and first_tsc_receiver with hypercalls. Then
> xc_domain_hvm_setcontext
> is called so that hpet_load has access to these time stamps. Hpet_load uses
> the timestamps
> to account for the time spent saving and loading the domain context. With this
> technique,
> the only neglected time is the time spent sending a small network message.
>
> 5. Test Results
>
> Some recent test results are:
>
> 5.1 Linux 4u664 and Windows 2k864 load test.
> Duration: 70 hours.
> Test date: 6/2/08
> Loads: usex -b48 on Linux; burn-in on Windows
> Guest vcpus: 8 for Linux; 2 for Windows
> Hardware: 8 physical cpu AMD
> Clock drift : Linux: .0012% Windows: .009%
>
> 5.2 Linux 4u664, Linux 4u464 , and Windows 2k864 no-load test
> Duration: 23 hours.
> Test date: 6/3/08
> Loads: none
> Guest vcpus: 8 for each Linux; 2 for Windows
> Hardware: 4 physical cpu AMD
> Clock drift : Linux: .033% Windows: .019%
>
> 6. Relation to recent work in xen-unstable
>

Quoted 7 lines

> There is a similarity between hvm_get_guest_time() in xen-unstable and
> read_64_main_counter()
> in this code. However, read_64_main_counter() is more tuned to the needs of
> hpet.c. It has no
> "set" operation, only the get. It isolates the mode,

physical or

Quoted 6 lines

simulated, in
> read_64_main_counter()
> itself. It uses no vcpu or domain state as it is a physical entity, in either
> mode. And it provides a real
> physical mode for every read for those applications

that desire

Quoted 5 lines

this.
>
> 7. Conclusion
>
> The virtual hpet is improved by this patch in terms

of accuracy and

Quoted 2 lines
Quoted 2 lines

monotonicity.
Tests performed to date verify this and more testing

is under way.

Quoted 12 lines
Quoted 4 lines

8. Future Work

Testing with Windows Vista will be performed soon. The reason

for accuracy
> variations
> on different platforms using the physical hpet device will be investigated.
> Additional overhead measurements on simulated vs physical hpet mode will be
> made.
>

Quoted 29 lines
Quoted 3 lines
Quoted 3 lines

Footnotes:

1. I don't recall the accuracy improvement with end

of interrupt

Quoted 23 lines

stamping, but
> it was
> significant, perhaps better than two to one improvement. It would be a very
> simple matter
> to re-measure the improvement as the facility can call back at injection time
> as well.
>
>
> Signed-off-by: Dave Winchell <[email protected]> > <mailto:[email protected]>
> Signed-off-by: Ben Guthro <[email protected]> > <mailto:[email protected]>
>
>
> _______________________________________________
> Xen-devel mailing list
> [email protected]
> http://lists.xensource.com/xen-devel

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

#22 lore raw
In reply to Dave Winchell

RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy> A disadvantage of the simulated mode is that it can return the same value

Quoted 1 line

for the counter in consecutive calls.

It also occurs to me that if the granularity is good enough, an easy fix to this problem might be to always increment the returned value by at least one. Then time is always at least increasing rather than stopped
-----Original Message-----
From: Dave Winchell [mailto:[email protected]]
Sent: Sunday, June 08, 2008 2:32 PM
To: [email protected]; Keir Fraser
Cc: Ben Guthro; xen-devel; Dave Winchell
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Hi Dan,

Quoted 7 lines

While I am fully supportive of offering hardware hpet as an option for hvm guests (let's call it hwhpet=1 for shorthand), I am very surprised by your preliminary results; the most obvious conclusion is that Xen system time is losing time at the rate of 1000 PPM though its possible there's a bug somewhere else in the "time stack". Your Windows result is jaw-dropping and inexplicable, though I have to admit ignorance of how Windows manages time.

I think xen system time is fine. You have to add the interrupt delivery policies decribed in the write-up for the patch to get accurate timekeeping in the guest.

The windows policy is obvious and results in a large improvement in accuracy. The Linux policy is more subtle, but is required to go from .1% to .03%.

Quoted 6 lines

I think with my recent patch and hpet=1 (essentially the same as your emulated hpet), hvm guest time should track Xen system time. I wonder if domain0 (which if I understand correctly is directly using Xen system time) is also seeing an error of .1%? Also I wonder for the skew you are seeing (in both hvm guests and domain0) is time moving too fast or two slow?

I don't recall the direction. I can look it up in my notes at work tomorrow.

Quoted 4 lines

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0.

Our patch is accurate to < .03% using the physical hpet mode or the simulated mode.

Quoted 5 lines

And if for some reason
Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

In our experience, Xen system time is accurate enough now.

Quoted 5 lines

One more thought... do you know the accuracy of the TSC crystals on your test systems? I posted a patch awhile ago that was intended to test that, though I guess it was only testing skew of different TSCs on the same system, not TSCs against an external time source.

I do not know the tsc accuracy.

Quoted 2 lines

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Regards,
Dave

-----Original Message-----
From: Dan Magenheimer [mailto:[email protected]]
Sent: Fri 6/6/2008 4:29 PM
To: Dave Winchell; Keir Fraser
Cc: Ben Guthro; xen-devel
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Dave --

Thanks much for posting the preliminary results!

While I am fully supportive of offering hardware hpet as an option for hvm guests (let's call it hwhpet=1 for shorthand), I am very surprised by your preliminary results; the most obvious conclusion is that Xen system time is losing time at the rate of 1000 PPM though its possible there's a bug somewhere else in the "time stack". Your Windows result is jaw-dropping and inexplicable, though I have to admit ignorance of how Windows manages time.

I think with my recent patch and hpet=1 (essentially the same as your emulated hpet), hvm guest time should track Xen system time. I wonder if domain0 (which if I understand correctly is directly using Xen system time) is also seeing an error of .1%? Also I wonder for the skew you are seeing (in both hvm guests and domain0) is time moving too fast or two slow?

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0. And if for some reason Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

One more thought... do you know the accuracy of the TSC crystals on your test systems? I posted a patch awhile ago that was intended to test that, though I guess it was only testing skew of different TSCs on the same system, not TSCs against an external time source.

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Thanks,
Dan

Quoted 510 lines

-----Original Message-----
From: Dave Winchell [mailto:[email protected]]
Sent: Friday, June 06, 2008 1:33 PM
To: [email protected]; Keir Fraser
Cc: Ben Guthro; xen-devel; Dave Winchell
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Dan, Keir:

Preliminary tests results indicate an error of .1% for Linux 64 bit guests configured
for hpet with xen-unstable as is. As we have discussed many times, the ntp requirement is .05%.
Tests on the patch we just submitted for hpet have indicated errors of .0012%
on this platform under similar test conditions and .03% on other platforms.

Windows vista64 has an error of 11% using hpet with the xen-unstable bits.
In an overnight test with our hpet patch, the Windows vista error was .008%.

The tests are with two or three guests on a physical node, all under load, and with
the ratio of vcpus to phys cpus > 1.

I will continue to run tests over the next few days.

thanks,
Dave

Quoted 3 lines · Dan Magenheimer

Hi Dave and Ben --

When running tests on xen-unstable (without your patch),

please ensure

Quoted 2 lines

that hpet=1 is set in the hvm config and also I think that when hpet is the clocksource on RHEL4-32, the clock IS resilient to

missed ticks

Quoted 1 line

so timer_mode should be 2 (vs when pit is the clocksource

on RHEL4-32,

Quoted 4 lines

all clock ticks must be delivered and so timer_mode should be 0).

Per

http://lists.xensource.com/archives/html/xen-devel/2008-06/msg
00098.html it's

Quoted 8 lines

my intent to clean this up, but I won't get to it until next week.

Thanks,
Dan

    -----Original Message-----
    *From:* [email protected]
    [mailto:[email protected]]*On

Behalf Of *Dave

Quoted 9 lines

Winchell
*Sent:* Friday, June 06, 2008 4:46 AM
*To:* Keir Fraser; Ben Guthro; xen-devel
*Cc:* [email protected]; Dave Winchell *Subject:* RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Keir,

I think the changes are required. We'll run some tests

today today so

Quoted 7 lines

that we have some data to talk about.

-Dave

-----Original Message-----
From: [email protected] on behalf

of Keir Fraser

Quoted 8 lines

Sent: Fri 6/6/2008 4:58 AM
To: Ben Guthro; xen-devel
Cc: [email protected]
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Are these patches needed now the timers are built on Xen system time rather
than host TSC? Dan has reported much better

time-keeping with his

Quoted 2 lines

patch
checked in, and it¹s for sure a lot less invasive than

this patchset.

Quoted 10 lines

     -- Keir

    On 5/6/08 15:59, "Ben Guthro" <[email protected]> wrote:

    >
    > 1. Introduction
    >
    > This patch improves the hpet based guest clock in

terms of drift and

Quoted 2 lines
Quoted 2 lines

monotonicity.
Prior to this work the drift with hpet was greater

than 2%, far

Quoted 10 lines

above the .05%
> limit
> for ntp to synchronize. With this code, the drift ranges from .001% to .0033%
> depending
> on guest and physical platform.
>
> Using hpet allows guest operating systems to provide monotonic time to their
> applications. Time sources other than hpet are not

monotonic because

Quoted 1 line
Quoted 1 line

of their reliance on tsc, which is not synchronized

across physical

Quoted 31 lines
Quoted 3 lines

processors.

Windows 2k864 and many Linux guests are supported with two

policies, one for
> guests
> that handle missed clock interrupts and the other for guests that require the
> correct number of interrupts.
>
> Guests may use hpet for the timing source even if the physical platform has no
> visible
> hpet. Migration is supported between physical machines which differ in
> physical
> hpet visibility.
>
> Most of the changes are in hpet.c. Two general facilities are added to track
> interrupt
> progress. The ideas here and the facilities would be useful in vpt.c, for
> other time
> sources, though no attempt is made here to improve vpt.c. >
> The following sections discuss hpet dependencies, interrupt delivery policies,
> live migration,
> test results, and relation to recent work with monotonic time. >
>

Quoted 3 lines

> 2. Virtual Hpet dependencies
>
> The virtual hpet depends on the ability to read the

physical or

Quoted 2 lines

simulated
> (see discussion below) hpet. For timekeeping, the

virtual hpet

Quoted 32 lines

also depends
> on two new interrupt notification facilities to implement its policies for
> interrupt delivery.
>
> 2.1. Two modes of low-level hpet main counter reads. >
> In this implementation, the virtual hpet reads with read_64_main_counter(),
> exported by
> time.c, either the real physical hpet main counter register directly or a
> "simulated"
> hpet main counter.
>
> The simulated mode uses a monotonic version of get_s_time() (NOW()), where the
> last
> time value is returned whenever the current time value is less than the last
> time
> value. In simulated mode, since it is layered on s_time, the underlying
> hardware
> can be hpet or some other device. The frequency of the main counter in
> simulated
> mode is the same as the standard physical hpet frequency, allowing live
> migration
> between nodes that are configured differently.
>

Quoted 1 line

> If the physical platform does not have an hpet

device, or if xen

Quoted 9 lines

is configured
> not
> to use the device, then the simulated method is used. If there is a physical
> hpet device,
> and xen has initialized it, then either simulated or physical mode can be
> used.
> This is governed by a boot time option, hpet-avoid.

Setting this

Quoted 3 lines

option to 1
> gives the
> simulated mode and 0 the physical mode. The default

is physical

Quoted 5 lines

mode.
>
> A disadvantage of the physical mode is that may take longer to read the device
> than in simulated mode. On some platforms the cost is

about the

Quoted 15 lines

same (less
> than 250 nsec) for
> physical and simulated modes, while on others physical cost is much higher
> than simulated.
> A disadvantage of the simulated mode is that it can return the same value
> for the counter in consecutive calls.
>
> 2.2. Interrupt notification facilities.
>
> Two interrupt notification facilities are introduced, one is > hvm_isa_irq_assert_cb()
> and the other hvm_register_intr_en_notif().
>

Quoted 5 lines

> The vhpet uses hvm_isa_irq_assert_cb to deliver interrupts to the vioapic.
> hvm_isa_irq_assert_cb allows a callback to be passed along to > vioapic_deliver()
> and this callback is called with a mask of the vcpus

which will

Quoted 4 lines

get the
> interrupt. This callback is made before any vcpus receive an interrupt.
>

Quoted 8 lines

> Vhpet uses hvm_register_intr_en_notif() to register a handler for a particular
> vector that will be called when that vector is injected in > [vmx,svm]_intr_assist()
> and also when the guest finishes handling the interrupt. Here finished is
> defined
> as the point when the guest re-enables interrupts or

lowers the

Quoted 4 lines

tpr value.
> EOI is not used as the end of interrupt as this is sometimes returned before
> the interrupt handler has done its work. A flag is

passed to the

Quoted 3 lines

handler
> indicating
> whether this is the injection point (post = 1) or the

interrupt

Quoted 6 lines

finished (post
> = 0) point.
> The need for the finished point callback is discussed in the missed ticks
> policy section.
>

Quoted 7 lines

> To prevent a possible early trigger of the finished callback, intr_en_notif
> logic
> has a two stage arm, the first at injection (hvm_intr_en_notif_arm()) and the
> second when
> interrupts are seen to be disabled

(hvm_intr_en_notif_disarm()).

Quoted 3 lines

Once fully
> armed, re-enabling
> interrupts will cause hvm_intr_en_notif_disarm() to

make the end

Quoted 2 lines

of interrupt
> callback. hvm_intr_en_notif_arm() and

hvm_intr_en_notif_disarm()

Quoted 6 lines

are called by
> [vmx,svm]_intr_assist().
>
> 3. Interrupt delivery policies
>
> The existing hpet interrupt delivery is preserved.

This includes

Quoted 8 lines
Quoted 1 line

vcpu round robin delivery used by Linux and broadcast delivery

used by
> Windows.
>
> There are two policies for interrupt delivery, one for Windows 2k8-64 and the
> other
> for Linux. The Linux policy takes advantage of the

(guest) Linux

Quoted 3 lines

missed tick
> and offset
> calculations and does not attempt to deliver the

right number of

Quoted 10 lines

interrupts.
> The Windows policy delivers the correct number of interrupts, even if
> sometimes much
> closer to each other than the period. The policies are similar to those in
> vpt.c, though
> there are some important differences.
>
> Policies are selected with an HVMOP_set_param

hypercall with index

Quoted 2 lines
Quoted 2 lines

HVM_PARAM_TIMER_MODE.
Two new values are added,

HVM_HPET_guest_computes_missed_ticks and

Quoted 7 lines
Quoted 1 line

HVM_HPET_guest_does_not_compute_missed_ticks. The reason that

two new ones
> are added is that
> in some guests (32bit Linux) a no-missed policy is needed for clock sources
> other than hpet
> and a missed ticks policy for hpet. It was felt that

there would

Quoted 14 lines

be less
> confusion by simply
> introducing the two hpet policies.
>
> 3.1. The missed ticks policy
>
> The Linux clock interrupt handler for hpet calculates missed ticks and offset
> using the hpet
> main counter. The algorithm works well when the time since the last interrupt
> is greater than
> or equal to a period and poorly otherwise.
>

Quoted 1 line

> The missed ticks policy ensures that no two clock

interrupts are

Quoted 6 lines

delivered to
> the guest at
> a time interval less than a period. A time stamp (hpet main counter value) is
> recorded (by a
> callback registered with hvm_register_intr_en_notif)

when Linux

Quoted 3 lines

finishes
> handling the clock
> interrupt. Then, ensuing interrupts are delivered to

the vioapic

Quoted 9 lines

only if the
> current main
> counter value is a period greater than when the last interrupt was handled.
>
> Tests showed a significant improvement in clock drift with end of interrupt
> time stamps
> versus beginning of interrupt[1]. It is believed that

the reason

Quoted 3 lines

for the
> improvement
> is that the clock interrupt handler goes for a

spinlock and can

Quoted 11 lines

be therefore
> delayed in its
> processing. Furthermore, the main counter is read by the guest under the lock.
> The net
> effect is that if we time stamp injection, we can get the difference in time
> between successive interrupt handler lock acquisitions to be less than the
> period.
>

Quoted 3 lines

> 3.2. The no-missed ticks policy
>
> Windows 2k864 keeps very poor time with the missed

ticks policy.

Quoted 6 lines

So the
> no-missed ticks policy
> was developed. In the no-missed ticks policy we deliver the correct number of
> interrupts,
> even if they are spaced less than a period apart

(when catching up).

Quoted 50 lines
Quoted 2 lines

Windows 2k864 uses a broadcast mode in the interrupt routing

such that
> all vcpus get the clock interrupt. The best Windows drift performance was
> achieved when the
> policy code ensured that all the previous interrupts (on the various vcpus)
> had been injected
> before injecting the next interrupt to the vioapic.. >
> The policy code works as follows. It uses the hvm_isa_irq_assert_cb() to
> record
> the vcpus to be interrupted in h->hpet.pending_mask. Then, in the callback
> registered
> with hvm_register_intr_en_notif() at post=1 time it clears the current vcpu in
> the pending_mask.
> When the pending_mask is clear it decrements hpet.intr_pending_nr and if
> intr_pending_nr is still
> non-zero posts another interrupt to the ioapic with hvm_isa_irq_assert_cb().
> Intr_pending_nr is incremented in hpet_route_decision_not_missed_ticks().
>
> The missed ticks policy intr_en_notif callback also uses the pending_mask
> method. So even though
> Linux does not broadcast its interrupts, the code could handle it if it did.
> In this case the end of interrupt time stamp is made when the pending_mask is
> clear.
>
> 4. Live Migration
>
> Live migration with hpet preserves the current offset of the guest clock with
> respect
> to ntp. This is accomplished by migrating all of the state in the h->hpet data
> structure
> in the usual way. The hp->mc_offset is recalculated on the receiving node so
> that the
> guest sees a continuous hpet main counter.
>

Quoted 6 lines

> Code as been added to xc_domain_save.c to send a small message after the
> domain context is sent. The contents of the message is the physical tsc
> timestamp, last_tsc,
> read just before the message is sent. When the

last_tsc message

Quoted 39 lines

is received in
> xc_domain_restore.c,
> another physical tsc timestamp, cur_tsc, is read. The two timestamps are
> loaded into the domain
> structure as last_tsc_sender and first_tsc_receiver with hypercalls. Then
> xc_domain_hvm_setcontext
> is called so that hpet_load has access to these time stamps. Hpet_load uses
> the timestamps
> to account for the time spent saving and loading the domain context. With this
> technique,
> the only neglected time is the time spent sending a small network message.
>
> 5. Test Results
>
> Some recent test results are:
>
> 5.1 Linux 4u664 and Windows 2k864 load test.
> Duration: 70 hours.
> Test date: 6/2/08
> Loads: usex -b48 on Linux; burn-in on Windows
> Guest vcpus: 8 for Linux; 2 for Windows
> Hardware: 8 physical cpu AMD
> Clock drift : Linux: .0012% Windows: .009%
>
> 5.2 Linux 4u664, Linux 4u464 , and Windows 2k864 no-load test
> Duration: 23 hours.
> Test date: 6/3/08
> Loads: none
> Guest vcpus: 8 for each Linux; 2 for Windows
> Hardware: 4 physical cpu AMD
> Clock drift : Linux: .033% Windows: .019%
>
> 6. Relation to recent work in xen-unstable
>

Quoted 7 lines

> There is a similarity between hvm_get_guest_time() in xen-unstable and
> read_64_main_counter()
> in this code. However, read_64_main_counter() is more tuned to the needs of
> hpet.c. It has no
> "set" operation, only the get. It isolates the mode,

physical or

Quoted 6 lines

simulated, in
> read_64_main_counter()
> itself. It uses no vcpu or domain state as it is a physical entity, in either
> mode. And it provides a real
> physical mode for every read for those applications

that desire

Quoted 5 lines

this.
>
> 7. Conclusion
>
> The virtual hpet is improved by this patch in terms

of accuracy and

Quoted 2 lines
Quoted 2 lines

monotonicity.
Tests performed to date verify this and more testing

is under way.

Quoted 12 lines
Quoted 4 lines

8. Future Work

Testing with Windows Vista will be performed soon. The reason

for accuracy
> variations
> on different platforms using the physical hpet device will be investigated.
> Additional overhead measurements on simulated vs physical hpet mode will be
> made.
>

Quoted 29 lines
Quoted 3 lines
Quoted 3 lines

Footnotes:

1. I don't recall the accuracy improvement with end

of interrupt

Quoted 23 lines

stamping, but
> it was
> significant, perhaps better than two to one improvement. It would be a very
> simple matter
> to re-measure the improvement as the facility can call back at injection time
> as well.
>
>
> Signed-off-by: Dave Winchell <[email protected]> > <mailto:[email protected]>
> Signed-off-by: Ben Guthro <[email protected]> > <mailto:[email protected]>
>
>
> _______________________________________________
> Xen-devel mailing list
> [email protected]
> http://lists.xensource.com/xen-devel

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

#13 lore raw
In reply to Dan Magenheimer

Hi Dan,

Quoted 3 lines

Could you please be very precise, when you say "Linux", as to what you are (and are not) testing? Specifically:
1) kernel version number and/or distro info

I personally have been testing with Linux red hat 4u4, 4u5 and 4u6 64 bit and red hat 4u4 32 bit. I've also tested Windows 2k8sp0 64bit and Vista sp1 64 bit. Our QA group is currently testing a wider set of guests.

Quoted 1 line

2) 32 vs 64

both.

Quoted 1 line

3) kernel parameters specified

I'm pretty sloppy here. Frequently I have clock=pit because our build process defaults to that and I know that the guests I use ignore clock= when hpet is in the acpi table. However, I don't recommend that others do this.
I check that Linux is using hpet in its boot log and I have statistics in the patch which tell me that hpet is being used. You've done a lot of investigation on clock= and clocksource= so I would like to hear your recommendation.

Quoted 1 line

4) config file parameters

Hpet enabled. Timer_mode set to HVM_HPET_guest_computes_missed_ticks for all Linux guests and to HVM_HPET_guest_does_not_compute_missed_ticks for Windows 2k8-64 and Vista 64.
8 vcpus for Linux and 2 for Windows.

Quoted 2 lines

5) relevant CPU info that may be passed through by Xen to hvm guests (e.g. whether "tsc is synchronized")

Whatever xen-unstable does. I have not changed it.

Quoted 1 line

6) relevant xen boot parameters (if any)

Nothing relevant.

Quoted 11 lines

As we've seen, different combinations of the above can yield very different test results. We'd like to confirm your tests, but if we can avoid unnecessary additional iterations (due to mismatches on the above), that would be helpful.

Our testing goal is to ensure that there is at least one known good combination of parameters for each of RHEL3, RHEL4, and RHEL5 (both 32 and 64) and that works on both tsc-synchronized and tsc-unsynchronized Intel and AMD boxes. And hopefully that works with and without a real physical hpet available.

Thanks very much for testing this.

Quoted 3 lines

We don't have a good test environment for Windows time, but if you can provide the same test configuration detail, we may be able to do some testing.

The configuration information was given above.

Thanks,
Dave

-----Original Message-----
From: Dan Magenheimer [mailto:[email protected]]
Sent: Sun 6/8/2008 5:10 PM
To: Dave Winchell; Keir Fraser
Cc: Ben Guthro; xen-devel
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracyHi Dave --

Thanks for the additional explanation.

Could you please be very precise, when you say "Linux", as to what you are (and are not) testing? Specifically:
1) kernel version number and/or distro info
2) 32 vs 64
3) kernel parameters specified
4) config file parameters
5) relevant CPU info that may be passed through by Xen to hvm guests (e.g. whether "tsc is synchronized")
6) relevant xen boot parameters (if any)

As we've seen, different combinations of the above can yield very different test results. We'd like to confirm your tests, but if we can avoid unnecessary additional iterations (due to mismatches on the above), that would be helpful.

Our testing goal is to ensure that there is at least one known good combination of parameters for each of RHEL3, RHEL4, and RHEL5 (both 32 and 64) and that works on both tsc-synchronized and tsc-unsynchronized Intel and AMD boxes. And hopefully that works with and without a real physical hpet available.

We don't have a good test environment for Windows time, but if you can provide the same test configuration detail, we may be able to do some testing.

Thanks,
Dan

-----Original Message-----
From: Dave Winchell [mailto:[email protected]]
Sent: Sunday, June 08, 2008 2:32 PM
To: [email protected]; Keir Fraser
Cc: Ben Guthro; xen-devel; Dave Winchell
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Hi Dan,

Quoted 7 lines

While I am fully supportive of offering hardware hpet as an option for hvm guests (let's call it hwhpet=1 for shorthand), I am very surprised by your preliminary results; the most obvious conclusion is that Xen system time is losing time at the rate of 1000 PPM though its possible there's a bug somewhere else in the "time stack". Your Windows result is jaw-dropping and inexplicable, though I have to admit ignorance of how Windows manages time.

I think xen system time is fine. You have to add the interrupt delivery policies decribed in the write-up for the patch to get accurate timekeeping in the guest.

The windows policy is obvious and results in a large improvement in accuracy. The Linux policy is more subtle, but is required to go from .1% to .03%.

Quoted 6 lines

I think with my recent patch and hpet=1 (essentially the same as your emulated hpet), hvm guest time should track Xen system time. I wonder if domain0 (which if I understand correctly is directly using Xen system time) is also seeing an error of .1%? Also I wonder for the skew you are seeing (in both hvm guests and domain0) is time moving too fast or two slow?

I don't recall the direction. I can look it up in my notes at work tomorrow.

Quoted 4 lines

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0.

Our patch is accurate to < .03% using the physical hpet mode or the simulated mode.

Quoted 5 lines

And if for some reason
Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

In our experience, Xen system time is accurate enough now.

Quoted 5 lines

One more thought... do you know the accuracy of the TSC crystals on your test systems? I posted a patch awhile ago that was intended to test that, though I guess it was only testing skew of different TSCs on the same system, not TSCs against an external time source.

I do not know the tsc accuracy.

Quoted 2 lines

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Regards,
Dave

-----Original Message-----
From: Dan Magenheimer [mailto:[email protected]]
Sent: Fri 6/6/2008 4:29 PM
To: Dave Winchell; Keir Fraser
Cc: Ben Guthro; xen-devel
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Dave --

Thanks much for posting the preliminary results!

While I am fully supportive of offering hardware hpet as an option for hvm guests (let's call it hwhpet=1 for shorthand), I am very surprised by your preliminary results; the most obvious conclusion is that Xen system time is losing time at the rate of 1000 PPM though its possible there's a bug somewhere else in the "time stack". Your Windows result is jaw-dropping and inexplicable, though I have to admit ignorance of how Windows manages time.

I think with my recent patch and hpet=1 (essentially the same as your emulated hpet), hvm guest time should track Xen system time. I wonder if domain0 (which if I understand correctly is directly using Xen system time) is also seeing an error of .1%? Also I wonder for the skew you are seeing (in both hvm guests and domain0) is time moving too fast or two slow?

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0. And if for some reason Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

One more thought... do you know the accuracy of the TSC crystals on your test systems? I posted a patch awhile ago that was intended to test that, though I guess it was only testing skew of different TSCs on the same system, not TSCs against an external time source.

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Thanks,
Dan

Quoted 510 lines

-----Original Message-----
From: Dave Winchell [mailto:[email protected]]
Sent: Friday, June 06, 2008 1:33 PM
To: [email protected]; Keir Fraser
Cc: Ben Guthro; xen-devel; Dave Winchell
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Dan, Keir:

Preliminary tests results indicate an error of .1% for Linux 64 bit guests configured
for hpet with xen-unstable as is. As we have discussed many times, the ntp requirement is .05%.
Tests on the patch we just submitted for hpet have indicated errors of .0012%
on this platform under similar test conditions and .03% on other platforms.

Windows vista64 has an error of 11% using hpet with the xen-unstable bits.
In an overnight test with our hpet patch, the Windows vista error was .008%.

The tests are with two or three guests on a physical node, all under load, and with
the ratio of vcpus to phys cpus > 1.

I will continue to run tests over the next few days.

thanks,
Dave

Quoted 3 lines · Dan Magenheimer

Hi Dave and Ben --

When running tests on xen-unstable (without your patch),

please ensure

Quoted 2 lines

that hpet=1 is set in the hvm config and also I think that when hpet is the clocksource on RHEL4-32, the clock IS resilient to

missed ticks

Quoted 1 line

so timer_mode should be 2 (vs when pit is the clocksource

on RHEL4-32,

Quoted 4 lines

all clock ticks must be delivered and so timer_mode should be 0).

Per

http://lists.xensource.com/archives/html/xen-devel/2008-06/msg
00098.html it's

Quoted 8 lines

my intent to clean this up, but I won't get to it until next week.

Thanks,
Dan

    -----Original Message-----
    *From:* [email protected]
    [mailto:[email protected]]*On

Behalf Of *Dave

Quoted 9 lines

Winchell
*Sent:* Friday, June 06, 2008 4:46 AM
*To:* Keir Fraser; Ben Guthro; xen-devel
*Cc:* [email protected]; Dave Winchell *Subject:* RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Keir,

I think the changes are required. We'll run some tests

today today so

Quoted 7 lines

that we have some data to talk about.

-Dave

-----Original Message-----
From: [email protected] on behalf

of Keir Fraser

Quoted 8 lines

Sent: Fri 6/6/2008 4:58 AM
To: Ben Guthro; xen-devel
Cc: [email protected]
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Are these patches needed now the timers are built on Xen system time rather
than host TSC? Dan has reported much better

time-keeping with his

Quoted 2 lines

patch
checked in, and it¹s for sure a lot less invasive than

this patchset.

Quoted 10 lines

     -- Keir

    On 5/6/08 15:59, "Ben Guthro" <[email protected]> wrote:

    >
    > 1. Introduction
    >
    > This patch improves the hpet based guest clock in

terms of drift and

Quoted 2 lines
Quoted 2 lines

monotonicity.
Prior to this work the drift with hpet was greater

than 2%, far

Quoted 10 lines

above the .05%
> limit
> for ntp to synchronize. With this code, the drift ranges from .001% to .0033%
> depending
> on guest and physical platform.
>
> Using hpet allows guest operating systems to provide monotonic time to their
> applications. Time sources other than hpet are not

monotonic because

Quoted 1 line
Quoted 1 line

of their reliance on tsc, which is not synchronized

across physical

Quoted 31 lines
Quoted 3 lines

processors.

Windows 2k864 and many Linux guests are supported with two

policies, one for
> guests
> that handle missed clock interrupts and the other for guests that require the
> correct number of interrupts.
>
> Guests may use hpet for the timing source even if the physical platform has no
> visible
> hpet. Migration is supported between physical machines which differ in
> physical
> hpet visibility.
>
> Most of the changes are in hpet.c. Two general facilities are added to track
> interrupt
> progress. The ideas here and the facilities would be useful in vpt.c, for
> other time
> sources, though no attempt is made here to improve vpt.c. >
> The following sections discuss hpet dependencies, interrupt delivery policies,
> live migration,
> test results, and relation to recent work with monotonic time. >
>

Quoted 3 lines

> 2. Virtual Hpet dependencies
>
> The virtual hpet depends on the ability to read the

physical or

Quoted 2 lines

simulated
> (see discussion below) hpet. For timekeeping, the

virtual hpet

Quoted 32 lines

also depends
> on two new interrupt notification facilities to implement its policies for
> interrupt delivery.
>
> 2.1. Two modes of low-level hpet main counter reads. >
> In this implementation, the virtual hpet reads with read_64_main_counter(),
> exported by
> time.c, either the real physical hpet main counter register directly or a
> "simulated"
> hpet main counter.
>
> The simulated mode uses a monotonic version of get_s_time() (NOW()), where the
> last
> time value is returned whenever the current time value is less than the last
> time
> value. In simulated mode, since it is layered on s_time, the underlying
> hardware
> can be hpet or some other device. The frequency of the main counter in
> simulated
> mode is the same as the standard physical hpet frequency, allowing live
> migration
> between nodes that are configured differently.
>

Quoted 1 line

> If the physical platform does not have an hpet

device, or if xen

Quoted 9 lines

is configured
> not
> to use the device, then the simulated method is used. If there is a physical
> hpet device,
> and xen has initialized it, then either simulated or physical mode can be
> used.
> This is governed by a boot time option, hpet-avoid.

Setting this

Quoted 3 lines

option to 1
> gives the
> simulated mode and 0 the physical mode. The default

is physical

Quoted 5 lines

mode.
>
> A disadvantage of the physical mode is that may take longer to read the device
> than in simulated mode. On some platforms the cost is

about the

Quoted 15 lines

same (less
> than 250 nsec) for
> physical and simulated modes, while on others physical cost is much higher
> than simulated.
> A disadvantage of the simulated mode is that it can return the same value
> for the counter in consecutive calls.
>
> 2.2. Interrupt notification facilities.
>
> Two interrupt notification facilities are introduced, one is > hvm_isa_irq_assert_cb()
> and the other hvm_register_intr_en_notif().
>

Quoted 5 lines

> The vhpet uses hvm_isa_irq_assert_cb to deliver interrupts to the vioapic.
> hvm_isa_irq_assert_cb allows a callback to be passed along to > vioapic_deliver()
> and this callback is called with a mask of the vcpus

which will

Quoted 4 lines

get the
> interrupt. This callback is made before any vcpus receive an interrupt.
>

Quoted 8 lines

> Vhpet uses hvm_register_intr_en_notif() to register a handler for a particular
> vector that will be called when that vector is injected in > [vmx,svm]_intr_assist()
> and also when the guest finishes handling the interrupt. Here finished is
> defined
> as the point when the guest re-enables interrupts or

lowers the

Quoted 4 lines

tpr value.
> EOI is not used as the end of interrupt as this is sometimes returned before
> the interrupt handler has done its work. A flag is

passed to the

Quoted 3 lines

handler
> indicating
> whether this is the injection point (post = 1) or the

interrupt

Quoted 6 lines

finished (post
> = 0) point.
> The need for the finished point callback is discussed in the missed ticks
> policy section.
>

Quoted 7 lines

> To prevent a possible early trigger of the finished callback, intr_en_notif
> logic
> has a two stage arm, the first at injection (hvm_intr_en_notif_arm()) and the
> second when
> interrupts are seen to be disabled

(hvm_intr_en_notif_disarm()).

Quoted 3 lines

Once fully
> armed, re-enabling
> interrupts will cause hvm_intr_en_notif_disarm() to

make the end

Quoted 2 lines

of interrupt
> callback. hvm_intr_en_notif_arm() and

hvm_intr_en_notif_disarm()

Quoted 6 lines

are called by
> [vmx,svm]_intr_assist().
>
> 3. Interrupt delivery policies
>
> The existing hpet interrupt delivery is preserved.

This includes

Quoted 8 lines
Quoted 1 line

vcpu round robin delivery used by Linux and broadcast delivery

used by
> Windows.
>
> There are two policies for interrupt delivery, one for Windows 2k8-64 and the
> other
> for Linux. The Linux policy takes advantage of the

(guest) Linux

Quoted 3 lines

missed tick
> and offset
> calculations and does not attempt to deliver the

right number of

Quoted 10 lines

interrupts.
> The Windows policy delivers the correct number of interrupts, even if
> sometimes much
> closer to each other than the period. The policies are similar to those in
> vpt.c, though
> there are some important differences.
>
> Policies are selected with an HVMOP_set_param

hypercall with index

Quoted 2 lines
Quoted 2 lines

HVM_PARAM_TIMER_MODE.
Two new values are added,

HVM_HPET_guest_computes_missed_ticks and

Quoted 7 lines
Quoted 1 line

HVM_HPET_guest_does_not_compute_missed_ticks. The reason that

two new ones
> are added is that
> in some guests (32bit Linux) a no-missed policy is needed for clock sources
> other than hpet
> and a missed ticks policy for hpet. It was felt that

there would

Quoted 14 lines

be less
> confusion by simply
> introducing the two hpet policies.
>
> 3.1. The missed ticks policy
>
> The Linux clock interrupt handler for hpet calculates missed ticks and offset
> using the hpet
> main counter. The algorithm works well when the time since the last interrupt
> is greater than
> or equal to a period and poorly otherwise.
>

Quoted 1 line

> The missed ticks policy ensures that no two clock

interrupts are

Quoted 6 lines

delivered to
> the guest at
> a time interval less than a period. A time stamp (hpet main counter value) is
> recorded (by a
> callback registered with hvm_register_intr_en_notif)

when Linux

Quoted 3 lines

finishes
> handling the clock
> interrupt. Then, ensuing interrupts are delivered to

the vioapic

Quoted 9 lines

only if the
> current main
> counter value is a period greater than when the last interrupt was handled.
>
> Tests showed a significant improvement in clock drift with end of interrupt
> time stamps
> versus beginning of interrupt[1]. It is believed that

the reason

Quoted 3 lines

for the
> improvement
> is that the clock interrupt handler goes for a

spinlock and can

Quoted 11 lines

be therefore
> delayed in its
> processing. Furthermore, the main counter is read by the guest under the lock.
> The net
> effect is that if we time stamp injection, we can get the difference in time
> between successive interrupt handler lock acquisitions to be less than the
> period.
>

Quoted 3 lines

> 3.2. The no-missed ticks policy
>
> Windows 2k864 keeps very poor time with the missed

ticks policy.

Quoted 6 lines

So the
> no-missed ticks policy
> was developed. In the no-missed ticks policy we deliver the correct number of
> interrupts,
> even if they are spaced less than a period apart

(when catching up).

Quoted 50 lines
Quoted 2 lines

Windows 2k864 uses a broadcast mode in the interrupt routing

such that
> all vcpus get the clock interrupt. The best Windows drift performance was
> achieved when the
> policy code ensured that all the previous interrupts (on the various vcpus)
> had been injected
> before injecting the next interrupt to the vioapic.. >
> The policy code works as follows. It uses the hvm_isa_irq_assert_cb() to
> record
> the vcpus to be interrupted in h->hpet.pending_mask. Then, in the callback
> registered
> with hvm_register_intr_en_notif() at post=1 time it clears the current vcpu in
> the pending_mask.
> When the pending_mask is clear it decrements hpet.intr_pending_nr and if
> intr_pending_nr is still
> non-zero posts another interrupt to the ioapic with hvm_isa_irq_assert_cb().
> Intr_pending_nr is incremented in hpet_route_decision_not_missed_ticks().
>
> The missed ticks policy intr_en_notif callback also uses the pending_mask
> method. So even though
> Linux does not broadcast its interrupts, the code could handle it if it did.
> In this case the end of interrupt time stamp is made when the pending_mask is
> clear.
>
> 4. Live Migration
>
> Live migration with hpet preserves the current offset of the guest clock with
> respect
> to ntp. This is accomplished by migrating all of the state in the h->hpet data
> structure
> in the usual way. The hp->mc_offset is recalculated on the receiving node so
> that the
> guest sees a continuous hpet main counter.
>

Quoted 6 lines

> Code as been added to xc_domain_save.c to send a small message after the
> domain context is sent. The contents of the message is the physical tsc
> timestamp, last_tsc,
> read just before the message is sent. When the

last_tsc message

Quoted 39 lines

is received in
> xc_domain_restore.c,
> another physical tsc timestamp, cur_tsc, is read. The two timestamps are
> loaded into the domain
> structure as last_tsc_sender and first_tsc_receiver with hypercalls. Then
> xc_domain_hvm_setcontext
> is called so that hpet_load has access to these time stamps. Hpet_load uses
> the timestamps
> to account for the time spent saving and loading the domain context. With this
> technique,
> the only neglected time is the time spent sending a small network message.
>
> 5. Test Results
>
> Some recent test results are:
>
> 5.1 Linux 4u664 and Windows 2k864 load test.
> Duration: 70 hours.
> Test date: 6/2/08
> Loads: usex -b48 on Linux; burn-in on Windows
> Guest vcpus: 8 for Linux; 2 for Windows
> Hardware: 8 physical cpu AMD
> Clock drift : Linux: .0012% Windows: .009%
>
> 5.2 Linux 4u664, Linux 4u464 , and Windows 2k864 no-load test
> Duration: 23 hours.
> Test date: 6/3/08
> Loads: none
> Guest vcpus: 8 for each Linux; 2 for Windows
> Hardware: 4 physical cpu AMD
> Clock drift : Linux: .033% Windows: .019%
>
> 6. Relation to recent work in xen-unstable
>

Quoted 7 lines

> There is a similarity between hvm_get_guest_time() in xen-unstable and
> read_64_main_counter()
> in this code. However, read_64_main_counter() is more tuned to the needs of
> hpet.c. It has no
> "set" operation, only the get. It isolates the mode,

physical or

Quoted 6 lines

simulated, in
> read_64_main_counter()
> itself. It uses no vcpu or domain state as it is a physical entity, in either
> mode. And it provides a real
> physical mode for every read for those applications

that desire

Quoted 5 lines

this.
>
> 7. Conclusion
>
> The virtual hpet is improved by this patch in terms

of accuracy and

Quoted 2 lines
Quoted 2 lines

monotonicity.
Tests performed to date verify this and more testing

is under way.

Quoted 12 lines
Quoted 4 lines

8. Future Work

Testing with Windows Vista will be performed soon. The reason

for accuracy
> variations
> on different platforms using the physical hpet device will be investigated.
> Additional overhead measurements on simulated vs physical hpet mode will be
> made.
>

Quoted 29 lines
Quoted 3 lines
Quoted 3 lines

Footnotes:

1. I don't recall the accuracy improvement with end

of interrupt

Quoted 23 lines

stamping, but
> it was
> significant, perhaps better than two to one improvement. It would be a very
> simple matter
> to re-measure the improvement as the facility can call back at injection time
> as well.
>
>
> Signed-off-by: Dave Winchell <[email protected]> > <mailto:[email protected]>
> Signed-off-by: Ben Guthro <[email protected]> > <mailto:[email protected]>
>
>
> _______________________________________________
> Xen-devel mailing list
> [email protected]
> http://lists.xensource.com/xen-devel

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

#14 lore raw
Quoted 6 lines · Dave Winchell
Quoted 1 line
Quoted 1 line

4) config file parameters

Hpet enabled. Timer_mode set to HVM_HPET_guest_computes_missed_ticks for all Linux guests and to HVM_HPET_guest_does_not_compute_missed_ticks for Windows 2k8-64 and Vista 64.
8 vcpus for Linux and 2 for Windows.

These new HVM_HPET options seems a weird design choice. It appears that you can only set these or one of the old options, so there¹s not actually any independence between the mode used by vpt.c and the mode used by hpet.c. At guest install time you ought to be able to tell whether the guest will use hpet or not based on its version (RHELx, SLESy, Winz etc etc) and decide whether missed-ticks accounting is required or not.

I¹d be more agreeable to a partch that stripped out the physical hpet accesses (since you say they are not the reason for the improvement in accuracy), built hpet on top of vpt, and added the necessary extra mechanisms to deal with interrupt broadcasts into vpt.c. And which was split into more separate pieces of mechanism.

-- Keir

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

#15 lore raw
Quoted 6 lines

These new HVM_HPET options seems a weird design choice. It appears that you can only set these or one of the old options, so there¹s not actually any independence between the mode used by vpt.c and the mode used by hpet.c. At guest install time you ought to be able to tell whether the guest will use hpet or not based on its version (RHELx, SLESy, Winz etc etc) and decide whether missed-ticks accounting is required or not.

I'll use the existing options instead.

Quoted 5 lines

I¹d be more agreeable to a partch that stripped out the physical hpet accesses (since you say they are not the reason for the improvement in accuracy), built hpet on top of vpt, and added the necessary extra mechanisms to deal with interrupt broadcasts into vpt.c. And which was split into more separate pieces of mechanism.

ok, I'll work on this. How much time do I have to make the release you are working on?

thanks,
Dave

-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Mon 6/9/2008 3:36 AM
To: Dave Winchell; [email protected]
Cc: Ben Guthro; xen-devel
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 6 lines · Dave Winchell
Quoted 1 line
Quoted 1 line

4) config file parameters

Hpet enabled. Timer_mode set to HVM_HPET_guest_computes_missed_ticks for all Linux guests and to HVM_HPET_guest_does_not_compute_missed_ticks for Windows 2k8-64 and Vista 64.
8 vcpus for Linux and 2 for Windows.

These new HVM_HPET options seems a weird design choice. It appears that you can only set these or one of the old options, so there¹s not actually any independence between the mode used by vpt.c and the mode used by hpet.c. At guest install time you ought to be able to tell whether the guest will use hpet or not based on its version (RHELx, SLESy, Winz etc etc) and decide whether missed-ticks accounting is required or not.

I¹d be more agreeable to a partch that stripped out the physical hpet accesses (since you say they are not the reason for the improvement in accuracy), built hpet on top of vpt, and added the necessary extra mechanisms to deal with interrupt broadcasts into vpt.c. And which was split into more separate pieces of mechanism.

-- Keir

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

#16 lore raw
Quoted 9 lines · Dave Winchell
Quoted 6 lines
Quoted 4 lines

I¹d be more agreeable to a partch that stripped out the physical hpet accesses (since you say they are not the reason for the improvement in accuracy), built hpet on top of vpt, and added the necessary extra mechanisms to deal with interrupt broadcasts into vpt.c. And which was

split

Quoted 1 line

into more separate pieces of mechanism.

ok, I'll work on this. How much time do I have to make the release you are working on?

I¹m thinking feature freeze at the end of the month.

-- Keir

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

#17 lore raw
Quoted 4 lines · Keir Fraser
Quoted 2 lines

ok, I'll work on this. How much time do I have to make the release you are working on?

I¹m thinking feature freeze at the end of the month.

Oh, while I¹m commenting on the current version of the patch, I will point out that changes to the save/restore format need careful thought. At the very least we¹d like to be backward compatible (old images restore on new Xen) even if we don¹t achieve compatibility the other way round. I didn¹t really look too closely at this aspect of the patches so perhaps these patches are fine in this regard. Either way I think the core re-architecting of the hpet device model to handle missed ticks and so on is independent of interface changes anyway (apart from reasonable extensions to the timer_mode option). Changes to save/restore format, addition of extra debugging code, and peripheral things like that it¹d be nice to have in separate patches. It makes the core stuff easier to review and more likely to get accepted without quibble (because there¹s less of it, and it is all dedicated to a single purpose which I think we all agree is where we want to be).

Thanks,
Keir

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

#18 lore raw
Quoted 11 lines · Keir Fraser

On 9/6/08 13:03, "Keir Fraser" <[email protected]> wrote:

ok, I'll work on this. How much time do I have to make the release you are
working on?

I’m thinking feature freeze at the end of the month.

Oh, while I’m commenting on the current version of the patch, I will point out that changes to the save/restore format need careful thought. At the very least we’d like to be backward compatible (old images restore on new Xen) even if we don’t achieve compatibility the other way round. I didn’t really look too closely at this aspect of the patches so perhaps these patches are fine in this regard.

I'll look into the live migrate from old to new.

Quoted 1 line

Either way I think the core re-architecting of the hpet device model to handle missed ticks and so on is independent of interface changes anyway (apart from reasonable extensions to the timer_mode option). Changes to save/restore format, addition of extra debugging code, and peripheral things like that it’d be nice to have in separate patches. It makes the core stuff easier to review and more likely to get accepted without quibble (because there’s less of it, and it is all dedicated to a single purpose which I think we all agree is where we want to be).

This is fine. I'll break up the patch along the lines you suggest.

Quoted 3 lines

Thanks,
Keir

Thanks,
Dave

#19 lore raw
In reply to Keir Fraser
Quoted 3 lines

At guest install time you ought to be able to tell whether the guest will use hpet or not based on its version (RHELx, SLESy, Winz etc etc) and decide whether missed-ticks accounting is required or not.

Unfortunately this is not true on Linux, at least without gathering (and hardcoding) more information about the system. Whether hpet is used or not is dependent not only on the OS/version and hvm config parameters, but also on kernel command line parameters and even the underlying CPU. For example, on RHEL5u1, if the tsc is synchronized and the CPU is Intel, and no kernel parameters are chosen, tsc will be chosen as the default clocksource even if hpet is present. Ugly.

That said, if Dave's patch achieves the stated accuracy on most versions of Linux (e.g. at least RHEL4+5, 32+64, smp+1p) for SOME set of parameters (which might be different on each Linux version), it would still be better than what we have now.

The ideal solution, I think, would be for the default hvm settings to "do the right thing" for both Windows and Linux at least for the vast majority of configuration choices. I'm not sure this is possible, but it sure would be nice.

Dan

-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Monday, June 09, 2008 1:36 AM
To: Dave Winchell; [email protected]
Cc: Ben Guthro; xen-devel
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 1 line · Dave Winchell

4) config file parameters

Hpet enabled. Timer_mode set to HVM_HPET_guest_computes_missed_ticks for all Linux guests and to HVM_HPET_guest_does_not_compute_missed_ticks for Windows 2k8-64 and Vista 64.
8 vcpus for Linux and 2 for Windows.

These new HVM_HPET options seems a weird design choice. It appears that you can only set these or one of the old options, so there's not actually any independence between the mode used by vpt.c and the mode used by hpet.c. At guest install time you ought to be able to tell whether the guest will use hpet or not based on its version (RHELx, SLESy, Winz etc etc) and decide whether missed-ticks accounting is required or not.

I'd be more agreeable to a partch that stripped out the physical hpet accesses (since you say they are not the reason for the improvement in accuracy), built hpet on top of vpt, and added the necessary extra mechanisms to deal with interrupt broadcasts into vpt.c. And which was split into more separate pieces of mechanism.

-- Keir

#9 lore raw
In reply to Dave Winchell

I'm wondering what is "magic" about 0.03% in all the non-hw-hpet measurements. Is that just the accuracy of the underlying tsc on your test system, e.g. the skew of tsc relative to an external (ntp) source? Or is Xen (tsc-based) system time skewing that much on an overcommitted system (and skewing much less than 0.03% on an unloaded system)?

Running the following on dom0 both on an unloaded and overcommitted system (with ntpd off in dom0 and all guests) might be interesting:

# ntpdate $NTPSERVER; sleep 3600; ntpdate -q $NTPSERVER

-----Original Message-----
From: Dave Winchell [mailto:[email protected]]
Sent: Saturday, June 07, 2008 3:21 PM
To: Keir Fraser; [email protected]
Cc: Ben Guthro; xen-devel; Dave Winchell
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 5 lines

Possibly there are bugs in the hpet device model which are fixed by Dave's patch. If this is actually the case, it would be nice to break those out as separate patches, as I think an 11% drift must largely be due to device-model bugs rather than relatively insignificant differences between hvm_get_guest_time() and physical HPET main counter.

Hi Keir,

I tried an experiment on Friday where I short circuited the missed ticks policy code in the hpet.c patch, but used the physical hpet each access. The result for Linux was a drift of .1%, same as the xen-unstable bits.

Conversely I get very good drift numbers, i.e., under .03%, when using the missed ticks policy code and running in simulated mode (layered on stime) when stime uses hpet.

So clearly, the improvement from .1% to .03% is due to the policy code. I haven't run the short circuit test with the windows policy but I can do that on Monday.

Note: For Windows and Linux I get < .03% drift using the policy code and running simulated mode whether stime is using hpet or some other device.

regards,
Dave

-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Fri 6/6/2008 6:34 PM
To: [email protected]; Dave Winchell
Cc: Ben Guthro; xen-devel
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 8 lines · Dan Magenheimer

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0. And if for some reason Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

Yes, this would be a sensible extra timer_mode: have hvm_get_guest_time() call to the platform time read function, and bypass TSC altogether. This would be cleaner than having only the vHPET code punch through to the physical HPET: instead we have the boot-time chosen platform timesource used by all virtual timers.

Quoted 2 lines

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Possibly there are bugs in the hpet device model which are fixed by Dave's patch. If this is actually the case, it would be nice to break those out as separate patches, as I think an 11% drift must largely be due to device-model bugs rather than relatively insignificant differences between hvm_get_guest_time() and physical HPET main counter.

-- Keir

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

At guest install time you ought to be able to tell whether the guest will use hpet or not based on its version (RHELx, SLESy, Winz etc etc) and decide whether missed-ticks accounting is required or not.

Unfortunately this is not true on Linux, at least without gathering (and hardcoding) more information about the system. Whether hpet is used or not is dependent not only on the OS/version and hvm config parameters, but also on kernel command line parameters and even the underlying CPU. For example, on RHEL5u1, if the tsc is synchronized and the CPU is Intel, and no kernel parameters are chosen, tsc will be chosen as the default clocksource even if hpet is present. Ugly.

It's not immediately obvious that adding further independent configuration knobs to twiddle would make our lives that much easier. However it certainly increases the test matrix.

In your example above, by synchronised TSC do you mean constant-rate TSC? That can at least be hidden in CPUID now.

-- Keir

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

I'm wondering what is "magic" about 0.03% in all the non-hw-hpet measurements.

.03% is simply the maximum error we've seen with hpet.
The maximum value (.03) is the same whether its simulated or physical. The best value physical is .001% and I don't remember the best value simulated bit I believe it is under .01%, perhaps well under. I'll have to repeat that measurement. I would think that simulated and physical would give roughly the same drift values, but perhaps at very low drifts that doesn't hold.

I think the .03% is mostly due to the stability of the physical hpet device on a platform. I've noticed on some platforms, the simulated hpet time actually improves if you disable the hpet in the bios so that stime() is layered on the pm timer or whatever.

I would like to get to the bottom of this hpet stability variance from platform to platform.

Regards,
Dave

Quoted 96 lines

Is that just the accuracy of the underlying tsc
on your test system, e.g. the skew of tsc relative to an external (ntp) source? Or is Xen (tsc-based) system time skewing that much on an overcommitted system (and skewing much less than 0.03% on an unloaded system)?

Running the following on dom0 both on an unloaded and overcommitted system (with ntpd off in dom0 and all guests) might be interesting:

# ntpdate $NTPSERVER; sleep 3600; ntpdate -q $NTPSERVER

-----Original Message-----
From: Dave Winchell [mailto:[email protected]]
Sent: Saturday, June 07, 2008 3:21 PM
To: Keir Fraser; [email protected]
Cc: Ben Guthro; xen-devel; Dave Winchell
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 7 lines

Possibly there are bugs in the hpet device model which are fixed by Dave's patch. If this is actually the case, it would be nice to break those out as separate patches, as I think an 11% drift must largely be due to device-model bugs rather than relatively insignificant differences between hvm_get_guest_time() and physical HPET main counter.

Hi Keir,

I tried an experiment on Friday where I short circuited the missed ticks policy code in the hpet.c patch, but used the physical hpet each access. The result for Linux was a drift of .1%, same as the xen-unstable bits.

Conversely I get very good drift numbers, i.e., under .03%, when using the missed ticks policy code and running in simulated mode (layered on stime) when stime uses hpet.

So clearly, the improvement from .1% to .03% is due to the policy code. I haven't run the short circuit test with the windows policy but I can do that on Monday.

Note: For Windows and Linux I get < .03% drift using the policy code and running simulated mode whether stime is using hpet or some other device.

regards,
Dave

-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Fri 6/6/2008 6:34 PM
To: [email protected]; Dave Winchell
Cc: Ben Guthro; xen-devel
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 10 lines · Dan Magenheimer

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0. And if for some reason Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

Yes, this would be a sensible extra timer_mode: have hvm_get_guest_time() call to the platform time read function, and bypass TSC altogether. This would be cleaner than having only the vHPET code punch through to the physical HPET: instead we have the boot-time chosen platform timesource used by all virtual timers.

Quoted 4 lines

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Possibly there are bugs in the hpet device model which are fixed by Dave's patch. If this is actually the case, it would be nice to break those out as separate patches, as I think an 11% drift must largely be due to device-model bugs rather than relatively insignificant differences between hvm_get_guest_time() and physical HPET main counter.

-- Keir

#21 lore raw
In reply to Keir Fraser
Quoted 13 lines
Quoted 1 line
Quoted 1 line

At guest install time you ought to be able to tell whether

the guest

Quoted 1 line
Quoted 1 line

will use hpet or not based on its version (RHELx, SLESy,

Winz etc etc)

Quoted 7 lines
Quoted 1 line

and decide whether missed-ticks accounting is required or not.

Unfortunately this is not true on Linux, at least without gathering (and hardcoding) more information about the system. Whether hpet is used or not is dependent not only on the OS/version and hvm config parameters, but also on kernel command line parameters and even the underlying CPU. For example, on RHEL5u1, if the tsc is

synchronized

Quoted 1 line

and the CPU is Intel, and no kernel parameters are chosen,

Quoted 8 lines

tsc will be

Quoted 1 line

chosen as the default clocksource even if hpet is present. Ugly.

It's not immediately obvious that adding further independent configuration
knobs to twiddle would make our lives that much easier. However it certainly
increases the test matrix.

I fully agree. That's why I think the default parameters in Xen should "do the right thing". The default will get the most testing and if users say "time hurts when I change the parameters" we can say "then don't change the parameters" ;-)

Quoted 3 lines

In your example above, by synchronised TSC do you mean constant-rate TSC?
That can at least be hidden in CPUID now.

I meant synchronized as defined in 2.6.18/arch/x86_64/kernel/time.c in the function unsynchronized_tsc() and as used in the same file in time_init_gtod(). To make this more complicated, these routines have had not-insignificant bug fixes in RHEL5u1/2.

But yes, it might be a good idea if X86_FEATURE_CONSTANT_TSC always returns 0 (or at least is configurable and defaults off), since the test may only be made in the guest at boot time and the guest may migrate to a machine without the feature.

More ugliness, I know. My head hurts.

Dan

#23 lore raw
In reply to Dave Winchell
Quoted 2 lines

The Linux policy is more subtle, but is required to go from .1% to .03%.

Thanks for the good documentation which I hadn't thoroughly read until now. I now understand that the essence of your hpet missed ticks policy is to ensure that ticks are never delivered too close together. But I'm trying to understand WHY your patch works, in other words, what problem it is countering. I care about this for more reasons than just because it is interesting: (1) I'd like to feel confident that it is fixing a bug rather than just a symptom of a bug; and (2) I wonder how universally it is applicable.

I see from code examination in mark_offset_hpet() in RHEL4u5/arch/i386/kernel/timers/timer_hpet.c, that the correction for lost ticks is just plain wrong in a virtual environment. (Suppose for example that a virtual tick was delivered every 1.999*hpet_tick... I think the clock would be off by 50%!) Is this the bug that is being "countered" by your policy?

However, the lost tick handling in RHEL5u1/kernel/timer.c (which I think is used also for hpet) is much better so I am eager to find out if your policy works there too.

If the hpet missed tick policy works for both, though, I should be happy, though I wonder about upstream kernels (e.g. the trend toward tickless). That said, I'd rather see this get into Xen 3.3 and worry about upstream kernels later :-)

-----Original Message-----
From: Dave Winchell [mailto:[email protected]]
Sent: Sunday, June 08, 2008 2:32 PM
To: [email protected]; Keir Fraser
Cc: Ben Guthro; xen-devel; Dave Winchell
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Hi Dan,

Quoted 7 lines

While I am fully supportive of offering hardware hpet as an option for hvm guests (let's call it hwhpet=1 for shorthand), I am very surprised by your preliminary results; the most obvious conclusion is that Xen system time is losing time at the rate of 1000 PPM though its possible there's a bug somewhere else in the "time stack". Your Windows result is jaw-dropping and inexplicable, though I have to admit ignorance of how Windows manages time.

I think xen system time is fine. You have to add the interrupt delivery policies decribed in the write-up for the patch to get accurate timekeeping in the guest.

The windows policy is obvious and results in a large improvement in accuracy. The Linux policy is more subtle, but is required to go from .1% to .03%.

Quoted 6 lines

I think with my recent patch and hpet=1 (essentially the same as your emulated hpet), hvm guest time should track Xen system time. I wonder if domain0 (which if I understand correctly is directly using Xen system time) is also seeing an error of .1%? Also I wonder for the skew you are seeing (in both hvm guests and domain0) is time moving too fast or two slow?

I don't recall the direction. I can look it up in my notes at work tomorrow.

Quoted 4 lines

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0.

Our patch is accurate to < .03% using the physical hpet mode or the simulated mode.

Quoted 5 lines

And if for some reason
Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

In our experience, Xen system time is accurate enough now.

Quoted 5 lines

One more thought... do you know the accuracy of the TSC crystals on your test systems? I posted a patch awhile ago that was intended to test that, though I guess it was only testing skew of different TSCs on the same system, not TSCs against an external time source.

I do not know the tsc accuracy.

Quoted 2 lines

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Regards,
Dave

-----Original Message-----
From: Dan Magenheimer [mailto:[email protected]]
Sent: Fri 6/6/2008 4:29 PM
To: Dave Winchell; Keir Fraser
Cc: Ben Guthro; xen-devel
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Dave --

Thanks much for posting the preliminary results!

While I am fully supportive of offering hardware hpet as an option for hvm guests (let's call it hwhpet=1 for shorthand), I am very surprised by your preliminary results; the most obvious conclusion is that Xen system time is losing time at the rate of 1000 PPM though its possible there's a bug somewhere else in the "time stack". Your Windows result is jaw-dropping and inexplicable, though I have to admit ignorance of how Windows manages time.

I think with my recent patch and hpet=1 (essentially the same as your emulated hpet), hvm guest time should track Xen system time. I wonder if domain0 (which if I understand correctly is directly using Xen system time) is also seeing an error of .1%? Also I wonder for the skew you are seeing (in both hvm guests and domain0) is time moving too fast or two slow?

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0. And if for some reason Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

One more thought... do you know the accuracy of the TSC crystals on your test systems? I posted a patch awhile ago that was intended to test that, though I guess it was only testing skew of different TSCs on the same system, not TSCs against an external time source.

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Thanks,
Dan

Quoted 539 lines

-----Original Message-----
From: Dave Winchell [mailto:[email protected]]
Sent: Friday, June 06, 2008 1:33 PM
To: [email protected]; Keir Fraser
Cc: Ben Guthro; xen-devel; Dave Winchell
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Dan, Keir:

Preliminary tests results indicate an error of .1% for Linux 64 bit guests configured
for hpet with xen-unstable as is. As we have discussed many times, the ntp requirement is .05%.
Tests on the patch we just submitted for hpet have indicated errors of .0012%
on this platform under similar test conditions and .03% on other platforms.

Windows vista64 has an error of 11% using hpet with the xen-unstable bits.
In an overnight test with our hpet patch, the Windows vista error was .008%.

The tests are with two or three guests on a physical node, all under load, and with
the ratio of vcpus to phys cpus > 1.

I will continue to run tests over the next few days.

thanks,
Dave

Quoted 3 lines · Dan Magenheimer

Hi Dave and Ben --

When running tests on xen-unstable (without your patch),

please ensure

Quoted 2 lines

that hpet=1 is set in the hvm config and also I think that when hpet is the clocksource on RHEL4-32, the clock IS resilient to

missed ticks

Quoted 1 line

so timer_mode should be 2 (vs when pit is the clocksource

on RHEL4-32,

Quoted 4 lines

all clock ticks must be delivered and so timer_mode should be 0).

Per

http://lists.xensource.com/archives/html/xen-devel/2008-06/msg
00098.html it's

Quoted 8 lines

my intent to clean this up, but I won't get to it until next week.

Thanks,
Dan

    -----Original Message-----
    *From:* [email protected]
    [mailto:[email protected]]*On

Behalf Of *Dave

Quoted 9 lines

Winchell
*Sent:* Friday, June 06, 2008 4:46 AM
*To:* Keir Fraser; Ben Guthro; xen-devel
*Cc:* [email protected]; Dave Winchell *Subject:* RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Keir,

I think the changes are required. We'll run some tests

today today so

Quoted 7 lines

that we have some data to talk about.

-Dave

-----Original Message-----
From: [email protected] on behalf

of Keir Fraser

Quoted 8 lines

Sent: Fri 6/6/2008 4:58 AM
To: Ben Guthro; xen-devel
Cc: [email protected]
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Are these patches needed now the timers are built on Xen system time rather
than host TSC? Dan has reported much better

time-keeping with his

Quoted 2 lines

patch
checked in, and it¹s for sure a lot less invasive than

this patchset.

Quoted 10 lines

     -- Keir

    On 5/6/08 15:59, "Ben Guthro" <[email protected]> wrote:

    >
    > 1. Introduction
    >
    > This patch improves the hpet based guest clock in

terms of drift and

Quoted 2 lines
Quoted 2 lines

monotonicity.
Prior to this work the drift with hpet was greater

than 2%, far

Quoted 10 lines

above the .05%
> limit
> for ntp to synchronize. With this code, the drift ranges from .001% to .0033%
> depending
> on guest and physical platform.
>
> Using hpet allows guest operating systems to provide monotonic time to their
> applications. Time sources other than hpet are not

monotonic because

Quoted 1 line
Quoted 1 line

of their reliance on tsc, which is not synchronized

across physical

Quoted 31 lines
Quoted 3 lines

processors.

Windows 2k864 and many Linux guests are supported with two

policies, one for
> guests
> that handle missed clock interrupts and the other for guests that require the
> correct number of interrupts.
>
> Guests may use hpet for the timing source even if the physical platform has no
> visible
> hpet. Migration is supported between physical machines which differ in
> physical
> hpet visibility.
>
> Most of the changes are in hpet.c. Two general facilities are added to track
> interrupt
> progress. The ideas here and the facilities would be useful in vpt.c, for
> other time
> sources, though no attempt is made here to improve vpt.c. >
> The following sections discuss hpet dependencies, interrupt delivery policies,
> live migration,
> test results, and relation to recent work with monotonic time. >
>

Quoted 3 lines

> 2. Virtual Hpet dependencies
>
> The virtual hpet depends on the ability to read the

physical or

Quoted 2 lines

simulated
> (see discussion below) hpet. For timekeeping, the

virtual hpet

Quoted 32 lines

also depends
> on two new interrupt notification facilities to implement its policies for
> interrupt delivery.
>
> 2.1. Two modes of low-level hpet main counter reads. >
> In this implementation, the virtual hpet reads with read_64_main_counter(),
> exported by
> time.c, either the real physical hpet main counter register directly or a
> "simulated"
> hpet main counter.
>
> The simulated mode uses a monotonic version of get_s_time() (NOW()), where the
> last
> time value is returned whenever the current time value is less than the last
> time
> value. In simulated mode, since it is layered on s_time, the underlying
> hardware
> can be hpet or some other device. The frequency of the main counter in
> simulated
> mode is the same as the standard physical hpet frequency, allowing live
> migration
> between nodes that are configured differently.
>

Quoted 1 line

> If the physical platform does not have an hpet

device, or if xen

Quoted 9 lines

is configured
> not
> to use the device, then the simulated method is used. If there is a physical
> hpet device,
> and xen has initialized it, then either simulated or physical mode can be
> used.
> This is governed by a boot time option, hpet-avoid.

Setting this

Quoted 3 lines

option to 1
> gives the
> simulated mode and 0 the physical mode. The default

is physical

Quoted 5 lines

mode.
>
> A disadvantage of the physical mode is that may take longer to read the device
> than in simulated mode. On some platforms the cost is

about the

Quoted 15 lines

same (less
> than 250 nsec) for
> physical and simulated modes, while on others physical cost is much higher
> than simulated.
> A disadvantage of the simulated mode is that it can return the same value
> for the counter in consecutive calls.
>
> 2.2. Interrupt notification facilities.
>
> Two interrupt notification facilities are introduced, one is > hvm_isa_irq_assert_cb()
> and the other hvm_register_intr_en_notif().
>

Quoted 5 lines

> The vhpet uses hvm_isa_irq_assert_cb to deliver interrupts to the vioapic.
> hvm_isa_irq_assert_cb allows a callback to be passed along to > vioapic_deliver()
> and this callback is called with a mask of the vcpus

which will

Quoted 4 lines

get the
> interrupt. This callback is made before any vcpus receive an interrupt.
>

Quoted 8 lines

> Vhpet uses hvm_register_intr_en_notif() to register a handler for a particular
> vector that will be called when that vector is injected in > [vmx,svm]_intr_assist()
> and also when the guest finishes handling the interrupt. Here finished is
> defined
> as the point when the guest re-enables interrupts or

lowers the

Quoted 4 lines

tpr value.
> EOI is not used as the end of interrupt as this is sometimes returned before
> the interrupt handler has done its work. A flag is

passed to the

Quoted 3 lines

handler
> indicating
> whether this is the injection point (post = 1) or the

interrupt

Quoted 6 lines

finished (post
> = 0) point.
> The need for the finished point callback is discussed in the missed ticks
> policy section.
>

Quoted 7 lines

> To prevent a possible early trigger of the finished callback, intr_en_notif
> logic
> has a two stage arm, the first at injection (hvm_intr_en_notif_arm()) and the
> second when
> interrupts are seen to be disabled

(hvm_intr_en_notif_disarm()).

Quoted 3 lines

Once fully
> armed, re-enabling
> interrupts will cause hvm_intr_en_notif_disarm() to

make the end

Quoted 2 lines

of interrupt
> callback. hvm_intr_en_notif_arm() and

hvm_intr_en_notif_disarm()

Quoted 6 lines

are called by
> [vmx,svm]_intr_assist().
>
> 3. Interrupt delivery policies
>
> The existing hpet interrupt delivery is preserved.

This includes

Quoted 8 lines
Quoted 1 line

vcpu round robin delivery used by Linux and broadcast delivery

used by
> Windows.
>
> There are two policies for interrupt delivery, one for Windows 2k8-64 and the
> other
> for Linux. The Linux policy takes advantage of the

(guest) Linux

Quoted 3 lines

missed tick
> and offset
> calculations and does not attempt to deliver the

right number of

Quoted 10 lines

interrupts.
> The Windows policy delivers the correct number of interrupts, even if
> sometimes much
> closer to each other than the period. The policies are similar to those in
> vpt.c, though
> there are some important differences.
>
> Policies are selected with an HVMOP_set_param

hypercall with index

Quoted 2 lines
Quoted 2 lines

HVM_PARAM_TIMER_MODE.
Two new values are added,

HVM_HPET_guest_computes_missed_ticks and

Quoted 7 lines
Quoted 1 line

HVM_HPET_guest_does_not_compute_missed_ticks. The reason that

two new ones
> are added is that
> in some guests (32bit Linux) a no-missed policy is needed for clock sources
> other than hpet
> and a missed ticks policy for hpet. It was felt that

there would

Quoted 14 lines

be less
> confusion by simply
> introducing the two hpet policies.
>
> 3.1. The missed ticks policy
>
> The Linux clock interrupt handler for hpet calculates missed ticks and offset
> using the hpet
> main counter. The algorithm works well when the time since the last interrupt
> is greater than
> or equal to a period and poorly otherwise.
>

Quoted 1 line

> The missed ticks policy ensures that no two clock

interrupts are

Quoted 6 lines

delivered to
> the guest at
> a time interval less than a period. A time stamp (hpet main counter value) is
> recorded (by a
> callback registered with hvm_register_intr_en_notif)

when Linux

Quoted 3 lines

finishes
> handling the clock
> interrupt. Then, ensuing interrupts are delivered to

the vioapic

Quoted 9 lines

only if the
> current main
> counter value is a period greater than when the last interrupt was handled.
>
> Tests showed a significant improvement in clock drift with end of interrupt
> time stamps
> versus beginning of interrupt[1]. It is believed that

the reason

Quoted 3 lines

for the
> improvement
> is that the clock interrupt handler goes for a

spinlock and can

Quoted 11 lines

be therefore
> delayed in its
> processing. Furthermore, the main counter is read by the guest under the lock.
> The net
> effect is that if we time stamp injection, we can get the difference in time
> between successive interrupt handler lock acquisitions to be less than the
> period.
>

Quoted 3 lines

> 3.2. The no-missed ticks policy
>
> Windows 2k864 keeps very poor time with the missed

ticks policy.

Quoted 6 lines

So the
> no-missed ticks policy
> was developed. In the no-missed ticks policy we deliver the correct number of
> interrupts,
> even if they are spaced less than a period apart

(when catching up).

Quoted 50 lines
Quoted 2 lines

Windows 2k864 uses a broadcast mode in the interrupt routing

such that
> all vcpus get the clock interrupt. The best Windows drift performance was
> achieved when the
> policy code ensured that all the previous interrupts (on the various vcpus)
> had been injected
> before injecting the next interrupt to the vioapic.. >
> The policy code works as follows. It uses the hvm_isa_irq_assert_cb() to
> record
> the vcpus to be interrupted in h->hpet.pending_mask. Then, in the callback
> registered
> with hvm_register_intr_en_notif() at post=1 time it clears the current vcpu in
> the pending_mask.
> When the pending_mask is clear it decrements hpet.intr_pending_nr and if
> intr_pending_nr is still
> non-zero posts another interrupt to the ioapic with hvm_isa_irq_assert_cb().
> Intr_pending_nr is incremented in hpet_route_decision_not_missed_ticks().
>
> The missed ticks policy intr_en_notif callback also uses the pending_mask
> method. So even though
> Linux does not broadcast its interrupts, the code could handle it if it did.
> In this case the end of interrupt time stamp is made when the pending_mask is
> clear.
>
> 4. Live Migration
>
> Live migration with hpet preserves the current offset of the guest clock with
> respect
> to ntp. This is accomplished by migrating all of the state in the h->hpet data
> structure
> in the usual way. The hp->mc_offset is recalculated on the receiving node so
> that the
> guest sees a continuous hpet main counter.
>

Quoted 6 lines

> Code as been added to xc_domain_save.c to send a small message after the
> domain context is sent. The contents of the message is the physical tsc
> timestamp, last_tsc,
> read just before the message is sent. When the

last_tsc message

Quoted 39 lines

is received in
> xc_domain_restore.c,
> another physical tsc timestamp, cur_tsc, is read. The two timestamps are
> loaded into the domain
> structure as last_tsc_sender and first_tsc_receiver with hypercalls. Then
> xc_domain_hvm_setcontext
> is called so that hpet_load has access to these time stamps. Hpet_load uses
> the timestamps
> to account for the time spent saving and loading the domain context. With this
> technique,
> the only neglected time is the time spent sending a small network message.
>
> 5. Test Results
>
> Some recent test results are:
>
> 5.1 Linux 4u664 and Windows 2k864 load test.
> Duration: 70 hours.
> Test date: 6/2/08
> Loads: usex -b48 on Linux; burn-in on Windows
> Guest vcpus: 8 for Linux; 2 for Windows
> Hardware: 8 physical cpu AMD
> Clock drift : Linux: .0012% Windows: .009%
>
> 5.2 Linux 4u664, Linux 4u464 , and Windows 2k864 no-load test
> Duration: 23 hours.
> Test date: 6/3/08
> Loads: none
> Guest vcpus: 8 for each Linux; 2 for Windows
> Hardware: 4 physical cpu AMD
> Clock drift : Linux: .033% Windows: .019%
>
> 6. Relation to recent work in xen-unstable
>

Quoted 7 lines

> There is a similarity between hvm_get_guest_time() in xen-unstable and
> read_64_main_counter()
> in this code. However, read_64_main_counter() is more tuned to the needs of
> hpet.c. It has no
> "set" operation, only the get. It isolates the mode,

physical or

Quoted 6 lines

simulated, in
> read_64_main_counter()
> itself. It uses no vcpu or domain state as it is a physical entity, in either
> mode. And it provides a real
> physical mode for every read for those applications

that desire

Quoted 5 lines

this.
>
> 7. Conclusion
>
> The virtual hpet is improved by this patch in terms

of accuracy and

Quoted 2 lines
Quoted 2 lines

monotonicity.
Tests performed to date verify this and more testing

is under way.

Quoted 12 lines
Quoted 4 lines

8. Future Work

Testing with Windows Vista will be performed soon. The reason

for accuracy
> variations
> on different platforms using the physical hpet device will be investigated.
> Additional overhead measurements on simulated vs physical hpet mode will be
> made.
>

Quoted 3 lines

> Footnotes:
>
> 1. I don't recall the accuracy improvement with end

of interrupt

Quoted 23 lines

stamping, but
> it was
> significant, perhaps better than two to one improvement. It would be a very
> simple matter
> to re-measure the improvement as the facility can call back at injection time
> as well.
>
>
> Signed-off-by: Dave Winchell <[email protected]> > <mailto:[email protected]>
> Signed-off-by: Ben Guthro <[email protected]> > <mailto:[email protected]>
>
>
> _______________________________________________
> Xen-devel mailing list
> [email protected]
> http://lists.xensource.com/xen-devel

#24 lore raw
Quoted 10 lines
Quoted 2 lines

The Linux policy is more subtle, but is required to go from .1% to .03%.

Thanks for the good documentation which I hadn't thoroughly read until now.
I now understand that the essence of your
hpet missed ticks policy is to ensure that ticks are never delivered too close together. But I'm trying to understand WHY your patch works, in other words, what problem it is countering.

I'll tell you what I recall about this. Tomorrow I'll check the guest code to verify. I think that Linux declares a full tick, even if the interrupt is early. That's the problem.
On the other hand, if the interrupt is late it in effect declares a tick plus fraction. If it just declared the fraction in the first place, we could deliver the interrupts whenever we wanted.

Its really not that different than the missed ticks policy in vpt.c except that there the period in vpt.c is based on start of interrupt and I have improved that with end-of interrupt as described in the patch note.

I don't recall what prompted me to try end-of-interrupt, but I saw a significant improvement. I may have been running a monotonicity test at the same time to explain the lock contention mentioned in the write-up.

Quoted 4 lines

I care about this for more reasons than just
because it is interesting: (1) I'd like to feel confident that it is fixing a bug rather than just a symptom of a bug; and (2) I wonder how universally it is applicable.

Its worked well my my small set of guests. You and our QA are going to tell us about the wider set. It doesn't matter if guest A handles interrupts closely spaced or not, just whether it handles them far apart. So it should be pretty universal with guests that really handle missed ticks. I think its interesting that some 32bit Linux guests handle missed ticks for hpet.

Quoted 7 lines

I see from code examination in mark_offset_hpet() in RHEL4u5/arch/i386/kernel/timers/timer_hpet.c, that the correction for lost ticks is just plain wrong in a virtual environment. (Suppose for example that a virtual tick was delivered every 1.999*hpet_tick... I think the clock would be off by 50%!) Is this the bug that is being "countered" by your policy?

I haven't looked at that code, perhaps.
I'll check it tomorrow.

Quoted 7 lines

However, the lost tick handling in RHEL5u1/kernel/timer.c (which I think is used also for hpet) is much better so I am eager to find out if your policy works there too.
If the hpet missed tick policy works for both, though, I should be happy, though I wonder about upstream kernels (e.g. the trend toward tickless).

I wasn't aware of this trend. If its robust, however, it should handle late interrupts ...

Quoted 3 lines

That said, I'd rather
see this get into Xen 3.3 and worry about upstream kernels later :-)

Regards,
Dave

-----Original Message-----
From: Dan Magenheimer [mailto:[email protected]]
Sent: Mon 6/9/2008 6:02 PM
To: Dave Winchell; Keir Fraser
Cc: Ben Guthro; xen-devel
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 2 lines

The Linux policy is more subtle, but is required to go from .1% to .03%.

Thanks for the good documentation which I hadn't thoroughly read until now. I now understand that the essence of your hpet missed ticks policy is to ensure that ticks are never delivered too close together. But I'm trying to understand WHY your patch works, in other words, what problem it is countering. I care about this for more reasons than just because it is interesting: (1) I'd like to feel confident that it is fixing a bug rather than just a symptom of a bug; and (2) I wonder how universally it is applicable.

I see from code examination in mark_offset_hpet() in RHEL4u5/arch/i386/kernel/timers/timer_hpet.c, that the correction for lost ticks is just plain wrong in a virtual environment. (Suppose for example that a virtual tick was delivered every 1.999*hpet_tick... I think the clock would be off by 50%!) Is this the bug that is being "countered" by your policy?

However, the lost tick handling in RHEL5u1/kernel/timer.c (which I think is used also for hpet) is much better so I am eager to find out if your policy works there too.

If the hpet missed tick policy works for both, though, I should be happy, though I wonder about upstream kernels (e.g. the trend toward tickless). That said, I'd rather see this get into Xen 3.3 and worry about upstream kernels later :-)

-----Original Message-----
From: Dave Winchell [mailto:[email protected]]
Sent: Sunday, June 08, 2008 2:32 PM
To: [email protected]; Keir Fraser
Cc: Ben Guthro; xen-devel; Dave Winchell
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Hi Dan,

Quoted 7 lines

While I am fully supportive of offering hardware hpet as an option for hvm guests (let's call it hwhpet=1 for shorthand), I am very surprised by your preliminary results; the most obvious conclusion is that Xen system time is losing time at the rate of 1000 PPM though its possible there's a bug somewhere else in the "time stack". Your Windows result is jaw-dropping and inexplicable, though I have to admit ignorance of how Windows manages time.

I think xen system time is fine. You have to add the interrupt delivery policies decribed in the write-up for the patch to get accurate timekeeping in the guest.

The windows policy is obvious and results in a large improvement in accuracy. The Linux policy is more subtle, but is required to go from .1% to .03%.

Quoted 6 lines

I think with my recent patch and hpet=1 (essentially the same as your emulated hpet), hvm guest time should track Xen system time. I wonder if domain0 (which if I understand correctly is directly using Xen system time) is also seeing an error of .1%? Also I wonder for the skew you are seeing (in both hvm guests and domain0) is time moving too fast or two slow?

I don't recall the direction. I can look it up in my notes at work tomorrow.

Quoted 4 lines

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0.

Our patch is accurate to < .03% using the physical hpet mode or the simulated mode.

Quoted 5 lines

And if for some reason
Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

In our experience, Xen system time is accurate enough now.

Quoted 5 lines

One more thought... do you know the accuracy of the TSC crystals on your test systems? I posted a patch awhile ago that was intended to test that, though I guess it was only testing skew of different TSCs on the same system, not TSCs against an external time source.

I do not know the tsc accuracy.

Quoted 2 lines

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Regards,
Dave

-----Original Message-----
From: Dan Magenheimer [mailto:[email protected]]
Sent: Fri 6/6/2008 4:29 PM
To: Dave Winchell; Keir Fraser
Cc: Ben Guthro; xen-devel
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Dave --

Thanks much for posting the preliminary results!

While I am fully supportive of offering hardware hpet as an option for hvm guests (let's call it hwhpet=1 for shorthand), I am very surprised by your preliminary results; the most obvious conclusion is that Xen system time is losing time at the rate of 1000 PPM though its possible there's a bug somewhere else in the "time stack". Your Windows result is jaw-dropping and inexplicable, though I have to admit ignorance of how Windows manages time.

I think with my recent patch and hpet=1 (essentially the same as your emulated hpet), hvm guest time should track Xen system time. I wonder if domain0 (which if I understand correctly is directly using Xen system time) is also seeing an error of .1%? Also I wonder for the skew you are seeing (in both hvm guests and domain0) is time moving too fast or two slow?

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0. And if for some reason Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

One more thought... do you know the accuracy of the TSC crystals on your test systems? I posted a patch awhile ago that was intended to test that, though I guess it was only testing skew of different TSCs on the same system, not TSCs against an external time source.

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Thanks,
Dan

Quoted 510 lines

-----Original Message-----
From: Dave Winchell [mailto:[email protected]]
Sent: Friday, June 06, 2008 1:33 PM
To: [email protected]; Keir Fraser
Cc: Ben Guthro; xen-devel; Dave Winchell
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Dan, Keir:

Preliminary tests results indicate an error of .1% for Linux 64 bit guests configured
for hpet with xen-unstable as is. As we have discussed many times, the ntp requirement is .05%.
Tests on the patch we just submitted for hpet have indicated errors of .0012%
on this platform under similar test conditions and .03% on other platforms.

Windows vista64 has an error of 11% using hpet with the xen-unstable bits.
In an overnight test with our hpet patch, the Windows vista error was .008%.

The tests are with two or three guests on a physical node, all under load, and with
the ratio of vcpus to phys cpus > 1.

I will continue to run tests over the next few days.

thanks,
Dave

Quoted 3 lines · Dan Magenheimer

Hi Dave and Ben --

When running tests on xen-unstable (without your patch),

please ensure

Quoted 2 lines

that hpet=1 is set in the hvm config and also I think that when hpet is the clocksource on RHEL4-32, the clock IS resilient to

missed ticks

Quoted 1 line

so timer_mode should be 2 (vs when pit is the clocksource

on RHEL4-32,

Quoted 4 lines

all clock ticks must be delivered and so timer_mode should be 0).

Per

http://lists.xensource.com/archives/html/xen-devel/2008-06/msg
00098.html it's

Quoted 8 lines

my intent to clean this up, but I won't get to it until next week.

Thanks,
Dan

    -----Original Message-----
    *From:* [email protected]
    [mailto:[email protected]]*On

Behalf Of *Dave

Quoted 9 lines

Winchell
*Sent:* Friday, June 06, 2008 4:46 AM
*To:* Keir Fraser; Ben Guthro; xen-devel
*Cc:* [email protected]; Dave Winchell *Subject:* RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Keir,

I think the changes are required. We'll run some tests

today today so

Quoted 7 lines

that we have some data to talk about.

-Dave

-----Original Message-----
From: [email protected] on behalf

of Keir Fraser

Quoted 8 lines

Sent: Fri 6/6/2008 4:58 AM
To: Ben Guthro; xen-devel
Cc: [email protected]
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Are these patches needed now the timers are built on Xen system time rather
than host TSC? Dan has reported much better

time-keeping with his

Quoted 2 lines

patch
checked in, and it¹s for sure a lot less invasive than

this patchset.

Quoted 10 lines

     -- Keir

    On 5/6/08 15:59, "Ben Guthro" <[email protected]> wrote:

    >
    > 1. Introduction
    >
    > This patch improves the hpet based guest clock in

terms of drift and

Quoted 2 lines
Quoted 2 lines

monotonicity.
Prior to this work the drift with hpet was greater

than 2%, far

Quoted 10 lines

above the .05%
> limit
> for ntp to synchronize. With this code, the drift ranges from .001% to .0033%
> depending
> on guest and physical platform.
>
> Using hpet allows guest operating systems to provide monotonic time to their
> applications. Time sources other than hpet are not

monotonic because

Quoted 1 line
Quoted 1 line

of their reliance on tsc, which is not synchronized

across physical

Quoted 31 lines
Quoted 3 lines

processors.

Windows 2k864 and many Linux guests are supported with two

policies, one for
> guests
> that handle missed clock interrupts and the other for guests that require the
> correct number of interrupts.
>
> Guests may use hpet for the timing source even if the physical platform has no
> visible
> hpet. Migration is supported between physical machines which differ in
> physical
> hpet visibility.
>
> Most of the changes are in hpet.c. Two general facilities are added to track
> interrupt
> progress. The ideas here and the facilities would be useful in vpt.c, for
> other time
> sources, though no attempt is made here to improve vpt.c. >
> The following sections discuss hpet dependencies, interrupt delivery policies,
> live migration,
> test results, and relation to recent work with monotonic time. >
>

Quoted 3 lines

> 2. Virtual Hpet dependencies
>
> The virtual hpet depends on the ability to read the

physical or

Quoted 2 lines

simulated
> (see discussion below) hpet. For timekeeping, the

virtual hpet

Quoted 32 lines

also depends
> on two new interrupt notification facilities to implement its policies for
> interrupt delivery.
>
> 2.1. Two modes of low-level hpet main counter reads. >
> In this implementation, the virtual hpet reads with read_64_main_counter(),
> exported by
> time.c, either the real physical hpet main counter register directly or a
> "simulated"
> hpet main counter.
>
> The simulated mode uses a monotonic version of get_s_time() (NOW()), where the
> last
> time value is returned whenever the current time value is less than the last
> time
> value. In simulated mode, since it is layered on s_time, the underlying
> hardware
> can be hpet or some other device. The frequency of the main counter in
> simulated
> mode is the same as the standard physical hpet frequency, allowing live
> migration
> between nodes that are configured differently.
>

Quoted 1 line

> If the physical platform does not have an hpet

device, or if xen

Quoted 9 lines

is configured
> not
> to use the device, then the simulated method is used. If there is a physical
> hpet device,
> and xen has initialized it, then either simulated or physical mode can be
> used.
> This is governed by a boot time option, hpet-avoid.

Setting this

Quoted 3 lines

option to 1
> gives the
> simulated mode and 0 the physical mode. The default

is physical

Quoted 5 lines

mode.
>
> A disadvantage of the physical mode is that may take longer to read the device
> than in simulated mode. On some platforms the cost is

about the

Quoted 15 lines

same (less
> than 250 nsec) for
> physical and simulated modes, while on others physical cost is much higher
> than simulated.
> A disadvantage of the simulated mode is that it can return the same value
> for the counter in consecutive calls.
>
> 2.2. Interrupt notification facilities.
>
> Two interrupt notification facilities are introduced, one is > hvm_isa_irq_assert_cb()
> and the other hvm_register_intr_en_notif().
>

Quoted 5 lines

> The vhpet uses hvm_isa_irq_assert_cb to deliver interrupts to the vioapic.
> hvm_isa_irq_assert_cb allows a callback to be passed along to > vioapic_deliver()
> and this callback is called with a mask of the vcpus

which will

Quoted 4 lines

get the
> interrupt. This callback is made before any vcpus receive an interrupt.
>

Quoted 8 lines

> Vhpet uses hvm_register_intr_en_notif() to register a handler for a particular
> vector that will be called when that vector is injected in > [vmx,svm]_intr_assist()
> and also when the guest finishes handling the interrupt. Here finished is
> defined
> as the point when the guest re-enables interrupts or

lowers the

Quoted 4 lines

tpr value.
> EOI is not used as the end of interrupt as this is sometimes returned before
> the interrupt handler has done its work. A flag is

passed to the

Quoted 3 lines

handler
> indicating
> whether this is the injection point (post = 1) or the

interrupt

Quoted 6 lines

finished (post
> = 0) point.
> The need for the finished point callback is discussed in the missed ticks
> policy section.
>

Quoted 7 lines

> To prevent a possible early trigger of the finished callback, intr_en_notif
> logic
> has a two stage arm, the first at injection (hvm_intr_en_notif_arm()) and the
> second when
> interrupts are seen to be disabled

(hvm_intr_en_notif_disarm()).

Quoted 3 lines

Once fully
> armed, re-enabling
> interrupts will cause hvm_intr_en_notif_disarm() to

make the end

Quoted 2 lines

of interrupt
> callback. hvm_intr_en_notif_arm() and

hvm_intr_en_notif_disarm()

Quoted 6 lines

are called by
> [vmx,svm]_intr_assist().
>
> 3. Interrupt delivery policies
>
> The existing hpet interrupt delivery is preserved.

This includes

Quoted 8 lines
Quoted 1 line

vcpu round robin delivery used by Linux and broadcast delivery

used by
> Windows.
>
> There are two policies for interrupt delivery, one for Windows 2k8-64 and the
> other
> for Linux. The Linux policy takes advantage of the

(guest) Linux

Quoted 3 lines

missed tick
> and offset
> calculations and does not attempt to deliver the

right number of

Quoted 10 lines

interrupts.
> The Windows policy delivers the correct number of interrupts, even if
> sometimes much
> closer to each other than the period. The policies are similar to those in
> vpt.c, though
> there are some important differences.
>
> Policies are selected with an HVMOP_set_param

hypercall with index

Quoted 2 lines
Quoted 2 lines

HVM_PARAM_TIMER_MODE.
Two new values are added,

HVM_HPET_guest_computes_missed_ticks and

Quoted 7 lines
Quoted 1 line

HVM_HPET_guest_does_not_compute_missed_ticks. The reason that

two new ones
> are added is that
> in some guests (32bit Linux) a no-missed policy is needed for clock sources
> other than hpet
> and a missed ticks policy for hpet. It was felt that

there would

Quoted 14 lines

be less
> confusion by simply
> introducing the two hpet policies.
>
> 3.1. The missed ticks policy
>
> The Linux clock interrupt handler for hpet calculates missed ticks and offset
> using the hpet
> main counter. The algorithm works well when the time since the last interrupt
> is greater than
> or equal to a period and poorly otherwise.
>

Quoted 1 line

> The missed ticks policy ensures that no two clock

interrupts are

Quoted 6 lines

delivered to
> the guest at
> a time interval less than a period. A time stamp (hpet main counter value) is
> recorded (by a
> callback registered with hvm_register_intr_en_notif)

when Linux

Quoted 3 lines

finishes
> handling the clock
> interrupt. Then, ensuing interrupts are delivered to

the vioapic

Quoted 9 lines

only if the
> current main
> counter value is a period greater than when the last interrupt was handled.
>
> Tests showed a significant improvement in clock drift with end of interrupt
> time stamps
> versus beginning of interrupt[1]. It is believed that

the reason

Quoted 3 lines

for the
> improvement
> is that the clock interrupt handler goes for a

spinlock and can

Quoted 11 lines

be therefore
> delayed in its
> processing. Furthermore, the main counter is read by the guest under the lock.
> The net
> effect is that if we time stamp injection, we can get the difference in time
> between successive interrupt handler lock acquisitions to be less than the
> period.
>

Quoted 3 lines

> 3.2. The no-missed ticks policy
>
> Windows 2k864 keeps very poor time with the missed

ticks policy.

Quoted 6 lines

So the
> no-missed ticks policy
> was developed. In the no-missed ticks policy we deliver the correct number of
> interrupts,
> even if they are spaced less than a period apart

(when catching up).

Quoted 50 lines
Quoted 2 lines

Windows 2k864 uses a broadcast mode in the interrupt routing

such that
> all vcpus get the clock interrupt. The best Windows drift performance was
> achieved when the
> policy code ensured that all the previous interrupts (on the various vcpus)
> had been injected
> before injecting the next interrupt to the vioapic.. >
> The policy code works as follows. It uses the hvm_isa_irq_assert_cb() to
> record
> the vcpus to be interrupted in h->hpet.pending_mask. Then, in the callback
> registered
> with hvm_register_intr_en_notif() at post=1 time it clears the current vcpu in
> the pending_mask.
> When the pending_mask is clear it decrements hpet.intr_pending_nr and if
> intr_pending_nr is still
> non-zero posts another interrupt to the ioapic with hvm_isa_irq_assert_cb().
> Intr_pending_nr is incremented in hpet_route_decision_not_missed_ticks().
>
> The missed ticks policy intr_en_notif callback also uses the pending_mask
> method. So even though
> Linux does not broadcast its interrupts, the code could handle it if it did.
> In this case the end of interrupt time stamp is made when the pending_mask is
> clear.
>
> 4. Live Migration
>
> Live migration with hpet preserves the current offset of the guest clock with
> respect
> to ntp. This is accomplished by migrating all of the state in the h->hpet data
> structure
> in the usual way. The hp->mc_offset is recalculated on the receiving node so
> that the
> guest sees a continuous hpet main counter.
>

Quoted 6 lines

> Code as been added to xc_domain_save.c to send a small message after the
> domain context is sent. The contents of the message is the physical tsc
> timestamp, last_tsc,
> read just before the message is sent. When the

last_tsc message

Quoted 39 lines

is received in
> xc_domain_restore.c,
> another physical tsc timestamp, cur_tsc, is read. The two timestamps are
> loaded into the domain
> structure as last_tsc_sender and first_tsc_receiver with hypercalls. Then
> xc_domain_hvm_setcontext
> is called so that hpet_load has access to these time stamps. Hpet_load uses
> the timestamps
> to account for the time spent saving and loading the domain context. With this
> technique,
> the only neglected time is the time spent sending a small network message.
>
> 5. Test Results
>
> Some recent test results are:
>
> 5.1 Linux 4u664 and Windows 2k864 load test.
> Duration: 70 hours.
> Test date: 6/2/08
> Loads: usex -b48 on Linux; burn-in on Windows
> Guest vcpus: 8 for Linux; 2 for Windows
> Hardware: 8 physical cpu AMD
> Clock drift : Linux: .0012% Windows: .009%
>
> 5.2 Linux 4u664, Linux 4u464 , and Windows 2k864 no-load test
> Duration: 23 hours.
> Test date: 6/3/08
> Loads: none
> Guest vcpus: 8 for each Linux; 2 for Windows
> Hardware: 4 physical cpu AMD
> Clock drift : Linux: .033% Windows: .019%
>
> 6. Relation to recent work in xen-unstable
>

Quoted 7 lines

> There is a similarity between hvm_get_guest_time() in xen-unstable and
> read_64_main_counter()
> in this code. However, read_64_main_counter() is more tuned to the needs of
> hpet.c. It has no
> "set" operation, only the get. It isolates the mode,

physical or

Quoted 6 lines

simulated, in
> read_64_main_counter()
> itself. It uses no vcpu or domain state as it is a physical entity, in either
> mode. And it provides a real
> physical mode for every read for those applications

that desire

Quoted 5 lines

this.
>
> 7. Conclusion
>
> The virtual hpet is improved by this patch in terms

of accuracy and

Quoted 2 lines
Quoted 2 lines

monotonicity.
Tests performed to date verify this and more testing

is under way.

Quoted 12 lines
Quoted 4 lines

8. Future Work

Testing with Windows Vista will be performed soon. The reason

for accuracy
> variations
> on different platforms using the physical hpet device will be investigated.
> Additional overhead measurements on simulated vs physical hpet mode will be
> made.
>

Quoted 29 lines
Quoted 3 lines
Quoted 3 lines

Footnotes:

1. I don't recall the accuracy improvement with end

of interrupt

Quoted 23 lines

stamping, but
> it was
> significant, perhaps better than two to one improvement. It would be a very
> simple matter
> to re-measure the improvement as the facility can call back at injection time
> as well.
>
>
> Signed-off-by: Dave Winchell <[email protected]> > <mailto:[email protected]>
> Signed-off-by: Ben Guthro <[email protected]> > <mailto:[email protected]>
>
>
> _______________________________________________
> Xen-devel mailing list
> [email protected]
> http://lists.xensource.com/xen-devel

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

#25 lore raw
Quoted 3 lines

I'll tell you what I recall about this. Tomorrow I'll check the guest code to verify. I think that Linux declares a full tick, even if the interrupt is early. That's the problem.

Yes, that makes sense and concurs with what I remember from the EL4u5-32 code. If this is true, one would expect the default "no missed tick" policy to see time moving faster than an external source -- the first missed tick delivered after a long sleep would "catch up" and then the remainder would each add another tick.

Quoted 3 lines

On the other hand, if the interrupt is late it in effect declares a tick plus fraction. If it just declared the fraction in the first place, we could deliver the interrupts whenever we wanted.

My read of the EL4u5-32 code is that the fraction is discarded and a new tick period commences at "now", so the fractions eventually accumulate as lost time.

In EL5u1-32 however it looks like the fractions are accounted for. Indeed the EL5u1-32 "lost tick handling" code resembles the Linux/ia64 code which is what I've always assumed was the "missed tick" model. In this case, I think no policy is necessary and the measured skew should be identical to any physical hpet skew. I'll have to test this hypothesis though.

-----Original Message-----
From: [email protected] [mailto:[email protected]]On Behalf Of Dave Winchell
Sent: Monday, June 09, 2008 5:35 PM
To: [email protected]; Keir Fraser
Cc: Dave Winchell; xen-devel; Ben Guthro
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 10 lines
Quoted 2 lines

The Linux policy is more subtle, but is required to go from .1% to .03%.

Thanks for the good documentation which I hadn't thoroughly read until now.
I now understand that the essence of your
hpet missed ticks policy is to ensure that ticks are never delivered too close together. But I'm trying to understand WHY your patch works, in other words, what problem it is countering.

I'll tell you what I recall about this. Tomorrow I'll check the guest code to verify. I think that Linux declares a full tick, even if the interrupt is early. That's the problem.
On the other hand, if the interrupt is late it in effect declares a tick plus fraction. If it just declared the fraction in the first place, we could deliver the interrupts whenever we wanted.

Its really not that different than the missed ticks policy in vpt.c except that there the period in vpt.c is based on start of interrupt and I have improved that with end-of interrupt as described in the patch note.

I don't recall what prompted me to try end-of-interrupt, but I saw a significant improvement. I may have been running a monotonicity test at the same time to explain the lock contention mentioned in the write-up.

Quoted 4 lines

I care about this for more reasons than just
because it is interesting: (1) I'd like to feel confident that it is fixing a bug rather than just a symptom of a bug; and (2) I wonder how universally it is applicable.

Its worked well my my small set of guests. You and our QA are going to tell us about the wider set. It doesn't matter if guest A handles interrupts closely spaced or not, just whether it handles them far apart. So it should be pretty universal with guests that really handle missed ticks. I think its interesting that some 32bit Linux guests handle missed ticks for hpet.

Quoted 7 lines

I see from code examination in mark_offset_hpet() in RHEL4u5/arch/i386/kernel/timers/timer_hpet.c, that the correction for lost ticks is just plain wrong in a virtual environment. (Suppose for example that a virtual tick was delivered every 1.999*hpet_tick... I think the clock would be off by 50%!) Is this the bug that is being "countered" by your policy?

I haven't looked at that code, perhaps.
I'll check it tomorrow.

Quoted 7 lines

However, the lost tick handling in RHEL5u1/kernel/timer.c (which I think is used also for hpet) is much better so I am eager to find out if your policy works there too.
If the hpet missed tick policy works for both, though, I should be happy, though I wonder about upstream kernels (e.g. the trend toward tickless).

I wasn't aware of this trend. If its robust, however, it should handle late interrupts ...

Quoted 3 lines

That said, I'd rather
see this get into Xen 3.3 and worry about upstream kernels later :-)

Regards,
Dave

-----Original Message-----
From: Dan Magenheimer [mailto:[email protected]]
Sent: Mon 6/9/2008 6:02 PM
To: Dave Winchell; Keir Fraser
Cc: Ben Guthro; xen-devel
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 2 lines

The Linux policy is more subtle, but is required to go from .1% to .03%.

Thanks for the good documentation which I hadn't thoroughly read until now. I now understand that the essence of your hpet missed ticks policy is to ensure that ticks are never delivered too close together. But I'm trying to understand WHY your patch works, in other words, what problem it is countering. I care about this for more reasons than just because it is interesting: (1) I'd like to feel confident that it is fixing a bug rather than just a symptom of a bug; and (2) I wonder how universally it is applicable.

I see from code examination in mark_offset_hpet() in RHEL4u5/arch/i386/kernel/timers/timer_hpet.c, that the correction for lost ticks is just plain wrong in a virtual environment. (Suppose for example that a virtual tick was delivered every 1.999*hpet_tick... I think the clock would be off by 50%!) Is this the bug that is being "countered" by your policy?

However, the lost tick handling in RHEL5u1/kernel/timer.c (which I think is used also for hpet) is much better so I am eager to find out if your policy works there too.

If the hpet missed tick policy works for both, though, I should be happy, though I wonder about upstream kernels (e.g. the trend toward tickless). That said, I'd rather see this get into Xen 3.3 and worry about upstream kernels later :-)

-----Original Message-----
From: Dave Winchell [mailto:[email protected]]
Sent: Sunday, June 08, 2008 2:32 PM
To: [email protected]; Keir Fraser
Cc: Ben Guthro; xen-devel; Dave Winchell
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Hi Dan,

Quoted 7 lines

While I am fully supportive of offering hardware hpet as an option for hvm guests (let's call it hwhpet=1 for shorthand), I am very surprised by your preliminary results; the most obvious conclusion is that Xen system time is losing time at the rate of 1000 PPM though its possible there's a bug somewhere else in the "time stack". Your Windows result is jaw-dropping and inexplicable, though I have to admit ignorance of how Windows manages time.

I think xen system time is fine. You have to add the interrupt delivery policies decribed in the write-up for the patch to get accurate timekeeping in the guest.

The windows policy is obvious and results in a large improvement in accuracy. The Linux policy is more subtle, but is required to go from .1% to .03%.

Quoted 6 lines

I think with my recent patch and hpet=1 (essentially the same as your emulated hpet), hvm guest time should track Xen system time. I wonder if domain0 (which if I understand correctly is directly using Xen system time) is also seeing an error of .1%? Also I wonder for the skew you are seeing (in both hvm guests and domain0) is time moving too fast or two slow?

I don't recall the direction. I can look it up in my notes at work tomorrow.

Quoted 4 lines

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0.

Our patch is accurate to < .03% using the physical hpet mode or the simulated mode.

Quoted 5 lines

And if for some reason
Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

In our experience, Xen system time is accurate enough now.

Quoted 5 lines

One more thought... do you know the accuracy of the TSC crystals on your test systems? I posted a patch awhile ago that was intended to test that, though I guess it was only testing skew of different TSCs on the same system, not TSCs against an external time source.

I do not know the tsc accuracy.

Quoted 2 lines

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Regards,
Dave

-----Original Message-----
From: Dan Magenheimer [mailto:[email protected]]
Sent: Fri 6/6/2008 4:29 PM
To: Dave Winchell; Keir Fraser
Cc: Ben Guthro; xen-devel
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Dave --

Thanks much for posting the preliminary results!

While I am fully supportive of offering hardware hpet as an option for hvm guests (let's call it hwhpet=1 for shorthand), I am very surprised by your preliminary results; the most obvious conclusion is that Xen system time is losing time at the rate of 1000 PPM though its possible there's a bug somewhere else in the "time stack". Your Windows result is jaw-dropping and inexplicable, though I have to admit ignorance of how Windows manages time.

I think with my recent patch and hpet=1 (essentially the same as your emulated hpet), hvm guest time should track Xen system time. I wonder if domain0 (which if I understand correctly is directly using Xen system time) is also seeing an error of .1%? Also I wonder for the skew you are seeing (in both hvm guests and domain0) is time moving too fast or two slow?

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0. And if for some reason Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

One more thought... do you know the accuracy of the TSC crystals on your test systems? I posted a patch awhile ago that was intended to test that, though I guess it was only testing skew of different TSCs on the same system, not TSCs against an external time source.

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Thanks,
Dan

Quoted 539 lines

-----Original Message-----
From: Dave Winchell [mailto:[email protected]]
Sent: Friday, June 06, 2008 1:33 PM
To: [email protected]; Keir Fraser
Cc: Ben Guthro; xen-devel; Dave Winchell
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Dan, Keir:

Preliminary tests results indicate an error of .1% for Linux 64 bit guests configured
for hpet with xen-unstable as is. As we have discussed many times, the ntp requirement is .05%.
Tests on the patch we just submitted for hpet have indicated errors of .0012%
on this platform under similar test conditions and .03% on other platforms.

Windows vista64 has an error of 11% using hpet with the xen-unstable bits.
In an overnight test with our hpet patch, the Windows vista error was .008%.

The tests are with two or three guests on a physical node, all under load, and with
the ratio of vcpus to phys cpus > 1.

I will continue to run tests over the next few days.

thanks,
Dave

Quoted 3 lines · Dan Magenheimer

Hi Dave and Ben --

When running tests on xen-unstable (without your patch),

please ensure

Quoted 2 lines

that hpet=1 is set in the hvm config and also I think that when hpet is the clocksource on RHEL4-32, the clock IS resilient to

missed ticks

Quoted 1 line

so timer_mode should be 2 (vs when pit is the clocksource

on RHEL4-32,

Quoted 4 lines

all clock ticks must be delivered and so timer_mode should be 0).

Per

http://lists.xensource.com/archives/html/xen-devel/2008-06/msg
00098.html it's

Quoted 8 lines

my intent to clean this up, but I won't get to it until next week.

Thanks,
Dan

    -----Original Message-----
    *From:* [email protected]
    [mailto:[email protected]]*On

Behalf Of *Dave

Quoted 9 lines

Winchell
*Sent:* Friday, June 06, 2008 4:46 AM
*To:* Keir Fraser; Ben Guthro; xen-devel
*Cc:* [email protected]; Dave Winchell *Subject:* RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Keir,

I think the changes are required. We'll run some tests

today today so

Quoted 7 lines

that we have some data to talk about.

-Dave

-----Original Message-----
From: [email protected] on behalf

of Keir Fraser

Quoted 8 lines

Sent: Fri 6/6/2008 4:58 AM
To: Ben Guthro; xen-devel
Cc: [email protected]
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Are these patches needed now the timers are built on Xen system time rather
than host TSC? Dan has reported much better

time-keeping with his

Quoted 2 lines

patch
checked in, and it¹s for sure a lot less invasive than

this patchset.

Quoted 10 lines

     -- Keir

    On 5/6/08 15:59, "Ben Guthro" <[email protected]> wrote:

    >
    > 1. Introduction
    >
    > This patch improves the hpet based guest clock in

terms of drift and

Quoted 2 lines
Quoted 2 lines

monotonicity.
Prior to this work the drift with hpet was greater

than 2%, far

Quoted 10 lines

above the .05%
> limit
> for ntp to synchronize. With this code, the drift ranges from .001% to .0033%
> depending
> on guest and physical platform.
>
> Using hpet allows guest operating systems to provide monotonic time to their
> applications. Time sources other than hpet are not

monotonic because

Quoted 1 line
Quoted 1 line

of their reliance on tsc, which is not synchronized

across physical

Quoted 31 lines
Quoted 3 lines

processors.

Windows 2k864 and many Linux guests are supported with two

policies, one for
> guests
> that handle missed clock interrupts and the other for guests that require the
> correct number of interrupts.
>
> Guests may use hpet for the timing source even if the physical platform has no
> visible
> hpet. Migration is supported between physical machines which differ in
> physical
> hpet visibility.
>
> Most of the changes are in hpet.c. Two general facilities are added to track
> interrupt
> progress. The ideas here and the facilities would be useful in vpt.c, for
> other time
> sources, though no attempt is made here to improve vpt.c. >
> The following sections discuss hpet dependencies, interrupt delivery policies,
> live migration,
> test results, and relation to recent work with monotonic time. >
>

Quoted 3 lines

> 2. Virtual Hpet dependencies
>
> The virtual hpet depends on the ability to read the

physical or

Quoted 2 lines

simulated
> (see discussion below) hpet. For timekeeping, the

virtual hpet

Quoted 32 lines

also depends
> on two new interrupt notification facilities to implement its policies for
> interrupt delivery.
>
> 2.1. Two modes of low-level hpet main counter reads. >
> In this implementation, the virtual hpet reads with read_64_main_counter(),
> exported by
> time.c, either the real physical hpet main counter register directly or a
> "simulated"
> hpet main counter.
>
> The simulated mode uses a monotonic version of get_s_time() (NOW()), where the
> last
> time value is returned whenever the current time value is less than the last
> time
> value. In simulated mode, since it is layered on s_time, the underlying
> hardware
> can be hpet or some other device. The frequency of the main counter in
> simulated
> mode is the same as the standard physical hpet frequency, allowing live
> migration
> between nodes that are configured differently.
>

Quoted 1 line

> If the physical platform does not have an hpet

device, or if xen

Quoted 9 lines

is configured
> not
> to use the device, then the simulated method is used. If there is a physical
> hpet device,
> and xen has initialized it, then either simulated or physical mode can be
> used.
> This is governed by a boot time option, hpet-avoid.

Setting this

Quoted 3 lines

option to 1
> gives the
> simulated mode and 0 the physical mode. The default

is physical

Quoted 5 lines

mode.
>
> A disadvantage of the physical mode is that may take longer to read the device
> than in simulated mode. On some platforms the cost is

about the

Quoted 15 lines

same (less
> than 250 nsec) for
> physical and simulated modes, while on others physical cost is much higher
> than simulated.
> A disadvantage of the simulated mode is that it can return the same value
> for the counter in consecutive calls.
>
> 2.2. Interrupt notification facilities.
>
> Two interrupt notification facilities are introduced, one is > hvm_isa_irq_assert_cb()
> and the other hvm_register_intr_en_notif().
>

Quoted 5 lines

> The vhpet uses hvm_isa_irq_assert_cb to deliver interrupts to the vioapic.
> hvm_isa_irq_assert_cb allows a callback to be passed along to > vioapic_deliver()
> and this callback is called with a mask of the vcpus

which will

Quoted 4 lines

get the
> interrupt. This callback is made before any vcpus receive an interrupt.
>

Quoted 8 lines

> Vhpet uses hvm_register_intr_en_notif() to register a handler for a particular
> vector that will be called when that vector is injected in > [vmx,svm]_intr_assist()
> and also when the guest finishes handling the interrupt. Here finished is
> defined
> as the point when the guest re-enables interrupts or

lowers the

Quoted 4 lines

tpr value.
> EOI is not used as the end of interrupt as this is sometimes returned before
> the interrupt handler has done its work. A flag is

passed to the

Quoted 3 lines

handler
> indicating
> whether this is the injection point (post = 1) or the

interrupt

Quoted 6 lines

finished (post
> = 0) point.
> The need for the finished point callback is discussed in the missed ticks
> policy section.
>

Quoted 7 lines

> To prevent a possible early trigger of the finished callback, intr_en_notif
> logic
> has a two stage arm, the first at injection (hvm_intr_en_notif_arm()) and the
> second when
> interrupts are seen to be disabled

(hvm_intr_en_notif_disarm()).

Quoted 3 lines

Once fully
> armed, re-enabling
> interrupts will cause hvm_intr_en_notif_disarm() to

make the end

Quoted 2 lines

of interrupt
> callback. hvm_intr_en_notif_arm() and

hvm_intr_en_notif_disarm()

Quoted 6 lines

are called by
> [vmx,svm]_intr_assist().
>
> 3. Interrupt delivery policies
>
> The existing hpet interrupt delivery is preserved.

This includes

Quoted 8 lines
Quoted 1 line

vcpu round robin delivery used by Linux and broadcast delivery

used by
> Windows.
>
> There are two policies for interrupt delivery, one for Windows 2k8-64 and the
> other
> for Linux. The Linux policy takes advantage of the

(guest) Linux

Quoted 3 lines

missed tick
> and offset
> calculations and does not attempt to deliver the

right number of

Quoted 10 lines

interrupts.
> The Windows policy delivers the correct number of interrupts, even if
> sometimes much
> closer to each other than the period. The policies are similar to those in
> vpt.c, though
> there are some important differences.
>
> Policies are selected with an HVMOP_set_param

hypercall with index

Quoted 2 lines
Quoted 2 lines

HVM_PARAM_TIMER_MODE.
Two new values are added,

HVM_HPET_guest_computes_missed_ticks and

Quoted 7 lines
Quoted 1 line

HVM_HPET_guest_does_not_compute_missed_ticks. The reason that

two new ones
> are added is that
> in some guests (32bit Linux) a no-missed policy is needed for clock sources
> other than hpet
> and a missed ticks policy for hpet. It was felt that

there would

Quoted 14 lines

be less
> confusion by simply
> introducing the two hpet policies.
>
> 3.1. The missed ticks policy
>
> The Linux clock interrupt handler for hpet calculates missed ticks and offset
> using the hpet
> main counter. The algorithm works well when the time since the last interrupt
> is greater than
> or equal to a period and poorly otherwise.
>

Quoted 1 line

> The missed ticks policy ensures that no two clock

interrupts are

Quoted 6 lines

delivered to
> the guest at
> a time interval less than a period. A time stamp (hpet main counter value) is
> recorded (by a
> callback registered with hvm_register_intr_en_notif)

when Linux

Quoted 3 lines

finishes
> handling the clock
> interrupt. Then, ensuing interrupts are delivered to

the vioapic

Quoted 9 lines

only if the
> current main
> counter value is a period greater than when the last interrupt was handled.
>
> Tests showed a significant improvement in clock drift with end of interrupt
> time stamps
> versus beginning of interrupt[1]. It is believed that

the reason

Quoted 3 lines

for the
> improvement
> is that the clock interrupt handler goes for a

spinlock and can

Quoted 11 lines

be therefore
> delayed in its
> processing. Furthermore, the main counter is read by the guest under the lock.
> The net
> effect is that if we time stamp injection, we can get the difference in time
> between successive interrupt handler lock acquisitions to be less than the
> period.
>

Quoted 3 lines

> 3.2. The no-missed ticks policy
>
> Windows 2k864 keeps very poor time with the missed

ticks policy.

Quoted 6 lines

So the
> no-missed ticks policy
> was developed. In the no-missed ticks policy we deliver the correct number of
> interrupts,
> even if they are spaced less than a period apart

(when catching up).

Quoted 50 lines
Quoted 2 lines

Windows 2k864 uses a broadcast mode in the interrupt routing

such that
> all vcpus get the clock interrupt. The best Windows drift performance was
> achieved when the
> policy code ensured that all the previous interrupts (on the various vcpus)
> had been injected
> before injecting the next interrupt to the vioapic.. >
> The policy code works as follows. It uses the hvm_isa_irq_assert_cb() to
> record
> the vcpus to be interrupted in h->hpet.pending_mask. Then, in the callback
> registered
> with hvm_register_intr_en_notif() at post=1 time it clears the current vcpu in
> the pending_mask.
> When the pending_mask is clear it decrements hpet.intr_pending_nr and if
> intr_pending_nr is still
> non-zero posts another interrupt to the ioapic with hvm_isa_irq_assert_cb().
> Intr_pending_nr is incremented in hpet_route_decision_not_missed_ticks().
>
> The missed ticks policy intr_en_notif callback also uses the pending_mask
> method. So even though
> Linux does not broadcast its interrupts, the code could handle it if it did.
> In this case the end of interrupt time stamp is made when the pending_mask is
> clear.
>
> 4. Live Migration
>
> Live migration with hpet preserves the current offset of the guest clock with
> respect
> to ntp. This is accomplished by migrating all of the state in the h->hpet data
> structure
> in the usual way. The hp->mc_offset is recalculated on the receiving node so
> that the
> guest sees a continuous hpet main counter.
>

Quoted 6 lines

> Code as been added to xc_domain_save.c to send a small message after the
> domain context is sent. The contents of the message is the physical tsc
> timestamp, last_tsc,
> read just before the message is sent. When the

last_tsc message

Quoted 39 lines

is received in
> xc_domain_restore.c,
> another physical tsc timestamp, cur_tsc, is read. The two timestamps are
> loaded into the domain
> structure as last_tsc_sender and first_tsc_receiver with hypercalls. Then
> xc_domain_hvm_setcontext
> is called so that hpet_load has access to these time stamps. Hpet_load uses
> the timestamps
> to account for the time spent saving and loading the domain context. With this
> technique,
> the only neglected time is the time spent sending a small network message.
>
> 5. Test Results
>
> Some recent test results are:
>
> 5.1 Linux 4u664 and Windows 2k864 load test.
> Duration: 70 hours.
> Test date: 6/2/08
> Loads: usex -b48 on Linux; burn-in on Windows
> Guest vcpus: 8 for Linux; 2 for Windows
> Hardware: 8 physical cpu AMD
> Clock drift : Linux: .0012% Windows: .009%
>
> 5.2 Linux 4u664, Linux 4u464 , and Windows 2k864 no-load test
> Duration: 23 hours.
> Test date: 6/3/08
> Loads: none
> Guest vcpus: 8 for each Linux; 2 for Windows
> Hardware: 4 physical cpu AMD
> Clock drift : Linux: .033% Windows: .019%
>
> 6. Relation to recent work in xen-unstable
>

Quoted 7 lines

> There is a similarity between hvm_get_guest_time() in xen-unstable and
> read_64_main_counter()
> in this code. However, read_64_main_counter() is more tuned to the needs of
> hpet.c. It has no
> "set" operation, only the get. It isolates the mode,

physical or

Quoted 6 lines

simulated, in
> read_64_main_counter()
> itself. It uses no vcpu or domain state as it is a physical entity, in either
> mode. And it provides a real
> physical mode for every read for those applications

that desire

Quoted 5 lines

this.
>
> 7. Conclusion
>
> The virtual hpet is improved by this patch in terms

of accuracy and

Quoted 2 lines
Quoted 2 lines

monotonicity.
Tests performed to date verify this and more testing

is under way.

Quoted 12 lines
Quoted 4 lines

8. Future Work

Testing with Windows Vista will be performed soon. The reason

for accuracy
> variations
> on different platforms using the physical hpet device will be investigated.
> Additional overhead measurements on simulated vs physical hpet mode will be
> made.
>

Quoted 3 lines

> Footnotes:
>
> 1. I don't recall the accuracy improvement with end

of interrupt

Quoted 23 lines

stamping, but
> it was
> significant, perhaps better than two to one improvement. It would be a very
> simple matter
> to re-measure the improvement as the facility can call back at injection time
> as well.
>
>
> Signed-off-by: Dave Winchell <[email protected]> > <mailto:[email protected]>
> Signed-off-by: Ben Guthro <[email protected]> > <mailto:[email protected]>
>
>
> _______________________________________________
> Xen-devel mailing list
> [email protected]
> http://lists.xensource.com/xen-devel

#30 lore raw
In reply to Dave Winchell
Quoted 4 lines · Dave Winchell

I don't recall what prompted me to try end-of-interrupt, but I saw a significant improvement. I may have been running a monotonicity test at the same time to explain the lock contention mentioned in the write-up.

Doesn¹t this policy guarantee that you actually deliver interrupts at consistently too low a rate? Since the delivery period is now timer period + latency of interrupt handling? I suppose it works for this guest type because it doesn¹t actually care about getting interrupts at the correct rate, so long as the ticks are always a bit late?

For those that do need missed ticks to be delivered, do you track missed ticks at the absolute correct rate?

This is perhaps a fine tradeoff for all platform timers ‹ those guests that can handle missed ticks obviously do not care about getting their timer interrupts at absolutely the correct rate, and delivering a little late is what they are geared to handle (getting delivered consistently early is just weird!). Whereas guests that need all ticks also want them (at least over the long run) at exactly the correct rate.

I think there¹s good empirical analysis in the work you¹ve done. We just need the patches cleaned up and generalised for vpt.c now.

-- Keir

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

#31 lore raw
Quoted 8 lines
Quoted 4 lines

I don't recall what prompted me to try end-of-interrupt, but I saw a significant improvement. I may have been running a monotonicity test at the same time to explain the lock contention mentioned in the write-up.

Doesn¹t this policy guarantee that you actually deliver interrupts at consistently too low a rate? Since the delivery period is now timer period + latency of interrupt handling?

Yes, the rate ends up being about half the normal rate because
I toss the interrupt if it doesn't meet the requirement. If, in testing, we find a guest that has a problem with half the normal rate, we can fine tune the policy. For example, instead of discarding set a small timer.

Quoted 3 lines

I suppose it works for this guest type
because it doesn¹t actually care about getting interrupts at the correct rate, so long as the ticks are always a bit late?

True.

Quoted 2 lines

For those that do need missed ticks to be delivered, do you track missed ticks at the absolute correct rate?

Yes.

Quoted 10 lines

This is perhaps a fine tradeoff for all platform timers < those guests that can handle missed ticks obviously do not care about getting their timer interrupts at absolutely the correct rate, and delivering a little late is what they are geared to handle (getting delivered consistently early is just weird!). Whereas guests that need all ticks also want them (at least over the long run) at exactly the correct rate.

I think there¹s good empirical analysis in the work you¹ve done. We just need the patches cleaned up and generalised for vpt.c now.

Thanks. I'll get to work on the generalization, etc.

-Dave

-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Tue 6/10/2008 3:52 AM
To: Dave Winchell; [email protected]
Cc: Ben Guthro; xen-devel
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 4 lines · Dave Winchell

I don't recall what prompted me to try end-of-interrupt, but I saw a significant improvement. I may have been running a monotonicity test at the same time to explain the lock contention mentioned in the write-up.

Doesn¹t this policy guarantee that you actually deliver interrupts at consistently too low a rate? Since the delivery period is now timer period + latency of interrupt handling? I suppose it works for this guest type because it doesn¹t actually care about getting interrupts at the correct rate, so long as the ticks are always a bit late?

For those that do need missed ticks to be delivered, do you track missed ticks at the absolute correct rate?

This is perhaps a fine tradeoff for all platform timers < those guests that can handle missed ticks obviously do not care about getting their timer interrupts at absolutely the correct rate, and delivering a little late is what they are geared to handle (getting delivered consistently early is just weird!). Whereas guests that need all ticks also want them (at least over the long run) at exactly the correct rate.

I think there¹s good empirical analysis in the work you¹ve done. We just need the patches cleaned up and generalised for vpt.c now.

-- Keir

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

#32 lore raw
Quoted 9 lines
Quoted 4 lines

Doesn¹t this policy guarantee that you actually deliver interrupts at consistently too low a rate? Since the delivery period is now timer period + latency of interrupt handling?

Yes, the rate ends up being about half the normal rate because
I toss the interrupt if it doesn't meet the requirement. If, in testing, we find a guest that has a problem with half the normal rate, we can fine tune the policy. For example, instead of discarding set a small timer.

A better solution is to set the new period timer at end-of-interrupt time (in the callback) instead of delivery time (timer expiration time). This way the rate will be very close to what the guest expects. I think I'll make this change.

-Dave

-----Original Message-----
From: Dave Winchell
Sent: Tue 6/10/2008 7:49 AM
To: Keir Fraser; [email protected]
Cc: Ben Guthro; xen-devel; Dave Winchell
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 8 lines
Quoted 4 lines

I don't recall what prompted me to try end-of-interrupt, but I saw a significant improvement. I may have been running a monotonicity test at the same time to explain the lock contention mentioned in the write-up.

Doesn¹t this policy guarantee that you actually deliver interrupts at consistently too low a rate? Since the delivery period is now timer period + latency of interrupt handling?

Yes, the rate ends up being about half the normal rate because
I toss the interrupt if it doesn't meet the requirement. If, in testing, we find a guest that has a problem with half the normal rate, we can fine tune the policy. For example, instead of discarding set a small timer.

Quoted 3 lines

I suppose it works for this guest type
because it doesn¹t actually care about getting interrupts at the correct rate, so long as the ticks are always a bit late?

True.

Quoted 2 lines

For those that do need missed ticks to be delivered, do you track missed ticks at the absolute correct rate?

Yes.

Quoted 10 lines

This is perhaps a fine tradeoff for all platform timers < those guests that can handle missed ticks obviously do not care about getting their timer interrupts at absolutely the correct rate, and delivering a little late is what they are geared to handle (getting delivered consistently early is just weird!). Whereas guests that need all ticks also want them (at least over the long run) at exactly the correct rate.

I think there¹s good empirical analysis in the work you¹ve done. We just need the patches cleaned up and generalised for vpt.c now.

Thanks. I'll get to work on the generalization, etc.

-Dave

-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Tue 6/10/2008 3:52 AM
To: Dave Winchell; [email protected]
Cc: Ben Guthro; xen-devel
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 4 lines · Dave Winchell

I don't recall what prompted me to try end-of-interrupt, but I saw a significant improvement. I may have been running a monotonicity test at the same time to explain the lock contention mentioned in the write-up.

Doesn¹t this policy guarantee that you actually deliver interrupts at consistently too low a rate? Since the delivery period is now timer period + latency of interrupt handling? I suppose it works for this guest type because it doesn¹t actually care about getting interrupts at the correct rate, so long as the ticks are always a bit late?

For those that do need missed ticks to be delivered, do you track missed ticks at the absolute correct rate?

This is perhaps a fine tradeoff for all platform timers < those guests that can handle missed ticks obviously do not care about getting their timer interrupts at absolutely the correct rate, and delivering a little late is what they are geared to handle (getting delivered consistently early is just weird!). Whereas guests that need all ticks also want them (at least over the long run) at exactly the correct rate.

I think there¹s good empirical analysis in the work you¹ve done. We just need the patches cleaned up and generalised for vpt.c now.

-- Keir

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

#33 lore raw
Quoted 10 lines · Dave Winchell
Quoted 5 lines
Quoted 2 lines

Yes, the rate ends up being about half the normal rate because I toss the interrupt if it doesn't meet the requirement. If, in testing, we

find

Quoted 2 lines

a guest that has a problem with half the normal rate, we can fine tune the policy. For example, instead of discarding set a small timer.

A better solution is to set the new period timer at end-of-interrupt time (in the callback) instead of delivery time (timer expiration time). This way the rate will be very close to what the guest expects. I think I'll make this change.

Yes, that¹s what I thought you¹d done. It sounds nicer to me.

-- Keir

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

#34 lore raw

Keir, Dan:

Although I plan to break up the patch, etc., I'm posting this fix to the patch for anyone who might be interested.

thanks,
Dave

# This is a BitKeeper generated diff -Nru style patch. #
# ChangeSet
# 2008/06/10 12:20:48-04:00 [email protected]
# vi-patch: xen-hpet
#
# Bug Id: 6057
#
# Reviewed by: Robert
#
# SUMMARY: Fix wrap issue in monotonic s_time().
#

# xen/arch/x86/time.c
#   2008/06/10 12:20:48-04:00 [email protected] +3 -2
#   Fix wrap issue in monotonic s_time().
# 
diff -Nru a/xen/arch/x86/time.c b/xen/arch/x86/time.c
--- a/xen/arch/x86/time.c	2008-06-10 13:08:39 -04:00
+++ b/xen/arch/x86/time.c	2008-06-10 13:08:39 -04:00
@@ -534,7 +534,7 @@
     u64 count;
     unsigned long flags;
     struct cpu_time *t = &this_cpu(cpu_time);
-    u64 tsc, delta;
+    u64 tsc, delta, diff;
     s_time_t now;
     if(hpet_main_counter_phys_avoid_hdw || !hpet_physical_inited) {
@@ -542,7 +542,8 @@
         rdtscll(tsc);
         delta = tsc - t->local_tsc_stamp;
         now = t->stime_local_stamp + scale_delta(delta, &t->tsc_scale);
-        if(now > get_s_time_mon.last_ret)
+        diff = (u64)now - (u64)get_s_time_mon.last_ret;
+        if((s64)diff > (s64)0)
             get_s_time_mon.last_ret = now;
         else
             now = get_s_time_mon.last_ret;

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

#26 lore raw
In reply to Dan Magenheimer
Quoted 6 lines

In EL5u1-32 however it looks like the fractions are accounted for. Indeed the EL5u1-32 "lost tick handling" code resembles the Linux/ia64 code which is what I've always assumed was the "missed tick" model. In this case, I think no policy is necessary and the measured skew should be identical to any physical hpet skew. I'll have to test this hypothesis though.

I've tested this hypothesis and it seems to hold true. This means the existing (unpatched) hpet code works fine on EL5-32bit (vcpus=1) when hpet is the clocksource,
even when the machine is overcommitted. A second hypothesis still needs to be tested that Dave's patch will not make this worse.

(Note that per previous discussion, my EL5u1-32bit guest running on an Intel dual-core physical box chose tsc as the best clocksource and I had to override it with clock=hpet in the kernel command line.)

Quoted 6 lines

Yes, that makes sense and concurs with what I remember from the EL4u5-32 code. If this is true, one would expect the default "no missed tick" policy to see time moving faster than an external source -- the first missed tick delivered after a long sleep would "catch up" and then the remainder would each add another tick.

Indeed with the existing (unpatched) hpet code, time is running faster on EL4u5-32 (vcpus=1, when overcommited). So Dave's patch is definitely needed here.

Will try 64-bit next.

Dan

Quoted 856 lines

-----Original Message-----
From: Dan Magenheimer [mailto:[email protected]]
Sent: Monday, June 09, 2008 9:21 PM
To: 'Dave Winchell'; 'Keir Fraser'
Cc: 'xen-devel'; 'Ben Guthro'
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 3 lines

I'll tell you what I recall about this. Tomorrow I'll check the guest code to verify. I think that Linux declares a full tick, even if the interrupt is early. That's the problem.

Yes, that makes sense and concurs with what I remember from the EL4u5-32 code. If this is true, one would expect the default "no missed tick" policy to see time moving faster than an external source -- the first missed tick delivered after a long sleep would "catch up" and then the remainder would each add another tick.

Quoted 2 lines

On the other hand, if the interrupt is late it in effect declares a tick plus fraction. If it just declared the fraction in

the first place,

Quoted 1 line

we could deliver the interrupts whenever we wanted.

My read of the EL4u5-32 code is that the fraction is discarded and a new tick period commences at "now", so the fractions eventually accumulate as lost time.

In EL5u1-32 however it looks like the fractions are accounted for. Indeed the EL5u1-32 "lost tick handling" code resembles the Linux/ia64 code which is what I've always assumed was the "missed tick" model. In this case, I think no policy is necessary and the measured skew should be identical to any physical hpet skew. I'll have to test this hypothesis though.

-----Original Message-----
From: [email protected] [mailto:[email protected]]On Behalf Of Dave Winchell
Sent: Monday, June 09, 2008 5:35 PM
To: [email protected]; Keir Fraser
Cc: Dave Winchell; xen-devel; Ben Guthro
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 10 lines
Quoted 2 lines

The Linux policy is more subtle, but is required to go from .1% to .03%.

Thanks for the good documentation which I hadn't thoroughly read until now.
I now understand that the essence of your
hpet missed ticks policy is to ensure that ticks are never delivered too close together. But I'm trying to understand WHY your patch works, in other words, what problem it is countering.

I'll tell you what I recall about this. Tomorrow I'll check the guest code to verify. I think that Linux declares a full tick, even if the interrupt is early. That's the problem.
On the other hand, if the interrupt is late it in effect declares a tick plus fraction. If it just declared the fraction in the first place,
we could deliver the interrupts whenever we wanted.

Its really not that different than the missed ticks policy in vpt.c except that there the period in vpt.c is based on start of interrupt and I have improved that with end-of interrupt as described in the patch note.

I don't recall what prompted me to try end-of-interrupt, but I saw a significant improvement. I may have been running a monotonicity test at the same time to explain the lock contention mentioned in the write-up.

Quoted 4 lines

I care about this for more reasons than just
because it is interesting: (1) I'd like to feel confident that it is fixing a bug rather than just a symptom of a bug; and (2) I wonder how universally it is applicable.

Its worked well my my small set of guests. You and our QA are going to tell us about the wider set. It doesn't matter if guest A handles interrupts closely spaced or not, just whether it handles them far apart. So it should be pretty universal with guests that really handle missed ticks. I think its interesting that some 32bit Linux guests handle missed ticks for hpet.

Quoted 7 lines

I see from code examination in mark_offset_hpet() in RHEL4u5/arch/i386/kernel/timers/timer_hpet.c, that the correction for lost ticks is just plain wrong in a virtual environment. (Suppose for example that a virtual tick was delivered every 1.999*hpet_tick... I think the clock would be off by 50%!) Is this the bug that is being "countered" by your policy?

I haven't looked at that code, perhaps.
I'll check it tomorrow.

Quoted 7 lines

However, the lost tick handling in RHEL5u1/kernel/timer.c (which I think is used also for hpet) is much better so I am eager to find out if your policy works there too.
If the hpet missed tick policy works for both, though, I should be happy, though I wonder about upstream kernels (e.g. the trend toward tickless).

I wasn't aware of this trend. If its robust, however, it should handle late interrupts ...

Quoted 3 lines

That said, I'd rather
see this get into Xen 3.3 and worry about upstream kernels later :-)

Regards,
Dave

-----Original Message-----
From: Dan Magenheimer [mailto:[email protected]]
Sent: Mon 6/9/2008 6:02 PM
To: Dave Winchell; Keir Fraser
Cc: Ben Guthro; xen-devel
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 2 lines

The Linux policy is more subtle, but is required to go from .1% to .03%.

Thanks for the good documentation which I hadn't thoroughly read until now. I now understand that the essence of your hpet missed ticks policy is to ensure that ticks are never delivered too close together. But I'm trying to understand WHY your patch works, in other words, what problem it is countering. I care about this for more reasons than just because it is interesting: (1) I'd like to feel confident that it is fixing a bug rather than just a symptom of a bug; and (2) I wonder how universally it is applicable.

I see from code examination in mark_offset_hpet() in RHEL4u5/arch/i386/kernel/timers/timer_hpet.c, that the correction for lost ticks is just plain wrong in a virtual environment. (Suppose for example that a virtual tick was delivered every 1.999*hpet_tick... I think the clock would be off by 50%!) Is this the bug that is being "countered" by your policy?

However, the lost tick handling in RHEL5u1/kernel/timer.c (which I think is used also for hpet) is much better so I am eager to find out if your policy works there too.

If the hpet missed tick policy works for both, though, I should be happy, though I wonder about upstream kernels (e.g. the trend toward tickless). That said, I'd rather see this get into Xen 3.3 and worry about upstream kernels later :-)

-----Original Message-----
From: Dave Winchell [mailto:[email protected]]
Sent: Sunday, June 08, 2008 2:32 PM
To: [email protected]; Keir Fraser
Cc: Ben Guthro; xen-devel; Dave Winchell
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Hi Dan,

Quoted 7 lines

While I am fully supportive of offering hardware hpet as an option for hvm guests (let's call it hwhpet=1 for shorthand), I am very surprised by your preliminary results; the most obvious conclusion is that Xen system time is losing time at the rate of 1000 PPM though its possible there's a bug somewhere else in the "time stack". Your Windows result is jaw-dropping and inexplicable, though I have to admit ignorance of how Windows manages time.

I think xen system time is fine. You have to add the interrupt delivery policies decribed in the write-up for the patch to get accurate timekeeping in the guest.

The windows policy is obvious and results in a large improvement in accuracy. The Linux policy is more subtle, but is required to go from .1% to .03%.

Quoted 6 lines

I think with my recent patch and hpet=1 (essentially the same as your emulated hpet), hvm guest time should track Xen system time. I wonder if domain0 (which if I understand correctly is directly using Xen system time) is also seeing an error of .1%? Also I wonder for the skew you are seeing (in both hvm guests and domain0) is time moving too fast or two slow?

I don't recall the direction. I can look it up in my notes at work tomorrow.

Quoted 4 lines

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0.

Our patch is accurate to < .03% using the physical hpet mode or the simulated mode.

Quoted 5 lines

And if for some reason
Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

In our experience, Xen system time is accurate enough now.

Quoted 5 lines

One more thought... do you know the accuracy of the TSC crystals on your test systems? I posted a patch awhile ago that was intended to test that, though I guess it was only testing skew of different TSCs on the same system, not TSCs against an external time source.

I do not know the tsc accuracy.

Quoted 2 lines

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Regards,
Dave

-----Original Message-----
From: Dan Magenheimer [mailto:[email protected]]
Sent: Fri 6/6/2008 4:29 PM
To: Dave Winchell; Keir Fraser
Cc: Ben Guthro; xen-devel
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Dave --

Thanks much for posting the preliminary results!

While I am fully supportive of offering hardware hpet as an option for hvm guests (let's call it hwhpet=1 for shorthand), I am very surprised by your preliminary results; the most obvious conclusion is that Xen system time is losing time at the rate of 1000 PPM though its possible there's a bug somewhere else in the "time stack". Your Windows result is jaw-dropping and inexplicable, though I have to admit ignorance of how Windows manages time.

I think with my recent patch and hpet=1 (essentially the same as your emulated hpet), hvm guest time should track Xen system time. I wonder if domain0 (which if I understand correctly is directly using Xen system time) is also seeing an error of .1%? Also I wonder for the skew you are seeing (in both hvm guests and domain0) is time moving too fast or two slow?

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0. And if for some reason Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

One more thought... do you know the accuracy of the TSC crystals on your test systems? I posted a patch awhile ago that was intended to test that, though I guess it was only testing skew of different TSCs on the same system, not TSCs against an external time source.

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Thanks,
Dan

Quoted 8 lines

-----Original Message-----
From: Dave Winchell [mailto:[email protected]]
Sent: Friday, June 06, 2008 1:33 PM
To: [email protected]; Keir Fraser
Cc: Ben Guthro; xen-devel; Dave Winchell
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 5 lines

Dan, Keir:

Preliminary tests results indicate an error of .1% for Linux 64 bit guests configured
for hpet with xen-unstable as is. As we have discussed many

times, the

Quoted 2 lines

ntp requirement is .05%.
Tests on the patch we just submitted for hpet have

indicated errors of

Quoted 19 lines

.0012%
on this platform under similar test conditions and .03% on other platforms.

Windows vista64 has an error of 11% using hpet with the xen-unstable bits.
In an overnight test with our hpet patch, the Windows vista error was .008%.

The tests are with two or three guests on a physical node, all under load, and with
the ratio of vcpus to phys cpus > 1.

I will continue to run tests over the next few days.

thanks,
Dave

Quoted 7 lines
Quoted 3 lines · Dan Magenheimer

Hi Dave and Ben --

When running tests on xen-unstable (without your patch),

please ensure

Quoted 1 line

that hpet=1 is set in the hvm config and also I think

that when hpet

Quoted 29 lines
Quoted 1 line

is the clocksource on RHEL4-32, the clock IS resilient to

missed ticks

Quoted 1 line

so timer_mode should be 2 (vs when pit is the clocksource

on RHEL4-32,

Quoted 4 lines

all clock ticks must be delivered and so timer_mode should be 0).

Per

http://lists.xensource.com/archives/html/xen-devel/2008-06/msg
00098.html it's

Quoted 8 lines

my intent to clean this up, but I won't get to it until next week.

Thanks,
Dan

    -----Original Message-----
    *From:* [email protected]
    [mailto:[email protected]]*On

Behalf Of *Dave

Quoted 9 lines

Winchell
*Sent:* Friday, June 06, 2008 4:46 AM
*To:* Keir Fraser; Ben Guthro; xen-devel
*Cc:* [email protected]; Dave Winchell *Subject:* RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Keir,

I think the changes are required. We'll run some tests

today today so

Quoted 14 lines
Quoted 7 lines

that we have some data to talk about.

-Dave

-----Original Message-----
From: [email protected] on behalf

of Keir Fraser

Quoted 6 lines

Sent: Fri 6/6/2008 4:58 AM
To: Ben Guthro; xen-devel
Cc: [email protected]
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Are these patches needed now the timers are built on

Xen system

Quoted 13 lines
Quoted 2 lines

time rather
than host TSC? Dan has reported much better

time-keeping with his

Quoted 2 lines

patch
checked in, and it¹s for sure a lot less invasive than

this patchset.

Quoted 7 lines

     -- Keir

    On 5/6/08 15:59, "Ben Guthro" <[email protected]> wrote:

    >
Quoted 10 lines
Quoted 3 lines
Quoted 3 lines

1. Introduction

This patch improves the hpet based guest clock in

terms of drift and

Quoted 2 lines
Quoted 2 lines

monotonicity.
Prior to this work the drift with hpet was greater

than 2%, far

Quoted 3 lines

above the .05%
> limit
> for ntp to synchronize. With this code, the drift

ranges from

Quoted 5 lines
Quoted 5 lines

.001% to .0033%
> depending
> on guest and physical platform.
>
> Using hpet allows guest operating systems to

provide monotonic

Quoted 15 lines
Quoted 2 lines

time to their
> applications. Time sources other than hpet are not

monotonic because

Quoted 1 line
Quoted 1 line

of their reliance on tsc, which is not synchronized

across physical

Quoted 10 lines
Quoted 3 lines

processors.

Windows 2k864 and many Linux guests are supported with two

policies, one for
> guests
> that handle missed clock interrupts and the other for guests that require the
> correct number of interrupts.
>
> Guests may use hpet for the timing source even if

the physical

Quoted 8 lines
Quoted 8 lines

platform has no
> visible
> hpet. Migration is supported between physical machines which differ in
> physical
> hpet visibility.
>
> Most of the changes are in hpet.c. Two general

facilities are

Quoted 3 lines
Quoted 3 lines

added to track
> interrupt
> progress. The ideas here and the facilities would

be useful in

Quoted 8 lines
Quoted 8 lines

vpt.c, for
> other time
> sources, though no attempt is made here to improve vpt.c. >
> The following sections discuss hpet dependencies, interrupt delivery policies,
> live migration,
> test results, and relation to recent work with

monotonic time.

Quoted 11 lines
Quoted 5 lines
Quoted 5 lines

2. Virtual Hpet dependencies

The virtual hpet depends on the ability to read the

physical or

Quoted 2 lines

simulated
> (see discussion below) hpet. For timekeeping, the

virtual hpet

Quoted 2 lines

also depends
> on two new interrupt notification facilities to

implement its

Quoted 13 lines
Quoted 13 lines

policies for
> interrupt delivery.
>
> 2.1. Two modes of low-level hpet main counter reads. >
> In this implementation, the virtual hpet reads with read_64_main_counter(),
> exported by
> time.c, either the real physical hpet main counter register directly or a
> "simulated"
> hpet main counter.
>

Quoted 4 lines
Quoted 4 lines
Quoted 1 line

The simulated mode uses a monotonic version of get_s_time()

(NOW()), where the
> last
> time value is returned whenever the current time

value is less

Quoted 13 lines
Quoted 13 lines

than the last
> time
> value. In simulated mode, since it is layered on s_time, the underlying
> hardware
> can be hpet or some other device. The frequency of the main counter in
> simulated
> mode is the same as the standard physical hpet frequency, allowing live
> migration
> between nodes that are configured differently.
>

Quoted 5 lines
Quoted 1 line
Quoted 1 line

If the physical platform does not have an hpet

device, or if xen

Quoted 3 lines

is configured
> not
> to use the device, then the simulated method is

used. If there

Quoted 3 lines
Quoted 3 lines

is a physical
> hpet device,
> and xen has initialized it, then either simulated

or physical

Quoted 11 lines
Quoted 3 lines

mode can be
> used.
> This is governed by a boot time option, hpet-avoid.

Setting this

Quoted 3 lines

option to 1
> gives the
> simulated mode and 0 the physical mode. The default

is physical

Quoted 3 lines

mode.
>
> A disadvantage of the physical mode is that may

take longer to

Quoted 6 lines
Quoted 2 lines

read the device
> than in simulated mode. On some platforms the cost is

about the

Quoted 3 lines

same (less
> than 250 nsec) for
> physical and simulated modes, while on others

physical cost is

Quoted 3 lines
Quoted 3 lines

much higher
> than simulated.
> A disadvantage of the simulated mode is that it can

return the

Quoted 10 lines
Quoted 10 lines

same value
> for the counter in consecutive calls.
>
> 2.2. Interrupt notification facilities.
>
> Two interrupt notification facilities are introduced, one is > hvm_isa_irq_assert_cb()
> and the other hvm_register_intr_en_notif().
>
> The vhpet uses hvm_isa_irq_assert_cb to deliver

interrupts to

Quoted 2 lines
Quoted 2 lines

the vioapic.
> hvm_isa_irq_assert_cb allows a callback to be

passed along to

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

vioapic_deliver()
and this callback is called with a mask of the vcpus

which will

Quoted 5 lines

get the
> interrupt. This callback is made before any vcpus receive an interrupt.
>
> Vhpet uses hvm_register_intr_en_notif() to register

a handler

Quoted 4 lines
Quoted 4 lines

for a particular
> vector that will be called when that vector is injected in > [vmx,svm]_intr_assist()
> and also when the guest finishes handling the

interrupt. Here

Quoted 8 lines
Quoted 3 lines

finished is
> defined
> as the point when the guest re-enables interrupts or

lowers the

Quoted 4 lines

tpr value.
> EOI is not used as the end of interrupt as this is sometimes returned before
> the interrupt handler has done its work. A flag is

Quoted 12 lines

passed to the

Quoted 3 lines

handler
> indicating
> whether this is the injection point (post = 1) or the

interrupt

Quoted 7 lines

finished (post
> = 0) point.
> The need for the finished point callback is discussed in the missed ticks
> policy section.
>
> To prevent a possible early trigger of the finished

callback,

Quoted 13 lines
Quoted 6 lines

intr_en_notif
> logic
> has a two stage arm, the first at injection
(hvm_intr_en_notif_arm()) and the
> second when
> interrupts are seen to be disabled

(hvm_intr_en_notif_disarm()).

Quoted 3 lines

Once fully
> armed, re-enabling
> interrupts will cause hvm_intr_en_notif_disarm() to

make the end

Quoted 2 lines

of interrupt
> callback. hvm_intr_en_notif_arm() and

Quoted 9 lines

hvm_intr_en_notif_disarm()

Quoted 6 lines

are called by
> [vmx,svm]_intr_assist().
>
> 3. Interrupt delivery policies
>
> The existing hpet interrupt delivery is preserved.

This includes

Quoted 1 line
Quoted 1 line

vcpu round robin delivery used by Linux and

broadcast delivery

Quoted 4 lines
Quoted 4 lines

used by
> Windows.
>
> There are two policies for interrupt delivery, one

for Windows

Quoted 10 lines
Quoted 3 lines

2k8-64 and the
> other
> for Linux. The Linux policy takes advantage of the

(guest) Linux

Quoted 3 lines

missed tick
> and offset
> calculations and does not attempt to deliver the

right number of

Quoted 2 lines

interrupts.
> The Windows policy delivers the correct number of

interrupts,

Quoted 3 lines
Quoted 3 lines

even if
> sometimes much
> closer to each other than the period. The policies

are similar

Quoted 10 lines
Quoted 5 lines

to those in
> vpt.c, though
> there are some important differences. >
> Policies are selected with an HVMOP_set_param

hypercall with index

Quoted 2 lines
Quoted 2 lines

HVM_PARAM_TIMER_MODE.
Two new values are added,

HVM_HPET_guest_computes_missed_ticks and

Quoted 1 line
Quoted 1 line

HVM_HPET_guest_does_not_compute_missed_ticks. The

reason that

Quoted 3 lines
Quoted 3 lines

two new ones
> are added is that
> in some guests (32bit Linux) a no-missed policy is

needed for

Quoted 14 lines
Quoted 3 lines

clock sources
> other than hpet
> and a missed ticks policy for hpet. It was felt that

there would

Quoted 10 lines

be less
> confusion by simply
> introducing the two hpet policies.
>
> 3.1. The missed ticks policy
>
> The Linux clock interrupt handler for hpet calculates missed ticks and offset
> using the hpet
> main counter. The algorithm works well when the

time since the

Quoted 12 lines
Quoted 5 lines

last interrupt
> is greater than
> or equal to a period and poorly otherwise. >
> The missed ticks policy ensures that no two clock

interrupts are

Quoted 6 lines

delivered to
> the guest at
> a time interval less than a period. A time stamp (hpet main counter value) is
> recorded (by a
> callback registered with hvm_register_intr_en_notif)

Quoted 8 lines

when Linux

Quoted 3 lines

finishes
> handling the clock
> interrupt. Then, ensuing interrupts are delivered to

the vioapic

Quoted 3 lines

only if the
> current main
> counter value is a period greater than when the

last interrupt

Quoted 3 lines
Quoted 3 lines

was handled.
>
> Tests showed a significant improvement in clock

drift with end

Quoted 11 lines
Quoted 3 lines

of interrupt
> time stamps
> versus beginning of interrupt[1]. It is believed that

the reason

Quoted 3 lines

for the
> improvement
> is that the clock interrupt handler goes for a

spinlock and can

Quoted 3 lines

be therefore
> delayed in its
> processing. Furthermore, the main counter is read

by the guest

Quoted 11 lines
Quoted 11 lines

under the lock.
> The net
> effect is that if we time stamp injection, we can get the difference in time
> between successive interrupt handler lock acquisitions to be less than the
> period.
>
> 3.2. The no-missed ticks policy
>
> Windows 2k864 keeps very poor time with the missed

Quoted 23 lines

ticks policy.

Quoted 6 lines

So the
> no-missed ticks policy
> was developed. In the no-missed ticks policy we deliver the correct number of
> interrupts,
> even if they are spaced less than a period apart

(when catching up).

Quoted 15 lines
Quoted 2 lines

Windows 2k864 uses a broadcast mode in the interrupt routing

such that
> all vcpus get the clock interrupt. The best Windows drift performance was
> achieved when the
> policy code ensured that all the previous interrupts (on the various vcpus)
> had been injected
> before injecting the next interrupt to the vioapic.. >
> The policy code works as follows. It uses the hvm_isa_irq_assert_cb() to
> record
> the vcpus to be interrupted in

h->hpet.pending_mask. Then, in

Quoted 3 lines
Quoted 3 lines

the callback
> registered
> with hvm_register_intr_en_notif() at post=1 time it

clears the

Quoted 10 lines
Quoted 10 lines

current vcpu in
> the pending_mask.
> When the pending_mask is clear it decrements hpet.intr_pending_nr and if
> intr_pending_nr is still
> non-zero posts another interrupt to the ioapic with hvm_isa_irq_assert_cb().
> Intr_pending_nr is incremented in hpet_route_decision_not_missed_ticks().
>

Quoted 4 lines
Quoted 4 lines
Quoted 1 line

The missed ticks policy intr_en_notif callback also uses the

pending_mask
> method. So even though
> Linux does not broadcast its interrupts, the code

could handle

Quoted 2 lines
Quoted 2 lines

it if it did.
> In this case the end of interrupt time stamp is

made when the

Quoted 9 lines
Quoted 9 lines

pending_mask is
> clear.
>
> 4. Live Migration
>
> Live migration with hpet preserves the current offset of the guest clock with
> respect
> to ntp. This is accomplished by migrating all of

the state in

Quoted 8 lines
Quoted 8 lines

the h->hpet data
> structure
> in the usual way. The hp->mc_offset is recalculated on the receiving node so
> that the
> guest sees a continuous hpet main counter.
>
> Code as been added to xc_domain_save.c to send a

small message

Quoted 5 lines
Quoted 5 lines

after the
> domain context is sent. The contents of the message is the physical tsc
> timestamp, last_tsc,
> read just before the message is sent. When the

Quoted 31 lines

last_tsc message

Quoted 30 lines

is received in
> xc_domain_restore.c,
> another physical tsc timestamp, cur_tsc, is read. The two timestamps are
> loaded into the domain
> structure as last_tsc_sender and first_tsc_receiver with hypercalls. Then
> xc_domain_hvm_setcontext
> is called so that hpet_load has access to these time stamps. Hpet_load uses
> the timestamps
> to account for the time spent saving and loading the domain context. With this
> technique,
> the only neglected time is the time spent sending a small network message.
>
> 5. Test Results
>
> Some recent test results are:
>
> 5.1 Linux 4u664 and Windows 2k864 load test.
> Duration: 70 hours.
> Test date: 6/2/08
> Loads: usex -b48 on Linux; burn-in on Windows
> Guest vcpus: 8 for Linux; 2 for Windows
> Hardware: 8 physical cpu AMD
> Clock drift : Linux: .0012% Windows: .009%
>
> 5.2 Linux 4u664, Linux 4u464 , and Windows 2k864

no-load test

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Duration: 23 hours.
Test date: 6/3/08
Loads: none
Guest vcpus: 8 for each Linux; 2 for Windows
Hardware: 4 physical cpu AMD
Clock drift : Linux: .033% Windows: .019%

Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

6. Relation to recent work in xen-unstable

There is a similarity between hvm_get_guest_time() in

xen-unstable and
> read_64_main_counter()
> in this code. However, read_64_main_counter() is

more tuned to

Quoted 13 lines
Quoted 3 lines

the needs of
> hpet.c. It has no
> "set" operation, only the get. It isolates the mode,

physical or

Quoted 6 lines

simulated, in
> read_64_main_counter()
> itself. It uses no vcpu or domain state as it is a physical entity, in either
> mode. And it provides a real
> physical mode for every read for those applications

that desire

Quoted 2 lines
    this.
    >
Quoted 11 lines
Quoted 3 lines
Quoted 3 lines

7. Conclusion

The virtual hpet is improved by this patch in terms

of accuracy and

Quoted 2 lines
Quoted 2 lines

monotonicity.
Tests performed to date verify this and more testing

is under way.

Quoted 4 lines
Quoted 4 lines

8. Future Work

Testing with Windows Vista will be performed soon.

The reason

Quoted 3 lines
Quoted 3 lines

for accuracy
> variations
> on different platforms using the physical hpet

device will be

Quoted 2 lines
Quoted 2 lines

investigated.
> Additional overhead measurements on simulated vs

physical hpet

Quoted 13 lines
Quoted 6 lines
    mode will be
    > made.
    >
    > Footnotes:
    >
    > 1. I don't recall the accuracy improvement with end

of interrupt

Quoted 6 lines

stamping, but
> it was
> significant, perhaps better than two to one improvement. It would be a very
> simple matter
> to re-measure the improvement as the facility can

call back at

Quoted 19 lines
Quoted 17 lines
    injection time
    > as well.
    >
    >
    > Signed-off-by: Dave Winchell <[email protected]>
    > <mailto:[email protected]>
    > Signed-off-by: Ben Guthro <[email protected]>
    > <mailto:[email protected]>
    >
    >
    > _______________________________________________
    > Xen-devel mailing list
    > [email protected]
    > http://lists.xensource.com/xen-devel
#35 lore raw
In reply to Dave Winchell

I implemented the monotonicity guarantee within hvm_get_guest_time(). We don't need or want get_s_time_mono().

-- Keir

Quoted 45 lines · Dave Winchell

Keir, Dan:

Although I plan to break up the patch, etc., I'm posting this fix to the patch for anyone who might be interested.

thanks,
Dave
# This is a BitKeeper generated diff -Nru style patch. #
# ChangeSet
# 2008/06/10 12:20:48-04:00 [email protected]
# vi-patch: xen-hpet
#
# Bug Id: 6057
#
# Reviewed by: Robert
#
# SUMMARY: Fix wrap issue in monotonic s_time().
#

# xen/arch/x86/time.c
#   2008/06/10 12:20:48-04:00 [email protected] +3 -2
#   Fix wrap issue in monotonic s_time().
# 
diff -Nru a/xen/arch/x86/time.c b/xen/arch/x86/time.c
xen/arch/x86/time.c 2008-06-10 13:08:39 -04:00 +3 -2
@@ -534,7 +534,7 @@
     u64 count;
     unsigned long flags;
     struct cpu_time *t = &this_cpu(cpu_time);
-    u64 tsc, delta;
+    u64 tsc, delta, diff;
     s_time_t now;
 
     if(hpet_main_counter_phys_avoid_hdw || !hpet_physical_inited) {
@@ -542,7 +542,8 @@
         rdtscll(tsc);
         delta = tsc - t->local_tsc_stamp;
         now = t->stime_local_stamp + scale_delta(delta, &t->tsc_scale);
-        if(now > get_s_time_mon.last_ret)
+        diff = (u64)now - (u64)get_s_time_mon.last_ret;
+        if((s64)diff > (s64)0)
             get_s_time_mon.last_ret = now;
         else
             now = get_s_time_mon.last_ret;
#36 lore raw
Quoted 2 lines

I implemented the monotonicity guarantee within hvm_get_guest_time(). We don't need or want get_s_time_mono().

I'll give hvm_get_guest_time() another look.

-Dave

-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Wed 6/11/2008 4:30 AM
To: Dave Winchell
Cc: [email protected]; Ben Guthro; xen-devel
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

I implemented the monotonicity guarantee within hvm_get_guest_time(). We don't need or want get_s_time_mono().

-- Keir

Quoted 36 lines · Dave Winchell

Keir, Dan:

Although I plan to break up the patch, etc., I'm posting this fix to the patch for anyone who might be interested.

thanks,
Dave
# This is a BitKeeper generated diff -Nru style patch. #
# ChangeSet
# 2008/06/10 12:20:48-04:00 [email protected]
# vi-patch: xen-hpet
#
# Bug Id: 6057
#
# Reviewed by: Robert
#
# SUMMARY: Fix wrap issue in monotonic s_time().
#

# xen/arch/x86/time.c
#   2008/06/10 12:20:48-04:00 [email protected] +3 -2
#   Fix wrap issue in monotonic s_time().
# 
diff -Nru a/xen/arch/x86/time.c b/xen/arch/x86/time.c
xen/arch/x86/time.c 2008-06-10 13:08:39 -04:00 +1 -1
@@ -534,7 +534,7 @@
     u64 count;
     unsigned long flags;
     struct cpu_time *t = &this_cpu(cpu_time);
-    u64 tsc, delta;
+    u64 tsc, delta, diff;
     s_time_t now;
 
     if(hpet_main_counter_phys_avoid_hdw || !hpet_physical_inited) {

xen/arch/x86/time.c 2008-06-10 13:08:39 -04:00 line 549

Quoted 9 lines
xen/arch/x86/time.c 2008-06-10 13:08:39 -04:00 +2 -1
@@ -542,7 +542,8 @@
         rdtscll(tsc);
         delta = tsc - t->local_tsc_stamp;
         now = t->stime_local_stamp + scale_delta(delta, &t->tsc_scale);
-        if(now > get_s_time_mon.last_ret)
+        diff = (u64)now - (u64)get_s_time_mon.last_ret;
+        if((s64)diff > (s64)0)
             get_s_time_mon.last_ret = now;
         else
             now = get_s_time_mon.last_ret;

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

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

In EL5u1-32 however it looks like the fractions are accounted for. Indeed the EL5u1-32 "lost tick handling" code resembles the Linux/ia64 code which is what I've always assumed was the "missed tick" model. In this case, I think no policy is necessary and the measured skew should be identical to any physical hpet skew. I'll have to test this hypothesis though.

Quoted 7 lines

I've tested this hypothesis and it seems to hold true. This means the existing (unpatched) hpet code works fine on EL5-32bit (vcpus=1) when hpet is the clocksource,
even when the machine is overcommitted. A second hypothesis still needs to be tested that Dave's patch will not make this worse.

Interesting, thanks for pointing this out and confirming.

Quoted 6 lines

(Note that per previous discussion, my EL5u1-32bit guest running on an Intel dual-core physical box chose tsc as the best clocksource and I had to override it with clock=hpet in the kernel command line.)

Is there one setting for all Linux guests that makes them choose hpet? Is it "clock=hpet clocksource=hpet"? I know you wrote at length about this before.

Quoted 11 lines
Quoted 10 lines

Yes, that makes sense and concurs with what I remember from the EL4u5-32 code. If this is true, one would expect the default "no missed tick" policy to see time moving faster than an external source -- the first missed tick delivered after a long sleep would "catch up" and then the remainder would each add another tick.

Quoted 5 lines

Indeed with the existing (unpatched) hpet code, time is running faster on EL4u5-32 (vcpus=1, when overcommited). So Dave's patch is definitely needed here.

Its good to get the verification of this.

thanks,
Dave

Quoted 1,273 lines

Will try 64-bit next.

Dan

Quoted 1,235 lines

-----Original Message-----
From: Dan Magenheimer [mailto:[email protected]]
Sent: Monday, June 09, 2008 9:21 PM
To: 'Dave Winchell'; 'Keir Fraser'
Cc: 'xen-devel'; 'Ben Guthro'
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 5 lines

I'll tell you what I recall about this. Tomorrow I'll check the guest code to verify. I think that Linux declares a full tick, even if the interrupt is early. That's the problem.

Yes, that makes sense and concurs with what I remember from the EL4u5-32 code. If this is true, one would expect the default "no missed tick" policy to see time moving faster than an external source -- the first missed tick delivered after a long sleep would "catch up" and then the remainder would each add another tick.

Quoted 4 lines

On the other hand, if the interrupt is late it in effect declares a tick plus fraction. If it just declared the fraction in

the first place,

Quoted 3 lines

we could deliver the interrupts whenever we wanted.

My read of the EL4u5-32 code is that the fraction is discarded and a new tick period commences at "now", so the fractions eventually accumulate as lost time.

In EL5u1-32 however it looks like the fractions are accounted for. Indeed the EL5u1-32 "lost tick handling" code resembles the Linux/ia64 code which is what I've always assumed was the "missed tick" model. In this case, I think no policy is necessary and the measured skew should be identical to any physical hpet skew. I'll have to test this hypothesis though.

-----Original Message-----
From: [email protected] [mailto:[email protected]]On Behalf Of Dave Winchell
Sent: Monday, June 09, 2008 5:35 PM
To: [email protected]; Keir Fraser
Cc: Dave Winchell; xen-devel; Ben Guthro
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 4 lines
Quoted 4 lines

The Linux policy is more subtle, but is required to go from .1% to .03%.

Quoted 9 lines

Thanks for the good documentation which I hadn't thoroughly read until now.
I now understand that the essence of your
hpet missed ticks policy is to ensure that ticks are never delivered too close together. But I'm trying to understand WHY your patch works, in other words, what problem it is countering.

I'll tell you what I recall about this. Tomorrow I'll check the guest code to verify. I think that Linux declares a full tick, even if the interrupt is early. That's the problem.
On the other hand, if the interrupt is late it in effect declares a tick plus fraction. If it just declared the fraction in the first place, we could deliver the interrupts whenever we wanted.

Its really not that different than the missed ticks policy in vpt.c except that there the period in vpt.c is based on start of interrupt and I have improved that with end-of interrupt as described in the patch note.

I don't recall what prompted me to try end-of-interrupt, but I saw a significant improvement. I may have been running a monotonicity test at the same time to explain the lock contention mentioned in the write-up.

Quoted 6 lines

I care about this for more reasons than just
because it is interesting: (1) I'd like to feel confident that it is fixing a bug rather than just a symptom of a bug; and (2) I wonder how universally it is applicable.

Its worked well my my small set of guests. You and our QA are going to tell us about the wider set. It doesn't matter if guest A handles interrupts closely spaced or not, just whether it handles them far apart. So it should be pretty universal with guests that really handle missed ticks. I think its interesting that some 32bit Linux guests handle missed ticks for hpet.

Quoted 9 lines

I see from code examination in mark_offset_hpet() in RHEL4u5/arch/i386/kernel/timers/timer_hpet.c, that the correction for lost ticks is just plain wrong in a virtual environment. (Suppose for example that a virtual tick was delivered every 1.999*hpet_tick... I think the clock would be off by 50%!) Is this the bug that is being "countered" by your policy?

I haven't looked at that code, perhaps.
I'll check it tomorrow.

Quoted 9 lines

However, the lost tick handling in RHEL5u1/kernel/timer.c (which I think is used also for hpet) is much better so I am eager to find out if your policy works there too.
If the hpet missed tick policy works for both, though, I should be happy, though I wonder about upstream kernels (e.g. the trend toward tickless).

I wasn't aware of this trend. If its robust, however, it should handle late interrupts ...

Quoted 5 lines

That said, I'd rather
see this get into Xen 3.3 and worry about upstream kernels later :-)

Regards,
Dave

-----Original Message-----
From: Dan Magenheimer [mailto:[email protected]]
Sent: Mon 6/9/2008 6:02 PM
To: Dave Winchell; Keir Fraser
Cc: Ben Guthro; xen-devel
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 4 lines

The Linux policy is more subtle, but is required to go from .1% to .03%.

Thanks for the good documentation which I hadn't thoroughly read until now. I now understand that the essence of your hpet missed ticks policy is to ensure that ticks are never delivered too close together. But I'm trying to understand WHY your patch works, in other words, what problem it is countering. I care about this for more reasons than just because it is interesting: (1) I'd like to feel confident that it is fixing a bug rather than just a symptom of a bug; and (2) I wonder how universally it is applicable.

I see from code examination in mark_offset_hpet() in RHEL4u5/arch/i386/kernel/timers/timer_hpet.c, that the correction for lost ticks is just plain wrong in a virtual environment. (Suppose for example that a virtual tick was delivered every 1.999*hpet_tick... I think the clock would be off by 50%!) Is this the bug that is being "countered" by your policy?

However, the lost tick handling in RHEL5u1/kernel/timer.c (which I think is used also for hpet) is much better so I am eager to find out if your policy works there too.

If the hpet missed tick policy works for both, though, I should be happy, though I wonder about upstream kernels (e.g. the trend toward tickless). That said, I'd rather see this get into Xen 3.3 and worry about upstream kernels later :-)

-----Original Message-----
From: Dave Winchell [mailto:[email protected]]
Sent: Sunday, June 08, 2008 2:32 PM
To: [email protected]; Keir Fraser
Cc: Ben Guthro; xen-devel; Dave Winchell
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Hi Dan,

Quoted 9 lines

While I am fully supportive of offering hardware hpet as an option for hvm guests (let's call it hwhpet=1 for shorthand), I am very surprised by your preliminary results; the most obvious conclusion is that Xen system time is losing time at the rate of 1000 PPM though its possible there's a bug somewhere else in the "time stack". Your Windows result is jaw-dropping and inexplicable, though I have to admit ignorance of how Windows manages time.

I think xen system time is fine. You have to add the interrupt delivery policies decribed in the write-up for the patch to get accurate timekeeping in the guest.

The windows policy is obvious and results in a large improvement in accuracy. The Linux policy is more subtle, but is required to go from .1% to .03%.

Quoted 8 lines

I think with my recent patch and hpet=1 (essentially the same as your emulated hpet), hvm guest time should track Xen system time. I wonder if domain0 (which if I understand correctly is directly using Xen system time) is also seeing an error of .1%? Also I wonder for the skew you are seeing (in both hvm guests and domain0) is time moving too fast or two slow?

I don't recall the direction. I can look it up in my notes at work tomorrow.

Quoted 6 lines

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0.

Our patch is accurate to < .03% using the physical hpet mode or the simulated mode.

Quoted 7 lines

And if for some reason
Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

In our experience, Xen system time is accurate enough now.

Quoted 7 lines

One more thought... do you know the accuracy of the TSC crystals on your test systems? I posted a patch awhile ago that was intended to test that, though I guess it was only testing skew of different TSCs on the same system, not TSCs against an external time source.

I do not know the tsc accuracy.

Quoted 4 lines

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Regards,
Dave

-----Original Message-----
From: Dan Magenheimer [mailto:[email protected]]
Sent: Fri 6/6/2008 4:29 PM
To: Dave Winchell; Keir Fraser
Cc: Ben Guthro; xen-devel
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Dave --

Thanks much for posting the preliminary results!

While I am fully supportive of offering hardware hpet as an option for hvm guests (let's call it hwhpet=1 for shorthand), I am very surprised by your preliminary results; the most obvious conclusion is that Xen system time is losing time at the rate of 1000 PPM though its possible there's a bug somewhere else in the "time stack". Your Windows result is jaw-dropping and inexplicable, though I have to admit ignorance of how Windows manages time.

I think with my recent patch and hpet=1 (essentially the same as your emulated hpet), hvm guest time should track Xen system time. I wonder if domain0 (which if I understand correctly is directly using Xen system time) is also seeing an error of .1%? Also I wonder for the skew you are seeing (in both hvm guests and domain0) is time moving too fast or two slow?

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0. And if for some reason Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

One more thought... do you know the accuracy of the TSC crystals on your test systems? I posted a patch awhile ago that was intended to test that, though I guess it was only testing skew of different TSCs on the same system, not TSCs against an external time source.

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Thanks,
Dan

Quoted 8 lines

-----Original Message-----
From: Dave Winchell [mailto:[email protected]]
Sent: Friday, June 06, 2008 1:33 PM
To: [email protected]; Keir Fraser
Cc: Ben Guthro; xen-devel; Dave Winchell
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 7 lines

Dan, Keir:

Preliminary tests results indicate an error of .1% for Linux 64 bit guests configured
for hpet with xen-unstable as is. As we have discussed many

times, the

Quoted 4 lines

ntp requirement is .05%.
Tests on the patch we just submitted for hpet have

indicated errors of

Quoted 23 lines

.0012%
on this platform under similar test conditions and .03% on other platforms.

Windows vista64 has an error of 11% using hpet with the xen-unstable bits.
In an overnight test with our hpet patch, the Windows vista error was .008%.

The tests are with two or three guests on a physical node, all under load, and with
the ratio of vcpus to phys cpus > 1.

I will continue to run tests over the next few days.

thanks,
Dave

Dan Magenheimer wrote:

Quoted 11 lines
Quoted 5 lines

Hi Dave and Ben --

When running tests on xen-unstable (without your patch),

please ensure

Quoted 3 lines

that hpet=1 is set in the hvm config and also I think

that when hpet

Quoted 51 lines
Quoted 3 lines

is the clocksource on RHEL4-32, the clock IS resilient to

missed ticks

Quoted 3 lines

so timer_mode should be 2 (vs when pit is the clocksource

on RHEL4-32,

Quoted 6 lines

all clock ticks must be delivered and so timer_mode should be 0).

Per

http://lists.xensource.com/archives/html/xen-devel/2008-06/msg
00098.html it's

Quoted 10 lines

my intent to clean this up, but I won't get to it until next week.

Thanks,
Dan

   -----Original Message-----
   *From:* [email protected]
   [mailto:[email protected]]*On

Behalf Of *Dave

Quoted 11 lines

Winchell
*Sent:* Friday, June 06, 2008 4:46 AM
*To:* Keir Fraser; Ben Guthro; xen-devel
*Cc:* [email protected]; Dave Winchell *Subject:* RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Keir,

I think the changes are required. We'll run some tests

today today so

Quoted 2 lines

that we have some data to talk about.

Quoted 18 lines
Quoted 7 lines

-Dave

-----Original Message-----
From: [email protected] on behalf

of Keir Fraser

Quoted 8 lines

Sent: Fri 6/6/2008 4:58 AM
To: Ben Guthro; xen-devel
Cc: [email protected]
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Are these patches needed now the timers are built on

Xen system

Quoted 24 lines
Quoted 4 lines

time rather
than host TSC? Dan has reported much better

time-keeping with his

Quoted 4 lines

patch
checked in, and it¹s for sure a lot less invasive than

this patchset.

Quoted 10 lines
    -- Keir
   On 5/6/08 15:59, "Ben Guthro" <[email protected]> wrote:

   >
   > 1. Introduction
   >
   > This patch improves the hpet based guest clock in
Quoted 15 lines

terms of drift and

Quoted 4 lines
Quoted 2 lines

monotonicity.
Prior to this work the drift with hpet was greater

than 2%, far

Quoted 5 lines

above the .05%

Quoted 2 lines

limit
for ntp to synchronize. With this code, the drift

ranges from

Quoted 7 lines
Quoted 7 lines

.001% to .0033%

Quoted 4 lines

depending
on guest and physical platform.

Using hpet allows guest operating systems to

provide monotonic

Quoted 4 lines
Quoted 4 lines

time to their

Quoted 1 line

applications. Time sources other than hpet are not

Quoted 21 lines

monotonic because

Quoted 3 lines
Quoted 1 line

of their reliance on tsc, which is not synchronized

across physical

Quoted 12 lines
Quoted 3 lines

processors.

Windows 2k864 and many Linux guests are supported with two

policies, one for

Quoted 2 lines

guests
that handle missed clock interrupts and the other for guests

that require the

Quoted 3 lines

correct number of interrupts.

Guests may use hpet for the timing source even if

the physical

Quoted 10 lines
Quoted 10 lines

platform has no

Quoted 2 lines

visible
hpet. Migration is supported between physical machines which

differ in

Quoted 4 lines

physical
hpet visibility.

Most of the changes are in hpet.c. Two general

facilities are

Quoted 5 lines
Quoted 5 lines

added to track

Quoted 2 lines

interrupt
progress. The ideas here and the facilities would

be useful in

Quoted 10 lines
Quoted 10 lines

vpt.c, for

Quoted 4 lines

other time
sources, though no attempt is made here to improve vpt.c.

The following sections discuss hpet dependencies, interrupt

delivery policies,

Quoted 2 lines

live migration,
test results, and relation to recent work with

monotonic time.

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

2. Virtual Hpet dependencies

The virtual hpet depends on the ability to read the

Quoted 14 lines

physical or

Quoted 4 lines

simulated

Quoted 1 line

(see discussion below) hpet. For timekeeping, the

virtual hpet

Quoted 4 lines

also depends

Quoted 1 line

on two new interrupt notification facilities to

implement its

Quoted 13 lines
Quoted 13 lines

policies for

Quoted 5 lines

interrupt delivery.

2.1. Two modes of low-level hpet main counter reads.

In this implementation, the virtual hpet reads with

read_64_main_counter(),

Quoted 2 lines

exported by
time.c, either the real physical hpet main counter register

directly or a

Quoted 3 lines

"simulated"
hpet main counter.

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

The simulated mode uses a monotonic version of get_s_time()

(NOW()), where the

Quoted 2 lines

last
time value is returned whenever the current time

value is less

Quoted 13 lines
Quoted 13 lines

than the last

Quoted 2 lines

time
value. In simulated mode, since it is layered on s_time, the

underlying

Quoted 2 lines

hardware
can be hpet or some other device. The frequency of the main

counter in

Quoted 2 lines

simulated
mode is the same as the standard physical hpet frequency,

allowing live

Quoted 3 lines

migration
between nodes that are configured differently.

Quoted 11 lines
Quoted 3 lines
Quoted 1 line

If the physical platform does not have an hpet

device, or if xen

Quoted 5 lines

is configured

Quoted 2 lines

not
to use the device, then the simulated method is

used. If there

Quoted 5 lines
Quoted 5 lines

is a physical

Quoted 2 lines

hpet device,
and xen has initialized it, then either simulated

or physical

Quoted 8 lines
Quoted 5 lines

mode can be

Quoted 2 lines

used.
This is governed by a boot time option, hpet-avoid.

Setting this

Quoted 13 lines
Quoted 5 lines

option to 1

Quoted 2 lines

gives the
simulated mode and 0 the physical mode. The default

is physical

Quoted 5 lines

mode.

Quoted 2 lines

A disadvantage of the physical mode is that may

take longer to

Quoted 4 lines
Quoted 4 lines

read the device

Quoted 1 line

than in simulated mode. On some platforms the cost is

Quoted 8 lines

about the

Quoted 5 lines

same (less

Quoted 2 lines

than 250 nsec) for
physical and simulated modes, while on others

physical cost is

Quoted 5 lines
Quoted 5 lines

much higher

Quoted 2 lines

than simulated.
A disadvantage of the simulated mode is that it can

return the

Quoted 5 lines
Quoted 5 lines

same value

Quoted 4 lines

for the counter in consecutive calls.

2.2. Interrupt notification facilities.

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

Two interrupt notification facilities are introduced, one is hvm_isa_irq_assert_cb()
and the other hvm_register_intr_en_notif().

The vhpet uses hvm_isa_irq_assert_cb to deliver

interrupts to

Quoted 4 lines
Quoted 4 lines

the vioapic.

Quoted 1 line

hvm_isa_irq_assert_cb allows a callback to be

passed along to

Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

vioapic_deliver()
and this callback is called with a mask of the vcpus

Quoted 10 lines

which will

Quoted 7 lines

get the

Quoted 1 line

interrupt. This callback is made before any vcpus receive an

interrupt.

Quoted 2 lines

Vhpet uses hvm_register_intr_en_notif() to register

a handler

Quoted 6 lines
Quoted 6 lines

for a particular

Quoted 3 lines

vector that will be called when that vector is injected in [vmx,svm]_intr_assist()
and also when the guest finishes handling the

interrupt. Here

Quoted 17 lines
Quoted 5 lines

finished is

Quoted 2 lines

defined
as the point when the guest re-enables interrupts or

lowers the

Quoted 6 lines

tpr value.

Quoted 1 line

EOI is not used as the end of interrupt as this is sometimes

returned before

Quoted 1 line

the interrupt handler has done its work. A flag is

passed to the

Quoted 17 lines
Quoted 5 lines

handler

Quoted 2 lines

indicating
whether this is the injection point (post = 1) or the

interrupt

Quoted 9 lines

finished (post

Quoted 2 lines

= 0) point.
The need for the finished point callback is discussed in the

missed ticks

Quoted 3 lines

policy section.

To prevent a possible early trigger of the finished

callback,

Quoted 29 lines
Quoted 8 lines

intr_en_notif

Quoted 2 lines

logic
has a two stage arm, the first at injection

(hvm_intr_en_notif_arm()) and the

Quoted 2 lines

second when
interrupts are seen to be disabled

(hvm_intr_en_notif_disarm()).

Quoted 5 lines

Once fully

Quoted 2 lines

armed, re-enabling
interrupts will cause hvm_intr_en_notif_disarm() to

make the end

Quoted 4 lines

of interrupt

Quoted 1 line

callback. hvm_intr_en_notif_arm() and

hvm_intr_en_notif_disarm()

Quoted 3 lines
   are called by
   > [vmx,svm]_intr_assist().
   >
Quoted 11 lines
Quoted 5 lines
Quoted 3 lines

3. Interrupt delivery policies

The existing hpet interrupt delivery is preserved.

This includes

Quoted 3 lines
Quoted 1 line

vcpu round robin delivery used by Linux and

broadcast delivery

Quoted 6 lines
Quoted 6 lines

used by

Quoted 3 lines

Windows.

There are two policies for interrupt delivery, one

for Windows

Quoted 8 lines
Quoted 5 lines

2k8-64 and the

Quoted 2 lines

other
for Linux. The Linux policy takes advantage of the

(guest) Linux

Quoted 12 lines
Quoted 5 lines

missed tick

Quoted 2 lines

and offset
calculations and does not attempt to deliver the

right number of

Quoted 4 lines

interrupts.

Quoted 1 line

The Windows policy delivers the correct number of

interrupts,

Quoted 5 lines
Quoted 5 lines

even if

Quoted 2 lines

sometimes much
closer to each other than the period. The policies

are similar

Quoted 7 lines
Quoted 7 lines

to those in

Quoted 4 lines

vpt.c, though
there are some important differences.

Policies are selected with an HVMOP_set_param

Quoted 13 lines

hypercall with index

Quoted 4 lines
Quoted 2 lines

HVM_PARAM_TIMER_MODE.
Two new values are added,

HVM_HPET_guest_computes_missed_ticks and

Quoted 3 lines
Quoted 1 line

HVM_HPET_guest_does_not_compute_missed_ticks. The

reason that

Quoted 5 lines
Quoted 5 lines

two new ones

Quoted 2 lines

are added is that
in some guests (32bit Linux) a no-missed policy is

needed for

Quoted 5 lines
Quoted 5 lines

clock sources

Quoted 2 lines

other than hpet
and a missed ticks policy for hpet. It was felt that

Quoted 15 lines

there would

Quoted 12 lines

be less

Quoted 6 lines

confusion by simply
introducing the two hpet policies.

3.1. The missed ticks policy

The Linux clock interrupt handler for hpet calculates missed

ticks and offset

Quoted 2 lines

using the hpet
main counter. The algorithm works well when the

time since the

Quoted 21 lines
Quoted 7 lines

last interrupt

Quoted 4 lines

is greater than
or equal to a period and poorly otherwise.

The missed ticks policy ensures that no two clock

interrupts are

Quoted 8 lines

delivered to

Quoted 2 lines

the guest at
a time interval less than a period. A time stamp (hpet main

counter value) is

Quoted 2 lines

recorded (by a
callback registered with hvm_register_intr_en_notif)

when Linux

Quoted 13 lines
Quoted 5 lines

finishes

Quoted 2 lines

handling the clock
interrupt. Then, ensuing interrupts are delivered to

the vioapic

Quoted 5 lines

only if the

Quoted 2 lines

current main
counter value is a period greater than when the

last interrupt

Quoted 5 lines
Quoted 5 lines

was handled.

Quoted 2 lines

Tests showed a significant improvement in clock

drift with end

Quoted 8 lines
Quoted 5 lines

of interrupt

Quoted 2 lines

time stamps
versus beginning of interrupt[1]. It is believed that

the reason

Quoted 13 lines
Quoted 5 lines

for the

Quoted 2 lines

improvement
is that the clock interrupt handler goes for a

spinlock and can

Quoted 5 lines

be therefore

Quoted 2 lines

delayed in its
processing. Furthermore, the main counter is read

by the guest

Quoted 24 lines
Quoted 13 lines

under the lock.

Quoted 2 lines

The net
effect is that if we time stamp injection, we can get the

difference in time

Quoted 1 line

between successive interrupt handler lock acquisitions to be

less than the

Quoted 5 lines

period.

3.2. The no-missed ticks policy

Windows 2k864 keeps very poor time with the missed

ticks policy.

Quoted 8 lines

So the

Quoted 2 lines

no-missed ticks policy
was developed. In the no-missed ticks policy we deliver the

correct number of

Quoted 2 lines

interrupts,
even if they are spaced less than a period apart

Quoted 20 lines

(when catching up).

Quoted 17 lines
Quoted 2 lines

Windows 2k864 uses a broadcast mode in the interrupt routing

such that

Quoted 1 line

all vcpus get the clock interrupt. The best Windows drift

performance was

Quoted 2 lines

achieved when the
policy code ensured that all the previous interrupts (on the

various vcpus)

Quoted 4 lines

had been injected
before injecting the next interrupt to the vioapic..

The policy code works as follows. It uses the

hvm_isa_irq_assert_cb() to

Quoted 2 lines

record
the vcpus to be interrupted in

h->hpet.pending_mask. Then, in

Quoted 5 lines
Quoted 5 lines

the callback

Quoted 2 lines

registered
with hvm_register_intr_en_notif() at post=1 time it

clears the

Quoted 10 lines
Quoted 10 lines

current vcpu in

Quoted 2 lines

the pending_mask.
When the pending_mask is clear it decrements

hpet.intr_pending_nr and if

Quoted 2 lines

intr_pending_nr is still
non-zero posts another interrupt to the ioapic with

hvm_isa_irq_assert_cb().

Quoted 1 line

Intr_pending_nr is incremented in

hpet_route_decision_not_missed_ticks().

Quoted 1 line
Quoted 6 lines
Quoted 6 lines
Quoted 1 line

The missed ticks policy intr_en_notif callback also uses the

pending_mask

Quoted 2 lines

method. So even though
Linux does not broadcast its interrupts, the code

could handle

Quoted 4 lines
Quoted 4 lines

it if it did.

Quoted 1 line

In this case the end of interrupt time stamp is

made when the

Quoted 11 lines
Quoted 11 lines

pending_mask is

Quoted 5 lines

clear.

4. Live Migration

Live migration with hpet preserves the current offset of the

guest clock with

Quoted 2 lines

respect
to ntp. This is accomplished by migrating all of

the state in

Quoted 10 lines
Quoted 10 lines

the h->hpet data

Quoted 2 lines

structure
in the usual way. The hp->mc_offset is recalculated on the

receiving node so

Quoted 4 lines

that the
guest sees a continuous hpet main counter.

Code as been added to xc_domain_save.c to send a

small message

Quoted 7 lines
Quoted 7 lines

after the

Quoted 1 line

domain context is sent. The contents of the message is the

physical tsc

Quoted 2 lines

timestamp, last_tsc,
read just before the message is sent. When the

Quoted 35 lines

last_tsc message

Quoted 32 lines

is received in

Quoted 2 lines

xc_domain_restore.c,
another physical tsc timestamp, cur_tsc, is read. The two

timestamps are

Quoted 2 lines

loaded into the domain
structure as last_tsc_sender and first_tsc_receiver with

hypercalls. Then

Quoted 2 lines

xc_domain_hvm_setcontext
is called so that hpet_load has access to these time stamps.

Hpet_load uses

Quoted 2 lines

the timestamps
to account for the time spent saving and loading the domain

context. With this

Quoted 2 lines

technique,
the only neglected time is the time spent sending a small

network message.

Quoted 14 lines

5. Test Results

Some recent test results are:

5.1 Linux 4u664 and Windows 2k864 load test.
Duration: 70 hours.
Test date: 6/2/08
Loads: usex -b48 on Linux; burn-in on Windows Guest vcpus: 8 for Linux; 2 for Windows
Hardware: 8 physical cpu AMD
Clock drift : Linux: .0012% Windows: .009%

5.2 Linux 4u664, Linux 4u464 , and Windows 2k864

no-load test

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Duration: 23 hours.
Test date: 6/3/08
Loads: none
Guest vcpus: 8 for each Linux; 2 for Windows
Hardware: 4 physical cpu AMD
Clock drift : Linux: .033% Windows: .019%

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

6. Relation to recent work in xen-unstable

There is a similarity between hvm_get_guest_time() in

xen-unstable and

Quoted 2 lines

read_64_main_counter()
in this code. However, read_64_main_counter() is

more tuned to

Quoted 26 lines
Quoted 5 lines

the needs of

Quoted 2 lines

hpet.c. It has no
"set" operation, only the get. It isolates the mode,

physical or

Quoted 8 lines

simulated, in

Quoted 2 lines

read_64_main_counter()
itself. It uses no vcpu or domain state as it is a physical

entity, in either

Quoted 2 lines

mode. And it provides a real
physical mode for every read for those applications

that desire

Quoted 7 lines

this.

Quoted 4 lines

7. Conclusion

The virtual hpet is improved by this patch in terms

Quoted 16 lines

of accuracy and

Quoted 4 lines
Quoted 2 lines

monotonicity.
Tests performed to date verify this and more testing

is under way.

Quoted 6 lines
Quoted 4 lines

8. Future Work

Testing with Windows Vista will be performed soon.

The reason

Quoted 5 lines
Quoted 5 lines

for accuracy

Quoted 2 lines

variations
on different platforms using the physical hpet

device will be

Quoted 4 lines
Quoted 4 lines

investigated.

Quoted 1 line

Additional overhead measurements on simulated vs

physical hpet

Quoted 19 lines
Quoted 8 lines
   mode will be
   > made.
   >
   > Footnotes:
   >
   > 1. I don't recall the accuracy improvement with end

of interrupt

Quoted 8 lines

stamping, but

Quoted 2 lines

it was
significant, perhaps better than two to one improvement. It

would be a very

Quoted 2 lines

simple matter
to re-measure the improvement as the facility can

Quoted 24 lines

call back at

Quoted 21 lines
Quoted 19 lines
   injection time
   > as well.
   >
   >
   > Signed-off-by: Dave Winchell <[email protected]>
   > <mailto:[email protected]>
   > Signed-off-by: Ben Guthro <[email protected]>
   > <mailto:[email protected]>
   >
   >
   > _______________________________________________
   > Xen-devel mailing list
   > [email protected]
   > http://lists.xensource.com/xen-devel

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

#28 lore raw

OK, I can confirm that without Dave's patch RHEL4- and RHEL5-based 64-bit uni-p kernels gain time when hpet is the clocksource.

But WHOA with vcpus=2, el5u1-32 time suddenly goes crazy when domains are added whereas it seems fine when vcpus=1.

All my testing so far has been on 3.1.3, so I am going to redo it on xen-unstable, first without Dave's patch then with.

Quoted 3 lines

Is there one setting for all Linux guests that makes them choose hpet? Is it "clock=hpet clocksource=hpet"? I know you wrote at length about this before.

In the hvm configuration file:

hpet=1
acpi=1

(Note, acpi unspecified works too as 1 appears to be the default; but hpet=1 is ignored if acpi=0)

In the kernel command line of the hvm domain (e.g. in grub.conf):

clock=hpet notsc nopmtimer

(Note, a different set of kernel parameters is necessary for each kernel but because the kernel either ignores or gives harmless warnings for invalid parameters, this set should always result in hpet being selected as the clocksource, at least on all RHEL4- and RHEL5-based kernels I've tested.)

Quoted 1,339 lines

-----Original Message-----
From: Dave Winchell [mailto:[email protected]]
Sent: Wednesday, June 11, 2008 7:58 AM
To: [email protected]
Cc: Keir Fraser; xen-devel; Ben Guthro; Dave Winchell
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 9 lines · Dan Magenheimer
Quoted 8 lines

In EL5u1-32 however it looks like the fractions are accounted for. Indeed the EL5u1-32 "lost tick handling" code resembles the Linux/ia64 code which is what I've always assumed was the "missed tick" model. In this case, I think no policy is necessary and the measured skew should be identical to any physical hpet skew. I'll have to test this hypothesis though.

Quoted 7 lines

I've tested this hypothesis and it seems to hold true. This means the existing (unpatched) hpet code works fine on EL5-32bit (vcpus=1) when hpet is the clocksource,
even when the machine is overcommitted. A second hypothesis still needs to be tested that Dave's patch will not make this worse.

Interesting, thanks for pointing this out and confirming.

Quoted 6 lines

(Note that per previous discussion, my EL5u1-32bit guest running on an Intel dual-core physical box chose tsc as the best clocksource and I had to override it with clock=hpet in the kernel command line.)

Is there one setting for all Linux guests that makes them choose hpet? Is it "clock=hpet clocksource=hpet"? I know you wrote at length about this before.

Quoted 11 lines
Quoted 10 lines

Yes, that makes sense and concurs with what I remember from the EL4u5-32 code. If this is true, one would expect the default "no missed tick" policy to see time moving faster than an external source -- the first missed tick delivered after a long sleep would "catch up" and then the remainder would each add another tick.

Quoted 5 lines

Indeed with the existing (unpatched) hpet code, time is running faster on EL4u5-32 (vcpus=1, when overcommited). So Dave's patch is definitely needed here.

Its good to get the verification of this.

thanks,
Dave

Quoted 238 lines

Will try 64-bit next.

Dan

Quoted 232 lines

-----Original Message-----
From: Dan Magenheimer [mailto:[email protected]]
Sent: Monday, June 09, 2008 9:21 PM
To: 'Dave Winchell'; 'Keir Fraser'
Cc: 'xen-devel'; 'Ben Guthro'
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 5 lines

I'll tell you what I recall about this. Tomorrow I'll check the guest code to verify. I think that Linux declares a full tick, even if the interrupt is early. That's the problem.

Yes, that makes sense and concurs with what I remember from the EL4u5-32 code. If this is true, one would expect the default "no missed tick" policy to see time moving faster than an external source -- the first missed tick delivered after a long sleep would "catch up" and then the remainder would each add another tick.

Quoted 4 lines

On the other hand, if the interrupt is late it in effect declares a tick plus fraction. If it just declared the fraction in

the first place,

Quoted 3 lines

we could deliver the interrupts whenever we wanted.

My read of the EL4u5-32 code is that the fraction is discarded and a new tick period commences at "now", so the fractions eventually accumulate as lost time.

In EL5u1-32 however it looks like the fractions are accounted for. Indeed the EL5u1-32 "lost tick handling" code resembles the Linux/ia64 code which is what I've always assumed was the "missed tick" model. In this case, I think no policy is necessary and the measured skew should be identical to any physical hpet skew. I'll have to test this hypothesis though.

-----Original Message-----
From: [email protected] [mailto:[email protected]]On Behalf Of Dave Winchell
Sent: Monday, June 09, 2008 5:35 PM
To: [email protected]; Keir Fraser
Cc: Dave Winchell; xen-devel; Ben Guthro
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 4 lines
Quoted 4 lines

The Linux policy is more subtle, but is required to go from .1% to .03%.

Quoted 9 lines

Thanks for the good documentation which I hadn't thoroughly read until now.
I now understand that the essence of your
hpet missed ticks policy is to ensure that ticks are never delivered too close together. But I'm trying to understand WHY your patch works, in other words, what problem it is countering.

I'll tell you what I recall about this. Tomorrow I'll check the guest code to verify. I think that Linux declares a full tick, even if the interrupt is early. That's the problem.
On the other hand, if the interrupt is late it in effect declares a tick plus fraction. If it just declared the fraction in the first place,
we could deliver the interrupts whenever we wanted.

Its really not that different than the missed ticks policy in vpt.c except that there the period in vpt.c is based on start of interrupt and I have improved that with end-of interrupt as described in the patch note.

I don't recall what prompted me to try end-of-interrupt, but I saw a significant improvement. I may have been running a monotonicity test at the same time to explain the lock contention mentioned in the write-up.

Quoted 6 lines

I care about this for more reasons than just
because it is interesting: (1) I'd like to feel confident that it is fixing a bug rather than just a symptom of a bug; and (2) I wonder how universally it is applicable.

Its worked well my my small set of guests. You and our QA are going to tell us about the wider set. It doesn't matter if guest A handles interrupts closely spaced or not, just whether it handles them far apart. So it should be pretty universal with guests that really handle missed ticks. I think its interesting that some 32bit Linux guests handle missed ticks for hpet.

Quoted 9 lines

I see from code examination in mark_offset_hpet() in RHEL4u5/arch/i386/kernel/timers/timer_hpet.c, that the correction for lost ticks is just plain wrong in a virtual environment. (Suppose for example that a virtual tick was delivered every 1.999*hpet_tick... I think the clock would be off by 50%!) Is this the bug that is being "countered" by your policy?

I haven't looked at that code, perhaps.
I'll check it tomorrow.

Quoted 9 lines

However, the lost tick handling in RHEL5u1/kernel/timer.c (which I think is used also for hpet) is much better so I am eager to find out if your policy works there too.
If the hpet missed tick policy works for both, though, I should be happy, though I wonder about upstream kernels (e.g. the trend toward tickless).

I wasn't aware of this trend. If its robust, however, it should handle late interrupts ...

Quoted 5 lines

That said, I'd rather
see this get into Xen 3.3 and worry about upstream kernels later :-)

Regards,
Dave

-----Original Message-----
From: Dan Magenheimer [mailto:[email protected]]
Sent: Mon 6/9/2008 6:02 PM
To: Dave Winchell; Keir Fraser
Cc: Ben Guthro; xen-devel
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 4 lines

The Linux policy is more subtle, but is required to go from .1% to .03%.

Thanks for the good documentation which I hadn't thoroughly read until now. I now understand that the essence of your hpet missed ticks policy is to ensure that ticks are never delivered too close together. But I'm trying to understand WHY your patch works, in other words, what problem it is countering. I care about this for more reasons than just because it is interesting: (1) I'd like to feel confident that it is fixing a bug rather than just a symptom of a bug; and (2) I wonder how universally it is applicable.

I see from code examination in mark_offset_hpet() in RHEL4u5/arch/i386/kernel/timers/timer_hpet.c, that the correction for lost ticks is just plain wrong in a virtual environment. (Suppose for example that a virtual tick was delivered every 1.999*hpet_tick... I think the clock would be off by 50%!) Is this the bug that is being "countered" by your policy?

However, the lost tick handling in RHEL5u1/kernel/timer.c (which I think is used also for hpet) is much better so I am eager to find out if your policy works there too.

If the hpet missed tick policy works for both, though, I should be happy, though I wonder about upstream kernels (e.g. the trend toward tickless). That said, I'd rather see this get into Xen 3.3 and worry about upstream kernels later :-)

-----Original Message-----
From: Dave Winchell [mailto:[email protected]]
Sent: Sunday, June 08, 2008 2:32 PM
To: [email protected]; Keir Fraser
Cc: Ben Guthro; xen-devel; Dave Winchell
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Hi Dan,

Quoted 9 lines

While I am fully supportive of offering hardware hpet as an option for hvm guests (let's call it hwhpet=1 for shorthand), I am very surprised by your preliminary results; the most obvious conclusion is that Xen system time is losing time at the rate of 1000 PPM though its possible there's a bug somewhere else in the "time stack". Your Windows result is jaw-dropping and inexplicable, though I have to admit ignorance of how Windows manages time.

I think xen system time is fine. You have to add the interrupt delivery policies decribed in the write-up for the patch to get accurate timekeeping in the guest.

The windows policy is obvious and results in a large improvement in accuracy. The Linux policy is more subtle, but is required to go from .1% to .03%.

Quoted 8 lines

I think with my recent patch and hpet=1 (essentially the same as your emulated hpet), hvm guest time should track Xen system time. I wonder if domain0 (which if I understand correctly is directly using Xen system time) is also seeing an error of .1%? Also I wonder for the skew you are seeing (in both hvm guests and domain0) is time moving too fast or two slow?

I don't recall the direction. I can look it up in my notes at work tomorrow.

Quoted 2 lines

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant

performance

Quoted 118 lines
Quoted 118 lines
Quoted 4 lines

issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0.

Our patch is accurate to < .03% using the physical hpet mode or the simulated mode.

Quoted 7 lines

And if for some reason
Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

In our experience, Xen system time is accurate enough now.

Quoted 7 lines

One more thought... do you know the accuracy of the TSC crystals on your test systems? I posted a patch awhile ago that was intended to test that, though I guess it was only testing skew of different TSCs on the same system, not TSCs against an external time source.

I do not know the tsc accuracy.

Quoted 4 lines

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Regards,
Dave

-----Original Message-----
From: Dan Magenheimer [mailto:[email protected]]
Sent: Fri 6/6/2008 4:29 PM
To: Dave Winchell; Keir Fraser
Cc: Ben Guthro; xen-devel
Subject: RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Dave --

Thanks much for posting the preliminary results!

While I am fully supportive of offering hardware hpet as an option for hvm guests (let's call it hwhpet=1 for shorthand), I am very surprised by your preliminary results; the most obvious conclusion is that Xen system time is losing time at the rate of 1000 PPM though its possible there's a bug somewhere else in the "time stack". Your Windows result is jaw-dropping and inexplicable, though I have to admit ignorance of how Windows manages time.

I think with my recent patch and hpet=1 (essentially the same as your emulated hpet), hvm guest time should track Xen system time. I wonder if domain0 (which if I understand correctly is directly using Xen system time) is also seeing an error of .1%? Also I wonder for the skew you are seeing (in both hvm guests and domain0) is time moving too fast or two slow?

Although hwhpet=1 is a fine alternative in many cases, it may be unavailable on some systems and may cause significant performance issues on others. So I think we will still need to track down the poor accuracy when hwhpet=0. And if for some reason Xen system time can't be made accurate enough (< 0.05%), then I think we should consider building Xen system time itself on top of hardware hpet instead of TSC... at least when Xen discovers a capable hpet.

One more thought... do you know the accuracy of the TSC crystals on your test systems? I posted a patch awhile ago that was intended to test that, though I guess it was only testing skew of different TSCs on the same system, not TSCs against an external time source.

Or maybe there's a computation error somewhere in the hvm hpet scaling code? Hmmm...

Thanks,
Dan

Quoted 8 lines

-----Original Message-----
From: Dave Winchell [mailto:[email protected]]
Sent: Friday, June 06, 2008 1:33 PM
To: [email protected]; Keir Fraser
Cc: Ben Guthro; xen-devel; Dave Winchell
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Quoted 7 lines

Dan, Keir:

Preliminary tests results indicate an error of .1% for Linux 64 bit guests configured
for hpet with xen-unstable as is. As we have discussed many

times, the

Quoted 4 lines

ntp requirement is .05%.
Tests on the patch we just submitted for hpet have

indicated errors of

Quoted 4 lines

.0012%
on this platform under similar test conditions and .03% on other platforms.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Windows vista64 has an error of 11% using hpet with the xen-unstable bits.
In an overnight test with our hpet patch, the Windows vista error was .008%.

The tests are with two or three guests on a physical node,

all under

Quoted 914 lines
Quoted 882 lines
Quoted 13 lines

load, and with
the ratio of vcpus to phys cpus > 1.

I will continue to run tests over the next few days.

thanks,
Dave

Dan Magenheimer wrote:

Quoted 11 lines
Quoted 5 lines

Hi Dave and Ben --

When running tests on xen-unstable (without your patch),

please ensure

Quoted 3 lines

that hpet=1 is set in the hvm config and also I think

that when hpet

Quoted 51 lines
Quoted 3 lines

is the clocksource on RHEL4-32, the clock IS resilient to

missed ticks

Quoted 3 lines

so timer_mode should be 2 (vs when pit is the clocksource

on RHEL4-32,

Quoted 6 lines

all clock ticks must be delivered and so timer_mode should be 0).

Per

http://lists.xensource.com/archives/html/xen-devel/2008-06/msg
00098.html it's

Quoted 10 lines

my intent to clean this up, but I won't get to it until next week.

Thanks,
Dan

   -----Original Message-----
   *From:* [email protected]
   [mailto:[email protected]]*On

Behalf Of *Dave

Quoted 11 lines

Winchell
*Sent:* Friday, June 06, 2008 4:46 AM
*To:* Keir Fraser; Ben Guthro; xen-devel
*Cc:* [email protected]; Dave Winchell *Subject:* RE: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Keir,

I think the changes are required. We'll run some tests

today today so

Quoted 2 lines

that we have some data to talk about.

Quoted 18 lines
Quoted 7 lines

-Dave

-----Original Message-----
From: [email protected] on behalf

of Keir Fraser

Quoted 8 lines

Sent: Fri 6/6/2008 4:58 AM
To: Ben Guthro; xen-devel
Cc: [email protected]
Subject: Re: [Xen-devel] [PATCH 0/2] Improve hpet accuracy

Are these patches needed now the timers are built on

Xen system

Quoted 24 lines
Quoted 4 lines

time rather
than host TSC? Dan has reported much better

time-keeping with his

Quoted 4 lines

patch
checked in, and it¹s for sure a lot less invasive than

this patchset.

Quoted 10 lines
    -- Keir
   On 5/6/08 15:59, "Ben Guthro" <[email protected]> wrote:

   >
   > 1. Introduction
   >
   > This patch improves the hpet based guest clock in
Quoted 15 lines

terms of drift and

Quoted 4 lines
Quoted 2 lines

monotonicity.
Prior to this work the drift with hpet was greater

than 2%, far

Quoted 5 lines

above the .05%

Quoted 2 lines

limit
for ntp to synchronize. With this code, the drift

ranges from

Quoted 7 lines
Quoted 7 lines

.001% to .0033%

Quoted 4 lines

depending
on guest and physical platform.

Using hpet allows guest operating systems to

provide monotonic

Quoted 4 lines
Quoted 4 lines

time to their

Quoted 1 line

applications. Time sources other than hpet are not

Quoted 21 lines

monotonic because

Quoted 3 lines
Quoted 1 line

of their reliance on tsc, which is not synchronized

across physical

Quoted 12 lines
Quoted 3 lines

processors.

Windows 2k864 and many Linux guests are supported with two

policies, one for

Quoted 2 lines

guests
that handle missed clock interrupts and the other for guests

that require the

Quoted 3 lines

correct number of interrupts.

Guests may use hpet for the timing source even if

the physical

Quoted 10 lines
Quoted 10 lines

platform has no

Quoted 2 lines

visible
hpet. Migration is supported between physical machines which

differ in

Quoted 4 lines

physical
hpet visibility.

Most of the changes are in hpet.c. Two general

facilities are

Quoted 5 lines
Quoted 5 lines

added to track

Quoted 2 lines

interrupt
progress. The ideas here and the facilities would

be useful in

Quoted 10 lines
Quoted 10 lines

vpt.c, for

Quoted 4 lines

other time
sources, though no attempt is made here to improve vpt.c.

The following sections discuss hpet dependencies, interrupt

delivery policies,

Quoted 2 lines

live migration,
test results, and relation to recent work with

monotonic time.

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

2. Virtual Hpet dependencies

The virtual hpet depends on the ability to read the

Quoted 14 lines

physical or

Quoted 4 lines

simulated

Quoted 1 line

(see discussion below) hpet. For timekeeping, the

virtual hpet

Quoted 4 lines

also depends

Quoted 1 line

on two new interrupt notification facilities to

implement its

Quoted 13 lines
Quoted 13 lines

policies for

Quoted 5 lines

interrupt delivery.

2.1. Two modes of low-level hpet main counter reads.

In this implementation, the virtual hpet reads with

read_64_main_counter(),

Quoted 2 lines

exported by
time.c, either the real physical hpet main counter register

directly or a

Quoted 3 lines

"simulated"
hpet main counter.

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

The simulated mode uses a monotonic version of get_s_time()

(NOW()), where the

Quoted 2 lines

last
time value is returned whenever the current time

value is less

Quoted 13 lines
Quoted 13 lines

than the last

Quoted 2 lines

time
value. In simulated mode, since it is layered on s_time, the

underlying

Quoted 2 lines

hardware
can be hpet or some other device. The frequency of the main

counter in

Quoted 2 lines

simulated
mode is the same as the standard physical hpet frequency,

allowing live

Quoted 3 lines

migration
between nodes that are configured differently.

Quoted 11 lines
Quoted 3 lines
Quoted 1 line

If the physical platform does not have an hpet

device, or if xen

Quoted 5 lines

is configured

Quoted 2 lines

not
to use the device, then the simulated method is

used. If there

Quoted 5 lines
Quoted 5 lines

is a physical

Quoted 2 lines

hpet device,
and xen has initialized it, then either simulated

or physical

Quoted 8 lines
Quoted 5 lines

mode can be

Quoted 2 lines

used.
This is governed by a boot time option, hpet-avoid.

Setting this

Quoted 13 lines
Quoted 5 lines

option to 1

Quoted 2 lines

gives the
simulated mode and 0 the physical mode. The default

is physical

Quoted 5 lines

mode.

Quoted 2 lines

A disadvantage of the physical mode is that may

take longer to

Quoted 4 lines
Quoted 4 lines

read the device

Quoted 1 line

than in simulated mode. On some platforms the cost is

Quoted 8 lines

about the

Quoted 5 lines

same (less

Quoted 2 lines

than 250 nsec) for
physical and simulated modes, while on others

physical cost is

Quoted 5 lines
Quoted 5 lines

much higher

Quoted 2 lines

than simulated.
A disadvantage of the simulated mode is that it can

return the

Quoted 5 lines
Quoted 5 lines

same value

Quoted 4 lines

for the counter in consecutive calls.

2.2. Interrupt notification facilities.

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

Two interrupt notification facilities are introduced, one is hvm_isa_irq_assert_cb()
and the other hvm_register_intr_en_notif().

The vhpet uses hvm_isa_irq_assert_cb to deliver

interrupts to

Quoted 4 lines
Quoted 4 lines

the vioapic.

Quoted 1 line

hvm_isa_irq_assert_cb allows a callback to be

passed along to

Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

vioapic_deliver()
and this callback is called with a mask of the vcpus

Quoted 10 lines

which will

Quoted 7 lines

get the

Quoted 1 line

interrupt. This callback is made before any vcpus receive an

interrupt.

Quoted 2 lines

Vhpet uses hvm_register_intr_en_notif() to register

a handler

Quoted 6 lines
Quoted 6 lines

for a particular

Quoted 3 lines

vector that will be called when that vector is injected in [vmx,svm]_intr_assist()
and also when the guest finishes handling the

interrupt. Here

Quoted 17 lines
Quoted 5 lines

finished is

Quoted 2 lines

defined
as the point when the guest re-enables interrupts or

lowers the

Quoted 6 lines

tpr value.

Quoted 1 line

EOI is not used as the end of interrupt as this is sometimes

returned before

Quoted 1 line

the interrupt handler has done its work. A flag is

passed to the

Quoted 17 lines
Quoted 5 lines

handler

Quoted 2 lines

indicating
whether this is the injection point (post = 1) or the

interrupt

Quoted 9 lines

finished (post

Quoted 2 lines

= 0) point.
The need for the finished point callback is discussed in the

missed ticks

Quoted 3 lines

policy section.

To prevent a possible early trigger of the finished

callback,

Quoted 29 lines
Quoted 8 lines

intr_en_notif

Quoted 2 lines

logic
has a two stage arm, the first at injection

(hvm_intr_en_notif_arm()) and the

Quoted 2 lines

second when
interrupts are seen to be disabled

(hvm_intr_en_notif_disarm()).

Quoted 5 lines

Once fully

Quoted 2 lines

armed, re-enabling
interrupts will cause hvm_intr_en_notif_disarm() to

make the end

Quoted 4 lines

of interrupt

Quoted 1 line

callback. hvm_intr_en_notif_arm() and

hvm_intr_en_notif_disarm()

Quoted 3 lines
   are called by
   > [vmx,svm]_intr_assist().
   >
Quoted 11 lines
Quoted 5 lines
Quoted 3 lines

3. Interrupt delivery policies

The existing hpet interrupt delivery is preserved.

This includes

Quoted 3 lines
Quoted 1 line

vcpu round robin delivery used by Linux and

broadcast delivery

Quoted 6 lines
Quoted 6 lines

used by

Quoted 3 lines

Windows.

There are two policies for interrupt delivery, one

for Windows

Quoted 8 lines
Quoted 5 lines

2k8-64 and the

Quoted 2 lines

other
for Linux. The Linux policy takes advantage of the

(guest) Linux

Quoted 12 lines
Quoted 5 lines

missed tick

Quoted 2 lines

and offset
calculations and does not attempt to deliver the

right number of

Quoted 4 lines

interrupts.

Quoted 1 line

The Windows policy delivers the correct number of

interrupts,

Quoted 5 lines
Quoted 5 lines

even if

Quoted 2 lines

sometimes much
closer to each other than the period. The policies

are similar

Quoted 7 lines
Quoted 7 lines

to those in

Quoted 4 lines

vpt.c, though
there are some important differences.

Policies are selected with an HVMOP_set_param

Quoted 13 lines

hypercall with index

Quoted 4 lines
Quoted 2 lines

HVM_PARAM_TIMER_MODE.
Two new values are added,

HVM_HPET_guest_computes_missed_ticks and

Quoted 3 lines
Quoted 1 line

HVM_HPET_guest_does_not_compute_missed_ticks. The

reason that

Quoted 5 lines
Quoted 5 lines

two new ones

Quoted 2 lines

are added is that
in some guests (32bit Linux) a no-missed policy is

needed for

Quoted 5 lines
Quoted 5 lines

clock sources

Quoted 2 lines

other than hpet
and a missed ticks policy for hpet. It was felt that

Quoted 15 lines

there would

Quoted 12 lines

be less

Quoted 6 lines

confusion by simply
introducing the two hpet policies.

3.1. The missed ticks policy

The Linux clock interrupt handler for hpet calculates missed

ticks and offset

Quoted 2 lines

using the hpet
main counter. The algorithm works well when the

time since the

Quoted 21 lines
Quoted 7 lines

last interrupt

Quoted 4 lines

is greater than
or equal to a period and poorly otherwise.

The missed ticks policy ensures that no two clock

interrupts are

Quoted 8 lines

delivered to

Quoted 2 lines

the guest at
a time interval less than a period. A time stamp (hpet main

counter value) is

Quoted 2 lines

recorded (by a
callback registered with hvm_register_intr_en_notif)

when Linux

Quoted 13 lines
Quoted 5 lines

finishes

Quoted 2 lines

handling the clock
interrupt. Then, ensuing interrupts are delivered to

the vioapic

Quoted 5 lines

only if the

Quoted 2 lines

current main
counter value is a period greater than when the

last interrupt

Quoted 5 lines
Quoted 5 lines

was handled.

Quoted 2 lines

Tests showed a significant improvement in clock

drift with end

Quoted 8 lines
Quoted 5 lines

of interrupt

Quoted 2 lines

time stamps
versus beginning of interrupt[1]. It is believed that

the reason

Quoted 13 lines
Quoted 5 lines

for the

Quoted 2 lines

improvement
is that the clock interrupt handler goes for a

spinlock and can

Quoted 5 lines

be therefore

Quoted 2 lines

delayed in its
processing. Furthermore, the main counter is read

by the guest

Quoted 24 lines
Quoted 13 lines

under the lock.

Quoted 2 lines

The net
effect is that if we time stamp injection, we can get the

difference in time

Quoted 1 line

between successive interrupt handler lock acquisitions to be

less than the

Quoted 5 lines

period.

3.2. The no-missed ticks policy

Windows 2k864 keeps very poor time with the missed

ticks policy.

Quoted 8 lines

So the

Quoted 2 lines

no-missed ticks policy
was developed. In the no-missed ticks policy we deliver the

correct number of

Quoted 2 lines

interrupts,
even if they are spaced less than a period apart

Quoted 20 lines

(when catching up).

Quoted 17 lines
Quoted 2 lines

Windows 2k864 uses a broadcast mode in the interrupt routing

such that

Quoted 1 line

all vcpus get the clock interrupt. The best Windows drift

performance was

Quoted 2 lines

achieved when the
policy code ensured that all the previous interrupts (on the

various vcpus)

Quoted 4 lines

had been injected
before injecting the next interrupt to the vioapic..

The policy code works as follows. It uses the

hvm_isa_irq_assert_cb() to

Quoted 2 lines

record
the vcpus to be interrupted in

h->hpet.pending_mask. Then, in

Quoted 5 lines
Quoted 5 lines

the callback

Quoted 2 lines

registered
with hvm_register_intr_en_notif() at post=1 time it

clears the

Quoted 10 lines
Quoted 10 lines

current vcpu in

Quoted 2 lines

the pending_mask.
When the pending_mask is clear it decrements

hpet.intr_pending_nr and if

Quoted 2 lines

intr_pending_nr is still
non-zero posts another interrupt to the ioapic with

hvm_isa_irq_assert_cb().

Quoted 1 line

Intr_pending_nr is incremented in

hpet_route_decision_not_missed_ticks().

Quoted 1 line
Quoted 6 lines
Quoted 6 lines
Quoted 1 line

The missed ticks policy intr_en_notif callback also uses the

pending_mask

Quoted 2 lines

method. So even though
Linux does not broadcast its interrupts, the code

could handle

Quoted 4 lines
Quoted 4 lines

it if it did.

Quoted 1 line

In this case the end of interrupt time stamp is

made when the

Quoted 11 lines
Quoted 11 lines

pending_mask is

Quoted 5 lines

clear.

4. Live Migration

Live migration with hpet preserves the current offset of the

guest clock with

Quoted 2 lines

respect
to ntp. This is accomplished by migrating all of

the state in

Quoted 10 lines
Quoted 10 lines

the h->hpet data

Quoted 2 lines

structure
in the usual way. The hp->mc_offset is recalculated on the

receiving node so

Quoted 4 lines

that the
guest sees a continuous hpet main counter.

Code as been added to xc_domain_save.c to send a

small message

Quoted 7 lines
Quoted 7 lines

after the

Quoted 1 line

domain context is sent. The contents of the message is the

physical tsc

Quoted 2 lines

timestamp, last_tsc,
read just before the message is sent. When the

Quoted 35 lines

last_tsc message

Quoted 32 lines

is received in

Quoted 2 lines

xc_domain_restore.c,
another physical tsc timestamp, cur_tsc, is read. The two

timestamps are

Quoted 2 lines

loaded into the domain
structure as last_tsc_sender and first_tsc_receiver with

hypercalls. Then

Quoted 2 lines

xc_domain_hvm_setcontext
is called so that hpet_load has access to these time stamps.

Hpet_load uses

Quoted 2 lines

the timestamps
to account for the time spent saving and loading the domain

context. With this

Quoted 2 lines

technique,
the only neglected time is the time spent sending a small

network message.

Quoted 14 lines

5. Test Results

Some recent test results are:

5.1 Linux 4u664 and Windows 2k864 load test.
Duration: 70 hours.
Test date: 6/2/08
Loads: usex -b48 on Linux; burn-in on Windows Guest vcpus: 8 for Linux; 2 for Windows
Hardware: 8 physical cpu AMD
Clock drift : Linux: .0012% Windows: .009%

5.2 Linux 4u664, Linux 4u464 , and Windows 2k864

no-load test

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Duration: 23 hours.
Test date: 6/3/08
Loads: none
Guest vcpus: 8 for each Linux; 2 for Windows
Hardware: 4 physical cpu AMD
Clock drift : Linux: .033% Windows: .019%

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

6. Relation to recent work in xen-unstable

There is a similarity between hvm_get_guest_time() in

xen-unstable and

Quoted 2 lines

read_64_main_counter()
in this code. However, read_64_main_counter() is

more tuned to

Quoted 26 lines
Quoted 5 lines

the needs of

Quoted 2 lines

hpet.c. It has no
"set" operation, only the get. It isolates the mode,

physical or

Quoted 8 lines

simulated, in

Quoted 2 lines

read_64_main_counter()
itself. It uses no vcpu or domain state as it is a physical

entity, in either

Quoted 2 lines

mode. And it provides a real
physical mode for every read for those applications

that desire

Quoted 7 lines

this.

Quoted 4 lines

7. Conclusion

The virtual hpet is improved by this patch in terms

Quoted 16 lines

of accuracy and

Quoted 4 lines
Quoted 2 lines

monotonicity.
Tests performed to date verify this and more testing

is under way.

Quoted 6 lines
Quoted 4 lines

8. Future Work

Testing with Windows Vista will be performed soon.

The reason

Quoted 5 lines
Quoted 5 lines

for accuracy

Quoted 2 lines

variations
on different platforms using the physical hpet

device will be

Quoted 4 lines
Quoted 4 lines

investigated.

Quoted 1 line

Additional overhead measurements on simulated vs

physical hpet

Quoted 19 lines
Quoted 8 lines
   mode will be
   > made.
   >
   > Footnotes:
   >
   > 1. I don't recall the accuracy improvement with end

of interrupt

Quoted 8 lines

stamping, but

Quoted 2 lines

it was
significant, perhaps better than two to one improvement. It

would be a very

Quoted 2 lines

simple matter
to re-measure the improvement as the facility can

Quoted 24 lines

call back at

Quoted 21 lines
Quoted 19 lines
   injection time
   > as well.
   >
   >
   > Signed-off-by: Dave Winchell <[email protected]>
   > <mailto:[email protected]>
   > Signed-off-by: Ben Guthro <[email protected]>
   > <mailto:[email protected]>
   >
   >
   > _______________________________________________
   > Xen-devel mailing list
   > [email protected]
   > http://lists.xensource.com/xen-devel

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

#29 lore raw
In reply to Dan Magenheimer
Quoted 7 lines
Quoted 6 lines

In EL5u1-32 however it looks like the fractions are accounted for. Indeed the EL5u1-32 "lost tick handling" code resembles the Linux/ia64 code which is what I've always assumed was the "missed tick" model. In this case, I think no policy is necessary and the measured skew should be identical to any physical hpet skew. I'll have to test this hypothesis though.

Quoted 5 lines

I've tested this hypothesis and it seems to hold true. This means the existing (unpatched) hpet code works fine on EL5-32bit (vcpus=1) when hpet is the clocksource,
even when the machine is overcommitted. A second hypothesis still needs to be tested that Dave's patch will not make this worse.

OK, I can confirm that Dave's patch, as expected, does not make this any worse.

The timer algorithm in 2.6.18 for x86 (i.e. RHEL5-32bit) is definitely the most resilient to variations in tick delivery for a monotonically-increasing timesource (i.e. hpet). This algorithm is in arch-independent code but sadly x86_64 didn't use it as of 2.6.18.

Dan