timing: Improve performance for most timing implementations.
[asterisk/asterisk.git] / res / res_timing_kqueue.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2010, Digium, Inc.
5  *
6  * Tilghman Lesher <tlesher AT digium DOT com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*!
20  * \file
21  * \author Tilghman Lesher \verbatim <tlesher AT digium DOT com> \endverbatim
22  *
23  * \brief kqueue timing interface
24  *
25  * \ingroup resource
26  */
27
28 /*** MODULEINFO
29         <depend>kqueue</depend>
30         <conflict>launchd</conflict>
31         <support_level>extended</support_level>
32  ***/
33
34 #include "asterisk.h"
35
36 #include <sys/types.h>
37 #include <sys/event.h>
38 #include <sys/time.h>
39
40 #include "asterisk/module.h"
41 #include "asterisk/astobj2.h"
42 #include "asterisk/timing.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/utils.h"
45 #include "asterisk/time.h"
46 #include "asterisk/test.h"
47 #include "asterisk/poll-compat.h"       /* for ast_poll() */
48
49 static void *timing_funcs_handle;
50
51 static void *kqueue_timer_open(void);
52 static void kqueue_timer_close(void *data);
53 static int kqueue_timer_set_rate(void *data, unsigned int rate);
54 static int kqueue_timer_ack(void *data, unsigned int quantity);
55 static int kqueue_timer_enable_continuous(void *data);
56 static int kqueue_timer_disable_continuous(void *data);
57 static enum ast_timer_event kqueue_timer_get_event(void *data);
58 static unsigned int kqueue_timer_get_max_rate(void *data);
59 static int kqueue_timer_fd(void *data);
60
61 static struct ast_timing_interface kqueue_timing = {
62         .name = "kqueue",
63         .priority = 150,
64         .timer_open = kqueue_timer_open,
65         .timer_close = kqueue_timer_close,
66         .timer_set_rate = kqueue_timer_set_rate,
67         .timer_ack = kqueue_timer_ack,
68         .timer_enable_continuous = kqueue_timer_enable_continuous,
69         .timer_disable_continuous = kqueue_timer_disable_continuous,
70         .timer_get_event = kqueue_timer_get_event,
71         .timer_get_max_rate = kqueue_timer_get_max_rate,
72         .timer_fd = kqueue_timer_fd,
73 };
74
75 struct kqueue_timer {
76         int handle;
77         uint64_t nsecs;
78         uint64_t unacked;
79         unsigned int is_continuous:1;
80 };
81
82 static void timer_destroy(void *obj)
83 {
84         struct kqueue_timer *timer = obj;
85         close(timer->handle);
86 }
87
88 static void *kqueue_timer_open(void)
89 {
90         struct kqueue_timer *timer;
91
92         if (!(timer = ao2_alloc(sizeof(*timer), timer_destroy))) {
93                 ast_log(LOG_ERROR, "Could not allocate memory for kqueue_timer structure\n");
94                 return -1;
95         }
96         if ((timer->handle = kqueue()) < 0) {
97                 ast_log(LOG_ERROR, "Failed to create kqueue timer: %s\n", strerror(errno));
98                 ao2_ref(timer, -1);
99                 return -1;
100         }
101
102         return timer;
103 }
104
105 static void kqueue_timer_close(void *data)
106 {
107         struct kqueue_timer *timer = data;
108
109         ao2_ref(timer, -1);
110 }
111
112 static void kqueue_set_nsecs(struct kqueue_timer *our_timer, uint64_t nsecs)
113 {
114         struct timespec nowait = { 0, 1 };
115 #ifdef HAVE_KEVENT64
116         struct kevent64_s kev;
117
118         EV_SET64(&kev, our_timer->handle, EVFILT_TIMER, EV_ADD | EV_ENABLE, NOTE_NSECONDS,
119                 nsecs, 0, 0, 0);
120         kevent64(our_timer->handle, &kev, 1, NULL, 0, 0, &nowait);
121 #else
122         struct kevent kev;
123
124         EV_SET(&kev, our_timer->handle, EVFILT_TIMER, EV_ADD | EV_ENABLE,
125 #ifdef NOTE_NSECONDS
126                 nsecs <= 0xFFffFFff ? NOTE_NSECONDS :
127 #endif
128 #ifdef NOTE_USECONDS
129                 NOTE_USECONDS
130 #else /* Milliseconds, if no constants are defined */
131                 0
132 #endif
133                 ,
134 #ifdef NOTE_NSECONDS
135                 nsecs <= 0xFFffFFff ? nsecs :
136 #endif
137 #ifdef NOTE_USECONDS
138         nsecs / 1000
139 #else /* Milliseconds, if nothing else is defined */
140         nsecs / 1000000
141 #endif
142         , NULL);
143         kevent(our_timer->handle, &kev, 1, NULL, 0, &nowait);
144 #endif
145 }
146
147 static int kqueue_timer_set_rate(void *data, unsigned int rate)
148 {
149         struct kqueue_timer *timer = data;
150
151         kqueue_set_nsecs(timer, (timer->nsecs = rate ? (long) (1000000000 / rate) : 0L));
152
153         return 0;
154 }
155
156 static int kqueue_timer_ack(void *data, unsigned int quantity)
157 {
158         struct kqueue_timer *timer = data;
159
160         if (timer->unacked < quantity) {
161                 ast_debug(1, "Acking more events than have expired?!!\n");
162                 timer->unacked = 0;
163                 return -1;
164         } else {
165                 timer->unacked -= quantity;
166         }
167
168         return 0;
169 }
170
171 static int kqueue_timer_enable_continuous(void *data)
172 {
173         struct kqueue_timer *timer = data;
174
175         kqueue_set_nsecs(timer, 1);
176         timer->is_continuous = 1;
177         timer->unacked = 0;
178
179         return 0;
180 }
181
182 static int kqueue_timer_disable_continuous(void *data)
183 {
184         struct kqueue_timer *timer = data;
185
186         kqueue_set_nsecs(timer, timer->nsecs);
187         timer->is_continuous = 0;
188         timer->unacked = 0;
189
190         return 0;
191 }
192
193 static enum ast_timer_event kqueue_timer_get_event(void *data)
194 {
195         struct kqueue_timer *timer = data;
196         enum ast_timer_event res = -1;
197         struct timespec sixty_seconds = { 60, 0 };
198         struct kevent kev;
199
200         /* If we have non-ACKed events, just return immediately */
201         if (timer->unacked == 0) {
202                 if (kevent(timer->handle, NULL, 0, &kev, 1, &sixty_seconds) > 0) {
203                         timer->unacked += kev.data;
204                 }
205         }
206
207         if (timer->unacked > 0) {
208                 res = timer->is_continuous ? AST_TIMING_EVENT_CONTINUOUS : AST_TIMING_EVENT_EXPIRED;
209         }
210
211         return res;
212 }
213
214 static unsigned int kqueue_timer_get_max_rate(void *data)
215 {
216         /* Actually, the max rate is 2^64-1 seconds, but that's not representable in a 32-bit integer. */
217         return UINT_MAX;
218 }
219
220 static int kqueue_timer_fd(void *data)
221 {
222         struct kqueue_timer *timer = data;
223
224         return timer->handle;
225 }
226
227 #ifdef TEST_FRAMEWORK
228 AST_TEST_DEFINE(test_kqueue_timing)
229 {
230         int res = AST_TEST_PASS, i;
231         uint64_t diff;
232         struct pollfd pfd = { 0, POLLIN, 0 };
233         struct kqueue_timer *kt;
234         struct timeval start;
235
236         switch (cmd) {
237         case TEST_INIT:
238                 info->name = "test_kqueue_timing";
239                 info->category = "/res/res_timing_kqueue/";
240                 info->summary = "Test KQueue timing interface";
241                 info->description = "Verify that the KQueue timing interface correctly generates timing events";
242                 return AST_TEST_NOT_RUN;
243         case TEST_EXECUTE:
244                 break;
245         }
246
247         if (!(kt = kqueue_timer_open())) {
248                 ast_test_status_update(test, "Cannot open timer!\n");
249                 return AST_TEST_FAIL;
250         }
251
252         do {
253                 pfd.fd = ast_timer_fd(kt);
254                 if (kqueue_timer_set_rate(kt, 1000)) {
255                         ast_test_status_update(test, "Cannot set timer rate to 1000/s\n");
256                         res = AST_TEST_FAIL;
257                         break;
258                 }
259                 if (ast_poll(&pfd, 1, 1000) < 1) {
260                         ast_test_status_update(test, "Polling on a kqueue doesn't work\n");
261                         res = AST_TEST_FAIL;
262                         break;
263                 }
264                 if (pfd.revents != POLLIN) {
265                         ast_test_status_update(test, "poll() should have returned POLLIN, but instead returned %hd\n", pfd.revents);
266                         res = AST_TEST_FAIL;
267                         break;
268                 }
269                 if (kqueue_timer_get_event(kt) <= 0) {
270                         ast_test_status_update(test, "No events generated after a poll returned successfully?!!\n");
271                         res = AST_TEST_FAIL;
272                         break;
273                 }
274 #if 0
275                 if (kt->unacked == 0) {
276                         ast_test_status_update(test, "Unacked events is 0, but there should be at least 1.\n");
277                         res = AST_TEST_FAIL;
278                         break;
279                 }
280 #endif
281                 kqueue_timer_enable_continuous(kt);
282                 start = ast_tvnow();
283                 for (i = 0; i < 100; i++) {
284                         if (ast_poll(&pfd, 1, 1000) < 1) {
285                                 ast_test_status_update(test, "Polling on a kqueue doesn't work\n");
286                                 res = AST_TEST_FAIL;
287                                 break;
288                         }
289                         if (kqueue_timer_get_event(kt) <= 0) {
290                                 ast_test_status_update(test, "No events generated in continuous mode after 1 microsecond?!!\n");
291                                 res = AST_TEST_FAIL;
292                                 break;
293                         }
294                 }
295                 diff = ast_tvdiff_us(ast_tvnow(), start);
296                 ast_test_status_update(test, "diff is %llu\n", diff);
297                 /*
298                 if (abs(diff - kt->unacked) == 0) {
299                         ast_test_status_update(test, "Unacked events should be around 1000, not %llu\n", kt->unacked);
300                         res = AST_TEST_FAIL;
301                 }
302                 */
303         } while (0);
304         kqueue_timer_close(kt);
305         return res;
306 }
307 #endif
308
309 /*!
310  * \brief Load the module
311  *
312  * Module loading including tests for configuration or dependencies.
313  * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
314  * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
315  * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the 
316  * configuration file or other non-critical problem return 
317  * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
318  */
319 static int load_module(void)
320 {
321         if (!(timing_funcs_handle = ast_register_timing_interface(&kqueue_timing))) {
322                 return AST_MODULE_LOAD_DECLINE;
323         }
324
325         AST_TEST_REGISTER(test_kqueue_timing);
326         return AST_MODULE_LOAD_SUCCESS;
327 }
328
329 static int unload_module(void)
330 {
331         AST_TEST_UNREGISTER(test_kqueue_timing);
332
333         return ast_unregister_timing_interface(timing_funcs_handle);
334 }
335
336 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "KQueue Timing Interface",
337                 .load = load_module,
338                 .unload = unload_module,
339                 .load_pri = AST_MODPRI_CHANNEL_DEPEND,
340                 );