res_timing_kqueue.c 9.8 KB

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