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 void *timer_default_alloc(struct timers *timers, size_t len)
19 static void timer_default_free(struct timers *timers, void *p)
24 static void *(*timer_alloc)(struct timers *, size_t) = timer_default_alloc;
25 static void (*timer_free)(struct timers *, void *) = timer_default_free;
27 void timers_set_allocator(void *(*alloc)(struct timers *, size_t len),
28 void (*free)(struct timers *, void *p))
31 alloc = timer_default_alloc;
33 free = timer_default_free;
38 static uint64_t time_to_grains(struct timemono t)
40 return t.ts.tv_sec * ((uint64_t)1000000000 / TIMER_GRANULARITY)
41 + (t.ts.tv_nsec / TIMER_GRANULARITY);
44 static struct timemono grains_to_time(uint64_t grains)
48 t.ts.tv_sec = grains / (1000000000 / TIMER_GRANULARITY);
49 t.ts.tv_nsec = (grains % (1000000000 / TIMER_GRANULARITY))
54 void timers_init(struct timers *timers, struct timemono start)
58 list_head_init(&timers->far);
59 timers->base = time_to_grains(start);
60 timers->first = -1ULL;
61 memset(timers->firsts, 0xFF, sizeof(timers->firsts));
62 for (i = 0; i < ARRAY_SIZE(timers->level); i++)
63 timers->level[i] = NULL;
66 static unsigned int level_of(const struct timers *timers, uint64_t time)
70 /* Level depends how far away it is. */
71 diff = time - timers->base;
72 return ilog64(diff / 2) / TIMER_LEVEL_BITS;
75 static void timer_add_raw(struct timers *timers, struct timer *t)
78 unsigned int level = level_of(timers, t->time);
81 if (!timers->level[level]) {
83 first = &timers->firsts[ARRAY_SIZE(timers->level)];
85 int off = (t->time >> (level*TIMER_LEVEL_BITS)) & (PER_LEVEL-1);
86 l = &timers->level[level]->list[off];
87 first = &timers->firsts[level];
90 list_add_tail(l, &t->list);
95 void timer_init(struct timer *t)
97 list_node_init(&t->list);
100 static inline bool list_node_initted(const struct list_node *n)
105 void timer_addrel(struct timers *timers, struct timer *t, struct timerel rel)
107 assert(list_node_initted(&t->list));
109 t->time = time_to_grains(timemono_add(time_mono(), rel));
111 /* Added in the past? Treat it as imminent. */
112 if (t->time < timers->base)
113 t->time = timers->base;
115 if (t->time < timers->first)
116 timers->first = t->time;
118 timer_add_raw(timers, t);
121 void timer_addmono(struct timers *timers, struct timer *t, struct timemono when)
123 assert(list_node_initted(&t->list));
125 t->time = time_to_grains(when);
127 /* Added in the past? Treat it as imminent. */
128 if (t->time < timers->base)
129 t->time = timers->base;
130 if (t->time < timers->first)
131 timers->first = t->time;
133 timer_add_raw(timers, t);
137 void timer_del(struct timers *timers UNNEEDED, struct timer *t)
139 list_del_init(&t->list);
142 static void timers_far_get(struct timers *timers,
143 struct list_head *list,
146 struct timer *i, *next;
148 list_for_each_safe(&timers->far, i, next, list) {
149 if (i->time <= when) {
150 list_del_from(&timers->far, &i->list);
151 list_add_tail(list, &i->list);
156 static void add_level(struct timers *timers, unsigned int level)
158 struct timer_level *l;
161 struct list_head from_far;
163 l = timer_alloc(timers, sizeof(*l));
167 for (i = 0; i < ARRAY_SIZE(l->list); i++)
168 list_head_init(&l->list[i]);
169 timers->level[level] = l;
171 list_head_init(&from_far);
172 timers_far_get(timers, &from_far,
173 timers->base + (1ULL << ((level+1)*TIMER_LEVEL_BITS)) - 1);
175 while ((t = list_pop(&from_far, struct timer, list)) != NULL)
176 timer_add_raw(timers, t);
179 /* We don't need to search past the first at level 0, since the
180 * bucket range is 1; they're all the same. */
181 static const struct timer *find_first(const struct list_head *list,
183 const struct timer *prev)
187 list_for_each(list, t, list) {
188 if (!prev || t->time < prev->time)
196 /* Update level's first watermark, and return overall first. */
197 static const struct timer *first_for_level(struct timers *timers,
199 const struct timer *level_first,
200 const struct timer *first)
203 timers->firsts[level] = level_first->time;
204 if (!first || level_first->time < first->time)
207 timers->firsts[level] = -1ULL;
212 static bool level_may_beat(const struct timers *timers, size_t level,
213 const struct timer *first)
215 return !first || timers->firsts[level] < first->time;
218 /* FIXME: Suboptimal */
219 static const struct timer *brute_force_first(struct timers *timers)
222 const struct timer *found = NULL;
224 for (l = 0; l < ARRAY_SIZE(timers->level) && timers->level[l]; l++) {
225 const struct timer *t = NULL;
227 /* Do we know they don't have a better one? */
228 if (!level_may_beat(timers, l, found))
231 /* Find first timer on this level. */
232 for (i = 0; i < PER_LEVEL; i++)
233 t = find_first(&timers->level[l]->list[i], l, t);
235 found = first_for_level(timers, l, t, found);
238 /* Check (and update) far list if there's a chance. */
239 l = ARRAY_SIZE(timers->level);
240 if (level_may_beat(timers, l, found)) {
241 const struct timer *t = find_first(&timers->far, l, NULL);
242 found = first_for_level(timers, l, t, found);
248 static const struct timer *get_first(struct timers *timers)
250 /* We can have just far timers, for example. */
251 if (timers->level[0]) {
252 /* First search rest of lower buckets; we've already spilled
253 * so if we find one there we don't need to search further. */
254 unsigned int i, off = timers->base % PER_LEVEL;
256 for (i = off; i < PER_LEVEL; i++) {
257 struct list_head *h = &timers->level[0]->list[i];
259 return find_first(h, 0, NULL);
263 /* From here on, we're searching non-normalized parts of the
264 * data structure, which is much subtler.
266 * So we brute force. */
267 return brute_force_first(timers);
270 static bool update_first(struct timers *timers)
272 const struct timer *found = get_first(timers);
275 timers->first = -1ULL;
279 timers->first = found->time;
283 bool timer_earliest(struct timers *timers, struct timemono *first)
285 if (!update_first(timers))
288 *first = grains_to_time(timers->first);
292 /* Assume no timers before 'time', cascade down and update base time. */
293 static void timer_fast_forward(struct timers *timers, uint64_t time)
295 unsigned int level, changed;
297 struct list_head list;
300 /* How many bits changed between base and time?
301 * Each time we wrap, we need to empty buckets from above. */
302 if (time == timers->base)
305 changed = ilog64_nz(time ^ timers->base);
306 level = (changed - 1) / TIMER_LEVEL_BITS;
308 /* Buckets always empty downwards, so we could cascade manually,
309 * but it's rarely very many so we just remove and re-add */
310 list_head_init(&list);
313 if (!timers->level[level]) {
314 /* We need any which belong on this level. */
315 timers_far_get(timers, &list,
317 + (1ULL << ((level+1)*TIMER_LEVEL_BITS))-1);
322 /* Get all timers from this bucket. */
323 src = (time >> (level * TIMER_LEVEL_BITS)) % PER_LEVEL;
324 list_append_list(&list,
325 &timers->level[level]->list[src]);
329 /* Did we hit the last level? If so, add. */
330 if (need_level != -1)
331 add_level(timers, need_level);
333 /* Fast-forward the time, and re-add everyone. */
335 while ((i = list_pop(&list, struct timer, list)) != NULL)
336 timer_add_raw(timers, i);
339 /* Returns an expired timer. */
340 struct timer *timers_expire(struct timers *timers, struct timemono expire)
342 uint64_t now = time_to_grains(expire);
346 /* This can happen without TIME_HAVE_MONOTONIC, but I also have
347 * a report of OpenBSD 6.8 under virtualbox doing this. */
348 if (now < timers->base) {
352 if (!timers->level[0]) {
353 if (list_empty(&timers->far))
355 add_level(timers, 0);
359 if (timers->first > now) {
360 timer_fast_forward(timers, now);
364 timer_fast_forward(timers, timers->first);
365 off = timers->base % PER_LEVEL;
367 /* This *may* be NULL, if we deleted the first timer */
368 t = list_pop(&timers->level[0]->list[off], struct timer, list);
370 list_node_init(&t->list);
371 } while (!t && update_first(timers));
376 static bool timer_list_check(const struct list_head *l,
377 uint64_t min, uint64_t max, uint64_t first,
378 const char *abortstr)
380 const struct timer *t;
382 if (!list_check(l, abortstr))
385 list_for_each(l, t, list) {
386 if (t->time < min || t->time > max) {
389 "%s: timer %p %llu not %llu-%llu\n",
390 abortstr, t, (long long)t->time,
391 (long long)min, (long long)max);
396 if (t->time < first) {
399 "%s: timer %p %llu < minimum %llu\n",
400 abortstr, t, (long long)t->time,
410 struct timers *timers_check(const struct timers *timers, const char *abortstr)
412 unsigned int l, i, off;
416 if (!timers->level[0])
419 /* First level is simple. */
420 off = timers->base % PER_LEVEL;
421 for (i = 0; i < PER_LEVEL; i++) {
424 h = &timers->level[l]->list[(i+off) % PER_LEVEL];
425 if (!timer_list_check(h, timers->base + i, timers->base + i,
426 timers->firsts[l], abortstr))
430 /* For other levels, "current" bucket has been emptied, and may contain
431 * entries for the current + level_size bucket. */
432 for (l = 1; l < ARRAY_SIZE(timers->level) && timers->level[l]; l++) {
433 uint64_t per_bucket = 1ULL << (TIMER_LEVEL_BITS * l);
435 off = ((timers->base >> (l*TIMER_LEVEL_BITS)) % PER_LEVEL);
436 /* We start at *next* bucket. */
437 base = (timers->base & ~(per_bucket - 1)) + per_bucket;
439 for (i = 1; i <= PER_LEVEL; i++) {
442 h = &timers->level[l]->list[(i+off) % PER_LEVEL];
443 if (!timer_list_check(h, base, base + per_bucket - 1,
444 timers->firsts[l], abortstr))
451 base = (timers->base & ~((1ULL << (TIMER_LEVEL_BITS * l)) - 1))
452 + (1ULL << (TIMER_LEVEL_BITS * l)) - 1;
453 if (!timer_list_check(&timers->far, base, -1ULL,
454 timers->firsts[ARRAY_SIZE(timers->level)],
458 return (struct timers *)timers;
461 #ifdef CCAN_TIMER_DEBUG
462 static void dump_bucket_stats(FILE *fp, const struct list_head *h)
464 unsigned long long min, max, num;
475 list_for_each(h, t, list) {
482 fprintf(fp, " %llu (%llu-%llu)\n",
486 void timers_dump(const struct timers *timers, FILE *fp)
488 unsigned int l, i, off;
489 unsigned long long base;
494 fprintf(fp, "Base: %llu\n", (unsigned long long)timers->base);
496 if (!timers->level[0])
499 fprintf(fp, "Level 0:\n");
501 /* First level is simple. */
502 off = timers->base % PER_LEVEL;
503 for (i = 0; i < PER_LEVEL; i++) {
504 const struct list_head *h;
506 fprintf(fp, " Bucket %llu (%lu):",
507 (i+off) % PER_LEVEL, timers->base + i);
508 h = &timers->level[0]->list[(i+off) % PER_LEVEL];
509 dump_bucket_stats(fp, h);
512 /* For other levels, "current" bucket has been emptied, and may contain
513 * entries for the current + level_size bucket. */
514 for (l = 1; l < ARRAY_SIZE(timers->level) && timers->level[l]; l++) {
515 uint64_t per_bucket = 1ULL << (TIMER_LEVEL_BITS * l);
517 off = ((timers->base >> (l*TIMER_LEVEL_BITS)) % PER_LEVEL);
518 /* We start at *next* bucket. */
519 base = (timers->base & ~(per_bucket - 1)) + per_bucket;
521 fprintf(fp, "Level %u:\n", l);
522 for (i = 1; i <= PER_LEVEL; i++) {
523 const struct list_head *h;
525 fprintf(fp, " Bucket %llu (%llu - %llu):",
527 base, base + per_bucket - 1);
529 h = &timers->level[l]->list[(i+off) % PER_LEVEL];
530 dump_bucket_stats(fp, h);
536 if (!list_empty(&timers->far)) {
537 fprintf(fp, "Far timers:");
538 dump_bucket_stats(fp, &timers->far);
543 void timers_cleanup(struct timers *timers)
547 for (l = 0; l < ARRAY_SIZE(timers->level); l++)
548 timer_free(timers, timers->level[l]);