From: "Jan Beulich" Subject: [PATCH, RFC] x86/HVM: batch vCPU wakeups Date: Tue, 09 Sep 2014 09:33:37 +0100 Message-ID: <540ED7810200007800032678@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartF8CDAF71.1__=" Return-path: Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1XRGrv-0006mP-Ez for xen-devel@lists.xenproject.org; Tue, 09 Sep 2014 08:33:43 +0000 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. --=__PartF8CDAF71.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. 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 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. With the patch here, average broadcast times for a 64-vCPU guest went to a measured maximum of 310us (which is still quite a lot). This isn't just helping to reduce the number of ICR writes when the host APICs run in clustered mode, but also to reduce 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 (to perhaps a large part due to overlapping the wakeups of multiple CPUs). 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 --- RFC for two reasons: 1) I started to consider elimination of the event-check IPIs in a more general way when MONITOR/MWAIT is in use: As long as the remote CPU is known to be MWAITing (or in the process of resuming after MWAIT), the write of softirq_pending(cpu) ought to be sufficient to wake it. This would yield the patch here pointless on MONITOR/MWAIT capable hardware. 2) The condition when to enable batching in vlapic_ipi() is already rather complex, but it is nevertheless unclear whether it would benefit from further tuning (as mentioned above). --- a/xen/arch/x86/hvm/vlapic.c +++ b/xen/arch/x86/hvm/vlapic.c @@ -447,12 +447,30 @@ void vlapic_ipi( =20 default: { struct vcpu *v; - for_each_vcpu ( vlapic_domain(vlapic), v ) + const struct domain *d =3D vlapic_domain(vlapic); + bool_t batch =3D d->max_vcpus > 2 && + (short_hand + ? short_hand !=3D APIC_DEST_SELF + : vlapic_x2apic_mode(vlapic) + ? dest_mode ? hweight16(dest) > 1 + : dest =3D=3D 0xffffffff + : dest_mode + ? hweight8(dest & + GET_xAPIC_DEST_FIELD( + vlapic_get_reg(vlapic, + APIC_DFR))) > 1 + : dest =3D=3D 0xff); + + if ( batch ) + cpu_raise_softirq_batch_begin(); + for_each_vcpu ( d, 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; @@ -70,22 +73,56 @@ void open_softirq(int nr, softirq_handle =20 void cpumask_raise_softirq(const cpumask_t *mask, unsigned int nr) { - int cpu; - cpumask_t send_mask; + unsigned int cpu =3D smp_processor_id(); + cpumask_t send_mask, *raise_mask; + + if ( !per_cpu(batching, cpu) || in_irq() ) + { + cpumask_clear(&send_mask); + raise_mask =3D &send_mask; + } + else + raise_mask =3D &per_cpu(batch_mask, cpu); =20 - cpumask_clear(&send_mask); for_each_cpu(cpu, mask) if ( !test_and_set_bit(nr, &softirq_pending(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()) ) + unsigned int this_cpu =3D smp_processor_id(); + + if ( test_and_set_bit(nr, &softirq_pending(cpu)) + || (cpu =3D=3D this_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) +{ + ++per_cpu(batching, smp_processor_id()); +} + +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. --=__PartF8CDAF71.1__= Content-Type: text/plain; name="vcpu-wake-batch.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="vcpu-wake-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. Considering that = Windows=0Abroadcasts IPIs from its timer interrupt, which at least at = certain=0Atimes can run at 1kHz, it is clear that this can result in = good=0Aguest behavior. In fact, on said hardware guests with significantly= =0Abeyond 40 vCPU-s simply hung when e.g. ServerManager gets started.=0AWit= h the patch here, average broadcast times for a 64-vCPU guest went=0Ato a = measured maximum of 310us (which is still quite a lot).=0A=0AThis isn't = just helping to reduce the number of ICR writes when the=0Ahost APICs run = in clustered mode, but also to reduce them by=0Asuppressing the sends = altogether when - by the time=0Acpu_raise_softirq_batch_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 (to perhaps a=0Alarge part due to overlapping the wakeups of = multiple CPUs).=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---= =0ARFC for two reasons:=0A1) I started to consider elimination of the = event-check IPIs in a more=0A general way when MONITOR/MWAIT is in use: = As long as the remote CPU=0A is known to be MWAITing (or in the process = of resuming after MWAIT),=0A the write of softirq_pending(cpu) ought to = be sufficient to wake it.=0A This would yield the patch here pointless = on MONITOR/MWAIT capable=0A hardware.=0A2) The condition when to enable = batching in vlapic_ipi() is already=0A rather complex, but it is = nevertheless unclear whether it would=0A benefit from further tuning (as = mentioned above).=0A=0A--- a/xen/arch/x86/hvm/vlapic.c=0A+++ b/xen/arch/x86= /hvm/vlapic.c=0A@@ -447,12 +447,30 @@ void vlapic_ipi(=0A =0A default: = {=0A struct vcpu *v;=0A- for_each_vcpu ( vlapic_domain(vlapi= c), v )=0A+ const struct domain *d =3D vlapic_domain(vlapic);=0A+ = bool_t batch =3D d->max_vcpus > 2 &&=0A+ = (short_hand=0A+ ? short_hand !=3D APIC_DEST_SELF=0A+= : vlapic_x2apic_mode(vlapic)=0A+ = ? dest_mode ? hweight16(dest) > 1=0A+ = : dest =3D=3D 0xffffffff=0A+ : dest_mode=0A= + ? hweight8(dest &=0A+ = GET_xAPIC_DEST_FIELD(=0A+ = vlapic_get_reg(vlapic,=0A+ = APIC_DFR))) > 1=0A+ : dest = =3D=3D 0xff);=0A+=0A+ if ( batch )=0A+ cpu_raise_softirq_= batch_begin();=0A+ for_each_vcpu ( d, 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_handlers[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@@ -70,22 +73,56 @@ void open_softirq(int nr, softirq_handle= =0A =0A void cpumask_raise_softirq(const cpumask_t *mask, unsigned int = nr)=0A {=0A- int cpu;=0A- cpumask_t send_mask;=0A+ unsigned int = cpu =3D smp_processor_id();=0A+ cpumask_t send_mask, *raise_mask;=0A+=0A= + if ( !per_cpu(batching, cpu) || in_irq() )=0A+ {=0A+ = cpumask_clear(&send_mask);=0A+ raise_mask =3D &send_mask;=0A+ = }=0A+ else=0A+ raise_mask =3D &per_cpu(batch_mask, cpu);=0A =0A- = cpumask_clear(&send_mask);=0A for_each_cpu(cpu, mask)=0A if = ( !test_and_set_bit(nr, &softirq_pending(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_mask);=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+ unsigned int this_cpu =3D smp_proces= sor_id();=0A+=0A+ if ( test_and_set_bit(nr, &softirq_pending(cpu))=0A+ = || (cpu =3D=3D this_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_begin(void)=0A+{=0A+ = ++per_cpu(batching, smp_processor_id());=0A+}=0A+=0A+void cpu_raise_softi= rq_batch_finish(void)=0A+{=0A+ unsigned int cpu, this_cpu =3D smp_proces= sor_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(ma= sk);=0A+ --per_cpu(batching, this_cpu);=0A }=0A =0A void raise_softirq(u= nsigned int nr)=0A--- a/xen/include/xen/softirq.h=0A+++ b/xen/include/xen/s= oftirq.h=0A@@ -30,6 +30,9 @@ void cpumask_raise_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(v= oid);=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 --=__PartF8CDAF71.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 --=__PartF8CDAF71.1__=--