[v9,16/32] timers: Optimization for timer_base_try_to_set_idle()

Message ID 20231201092654.34614-17-anna-maria@linutronix.de
State New
Headers
Series timers: Move from a push remote at enqueue to a pull at expiry model |

Commit Message

Anna-Maria Behnsen Dec. 1, 2023, 9:26 a.m. UTC
  When tick is stopped also the timer base is_idle flag is set. When
reentering the timer_base_try_to_set_idle() with the tick stopped, there is
no need to check whether the timer base needs to be set idle again. When a
timer was enqueued in the meantime, this is already handled by the
nohz_get_next_event() call which was executed before tick_nohz_stop_tick().

Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
---
v9: New, as this optimization was splitted from the patch before.
---
 kernel/time/tick-sched.c |  2 +-
 kernel/time/timer.c      | 11 ++++++++---
 2 files changed, 9 insertions(+), 4 deletions(-)
  

Comments

Sebastian Andrzej Siewior Dec. 4, 2023, 5:52 p.m. UTC | #1
On 2023-12-01 10:26:38 [+0100], Anna-Maria Behnsen wrote:
> When tick is stopped also the timer base is_idle flag is set. When
> reentering the timer_base_try_to_set_idle() with the tick stopped, there is
> no need to check whether the timer base needs to be set idle again. When a
> timer was enqueued in the meantime, this is already handled by the
> nohz_get_next_event() call which was executed before tick_nohz_stop_tick().

as of #15 tick_stopped is set later in tick_nohz_stop_tick() and both
(tick_sched::tick_stopped and timer_base::is_idle) are cleared in
tick_nohz_restart_sched_tick().

Then we have tick_nohz_idle_retain_tick() with only one caller and is
only clearing timer_base::is_idle. Now, wouldn't it make sense to
preload timer_idle based on timer_base::is_idle?

I don't know if it there is a different outcome if timer_base::is_idle
gets cleared in the idle path vs tick_sched::tick_stopped.
I can't find nohz_get_next_event().

> Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>

Sebastian
  
Anna-Maria Behnsen Dec. 5, 2023, 12:05 p.m. UTC | #2
Sebastian Siewior <bigeasy@linutronix.de> writes:

> On 2023-12-01 10:26:38 [+0100], Anna-Maria Behnsen wrote:
>> When tick is stopped also the timer base is_idle flag is set. When
>> reentering the timer_base_try_to_set_idle() with the tick stopped, there is
>> no need to check whether the timer base needs to be set idle again. When a
>> timer was enqueued in the meantime, this is already handled by the
>> nohz_get_next_event() call which was executed before tick_nohz_stop_tick().
>
> as of #15 tick_stopped is set later in tick_nohz_stop_tick() and both
> (tick_sched::tick_stopped and timer_base::is_idle) are cleared in
> tick_nohz_restart_sched_tick().
>
> Then we have tick_nohz_idle_retain_tick() with only one caller and is
> only clearing timer_base::is_idle. Now, wouldn't it make sense to
> preload timer_idle based on timer_base::is_idle?

When revisting the code, this timer_clear_idle() is no longer required
in tick_nohz_idle_retain_tick(). This is only called when the tick is
not stopped - so timer base is not idle as well and this call is
superfluous.

As we keep both states in sync (tick_sched::tick_stopped and
timer_base::is_idle) it doesn't matter which one is used. In
tick_nohz_stop_tick() I don't have access to timer base. I could add it
to timer_base_try_to_set_idle() but it will not make a difference.

> I don't know if it there is a different outcome if timer_base::is_idle
> gets cleared in the idle path vs tick_sched::tick_stopped.
> I can't find nohz_get_next_event().

s/nohz_get_next_event/tick_nohz_next_event/ ...

>> Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
>
> Sebastian

Thanks,

	Anna-Maria
  

Patch

diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
index 3e1cdb7c6966..c6b415052c56 100644
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -886,7 +886,7 @@  static void tick_nohz_stop_tick(struct tick_sched *ts, int cpu)
 	struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
 	unsigned long basejiff = ts->last_jiffies;
 	u64 basemono = ts->timer_expires_base;
-	bool timer_idle;
+	bool timer_idle = ts->tick_stopped;
 	u64 expires;
 
 	/* Make sure we won't be trying to stop it twice in a row. */
diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index df6558f62e6f..177bcde8a2c0 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -1996,13 +1996,18 @@  u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
  * timer_base_try_to_set_idle() - Try to set the idle state of the timer bases
  * @basej:	base time jiffies
  * @basem:	base time clock monotonic
- * @idle:	pointer to store the value of timer_base->is_idle
+ * @idle:	pointer to store the value of timer_base->is_idle on return;
+ *		*idle contains the information whether tick was already stopped
  *
- * Returns the tick aligned clock monotonic time of the next pending
- * timer or KTIME_MAX if no timer is pending.
+ * Returns the tick aligned clock monotonic time of the next pending timer or
+ * KTIME_MAX if no timer is pending. When tick was already stopped KTIME_MAX is
+ * returned as well.
  */
 u64 timer_base_try_to_set_idle(unsigned long basej, u64 basem, bool *idle)
 {
+	if (*idle)
+		return KTIME_MAX;
+
 	return __get_next_timer_interrupt(basej, basem, idle);
 }