1 /* LGPL (v2.1 or any later version) - see LICENSE file for details */
2 #include <ccan/timer/timer.h>
3 #include <ccan/array_size/array_size.h>
4 #include <ccan/ilog/ilog.h>
5 #include <ccan/likely/likely.h>
9 #define PER_LEVEL (1ULL << TIMER_LEVEL_BITS)
12 struct list_head list[PER_LEVEL];
15 static uint64_t time_to_grains(struct timespec ts)
17 return ts.tv_sec * ((uint64_t)1000000000 / TIMER_GRANULARITY)
18 + (ts.tv_nsec / TIMER_GRANULARITY);
21 static struct timespec grains_to_time(uint64_t grains)
25 ts.tv_sec = grains / (1000000000 / TIMER_GRANULARITY);
26 ts.tv_nsec = (grains % (1000000000 / TIMER_GRANULARITY))
31 void timers_init(struct timers *timers, struct timespec start)
35 list_head_init(&timers->far);
36 timers->base = time_to_grains(start);
37 timers->first = -1ULL;
38 for (i = 0; i < ARRAY_SIZE(timers->level); i++)
39 timers->level[i] = NULL;
42 static unsigned int level_of(const struct timers *timers, uint64_t time)
46 /* Level depends how far away it is. */
47 diff = time - timers->base;
48 return ilog64(diff / 2) / TIMER_LEVEL_BITS;
51 static void timer_add_raw(struct timers *timers, struct timer *t)
54 unsigned int level = level_of(timers, t->time);
56 if (!timers->level[level])
59 int off = (t->time >> (level*TIMER_LEVEL_BITS)) & (PER_LEVEL-1);
60 l = &timers->level[level]->list[off];
63 list_add_tail(l, &t->list);
66 void timer_add(struct timers *timers, struct timer *t, struct timespec when)
68 t->time = time_to_grains(when);
70 /* Added in the past? Treat it as imminent. */
71 if (t->time < timers->base)
72 t->time = timers->base;
73 if (t->time < timers->first)
74 timers->first = t->time;
76 timer_add_raw(timers, t);
80 void timer_del(struct timers *timers, struct timer *t)
85 static void timers_far_get(struct timers *timers,
86 struct list_head *list,
89 struct timer *i, *next;
91 list_for_each_safe(&timers->far, i, next, list) {
92 if (i->time <= when) {
93 list_del_from(&timers->far, &i->list);
94 list_add_tail(list, &i->list);
99 static void add_level(struct timers *timers, unsigned int level)
101 struct timer_level *l;
104 struct list_head from_far;
106 l = malloc(sizeof(*l));
110 for (i = 0; i < ARRAY_SIZE(l->list); i++)
111 list_head_init(&l->list[i]);
112 timers->level[level] = l;
114 list_head_init(&from_far);
115 timers_far_get(timers, &from_far,
116 timers->base + (1ULL << ((level+1)*TIMER_LEVEL_BITS)) - 1);
118 while ((t = list_pop(&from_far, struct timer, list)) != NULL)
119 timer_add_raw(timers, t);
122 static const struct timer *find_first(const struct list_head *list,
123 const struct timer *prev)
127 list_for_each(list, t, list) {
128 if (!prev || t->time < prev->time)
134 static const struct timer *get_first(const struct timers *timers)
136 unsigned int level, i, off;
139 const struct timer *found = NULL;
142 if (timers->first < timers->base) {
146 /* May not be accurate, due to timer_del / expiry. */
147 level = level_of(timers, timers->first);
148 base = timers->first >> (TIMER_LEVEL_BITS * level);
152 if (!timers->level[level])
153 return find_first(&timers->far, NULL);
156 off = base % PER_LEVEL;
157 for (i = 0; i < PER_LEVEL; i++) {
158 h = &timers->level[level]->list[(i+off) % PER_LEVEL];
163 /* We haven't cascaded yet, so if we wrap, we'll need to
164 * check next level, too. */
165 if (i + off == PER_LEVEL)
168 if (i == PER_LEVEL) {
170 base >>= TIMER_LEVEL_BITS;
174 /* Level 0 is exact, so they're all the same. */
176 found = list_top(h, struct timer, list);
178 found = find_first(h, NULL);
181 if (!timers->level[level+1]) {
182 found = find_first(&timers->far, found);
184 base >>= TIMER_LEVEL_BITS;
185 off = base % PER_LEVEL;
186 h = &timers->level[level+1]->list[off];
187 found = find_first(h, found);
193 static bool update_first(struct timers *timers)
195 const struct timer *found = get_first(timers);
198 timers->first = -1ULL;
202 timers->first = found->time;
206 bool timer_earliest(struct timers *timers, struct timespec *first)
208 if (!update_first(timers))
211 *first = grains_to_time(timers->first);
215 /* Assume no timers before 'time', cascade down and update base time. */
216 static void timer_fast_forward(struct timers *timers, uint64_t time)
218 unsigned int level, changed;
220 struct list_head list;
223 /* How many bits changed between base and time?
224 * Each time we wrap, we need to empty buckets from above. */
225 if (time == timers->base)
228 changed = ilog64_nz(time ^ timers->base);
229 level = (changed - 1) / TIMER_LEVEL_BITS;
231 /* Buckets always empty downwards, so we could cascade manually,
232 * but it's rarely very many so we just remove and re-add */
233 list_head_init(&list);
236 if (!timers->level[level]) {
237 /* We need any which belong on this level. */
238 timers_far_get(timers, &list,
240 + (1ULL << ((level+1)*TIMER_LEVEL_BITS))-1);
245 /* Get all timers from this bucket. */
246 src = (time >> (level * TIMER_LEVEL_BITS)) % PER_LEVEL;
247 list_append_list(&list,
248 &timers->level[level]->list[src]);
252 /* Did we hit the last level? If so, add. */
253 if (need_level != -1)
254 add_level(timers, need_level);
256 /* Fast-forward the time, and re-add everyone. */
258 while ((i = list_pop(&list, struct timer, list)) != NULL)
259 timer_add_raw(timers, i);
262 /* Fills list of expired timers. */
263 void timers_expire(struct timers *timers,
264 struct timespec expire,
265 struct list_head *list)
267 uint64_t now = time_to_grains(expire);
270 assert(now >= timers->base);
272 list_head_init(list);
274 if (!timers->level[0]) {
275 if (list_empty(&timers->far))
277 add_level(timers, 0);
281 if (timers->first > now) {
282 timer_fast_forward(timers, now);
286 timer_fast_forward(timers, timers->first);
287 off = timers->base % PER_LEVEL;
289 list_append_list(list, &timers->level[0]->list[off]);
290 if (timers->base == now)
292 } while (update_first(timers));
295 static bool timer_list_check(const struct list_head *l,
296 uint64_t min, uint64_t max, uint64_t first,
297 const char *abortstr)
299 const struct timer *t;
301 if (!list_check(l, abortstr))
304 list_for_each(l, t, list) {
305 if (t->time < min || t->time > max) {
308 "%s: timer %p %llu not %llu-%llu\n",
309 abortstr, t, t->time, min, max);
314 if (t->time < first) {
317 "%s: timer %p %llu < minimum %llu\n",
318 abortstr, t, t->time, first);
327 struct timers *timers_check(const struct timers *timers, const char *abortstr)
329 unsigned int l, i, off;
333 if (!timers->level[0])
336 /* First level is simple. */
337 off = timers->base % PER_LEVEL;
338 for (i = 0; i < PER_LEVEL; i++) {
341 h = &timers->level[l]->list[(i+off) % PER_LEVEL];
342 if (!timer_list_check(h, timers->base + i, timers->base + i,
343 timers->first, abortstr))
347 /* For other levels, "current" bucket has been emptied, and may contain
348 * entries for the current + level_size bucket. */
349 for (l = 1; timers->level[l] && l < PER_LEVEL; l++) {
350 uint64_t per_bucket = 1ULL << (TIMER_LEVEL_BITS * l);
352 off = ((timers->base >> (l*TIMER_LEVEL_BITS)) % PER_LEVEL);
353 /* We start at *next* bucket. */
354 base = (timers->base & ~(per_bucket - 1)) + per_bucket;
356 for (i = 1; i <= PER_LEVEL; i++) {
359 h = &timers->level[l]->list[(i+off) % PER_LEVEL];
360 if (!timer_list_check(h, base, base + per_bucket - 1,
361 timers->first, abortstr))
368 base = (timers->base & ~((1ULL << (TIMER_LEVEL_BITS * l)) - 1))
369 + (1ULL << (TIMER_LEVEL_BITS * l)) - 1;
370 if (!timer_list_check(&timers->far, base, -1ULL, timers->first,
374 return (struct timers *)timers;
377 #ifdef CCAN_TIMER_DEBUG
378 void timers_dump(const struct timers *timers, FILE *fp)
381 uint64_t min, max, num;
387 fprintf(fp, "Base: %llu\n", timers->base);
389 for (l = 0; timers->level[l] && l < ARRAY_SIZE(timers->level); l++) {
390 fprintf(fp, "Level %i (+%llu):\n",
391 l, (uint64_t)1 << (TIMER_LEVEL_BITS * l));
392 for (i = 0; i < (1 << TIMER_LEVEL_BITS); i++) {
394 if (list_empty(&timers->level[l]->list[i]))
399 list_for_each(&timers->level[l]->list[i], t, list) {
406 fprintf(stderr, " %llu (+%llu-+%llu)\n",
407 num, min - timers->base, max - timers->base);
414 list_for_each(&timers->far, t, list) {
421 fprintf(stderr, "Far: %llu (%llu-%llu)\n", num, min, max);
425 void timers_cleanup(struct timers *timers)
429 for (l = 0; l < ARRAY_SIZE(timers->level); l++)
430 free(timers->level[l]);