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 timemono t)
17 return t.ts.tv_sec * ((uint64_t)1000000000 / TIMER_GRANULARITY)
18 + (t.ts.tv_nsec / TIMER_GRANULARITY);
21 static struct timemono 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 timemono start)
35 list_head_init(&timers->far);
36 timers->base = time_to_grains(start);
37 timers->first = -1ULL;
38 memset(timers->firsts, 0xFF, sizeof(timers->firsts));
39 for (i = 0; i < ARRAY_SIZE(timers->level); i++)
40 timers->level[i] = NULL;
43 static unsigned int level_of(const struct timers *timers, uint64_t time)
47 /* Level depends how far away it is. */
48 diff = time - timers->base;
49 return ilog64(diff / 2) / TIMER_LEVEL_BITS;
52 static void timer_add_raw(struct timers *timers, struct timer *t)
55 unsigned int level = level_of(timers, t->time);
58 if (!timers->level[level]) {
60 first = &timers->firsts[ARRAY_SIZE(timers->level)];
62 int off = (t->time >> (level*TIMER_LEVEL_BITS)) & (PER_LEVEL-1);
63 l = &timers->level[level]->list[off];
64 first = &timers->firsts[level];
67 list_add_tail(l, &t->list);
72 void timer_init(struct timer *t)
74 list_node_init(&t->list);
77 static bool list_node_initted(const struct list_node *n)
82 void timer_addrel(struct timers *timers, struct timer *t, struct timerel rel)
84 assert(list_node_initted(&t->list));
86 t->time = time_to_grains(timemono_add(time_mono(), rel));
88 #if TIME_HAVE_MONOTONIC
89 assert(t->time >= timers->base);
91 /* Added in the past? Treat it as imminent. */
92 if (t->time < timers->base)
93 t->time = timers->base;
95 if (t->time < timers->first)
96 timers->first = t->time;
98 timer_add_raw(timers, t);
101 void timer_addmono(struct timers *timers, struct timer *t, struct timemono when)
103 assert(list_node_initted(&t->list));
105 t->time = time_to_grains(when);
107 /* Added in the past? Treat it as imminent. */
108 if (t->time < timers->base)
109 t->time = timers->base;
110 if (t->time < timers->first)
111 timers->first = t->time;
113 timer_add_raw(timers, t);
117 void timer_del(struct timers *timers UNNEEDED, struct timer *t)
119 list_del_init(&t->list);
122 static void timers_far_get(struct timers *timers,
123 struct list_head *list,
126 struct timer *i, *next;
128 list_for_each_safe(&timers->far, i, next, list) {
129 if (i->time <= when) {
130 list_del_from(&timers->far, &i->list);
131 list_add_tail(list, &i->list);
136 static void add_level(struct timers *timers, unsigned int level)
138 struct timer_level *l;
141 struct list_head from_far;
143 l = malloc(sizeof(*l));
147 for (i = 0; i < ARRAY_SIZE(l->list); i++)
148 list_head_init(&l->list[i]);
149 timers->level[level] = l;
151 list_head_init(&from_far);
152 timers_far_get(timers, &from_far,
153 timers->base + (1ULL << ((level+1)*TIMER_LEVEL_BITS)) - 1);
155 while ((t = list_pop(&from_far, struct timer, list)) != NULL)
156 timer_add_raw(timers, t);
159 /* We don't need to search past the first at level 0, since the
160 * bucket range is 1; they're all the same. */
161 static const struct timer *find_first(const struct list_head *list,
163 const struct timer *prev)
167 list_for_each(list, t, list) {
168 if (!prev || t->time < prev->time)
176 /* Update level's first watermark, and return overall first. */
177 static const struct timer *first_for_level(struct timers *timers,
179 const struct timer *level_first,
180 const struct timer *first)
183 timers->firsts[level] = level_first->time;
184 if (!first || level_first->time < first->time)
187 timers->firsts[level] = -1ULL;
192 static bool level_may_beat(const struct timers *timers, size_t level,
193 const struct timer *first)
195 return !first || timers->firsts[level] < first->time;
198 /* FIXME: Suboptimal */
199 static const struct timer *brute_force_first(struct timers *timers)
202 const struct timer *found = NULL;
204 for (l = 0; l < ARRAY_SIZE(timers->level) && timers->level[l]; l++) {
205 const struct timer *t = NULL;
207 /* Do we know they don't have a better one? */
208 if (!level_may_beat(timers, l, found))
211 /* Find first timer on this level. */
212 for (i = 0; i < PER_LEVEL; i++)
213 t = find_first(&timers->level[l]->list[i], l, t);
215 found = first_for_level(timers, l, t, found);
218 /* Check (and update) far list if there's a chance. */
219 l = ARRAY_SIZE(timers->level);
220 if (level_may_beat(timers, l, found)) {
221 const struct timer *t = find_first(&timers->far, l, NULL);
222 found = first_for_level(timers, l, t, found);
228 static const struct timer *get_first(struct timers *timers)
230 /* We can have just far timers, for example. */
231 if (timers->level[0]) {
232 /* First search rest of lower buckets; we've already spilled
233 * so if we find one there we don't need to search further. */
234 unsigned int i, off = timers->base % PER_LEVEL;
236 for (i = off; i < PER_LEVEL; i++) {
237 struct list_head *h = &timers->level[0]->list[i];
239 return find_first(h, 0, NULL);
243 /* From here on, we're searching non-normalized parts of the
244 * data structure, which is much subtler.
246 * So we brute force. */
247 return brute_force_first(timers);
250 static bool update_first(struct timers *timers)
252 const struct timer *found = get_first(timers);
255 timers->first = -1ULL;
259 timers->first = found->time;
263 bool timer_earliest(struct timers *timers, struct timemono *first)
265 if (!update_first(timers))
268 *first = grains_to_time(timers->first);
272 /* Assume no timers before 'time', cascade down and update base time. */
273 static void timer_fast_forward(struct timers *timers, uint64_t time)
275 unsigned int level, changed;
277 struct list_head list;
280 /* How many bits changed between base and time?
281 * Each time we wrap, we need to empty buckets from above. */
282 if (time == timers->base)
285 changed = ilog64_nz(time ^ timers->base);
286 level = (changed - 1) / TIMER_LEVEL_BITS;
288 /* Buckets always empty downwards, so we could cascade manually,
289 * but it's rarely very many so we just remove and re-add */
290 list_head_init(&list);
293 if (!timers->level[level]) {
294 /* We need any which belong on this level. */
295 timers_far_get(timers, &list,
297 + (1ULL << ((level+1)*TIMER_LEVEL_BITS))-1);
302 /* Get all timers from this bucket. */
303 src = (time >> (level * TIMER_LEVEL_BITS)) % PER_LEVEL;
304 list_append_list(&list,
305 &timers->level[level]->list[src]);
309 /* Did we hit the last level? If so, add. */
310 if (need_level != -1)
311 add_level(timers, need_level);
313 /* Fast-forward the time, and re-add everyone. */
315 while ((i = list_pop(&list, struct timer, list)) != NULL)
316 timer_add_raw(timers, i);
319 /* Returns an expired timer. */
320 struct timer *timers_expire(struct timers *timers, struct timemono expire)
322 uint64_t now = time_to_grains(expire);
326 assert(now >= timers->base);
328 if (!timers->level[0]) {
329 if (list_empty(&timers->far))
331 add_level(timers, 0);
335 if (timers->first > now) {
336 timer_fast_forward(timers, now);
340 timer_fast_forward(timers, timers->first);
341 off = timers->base % PER_LEVEL;
343 /* This *may* be NULL, if we deleted the first timer */
344 t = list_pop(&timers->level[0]->list[off], struct timer, list);
346 list_node_init(&t->list);
347 } while (!t && update_first(timers));
352 static bool timer_list_check(const struct list_head *l,
353 uint64_t min, uint64_t max, uint64_t first,
354 const char *abortstr)
356 const struct timer *t;
358 if (!list_check(l, abortstr))
361 list_for_each(l, t, list) {
362 if (t->time < min || t->time > max) {
365 "%s: timer %p %llu not %llu-%llu\n",
366 abortstr, t, (long long)t->time,
367 (long long)min, (long long)max);
372 if (t->time < first) {
375 "%s: timer %p %llu < minimum %llu\n",
376 abortstr, t, (long long)t->time,
386 struct timers *timers_check(const struct timers *timers, const char *abortstr)
388 unsigned int l, i, off;
392 if (!timers->level[0])
395 /* First level is simple. */
396 off = timers->base % PER_LEVEL;
397 for (i = 0; i < PER_LEVEL; i++) {
400 h = &timers->level[l]->list[(i+off) % PER_LEVEL];
401 if (!timer_list_check(h, timers->base + i, timers->base + i,
402 timers->firsts[l], abortstr))
406 /* For other levels, "current" bucket has been emptied, and may contain
407 * entries for the current + level_size bucket. */
408 for (l = 1; l < ARRAY_SIZE(timers->level) && timers->level[l]; l++) {
409 uint64_t per_bucket = 1ULL << (TIMER_LEVEL_BITS * l);
411 off = ((timers->base >> (l*TIMER_LEVEL_BITS)) % PER_LEVEL);
412 /* We start at *next* bucket. */
413 base = (timers->base & ~(per_bucket - 1)) + per_bucket;
415 for (i = 1; i <= PER_LEVEL; i++) {
418 h = &timers->level[l]->list[(i+off) % PER_LEVEL];
419 if (!timer_list_check(h, base, base + per_bucket - 1,
420 timers->firsts[l], abortstr))
427 base = (timers->base & ~((1ULL << (TIMER_LEVEL_BITS * l)) - 1))
428 + (1ULL << (TIMER_LEVEL_BITS * l)) - 1;
429 if (!timer_list_check(&timers->far, base, -1ULL,
430 timers->firsts[ARRAY_SIZE(timers->level)],
434 return (struct timers *)timers;
437 #ifdef CCAN_TIMER_DEBUG
438 static void dump_bucket_stats(FILE *fp, const struct list_head *h)
440 unsigned long long min, max, num;
451 list_for_each(h, t, list) {
458 fprintf(fp, " %llu (%llu-%llu)\n",
462 void timers_dump(const struct timers *timers, FILE *fp)
464 unsigned int l, i, off;
465 unsigned long long base;
470 fprintf(fp, "Base: %llu\n", (unsigned long long)timers->base);
472 if (!timers->level[0])
475 fprintf(fp, "Level 0:\n");
477 /* First level is simple. */
478 off = timers->base % PER_LEVEL;
479 for (i = 0; i < PER_LEVEL; i++) {
480 const struct list_head *h;
482 fprintf(fp, " Bucket %llu (%lu):",
483 (i+off) % PER_LEVEL, timers->base + i);
484 h = &timers->level[0]->list[(i+off) % PER_LEVEL];
485 dump_bucket_stats(fp, h);
488 /* For other levels, "current" bucket has been emptied, and may contain
489 * entries for the current + level_size bucket. */
490 for (l = 1; l < ARRAY_SIZE(timers->level) && timers->level[l]; l++) {
491 uint64_t per_bucket = 1ULL << (TIMER_LEVEL_BITS * l);
493 off = ((timers->base >> (l*TIMER_LEVEL_BITS)) % PER_LEVEL);
494 /* We start at *next* bucket. */
495 base = (timers->base & ~(per_bucket - 1)) + per_bucket;
497 fprintf(fp, "Level %u:\n", l);
498 for (i = 1; i <= PER_LEVEL; i++) {
499 const struct list_head *h;
501 fprintf(fp, " Bucket %llu (%llu - %llu):",
503 base, base + per_bucket - 1);
505 h = &timers->level[l]->list[(i+off) % PER_LEVEL];
506 dump_bucket_stats(fp, h);
512 if (!list_empty(&timers->far)) {
513 fprintf(fp, "Far timers:");
514 dump_bucket_stats(fp, &timers->far);
519 void timers_cleanup(struct timers *timers)
523 for (l = 0; l < ARRAY_SIZE(timers->level); l++)
524 free(timers->level[l]);