Skip to contentSkip to conversation

x86/cpuidle: get accurate C0 value with xenpm tool

PATCH Superseded Huaitong Han 2 participants 4 messages May 11, 2015 – May 12, 2015

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 x86/cpuidle: get accurate C0 value with xenpm tool

When checking the ACPI funciton of C-status, after 100 seconds sleep, the sampling value of C0 status from the xenpm tool decreases. Because C0=NOW()-C1-C2-C3-C4, when NOW() value is during idle time, NOW() value is bigger than last C-status update time, and C0 value is also bigger than ture value. if margin of the second error cannot make up for margin of the first error, the value of C0 would decrease.

---
ChangeLog:
V4:
delete pointless initializers and hard tabs.

V3:
1.Don't use tick_to_ns inside lock in print_acpi_power. 2.Use 08 padding in printk.
3.Merge two "for" circulation into one for coding style.

V2:
C0 = last_cx_update_time-C1-C2-C3-C4, but last_cx_update_time is not now, so the C0 value is stale, NOW-last_update_time should be calculated. C[current_cx_stat]+=NOW-last_update_time, so the CX value is fresh.

V1:
Initial patch
---

xen/arch/x86/acpi/cpu_idle.c +50 -22
index e639c99..9f7ccc4 100644
@@ -254,9 +254,10 @@ static char* acpi_cstate_method_name[] =
 
 static void print_acpi_power(uint32_t cpu, struct acpi_processor_power *power)
 {
-    uint32_t i, idle_usage = 0;
-    uint64_t res, idle_res = 0;
-    u32 usage;
+    uint64_t idle_res = 0, idle_usage = 0, last_state_update_tick, now;
+    uint64_t usage[ACPI_PROCESSOR_MAX_POWER] = { 0 };
+    uint64_t res[ACPI_PROCESSOR_MAX_POWER] = { 0 };
+    unsigned int i;
     u8 last_state_idx;
 
     printk("==cpu%d==\n", cpu);
@@ -264,28 +265,36 @@ static void print_acpi_power(uint32_t cpu, struct acpi_processor_power *power)
     printk("active state:\t\tC%d\n", last_state_idx);
     printk("max_cstate:\t\tC%d\n", max_cstate);
     printk("states:\n");
-    
+
+    spin_lock_irq(&power->stat_lock);
+    now = NOW();
     for ( i = 1; i < power->count; i++ )
     {
-        spin_lock_irq(&power->stat_lock);	
-        res = tick_to_ns(power->states[i].time);
-        usage = power->states[i].usage;
-        spin_unlock_irq(&power->stat_lock);
+        res[i] = tick_to_ns(power->states[i].time);
+        usage[i] = power->states[i].usage;
+    }
+    last_state_update_tick = power->last_state_update_tick;
+    spin_unlock_irq(&power->stat_lock);
+
+    res[last_state_idx] += now - tick_to_ns(last_state_update_tick);
+    usage[last_state_idx] += 1;
 
-        idle_usage += usage;
-        idle_res += res;
+    for ( i = 1; i < power->count; i++ )
+    {
+        idle_usage += usage[i];
+        idle_res += res[i];
 
         printk((last_state_idx == i) ? "   *" : "    ");
         printk("C%d:\t", i);
         printk("type[C%d] ", power->states[i].type);
         printk("latency[%03d] ", power->states[i].latency);
-        printk("usage[%08d] ", usage);
+        printk("usage[%08"PRIu64"] ", usage[i]);
         printk("method[%5s] ", acpi_cstate_method_name[power->states[i].entry_method]);
-        printk("duration[%"PRId64"]\n", res);
+        printk("duration[%"PRIu64"]\n", res[i]);
     }
     printk((last_state_idx == 0) ? "   *" : "    ");
-    printk("C0:\tusage[%08d] duration[%"PRId64"]\n",
-           idle_usage, NOW() - idle_res);
+    printk("C0:\tusage[%08"PRIu64"] duration[%"PRIu64"]\n",
+           usage[0] + idle_usage, res[0] + tick_to_ns(last_state_update_tick) - idle_res);
 
     print_hw_residencies(cpu);
 }
