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