scheduler.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /* * Copyright (c) 2013-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "or.h"
  4. #include "config.h"
  5. #include "compat_libevent.h"
  6. #define SCHEDULER_PRIVATE_
  7. #define SCHEDULER_KIST_PRIVATE
  8. #include "scheduler.h"
  9. #include <event2/event.h>
  10. /**
  11. * \file scheduler.c
  12. * \brief Channel scheduling system: decides which channels should send and
  13. * receive when.
  14. *
  15. * This module is the global/common parts of the scheduling system. This system
  16. * is what decides what channels get to send cells on their circuits and when.
  17. *
  18. * Terms:
  19. * - "Scheduling system": the collection of scheduler*.{h,c} files and their
  20. * aggregate behavior.
  21. * - "Scheduler implementation": a scheduler_t. The scheduling system has one
  22. * active scheduling implementation at a time.
  23. *
  24. * In this file you will find state that any scheduler implmentation can have
  25. * access to as well as the functions the rest of Tor uses to interact with the
  26. * scheduling system.
  27. *
  28. * The earliest versions of Tor approximated a kind of round-robin system
  29. * among active connections, but only approximated it. It would only consider
  30. * one connection (roughly equal to a channel in today's terms) at a time, and
  31. * thus could only prioritize circuits against others on the same connection.
  32. *
  33. * Then in response to the KIST paper[0], Tor implemented a global
  34. * circuit scheduler. It was supposed to prioritize circuits across man
  35. * channels, but wasn't effective. It is preserved in scheduler_vanilla.c.
  36. *
  37. * [0]: http://www.robgjansen.com/publications/kist-sec2014.pdf
  38. *
  39. * Then we actually got around to implementing KIST for real. We decided to
  40. * modularize the scheduler so new ones can be implemented. You can find KIST
  41. * in scheduler_kist.c.
  42. *
  43. * Channels have one of four scheduling states based on whether or not they
  44. * have cells to send and whether or not they are able to send.
  45. *
  46. * <ol>
  47. * <li>
  48. * Not open for writes, no cells to send.
  49. * <ul><li> Not much to do here, and the channel will have scheduler_state
  50. * == SCHED_CHAN_IDLE
  51. * <li> Transitions from:
  52. * <ul>
  53. * <li>Open for writes/has cells by simultaneously draining all circuit
  54. * queues and filling the output buffer.
  55. * </ul>
  56. * <li> Transitions to:
  57. * <ul>
  58. * <li> Not open for writes/has cells by arrival of cells on an attached
  59. * circuit (this would be driven from append_cell_to_circuit_queue())
  60. * <li> Open for writes/no cells by a channel type specific path;
  61. * driven from connection_or_flushed_some() for channel_tls_t.
  62. * </ul>
  63. * </ul>
  64. *
  65. * <li> Open for writes, no cells to send
  66. * <ul>
  67. * <li>Not much here either; this will be the state an idle but open
  68. * channel can be expected to settle in. It will have scheduler_state
  69. * == SCHED_CHAN_WAITING_FOR_CELLS
  70. * <li> Transitions from:
  71. * <ul>
  72. * <li>Not open for writes/no cells by flushing some of the output
  73. * buffer.
  74. * <li>Open for writes/has cells by the scheduler moving cells from
  75. * circuit queues to channel output queue, but not having enough
  76. * to fill the output queue.
  77. * </ul>
  78. * <li> Transitions to:
  79. * <ul>
  80. * <li>Open for writes/has cells by arrival of new cells on an attached
  81. * circuit, in append_cell_to_circuit_queue()
  82. * </ul>
  83. * </ul>
  84. *
  85. * <li>Not open for writes, cells to send
  86. * <ul>
  87. * <li>This is the state of a busy circuit limited by output bandwidth;
  88. * cells have piled up in the circuit queues waiting to be relayed.
  89. * The channel will have scheduler_state == SCHED_CHAN_WAITING_TO_WRITE.
  90. * <li> Transitions from:
  91. * <ul>
  92. * <li>Not open for writes/no cells by arrival of cells on an attached
  93. * circuit
  94. * <li> Open for writes/has cells by filling an output buffer without
  95. * draining all cells from attached circuits
  96. * </ul>
  97. * <li> Transitions to:
  98. * <ul>
  99. * <li>Opens for writes/has cells by draining some of the output buffer
  100. * via the connection_or_flushed_some() path (for channel_tls_t).
  101. * </ul>
  102. * </ul>
  103. *
  104. * <li>Open for writes, cells to send
  105. * <ul>
  106. * <li>This connection is ready to relay some cells and waiting for
  107. * the scheduler to choose it. The channel will have scheduler_state ==
  108. * SCHED_CHAN_PENDING.
  109. * <li>Transitions from:
  110. * <ul>
  111. * <li> Not open for writes/has cells by the connection_or_flushed_some()
  112. * path
  113. * <li> Open for writes/no cells by the append_cell_to_circuit_queue()
  114. * path
  115. * </ul>
  116. * <li> Transitions to:
  117. * <ul>
  118. * <li>Not open for writes/no cells by draining all circuit queues and
  119. * simultaneously filling the output buffer.
  120. * <li>Not open for writes/has cells by writing enough cells to fill the
  121. * output buffer
  122. * <li>Open for writes/no cells by draining all attached circuit queues
  123. * without also filling the output buffer
  124. * </ul>
  125. * </ul>
  126. * </ol>
  127. *
  128. * Other event-driven parts of the code move channels between these scheduling
  129. * states by calling scheduler functions. The scheduling system builds up a
  130. * list of channels in the SCHED_CHAN_PENDING state that the scheduler
  131. * implementation should then use when it runs. Scheduling implementations need
  132. * to properly update channel states during their scheduler_t->run() function
  133. * as that is the only opportunity for channels to move from SCHED_CHAN_PENDING
  134. * to any other state.
  135. *
  136. * The remainder of this file is a small amount of state that any scheduler
  137. * implementation should have access to, and the functions the rest of Tor uses
  138. * to interact with the scheduling system.
  139. */
  140. /*****************************************************************************
  141. * Scheduling system state
  142. *
  143. * State that can be accessed from any scheduler implementation (but not
  144. * outside the scheduling system)
  145. *****************************************************************************/
  146. STATIC const scheduler_t *the_scheduler;
  147. /*
  148. * We keep a list of channels that are pending - i.e, have cells to write
  149. * and can accept them to send. The enum scheduler_state in channel_t
  150. * is reserved for our use.
  151. *
  152. * Priority queue of channels that can write and have cells (pending work)
  153. */
  154. STATIC smartlist_t *channels_pending = NULL;
  155. /*
  156. * This event runs the scheduler from its callback, and is manually
  157. * activated whenever a channel enters open for writes/cells to send.
  158. */
  159. STATIC struct event *run_sched_ev = NULL;
  160. /*****************************************************************************
  161. * Scheduling system static function definitions
  162. *
  163. * Functions that can only be accessed from this file.
  164. *****************************************************************************/
  165. /*
  166. * Scheduler event callback; this should get triggered once per event loop
  167. * if any scheduling work was created during the event loop.
  168. */
  169. static void
  170. scheduler_evt_callback(evutil_socket_t fd, short events, void *arg)
  171. {
  172. (void) fd;
  173. (void) events;
  174. (void) arg;
  175. log_debug(LD_SCHED, "Scheduler event callback called");
  176. /* Run the scheduler. This is a mandatory function. */
  177. /* We might as well assert on this. If this function doesn't exist, no cells
  178. * are getting scheduled. Things are very broken. scheduler_t says the run()
  179. * function is mandatory. */
  180. tor_assert(the_scheduler->run);
  181. the_scheduler->run();
  182. /* Schedule itself back in if it has more work. */
  183. /* Again, might as well assert on this mandatory scheduler_t function. If it
  184. * doesn't exist, there's no way to tell libevent to run the scheduler again
  185. * in the future. */
  186. tor_assert(the_scheduler->schedule);
  187. the_scheduler->schedule();
  188. }
  189. /*****************************************************************************
  190. * Scheduling system private function definitions
  191. *
  192. * Functions that can only be accessed from scheduler*.c
  193. *****************************************************************************/
  194. /* Return the pending channel list. */
  195. smartlist_t *
  196. get_channels_pending(void)
  197. {
  198. return channels_pending;
  199. }
  200. /* Comparison function to use when sorting pending channels */
  201. MOCK_IMPL(int,
  202. scheduler_compare_channels, (const void *c1_v, const void *c2_v))
  203. {
  204. const channel_t *c1 = NULL, *c2 = NULL;
  205. /* These are a workaround for -Wbad-function-cast throwing a fit */
  206. const circuitmux_policy_t *p1, *p2;
  207. uintptr_t p1_i, p2_i;
  208. c1 = (const channel_t *)(c1_v);
  209. c2 = (const channel_t *)(c2_v);
  210. IF_BUG_ONCE(!c1 || !c2) {
  211. if (c1 && !c2) {
  212. return -1;
  213. } else if (c2 && !c1) {
  214. return 1;
  215. } else {
  216. return -1;
  217. }
  218. }
  219. if (c1 != c2) {
  220. if (circuitmux_get_policy(c1->cmux) ==
  221. circuitmux_get_policy(c2->cmux)) {
  222. /* Same cmux policy, so use the mux comparison */
  223. return circuitmux_compare_muxes(c1->cmux, c2->cmux);
  224. } else {
  225. /*
  226. * Different policies; not important to get this edge case perfect
  227. * because the current code never actually gives different channels
  228. * different cmux policies anyway. Just use this arbitrary but
  229. * definite choice.
  230. */
  231. p1 = circuitmux_get_policy(c1->cmux);
  232. p2 = circuitmux_get_policy(c2->cmux);
  233. p1_i = (uintptr_t)p1;
  234. p2_i = (uintptr_t)p2;
  235. return (p1_i < p2_i) ? -1 : 1;
  236. }
  237. } else {
  238. /* c1 == c2, so always equal */
  239. return 0;
  240. }
  241. }
  242. /*****************************************************************************
  243. * Scheduling system global functions
  244. *
  245. * Functions that can be accessed from anywhere in Tor.
  246. *****************************************************************************/
  247. /* Using the global options, select the scheduler we should be using. */
  248. static void
  249. select_scheduler(void)
  250. {
  251. const char *chosen_sched_type = NULL;
  252. #ifdef TOR_UNIT_TESTS
  253. /* This is hella annoying to set in the options for every test that passes
  254. * through the scheduler and there are many so if we don't explicitely have
  255. * a list of types set, just put the vanilla one. */
  256. if (get_options()->SchedulerTypes_ == NULL) {
  257. the_scheduler = get_vanilla_scheduler();
  258. return;
  259. }
  260. #endif
  261. /* This list is ordered that is first entry has the first priority. Thus, as
  262. * soon as we find a scheduler type that we can use, we use it and stop. */
  263. SMARTLIST_FOREACH_BEGIN(get_options()->SchedulerTypes_, int *, type) {
  264. switch (*type) {
  265. case SCHEDULER_VANILLA:
  266. the_scheduler = get_vanilla_scheduler();
  267. chosen_sched_type = "Vanilla";
  268. goto end;
  269. case SCHEDULER_KIST:
  270. if (!scheduler_can_use_kist()) {
  271. #ifdef HAVE_KIST_SUPPORT
  272. if (get_options()->KISTSchedRunInterval == -1) {
  273. log_info(LD_SCHED, "Scheduler type KIST can not be used. It is "
  274. "disabled because KISTSchedRunInterval=-1");
  275. } else {
  276. log_notice(LD_SCHED, "Scheduler type KIST has been disabled by "
  277. "the consensus.");
  278. }
  279. #else /* HAVE_KIST_SUPPORT */
  280. log_info(LD_SCHED, "Scheduler type KIST not built in");
  281. #endif /* HAVE_KIST_SUPPORT */
  282. continue;
  283. }
  284. the_scheduler = get_kist_scheduler();
  285. chosen_sched_type = "KIST";
  286. scheduler_kist_set_full_mode();
  287. goto end;
  288. case SCHEDULER_KIST_LITE:
  289. chosen_sched_type = "KISTLite";
  290. the_scheduler = get_kist_scheduler();
  291. scheduler_kist_set_lite_mode();
  292. goto end;
  293. default:
  294. /* Our option validation should have caught this. */
  295. tor_assert_unreached();
  296. }
  297. } SMARTLIST_FOREACH_END(type);
  298. end:
  299. log_notice(LD_CONFIG, "Scheduler type %s has been enabled.",
  300. chosen_sched_type);
  301. }
  302. /*
  303. * Little helper function called from a few different places. It changes the
  304. * scheduler implementation, if necessary. And if it did, it then tells the
  305. * old one to free its state and the new one to initialize.
  306. */
  307. static void
  308. set_scheduler(void)
  309. {
  310. const scheduler_t *old_scheduler = the_scheduler;
  311. /* From the options, select the scheduler type to set. */
  312. select_scheduler();
  313. if (old_scheduler != the_scheduler) {
  314. /* Allow the old scheduler to clean up, if needed. */
  315. if (old_scheduler && old_scheduler->free_all) {
  316. old_scheduler->free_all();
  317. }
  318. /* We don't clean up the old scheduler_t. We keep any type of scheduler
  319. * we've allocated so we can do an easy switch back. */
  320. /* Initialize the new scheduler. */
  321. if (the_scheduler->init) {
  322. the_scheduler->init();
  323. }
  324. }
  325. }
  326. /*
  327. * This is how the scheduling system is notified of Tor's configuration
  328. * changing. For example: a SIGHUP was issued.
  329. */
  330. void
  331. scheduler_conf_changed(void)
  332. {
  333. /* Let the scheduler decide what it should do. */
  334. set_scheduler();
  335. /* Then tell the (possibly new) scheduler that we have new options. */
  336. if (the_scheduler->on_new_options) {
  337. the_scheduler->on_new_options();
  338. }
  339. }
  340. /*
  341. * Whenever we get a new consensus, this function is called.
  342. */
  343. void
  344. scheduler_notify_networkstatus_changed(const networkstatus_t *old_c,
  345. const networkstatus_t *new_c)
  346. {
  347. /* Then tell the (possibly new) scheduler that we have a new consensus */
  348. if (the_scheduler->on_new_consensus) {
  349. the_scheduler->on_new_consensus(old_c, new_c);
  350. }
  351. /* Maybe the consensus param made us change the scheduler. */
  352. set_scheduler();
  353. }
  354. /*
  355. * Free everything scheduling-related from main.c. Note this is only called
  356. * when Tor is shutting down, while scheduler_t->free_all() is called both when
  357. * Tor is shutting down and when we are switching schedulers.
  358. */
  359. void
  360. scheduler_free_all(void)
  361. {
  362. log_debug(LD_SCHED, "Shutting down scheduler");
  363. if (run_sched_ev) {
  364. if (event_del(run_sched_ev) < 0) {
  365. log_warn(LD_BUG, "Problem deleting run_sched_ev");
  366. }
  367. tor_event_free(run_sched_ev);
  368. run_sched_ev = NULL;
  369. }
  370. if (channels_pending) {
  371. /* We don't have ownership of the object in this list. */
  372. smartlist_free(channels_pending);
  373. channels_pending = NULL;
  374. }
  375. if (the_scheduler && the_scheduler->free_all) {
  376. the_scheduler->free_all();
  377. }
  378. the_scheduler = NULL;
  379. }
  380. /** Mark a channel as no longer ready to accept writes */
  381. MOCK_IMPL(void,
  382. scheduler_channel_doesnt_want_writes,(channel_t *chan))
  383. {
  384. IF_BUG_ONCE(!chan) {
  385. return;
  386. }
  387. IF_BUG_ONCE(!channels_pending) {
  388. return;
  389. }
  390. /* If it's already in pending, we can put it in waiting_to_write */
  391. if (chan->scheduler_state == SCHED_CHAN_PENDING) {
  392. /*
  393. * It's in channels_pending, so it shouldn't be in any of
  394. * the other lists. It can't write any more, so it goes to
  395. * channels_waiting_to_write.
  396. */
  397. smartlist_pqueue_remove(channels_pending,
  398. scheduler_compare_channels,
  399. offsetof(channel_t, sched_heap_idx),
  400. chan);
  401. chan->scheduler_state = SCHED_CHAN_WAITING_TO_WRITE;
  402. log_debug(LD_SCHED,
  403. "Channel " U64_FORMAT " at %p went from pending "
  404. "to waiting_to_write",
  405. U64_PRINTF_ARG(chan->global_identifier), chan);
  406. } else {
  407. /*
  408. * It's not in pending, so it can't become waiting_to_write; it's
  409. * either not in any of the lists (nothing to do) or it's already in
  410. * waiting_for_cells (remove it, can't write any more).
  411. */
  412. if (chan->scheduler_state == SCHED_CHAN_WAITING_FOR_CELLS) {
  413. chan->scheduler_state = SCHED_CHAN_IDLE;
  414. log_debug(LD_SCHED,
  415. "Channel " U64_FORMAT " at %p left waiting_for_cells",
  416. U64_PRINTF_ARG(chan->global_identifier), chan);
  417. }
  418. }
  419. }
  420. /** Mark a channel as having waiting cells */
  421. MOCK_IMPL(void,
  422. scheduler_channel_has_waiting_cells,(channel_t *chan))
  423. {
  424. IF_BUG_ONCE(!chan) {
  425. return;
  426. }
  427. IF_BUG_ONCE(!channels_pending) {
  428. return;
  429. }
  430. /* First, check if this one also writeable */
  431. if (chan->scheduler_state == SCHED_CHAN_WAITING_FOR_CELLS) {
  432. /*
  433. * It's in channels_waiting_for_cells, so it shouldn't be in any of
  434. * the other lists. It has waiting cells now, so it goes to
  435. * channels_pending.
  436. */
  437. chan->scheduler_state = SCHED_CHAN_PENDING;
  438. smartlist_pqueue_add(channels_pending,
  439. scheduler_compare_channels,
  440. offsetof(channel_t, sched_heap_idx),
  441. chan);
  442. log_debug(LD_SCHED,
  443. "Channel " U64_FORMAT " at %p went from waiting_for_cells "
  444. "to pending",
  445. U64_PRINTF_ARG(chan->global_identifier), chan);
  446. /* If we made a channel pending, we potentially have scheduling work to
  447. * do. */
  448. the_scheduler->schedule();
  449. } else {
  450. /*
  451. * It's not in waiting_for_cells, so it can't become pending; it's
  452. * either not in any of the lists (we add it to waiting_to_write)
  453. * or it's already in waiting_to_write or pending (we do nothing)
  454. */
  455. if (!(chan->scheduler_state == SCHED_CHAN_WAITING_TO_WRITE ||
  456. chan->scheduler_state == SCHED_CHAN_PENDING)) {
  457. chan->scheduler_state = SCHED_CHAN_WAITING_TO_WRITE;
  458. log_debug(LD_SCHED,
  459. "Channel " U64_FORMAT " at %p entered waiting_to_write",
  460. U64_PRINTF_ARG(chan->global_identifier), chan);
  461. }
  462. }
  463. }
  464. /* Add the scheduler event to the set of pending events with next_run being
  465. * the time up to libevent should wait before triggering the event. */
  466. void
  467. scheduler_ev_add(const struct timeval *next_run)
  468. {
  469. tor_assert(run_sched_ev);
  470. tor_assert(next_run);
  471. event_add(run_sched_ev, next_run);
  472. }
  473. /* Make the scheduler event active with the given flags. */
  474. void
  475. scheduler_ev_active(int flags)
  476. {
  477. tor_assert(run_sched_ev);
  478. event_active(run_sched_ev, flags, 1);
  479. }
  480. /*
  481. * Initialize everything scheduling-related from config.c. Note this is only
  482. * called when Tor is starting up, while scheduler_t->init() is called both
  483. * when Tor is starting up and when we are switching schedulers.
  484. */
  485. void
  486. scheduler_init(void)
  487. {
  488. log_debug(LD_SCHED, "Initting scheduler");
  489. // Two '!' because we really do want to check if the pointer is non-NULL
  490. IF_BUG_ONCE(!!run_sched_ev) {
  491. log_warn(LD_SCHED, "We should not already have a libevent scheduler event."
  492. "I'll clean the old one up, but this is odd.");
  493. tor_event_free(run_sched_ev);
  494. run_sched_ev = NULL;
  495. }
  496. run_sched_ev = tor_event_new(tor_libevent_get_base(), -1,
  497. 0, scheduler_evt_callback, NULL);
  498. channels_pending = smartlist_new();
  499. set_scheduler();
  500. }
  501. /*
  502. * If a channel is going away, this is how the scheduling system is informed
  503. * so it can do any freeing necessary. This ultimately calls
  504. * scheduler_t->on_channel_free() so the current scheduler can release any
  505. * state specific to this channel.
  506. */
  507. MOCK_IMPL(void,
  508. scheduler_release_channel,(channel_t *chan))
  509. {
  510. IF_BUG_ONCE(!chan) {
  511. return;
  512. }
  513. IF_BUG_ONCE(!channels_pending) {
  514. return;
  515. }
  516. if (chan->scheduler_state == SCHED_CHAN_PENDING) {
  517. if (smartlist_pos(channels_pending, chan) == -1) {
  518. log_warn(LD_SCHED, "Scheduler asked to release channel %" PRIu64 " "
  519. "but it wasn't in channels_pending",
  520. chan->global_identifier);
  521. } else {
  522. smartlist_pqueue_remove(channels_pending,
  523. scheduler_compare_channels,
  524. offsetof(channel_t, sched_heap_idx),
  525. chan);
  526. }
  527. }
  528. if (the_scheduler->on_channel_free) {
  529. the_scheduler->on_channel_free(chan);
  530. }
  531. chan->scheduler_state = SCHED_CHAN_IDLE;
  532. }
  533. /** Mark a channel as ready to accept writes */
  534. void
  535. scheduler_channel_wants_writes(channel_t *chan)
  536. {
  537. IF_BUG_ONCE(!chan) {
  538. return;
  539. }
  540. IF_BUG_ONCE(!channels_pending) {
  541. return;
  542. }
  543. /* If it's already in waiting_to_write, we can put it in pending */
  544. if (chan->scheduler_state == SCHED_CHAN_WAITING_TO_WRITE) {
  545. /*
  546. * It can write now, so it goes to channels_pending.
  547. */
  548. log_debug(LD_SCHED, "chan=%" PRIu64 " became pending",
  549. chan->global_identifier);
  550. smartlist_pqueue_add(channels_pending,
  551. scheduler_compare_channels,
  552. offsetof(channel_t, sched_heap_idx),
  553. chan);
  554. chan->scheduler_state = SCHED_CHAN_PENDING;
  555. log_debug(LD_SCHED,
  556. "Channel " U64_FORMAT " at %p went from waiting_to_write "
  557. "to pending",
  558. U64_PRINTF_ARG(chan->global_identifier), chan);
  559. /* We just made a channel pending, we have scheduling work to do. */
  560. the_scheduler->schedule();
  561. } else {
  562. /*
  563. * It's not in SCHED_CHAN_WAITING_TO_WRITE, so it can't become pending;
  564. * it's either idle and goes to WAITING_FOR_CELLS, or it's a no-op.
  565. */
  566. if (!(chan->scheduler_state == SCHED_CHAN_WAITING_FOR_CELLS ||
  567. chan->scheduler_state == SCHED_CHAN_PENDING)) {
  568. chan->scheduler_state = SCHED_CHAN_WAITING_FOR_CELLS;
  569. log_debug(LD_SCHED,
  570. "Channel " U64_FORMAT " at %p entered waiting_for_cells",
  571. U64_PRINTF_ARG(chan->global_identifier), chan);
  572. }
  573. }
  574. }
  575. #ifdef TOR_UNIT_TESTS
  576. /*
  577. * Notify scheduler that a channel's queue position may have changed.
  578. */
  579. void
  580. scheduler_touch_channel(channel_t *chan)
  581. {
  582. IF_BUG_ONCE(!chan) {
  583. return;
  584. }
  585. if (chan->scheduler_state == SCHED_CHAN_PENDING) {
  586. /* Remove and re-add it */
  587. smartlist_pqueue_remove(channels_pending,
  588. scheduler_compare_channels,
  589. offsetof(channel_t, sched_heap_idx),
  590. chan);
  591. smartlist_pqueue_add(channels_pending,
  592. scheduler_compare_channels,
  593. offsetof(channel_t, sched_heap_idx),
  594. chan);
  595. }
  596. /* else no-op, since it isn't in the queue */
  597. }
  598. #endif /* TOR_UNIT_TESTS */