@@ -486,6 +495,17 @@ bool_t errata_c6_eoi_workaround(void)
     return (fix_needed && cpu_has_pending_apic_eoi());
 }
 
+void update_last_cx_stat(struct acpi_processor_power *power,
+                         struct acpi_processor_cx *cx, uint64_t ticks)
+{
+    ASSERT(!local_irq_is_enabled());
+
+    spin_lock(&power->stat_lock);
+    power->last_state = cx;
+    power->last_state_update_tick = ticks;
+    spin_unlock(&power->stat_lock);
+}
+
 void update_idle_stats(struct acpi_processor_power *power,
                        struct acpi_processor_cx *cx,
                        uint64_t before, uint64_t after)
@@ -501,6 +521,8 @@ void update_idle_stats(struct acpi_processor_power *power,
         power->last_residency = tick_to_ns(sleep_ticks) / 1000UL;
         cx->time += sleep_ticks;
     }
+    power->last_state = &power->states[0];
+    power->last_state_update_tick = after;
 
     spin_unlock(&power->stat_lock);
 }
@@ -557,7 +579,6 @@ static void acpi_processor_idle(void)
     if ( (cx->type == ACPI_STATE_C3) && errata_c6_eoi_workaround() )
         cx = power->safe_state;
 
-    power->last_state = cx;
 
     /*
      * Sleep:
@@ -574,6 +595,7 @@ static void acpi_processor_idle(void)
             t1 = cpuidle_get_tick();
             /* Trace cpu idle entry */
             TRACE_4D(TRC_PM_IDLE_ENTRY, cx->idx, t1, exp, pred);
+            update_last_cx_stat(power, cx, t1);
             /* Invoke C2 */
             acpi_idle_do_entry(cx);
             /* Get end time (ticks) */
@@ -602,7 +624,7 @@ static void acpi_processor_idle(void)
         t1 = cpuidle_get_tick();
         /* Trace cpu idle entry */
         TRACE_4D(TRC_PM_IDLE_ENTRY, cx->idx, t1, exp, pred);
-
+        update_last_cx_stat(power, cx, t1);
         /*
          * disable bus master
          * bm_check implies we need ARB_DIS
@@ -1171,7 +1193,7 @@ uint32_t pmstat_get_cx_nr(uint32_t cpuid)
 int pmstat_get_cx_stat(uint32_t cpuid, struct pm_cx_stat *stat)
 {
     struct acpi_processor_power *power = processor_powers[cpuid];
-    uint64_t idle_usage = 0, idle_res = 0;
+    uint64_t idle_usage = 0, idle_res = 0, last_state_update_time, now;
     uint64_t usage[ACPI_PROCESSOR_MAX_POWER], res[ACPI_PROCESSOR_MAX_POWER];
     unsigned int i, nr, nr_pc = 0, nr_cc = 0;
 
@@ -1185,7 +1207,6 @@ int pmstat_get_cx_stat(uint32_t cpuid, struct pm_cx_stat *stat)
         return 0;
     }
 
-    stat->last = power->last_state ? power->last_state->idx : 0;
     stat->idle_time = get_cpu_idle_time(cpuid);
     nr = min(stat->nr, power->count);
 
@@ -1193,9 +1214,11 @@ int pmstat_get_cx_stat(uint32_t cpuid, struct pm_cx_stat *stat)
     if ( pm_idle_save == NULL )
     {
         stat->nr = 2;
+        stat->last = power->last_state ? power->last_state->idx : 0;
 
         usage[1] = idle_usage = 1;
         res[1] = idle_res = stat->idle_time;
+        last_state_update_time = now = NOW();
     }
     else
     {
@@ -1203,16 +1226,18 @@ int pmstat_get_cx_stat(uint32_t cpuid, struct pm_cx_stat *stat)
 
         stat->nr = power->count;
 
+        spin_lock_irq(&power->stat_lock);
+        now = NOW();
         for ( i = 1; i < nr; i++ )
         {
-            spin_lock_irq(&power->stat_lock);
             usage[i] = power->states[i].usage;
             res[i] = tick_to_ns(power->states[i].time);
-            spin_unlock_irq(&power->stat_lock);
-
             idle_usage += usage[i];
             idle_res += res[i];
         }
+        last_state_update_time = tick_to_ns(power->last_state_update_tick);
+        stat->last = power->last_state ? power->last_state->idx : 0;
+        spin_unlock_irq(&power->stat_lock);
 
         get_hw_residencies(cpuid, &hw_res);
 
@@ -1243,7 +1268,10 @@ int pmstat_get_cx_stat(uint32_t cpuid, struct pm_cx_stat *stat)
     }
 
     usage[0] = idle_usage;
-    res[0] = NOW() - idle_res;
+    usage[stat->last] += 1;
+
+    res[0] = last_state_update_time - idle_res;
+    res[stat->last] += now - last_state_update_time;
 
     if ( copy_to_guest(stat->triggers, usage, nr) ||
          copy_to_guest(stat->residencies, res, nr) )
xen/arch/x86/cpu/mwait-idle.c +3 -4
index 6dd5822..596d32f 100644
@@ -536,7 +536,6 @@ static void mwait_idle(void)
 		return;
 	}
 
-	power->last_state = cx;
 	eax = cx->address;
 	cstate = ((eax >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK) + 1;
 
@@ -554,6 +553,8 @@ static void mwait_idle(void)
 
 	before = cpuidle_get_tick();
 	TRACE_4D(TRC_PM_IDLE_ENTRY, cx->type, before, exp, pred);
+	/* Now in CX */
+	update_last_cx_stat(power, cx, before);
 
 	if (cpu_is_haltable(cpu))
 		mwait_idle_with_hints(eax, MWAIT_ECX_INTERRUPT_BREAK);
