2008-06-12 14:21:32 +00:00
|
|
|
/*
|
|
|
|
* Asterisk -- An open source telephony toolkit.
|
|
|
|
*
|
2009-02-17 21:22:40 +00:00
|
|
|
* Copyright (C) 2008 - 2009, Digium, Inc.
|
2008-06-12 14:21:32 +00:00
|
|
|
*
|
|
|
|
* Kevin P. Fleming <kpfleming@digium.com>
|
2009-02-17 21:22:40 +00:00
|
|
|
* Russell Bryant <russell@digium.com>
|
2008-06-12 14:21:32 +00:00
|
|
|
*
|
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file
|
|
|
|
*
|
|
|
|
* \brief Timing source management
|
|
|
|
*
|
|
|
|
* \author Kevin P. Fleming <kpfleming@digium.com>
|
2009-02-17 21:22:40 +00:00
|
|
|
* \author Russell Bryant <russell@digium.com>
|
2008-06-12 14:21:32 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "asterisk.h"
|
|
|
|
|
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|
|
|
|
2008-06-16 13:03:40 +00:00
|
|
|
#include "asterisk/_private.h"
|
|
|
|
|
2008-06-12 14:21:32 +00:00
|
|
|
#include "asterisk/timing.h"
|
|
|
|
#include "asterisk/lock.h"
|
2008-06-16 13:03:40 +00:00
|
|
|
#include "asterisk/cli.h"
|
|
|
|
#include "asterisk/utils.h"
|
|
|
|
#include "asterisk/time.h"
|
2009-02-17 21:22:40 +00:00
|
|
|
#include "asterisk/heap.h"
|
|
|
|
#include "asterisk/module.h"
|
|
|
|
|
|
|
|
struct timing_holder {
|
|
|
|
/*! Do _not_ move this from the beginning of the struct. */
|
|
|
|
ssize_t __heap_index;
|
|
|
|
struct ast_module *mod;
|
|
|
|
struct ast_timing_interface *iface;
|
|
|
|
};
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
static struct ast_heap *timing_interfaces;
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
static int timing_holder_cmp(void *_h1, void *_h2)
|
|
|
|
{
|
|
|
|
struct timing_holder *h1 = _h1;
|
|
|
|
struct timing_holder *h2 = _h2;
|
|
|
|
|
|
|
|
if (h1->iface->priority > h2->iface->priority) {
|
|
|
|
return 1;
|
|
|
|
} else if (h1->iface->priority == h2->iface->priority) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
void *_ast_register_timing_interface(struct ast_timing_interface *funcs,
|
|
|
|
struct ast_module *mod)
|
2008-06-12 14:21:32 +00:00
|
|
|
{
|
2009-02-17 21:22:40 +00:00
|
|
|
struct timing_holder *h;
|
|
|
|
|
2008-06-12 14:21:32 +00:00
|
|
|
if (!funcs->timer_open ||
|
|
|
|
!funcs->timer_close ||
|
2008-06-13 12:45:50 +00:00
|
|
|
!funcs->timer_set_rate ||
|
2008-06-12 14:21:32 +00:00
|
|
|
!funcs->timer_ack ||
|
|
|
|
!funcs->timer_get_event ||
|
2008-06-26 15:37:01 +00:00
|
|
|
!funcs->timer_get_max_rate ||
|
2008-06-12 14:21:32 +00:00
|
|
|
!funcs->timer_enable_continuous ||
|
|
|
|
!funcs->timer_disable_continuous) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
if (!(h = ast_calloc(1, sizeof(*h)))) {
|
2008-06-12 14:21:32 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
h->iface = funcs;
|
|
|
|
h->mod = mod;
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
ast_heap_wrlock(timing_interfaces);
|
|
|
|
ast_heap_push(timing_interfaces, h);
|
|
|
|
ast_heap_unlock(timing_interfaces);
|
|
|
|
|
|
|
|
return h;
|
2008-06-12 14:21:32 +00:00
|
|
|
}
|
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
int ast_unregister_timing_interface(void *handle)
|
2008-06-12 14:21:32 +00:00
|
|
|
{
|
2009-02-17 21:22:40 +00:00
|
|
|
struct timing_holder *h = handle;
|
|
|
|
int res = -1;
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
ast_heap_wrlock(timing_interfaces);
|
|
|
|
h = ast_heap_remove(timing_interfaces, h);
|
|
|
|
ast_heap_unlock(timing_interfaces);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
if (h) {
|
|
|
|
ast_free(h);
|
|
|
|
h = NULL;
|
|
|
|
res = 0;
|
|
|
|
}
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
return res;
|
2008-06-12 14:21:32 +00:00
|
|
|
}
|
|
|
|
|
2008-06-13 12:45:50 +00:00
|
|
|
int ast_timer_open(void)
|
2008-06-12 14:21:32 +00:00
|
|
|
{
|
2009-02-17 21:22:40 +00:00
|
|
|
int fd = -1;
|
|
|
|
struct timing_holder *h;
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
ast_heap_rdlock(timing_interfaces);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
if ((h = ast_heap_peek(timing_interfaces, 1))) {
|
|
|
|
fd = h->iface->timer_open();
|
|
|
|
ast_module_ref(h->mod);
|
2008-06-12 14:21:32 +00:00
|
|
|
}
|
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
ast_heap_unlock(timing_interfaces);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
return fd;
|
2008-06-12 14:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ast_timer_close(int timer)
|
|
|
|
{
|
2009-02-17 21:22:40 +00:00
|
|
|
struct timing_holder *h;
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
ast_heap_rdlock(timing_interfaces);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
if ((h = ast_heap_peek(timing_interfaces, 1))) {
|
|
|
|
h->iface->timer_close(timer);
|
|
|
|
ast_module_unref(h->mod);
|
|
|
|
}
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
ast_heap_unlock(timing_interfaces);
|
2008-06-13 12:45:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ast_timer_set_rate(int handle, unsigned int rate)
|
|
|
|
{
|
2009-02-17 21:22:40 +00:00
|
|
|
struct timing_holder *h;
|
|
|
|
int res = -1;
|
2008-06-13 12:45:50 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
ast_heap_rdlock(timing_interfaces);
|
2008-06-13 12:45:50 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
if ((h = ast_heap_peek(timing_interfaces, 1))) {
|
|
|
|
res = h->iface->timer_set_rate(handle, rate);
|
2008-06-13 12:45:50 +00:00
|
|
|
}
|
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
ast_heap_unlock(timing_interfaces);
|
2008-06-13 12:45:50 +00:00
|
|
|
|
|
|
|
return res;
|
2008-06-12 14:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ast_timer_ack(int handle, unsigned int quantity)
|
|
|
|
{
|
2009-02-17 21:22:40 +00:00
|
|
|
struct timing_holder *h;
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
ast_heap_rdlock(timing_interfaces);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
if ((h = ast_heap_peek(timing_interfaces, 1))) {
|
|
|
|
h->iface->timer_ack(handle, quantity);
|
|
|
|
}
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
ast_heap_unlock(timing_interfaces);
|
2008-06-12 14:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ast_timer_enable_continuous(int handle)
|
|
|
|
{
|
2009-02-17 21:22:40 +00:00
|
|
|
struct timing_holder *h;
|
|
|
|
int res = -1;
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
ast_heap_rdlock(timing_interfaces);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
if ((h = ast_heap_peek(timing_interfaces, 1))) {
|
|
|
|
res = h->iface->timer_enable_continuous(handle);
|
2008-06-12 14:21:32 +00:00
|
|
|
}
|
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
ast_heap_unlock(timing_interfaces);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
return res;
|
2008-06-12 14:21:32 +00:00
|
|
|
}
|
|
|
|
|
2008-06-16 12:48:11 +00:00
|
|
|
int ast_timer_disable_continuous(int handle)
|
2008-06-12 14:21:32 +00:00
|
|
|
{
|
2009-02-17 21:22:40 +00:00
|
|
|
struct timing_holder *h;
|
|
|
|
int res = -1;
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
ast_heap_rdlock(timing_interfaces);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
if ((h = ast_heap_peek(timing_interfaces, 1))) {
|
|
|
|
res = h->iface->timer_disable_continuous(handle);
|
2008-06-12 14:21:32 +00:00
|
|
|
}
|
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
ast_heap_unlock(timing_interfaces);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
return res;
|
2008-06-12 14:21:32 +00:00
|
|
|
}
|
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
enum ast_timer_event ast_timer_get_event(int handle)
|
2008-06-12 14:21:32 +00:00
|
|
|
{
|
2009-02-17 21:22:40 +00:00
|
|
|
struct timing_holder *h;
|
|
|
|
enum ast_timer_event res = -1;
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
ast_heap_rdlock(timing_interfaces);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
if ((h = ast_heap_peek(timing_interfaces, 1))) {
|
|
|
|
res = h->iface->timer_get_event(handle);
|
2008-06-12 14:21:32 +00:00
|
|
|
}
|
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
ast_heap_unlock(timing_interfaces);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
return res;
|
2008-06-12 14:21:32 +00:00
|
|
|
}
|
2008-06-16 13:03:40 +00:00
|
|
|
|
2008-06-26 15:37:01 +00:00
|
|
|
unsigned int ast_timer_get_max_rate(int handle)
|
|
|
|
{
|
2009-02-17 21:22:40 +00:00
|
|
|
struct timing_holder *h;
|
|
|
|
unsigned int res = 0;
|
2008-06-26 15:37:01 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
ast_heap_rdlock(timing_interfaces);
|
2008-06-26 15:37:01 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
if ((h = ast_heap_peek(timing_interfaces, 1))) {
|
|
|
|
res = h->iface->timer_get_max_rate(handle);
|
|
|
|
}
|
2008-06-26 15:37:01 +00:00
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
ast_heap_unlock(timing_interfaces);
|
2008-06-26 15:37:01 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-06-16 13:03:40 +00:00
|
|
|
static char *timing_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
|
|
|
|
{
|
|
|
|
int fd, count = 0;
|
|
|
|
struct timeval start, end;
|
2008-06-19 18:30:49 +00:00
|
|
|
unsigned int test_rate = 50;
|
2009-02-17 21:22:40 +00:00
|
|
|
struct timing_holder *h;
|
2008-06-16 13:03:40 +00:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case CLI_INIT:
|
|
|
|
e->command = "timing test";
|
2008-06-19 18:30:49 +00:00
|
|
|
e->usage = "Usage: timing test <rate>\n"
|
|
|
|
" Test a timer with a specified rate, 100/sec by default.\n"
|
|
|
|
"";
|
2008-06-16 13:03:40 +00:00
|
|
|
return NULL;
|
|
|
|
case CLI_GENERATE:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-06-19 18:30:49 +00:00
|
|
|
if (a->argc != 2 && a->argc != 3) {
|
|
|
|
return CLI_SHOWUSAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a->argc == 3) {
|
|
|
|
unsigned int rate;
|
|
|
|
if (sscanf(a->argv[2], "%u", &rate) == 1) {
|
|
|
|
test_rate = rate;
|
|
|
|
} else {
|
|
|
|
ast_cli(a->fd, "Invalid rate '%s', using default of %u\n", a->argv[2], test_rate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
ast_cli(a->fd, "Attempting to test a timer with %u ticks per second.\n", test_rate);
|
2008-06-16 13:03:40 +00:00
|
|
|
|
|
|
|
if ((fd = ast_timer_open()) == -1) {
|
|
|
|
ast_cli(a->fd, "Failed to open timing fd\n");
|
|
|
|
return CLI_FAILURE;
|
|
|
|
}
|
|
|
|
|
2009-02-17 21:22:40 +00:00
|
|
|
ast_heap_rdlock(timing_interfaces);
|
|
|
|
if ((h = ast_heap_peek(timing_interfaces, 1))) {
|
|
|
|
ast_cli(a->fd, "Using the '%s' timing module for this test.\n", h->iface->name);
|
|
|
|
h = NULL;
|
|
|
|
}
|
|
|
|
ast_heap_unlock(timing_interfaces);
|
|
|
|
|
2008-06-16 13:03:40 +00:00
|
|
|
start = ast_tvnow();
|
|
|
|
|
2008-06-19 18:30:49 +00:00
|
|
|
ast_timer_set_rate(fd, test_rate);
|
2008-06-16 13:03:40 +00:00
|
|
|
|
|
|
|
while (ast_tvdiff_ms((end = ast_tvnow()), start) < 1000) {
|
|
|
|
int res;
|
|
|
|
struct pollfd pfd = {
|
|
|
|
.fd = fd,
|
|
|
|
.events = POLLIN | POLLPRI,
|
|
|
|
};
|
|
|
|
|
2009-03-25 02:03:13 +00:00
|
|
|
res = ast_poll(&pfd, 1, 100);
|
2008-06-16 13:03:40 +00:00
|
|
|
|
|
|
|
if (res == 1) {
|
|
|
|
count++;
|
|
|
|
ast_timer_ack(fd, 1);
|
|
|
|
} else if (!res) {
|
|
|
|
ast_cli(a->fd, "poll() timed out! This is bad.\n");
|
|
|
|
} else if (errno != EAGAIN && errno != EINTR) {
|
|
|
|
ast_cli(a->fd, "poll() returned error: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_timer_close(fd);
|
|
|
|
|
|
|
|
ast_cli(a->fd, "It has been %d milliseconds, and we got %d timer ticks\n",
|
|
|
|
ast_tvdiff_ms(end, start), count);
|
|
|
|
|
|
|
|
return CLI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ast_cli_entry cli_timing[] = {
|
|
|
|
AST_CLI_DEFINE(timing_test, "Run a timing test"),
|
|
|
|
};
|
|
|
|
|
|
|
|
int ast_timing_init(void)
|
|
|
|
{
|
2009-02-17 21:22:40 +00:00
|
|
|
if (!(timing_interfaces = ast_heap_create(2, timing_holder_cmp, 0))) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-06-16 13:03:40 +00:00
|
|
|
return ast_cli_register_multiple(cli_timing, ARRAY_LEN(cli_timing));
|
|
|
|
}
|