tokio_util/time/wheel/
level.rs

1use crate::time::wheel::Stack;
2
3use std::fmt;
4
5/// Wheel for a single level in the timer. This wheel contains 64 slots.
6pub(crate) struct Level<T> {
7    level: usize,
8
9    /// Bit field tracking which slots currently contain entries.
10    ///
11    /// Using a bit field to track slots that contain entries allows avoiding a
12    /// scan to find entries. This field is updated when entries are added or
13    /// removed from a slot.
14    ///
15    /// The least-significant bit represents slot zero.
16    occupied: u64,
17
18    /// Slots
19    slot: [T; LEVEL_MULT],
20}
21
22/// Indicates when a slot must be processed next.
23#[derive(Debug)]
24pub(crate) struct Expiration {
25    /// The level containing the slot.
26    pub(crate) level: usize,
27
28    /// The slot index.
29    pub(crate) slot: usize,
30
31    /// The instant at which the slot needs to be processed.
32    pub(crate) deadline: u64,
33}
34
35/// Level multiplier.
36///
37/// Being a power of 2 is very important.
38const LEVEL_MULT: usize = 64;
39
40impl<T: Stack> Level<T> {
41    pub(crate) fn new(level: usize) -> Level<T> {
42        Level {
43            level,
44            occupied: 0,
45            slot: std::array::from_fn(|_| T::default()),
46        }
47    }
48
49    /// Finds the slot that needs to be processed next and returns the slot and
50    /// `Instant` at which this slot must be processed.
51    pub(crate) fn next_expiration(&self, now: u64) -> Option<Expiration> {
52        // Use the `occupied` bit field to get the index of the next slot that
53        // needs to be processed.
54        let slot = self.next_occupied_slot(now)?;
55
56        // From the slot index, calculate the `Instant` at which it needs to be
57        // processed. This value *must* be in the future with respect to `now`.
58
59        let level_range = level_range(self.level);
60        let slot_range = slot_range(self.level);
61
62        // TODO: This can probably be simplified w/ power of 2 math
63        let level_start = now - (now % level_range);
64        let mut deadline = level_start + slot as u64 * slot_range;
65        if deadline < now {
66            // A timer is in a slot "prior" to the current time. This can occur
67            // because we do not have an infinite hierarchy of timer levels, and
68            // eventually a timer scheduled for a very distant time might end up
69            // being placed in a slot that is beyond the end of all of the
70            // arrays.
71            //
72            // To deal with this, we first limit timers to being scheduled no
73            // more than MAX_DURATION ticks in the future; that is, they're at
74            // most one rotation of the top level away. Then, we force timers
75            // that logically would go into the top+1 level, to instead go into
76            // the top level's slots.
77            //
78            // What this means is that the top level's slots act as a
79            // pseudo-ring buffer, and we rotate around them indefinitely. If we
80            // compute a deadline before now, and it's the top level, it
81            // therefore means we're actually looking at a slot in the future.
82            debug_assert_eq!(self.level, super::NUM_LEVELS - 1);
83
84            deadline += level_range;
85        }
86        debug_assert!(
87            deadline >= now,
88            "deadline={:016X}; now={:016X}; level={}; slot={}; occupied={:b}",
89            deadline,
90            now,
91            self.level,
92            slot,
93            self.occupied
94        );
95
96        Some(Expiration {
97            level: self.level,
98            slot,
99            deadline,
100        })
101    }
102
103    fn next_occupied_slot(&self, now: u64) -> Option<usize> {
104        if self.occupied == 0 {
105            return None;
106        }
107
108        // Get the slot for now using Maths
109        let now_slot = (now / slot_range(self.level)) as usize;
110        let occupied = self.occupied.rotate_right(now_slot as u32);
111        let zeros = occupied.trailing_zeros() as usize;
112        let slot = (zeros + now_slot) % 64;
113
114        Some(slot)
115    }
116
117    pub(crate) fn add_entry(&mut self, when: u64, item: T::Owned, store: &mut T::Store) {
118        let slot = slot_for(when, self.level);
119
120        self.slot[slot].push(item, store);
121        self.occupied |= occupied_bit(slot);
122    }
123
124    pub(crate) fn remove_entry(&mut self, when: u64, item: &T::Borrowed, store: &mut T::Store) {
125        let slot = slot_for(when, self.level);
126
127        self.slot[slot].remove(item, store);
128
129        if self.slot[slot].is_empty() {
130            // The bit is currently set
131            debug_assert!(self.occupied & occupied_bit(slot) != 0);
132
133            // Unset the bit
134            self.occupied ^= occupied_bit(slot);
135        }
136    }
137
138    pub(crate) fn pop_entry_slot(&mut self, slot: usize, store: &mut T::Store) -> Option<T::Owned> {
139        let ret = self.slot[slot].pop(store);
140
141        if ret.is_some() && self.slot[slot].is_empty() {
142            // The bit is currently set
143            debug_assert!(self.occupied & occupied_bit(slot) != 0);
144
145            self.occupied ^= occupied_bit(slot);
146        }
147
148        ret
149    }
150
151    pub(crate) fn peek_entry_slot(&self, slot: usize) -> Option<T::Owned> {
152        self.slot[slot].peek()
153    }
154}
155
156impl<T> fmt::Debug for Level<T> {
157    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
158        fmt.debug_struct("Level")
159            .field("occupied", &self.occupied)
160            .finish()
161    }
162}
163
164fn occupied_bit(slot: usize) -> u64 {
165    1 << slot
166}
167
168fn slot_range(level: usize) -> u64 {
169    LEVEL_MULT.pow(level as u32) as u64
170}
171
172fn level_range(level: usize) -> u64 {
173    LEVEL_MULT as u64 * slot_range(level)
174}
175
176/// Convert a duration (milliseconds) and a level to a slot position
177fn slot_for(duration: u64, level: usize) -> usize {
178    ((duration >> (level * 6)) % LEVEL_MULT as u64) as usize
179}
180
181#[cfg(all(test, not(loom)))]
182mod test {
183    use super::*;
184
185    #[test]
186    fn test_slot_for() {
187        for pos in 0..64 {
188            assert_eq!(pos as usize, slot_for(pos, 0));
189        }
190
191        for level in 1..5 {
192            for pos in level..64 {
193                let a = pos * 64_usize.pow(level as u32);
194                assert_eq!(pos, slot_for(a as u64, level));
195            }
196        }
197    }
198}