sched.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Asterisk
  3. *
  4. * Mark Spencer <markster@marko.net>
  5. *
  6. * Copyright(C) Mark Spencer
  7. *
  8. * Distributed under the terms of the GNU General Public License (GPL) Version 2
  9. *
  10. * Scheduler Routines (form cheops-NG)
  11. *
  12. */
  13. #ifdef DEBUG_SCHEDULER
  14. #define DEBUG(a) DEBUG_M(a)
  15. #else
  16. #define DEBUG(a)
  17. #endif
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <sys/time.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <asterisk/sched.h>
  24. #include <asterisk/logger.h>
  25. #include <asterisk/channel.h>
  26. #include <asterisk/lock.h>
  27. /* Determine if a is sooner than b */
  28. #define SOONER(a,b) (((b).tv_sec > (a).tv_sec) || \
  29. (((b).tv_sec == (a).tv_sec) && ((b).tv_usec > (a).tv_usec)))
  30. struct sched {
  31. struct sched *next; /* Next event in the list */
  32. int id; /* ID number of event */
  33. struct timeval when; /* Absolute time event should take place */
  34. int resched; /* When to reschedule */
  35. void *data; /* Data */
  36. ast_sched_cb callback; /* Callback */
  37. };
  38. struct sched_context {
  39. ast_mutex_t lock;
  40. /* Number of events processed */
  41. int eventcnt;
  42. /* Number of outstanding schedule events */
  43. int schedcnt;
  44. /* Schedule entry and main queue */
  45. struct sched *schedq;
  46. #ifdef SCHED_MAX_CACHE
  47. /* Cache of unused schedule structures and how many */
  48. struct sched *schedc;
  49. int schedccnt;
  50. #endif
  51. };
  52. struct sched_context *sched_context_create(void)
  53. {
  54. struct sched_context *tmp;
  55. tmp = malloc(sizeof(struct sched_context));
  56. if (tmp) {
  57. memset(tmp, 0, sizeof(struct sched_context));
  58. ast_mutex_init(&tmp->lock);
  59. tmp->eventcnt = 1;
  60. tmp->schedcnt = 0;
  61. tmp->schedq = NULL;
  62. #ifdef SCHED_MAX_CACHE
  63. tmp->schedc = NULL;
  64. tmp->schedccnt = 0;
  65. #endif
  66. }
  67. return tmp;
  68. }
  69. void sched_context_destroy(struct sched_context *con)
  70. {
  71. struct sched *s, *sl;
  72. ast_mutex_lock(&con->lock);
  73. #ifdef SCHED_MAX_CACHE
  74. /* Eliminate the cache */
  75. s = con->schedc;
  76. while(s) {
  77. sl = s;
  78. s = s->next;
  79. free(sl);
  80. }
  81. #endif
  82. /* And the queue */
  83. s = con->schedq;
  84. while(s) {
  85. sl = s;
  86. s = s->next;
  87. free(sl);
  88. }
  89. /* And the context */
  90. ast_mutex_unlock(&con->lock);
  91. ast_mutex_destroy(&con->lock);
  92. free(con);
  93. }
  94. static struct sched *sched_alloc(struct sched_context *con)
  95. {
  96. /*
  97. * We keep a small cache of schedule entries
  98. * to minimize the number of necessary malloc()'s
  99. */
  100. struct sched *tmp;
  101. #ifdef SCHED_MAX_CACHE
  102. if (con->schedc) {
  103. tmp = con->schedc;
  104. con->schedc = con->schedc->next;
  105. con->schedccnt--;
  106. } else
  107. #endif
  108. tmp = malloc(sizeof(struct sched));
  109. return tmp;
  110. }
  111. static void sched_release(struct sched_context *con, struct sched *tmp)
  112. {
  113. /*
  114. * Add to the cache, or just free() if we
  115. * already have too many cache entries
  116. */
  117. #ifdef SCHED_MAX_CACHE
  118. if (con->schedccnt < SCHED_MAX_CACHE) {
  119. tmp->next = con->schedc;
  120. con->schedc = tmp;
  121. con->schedccnt++;
  122. } else
  123. #endif
  124. free(tmp);
  125. }
  126. int ast_sched_wait(struct sched_context *con)
  127. {
  128. /*
  129. * Return the number of milliseconds
  130. * until the next scheduled event
  131. */
  132. struct timeval tv;
  133. int ms;
  134. DEBUG(ast_log(LOG_DEBUG, "ast_sched_wait()\n"));
  135. ast_mutex_lock(&con->lock);
  136. if (!con->schedq) {
  137. ms = -1;
  138. } else if (gettimeofday(&tv, NULL) < 0) {
  139. /* This should never happen */
  140. ms = 0;
  141. } else {
  142. ms = (con->schedq->when.tv_sec - tv.tv_sec) * 1000;
  143. ms += (con->schedq->when.tv_usec - tv.tv_usec) / 1000;
  144. if (ms < 0)
  145. ms = 0;
  146. }
  147. ast_mutex_unlock(&con->lock);
  148. return ms;
  149. }
  150. static void schedule(struct sched_context *con, struct sched *s)
  151. {
  152. /*
  153. * Take a sched structure and put it in the
  154. * queue, such that the soonest event is
  155. * first in the list.
  156. */
  157. struct sched *last=NULL;
  158. struct sched *current=con->schedq;
  159. while(current) {
  160. if (SOONER(s->when, current->when))
  161. break;
  162. last = current;
  163. current = current->next;
  164. }
  165. /* Insert this event into the schedule */
  166. s->next = current;
  167. if (last)
  168. last->next = s;
  169. else
  170. con->schedq = s;
  171. con->schedcnt++;
  172. }
  173. static inline int sched_settime(struct timeval *tv, int when)
  174. {
  175. struct timeval tv_tmp;
  176. long error_sec, error_usec;
  177. if (gettimeofday(&tv_tmp, NULL) < 0) {
  178. /* This shouldn't ever happen, but let's be sure */
  179. ast_log(LOG_NOTICE, "gettimeofday() failed!\n");
  180. return -1;
  181. }
  182. /*ast_log(LOG_DEBUG, "TV -> %lu,%lu\n", tv->tv_sec, tv->tv_usec);*/
  183. if (((unsigned long)(tv->tv_sec) > 0)||((unsigned long)(tv->tv_usec) > 0)) {
  184. if ((unsigned long)(tv_tmp.tv_usec) < (unsigned long)(tv->tv_usec)) {
  185. tv_tmp.tv_usec += 1000000;
  186. tv_tmp.tv_sec -= 1;
  187. }
  188. error_sec = (unsigned long)(tv_tmp.tv_sec) - (unsigned long)(tv->tv_sec);
  189. error_usec = (unsigned long)(tv_tmp.tv_usec) - (unsigned long)(tv->tv_usec);
  190. } else {
  191. /*ast_log(LOG_DEBUG, "Initializing error\n");*/
  192. error_sec = 0;
  193. error_usec = 0;
  194. }
  195. /*ast_log(LOG_DEBUG, "ERROR -> %lu,%lu\n", error_sec, error_usec);*/
  196. if (error_sec * 1000 + error_usec / 1000 < when) {
  197. tv->tv_sec = tv_tmp.tv_sec + (when/1000 - error_sec);
  198. tv->tv_usec = tv_tmp.tv_usec + ((when % 1000) * 1000 - error_usec);
  199. } else {
  200. ast_log(LOG_DEBUG, "Request to schedule in the past?!?!\n");
  201. tv->tv_sec = tv_tmp.tv_sec;
  202. tv->tv_usec = tv_tmp.tv_usec;
  203. }
  204. if (tv->tv_usec > 1000000) {
  205. tv->tv_sec++;
  206. tv->tv_usec-= 1000000;
  207. }
  208. return 0;
  209. }
  210. int ast_sched_add(struct sched_context *con, int when, ast_sched_cb callback, void *data)
  211. {
  212. /*
  213. * Schedule callback(data) to happen when ms into the future
  214. */
  215. struct sched *tmp;
  216. int res = -1;
  217. DEBUG(ast_log(LOG_DEBUG, "ast_sched_add()\n"));
  218. if (!when) {
  219. ast_log(LOG_NOTICE, "Scheduled event in 0 ms?\n");
  220. return -1;
  221. }
  222. ast_mutex_lock(&con->lock);
  223. if ((tmp = sched_alloc(con))) {
  224. tmp->id = con->eventcnt++;
  225. tmp->callback = callback;
  226. tmp->data = data;
  227. tmp->resched = when;
  228. tmp->when.tv_sec = 0;
  229. tmp->when.tv_usec = 0;
  230. if (sched_settime(&tmp->when, when)) {
  231. sched_release(con, tmp);
  232. } else {
  233. schedule(con, tmp);
  234. res = tmp->id;
  235. }
  236. }
  237. ast_mutex_unlock(&con->lock);
  238. return res;
  239. }
  240. int ast_sched_del(struct sched_context *con, int id)
  241. {
  242. /*
  243. * Delete the schedule entry with number
  244. * "id". It's nearly impossible that there
  245. * would be two or more in the list with that
  246. * id.
  247. */
  248. struct sched *last=NULL, *s;
  249. DEBUG(ast_log(LOG_DEBUG, "ast_sched_del()\n"));
  250. ast_mutex_lock(&con->lock);
  251. s = con->schedq;
  252. while(s) {
  253. if (s->id == id) {
  254. if (last)
  255. last->next = s->next;
  256. else
  257. con->schedq = s->next;
  258. con->schedcnt--;
  259. sched_release(con, s);
  260. break;
  261. }
  262. last = s;
  263. s = s->next;
  264. }
  265. ast_mutex_unlock(&con->lock);
  266. if (!s) {
  267. ast_log(LOG_NOTICE, "Attempted to delete nonexistent schedule entry %d!\n", id);
  268. #ifdef DO_CRASH
  269. CRASH;
  270. #endif
  271. return -1;
  272. } else
  273. return 0;
  274. }
  275. void ast_sched_dump(struct sched_context *con)
  276. {
  277. /*
  278. * Dump the contents of the scheduler to
  279. * stderr
  280. */
  281. struct sched *q;
  282. struct timeval tv;
  283. time_t s, ms;
  284. gettimeofday(&tv, NULL);
  285. #ifdef SCHED_MAX_CACHE
  286. ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%d in Q, %d Total, %d Cache)\n",
  287. con-> schedcnt, con->eventcnt - 1, con->schedccnt);
  288. #else
  289. ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%d in Q, %d Total)\n",
  290. con-> schedcnt, con->eventcnt - 1);
  291. #endif
  292. ast_log(LOG_DEBUG, "=================================================\n");
  293. ast_log(LOG_DEBUG, "|ID Callback Data Time (sec:ms) |\n");
  294. ast_log(LOG_DEBUG, "+-----+-----------+-----------+-----------------+\n");
  295. q = con->schedq;
  296. while(q) {
  297. s = q->when.tv_sec - tv.tv_sec;
  298. ms = q->when.tv_usec - tv.tv_usec;
  299. if (ms < 0) {
  300. ms += 1000000;
  301. s--;
  302. }
  303. ast_log(LOG_DEBUG, "|%.4d | %p | %p | %.6ld : %.6ld |\n",
  304. q->id,
  305. q->callback,
  306. q->data,
  307. (long)s,
  308. (long)ms);
  309. q=q->next;
  310. }
  311. ast_log(LOG_DEBUG, "=================================================\n");
  312. }
  313. int ast_sched_runq(struct sched_context *con)
  314. {
  315. /*
  316. * Launch all events which need to be run at this time.
  317. */
  318. struct sched *current;
  319. struct timeval tv;
  320. int x=0;
  321. int res;
  322. DEBUG(ast_log(LOG_DEBUG, "ast_sched_runq()\n"));
  323. ast_mutex_lock(&con->lock);
  324. for(;;) {
  325. if (!con->schedq)
  326. break;
  327. if (gettimeofday(&tv, NULL)) {
  328. /* This should never happen */
  329. ast_log(LOG_NOTICE, "gettimeofday() failed!\n");
  330. break;
  331. }
  332. /* We only care about millisecond accuracy anyway, so this will
  333. help us get more than one event at one time if they are very
  334. close together. */
  335. tv.tv_usec += 1000;
  336. if (SOONER(con->schedq->when, tv)) {
  337. current = con->schedq;
  338. con->schedq = con->schedq->next;
  339. con->schedcnt--;
  340. /*
  341. * At this point, the schedule queue is still intact. We
  342. * have removed the first event and the rest is still there,
  343. * so it's permissible for the callback to add new events, but
  344. * trying to delete itself won't work because it isn't in
  345. * the schedule queue. If that's what it wants to do, it
  346. * should return 0.
  347. */
  348. ast_mutex_unlock(&con->lock);
  349. res = current->callback(current->data);
  350. ast_mutex_lock(&con->lock);
  351. if (res) {
  352. /*
  353. * If they return non-zero, we should schedule them to be
  354. * run again.
  355. */
  356. if (sched_settime(&current->when, current->resched)) {
  357. sched_release(con, current);
  358. } else
  359. schedule(con, current);
  360. } else {
  361. /* No longer needed, so release it */
  362. sched_release(con, current);
  363. }
  364. x++;
  365. } else
  366. break;
  367. }
  368. ast_mutex_unlock(&con->lock);
  369. return x;
  370. }
  371. long ast_sched_when(struct sched_context *con,int id)
  372. {
  373. struct sched *s;
  374. long secs;
  375. struct timeval now;
  376. DEBUG(ast_log(LOG_DEBUG, "ast_sched_when()\n"));
  377. ast_mutex_lock(&con->lock);
  378. s=con->schedq;
  379. while (s!=NULL) {
  380. if (s->id==id) break;
  381. s=s->next;
  382. }
  383. secs=-1;
  384. if (s!=NULL) {
  385. if (gettimeofday(&now, NULL)) {
  386. ast_log(LOG_NOTICE, "gettimeofday() failed!\n");
  387. } else {
  388. secs=s->when.tv_sec-now.tv_sec;
  389. }
  390. }
  391. ast_mutex_unlock(&con->lock);
  392. return secs;
  393. }