core: Fix unused variable error in handle_show_sysinfo.
[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 #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         /*! Do _not_ move this from the beginning of the struct. */
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
128         ast_heap_rdlock(timing_interfaces);
129
130         if ((h = ast_heap_peek(timing_interfaces, 1))) {
131                 data = h->iface->timer_open();
132                 ast_module_ref(h->mod);
133         }
134
135         if (data) {
136                 if (!(t = ast_calloc(1, sizeof(*t)))) {
137                         h->iface->timer_close(data);
138                 } else {
139                         t->data = data;
140                         t->holder = h;
141                 }
142         }
143
144         ast_heap_unlock(timing_interfaces);
145
146         return t;
147 }
148
149 void ast_timer_close(struct ast_timer *handle)
150 {
151         handle->holder->iface->timer_close(handle->data);
152         ast_module_unref(handle->holder->mod);
153         ast_free(handle);
154 }
155
156 int ast_timer_fd(const struct ast_timer *handle)
157 {
158         return handle->holder->iface->timer_fd(handle->data);
159 }
160
161 int ast_timer_set_rate(const struct ast_timer *handle, unsigned int rate)
162 {
163         return handle->holder->iface->timer_set_rate(handle->data, rate);
164 }
165
166 int ast_timer_ack(const struct ast_timer *handle, unsigned int quantity)
167 {
168         return handle->holder->iface->timer_ack(handle->data, quantity);
169 }
170
171 int ast_timer_enable_continuous(const struct ast_timer *handle)
172 {
173         return handle->holder->iface->timer_enable_continuous(handle->data);
174 }
175
176 int ast_timer_disable_continuous(const struct ast_timer *handle)
177 {
178         return handle->holder->iface->timer_disable_continuous(handle->data);
179 }
180
181 enum ast_timer_event ast_timer_get_event(const struct ast_timer *handle)
182 {
183         return handle->holder->iface->timer_get_event(handle->data);
184 }
185
186 unsigned int ast_timer_get_max_rate(const struct ast_timer *handle)
187 {
188         return handle->holder->iface->timer_get_max_rate(handle->data);
189 }
190
191 const char *ast_timer_get_name(const struct ast_timer *handle)
192 {
193         return handle->holder->iface->name;
194 }
195
196 static char *timing_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
197 {
198         struct ast_timer *timer;
199         int count = 0;
200         struct timeval start, end;
201         unsigned int test_rate = 50;
202
203         switch (cmd) {
204         case CLI_INIT:
205                 e->command = "timing test";
206                 e->usage = "Usage: timing test <rate>\n"
207                            "   Test a timer with a specified rate, 50/sec by default.\n"
208                            "";
209                 return NULL;
210         case CLI_GENERATE:
211                 return NULL;
212         }
213
214         if (a->argc != 2 && a->argc != 3) {
215                 return CLI_SHOWUSAGE;
216         }
217
218         if (a->argc == 3) {
219                 unsigned int rate;
220                 if (sscanf(a->argv[2], "%30u", &rate) == 1) {
221                         test_rate = rate;
222                 } else {
223                         ast_cli(a->fd, "Invalid rate '%s', using default of %u\n", a->argv[2], test_rate);
224                 }
225         }
226
227         ast_cli(a->fd, "Attempting to test a timer with %u ticks per second.\n", test_rate);
228
229         if (!(timer = ast_timer_open())) {
230                 ast_cli(a->fd, "Failed to open timing fd\n");
231                 return CLI_FAILURE;
232         }
233
234         ast_cli(a->fd, "Using the '%s' timing module for this test.\n", timer->holder->iface->name);
235
236         start = ast_tvnow();
237
238         ast_timer_set_rate(timer, test_rate);
239
240         while (ast_tvdiff_ms((end = ast_tvnow()), start) < 1000) {
241                 int res;
242                 struct pollfd pfd = {
243                         .fd = ast_timer_fd(timer),
244                         .events = POLLIN | POLLPRI,
245                 };
246
247                 res = ast_poll(&pfd, 1, 100);
248
249                 if (res == 1) {
250                         count++;
251                         if (ast_timer_ack(timer, 1) < 0) {
252                                 ast_cli(a->fd, "Timer failed to acknowledge.\n");
253                                 ast_timer_close(timer);
254                                 return CLI_FAILURE;
255                         }
256                 } else if (!res) {
257                         ast_cli(a->fd, "poll() timed out!  This is bad.\n");
258                 } else if (errno != EAGAIN && errno != EINTR) {
259                         ast_cli(a->fd, "poll() returned error: %s\n", strerror(errno));
260                 }
261         }
262
263         ast_timer_close(timer);
264         timer = NULL;
265
266         ast_cli(a->fd, "It has been %" PRIi64 " milliseconds, and we got %d timer ticks\n",
267                 ast_tvdiff_ms(end, start), count);
268
269         return CLI_SUCCESS;
270 }
271
272 static struct ast_cli_entry cli_timing[] = {
273         AST_CLI_DEFINE(timing_test, "Run a timing test"),
274 };
275
276 static void timing_shutdown(void)
277 {
278         ast_cli_unregister_multiple(cli_timing, ARRAY_LEN(cli_timing));
279
280         ast_heap_destroy(timing_interfaces);
281         timing_interfaces = NULL;
282 }
283
284 int ast_timing_init(void)
285 {
286         if (!(timing_interfaces = ast_heap_create(2, timing_holder_cmp, 0))) {
287                 return -1;
288         }
289
290         ast_register_cleanup(timing_shutdown);
291
292         return ast_cli_register_multiple(cli_timing, ARRAY_LEN(cli_timing));
293 }