All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Always save/restore performance counters when HVM guest switching VCPU
@ 2013-03-01 20:49 suravee.suthikulpanit
  2013-03-01 23:02 ` Boris Ostrovsky
  2013-03-04 12:42 ` George Dunlap
  0 siblings, 2 replies; 16+ messages in thread
From: suravee.suthikulpanit @ 2013-03-01 20:49 UTC (permalink / raw)
  To: xen-devel, JBeulich; +Cc: Suravee Suthikulpanit

From: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>

Currently, the performance counter registers are saved/restores
when the HVM guest switchs VCPUs only if they are running.
However, PERF has one check where it writes the MSR and read back
the value to check if the MSR is working.  This has shown to fails
the check if the VCPU is moved in between rdmsr and wrmsr and
resulting in the values are different.

Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
---
 xen/arch/x86/hvm/svm/vpmu.c |   62 ++++++++++++++++++++++++-------------------
 1 file changed, 35 insertions(+), 27 deletions(-)

diff --git a/xen/arch/x86/hvm/svm/vpmu.c b/xen/arch/x86/hvm/svm/vpmu.c
index bf186fe..4854cf3 100644
--- a/xen/arch/x86/hvm/svm/vpmu.c
+++ b/xen/arch/x86/hvm/svm/vpmu.c
@@ -172,12 +172,16 @@ static inline void context_restore(struct vcpu *v)
     {
         wrmsrl(counters[i], ctxt->counters[i]);
 
+        if ( !vpmu_is_set(vpmu, VPMU_RUNNING) )
+            continue;
+
         /* Force an interrupt to allow guest reset the counter,
         if the value is positive */
         if ( is_overflowed(ctxt->counters[i]) && (ctxt->counters[i] > 0) )
         {
             gdprintk(XENLOG_WARNING, "VPMU: Force a performance counter "
-                "overflow interrupt!\n");
+                "overflow interrupt! (counter:%u value:0x%lx)\n",
+                i, ctxt->counters[i]);
             amd_vpmu_do_interrupt(0);
         }
     }
@@ -188,12 +192,13 @@ static void amd_vpmu_restore(struct vcpu *v)
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
     struct amd_vpmu_context *ctxt = vpmu->context;
 
-    if ( !(vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) &&
-           vpmu_is_set(vpmu, VPMU_RUNNING)) )
+    if ( !(vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED)) )
         return;
 
     context_restore(v);
-    apic_write(APIC_LVTPC, ctxt->hw_lapic_lvtpc);
+
+    if ( vpmu_is_set(vpmu, VPMU_RUNNING) ) 
+        apic_write(APIC_LVTPC, ctxt->hw_lapic_lvtpc);
 }
 
 static inline void context_save(struct vcpu *v)
@@ -214,13 +219,16 @@ static void amd_vpmu_save(struct vcpu *v)
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
     struct amd_vpmu_context *ctx = vpmu->context;
 
-    if ( !(vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) &&
-           vpmu_is_set(vpmu, VPMU_RUNNING)) )
+    if ( !(vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED)) )
         return;
 
     context_save(v);
-    ctx->hw_lapic_lvtpc = apic_read(APIC_LVTPC);
-    apic_write(APIC_LVTPC,  ctx->hw_lapic_lvtpc | APIC_LVT_MASKED);
+
+    if ( vpmu_is_set(vpmu, VPMU_RUNNING) )
+    {
+        ctx->hw_lapic_lvtpc = apic_read(APIC_LVTPC);
+        apic_write(APIC_LVTPC,  ctx->hw_lapic_lvtpc | APIC_LVT_MASKED);
+    }
 }
 
 static void context_update(unsigned int msr, u64 msr_content)
