AST_MODULE_INFO: Format corrections to the usages of AST_MODULE_INFO macro.
[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         intptr_t period;
77         int handle;
78 #ifndef EVFILT_USER
79         int continuous_fd;
80         unsigned int continuous_fd_valid:1;
81 #endif
82         unsigned int is_continuous:1;
83 };
84
85 #ifdef EVFILT_USER
86 #define CONTINUOUS_EVFILT_TYPE EVFILT_USER
87 static int kqueue_timer_init_continuous_event(struct kqueue_timer *timer)
88 {
89         return 0;
90 }
91
92 static int kqueue_timer_enable_continuous_event(struct kqueue_timer *timer)
93 {
94         struct kevent kev[2];
95
96         EV_SET(&kev[0], (uintptr_t)timer, EVFILT_USER, EV_ADD | EV_ENABLE,
97                 0, 0, NULL);
98         EV_SET(&kev[1], (uintptr_t)timer, EVFILT_USER, 0, NOTE_TRIGGER,
99                 0, NULL);
100         return kevent(timer->handle, kev, 2, NULL, 0, NULL);
101 }
102
103 static int kqueue_timer_disable_continuous_event(struct kqueue_timer *timer)
104 {
105         struct kevent kev;
106
107         EV_SET(&kev, (uintptr_t)timer, EVFILT_USER, EV_DELETE, 0, 0, NULL);
108         return kevent(timer->handle, &kev, 1, NULL, 0, NULL);
109 }
110
111 static void kqueue_timer_fini_continuous_event(struct kqueue_timer *timer)
112 {
113 }
114
115 #else /* EVFILT_USER */
116
117 #define CONTINUOUS_EVFILT_TYPE EVFILT_READ
118 static int kqueue_timer_init_continuous_event(struct kqueue_timer *timer)
119 {
120         int pipefds[2];
121         int retval;
122
123         retval = pipe(pipefds);
124         if (retval == 0) {
125                 timer->continuous_fd = pipefds[0];
126                 timer->continuous_fd_valid = 1;
127                 close(pipefds[1]);
128         }
129         return retval;
130 }
131
132 static void kqueue_timer_fini_continuous_event(struct kqueue_timer *timer)
133 {
134         if (timer->continuous_fd_valid) {
135                 close(timer->continuous_fd);
136         }
137 }
138
139 static int kqueue_timer_enable_continuous_event(struct kqueue_timer *timer)
140 {
141         struct kevent kev;
142
143         EV_SET(&kev, timer->continuous_fd, EVFILT_READ, EV_ADD | EV_ENABLE,
144                 0, 0, NULL);
145         return kevent(timer->handle, &kev, 1, NULL, 0, NULL);
146 }
147
148 static int kqueue_timer_disable_continuous_event(struct kqueue_timer *timer)
149 {
150         struct kevent kev;
151
152         EV_SET(&kev, timer->continuous_fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
153         return kevent(timer->handle, &kev, 1, NULL, 0, NULL);
154 }
155 #endif
156
157 static void timer_destroy(void *obj)
158 {
159         struct kqueue_timer *timer = obj;
160         ast_debug(5, "[%d]: Timer Destroy\n", timer->handle);
161         kqueue_timer_fini_continuous_event(timer);
162         close(timer->handle);
163 }
164
165 static void *kqueue_timer_open(void)
166 {
167         struct kqueue_timer *timer;
168
169         if (!(timer = ao2_alloc(sizeof(*timer), timer_destroy))) {
170                 ast_log(LOG_ERROR, "Alloc failed for kqueue_timer structure\n");
171                 return NULL;
172         }
173
174         if ((timer->handle = kqueue()) < 0) {
175                 ast_log(LOG_ERROR, "Failed to create kqueue fd: %s\n",
176                         strerror(errno));
177                 ao2_ref(timer, -1);
178                 return NULL;
179         }
180
181         if (kqueue_timer_init_continuous_event(timer) != 0) {
182                 ast_log(LOG_ERROR, "Failed to create continuous event: %s\n",
183                         strerror(errno));
184                 ao2_ref(timer, -1);
185                 return NULL;
186         }
187         ast_debug(5, "[%d]: Create timer\n", timer->handle);
188         return timer;
189 }
190
191 static void kqueue_timer_close(void *data)
192 {
193         struct kqueue_timer *timer = data;
194
195         ast_debug(5, "[%d]: Timer Close\n", timer->handle);
196         ao2_ref(timer, -1);
197 }
198
199 /*
200  * Use the highest precision available that does not overflow
201  * the datatype kevent is using for time.
202  */
203 static intptr_t kqueue_scale_period(unsigned int period_ns, int *units)
204 {
205         uint64_t period = period_ns;
206         *units = 0;
207 #ifdef NOTE_NSECONDS
208         if (period < INTPTR_MAX) {
209                 *units = NOTE_NSECONDS;
210         } else {
211 #ifdef NOTE_USECONDS
212                 period /= 1000;
213                 if (period < INTPTR_MAX) {
214                         *units = NOTE_USECONDS;
215                 } else {
216                         period /= 1000;
217 #ifdef NOTE_MSECONDS
218                         *units = NOTE_MSECONDS;
219 #endif  /* NOTE_MSECONDS */
220                 }
221 #else   /* NOTE_USECONDS */
222                 period /= 1000000;
223 #ifdef NOTE_MSECONDS
224                 *units = NOTE_MSECONDS;
225 #endif  /* NOTE_MSECONDS */
226 #endif  /* NOTE_USECONDS */
227         }
228 #else   /* NOTE_NSECONDS */
229         period /= 1000000;
230 #endif
231         if (period > INTPTR_MAX) {
232                 period = INTPTR_MAX;
233         }
234         return period;
235 }
236
237 static int kqueue_timer_set_rate(void *data, unsigned int rate)
238 {
239         struct kevent kev;
240         struct kqueue_timer *timer = data;
241         uint64_t period_ns;
242         int flags;
243         int units;
244         int retval;
245
246         ao2_lock(timer);
247
248         if (rate == 0) {
249                 if (timer->period == 0) {
250                         ao2_unlock(timer);
251                         return (0);
252                 }
253                 flags = EV_DELETE;
254                 timer->period = 0;
255                 units = 0;
256         } else  {
257                 flags = EV_ADD | EV_ENABLE;
258                 period_ns = (uint64_t)1000000000 / rate;
259                 timer->period = kqueue_scale_period(period_ns, &units);
260         }
261         ast_debug(5, "[%d]: Set rate %u:%ju\n",
262                 timer->handle, units, (uintmax_t)timer->period);
263         EV_SET(&kev, timer->handle, EVFILT_TIMER, flags, units,
264                 timer->period, NULL);
265         retval = kevent(timer->handle, &kev, 1, NULL, 0, NULL);
266
267         if (retval == -1) {
268                 ast_log(LOG_ERROR, "[%d]: Error queing timer: %s\n",
269                         timer->handle, strerror(errno));
270         }
271
272         ao2_unlock(timer);
273
274         return 0;
275 }
276
277 static int kqueue_timer_ack(void *data, unsigned int quantity)
278 {
279         static struct timespec ts_nowait = { 0, 0 };
280         struct kqueue_timer *timer = data;
281         struct kevent kev[2];
282         int i, retval;
283
284         ao2_lock(timer);
285
286         retval = kevent(timer->handle, NULL, 0, kev, 2, &ts_nowait);
287         if (retval == -1) {
288                 ast_log(LOG_ERROR, "[%d]: Error sampling kqueue: %s\n",
289                         timer->handle, strerror(errno));
290                 ao2_unlock(timer);
291                 return -1;
292         }
293
294         for (i = 0; i < retval; i++) {
295                 switch (kev[i].filter) {
296                 case EVFILT_TIMER:
297                         if (kev[i].data > quantity) {
298                                 ast_log(LOG_ERROR, "[%d]: Missed %ju\n",
299                                         timer->handle,
300                                         (uintmax_t)kev[i].data - quantity);
301                         }
302                         break;
303                 case CONTINUOUS_EVFILT_TYPE:
304                         if (!timer->is_continuous) {
305                                 ast_log(LOG_ERROR,
306                                         "[%d]: Spurious user event\n",
307                                         timer->handle);
308                         }
309                         break;
310                 default:
311                         ast_log(LOG_ERROR, "[%d]: Spurious kevent type %d.\n",
312                                 timer->handle, kev[i].filter);
313                 }
314         }
315
316         ao2_unlock(timer);
317
318         return 0;
319 }
320
321 static int kqueue_timer_enable_continuous(void *data)
322 {
323         struct kqueue_timer *timer = data;
324         int retval;
325
326         ao2_lock(timer);
327
328         if (!timer->is_continuous) {
329                 ast_debug(5, "[%d]: Enable Continuous\n", timer->handle);
330                 retval = kqueue_timer_enable_continuous_event(timer);
331                 if (retval == -1) {
332                         ast_log(LOG_ERROR,
333                                 "[%d]: Error signaling continuous event: %s\n",
334                                 timer->handle, strerror(errno));
335                 }
336                 timer->is_continuous = 1;
337         }
338
339         ao2_unlock(timer);
340
341         return 0;
342 }
343
344 static int kqueue_timer_disable_continuous(void *data)
345 {
346         struct kqueue_timer *timer = data;
347         int retval;
348
349         ao2_lock(timer);
350
351         if (timer->is_continuous) {
352                 ast_debug(5, "[%d]: Disable Continuous\n", timer->handle);
353                 retval = kqueue_timer_disable_continuous_event(timer);
354                 if (retval == -1) {
355                         ast_log(LOG_ERROR,
356                                 "[%d]: Error clearing continuous event: %s\n",
357                                 timer->handle, strerror(errno));
358                 }
359                 timer->is_continuous = 0;
360         }
361
362         ao2_unlock(timer);
363
364         return 0;
365 }
366
367 static enum ast_timer_event kqueue_timer_get_event(void *data)
368 {
369         struct kqueue_timer *timer = data;
370         enum ast_timer_event res;
371
372         if (timer->is_continuous) {
373                 res = AST_TIMING_EVENT_CONTINUOUS;
374         } else {
375                 res = AST_TIMING_EVENT_EXPIRED;
376         }
377
378         return res;
379 }
380
381 static unsigned int kqueue_timer_get_max_rate(void *data)
382 {
383         return INTPTR_MAX > UINT_MAX ? UINT_MAX : INTPTR_MAX;
384 }
385
386 static int kqueue_timer_fd(void *data)
387 {
388         struct kqueue_timer *timer = data;
389
390         return timer->handle;
391 }
392
393 #ifdef TEST_FRAMEWORK
394 AST_TEST_DEFINE(test_kqueue_timing)
395 {
396         int res = AST_TEST_PASS, i;
397         uint64_t diff;
398         struct pollfd pfd = { 0, POLLIN, 0 };
399         struct kqueue_timer *kt;
400         struct timeval start;
401
402         switch (cmd) {
403         case TEST_INIT:
404                 info->name = "test_kqueue_timing";
405                 info->category = "/res/res_timing_kqueue/";
406                 info->summary = "Test KQueue timing interface";
407                 info->description = "Verify that the KQueue timing interface correctly generates timing events";
408                 return AST_TEST_NOT_RUN;
409         case TEST_EXECUTE:
410                 break;
411         }
412
413         if (!(kt = kqueue_timer_open())) {
414                 ast_test_status_update(test, "Cannot open timer!\n");
415                 return AST_TEST_FAIL;
416         }
417
418         do {
419                 pfd.fd = kqueue_timer_fd(kt);
420                 if (kqueue_timer_set_rate(kt, 1000)) {
421                         ast_test_status_update(test, "Cannot set timer rate to 1000/s\n");
422                         res = AST_TEST_FAIL;
423                         break;
424                 }
425                 if (ast_poll(&pfd, 1, 1000) < 1) {
426                         ast_test_status_update(test, "Polling on a kqueue doesn't work\n");
427                         res = AST_TEST_FAIL;
428                         break;
429                 }
430                 if (pfd.revents != POLLIN) {
431                         ast_test_status_update(test, "poll() should have returned POLLIN, but instead returned %hd\n", pfd.revents);
432                         res = AST_TEST_FAIL;
433                         break;
434                 }
435                 if (kqueue_timer_get_event(kt) <= 0) {
436                         ast_test_status_update(test, "No events generated after a poll returned successfully?!!\n");
437                         res = AST_TEST_FAIL;
438                         break;
439                 }
440                 if (kqueue_timer_ack(kt, 1) != 0) {
441                         ast_test_status_update(test, "Acking event failed.\n");
442                         res = AST_TEST_FAIL;
443                         break;
444                 }
445
446                 kqueue_timer_enable_continuous(kt);
447                 start = ast_tvnow();
448                 for (i = 0; i < 100; i++) {
449                         if (ast_poll(&pfd, 1, 1000) < 1) {
450                                 ast_test_status_update(test, "Polling on a kqueue doesn't work\n");
451                                 res = AST_TEST_FAIL;
452                                 break;
453                         }
454                         if (kqueue_timer_get_event(kt) <= 0) {
455                                 ast_test_status_update(test, "No events generated in continuous mode after 1 microsecond?!!\n");
456                                 res = AST_TEST_FAIL;
457                                 break;
458                         }
459                         if (kqueue_timer_ack(kt, 1) != 0) {
460                                 ast_test_status_update(test, "Acking event failed.\n");
461                                 res = AST_TEST_FAIL;
462                                 break;
463                         }
464
465                 }
466                 diff = ast_tvdiff_us(ast_tvnow(), start);
467                 ast_test_status_update(test, "diff is %llu\n", diff);
468         } while (0);
469         kqueue_timer_close(kt);
470         return res;
471 }
472 #endif
473
474 /*!
475  * \brief Load the module
476  *
477  * Module loading including tests for configuration or dependencies.
478  * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
479  * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
480  * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
481  * configuration file or other non-critical problem return
482  * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
483  */
484 static int load_module(void)
485 {
486         if (!(timing_funcs_handle = ast_register_timing_interface(&kqueue_timing))) {
487                 return AST_MODULE_LOAD_DECLINE;
488         }
489
490         AST_TEST_REGISTER(test_kqueue_timing);
491         return AST_MODULE_LOAD_SUCCESS;
492 }
493
494 static int unload_module(void)
495 {
496         AST_TEST_UNREGISTER(test_kqueue_timing);
497
498         return ast_unregister_timing_interface(timing_funcs_handle);
499 }
500
501 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "KQueue Timing Interface",
502         .support_level = AST_MODULE_SUPPORT_EXTENDED,
503         .load = load_module,
504         .unload = unload_module,
505         .load_pri = AST_MODPRI_CHANNEL_DEPEND,
506 );