Merge the cli_cleanup branch.
[asterisk/asterisk.git] / main / taskprocessor.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007-2008, Dwayne M. Hubbard 
5  *
6  * Dwayne M. Hubbard <dhubbard@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 /*! \file
19  *
20  * \brief Maintain a container of uniquely-named taskprocessor threads that can be shared across modules.
21  *
22  * \author Dwayne Hubbard <dhubbard@digium.com>
23  */
24
25 #include "asterisk.h"
26
27 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
28
29 #include <signal.h>
30 #include <sys/time.h>
31
32 #include "asterisk/_private.h"
33 #include "asterisk/module.h"
34 #include "asterisk/time.h"
35 #include "asterisk/astobj2.h"
36 #include "asterisk/cli.h"
37 #include "asterisk/taskprocessor.h"
38
39
40 /*! \brief tps_task structure is queued to a taskprocessor
41  *
42  * tps_tasks are processed in FIFO order and freed by the taskprocessing
43  * thread after the task handler returns.  The callback function that is assigned
44  * to the execute() function pointer is responsible for releasing datap resources if necessary. */
45 struct tps_task {
46         /*! \brief The execute() task callback function pointer */
47         int (*execute)(void *datap);
48         /*! \brief The data pointer for the task execute() function */
49         void *datap;
50         /*! \brief AST_LIST_ENTRY overhead */
51         AST_LIST_ENTRY(tps_task) list;
52 };
53
54 /*! \brief tps_taskprocessor_stats maintain statistics for a taskprocessor. */
55 struct tps_taskprocessor_stats {
56         /*! \brief This is the maximum number of tasks queued at any one time */
57         unsigned long max_qsize;
58         /*! \brief This is the current number of tasks processed */
59         unsigned long _tasks_processed_count;
60 };
61
62 /*! \brief A ast_taskprocessor structure is a singleton by name */
63 struct ast_taskprocessor {
64         /*! \brief Friendly name of the taskprocessor */
65         char *name;
66         /*! \brief Thread poll condition */
67         ast_cond_t poll_cond;
68         /*! \brief Taskprocessor thread */
69         pthread_t poll_thread;
70         /*! \brief Taskprocessor lock */
71         ast_mutex_t taskprocessor_lock;
72         /*! \brief Taskprocesor thread run flag */
73         unsigned char poll_thread_run;
74         /*! \brief Taskprocessor statistics */
75         struct tps_taskprocessor_stats *stats;
76         /*! \brief Taskprocessor current queue size */
77         long tps_queue_size;
78         /*! \brief Taskprocessor queue */
79         AST_LIST_HEAD_NOLOCK(tps_queue, tps_task) tps_queue;
80         /*! \brief Taskprocessor singleton list entry */
81         AST_LIST_ENTRY(ast_taskprocessor) list;
82 };
83 #define TPS_MAX_BUCKETS 7
84 /*! \brief tps_singletons is the astobj2 container for taskprocessor singletons */
85 static struct ao2_container *tps_singletons;
86
87 /*! \brief CLI 'taskprocessor ping <blah>' operation requires a ping condition */
88 static ast_cond_t cli_ping_cond;
89
90 /*! \brief CLI 'taskprocessor ping <blah>' operation requires a ping condition lock */
91 AST_MUTEX_DEFINE_STATIC(cli_ping_cond_lock);
92
93 /*! \brief The astobj2 hash callback for taskprocessors */
94 static int tps_hash_cb(const void *obj, const int flags);
95 /*! \brief The astobj2 compare callback for taskprocessors */
96 static int tps_cmp_cb(void *obj, void *arg, int flags);
97
98 /*! \brief The task processing function executed by a taskprocessor */
99 static void *tps_processing_function(void *data);
100
101 /*! \brief Destroy the taskprocessor when its refcount reaches zero */
102 static void tps_taskprocessor_destroy(void *tps);
103
104 /*! \brief CLI 'taskprocessor ping <blah>' handler function */
105 static int tps_ping_handler(void *datap);
106
107 /*! \brief Remove the front task off the taskprocessor queue */
108 static struct tps_task *tps_taskprocessor_pop(struct ast_taskprocessor *tps);
109
110 /*! \brief Return the size of the taskprocessor queue */
111 static int tps_taskprocessor_depth(struct ast_taskprocessor *tps);
112
113 static char *cli_tps_ping(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
114 static char *cli_tps_report(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
115
116 static struct ast_cli_entry taskprocessor_clis[] = {
117         AST_CLI_DEFINE(cli_tps_ping, "Ping a named task processors"),
118         AST_CLI_DEFINE(cli_tps_report, "List instantiated task processors and statistics"),
119 };
120
121 /* initialize the taskprocessor container and register CLI operations */
122 int ast_tps_init(void)
123 {
124         if (!(tps_singletons = ao2_container_alloc(TPS_MAX_BUCKETS, tps_hash_cb, tps_cmp_cb))) {
125                 ast_log(LOG_ERROR, "taskprocessor container failed to initialize!\n");
126                 return -1;
127         }
128
129         ast_cond_init(&cli_ping_cond, NULL);
130
131         ast_cli_register_multiple(taskprocessor_clis, ARRAY_LEN(taskprocessor_clis));
132         return 0;
133 }
134
135 /* allocate resources for the task */
136 static struct tps_task *tps_task_alloc(int (*task_exe)(void *datap), void *datap)
137 {
138         struct tps_task *t;
139         if ((t = ast_calloc(1, sizeof(*t)))) {
140                 t->execute = task_exe;
141                 t->datap = datap;
142         }
143         return t;
144 }
145
146 /* release task resources */    
147 static void *tps_task_free(struct tps_task *task)
148 {
149         if (task) {
150                 ast_free(task);
151         }
152         return NULL;
153 }
154
155 /* taskprocessor tab completion */
156 static char *tps_taskprocessor_tab_complete(struct ast_taskprocessor *p, struct ast_cli_args *a) 
157 {
158         int tklen;
159         int wordnum = 0;
160         char *name = NULL;
161         struct ao2_iterator i;
162
163         if (a->pos != 3)
164                 return NULL;
165
166         tklen = strlen(a->word);
167         i = ao2_iterator_init(tps_singletons, 0);
168         while ((p = ao2_iterator_next(&i))) {
169                 if (!strncasecmp(a->word, p->name, tklen) && ++wordnum > a->n) {
170                         name = ast_strdup(p->name);
171                         ao2_ref(p, -1);
172                         break;
173                 }
174                 ao2_ref(p, -1);
175         }
176         return name;
177 }
178
179 /* ping task handling function */
180 static int tps_ping_handler(void *datap)
181 {
182         ast_mutex_lock(&cli_ping_cond_lock);
183         ast_cond_signal(&cli_ping_cond);
184         ast_mutex_unlock(&cli_ping_cond_lock);
185         return 0;
186 }
187
188 /* ping the specified taskprocessor and display the ping time on the CLI */
189 static char *cli_tps_ping(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
190 {
191         struct timeval begin, end, delta;
192         char *name;
193         struct timeval when;
194         struct timespec ts;
195         struct ast_taskprocessor *tps = NULL;
196
197         switch (cmd) {
198         case CLI_INIT:
199                 e->command = "core ping taskprocessor";
200                 e->usage = 
201                         "Usage: core ping taskprocessor <taskprocessor>\n"
202                         "       Displays the time required for a task to be processed\n";
203                 return NULL;
204         case CLI_GENERATE:
205                 return tps_taskprocessor_tab_complete(tps, a);
206         }
207
208         if (a->argc != 4)
209                 return CLI_SHOWUSAGE;
210
211         name = a->argv[3];
212         if (!(tps = ast_taskprocessor_get(name, TPS_REF_IF_EXISTS))) {
213                 ast_cli(a->fd, "\nping failed: %s not found\n\n", name);
214                 return CLI_SUCCESS;
215         }
216         ast_cli(a->fd, "\npinging %s ...", name);
217         when = ast_tvadd((begin = ast_tvnow()), ast_samp2tv(1000, 1000));
218         ts.tv_sec = when.tv_sec;
219         ts.tv_nsec = when.tv_usec * 1000;
220         ast_mutex_lock(&cli_ping_cond_lock);
221         if (ast_taskprocessor_push(tps, tps_ping_handler, 0) < 0) {
222                 ast_cli(a->fd, "\nping failed: could not push task to %s\n\n", name);
223                 ao2_ref(tps, -1);
224                 return CLI_FAILURE;
225         }
226         ast_cond_timedwait(&cli_ping_cond, &cli_ping_cond_lock, &ts);
227         ast_mutex_unlock(&cli_ping_cond_lock);
228         end = ast_tvnow();
229         delta = ast_tvsub(end, begin);
230         ast_cli(a->fd, "\n\t%24s ping time: %.1ld.%.6ld sec\n\n", name, (long)delta.tv_sec, (long int)delta.tv_usec);
231         ao2_ref(tps, -1);
232         return CLI_SUCCESS;     
233 }
234
235 static char *cli_tps_report(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
236 {
237         char name[256];
238         int tcount;
239         unsigned long qsize;
240         unsigned long maxqsize;
241         unsigned long processed;
242         struct ast_taskprocessor *p;
243         struct ao2_iterator i;
244
245         switch (cmd) {
246         case CLI_INIT:
247                 e->command = "core show taskprocessors";
248                 e->usage = 
249                         "Usage: core show taskprocessors\n"
250                         "       Shows a list of instantiated task processors and their statistics\n";
251                 return NULL;
252         case CLI_GENERATE:
253                 return NULL;    
254         }
255
256         if (a->argc != e->args)
257                 return CLI_SHOWUSAGE;
258
259         ast_cli(a->fd, "\n\t+----- Processor -----+--- Processed ---+- In Queue -+- Max Depth -+");
260         i = ao2_iterator_init(tps_singletons, 0);
261         while ((p = ao2_iterator_next(&i))) {
262                 ast_copy_string(name, p->name, sizeof(name));
263                 qsize = p->tps_queue_size;
264                 maxqsize = p->stats->max_qsize;
265                 processed = p->stats->_tasks_processed_count;
266                 ast_cli(a->fd, "\n%24s   %17ld %12ld %12ld", name, processed, qsize, maxqsize);
267                 ao2_ref(p, -1);
268         }
269         tcount = ao2_container_count(tps_singletons); 
270         ast_cli(a->fd, "\n\t+---------------------+-----------------+------------+-------------+\n\t%d taskprocessors\n\n", tcount);
271         return CLI_SUCCESS;     
272 }
273
274 /* this is the task processing worker function */
275 static void *tps_processing_function(void *data)
276 {
277         struct ast_taskprocessor *i = data;
278         struct tps_task *t;
279         int size;
280
281         if (!i) {
282                 ast_log(LOG_ERROR, "cannot start thread_function loop without a ast_taskprocessor structure.\n");
283                 return NULL;
284         }
285
286         while (i->poll_thread_run) {
287                 ast_mutex_lock(&i->taskprocessor_lock);
288                 if (!i->poll_thread_run) {
289                         ast_mutex_unlock(&i->taskprocessor_lock);
290                         break;
291                 }
292                 if (!(size = tps_taskprocessor_depth(i))) {
293                         ast_cond_wait(&i->poll_cond, &i->taskprocessor_lock);
294                         if (!i->poll_thread_run) {
295                                 ast_mutex_unlock(&i->taskprocessor_lock);
296                                 break;
297                         }
298                 }
299                 ast_mutex_unlock(&i->taskprocessor_lock);
300                 /* stuff is in the queue */
301                 if (!(t = tps_taskprocessor_pop(i))) {
302                         ast_log(LOG_ERROR, "Wtf?? %d tasks in the queue, but we're popping blanks!\n", size);
303                         continue;
304                 }
305                 if (!t->execute) {
306                         ast_log(LOG_WARNING, "Task is missing a function to execute!\n");
307                         tps_task_free(t);
308                         continue;
309                 }
310                 t->execute(t->datap);
311  
312                 ast_mutex_lock(&i->taskprocessor_lock);
313                 if (i->stats) {
314                         i->stats->_tasks_processed_count++;
315                         if (size > i->stats->max_qsize) {
316                                 i->stats->max_qsize = size;
317                         }
318                 }
319                 ast_mutex_unlock(&i->taskprocessor_lock);
320  
321                 tps_task_free(t);
322         }
323         while ((t = tps_taskprocessor_pop(i))) {
324                 tps_task_free(t);
325         }
326         return NULL;
327 }
328
329 /* hash callback for astobj2 */
330 static int tps_hash_cb(const void *obj, const int flags)
331 {
332         const struct ast_taskprocessor *tps = obj;
333
334         return ast_str_hash(tps->name);
335 }
336
337 /* compare callback for astobj2 */
338 static int tps_cmp_cb(void *obj, void *arg, int flags)
339 {
340         struct ast_taskprocessor *lhs = obj, *rhs = arg;
341
342         return !strcasecmp(lhs->name, rhs->name) ? CMP_MATCH | CMP_STOP : 0;
343 }
344
345 /* destroy the taskprocessor */
346 static void tps_taskprocessor_destroy(void *tps)
347 {
348         struct ast_taskprocessor *t = tps;
349         
350         if (!tps) {
351                 ast_log(LOG_ERROR, "missing taskprocessor\n");
352                 return;
353         }
354         ast_log(LOG_DEBUG, "destroying taskprocessor '%s'\n", t->name);
355         /* kill it */   
356         ast_mutex_lock(&t->taskprocessor_lock);
357         t->poll_thread_run = 0;
358         ast_cond_signal(&t->poll_cond);
359         ast_mutex_unlock(&t->taskprocessor_lock);
360         pthread_join(t->poll_thread, NULL);
361         t->poll_thread = AST_PTHREADT_NULL;
362         ast_mutex_destroy(&t->taskprocessor_lock);
363         ast_cond_destroy(&t->poll_cond);
364         /* free it */
365         if (t->stats) {
366                 ast_free(t->stats);
367                 t->stats = NULL;
368         }
369         ast_free(t->name);
370 }
371
372 /* pop the front task and return it */
373 static struct tps_task *tps_taskprocessor_pop(struct ast_taskprocessor *tps)
374 {
375         struct tps_task *task;
376
377         if (!tps) {
378                 ast_log(LOG_ERROR, "missing taskprocessor\n");
379                 return NULL;
380         }
381         ast_mutex_lock(&tps->taskprocessor_lock);
382         if ((task = AST_LIST_REMOVE_HEAD(&tps->tps_queue, list))) {
383                 tps->tps_queue_size--;
384         }
385         ast_mutex_unlock(&tps->taskprocessor_lock);
386         return task;
387 }
388
389 static int tps_taskprocessor_depth(struct ast_taskprocessor *tps)
390 {
391         return (tps) ? tps->tps_queue_size : -1;
392 }
393
394 /* taskprocessor name accessor */
395 const char *ast_taskprocessor_name(struct ast_taskprocessor *tps)
396 {
397         if (!tps) {
398                 ast_log(LOG_ERROR, "no taskprocessor specified!\n");
399                 return NULL;
400         }
401         return tps->name;
402 }
403
404 /* Provide a reference to a taskprocessor.  Create the taskprocessor if necessary, but don't
405  * create the taskprocessor if we were told via ast_tps_options to return a reference only 
406  * if it already exists */
407 struct ast_taskprocessor *ast_taskprocessor_get(char *name, enum ast_tps_options create)
408 {
409         struct ast_taskprocessor *p, tmp_tps = {
410                 .name = name,
411         };
412                 
413         if (ast_strlen_zero(name)) {
414                 ast_log(LOG_ERROR, "requesting a nameless taskprocessor!!!\n");
415                 return NULL;
416         }
417         ao2_lock(tps_singletons);
418         p = ao2_find(tps_singletons, &tmp_tps, OBJ_POINTER);
419         if (p) {
420                 ao2_unlock(tps_singletons);
421                 return p;
422         }
423         if (create & TPS_REF_IF_EXISTS) {
424                 /* calling function does not want a new taskprocessor to be created if it doesn't already exist */
425                 ao2_unlock(tps_singletons);
426                 return NULL;
427         }
428         /* create a new taskprocessor */
429         if (!(p = ao2_alloc(sizeof(*p), tps_taskprocessor_destroy))) {
430                 ao2_unlock(tps_singletons);
431                 ast_log(LOG_WARNING, "failed to create taskprocessor '%s'\n", name);
432                 return NULL;
433         }
434
435         ast_cond_init(&p->poll_cond, NULL);
436         ast_mutex_init(&p->taskprocessor_lock);
437
438         if (!(p->stats = ast_calloc(1, sizeof(*p->stats)))) {
439                 ao2_unlock(tps_singletons);
440                 ast_log(LOG_WARNING, "failed to create taskprocessor stats for '%s'\n", name);
441                 ao2_ref(p, -1);
442                 return NULL;
443         }
444         if (!(p->name = ast_strdup(name))) {
445                 ao2_unlock(tps_singletons);
446                 ao2_ref(p, -1);
447                 return NULL;
448         }
449         p->poll_thread_run = 1;
450         p->poll_thread = AST_PTHREADT_NULL;
451         if (ast_pthread_create(&p->poll_thread, NULL, tps_processing_function, p) < 0) {
452                 ao2_unlock(tps_singletons);
453                 ast_log(LOG_ERROR, "Taskprocessor '%s' failed to create the processing thread.\n", p->name);
454                 ao2_ref(p, -1);
455                 return NULL;
456         }
457         if (!(ao2_link(tps_singletons, p))) {
458                 ao2_unlock(tps_singletons);
459                 ast_log(LOG_ERROR, "Failed to add taskprocessor '%s' to container\n", p->name);
460                 ao2_ref(p, -1);
461                 return NULL;
462         }
463         ao2_unlock(tps_singletons);
464         return p;
465 }
466
467 /* decrement the taskprocessor reference count and unlink from the container if necessary */
468 void *ast_taskprocessor_unreference(struct ast_taskprocessor *tps)
469 {
470         if (tps) {
471                 ao2_lock(tps_singletons);
472                 ao2_unlink(tps_singletons, tps);
473                 if (ao2_ref(tps, -1) > 1) {
474                         ao2_link(tps_singletons, tps);
475                 }
476                 ao2_unlock(tps_singletons);
477         }
478         return NULL;
479 }
480
481 /* push the task into the taskprocessor queue */        
482 int ast_taskprocessor_push(struct ast_taskprocessor *tps, int (*task_exe)(void *datap), void *datap)
483 {
484         struct tps_task *t;
485
486         if (!tps || !task_exe) {
487                 ast_log(LOG_ERROR, "%s is missing!!\n", (tps) ? "task callback" : "taskprocessor");
488                 return -1;
489         }
490         if (!(t = tps_task_alloc(task_exe, datap))) {
491                 ast_log(LOG_ERROR, "failed to allocate task!  Can't push to '%s'\n", tps->name);
492                 return -1;
493         }
494         ast_mutex_lock(&tps->taskprocessor_lock);
495         AST_LIST_INSERT_TAIL(&tps->tps_queue, t, list);
496         tps->tps_queue_size++;
497         ast_cond_signal(&tps->poll_cond);
498         ast_mutex_unlock(&tps->taskprocessor_lock);
499         return 0;
500 }
501