Skip to contentSkip to conversation

del_timer_sync scalability patch

PATCH In review Christoph Lameter 4 participants 6 messages Mar 8, 2005 High-resolution timers, timer wheel, clockevents

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 del_timer_sync scalability patch

When a potential periodic timer is deleted through timer_del_sync, all cpus are scanned to determine if the timer is running on that cpu. In a NUMA configuration doing so will cause NUMA interlink traffic which limits the scalability of timers.

The following patch makes the timer remember where the timer was last started. It is then possible to only wait for the completion of the timer on that specific cpu.

include/linux/timer.h +13 -0
Index: linux-2.6.11/include/linux/timer.h
===================================================================
@@ -19,10 +19,19 @@ struct timer_list {
 	unsigned long data;
 
 	struct tvec_t_base_s *base;
+#ifdef CONFIG_SMP
+	struct tvec_t_base_s *last_running;
+#endif
 };
 
 #define TIMER_MAGIC	0x4b87ad6e
 
+#ifdef CONFIG_SMP
+#define TIMER_INIT_LASTRUNNING .last_running = NULL,
+#else
+#define TIMER_INIT_LASTRUNNING
+#endif
+
 #define TIMER_INITIALIZER(_function, _expires, _data) {		\
 		.function = (_function),			\
 		.expires = (_expires),				\
