timer.c 9.9 KB

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