x86/mwait-idle: updates from Linux (and more)
v3 cover x86/mwait-idle: updates from Linux (and more)
1: enable interrupts before C1 on Xeons
2: squash stats update when not actually entering C-state
Jan
PATCH v3 1/2 x86/mwait-idle: enable interrupts before C1 on Xeons
From: Artem Bityutskiy <[email protected]>
Enable local interrupts before requesting C1 on the last two generations of Intel Xeon platforms: Sky Lake, Cascade Lake, Cooper Lake, Ice Lake. This decreases average C1 interrupt latency by about 5-10%, as measured with the 'wult' tool.
The '->enter()' function of the driver enters C-states with local interrupts disabled by executing the 'monitor' and 'mwait' pair of instructions. If an interrupt happens, the CPU exits the C-state and continues executing instructions after 'mwait'. It does not jump to the interrupt handler, because local interrupts are disabled. The cpuidle subsystem enables interrupts a bit later, after doing some housekeeping.
With this patch, we enable local interrupts before requesting C1. In this case, if the CPU wakes up because of an interrupt, it will jump to the interrupt handler right away. The cpuidle housekeeping will be done after the pending interrupt(s) are handled.
Enabling interrupts before entering a C-state has measurable impact for faster C-states, like C1. Deeper, but slower C-states like C6 do not really benefit from this sort of change, because their latency is a lot higher comparing to the delay added by cpuidle housekeeping.
This change was also tested with cyclictest and dbench. In case of Ice Lake, the average cyclictest latency decreased by 5.1%, and the average 'dbench' throughput increased by about 0.8%. Both tests were run for 4 hours with only C1 enabled (all other idle states, including 'POLL', were disabled). CPU frequency was pinned to HFM, and uncore frequency was pinned to the maximum value. The other platforms had similar single-digit percentage improvements.
It is worth noting that this patch affects 'cpuidle' statistics a tiny bit. Before this patch, C1 residency did not include the interrupt handling time, but with this patch, it will include it. This is similar to what happens in case of the 'POLL' state, which also runs with interrupts enabled.
We don't have a pointer into cpuidle_state_table[] readily available. To compensate, propagate the flag into struct acpi_processor_cx.
Unlike Linux we want to
- disable IRQs again after MWAITing, as subsequently invoked functions assume so,
- avoid enabling IRQs if cstate_restore_tsc() is not a no-op, to avoid interfering with, in particular, the time rendezvous.
---
RFC: I'm not entirely certain that we want to take this, i.e. whether
we're as much worried about interrupt latency.
RFC: I was going back and forth between putting the local_irq_enable()
ahead of or after cpu_is_haltable().
---
v3: Propagate flag to struct acpi_processor_cx. Don't set flag when TSC
may stop whild in a C-state.
v2: New.
xen/arch/x86/cpu/mwait-idle.c +19 -3
@@ -108,6 +108,11 @@ static const struct cpuidle_state {#define CPUIDLE_FLAG_DISABLED 0x1/* +* Enable interrupts before entering the C-state. On some platforms and for +* some C-states, this may measurably decrease interrupt latency. +*/ +#define CPUIDLE_FLAG_IRQ_ENABLE 0x8000 +/* * Set this flag for states where the HW flushes the TLB for us * and so we don't need cross-calls to keep it consistent. * If this flag is set, SW flushes the TLB, so even if the
@@ -539,7 +544,7 @@ static struct cpuidle_state __read_mostlstatic struct cpuidle_state __read_mostly skx_cstates[] = { { .name ="C1" , - .flags =MWAIT2flg (0x00 ), + .flags =MWAIT2flg (0x00 ) |CPUIDLE_FLAG_IRQ_ENABLE , .exit_latency =2 , .target_residency =2 , },
@@ -561,7 +566,7 @@ static struct cpuidle_state __read_mostlstatic const struct cpuidle_state icx_cstates[] = { { .name ="C1" , - .flags =MWAIT2flg (0x00 ), + .flags =MWAIT2flg (0x00 ) |CPUIDLE_FLAG_IRQ_ENABLE , .exit_latency =1 , .target_residency =1 , },
@@ -842,9 +847,15 @@ static void mwait_idle(void)update_last_cx_stat (power, cx, before); -if (cpu_is_haltable (cpu)) +if (cpu_is_haltable (cpu)) { +if (cx->irq_enable_early) +local_irq_enable (); +mwait_idle_with_hints (cx->address,MWAIT_ECX_INTERRUPT_BREAK ); +local_irq_disable (); + } + after =alternative_call (cpuidle_get_tick);cstate_restore_tsc ();
@@ -1335,6 +1346,11 @@ static int mwait_idle_cpu_init(struct no cx->latency = cpuidle_state_table[cstate].exit_latency; cx->target_residency = cpuidle_state_table[cstate].target_residency; +if ((cpuidle_state_table[cstate].flags & +CPUIDLE_FLAG_IRQ_ENABLE ) && +/* cstate_restore_tsc() needs to be a no-op */ +boot_cpu_has (X86_FEATURE_NONSTOP_TSC )) + cx->irq_enable_early = true; dev->count++; }
xen/include/xen/cpuidle.h +1 -0
@@ -42,6 +42,7 @@ struct acpi_processor_cxu8 idx;u8 type;/* ACPI_STATE_Cn */ u8 entry_method;/* ACPI_CSTATE_EM_xxx */ +bool irq_enable_early;u32 address;u32 latency;u32 target_residency;
Re: [PATCH v3 1/2] x86/mwait-idle: enable interrupts before C1 on Xeons
Quoted 46 lines · Jan Beulich
From: Artem Bityutskiy <[email protected]>
Enable local interrupts before requesting C1 on the last two generations of Intel Xeon platforms: Sky Lake, Cascade Lake, Cooper Lake, Ice Lake. This decreases average C1 interrupt latency by about 5-10%, as measured with the 'wult' tool.
The '->enter()' function of the driver enters C-states with local interrupts disabled by executing the 'monitor' and 'mwait' pair of instructions. If an interrupt happens, the CPU exits the C-state and continues executing instructions after 'mwait'. It does not jump to the interrupt handler, because local interrupts are disabled. The cpuidle subsystem enables interrupts a bit later, after doing some housekeeping.
With this patch, we enable local interrupts before requesting C1. In this case, if the CPU wakes up because of an interrupt, it will jump to the interrupt handler right away. The cpuidle housekeeping will be done after the pending interrupt(s) are handled.
Enabling interrupts before entering a C-state has measurable impact for faster C-states, like C1. Deeper, but slower C-states like C6 do not really benefit from this sort of change, because their latency is a lot higher comparing to the delay added by cpuidle housekeeping.
This change was also tested with cyclictest and dbench. In case of Ice Lake, the average cyclictest latency decreased by 5.1%, and the average 'dbench' throughput increased by about 0.8%. Both tests were run for 4 hours with only C1 enabled (all other idle states, including 'POLL', were disabled). CPU frequency was pinned to HFM, and uncore frequency was pinned to the maximum value. The other platforms had similar single-digit percentage improvements.
It is worth noting that this patch affects 'cpuidle' statistics a tiny bit. Before this patch, C1 residency did not include the interrupt handling time, but with this patch, it will include it. This is similar to what happens in case of the 'POLL' state, which also runs with interrupts enabled.
Suggested-by Len Brown <[email protected]>Signed-off-by Artem Bityutskiy <[email protected]>[Linux commit: c227233ad64c77e57db738ab0e46439db71822a3]We don't have a pointer into cpuidle_state_table[] readily available. To compensate, propagate the flag into struct
acpi_processor_cx.
Quoted 7 lines
Unlike Linux we want to
- disable IRQs again after MWAITing, as subsequently invoked functions assume so,
- avoid enabling IRQs ifcstate_restore_tsc()is not a no-op, to avoid interfering with, in particular, the time rendezvous.Signed-off-by Jan Beulich <[email protected]>
Quoted 3 lines
---
RFC: I'm not entirely certain that we want to take this, i.e. whether
we're as much worried about interrupt latency.
I would assume taking this would make it easier for you to pick further patches.
Quoted 71 lines
RFC: I was going back and forth between putting the
local_irq_enable()
ahead of or aftercpu_is_haltable().
---
v3: Propagate flag to structacpi_processor_cx. Don't set flag when TSC
may stop whild in a C-state.
v2: New.xen/arch/x86/cpu/mwait-idle.c +19
-3@@ -108,6 +108,11 @@ static const struct cpuidle_state {#define CPUIDLE_FLAG_DISABLED 0x1/* +* Enable interrupts before entering the C-state. On some platforms and for +* some C-states, this may measurably decrease interrupt latency. +*/ +#define CPUIDLE_FLAG_IRQ_ENABLE 0x8000 +/* * Set this flag for states where the HW flushes the TLB for us * and so we don't need cross-calls to keep it consistent. * If this flag is set, SW flushes the TLB, so even if the @@ -539,7 +544,7 @@ static struct cpuidle_state __read_mostlstatic struct cpuidle_state __read_mostly skx_cstates[] = { { .name ="C1" , - .flags =MWAIT2flg (0x00 ), + .flags =MWAIT2flg (0x00 ) |CPUIDLE_FLAG_IRQ_ENABLE , .exit_latency =2 , .target_residency =2 , },@@ -561,7 +566,7 @@ static struct cpuidle_state __read_mostlstatic const struct cpuidle_state icx_cstates[] = { { .name ="C1" , - .flags =MWAIT2flg (0x00 ), + .flags =MWAIT2flg (0x00 ) |CPUIDLE_FLAG_IRQ_ENABLE , .exit_latency =1 , .target_residency =1 , },@@ -842,9 +847,15 @@ static void mwait_idle(void)update_last_cx_stat (power, cx, before); -if (cpu_is_haltable (cpu)) +if (cpu_is_haltable (cpu)) { +if (cx->irq_enable_early) +local_irq_enable (); +mwait_idle_with_hints (cx->address,MWAIT_ECX_INTERRUPT_BREAK ); +local_irq_disable (); + } + after =alternative_call (cpuidle_get_tick);cstate_restore_tsc ();@@ -1335,6 +1346,11 @@ static int mwait_idle_cpu_init(struct no cx->latency = cpuidle_state_table[cstate].exit_latency; cx->target_residency = cpuidle_state_table[cstate].target_residency; +if ((cpuidle_state_table[cstate].flags & +CPUIDLE_FLAG_IRQ_ENABLE ) && +/* cstate_restore_tsc() needs to be a no-op */ +boot_cpu_has (X86_FEATURE_NONSTOP_TSC )) + cx->irq_enable_early = true; dev->count++; }
xen/include/xen/cpuidle.h struct acpi_processor_cx · line 45
Quoted 4 lines
xen/include/xen/cpuidle.h +1
-0@@ -42,6 +42,7 @@ struct acpi_processor_cxu8 idx;u8 type;/* ACPI_STATE_Cn */ u8 entry_method;/* ACPI_CSTATE_EM_xxx */ +bool irq_enable_early;
Should you use a bit field here and limit the field to :1 in expectation of maybe adding more flags at a later point?
Thanks, Roger.
Re: [PATCH v3 1/2] x86/mwait-idle: enable interrupts before C1 on Xeons
Quoted 53 lines · Roger Pau Monné
Quoted 52 lines · Jan Beulich
From: Artem Bityutskiy <[email protected]>
Enable local interrupts before requesting C1 on the last two generations of Intel Xeon platforms: Sky Lake, Cascade Lake, Cooper Lake, Ice Lake. This decreases average C1 interrupt latency by about 5-10%, as measured with the 'wult' tool.
The '->enter()' function of the driver enters C-states with local interrupts disabled by executing the 'monitor' and 'mwait' pair of instructions. If an interrupt happens, the CPU exits the C-state and continues executing instructions after 'mwait'. It does not jump to the interrupt handler, because local interrupts are disabled. The cpuidle subsystem enables interrupts a bit later, after doing some housekeeping.
With this patch, we enable local interrupts before requesting C1. In this case, if the CPU wakes up because of an interrupt, it will jump to the interrupt handler right away. The cpuidle housekeeping will be done after the pending interrupt(s) are handled.
Enabling interrupts before entering a C-state has measurable impact for faster C-states, like C1. Deeper, but slower C-states like C6 do not really benefit from this sort of change, because their latency is a lot higher comparing to the delay added by cpuidle housekeeping.
This change was also tested with cyclictest and dbench. In case of Ice Lake, the average cyclictest latency decreased by 5.1%, and the average 'dbench' throughput increased by about 0.8%. Both tests were run for 4 hours with only C1 enabled (all other idle states, including 'POLL', were disabled). CPU frequency was pinned to HFM, and uncore frequency was pinned to the maximum value. The other platforms had similar single-digit percentage improvements.
It is worth noting that this patch affects 'cpuidle' statistics a tiny bit. Before this patch, C1 residency did not include the interrupt handling time, but with this patch, it will include it. This is similar to what happens in case of the 'POLL' state, which also runs with interrupts enabled.
Suggested-by Len Brown <[email protected]>Signed-off-by Artem Bityutskiy <[email protected]>[Linux commit: c227233ad64c77e57db738ab0e46439db71822a3]We don't have a pointer into cpuidle_state_table[] readily available. To compensate, propagate the flag into struct
acpi_processor_cx.Unlike Linux we want to
- disable IRQs again after MWAITing, as subsequently invoked functions assume so,
- avoid enabling IRQs ifcstate_restore_tsc()is not a no-op, to avoid interfering with, in particular, the time rendezvous.
Quoted 3 lines
Quoted 1 line
Signed-off-by Jan Beulich <[email protected]>Acked-by Roger Pau Monné <[email protected]>
Thanks.
Quoted 6 lines
Quoted 3 lines
---
RFC: I'm not entirely certain that we want to take this, i.e. whether
we're as much worried about interrupt latency.I would assume taking this would make it easier for you to pick further patches.
At least a little, yes.
Quoted 10 lines
xen/include/xen/cpuidle.h struct acpi_processor_cx · line 45
Quoted 7 lines
xen/include/xen/cpuidle.h +1
-0@@ -42,6 +42,7 @@ struct acpi_processor_cxu8 idx;u8 type;/* ACPI_STATE_Cn */ u8 entry_method;/* ACPI_CSTATE_EM_xxx */ +bool irq_enable_early;Should you use a bit field here and limit the field to :1 in expectation of maybe adding more flags at a later point?
Well, I had considered doing so but then thought we can easily switch at such future time, leaving it consistently witout bit fields for now. The more that there's still a 32-bit padding field left to put stuff in.
Jan
PATCH v3 2/2 x86/mwait-idle: squash stats update when not actually entering C-state
In reply to Jan BeulichWhile we don't want to skip calling update_idle_stats(), arrange for it to not increment the overall time spent in the state we didn't really enter.
---
RFC: If we wanted to also move the tracing, then I think the part ahead
of the if() also would need moving. At that point we could as well move update_last_cx_stat(), too, which afaict would allow skipping update_idle_stats() on the "else" path (which therefore would go away). Yet then, with the setting of power->safe_state moved up a little (which imo it should have been anyway) the two cpu_is_haltable() invocations would only have the lapic_timer_off() invocation left in between. This would then seem to call for simply ditching the 2nd one - acpi-idle also doesn't have a 2nd instance.
TBD: For the tracing I wonder if that really needs to come ahead of the
local_irq_enable(). Maybe trace_exit_reason() needs to, but quite certainly TRACE_6D() doesn't.
---
v3: Also move cstate_restore_tsc() invocation and split ones to
update_idle_stats().
v2: New.
xen/arch/x86/cpu/mwait-idle.c +11 -5
@@ -854,17 +854,23 @@ static void mwait_idle(void)mwait_idle_with_hints (cx->address,MWAIT_ECX_INTERRUPT_BREAK );local_irq_disable (); - } - after =alternative_call (cpuidle_get_tick); + after =alternative_call (cpuidle_get_tick); + +cstate_restore_tsc (); + +/* Now back in C0. */ +update_idle_stats (power, cx, before, after); + }else { +/* Never left C0. */ + after =alternative_call (cpuidle_get_tick); +update_idle_stats (power, cx, after, after); + } -cstate_restore_tsc ();trace_exit_reason (irq_traced);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 << cx->type)))
Re: [PATCH v3 2/2] x86/mwait-idle: squash stats update when not actually entering C-state
Quoted 5 lines · Jan Beulich
While we don't want to skip calling
update_idle_stats(), arrange for it to not increment the overall time spent in the state we didn't really enter.Signed-off-by Jan Beulich <[email protected]>
Quoted 10 lines
---
RFC: If we wanted to also move the tracing, then I think the part ahead
of theif()also would need moving. At that point we could as well moveupdate_last_cx_stat(), too, which afaict would allow skippingupdate_idle_stats()on the "else" path (which therefore would go away). Yet then, with the setting ofpower->safe_statemoved up a little (which imo it should have been anyway) the twocpu_is_haltable()invocations would only have thelapic_timer_off()invocation left in between. This would then seem to call for simply ditching the 2nd one - acpi-idle also doesn't have a 2nd instance.
It's possible for lapic_timer_off to take a non-trivial amount of time when virtualized, but it's likely we won't be using mwait in that case, so not sure it matter much to have the two cpu_is_haltable calls if there's just a lapic_timer_off between them.
Quoted 3 lines
TBD: For the tracing I wonder if that really needs to come ahead of the
local_irq_enable(). Maybetrace_exit_reason()needs to, but quite certainlyTRACE_6D()doesn't.
Would be good if it could be moved after the local_irq_enable call, as it's not as trivial as I've expected, and will just add latency to any pending interrupt waiting to be serviced. FWIW, I haven't spotted a need to call it with interrupt disabled.
Quoted 14 lines
---
v3: Also movecstate_restore_tsc()invocation and split ones to
update_idle_stats().
v2: New.xen/arch/x86/cpu/mwait-idle.c +0
-2@@ -854,17 +854,23 @@ static void mwait_idle(void)mwait_idle_with_hints (cx->address,MWAIT_ECX_INTERRUPT_BREAK );local_irq_disable (); - } - after =alternative_call (cpuidle_get_tick);
xen/arch/x86/cpu/mwait-idle.c static void mwait_idle(void) · line 867
Quoted 10 lines
xen/arch/x86/cpu/mwait-idle.c +10
-0+ after =alternative_call (cpuidle_get_tick); + +cstate_restore_tsc (); + +/* Now back in C0. */ +update_idle_stats (power, cx, before, after); + }else { +/* Never left C0. */ + after =alternative_call (cpuidle_get_tick); +update_idle_stats (power, cx, after, after);
While adjusting this, could you also modify update_idle_stats to avoid increasing cx->usage if before == after (or !sleep_ticks). I don't think it's fine to increase the state counter if we never actually entered it.
I was also going to suggest that you don't set 'after' and just use update_idle_stats(power, cx, before, before); but seeing as TRACE_6D also makes use of 'after' there's not much point without further rework. I also see the RFC note at the top, so while I think this is an improvement, I agree it would be nice to avoid the trace altogether if we never actually enter the state. If you want to rework the patch or send a followup that's fine for me.
Thanks, Roger.
Re: [PATCH v3 2/2] x86/mwait-idle: squash stats update when not actually entering C-state
Quoted 22 lines · Roger Pau Monné
Quoted 5 lines · Jan Beulich
While we don't want to skip calling
update_idle_stats(), arrange for it to not increment the overall time spent in the state we didn't really enter.Signed-off-by Jan Beulich <[email protected]>Quoted 10 lines
---
RFC: If we wanted to also move the tracing, then I think the part ahead
of theif()also would need moving. At that point we could as well moveupdate_last_cx_stat(), too, which afaict would allow skippingupdate_idle_stats()on the "else" path (which therefore would go away). Yet then, with the setting ofpower->safe_statemoved up a little (which imo it should have been anyway) the twocpu_is_haltable()invocations would only have thelapic_timer_off()invocation left in between. This would then seem to call for simply ditching the 2nd one - acpi-idle also doesn't have a 2nd instance.It's possible for
lapic_timer_offto take a non-trivial amount of time when virtualized, but it's likely we won't be using mwait in that case, so not sure it matter much to have the twocpu_is_haltablecalls if there's just alapic_timer_offbetween them.
Quoted 8 lines
Quoted 3 lines
TBD: For the tracing I wonder if that really needs to come ahead of the
local_irq_enable(). Maybetrace_exit_reason()needs to, but quite certainlyTRACE_6D()doesn't.Would be good if it could be moved after the
local_irq_enablecall, as it's not as trivial as I've expected, and will just add latency to any pending interrupt waiting to be serviced. FWIW, I haven't spotted a need to call it with interrupt disabled.
Okay, I guess I'll to the larger rework then.
Quoted 8 lines
Quoted 8 lines
xen/arch/x86/cpu/mwait-idle.c +0
-1@@ -854,17 +854,23 @@ static void mwait_idle(void)mwait_idle_with_hints (cx->address,MWAIT_ECX_INTERRUPT_BREAK );local_irq_disable (); - }
Quoted 16 lines
xen/arch/x86/cpu/mwait-idle.c static void mwait_idle(void) · line 867
Quoted 11 lines
xen/arch/x86/cpu/mwait-idle.c +10
-1- after =alternative_call (cpuidle_get_tick); + after =alternative_call (cpuidle_get_tick); + +cstate_restore_tsc (); + +/* Now back in C0. */ +update_idle_stats (power, cx, before, after); + }else { +/* Never left C0. */ + after =alternative_call (cpuidle_get_tick); +update_idle_stats (power, cx, after, after);While adjusting this, could you also modify
update_idle_statsto avoid increasingcx->usageif before == after (or !sleep_ticks). I don't think it's fine to increase the state counter if we never actually entered it.
I did consider it but then decided against. Even leaving this aspect aside the counter only counts _attempts_ to enter a certain state; the CPU may find reasons to never actually enter it. And what we have when before == after is still an attempt, albeit an unsuccessful one.
Jan
Roger Pau Monné
Acked-byRe: [PATCH v3 2/2] x86/mwait-idle: squash stats update when not actually entering C-state
Quoted 59 lines · Jan Beulich
Quoted 22 lines · Roger Pau Monné
Quoted 5 lines · Jan Beulich
While we don't want to skip calling
update_idle_stats(), arrange for it to not increment the overall time spent in the state we didn't really enter.Signed-off-by Jan Beulich <[email protected]>Quoted 10 lines
---
RFC: If we wanted to also move the tracing, then I think the part ahead
of theif()also would need moving. At that point we could as well moveupdate_last_cx_stat(), too, which afaict would allow skippingupdate_idle_stats()on the "else" path (which therefore would go away). Yet then, with the setting ofpower->safe_statemoved up a little (which imo it should have been anyway) the twocpu_is_haltable()invocations would only have thelapic_timer_off()invocation left in between. This would then seem to call for simply ditching the 2nd one - acpi-idle also doesn't have a 2nd instance.It's possible for
lapic_timer_offto take a non-trivial amount of time when virtualized, but it's likely we won't be using mwait in that case, so not sure it matter much to have the twocpu_is_haltablecalls if there's just alapic_timer_offbetween them.Quoted 8 lines
Quoted 3 lines
TBD: For the tracing I wonder if that really needs to come ahead of the
local_irq_enable(). Maybetrace_exit_reason()needs to, but quite certainlyTRACE_6D()doesn't.Would be good if it could be moved after the
local_irq_enablecall, as it's not as trivial as I've expected, and will just add latency to any pending interrupt waiting to be serviced. FWIW, I haven't spotted a need to call it with interrupt disabled.Okay, I guess I'll to the larger rework then.
Quoted 8 lines
Quoted 8 lines
xen/arch/x86/cpu/mwait-idle.c +0
-1@@ -854,17 +854,23 @@ static void mwait_idle(void)mwait_idle_with_hints (cx->address,MWAIT_ECX_INTERRUPT_BREAK );local_irq_disable (); - }Quoted 16 lines
xen/arch/x86/cpu/mwait-idle.c static void mwait_idle(void) · line 867
Quoted 11 lines
xen/arch/x86/cpu/mwait-idle.c +10
-1- after =alternative_call (cpuidle_get_tick); + after =alternative_call (cpuidle_get_tick); + +cstate_restore_tsc (); + +/* Now back in C0. */ +update_idle_stats (power, cx, before, after); + }else { +/* Never left C0. */ + after =alternative_call (cpuidle_get_tick); +update_idle_stats (power, cx, after, after);While adjusting this, could you also modify
update_idle_statsto avoid increasingcx->usageif before == after (or !sleep_ticks). I don't think it's fine to increase the state counter if we never actually entered it.
Quoted 4 lines
I did consider it but then decided against. Even leaving this aspect aside the counter only counts _attempts_ to enter a certain state; the CPU may find reasons to never actually enter it. And what we have when before == after is still an attempt, albeit an unsuccessful one.
Right, in which case:
Not sure whether you would like to commit this now and do the lager rework as a followup patch. That would be fine by me.
Thanks, Roger.
Re: [PATCH v3 2/2] x86/mwait-idle: squash stats update when not actually entering C-state
Quoted 60 lines · Roger Pau Monné
Quoted 59 lines · Jan Beulich
Quoted 22 lines · Roger Pau Monné
Quoted 5 lines · Jan Beulich
While we don't want to skip calling
update_idle_stats(), arrange for it to not increment the overall time spent in the state we didn't really enter.Signed-off-by Jan Beulich <[email protected]>Quoted 10 lines
---
RFC: If we wanted to also move the tracing, then I think the part ahead
of theif()also would need moving. At that point we could as well moveupdate_last_cx_stat(), too, which afaict would allow skippingupdate_idle_stats()on the "else" path (which therefore would go away). Yet then, with the setting ofpower->safe_statemoved up a little (which imo it should have been anyway) the twocpu_is_haltable()invocations would only have thelapic_timer_off()invocation left in between. This would then seem to call for simply ditching the 2nd one - acpi-idle also doesn't have a 2nd instance.It's possible for
lapic_timer_offto take a non-trivial amount of time when virtualized, but it's likely we won't be using mwait in that case, so not sure it matter much to have the twocpu_is_haltablecalls if there's just alapic_timer_offbetween them.Quoted 8 lines
Quoted 3 lines
TBD: For the tracing I wonder if that really needs to come ahead of the
local_irq_enable(). Maybetrace_exit_reason()needs to, but quite certainlyTRACE_6D()doesn't.Would be good if it could be moved after the
local_irq_enablecall, as it's not as trivial as I've expected, and will just add latency to any pending interrupt waiting to be serviced. FWIW, I haven't spotted a need to call it with interrupt disabled.Okay, I guess I'll to the larger rework then.
Quoted 24 lines
Quoted 9 lines
xen/arch/x86/cpu/mwait-idle.c +0
-2@@ -854,17 +854,23 @@ static void mwait_idle(void)mwait_idle_with_hints (cx->address,MWAIT_ECX_INTERRUPT_BREAK );local_irq_disable (); - } - after =alternative_call (cpuidle_get_tick);xen/arch/x86/cpu/mwait-idle.c static void mwait_idle(void) · line 867
Quoted 10 lines
xen/arch/x86/cpu/mwait-idle.c +10
-0+ after =alternative_call (cpuidle_get_tick); + +cstate_restore_tsc (); + +/* Now back in C0. */ +update_idle_stats (power, cx, before, after); + }else { +/* Never left C0. */ + after =alternative_call (cpuidle_get_tick); +update_idle_stats (power, cx, after, after);While adjusting this, could you also modify
update_idle_statsto avoid increasingcx->usageif before == after (or !sleep_ticks). I don't think it's fine to increase the state counter if we never actually entered it.
Quoted 8 lines
Quoted 4 lines
I did consider it but then decided against. Even leaving this aspect aside the counter only counts _attempts_ to enter a certain state; the CPU may find reasons to never actually enter it. And what we have when before == after is still an attempt, albeit an unsuccessful one.
Right, in which case:
Acked-by Roger Pau Monné <[email protected]>
Thanks, but ...
Quoted 2 lines
Not sure whether you would like to commit this now and do the lager rework as a followup patch. That would be fine by me.
... no, I'd rather do this in a single step. In its current shape the patch is actually moving us in the opposite direction.
Jan
PATCH v4 1/1 x86/mwait-idle: re-order state entry/exit code a little
In reply to Jan BeulichThe initial observation is that unlike the original ACPI idle driver we have a 2nd cpu_is_haltable() in here. By making the actual state entry conditional, the emitted trace records as well as the subsequent stats update are at least misleading in case the state wasn't actually entered. Hence they would want moving inside the conditional. At which point the cpuidle_get_tick() invocations could (and hence should) move as well. cstate_restore_tsc() also isn't needed if we didn't actually enter the state.
This leaves only the errata_c6_workaround() and lapic_timer_off() invocations outside the conditional. As a result it looks easier to drop the conditional (and come back in sync with the other driver again) than to move almost everything into the conditional.
While there also move the TRACE_6D() out of the IRQ-disabled region.
---
Moving the TRACE_6D() may be a little controversial, as this could lead to a sequence of trace records not actually representing the sequence of events, in case further records get emitted by interrupt handlers. But with us now conditionally enabling interrupts around MWAIT, that issue exists already anyway.
Unlike said in the earlier outline of the alternative approach, errata_c6_workaround() cannot be moved: cpu_has_pending_apic_eoi() needs to be called when IRQs are already off.
---
v4: Different approach (and title), as was previously outlined as an
alternative.
v3: Also move cstate_restore_tsc() invocation and split ones to
update_idle_stats().
v2: New.
xen/arch/x86/cpu/mwait-idle.c +7 -8
@@ -847,26 +847,25 @@ static void mwait_idle(void)update_last_cx_stat (power, cx, before); -if (cpu_is_haltable (cpu)) { -if (cx->irq_enable_early) -local_irq_enable (); +if (cx->irq_enable_early) +local_irq_enable (); -mwait_idle_with_hints (cx->address,MWAIT_ECX_INTERRUPT_BREAK ); +mwait_idle_with_hints (cx->address,MWAIT_ECX_INTERRUPT_BREAK ); -local_irq_disable (); - } +local_irq_disable (); after =alternative_call (cpuidle_get_tick);cstate_restore_tsc ();trace_exit_reason (irq_traced); -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 (); +TRACE_6D (TRC_PM_IDLE_EXIT , cx->type, after, + irq_traced[0 ], irq_traced[1 ], irq_traced[2 ], irq_traced[3 ]); +if (!(lapic_timer_reliable_states & (1 << cx->type)))lapic_timer_on ();
Re: [PATCH v4] x86/mwait-idle: re-order state entry/exit code a little
Quoted 9 lines · Jan Beulich
The initial observation is that unlike the original ACPI idle driver we have a 2nd
cpu_is_haltable()in here. By making the actual state entry conditional, the emitted trace records as well as the subsequent stats update are at least misleading in case the state wasn't actually entered. Hence they would want moving inside the conditional. At which point thecpuidle_get_tick()invocations could (and hence should) move as well.cstate_restore_tsc()also isn't needed if we didn't actually enter the state.
Quoted 8 lines
This leaves only the
errata_c6_workaround()andlapic_timer_off()invocations outside the conditional. As a result it looks easier to drop the conditional (and come back in sync with the other driver again) than to move almost everything into the conditional.While there also move the
TRACE_6D()out of the IRQ-disabled region.Signed-off-by Jan Beulich <[email protected]>
Quoted 6 lines
---
Moving theTRACE_6D()may be a little controversial, as this could lead to a sequence of trace records not actually representing the sequence of events, in case further records get emitted by interrupt handlers. But with us now conditionally enabling interrupts around MWAIT, that issue exists already anyway.
I think that's OK. We have to priority interrupt latency over trace readability.
Quoted 13 lines
Unlike said in the earlier outline of the alternative approach,
errata_c6_workaround()cannot be moved:cpu_has_pending_apic_eoi()needs to be called when IRQs are already off.
---
v4: Different approach (and title), as was previously outlined as an
alternative.
v3: Also movecstate_restore_tsc()invocation and split ones to
update_idle_stats().
v2: New.
xen/arch/x86/cpu/mwait-idle.c static void mwait_idle(void) · line 851
Quoted 8 lines
xen/arch/x86/cpu/mwait-idle.c +2
-3@@ -847,26 +847,25 @@ static void mwait_idle(void)update_last_cx_stat (power, cx, before); -if (cpu_is_haltable (cpu)) { -if (cx->irq_enable_early) -local_irq_enable (); +if (cx->irq_enable_early) +local_irq_enable ();
Now that I look at this again, we need to be careful with the enabling interrupts and the interaction with errata_c6_workaround. Enabling interrupts here could change the result of the check for pending EOIs, and thus enter mwait with a condition that could trigger the erratas. Hopefully CPUIDLE_FLAG_IRQ_ENABLE is only set for C1.
It might be prudent to only allow setting CPUIDLE_FLAG_IRQ_ENABLE for states <= 2.
Thanks, Roger.
Re: [PATCH v4] x86/mwait-idle: re-order state entry/exit code a little
Quoted 15 lines · Roger Pau Monné
Quoted 14 lines · Jan Beulich
The initial observation is that unlike the original ACPI idle driver we have a 2nd
cpu_is_haltable()in here. By making the actual state entry conditional, the emitted trace records as well as the subsequent stats update are at least misleading in case the state wasn't actually entered. Hence they would want moving inside the conditional. At which point thecpuidle_get_tick()invocations could (and hence should) move as well.cstate_restore_tsc()also isn't needed if we didn't actually enter the state.This leaves only the
errata_c6_workaround()andlapic_timer_off()invocations outside the conditional. As a result it looks easier to drop the conditional (and come back in sync with the other driver again) than to move almost everything into the conditional.
Quoted 5 lines
Quoted 3 lines
While there also move the
TRACE_6D()out of the IRQ-disabled region.Signed-off-by Jan Beulich <[email protected]>Reviewed-by Roger Pau Monné <[email protected]>
Thanks.
Quoted 18 lines
xen/arch/x86/cpu/mwait-idle.c static void mwait_idle(void) · line 851
Quoted 11 lines
xen/arch/x86/cpu/mwait-idle.c +2
-3@@ -847,26 +847,25 @@ static void mwait_idle(void)update_last_cx_stat (power, cx, before); -if (cpu_is_haltable (cpu)) { -if (cx->irq_enable_early) -local_irq_enable (); +if (cx->irq_enable_early) +local_irq_enable ();Now that I look at this again, we need to be careful with the enabling interrupts and the interaction with
errata_c6_workaround. Enabling interrupts here could change the result of the check for pending EOIs, and thus enter mwait with a condition that could trigger the erratas. HopefullyCPUIDLE_FLAG_IRQ_ENABLEis only set for C1.
Quoted 2 lines
It might be prudent to only allow setting
CPUIDLE_FLAG_IRQ_ENABLEfor states <= 2.
Well, the justification for enabling was the low exit time. I don't expect states > 2 to satisfy this criteria, so I think we're okay without further precautions added right away.
Jan