timer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. /* We don't need to search past the first at level 0, since the
  108. * bucket range is 1; they're all the same. */
  109. static const struct timer *find_first(const struct list_head *list,
  110. unsigned int level,
  111. const struct timer *prev)
  112. {
  113. struct timer *t;
  114. list_for_each(list, t, list) {
  115. if (!prev || t->time < prev->time)
  116. prev = t;
  117. if (level == 0)
  118. break;
  119. }
  120. return prev;
  121. }
  122. static const struct timer *get_first(const struct timers *timers)
  123. {
  124. unsigned int level, i, off;
  125. bool need_next;
  126. uint64_t base;
  127. const struct timer *found = NULL;
  128. struct list_head *h;
  129. if (timers->first < timers->base) {
  130. base = timers->base;
  131. level = 0;
  132. } else {
  133. /* May not be accurate, due to timer_del / expiry. */
  134. level = level_of(timers, timers->first);
  135. base = timers->first >> (TIMER_LEVEL_BITS * level);
  136. }
  137. next:
  138. if (!timers->level[level])
  139. return find_first(&timers->far, -1U, NULL);
  140. need_next = false;
  141. off = base % PER_LEVEL;
  142. for (i = 0; i < PER_LEVEL; i++) {
  143. h = &timers->level[level]->list[(i+off) % PER_LEVEL];
  144. if (!list_empty(h))
  145. break;
  146. /* We haven't cascaded yet, so if we wrap, we'll need to
  147. * check next level, too. */
  148. if (i + off == PER_LEVEL)
  149. need_next = true;
  150. }
  151. if (i == PER_LEVEL) {
  152. level++;
  153. base >>= TIMER_LEVEL_BITS;
  154. if (off != 0)
  155. /* We need *next* bucket: we've started reusing the
  156. * one above */
  157. base++;
  158. goto next;
  159. }
  160. /* Level 0 is exact, so they're all the same. */
  161. found = find_first(h, level, NULL);
  162. while (need_next) {
  163. need_next = false;
  164. if (!timers->level[level+1]) {
  165. found = find_first(&timers->far, -1U, found);
  166. } else {
  167. /* Current upper bucket has emptied into this
  168. * bucket; we want *next* one. */
  169. base >>= TIMER_LEVEL_BITS;
  170. base++;
  171. off = base % PER_LEVEL;
  172. if (off == 0) {
  173. need_next = true;
  174. } else {
  175. h = &timers->level[level+1]->list[off];
  176. found = find_first(h, level+1, found);
  177. }
  178. }
  179. }
  180. return found;
  181. }
  182. static bool update_first(struct timers *timers)
  183. {
  184. const struct timer *found = get_first(timers);
  185. if (!found) {
  186. timers->first = -1ULL;
  187. return false;
  188. }
  189. timers->first = found->time;
  190. return true;
  191. }
  192. bool timer_earliest(struct timers *timers, struct timeabs *first)
  193. {
  194. if (!update_first(timers))
  195. return false;
  196. *first = grains_to_time(timers->first);
  197. return true;
  198. }
  199. /* Assume no timers before 'time', cascade down and update base time. */
  200. static void timer_fast_forward(struct timers *timers, uint64_t time)
  201. {
  202. unsigned int level, changed;
  203. int need_level = -1;
  204. struct list_head list;
  205. struct timer *i;
  206. /* How many bits changed between base and time?
  207. * Each time we wrap, we need to empty buckets from above. */
  208. if (time == timers->base)
  209. return;
  210. changed = ilog64_nz(time ^ timers->base);
  211. level = (changed - 1) / TIMER_LEVEL_BITS;
  212. /* Buckets always empty downwards, so we could cascade manually,
  213. * but it's rarely very many so we just remove and re-add */
  214. list_head_init(&list);
  215. do {
  216. if (!timers->level[level]) {
  217. /* We need any which belong on this level. */
  218. timers_far_get(timers, &list,
  219. timers->base
  220. + (1ULL << ((level+1)*TIMER_LEVEL_BITS))-1);
  221. need_level = level;
  222. } else {
  223. unsigned src;
  224. /* Get all timers from this bucket. */
  225. src = (time >> (level * TIMER_LEVEL_BITS)) % PER_LEVEL;
  226. list_append_list(&list,
  227. &timers->level[level]->list[src]);
  228. }
  229. } while (level--);
  230. /* Did we hit the last level? If so, add. */
  231. if (need_level != -1)
  232. add_level(timers, need_level);
  233. /* Fast-forward the time, and re-add everyone. */
  234. timers->base = time;
  235. while ((i = list_pop(&list, struct timer, list)) != NULL)
  236. timer_add_raw(timers, i);
  237. }
  238. /* Returns an expired timer. */
  239. struct timer *timers_expire(struct timers *timers, struct timeabs expire)
  240. {
  241. uint64_t now = time_to_grains(expire);
  242. unsigned int off;
  243. struct timer *t;
  244. assert(now >= timers->base);
  245. if (!timers->level[0]) {
  246. if (list_empty(&timers->far))
  247. return NULL;
  248. add_level(timers, 0);
  249. }
  250. do {
  251. if (timers->first > now) {
  252. timer_fast_forward(timers, now);
  253. return NULL;
  254. }
  255. timer_fast_forward(timers, timers->first);
  256. off = timers->base % PER_LEVEL;
  257. /* This *may* be NULL, if we deleted the first timer */
  258. t = list_pop(&timers->level[0]->list[off], struct timer, list);
  259. if (t)
  260. list_node_init(&t->list);
  261. } while (!t && update_first(timers));
  262. return t;
  263. }
  264. static bool timer_list_check(const struct list_head *l,
  265. uint64_t min, uint64_t max, uint64_t first,
  266. const char *abortstr)
  267. {
  268. const struct timer *t;
  269. if (!list_check(l, abortstr))
  270. return false;
  271. list_for_each(l, t, list) {
  272. if (t->time < min || t->time > max) {
  273. if (abortstr) {
  274. fprintf(stderr,
  275. "%s: timer %p %llu not %llu-%llu\n",
  276. abortstr, t, (long long)t->time,
  277. (long long)min, (long long)max);
  278. abort();
  279. }
  280. return false;
  281. }
  282. if (t->time < first) {
  283. if (abortstr) {
  284. fprintf(stderr,
  285. "%s: timer %p %llu < minimum %llu\n",
  286. abortstr, t, (long long)t->time,
  287. (long long)first);
  288. abort();
  289. }
  290. return false;
  291. }
  292. }
  293. return true;
  294. }
  295. struct timers *timers_check(const struct timers *timers, const char *abortstr)
  296. {
  297. unsigned int l, i, off;
  298. uint64_t base;
  299. l = 0;
  300. if (!timers->level[0])
  301. goto past_levels;
  302. /* First level is simple. */
  303. off = timers->base % PER_LEVEL;
  304. for (i = 0; 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, timers->base + i, timers->base + i,
  308. timers->first, abortstr))
  309. return NULL;
  310. }
  311. /* For other levels, "current" bucket has been emptied, and may contain
  312. * entries for the current + level_size bucket. */
  313. for (l = 1; l < ARRAY_SIZE(timers->level) && timers->level[l]; l++) {
  314. uint64_t per_bucket = 1ULL << (TIMER_LEVEL_BITS * l);
  315. off = ((timers->base >> (l*TIMER_LEVEL_BITS)) % PER_LEVEL);
  316. /* We start at *next* bucket. */
  317. base = (timers->base & ~(per_bucket - 1)) + per_bucket;
  318. for (i = 1; i <= PER_LEVEL; i++) {
  319. struct list_head *h;
  320. h = &timers->level[l]->list[(i+off) % PER_LEVEL];
  321. if (!timer_list_check(h, base, base + per_bucket - 1,
  322. timers->first, abortstr))
  323. return NULL;
  324. base += per_bucket;
  325. }
  326. }
  327. past_levels:
  328. base = (timers->base & ~((1ULL << (TIMER_LEVEL_BITS * l)) - 1))
  329. + (1ULL << (TIMER_LEVEL_BITS * l)) - 1;
  330. if (!timer_list_check(&timers->far, base, -1ULL, timers->first,
  331. abortstr))
  332. return NULL;
  333. return (struct timers *)timers;
  334. }
  335. #ifdef CCAN_TIMER_DEBUG
  336. static void dump_bucket_stats(FILE *fp, const struct list_head *h)
  337. {
  338. unsigned long long min, max, num;
  339. struct timer *t;
  340. if (list_empty(h)) {
  341. printf("\n");
  342. return;
  343. }
  344. min = -1ULL;
  345. max = 0;
  346. num = 0;
  347. list_for_each(h, t, list) {
  348. if (t->time < min)
  349. min = t->time;
  350. if (t->time > max)
  351. max = t->time;
  352. num++;
  353. }
  354. fprintf(fp, " %llu (%llu-%llu)\n",
  355. num, min, max);
  356. }
  357. void timers_dump(const struct timers *timers, FILE *fp)
  358. {
  359. unsigned int l, i, off;
  360. unsigned long long base;
  361. if (!fp)
  362. fp = stderr;
  363. fprintf(fp, "Base: %llu\n", (unsigned long long)timers->base);
  364. if (!timers->level[0])
  365. goto past_levels;
  366. fprintf(fp, "Level 0:\n");
  367. /* First level is simple. */
  368. off = timers->base % PER_LEVEL;
  369. for (i = 0; i < PER_LEVEL; i++) {
  370. const struct list_head *h;
  371. fprintf(fp, " Bucket %llu (%lu):",
  372. (i+off) % PER_LEVEL, timers->base + i);
  373. h = &timers->level[0]->list[(i+off) % PER_LEVEL];
  374. dump_bucket_stats(fp, h);
  375. }
  376. /* For other levels, "current" bucket has been emptied, and may contain
  377. * entries for the current + level_size bucket. */
  378. for (l = 1; l < ARRAY_SIZE(timers->level) && timers->level[l]; l++) {
  379. uint64_t per_bucket = 1ULL << (TIMER_LEVEL_BITS * l);
  380. off = ((timers->base >> (l*TIMER_LEVEL_BITS)) % PER_LEVEL);
  381. /* We start at *next* bucket. */
  382. base = (timers->base & ~(per_bucket - 1)) + per_bucket;
  383. fprintf(fp, "Level %u:\n", l);
  384. for (i = 1; i <= PER_LEVEL; i++) {
  385. const struct list_head *h;
  386. fprintf(fp, " Bucket %llu (%llu - %llu):",
  387. (i+off) % PER_LEVEL,
  388. base, base + per_bucket - 1);
  389. h = &timers->level[l]->list[(i+off) % PER_LEVEL];
  390. dump_bucket_stats(fp, h);
  391. base += per_bucket;
  392. }
  393. }
  394. past_levels:
  395. if (!list_empty(&timers->far)) {
  396. fprintf(fp, "Far timers:");
  397. dump_bucket_stats(fp, &timers->far);
  398. }
  399. }
  400. #endif
  401. void timers_cleanup(struct timers *timers)
  402. {
  403. unsigned int l;
  404. for (l = 0; l < ARRAY_SIZE(timers->level); l++)
  405. free(timers->level[l]);
  406. }