Timing wheels

1,448 views
1,167 views

Published on

时间轮算法

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,448
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
10
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Timing wheels

  1. 1. Hashed and Hierarchical Timing Wheels A paper by George Varghese and Tony Lauck
  2. 2. Motivation <ul><li>Timers are important for </li></ul><ul><ul><li>Failure recovery, rate based flow control, scheduling algorithms, controlling packet lifetime in networks </li></ul></ul><ul><li>Timer maintenance high if </li></ul><ul><ul><li>Processor interrupted every clock tick </li></ul></ul><ul><ul><li>Fine granularity timers are used </li></ul></ul><ul><ul><li># outstanding timers is high </li></ul></ul><ul><li>Efficient timer algorithms are required to reduce the overall interrupt overhead </li></ul>
  3. 3. Model & Performance Measure <ul><li>Routines in the model </li></ul><ul><ul><li>Client Invoked : </li></ul></ul><ul><ul><ul><li>START_TIMER(Interval, Request_ID, Expiry_Action) </li></ul></ul></ul><ul><ul><ul><li>STOP_TIMER(Request_ID) </li></ul></ul></ul><ul><ul><li>Timer tick invoked : </li></ul></ul><ul><ul><ul><li>PER_TICK_BOOKKEEPING </li></ul></ul></ul><ul><ul><ul><li>EXPIRY_PROCESSING </li></ul></ul></ul><ul><li>Performance Measure </li></ul><ul><ul><li>Space : Memory used by the data structures </li></ul></ul><ul><ul><li>Latency : Time required to begin and end any of the routines mentioned above </li></ul></ul>
  4. 4. Currently Used Timer Schemes a b c d e Can maintain absolute expiry time or the timer interval START_TIMER = O(1) STOP_TIMER = O(1) PER_TICK_BOOKKEEPING = O(n) a b c d e f maintain absolute expiry time START_TIMER = O(n) STOP_TIMER = O(1) PER_TICK_BOOKKEEPING = O(1)
  5. 5. Tree based timers a b c d a b c d maintain absolute expiry time START_TIMER = O(log(n)) STOP_TIMER = O(1) PER_TICK_BOOKKEEPING = O(1) Can degenerate to a linear list in case of equal Interval timers START_TIMER = O(n) STOP_TIMER = O(1) PER_TICK_BOOKKEEPING = O(1)
  6. 6. Simple Timing Wheel <ul><li>Keep a large timing wheel </li></ul><ul><li>A curser in the timing wheel moves one location every time unit (just like a seconds hand in the clock) </li></ul><ul><li>If the timer interval is within a rotation from the current curser position then put the timer in the corresponding location </li></ul><ul><li>Requires exponential amount of memory </li></ul>START_TIMER = O(1) STOP_TIMER = O(1) PER_TICK_BOOKKEEPING = O(1) 1 2 3 4 5 6 7 0
  7. 7. Hashed Timing Wheel <ul><li>Say wheel has 8 ticks </li></ul><ul><li>Timer value = 17 </li></ul><ul><li>Make 2 rounds of wheel + 1 more tick </li></ul><ul><li>Schedule the timer in the bucket “1” </li></ul><ul><li>Keep the # rounds with the timer </li></ul><ul><li>At the expiry processing if the # rounds > 0 then reinsert the timer </li></ul>1 2 3 4 5 6 7 0 2 4 1 2 2 1 1 2 # of rounds remaining
  8. 8. Hashed Timing Wheel <ul><li>Sorted Lists in each bucket </li></ul><ul><ul><li>The list in each bucket can be insertion sorted </li></ul></ul><ul><ul><li>Hence START_TIMER takes O(n) time in the worst case </li></ul></ul><ul><ul><li>If n < WheelSize then average O(1) </li></ul></ul><ul><li>Unsorted list in each bucket </li></ul><ul><ul><li>List can be kept unsorted to avoid worst case O(n) latency for START_TIMER </li></ul></ul><ul><ul><li>However worst case PER_TICK_BOOKKEEPING = O(n) </li></ul></ul><ul><ul><li>Again, if n < WheelSize then average O(1) </li></ul></ul>
  9. 9. Hierarchical Timing Wheel 1 2 3 4 5 6 7 0 3 5 7 5 2 1 1 Hours wheel 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 Minutes wheel Seconds wheel 1 3 3 5 3 7 1 5
  10. 10. Hierarchical Timing Wheels <ul><li>START_TIMER = O(m) where m is the number of wheels </li></ul><ul><ul><li>The bucket value on each wheel needs to be calculated </li></ul></ul><ul><li>STOP_TIMER = O(1) </li></ul><ul><li>PER_TICK_BOOKKEEPING = O(1) on avg. </li></ul>
  11. 11. Comparison START_TIMER STOP_TIMER PER_TICK O(1) Straight Fwd O(1) O(n) Sequential List O(n) O(1) O(1) Tree Based O(log(n)) O(1) O(1) Simple Wheel O(1) O(1) O(1) High memory requirement Hashed Wheel (sorted) Hashed Wheel (unsorted) Hierarchical Wheels O(n) worst case O(1) avg O(1) O(m) O(1) O(1) O(1) O(1) O(n) worst case O(1) avg O(1)

×