circuitmux_ewma.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /* * Copyright (c) 2012, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file circuitmux_ewma.c
  5. * \brief EWMA circuit selection as a circuitmux_t policy
  6. **/
  7. #define TOR_CIRCUITMUX_EWMA_C_
  8. #include <math.h>
  9. #include "or.h"
  10. #include "circuitmux.h"
  11. #include "circuitmux_ewma.h"
  12. #include "networkstatus.h"
  13. /*** EWMA parameter #defines ***/
  14. /** How long does a tick last (seconds)? */
  15. #define EWMA_TICK_LEN 10
  16. /** The default per-tick scale factor, if it hasn't been overridden by a
  17. * consensus or a configuration setting. zero means "disabled". */
  18. #define EWMA_DEFAULT_HALFLIFE 0.0
  19. /*** Some useful constant #defines ***/
  20. /*DOCDOC*/
  21. #define EPSILON 0.00001
  22. /*DOCDOC*/
  23. #define LOG_ONEHALF -0.69314718055994529
  24. /*** EWMA structures ***/
  25. typedef struct cell_ewma_s cell_ewma_t;
  26. typedef struct ewma_policy_data_s ewma_policy_data_t;
  27. typedef struct ewma_policy_circ_data_s ewma_policy_circ_data_t;
  28. /**
  29. * The cell_ewma_t structure keeps track of how many cells a circuit has
  30. * transferred recently. It keeps an EWMA (exponentially weighted moving
  31. * average) of the number of cells flushed from the circuit queue onto a
  32. * connection in channel_flush_from_first_active_circuit().
  33. */
  34. struct cell_ewma_s {
  35. /** The last 'tick' at which we recalibrated cell_count.
  36. *
  37. * A cell sent at exactly the start of this tick has weight 1.0. Cells sent
  38. * since the start of this tick have weight greater than 1.0; ones sent
  39. * earlier have less weight. */
  40. unsigned int last_adjusted_tick;
  41. /** The EWMA of the cell count. */
  42. double cell_count;
  43. /** True iff this is the cell count for a circuit's previous
  44. * channel. */
  45. unsigned int is_for_p_chan : 1;
  46. /** The position of the circuit within the OR connection's priority
  47. * queue. */
  48. int heap_index;
  49. };
  50. struct ewma_policy_data_s {
  51. circuitmux_policy_data_t base_;
  52. /**
  53. * Priority queue of cell_ewma_t for circuits with queued cells waiting
  54. * for room to free up on the channel that owns this circuitmux. Kept
  55. * in heap order according to EWMA. This was formerly in channel_t, and
  56. * in or_connection_t before that.
  57. */
  58. smartlist_t *active_circuit_pqueue;
  59. /**
  60. * The tick on which the cell_ewma_ts in active_circuit_pqueue last had
  61. * their ewma values rescaled. This was formerly in channel_t, and in
  62. * or_connection_t before that.
  63. */
  64. unsigned int active_circuit_pqueue_last_recalibrated;
  65. };
  66. struct ewma_policy_circ_data_s {
  67. circuitmux_policy_circ_data_t base_;
  68. /**
  69. * The EWMA count for the number of cells flushed from this circuit
  70. * onto this circuitmux. Used to determine which circuit to flush
  71. * from next. This was formerly in circuit_t and or_circuit_t.
  72. */
  73. cell_ewma_t cell_ewma;
  74. /**
  75. * Pointer back to the circuit_t this is for; since we're separating
  76. * out circuit selection policy like this, we can't attach cell_ewma_t
  77. * to the circuit_t any more, so we can't use SUBTYPE_P directly to a
  78. * circuit_t like before; instead get it here.
  79. */
  80. circuit_t *circ;
  81. };
  82. #define EWMA_POL_DATA_MAGIC 0x2fd8b16aU
  83. #define EWMA_POL_CIRC_DATA_MAGIC 0x761e7747U
  84. /*** Downcasts for the above types ***/
  85. static ewma_policy_data_t *
  86. TO_EWMA_POL_DATA(circuitmux_policy_data_t *);
  87. static ewma_policy_circ_data_t *
  88. TO_EWMA_POL_CIRC_DATA(circuitmux_policy_circ_data_t *);
  89. /**
  90. * Downcast a circuitmux_policy_data_t to an ewma_policy_data_t and assert
  91. * if the cast is impossible.
  92. */
  93. static INLINE ewma_policy_data_t *
  94. TO_EWMA_POL_DATA(circuitmux_policy_data_t *pol)
  95. {
  96. if (!pol) return NULL;
  97. else {
  98. tor_assert(pol->magic == EWMA_POL_DATA_MAGIC);
  99. return DOWNCAST(ewma_policy_data_t, pol);
  100. }
  101. }
  102. /**
  103. * Downcast a circuitmux_policy_circ_data_t to an ewma_policy_circ_data_t
  104. * and assert if the cast is impossible.
  105. */
  106. static INLINE ewma_policy_circ_data_t *
  107. TO_EWMA_POL_CIRC_DATA(circuitmux_policy_circ_data_t *pol)
  108. {
  109. if (!pol) return NULL;
  110. else {
  111. tor_assert(pol->magic == EWMA_POL_CIRC_DATA_MAGIC);
  112. return DOWNCAST(ewma_policy_circ_data_t, pol);
  113. }
  114. }
  115. /*** Static declarations for circuitmux_ewma.c ***/
  116. static void add_cell_ewma(ewma_policy_data_t *pol, cell_ewma_t *ewma);
  117. static int compare_cell_ewma_counts(const void *p1, const void *p2);
  118. static unsigned cell_ewma_tick_from_timeval(const struct timeval *now,
  119. double *remainder_out);
  120. static circuit_t * cell_ewma_to_circuit(cell_ewma_t *ewma);
  121. static INLINE double get_scale_factor(unsigned from_tick, unsigned to_tick);
  122. static cell_ewma_t * pop_first_cell_ewma(ewma_policy_data_t *pol);
  123. static void remove_cell_ewma(ewma_policy_data_t *pol, cell_ewma_t *ewma);
  124. static void scale_single_cell_ewma(cell_ewma_t *ewma, unsigned cur_tick);
  125. static void scale_active_circuits(ewma_policy_data_t *pol,
  126. unsigned cur_tick);
  127. /*** Circuitmux policy methods ***/
  128. static circuitmux_policy_data_t * ewma_alloc_cmux_data(circuitmux_t *cmux);
  129. static void ewma_free_cmux_data(circuitmux_t *cmux,
  130. circuitmux_policy_data_t *pol_data);
  131. static circuitmux_policy_circ_data_t *
  132. ewma_alloc_circ_data(circuitmux_t *cmux, circuitmux_policy_data_t *pol_data,
  133. circuit_t *circ, cell_direction_t direction,
  134. unsigned int cell_count);
  135. static void
  136. ewma_free_circ_data(circuitmux_t *cmux,
  137. circuitmux_policy_data_t *pol_data,
  138. circuit_t *circ,
  139. circuitmux_policy_circ_data_t *pol_circ_data);
  140. static void
  141. ewma_notify_circ_active(circuitmux_t *cmux,
  142. circuitmux_policy_data_t *pol_data,
  143. circuit_t *circ,
  144. circuitmux_policy_circ_data_t *pol_circ_data);
  145. static void
  146. ewma_notify_circ_inactive(circuitmux_t *cmux,
  147. circuitmux_policy_data_t *pol_data,
  148. circuit_t *circ,
  149. circuitmux_policy_circ_data_t *pol_circ_data);
  150. static void
  151. ewma_notify_xmit_cells(circuitmux_t *cmux,
  152. circuitmux_policy_data_t *pol_data,
  153. circuit_t *circ,
  154. circuitmux_policy_circ_data_t *pol_circ_data,
  155. unsigned int n_cells);
  156. static circuit_t *
  157. ewma_pick_active_circuit(circuitmux_t *cmux,
  158. circuitmux_policy_data_t *pol_data);
  159. /*** EWMA global variables ***/
  160. /** The per-tick scale factor to be used when computing cell-count EWMA
  161. * values. (A cell sent N ticks before the start of the current tick
  162. * has value ewma_scale_factor ** N.)
  163. */
  164. static double ewma_scale_factor = 0.1;
  165. /* DOCDOC ewma_enabled */
  166. static int ewma_enabled = 0;
  167. /*** EWMA circuitmux_policy_t method table ***/
  168. circuitmux_policy_t ewma_policy = {
  169. /*.alloc_cmux_data =*/ ewma_alloc_cmux_data,
  170. /*.free_cmux_data =*/ ewma_free_cmux_data,
  171. /*.alloc_circ_data =*/ ewma_alloc_circ_data,
  172. /*.free_circ_data =*/ ewma_free_circ_data,
  173. /*.notify_circ_active =*/ ewma_notify_circ_active,
  174. /*.notify_circ_inactive =*/ ewma_notify_circ_inactive,
  175. /*.notify_set_n_cells =*/ NULL, /* EWMA doesn't need this */
  176. /*.notify_xmit_cells =*/ ewma_notify_xmit_cells,
  177. /*.pick_active_circuit =*/ ewma_pick_active_circuit
  178. };
  179. /*** EWMA method implementations using the below EWMA helper functions ***/
  180. /**
  181. * Allocate an ewma_policy_data_t and upcast it to a circuitmux_policy_data_t;
  182. * this is called when setting the policy on a circuitmux_t to ewma_policy.
  183. */
  184. static circuitmux_policy_data_t *
  185. ewma_alloc_cmux_data(circuitmux_t *cmux)
  186. {
  187. ewma_policy_data_t *pol = NULL;
  188. tor_assert(cmux);
  189. pol = tor_malloc_zero(sizeof(*pol));
  190. pol->base_.magic = EWMA_POL_DATA_MAGIC;
  191. pol->active_circuit_pqueue = smartlist_new();
  192. pol->active_circuit_pqueue_last_recalibrated = cell_ewma_get_tick();
  193. return TO_CMUX_POL_DATA(pol);
  194. }
  195. /**
  196. * Free an ewma_policy_data_t allocated with ewma_alloc_cmux_data()
  197. */
  198. static void
  199. ewma_free_cmux_data(circuitmux_t *cmux,
  200. circuitmux_policy_data_t *pol_data)
  201. {
  202. ewma_policy_data_t *pol = NULL;
  203. tor_assert(cmux);
  204. if (!pol_data) return;
  205. pol = TO_EWMA_POL_DATA(pol_data);
  206. smartlist_free(pol->active_circuit_pqueue);
  207. tor_free(pol);
  208. }
  209. /**
  210. * Allocate an ewma_policy_circ_data_t and upcast it to a
  211. * circuitmux_policy_data_t; this is called when attaching a circuit to a
  212. * circuitmux_t with ewma_policy.
  213. */
  214. static circuitmux_policy_circ_data_t *
  215. ewma_alloc_circ_data(circuitmux_t *cmux,
  216. circuitmux_policy_data_t *pol_data,
  217. circuit_t *circ,
  218. cell_direction_t direction,
  219. unsigned int cell_count)
  220. {
  221. ewma_policy_circ_data_t *cdata = NULL;
  222. tor_assert(cmux);
  223. tor_assert(pol_data);
  224. tor_assert(circ);
  225. tor_assert(direction == CELL_DIRECTION_OUT ||
  226. direction == CELL_DIRECTION_IN);
  227. /* Shut the compiler up */
  228. tor_assert(cell_count == cell_count);
  229. cdata = tor_malloc_zero(sizeof(*cdata));
  230. cdata->base_.magic = EWMA_POL_CIRC_DATA_MAGIC;
  231. cdata->circ = circ;
  232. /*
  233. * Initialize the cell_ewma_t structure (formerly in
  234. * init_circuit_base())
  235. */
  236. cdata->cell_ewma.last_adjusted_tick = cell_ewma_get_tick();
  237. cdata->cell_ewma.cell_count = 0.0;
  238. cdata->cell_ewma.heap_index = -1;
  239. if (direction == CELL_DIRECTION_IN) {
  240. cdata->cell_ewma.is_for_p_chan = 1;
  241. } else {
  242. cdata->cell_ewma.is_for_p_chan = 0;
  243. }
  244. return TO_CMUX_POL_CIRC_DATA(cdata);
  245. }
  246. /**
  247. * Free an ewma_policy_circ_data_t allocated with ewma_alloc_circ_data()
  248. */
  249. static void
  250. ewma_free_circ_data(circuitmux_t *cmux,
  251. circuitmux_policy_data_t *pol_data,
  252. circuit_t *circ,
  253. circuitmux_policy_circ_data_t *pol_circ_data)
  254. {
  255. ewma_policy_circ_data_t *cdata = NULL;
  256. tor_assert(cmux);
  257. tor_assert(circ);
  258. tor_assert(pol_data);
  259. if (!pol_circ_data) return;
  260. cdata = TO_EWMA_POL_CIRC_DATA(pol_circ_data);
  261. tor_free(cdata);
  262. }
  263. /**
  264. * Handle circuit activation; this inserts the circuit's cell_ewma into
  265. * the active_circuits_pqueue.
  266. */
  267. static void
  268. ewma_notify_circ_active(circuitmux_t *cmux,
  269. circuitmux_policy_data_t *pol_data,
  270. circuit_t *circ,
  271. circuitmux_policy_circ_data_t *pol_circ_data)
  272. {
  273. ewma_policy_data_t *pol = NULL;
  274. ewma_policy_circ_data_t *cdata = NULL;
  275. tor_assert(cmux);
  276. tor_assert(pol_data);
  277. tor_assert(circ);
  278. tor_assert(pol_circ_data);
  279. pol = TO_EWMA_POL_DATA(pol_data);
  280. cdata = TO_EWMA_POL_CIRC_DATA(pol_circ_data);
  281. add_cell_ewma(pol, &(cdata->cell_ewma));
  282. }
  283. /**
  284. * Handle circuit deactivation; this removes the circuit's cell_ewma from
  285. * the active_circuits_pqueue.
  286. */
  287. static void
  288. ewma_notify_circ_inactive(circuitmux_t *cmux,
  289. circuitmux_policy_data_t *pol_data,
  290. circuit_t *circ,
  291. circuitmux_policy_circ_data_t *pol_circ_data)
  292. {
  293. ewma_policy_data_t *pol = NULL;
  294. ewma_policy_circ_data_t *cdata = NULL;
  295. tor_assert(cmux);
  296. tor_assert(pol_data);
  297. tor_assert(circ);
  298. tor_assert(pol_circ_data);
  299. pol = TO_EWMA_POL_DATA(pol_data);
  300. cdata = TO_EWMA_POL_CIRC_DATA(pol_circ_data);
  301. remove_cell_ewma(pol, &(cdata->cell_ewma));
  302. }
  303. /**
  304. * Update cell_ewma for this circuit after we've sent some cells, and
  305. * remove/reinsert it in the queue. This used to be done (brokenly,
  306. * see bug 6816) in channel_flush_from_first_active_circuit().
  307. */
  308. static void
  309. ewma_notify_xmit_cells(circuitmux_t *cmux,
  310. circuitmux_policy_data_t *pol_data,
  311. circuit_t *circ,
  312. circuitmux_policy_circ_data_t *pol_circ_data,
  313. unsigned int n_cells)
  314. {
  315. ewma_policy_data_t *pol = NULL;
  316. ewma_policy_circ_data_t *cdata = NULL;
  317. unsigned int tick;
  318. double fractional_tick, ewma_increment;
  319. /* The current (hi-res) time */
  320. struct timeval now_hires;
  321. cell_ewma_t *cell_ewma, *tmp;
  322. tor_assert(cmux);
  323. tor_assert(pol_data);
  324. tor_assert(circ);
  325. tor_assert(pol_circ_data);
  326. tor_assert(n_cells > 0);
  327. pol = TO_EWMA_POL_DATA(pol_data);
  328. cdata = TO_EWMA_POL_CIRC_DATA(pol_circ_data);
  329. /* Rescale the EWMAs if needed */
  330. tor_gettimeofday_cached(&now_hires);
  331. tick = cell_ewma_tick_from_timeval(&now_hires, &fractional_tick);
  332. if (tick != pol->active_circuit_pqueue_last_recalibrated) {
  333. scale_active_circuits(pol, tick);
  334. }
  335. /* How much do we adjust the cell count in cell_ewma by? */
  336. ewma_increment =
  337. ((double)(n_cells)) * pow(ewma_scale_factor, -fractional_tick);
  338. /* Do the adjustment */
  339. cell_ewma = &(cdata->cell_ewma);
  340. cell_ewma->cell_count += ewma_increment;
  341. /*
  342. * Since we just sent on this circuit, it should be at the head of
  343. * the queue. Pop the head, assert that it matches, then re-add.
  344. */
  345. tmp = pop_first_cell_ewma(pol);
  346. tor_assert(tmp == cell_ewma);
  347. add_cell_ewma(pol, cell_ewma);
  348. }
  349. /**
  350. * Pick the preferred circuit to send from; this will be the one with
  351. * the lowest EWMA value in the priority queue. This used to be done
  352. * in channel_flush_from_first_active_circuit().
  353. */
  354. static circuit_t *
  355. ewma_pick_active_circuit(circuitmux_t *cmux,
  356. circuitmux_policy_data_t *pol_data)
  357. {
  358. ewma_policy_data_t *pol = NULL;
  359. circuit_t *circ = NULL;
  360. cell_ewma_t *cell_ewma = NULL;
  361. tor_assert(cmux);
  362. tor_assert(pol_data);
  363. pol = TO_EWMA_POL_DATA(pol_data);
  364. if (smartlist_len(pol->active_circuit_pqueue) > 0) {
  365. /* Get the head of the queue */
  366. cell_ewma = smartlist_get(pol->active_circuit_pqueue, 0);
  367. circ = cell_ewma_to_circuit(cell_ewma);
  368. }
  369. return circ;
  370. }
  371. /** Helper for sorting cell_ewma_t values in their priority queue. */
  372. static int
  373. compare_cell_ewma_counts(const void *p1, const void *p2)
  374. {
  375. const cell_ewma_t *e1 = p1, *e2 = p2;
  376. if (e1->cell_count < e2->cell_count)
  377. return -1;
  378. else if (e1->cell_count > e2->cell_count)
  379. return 1;
  380. else
  381. return 0;
  382. }
  383. /** Given a cell_ewma_t, return a pointer to the circuit containing it. */
  384. static circuit_t *
  385. cell_ewma_to_circuit(cell_ewma_t *ewma)
  386. {
  387. ewma_policy_circ_data_t *cdata = NULL;
  388. tor_assert(ewma);
  389. cdata = SUBTYPE_P(ewma, ewma_policy_circ_data_t, cell_ewma);
  390. tor_assert(cdata);
  391. return cdata->circ;
  392. }
  393. /* ==== Functions for scaling cell_ewma_t ====
  394. When choosing which cells to relay first, we favor circuits that have been
  395. quiet recently. This gives better latency on connections that aren't
  396. pushing lots of data, and makes the network feel more interactive.
  397. Conceptually, we take an exponentially weighted mean average of the number
  398. of cells a circuit has sent, and allow active circuits (those with cells to
  399. relay) to send cells in reverse order of their exponentially-weighted mean
  400. average (EWMA) cell count. [That is, a cell sent N seconds ago 'counts'
  401. F^N times as much as a cell sent now, for 0<F<1.0, and we favor the
  402. circuit that has sent the fewest cells]
  403. If 'double' had infinite precision, we could do this simply by counting a
  404. cell sent at startup as having weight 1.0, and a cell sent N seconds later
  405. as having weight F^-N. This way, we would never need to re-scale
  406. any already-sent cells.
  407. To prevent double from overflowing, we could count a cell sent now as
  408. having weight 1.0 and a cell sent N seconds ago as having weight F^N.
  409. This, however, would mean we'd need to re-scale *ALL* old circuits every
  410. time we wanted to send a cell.
  411. So as a compromise, we divide time into 'ticks' (currently, 10-second
  412. increments) and say that a cell sent at the start of a current tick is
  413. worth 1.0, a cell sent N seconds before the start of the current tick is
  414. worth F^N, and a cell sent N seconds after the start of the current tick is
  415. worth F^-N. This way we don't overflow, and we don't need to constantly
  416. rescale.
  417. */
  418. /** Given a timeval <b>now</b>, compute the cell_ewma tick in which it occurs
  419. * and the fraction of the tick that has elapsed between the start of the tick
  420. * and <b>now</b>. Return the former and store the latter in
  421. * *<b>remainder_out</b>.
  422. *
  423. * These tick values are not meant to be shared between Tor instances, or used
  424. * for other purposes. */
  425. static unsigned
  426. cell_ewma_tick_from_timeval(const struct timeval *now,
  427. double *remainder_out)
  428. {
  429. unsigned res = (unsigned) (now->tv_sec / EWMA_TICK_LEN);
  430. /* rem */
  431. double rem = (now->tv_sec % EWMA_TICK_LEN) +
  432. ((double)(now->tv_usec)) / 1.0e6;
  433. *remainder_out = rem / EWMA_TICK_LEN;
  434. return res;
  435. }
  436. /** Tell the caller whether ewma_enabled is set */
  437. int
  438. cell_ewma_enabled(void)
  439. {
  440. return ewma_enabled;
  441. }
  442. /** Compute and return the current cell_ewma tick. */
  443. unsigned int
  444. cell_ewma_get_tick(void)
  445. {
  446. return ((unsigned)approx_time() / EWMA_TICK_LEN);
  447. }
  448. /** Adjust the global cell scale factor based on <b>options</b> */
  449. void
  450. cell_ewma_set_scale_factor(const or_options_t *options,
  451. const networkstatus_t *consensus)
  452. {
  453. int32_t halflife_ms;
  454. double halflife;
  455. const char *source;
  456. if (options && options->CircuitPriorityHalflife >= -EPSILON) {
  457. halflife = options->CircuitPriorityHalflife;
  458. source = "CircuitPriorityHalflife in configuration";
  459. } else if (consensus && (halflife_ms = networkstatus_get_param(
  460. consensus, "CircuitPriorityHalflifeMsec",
  461. -1, -1, INT32_MAX)) >= 0) {
  462. halflife = ((double)halflife_ms)/1000.0;
  463. source = "CircuitPriorityHalflifeMsec in consensus";
  464. } else {
  465. halflife = EWMA_DEFAULT_HALFLIFE;
  466. source = "Default value";
  467. }
  468. if (halflife <= EPSILON) {
  469. /* The cell EWMA algorithm is disabled. */
  470. ewma_scale_factor = 0.1;
  471. ewma_enabled = 0;
  472. log_info(LD_OR,
  473. "Disabled cell_ewma algorithm because of value in %s",
  474. source);
  475. } else {
  476. /* convert halflife into halflife-per-tick. */
  477. halflife /= EWMA_TICK_LEN;
  478. /* compute per-tick scale factor. */
  479. ewma_scale_factor = exp( LOG_ONEHALF / halflife );
  480. ewma_enabled = 1;
  481. log_info(LD_OR,
  482. "Enabled cell_ewma algorithm because of value in %s; "
  483. "scale factor is %f per %d seconds",
  484. source, ewma_scale_factor, EWMA_TICK_LEN);
  485. }
  486. }
  487. /** Return the multiplier necessary to convert the value of a cell sent in
  488. * 'from_tick' to one sent in 'to_tick'. */
  489. static INLINE double
  490. get_scale_factor(unsigned from_tick, unsigned to_tick)
  491. {
  492. /* This math can wrap around, but that's okay: unsigned overflow is
  493. well-defined */
  494. int diff = (int)(to_tick - from_tick);
  495. return pow(ewma_scale_factor, diff);
  496. }
  497. /** Adjust the cell count of <b>ewma</b> so that it is scaled with respect to
  498. * <b>cur_tick</b> */
  499. static void
  500. scale_single_cell_ewma(cell_ewma_t *ewma, unsigned cur_tick)
  501. {
  502. double factor = get_scale_factor(ewma->last_adjusted_tick, cur_tick);
  503. ewma->cell_count *= factor;
  504. ewma->last_adjusted_tick = cur_tick;
  505. }
  506. /** Adjust the cell count of every active circuit on <b>chan</b> so
  507. * that they are scaled with respect to <b>cur_tick</b> */
  508. static void
  509. scale_active_circuits(ewma_policy_data_t *pol, unsigned cur_tick)
  510. {
  511. double factor;
  512. tor_assert(pol);
  513. tor_assert(pol->active_circuit_pqueue);
  514. factor =
  515. get_scale_factor(
  516. pol->active_circuit_pqueue_last_recalibrated,
  517. cur_tick);
  518. /** Ordinarily it isn't okay to change the value of an element in a heap,
  519. * but it's okay here, since we are preserving the order. */
  520. SMARTLIST_FOREACH_BEGIN(
  521. pol->active_circuit_pqueue,
  522. cell_ewma_t *, e) {
  523. tor_assert(e->last_adjusted_tick ==
  524. pol->active_circuit_pqueue_last_recalibrated);
  525. e->cell_count *= factor;
  526. e->last_adjusted_tick = cur_tick;
  527. } SMARTLIST_FOREACH_END(e);
  528. pol->active_circuit_pqueue_last_recalibrated = cur_tick;
  529. }
  530. /** Rescale <b>ewma</b> to the same scale as <b>pol</b>, and add it to
  531. * <b>pol</b>'s priority queue of active circuits */
  532. static void
  533. add_cell_ewma(ewma_policy_data_t *pol, cell_ewma_t *ewma)
  534. {
  535. tor_assert(pol);
  536. tor_assert(pol->active_circuit_pqueue);
  537. tor_assert(ewma);
  538. tor_assert(ewma->heap_index == -1);
  539. scale_single_cell_ewma(
  540. ewma,
  541. pol->active_circuit_pqueue_last_recalibrated);
  542. smartlist_pqueue_add(pol->active_circuit_pqueue,
  543. compare_cell_ewma_counts,
  544. STRUCT_OFFSET(cell_ewma_t, heap_index),
  545. ewma);
  546. }
  547. /** Remove <b>ewma</b> from <b>pol</b>'s priority queue of active circuits */
  548. static void
  549. remove_cell_ewma(ewma_policy_data_t *pol, cell_ewma_t *ewma)
  550. {
  551. tor_assert(pol);
  552. tor_assert(pol->active_circuit_pqueue);
  553. tor_assert(ewma);
  554. tor_assert(ewma->heap_index != -1);
  555. smartlist_pqueue_remove(pol->active_circuit_pqueue,
  556. compare_cell_ewma_counts,
  557. STRUCT_OFFSET(cell_ewma_t, heap_index),
  558. ewma);
  559. }
  560. /** Remove and return the first cell_ewma_t from pol's priority queue of
  561. * active circuits. Requires that the priority queue is nonempty. */
  562. static cell_ewma_t *
  563. pop_first_cell_ewma(ewma_policy_data_t *pol)
  564. {
  565. tor_assert(pol);
  566. tor_assert(pol->active_circuit_pqueue);
  567. return smartlist_pqueue_pop(pol->active_circuit_pqueue,
  568. compare_cell_ewma_counts,
  569. STRUCT_OFFSET(cell_ewma_t, heap_index));
  570. }