scheduler.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /* * Copyright (c) 2013, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file scheduler.c
  5. * \brief Relay scheduling system
  6. **/
  7. #include "or.h"
  8. #include "channel.h"
  9. #include "compat_libevent.h"
  10. #include "scheduler.h"
  11. #ifdef HAVE_EVENT2_EVENT_H
  12. #include <event2/event.h>
  13. #else
  14. #include <event.h>
  15. #endif
  16. /*
  17. * Write scheduling works by keeping track of lists of channels that can
  18. * accept cells, and have cells to write. From the scheduler's perspective,
  19. * a channel can be in four possible states:
  20. *
  21. * 1.) Not open for writes, no cells to send
  22. * - Not much to do here, and the channel will appear in neither list.
  23. * - Transitions from:
  24. * - Open for writes/has cells by simultaneously draining all circuit
  25. * queues and filling the output buffer.
  26. * - Transitions to:
  27. * - Not open for writes/has cells by arrival of cells on an attached
  28. * circuit (this would be driven from append_cell_to_circuit_queue())
  29. * - Open for writes/no cells by a channel type specific path;
  30. * driven from connection_or_flushed_some() for channel_tls_t.
  31. *
  32. * 2.) Open for writes, no cells to send
  33. * - Not much here either; this will be the state an idle but open channel
  34. * can be expected to settle in.
  35. * - Transitions from:
  36. * - Not open for writes/no cells by flushing some of the output
  37. * buffer.
  38. * - Open for writes/has cells by the scheduler moving cells from
  39. * circuit queues to channel output queue, but not having enough
  40. * to fill the output queue.
  41. * - Transitions to:
  42. * - Open for writes/has cells by arrival of new cells on an attached
  43. * circuit, in append_cell_to_circuit_queue()
  44. *
  45. * 3.) Not open for writes, cells to send
  46. * - This is the state of a busy circuit limited by output bandwidth;
  47. * cells have piled up in the circuit queues waiting to be relayed.
  48. * - Transitions from:
  49. * - Not open for writes/no cells by arrival of cells on an attached
  50. * circuit
  51. * - Open for writes/has cells by filling an output buffer without
  52. * draining all cells from attached circuits
  53. * - Transitions to:
  54. * - Opens for writes/has cells by draining some of the output buffer
  55. * via the connection_or_flushed_some() path (for channel_tls_t).
  56. *
  57. * 4.) Open for writes, cells to send
  58. * - This connection is ready to relay some cells and waiting for
  59. * the scheduler to choose it
  60. * - Transitions from:
  61. * - Not open for writes/has cells by the connection_or_flushed_some()
  62. * path
  63. * - Open for writes/no cells by the append_cell_to_circuit_queue()
  64. * path
  65. * - Transitions to:
  66. * - Not open for writes/no cells by draining all circuit queues and
  67. * simultaneously filling the output buffer.
  68. * - Not open for writes/has cells by writing enough cells to fill the
  69. * output buffer
  70. * - Open for writes/no cells by draining all attached circuit queues
  71. * without also filling the output buffer
  72. *
  73. * Other event-driven parts of the code move channels between these scheduling
  74. * states by calling scheduler functions; the scheduler only runs on open-for-
  75. * writes/has-cells channels and is the only path for those to transition to
  76. * other states. The scheduler_run() function gives us the opportunity to do
  77. * scheduling work, and is called from other scheduler functions whenever a
  78. * state transition occurs, and periodically from the main event loop.
  79. */
  80. /* Scheduler global data structures */
  81. /*
  82. * We keep lists of channels that either have cells queued, can accept
  83. * writes, or both (states 2, 3 and 4 above) - no explicit list of state
  84. * 1 channels is kept, so we don't have to worry about registering new
  85. * channels here or anything. The scheduler will learn about them when
  86. * it needs to. We can check how many channels in state 4 in O(1), so
  87. * the test whether we have anything to do in scheduler_run() is fast
  88. * and there's no harm in calling it opportunistically whenever we get
  89. * the chance.
  90. *
  91. * Note that it takes time O(n) to search for a channel in these smartlists
  92. * or move one; I don't think the number of channels on a relay will be large
  93. * enough for this to be a severe problem, but this would benefit from using
  94. * a doubly-linked list rather than smartlist_t, together with a hash map from
  95. * channel identifiers to pointers to list entries, so we can perform those
  96. * operations in O(log(n)).
  97. */
  98. /* List of channels that can write but have no cells (state 2 above) */
  99. static smartlist_t *channels_waiting_for_cells = NULL;
  100. /* List of channels with cells waiting to write (state 3 above) */
  101. static smartlist_t *channels_waiting_to_write = NULL;
  102. /* List of channels that can write and have cells (pending work) */
  103. static smartlist_t *channels_pending = NULL;
  104. /*
  105. * This event runs the scheduler from its callback, and is manually
  106. * activated whenever a channel enters open for writes/cells to send.
  107. */
  108. static struct event *run_sched_ev = NULL;
  109. static struct timeval run_sched_tv;
  110. /* Scheduler static function declarations */
  111. static void scheduler_evt_callback(evutil_socket_t fd,
  112. short events, void *arg);
  113. static int scheduler_more_work(void);
  114. static void scheduler_retrigger(void);
  115. static void scheduler_trigger(void);
  116. /* Scheduler function implementations */
  117. /** Free everything and shut down the scheduling system */
  118. void
  119. scheduler_free_all(void)
  120. {
  121. log_debug(LD_SCHED, "Shutting down scheduler");
  122. if (run_sched_ev) {
  123. event_del(run_sched_ev);
  124. tor_event_free(run_sched_ev);
  125. run_sched_ev = NULL;
  126. }
  127. if (channels_waiting_for_cells) {
  128. smartlist_free(channels_waiting_for_cells);
  129. channels_waiting_for_cells = NULL;
  130. }
  131. if (channels_waiting_to_write) {
  132. smartlist_free(channels_waiting_to_write);
  133. channels_waiting_to_write = NULL;
  134. }
  135. if (channels_pending) {
  136. smartlist_free(channels_pending);
  137. channels_pending = NULL;
  138. }
  139. }
  140. /*
  141. * Scheduler event callback; this should get triggered once per event loop
  142. * if any scheduling work was created during the event loop.
  143. */
  144. static void
  145. scheduler_evt_callback(evutil_socket_t fd, short events, void *arg)
  146. {
  147. log_debug(LD_SCHED, "Scheduler event callback called");
  148. tor_assert(run_sched_ev);
  149. /* Run the scheduler */
  150. scheduler_run();
  151. /* Do we have more work to do? */
  152. if (scheduler_more_work()) scheduler_retrigger();
  153. }
  154. /** Mark a channel as no longer ready to accept writes */
  155. void
  156. scheduler_channel_doesnt_want_writes(channel_t *chan)
  157. {
  158. tor_assert(chan);
  159. tor_assert(channels_waiting_for_cells);
  160. tor_assert(channels_waiting_to_write);
  161. tor_assert(channels_pending);
  162. /* If it's already in pending, we can put it in waiting_to_write */
  163. if (smartlist_contains(channels_pending, chan)) {
  164. /*
  165. * It's in channels_pending, so it shouldn't be in any of
  166. * the other lists. It can't write any more, so it goes to
  167. * channels_waiting_to_write.
  168. */
  169. smartlist_remove(channels_pending, chan);
  170. smartlist_add(channels_waiting_to_write, chan);
  171. log_debug(LD_SCHED,
  172. "Channel " U64_FORMAT " at %p went from pending "
  173. "to waiting_to_write",
  174. U64_PRINTF_ARG(chan->global_identifier), chan);
  175. } else {
  176. /*
  177. * It's not in pending, so it can't become waiting_to_write; it's
  178. * either not in any of the lists (nothing to do) or it's already in
  179. * waiting_for_cells (remove it, can't write any more).
  180. */
  181. if (smartlist_contains(channels_waiting_for_cells, chan)) {
  182. smartlist_remove(channels_waiting_for_cells, chan);
  183. log_debug(LD_SCHED,
  184. "Channel " U64_FORMAT " at %p left waiting_for_cells",
  185. U64_PRINTF_ARG(chan->global_identifier), chan);
  186. }
  187. }
  188. }
  189. /** Mark a channel as having waiting cells */
  190. void
  191. scheduler_channel_has_waiting_cells(channel_t *chan)
  192. {
  193. int became_pending = 0;
  194. tor_assert(chan);
  195. tor_assert(channels_waiting_for_cells);
  196. tor_assert(channels_waiting_to_write);
  197. tor_assert(channels_pending);
  198. /* First, check if this one also writeable */
  199. if (smartlist_contains(channels_waiting_for_cells, chan)) {
  200. /*
  201. * It's in channels_waiting_for_cells, so it shouldn't be in any of
  202. * the other lists. It has waiting cells now, so it goes to
  203. * channels_pending.
  204. */
  205. smartlist_remove(channels_waiting_for_cells, chan);
  206. smartlist_add(channels_pending, chan);
  207. log_debug(LD_SCHED,
  208. "Channel " U64_FORMAT " at %p went from waiting_for_cells "
  209. "to pending",
  210. U64_PRINTF_ARG(chan->global_identifier), chan);
  211. became_pending = 1;
  212. } else {
  213. /*
  214. * It's not in waiting_for_cells, so it can't become pending; it's
  215. * either not in any of the lists (we add it to waiting_to_write)
  216. * or it's already in waiting_to_write or pending (we do nothing)
  217. */
  218. if (!(smartlist_contains(channels_waiting_to_write, chan) ||
  219. smartlist_contains(channels_pending, chan))) {
  220. smartlist_add(channels_waiting_to_write, chan);
  221. log_debug(LD_SCHED,
  222. "Channel " U64_FORMAT " at %p entered waiting_to_write",
  223. U64_PRINTF_ARG(chan->global_identifier), chan);
  224. }
  225. }
  226. /*
  227. * If we made a channel pending, we potentially have scheduling work
  228. * to do.
  229. */
  230. if (became_pending) scheduler_retrigger();
  231. }
  232. /** Set up the scheduling system */
  233. void
  234. scheduler_init(void)
  235. {
  236. log_debug(LD_SCHED, "Initting scheduler");
  237. tor_assert(!run_sched_ev);
  238. run_sched_ev = tor_event_new(tor_libevent_get_base(), -1,
  239. 0, scheduler_evt_callback, NULL);
  240. channels_waiting_for_cells = smartlist_new();
  241. channels_waiting_to_write = smartlist_new();
  242. channels_pending = smartlist_new();
  243. }
  244. /** Check if there's more scheduling work */
  245. static int
  246. scheduler_more_work(void)
  247. {
  248. tor_assert(channels_pending);
  249. return (smartlist_len(channels_pending) > 0) ? 1 : 0;
  250. }
  251. /** Retrigger the scheduler in a way safe to use from the callback */
  252. static void
  253. scheduler_retrigger(void)
  254. {
  255. tor_assert(run_sched_ev);
  256. if (!evtimer_pending(run_sched_ev, NULL)) {
  257. log_debug(LD_SCHED, "Retriggering scheduler event");
  258. event_del(run_sched_ev);
  259. evtimer_add(run_sched_ev, &run_sched_tv);
  260. }
  261. }
  262. /** Notify the scheduler of a channel being closed */
  263. void
  264. scheduler_release_channel(channel_t *chan)
  265. {
  266. tor_assert(chan);
  267. tor_assert(channels_waiting_for_cells);
  268. tor_assert(channels_waiting_to_write);
  269. tor_assert(channels_pending);
  270. smartlist_remove(channels_waiting_for_cells, chan);
  271. smartlist_remove(channels_waiting_to_write, chan);
  272. smartlist_remove(channels_pending, chan);
  273. }
  274. /** Run the scheduling algorithm if necessary */
  275. void
  276. scheduler_run(void)
  277. {
  278. smartlist_t *tmp = NULL;
  279. log_debug(LD_SCHED, "We have a chance to run the scheduler");
  280. /*
  281. * TODO make this work properly
  282. *
  283. * For now, just empty the pending list and log that we saw stuff in it
  284. */
  285. tmp = channels_pending;
  286. channels_pending = smartlist_new();
  287. SMARTLIST_FOREACH_BEGIN(tmp, channel_t *, chan) {
  288. log_debug(LD_SCHED,
  289. "Scheduler saw pending channel " U64_FORMAT " at %p",
  290. U64_PRINTF_ARG(chan->global_identifier), chan);
  291. } SMARTLIST_FOREACH_END(chan);
  292. smartlist_free(tmp);
  293. }
  294. /** Trigger the scheduling event so we run the scheduler later */
  295. static void
  296. scheduler_trigger(void)
  297. {
  298. log_debug(LD_SCHED, "Triggering scheduler event");
  299. tor_assert(run_sched_ev);
  300. run_sched_tv.tv_sec = 0;
  301. run_sched_tv.tv_usec = 0;
  302. evtimer_add(run_sched_ev, &run_sched_tv);
  303. }
  304. /** Mark a channel as ready to accept writes */
  305. void
  306. scheduler_channel_wants_writes(channel_t *chan)
  307. {
  308. int became_pending = 0;
  309. tor_assert(chan);
  310. tor_assert(channels_waiting_for_cells);
  311. tor_assert(channels_waiting_to_write);
  312. tor_assert(channels_pending);
  313. /* If it's already in waiting_to_write, we can put it in pending */
  314. if (smartlist_contains(channels_waiting_to_write, chan)) {
  315. /*
  316. * It's in channels_waiting_to_write, so it shouldn't be in any of
  317. * the other lists. It can write now, so it goes to channels_pending.
  318. */
  319. smartlist_remove(channels_waiting_to_write, chan);
  320. smartlist_add(channels_pending, chan);
  321. log_debug(LD_SCHED,
  322. "Channel " U64_FORMAT " at %p went from waiting_to_write "
  323. "to pending",
  324. U64_PRINTF_ARG(chan->global_identifier), chan);
  325. became_pending = 1;
  326. } else {
  327. /*
  328. * It's not in waiting_to_write, so it can't become pending; it's
  329. * either not in any of the lists (we add it to waiting_for_cells)
  330. * or it's already in waiting_for_cells or pending (we do nothing)
  331. */
  332. if (!(smartlist_contains(channels_waiting_for_cells, chan) ||
  333. smartlist_contains(channels_pending, chan))) {
  334. smartlist_add(channels_waiting_for_cells, chan);
  335. log_debug(LD_SCHED,
  336. "Channel " U64_FORMAT " at %p entered waiting_for_cells",
  337. U64_PRINTF_ARG(chan->global_identifier), chan);
  338. }
  339. }
  340. /*
  341. * If we made a channel pending, we potentially have scheduling work
  342. * to do.
  343. */
  344. if (became_pending) scheduler_retrigger();
  345. }