Skip to contentSkip to conversation

Always save/restore performance counters when HVM guest switching VCPU

PATCH In review [email protected] 4 participants 5 messages Mar 1, 2013 – Mar 8, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Always save/restore performance counters when HVM guest switching VCPU

From: Suravee Suthikulpanit <[email protected]>

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.

---
 xen/arch/x86/hvm/svm/vpmu.c |   62 ++++++++++++++++++++++++-------------------
 1 file changed, 35 insertions(+), 27 deletions(-)
xen/arch/x86/hvm/svm/vpmu.c +35 -27
index bf186fe..4854cf3 100644
@@ -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);
Signature · 1 line
1.7.10.4
Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · On 03/01/2013 03:49 PM, [email protected] wrote:

From: Suravee Suthikulpanit <[email protected]>

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.

Description may need to be cleaned up a bit but other than that

Quoted 109 lines
---
  xen/arch/x86/hvm/svm/vpmu.c |   62 ++++++++++++++++++++++++-------------------
  1 file changed, 35 insertions(+), 27 deletions(-)
xen/arch/x86/hvm/svm/vpmu.c +35 -27
index bf186fe..4854cf3 100644
@@ -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);
#2 lore raw
In reply to [email protected]
Quoted 8 lines · On Fri, Mar 1, 2013 at 8:49 PM, <[email protected]> wrote:

From: Suravee Suthikulpanit <[email protected]>

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?

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.

-George

#3 lore raw
Quoted 17 lines
Quoted 1 line
Quoted 1 line · George Dunlap
Quoted 8 lines · On Fri, Mar 1, 2013 at 8:49 PM, <[email protected]> wrote:

From: Suravee Suthikulpanit <[email protected]>

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?

Quoted 3 lines

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

#4 lore raw
Quoted 19 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · George Dunlap
Quoted 8 lines · On Fri, Mar 1, 2013 at 8:49 PM, <[email protected]> wrote:

From: Suravee Suthikulpanit <[email protected]>

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?

Quoted 3 lines

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.

Quoted 9 lines

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

I understand that we don't want to introduce this overhead. Let me look into:
1. Measuring the overhead in this case.
2. Looking into the alternative approach (lazy save/restore)

and get back to you all.

Suravee