scheduler.c 22 KB

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