KqueueEventPoll.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "KqueueEventPoll.h"
  36. #include <cerrno>
  37. #include <cstring>
  38. #include <algorithm>
  39. #include <numeric>
  40. #include "Command.h"
  41. #include "LogFactory.h"
  42. #include "Logger.h"
  43. #include "util.h"
  44. #include "fmt.h"
  45. #ifdef KEVENT_UDATA_INTPTR_T
  46. #define PTR_TO_UDATA(X) (reinterpret_cast<intptr_t>(X))
  47. #else // !KEVENT_UDATA_INTPTR_T
  48. #define PTR_TO_UDATA(X) (X)
  49. #endif // !KEVENT_UDATA_INTPTR_T
  50. namespace aria2 {
  51. KqueueEventPoll::KSocketEntry::KSocketEntry(sock_t s)
  52. : SocketEntry<KCommandEvent, KADNSEvent>(s)
  53. {
  54. }
  55. int accumulateEvent(int events, const KqueueEventPoll::KEvent& event)
  56. {
  57. return events | event.getEvents();
  58. }
  59. size_t KqueueEventPoll::KSocketEntry::getEvents(struct kevent* eventlist)
  60. {
  61. int events;
  62. #ifdef ENABLE_ASYNC_DNS
  63. events =
  64. std::accumulate(adnsEvents_.begin(), adnsEvents_.end(),
  65. std::accumulate(commandEvents_.begin(),
  66. commandEvents_.end(), 0, accumulateEvent),
  67. accumulateEvent);
  68. #else // !ENABLE_ASYNC_DNS
  69. events = std::accumulate(commandEvents_.begin(), commandEvents_.end(), 0,
  70. accumulateEvent);
  71. #endif // !ENABLE_ASYNC_DNS
  72. EV_SET(&eventlist[0], socket_, EVFILT_READ,
  73. EV_ADD |
  74. ((events & KqueueEventPoll::IEV_READ) ? EV_ENABLE : EV_DISABLE),
  75. 0, 0, PTR_TO_UDATA(this));
  76. EV_SET(&eventlist[1], socket_, EVFILT_WRITE,
  77. EV_ADD |
  78. ((events & KqueueEventPoll::IEV_WRITE) ? EV_ENABLE : EV_DISABLE),
  79. 0, 0, PTR_TO_UDATA(this));
  80. return 2;
  81. }
  82. KqueueEventPoll::KqueueEventPoll()
  83. : kqEventsSize_(KQUEUE_EVENTS_MAX),
  84. kqEvents_(make_unique<struct kevent[]>(kqEventsSize_))
  85. {
  86. kqfd_ = kqueue();
  87. }
  88. KqueueEventPoll::~KqueueEventPoll()
  89. {
  90. if (kqfd_ != -1) {
  91. int r = close(kqfd_);
  92. int errNum = errno;
  93. if (r == -1) {
  94. A2_LOG_ERROR(fmt("Error occurred while closing kqueue file descriptor"
  95. " %d: %s",
  96. kqfd_, util::safeStrerror(errNum).c_str()));
  97. }
  98. }
  99. }
  100. bool KqueueEventPoll::good() const { return kqfd_ != -1; }
  101. void KqueueEventPoll::poll(const struct timeval& tv)
  102. {
  103. struct timespec timeout = {tv.tv_sec, tv.tv_usec * 1000};
  104. int res;
  105. while ((res = kevent(kqfd_, kqEvents_.get(), 0, kqEvents_.get(),
  106. kqEventsSize_, &timeout)) == -1 &&
  107. errno == EINTR)
  108. ;
  109. if (res > 0) {
  110. for (int i = 0; i < res; ++i) {
  111. KSocketEntry* p = reinterpret_cast<KSocketEntry*>(kqEvents_[i].udata);
  112. int events = 0;
  113. int filter = kqEvents_[i].filter;
  114. if (filter == EVFILT_READ) {
  115. events = KqueueEventPoll::IEV_READ;
  116. }
  117. else if (filter == EVFILT_WRITE) {
  118. events = KqueueEventPoll::IEV_WRITE;
  119. }
  120. p->processEvents(events);
  121. }
  122. }
  123. else if (res == -1) {
  124. int errNum = errno;
  125. A2_LOG_INFO(fmt("kevent error: %s", util::safeStrerror(errNum).c_str()));
  126. }
  127. #ifdef ENABLE_ASYNC_DNS
  128. // It turns out that we have to call ares_process_fd before ares's
  129. // own timeout and ares may create new sockets or closes socket in
  130. // their API. So we call ares_process_fd for all ares_channel and
  131. // re-register their sockets.
  132. for (auto& r : nameResolverEntries_) {
  133. auto& ent = r.second;
  134. ent.processTimeout();
  135. ent.removeSocketEvents(this);
  136. ent.addSocketEvents(this);
  137. }
  138. #endif // ENABLE_ASYNC_DNS
  139. // TODO timeout of name resolver is determined in Command(AbstractCommand,
  140. // DHTEntryPoint...Command)
  141. }
  142. namespace {
  143. int translateEvents(EventPoll::EventType events)
  144. {
  145. int newEvents = 0;
  146. if (EventPoll::EVENT_READ & events) {
  147. newEvents |= KqueueEventPoll::IEV_READ;
  148. }
  149. if (EventPoll::EVENT_WRITE & events) {
  150. newEvents |= KqueueEventPoll::IEV_WRITE;
  151. }
  152. return newEvents;
  153. }
  154. } // namespace
  155. bool KqueueEventPoll::addEvents(sock_t socket,
  156. const KqueueEventPoll::KEvent& event)
  157. {
  158. auto i = socketEntries_.lower_bound(socket);
  159. int r = 0;
  160. struct timespec zeroTimeout = {0, 0};
  161. struct kevent changelist[2];
  162. size_t n;
  163. if (i != std::end(socketEntries_) && (*i).first == socket) {
  164. auto& socketEntry = (*i).second;
  165. event.addSelf(&socketEntry);
  166. n = socketEntry.getEvents(changelist);
  167. }
  168. else {
  169. i = socketEntries_.insert(i, std::make_pair(socket, KSocketEntry(socket)));
  170. auto& socketEntry = (*i).second;
  171. if (socketEntries_.size() > kqEventsSize_) {
  172. kqEventsSize_ *= 2;
  173. kqEvents_ = make_unique<struct kevent[]>(kqEventsSize_);
  174. }
  175. event.addSelf(&socketEntry);
  176. n = socketEntry.getEvents(changelist);
  177. }
  178. r = kevent(kqfd_, changelist, n, changelist, 0, &zeroTimeout);
  179. int errNum = errno;
  180. if (r == -1) {
  181. A2_LOG_DEBUG(fmt("Failed to add socket event %d:%s", socket,
  182. util::safeStrerror(errNum).c_str()));
  183. return false;
  184. }
  185. else {
  186. return true;
  187. }
  188. }
  189. bool KqueueEventPoll::addEvents(sock_t socket, Command* command,
  190. EventPoll::EventType events)
  191. {
  192. int kqEvents = translateEvents(events);
  193. return addEvents(socket, KCommandEvent(command, kqEvents));
  194. }
  195. #ifdef ENABLE_ASYNC_DNS
  196. bool KqueueEventPoll::addEvents(sock_t socket, Command* command, int events,
  197. const std::shared_ptr<AsyncNameResolver>& rs)
  198. {
  199. return addEvents(socket, KADNSEvent(rs, command, socket, events));
  200. }
  201. #endif // ENABLE_ASYNC_DNS
  202. bool KqueueEventPoll::deleteEvents(sock_t socket,
  203. const KqueueEventPoll::KEvent& event)
  204. {
  205. auto i = socketEntries_.find(socket);
  206. if (i == std::end(socketEntries_)) {
  207. A2_LOG_DEBUG(fmt("Socket %d is not found in SocketEntries.", socket));
  208. return false;
  209. }
  210. auto& socketEntry = (*i).second;
  211. event.removeSelf(&socketEntry);
  212. int r = 0;
  213. struct timespec zeroTimeout = {0, 0};
  214. struct kevent changelist[2];
  215. size_t n = socketEntry.getEvents(changelist);
  216. r = kevent(kqfd_, changelist, n, changelist, 0, &zeroTimeout);
  217. int errNum = errno;
  218. if (socketEntry.eventEmpty()) {
  219. socketEntries_.erase(i);
  220. }
  221. if (r == -1) {
  222. A2_LOG_DEBUG(fmt("Failed to delete socket event:%s",
  223. util::safeStrerror(errNum).c_str()));
  224. return false;
  225. }
  226. else {
  227. return true;
  228. }
  229. }
  230. #ifdef ENABLE_ASYNC_DNS
  231. bool KqueueEventPoll::deleteEvents(sock_t socket, Command* command,
  232. const std::shared_ptr<AsyncNameResolver>& rs)
  233. {
  234. return deleteEvents(socket, KADNSEvent(rs, command, socket, 0));
  235. }
  236. #endif // ENABLE_ASYNC_DNS
  237. bool KqueueEventPoll::deleteEvents(sock_t socket, Command* command,
  238. EventPoll::EventType events)
  239. {
  240. int kqEvents = translateEvents(events);
  241. return deleteEvents(socket, KCommandEvent(command, kqEvents));
  242. }
  243. #ifdef ENABLE_ASYNC_DNS
  244. bool KqueueEventPoll::addNameResolver(
  245. const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
  246. {
  247. auto key = std::make_pair(resolver.get(), command);
  248. auto itr = nameResolverEntries_.lower_bound(key);
  249. if (itr != std::end(nameResolverEntries_) && (*itr).first == key) {
  250. return false;
  251. }
  252. itr = nameResolverEntries_.insert(
  253. itr, std::make_pair(key, KAsyncNameResolverEntry(resolver, command)));
  254. (*itr).second.addSocketEvents(this);
  255. return true;
  256. }
  257. bool KqueueEventPoll::deleteNameResolver(
  258. const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
  259. {
  260. auto key = std::make_pair(resolver.get(), command);
  261. auto itr = nameResolverEntries_.find(key);
  262. if (itr == std::end(nameResolverEntries_)) {
  263. return false;
  264. }
  265. (*itr).second.removeSocketEvents(this);
  266. nameResolverEntries_.erase(itr);
  267. return true;
  268. }
  269. #endif // ENABLE_ASYNC_DNS
  270. } // namespace aria2