]>
Commit | Line | Data |
---|---|---|
cbea8737 DS |
1 | /* |
2 | * Timer Wheel | |
3 | * Copyright (C) 2016 Cumulus Networks, Inc. | |
4 | * Donald Sharp | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
896014f4 DL |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
cbea8737 DS |
19 | */ |
20 | #ifndef __WHEEL_H__ | |
21 | #define __WHEEL_H__ | |
22 | ||
d62a17ae | 23 | struct timer_wheel { |
24 | struct thread_master *master; | |
25 | int slots; | |
26 | long long curr_slot; | |
27 | unsigned int period; | |
28 | unsigned int nexttime; | |
29 | unsigned int slots_to_skip; | |
cbea8737 | 30 | |
d62a17ae | 31 | struct list **wheel_slot_lists; |
32 | struct thread *timer; | |
33 | /* | |
34 | * Key to determine what slot the item belongs in | |
35 | */ | |
36 | unsigned int (*slot_key)(void *); | |
cbea8737 | 37 | |
d62a17ae | 38 | void (*slot_run)(void *); |
cbea8737 DS |
39 | }; |
40 | ||
e2064401 DS |
41 | /* |
42 | * Creates a timer wheel | |
43 | * | |
44 | * master - Thread master structure for the process | |
45 | * period - The Time in seconds that the timer wheel will | |
46 | * take before it starts issuing commands again | |
47 | * for items in each slot | |
48 | * slots - The number of slots to have in this particular | |
49 | * timer wheel | |
50 | * slot_key - A hashing function of some sort that will allow | |
51 | * the timer wheel to put items into individual slots | |
52 | * slot_run - The function to run over each item in a particular slot | |
53 | * | |
54 | * Creates a timer wheel that will wake up 'slots' times over the entire | |
55 | * wheel. Each time the timer wheel wakes up it will iterate through | |
56 | * and run the slot_run function for each item stored in that particular | |
57 | * slot. | |
58 | * | |
59 | * The timer code is 'intelligent' in that it notices if anything is | |
60 | * in a particular slot and can schedule the next timer to skip | |
61 | * the empty slot. | |
62 | * | |
63 | * The general purpose of a timer wheel is to reduce events in a system. | |
64 | * A perfect example of usage for this is say hello packets that need | |
65 | * to be sent out to all your neighbors. Suppose a large routing protocol | |
66 | * has to send keepalive packets every Y seconds to each of it's peers. | |
67 | * At scale we can have a very large number of peers, X. | |
68 | * This means that we will have X timing events every Y seconds. | |
69 | * If you replace these events with a timer wheel that has Z slots | |
70 | * you will have at most Y/Z timer events if each slot has a work item | |
71 | * in it. | |
72 | * | |
73 | * When X is large the number of events in a system can quickly escalate | |
74 | * and cause significant amount of time handling thread events instead | |
75 | * of running your code. | |
76 | */ | |
d62a17ae | 77 | struct timer_wheel *wheel_init(struct thread_master *master, int period, |
78 | size_t slots, unsigned int (*slot_key)(void *), | |
79 | void (*slot_run)(void *)); | |
e2064401 DS |
80 | |
81 | /* | |
82 | * Delete the specified timer wheel created | |
83 | */ | |
d62a17ae | 84 | void wheel_delete(struct timer_wheel *); |
cbea8737 DS |
85 | |
86 | /* | |
87 | * Pause the Wheel from running | |
88 | */ | |
d62a17ae | 89 | int wheel_stop(struct timer_wheel *wheel); |
cbea8737 DS |
90 | |
91 | /* | |
e2064401 | 92 | * Start the wheel running again |
cbea8737 | 93 | */ |
d62a17ae | 94 | int wheel_start(struct timer_wheel *wheel); |
cbea8737 DS |
95 | |
96 | /* | |
e2064401 DS |
97 | * wheel - The Timer wheel being modified |
98 | * item - The generic data structure that will be handed | |
99 | * to the slot_run function. | |
100 | * | |
cbea8737 DS |
101 | * Add item to a slot setup by the slot_key, |
102 | * possibly change next time pop. | |
103 | */ | |
d62a17ae | 104 | int wheel_add_item(struct timer_wheel *wheel, void *item); |
cbea8737 DS |
105 | |
106 | /* | |
e2064401 DS |
107 | * wheel - The Timer wheel being modified. |
108 | * item - The item to remove from one of the slots in | |
109 | * the timer wheel. | |
110 | * | |
cbea8737 DS |
111 | * Remove a item to a slot setup by the slot_key, |
112 | * possibly change next time pop. | |
113 | */ | |
d62a17ae | 114 | int wheel_remove_item(struct timer_wheel *wheel, void *item); |
cbea8737 DS |
115 | |
116 | #endif |