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 timespec ts)
17 return ts.tv_sec * ((uint64_t)1000000000 / TIMER_GRANULARITY)
18 + (ts.tv_nsec / TIMER_GRANULARITY);
21 static struct timespec grains_to_time(uint64_t grains)
25 ts.tv_sec = grains / (1000000000 / TIMER_GRANULARITY);
26 ts.tv_nsec = (grains % (1000000000 / TIMER_GRANULARITY))
31 void timers_init(struct timers *timers, struct timespec 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_add(struct timers *timers, struct timer *t, struct timespec when)
68 t->time = time_to_grains(when);
70 /* Added in the past? Treat it as imminent. */
71 if (t->time < timers->base)
72 t->time = timers->base;
73 if (t->time < timers->first)
74 timers->first = t->time;
76 timer_add_raw(timers, t);
80 void timer_del(struct timers *timers, struct timer *t)
85 static void timers_far_get(struct timers *timers,
86 struct list_head *list,
89 struct timer *i, *next;
91 list_for_each_safe(&timers->far, i, next, list) {
92 if (i->time <= when) {
93 list_del_from(&timers->far, &i->list);
94 list_add_tail(list, &i->list);
99 static void add_level(struct timers *timers, unsigned int level)
101 struct timer_level *l;
104 struct list_head from_far;
106 l = malloc(sizeof(*l));
110 for (i = 0; i < ARRAY_SIZE(l->list); i++)
111 list_head_init(&l->list[i]);
112 timers->level[level] = l;
114 list_head_init(&from_far);
115 timers_far_get(timers, &from_far,
116 timers->base + (1ULL << ((level+1)*TIMER_LEVEL_BITS)) - 1);
118 while ((t = list_pop(&from_far, struct timer, list)) != NULL)
119 timer_add_raw(timers, t);
122 /* Take timers from level and distribute them down one. */
123 static void cascade(struct timers *timers, unsigned int level)
126 struct list_head from_far, *list;
128 if (level == ARRAY_SIZE(timers->level) || !timers->level[level]) {
129 list_head_init(&from_far);
130 timers_far_get(timers, &from_far,
132 + (1ULL << (level*TIMER_LEVEL_BITS))-1);
134 if (level != ARRAY_SIZE(timers->level))
135 add_level(timers, level);
139 src = (timers->base >> (level * TIMER_LEVEL_BITS)) % PER_LEVEL;
141 cascade(timers, level + 1);
142 list = &timers->level[level]->list[src];
145 while ((i = list_pop(list, struct timer, list)) != NULL) {
148 assert(i->time >= timers->base);
149 assert(i->time < (timers->base
150 + (1ULL << ((level+1)*TIMER_LEVEL_BITS))));
152 dst = (i->time >> ((level-1)*TIMER_LEVEL_BITS)) % PER_LEVEL;
153 list_add_tail(&timers->level[level-1]->list[dst], &i->list);
157 static const struct timer *find_first(const struct list_head *list,
158 const struct timer *prev)
162 list_for_each(list, t, list) {
163 if (!prev || t->time < prev->time)
169 static const struct timer *get_first(const struct timers *timers)
171 unsigned int level, i, off;
174 const struct timer *found = NULL;
177 if (timers->first < timers->base) {
181 /* May not be accurate, due to timer_del / expiry. */
182 level = level_of(timers, timers->first);
183 base = timers->first >> (TIMER_LEVEL_BITS * level);
187 if (!timers->level[level])
188 return find_first(&timers->far, NULL);
191 off = base % PER_LEVEL;
192 for (i = 0; i < PER_LEVEL; i++) {
193 h = &timers->level[level]->list[(i+off) % PER_LEVEL];
198 /* We haven't cascaded yet, so if we wrap, we'll need to
199 * check next level, too. */
200 if (i + off == PER_LEVEL)
203 if (i == PER_LEVEL) {
205 base >>= TIMER_LEVEL_BITS;
209 /* Level 0 is exact, so they're all the same. */
211 found = list_top(h, struct timer, list);
213 found = find_first(h, NULL);
216 if (!timers->level[level+1]) {
217 found = find_first(&timers->far, found);
219 base >>= TIMER_LEVEL_BITS;
220 off = base % PER_LEVEL;
221 h = &timers->level[level+1]->list[off];
222 found = find_first(h, found);
228 static bool update_first(struct timers *timers)
230 const struct timer *found = get_first(timers);
233 timers->first = -1ULL;
237 timers->first = found->time;
241 bool timer_earliest(struct timers *timers, struct timespec *first)
243 if (!update_first(timers))
246 *first = grains_to_time(timers->first);
250 /* Assume no timers before 'time', cascade down and update base time. */
251 static void timer_fast_forward(struct timers *timers, uint64_t time)
253 unsigned int level, changed;
255 struct list_head list;
258 /* How many bits changed between base and time?
259 * Each time we wrap, we need to empty buckets from above. */
260 if (time == timers->base)
263 changed = ilog64_nz(time ^ timers->base);
264 level = (changed - 1) / TIMER_LEVEL_BITS;
266 /* Buckets always empty downwards, so we could cascade manually,
267 * but it's rarely very many so we just remove and re-add */
268 list_head_init(&list);
271 if (!timers->level[level]) {
272 /* We need any which belong on this level. */
273 timers_far_get(timers, &list,
275 + (1ULL << ((level+1)*TIMER_LEVEL_BITS))-1);
280 /* Get all timers from this bucket. */
281 src = (time >> (level * TIMER_LEVEL_BITS)) % PER_LEVEL;
282 list_append_list(&list,
283 &timers->level[level]->list[src]);
287 /* Did we hit the last level? If so, add. */
288 if (need_level != -1)
289 add_level(timers, need_level);
291 /* Fast-forward the time, and re-add everyone. */
293 while ((i = list_pop(&list, struct timer, list)) != NULL)
294 timer_add_raw(timers, i);
297 /* Fills list of expired timers. */
298 void timers_expire(struct timers *timers,
299 struct timespec expire,
300 struct list_head *list)
302 uint64_t now = time_to_grains(expire);
305 assert(now >= timers->base);
307 list_head_init(list);
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 list_append_list(list, &timers->level[0]->list[off]);
325 if (timers->base == now)
327 } while (update_first(timers));
330 static bool timer_list_check(const struct list_head *l,
331 uint64_t min, uint64_t max, uint64_t first,
332 const char *abortstr)
334 const struct timer *t;
336 if (!list_check(l, abortstr))
339 list_for_each(l, t, list) {
340 if (t->time < min || t->time > max) {
343 "%s: timer %p %llu not %llu-%llu\n",
344 abortstr, t, t->time, min, max);
349 if (t->time < first) {
352 "%s: timer %p %llu < minimum %llu\n",
353 abortstr, t, t->time, first);
362 struct timers *timers_check(const struct timers *timers, const char *abortstr)
364 unsigned int l, i, off;
368 if (!timers->level[0])
371 /* First level is simple. */
372 off = timers->base % PER_LEVEL;
373 for (i = 0; i < PER_LEVEL; i++) {
376 h = &timers->level[l]->list[(i+off) % PER_LEVEL];
377 if (!timer_list_check(h, timers->base + i, timers->base + i,
378 timers->first, abortstr))
382 /* For other levels, "current" bucket has been emptied, and may contain
383 * entries for the current + level_size bucket. */
384 for (l = 1; timers->level[l] && l < PER_LEVEL; l++) {
385 uint64_t per_bucket = 1ULL << (TIMER_LEVEL_BITS * l);
387 off = ((timers->base >> (l*TIMER_LEVEL_BITS)) % PER_LEVEL);
388 /* We start at *next* bucket. */
389 base = (timers->base & ~(per_bucket - 1)) + per_bucket;
391 for (i = 1; i <= PER_LEVEL; i++) {
394 h = &timers->level[l]->list[(i+off) % PER_LEVEL];
395 if (!timer_list_check(h, base, base + per_bucket - 1,
396 timers->first, abortstr))
403 base = (timers->base & ~((1ULL << (TIMER_LEVEL_BITS * l)) - 1))
404 + (1ULL << (TIMER_LEVEL_BITS * l)) - 1;
405 if (!timer_list_check(&timers->far, base, -1ULL, timers->first,
409 return (struct timers *)timers;
412 //#ifdef CCAN_TIMER_DEBUG
413 void timers_dump(const struct timers *timers, FILE *fp)
416 uint64_t min, max, num;
422 fprintf(fp, "Base: %llu\n", timers->base);
424 for (l = 0; timers->level[l] && l < ARRAY_SIZE(timers->level); l++) {
425 fprintf(fp, "Level %i (+%llu):\n",
426 l, (uint64_t)1 << (TIMER_LEVEL_BITS * l));
427 for (i = 0; i < (1 << TIMER_LEVEL_BITS); i++) {
429 if (list_empty(&timers->level[l]->list[i]))
434 list_for_each(&timers->level[l]->list[i], t, list) {
441 fprintf(stderr, " %llu (+%llu-+%llu)\n",
442 num, min - timers->base, max - timers->base);
449 list_for_each(&timers->far, t, list) {
456 fprintf(stderr, "Far: %llu (%llu-%llu)\n", num, min, max);
460 void timers_cleanup(struct timers *timers)
464 for (l = 0; l < ARRAY_SIZE(timers->level); l++)
465 free(timers->level[l]);