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