Wed Jan 8 2020 09:49:50

Asterisk developer's documentation


res_timing_kqueue.c
Go to the documentation of this file.
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 <tlesher AT digium DOT com>
22  *
23  * \brief kqueue timing interface
24  */
25 
26 /*** MODULEINFO
27  <depend>kqueue</depend>
28  <conflict>launchd</conflict>
29  <support_level>extended</support_level>
30  ***/
31 
32 #include "asterisk.h"
33 
34 #include <sys/types.h>
35 #include <sys/event.h>
36 #include <sys/time.h>
37 
38 #include "asterisk/module.h"
39 #include "asterisk/astobj2.h"
40 #include "asterisk/timing.h"
41 #include "asterisk/logger.h"
42 #include "asterisk/utils.h"
43 #include "asterisk/time.h"
44 #include "asterisk/test.h"
45 #include "asterisk/poll-compat.h" /* for ast_poll() */
46 
47 static void *timing_funcs_handle;
48 
49 static int kqueue_timer_open(void);
50 static void kqueue_timer_close(int handle);
51 static int kqueue_timer_set_rate(int handle, unsigned int rate);
52 static int kqueue_timer_ack(int handle, unsigned int quantity);
53 static int kqueue_timer_enable_continuous(int handle);
54 static int kqueue_timer_disable_continuous(int handle);
55 static enum ast_timer_event kqueue_timer_get_event(int handle);
56 static unsigned int kqueue_timer_get_max_rate(int handle);
57 
59  .name = "kqueue",
60  .priority = 150,
61  .timer_open = kqueue_timer_open,
62  .timer_close = kqueue_timer_close,
63  .timer_set_rate = kqueue_timer_set_rate,
64  .timer_ack = kqueue_timer_ack,
65  .timer_enable_continuous = kqueue_timer_enable_continuous,
66  .timer_disable_continuous = kqueue_timer_disable_continuous,
67  .timer_get_event = kqueue_timer_get_event,
68  .timer_get_max_rate = kqueue_timer_get_max_rate,
69 };
70 
72 
73 struct kqueue_timer {
74  int handle;
75  uint64_t nsecs;
76  uint64_t unacked;
77  unsigned int is_continuous:1;
78 };
79 
80 static int kqueue_timer_hash(const void *obj, const int flags)
81 {
82  const struct kqueue_timer *timer = obj;
83 
84  return timer->handle;
85 }
86 
87 static int kqueue_timer_cmp(void *obj, void *args, int flags)
88 {
89  struct kqueue_timer *timer1 = obj, *timer2 = args;
90  return timer1->handle == timer2->handle ? CMP_MATCH | CMP_STOP : 0;
91 }
92 
93 static void timer_destroy(void *obj)
94 {
95  struct kqueue_timer *timer = obj;
96  close(timer->handle);
97 }
98 
99 #define lookup_timer(a) _lookup_timer(a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
100 static struct kqueue_timer *_lookup_timer(int handle, const char *file, int line, const char *func)
101 {
102  struct kqueue_timer *our_timer, find_helper = {
103  .handle = handle,
104  };
105 
106  if (!(our_timer = ao2_find(kqueue_timers, &find_helper, OBJ_POINTER))) {
107  ast_log(__LOG_ERROR, file, line, func, "Couldn't find timer with handle %d\n", handle);
108  /* API says we set errno */
109  errno = ESRCH;
110  return NULL;
111  }
112  return our_timer;
113 }
114 
115 static int kqueue_timer_open(void)
116 {
117  struct kqueue_timer *timer;
118  int handle;
119 
120  if (!(timer = ao2_alloc(sizeof(*timer), timer_destroy))) {
121  ast_log(LOG_ERROR, "Could not allocate memory for kqueue_timer structure\n");
122  return -1;
123  }
124  if ((handle = kqueue()) < 0) {
125  ast_log(LOG_ERROR, "Failed to create kqueue timer: %s\n", strerror(errno));
126  ao2_ref(timer, -1);
127  return -1;
128  }
129 
130  timer->handle = handle;
131  ao2_link(kqueue_timers, timer);
132  /* Get rid of the reference from the allocation */
133  ao2_ref(timer, -1);
134  return handle;
135 }
136 
137 static void kqueue_timer_close(int handle)
138 {
139  struct kqueue_timer *our_timer;
140 
141  if (!(our_timer = lookup_timer(handle))) {
142  return;
143  }
144 
145  ao2_unlink(kqueue_timers, our_timer);
146  ao2_ref(our_timer, -1);
147 }
148 
149 static void kqueue_set_nsecs(struct kqueue_timer *our_timer, uint64_t nsecs)
150 {
151  struct timespec nowait = { 0, 1 };
152 #ifdef HAVE_KEVENT64
153  struct kevent64_s kev;
154 
155  EV_SET64(&kev, our_timer->handle, EVFILT_TIMER, EV_ADD | EV_ENABLE, NOTE_NSECONDS,
156  nsecs, 0, 0, 0);
157  kevent64(our_timer->handle, &kev, 1, NULL, 0, 0, &nowait);
158 #else
159  struct kevent kev;
160 
161  EV_SET(&kev, our_timer->handle, EVFILT_TIMER, EV_ADD | EV_ENABLE,
162 #ifdef NOTE_NSECONDS
163  nsecs <= 0xFFffFFff ? NOTE_NSECONDS :
164 #endif
165 #ifdef NOTE_USECONDS
166  NOTE_USECONDS
167 #else /* Milliseconds, if no constants are defined */
168  0
169 #endif
170  ,
171 #ifdef NOTE_NSECONDS
172  nsecs <= 0xFFffFFff ? nsecs :
173 #endif
174 #ifdef NOTE_USECONDS
175  nsecs / 1000
176 #else /* Milliseconds, if nothing else is defined */
177  nsecs / 1000000
178 #endif
179  , NULL);
180  kevent(our_timer->handle, &kev, 1, NULL, 0, &nowait);
181 #endif
182 }
183 
184 static int kqueue_timer_set_rate(int handle, unsigned int rate)
185 {
186  struct kqueue_timer *our_timer;
187 
188  if (!(our_timer = lookup_timer(handle))) {
189  return -1;
190  }
191 
192  kqueue_set_nsecs(our_timer, (our_timer->nsecs = rate ? (long) (1000000000 / rate) : 0L));
193  ao2_ref(our_timer, -1);
194 
195  return 0;
196 }
197 
198 static int kqueue_timer_ack(int handle, unsigned int quantity)
199 {
200  struct kqueue_timer *our_timer;
201 
202  if (!(our_timer = lookup_timer(handle))) {
203  return -1;
204  }
205 
206  if (our_timer->unacked < quantity) {
207  ast_debug(1, "Acking more events than have expired?!!\n");
208  our_timer->unacked = 0;
209  ao2_ref(our_timer, -1);
210  return -1;
211  } else {
212  our_timer->unacked -= quantity;
213  }
214 
215  ao2_ref(our_timer, -1);
216  return 0;
217 }
218 
220 {
221  struct kqueue_timer *our_timer;
222 
223  if (!(our_timer = lookup_timer(handle))) {
224  return -1;
225  }
226 
227  kqueue_set_nsecs(our_timer, 1);
228  our_timer->is_continuous = 1;
229  our_timer->unacked = 0;
230  ao2_ref(our_timer, -1);
231  return 0;
232 }
233 
235 {
236  struct kqueue_timer *our_timer;
237 
238  if (!(our_timer = lookup_timer(handle))) {
239  return -1;
240  }
241 
242  kqueue_set_nsecs(our_timer, our_timer->nsecs);
243  our_timer->is_continuous = 0;
244  our_timer->unacked = 0;
245  ao2_ref(our_timer, -1);
246  return 0;
247 }
248 
250 {
251  enum ast_timer_event res = -1;
252  struct kqueue_timer *our_timer;
253  struct timespec sixty_seconds = { 60, 0 };
254  struct kevent kev;
255 
256  if (!(our_timer = lookup_timer(handle))) {
257  return -1;
258  }
259 
260  /* If we have non-ACKed events, just return immediately */
261  if (our_timer->unacked == 0) {
262  if (kevent(handle, NULL, 0, &kev, 1, &sixty_seconds) > 0) {
263  our_timer->unacked += kev.data;
264  }
265  }
266 
267  if (our_timer->unacked > 0) {
269  }
270 
271  ao2_ref(our_timer, -1);
272  return res;
273 }
274 
275 static unsigned int kqueue_timer_get_max_rate(int handle)
276 {
277  /* Actually, the max rate is 2^64-1 seconds, but that's not representable in a 32-bit integer. */
278  return UINT_MAX;
279 }
280 
281 #ifdef TEST_FRAMEWORK
282 AST_TEST_DEFINE(test_kqueue_timing)
283 {
284  int res = AST_TEST_PASS, handle, i;
285  uint64_t diff;
286  struct pollfd pfd = { 0, POLLIN, 0 };
287  struct kqueue_timer *kt;
288  struct timeval start;
289 
290  switch (cmd) {
291  case TEST_INIT:
292  info->name = "test_kqueue_timing";
293  info->category = "/res/res_timing_kqueue/";
294  info->summary = "Test KQueue timing interface";
295  info->description = "Verify that the KQueue timing interface correctly generates timing events";
296  return AST_TEST_NOT_RUN;
297  case TEST_EXECUTE:
298  break;
299  }
300 
301  if (!(handle = kqueue_timer_open())) {
302  ast_test_status_update(test, "Cannot open timer!\n");
303  return AST_TEST_FAIL;
304  }
305 
306  do {
307  pfd.fd = handle;
308  if (kqueue_timer_set_rate(handle, 1000)) {
309  ast_test_status_update(test, "Cannot set timer rate to 1000/s\n");
310  res = AST_TEST_FAIL;
311  break;
312  }
313  if (ast_poll(&pfd, 1, 1000) < 1) {
314  ast_test_status_update(test, "Polling on a kqueue doesn't work\n");
315  res = AST_TEST_FAIL;
316  break;
317  }
318  if (pfd.revents != POLLIN) {
319  ast_test_status_update(test, "poll() should have returned POLLIN, but instead returned %hd\n", pfd.revents);
320  res = AST_TEST_FAIL;
321  break;
322  }
323  if (!(kt = lookup_timer(handle))) {
324  ast_test_status_update(test, "Could not find timer structure in container?!!\n");
325  res = AST_TEST_FAIL;
326  break;
327  }
328  if (kqueue_timer_get_event(handle) <= 0) {
329  ast_test_status_update(test, "No events generated after a poll returned successfully?!!\n");
330  res = AST_TEST_FAIL;
331  break;
332  }
333 #if 0
334  if (kt->unacked == 0) {
335  ast_test_status_update(test, "Unacked events is 0, but there should be at least 1.\n");
336  res = AST_TEST_FAIL;
337  break;
338  }
339 #endif
341  start = ast_tvnow();
342  for (i = 0; i < 100; i++) {
343  if (ast_poll(&pfd, 1, 1000) < 1) {
344  ast_test_status_update(test, "Polling on a kqueue doesn't work\n");
345  res = AST_TEST_FAIL;
346  break;
347  }
348  if (kqueue_timer_get_event(handle) <= 0) {
349  ast_test_status_update(test, "No events generated in continuous mode after 1 microsecond?!!\n");
350  res = AST_TEST_FAIL;
351  break;
352  }
353  }
354  diff = ast_tvdiff_us(ast_tvnow(), start);
355  ast_test_status_update(test, "diff is %llu\n", diff);
356  /*
357  if (abs(diff - kt->unacked) == 0) {
358  ast_test_status_update(test, "Unacked events should be around 1000, not %llu\n", kt->unacked);
359  res = AST_TEST_FAIL;
360  }
361  */
362  } while (0);
363  kqueue_timer_close(handle);
364  return res;
365 }
366 #endif
367 
368 static int load_module(void)
369 {
370  if (!(kqueue_timers = ao2_container_alloc(563, kqueue_timer_hash, kqueue_timer_cmp))) {
372  }
373 
374  if (!(timing_funcs_handle = ast_register_timing_interface(&kqueue_timing))) {
375  ao2_ref(kqueue_timers, -1);
377  }
378 
379  AST_TEST_REGISTER(test_kqueue_timing);
381 }
382 
383 static int unload_module(void)
384 {
385  int res;
386 
387  AST_TEST_UNREGISTER(test_kqueue_timing);
389  ao2_ref(kqueue_timers, -1);
390  kqueue_timers = NULL;
391  }
392 
393  return res;
394 }
395 
396 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "KQueue Timing Interface",
397  .load = load_module,
398  .unload = unload_module,
399  .load_pri = AST_MODPRI_CHANNEL_DEPEND,
400  );
const char * name
Definition: timing.h:70
Timing module interface.
Definition: timing.h:69
static int kqueue_timer_disable_continuous(int handle)
static enum ast_timer_event kqueue_timer_get_event(int handle)
Asterisk main include file. File version handling, generic pbx functions.
#define ao2_link(arg1, arg2)
Definition: astobj2.h:785
static int kqueue_timer_open(void)
static void timer_destroy(void *obj)
static struct ast_timing_interface kqueue_timing
Time-related functions and macros.
int ast_unregister_timing_interface(void *handle)
Unregister a previously registered timing interface.
Definition: timing.c:105
static unsigned int kqueue_timer_get_max_rate(int handle)
ast_timer_event
Definition: timing.h:57
Test Framework API.
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:142
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:374
#define __LOG_ERROR
Definition: logger.h:154
Utility functions.
static int kqueue_timer_ack(int handle, unsigned int quantity)
static struct kqueue_timer * _lookup_timer(int handle, const char *file, int line, const char *func)
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
static int kqueue_timer_enable_continuous(int handle)
#define ast_poll(a, b, c)
Definition: poll-compat.h:88
#define ao2_ref(o, delta)
Definition: astobj2.h:472
static int kqueue_timer_cmp(void *obj, void *args, int flags)
static int load_module(void)
static int kqueue_timer_set_rate(int handle, unsigned int rate)
#define LOG_ERROR
Definition: logger.h:155
static struct @350 args
static int kqueue_timer_hash(const void *obj, const int flags)
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128
static void * timing_funcs_handle
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:430
#define ao2_find(arg1, arg2, arg3)
Definition: astobj2.h:964
int errno
Support for logging to various files, console and syslog Configuration in file logger.conf.
static void kqueue_set_nsecs(struct kqueue_timer *our_timer, uint64_t nsecs)
#define ao2_container_alloc(arg1, arg2, arg3)
Definition: astobj2.h:734
#define lookup_timer(a)
#define AST_TEST_DEFINE(hdr)
Definition: test.h:126
#define ast_register_timing_interface(i)
Register a set of timing functions.
Definition: timing.h:94
int64_t ast_tvdiff_us(struct timeval end, struct timeval start)
Computes the difference (in microseconds) between two struct timeval instances.
Definition: time.h:70
static int unload_module(void)
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
static struct ast_timer * timer
Definition: chan_iax2.c:313
Asterisk module definitions.
static struct ao2_container * kqueue_timers
#define ao2_unlink(arg1, arg2)
Definition: astobj2.h:817
static void kqueue_timer_close(int handle)
Timing source management.
unsigned int is_continuous