timer_funcs.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * $Id$
  3. *
  4. *
  5. * timer related functions (internal)
  6. *
  7. * Copyright (C) 2005 iptelorg GmbH
  8. *
  9. * This file is part of ser, a free SIP server.
  10. *
  11. * ser 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. * For a license to use the ser software under conditions
  17. * other than those described here, or to purchase support for this
  18. * software, please contact iptel.org by e-mail at the following addresses:
  19. * [email protected]
  20. *
  21. * ser is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. */
  30. /* History:
  31. * --------
  32. * 2005-07-27 complete re-design/re-implemnetation (andrei)
  33. */
  34. #ifndef timer_funcs_h
  35. #define timer_funcs_h
  36. #include "timer.h"
  37. struct timer_head{
  38. struct timer_ln* volatile next;
  39. struct timer_ln* volatile prev;
  40. };
  41. /* hierarchical timing wheel with 3 levels
  42. * Most timeouts should go in the first "wheel" (h0)
  43. * h0 will contain timers expiring from crt. time up to
  44. * crt. time + (1<<H0_BITS)/TICKS_HZ s and will use
  45. * (1<<H0_BITS)*sizeof(struct timer_head) bytes of memory, so arrange it
  46. * accordingly
  47. */
  48. #define H0_BITS 14
  49. #define H1_BITS 9
  50. #define H2_BITS (32-H1_BITS-H0_BITS)
  51. /* Uses ~280K on a 64 bits system and ~140K on a 32 bit system; for TICKS_HZ=10
  52. * holds ~ 30 min in the first hash/wheel and ~233h in the first two.
  53. * More perfomant arrangement: 16, 8, 8 (but eats 1 MB on a 64 bit system, and
  54. * 512K on a 32 bit one). For TICKS_HZ=10 it holds almost 2h in the
  55. * first hash/wheel and ~460h in the first two.
  56. */
  57. #define H0_ENTRIES (1<<H0_BITS)
  58. #define H1_ENTRIES (1<<H1_BITS)
  59. #define H2_ENTRIES (1<<H2_BITS)
  60. #define H0_MASK (H0_ENTRIES-1)
  61. #define H1_MASK (H1_ENTRIES-1)
  62. #define H1_H0_MASK ((1<<(H0_BITS+H1_BITS))-1)
  63. struct timer_lists{
  64. struct timer_head h0[H0_ENTRIES];
  65. struct timer_head h1[H1_ENTRIES];
  66. struct timer_head h2[H2_ENTRIES];
  67. struct timer_head expired; /* list of expired entries */
  68. };
  69. extern struct timer_lists* timer_lst;
  70. #define _timer_init_list(head) clist_init((head), next, prev)
  71. #define _timer_add_list(head, tl) \
  72. clist_append((head), (tl), next, prev)
  73. #define _timer_rm_list(tl) \
  74. clist_rm((tl), next, prev)
  75. #define timer_foreach(tl, head) clist_foreach((head), (tl), next)
  76. #define timer_foreach_safe(tl, tmp, head) \
  77. clist_foreach_safe((head), (tl), (tmp), next)
  78. /* generic add timer entry to the timer lists function (see _timer_add)
  79. * tl->expire must be set previously, delta is the difference in ticks
  80. * from current time to the timer desired expire (should be tl->expire-*tick)
  81. * If you don't know delta, you probably want to call _timer_add instead.
  82. */
  83. static inline int _timer_dist_tl(struct timer_ln* tl, ticks_t delta)
  84. {
  85. if (delta<H0_ENTRIES){
  86. if (delta==0){
  87. LOG(L_WARN, "WARNING: timer: add_timeout: 0 expire timer added\n");
  88. _timer_add_list(&timer_lst->expired, tl);
  89. }else{
  90. _timer_add_list( &timer_lst->h0[tl->expire & H0_MASK], tl);
  91. }
  92. }else if (delta<(H0_ENTRIES*H1_ENTRIES)){
  93. _timer_add_list(&timer_lst->h1[(tl->expire & H1_H0_MASK)>>H0_BITS],tl);
  94. }else{
  95. _timer_add_list(&timer_lst->h2[tl->expire>>(H1_BITS+H0_BITS)], tl);
  96. }
  97. return 0;
  98. }
  99. #define _timer_mv_expire(h) \
  100. do{ \
  101. if ((h)->next!=(struct timer_ln*)(h)){ \
  102. clist_append_sublist(&timer_lst->expired, (h)->next, \
  103. (h)->prev, next, prev); \
  104. _timer_init_list(h); \
  105. } \
  106. }while(0)
  107. #if 1
  108. static inline void timer_redist(ticks_t t, struct timer_head *h)
  109. {
  110. struct timer_ln* tl;
  111. struct timer_ln* tmp;
  112. timer_foreach_safe(tl, tmp, h){
  113. _timer_dist_tl(tl, tl->expire-t);
  114. }
  115. /* clear the current list */
  116. _timer_init_list(h);
  117. }
  118. static inline void timer_run(ticks_t t)
  119. {
  120. /* trust the compiler for optimizing */
  121. if ((t & H0_MASK)==0){ /*r1*/
  122. if ((t & H1_H0_MASK)==0){ /*r2*/
  123. timer_redist(t, &timer_lst->h2[t>>(H0_BITS+H1_BITS)]);
  124. }
  125. timer_redist(t, &timer_lst->h1[(t & H1_H0_MASK)>>H0_BITS]);/*r2 >> H0*/
  126. }
  127. /*
  128. DBG("timer_run: ticks %u, expire h0[%u]\n",
  129. (unsigned ) t, (unsigned)(t & H0_MASK));*/
  130. _timer_mv_expire(&timer_lst->h0[t & H0_MASK]); /*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. /* 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