scheduler.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. #if 0
  116. static void scheduler_trigger(void);
  117. #endif
  118. /* Scheduler function implementations */
  119. /** Free everything and shut down the scheduling system */
  120. void
  121. scheduler_free_all(void)
  122. {
  123. log_debug(LD_SCHED, "Shutting down scheduler");
  124. if (run_sched_ev) {
  125. event_del(run_sched_ev);
  126. tor_event_free(run_sched_ev);
  127. run_sched_ev = NULL;
  128. }
  129. if (channels_waiting_for_cells) {
  130. smartlist_free(channels_waiting_for_cells);
  131. channels_waiting_for_cells = NULL;
  132. }
  133. if (channels_waiting_to_write) {
  134. smartlist_free(channels_waiting_to_write);
  135. channels_waiting_to_write = NULL;
  136. }
  137. if (channels_pending) {
  138. smartlist_free(channels_pending);
  139. channels_pending = NULL;
  140. }
  141. }
  142. /*
  143. * Scheduler event callback; this should get triggered once per event loop
  144. * if any scheduling work was created during the event loop.
  145. */
  146. static void
  147. scheduler_evt_callback(evutil_socket_t fd, short events, void *arg)
  148. {
  149. log_debug(LD_SCHED, "Scheduler event callback called");
  150. tor_assert(run_sched_ev);
  151. /* Run the scheduler */
  152. scheduler_run();
  153. /* Do we have more work to do? */
  154. if (scheduler_more_work()) scheduler_retrigger();
  155. }
  156. /** Mark a channel as no longer ready to accept writes */
  157. void
  158. scheduler_channel_doesnt_want_writes(channel_t *chan)
  159. {
  160. tor_assert(chan);
  161. tor_assert(channels_waiting_for_cells);
  162. tor_assert(channels_waiting_to_write);
  163. tor_assert(channels_pending);
  164. /* If it's already in pending, we can put it in waiting_to_write */
  165. if (smartlist_contains(channels_pending, chan)) {
  166. /*
  167. * It's in channels_pending, so it shouldn't be in any of
  168. * the other lists. It can't write any more, so it goes to
  169. * channels_waiting_to_write.
  170. */
  171. smartlist_remove(channels_pending, chan);
  172. smartlist_add(channels_waiting_to_write, chan);
  173. log_debug(LD_SCHED,
  174. "Channel " U64_FORMAT " at %p went from pending "
  175. "to waiting_to_write",
  176. U64_PRINTF_ARG(chan->global_identifier), chan);
  177. } else {
  178. /*
  179. * It's not in pending, so it can't become waiting_to_write; it's
  180. * either not in any of the lists (nothing to do) or it's already in
  181. * waiting_for_cells (remove it, can't write any more).
  182. */
  183. if (smartlist_contains(channels_waiting_for_cells, chan)) {
  184. smartlist_remove(channels_waiting_for_cells, chan);
  185. log_debug(LD_SCHED,
  186. "Channel " U64_FORMAT " at %p left waiting_for_cells",
  187. U64_PRINTF_ARG(chan->global_identifier), chan);
  188. }
  189. }
  190. }
  191. /** Mark a channel as having waiting cells */
  192. void
  193. scheduler_channel_has_waiting_cells(channel_t *chan)
  194. {
  195. int became_pending = 0;
  196. tor_assert(chan);
  197. tor_assert(channels_waiting_for_cells);
  198. tor_assert(channels_waiting_to_write);
  199. tor_assert(channels_pending);
  200. /* First, check if this one also writeable */
  201. if (smartlist_contains(channels_waiting_for_cells, chan)) {
  202. /*
  203. * It's in channels_waiting_for_cells, so it shouldn't be in any of
  204. * the other lists. It has waiting cells now, so it goes to
  205. * channels_pending.
  206. */
  207. smartlist_remove(channels_waiting_for_cells, chan);
  208. smartlist_add(channels_pending, chan);
  209. log_debug(LD_SCHED,
  210. "Channel " U64_FORMAT " at %p went from waiting_for_cells "
  211. "to pending",
  212. U64_PRINTF_ARG(chan->global_identifier), chan);
  213. became_pending = 1;
  214. } else {
  215. /*
  216. * It's not in waiting_for_cells, so it can't become pending; it's
  217. * either not in any of the lists (we add it to waiting_to_write)
  218. * or it's already in waiting_to_write or pending (we do nothing)
  219. */
  220. if (!(smartlist_contains(channels_waiting_to_write, chan) ||
  221. smartlist_contains(channels_pending, chan))) {
  222. smartlist_add(channels_waiting_to_write, chan);
  223. log_debug(LD_SCHED,
  224. "Channel " U64_FORMAT " at %p entered waiting_to_write",
  225. U64_PRINTF_ARG(chan->global_identifier), chan);
  226. }
  227. }
  228. /*
  229. * If we made a channel pending, we potentially have scheduling work
  230. * to do.
  231. */
  232. if (became_pending) scheduler_retrigger();
  233. }
  234. /** Set up the scheduling system */
  235. void
  236. scheduler_init(void)
  237. {
  238. log_debug(LD_SCHED, "Initting scheduler");
  239. tor_assert(!run_sched_ev);
  240. run_sched_ev = tor_event_new(tor_libevent_get_base(), -1,
  241. 0, scheduler_evt_callback, NULL);
  242. channels_waiting_for_cells = smartlist_new();
  243. channels_waiting_to_write = smartlist_new();
  244. channels_pending = smartlist_new();
  245. }
  246. /** Check if there's more scheduling work */
  247. static int
  248. scheduler_more_work(void)
  249. {
  250. tor_assert(channels_pending);
  251. return (smartlist_len(channels_pending) > 0) ? 1 : 0;
  252. }
  253. /** Retrigger the scheduler in a way safe to use from the callback */
  254. static void
  255. scheduler_retrigger(void)
  256. {
  257. tor_assert(run_sched_ev);
  258. if (!evtimer_pending(run_sched_ev, NULL)) {
  259. log_debug(LD_SCHED, "Retriggering scheduler event");
  260. event_del(run_sched_ev);
  261. evtimer_add(run_sched_ev, &run_sched_tv);
  262. }
  263. }
  264. /** Notify the scheduler of a channel being closed */
  265. void
  266. scheduler_release_channel(channel_t *chan)
  267. {
  268. tor_assert(chan);
  269. tor_assert(channels_waiting_for_cells);
  270. tor_assert(channels_waiting_to_write);
  271. tor_assert(channels_pending);
  272. smartlist_remove(channels_waiting_for_cells, chan);
  273. smartlist_remove(channels_waiting_to_write, chan);
  274. smartlist_remove(channels_pending, chan);
  275. }
  276. /** Run the scheduling algorithm if necessary */
  277. void
  278. scheduler_run(void)
  279. {
  280. smartlist_t *tmp = NULL;
  281. log_debug(LD_SCHED, "We have a chance to run the scheduler");
  282. /*
  283. * TODO make this work properly
  284. *
  285. * For now, just empty the pending list and log that we saw stuff in it
  286. */
  287. tmp = channels_pending;
  288. channels_pending = smartlist_new();
  289. SMARTLIST_FOREACH_BEGIN(tmp, channel_t *, chan) {
  290. log_debug(LD_SCHED,
  291. "Scheduler saw pending channel " U64_FORMAT " at %p",
  292. U64_PRINTF_ARG(chan->global_identifier), chan);
  293. } SMARTLIST_FOREACH_END(chan);
  294. smartlist_free(tmp);
  295. }
  296. /** Trigger the scheduling event so we run the scheduler later */
  297. #if 0
  298. static void
  299. scheduler_trigger(void)
  300. {
  301. log_debug(LD_SCHED, "Triggering scheduler event");
  302. tor_assert(run_sched_ev);
  303. run_sched_tv.tv_sec = 0;
  304. run_sched_tv.tv_usec = 0;
  305. evtimer_add(run_sched_ev, &run_sched_tv);
  306. }
  307. #endif
  308. /** Mark a channel as ready to accept writes */
  309. void
  310. scheduler_channel_wants_writes(channel_t *chan)
  311. {
  312. int became_pending = 0;
  313. tor_assert(chan);
  314. tor_assert(channels_waiting_for_cells);
  315. tor_assert(channels_waiting_to_write);
  316. tor_assert(channels_pending);
  317. /* If it's already in waiting_to_write, we can put it in pending */
  318. if (smartlist_contains(channels_waiting_to_write, chan)) {
  319. /*
  320. * It's in channels_waiting_to_write, so it shouldn't be in any of
  321. * the other lists. It can write now, so it goes to channels_pending.
  322. */
  323. smartlist_remove(channels_waiting_to_write, chan);
  324. smartlist_add(channels_pending, chan);
  325. log_debug(LD_SCHED,
  326. "Channel " U64_FORMAT " at %p went from waiting_to_write "
  327. "to pending",
  328. U64_PRINTF_ARG(chan->global_identifier), chan);
  329. became_pending = 1;
  330. } else {
  331. /*
  332. * It's not in waiting_to_write, so it can't become pending; it's
  333. * either not in any of the lists (we add it to waiting_for_cells)
  334. * or it's already in waiting_for_cells or pending (we do nothing)
  335. */
  336. if (!(smartlist_contains(channels_waiting_for_cells, chan) ||
  337. smartlist_contains(channels_pending, chan))) {
  338. smartlist_add(channels_waiting_for_cells, chan);
  339. log_debug(LD_SCHED,
  340. "Channel " U64_FORMAT " at %p entered waiting_for_cells",
  341. U64_PRINTF_ARG(chan->global_identifier), chan);
  342. }
  343. }
  344. /*
  345. * If we made a channel pending, we potentially have scheduling work
  346. * to do.
  347. */
  348. if (became_pending) scheduler_retrigger();
  349. }