scheduler.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. #include "scheduler.h"
  12. #ifdef HAVE_EVENT2_EVENT_H
  13. #include <event2/event.h>
  14. #else
  15. #include <event.h>
  16. #endif
  17. #define SCHED_Q_LOW_WATER 16384
  18. #define SCHED_Q_HIGH_WATER (2 * SCHED_Q_LOW_WATER)
  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. #define 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 int scheduler_compare_channels(const void *c1_v, const void *c2_v);
  117. static void scheduler_evt_callback(evutil_socket_t fd,
  118. short events, void *arg);
  119. static int scheduler_more_work(void);
  120. static void scheduler_retrigger(void);
  121. #if 0
  122. static void scheduler_trigger(void);
  123. #endif
  124. static uint64_t scheduler_get_queue_heuristic(void);
  125. static void scheduler_update_queue_heuristic(time_t now);
  126. /* Scheduler function implementations */
  127. /** Free everything and shut down the scheduling system */
  128. void
  129. scheduler_free_all(void)
  130. {
  131. log_debug(LD_SCHED, "Shutting down scheduler");
  132. if (run_sched_ev) {
  133. event_del(run_sched_ev);
  134. tor_event_free(run_sched_ev);
  135. run_sched_ev = NULL;
  136. }
  137. if (channels_pending) {
  138. smartlist_free(channels_pending);
  139. channels_pending = NULL;
  140. }
  141. }
  142. /**
  143. * Comparison function to use when sorting pending channels
  144. */
  145. static int
  146. scheduler_compare_channels(const void *c1_v, const void *c2_v)
  147. {
  148. channel_t *c1 = NULL, *c2 = NULL;
  149. /* These are a workaround for -Wbad-function-cast throwing a fit */
  150. const circuitmux_policy_t *p1, *p2;
  151. uintptr_t p1_i, p2_i;
  152. tor_assert(c1_v);
  153. tor_assert(c2_v);
  154. c1 = (channel_t *)(c1_v);
  155. c2 = (channel_t *)(c2_v);
  156. tor_assert(c1);
  157. tor_assert(c2);
  158. if (c1 != c2) {
  159. if (circuitmux_get_policy(c1->cmux) ==
  160. circuitmux_get_policy(c2->cmux)) {
  161. /* Same cmux policy, so use the mux comparison */
  162. return circuitmux_compare_muxes(c1->cmux, c2->cmux);
  163. } else {
  164. /*
  165. * Different policies; not important to get this edge case perfect
  166. * because the current code never actually gives different channels
  167. * different cmux policies anyway. Just use this arbitrary but
  168. * definite choice.
  169. */
  170. p1 = circuitmux_get_policy(c1->cmux);
  171. p2 = circuitmux_get_policy(c2->cmux);
  172. p1_i = (uintptr_t)p1;
  173. p2_i = (uintptr_t)p2;
  174. return (p1_i < p2_i) ? -1 : 1;
  175. }
  176. } else {
  177. /* c1 == c2, so always equal */
  178. return 0;
  179. }
  180. }
  181. /*
  182. * Scheduler event callback; this should get triggered once per event loop
  183. * if any scheduling work was created during the event loop.
  184. */
  185. static void
  186. scheduler_evt_callback(evutil_socket_t fd, short events, void *arg)
  187. {
  188. (void)fd;
  189. (void)events;
  190. (void)arg;
  191. log_debug(LD_SCHED, "Scheduler event callback called");
  192. tor_assert(run_sched_ev);
  193. /* Run the scheduler */
  194. scheduler_run();
  195. /* Do we have more work to do? */
  196. if (scheduler_more_work()) scheduler_retrigger();
  197. }
  198. /** Mark a channel as no longer ready to accept writes */
  199. MOCK_IMPL(void,
  200. scheduler_channel_doesnt_want_writes,(channel_t *chan))
  201. {
  202. tor_assert(chan);
  203. tor_assert(channels_pending);
  204. /* If it's already in pending, we can put it in waiting_to_write */
  205. if (chan->scheduler_state == SCHED_CHAN_PENDING) {
  206. /*
  207. * It's in channels_pending, so it shouldn't be in any of
  208. * the other lists. It can't write any more, so it goes to
  209. * channels_waiting_to_write.
  210. */
  211. smartlist_pqueue_remove(channels_pending,
  212. scheduler_compare_channels,
  213. STRUCT_OFFSET(channel_t, sched_heap_idx),
  214. chan);
  215. chan->scheduler_state = SCHED_CHAN_WAITING_TO_WRITE;
  216. log_debug(LD_SCHED,
  217. "Channel " U64_FORMAT " at %p went from pending "
  218. "to waiting_to_write",
  219. U64_PRINTF_ARG(chan->global_identifier), chan);
  220. } else {
  221. /*
  222. * It's not in pending, so it can't become waiting_to_write; it's
  223. * either not in any of the lists (nothing to do) or it's already in
  224. * waiting_for_cells (remove it, can't write any more).
  225. */
  226. if (chan->scheduler_state == SCHED_CHAN_WAITING_FOR_CELLS) {
  227. chan->scheduler_state = SCHED_CHAN_IDLE;
  228. log_debug(LD_SCHED,
  229. "Channel " U64_FORMAT " at %p left waiting_for_cells",
  230. U64_PRINTF_ARG(chan->global_identifier), chan);
  231. }
  232. }
  233. }
  234. /** Mark a channel as having waiting cells */
  235. MOCK_IMPL(void,
  236. scheduler_channel_has_waiting_cells,(channel_t *chan))
  237. {
  238. int became_pending = 0;
  239. tor_assert(chan);
  240. tor_assert(channels_pending);
  241. /* First, check if this one also writeable */
  242. if (chan->scheduler_state == SCHED_CHAN_WAITING_FOR_CELLS) {
  243. /*
  244. * It's in channels_waiting_for_cells, so it shouldn't be in any of
  245. * the other lists. It has waiting cells now, so it goes to
  246. * channels_pending.
  247. */
  248. chan->scheduler_state = SCHED_CHAN_PENDING;
  249. smartlist_pqueue_add(channels_pending,
  250. scheduler_compare_channels,
  251. STRUCT_OFFSET(channel_t, sched_heap_idx),
  252. chan);
  253. log_debug(LD_SCHED,
  254. "Channel " U64_FORMAT " at %p went from waiting_for_cells "
  255. "to pending",
  256. U64_PRINTF_ARG(chan->global_identifier), chan);
  257. became_pending = 1;
  258. } else {
  259. /*
  260. * It's not in waiting_for_cells, so it can't become pending; it's
  261. * either not in any of the lists (we add it to waiting_to_write)
  262. * or it's already in waiting_to_write or pending (we do nothing)
  263. */
  264. if (!(chan->scheduler_state == SCHED_CHAN_WAITING_TO_WRITE ||
  265. chan->scheduler_state == SCHED_CHAN_PENDING)) {
  266. chan->scheduler_state = SCHED_CHAN_WAITING_TO_WRITE;
  267. log_debug(LD_SCHED,
  268. "Channel " U64_FORMAT " at %p entered waiting_to_write",
  269. U64_PRINTF_ARG(chan->global_identifier), chan);
  270. }
  271. }
  272. /*
  273. * If we made a channel pending, we potentially have scheduling work
  274. * to do.
  275. */
  276. if (became_pending) scheduler_retrigger();
  277. }
  278. /** Set up the scheduling system */
  279. void
  280. scheduler_init(void)
  281. {
  282. log_debug(LD_SCHED, "Initting scheduler");
  283. tor_assert(!run_sched_ev);
  284. run_sched_ev = tor_event_new(tor_libevent_get_base(), -1,
  285. 0, scheduler_evt_callback, NULL);
  286. channels_pending = smartlist_new();
  287. queue_heuristic = 0;
  288. queue_heuristic_timestamp = approx_time();
  289. }
  290. /** Check if there's more scheduling work */
  291. static int
  292. scheduler_more_work(void)
  293. {
  294. tor_assert(channels_pending);
  295. return ((scheduler_get_queue_heuristic() < SCHED_Q_LOW_WATER) &&
  296. ((smartlist_len(channels_pending) > 0))) ? 1 : 0;
  297. }
  298. /** Retrigger the scheduler in a way safe to use from the callback */
  299. static void
  300. scheduler_retrigger(void)
  301. {
  302. tor_assert(run_sched_ev);
  303. event_active(run_sched_ev, EV_TIMEOUT, 1);
  304. }
  305. /** Notify the scheduler of a channel being closed */
  306. MOCK_IMPL(void,
  307. scheduler_release_channel,(channel_t *chan))
  308. {
  309. tor_assert(chan);
  310. tor_assert(channels_pending);
  311. if (chan->scheduler_state == SCHED_CHAN_PENDING) {
  312. smartlist_pqueue_remove(channels_pending,
  313. scheduler_compare_channels,
  314. STRUCT_OFFSET(channel_t, sched_heap_idx),
  315. chan);
  316. }
  317. chan->scheduler_state = SCHED_CHAN_IDLE;
  318. }
  319. /** Run the scheduling algorithm if necessary */
  320. void
  321. scheduler_run(void)
  322. {
  323. int n_cells, n_chans_before, n_chans_after;
  324. uint64_t q_len_before, q_heur_before, q_len_after, q_heur_after;
  325. ssize_t flushed, flushed_this_time;
  326. smartlist_t *to_readd = NULL;
  327. channel_t *chan = NULL;
  328. log_debug(LD_SCHED, "We have a chance to run the scheduler");
  329. if (scheduler_get_queue_heuristic() < SCHED_Q_LOW_WATER) {
  330. n_chans_before = smartlist_len(channels_pending);
  331. q_len_before = channel_get_global_queue_estimate();
  332. q_heur_before = scheduler_get_queue_heuristic();
  333. while (scheduler_get_queue_heuristic() <= SCHED_Q_HIGH_WATER &&
  334. smartlist_len(channels_pending) > 0) {
  335. /* Pop off a channel */
  336. chan = smartlist_pqueue_pop(channels_pending,
  337. scheduler_compare_channels,
  338. STRUCT_OFFSET(channel_t, sched_heap_idx));
  339. tor_assert(chan);
  340. /* Figure out how many cells we can write */
  341. n_cells = channel_num_cells_writeable(chan);
  342. if (n_cells > 0) {
  343. log_debug(LD_SCHED,
  344. "Scheduler saw pending channel " U64_FORMAT " at %p with "
  345. "%d cells writeable",
  346. U64_PRINTF_ARG(chan->global_identifier), chan, n_cells);
  347. flushed = 0;
  348. while (flushed < n_cells &&
  349. scheduler_get_queue_heuristic() <= SCHED_Q_HIGH_WATER) {
  350. flushed_this_time =
  351. channel_flush_some_cells(chan,
  352. MIN(SCHED_MAX_FLUSH_CELLS,
  353. n_cells - flushed));
  354. if (flushed_this_time <= 0) break;
  355. flushed += flushed_this_time;
  356. }
  357. if (flushed < n_cells) {
  358. /* We ran out of cells to flush */
  359. chan->scheduler_state = SCHED_CHAN_WAITING_FOR_CELLS;
  360. log_debug(LD_SCHED,
  361. "Channel " U64_FORMAT " at %p "
  362. "entered waiting_for_cells from pending",
  363. U64_PRINTF_ARG(chan->global_identifier),
  364. chan);
  365. } else {
  366. /* The channel may still have some cells */
  367. if (channel_more_to_flush(chan)) {
  368. /* The channel goes to either pending or waiting_to_write */
  369. if (channel_num_cells_writeable(chan) > 0) {
  370. /* Add it back to pending later */
  371. if (!to_readd) to_readd = smartlist_new();
  372. smartlist_add(to_readd, chan);
  373. log_debug(LD_SCHED,
  374. "Channel " U64_FORMAT " at %p "
  375. "is still pending",
  376. U64_PRINTF_ARG(chan->global_identifier),
  377. chan);
  378. } else {
  379. /* It's waiting to be able to write more */
  380. chan->scheduler_state = SCHED_CHAN_WAITING_TO_WRITE;
  381. log_debug(LD_SCHED,
  382. "Channel " U64_FORMAT " at %p "
  383. "entered waiting_to_write from pending",
  384. U64_PRINTF_ARG(chan->global_identifier),
  385. chan);
  386. }
  387. } else {
  388. /* No cells left; it can go to idle or waiting_for_cells */
  389. if (channel_num_cells_writeable(chan) > 0) {
  390. /*
  391. * It can still accept writes, so it goes to
  392. * waiting_for_cells
  393. */
  394. chan->scheduler_state = SCHED_CHAN_WAITING_FOR_CELLS;
  395. log_debug(LD_SCHED,
  396. "Channel " U64_FORMAT " at %p "
  397. "entered waiting_for_cells from pending",
  398. U64_PRINTF_ARG(chan->global_identifier),
  399. chan);
  400. } else {
  401. /*
  402. * We exactly filled up the output queue with all available
  403. * cells; go to idle.
  404. */
  405. chan->scheduler_state = SCHED_CHAN_IDLE;
  406. log_debug(LD_SCHED,
  407. "Channel " U64_FORMAT " at %p "
  408. "become idle from pending",
  409. U64_PRINTF_ARG(chan->global_identifier),
  410. chan);
  411. }
  412. }
  413. }
  414. log_debug(LD_SCHED,
  415. "Scheduler flushed %d cells onto pending channel "
  416. U64_FORMAT " at %p",
  417. (int)flushed, U64_PRINTF_ARG(chan->global_identifier),
  418. chan);
  419. } else {
  420. log_info(LD_SCHED,
  421. "Scheduler saw pending channel " U64_FORMAT " at %p with "
  422. "no cells writeable",
  423. U64_PRINTF_ARG(chan->global_identifier), chan);
  424. /* Put it back to WAITING_TO_WRITE */
  425. chan->scheduler_state = SCHED_CHAN_WAITING_TO_WRITE;
  426. }
  427. }
  428. /* Readd any channels we need to */
  429. if (to_readd) {
  430. SMARTLIST_FOREACH_BEGIN(to_readd, channel_t *, chan) {
  431. chan->scheduler_state = SCHED_CHAN_PENDING;
  432. smartlist_pqueue_add(channels_pending,
  433. scheduler_compare_channels,
  434. STRUCT_OFFSET(channel_t, sched_heap_idx),
  435. chan);
  436. } SMARTLIST_FOREACH_END(chan);
  437. smartlist_free(to_readd);
  438. }
  439. n_chans_after = smartlist_len(channels_pending);
  440. q_len_after = channel_get_global_queue_estimate();
  441. q_heur_after = scheduler_get_queue_heuristic();
  442. log_debug(LD_SCHED,
  443. "Scheduler handled %d of %d pending channels, queue size from "
  444. U64_FORMAT " to " U64_FORMAT ", queue heuristic from "
  445. U64_FORMAT " to " U64_FORMAT,
  446. n_chans_before - n_chans_after, n_chans_before,
  447. U64_PRINTF_ARG(q_len_before), U64_PRINTF_ARG(q_len_after),
  448. U64_PRINTF_ARG(q_heur_before), U64_PRINTF_ARG(q_heur_after));
  449. }
  450. }
  451. /** Trigger the scheduling event so we run the scheduler later */
  452. #if 0
  453. static void
  454. scheduler_trigger(void)
  455. {
  456. log_debug(LD_SCHED, "Triggering scheduler event");
  457. tor_assert(run_sched_ev);
  458. event_add(run_sched_ev, EV_TIMEOUT, 1);
  459. }
  460. #endif
  461. /** Mark a channel as ready to accept writes */
  462. void
  463. scheduler_channel_wants_writes(channel_t *chan)
  464. {
  465. int became_pending = 0;
  466. tor_assert(chan);
  467. tor_assert(channels_pending);
  468. /* If it's already in waiting_to_write, we can put it in pending */
  469. if (chan->scheduler_state == SCHED_CHAN_WAITING_TO_WRITE) {
  470. /*
  471. * It can write now, so it goes to channels_pending.
  472. */
  473. smartlist_pqueue_add(channels_pending,
  474. scheduler_compare_channels,
  475. STRUCT_OFFSET(channel_t, sched_heap_idx),
  476. chan);
  477. chan->scheduler_state = SCHED_CHAN_PENDING;
  478. log_debug(LD_SCHED,
  479. "Channel " U64_FORMAT " at %p went from waiting_to_write "
  480. "to pending",
  481. U64_PRINTF_ARG(chan->global_identifier), chan);
  482. became_pending = 1;
  483. } else {
  484. /*
  485. * It's not in SCHED_CHAN_WAITING_TO_WRITE, so it can't become pending;
  486. * it's either idle and goes to WAITING_FOR_CELLS, or it's a no-op.
  487. */
  488. if (!(chan->scheduler_state == SCHED_CHAN_WAITING_FOR_CELLS ||
  489. chan->scheduler_state == SCHED_CHAN_PENDING)) {
  490. chan->scheduler_state = SCHED_CHAN_WAITING_FOR_CELLS;
  491. log_debug(LD_SCHED,
  492. "Channel " U64_FORMAT " at %p entered waiting_for_cells",
  493. U64_PRINTF_ARG(chan->global_identifier), chan);
  494. }
  495. }
  496. /*
  497. * If we made a channel pending, we potentially have scheduling work
  498. * to do.
  499. */
  500. if (became_pending) scheduler_retrigger();
  501. }
  502. /**
  503. * Notify the scheduler that a channel's position in the pqueue may have
  504. * changed
  505. */
  506. void
  507. scheduler_touch_channel(channel_t *chan)
  508. {
  509. tor_assert(chan);
  510. if (chan->scheduler_state == SCHED_CHAN_PENDING) {
  511. /* Remove and re-add it */
  512. smartlist_pqueue_remove(channels_pending,
  513. scheduler_compare_channels,
  514. STRUCT_OFFSET(channel_t, sched_heap_idx),
  515. chan);
  516. smartlist_pqueue_add(channels_pending,
  517. scheduler_compare_channels,
  518. STRUCT_OFFSET(channel_t, sched_heap_idx),
  519. chan);
  520. }
  521. /* else no-op, since it isn't in the queue */
  522. }
  523. /**
  524. * Notify the scheduler of a queue size adjustment, to recalculate the
  525. * queue heuristic.
  526. */
  527. void
  528. scheduler_adjust_queue_size(channel_t *chan, char dir, uint64_t adj)
  529. {
  530. time_t now = approx_time();
  531. log_debug(LD_SCHED,
  532. "Queue size adjustment by %s" U64_FORMAT " for channel "
  533. U64_FORMAT,
  534. (dir >= 0) ? "+" : "-",
  535. U64_PRINTF_ARG(adj),
  536. U64_PRINTF_ARG(chan->global_identifier));
  537. /* Get the queue heuristic up to date */
  538. scheduler_update_queue_heuristic(now);
  539. /* Adjust as appropriate */
  540. if (dir >= 0) {
  541. /* Increasing it */
  542. queue_heuristic += adj;
  543. } else {
  544. /* Decreasing it */
  545. if (queue_heuristic > adj) queue_heuristic -= adj;
  546. else queue_heuristic = 0;
  547. }
  548. log_debug(LD_SCHED,
  549. "Queue heuristic is now " U64_FORMAT,
  550. U64_PRINTF_ARG(queue_heuristic));
  551. }
  552. /**
  553. * Query the current value of the queue heuristic
  554. */
  555. static uint64_t
  556. scheduler_get_queue_heuristic(void)
  557. {
  558. time_t now = approx_time();
  559. scheduler_update_queue_heuristic(now);
  560. return queue_heuristic;
  561. }
  562. /**
  563. * Adjust the queue heuristic value to the present time
  564. */
  565. static void
  566. scheduler_update_queue_heuristic(time_t now)
  567. {
  568. time_t diff;
  569. if (queue_heuristic_timestamp == 0) {
  570. /*
  571. * Nothing we can sensibly do; must not have been initted properly.
  572. * Oh well.
  573. */
  574. queue_heuristic_timestamp = now;
  575. } else if (queue_heuristic_timestamp < now) {
  576. diff = now - queue_heuristic_timestamp;
  577. /*
  578. * This is a simple exponential age-out; the other proposed alternative
  579. * was a linear age-out using the bandwidth history in rephist.c; I'm
  580. * going with this out of concern that if an adversary can jam the
  581. * scheduler long enough, it would cause the bandwidth to drop to
  582. * zero and render the aging mechanism ineffective thereafter.
  583. */
  584. if (0 <= diff && diff < 64) queue_heuristic >>= diff;
  585. else queue_heuristic = 0;
  586. queue_heuristic_timestamp = now;
  587. log_debug(LD_SCHED,
  588. "Queue heuristic is now " U64_FORMAT,
  589. U64_PRINTF_ARG(queue_heuristic));
  590. }
  591. /* else no update needed, or time went backward */
  592. }