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>
8 #define PER_LEVEL (1ULL << TIMER_LEVEL_BITS)
11 struct list_head list[PER_LEVEL];
14 static uint64_t time_to_grains(struct timemono t)
16 return t.ts.tv_sec * ((uint64_t)1000000000 / TIMER_GRANULARITY)
17 + (t.ts.tv_nsec / TIMER_GRANULARITY);
20 static struct timemono grains_to_time(uint64_t grains)
24 t.ts.tv_sec = grains / (1000000000 / TIMER_GRANULARITY);
25 t.ts.tv_nsec = (grains % (1000000000 / TIMER_GRANULARITY))
30 void timers_init(struct timers *timers, struct timemono start)
34 list_head_init(&timers->far);
35 timers->base = time_to_grains(start);
36 timers->first = -1ULL;
37 memset(timers->firsts, 0xFF, sizeof(timers->firsts));
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);
57 if (!timers->level[level]) {
59 first = &timers->firsts[ARRAY_SIZE(timers->level)];
61 int off = (t->time >> (level*TIMER_LEVEL_BITS)) & (PER_LEVEL-1);
62 l = &timers->level[level]->list[off];
63 first = &timers->firsts[level];
66 list_add_tail(l, &t->list);
71 void timer_init(struct timer *t)
73 list_node_init(&t->list);
76 static bool list_node_initted(const struct list_node *n)
81 void timer_addrel(struct timers *timers, struct timer *t, struct timerel rel)
83 assert(list_node_initted(&t->list));
85 t->time = time_to_grains(timemono_add(time_mono(), rel));
87 #if TIME_HAVE_MONOTONIC
88 assert(t->time >= timers->base);
90 /* Added in the past? Treat it as imminent. */
91 if (t->time < timers->base)
92 t->time = timers->base;
94 if (t->time < timers->first)
95 timers->first = t->time;
97 timer_add_raw(timers, t);
100 void timer_addmono(struct timers *timers, struct timer *t, struct timemono when)
102 assert(list_node_initted(&t->list));
104 t->time = time_to_grains(when);
106 /* Added in the past? Treat it as imminent. */
107 if (t->time < timers->base)
108 t->time = timers->base;
109 if (t->time < timers->first)
110 timers->first = t->time;
112 timer_add_raw(timers, t);
116 void timer_del(struct timers *timers UNNEEDED, struct timer *t)
118 list_del_init(&t->list);
121 static void timers_far_get(struct timers *timers,
122 struct list_head *list,
125 struct timer *i, *next;
127 list_for_each_safe(&timers->far, i, next, list) {
128 if (i->time <= when) {
129 list_del_from(&timers->far, &i->list);
130 list_add_tail(list, &i->list);
135 static void add_level(struct timers *timers, unsigned int level)
137 struct timer_level *l;
140 struct list_head from_far;
142 l = malloc(sizeof(*l));
146 for (i = 0; i < ARRAY_SIZE(l->list); i++)
147 list_head_init(&l->list[i]);
148 timers->level[level] = l;
150 list_head_init(&from_far);
151 timers_far_get(timers, &from_far,
152 timers->base + (1ULL << ((level+1)*TIMER_LEVEL_BITS)) - 1);
154 while ((t = list_pop(&from_far, struct timer, list)) != NULL)
155 timer_add_raw(timers, t);
158 /* We don't need to search past the first at level 0, since the
159 * bucket range is 1; they're all the same. */
160 static const struct timer *find_first(const struct list_head *list,
162 const struct timer *prev)
166 list_for_each(list, t, list) {
167 if (!prev || t->time < prev->time)
175 /* Update level's first watermark, and return overall first. */
176 static const struct timer *first_for_level(struct timers *timers,
178 const struct timer *level_first,
179 const struct timer *first)
182 timers->firsts[level] = level_first->time;
183 if (!first || level_first->time < first->time)
186 timers->firsts[level] = -1ULL;
191 static bool level_may_beat(const struct timers *timers, size_t level,
192 const struct timer *first)
194 return !first || timers->firsts[level] < first->time;
197 /* FIXME: Suboptimal */
198 static const struct timer *brute_force_first(struct timers *timers)
201 const struct timer *found = NULL;
203 for (l = 0; l < ARRAY_SIZE(timers->level) && timers->level[l]; l++) {
204 const struct timer *t = NULL;
206 /* Do we know they don't have a better one? */
207 if (!level_may_beat(timers, l, found))
210 /* Find first timer on this level. */
211 for (i = 0; i < PER_LEVEL; i++)
212 t = find_first(&timers->level[l]->list[i], l, t);
214 found = first_for_level(timers, l, t, found);
217 /* Check (and update) far list if there's a chance. */
218 l = ARRAY_SIZE(timers->level);
219 if (level_may_beat(timers, l, found)) {
220 const struct timer *t = find_first(&timers->far, l, NULL);
221 found = first_for_level(timers, l, t, found);
227 static const struct timer *get_first(struct timers *timers)
229 /* We can have just far timers, for example. */
230 if (timers->level[0]) {
231 /* First search rest of lower buckets; we've already spilled
232 * so if we find one there we don't need to search further. */
233 unsigned int i, off = timers->base % PER_LEVEL;
235 for (i = off; i < PER_LEVEL; i++) {
236 struct list_head *h = &timers->level[0]->list[i];
238 return find_first(h, 0, NULL);
242 /* From here on, we're searching non-normalized parts of the
243 * data structure, which is much subtler.
245 * So we brute force. */
246 return brute_force_first(timers);
249 static bool update_first(struct timers *timers)
251 const struct timer *found = get_first(timers);
254 timers->first = -1ULL;
258 timers->first = found->time;
262 bool timer_earliest(struct timers *timers, struct timemono *first)
264 if (!update_first(timers))
267 *first = grains_to_time(timers->first);
271 /* Assume no timers before 'time', cascade down and update base time. */
272 static void timer_fast_forward(struct timers *timers, uint64_t time)
274 unsigned int level, changed;
276 struct list_head list;
279 /* How many bits changed between base and time?
280 * Each time we wrap, we need to empty buckets from above. */
281 if (time == timers->base)
284 changed = ilog64_nz(time ^ timers->base);
285 level = (changed - 1) / TIMER_LEVEL_BITS;
287 /* Buckets always empty downwards, so we could cascade manually,
288 * but it's rarely very many so we just remove and re-add */
289 list_head_init(&list);
292 if (!timers->level[level]) {
293 /* We need any which belong on this level. */
294 timers_far_get(timers, &list,
296 + (1ULL << ((level+1)*TIMER_LEVEL_BITS))-1);
301 /* Get all timers from this bucket. */
302 src = (time >> (level * TIMER_LEVEL_BITS)) % PER_LEVEL;
303 list_append_list(&list,
304 &timers->level[level]->list[src]);
308 /* Did we hit the last level? If so, add. */
309 if (need_level != -1)
310 add_level(timers, need_level);
312 /* Fast-forward the time, and re-add everyone. */
314 while ((i = list_pop(&list, struct timer, list)) != NULL)
315 timer_add_raw(timers, i);
318 /* Returns an expired timer. */
319 struct timer *timers_expire(struct timers *timers, struct timemono expire)
321 uint64_t now = time_to_grains(expire);
325 assert(now >= timers->base);
327 if (!timers->level[0]) {
328 if (list_empty(&timers->far))
330 add_level(timers, 0);
334 if (timers->first > now) {
335 timer_fast_forward(timers, now);
339 timer_fast_forward(timers, timers->first);
340 off = timers->base % PER_LEVEL;
342 /* This *may* be NULL, if we deleted the first timer */
343 t = list_pop(&timers->level[0]->list[off], struct timer, list);
345 list_node_init(&t->list);
346 } while (!t && update_first(timers));
351 static bool timer_list_check(const struct list_head *l,
352 uint64_t min, uint64_t max, uint64_t first,
353 const char *abortstr)
355 const struct timer *t;
357 if (!list_check(l, abortstr))
360 list_for_each(l, t, list) {
361 if (t->time < min || t->time > max) {
364 "%s: timer %p %llu not %llu-%llu\n",
365 abortstr, t, (long long)t->time,
366 (long long)min, (long long)max);
371 if (t->time < first) {
374 "%s: timer %p %llu < minimum %llu\n",
375 abortstr, t, (long long)t->time,
385 struct timers *timers_check(const struct timers *timers, const char *abortstr)
387 unsigned int l, i, off;
391 if (!timers->level[0])
394 /* First level is simple. */
395 off = timers->base % PER_LEVEL;
396 for (i = 0; i < PER_LEVEL; i++) {
399 h = &timers->level[l]->list[(i+off) % PER_LEVEL];
400 if (!timer_list_check(h, timers->base + i, timers->base + i,
401 timers->firsts[l], abortstr))
405 /* For other levels, "current" bucket has been emptied, and may contain
406 * entries for the current + level_size bucket. */
407 for (l = 1; l < ARRAY_SIZE(timers->level) && timers->level[l]; l++) {
408 uint64_t per_bucket = 1ULL << (TIMER_LEVEL_BITS * l);
410 off = ((timers->base >> (l*TIMER_LEVEL_BITS)) % PER_LEVEL);
411 /* We start at *next* bucket. */
412 base = (timers->base & ~(per_bucket - 1)) + per_bucket;
414 for (i = 1; i <= PER_LEVEL; i++) {
417 h = &timers->level[l]->list[(i+off) % PER_LEVEL];
418 if (!timer_list_check(h, base, base + per_bucket - 1,
419 timers->firsts[l], abortstr))
426 base = (timers->base & ~((1ULL << (TIMER_LEVEL_BITS * l)) - 1))
427 + (1ULL << (TIMER_LEVEL_BITS * l)) - 1;
428 if (!timer_list_check(&timers->far, base, -1ULL,
429 timers->firsts[ARRAY_SIZE(timers->level)],
433 return (struct timers *)timers;
436 #ifdef CCAN_TIMER_DEBUG
437 static void dump_bucket_stats(FILE *fp, const struct list_head *h)
439 unsigned long long min, max, num;
450 list_for_each(h, t, list) {
457 fprintf(fp, " %llu (%llu-%llu)\n",
461 void timers_dump(const struct timers *timers, FILE *fp)
463 unsigned int l, i, off;
464 unsigned long long base;
469 fprintf(fp, "Base: %llu\n", (unsigned long long)timers->base);
471 if (!timers->level[0])
474 fprintf(fp, "Level 0:\n");
476 /* First level is simple. */
477 off = timers->base % PER_LEVEL;
478 for (i = 0; i < PER_LEVEL; i++) {
479 const struct list_head *h;
481 fprintf(fp, " Bucket %llu (%lu):",
482 (i+off) % PER_LEVEL, timers->base + i);
483 h = &timers->level[0]->list[(i+off) % PER_LEVEL];
484 dump_bucket_stats(fp, h);
487 /* For other levels, "current" bucket has been emptied, and may contain
488 * entries for the current + level_size bucket. */
489 for (l = 1; l < ARRAY_SIZE(timers->level) && timers->level[l]; l++) {
490 uint64_t per_bucket = 1ULL << (TIMER_LEVEL_BITS * l);
492 off = ((timers->base >> (l*TIMER_LEVEL_BITS)) % PER_LEVEL);
493 /* We start at *next* bucket. */
494 base = (timers->base & ~(per_bucket - 1)) + per_bucket;
496 fprintf(fp, "Level %u:\n", l);
497 for (i = 1; i <= PER_LEVEL; i++) {
498 const struct list_head *h;
500 fprintf(fp, " Bucket %llu (%llu - %llu):",
502 base, base + per_bucket - 1);
504 h = &timers->level[l]->list[(i+off) % PER_LEVEL];
505 dump_bucket_stats(fp, h);
511 if (!list_empty(&timers->far)) {
512 fprintf(fp, "Far timers:");
513 dump_bucket_stats(fp, &timers->far);
518 void timers_cleanup(struct timers *timers)
522 for (l = 0; l < ARRAY_SIZE(timers->level); l++)
523 free(timers->level[l]);