@@ -565,15 +566,13 @@ static void mwait_idle(void)
 	TRACE_6D(TRC_PM_IDLE_EXIT, cx->type, after,
 		irq_traced[0], irq_traced[1], irq_traced[2], irq_traced[3]);
 
+	/* Now back in C0 */
 	update_idle_stats(power, cx, before, after);
 	local_irq_enable();
 
 	if (!(lapic_timer_reliable_states & (1 << cstate)))
 		lapic_timer_on();
 
-	/* Now back in C0. */
-	power->last_state = &power->states[0];
-
 	sched_tick_resume();
 	cpufreq_dbs_timer_resume();
 
xen/include/asm-x86/cpuidle.h +2 -0
index 4d70677..46e614b 100644
@@ -23,6 +23,8 @@ void acpi_dead_idle(void);
 void trace_exit_reason(u32 *irq_traced);
 void update_idle_stats(struct acpi_processor_power *,
                        struct acpi_processor_cx *, uint64_t, uint64_t);
+void update_last_cx_stat(struct acpi_processor_power *,
+                         struct acpi_processor_cx *, uint64_t);
 
 /*
  * vcpu is urgent if vcpu is polling event channel
xen/include/xen/cpuidle.h +1 -0
index b7b9e8c..342f4fe 100644
@@ -66,6 +66,7 @@ struct acpi_processor_power
     struct acpi_processor_cx *last_state;
     struct acpi_processor_cx *safe_state;
     void *gdata; /* governor specific data */
+    u64 last_state_update_tick;
     u32 last_residency;
     u32 count;
     spinlock_t stat_lock;
Signature · 1 line
1.9.1
#1 lore raw
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 11.05.15 at 11:34, <[email protected]> wrote:

---
ChangeLog:
V4:
delete pointless initializers and hard tabs.

Thanks, but ...

Quoted 2 lines

V3:
1.Don't use tick_to_ns inside lock in print_acpi_power.

... note that this also was not fully done:

