Thu Apr 28 2011 17:15:25

Asterisk developer's documentation


timing.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2008 - 2009, Digium, Inc.
00005  *
00006  * Kevin P. Fleming <kpfleming@digium.com>
00007  * Russell Bryant <russell@digium.com>
00008  *
00009  * See http://www.asterisk.org for more information about
00010  * the Asterisk project. Please do not directly contact
00011  * any of the maintainers of this project for assistance;
00012  * the project provides a web site, mailing lists and IRC
00013  * channels for your use.
00014  *
00015  * This program is free software, distributed under the terms of
00016  * the GNU General Public License Version 2. See the LICENSE file
00017  * at the top of the source tree.
00018  */
00019 
00020 /*! \file
00021  *
00022  * \brief Timing source management
00023  *
00024  * \author Kevin P. Fleming <kpfleming@digium.com>
00025  * \author Russell Bryant <russell@digium.com>
00026  */
00027 
00028 #include "asterisk.h"
00029 
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 294277 $")
00031 
00032 #include "asterisk/_private.h"
00033 
00034 #include "asterisk/timing.h"
00035 #include "asterisk/lock.h"
00036 #include "asterisk/cli.h"
00037 #include "asterisk/utils.h"
00038 #include "asterisk/time.h"
00039 #include "asterisk/heap.h"
00040 #include "asterisk/module.h"
00041 #include "asterisk/poll-compat.h"
00042 
00043 struct timing_holder {
00044    /*! Do _not_ move this from the beginning of the struct. */
00045    ssize_t __heap_index;
00046    struct ast_module *mod;
00047    struct ast_timing_interface *iface;
00048 };
00049 
00050 static struct ast_heap *timing_interfaces;
00051 
00052 struct ast_timer {
00053    int fd;
00054    struct timing_holder *holder;
00055 };
00056 
00057 static int timing_holder_cmp(void *_h1, void *_h2)
00058 {
00059    struct timing_holder *h1 = _h1;
00060    struct timing_holder *h2 = _h2;
00061 
00062    if (h1->iface->priority > h2->iface->priority) {
00063       return 1;
00064    } else if (h1->iface->priority == h2->iface->priority) {
00065       return 0;
00066    } else {
00067       return -1;
00068    }
00069 }
00070 
00071 void *_ast_register_timing_interface(struct ast_timing_interface *funcs, 
00072                  struct ast_module *mod)
00073 {
00074    struct timing_holder *h;
00075 
00076    if (!funcs->timer_open ||
00077        !funcs->timer_close ||
00078        !funcs->timer_set_rate ||
00079        !funcs->timer_ack ||
00080        !funcs->timer_get_event ||
00081        !funcs->timer_get_max_rate ||
00082        !funcs->timer_enable_continuous ||
00083        !funcs->timer_disable_continuous) {
00084       return NULL;
00085    }
00086 
00087    if (!(h = ast_calloc(1, sizeof(*h)))) {
00088       return NULL;
00089    }
00090 
00091    h->iface = funcs;
00092    h->mod = mod;
00093 
00094    ast_heap_wrlock(timing_interfaces);
00095    ast_heap_push(timing_interfaces, h);
00096    ast_heap_unlock(timing_interfaces);
00097 
00098    return h;
00099 }
00100 
00101 int ast_unregister_timing_interface(void *handle)
00102 {
00103    struct timing_holder *h = handle;
00104    int res = -1;
00105 
00106    ast_heap_wrlock(timing_interfaces);
00107    h = ast_heap_remove(timing_interfaces, h);
00108    ast_heap_unlock(timing_interfaces);
00109 
00110    if (h) {
00111       ast_free(h);
00112       h = NULL;
00113       res = 0;
00114    }
00115 
00116    return res;
00117 }
00118 
00119 struct ast_timer *ast_timer_open(void)
00120 {
00121    int fd = -1;
00122    struct timing_holder *h;
00123    struct ast_timer *t = NULL;
00124 
00125    ast_heap_rdlock(timing_interfaces);
00126 
00127    if ((h = ast_heap_peek(timing_interfaces, 1))) {
00128       fd = h->iface->timer_open();
00129       ast_module_ref(h->mod);
00130    }
00131 
00132    if (fd != -1) {
00133       if (!(t = ast_calloc(1, sizeof(*t)))) {
00134          h->iface->timer_close(fd);
00135       } else {
00136          t->fd = fd;
00137          t->holder = h;
00138       }
00139    }
00140 
00141    ast_heap_unlock(timing_interfaces);
00142 
00143    return t;
00144 }
00145 
00146 void ast_timer_close(struct ast_timer *handle)
00147 {
00148    handle->holder->iface->timer_close(handle->fd);
00149    ast_module_unref(handle->holder->mod);
00150    ast_free(handle);
00151 }
00152 
00153 int ast_timer_fd(const struct ast_timer *handle)
00154 {
00155    return handle->fd;
00156 }
00157 
00158 int ast_timer_set_rate(const struct ast_timer *handle, unsigned int rate)
00159 {
00160    int res = -1;
00161 
00162    res = handle->holder->iface->timer_set_rate(handle->fd, rate);
00163 
00164    return res;
00165 }
00166 
00167 void ast_timer_ack(const struct ast_timer *handle, unsigned int quantity)
00168 {
00169    handle->holder->iface->timer_ack(handle->fd, quantity);
00170 }
00171 
00172 int ast_timer_enable_continuous(const struct ast_timer *handle)
00173 {
00174    int res = -1;
00175 
00176    res = handle->holder->iface->timer_enable_continuous(handle->fd);
00177 
00178    return res;
00179 }
00180 
00181 int ast_timer_disable_continuous(const struct ast_timer *handle)
00182 {
00183    int res = -1;
00184 
00185    res = handle->holder->iface->timer_disable_continuous(handle->fd);
00186 
00187    return res;
00188 }
00189 
00190 enum ast_timer_event ast_timer_get_event(const struct ast_timer *handle)
00191 {
00192    enum ast_timer_event res = -1;
00193 
00194    res = handle->holder->iface->timer_get_event(handle->fd);
00195 
00196    return res;
00197 }
00198 
00199 unsigned int ast_timer_get_max_rate(const struct ast_timer *handle)
00200 {
00201    unsigned int res = 0;
00202 
00203    res = handle->holder->iface->timer_get_max_rate(handle->fd);
00204 
00205    return res;
00206 }
00207 
00208 const char *ast_timer_get_name(const struct ast_timer *handle)
00209 {
00210    return handle->holder->iface->name;
00211 }
00212 
00213 static char *timing_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00214 {
00215    struct ast_timer *timer;
00216    int count = 0;
00217    struct timeval start, end;
00218    unsigned int test_rate = 50;
00219 
00220    switch (cmd) {
00221    case CLI_INIT:
00222       e->command = "timing test";
00223       e->usage = "Usage: timing test <rate>\n"
00224                  "   Test a timer with a specified rate, 50/sec by default.\n"
00225                  "";
00226       return NULL;
00227    case CLI_GENERATE:
00228       return NULL;
00229    }
00230 
00231    if (a->argc != 2 && a->argc != 3) {
00232       return CLI_SHOWUSAGE;
00233    }
00234 
00235    if (a->argc == 3) {
00236       unsigned int rate;
00237       if (sscanf(a->argv[2], "%30u", &rate) == 1) {
00238          test_rate = rate;
00239       } else {
00240          ast_cli(a->fd, "Invalid rate '%s', using default of %u\n", a->argv[2], test_rate);  
00241       }
00242    }
00243 
00244    ast_cli(a->fd, "Attempting to test a timer with %u ticks per second.\n", test_rate);
00245 
00246    if (!(timer = ast_timer_open())) {
00247       ast_cli(a->fd, "Failed to open timing fd\n");
00248       return CLI_FAILURE;
00249    }
00250 
00251    ast_cli(a->fd, "Using the '%s' timing module for this test.\n", timer->holder->iface->name);
00252 
00253    start = ast_tvnow();
00254 
00255    ast_timer_set_rate(timer, test_rate);
00256 
00257    while (ast_tvdiff_ms((end = ast_tvnow()), start) < 1000) {
00258       int res;
00259       struct pollfd pfd = {
00260          .fd = ast_timer_fd(timer),
00261          .events = POLLIN | POLLPRI,
00262       };
00263 
00264       res = ast_poll(&pfd, 1, 100);
00265 
00266       if (res == 1) {
00267          count++;
00268          ast_timer_ack(timer, 1);
00269       } else if (!res) {
00270          ast_cli(a->fd, "poll() timed out!  This is bad.\n");
00271       } else if (errno != EAGAIN && errno != EINTR) {
00272          ast_cli(a->fd, "poll() returned error: %s\n", strerror(errno));
00273       }
00274    }
00275 
00276    ast_timer_close(timer);
00277 
00278    ast_cli(a->fd, "It has been %d milliseconds, and we got %d timer ticks\n", 
00279       ast_tvdiff_ms(end, start), count);
00280 
00281    return CLI_SUCCESS;
00282 }
00283 
00284 static struct ast_cli_entry cli_timing[] = {
00285    AST_CLI_DEFINE(timing_test, "Run a timing test"),
00286 };
00287 
00288 int ast_timing_init(void)
00289 {
00290    if (!(timing_interfaces = ast_heap_create(2, timing_holder_cmp, 0))) {
00291       return -1;
00292    }
00293 
00294    return ast_cli_register_multiple(cli_timing, ARRAY_LEN(cli_timing));
00295 }