timers: implementation of lazily-ordered timers.
[ccan] / ccan / timer / timer.c
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>
6 #include <stdlib.h>
7 #include <stdio.h>
8
9 #define PER_LEVEL (1ULL << TIMER_LEVEL_BITS)
10
11 struct timer_level {
12         struct list_head list[PER_LEVEL];
13 };
14
15 static uint64_t time_to_grains(struct timespec ts)
16 {
17         return ts.tv_sec * ((uint64_t)1000000000 / TIMER_GRANULARITY)
18                 + (ts.tv_nsec / TIMER_GRANULARITY);
19 }
20
21 static struct timespec grains_to_time(uint64_t grains)
22 {
23         struct timespec ts;
24
25         ts.tv_sec = grains / (1000000000 / TIMER_GRANULARITY);
26         ts.tv_nsec = (grains % (1000000000 / TIMER_GRANULARITY))
27                 * TIMER_GRANULARITY;
28         return ts;
29 }
30
31 void timers_init(struct timers *timers, struct timespec start)
32 {
33         unsigned int i;
34
35         list_head_init(&timers->far);
36         timers->base = time_to_grains(start);
37         for (i = 0; i < ARRAY_SIZE(timers->level); i++)
38                 timers->level[i] = NULL;
39 }
40
41 static void timer_add_raw(struct timers *timers, struct timer *t)
42 {
43         struct list_head *l;
44         uint64_t diff;
45         unsigned int level;
46
47         /* Level depends how far away it is. */
48         diff = t->time - timers->base;
49         level = ilog64(diff / 2) / TIMER_LEVEL_BITS;
50
51         if (!timers->level[level])
52                 l = &timers->far;
53         else {
54                 int off = (t->time >> (level*TIMER_LEVEL_BITS)) & (PER_LEVEL-1);
55                 l = &timers->level[level]->list[off];
56         }
57
58         list_add_tail(l, &t->list);
59 }
60
61 void timer_add(struct timers *timers, struct timer *t, struct timespec when)
62 {
63         t->time = time_to_grains(when);
64
65         /* Added in the past?  Treat it as imminent. */
66         if (t->time < timers->base)
67                 t->time = timers->base;
68
69         timer_add_raw(timers, t);
70 }
71
72 /* FIXME: inline */
73 void timer_del(struct timers *timers, struct timer *t)
74 {
75         list_del(&t->list);
76 }
77
78 static void timers_far_get(struct timers *timers,
79                            struct list_head *list,
80                            uint64_t when)
81 {
82         struct timer *i, *next;
83
84         list_for_each_safe(&timers->far, i, next, list) {
85                 if (i->time <= when) {
86                         list_del_from(&timers->far, &i->list);
87                         list_add_tail(list, &i->list);
88                 }
89         }
90 }
91
92 static void add_level(struct timers *timers, unsigned int level)
93 {
94         struct timer_level *l;
95         struct timer *t;
96         unsigned int i;
97         struct list_head from_far;
98
99         l = malloc(sizeof(*l));
100         if (!l)
101                 return;
102
103         for (i = 0; i < ARRAY_SIZE(l->list); i++)
104                 list_head_init(&l->list[i]);
105         timers->level[level] = l;
106
107         list_head_init(&from_far);
108         timers_far_get(timers, &from_far,
109                        timers->base + (1ULL << ((level+1)*TIMER_LEVEL_BITS)) - 1);
110
111         while ((t = list_pop(&from_far, struct timer, list)) != NULL)
112                 timer_add_raw(timers, t);
113 }
114
115 /* Take timers from level and distribute them down one. */
116 static void cascade(struct timers *timers, unsigned int level)
117 {
118         struct timer *i;
119         struct list_head from_far, *list;
120
121         if (level == ARRAY_SIZE(timers->level) || !timers->level[level]) {
122                 list_head_init(&from_far);
123                 timers_far_get(timers, &from_far,
124                                timers->base
125                                + (1ULL << (level*TIMER_LEVEL_BITS))-1);
126                 list = &from_far;
127                 if (level != ARRAY_SIZE(timers->level))
128                         add_level(timers, level);
129         } else {
130                 unsigned src;
131
132                 src = (timers->base >> (level * TIMER_LEVEL_BITS)) % PER_LEVEL;
133                 if (src == 0)
134                         cascade(timers, level + 1);
135                 list = &timers->level[level]->list[src];
136         }
137
138         while ((i = list_pop(list, struct timer, list)) != NULL) {
139                 unsigned dst;
140
141                 assert(i->time >= timers->base);
142                 assert(i->time < (timers->base
143                                   + (1ULL << ((level+1)*TIMER_LEVEL_BITS))));
144
145                 dst = (i->time >> ((level-1)*TIMER_LEVEL_BITS)) % PER_LEVEL;
146                 list_add_tail(&timers->level[level-1]->list[dst], &i->list);
147         }
148 }
149
150 static const struct timer *find_first(const struct list_head *list,
151                                       const struct timer *prev)
152 {
153         struct timer *t;
154
155         list_for_each(list, t, list) {
156                 if (!prev || t->time < prev->time)
157                         prev = t;
158         }
159         return prev;
160 }
161
162 static struct timer *get_first(const struct timers *timers)
163 {
164         unsigned int level = 0, i, off;
165         bool need_next;
166         uint64_t base = timers->base;
167         const struct timer *found = NULL;
168         struct list_head *h;
169
170 next:
171         if (!timers->level[level])
172                 return (struct timer *)find_first(&timers->far, NULL);
173
174         need_next = false;
175         off = base % PER_LEVEL;
176         for (i = 0; i < PER_LEVEL; i++) {
177                 h = &timers->level[level]->list[(i+off) % PER_LEVEL];
178
179                 if (!list_empty(h))
180                         break;
181
182                 /* We haven't cascaded yet, so if we wrap, we'll need to
183                  * check next level, too. */
184                 if (i + off == PER_LEVEL)
185                         need_next = true;
186         }
187         if (i == PER_LEVEL) {
188                 level++;
189                 base >>= TIMER_LEVEL_BITS;
190                 goto next;
191         }
192
193         /* Level 0 is exact, so they're all the same. */
194         if (level == 0)
195                 found = list_top(h, struct timer, list);
196         else
197                 found = find_first(h, NULL);
198
199         if (need_next) {
200                 if (!timers->level[level+1]) {
201                         found = find_first(&timers->far, found);
202                 } else {
203                         base >>= TIMER_LEVEL_BITS;
204                         off = base % PER_LEVEL;
205                         h = &timers->level[level+1]->list[off];
206                         found = find_first(h, found);
207                 }
208         }
209
210         return (struct timer *)found;
211 }
212
213 bool timer_earliest(const struct timers *timers, struct timespec *first)
214 {
215         struct timer *found = get_first(timers);
216
217         if (!found)
218                 return false;
219         *first = grains_to_time(found->time);
220         return true;
221 }
222
223 /* Assume no timers before 'time', cascade down and update base time. */
224 static void timer_fast_forward(struct timers *timers, uint64_t time)
225 {
226         unsigned int level, changed;
227         int need_level = -1;
228         struct list_head list;
229         struct timer *i;
230
231         /* How many bits changed between base and time?
232          * Each time we wrap, we need to empty buckets from above. */
233         if (time == timers->base)
234                 return;
235
236         changed = ilog64_nz(time ^ timers->base);
237         level = (changed - 1) / TIMER_LEVEL_BITS;
238
239         /* Buckets always empty downwards, so we could cascade manually,
240          * but it's rarely very many so we just remove and re-add */
241         list_head_init(&list);
242
243         do {
244                 if (!timers->level[level]) {
245                         /* We need any which belong on this level. */
246                         timers_far_get(timers, &list,
247                                        timers->base
248                                        + (1ULL << ((level+1)*TIMER_LEVEL_BITS))-1);
249                         need_level = level;
250                 } else {
251                         unsigned src;
252
253                         /* Get all timers from this bucket. */
254                         src = (time >> (level * TIMER_LEVEL_BITS)) % PER_LEVEL;
255                         list_append_list(&list,
256                                          &timers->level[level]->list[src]);
257                 }
258         } while (level--);
259
260         /* Did we hit the last level?  If so, add. */
261         if (need_level != -1)
262                 add_level(timers, need_level);
263
264         /* Fast-forward the time, and re-add everyone. */
265         timers->base = time;
266         while ((i = list_pop(&list, struct timer, list)) != NULL)
267                 timer_add_raw(timers, i);
268 }
269
270 /* Fills list of expired timers. */
271 void timers_expire(struct timers *timers,
272                    struct timespec expire,
273                    struct list_head *list)
274 {
275         uint64_t now = time_to_grains(expire);
276         unsigned int off;
277         const struct timer *first;
278
279         assert(now >= timers->base);
280
281         list_head_init(list);
282
283         if (!timers->level[0]) {
284                 if (list_empty(&timers->far))
285                         return;
286                 add_level(timers, 0);
287         }
288
289         while ((first = get_first(timers)) != NULL) {
290                 assert(first->time >= timers->base);
291                 if (first->time > now) {
292                         timer_fast_forward(timers, now);
293                         break;
294                 }
295
296                 timer_fast_forward(timers, first->time);
297                 off = timers->base % PER_LEVEL;
298
299                 list_append_list(list, &timers->level[0]->list[off]);
300                 if (timers->base == now)
301                         break;
302         }
303 }
304
305 static bool timer_list_check(const struct list_head *l,
306                              uint64_t min, uint64_t max,
307                              const char *abortstr)
308 {
309         const struct timer *t;
310
311         if (!list_check(l, abortstr))
312                 return false;
313
314         list_for_each(l, t, list) {
315                 if (t->time < min || t->time > max) {
316                         if (abortstr) {
317                                 fprintf(stderr,
318                                         "%s: timer %p %llu not %llu-%llu\n",
319                                         abortstr, t, t->time, min, max);
320                                 abort();
321                         }
322                         return false;
323                 }
324         }
325         return true;
326 }
327
328 struct timers *timers_check(const struct timers *timers, const char *abortstr)
329 {
330         unsigned int l, i, off;
331         uint64_t base;
332
333         l = 0;
334         if (!timers->level[0])
335                 goto past_levels;
336
337         /* First level is simple. */
338         off = timers->base % PER_LEVEL;
339         for (i = 0; i < PER_LEVEL; i++) {
340                 struct list_head *h;
341
342                 h = &timers->level[l]->list[(i+off) % PER_LEVEL];
343                 if (!timer_list_check(h, timers->base + i, timers->base + i,
344                                       abortstr))
345                         return NULL;
346         }
347
348         /* For other levels, "current" bucket has been emptied, and may contain
349          * entries for the current + level_size bucket. */
350         for (l = 1; timers->level[l] && l < PER_LEVEL; l++) {
351                 uint64_t per_bucket = 1ULL << (TIMER_LEVEL_BITS * l);
352
353                 off = ((timers->base >> (l*TIMER_LEVEL_BITS)) % PER_LEVEL);
354                 /* We start at *next* bucket. */
355                 base = (timers->base & ~(per_bucket - 1)) + per_bucket;
356
357                 for (i = 1; i <= PER_LEVEL; i++) {
358                         struct list_head *h;
359
360                         h = &timers->level[l]->list[(i+off) % PER_LEVEL];
361                         if (!timer_list_check(h, base, base + per_bucket - 1,
362                                               abortstr))
363                                 return NULL;
364                         base += per_bucket;
365                 }
366         }
367
368 past_levels:
369         base = (timers->base & ~((1ULL << (TIMER_LEVEL_BITS * l)) - 1))
370                 + (1ULL << (TIMER_LEVEL_BITS * l)) - 1;
371         if (!timer_list_check(&timers->far, base, -1ULL, abortstr))
372                 return NULL;
373
374         return (struct timers *)timers;
375 }
376
377 //#ifdef CCAN_TIMER_DEBUG
378 void timers_dump(const struct timers *timers, FILE *fp)
379 {
380         unsigned int l, i;
381         uint64_t min, max, num;
382         struct timer *t;
383
384         if (!fp)
385                 fp = stderr;
386
387         fprintf(fp, "Base: %llu\n", timers->base);
388
389         for (l = 0; timers->level[l] && l < ARRAY_SIZE(timers->level); l++) {
390                 fprintf(fp, "Level %i (+%llu):\n",
391                         l, (uint64_t)1 << (TIMER_LEVEL_BITS * l));
392                 for (i = 0; i < (1 << TIMER_LEVEL_BITS); i++) {
393
394                         if (list_empty(&timers->level[l]->list[i]))
395                                 continue;
396                         min = -1ULL;
397                         max = 0;
398                         num = 0;
399                         list_for_each(&timers->level[l]->list[i], t, list) {
400                                 if (t->time < min)
401                                         min = t->time;
402                                 if (t->time > max)
403                                         max = t->time;
404                                 num++;
405                         }
406                         fprintf(stderr, "  %llu (+%llu-+%llu)\n",
407                                 num, min - timers->base, max - timers->base);
408                 }
409         }
410
411         min = -1ULL;
412         max = 0;
413         num = 0;
414         list_for_each(&timers->far, t, list) {
415                 if (t->time < min)
416                         min = t->time;
417                 if (t->time > max)
418                         max = t->time;
419                 num++;
420         }
421         fprintf(stderr, "Far: %llu (%llu-%llu)\n", num, min, max);
422 }
423 //#endif
424
425 void timers_cleanup(struct timers *timers)
426 {
427         unsigned int l;
428
429         for (l = 0; l < ARRAY_SIZE(timers->level); l++)
430                 free(timers->level[l]);
431 }