res_timing_kqueue.c 9.8 KB

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