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