@@ -303,25 +311,25 @@ static int amd_vpmu_initialise(struct vcpu *v)
 
     if ( counters == NULL )
     {
-         switch ( family )
-	 {
-	 case 0x15:
-	     num_counters = F15H_NUM_COUNTERS;
-	     counters = AMD_F15H_COUNTERS;
-	     ctrls = AMD_F15H_CTRLS;
-	     k7_counters_mirrored = 1;
-	     break;
-	 case 0x10:
-	 case 0x12:
-	 case 0x14:
-	 case 0x16:
-	 default:
-	     num_counters = F10H_NUM_COUNTERS;
-	     counters = AMD_F10H_COUNTERS;
-	     ctrls = AMD_F10H_CTRLS;
-	     k7_counters_mirrored = 0;
-	     break;
-	 }
+        switch ( family )
+        {
+        case 0x15:
+            num_counters = F15H_NUM_COUNTERS;
+            counters = AMD_F15H_COUNTERS;
+            ctrls = AMD_F15H_CTRLS;
+            k7_counters_mirrored = 1;
+            break;
+        case 0x10:
+        case 0x12:
+        case 0x14:
+        case 0x16:
+        default:
+            num_counters = F10H_NUM_COUNTERS;
+            counters = AMD_F10H_COUNTERS;
+            ctrls = AMD_F10H_CTRLS;
+            k7_counters_mirrored = 0;
+            break;
+        }
     }
 
     ctxt = xzalloc(struct amd_vpmu_context);
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 16+ messages in thread
* Re: [PATCH] Always save/restore performance counters when HVM guest switching VCPU
@ 2013-03-08 14:50 Boris Ostrovsky
  2013-03-08 14:56 ` George Dunlap
  0 siblings, 1 reply; 16+ messages in thread
From: Boris Ostrovsky @ 2013-03-08 14:50 UTC (permalink / raw)
  To: JBeulich; +Cc: George.Dunlap, suravee.suthikulpanit, xen-devel


----- JBeulich@suse.com wrote:

> >>> On 04.03.13 at 13:42, George Dunlap <George.Dunlap@eu.citrix.com>
> wrote:
> > On Fri, Mar 1, 2013 at 8:49 PM,  <suravee.suthikulpanit@amd.com>
> wrote:
> >> From: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
> >>
> >> Currently, the performance counter registers are saved/restores
> >> when the HVM guest switchs VCPUs only if they are running.
> >> However, PERF has one check where it writes the MSR and read back
> >> the value to check if the MSR is working.  This has shown to fails
> >> the check if the VCPU is moved in between rdmsr and wrmsr and
> >> resulting in the values are different.
> > 
> > Many moons ago (circa 2005) when I used performance counters, I
> found
> > that adding them to the save/restore path added a non-neligible
> > overhead -- something like 5% slow-down.  Do you have any reason to
> > believe this is no longer the case?  Have you done any benchmarks
> > before and after?

I was doing some VPMU tracing a couple of weeks ago and by looking at 
trace timestamps I think I saw about 4000 cycles on VPMU save and 
~9000 cycles on restore. Don't remember what it was percentage-wise of
a whole context switch.

This was on Intel.

-boris


> > 
> > If there is a performance slow-down, you may have to implement
> > something like the "lazy FPU" save/restore, where you remove access
> to
> > the VPMU MSRs to detect that the guest is accessing them.
> 
> Suravee,
> 
> without addressing George's concerns, I don't think you can
> expect the patch to be committed (the more that Boris, along
> with his ack, also asked to adjust the description).
> 
> Jan
> 
> 
> _______________________________________________
> Xen-devel mailing list
> Xen-devel@lists.xen.org
> http://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 16+ messages in thread
* Re: [PATCH] Always save/restore performance counters when HVM guest switching VCPU
@ 2013-03-08 15:11 Boris Ostrovsky
  2013-03-11 11:11 ` George Dunlap
  0 siblings, 1 reply; 16+ messages in thread
From: Boris Ostrovsky @ 2013-03-08 15:11 UTC (permalink / raw)
  To: george.dunlap; +Cc: JBeulich, suravee.suthikulpanit, xen-devel


----- george.dunlap@eu.citrix.com wrote:

> On 08/03/13 14:50, Boris Ostrovsky wrote:
> > ----- JBeulich@suse.com wrote:
> >
> >>>>> On 04.03.13 at 13:42, George Dunlap
> <George.Dunlap@eu.citrix.com>
> >> wrote:
> >>> On Fri, Mar 1, 2013 at 8:49 PM,  <suravee.suthikulpanit@amd.com>
> >> wrote:
> >>>> From: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
> >>>>
> >>>> Currently, the performance counter registers are saved/restores
> >>>> when the HVM guest switchs VCPUs only if they are running.
> >>>> However, PERF has one check where it writes the MSR and read
> back
> >>>> the value to check if the MSR is working.  This has shown to
> fails
> >>>> the check if the VCPU is moved in between rdmsr and wrmsr and
> >>>> resulting in the values are different.
> >>> Many moons ago (circa 2005) when I used performance counters, I
> >> found
> >>> that adding them to the save/restore path added a non-neligible
> >>> overhead -- something like 5% slow-down.  Do you have any reason
> to
> >>> believe this is no longer the case?  Have you done any benchmarks
> >>> before and after?
> > I was doing some VPMU tracing a couple of weeks ago and by looking
> at
> > trace timestamps I think I saw about 4000 cycles on VPMU save and
> > ~9000 cycles on restore. Don't remember what it was percentage-wise
> of
> > a whole context switch.
> >
> > This was on Intel.
> 
> That's a really hefty expense to make all users pay on every context 
> switch, on behalf of a random check in a piece of software that only a
> handful of people are going to be actually using.

I believe Linux uses perf infrastructure to implement the watchdog.

> 
> I'm having a hard time telling what PERF is being talked about here --
> couldn't this check be fixed on their side, by perhaps checking the 
> CPUID leaf for the existence of Xen?

If by "here" you refer to the problem that Suravee's patch is trying to
address then I suspect it's this:
  http://lxr.linux.no/#linux+v3.8.2/arch/x86/kernel/cpu/perf_event.c#L210

> 
> If not I think a "lazy vpmu activation" is going to be the only
> option.

Yes, I actually was going to look at that.

-boris

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2013-03-12 15:12 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-03-01 20:49 [PATCH] Always save/restore performance counters when HVM guest switching VCPU suravee.suthikulpanit
2013-03-01 23:02 ` Boris Ostrovsky
2013-03-04 12:42 ` George Dunlap
2013-03-08  8:47   ` Jan Beulich
2013-03-08 22:52     ` Suravee Suthikulanit
  -- strict thread matches above, loose matches on Subject: below --
2013-03-08 14:50 Boris Ostrovsky
2013-03-08 14:56 ` George Dunlap
2013-03-08 15:15   ` Jan Beulich
2013-03-08 15:11 Boris Ostrovsky
2013-03-11 11:11 ` George Dunlap
2013-03-11 14:53   ` Konrad Rzeszutek Wilk
2013-03-11 14:59     ` George Dunlap
2013-03-11 15:54       ` Boris Ostrovsky
2013-03-11 16:03     ` Jan Beulich
2013-03-12  8:18     ` Dietmar Hahn
2013-03-12 15:12       ` Konrad Rzeszutek Wilk

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.