From: "Jan Beulich" Subject: [PATCH 2/2] x86/HVM: batch vCPU wakeups Date: Thu, 11 Sep 2014 10:40:42 +0100 Message-ID: <54118A3A0200007800033BCD@mail.emea.novell.com> References: <541189280200007800033BB5@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part497C010A.1__=" Return-path: Received: from mail6.bemta4.messagelabs.com ([85.158.143.247]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1XS0rt-0004eB-D2 for xen-devel@lists.xenproject.org; Thu, 11 Sep 2014 09:40:45 +0000 In-Reply-To: <541189280200007800033BB5@mail.emea.novell.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: Ian Campbell , Keir Fraser , Ian Jackson , Tim Deegan List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part497C010A.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Mass wakeups (via vlapic_ipi()) can take enormous amounts of time, especially when many of the remote pCPU-s are in deep C-states. For 64-vCPU Windows Server 2012 R2 guests on Ivybridge hardware, accumulated times of over 2ms were observed (average 1.1ms). Considering that Windows broadcasts IPIs from its timer interrupt, which at least at certain times can run at 1kHz, it is clear that this can't result in good guest behavior. In fact, on said hardware guests with significantly beyond 40 vCPU-s simply hung when e.g. ServerManager gets started. This isn't just helping to reduce the number of ICR writes when the host APICs run in clustered mode, it also reduces them by suppressing the sends altogether when - by the time cpu_raise_softirq_batch_finish() is reached - the remote CPU already managed to handle the softirq. Plus - when using MONITOR/MWAIT - the update of softirq_pending(cpu), being on the monitored cache line - should make the remote CPU wake up ahead of the ICR being sent, allowing the wait-for-ICR-idle latencies to be reduced (perhaps to a large part due to overlapping the wakeups of multiple CPUs). With this alone (i.e. without the IPI avoidance patch in place), average broadcast times for a 64-vCPU guest went down to a measured maximum of 310us. With that other patch in place, improvements aren't as clear anymore (short term averages only went down from 255us to 250us, which clearly is within the error range of the measurements), but longer term an improvement of the averages is still visible. Depending on hardware, long term maxima were observed to go down quite a bit (on aforementioned hardware), while they were seen to go up again on a (single core) Nehalem (where instead the improvement on the average values was more visible). Of course this necessarily increases the latencies for the remote CPU wakeup at least slightly. To weigh between the effects, the condition to enable batching in vlapic_ipi() may need further tuning. Signed-off-by: Jan Beulich --- a/xen/arch/x86/hvm/vlapic.c +++ b/xen/arch/x86/hvm/vlapic.c @@ -409,6 +409,26 @@ void vlapic_handle_EOI_induced_exit(stru hvm_dpci_msi_eoi(current->domain, vector); } =20 +static bool_t is_multicast_dest(struct vlapic *vlapic, unsigned int = short_hand, + uint32_t dest, bool_t dest_mode) +{ + if ( vlapic_domain(vlapic)->max_vcpus <=3D 2 ) + return 0; + + if ( short_hand ) + return short_hand !=3D APIC_DEST_SELF; + + if ( vlapic_x2apic_mode(vlapic) ) + return dest_mode ? hweight16(dest) > 1 : dest =3D=3D 0xffffffff; + + if ( dest_mode ) + return hweight8(dest & + GET_xAPIC_DEST_FIELD(vlapic_get_reg(vlapic, + APIC_DFR))) > = 1; + + return dest =3D=3D 0xff; +} + void vlapic_ipi( struct vlapic *vlapic, uint32_t icr_low, uint32_t icr_high) { @@ -447,12 +467,18 @@ void vlapic_ipi( =20 default: { struct vcpu *v; + bool_t batch =3D is_multicast_dest(vlapic, short_hand, dest, = dest_mode); + + if ( batch ) + cpu_raise_softirq_batch_begin(); for_each_vcpu ( vlapic_domain(vlapic), v ) { if ( vlapic_match_dest(vcpu_vlapic(v), vlapic, short_hand, dest, dest_mode) ) vlapic_accept_irq(v, icr_low); } + if ( batch ) + cpu_raise_softirq_batch_finish(); break; } } --- a/xen/common/softirq.c +++ b/xen/common/softirq.c @@ -23,6 +23,9 @@ irq_cpustat_t irq_stat[NR_CPUS]; =20 static softirq_handler softirq_handlers[NR_SOFTIRQS]; =20 +static DEFINE_PER_CPU(cpumask_t, batch_mask); +static DEFINE_PER_CPU(unsigned int, batching); + static void __do_softirq(unsigned long ignore_mask) { unsigned int i, cpu; @@ -71,24 +74,58 @@ void open_softirq(int nr, softirq_handle void cpumask_raise_softirq(const cpumask_t *mask, unsigned int nr) { unsigned int cpu, this_cpu =3D smp_processor_id(); - cpumask_t send_mask; + cpumask_t send_mask, *raise_mask; + + if ( !per_cpu(batching, this_cpu) || in_irq() ) + { + cpumask_clear(&send_mask); + raise_mask =3D &send_mask; + } + else + raise_mask =3D &per_cpu(batch_mask, this_cpu); =20 - cpumask_clear(&send_mask); for_each_cpu(cpu, mask) if ( !test_and_set_bit(nr, &softirq_pending(cpu)) && cpu !=3D this_cpu && !arch_skip_send_event_check(cpu) ) - cpumask_set_cpu(cpu, &send_mask); + cpumask_set_cpu(cpu, raise_mask); =20 - smp_send_event_check_mask(&send_mask); + if ( raise_mask =3D=3D &send_mask ) + smp_send_event_check_mask(raise_mask); } =20 void cpu_raise_softirq(unsigned int cpu, unsigned int nr) { - if ( !test_and_set_bit(nr, &softirq_pending(cpu)) - && (cpu !=3D smp_processor_id()) - && !arch_skip_send_event_check(cpu) ) + unsigned int this_cpu =3D smp_processor_id(); + + if ( test_and_set_bit(nr, &softirq_pending(cpu)) + || (cpu =3D=3D this_cpu) + || arch_skip_send_event_check(cpu) ) + return; + + if ( !per_cpu(batching, this_cpu) || in_irq() ) smp_send_event_check_cpu(cpu); + else + set_bit(nr, &per_cpu(batch_mask, this_cpu)); +} + +void cpu_raise_softirq_batch_begin(void) +{ + ++this_cpu(batching); +} + +void cpu_raise_softirq_batch_finish(void) +{ + unsigned int cpu, this_cpu =3D smp_processor_id(); + cpumask_t *mask =3D &per_cpu(batch_mask, this_cpu); + + ASSERT(per_cpu(batching, this_cpu)); + for_each_cpu ( cpu, mask ) + if ( !softirq_pending(cpu) ) + cpumask_clear_cpu(cpu, mask); + smp_send_event_check_mask(mask); + cpumask_clear(mask); + --per_cpu(batching, this_cpu); } =20 void raise_softirq(unsigned int nr) --- a/xen/include/xen/softirq.h +++ b/xen/include/xen/softirq.h @@ -30,6 +30,9 @@ void cpumask_raise_softirq(const cpumask void cpu_raise_softirq(unsigned int cpu, unsigned int nr); void raise_softirq(unsigned int nr); =20 +void cpu_raise_softirq_batch_begin(void); +void cpu_raise_softirq_batch_finish(void); + /* * Process pending softirqs on this CPU. This should be called periodicall= y * when performing work that prevents softirqs from running in a timely = manner. --=__Part497C010A.1__= Content-Type: text/plain; name="raise-softirq-batch.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="raise-softirq-batch.patch" x86/HVM: batch vCPU wakeups=0A=0AMass wakeups (via vlapic_ipi()) can take = enormous amounts of time,=0Aespecially when many of the remote pCPU-s are = in deep C-states. For=0A64-vCPU Windows Server 2012 R2 guests on Ivybridge = hardware,=0Aaccumulated times of over 2ms were observed (average 1.1ms).=0A= Considering that Windows broadcasts IPIs from its timer interrupt,=0Awhich = at least at certain times can run at 1kHz, it is clear that this=0Acan't = result in good guest behavior. In fact, on said hardware guests=0Awith = significantly beyond 40 vCPU-s simply hung when e.g. ServerManager=0Agets = started.=0A=0AThis isn't just helping to reduce the number of ICR writes = when the=0Ahost APICs run in clustered mode, it also reduces them by = suppressing=0Athe sends altogether when - by the time=0Acpu_raise_softirq_b= atch_finish() is reached - the remote CPU already=0Amanaged to handle the = softirq. Plus - when using MONITOR/MWAIT - the=0Aupdate of softirq_pending(= cpu), being on the monitored cache line -=0Ashould make the remote CPU = wake up ahead of the ICR being sent,=0Aallowing the wait-for-ICR-idle = latencies to be reduced (perhaps to a=0Alarge part due to overlapping the = wakeups of multiple CPUs).=0A=0AWith this alone (i.e. without the IPI = avoidance patch in place),=0Aaverage broadcast times for a 64-vCPU guest = went down to a measured=0Amaximum of 310us. With that other patch in = place, improvements aren't=0Aas clear anymore (short term averages only = went down from 255us to=0A250us, which clearly is within the error range = of the measurements),=0Abut longer term an improvement of the averages is = still visible.=0ADepending on hardware, long term maxima were observed to = go down quite=0Aa bit (on aforementioned hardware), while they were seen = to go up=0Aagain on a (single core) Nehalem (where instead the improvement = on the=0Aaverage values was more visible).=0A=0AOf course this necessarily = increases the latencies for the remote=0ACPU wakeup at least slightly. To = weigh between the effects, the=0Acondition to enable batching in vlapic_ipi= () may need further tuning.=0A=0ASigned-off-by: Jan Beulich =0A=0A--- a/xen/arch/x86/hvm/vlapic.c=0A+++ b/xen/arch/x86/hvm/vlapic.c= =0A@@ -409,6 +409,26 @@ void vlapic_handle_EOI_induced_exit(stru=0A = hvm_dpci_msi_eoi(current->domain, vector);=0A }=0A =0A+static bool_t = is_multicast_dest(struct vlapic *vlapic, unsigned int short_hand,=0A+ = uint32_t dest, bool_t dest_mode)=0A+{=0A+ if = ( vlapic_domain(vlapic)->max_vcpus <=3D 2 )=0A+ return 0;=0A+=0A+ = if ( short_hand )=0A+ return short_hand !=3D APIC_DEST_SELF;=0A+=0A= + if ( vlapic_x2apic_mode(vlapic) )=0A+ return dest_mode ? = hweight16(dest) > 1 : dest =3D=3D 0xffffffff;=0A+=0A+ if ( dest_mode = )=0A+ return hweight8(dest &=0A+ GET_xAPIC_DE= ST_FIELD(vlapic_get_reg(vlapic,=0A+ = APIC_DFR))) > 1;=0A+=0A+ return dest =3D=3D = 0xff;=0A+}=0A+=0A void vlapic_ipi(=0A struct vlapic *vlapic, uint32_t = icr_low, uint32_t icr_high)=0A {=0A@@ -447,12 +467,18 @@ void vlapic_ipi(= =0A =0A default: {=0A struct vcpu *v;=0A+ bool_t batch = =3D is_multicast_dest(vlapic, short_hand, dest, dest_mode);=0A+=0A+ = if ( batch )=0A+ cpu_raise_softirq_batch_begin();=0A = for_each_vcpu ( vlapic_domain(vlapic), v )=0A {=0A if = ( vlapic_match_dest(vcpu_vlapic(v), vlapic,=0A = short_hand, dest, dest_mode) )=0A vlapic_accept_irq(= v, icr_low);=0A }=0A+ if ( batch )=0A+ cpu_raise_= softirq_batch_finish();=0A break;=0A }=0A }=0A--- = a/xen/common/softirq.c=0A+++ b/xen/common/softirq.c=0A@@ -23,6 +23,9 @@ = irq_cpustat_t irq_stat[NR_CPUS];=0A =0A static softirq_handler softirq_hand= lers[NR_SOFTIRQS];=0A =0A+static DEFINE_PER_CPU(cpumask_t, batch_mask);=0A+= static DEFINE_PER_CPU(unsigned int, batching);=0A+=0A static void = __do_softirq(unsigned long ignore_mask)=0A {=0A unsigned int i, = cpu;=0A@@ -71,24 +74,58 @@ void open_softirq(int nr, softirq_handle=0A = void cpumask_raise_softirq(const cpumask_t *mask, unsigned int nr)=0A {=0A = unsigned int cpu, this_cpu =3D smp_processor_id();=0A- cpumask_t = send_mask;=0A+ cpumask_t send_mask, *raise_mask;=0A+=0A+ if ( = !per_cpu(batching, this_cpu) || in_irq() )=0A+ {=0A+ cpumask_clea= r(&send_mask);=0A+ raise_mask =3D &send_mask;=0A+ }=0A+ = else=0A+ raise_mask =3D &per_cpu(batch_mask, this_cpu);=0A =0A- = cpumask_clear(&send_mask);=0A for_each_cpu(cpu, mask)=0A if ( = !test_and_set_bit(nr, &softirq_pending(cpu)) &&=0A cpu !=3D = this_cpu &&=0A !arch_skip_send_event_check(cpu) )=0A- = cpumask_set_cpu(cpu, &send_mask);=0A+ cpumask_set_cpu(cpu, = raise_mask);=0A =0A- smp_send_event_check_mask(&send_mask);=0A+ if ( = raise_mask =3D=3D &send_mask )=0A+ smp_send_event_check_mask(raise_m= ask);=0A }=0A =0A void cpu_raise_softirq(unsigned int cpu, unsigned int = nr)=0A {=0A- if ( !test_and_set_bit(nr, &softirq_pending(cpu))=0A- = && (cpu !=3D smp_processor_id())=0A- && !arch_skip_send_event_ch= eck(cpu) )=0A+ unsigned int this_cpu =3D smp_processor_id();=0A+=0A+ = if ( test_and_set_bit(nr, &softirq_pending(cpu))=0A+ || (cpu = =3D=3D this_cpu)=0A+ || arch_skip_send_event_check(cpu) )=0A+ = return;=0A+=0A+ if ( !per_cpu(batching, this_cpu) || in_irq() )=0A = smp_send_event_check_cpu(cpu);=0A+ else=0A+ set_bit(nr, = &per_cpu(batch_mask, this_cpu));=0A+}=0A+=0A+void cpu_raise_softirq_batch_b= egin(void)=0A+{=0A+ ++this_cpu(batching);=0A+}=0A+=0A+void cpu_raise_sof= tirq_batch_finish(void)=0A+{=0A+ unsigned int cpu, this_cpu =3D = smp_processor_id();=0A+ cpumask_t *mask =3D &per_cpu(batch_mask, = this_cpu);=0A+=0A+ ASSERT(per_cpu(batching, this_cpu));=0A+ = for_each_cpu ( cpu, mask )=0A+ if ( !softirq_pending(cpu) )=0A+ = cpumask_clear_cpu(cpu, mask);=0A+ smp_send_event_check_mask(mask)= ;=0A+ cpumask_clear(mask);=0A+ --per_cpu(batching, this_cpu);=0A = }=0A =0A void raise_softirq(unsigned int nr)=0A--- a/xen/include/xen/softir= q.h=0A+++ b/xen/include/xen/softirq.h=0A@@ -30,6 +30,9 @@ void cpumask_rais= e_softirq(const cpumask=0A void cpu_raise_softirq(unsigned int cpu, = unsigned int nr);=0A void raise_softirq(unsigned int nr);=0A =0A+void = cpu_raise_softirq_batch_begin(void);=0A+void cpu_raise_softirq_batch_finish= (void);=0A+=0A /*=0A * Process pending softirqs on this CPU. This should = be called periodically=0A * when performing work that prevents softirqs = from running in a timely manner.=0A --=__Part497C010A.1__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__Part497C010A.1__=--