Skip to contentSkip to conversation

timer: Proof of concept wheel replacement

RFD Thomas Gleixner 1 participant 1 message May 26, 2015

Subsystems (3)
lore
Share & tools
lore
#0 lore raw
Quoted 50 lines · Thomas Gleixner

So we need to address two issues:

1) Keeping track of the first expiring timer in the wheel.

Don't even think about rbtrees or such, it just wont work, but I'm willing to accept prove of the contrary.

One of the ideas I had a long time ago is to have a wheel implementation, which does never cascade and therefor provides different levels of granularity per wheel level.

   LVL0	   1  jiffy granularity
   LVL1	   8  jiffies granularity
   LVL1	   64 jiffies granularity

....

This requires more levels than the classic timer wheel, so its not feasible from a memory consumption POV.

But we can have a compromise and put all 'soon' expiring timers into these fancy new wheels and stick all 'far out' timers into the last level of the wheel and cascade them occasionally.

That should work because:

- Most timers are short term expiry (< 1h)
- Most timers are canceled before expiry

So we need a sensible upper bound of levels and get the following properties:

- Natural batching (no more slack magic). This might also help networking to avoid rearming of timers.

- Long out timers are queued in the slowest wheel and ocassionally with the granularity of the slowest wheel cascaded

- Tracking the next expiring timer can be done with a bitmap, so the 'get next expiring timer' becomes O(1) without touching anything else than the bitmap, if we accept that the upper limit of what we can retrieve O(1) is the granularity of the last level, i.e. we treat timers which need recascading simple as normal inhabitants of the last wheel level.

- The expiry code (run_timers) gets more complicated as it needs to walk through the different levels more often, but with the bitmap that shouldnt be a too big issue.

Quoted 5 lines

- Seperate storage for non-deferrable and deferrable timers.

I spent some time in coding that up. It barely boots and has certainly a few bugs left and right, but I will send it out nevertheless as a reply to this mail to get the discussion going.

Here is the proof of concept implementation for the above.

Be aware that it might eat your disk, kill you cat and make your kids miss the bus, but with some care one it should be able to tame the beast and get some real testing done.

I have no idea how that behaves performance wise as I was trying to shakeout the bugs.

---
 include/linux/list.h         |   10 
 include/linux/timer.h        |    2 
 include/trace/events/timer.h |   11 
 kernel/time/timer.c          |  657 ++++++++++++++++++++++---------------------
 4 files changed, 369 insertions(+), 311 deletions(-)
include/linux/list.h +10 -0
Index: tip/include/linux/list.h
===================================================================
@@ -673,6 +673,16 @@ static inline void hlist_add_fake(struct
 }
 
 /*
+ * Check whether the node is the last node of the head without
+ * accessing head.
+ */
+static inline bool hlist_is_last_node(struct hlist_node *n,
+				      struct hlist_head *h)
+{
+	return !n->next && n->pprev == &h->first;
+}
+
+/*
  * Move a list from one list head to another. Fixup the pprev
  * reference of the first entry if it exists.
  */
include/linux/timer.h +2 -0
Index: tip/include/linux/timer.h
===================================================================
@@ -63,6 +63,8 @@ struct timer_list {
 #define TIMER_BASEMASK		(TIMER_CPUMASK | TIMER_MIGRATING)
 #define TIMER_DEFERRABLE	0x00100000
 #define TIMER_IRQSAFE		0x00200000
+#define TIMER_HASHSHIFT		23
+#define TIMER_HASHMASK		0xFF800000
 
 #define __TIMER_INITIALIZER(_function, _expires, _data, _flags) { \
 		.entry = { .next = TIMER_ENTRY_STATIC },	\
include/trace/events/timer.h +11 -0
Index: tip/include/trace/events/timer.h
===================================================================
@@ -126,6 +126,17 @@ DEFINE_EVENT(timer_class, timer_cancel,
 );
 
 /**
+ * timer_cascade - called when the timer is cascaded
+ * @timer:	pointer to struct timer_list
+ */
+DEFINE_EVENT(timer_class, timer_cascade,
+
+	TP_PROTO(struct timer_list *timer),
+
+	TP_ARGS(timer)
+);
+
+/**
  * hrtimer_init - called when the hrtimer is initialized
  * @hrtimer:	pointer to struct hrtimer
  * @clockid:	the hrtimers clock
kernel/time/timer.c +346 -311

798 lines not on this page · raw message