scheduler_vanilla.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* Copyright (c) 2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include <event2/event.h>
  4. #include "or.h"
  5. #include "config.h"
  6. #define TOR_CHANNEL_INTERNAL_
  7. #include "channel.h"
  8. #define SCHEDULER_PRIVATE_
  9. #include "scheduler.h"
  10. /*****************************************************************************
  11. * Other internal data
  12. *****************************************************************************/
  13. /* Maximum cells to flush in a single call to channel_flush_some_cells(); */
  14. #define MAX_FLUSH_CELLS 1000
  15. /* Stores the vanilla scheduler function pointers. */
  16. static scheduler_t *vanilla_scheduler = NULL;
  17. /*****************************************************************************
  18. * Externally called function implementations
  19. *****************************************************************************/
  20. /* Return true iff the scheduler has work to perform. */
  21. static int
  22. have_work(void)
  23. {
  24. smartlist_t *cp = get_channels_pending();
  25. IF_BUG_ONCE(!cp) {
  26. return 0; // channels_pending doesn't exist so... no work?
  27. }
  28. return smartlist_len(cp) > 0;
  29. }
  30. /** Retrigger the scheduler in a way safe to use from the callback */
  31. static void
  32. vanilla_scheduler_schedule(void)
  33. {
  34. if (!have_work()) {
  35. return;
  36. }
  37. struct event *ev = get_run_sched_ev();
  38. IF_BUG_ONCE(!ev) {
  39. log_warn(LD_SCHED, "Wow we don't have a scheduler event. That's really "
  40. "weird! We can't really schedule a scheduling run with libevent "
  41. "without it. So we're going to stop trying now and hope we have "
  42. "one next time. If we never get one, we're broken.");
  43. return;
  44. }
  45. event_active(ev, EV_TIMEOUT, 1);
  46. }
  47. static void
  48. vanilla_scheduler_run(void)
  49. {
  50. int n_cells, n_chans_before, n_chans_after;
  51. ssize_t flushed, flushed_this_time;
  52. smartlist_t *cp = get_channels_pending();
  53. smartlist_t *to_readd = NULL;
  54. channel_t *chan = NULL;
  55. log_debug(LD_SCHED, "We have a chance to run the scheduler");
  56. n_chans_before = smartlist_len(cp);
  57. while (smartlist_len(cp) > 0) {
  58. /* Pop off a channel */
  59. chan = smartlist_pqueue_pop(cp,
  60. scheduler_compare_channels,
  61. offsetof(channel_t, sched_heap_idx));
  62. IF_BUG_ONCE(!chan) {
  63. /* Some-freaking-how a NULL got into the channels_pending. That should
  64. * never happen, but it should be harmless to ignore it and keep looping.
  65. */
  66. continue;
  67. }
  68. /* Figure out how many cells we can write */
  69. n_cells = channel_num_cells_writeable(chan);
  70. if (n_cells > 0) {
  71. log_debug(LD_SCHED,
  72. "Scheduler saw pending channel " U64_FORMAT " at %p with "
  73. "%d cells writeable",
  74. U64_PRINTF_ARG(chan->global_identifier), chan, n_cells);
  75. flushed = 0;
  76. while (flushed < n_cells) {
  77. flushed_this_time =
  78. channel_flush_some_cells(chan,
  79. MIN(MAX_FLUSH_CELLS, (size_t) n_cells - flushed));
  80. if (flushed_this_time <= 0) break;
  81. flushed += flushed_this_time;
  82. }
  83. if (flushed < n_cells) {
  84. /* We ran out of cells to flush */
  85. chan->scheduler_state = SCHED_CHAN_WAITING_FOR_CELLS;
  86. log_debug(LD_SCHED,
  87. "Channel " U64_FORMAT " at %p "
  88. "entered waiting_for_cells from pending",
  89. U64_PRINTF_ARG(chan->global_identifier),
  90. chan);
  91. } else {
  92. /* The channel may still have some cells */
  93. if (channel_more_to_flush(chan)) {
  94. /* The channel goes to either pending or waiting_to_write */
  95. if (channel_num_cells_writeable(chan) > 0) {
  96. /* Add it back to pending later */
  97. if (!to_readd) to_readd = smartlist_new();
  98. smartlist_add(to_readd, chan);
  99. log_debug(LD_SCHED,
  100. "Channel " U64_FORMAT " at %p "
  101. "is still pending",
  102. U64_PRINTF_ARG(chan->global_identifier),
  103. chan);
  104. } else {
  105. /* It's waiting to be able to write more */
  106. chan->scheduler_state = SCHED_CHAN_WAITING_TO_WRITE;
  107. log_debug(LD_SCHED,
  108. "Channel " U64_FORMAT " at %p "
  109. "entered waiting_to_write from pending",
  110. U64_PRINTF_ARG(chan->global_identifier),
  111. chan);
  112. }
  113. } else {
  114. /* No cells left; it can go to idle or waiting_for_cells */
  115. if (channel_num_cells_writeable(chan) > 0) {
  116. /*
  117. * It can still accept writes, so it goes to
  118. * waiting_for_cells
  119. */
  120. chan->scheduler_state = SCHED_CHAN_WAITING_FOR_CELLS;
  121. log_debug(LD_SCHED,
  122. "Channel " U64_FORMAT " at %p "
  123. "entered waiting_for_cells from pending",
  124. U64_PRINTF_ARG(chan->global_identifier),
  125. chan);
  126. } else {
  127. /*
  128. * We exactly filled up the output queue with all available
  129. * cells; go to idle.
  130. */
  131. chan->scheduler_state = SCHED_CHAN_IDLE;
  132. log_debug(LD_SCHED,
  133. "Channel " U64_FORMAT " at %p "
  134. "become idle from pending",
  135. U64_PRINTF_ARG(chan->global_identifier),
  136. chan);
  137. }
  138. }
  139. }
  140. log_debug(LD_SCHED,
  141. "Scheduler flushed %d cells onto pending channel "
  142. U64_FORMAT " at %p",
  143. (int)flushed, U64_PRINTF_ARG(chan->global_identifier),
  144. chan);
  145. } else {
  146. log_info(LD_SCHED,
  147. "Scheduler saw pending channel " U64_FORMAT " at %p with "
  148. "no cells writeable",
  149. U64_PRINTF_ARG(chan->global_identifier), chan);
  150. /* Put it back to WAITING_TO_WRITE */
  151. chan->scheduler_state = SCHED_CHAN_WAITING_TO_WRITE;
  152. }
  153. }
  154. /* Readd any channels we need to */
  155. if (to_readd) {
  156. SMARTLIST_FOREACH_BEGIN(to_readd, channel_t *, readd_chan) {
  157. readd_chan->scheduler_state = SCHED_CHAN_PENDING;
  158. smartlist_pqueue_add(cp,
  159. scheduler_compare_channels,
  160. offsetof(channel_t, sched_heap_idx),
  161. readd_chan);
  162. } SMARTLIST_FOREACH_END(readd_chan);
  163. smartlist_free(to_readd);
  164. }
  165. n_chans_after = smartlist_len(cp);
  166. log_debug(LD_SCHED, "Scheduler handled %d of %d pending channels",
  167. n_chans_before - n_chans_after, n_chans_before);
  168. }
  169. scheduler_t *
  170. get_vanilla_scheduler(void)
  171. {
  172. if (!vanilla_scheduler) {
  173. log_debug(LD_SCHED, "Initializing vanilla scheduler struct");
  174. vanilla_scheduler = tor_malloc_zero(sizeof(*vanilla_scheduler));
  175. vanilla_scheduler->free_all = NULL;
  176. vanilla_scheduler->on_channel_free = NULL;
  177. vanilla_scheduler->init = NULL;
  178. vanilla_scheduler->on_new_consensus = NULL;
  179. vanilla_scheduler->schedule = vanilla_scheduler_schedule;
  180. vanilla_scheduler->run = vanilla_scheduler_run;
  181. vanilla_scheduler->on_new_options = NULL;
  182. }
  183. return vanilla_scheduler;
  184. }