| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include "asterisk/_private.h" |
| 35 |
|
| 36 |
#include "asterisk/timing.h" |
| 37 |
#include "asterisk/lock.h" |
| 38 |
#include "asterisk/cli.h" |
| 39 |
#include "asterisk/utils.h" |
| 40 |
#include "asterisk/time.h" |
| 41 |
#include "asterisk/heap.h" |
| 42 |
#include "asterisk/module.h" |
| 43 |
#include "asterisk/poll-compat.h" |
| 44 |
|
| 45 |
struct timing_holder { |
| 46 |
|
| 47 |
ssize_t __heap_index; |
| 48 |
struct ast_module *mod; |
| 49 |
struct ast_timing_interface *iface; |
| 50 |
}; |
| 51 |
|
| 52 |
static struct ast_heap *timing_interfaces; |
| 53 |
|
| 54 |
struct ast_timer { |
| 55 |
void *data; |
| 56 |
struct timing_holder *holder; |
| 57 |
}; |
| 58 |
|
| 59 |
static int timing_holder_cmp(void *_h1, void *_h2) |
| 60 |
{ |
| 61 |
struct timing_holder *h1 = _h1; |
| 62 |
struct timing_holder *h2 = _h2; |
| 63 |
|
| 64 |
if (h1->iface->priority > h2->iface->priority) { |
| 65 |
return 1; |
| 66 |
} else if (h1->iface->priority == h2->iface->priority) { |
| 67 |
return 0; |
| 68 |
} else { |
| 69 |
return -1; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
void *_ast_register_timing_interface(struct ast_timing_interface *funcs, |
| 74 |
struct ast_module *mod) |
| 75 |
{ |
| 76 |
struct timing_holder *h; |
| 77 |
|
| 78 |
if (!funcs->timer_open || |
| 79 |
!funcs->timer_close || |
| 80 |
!funcs->timer_set_rate || |
| 81 |
!funcs->timer_ack || |
| 82 |
!funcs->timer_get_event || |
| 83 |
!funcs->timer_get_max_rate || |
| 84 |
!funcs->timer_enable_continuous || |
| 85 |
!funcs->timer_disable_continuous || |
| 86 |
!funcs->timer_fd) { |
| 87 |
return NULL; |
| 88 |
} |
| 89 |
|
| 90 |
if (!(h = ast_calloc(1, sizeof(*h)))) { |
| 91 |
return NULL; |
| 92 |
} |
| 93 |
|
| 94 |
h->iface = funcs; |
| 95 |
h->mod = mod; |
| 96 |
|
| 97 |
ast_heap_wrlock(timing_interfaces); |
| 98 |
ast_heap_push(timing_interfaces, h); |
| 99 |
ast_heap_unlock(timing_interfaces); |
| 100 |
|
| 101 |
return h; |
| 102 |
} |
| 103 |
|
| 104 |
int ast_unregister_timing_interface(void *handle) |
| 105 |
{ |
| 106 |
struct timing_holder *h = handle; |
| 107 |
int res = -1; |
| 108 |
|
| 109 |
ast_heap_wrlock(timing_interfaces); |
| 110 |
h = ast_heap_remove(timing_interfaces, h); |
| 111 |
ast_heap_unlock(timing_interfaces); |
| 112 |
|
| 113 |
if (h) { |
| 114 |
ast_free(h); |
| 115 |
h = NULL; |
| 116 |
res = 0; |
| 117 |
} |
| 118 |
|
| 119 |
return res; |
| 120 |
} |
| 121 |
|
| 122 |
struct ast_timer *ast_timer_open(void) |
| 123 |
{ |
| 124 |
void *data = NULL; |
| 125 |
struct timing_holder *h; |
| 126 |
struct ast_timer *t = NULL; |
| 127 |
int idx = 1; |
| 128 |
|
| 129 |
ast_heap_rdlock(timing_interfaces); |
| 130 |
|
| 131 |
while ((h = ast_heap_peek(timing_interfaces, idx))) { |
| 132 |
if (ast_module_running_ref(h->mod)) { |
| 133 |
data = h->iface->timer_open(); |
| 134 |
break; |
| 135 |
} |
| 136 |
idx++; |
| 137 |
} |
| 138 |
|
| 139 |
if (data) { |
| 140 |
if (!(t = ast_calloc(1, sizeof(*t)))) { |
| 141 |
h->iface->timer_close(data); |
| 142 |
ast_module_unref(h->mod); |
| 143 |
} else { |
| 144 |
t->data = data; |
| 145 |
t->holder = h; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
ast_heap_unlock(timing_interfaces); |
| 150 |
|
| 151 |
return t; |
| 152 |
} |
| 153 |
|
| 154 |
void ast_timer_close(struct ast_timer *handle) |
| 155 |
{ |
| 156 |
handle->holder->iface->timer_close(handle->data); |
| 157 |
ast_module_unref(handle->holder->mod); |
| 158 |
ast_free(handle); |
| 159 |
} |
| 160 |
|
| 161 |
int ast_timer_fd(const struct ast_timer *handle) |
| 162 |
{ |
| 163 |
return handle->holder->iface->timer_fd(handle->data); |
| 164 |
} |
| 165 |
|
| 166 |
int ast_timer_set_rate(const struct ast_timer *handle, unsigned int rate) |
| 167 |
{ |
| 168 |
return handle->holder->iface->timer_set_rate(handle->data, rate); |
| 169 |
} |
| 170 |
|
| 171 |
int ast_timer_ack(const struct ast_timer *handle, unsigned int quantity) |
| 172 |
{ |
| 173 |
return handle->holder->iface->timer_ack(handle->data, quantity); |
| 174 |
} |
| 175 |
|
| 176 |
int ast_timer_enable_continuous(const struct ast_timer *handle) |
| 177 |
{ |
| 178 |
return handle->holder->iface->timer_enable_continuous(handle->data); |
| 179 |
} |
| 180 |
|
| 181 |
int ast_timer_disable_continuous(const struct ast_timer *handle) |
| 182 |
{ |
| 183 |
return handle->holder->iface->timer_disable_continuous(handle->data); |
| 184 |
} |
| 185 |
|
| 186 |
enum ast_timer_event ast_timer_get_event(const struct ast_timer *handle) |
| 187 |
{ |
| 188 |
return handle->holder->iface->timer_get_event(handle->data); |
| 189 |
} |
| 190 |
|
| 191 |
unsigned int ast_timer_get_max_rate(const struct ast_timer *handle) |
| 192 |
{ |
| 193 |
return handle->holder->iface->timer_get_max_rate(handle->data); |
| 194 |
} |
| 195 |
|
| 196 |
const char *ast_timer_get_name(const struct ast_timer *handle) |
| 197 |
{ |
| 198 |
return handle->holder->iface->name; |
| 199 |
} |
| 200 |
|
| 201 |
static char *timing_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 202 |
{ |
| 203 |
struct ast_timer *timer; |
| 204 |
int count = 0; |
| 205 |
struct timeval start, end; |
| 206 |
unsigned int test_rate = 50; |
| 207 |
|
| 208 |
switch (cmd) { |
| 209 |
case CLI_INIT: |
| 210 |
e->command = "timing test"; |
| 211 |
e->usage = "Usage: timing test <rate>\n" |
| 212 |
" Test a timer with a specified rate, 50/sec by default.\n" |
| 213 |
""; |
| 214 |
return NULL; |
| 215 |
case CLI_GENERATE: |
| 216 |
return NULL; |
| 217 |
} |
| 218 |
|
| 219 |
if (a->argc != 2 && a->argc != 3) { |
| 220 |
return CLI_SHOWUSAGE; |
| 221 |
} |
| 222 |
|
| 223 |
if (a->argc == 3) { |
| 224 |
unsigned int rate; |
| 225 |
if (sscanf(a->argv[2], "%30u", &rate) == 1) { |
| 226 |
test_rate = rate; |
| 227 |
} else { |
| 228 |
ast_cli(a->fd, "Invalid rate '%s', using default of %u\n", a->argv[2], test_rate); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
ast_cli(a->fd, "Attempting to test a timer with %u ticks per second.\n", test_rate); |
| 233 |
|
| 234 |
if (!(timer = ast_timer_open())) { |
| 235 |
ast_cli(a->fd, "Failed to open timing fd\n"); |
| 236 |
return CLI_FAILURE; |
| 237 |
} |
| 238 |
|
| 239 |
ast_cli(a->fd, "Using the '%s' timing module for this test.\n", timer->holder->iface->name); |
| 240 |
|
| 241 |
start = ast_tvnow(); |
| 242 |
|
| 243 |
ast_timer_set_rate(timer, test_rate); |
| 244 |
|
| 245 |
while (ast_tvdiff_ms((end = ast_tvnow()), start) < 1000) { |
| 246 |
int res; |
| 247 |
struct pollfd pfd = { |
| 248 |
.fd = ast_timer_fd(timer), |
| 249 |
.events = POLLIN | POLLPRI, |
| 250 |
}; |
| 251 |
|
| 252 |
res = ast_poll(&pfd, 1, 100); |
| 253 |
|
| 254 |
if (res == 1) { |
| 255 |
count++; |
| 256 |
if (ast_timer_ack(timer, 1) < 0) { |
| 257 |
ast_cli(a->fd, "Timer failed to acknowledge.\n"); |
| 258 |
ast_timer_close(timer); |
| 259 |
return CLI_FAILURE; |
| 260 |
} |
| 261 |
} else if (!res) { |
| 262 |
ast_cli(a->fd, "poll() timed out! This is bad.\n"); |
| 263 |
} else if (errno != EAGAIN && errno != EINTR) { |
| 264 |
ast_cli(a->fd, "poll() returned error: %s\n", strerror(errno)); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
ast_timer_close(timer); |
| 269 |
timer = NULL; |
| 270 |
|
| 271 |
ast_cli(a->fd, "It has been %" PRIi64 " milliseconds, and we got %d timer ticks\n", |
| 272 |
ast_tvdiff_ms(end, start), count); |
| 273 |
|
| 274 |
return CLI_SUCCESS; |
| 275 |
} |
| 276 |
|
| 277 |
static struct ast_cli_entry cli_timing[] = { |
| 278 |
AST_CLI_DEFINE(timing_test, "Run a timing test"), |
| 279 |
}; |
| 280 |
|
| 281 |
static void timing_shutdown(void) |
| 282 |
{ |
| 283 |
ast_cli_unregister_multiple(cli_timing, ARRAY_LEN(cli_timing)); |
| 284 |
|
| 285 |
ast_heap_destroy(timing_interfaces); |
| 286 |
timing_interfaces = NULL; |
| 287 |
} |
| 288 |
|
| 289 |
int ast_timing_init(void) |
| 290 |
{ |
| 291 |
if (!(timing_interfaces = ast_heap_create(2, timing_holder_cmp, 0))) { |
| 292 |
return -1; |
| 293 |
} |
| 294 |
|
| 295 |
ast_register_cleanup(timing_shutdown); |
| 296 |
|
| 297 |
return ast_cli_register_multiple(cli_timing, ARRAY_LEN(cli_timing)); |
| 298 |
} |