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 timeabs t)
17 return t.ts.tv_sec * ((uint64_t)1000000000 / TIMER_GRANULARITY)
18 + (t.ts.tv_nsec / TIMER_GRANULARITY);
21 static struct timeabs grains_to_time(uint64_t grains)
25 t.ts.tv_sec = grains / (1000000000 / TIMER_GRANULARITY);
26 t.ts.tv_nsec = (grains % (1000000000 / TIMER_GRANULARITY))
31 void timers_init(struct timers *timers, struct timeabs 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_init(struct timer *t)
68 list_node_init(&t->list);
71 static bool list_node_initted(const struct list_node *n)
76 void timer_add(struct timers *timers, struct timer *t, struct timeabs when)
78 assert(list_node_initted(&t->list));
80 t->time = time_to_grains(when);
82 /* Added in the past? Treat it as imminent. */
83 if (t->time < timers->base)
84 t->time = timers->base;
85 if (t->time < timers->first)
86 timers->first = t->time;
88 timer_add_raw(timers, t);
92 void timer_del(struct timers *timers, struct timer *t)
94 list_del_init(&t->list);
97 static void timers_far_get(struct timers *timers,
98 struct list_head *list,
101 struct timer *i, *next;
103 list_for_each_safe(&timers->far, i, next, list) {
104 if (i->time <= when) {
105 list_del_from(&timers->far, &i->list);
106 list_add_tail(list, &i->list);
111 static void add_level(struct timers *timers, unsigned int level)
113 struct timer_level *l;
116 struct list_head from_far;
118 l = malloc(sizeof(*l));
122 for (i = 0; i < ARRAY_SIZE(l->list); i++)
123 list_head_init(&l->list[i]);
124 timers->level[level] = l;
126 list_head_init(&from_far);
127 timers_far_get(timers, &from_far,
128 timers->base + (1ULL << ((level+1)*TIMER_LEVEL_BITS)) - 1);
130 while ((t = list_pop(&from_far, struct timer, list)) != NULL)
131 timer_add_raw(timers, t);
134 static const struct timer *find_first(const struct list_head *list,
135 const struct timer *prev)
139 list_for_each(list, t, list) {
140 if (!prev || t->time < prev->time)
146 static const struct timer *get_first(const struct timers *timers)
148 unsigned int level, i, off;
151 const struct timer *found = NULL;
154 if (timers->first < timers->base) {
158 /* May not be accurate, due to timer_del / expiry. */
159 level = level_of(timers, timers->first);
160 base = timers->first >> (TIMER_LEVEL_BITS * level);
164 if (!timers->level[level])
165 return find_first(&timers->far, NULL);
168 off = base % PER_LEVEL;
169 for (i = 0; i < PER_LEVEL; i++) {
170 h = &timers->level[level]->list[(i+off) % PER_LEVEL];
175 /* We haven't cascaded yet, so if we wrap, we'll need to
176 * check next level, too. */
177 if (i + off == PER_LEVEL)
180 if (i == PER_LEVEL) {
182 base >>= TIMER_LEVEL_BITS;
186 /* Level 0 is exact, so they're all the same. */
188 found = list_top(h, struct timer, list);
190 found = find_first(h, NULL);
193 if (!timers->level[level+1]) {
194 found = find_first(&timers->far, found);
196 base >>= TIMER_LEVEL_BITS;
197 off = base % PER_LEVEL;
198 h = &timers->level[level+1]->list[off];
199 found = find_first(h, found);
205 static bool update_first(struct timers *timers)
207 const struct timer *found = get_first(timers);
210 timers->first = -1ULL;
214 timers->first = found->time;
218 bool timer_earliest(struct timers *timers, struct timeabs *first)
220 if (!update_first(timers))
223 *first = grains_to_time(timers->first);
227 /* Assume no timers before 'time', cascade down and update base time. */
228 static void timer_fast_forward(struct timers *timers, uint64_t time)
230 unsigned int level, changed;
232 struct list_head list;
235 /* How many bits changed between base and time?
236 * Each time we wrap, we need to empty buckets from above. */
237 if (time == timers->base)
240 changed = ilog64_nz(time ^ timers->base);
241 level = (changed - 1) / TIMER_LEVEL_BITS;
243 /* Buckets always empty downwards, so we could cascade manually,
244 * but it's rarely very many so we just remove and re-add */
245 list_head_init(&list);
248 if (!timers->level[level]) {
249 /* We need any which belong on this level. */
250 timers_far_get(timers, &list,
252 + (1ULL << ((level+1)*TIMER_LEVEL_BITS))-1);
257 /* Get all timers from this bucket. */
258 src = (time >> (level * TIMER_LEVEL_BITS)) % PER_LEVEL;
259 list_append_list(&list,
260 &timers->level[level]->list[src]);
264 /* Did we hit the last level? If so, add. */
265 if (need_level != -1)
266 add_level(timers, need_level);
268 /* Fast-forward the time, and re-add everyone. */
270 while ((i = list_pop(&list, struct timer, list)) != NULL)
271 timer_add_raw(timers, i);
274 /* Returns an expired timer. */
275 struct timer *timers_expire(struct timers *timers, struct timeabs expire)
277 uint64_t now = time_to_grains(expire);
281 assert(now >= timers->base);
283 if (!timers->level[0]) {
284 if (list_empty(&timers->far))
286 add_level(timers, 0);
290 if (timers->first > now) {
291 timer_fast_forward(timers, now);
295 timer_fast_forward(timers, timers->first);
296 off = timers->base % PER_LEVEL;
298 /* This *may* be NULL, if we deleted the first timer */
299 t = list_pop(&timers->level[0]->list[off], struct timer, list);
301 list_node_init(&t->list);
302 } while (!t && update_first(timers));
307 static bool timer_list_check(const struct list_head *l,
308 uint64_t min, uint64_t max, uint64_t first,
309 const char *abortstr)
311 const struct timer *t;
313 if (!list_check(l, abortstr))
316 list_for_each(l, t, list) {
317 if (t->time < min || t->time > max) {
320 "%s: timer %p %llu not %llu-%llu\n",
321 abortstr, t, (long long)t->time,
322 (long long)min, (long long)max);
327 if (t->time < first) {
330 "%s: timer %p %llu < minimum %llu\n",
331 abortstr, t, (long long)t->time,
341 struct timers *timers_check(const struct timers *timers, const char *abortstr)
343 unsigned int l, i, off;
347 if (!timers->level[0])
350 /* First level is simple. */
351 off = timers->base % PER_LEVEL;
352 for (i = 0; i < PER_LEVEL; i++) {
355 h = &timers->level[l]->list[(i+off) % PER_LEVEL];
356 if (!timer_list_check(h, timers->base + i, timers->base + i,
357 timers->first, abortstr))
361 /* For other levels, "current" bucket has been emptied, and may contain
362 * entries for the current + level_size bucket. */
363 for (l = 1; timers->level[l] && l < PER_LEVEL; l++) {
364 uint64_t per_bucket = 1ULL << (TIMER_LEVEL_BITS * l);
366 off = ((timers->base >> (l*TIMER_LEVEL_BITS)) % PER_LEVEL);
367 /* We start at *next* bucket. */
368 base = (timers->base & ~(per_bucket - 1)) + per_bucket;
370 for (i = 1; i <= PER_LEVEL; i++) {
373 h = &timers->level[l]->list[(i+off) % PER_LEVEL];
374 if (!timer_list_check(h, base, base + per_bucket - 1,
375 timers->first, abortstr))
382 base = (timers->base & ~((1ULL << (TIMER_LEVEL_BITS * l)) - 1))
383 + (1ULL << (TIMER_LEVEL_BITS * l)) - 1;
384 if (!timer_list_check(&timers->far, base, -1ULL, timers->first,
388 return (struct timers *)timers;
391 #ifdef CCAN_TIMER_DEBUG
392 void timers_dump(const struct timers *timers, FILE *fp)
395 uint64_t min, max, num;
401 fprintf(fp, "Base: %llu\n", timers->base);
403 for (l = 0; timers->level[l] && l < ARRAY_SIZE(timers->level); l++) {
404 fprintf(fp, "Level %i (+%llu):\n",
405 l, (uint64_t)1 << (TIMER_LEVEL_BITS * l));
406 for (i = 0; i < (1 << TIMER_LEVEL_BITS); i++) {
408 if (list_empty(&timers->level[l]->list[i]))
413 list_for_each(&timers->level[l]->list[i], t, list) {
420 fprintf(stderr, " %llu (+%llu-+%llu)\n",
421 num, min - timers->base, max - timers->base);
428 list_for_each(&timers->far, t, list) {
435 fprintf(stderr, "Far: %llu (%llu-%llu)\n", num, min, max);
439 void timers_cleanup(struct timers *timers)
443 for (l = 0; l < ARRAY_SIZE(timers->level); l++)
444 free(timers->level[l]);