Quoted 21 lines
xen/arch/x86/acpi/cpu_idle.c +4 -4
@@ -254,9 +254,10 @@ static char* acpi_cstate_method_name[] =
 
 static void print_acpi_power(uint32_t cpu, struct acpi_processor_power *power)
 {
-    uint32_t i, idle_usage = 0;
-    uint64_t res, idle_res = 0;
-    u32 usage;
+    uint64_t idle_res = 0, idle_usage = 0, last_state_update_tick, now;
+    uint64_t usage[ACPI_PROCESSOR_MAX_POWER] = { 0 };
+    uint64_t res[ACPI_PROCESSOR_MAX_POWER] = { 0 };
+    unsigned int i;
     u8 last_state_idx;
 
     printk("==cpu%d==\n", cpu);
@@ -264,28 +265,36 @@ static void print_acpi_power(uint32_t cpu, struct acpi_processor_power *power)
     printk("active state:\t\tC%d\n", last_state_idx);
     printk("max_cstate:\t\tC%d\n", max_cstate);
     printk("states:\n");
-    

xen/arch/x86/acpi/cpu_idle.c static void print_acpi_power(uint32_t cpu, struct acpi_processor_power *power) · line 273

Quoted 10 lines
xen/arch/x86/acpi/cpu_idle.c +4 -4
+
+    spin_lock_irq(&power->stat_lock);
+    now = NOW();
     for ( i = 1; i < power->count; i++ )
     {
-        spin_lock_irq(&power->stat_lock);	
-        res = tick_to_ns(power->states[i].time);
-        usage = power->states[i].usage;
-        spin_unlock_irq(&power->stat_lock);
+        res[i] = tick_to_ns(power->states[i].time);

This one still sits in a locked region.

xen/arch/x86/acpi/cpu_idle.c static void print_acpi_power(uint32_t cpu, struct acpi_processor_power *power) · line 280

Quoted 7 lines
xen/arch/x86/acpi/cpu_idle.c +7 -0
+        usage[i] = power->states[i].usage;
+    }
+    last_state_update_tick = power->last_state_update_tick;
+    spin_unlock_irq(&power->stat_lock);
+
+    res[last_state_idx] += now - tick_to_ns(last_state_update_tick);
+    usage[last_state_idx] += 1;

I'm also pretty certain that I had asked to use ++ in cases like this.

Quoted 9 lines
xen/arch/x86/acpi/cpu_idle.c +2 -1
@@ -1203,16 +1226,18 @@ int pmstat_get_cx_stat(uint32_t cpuid, struct pm_cx_stat *stat)
 
         stat->nr = power->count;
 
+        spin_lock_irq(&power->stat_lock);
+        now = NOW();
         for ( i = 1; i < nr; i++ )
         {
-            spin_lock_irq(&power->stat_lock);

xen/arch/x86/acpi/cpu_idle.c int pmstat_get_cx_stat(uint32_t cpuid, struct pm_cx_stat *stat) · line 1240

Quoted 10 lines
xen/arch/x86/acpi/cpu_idle.c +3 -2
             usage[i] = power->states[i].usage;
             res[i] = tick_to_ns(power->states[i].time);
-            spin_unlock_irq(&power->stat_lock);
-
             idle_usage += usage[i];
             idle_res += res[i];
         }
+        last_state_update_time = tick_to_ns(power->last_state_update_tick);
+        stat->last = power->last_state ? power->last_state->idx : 0;
+        spin_unlock_irq(&power->stat_lock);

And just like in the other case the loop above should be split, such that the lock gets dropped as quickly as possible.

Quoted 12 lines
xen/arch/x86/acpi/cpu_idle.c +2 -0
@@ -554,6 +553,8 @@ static void mwait_idle(void)
 
 	before = cpuidle_get_tick();
 	TRACE_4D(TRC_PM_IDLE_ENTRY, cx->type, before, exp, pred);
+	/* Now in CX */
+	update_last_cx_stat(power, cx, before);
 
 	if (cpu_is_haltable(cpu))
 		mwait_idle_with_hints(eax, MWAIT_ECX_INTERRUPT_BREAK);
@@ -565,15 +566,13 @@ static void mwait_idle(void)
 	TRACE_6D(TRC_PM_IDLE_EXIT, cx->type, after,
 		irq_traced[0], irq_traced[1], irq_traced[2], irq_traced[3]);

xen/arch/x86/acpi/cpu_idle.c static void mwait_idle(void) · line 575

Quoted 10 lines
xen/arch/x86/acpi/cpu_idle.c +1 -2
 
+	/* Now back in C0 */
 	update_idle_stats(power, cx, before, after);
 	local_irq_enable();
 
 	if (!(lapic_timer_reliable_states & (1 << cstate)))
 		lapic_timer_on();
 
-	/* Now back in C0. */
-	power->last_state = &power->states[0];

Please obey coding style, even more so when the comment was well formed so far.

Jan

#2 lore raw
Quoted 38 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 11.05.15 at 11:34, <[email protected]> wrote:

---
ChangeLog:
V4:
delete pointless initializers and hard tabs.

Thanks, but ...

Quoted 2 lines

V3:
1.Don't use tick_to_ns inside lock in print_acpi_power.

... note that this also was not fully done:

Quoted 25 lines
xen/arch/x86/acpi/cpu_idle.c +7 -4
@@ -254,9 +254,10 @@ static char* acpi_cstate_method_name[] =
 
 static void print_acpi_power(uint32_t cpu, struct acpi_processor_power *power)
 {
-    uint32_t i, idle_usage = 0;
-    uint64_t res, idle_res = 0;
-    u32 usage;
+    uint64_t idle_res = 0, idle_usage = 0, last_state_update_tick, now;
+    uint64_t usage[ACPI_PROCESSOR_MAX_POWER] = { 0 };
+    uint64_t res[ACPI_PROCESSOR_MAX_POWER] = { 0 };
+    unsigned int i;
     u8 last_state_idx;
 
     printk("==cpu%d==\n", cpu);
@@ -264,28 +265,36 @@ static void print_acpi_power(uint32_t cpu, struct acpi_processor_power *power)
     printk("active state:\t\tC%d\n", last_state_idx);
     printk("max_cstate:\t\tC%d\n", max_cstate);
     printk("states:\n");
-    
+
+    spin_lock_irq(&power->stat_lock);
+    now = NOW();
     for ( i = 1; i < power->count; i++ )
Quoted 8 lines

xen/arch/x86/acpi/cpu_idle.c static void print_acpi_power(uint32_t cpu, struct acpi_processor_power *power) · line 273

Quoted 6 lines
xen/arch/x86/acpi/cpu_idle.c +1 -4
     {
-        spin_lock_irq(&power->stat_lock);	
-        res = tick_to_ns(power->states[i].time);
-        usage = power->states[i].usage;
-        spin_unlock_irq(&power->stat_lock);
+        res[i] = tick_to_ns(power->states[i].time);

This one still sits in a locked region.

Accepted.

Quoted 10 lines

xen/arch/x86/acpi/cpu_idle.c static void print_acpi_power(uint32_t cpu, struct acpi_processor_power *power) · line 280

Quoted 8 lines
xen/arch/x86/acpi/cpu_idle.c +7 -0
+        usage[i] = power->states[i].usage;
+    }
+    last_state_update_tick = power->last_state_update_tick;
+    spin_unlock_irq(&power->stat_lock);
+
+    res[last_state_idx] += now - tick_to_ns(last_state_update_tick);
+    usage[last_state_idx] += 1;

I'm also pretty certain that I had asked to use ++ in cases like this.

Accepted.

Quoted 5 lines
Quoted 5 lines
xen/arch/x86/acpi/cpu_idle.c +0 -0
@@ -1203,16 +1226,18 @@ int pmstat_get_cx_stat(uint32_t cpuid, struct pm_cx_stat *stat)
 
         stat->nr = power->count;
 
Quoted 13 lines

xen/arch/x86/acpi/cpu_idle.c int pmstat_get_cx_stat(uint32_t cpuid, struct pm_cx_stat *stat) · line 1238

Quoted 13 lines
xen/arch/x86/acpi/cpu_idle.c +3 -3
+        spin_lock_irq(&power->stat_lock);
+        now = NOW();
         for ( i = 1; i < nr; i++ )
         {
-            spin_lock_irq(&power->stat_lock);
             usage[i] = power->states[i].usage;
             res[i] = tick_to_ns(power->states[i].time);
-            spin_unlock_irq(&power->stat_lock);
-
             idle_usage += usage[i];
             idle_res += res[i];
         }
+        last_state_update_time = tick_to_ns(power->last_state_update_tick);

This "tick_to_ns" still sits in a locked region. for moving out locked region, a temp variable named "last_state_update_tick" need be defined, the coding style may be not look nice as there is a variable named "last_state_update_time".

Quoted 5 lines

xen/arch/x86/acpi/cpu_idle.c int pmstat_get_cx_stat(uint32_t cpuid, struct pm_cx_stat *stat) · line 1240

Quoted 2 lines
xen/arch/x86/acpi/cpu_idle.c +2 -0
+        stat->last = power->last_state ? power->last_state->idx : 0;
+        spin_unlock_irq(&power->stat_lock);

And just like in the other case the loop above should be split, such that the lock gets dropped as quickly as possible.

Accepted. The loop can be split as the same as the V2 edition code,but "coding style (you have the same "for" a few lines up for reference) " doesn't look nice, and I think it is not useful just to move a "for" loop down a few lines.

Quoted 18 lines
Quoted 18 lines
xen/arch/x86/acpi/cpu_idle.c +3 -0
@@ -554,6 +553,8 @@ static void mwait_idle(void)
 
 	before = cpuidle_get_tick();
 	TRACE_4D(TRC_PM_IDLE_ENTRY, cx->type, before, exp, pred);
+	/* Now in CX */
+	update_last_cx_stat(power, cx, before);
 
 	if (cpu_is_haltable(cpu))
 		mwait_idle_with_hints(eax, MWAIT_ECX_INTERRUPT_BREAK);
@@ -565,15 +566,13 @@ static void mwait_idle(void)
 	TRACE_6D(TRC_PM_IDLE_EXIT, cx->type, after,
 		irq_traced[0], irq_traced[1], irq_traced[2], irq_traced[3]);
 
+	/* Now back in C0 */
 	update_idle_stats(power, cx, before, after);
 	local_irq_enable();
 
Quoted 8 lines

xen/arch/x86/acpi/cpu_idle.c static void mwait_idle(void) · line 575

Quoted 5 lines
xen/arch/x86/acpi/cpu_idle.c +0 -2
 	if (!(lapic_timer_reliable_states & (1 << cstate)))
 		lapic_timer_on();
 
-	/* Now back in C0. */
-	power->last_state = &power->states[0];

Please obey coding style, even more so when the comment was well formed so far.

I will pay more attention to coding style.

Quoted 3 lines

Jan

#3 lore raw
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 11.05.15 at 16:12, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 11.05.15 at 11:34, <[email protected]> wrote:
xen/arch/x86/acpi/cpu_idle.c +2 -3
@@ -1203,16 +1226,18 @@ int pmstat_get_cx_stat(uint32_t cpuid, struct pm_cx_stat *stat)
 
         stat->nr = power->count;
 
+        spin_lock_irq(&power->stat_lock);
+        now = NOW();
         for ( i = 1; i < nr; i++ )
         {
-            spin_lock_irq(&power->stat_lock);
             usage[i] = power->states[i].usage;
             res[i] = tick_to_ns(power->states[i].time);
-            spin_unlock_irq(&power->stat_lock);
-
Quoted 8 lines
Quoted 4 lines

xen/arch/x86/acpi/cpu_idle.c int pmstat_get_cx_stat(uint32_t cpuid, struct pm_cx_stat *stat) · line 1238

Quoted 4 lines
xen/arch/x86/acpi/cpu_idle.c +1 -0
             idle_usage += usage[i];
             idle_res += res[i];
         }
+        last_state_update_time = tick_to_ns(power->last_state_update_tick);

This "tick_to_ns" still sits in a locked region. for moving out locked region, a temp variable named "last_state_update_tick" need be defined, the coding style may be not look nice as there is a variable named "last_state_update_time".

I don't see the need for yet another variable. If need be, just rename the existing one to ..._tick.

Jan