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 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_add(struct timers *timers, struct timer *t, struct timeabs when)
84 assert(list_node_initted(&t->list));
86 t->time = time_to_grains(when);
88 /* Added in the past? Treat it as imminent. */
89 if (t->time < timers->base)
90 t->time = timers->base;
91 if (t->time < timers->first)
92 timers->first = t->time;
94 timer_add_raw(timers, t);
98 void timer_del(struct timers *timers, struct timer *t)
100 list_del_init(&t->list);
103 static void timers_far_get(struct timers *timers,
104 struct list_head *list,
107 struct timer *i, *next;
109 list_for_each_safe(&timers->far, i, next, list) {
110 if (i->time <= when) {
111 list_del_from(&timers->far, &i->list);
112 list_add_tail(list, &i->list);
117 static void add_level(struct timers *timers, unsigned int level)
119 struct timer_level *l;
122 struct list_head from_far;
124 l = malloc(sizeof(*l));
128 for (i = 0; i < ARRAY_SIZE(l->list); i++)
129 list_head_init(&l->list[i]);
130 timers->level[level] = l;
132 list_head_init(&from_far);
133 timers_far_get(timers, &from_far,
134 timers->base + (1ULL << ((level+1)*TIMER_LEVEL_BITS)) - 1);
136 while ((t = list_pop(&from_far, struct timer, list)) != NULL)
137 timer_add_raw(timers, t);
140 /* We don't need to search past the first at level 0, since the
141 * bucket range is 1; they're all the same. */
142 static const struct timer *find_first(const struct list_head *list,
144 const struct timer *prev)
148 list_for_each(list, t, list) {
149 if (!prev || t->time < prev->time)
157 /* Update level's first watermark, and return overall first. */
158 static const struct timer *first_for_level(struct timers *timers,
160 const struct timer *level_first,
161 const struct timer *first)
164 timers->firsts[level] = level_first->time;
165 if (!first || level_first->time < first->time)
168 timers->firsts[level] = -1ULL;
173 static bool level_may_beat(const struct timers *timers, size_t level,
174 const struct timer *first)
176 return !first || timers->firsts[level] < first->time;
179 /* FIXME: Suboptimal */
180 static const struct timer *brute_force_first(struct timers *timers)
183 const struct timer *found = NULL;
185 for (l = 0; l < ARRAY_SIZE(timers->level) && timers->level[l]; l++) {
186 const struct timer *t = NULL;
188 /* Do we know they don't have a better one? */
189 if (!level_may_beat(timers, l, found))
192 /* Find first timer on this level. */
193 for (i = 0; i < PER_LEVEL; i++)
194 t = find_first(&timers->level[l]->list[i], l, t);
196 found = first_for_level(timers, l, t, found);
199 /* Check (and update) far list if there's a chance. */
200 l = ARRAY_SIZE(timers->level);
201 if (level_may_beat(timers, l, found)) {
202 const struct timer *t = find_first(&timers->far, l, NULL);
203 found = first_for_level(timers, l, t, found);
209 static const struct timer *get_first(struct timers *timers)
211 /* We can have just far timers, for example. */
212 if (timers->level[0]) {
213 /* First search rest of lower buckets; we've already spilled
214 * so if we find one there we don't need to search further. */
215 unsigned int i, off = timers->base % PER_LEVEL;
217 for (i = off; i < PER_LEVEL; i++) {
218 struct list_head *h = &timers->level[0]->list[i];
220 return find_first(h, 0, NULL);
224 /* From here on, we're searching non-normalized parts of the
225 * data structure, which is much subtler.
227 * So we brute force. */
228 return brute_force_first(timers);
231 static bool update_first(struct timers *timers)
233 const struct timer *found = get_first(timers);
236 timers->first = -1ULL;
240 timers->first = found->time;
244 bool timer_earliest(struct timers *timers, struct timeabs *first)
246 if (!update_first(timers))
249 *first = grains_to_time(timers->first);
253 /* Assume no timers before 'time', cascade down and update base time. */
254 static void timer_fast_forward(struct timers *timers, uint64_t time)
256 unsigned int level, changed;
258 struct list_head list;
261 /* How many bits changed between base and time?
262 * Each time we wrap, we need to empty buckets from above. */
263 if (time == timers->base)
266 changed = ilog64_nz(time ^ timers->base);
267 level = (changed - 1) / TIMER_LEVEL_BITS;
269 /* Buckets always empty downwards, so we could cascade manually,
270 * but it's rarely very many so we just remove and re-add */
271 list_head_init(&list);
274 if (!timers->level[level]) {
275 /* We need any which belong on this level. */
276 timers_far_get(timers, &list,
278 + (1ULL << ((level+1)*TIMER_LEVEL_BITS))-1);
283 /* Get all timers from this bucket. */
284 src = (time >> (level * TIMER_LEVEL_BITS)) % PER_LEVEL;
285 list_append_list(&list,
286 &timers->level[level]->list[src]);
290 /* Did we hit the last level? If so, add. */
291 if (need_level != -1)
292 add_level(timers, need_level);
294 /* Fast-forward the time, and re-add everyone. */
296 while ((i = list_pop(&list, struct timer, list)) != NULL)
297 timer_add_raw(timers, i);
300 /* Returns an expired timer. */
301 struct timer *timers_expire(struct timers *timers, struct timeabs expire)
303 uint64_t now = time_to_grains(expire);
307 assert(now >= timers->base);
309 if (!timers->level[0]) {
310 if (list_empty(&timers->far))
312 add_level(timers, 0);
316 if (timers->first > now) {
317 timer_fast_forward(timers, now);
321 timer_fast_forward(timers, timers->first);
322 off = timers->base % PER_LEVEL;
324 /* This *may* be NULL, if we deleted the first timer */
325 t = list_pop(&timers->level[0]->list[off], struct timer, list);
327 list_node_init(&t->list);
328 } while (!t && update_first(timers));
333 static bool timer_list_check(const struct list_head *l,
334 uint64_t min, uint64_t max, uint64_t first,
335 const char *abortstr)
337 const struct timer *t;
339 if (!list_check(l, abortstr))
342 list_for_each(l, t, list) {
343 if (t->time < min || t->time > max) {
346 "%s: timer %p %llu not %llu-%llu\n",
347 abortstr, t, (long long)t->time,
348 (long long)min, (long long)max);
353 if (t->time < first) {
356 "%s: timer %p %llu < minimum %llu\n",
357 abortstr, t, (long long)t->time,
367 struct timers *timers_check(const struct timers *timers, const char *abortstr)
369 unsigned int l, i, off;
373 if (!timers->level[0])
376 /* First level is simple. */
377 off = timers->base % PER_LEVEL;
378 for (i = 0; i < PER_LEVEL; i++) {
381 h = &timers->level[l]->list[(i+off) % PER_LEVEL];
382 if (!timer_list_check(h, timers->base + i, timers->base + i,
383 timers->firsts[l], abortstr))
387 /* For other levels, "current" bucket has been emptied, and may contain
388 * entries for the current + level_size bucket. */
389 for (l = 1; l < ARRAY_SIZE(timers->level) && timers->level[l]; l++) {
390 uint64_t per_bucket = 1ULL << (TIMER_LEVEL_BITS * l);
392 off = ((timers->base >> (l*TIMER_LEVEL_BITS)) % PER_LEVEL);
393 /* We start at *next* bucket. */
394 base = (timers->base & ~(per_bucket - 1)) + per_bucket;
396 for (i = 1; i <= PER_LEVEL; i++) {
399 h = &timers->level[l]->list[(i+off) % PER_LEVEL];
400 if (!timer_list_check(h, base, base + per_bucket - 1,
401 timers->firsts[l], abortstr))
408 base = (timers->base & ~((1ULL << (TIMER_LEVEL_BITS * l)) - 1))
409 + (1ULL << (TIMER_LEVEL_BITS * l)) - 1;
410 if (!timer_list_check(&timers->far, base, -1ULL,
411 timers->firsts[ARRAY_SIZE(timers->level)],
415 return (struct timers *)timers;
418 #ifdef CCAN_TIMER_DEBUG
419 static void dump_bucket_stats(FILE *fp, const struct list_head *h)
421 unsigned long long min, max, num;
432 list_for_each(h, t, list) {
439 fprintf(fp, " %llu (%llu-%llu)\n",
443 void timers_dump(const struct timers *timers, FILE *fp)
445 unsigned int l, i, off;
446 unsigned long long base;
451 fprintf(fp, "Base: %llu\n", (unsigned long long)timers->base);
453 if (!timers->level[0])
456 fprintf(fp, "Level 0:\n");
458 /* First level is simple. */
459 off = timers->base % PER_LEVEL;
460 for (i = 0; i < PER_LEVEL; i++) {
461 const struct list_head *h;
463 fprintf(fp, " Bucket %llu (%lu):",
464 (i+off) % PER_LEVEL, timers->base + i);
465 h = &timers->level[0]->list[(i+off) % PER_LEVEL];
466 dump_bucket_stats(fp, h);
469 /* For other levels, "current" bucket has been emptied, and may contain
470 * entries for the current + level_size bucket. */
471 for (l = 1; l < ARRAY_SIZE(timers->level) && timers->level[l]; l++) {
472 uint64_t per_bucket = 1ULL << (TIMER_LEVEL_BITS * l);
474 off = ((timers->base >> (l*TIMER_LEVEL_BITS)) % PER_LEVEL);
475 /* We start at *next* bucket. */
476 base = (timers->base & ~(per_bucket - 1)) + per_bucket;
478 fprintf(fp, "Level %u:\n", l);
479 for (i = 1; i <= PER_LEVEL; i++) {
480 const struct list_head *h;
482 fprintf(fp, " Bucket %llu (%llu - %llu):",
484 base, base + per_bucket - 1);
486 h = &timers->level[l]->list[(i+off) % PER_LEVEL];
487 dump_bucket_stats(fp, h);
493 if (!list_empty(&timers->far)) {
494 fprintf(fp, "Far timers:");
495 dump_bucket_stats(fp, &timers->far);
500 void timers_cleanup(struct timers *timers)
504 for (l = 0; l < ARRAY_SIZE(timers->level); l++)
505 free(timers->level[l]);