Introducing a small upgrade to the ast_sched_xxx facility, to keep it from eating...
[asterisk/asterisk.git] / main / sched.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Scheduler Routines (from cheops-NG)
22  *
23  * \author Mark Spencer <markster@digium.com>
24  */
25
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #ifdef DEBUG_SCHEDULER
31 #define DEBUG(a) do { \
32         if (option_debug) \
33                 DEBUG_M(a) \
34         } while (0)
35 #else
36 #define DEBUG(a) 
37 #endif
38
39 #include <sys/time.h>
40
41 #include "asterisk/sched.h"
42 #include "asterisk/channel.h"
43 #include "asterisk/lock.h"
44 #include "asterisk/utils.h"
45 #include "asterisk/linkedlists.h"
46 #include "asterisk/dlinkedlists.h"
47 #include "asterisk/hashtab.h"
48
49 struct sched {
50         AST_DLLIST_ENTRY(sched) list;
51         int id;                       /*!< ID number of event */
52         struct timeval when;          /*!< Absolute time event should take place */
53         int resched;                  /*!< When to reschedule */
54         int variable;                 /*!< Use return value from callback to reschedule */
55         const void *data;             /*!< Data */
56         ast_sched_cb callback;        /*!< Callback */
57 };
58
59 struct sched_context {
60         ast_mutex_t lock;
61         unsigned int eventcnt;                  /*!< Number of events processed */
62         unsigned int schedcnt;                  /*!< Number of outstanding schedule events */
63         unsigned int highwater;                                 /*!< highest count so far */
64         AST_DLLIST_HEAD_NOLOCK(, sched) schedq;   /*!< Schedule entry and main queue */
65         struct ast_hashtab *schedq_ht;             /*!< hash table for fast searching */
66
67 #ifdef SCHED_MAX_CACHE
68         AST_LIST_HEAD_NOLOCK(, sched) schedc;   /*!< Cache of unused schedule structures and how many */
69         unsigned int schedccnt;
70 #endif
71 };
72
73
74 /* hash routines for sched */
75
76 static int sched_cmp(const void *a, const void *b)
77 {
78         const struct sched *as = a;
79         const struct sched *bs = b;
80         return as->id != bs->id; /* return 0 on a match like strcmp would */
81 }
82
83 static unsigned int sched_hash(const void *obj)
84 {
85         const struct sched *s = obj;
86         unsigned int h = s->id;
87         return h;
88 }
89
90 struct sched_context *sched_context_create(void)
91 {
92         struct sched_context *tmp;
93
94         if (!(tmp = ast_calloc(1, sizeof(*tmp))))
95                 return NULL;
96
97         ast_mutex_init(&tmp->lock);
98         tmp->eventcnt = 1;
99         
100         tmp->schedq_ht = ast_hashtab_create(23, sched_cmp, ast_hashtab_resize_java, ast_hashtab_newsize_java, sched_hash, 1);
101         
102         return tmp;
103 }
104
105 void sched_context_destroy(struct sched_context *con)
106 {
107         struct sched *s;
108
109         ast_mutex_lock(&con->lock);
110
111 #ifdef SCHED_MAX_CACHE
112         /* Eliminate the cache */
113         while ((s = AST_LIST_REMOVE_HEAD(&con->schedc, list)))
114                 ast_free(s);
115 #endif
116
117         /* And the queue */
118         while ((s = AST_DLLIST_REMOVE_HEAD(&con->schedq, list)))
119                 ast_free(s);
120
121         ast_hashtab_destroy(con->schedq_ht, NULL);
122         con->schedq_ht = NULL;
123         
124         /* And the context */
125         ast_mutex_unlock(&con->lock);
126         ast_mutex_destroy(&con->lock);
127         ast_free(con);
128 }
129
130 static struct sched *sched_alloc(struct sched_context *con)
131 {
132         struct sched *tmp;
133
134         /*
135          * We keep a small cache of schedule entries
136          * to minimize the number of necessary malloc()'s
137          */
138 #ifdef SCHED_MAX_CACHE
139         if ((tmp = AST_LIST_REMOVE_HEAD(&con->schedc, list)))
140                 con->schedccnt--;
141         else
142 #endif
143                 tmp = ast_calloc(1, sizeof(*tmp));
144
145         return tmp;
146 }
147
148 static void sched_release(struct sched_context *con, struct sched *tmp)
149 {
150         /*
151          * Add to the cache, or just free() if we
152          * already have too many cache entries
153          */
154
155 #ifdef SCHED_MAX_CACHE   
156         if (con->schedccnt < SCHED_MAX_CACHE) {
157                 AST_LIST_INSERT_HEAD(&con->schedc, tmp, list);
158                 con->schedccnt++;
159         } else
160 #endif
161                 ast_free(tmp);
162 }
163
164 /*! \brief
165  * Return the number of milliseconds 
166  * until the next scheduled event
167  */
168 int ast_sched_wait(struct sched_context *con)
169 {
170         int ms;
171
172         DEBUG(ast_debug(1, "ast_sched_wait()\n"));
173
174         ast_mutex_lock(&con->lock);
175         if (AST_DLLIST_EMPTY(&con->schedq)) {
176                 ms = -1;
177         } else {
178                 ms = ast_tvdiff_ms(AST_DLLIST_FIRST(&con->schedq)->when, ast_tvnow());
179                 if (ms < 0)
180                         ms = 0;
181         }
182         ast_mutex_unlock(&con->lock);
183
184         return ms;
185 }
186
187
188 /*! \brief
189  * Take a sched structure and put it in the
190  * queue, such that the soonest event is
191  * first in the list. 
192  */
193 static void schedule(struct sched_context *con, struct sched *s)
194 {
195         struct sched *cur = NULL;
196         int ret;
197         int df = 0;
198         int de = 0;
199         struct sched *first = AST_DLLIST_FIRST(&con->schedq);
200         struct sched *last = AST_DLLIST_LAST(&con->schedq);
201         if (first)
202                 df = ast_tvdiff_us(s->when, first->when);
203         if (last)
204                 de = ast_tvdiff_us(s->when, last->when);
205         if (df < 0)
206                 df = -df;
207         if (de < 0)
208                 de = -de;
209         if (df < de)
210                 AST_DLLIST_TRAVERSE(&con->schedq, cur, list) {
211                         if (ast_tvcmp(s->when, cur->when) == -1) {
212                                 AST_DLLIST_INSERT_BEFORE(&con->schedq, cur, s, list);
213                                 break;
214                         }
215                 }
216         else
217                 AST_DLLIST_TRAVERSE_BACKWARDS(&con->schedq, cur, list) {
218                         if (ast_tvcmp(s->when, cur->when) == 1) {
219                                 AST_DLLIST_INSERT_AFTER(&con->schedq, cur, s, list);
220                                 break;
221                         }
222                 }
223         if (!cur)
224                 AST_DLLIST_INSERT_TAIL(&con->schedq, s, list);
225         ret = ast_hashtab_insert_safe(con->schedq_ht, s);
226         if (!ret)
227                 ast_log(LOG_WARNING,"Schedule Queue entry %d is already in table!\n",s->id);
228         con->schedcnt++;
229         if (con->schedcnt > con->highwater)
230                 con->highwater = con->schedcnt;
231 }
232
233 /*! \brief
234  * given the last event *tv and the offset in milliseconds 'when',
235  * computes the next value,
236  */
237 static int sched_settime(struct timeval *tv, int when)
238 {
239         struct timeval now = ast_tvnow();
240
241         /*ast_debug(1, "TV -> %lu,%lu\n", tv->tv_sec, tv->tv_usec);*/
242         if (ast_tvzero(*tv))    /* not supplied, default to now */
243                 *tv = now;
244         *tv = ast_tvadd(*tv, ast_samp2tv(when, 1000));
245         if (ast_tvcmp(*tv, now) < 0) {
246                 ast_debug(1, "Request to schedule in the past?!?!\n");
247                 *tv = now;
248         }
249         return 0;
250 }
251
252 int ast_sched_replace_variable(int old_id, struct sched_context *con, int when, ast_sched_cb callback, const void *data, int variable)
253 {
254         /* 0 means the schedule item is new; do not delete */
255         if (old_id > 0)
256                 ast_sched_del(con, old_id);
257         return ast_sched_add_variable(con, when, callback, data, variable);
258 }
259
260 /*! \brief
261  * Schedule callback(data) to happen when ms into the future
262  */
263 int ast_sched_add_variable(struct sched_context *con, int when, ast_sched_cb callback, const void *data, int variable)
264 {
265         struct sched *tmp;
266         int res = -1;
267         DEBUG(ast_debug(1, "ast_sched_add()\n"));
268         if (!when) {
269                 ast_log(LOG_NOTICE, "Scheduled event in 0 ms?\n");
270                 return -1;
271         }
272         ast_mutex_lock(&con->lock);
273         if ((tmp = sched_alloc(con))) {
274                 tmp->id = con->eventcnt++;
275                 tmp->callback = callback;
276                 tmp->data = data;
277                 tmp->resched = when;
278                 tmp->variable = variable;
279                 tmp->when = ast_tv(0, 0);
280                 if (sched_settime(&tmp->when, when)) {
281                         sched_release(con, tmp);
282                 } else {
283                         schedule(con, tmp);
284                         res = tmp->id;
285                 }
286         }
287 #ifdef DUMP_SCHEDULER
288         /* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
289         if (option_debug)
290                 ast_sched_dump(con);
291 #endif
292         ast_mutex_unlock(&con->lock);
293         return res;
294 }
295
296 int ast_sched_replace(int old_id, struct sched_context *con, int when, ast_sched_cb callback, const void *data)
297 {
298         if (old_id > -1)
299                 ast_sched_del(con, old_id);
300         return ast_sched_add(con, when, callback, data);
301 }
302
303 int ast_sched_add(struct sched_context *con, int when, ast_sched_cb callback, const void *data)
304 {
305         return ast_sched_add_variable(con, when, callback, data, 0);
306 }
307
308 const void *ast_sched_find_data(struct sched_context *con, int id)
309 {
310         struct sched tmp,*res;
311         tmp.id = id;
312         res = ast_hashtab_lookup(con->schedq_ht, &tmp);
313         if (res)
314                 return res->data;
315         return NULL;
316 }
317         
318 /*! \brief
319  * Delete the schedule entry with number
320  * "id".  It's nearly impossible that there
321  * would be two or more in the list with that
322  * id.
323  */
324 int ast_sched_del(struct sched_context *con, int id)
325 {
326         struct sched *s, tmp;
327
328         DEBUG(ast_debug(1, "ast_sched_del(%d)\n", id));
329         
330         ast_mutex_lock(&con->lock);
331
332         /* OK, this is the heart of the sched performance upgrade.
333            If we have 4700 peers, we can have 4700+ entries in the
334            schedq list. searching this would take time. So, I add a 
335            hashtab to the context to keep track of each entry, by id.
336            I also leave the linked list alone, almost, --  I implement
337        a doubly-linked list instead, because it would do little good
338            to look up the id in a hashtab, and then have to run thru 
339            a couple thousand entries to remove it from the schedq list! */
340         tmp.id = id;
341         s = ast_hashtab_lookup(con->schedq_ht, &tmp);
342         if (s) {
343                 struct sched *x = AST_DLLIST_REMOVE(&con->schedq, s, list);
344                 
345                 if (!x)
346                         ast_log(LOG_WARNING,"sched entry %d not in the schedq list?\n", s->id);
347
348                 if (!ast_hashtab_remove_this_object(con->schedq_ht, s))
349                         ast_log(LOG_WARNING,"Found sched entry %d, then couldn't remove it?\n", s->id);
350                 con->schedcnt--;
351                 sched_release(con, s);
352         }
353         
354 #ifdef DUMP_SCHEDULER
355         /* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
356         if (option_debug)
357                 ast_sched_dump(con);
358 #endif
359         ast_mutex_unlock(&con->lock);
360
361         if (!s) {
362                 ast_debug(1, "Attempted to delete nonexistent schedule entry %d!\n", id);
363 #ifdef DO_CRASH
364                 CRASH;
365 #endif
366                 return -1;
367         }
368         
369         return 0;
370 }
371
372
373 char *ast_sched_report(struct sched_context *con, char *buf, int bufsiz, struct ast_cb_names *cbnames)
374 {
375         int *countlist,i;
376         struct sched *cur;
377         char buf2[1200];
378         ast_sched_cb xxx = NULL;
379         
380         buf[0] = 0;
381         sprintf(buf, " Highwater = %d\n schedcnt = %d\n", con->highwater, con->schedcnt);
382         countlist = ast_calloc(sizeof(int),cbnames->numassocs+1);
383         
384         AST_DLLIST_TRAVERSE(&con->schedq, cur, list) {
385                 /* match the callback to the cblist */
386                 for (i=0;i<cbnames->numassocs;i++) {
387                         if (cur->callback == cbnames->cblist[i])
388                                 break;
389                 }
390                 if (i < cbnames->numassocs)
391                         countlist[i]++;
392                 else {
393                         xxx = cur->callback;
394                         countlist[cbnames->numassocs]++;
395                 }
396         }
397         for (i=0;i<cbnames->numassocs;i++) {
398                 sprintf(buf2,"    %s : %d\n", cbnames->list[i], countlist[i]);
399                 strcat(buf, buf2);
400         }
401         sprintf(buf2,"   <unknown:%p> : %d\n", xxx, countlist[cbnames->numassocs]);
402         strcat( buf, buf2);
403         return buf;
404 }
405
406
407         
408 /*! \brief Dump the contents of the scheduler to LOG_DEBUG */
409 void ast_sched_dump(const struct sched_context *con)
410 {
411         struct sched *q;
412         struct timeval tv = ast_tvnow();
413 #ifdef SCHED_MAX_CACHE
414         ast_debug(1, "Asterisk Schedule Dump (%d in Q, %d Total, %d Cache, %d high-water)\n", con->schedcnt, con->eventcnt - 1, con->schedccnt, con->highwater);
415 #else
416         ast_debug(1, "Asterisk Schedule Dump (%d in Q, %d Total, %d high-water)\n", con->schedcnt, con->eventcnt - 1, con->highwater);
417 #endif
418
419         ast_debug(1, "=============================================================\n");
420         ast_debug(1, "|ID    Callback          Data              Time  (sec:ms)   |\n");
421         ast_debug(1, "+-----+-----------------+-----------------+-----------------+\n");
422         AST_DLLIST_TRAVERSE(&con->schedq, q, list) {
423                 struct timeval delta = ast_tvsub(q->when, tv);
424
425                 ast_debug(1, "|%.4d | %-15p | %-15p | %.6ld : %.6ld |\n", 
426                         q->id,
427                         q->callback,
428                         q->data,
429                         delta.tv_sec,
430                         (long int)delta.tv_usec);
431         }
432         ast_debug(1, "=============================================================\n");
433         
434 }
435
436 /*! \brief
437  * Launch all events which need to be run at this time.
438  */
439 int ast_sched_runq(struct sched_context *con)
440 {
441         struct sched *current;
442         struct timeval tv;
443         int numevents;
444         int res;
445
446         DEBUG(ast_debug(1, "ast_sched_runq()\n"));
447                 
448         ast_mutex_lock(&con->lock);
449
450         for (numevents = 0; !AST_DLLIST_EMPTY(&con->schedq); numevents++) {
451                 /* schedule all events which are going to expire within 1ms.
452                  * We only care about millisecond accuracy anyway, so this will
453                  * help us get more than one event at one time if they are very
454                  * close together.
455                  */
456                 tv = ast_tvadd(ast_tvnow(), ast_tv(0, 1000));
457                 if (ast_tvcmp(AST_DLLIST_FIRST(&con->schedq)->when, tv) != -1)
458                         break;
459                 
460                 current = AST_DLLIST_REMOVE_HEAD(&con->schedq, list);
461                 if (!ast_hashtab_remove_this_object(con->schedq_ht, current))
462                         ast_log(LOG_ERROR,"Sched entry %d was in the schedq list but not in the hashtab???\n", current->id);
463
464                 con->schedcnt--;
465
466                 /*
467                  * At this point, the schedule queue is still intact.  We
468                  * have removed the first event and the rest is still there,
469                  * so it's permissible for the callback to add new events, but
470                  * trying to delete itself won't work because it isn't in
471                  * the schedule queue.  If that's what it wants to do, it 
472                  * should return 0.
473                  */
474                         
475                 ast_mutex_unlock(&con->lock);
476                 res = current->callback(current->data);
477                 ast_mutex_lock(&con->lock);
478                         
479                 if (res) {
480                         /*
481                          * If they return non-zero, we should schedule them to be
482                          * run again.
483                          */
484                         if (sched_settime(&current->when, current->variable? res : current->resched)) {
485                                 sched_release(con, current);
486                         } else
487                                 schedule(con, current);
488                 } else {
489                         /* No longer needed, so release it */
490                         sched_release(con, current);
491                 }
492         }
493
494         ast_mutex_unlock(&con->lock);
495         
496         return numevents;
497 }
498
499 long ast_sched_when(struct sched_context *con,int id)
500 {
501         struct sched *s, tmp;
502         long secs = -1;
503         DEBUG(ast_debug(1, "ast_sched_when()\n"));
504
505         ast_mutex_lock(&con->lock);
506         
507         /* these next 2 lines replace a lookup loop */
508         tmp.id = id;
509         s = ast_hashtab_lookup(con->schedq_ht, &tmp);
510         
511         if (s) {
512                 struct timeval now = ast_tvnow();
513                 secs = s->when.tv_sec - now.tv_sec;
514         }
515         ast_mutex_unlock(&con->lock);
516         
517         return secs;
518 }