scheduler_vanilla.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. tor_assert(cp);
  26. return smartlist_len(cp) > 0;
  27. }
  28. /** Retrigger the scheduler in a way safe to use from the callback */
  29. static void
  30. vanilla_scheduler_schedule(void)
  31. {
  32. if (!have_work()) {
  33. return;
  34. }
  35. struct event *ev = get_run_sched_ev();
  36. tor_assert(ev);
  37. event_active(ev, EV_TIMEOUT, 1);
  38. }
  39. static void
  40. vanilla_scheduler_run(void)
  41. {
  42. int n_cells, n_chans_before, n_chans_after;
  43. ssize_t flushed, flushed_this_time;
  44. smartlist_t *cp = get_channels_pending();
  45. smartlist_t *to_readd = NULL;
  46. channel_t *chan = NULL;
  47. log_debug(LD_SCHED, "We have a chance to run the scheduler");
  48. n_chans_before = smartlist_len(cp);
  49. while (smartlist_len(cp) > 0) {
  50. /* Pop off a channel */
  51. chan = smartlist_pqueue_pop(cp,
  52. scheduler_compare_channels,
  53. offsetof(channel_t, sched_heap_idx));
  54. tor_assert(chan);
  55. /* Figure out how many cells we can write */
  56. n_cells = channel_num_cells_writeable(chan);
  57. if (n_cells > 0) {
  58. log_debug(LD_SCHED,
  59. "Scheduler saw pending channel " U64_FORMAT " at %p with "
  60. "%d cells writeable",
  61. U64_PRINTF_ARG(chan->global_identifier), chan, n_cells);
  62. flushed = 0;
  63. while (flushed < n_cells) {
  64. flushed_this_time =
  65. channel_flush_some_cells(chan,
  66. MIN(MAX_FLUSH_CELLS, (size_t) n_cells - flushed));
  67. if (flushed_this_time <= 0) break;
  68. flushed += flushed_this_time;
  69. }
  70. if (flushed < n_cells) {
  71. /* We ran out of cells to flush */
  72. chan->scheduler_state = SCHED_CHAN_WAITING_FOR_CELLS;
  73. log_debug(LD_SCHED,
  74. "Channel " U64_FORMAT " at %p "
  75. "entered waiting_for_cells from pending",
  76. U64_PRINTF_ARG(chan->global_identifier),
  77. chan);
  78. } else {
  79. /* The channel may still have some cells */
  80. if (channel_more_to_flush(chan)) {
  81. /* The channel goes to either pending or waiting_to_write */
  82. if (channel_num_cells_writeable(chan) > 0) {
  83. /* Add it back to pending later */
  84. if (!to_readd) to_readd = smartlist_new();
  85. smartlist_add(to_readd, chan);
  86. log_debug(LD_SCHED,
  87. "Channel " U64_FORMAT " at %p "
  88. "is still pending",
  89. U64_PRINTF_ARG(chan->global_identifier),
  90. chan);
  91. } else {
  92. /* It's waiting to be able to write more */
  93. chan->scheduler_state = SCHED_CHAN_WAITING_TO_WRITE;
  94. log_debug(LD_SCHED,
  95. "Channel " U64_FORMAT " at %p "
  96. "entered waiting_to_write from pending",
  97. U64_PRINTF_ARG(chan->global_identifier),
  98. chan);
  99. }
  100. } else {
  101. /* No cells left; it can go to idle or waiting_for_cells */
  102. if (channel_num_cells_writeable(chan) > 0) {
  103. /*
  104. * It can still accept writes, so it goes to
  105. * waiting_for_cells
  106. */
  107. chan->scheduler_state = SCHED_CHAN_WAITING_FOR_CELLS;
  108. log_debug(LD_SCHED,
  109. "Channel " U64_FORMAT " at %p "
  110. "entered waiting_for_cells from pending",
  111. U64_PRINTF_ARG(chan->global_identifier),
  112. chan);
  113. } else {
  114. /*
  115. * We exactly filled up the output queue with all available
  116. * cells; go to idle.
  117. */
  118. chan->scheduler_state = SCHED_CHAN_IDLE;
  119. log_debug(LD_SCHED,
  120. "Channel " U64_FORMAT " at %p "
  121. "become idle from pending",
  122. U64_PRINTF_ARG(chan->global_identifier),
  123. chan);
  124. }
  125. }
  126. }
  127. log_debug(LD_SCHED,
  128. "Scheduler flushed %d cells onto pending channel "
  129. U64_FORMAT " at %p",
  130. (int)flushed, U64_PRINTF_ARG(chan->global_identifier),
  131. chan);
  132. } else {
  133. log_info(LD_SCHED,
  134. "Scheduler saw pending channel " U64_FORMAT " at %p with "
  135. "no cells writeable",
  136. U64_PRINTF_ARG(chan->global_identifier), chan);
  137. /* Put it back to WAITING_TO_WRITE */
  138. chan->scheduler_state = SCHED_CHAN_WAITING_TO_WRITE;
  139. }
  140. }
  141. /* Readd any channels we need to */
  142. if (to_readd) {
  143. SMARTLIST_FOREACH_BEGIN(to_readd, channel_t *, readd_chan) {
  144. readd_chan->scheduler_state = SCHED_CHAN_PENDING;
  145. smartlist_pqueue_add(cp,
  146. scheduler_compare_channels,
  147. offsetof(channel_t, sched_heap_idx),
  148. readd_chan);
  149. } SMARTLIST_FOREACH_END(readd_chan);
  150. smartlist_free(to_readd);
  151. }
  152. n_chans_after = smartlist_len(cp);
  153. log_debug(LD_SCHED, "Scheduler handled %d of %d pending channels",
  154. n_chans_before - n_chans_after, n_chans_before);
  155. }
  156. scheduler_t *
  157. get_vanilla_scheduler(void)
  158. {
  159. if (!vanilla_scheduler) {
  160. log_debug(LD_SCHED, "Initializing vanilla scheduler struct");
  161. vanilla_scheduler = tor_malloc_zero(sizeof(*vanilla_scheduler));
  162. vanilla_scheduler->free_all = NULL;
  163. vanilla_scheduler->on_channel_free = NULL;
  164. vanilla_scheduler->init = NULL;
  165. vanilla_scheduler->on_new_consensus = NULL;
  166. vanilla_scheduler->schedule = vanilla_scheduler_schedule;
  167. vanilla_scheduler->run = vanilla_scheduler_run;
  168. vanilla_scheduler->on_new_options = NULL;
  169. }
  170. return vanilla_scheduler;
  171. }