timer_funcs.h 5.5 KB

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