2
0

timer.c 31 KB

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