scheduler.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /* * Copyright (c) 2013-2016, 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. #define TOR_CHANNEL_INTERNAL_ /* For channel_flush_some_cells() */
  9. #include "channel.h"
  10. #include "compat_libevent.h"
  11. #define SCHEDULER_PRIVATE_
  12. #include "scheduler.h"
  13. #include <event2/event.h>
  14. /*
  15. * Scheduler high/low watermarks
  16. */
  17. static uint32_t sched_q_low_water = 16384;
  18. static uint32_t sched_q_high_water = 32768;
  19. /*
  20. * Maximum cells to flush in a single call to channel_flush_some_cells();
  21. * setting this low means more calls, but too high and we could overshoot
  22. * sched_q_high_water.
  23. */
  24. static uint32_t sched_max_flush_cells = 16;
  25. /*
  26. * Write scheduling works by keeping track of which channels can
  27. * accept cells, and have cells to write. From the scheduler's perspective,
  28. * a channel can be in four possible states:
  29. *
  30. * 1.) Not open for writes, no cells to send
  31. * - Not much to do here, and the channel will have scheduler_state ==
  32. * SCHED_CHAN_IDLE
  33. * - Transitions from:
  34. * - Open for writes/has cells by simultaneously draining all circuit
  35. * queues and filling the output buffer.
  36. * - Transitions to:
  37. * - Not open for writes/has cells by arrival of cells on an attached
  38. * circuit (this would be driven from append_cell_to_circuit_queue())
  39. * - Open for writes/no cells by a channel type specific path;
  40. * driven from connection_or_flushed_some() for channel_tls_t.
  41. *
  42. * 2.) Open for writes, no cells to send
  43. * - Not much here either; this will be the state an idle but open channel
  44. * can be expected to settle in. It will have scheduler_state ==
  45. * SCHED_CHAN_WAITING_FOR_CELLS
  46. * - Transitions from:
  47. * - Not open for writes/no cells by flushing some of the output
  48. * buffer.
  49. * - Open for writes/has cells by the scheduler moving cells from
  50. * circuit queues to channel output queue, but not having enough
  51. * to fill the output queue.
  52. * - Transitions to:
  53. * - Open for writes/has cells by arrival of new cells on an attached
  54. * circuit, in append_cell_to_circuit_queue()
  55. *
  56. * 3.) Not open for writes, cells to send
  57. * - This is the state of a busy circuit limited by output bandwidth;
  58. * cells have piled up in the circuit queues waiting to be relayed.
  59. * The channel will have scheduler_state == SCHED_CHAN_WAITING_TO_WRITE.
  60. * - Transitions from:
  61. * - Not open for writes/no cells by arrival of cells on an attached
  62. * circuit
  63. * - Open for writes/has cells by filling an output buffer without
  64. * draining all cells from attached circuits
  65. * - Transitions to:
  66. * - Opens for writes/has cells by draining some of the output buffer
  67. * via the connection_or_flushed_some() path (for channel_tls_t).
  68. *
  69. * 4.) Open for writes, cells to send
  70. * - This connection is ready to relay some cells and waiting for
  71. * the scheduler to choose it. The channel will have scheduler_state ==
  72. * SCHED_CHAN_PENDING.
  73. * - Transitions from:
  74. * - Not open for writes/has cells by the connection_or_flushed_some()
  75. * path
  76. * - Open for writes/no cells by the append_cell_to_circuit_queue()
  77. * path
  78. * - Transitions to:
  79. * - Not open for writes/no cells by draining all circuit queues and
  80. * simultaneously filling the output buffer.
  81. * - Not open for writes/has cells by writing enough cells to fill the
  82. * output buffer
  83. * - Open for writes/no cells by draining all attached circuit queues
  84. * without also filling the output buffer
  85. *
  86. * Other event-driven parts of the code move channels between these scheduling
  87. * states by calling scheduler functions; the scheduler only runs on open-for-
  88. * writes/has-cells channels and is the only path for those to transition to
  89. * other states. The scheduler_run() function gives us the opportunity to do
  90. * scheduling work, and is called from other scheduler functions whenever a
  91. * state transition occurs, and periodically from the main event loop.
  92. */
  93. /* Scheduler global data structures */
  94. /*
  95. * We keep a list of channels that are pending - i.e, have cells to write
  96. * and can accept them to send. The enum scheduler_state in channel_t
  97. * is reserved for our use.
  98. */
  99. /* Pqueue of channels that can write and have cells (pending work) */
  100. STATIC smartlist_t *channels_pending = NULL;
  101. /*
  102. * This event runs the scheduler from its callback, and is manually
  103. * activated whenever a channel enters open for writes/cells to send.
  104. */
  105. STATIC struct event *run_sched_ev = NULL;
  106. /*
  107. * Queue heuristic; this is not the queue size, but an 'effective queuesize'
  108. * that ages out contributions from stalled channels.
  109. */
  110. STATIC uint64_t queue_heuristic = 0;
  111. /*
  112. * Timestamp for last queue heuristic update
  113. */
  114. STATIC time_t queue_heuristic_timestamp = 0;
  115. /* Scheduler static function declarations */
  116. static void scheduler_evt_callback(evutil_socket_t fd,
  117. short events, void *arg);
  118. static int scheduler_more_work(void);
  119. static void scheduler_retrigger(void);
  120. #if 0
  121. static void scheduler_trigger(void);
  122. #endif
  123. /* Scheduler function implementations */
  124. /** Free everything and shut down the scheduling system */
  125. void
  126. scheduler_free_all(void)
  127. {
  128. log_debug(LD_SCHED, "Shutting down scheduler");
  129. if (run_sched_ev) {
  130. if (event_del(run_sched_ev) < 0) {
  131. log_warn(LD_BUG, "Problem deleting run_sched_ev");
  132. }
  133. tor_event_free(run_sched_ev);
  134. run_sched_ev = NULL;
  135. }
  136. if (channels_pending) {
  137. smartlist_free(channels_pending);
  138. channels_pending = NULL;
  139. }
  140. }
  141. /**
  142. * Comparison function to use when sorting pending channels
  143. */
  144. MOCK_IMPL(STATIC int,
  145. scheduler_compare_channels, (const void *c1_v, const void *c2_v))
  146. {
  147. channel_t *c1 = NULL, *c2 = NULL;
  148. /* These are a workaround for -Wbad-function-cast throwing a fit */
  149. const circuitmux_policy_t *p1, *p2;
  150. uintptr_t p1_i, p2_i;
  151. tor_assert(c1_v);
  152. tor_assert(c2_v);
  153. c1 = (channel_t *)(c1_v);
  154. c2 = (channel_t *)(c2_v);
  155. tor_assert(c1);
  156. tor_assert(c2);
  157. if (c1 != c2) {
  158. if (circuitmux_get_policy(c1->cmux) ==
  159. circuitmux_get_policy(c2->cmux)) {
  160. /* Same cmux policy, so use the mux comparison */
  161. return circuitmux_compare_muxes(c1->cmux, c2->cmux);
  162. } else {
  163. /*
  164. * Different policies; not important to get this edge case perfect
  165. * because the current code never actually gives different channels
  166. * different cmux policies anyway. Just use this arbitrary but
  167. * definite choice.
  168. */
  169. p1 = circuitmux_get_policy(c1->cmux);
  170. p2 = circuitmux_get_policy(c2->cmux);
  171. p1_i = (uintptr_t)p1;
  172. p2_i = (uintptr_t)p2;
  173. return (p1_i < p2_i) ? -1 : 1;
  174. }
  175. } else {
  176. /* c1 == c2, so always equal */
  177. return 0;
  178. }
  179. }
  180. /*
  181. * Scheduler event callback; this should get triggered once per event loop
  182. * if any scheduling work was created during the event loop.
  183. */
  184. static void
  185. scheduler_evt_callback(evutil_socket_t fd, short events, void *arg)
  186. {
  187. (void)fd;
  188. (void)events;
  189. (void)arg;
  190. log_debug(LD_SCHED, "Scheduler event callback called");
  191. tor_assert(run_sched_ev);
  192. /* Run the scheduler */
  193. scheduler_run();
  194. /* Do we have more work to do? */
  195. if (scheduler_more_work()) scheduler_retrigger();
  196. }
  197. /** Mark a channel as no longer ready to accept writes */
  198. MOCK_IMPL(void,
  199. scheduler_channel_doesnt_want_writes,(channel_t *chan))
  200. {
  201. tor_assert(chan);
  202. tor_assert(channels_pending);
  203. /* If it's already in pending, we can put it in waiting_to_write */
  204. if (chan->scheduler_state == SCHED_CHAN_PENDING) {
  205. /*
  206. * It's in channels_pending, so it shouldn't be in any of
  207. * the other lists. It can't write any more, so it goes to
  208. * channels_waiting_to_write.
  209. */
  210. smartlist_pqueue_remove(channels_pending,
  211. scheduler_compare_channels,
  212. STRUCT_OFFSET(channel_t, sched_heap_idx),
  213. chan);
  214. chan->scheduler_state = SCHED_CHAN_WAITING_TO_WRITE;
  215. log_debug(LD_SCHED,
  216. "Channel " U64_FORMAT " at %p went from pending "
  217. "to waiting_to_write",
  218. U64_PRINTF_ARG(chan->global_identifier), chan);
  219. } else {
  220. /*
  221. * It's not in pending, so it can't become waiting_to_write; it's
  222. * either not in any of the lists (nothing to do) or it's already in
  223. * waiting_for_cells (remove it, can't write any more).
  224. */
  225. if (chan->scheduler_state == SCHED_CHAN_WAITING_FOR_CELLS) {
  226. chan->scheduler_state = SCHED_CHAN_IDLE;
  227. log_debug(LD_SCHED,
  228. "Channel " U64_FORMAT " at %p left waiting_for_cells",
  229. U64_PRINTF_ARG(chan->global_identifier), chan);
  230. }
  231. }
  232. }
  233. /** Mark a channel as having waiting cells */
  234. MOCK_IMPL(void,
  235. scheduler_channel_has_waiting_cells,(channel_t *chan))
  236. {
  237. int became_pending = 0;
  238. tor_assert(chan);
  239. tor_assert(channels_pending);
  240. /* First, check if this one also writeable */
  241. if (chan->scheduler_state == SCHED_CHAN_WAITING_FOR_CELLS) {
  242. /*
  243. * It's in channels_waiting_for_cells, so it shouldn't be in any of
  244. * the other lists. It has waiting cells now, so it goes to
  245. * channels_pending.
  246. */
  247. chan->scheduler_state = SCHED_CHAN_PENDING;
  248. smartlist_pqueue_add(channels_pending,
  249. scheduler_compare_channels,
  250. STRUCT_OFFSET(channel_t, sched_heap_idx),
  251. chan);
  252. log_debug(LD_SCHED,
  253. "Channel " U64_FORMAT " at %p went from waiting_for_cells "
  254. "to pending",
  255. U64_PRINTF_ARG(chan->global_identifier), chan);
  256. became_pending = 1;
  257. } else {
  258. /*
  259. * It's not in waiting_for_cells, so it can't become pending; it's
  260. * either not in any of the lists (we add it to waiting_to_write)
  261. * or it's already in waiting_to_write or pending (we do nothing)
  262. */
  263. if (!(chan->scheduler_state == SCHED_CHAN_WAITING_TO_WRITE ||
  264. chan->scheduler_state == SCHED_CHAN_PENDING)) {
  265. chan->scheduler_state = SCHED_CHAN_WAITING_TO_WRITE;
  266. log_debug(LD_SCHED,
  267. "Channel " U64_FORMAT " at %p entered waiting_to_write",
  268. U64_PRINTF_ARG(chan->global_identifier), chan);
  269. }
  270. }
  271. /*
  272. * If we made a channel pending, we potentially have scheduling work
  273. * to do.
  274. */
  275. if (became_pending) scheduler_retrigger();
  276. }
  277. /** Set up the scheduling system */
  278. void
  279. scheduler_init(void)
  280. {
  281. log_debug(LD_SCHED, "Initting scheduler");
  282. tor_assert(!run_sched_ev);
  283. run_sched_ev = tor_event_new(tor_libevent_get_base(), -1,
  284. 0, scheduler_evt_callback, NULL);
  285. channels_pending = smartlist_new();
  286. queue_heuristic = 0;
  287. queue_heuristic_timestamp = approx_time();
  288. }
  289. /** Check if there's more scheduling work */
  290. static int
  291. scheduler_more_work(void)
  292. {
  293. tor_assert(channels_pending);
  294. return ((scheduler_get_queue_heuristic() < sched_q_low_water) &&
  295. ((smartlist_len(channels_pending) > 0))) ? 1 : 0;
  296. }
  297. /** Retrigger the scheduler in a way safe to use from the callback */
  298. static void
  299. scheduler_retrigger(void)
  300. {
  301. tor_assert(run_sched_ev);
  302. event_active(run_sched_ev, EV_TIMEOUT, 1);
  303. }
  304. /** Notify the scheduler of a channel being closed */
  305. MOCK_IMPL(void,
  306. scheduler_release_channel,(channel_t *chan))
  307. {
  308. tor_assert(chan);
  309. tor_assert(channels_pending);
  310. if (chan->scheduler_state == SCHED_CHAN_PENDING) {
  311. smartlist_pqueue_remove(channels_pending,
  312. scheduler_compare_channels,
  313. STRUCT_OFFSET(channel_t, sched_heap_idx),
  314. chan);
  315. }
  316. chan->scheduler_state = SCHED_CHAN_IDLE;
  317. }
  318. /** Run the scheduling algorithm if necessary */
  319. MOCK_IMPL(void,
  320. scheduler_run, (void))
  321. {
  322. int n_cells, n_chans_before, n_chans_after;
  323. uint64_t q_len_before, q_heur_before, q_len_after, q_heur_after;
  324. ssize_t flushed, flushed_this_time;
  325. smartlist_t *to_readd = NULL;
  326. channel_t *chan = NULL;
  327. log_debug(LD_SCHED, "We have a chance to run the scheduler");
  328. if (scheduler_get_queue_heuristic() < sched_q_low_water) {
  329. n_chans_before = smartlist_len(channels_pending);
  330. q_len_before = channel_get_global_queue_estimate();
  331. q_heur_before = scheduler_get_queue_heuristic();
  332. while (scheduler_get_queue_heuristic() <= sched_q_high_water &&
  333. smartlist_len(channels_pending) > 0) {
  334. /* Pop off a channel */
  335. chan = smartlist_pqueue_pop(channels_pending,
  336. scheduler_compare_channels,
  337. STRUCT_OFFSET(channel_t, sched_heap_idx));
  338. tor_assert(chan);
  339. /* Figure out how many cells we can write */
  340. n_cells = channel_num_cells_writeable(chan);
  341. if (n_cells > 0) {
  342. log_debug(LD_SCHED,
  343. "Scheduler saw pending channel " U64_FORMAT " at %p with "
  344. "%d cells writeable",
  345. U64_PRINTF_ARG(chan->global_identifier), chan, n_cells);
  346. flushed = 0;
  347. while (flushed < n_cells &&
  348. scheduler_get_queue_heuristic() <= sched_q_high_water) {
  349. flushed_this_time =
  350. channel_flush_some_cells(chan,
  351. MIN(sched_max_flush_cells,
  352. (size_t) n_cells - flushed));
  353. if (flushed_this_time <= 0) break;
  354. flushed += flushed_this_time;
  355. }
  356. if (flushed < n_cells) {
  357. /* We ran out of cells to flush */
  358. chan->scheduler_state = SCHED_CHAN_WAITING_FOR_CELLS;
  359. log_debug(LD_SCHED,
  360. "Channel " U64_FORMAT " at %p "
  361. "entered waiting_for_cells from pending",
  362. U64_PRINTF_ARG(chan->global_identifier),
  363. chan);
  364. } else {
  365. /* The channel may still have some cells */
  366. if (channel_more_to_flush(chan)) {
  367. /* The channel goes to either pending or waiting_to_write */
  368. if (channel_num_cells_writeable(chan) > 0) {
  369. /* Add it back to pending later */
  370. if (!to_readd) to_readd = smartlist_new();
  371. smartlist_add(to_readd, chan);
  372. log_debug(LD_SCHED,
  373. "Channel " U64_FORMAT " at %p "
  374. "is still pending",
  375. U64_PRINTF_ARG(chan->global_identifier),
  376. chan);
  377. } else {
  378. /* It's waiting to be able to write more */
  379. chan->scheduler_state = SCHED_CHAN_WAITING_TO_WRITE;
  380. log_debug(LD_SCHED,
  381. "Channel " U64_FORMAT " at %p "
  382. "entered waiting_to_write from pending",
  383. U64_PRINTF_ARG(chan->global_identifier),
  384. chan);
  385. }
  386. } else {
  387. /* No cells left; it can go to idle or waiting_for_cells */
  388. if (channel_num_cells_writeable(chan) > 0) {
  389. /*
  390. * It can still accept writes, so it goes to
  391. * waiting_for_cells
  392. */
  393. chan->scheduler_state = SCHED_CHAN_WAITING_FOR_CELLS;
  394. log_debug(LD_SCHED,
  395. "Channel " U64_FORMAT " at %p "
  396. "entered waiting_for_cells from pending",
  397. U64_PRINTF_ARG(chan->global_identifier),
  398. chan);
  399. } else {
  400. /*
  401. * We exactly filled up the output queue with all available
  402. * cells; go to idle.
  403. */
  404. chan->scheduler_state = SCHED_CHAN_IDLE;
  405. log_debug(LD_SCHED,
  406. "Channel " U64_FORMAT " at %p "
  407. "become idle from pending",
  408. U64_PRINTF_ARG(chan->global_identifier),
  409. chan);
  410. }
  411. }
  412. }
  413. log_debug(LD_SCHED,
  414. "Scheduler flushed %d cells onto pending channel "
  415. U64_FORMAT " at %p",
  416. (int)flushed, U64_PRINTF_ARG(chan->global_identifier),
  417. chan);
  418. } else {
  419. log_info(LD_SCHED,
  420. "Scheduler saw pending channel " U64_FORMAT " at %p with "
  421. "no cells writeable",
  422. U64_PRINTF_ARG(chan->global_identifier), chan);
  423. /* Put it back to WAITING_TO_WRITE */
  424. chan->scheduler_state = SCHED_CHAN_WAITING_TO_WRITE;
  425. }
  426. }
  427. /* Readd any channels we need to */
  428. if (to_readd) {
  429. SMARTLIST_FOREACH_BEGIN(to_readd, channel_t *, chan) {
  430. chan->scheduler_state = SCHED_CHAN_PENDING;
  431. smartlist_pqueue_add(channels_pending,
  432. scheduler_compare_channels,
  433. STRUCT_OFFSET(channel_t, sched_heap_idx),
  434. chan);
  435. } SMARTLIST_FOREACH_END(chan);
  436. smartlist_free(to_readd);
  437. }
  438. n_chans_after = smartlist_len(channels_pending);
  439. q_len_after = channel_get_global_queue_estimate();
  440. q_heur_after = scheduler_get_queue_heuristic();
  441. log_debug(LD_SCHED,
  442. "Scheduler handled %d of %d pending channels, queue size from "
  443. U64_FORMAT " to " U64_FORMAT ", queue heuristic from "
  444. U64_FORMAT " to " U64_FORMAT,
  445. n_chans_before - n_chans_after, n_chans_before,
  446. U64_PRINTF_ARG(q_len_before), U64_PRINTF_ARG(q_len_after),
  447. U64_PRINTF_ARG(q_heur_before), U64_PRINTF_ARG(q_heur_after));
  448. }
  449. }
  450. /** Trigger the scheduling event so we run the scheduler later */
  451. #if 0
  452. static void
  453. scheduler_trigger(void)
  454. {
  455. log_debug(LD_SCHED, "Triggering scheduler event");
  456. tor_assert(run_sched_ev);
  457. event_add(run_sched_ev, EV_TIMEOUT, 1);
  458. }
  459. #endif
  460. /** Mark a channel as ready to accept writes */
  461. void
  462. scheduler_channel_wants_writes(channel_t *chan)
  463. {
  464. int became_pending = 0;
  465. tor_assert(chan);
  466. tor_assert(channels_pending);
  467. /* If it's already in waiting_to_write, we can put it in pending */
  468. if (chan->scheduler_state == SCHED_CHAN_WAITING_TO_WRITE) {
  469. /*
  470. * It can write now, so it goes to channels_pending.
  471. */
  472. smartlist_pqueue_add(channels_pending,
  473. scheduler_compare_channels,
  474. STRUCT_OFFSET(channel_t, sched_heap_idx),
  475. chan);
  476. chan->scheduler_state = SCHED_CHAN_PENDING;
  477. log_debug(LD_SCHED,
  478. "Channel " U64_FORMAT " at %p went from waiting_to_write "
  479. "to pending",
  480. U64_PRINTF_ARG(chan->global_identifier), chan);
  481. became_pending = 1;
  482. } else {
  483. /*
  484. * It's not in SCHED_CHAN_WAITING_TO_WRITE, so it can't become pending;
  485. * it's either idle and goes to WAITING_FOR_CELLS, or it's a no-op.
  486. */
  487. if (!(chan->scheduler_state == SCHED_CHAN_WAITING_FOR_CELLS ||
  488. chan->scheduler_state == SCHED_CHAN_PENDING)) {
  489. chan->scheduler_state = SCHED_CHAN_WAITING_FOR_CELLS;
  490. log_debug(LD_SCHED,
  491. "Channel " U64_FORMAT " at %p entered waiting_for_cells",
  492. U64_PRINTF_ARG(chan->global_identifier), chan);
  493. }
  494. }
  495. /*
  496. * If we made a channel pending, we potentially have scheduling work
  497. * to do.
  498. */
  499. if (became_pending) scheduler_retrigger();
  500. }
  501. /**
  502. * Notify the scheduler that a channel's position in the pqueue may have
  503. * changed
  504. */
  505. void
  506. scheduler_touch_channel(channel_t *chan)
  507. {
  508. tor_assert(chan);
  509. if (chan->scheduler_state == SCHED_CHAN_PENDING) {
  510. /* Remove and re-add it */
  511. smartlist_pqueue_remove(channels_pending,
  512. scheduler_compare_channels,
  513. STRUCT_OFFSET(channel_t, sched_heap_idx),
  514. chan);
  515. smartlist_pqueue_add(channels_pending,
  516. scheduler_compare_channels,
  517. STRUCT_OFFSET(channel_t, sched_heap_idx),
  518. chan);
  519. }
  520. /* else no-op, since it isn't in the queue */
  521. }
  522. /**
  523. * Notify the scheduler of a queue size adjustment, to recalculate the
  524. * queue heuristic.
  525. */
  526. void
  527. scheduler_adjust_queue_size(channel_t *chan, int dir, uint64_t adj)
  528. {
  529. time_t now = approx_time();
  530. log_debug(LD_SCHED,
  531. "Queue size adjustment by %s" U64_FORMAT " for channel "
  532. U64_FORMAT,
  533. (dir >= 0) ? "+" : "-",
  534. U64_PRINTF_ARG(adj),
  535. U64_PRINTF_ARG(chan->global_identifier));
  536. /* Get the queue heuristic up to date */
  537. scheduler_update_queue_heuristic(now);
  538. /* Adjust as appropriate */
  539. if (dir >= 0) {
  540. /* Increasing it */
  541. queue_heuristic += adj;
  542. } else {
  543. /* Decreasing it */
  544. if (queue_heuristic > adj) queue_heuristic -= adj;
  545. else queue_heuristic = 0;
  546. }
  547. log_debug(LD_SCHED,
  548. "Queue heuristic is now " U64_FORMAT,
  549. U64_PRINTF_ARG(queue_heuristic));
  550. }
  551. /**
  552. * Query the current value of the queue heuristic
  553. */
  554. STATIC uint64_t
  555. scheduler_get_queue_heuristic(void)
  556. {
  557. time_t now = approx_time();
  558. scheduler_update_queue_heuristic(now);
  559. return queue_heuristic;
  560. }
  561. /**
  562. * Adjust the queue heuristic value to the present time
  563. */
  564. STATIC void
  565. scheduler_update_queue_heuristic(time_t now)
  566. {
  567. time_t diff;
  568. if (queue_heuristic_timestamp == 0) {
  569. /*
  570. * Nothing we can sensibly do; must not have been initted properly.
  571. * Oh well.
  572. */
  573. queue_heuristic_timestamp = now;
  574. } else if (queue_heuristic_timestamp < now) {
  575. diff = now - queue_heuristic_timestamp;
  576. /*
  577. * This is a simple exponential age-out; the other proposed alternative
  578. * was a linear age-out using the bandwidth history in rephist.c; I'm
  579. * going with this out of concern that if an adversary can jam the
  580. * scheduler long enough, it would cause the bandwidth to drop to
  581. * zero and render the aging mechanism ineffective thereafter.
  582. */
  583. if (0 <= diff && diff < 64) queue_heuristic >>= diff;
  584. else queue_heuristic = 0;
  585. queue_heuristic_timestamp = now;
  586. log_debug(LD_SCHED,
  587. "Queue heuristic is now " U64_FORMAT,
  588. U64_PRINTF_ARG(queue_heuristic));
  589. }
  590. /* else no update needed, or time went backward */
  591. }
  592. /**
  593. * Set scheduler watermarks and flush size
  594. */
  595. void
  596. scheduler_set_watermarks(uint32_t lo, uint32_t hi, uint32_t max_flush)
  597. {
  598. /* Sanity assertions - caller should ensure these are true */
  599. tor_assert(lo > 0);
  600. tor_assert(hi > lo);
  601. tor_assert(max_flush > 0);
  602. sched_q_low_water = lo;
  603. sched_q_high_water = hi;
  604. sched_max_flush_cells = max_flush;
  605. }