timer.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of SIP-router, a free SIP server.
  7. *
  8. * SIP-router 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. * SIP-router 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. /* History:
  23. * --------
  24. * 2003-03-19 replaced all the mallocs/frees w/ pkg_malloc/pkg_free (andrei)
  25. * 2003-03-29 cleaning pkg_mallocs introduced (jiri)
  26. * 2005-07-27 complete re-design/re-implementation (andrei)
  27. * 2005-12-12 workaround & bug reporting for timer_del(self) called from
  28. * a timer handle; added timer_allow_del() (andrei)
  29. * 2007-05-26 workaround for darwin sigwait() bug, see slow_timer_main() or
  30. * grep __OS_darwin for more info (andrei)
  31. * 2007-07-01 timer_del() returns <0 if the timer is not active or
  32. * cannot be deleted (andrei)
  33. */
  34. /**
  35. * @file
  36. * @brief SIP-router core ::
  37. * @ingroup core
  38. * Module: @ref core
  39. */
  40. #include "timer.h"
  41. #include "timer_funcs.h"
  42. #include "timer_ticks.h"
  43. #include "dprint.h"
  44. #include <time.h> /* gettimeofday */
  45. #include <sys/time.h> /* setitimer, gettimeofday */
  46. #include <signal.h> /* SIGALRM */
  47. #include <errno.h>
  48. #include <unistd.h> /* pause() */
  49. #include <stdlib.h> /* random, debugging only */
  50. #include "error.h"
  51. #include "signals.h"
  52. /*
  53. #include "config.h"
  54. */
  55. #include "globals.h"
  56. #include "mem/mem.h"
  57. #ifdef SHM_MEM
  58. #include "mem/shm_mem.h"
  59. #endif
  60. #include "locking.h"
  61. #include "sched_yield.h"
  62. #include "cfg/cfg_struct.h"
  63. /* how often will the timer handler be called (in ticks) */
  64. #define TIMER_HANDLER_INTERVAL 1U
  65. /* how often to try to re-adjust the ticks */
  66. #define TIMER_RESYNC_TICKS (TIMER_TICKS_HZ*5U) /* each 5 s */
  67. #define TIMER_MAX_DRIFT (TIMER_TICKS_HZ/10U) /* if drift > 0.1s adjust */
  68. static ticks_t* ticks=0;
  69. static ticks_t last_ticks; /* last time we adjusted the time */
  70. static ticks_t last_adj_check; /* last time we ran adjust_ticks */
  71. static ticks_t prev_ticks; /* last time we ran the timer, also used as
  72. "current" ticks when running the timer for
  73. "skipped" ticks */
  74. static struct timeval last_time;
  75. static struct timeval start_time; /* for debugging */
  76. static volatile int run_timer=0;
  77. static int timer_id=0;
  78. static gen_lock_t* timer_lock=0;
  79. static struct timer_ln* volatile* running_timer=0;/* running timer handler */
  80. static int in_timer=0;
  81. #define IS_IN_TIMER() (in_timer)
  82. #define LOCK_TIMER_LIST() lock_get(timer_lock)
  83. #define UNLOCK_TIMER_LIST() lock_release(timer_lock)
  84. /* we can get away without atomic_set/atomic_cmp and write barriers because we
  85. * always call SET_RUNNING and IS_RUNNING while holding the timer lock
  86. * => it's implicitly atomic and the lock acts as write barrier */
  87. #define SET_RUNNING(t) (*running_timer=(t))
  88. #define IS_RUNNING(t) (*running_timer==(t))
  89. #define UNSET_RUNNING() (*running_timer=0)
  90. #ifdef USE_SLOW_TIMER
  91. #define SLOW_TIMER_SIG SIGUSR2
  92. /* timer flags checks */
  93. #define IS_FAST_TIMER(t) (t->flags&F_TIMER_FAST)
  94. #define SET_SLOW_LIST(t) (t->flags|=F_TIMER_ON_SLOW_LIST)
  95. #define RESET_SLOW_LIST(t) (t->flags&=~F_TIMER_ON_SLOW_LIST)
  96. #define IS_ON_SLOW_LIST(t) (t->flags&F_TIMER_ON_SLOW_LIST)
  97. #define SLOW_LISTS_NO 1024U /* slow lists number, 2^k recommended */
  98. static gen_lock_t* slow_timer_lock; /* slow timer lock */
  99. static struct timer_head* slow_timer_lists;
  100. static volatile unsigned short* t_idx; /* "main" timer index in slow_lists[] */
  101. static volatile unsigned short* s_idx; /* "slow" timer index in slow_lists[] */
  102. static struct timer_ln* volatile* running_timer2=0; /* timer handler running
  103. in the "slow" timer */
  104. static sigset_t slow_timer_sset;
  105. pid_t slow_timer_pid;
  106. static int in_slow_timer=0;
  107. #define IS_IN_TIMER_SLOW() (in_slow_timer)
  108. #define SET_RUNNING_SLOW(t) (*running_timer2=(t))
  109. #define IS_RUNNING_SLOW(t) (*running_timer2==(t))
  110. #define UNSET_RUNNING_SLOW() (*running_timer2=0)
  111. #define LOCK_SLOW_TIMER_LIST() lock_get(slow_timer_lock)
  112. #define UNLOCK_SLOW_TIMER_LIST() lock_release(slow_timer_lock)
  113. #endif
  114. struct timer_lists* timer_lst=0;
  115. void sig_timer(int signo)
  116. {
  117. (*ticks)++;
  118. if (( *ticks % TIMER_HANDLER_INTERVAL)==0){
  119. /* set a flag to run the handler */
  120. run_timer=1;
  121. }
  122. }
  123. void destroy_timer()
  124. {
  125. struct itimerval it;
  126. /* disable timer */
  127. memset(&it, 0, sizeof(it));
  128. setitimer(ITIMER_REAL, &it, 0);
  129. set_sig_h(SIGALRM, SIG_IGN);
  130. if (timer_lock){
  131. lock_destroy(timer_lock);
  132. lock_dealloc(timer_lock);
  133. timer_lock=0;
  134. }
  135. if (ticks){
  136. #ifdef SHM_MEM
  137. shm_free(ticks);
  138. #else
  139. pkg_free(ticks);
  140. #endif
  141. ticks=0;
  142. }
  143. if (timer_lst){
  144. #ifdef SHM_MEM
  145. shm_free(timer_lst);
  146. #else
  147. pkg_free(timer_lst);
  148. #endif
  149. timer_lst=0;
  150. }
  151. if (running_timer){
  152. shm_free((void*)running_timer);
  153. running_timer=0;
  154. }
  155. #ifdef USE_SLOW_TIMER
  156. if (slow_timer_lock){
  157. lock_destroy(slow_timer_lock);
  158. lock_dealloc(slow_timer_lock);
  159. slow_timer_lock=0;
  160. }
  161. if (slow_timer_lists){
  162. shm_free((void*)slow_timer_lists);
  163. slow_timer_lists=0;
  164. }
  165. if (t_idx){
  166. shm_free((void*)t_idx);
  167. t_idx=0;
  168. }
  169. if (s_idx){
  170. shm_free((void*)s_idx);
  171. s_idx=0;
  172. }
  173. if(running_timer2){
  174. shm_free((void*)running_timer2);
  175. running_timer2=0;
  176. }
  177. #endif
  178. }
  179. /* ret 0 on success, <0 on error*/
  180. int init_timer()
  181. {
  182. int r;
  183. int ret;
  184. ret=-1;
  185. /* init the locks */
  186. timer_lock=lock_alloc();
  187. if (timer_lock==0){
  188. ret=E_OUT_OF_MEM;
  189. goto error;
  190. }
  191. if (lock_init(timer_lock)==0){
  192. lock_dealloc(timer_lock);
  193. timer_lock=0;
  194. ret=-1;
  195. goto error;
  196. }
  197. /* init the shared structs */
  198. #ifdef SHM_MEM
  199. ticks=shm_malloc(sizeof(ticks_t));
  200. timer_lst=shm_malloc(sizeof(struct timer_lists));
  201. #else
  202. /* in this case get_ticks won't work! */
  203. LM_WARN("no shared memory support compiled in get_ticks won't work\n");
  204. ticks=pkg_malloc(sizeof(ticks_t));
  205. timer_lst=pkg_malloc(sizeof(struct timer_lists));
  206. #endif
  207. if (ticks==0){
  208. LM_CRIT("out of shared memory (ticks)\n");
  209. ret=E_OUT_OF_MEM;
  210. goto error;
  211. }
  212. if (timer_lst==0){
  213. LM_CRIT("out of shared memory (timer_lst)\n");
  214. ret=E_OUT_OF_MEM;
  215. goto error;
  216. }
  217. running_timer=shm_malloc(sizeof(struct timer_ln*));
  218. if (running_timer==0){
  219. LM_CRIT("out of memory (running_timer)\n");
  220. ret=E_OUT_OF_MEM;
  221. goto error;
  222. }
  223. /* initial values */
  224. memset(timer_lst, 0, sizeof(struct timer_lists));
  225. *ticks=random(); /* random value for start, for debugging */
  226. prev_ticks=last_ticks=last_adj_check=*ticks;
  227. *running_timer=0;
  228. if (gettimeofday(&start_time, 0)<0){
  229. LM_ERR("gettimeofday failed: %s [%d]\n", strerror(errno), errno);
  230. ret=-1;
  231. goto error;
  232. }
  233. last_time=start_time;
  234. DBG("init_timer: starting with *ticks=%u\n", (unsigned) *ticks);
  235. /* init timer structures */
  236. for (r=0; r<H0_ENTRIES; r++)
  237. _timer_init_list(&timer_lst->h0[r]);
  238. for (r=0; r<H1_ENTRIES; r++)
  239. _timer_init_list(&timer_lst->h1[r]);
  240. for (r=0; r<H2_ENTRIES; r++)
  241. _timer_init_list(&timer_lst->h2[r]);
  242. _timer_init_list(&timer_lst->expired);
  243. #ifdef USE_SLOW_TIMER
  244. /* init the locks */
  245. slow_timer_lock=lock_alloc();
  246. if (slow_timer_lock==0){
  247. ret=E_OUT_OF_MEM;
  248. goto error;
  249. }
  250. if (lock_init(slow_timer_lock)==0){
  251. lock_dealloc(slow_timer_lock);
  252. slow_timer_lock=0;
  253. ret=-1;
  254. goto error;
  255. }
  256. t_idx=shm_malloc(sizeof(*t_idx));
  257. s_idx=shm_malloc(sizeof(*s_idx));
  258. slow_timer_lists=shm_malloc(sizeof(struct timer_head)*SLOW_LISTS_NO);
  259. running_timer2=shm_malloc(sizeof(struct timer_ln*));
  260. if ((t_idx==0)||(s_idx==0) || (slow_timer_lists==0) ||(running_timer2==0)){
  261. LM_ERR("out of shared memory (slow)\n");
  262. ret=E_OUT_OF_MEM;
  263. goto error;
  264. }
  265. *t_idx=*s_idx=0;
  266. *running_timer2=0;
  267. for (r=0; r<SLOW_LISTS_NO; r++)
  268. _timer_init_list(&slow_timer_lists[r]);
  269. #endif
  270. DBG("init_timer: timer_list between %p and %p\n",
  271. &timer_lst->h0[0], &timer_lst->h2[H2_ENTRIES]);
  272. return 0;
  273. error:
  274. destroy_timer();
  275. return ret;
  276. }
  277. #ifdef USE_SLOW_TIMER
  278. /* arm the "slow" timer ( start it)
  279. * returns -1 on error
  280. * WARNING: use it in the same process as the timer
  281. * (the one using pause(); timer_handler()) or
  282. * change run_timer to a pointer in shared mem */
  283. int arm_slow_timer()
  284. {
  285. sigemptyset(&slow_timer_sset);
  286. sigaddset(&slow_timer_sset, SLOW_TIMER_SIG);
  287. again:
  288. if (sigprocmask(SIG_BLOCK, &slow_timer_sset, 0)==-1){
  289. if (errno==EINTR) goto again;
  290. LM_ERR("sigprocmask failed: %s [%d]}n", strerror(errno), errno);
  291. goto error;
  292. }
  293. #ifdef __OS_darwin
  294. /* workaround for darwin sigwait bug, see slow_timer_main() for more
  295. info (or grep __OS_darwin) */
  296. /* keep in sync wih main.c: sig_usr() - signals we are interested in */
  297. sigaddset(&slow_timer_sset, SIGINT);
  298. sigaddset(&slow_timer_sset, SIGTERM);
  299. sigaddset(&slow_timer_sset, SIGUSR1);
  300. sigaddset(&slow_timer_sset, SIGHUP);
  301. sigaddset(&slow_timer_sset, SIGCHLD);
  302. sigaddset(&slow_timer_sset, SIGALRM);
  303. #endif
  304. /* initialize the config framework */
  305. if (cfg_child_init()) goto error;
  306. return 0;
  307. error:
  308. return -1;
  309. }
  310. #endif
  311. /* arm the timer ( start it)
  312. * returns -1 on error
  313. * WARNING: use it in the same process as the timer
  314. * (the one using pause(); timer_handler()) or
  315. * change run_timer to a pointer in shared mem */
  316. int arm_timer()
  317. {
  318. struct itimerval it;
  319. /* init signal generation */
  320. it.it_interval.tv_sec=0;
  321. it.it_interval.tv_usec=1000000/TIMER_TICKS_HZ;
  322. it.it_value=it.it_interval;
  323. /* install the signal handler */
  324. if (set_sig_h(SIGALRM, sig_timer) == SIG_ERR ){
  325. LM_CRIT("SIGALRM signal handler cannot be installed: %s [%d]\n",
  326. strerror(errno), errno);
  327. return -1;
  328. }
  329. if (setitimer(ITIMER_REAL, &it, 0) == -1){
  330. LM_CRIT("setitimer failed: %s [%d]\n", strerror(errno), errno);
  331. return -1;
  332. }
  333. if (gettimeofday(&last_time, 0)<0){
  334. LM_ERR("gettimeofday failed: %s [%d]\n", strerror(errno), errno);
  335. return -1;
  336. }
  337. /* initialize the config framework */
  338. if (cfg_child_init()) return -1;
  339. return 0;
  340. }
  341. #ifdef DBG_ser_time
  342. /* debugging only */
  343. void check_ser_drift();
  344. #endif /* DBG_set_time */
  345. /* adjust the timer using the "real" time, each TIMER_RESYNC_TICKS, but only
  346. * if timer drift > TIMER_MAX_DRIFT
  347. * NOTES: - it will adjust time within TIMER_MAX_DRIFT from the "real"
  348. * elapsed time
  349. * - it will never decrease the *ticks, only increase it (monotonic)
  350. * - it works ok as long as the adjustment interval < MAX_TICKS_T
  351. * -- andrei
  352. */
  353. inline static void adjust_ticks(void)
  354. {
  355. struct timeval crt_time;
  356. long long diff_time;
  357. ticks_t diff_time_ticks;
  358. ticks_t diff_ticks_raw;
  359. s_ticks_t delta;
  360. /* fix ticks if necessary */
  361. if ((*ticks-last_adj_check)>=(ticks_t)TIMER_RESYNC_TICKS){
  362. #ifdef DBG_ser_time
  363. check_ser_drift();
  364. #endif /* DBG_ser_time */
  365. last_adj_check=*ticks;
  366. if (gettimeofday(&crt_time, 0)<0){
  367. LM_ERR("gettimeofday failed: %s [%d]\n", strerror(errno), errno);
  368. return; /* ignore */
  369. }
  370. diff_time=(long long)crt_time.tv_sec*1000000+crt_time.tv_usec-
  371. ((long long) last_time.tv_sec*1000000+last_time.tv_usec);
  372. if (diff_time<0){
  373. LM_WARN("time changed backwards %ld ms ignoring...\n",
  374. (long)(diff_time/1000));
  375. last_time=crt_time;
  376. last_ticks=*ticks;
  377. }else{
  378. diff_ticks_raw=*ticks-last_ticks;
  379. diff_time_ticks=(ticks_t)((diff_time*TIMER_TICKS_HZ)/1000000LL);
  380. delta=(s_ticks_t)(diff_time_ticks-diff_ticks_raw);
  381. if (delta<-1){
  382. LM_WARN("our timer runs faster then real-time"
  383. " (%lu ms / %u ticks our time .->"
  384. " %lu ms / %u ticks real time)\n",
  385. (unsigned long)(diff_ticks_raw*1000L/TIMER_TICKS_HZ),
  386. diff_ticks_raw,
  387. (unsigned long)(diff_time/1000), diff_time_ticks);
  388. last_time=crt_time;
  389. last_ticks=*ticks;
  390. }else{
  391. /* fix the ticks */
  392. if (delta>(s_ticks_t)TIMER_MAX_DRIFT){
  393. #ifndef TIMER_DEBUG
  394. if (delta > 2*(s_ticks_t)TIMER_MAX_DRIFT+1)
  395. #endif
  396. DBG("adjusting timer ticks (%lu) with %ld ms"
  397. " (%ld ticks)\n",
  398. (unsigned long)*ticks,
  399. (long)(delta*1000)/TIMER_TICKS_HZ, (long)delta);
  400. *ticks+=(ticks_t)delta;
  401. }else{
  402. /*DBG("incredible, but our timer is in sync with"
  403. " real time (%lu)\n", (unsigned long)*ticks);
  404. */
  405. }
  406. }
  407. }
  408. }
  409. }
  410. /* time(2) equivalent, using ser internal timers (faster then a syscall) */
  411. time_t ser_time(time_t *t)
  412. {
  413. if (likely(t==0))
  414. return last_time.tv_sec+TICKS_TO_S(*ticks-last_ticks);
  415. *t=last_time.tv_sec+TICKS_TO_S(*ticks-last_ticks);
  416. return *t;
  417. }
  418. /* gettimeofday(2) equivalent, using ser internal timers (faster
  419. * but more imprecise)
  420. * WARNING: ignores tz (it's obsolete anyway)*/
  421. int ser_gettimeofday(struct timeval* tv, struct timezone* tz)
  422. {
  423. if (likely(tv!=0)){
  424. tv->tv_sec=last_time.tv_sec+TICKS_TO_S(*ticks-last_ticks);
  425. tv->tv_usec=last_time.tv_usec+
  426. (TICKS_TO_MS(*ticks-last_ticks)%1000)*1000;
  427. }
  428. return 0;
  429. }
  430. #ifdef DBG_ser_time
  431. /* debugging only, remove */
  432. void check_ser_drift()
  433. {
  434. time_t t1, t2;
  435. struct timeval tv1, tv2;
  436. int r;
  437. t1=time(0);
  438. t2=ser_time(0);
  439. if (t1!=t2)
  440. BUG("time(0)!=ser_time(0) : %d != %d \n", (unsigned)t1, (unsigned)t2);
  441. r=gettimeofday(&tv1, 0);
  442. ser_gettimeofday(&tv2, 0);
  443. if (tv1.tv_sec!=tv2.tv_sec)
  444. BUG("gettimeofday seconds!=ser_gettimeofday seconds : %d != %d \n",
  445. (unsigned)tv1.tv_sec, (unsigned)tv2.tv_sec);
  446. else if ((tv1.tv_usec > tv2.tv_usec) &&
  447. (unsigned)(tv1.tv_usec-tv2.tv_usec)>100000)
  448. BUG("gettimeofday usecs > ser_gettimeofday with > 0.1s : %d ms\n",
  449. (unsigned)(tv1.tv_usec-tv2.tv_usec)/1000);
  450. else if ((tv1.tv_usec < tv2.tv_usec) &&
  451. (unsigned)(tv2.tv_usec-tv1.tv_usec)>100000)
  452. BUG("gettimeofday usecs < ser_gettimeofday with > 0.1s : %d ms\n",
  453. (unsigned)(tv2.tv_usec-tv1.tv_usec)/1000);
  454. }
  455. #endif /* DBG_ser_time */
  456. struct timer_ln* timer_alloc()
  457. {
  458. return shm_malloc(sizeof(struct timer_ln));
  459. }
  460. void timer_free(struct timer_ln* t)
  461. {
  462. shm_free(t);
  463. }
  464. /* unsafe (no lock ) timer add function
  465. * t = current ticks
  466. * tl must be filled (the intial_timeout and flags must be set)
  467. * returns -1 on error, 0 on success */
  468. static inline int _timer_add(ticks_t t, struct timer_ln* tl)
  469. {
  470. ticks_t delta;
  471. #ifdef USE_SLOW_TIMER
  472. tl->flags&=~((unsigned short)F_TIMER_ON_SLOW_LIST);
  473. tl->slow_idx=0;
  474. #endif
  475. delta=tl->initial_timeout;
  476. tl->expire=t+delta;
  477. return _timer_dist_tl(tl, delta);
  478. }
  479. /* "public", safe timer add functions
  480. * adds a timer at delta ticks from the current time
  481. * returns -1 on error, 0 on success
  482. * WARNING: to re-add an expired or deleted timer you must call
  483. * timer_reinit(tl) prior to timer_add
  484. * The default behaviour allows timer_add to add a timer only if it
  485. * has never been added before.
  486. */
  487. #ifdef TIMER_DEBUG
  488. int timer_add_safe(struct timer_ln* tl, ticks_t delta,
  489. const char* file, const char* func, unsigned line)
  490. #else
  491. int timer_add_safe(struct timer_ln* tl, ticks_t delta)
  492. #endif
  493. {
  494. int ret;
  495. LOCK_TIMER_LIST();
  496. if (tl->flags & F_TIMER_ACTIVE){
  497. #ifdef TIMER_DEBUG
  498. LOG(timerlog, "timer_add called on an active timer %p (%p, %p),"
  499. " flags %x\n", tl, tl->next, tl->prev, tl->flags);
  500. LOG(timerlog, "WARN: -timer_add-; called from %s(%s):%d\n",
  501. func, file, line);
  502. LOG(timerlog, "WARN: -timer_add-: added %d times"
  503. ", last from: %s(%s):%d, deleted %d times"
  504. ", last from: %s(%s):%d, init %d times, expired %d \n",
  505. tl->add_calls, tl->add_func, tl->add_file, tl->add_line,
  506. tl->del_calls, tl->del_func, tl->del_file, tl->del_line,
  507. tl->init, tl->expires_no);
  508. #else
  509. DBG("timer_add called on an active timer %p (%p, %p),"
  510. " flags %x\n", tl, tl->next, tl->prev, tl->flags);
  511. #endif
  512. ret=-1; /* refusing to add active or non-reinit. timer */
  513. goto error;
  514. }
  515. tl->initial_timeout=delta;
  516. if ((tl->next!=0) || (tl->prev!=0)){
  517. LM_CRIT("timer_add: called with linked timer: %p (%p, %p)\n",
  518. tl, tl->next, tl->prev);
  519. ret=-1;
  520. goto error;
  521. }
  522. tl->flags|=F_TIMER_ACTIVE;
  523. #ifdef TIMER_DEBUG
  524. tl->add_file=file;
  525. tl->add_func=func;
  526. tl->add_line=line;
  527. tl->add_calls++;
  528. #endif
  529. ret=_timer_add(*ticks, tl);
  530. error:
  531. UNLOCK_TIMER_LIST();
  532. return ret;
  533. }
  534. /* safe timer delete
  535. * deletes tl and inits the list pointer to 0
  536. * returns <0 on error (-1 if timer not active/already deleted and -2 if
  537. * delete attempted from the timer handler) and 0 on success
  538. */
  539. #ifdef TIMER_DEBUG
  540. int timer_del_safe(struct timer_ln* tl,
  541. const char* file, const char* func, unsigned line)
  542. #else
  543. int timer_del_safe(struct timer_ln* tl)
  544. #endif
  545. {
  546. int ret;
  547. ret=-1;
  548. again:
  549. /* quick exit if timer inactive */
  550. if ( !(tl->flags & F_TIMER_ACTIVE)){
  551. #ifdef TIMER_DEBUG
  552. LOG(timerlog, "timer_del called on an inactive timer %p (%p, %p),"
  553. " flags %x\n", tl, tl->next, tl->prev, tl->flags);
  554. LOG(timerlog, "WARN: -timer_del-; called from %s(%s):%d\n",
  555. func, file, line);
  556. LOG(timerlog, "WARN: -timer_del-: added %d times"
  557. ", last from: %s(%s):%d, deleted %d times"
  558. ", last from: %s(%s):%d, init %d times, expired %d \n",
  559. tl->add_calls, tl->add_func, tl->add_file, tl->add_line,
  560. tl->del_calls, tl->del_func, tl->del_file, tl->del_line,
  561. tl->init, tl->expires_no);
  562. #else
  563. /*
  564. DBG("timer_del called on an inactive timer %p (%p, %p),"
  565. " flags %x\n", tl, tl->next, tl->prev, tl->flags);
  566. */
  567. #endif
  568. return -1;
  569. }
  570. #ifdef USE_SLOW_TIMER
  571. if (IS_ON_SLOW_LIST(tl) && (tl->slow_idx!=*t_idx)){
  572. LOCK_SLOW_TIMER_LIST();
  573. if (!IS_ON_SLOW_LIST(tl) || (tl->slow_idx==*t_idx)){
  574. UNLOCK_SLOW_TIMER_LIST();
  575. goto again;
  576. }
  577. if (IS_RUNNING_SLOW(tl)){
  578. UNLOCK_SLOW_TIMER_LIST();
  579. if (IS_IN_TIMER_SLOW()){
  580. /* if somebody tries to shoot himself in the foot,
  581. * warn him and ignore the delete */
  582. LM_CRIT("timer handle %p (s) tried to delete"
  583. " itself\n", tl);
  584. #ifdef TIMER_DEBUG
  585. LOG(timerlog, "WARN: -timer_del-: called from %s(%s):%d\n",
  586. func, file, line);
  587. LOG(timerlog, "WARN: -timer_del-: added %d times"
  588. ", last from: %s(%s):%d, deleted %d times"
  589. ", last from: %s(%s):%d, init %d times, expired %d \n",
  590. tl->add_calls, tl->add_func, tl->add_file,
  591. tl->add_line, tl->del_calls, tl->del_func,
  592. tl->del_file, tl->del_line, tl->init, tl->expires_no);
  593. #endif
  594. return -2; /* do nothing */
  595. }
  596. sched_yield(); /* wait for it to complete */
  597. goto again;
  598. }
  599. if (tl->next!=0){
  600. _timer_rm_list(tl); /* detach */
  601. tl->next=tl->prev=0;
  602. ret=0;
  603. #ifdef TIMER_DEBUG
  604. tl->del_file=file;
  605. tl->del_func=func;
  606. tl->del_line=line;
  607. tl->flags|=F_TIMER_DELETED;
  608. #endif
  609. }else{
  610. #ifdef TIMER_DEBUG
  611. LOG(timerlog, "timer_del: (s) timer %p (%p, %p) flags %x "
  612. "already detached\n",
  613. tl, tl->next, tl->prev, tl->flags);
  614. LOG(timerlog, "WARN: -timer_del-: @%d tl=%p "
  615. "{ %p, %p, %d, %d, %p, %p, %04x, -}\n", get_ticks_raw(),
  616. tl, tl->next, tl->prev, tl->expire, tl->initial_timeout,
  617. tl->data, tl->f, tl->flags);
  618. LOG(timerlog, "WARN: -timer_del-; called from %s(%s):%d\n",
  619. func, file, line);
  620. LOG(timerlog, "WARN: -timer_del-: added %d times"
  621. ", last from: %s(%s):%d, deleted %d times"
  622. ", last from: %s(%s):%d, init %d times, expired %d \n",
  623. tl->add_calls,
  624. tl->add_func, tl->add_file, tl->add_line,
  625. tl->del_calls,
  626. tl->del_func, tl->del_file, tl->del_line,
  627. tl->init, tl->expires_no);
  628. #else
  629. /*
  630. DBG("timer_del: (s) timer %p (%p, %p) flags %x "
  631. "already detached\n",
  632. tl, tl->next, tl->prev, tl->flags);
  633. */
  634. #endif
  635. ret=-1;
  636. }
  637. UNLOCK_SLOW_TIMER_LIST();
  638. }else{
  639. #endif
  640. LOCK_TIMER_LIST();
  641. #ifdef USE_SLOW_TIMER
  642. if (IS_ON_SLOW_LIST(tl) && (tl->slow_idx!=*t_idx)){
  643. UNLOCK_TIMER_LIST();
  644. goto again;
  645. }
  646. #endif
  647. if (IS_RUNNING(tl)){
  648. UNLOCK_TIMER_LIST();
  649. if (IS_IN_TIMER()){
  650. /* if somebody tries to shoot himself in the foot,
  651. * warn him and ignore the delete */
  652. LM_CRIT("timer handle %p tried to delete"
  653. " itself\n", tl);
  654. #ifdef TIMER_DEBUG
  655. LOG(timerlog, "WARN: -timer_del-: called from %s(%s):%d\n",
  656. func, file, line);
  657. LOG(timerlog, "WARN: -timer_del-: added %d times"
  658. ", last from: %s(%s):%d, deleted %d times"
  659. ", last from: %s(%s):%d, init %d times, expired %d \n",
  660. tl->add_calls, tl->add_func, tl->add_file,
  661. tl->add_line, tl->del_calls, tl->del_func,
  662. tl->del_file, tl->del_line, tl->init, tl->expires_no);
  663. #endif
  664. return -2; /* do nothing */
  665. }
  666. sched_yield(); /* wait for it to complete */
  667. goto again;
  668. }
  669. if ((tl->next!=0)&&(tl->prev!=0)){
  670. _timer_rm_list(tl); /* detach */
  671. tl->next=tl->prev=0;
  672. ret=0;
  673. #ifdef TIMER_DEBUG
  674. tl->del_file=file;
  675. tl->del_func=func;
  676. tl->del_line=line;
  677. tl->flags|=F_TIMER_DELETED;
  678. #endif
  679. }else{
  680. #ifdef TIMER_DEBUG
  681. LOG(timerlog, "timer_del: (f) timer %p (%p, %p) flags %x "
  682. "already detached\n",
  683. tl, tl->next, tl->prev, tl->flags);
  684. LOG(timerlog, "WARN: -timer_del-: @%d tl=%p "
  685. "{ %p, %p, %d, %d, %p, %p, %04x, -}\n", get_ticks_raw(),
  686. tl, tl->next, tl->prev, tl->expire, tl->initial_timeout,
  687. tl->data, tl->f, tl->flags);
  688. LOG(timerlog, "WARN: -timer_del-; called from %s(%s):%d\n",
  689. func, file, line);
  690. LOG(timerlog, "WARN: -timer_del-: added %d times"
  691. ", last from: %s(%s):%d, deleted %d times"
  692. ", last from: %s(%s):%d, init %d times, expired %d \n",
  693. tl->add_calls,
  694. tl->add_func, tl->add_file, tl->add_line,
  695. tl->del_calls,
  696. tl->del_func, tl->del_file, tl->del_line,
  697. tl->init, tl->expires_no);
  698. #else
  699. /*
  700. DBG("timer_del: (f) timer %p (%p, %p) flags %x "
  701. "already detached\n",
  702. tl, tl->next, tl->prev, tl->flags);
  703. */
  704. #endif
  705. ret=-1;
  706. }
  707. UNLOCK_TIMER_LIST();
  708. #ifdef USE_SLOW_TIMER
  709. }
  710. #endif
  711. return ret;
  712. }
  713. /* marks a timer as "to be deleted when the handler ends", usefull when
  714. * the timer handler knows it won't prolong the timer anymore (it will
  715. * return 0) and will do some time consuming work. Calling this function
  716. * will cause simultaneous timer_dels to return immediately (they won't
  717. * wait anymore for the timer handle to finish). It will also allow
  718. * self-deleting from the timer handle without bug reports.
  719. * WARNING: - if you rely on timer_del to know when the timer handle execution
  720. * finishes (e.g. to free resources used in the timer handle), don't
  721. * use this function.
  722. * - this function can be called only from a timer handle (in timer
  723. * context), all other calls will have no effect and will log a
  724. * bug message
  725. */
  726. void timer_allow_del(void)
  727. {
  728. if (IS_IN_TIMER() ){
  729. UNSET_RUNNING();
  730. }else
  731. #ifdef USE_SLOW_TIMER
  732. if (IS_IN_TIMER_SLOW()){
  733. UNSET_RUNNING_SLOW();
  734. }else
  735. #endif
  736. LM_CRIT("timer_allow_del called outside a timer handle\n");
  737. }
  738. /* called from timer_handle, must be called with the timer lock held
  739. * WARNING: expired one shot timers are _not_ automatically reinit
  740. * (because they could have been already freed from the timer
  741. * handler so a reinit would not be safe!) */
  742. inline static void timer_list_expire(ticks_t t, struct timer_head* h
  743. #ifdef USE_SLOW_TIMER
  744. , struct timer_head* slow_l,
  745. slow_idx_t slow_mark
  746. #endif
  747. )
  748. {
  749. struct timer_ln * tl;
  750. ticks_t ret;
  751. #ifdef TIMER_DEBUG
  752. struct timer_ln* first;
  753. int i=0;
  754. first=h->next;
  755. #endif
  756. /*DBG("timer_list_expire @ ticks = %lu, list =%p\n",
  757. (unsigned long) *ticks, h);
  758. */
  759. while(h->next!=(struct timer_ln*)h){
  760. tl=h->next;
  761. #ifdef TIMER_DEBUG /* FIXME: replace w/ EXTRA_DEBUG */
  762. if (tl==0){
  763. LM_CRIT("timer_list_expire: tl=%p, h=%p {%p, %p}\n",
  764. tl, h, h->next, h->prev);
  765. abort();
  766. }else if((tl->next==0) || (tl->prev==0)){
  767. LM_CRIT("timer_list_expire: @%d tl=%p "
  768. "{ %p, %p, %d, %d, %p, %p, %04x, -},"
  769. " h=%p {%p, %p}\n", t,
  770. tl, tl->next, tl->prev, tl->expire, tl->initial_timeout,
  771. tl->data, tl->f, tl->flags,
  772. h, h->next, h->prev);
  773. LM_CRIT("-timer_list_expire-: cycle %d, first %p,"
  774. "running %p\n", i, first, *running_timer);
  775. LM_CRIT("-timer_list_expire-: added %d times"
  776. ", last from: %s(%s):%d, deleted %d times"
  777. ", last from: %s(%s):%d, init %d times, expired %d \n",
  778. tl->add_calls,
  779. tl->add_func, tl->add_file, tl->add_line,
  780. tl->del_calls,
  781. tl->del_func, tl->del_file, tl->del_line,
  782. tl->init, tl->expires_no);
  783. abort();
  784. }
  785. i++;
  786. #endif
  787. _timer_rm_list(tl); /* detach */
  788. #ifdef USE_SLOW_TIMER
  789. if (IS_FAST_TIMER(tl)){
  790. #endif
  791. /* if fast timer */
  792. SET_RUNNING(tl);
  793. tl->next=tl->prev=0; /* debugging */
  794. #ifdef TIMER_DEBUG
  795. tl->expires_no++;
  796. #endif
  797. UNLOCK_TIMER_LIST(); /* acts also as write barrier */
  798. ret=tl->f(t, tl, tl->data);
  799. /* reset the configuration group handles */
  800. cfg_reset_all();
  801. if (ret==0){
  802. UNSET_RUNNING();
  803. LOCK_TIMER_LIST();
  804. }else{
  805. /* not one-shot, re-add it */
  806. LOCK_TIMER_LIST();
  807. if (ret!=(ticks_t)-1) /* ! periodic */
  808. tl->initial_timeout=ret;
  809. _timer_add(t, tl);
  810. UNSET_RUNNING();
  811. }
  812. #ifdef USE_SLOW_TIMER
  813. }else{
  814. /* slow timer */
  815. SET_SLOW_LIST(tl);
  816. tl->slow_idx=slow_mark; /* current index */
  817. /* overflow check in timer_handler*/
  818. _timer_add_list(slow_l, tl);
  819. }
  820. #endif
  821. }
  822. }
  823. /* "main" timer routine
  824. * WARNING: it should never be called twice for the same *ticks value
  825. * (it could cause too fast expires for long timers), *ticks must be also
  826. * always increasing */
  827. static void timer_handler(void)
  828. {
  829. ticks_t saved_ticks;
  830. #ifdef USE_SLOW_TIMER
  831. int run_slow_timer;
  832. int i;
  833. run_slow_timer=0;
  834. i=(slow_idx_t)(*t_idx%SLOW_LISTS_NO);
  835. #endif
  836. /*DBG("timer_handler: called, ticks=%lu, prev_ticks=%lu\n",
  837. (unsigned long)*ticks, (unsigned long)prev_ticks);
  838. */
  839. run_timer=0; /* reset run_timer */
  840. adjust_ticks();
  841. LOCK_TIMER_LIST();
  842. do{
  843. saved_ticks=*ticks; /* protect against time running backwards */
  844. if (prev_ticks>=saved_ticks){
  845. LM_CRIT("backwards or still time\n");
  846. /* try to continue */
  847. prev_ticks=saved_ticks-1;
  848. break;
  849. }
  850. /* go through all the "missed" ticks, taking a possible overflow
  851. * into account */
  852. for (prev_ticks=prev_ticks+1; prev_ticks!=saved_ticks; prev_ticks++)
  853. timer_run(prev_ticks);
  854. timer_run(prev_ticks); /* do it for saved_ticks too */
  855. }while(saved_ticks!=*ticks); /* in case *ticks changed */
  856. #ifdef USE_SLOW_TIMER
  857. timer_list_expire(*ticks, &timer_lst->expired, &slow_timer_lists[i],
  858. *t_idx);
  859. #else
  860. timer_list_expire(*ticks, &timer_lst->expired);
  861. #endif
  862. /* WARNING: add_timer(...,0) must go directly to expired list, since
  863. * otherwise there is a race between timer running and adding it
  864. * (it could expire it H0_ENTRIES ticks later instead of 'now')*/
  865. #ifdef USE_SLOW_TIMER
  866. if (slow_timer_lists[i].next!=(struct timer_ln*)&slow_timer_lists[i]){
  867. run_slow_timer=1;
  868. if ((slow_idx_t)(*t_idx-*s_idx) < (SLOW_LISTS_NO-1U))
  869. (*t_idx)++;
  870. else{
  871. LM_WARN("slow timer too slow: overflow (%d - %d = %d)\n",
  872. *t_idx, *s_idx, *t_idx-*s_idx);
  873. /* trying to continue */
  874. }
  875. }
  876. #endif
  877. UNLOCK_TIMER_LIST();
  878. #ifdef USE_SLOW_TIMER
  879. /* wake up the "slow" timer */
  880. if (run_slow_timer)
  881. kill(slow_timer_pid, SLOW_TIMER_SIG);
  882. #endif
  883. }
  884. /* main timer function, never exists */
  885. void timer_main()
  886. {
  887. in_timer=1; /* mark this process as the fast timer */
  888. while(1){
  889. if (run_timer){
  890. /* update the local cfg if needed */
  891. cfg_update();
  892. timer_handler();
  893. }
  894. pause();
  895. }
  896. }
  897. /* generic call back for the old style timer functions */
  898. static ticks_t compat_old_handler(ticks_t ti, struct timer_ln* tl,
  899. void * data)
  900. {
  901. struct sr_timer* t;
  902. #ifdef TIMER_DEBUG
  903. DBG("timer: compat_old_handler: calling, ticks=%u/%u, tl=%p, t=%p\n",
  904. prev_ticks, (unsigned)*ticks, tl, data);
  905. #endif
  906. t=(struct sr_timer*)data;
  907. t->timer_f(TICKS_TO_S(*ticks), t->t_param);
  908. return (ticks_t)-1; /* periodic */
  909. }
  910. /* register a periodic timer;
  911. * compatibility mode.w/ the old timer interface...
  912. * ret: <0 on error
  913. * Hint: if you need it in a module, register it from mod_init or it
  914. * won't work otherwise*/
  915. int register_timer(timer_function f, void* param, unsigned int interval)
  916. {
  917. struct sr_timer* t;
  918. t=shm_malloc(sizeof(struct sr_timer));
  919. if (t==0){
  920. LM_ERR("out of memory\n");
  921. goto error;
  922. }
  923. t->id=timer_id++;
  924. t->timer_f=f;
  925. t->t_param=param;
  926. timer_init(&t->tl, compat_old_handler, t, 0); /* is slow */
  927. if (timer_add(&t->tl, S_TO_TICKS(interval))!=0){
  928. LM_ERR("timer_add failed\n");
  929. return -1;
  930. }
  931. return t->id;
  932. error:
  933. return E_OUT_OF_MEM;
  934. }
  935. ticks_t get_ticks_raw()
  936. {
  937. #ifndef SHM_MEM
  938. LM_CRIT("no shared memory support compiled in"
  939. ", returning 0 (probably wrong)");
  940. return 0;
  941. #endif
  942. return *ticks;
  943. }
  944. /* returns tick in s (for compatibility with the old code) */
  945. ticks_t get_ticks()
  946. {
  947. #ifndef SHM_MEM
  948. LM_CRIT("no shared memory support compiled in"
  949. ", returning 0 (probably wrong)");
  950. return 0;
  951. #endif
  952. return TICKS_TO_S(*ticks);
  953. }
  954. #ifdef USE_SLOW_TIMER
  955. /* slow timer main function, never exists
  956. * This function is intended to be executed in a special separated process
  957. * (the "slow" timer) which will run the timer handlers of all the registered
  958. * timers not marked as "fast". The ideea is to execute the fast timers in the
  959. * "main" timer process, as accurate as possible and defer the execution of the
  960. * timers marked as "slow" to the "slow" timer.
  961. * Implementation details:
  962. * - it waits for a signal and then wakes up and processes
  963. * all the lists in slow_timer_lists from [s_idx, t_idx). It will
  964. * -it increments *s_idx (at the end it will be == *t_idx)
  965. * -all list operations are protected by the "slow" timer lock
  966. */
  967. #ifdef __OS_darwin
  968. extern void sig_usr(int signo);
  969. #endif
  970. void slow_timer_main()
  971. {
  972. int n;
  973. ticks_t ret;
  974. struct timer_ln* tl;
  975. unsigned short i;
  976. #ifdef USE_SIGWAIT
  977. int sig;
  978. #endif
  979. in_slow_timer=1; /* mark this process as the slow timer */
  980. while(1){
  981. #ifdef USE_SIGWAIT
  982. n=sigwait(&slow_timer_sset, &sig);
  983. #else
  984. n=sigwaitinfo(&slow_timer_sset, 0);
  985. #endif
  986. if (n==-1){
  987. if (errno==EINTR) continue; /* some other signal, ignore it */
  988. LM_ERR("sigwaitinfo failed: %s [%d]\n", strerror(errno), errno);
  989. sleep(1);
  990. /* try to continue */
  991. }
  992. #ifdef USE_SIGWAIT
  993. if (sig!=SLOW_TIMER_SIG){
  994. #ifdef __OS_darwin
  995. /* on darwin sigwait is buggy: it will cause extreme slow down
  996. on signal delivery for the signals it doesn't wait on
  997. (on darwin 8.8.0, g4 1.5Ghz I've measured a 36s delay!).
  998. To work arround this bug, we sigwait() on all the signals we
  999. are interested in ser and manually call the master signal handler
  1000. if the signal!= slow timer signal -- andrei */
  1001. sig_usr(sig);
  1002. #endif
  1003. continue;
  1004. }
  1005. #endif
  1006. /* update the local cfg if needed */
  1007. cfg_update();
  1008. LOCK_SLOW_TIMER_LIST();
  1009. while(*s_idx!=*t_idx){
  1010. i= *s_idx%SLOW_LISTS_NO;
  1011. while(slow_timer_lists[i].next!=
  1012. (struct timer_ln*)&slow_timer_lists[i]){
  1013. tl=slow_timer_lists[i].next;
  1014. _timer_rm_list(tl);
  1015. tl->next=tl->prev=0;
  1016. #ifdef TIMER_DEBUG
  1017. tl->expires_no++;
  1018. #endif
  1019. SET_RUNNING_SLOW(tl);
  1020. UNLOCK_SLOW_TIMER_LIST();
  1021. ret=tl->f(*ticks, tl, tl->data);
  1022. /* reset the configuration group handles */
  1023. cfg_reset_all();
  1024. if (ret==0){
  1025. /* one shot */
  1026. UNSET_RUNNING_SLOW();
  1027. LOCK_SLOW_TIMER_LIST();
  1028. }else{
  1029. /* not one shot, re-add it */
  1030. LOCK_TIMER_LIST(); /* add it to the "main" list */
  1031. RESET_SLOW_LIST(tl);
  1032. if (ret!=(ticks_t)-1) /* != periodic */
  1033. tl->initial_timeout=ret;
  1034. _timer_add(*ticks, tl);
  1035. UNLOCK_TIMER_LIST();
  1036. LOCK_SLOW_TIMER_LIST();
  1037. UNSET_RUNNING_SLOW();
  1038. }
  1039. }
  1040. (*s_idx)++;
  1041. }
  1042. UNLOCK_SLOW_TIMER_LIST();
  1043. }
  1044. }
  1045. #endif