timer_funcs.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * $Id$
  3. *
  4. *
  5. * timer related functions (internal)
  6. *
  7. * Copyright (C) 2005 iptelorg GmbH
  8. *
  9. * This file is part of SIP-router, a free SIP server.
  10. *
  11. * SIP-router is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version
  15. *
  16. * SIP-router is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. /* History:
  26. * --------
  27. * 2005-07-27 complete re-design/re-implemnetation (andrei)
  28. */
  29. /**
  30. * @file
  31. * @brief SIP-router core :: Timer related functions (internal)
  32. * @ingroup core
  33. * Module: @ref core
  34. */
  35. #ifndef timer_funcs_h
  36. #define timer_funcs_h
  37. #include "timer.h"
  38. struct timer_head{
  39. struct timer_ln* volatile next;
  40. struct timer_ln* volatile prev;
  41. };
  42. /** @name hierarchical timing wheel with 3 levels
  43. *
  44. * Most timeouts should go in the first "wheel" (h0)
  45. * h0 will contain timers expiring from crt. time up to
  46. * crt. time + (1<<H0_BITS)/TICKS_HZ s and will use
  47. * (1<<H0_BITS)*sizeof(struct timer_head) bytes of memory, so arrange it
  48. * accordingly
  49. *
  50. * Uses ~280K on a 64 bits system and ~140K on a 32 bit system; for TICKS_HZ=10
  51. * holds ~ 30 min in the first hash/wheel and ~233h in the first two.
  52. * More perfomant arrangement: 16, 8, 8 (but eats 1 MB on a 64 bit system, and
  53. * 512K on a 32 bit one). For TICKS_HZ=10 it holds almost 2h in the
  54. * first hash/wheel and ~460h in the first two.
  55. */
  56. /*@{ */
  57. #define H0_BITS 14
  58. #define H1_BITS 9
  59. #define H2_BITS (32-H1_BITS-H0_BITS)
  60. #define H0_ENTRIES (1<<H0_BITS)
  61. #define H1_ENTRIES (1<<H1_BITS)
  62. #define H2_ENTRIES (1<<H2_BITS)
  63. #define H0_MASK (H0_ENTRIES-1)
  64. #define H1_MASK (H1_ENTRIES-1)
  65. #define H1_H0_MASK ((1<<(H0_BITS+H1_BITS))-1)
  66. /*@} */
  67. struct timer_lists{
  68. struct timer_head h0[H0_ENTRIES];
  69. struct timer_head h1[H1_ENTRIES];
  70. struct timer_head h2[H2_ENTRIES];
  71. struct timer_head expired; /* list of expired entries */
  72. };
  73. extern struct timer_lists* timer_lst;
  74. #define _timer_init_list(head) clist_init((head), next, prev)
  75. #define _timer_add_list(head, tl) \
  76. clist_append((head), (tl), next, prev)
  77. #define _timer_rm_list(tl) \
  78. clist_rm((tl), next, prev)
  79. #define timer_foreach(tl, head) clist_foreach((head), (tl), next)
  80. #define timer_foreach_safe(tl, tmp, head) \
  81. clist_foreach_safe((head), (tl), (tmp), next)
  82. /** @brief generic add timer entry to the timer lists function (see _timer_add)
  83. *
  84. * tl->expire must be set previously, delta is the difference in ticks
  85. * from current time to the timer desired expire (should be tl->expire-*tick)
  86. * If you don't know delta, you probably want to call _timer_add instead.
  87. */
  88. static inline int _timer_dist_tl(struct timer_ln* tl, ticks_t delta)
  89. {
  90. if (delta<H0_ENTRIES){
  91. if (delta==0){
  92. LM_WARN("0 expire timer added\n");
  93. _timer_add_list(&timer_lst->expired, tl);
  94. }else{
  95. _timer_add_list( &timer_lst->h0[tl->expire & H0_MASK], tl);
  96. }
  97. }else if (delta<(H0_ENTRIES*H1_ENTRIES)){
  98. _timer_add_list(&timer_lst->h1[(tl->expire & H1_H0_MASK)>>H0_BITS],tl);
  99. }else{
  100. _timer_add_list(&timer_lst->h2[tl->expire>>(H1_BITS+H0_BITS)], tl);
  101. }
  102. return 0;
  103. }
  104. #define _timer_mv_expire(h) \
  105. do{ \
  106. if ((h)->next!=(struct timer_ln*)(h)){ \
  107. clist_append_sublist(&timer_lst->expired, (h)->next, \
  108. (h)->prev, next, prev); \
  109. _timer_init_list(h); \
  110. } \
  111. }while(0)
  112. #if 1
  113. static inline void timer_redist(ticks_t t, struct timer_head *h)
  114. {
  115. struct timer_ln* tl;
  116. struct timer_ln* tmp;
  117. timer_foreach_safe(tl, tmp, h){
  118. _timer_dist_tl(tl, tl->expire-t);
  119. }
  120. /* clear the current list */
  121. _timer_init_list(h);
  122. }
  123. static inline void timer_run(ticks_t t)
  124. {
  125. struct timer_head *thp;
  126. /* trust the compiler for optimizing */
  127. if ((t & H0_MASK)==0){ /*r1*/
  128. if ((t & H1_H0_MASK)==0){ /*r2*/
  129. timer_redist(t, &timer_lst->h2[t>>(H0_BITS+H1_BITS)]);
  130. }
  131. timer_redist(t, &timer_lst->h1[(t & H1_H0_MASK)>>H0_BITS]);/*r2 >> H0*/
  132. }
  133. /*
  134. DBG("timer_run: ticks %u, expire h0[%u]\n",
  135. (unsigned ) t, (unsigned)(t & H0_MASK));*/
  136. thp = &timer_lst->h0[t & H0_MASK];
  137. _timer_mv_expire(thp); /*r1*/
  138. }
  139. #else
  140. static inline void timer_lst_mv0(ticks_t t, struct timer_head* h)
  141. {
  142. struct timer_ln* tl;
  143. struct timer_ln* tmp;
  144. timer_foreach_safe(tl, tmp, h){
  145. _timer_dist_tl(tl, &timer_lst->h0[tl->expire & H0_MASK]);
  146. }
  147. /* clear the current list */
  148. _timer_init_list(h);
  149. }
  150. static inline void timer_lst_mv1(ticks_t t, struct timer_head* h)
  151. {
  152. struct timer_ln* tl;
  153. struct timer_ln* tmp;
  154. timer_foreach_safe(tl, tmp, h){
  155. if ((tl->expire & H0_MASK)==0) /* directly to h0 */
  156. _timer_add_list(tl, &timer_lst->h0[tl->expire & H0_MASK]);
  157. else /* to h1 */
  158. _timer_add_list(tl,
  159. &timer_lst->h1[(tl->expire & H1_H0_MASK)>>H0_BITS]);
  160. }
  161. /* clear the current list */
  162. _timer_init_list(h);
  163. }
  164. /** @brief possible faster version */
  165. static inline void timer_run(ticks_t t)
  166. {
  167. /* trust the compiler for optimizing */
  168. if ((t & H0_MASK)==0){ /*r1*/
  169. if ((t & H1_H0_MASK)==0) /*r2*/
  170. /* just move the list "down" to hash1 */
  171. timer_lst_mv1(&timer_lst->h2[t>>(H0_BITS+H1_BITS)]);
  172. /* move "down" to hash0 */
  173. timer_lst_mv0(&timer_lst->h1[(t & H1_H0_MASK)>>H0_BITS]);
  174. }
  175. _timer_mv_expire(t, &timer_lst->h0[t & H0_MASK]); /*r1*/
  176. }
  177. #endif
  178. #endif