Wed Nov 28 2012 04:18:16

Asterisk developer's documentation


taskprocessor.c
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2007-2008, Digium, Inc.
00005  *
00006  * Dwayne M. Hubbard <dhubbard@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*!
00020  * \file
00021  * \brief Maintain a container of uniquely-named taskprocessor threads that can be shared across modules.
00022  *
00023  * \author Dwayne Hubbard <dhubbard@digium.com>
00024  */
00025 
00026 /*** MODULEINFO
00027    <support_level>core</support_level>
00028  ***/
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 374177 $")
00033 
00034 #include "asterisk/_private.h"
00035 #include "asterisk/module.h"
00036 #include "asterisk/time.h"
00037 #include "asterisk/astobj2.h"
00038 #include "asterisk/cli.h"
00039 #include "asterisk/taskprocessor.h"
00040 
00041 
00042 /*!
00043  * \brief tps_task structure is queued to a taskprocessor
00044  *
00045  * tps_tasks are processed in FIFO order and freed by the taskprocessing
00046  * thread after the task handler returns.  The callback function that is assigned
00047  * to the execute() function pointer is responsible for releasing datap resources if necessary.
00048  */
00049 struct tps_task {
00050    /*! \brief The execute() task callback function pointer */
00051    int (*execute)(void *datap);
00052    /*! \brief The data pointer for the task execute() function */
00053    void *datap;
00054    /*! \brief AST_LIST_ENTRY overhead */
00055    AST_LIST_ENTRY(tps_task) list;
00056 };
00057 
00058 /*! \brief tps_taskprocessor_stats maintain statistics for a taskprocessor. */
00059 struct tps_taskprocessor_stats {
00060    /*! \brief This is the maximum number of tasks queued at any one time */
00061    unsigned long max_qsize;
00062    /*! \brief This is the current number of tasks processed */
00063    unsigned long _tasks_processed_count;
00064 };
00065 
00066 /*! \brief A ast_taskprocessor structure is a singleton by name */
00067 struct ast_taskprocessor {
00068    /*! \brief Friendly name of the taskprocessor */
00069    const char *name;
00070    /*! \brief Thread poll condition */
00071    ast_cond_t poll_cond;
00072    /*! \brief Taskprocessor thread */
00073    pthread_t poll_thread;
00074    /*! \brief Taskprocessor lock */
00075    ast_mutex_t taskprocessor_lock;
00076    /*! \brief Taskprocesor thread run flag */
00077    unsigned char poll_thread_run;
00078    /*! \brief Taskprocessor statistics */
00079    struct tps_taskprocessor_stats *stats;
00080    /*! \brief Taskprocessor current queue size */
00081    long tps_queue_size;
00082    /*! \brief Taskprocessor queue */
00083    AST_LIST_HEAD_NOLOCK(tps_queue, tps_task) tps_queue;
00084    /*! \brief Taskprocessor singleton list entry */
00085    AST_LIST_ENTRY(ast_taskprocessor) list;
00086 };
00087 #define TPS_MAX_BUCKETS 7
00088 /*! \brief tps_singletons is the astobj2 container for taskprocessor singletons */
00089 static struct ao2_container *tps_singletons;
00090 
00091 /*! \brief CLI <example>taskprocessor ping &lt;blah&gt;</example> operation requires a ping condition */
00092 static ast_cond_t cli_ping_cond;
00093 
00094 /*! \brief CLI <example>taskprocessor ping &lt;blah&gt;</example> operation requires a ping condition lock */
00095 AST_MUTEX_DEFINE_STATIC(cli_ping_cond_lock);
00096 
00097 /*! \brief The astobj2 hash callback for taskprocessors */
00098 static int tps_hash_cb(const void *obj, const int flags);
00099 /*! \brief The astobj2 compare callback for taskprocessors */
00100 static int tps_cmp_cb(void *obj, void *arg, int flags);
00101 
00102 /*! \brief The task processing function executed by a taskprocessor */
00103 static void *tps_processing_function(void *data);
00104 
00105 /*! \brief Destroy the taskprocessor when its refcount reaches zero */
00106 static void tps_taskprocessor_destroy(void *tps);
00107 
00108 /*! \brief CLI <example>taskprocessor ping &lt;blah&gt;</example> handler function */
00109 static int tps_ping_handler(void *datap);
00110 
00111 /*! \brief Remove the front task off the taskprocessor queue */
00112 static struct tps_task *tps_taskprocessor_pop(struct ast_taskprocessor *tps);
00113 
00114 /*! \brief Return the size of the taskprocessor queue */
00115 static int tps_taskprocessor_depth(struct ast_taskprocessor *tps);
00116 
00117 static char *cli_tps_ping(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
00118 static char *cli_tps_report(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
00119 
00120 static struct ast_cli_entry taskprocessor_clis[] = {
00121    AST_CLI_DEFINE(cli_tps_ping, "Ping a named task processor"),
00122    AST_CLI_DEFINE(cli_tps_report, "List instantiated task processors and statistics"),
00123 };
00124 
00125 /*! \internal \brief Clean up resources on Asterisk shutdown */
00126 static void tps_shutdown(void)
00127 {
00128    ao2_t_ref(tps_singletons, -1, "Unref tps_singletons in shutdown");
00129 }
00130 
00131 /* initialize the taskprocessor container and register CLI operations */
00132 int ast_tps_init(void)
00133 {
00134    if (!(tps_singletons = ao2_container_alloc(TPS_MAX_BUCKETS, tps_hash_cb, tps_cmp_cb))) {
00135       ast_log(LOG_ERROR, "taskprocessor container failed to initialize!\n");
00136       return -1;
00137    }
00138 
00139    ast_cond_init(&cli_ping_cond, NULL);
00140 
00141    ast_cli_register_multiple(taskprocessor_clis, ARRAY_LEN(taskprocessor_clis));
00142 
00143    ast_register_atexit(tps_shutdown);
00144 
00145    return 0;
00146 }
00147 
00148 /* allocate resources for the task */
00149 static struct tps_task *tps_task_alloc(int (*task_exe)(void *datap), void *datap)
00150 {
00151    struct tps_task *t;
00152    if ((t = ast_calloc(1, sizeof(*t)))) {
00153       t->execute = task_exe;
00154       t->datap = datap;
00155    }
00156    return t;
00157 }
00158 
00159 /* release task resources */  
00160 static void *tps_task_free(struct tps_task *task)
00161 {
00162    if (task) {
00163       ast_free(task);
00164    }
00165    return NULL;
00166 }
00167 
00168 /* taskprocessor tab completion */
00169 static char *tps_taskprocessor_tab_complete(struct ast_taskprocessor *p, struct ast_cli_args *a) 
00170 {
00171    int tklen;
00172    int wordnum = 0;
00173    char *name = NULL;
00174    struct ao2_iterator i;
00175 
00176    if (a->pos != 3)
00177       return NULL;
00178 
00179    tklen = strlen(a->word);
00180    i = ao2_iterator_init(tps_singletons, 0);
00181    while ((p = ao2_iterator_next(&i))) {
00182       if (!strncasecmp(a->word, p->name, tklen) && ++wordnum > a->n) {
00183          name = ast_strdup(p->name);
00184          ao2_ref(p, -1);
00185          break;
00186       }
00187       ao2_ref(p, -1);
00188    }
00189    ao2_iterator_destroy(&i);
00190    return name;
00191 }
00192 
00193 /* ping task handling function */
00194 static int tps_ping_handler(void *datap)
00195 {
00196    ast_mutex_lock(&cli_ping_cond_lock);
00197    ast_cond_signal(&cli_ping_cond);
00198    ast_mutex_unlock(&cli_ping_cond_lock);
00199    return 0;
00200 }
00201 
00202 /* ping the specified taskprocessor and display the ping time on the CLI */
00203 static char *cli_tps_ping(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00204 {
00205    struct timeval begin, end, delta;
00206    const char *name;
00207    struct timeval when;
00208    struct timespec ts;
00209    struct ast_taskprocessor *tps = NULL;
00210 
00211    switch (cmd) {
00212    case CLI_INIT:
00213       e->command = "core ping taskprocessor";
00214       e->usage = 
00215          "Usage: core ping taskprocessor <taskprocessor>\n"
00216          "  Displays the time required for a task to be processed\n";
00217       return NULL;
00218    case CLI_GENERATE:
00219       return tps_taskprocessor_tab_complete(tps, a);
00220    }
00221 
00222    if (a->argc != 4)
00223       return CLI_SHOWUSAGE;
00224 
00225    name = a->argv[3];
00226    if (!(tps = ast_taskprocessor_get(name, TPS_REF_IF_EXISTS))) {
00227       ast_cli(a->fd, "\nping failed: %s not found\n\n", name);
00228       return CLI_SUCCESS;
00229    }
00230    ast_cli(a->fd, "\npinging %s ...", name);
00231    when = ast_tvadd((begin = ast_tvnow()), ast_samp2tv(1000, 1000));
00232    ts.tv_sec = when.tv_sec;
00233    ts.tv_nsec = when.tv_usec * 1000;
00234    ast_mutex_lock(&cli_ping_cond_lock);
00235    if (ast_taskprocessor_push(tps, tps_ping_handler, 0) < 0) {
00236       ast_cli(a->fd, "\nping failed: could not push task to %s\n\n", name);
00237       ao2_ref(tps, -1);
00238       return CLI_FAILURE;
00239    }
00240    ast_cond_timedwait(&cli_ping_cond, &cli_ping_cond_lock, &ts);
00241    ast_mutex_unlock(&cli_ping_cond_lock);
00242    end = ast_tvnow();
00243    delta = ast_tvsub(end, begin);
00244    ast_cli(a->fd, "\n\t%24s ping time: %.1ld.%.6ld sec\n\n", name, (long)delta.tv_sec, (long int)delta.tv_usec);
00245    ao2_ref(tps, -1);
00246    return CLI_SUCCESS;  
00247 }
00248 
00249 static char *cli_tps_report(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00250 {
00251    char name[256];
00252    int tcount;
00253    unsigned long qsize;
00254    unsigned long maxqsize;
00255    unsigned long processed;
00256    struct ast_taskprocessor *p;
00257    struct ao2_iterator i;
00258 
00259    switch (cmd) {
00260    case CLI_INIT:
00261       e->command = "core show taskprocessors";
00262       e->usage = 
00263          "Usage: core show taskprocessors\n"
00264          "  Shows a list of instantiated task processors and their statistics\n";
00265       return NULL;
00266    case CLI_GENERATE:
00267       return NULL;   
00268    }
00269 
00270    if (a->argc != e->args)
00271       return CLI_SHOWUSAGE;
00272 
00273    ast_cli(a->fd, "\n\t+----- Processor -----+--- Processed ---+- In Queue -+- Max Depth -+");
00274    i = ao2_iterator_init(tps_singletons, 0);
00275    while ((p = ao2_iterator_next(&i))) {
00276       ast_copy_string(name, p->name, sizeof(name));
00277       qsize = p->tps_queue_size;
00278       maxqsize = p->stats->max_qsize;
00279       processed = p->stats->_tasks_processed_count;
00280       ast_cli(a->fd, "\n%24s   %17ld %12ld %12ld", name, processed, qsize, maxqsize);
00281       ao2_ref(p, -1);
00282    }
00283    ao2_iterator_destroy(&i);
00284    tcount = ao2_container_count(tps_singletons); 
00285    ast_cli(a->fd, "\n\t+---------------------+-----------------+------------+-------------+\n\t%d taskprocessors\n\n", tcount);
00286    return CLI_SUCCESS;  
00287 }
00288 
00289 /* this is the task processing worker function */
00290 static void *tps_processing_function(void *data)
00291 {
00292    struct ast_taskprocessor *i = data;
00293    struct tps_task *t;
00294    int size;
00295 
00296    if (!i) {
00297       ast_log(LOG_ERROR, "cannot start thread_function loop without a ast_taskprocessor structure.\n");
00298       return NULL;
00299    }
00300 
00301    while (i->poll_thread_run) {
00302       ast_mutex_lock(&i->taskprocessor_lock);
00303       if (!i->poll_thread_run) {
00304          ast_mutex_unlock(&i->taskprocessor_lock);
00305          break;
00306       }
00307       if (!(size = tps_taskprocessor_depth(i))) {
00308          ast_cond_wait(&i->poll_cond, &i->taskprocessor_lock);
00309          if (!i->poll_thread_run) {
00310             ast_mutex_unlock(&i->taskprocessor_lock);
00311             break;
00312          }
00313       }
00314       ast_mutex_unlock(&i->taskprocessor_lock);
00315       /* stuff is in the queue */
00316       if (!(t = tps_taskprocessor_pop(i))) {
00317          ast_log(LOG_ERROR, "Wtf?? %d tasks in the queue, but we're popping blanks!\n", size);
00318          continue;
00319       }
00320       if (!t->execute) {
00321          ast_log(LOG_WARNING, "Task is missing a function to execute!\n");
00322          tps_task_free(t);
00323          continue;
00324       }
00325       t->execute(t->datap);
00326  
00327       ast_mutex_lock(&i->taskprocessor_lock);
00328       if (i->stats) {
00329          i->stats->_tasks_processed_count++;
00330          if (size > i->stats->max_qsize) {
00331             i->stats->max_qsize = size;
00332          }
00333       }
00334       ast_mutex_unlock(&i->taskprocessor_lock);
00335  
00336       tps_task_free(t);
00337    }
00338    while ((t = tps_taskprocessor_pop(i))) {
00339       tps_task_free(t);
00340    }
00341    return NULL;
00342 }
00343 
00344 /* hash callback for astobj2 */
00345 static int tps_hash_cb(const void *obj, const int flags)
00346 {
00347    const struct ast_taskprocessor *tps = obj;
00348 
00349    return ast_str_case_hash(tps->name);
00350 }
00351 
00352 /* compare callback for astobj2 */
00353 static int tps_cmp_cb(void *obj, void *arg, int flags)
00354 {
00355    struct ast_taskprocessor *lhs = obj, *rhs = arg;
00356 
00357    return !strcasecmp(lhs->name, rhs->name) ? CMP_MATCH | CMP_STOP : 0;
00358 }
00359 
00360 /* destroy the taskprocessor */
00361 static void tps_taskprocessor_destroy(void *tps)
00362 {
00363    struct ast_taskprocessor *t = tps;
00364    
00365    if (!tps) {
00366       ast_log(LOG_ERROR, "missing taskprocessor\n");
00367       return;
00368    }
00369    ast_log(LOG_DEBUG, "destroying taskprocessor '%s'\n", t->name);
00370    /* kill it */  
00371    ast_mutex_lock(&t->taskprocessor_lock);
00372    t->poll_thread_run = 0;
00373    ast_cond_signal(&t->poll_cond);
00374    ast_mutex_unlock(&t->taskprocessor_lock);
00375    pthread_join(t->poll_thread, NULL);
00376    t->poll_thread = AST_PTHREADT_NULL;
00377    ast_mutex_destroy(&t->taskprocessor_lock);
00378    ast_cond_destroy(&t->poll_cond);
00379    /* free it */
00380    if (t->stats) {
00381       ast_free(t->stats);
00382       t->stats = NULL;
00383    }
00384    ast_free((char *) t->name);
00385 }
00386 
00387 /* pop the front task and return it */
00388 static struct tps_task *tps_taskprocessor_pop(struct ast_taskprocessor *tps)
00389 {
00390    struct tps_task *task;
00391 
00392    if (!tps) {
00393       ast_log(LOG_ERROR, "missing taskprocessor\n");
00394       return NULL;
00395    }
00396    ast_mutex_lock(&tps->taskprocessor_lock);
00397    if ((task = AST_LIST_REMOVE_HEAD(&tps->tps_queue, list))) {
00398       tps->tps_queue_size--;
00399    }
00400    ast_mutex_unlock(&tps->taskprocessor_lock);
00401    return task;
00402 }
00403 
00404 static int tps_taskprocessor_depth(struct ast_taskprocessor *tps)
00405 {
00406    return (tps) ? tps->tps_queue_size : -1;
00407 }
00408 
00409 /* taskprocessor name accessor */
00410 const char *ast_taskprocessor_name(struct ast_taskprocessor *tps)
00411 {
00412    if (!tps) {
00413       ast_log(LOG_ERROR, "no taskprocessor specified!\n");
00414       return NULL;
00415    }
00416    return tps->name;
00417 }
00418 
00419 /* Provide a reference to a taskprocessor.  Create the taskprocessor if necessary, but don't
00420  * create the taskprocessor if we were told via ast_tps_options to return a reference only 
00421  * if it already exists */
00422 struct ast_taskprocessor *ast_taskprocessor_get(const char *name, enum ast_tps_options create)
00423 {
00424    struct ast_taskprocessor *p, tmp_tps = {
00425       .name = name,
00426    };
00427       
00428    if (ast_strlen_zero(name)) {
00429       ast_log(LOG_ERROR, "requesting a nameless taskprocessor!!!\n");
00430       return NULL;
00431    }
00432    ao2_lock(tps_singletons);
00433    p = ao2_find(tps_singletons, &tmp_tps, OBJ_POINTER);
00434    if (p) {
00435       ao2_unlock(tps_singletons);
00436       return p;
00437    }
00438    if (create & TPS_REF_IF_EXISTS) {
00439       /* calling function does not want a new taskprocessor to be created if it doesn't already exist */
00440       ao2_unlock(tps_singletons);
00441       return NULL;
00442    }
00443    /* create a new taskprocessor */
00444    if (!(p = ao2_alloc(sizeof(*p), tps_taskprocessor_destroy))) {
00445       ao2_unlock(tps_singletons);
00446       ast_log(LOG_WARNING, "failed to create taskprocessor '%s'\n", name);
00447       return NULL;
00448    }
00449 
00450    ast_cond_init(&p->poll_cond, NULL);
00451    ast_mutex_init(&p->taskprocessor_lock);
00452 
00453    if (!(p->stats = ast_calloc(1, sizeof(*p->stats)))) {
00454       ao2_unlock(tps_singletons);
00455       ast_log(LOG_WARNING, "failed to create taskprocessor stats for '%s'\n", name);
00456       ao2_ref(p, -1);
00457       return NULL;
00458    }
00459    if (!(p->name = ast_strdup(name))) {
00460       ao2_unlock(tps_singletons);
00461       ao2_ref(p, -1);
00462       return NULL;
00463    }
00464    p->poll_thread_run = 1;
00465    p->poll_thread = AST_PTHREADT_NULL;
00466    if (ast_pthread_create(&p->poll_thread, NULL, tps_processing_function, p) < 0) {
00467       ao2_unlock(tps_singletons);
00468       ast_log(LOG_ERROR, "Taskprocessor '%s' failed to create the processing thread.\n", p->name);
00469       ao2_ref(p, -1);
00470       return NULL;
00471    }
00472    if (!(ao2_link(tps_singletons, p))) {
00473       ao2_unlock(tps_singletons);
00474       ast_log(LOG_ERROR, "Failed to add taskprocessor '%s' to container\n", p->name);
00475       ao2_ref(p, -1);
00476       return NULL;
00477    }
00478    ao2_unlock(tps_singletons);
00479    return p;
00480 }
00481 
00482 /* decrement the taskprocessor reference count and unlink from the container if necessary */
00483 void *ast_taskprocessor_unreference(struct ast_taskprocessor *tps)
00484 {
00485    if (tps) {
00486       ao2_lock(tps_singletons);
00487       ao2_unlink(tps_singletons, tps);
00488       if (ao2_ref(tps, -1) > 1) {
00489          ao2_link(tps_singletons, tps);
00490       }
00491       ao2_unlock(tps_singletons);
00492    }
00493    return NULL;
00494 }
00495 
00496 /* push the task into the taskprocessor queue */   
00497 int ast_taskprocessor_push(struct ast_taskprocessor *tps, int (*task_exe)(void *datap), void *datap)
00498 {
00499    struct tps_task *t;
00500 
00501    if (!tps || !task_exe) {
00502       ast_log(LOG_ERROR, "%s is missing!!\n", (tps) ? "task callback" : "taskprocessor");
00503       return -1;
00504    }
00505    if (!(t = tps_task_alloc(task_exe, datap))) {
00506       ast_log(LOG_ERROR, "failed to allocate task!  Can't push to '%s'\n", tps->name);
00507       return -1;
00508    }
00509    ast_mutex_lock(&tps->taskprocessor_lock);
00510    AST_LIST_INSERT_TAIL(&tps->tps_queue, t, list);
00511    tps->tps_queue_size++;
00512    ast_cond_signal(&tps->poll_cond);
00513    ast_mutex_unlock(&tps->taskprocessor_lock);
00514    return 0;
00515 }
00516