circuitmux_ewma.c 24 KB

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