res_timing_timerfd.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2008, Digium, Inc.
  5. *
  6. * Mark Michelson <mmichelson@digium.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 Mark Michelson <mmichelson@digium.com>
  21. *
  22. * \brief timerfd timing interface
  23. */
  24. /*** MODULEINFO
  25. <depend>timerfd</depend>
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. #include <sys/timerfd.h>
  30. #include "asterisk/module.h"
  31. #include "asterisk/astobj2.h"
  32. #include "asterisk/timing.h"
  33. #include "asterisk/logger.h"
  34. #include "asterisk/utils.h"
  35. #include "asterisk/time.h"
  36. static void *timing_funcs_handle;
  37. static int timerfd_timer_open(void);
  38. static void timerfd_timer_close(int handle);
  39. static int timerfd_timer_set_rate(int handle, unsigned int rate);
  40. static int timerfd_timer_ack(int handle, unsigned int quantity);
  41. static int timerfd_timer_enable_continuous(int handle);
  42. static int timerfd_timer_disable_continuous(int handle);
  43. static enum ast_timer_event timerfd_timer_get_event(int handle);
  44. static unsigned int timerfd_timer_get_max_rate(int handle);
  45. static struct ast_timing_interface timerfd_timing = {
  46. .name = "timerfd",
  47. .priority = 200,
  48. .timer_open = timerfd_timer_open,
  49. .timer_close = timerfd_timer_close,
  50. .timer_set_rate = timerfd_timer_set_rate,
  51. .timer_ack = timerfd_timer_ack,
  52. .timer_enable_continuous = timerfd_timer_enable_continuous,
  53. .timer_disable_continuous = timerfd_timer_disable_continuous,
  54. .timer_get_event = timerfd_timer_get_event,
  55. .timer_get_max_rate = timerfd_timer_get_max_rate,
  56. };
  57. static struct ao2_container *timerfd_timers;
  58. #define TIMERFD_TIMER_BUCKETS 563
  59. #define TIMERFD_MAX_RATE 1000
  60. struct timerfd_timer {
  61. int handle;
  62. struct itimerspec saved_timer;
  63. unsigned int is_continuous:1;
  64. };
  65. static int timerfd_timer_hash(const void *obj, const int flags)
  66. {
  67. const struct timerfd_timer *timer = obj;
  68. return timer->handle;
  69. }
  70. static int timerfd_timer_cmp(void *obj, void *args, int flags)
  71. {
  72. struct timerfd_timer *timer1 = obj, *timer2 = args;
  73. return timer1->handle == timer2->handle ? CMP_MATCH | CMP_STOP : 0;
  74. }
  75. static void timer_destroy(void *obj)
  76. {
  77. struct timerfd_timer *timer = obj;
  78. close(timer->handle);
  79. timer->handle = -1;
  80. }
  81. static int timerfd_timer_open(void)
  82. {
  83. struct timerfd_timer *timer;
  84. int handle;
  85. if (!(timer = ao2_alloc(sizeof(*timer), timer_destroy))) {
  86. ast_log(LOG_ERROR, "Could not allocate memory for timerfd_timer structure\n");
  87. return -1;
  88. }
  89. if ((handle = timerfd_create(CLOCK_MONOTONIC, 0)) < 0) {
  90. ast_log(LOG_ERROR, "Failed to create timerfd timer: %s\n", strerror(errno));
  91. ao2_ref(timer, -1);
  92. return -1;
  93. }
  94. timer->handle = handle;
  95. ao2_link(timerfd_timers, timer);
  96. /* Get rid of the reference from the allocation */
  97. ao2_ref(timer, -1);
  98. return handle;
  99. }
  100. static void timerfd_timer_close(int handle)
  101. {
  102. struct timerfd_timer *our_timer, find_helper = {
  103. .handle = handle,
  104. };
  105. if (handle == -1) {
  106. ast_log(LOG_ERROR, "Attempting to close timerfd handle -1");
  107. return;
  108. }
  109. if (!(our_timer = ao2_find(timerfd_timers, &find_helper, OBJ_POINTER))) {
  110. ast_log(LOG_ERROR, "Couldn't find timer with handle %d\n", handle);
  111. return;
  112. }
  113. ao2_unlink(timerfd_timers, our_timer);
  114. ao2_ref(our_timer, -1);
  115. }
  116. static int timerfd_timer_set_rate(int handle, unsigned int rate)
  117. {
  118. struct timerfd_timer *our_timer, find_helper = {
  119. .handle = handle,
  120. };
  121. int res = 0;
  122. if (handle == -1) {
  123. ast_log(LOG_ERROR, "Attempting to set rate on timerfd handle -1");
  124. return -1;
  125. }
  126. if (!(our_timer = ao2_find(timerfd_timers, &find_helper, OBJ_POINTER))) {
  127. ast_log(LOG_ERROR, "Couldn't find timer with handle %d\n", handle);
  128. return -1;
  129. }
  130. ao2_lock(our_timer);
  131. our_timer->saved_timer.it_value.tv_sec = 0;
  132. our_timer->saved_timer.it_value.tv_nsec = rate ? (long) (1000000000 / rate) : 0L;
  133. our_timer->saved_timer.it_interval.tv_sec = our_timer->saved_timer.it_value.tv_sec;
  134. our_timer->saved_timer.it_interval.tv_nsec = our_timer->saved_timer.it_value.tv_nsec;
  135. if (!our_timer->is_continuous) {
  136. res = timerfd_settime(handle, 0, &our_timer->saved_timer, NULL);
  137. }
  138. ao2_unlock(our_timer);
  139. ao2_ref(our_timer, -1);
  140. return res;
  141. }
  142. static int timerfd_timer_ack(int handle, unsigned int quantity)
  143. {
  144. uint64_t expirations;
  145. int read_result = 0;
  146. int res = 0;
  147. struct timerfd_timer *our_timer, find_helper = {
  148. .handle = handle,
  149. };
  150. if (handle == -1) {
  151. ast_log(LOG_ERROR, "Attempting to ack timerfd handle -1");
  152. return -1;
  153. }
  154. if (!(our_timer = ao2_find(timerfd_timers, &find_helper, OBJ_POINTER))) {
  155. ast_log(LOG_ERROR, "Couldn't find a timer with handle %d\n", handle);
  156. return -1;
  157. }
  158. ao2_lock(our_timer);
  159. do {
  160. struct itimerspec timer_status;
  161. if (timerfd_gettime(handle, &timer_status)) {
  162. ast_log(LOG_ERROR, "Call to timerfd_gettime() using handle %d error: %s\n", handle, strerror(errno));
  163. expirations = 0;
  164. res = -1;
  165. break;
  166. }
  167. if (timer_status.it_value.tv_sec == 0 && timer_status.it_value.tv_nsec == 0) {
  168. ast_debug(1, "Avoiding read on disarmed timerfd %d\n", handle);
  169. expirations = 0;
  170. break;
  171. }
  172. read_result = read(handle, &expirations, sizeof(expirations));
  173. if (read_result == -1) {
  174. if (errno == EINTR || errno == EAGAIN) {
  175. continue;
  176. } else {
  177. ast_log(LOG_ERROR, "Read error: %s\n", strerror(errno));
  178. res = -1;
  179. break;
  180. }
  181. }
  182. } while (read_result != sizeof(expirations));
  183. ao2_unlock(our_timer);
  184. ao2_ref(our_timer, -1);
  185. if (expirations != quantity) {
  186. ast_debug(2, "Expected to acknowledge %u ticks but got %llu instead\n", quantity, (unsigned long long) expirations);
  187. }
  188. return res;
  189. }
  190. static int timerfd_timer_enable_continuous(int handle)
  191. {
  192. int res;
  193. struct itimerspec continuous_timer = {
  194. .it_value.tv_nsec = 1L,
  195. };
  196. struct timerfd_timer *our_timer, find_helper = {
  197. .handle = handle,
  198. };
  199. if (handle == -1) {
  200. ast_log(LOG_ERROR, "Attempting to enable timerfd handle -1");
  201. return -1;
  202. }
  203. if (!(our_timer = ao2_find(timerfd_timers, &find_helper, OBJ_POINTER))) {
  204. ast_log(LOG_ERROR, "Couldn't find timer with handle %d\n", handle);
  205. return -1;
  206. }
  207. ao2_lock(our_timer);
  208. if (our_timer->is_continuous) {
  209. /*It's already in continous mode, no need to do
  210. * anything further
  211. */
  212. ao2_unlock(our_timer);
  213. ao2_ref(our_timer, -1);
  214. return 0;
  215. }
  216. res = timerfd_settime(handle, 0, &continuous_timer, &our_timer->saved_timer);
  217. our_timer->is_continuous = 1;
  218. ao2_unlock(our_timer);
  219. ao2_ref(our_timer, -1);
  220. return res;
  221. }
  222. static int timerfd_timer_disable_continuous(int handle)
  223. {
  224. int res;
  225. struct timerfd_timer *our_timer, find_helper = {
  226. .handle = handle,
  227. };
  228. if (handle == -1) {
  229. ast_log(LOG_ERROR, "Attempting to disable timerfd handle -1");
  230. return -1;
  231. }
  232. if (!(our_timer = ao2_find(timerfd_timers, &find_helper, OBJ_POINTER))) {
  233. ast_log(LOG_ERROR, "Couldn't find timer with handle %d\n", handle);
  234. return -1;
  235. }
  236. ao2_lock(our_timer);
  237. if (!our_timer->is_continuous) {
  238. /* No reason to do anything if we're not
  239. * in continuous mode
  240. */
  241. ao2_unlock(our_timer);
  242. ao2_ref(our_timer, -1);
  243. return 0;
  244. }
  245. res = timerfd_settime(handle, 0, &our_timer->saved_timer, NULL);
  246. our_timer->is_continuous = 0;
  247. memset(&our_timer->saved_timer, 0, sizeof(our_timer->saved_timer));
  248. ao2_unlock(our_timer);
  249. ao2_ref(our_timer, -1);
  250. return res;
  251. }
  252. static enum ast_timer_event timerfd_timer_get_event(int handle)
  253. {
  254. enum ast_timer_event res;
  255. struct timerfd_timer *our_timer, find_helper = {
  256. .handle = handle,
  257. };
  258. if (handle == -1) {
  259. ast_log(LOG_ERROR, "Attempting to get event from timerfd handle -1");
  260. return -1;
  261. }
  262. if (!(our_timer = ao2_find(timerfd_timers, &find_helper, OBJ_POINTER))) {
  263. ast_log(LOG_ERROR, "Couldn't find timer with handle %d\n", handle);
  264. return -1;
  265. }
  266. ao2_lock(our_timer);
  267. if (our_timer->is_continuous) {
  268. res = AST_TIMING_EVENT_CONTINUOUS;
  269. } else {
  270. res = AST_TIMING_EVENT_EXPIRED;
  271. }
  272. ao2_unlock(our_timer);
  273. ao2_ref(our_timer, -1);
  274. return res;
  275. }
  276. static unsigned int timerfd_timer_get_max_rate(int handle)
  277. {
  278. return TIMERFD_MAX_RATE;
  279. }
  280. static int load_module(void)
  281. {
  282. int fd;
  283. /* Make sure we support the necessary clock type */
  284. if ((fd = timerfd_create(CLOCK_MONOTONIC, 0)) < 0) {
  285. ast_log(LOG_ERROR, "timerfd_create() not supported by the kernel. Not loading.\n");
  286. return AST_MODULE_LOAD_DECLINE;
  287. }
  288. close(fd);
  289. if (!(timerfd_timers = ao2_container_alloc(TIMERFD_TIMER_BUCKETS, timerfd_timer_hash, timerfd_timer_cmp))) {
  290. return AST_MODULE_LOAD_DECLINE;
  291. }
  292. if (!(timing_funcs_handle = ast_register_timing_interface(&timerfd_timing))) {
  293. ao2_ref(timerfd_timers, -1);
  294. return AST_MODULE_LOAD_DECLINE;
  295. }
  296. return AST_MODULE_LOAD_SUCCESS;
  297. }
  298. static int unload_module(void)
  299. {
  300. int res;
  301. if (!(res = ast_unregister_timing_interface(timing_funcs_handle))) {
  302. ao2_ref(timerfd_timers, -1);
  303. timerfd_timers = NULL;
  304. }
  305. return res;
  306. }
  307. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Timerfd Timing Interface",
  308. .load = load_module,
  309. .unload = unload_module,
  310. .load_pri = AST_MODPRI_TIMING,
  311. );