scheduler.c 13 KB

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