@@ -30,6 +39,7 @@ struct timer_list {
 		.base = NULL,					\
 		.magic = TIMER_MAGIC,				\
 		.lock = SPIN_LOCK_UNLOCKED,			\
+		TIMER_INIT_LASTRUNNING                          \
 	}
 
 /***
@@ -41,6 +51,9 @@ struct timer_list {
  */
 static inline void init_timer(struct timer_list * timer)
 {
+#ifdef CONFIG_SMP
+	timer->last_running = NULL;
+#endif
 	timer->base = NULL;
 	timer->magic = TIMER_MAGIC;
 	spin_lock_init(&timer->lock);
kernel/timer.c +31 -14

76 lines not on this page · raw message

#1 lore raw
Quoted 9 lines · Christoph Lameter

When a potential periodic timer is deleted through timer_del_sync, all
cpus are scanned to determine if the timer is running on that cpu. In a NUMA configuration doing so will cause NUMA interlink traffic which limits the scalability of timers.

The following patch makes the timer remember where the timer was last started. It is then possible to only wait for the completion of the timer on that specific cpu.

OK, I stared at this for a while and cannot see holes in it. There may still be one though - this code is tricky. Probably any such holes would be covered up by the fact that timers never migrate between CPUs anyway, unless the handler chooses to do add_timer_on(), which I'm sure none do..

Ingo, could you take a look? Especially what happens if the timer hops CPUs after the

	/* Get where the timer ran last */
	base = timer->last_running;

statement. Do we have sufficient barriers in there for this?

A few tidyings which I'd suggest:

- Remove TIMER_INIT_LASTRUNNING: struct initialisers set unmentioned fields to zero anyway.

- Rename set_last_running() to timer_set_last_running, move it to timer.h. Then use it in init_timer() to avoid an ifdef.

---

 25-akpm/include/linux/timer.h |   21 ++++++++++-----------
 25-akpm/kernel/timer.c        |   10 +---------
 2 files changed, 11 insertions(+), 20 deletions(-)
include/linux/timer.h renamed from include/linux/timer.h~del_timer_sync-scalability-patch-tidy +10 -11
diff -puN include/linux/timer.h~del_timer_sync-scalability-patch-tidy include/linux/timer.h
@@ -26,12 +26,6 @@ struct timer_list {
 
 #define TIMER_MAGIC	0x4b87ad6e
 
-#ifdef CONFIG_SMP
-#define TIMER_INIT_LASTRUNNING .last_running = NULL,
-#else
-#define TIMER_INIT_LASTRUNNING
-#endif
-
 #define TIMER_INITIALIZER(_function, _expires, _data) {		\
 		.function = (_function),			\
 		.expires = (_expires),				\
@@ -39,9 +33,16 @@ struct timer_list {
 		.base = NULL,					\
 		.magic = TIMER_MAGIC,				\
 		.lock = SPIN_LOCK_UNLOCKED,			\
-		TIMER_INIT_LASTRUNNING                          \
 	}
 
+static inline void
+timer_set_last_running(struct timer_list *timer, struct tvec_t_base_s *base)
+{
+#ifdef CONFIG_SMP
+	timer->last_running = base;
+#endif
+}
+
 /***
  * init_timer - initialize a timer.
  * @timer: the timer to be initialized
@@ -49,11 +50,9 @@ struct timer_list {
  * init_timer() must be done to a timer prior calling *any* of the
  * other timer functions.
  */
-static inline void init_timer(struct timer_list * timer)
+static inline void init_timer(struct timer_list *timer)
 {
-#ifdef CONFIG_SMP
-	timer->last_running = NULL;
-#endif
+	timer_set_last_running(timer, NULL);
 	timer->base = NULL;
 	timer->magic = TIMER_MAGIC;
 	spin_lock_init(&timer->lock);
kernel/timer.c renamed from kernel/timer.c~del_timer_sync-scalability-patch-tidy +1 -9
diff -puN kernel/timer.c~del_timer_sync-scalability-patch-tidy kernel/timer.c
@@ -86,14 +86,6 @@ static inline void set_running_timer(tve
 #endif
 }
 
-static inline void set_last_running(struct timer_list *timer,
-					tvec_base_t *base)
-{
-#ifdef CONFIG_SMP
-	timer->last_running = base;
-#endif
-}
-
 /* Fake initialization */
 static DEFINE_PER_CPU(tvec_base_t, tvec_bases) = { SPIN_LOCK_UNLOCKED };
 
@@ -482,7 +474,7 @@ repeat:
 			set_running_timer(base, timer);
 			smp_wmb();
 			timer->base = NULL;
-			set_last_running(timer, base);
+			timer_set_last_running(timer, base);
 			spin_unlock_irq(&base->lock);
 			{
 				u32 preempt_count = preempt_count();

_

#2 lore raw
Quoted 10 lines · Andrew Morton
Quoted 9 lines · Christoph Lameter

When a potential periodic timer is deleted through timer_del_sync, all
cpus are scanned to determine if the timer is running on that cpu. In a NUMA configuration doing so will cause NUMA interlink traffic which limits the scalability of timers.

The following patch makes the timer remember where the timer was last started. It is then possible to only wait for the completion of the timer on that specific cpu.

i'm not sure about this. The patch adds one more pointer to a very frequently used and frequently embedded data structure (struct timer_list), for the benefit of a rarely used API variant (timer_del_sync()).

Furthermore, timer->base itself is affine too, a timer always runs on the CPU belonging to timer->base. So a more scalable variant of del_timer_sync() would perhaps be possible by carefully deleting the timer and only going into the full loop if the timer was deleted before. (and in which case semantics require us to synchronize on timer execution.) Or we could skip the full loop altogether and just synchronize with timer execution if _we_ deleted the timer. This should work fine as far as itimers are concerned.

Ingo

#3 lore raw
Quoted 11 lines · Ingo Molnar
Quoted 7 lines · Andrew Morton
Quoted 6 lines · Christoph Lameter

When a potential periodic timer is deleted through timer_del_sync, all
cpus are scanned to determine if the timer is running on that cpu. In a NUMA configuration doing so will cause NUMA interlink traffic which limits the scalability of timers.

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines

The following patch makes the timer remember where the timer was last started. It is then possible to only wait for the completion of the timer on that specific cpu.

i'm not sure about this. The patch adds one more pointer to a very frequently used and frequently embedded data structure (struct timer_list), for the benefit of a rarely used API variant (timer_del_sync()).

True.

(We can delete timer.magic and check_timer() now. It has served its purpose)

Quoted 8 lines

Furthermore, timer->base itself is affine too, a timer always runs on the CPU belonging to timer->base. So a more scalable variant of del_timer_sync() would perhaps be possible by carefully deleting the timer and only going into the full loop if the timer was deleted before. (and in which case semantics require us to synchronize on timer execution.) Or we could skip the full loop altogether and just synchronize with timer execution if _we_ deleted the timer. This should work fine as far as itimers are concerned.

If we're prepared to rule that a timer handler is not allowed to do add_timer_on() then a recurring timer is permanently pinned to a CPU, isn't it?

That should make things simpler?

#4 lore raw
Quoted 3 lines · Andrew Morton

If we're prepared to rule that a timer handler is not allowed to do add_timer_on() then a recurring timer is permanently pinned to a CPU, isn't it?

The process may be rescheduled to run on different processor. Then the add_timer() function (called from schedule_next_timer in kernel/posix-timers.c) will also add the timer to the new processor because it is called from the signal handling code. So I think that it is possible that a periodic timer will be scheduled on different processors.

#5 lore raw
In reply to Ingo Molnar
Quoted 8 lines · Ingo Molnar
Quoted 3 lines
Quoted 3 lines

The following patch makes the timer remember where the timer was last started. It is then possible to only wait for the completion of the timer on that specific cpu.

i'm not sure about this. The patch adds one more pointer to a very frequently used and frequently embedded data structure (struct timer_list), for the benefit of a rarely used API variant (timer_del_sync()).

Without that pointer there is no information in the timer_list struct as to where the timer is/was running since base is set to NULL when the timer function is executed.

Quoted 8 lines

Furthermore, timer->base itself is affine too, a timer always runs on the CPU belonging to timer->base. So a more scalable variant of del_timer_sync() would perhaps be possible by carefully deleting the timer and only going into the full loop if the timer was deleted before. (and in which case semantics require us to synchronize on timer execution.) Or we could skip the full loop altogether and just synchronize with timer execution if _we_ deleted the timer. This should work fine as far as itimers are concerned.

There is no easy way to distinguish the case that a timer was deleted from the case that the timer function is running (which may reschedule the periodic timer). A scan over all the bases is required to insure that the timer function is not being executed right now.

If we only would sync when we deleted the timer (meaning timer->base was != NULL) then we would not synchronize when the timer function is executing (and therefore timer->base == NULL). However, we need to sync exactly in that case because a periodic timer may set timer->base again when scheduling the next event. That is the main stated purpose of del_timer_sync.