res_timing_kqueue.c 8.6 KB

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