circuitmux_ewma.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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. /*** EWMA circuitmux_policy_t method table ***/
  193. circuitmux_policy_t ewma_policy = {
  194. /*.alloc_cmux_data =*/ ewma_alloc_cmux_data,
  195. /*.free_cmux_data =*/ ewma_free_cmux_data,
  196. /*.alloc_circ_data =*/ ewma_alloc_circ_data,
  197. /*.free_circ_data =*/ ewma_free_circ_data,
  198. /*.notify_circ_active =*/ ewma_notify_circ_active,
  199. /*.notify_circ_inactive =*/ ewma_notify_circ_inactive,
  200. /*.notify_set_n_cells =*/ NULL, /* EWMA doesn't need this */
  201. /*.notify_xmit_cells =*/ ewma_notify_xmit_cells,
  202. /*.pick_active_circuit =*/ ewma_pick_active_circuit,
  203. /*.cmp_cmux =*/ ewma_cmp_cmux
  204. };
  205. /*** EWMA method implementations using the below EWMA helper functions ***/
  206. /** Compute and return the current cell_ewma tick. */
  207. static inline unsigned int
  208. cell_ewma_get_tick(void)
  209. {
  210. return ((unsigned)approx_time() / EWMA_TICK_LEN);
  211. }
  212. /**
  213. * Allocate an ewma_policy_data_t and upcast it to a circuitmux_policy_data_t;
  214. * this is called when setting the policy on a circuitmux_t to ewma_policy.
  215. */
  216. static circuitmux_policy_data_t *
  217. ewma_alloc_cmux_data(circuitmux_t *cmux)
  218. {
  219. ewma_policy_data_t *pol = NULL;
  220. tor_assert(cmux);
  221. pol = tor_malloc_zero(sizeof(*pol));
  222. pol->base_.magic = EWMA_POL_DATA_MAGIC;
  223. pol->active_circuit_pqueue = smartlist_new();
  224. pol->active_circuit_pqueue_last_recalibrated = cell_ewma_get_tick();
  225. return TO_CMUX_POL_DATA(pol);
  226. }
  227. /**
  228. * Free an ewma_policy_data_t allocated with ewma_alloc_cmux_data()
  229. */
  230. static void
  231. ewma_free_cmux_data(circuitmux_t *cmux,
  232. circuitmux_policy_data_t *pol_data)
  233. {
  234. ewma_policy_data_t *pol = NULL;
  235. tor_assert(cmux);
  236. if (!pol_data) return;
  237. pol = TO_EWMA_POL_DATA(pol_data);
  238. smartlist_free(pol->active_circuit_pqueue);
  239. tor_free(pol);
  240. }
  241. /**
  242. * Allocate an ewma_policy_circ_data_t and upcast it to a
  243. * circuitmux_policy_data_t; this is called when attaching a circuit to a
  244. * circuitmux_t with ewma_policy.
  245. */
  246. static circuitmux_policy_circ_data_t *
  247. ewma_alloc_circ_data(circuitmux_t *cmux,
  248. circuitmux_policy_data_t *pol_data,
  249. circuit_t *circ,
  250. cell_direction_t direction,
  251. unsigned int cell_count)
  252. {
  253. ewma_policy_circ_data_t *cdata = NULL;
  254. tor_assert(cmux);
  255. tor_assert(pol_data);
  256. tor_assert(circ);
  257. tor_assert(direction == CELL_DIRECTION_OUT ||
  258. direction == CELL_DIRECTION_IN);
  259. /* Shut the compiler up without triggering -Wtautological-compare */
  260. (void)cell_count;
  261. cdata = tor_malloc_zero(sizeof(*cdata));
  262. cdata->base_.magic = EWMA_POL_CIRC_DATA_MAGIC;
  263. cdata->circ = circ;
  264. /*
  265. * Initialize the cell_ewma_t structure (formerly in
  266. * init_circuit_base())
  267. */
  268. cdata->cell_ewma.last_adjusted_tick = cell_ewma_get_tick();
  269. cdata->cell_ewma.cell_count = 0.0;
  270. cdata->cell_ewma.heap_index = -1;
  271. if (direction == CELL_DIRECTION_IN) {
  272. cdata->cell_ewma.is_for_p_chan = 1;
  273. } else {
  274. cdata->cell_ewma.is_for_p_chan = 0;
  275. }
  276. return TO_CMUX_POL_CIRC_DATA(cdata);
  277. }
  278. /**
  279. * Free an ewma_policy_circ_data_t allocated with ewma_alloc_circ_data()
  280. */
  281. static void
  282. ewma_free_circ_data(circuitmux_t *cmux,
  283. circuitmux_policy_data_t *pol_data,
  284. circuit_t *circ,
  285. circuitmux_policy_circ_data_t *pol_circ_data)
  286. {
  287. ewma_policy_circ_data_t *cdata = NULL;
  288. tor_assert(cmux);
  289. tor_assert(circ);
  290. tor_assert(pol_data);
  291. if (!pol_circ_data) return;
  292. cdata = TO_EWMA_POL_CIRC_DATA(pol_circ_data);
  293. tor_free(cdata);
  294. }
  295. /**
  296. * Handle circuit activation; this inserts the circuit's cell_ewma into
  297. * the active_circuits_pqueue.
  298. */
  299. static void
  300. ewma_notify_circ_active(circuitmux_t *cmux,
  301. circuitmux_policy_data_t *pol_data,
  302. circuit_t *circ,
  303. circuitmux_policy_circ_data_t *pol_circ_data)
  304. {
  305. ewma_policy_data_t *pol = NULL;
  306. ewma_policy_circ_data_t *cdata = NULL;
  307. tor_assert(cmux);
  308. tor_assert(pol_data);
  309. tor_assert(circ);
  310. tor_assert(pol_circ_data);
  311. pol = TO_EWMA_POL_DATA(pol_data);
  312. cdata = TO_EWMA_POL_CIRC_DATA(pol_circ_data);
  313. add_cell_ewma(pol, &(cdata->cell_ewma));
  314. }
  315. /**
  316. * Handle circuit deactivation; this removes the circuit's cell_ewma from
  317. * the active_circuits_pqueue.
  318. */
  319. static void
  320. ewma_notify_circ_inactive(circuitmux_t *cmux,
  321. circuitmux_policy_data_t *pol_data,
  322. circuit_t *circ,
  323. circuitmux_policy_circ_data_t *pol_circ_data)
  324. {
  325. ewma_policy_data_t *pol = NULL;
  326. ewma_policy_circ_data_t *cdata = NULL;
  327. tor_assert(cmux);
  328. tor_assert(pol_data);
  329. tor_assert(circ);
  330. tor_assert(pol_circ_data);
  331. pol = TO_EWMA_POL_DATA(pol_data);
  332. cdata = TO_EWMA_POL_CIRC_DATA(pol_circ_data);
  333. remove_cell_ewma(pol, &(cdata->cell_ewma));
  334. }
  335. /**
  336. * Update cell_ewma for this circuit after we've sent some cells, and
  337. * remove/reinsert it in the queue. This used to be done (brokenly,
  338. * see bug 6816) in channel_flush_from_first_active_circuit().
  339. */
  340. static void
  341. ewma_notify_xmit_cells(circuitmux_t *cmux,
  342. circuitmux_policy_data_t *pol_data,
  343. circuit_t *circ,
  344. circuitmux_policy_circ_data_t *pol_circ_data,
  345. unsigned int n_cells)
  346. {
  347. ewma_policy_data_t *pol = NULL;
  348. ewma_policy_circ_data_t *cdata = NULL;
  349. unsigned int tick;
  350. double fractional_tick, ewma_increment;
  351. /* The current (hi-res) time */
  352. struct timeval now_hires;
  353. cell_ewma_t *cell_ewma, *tmp;
  354. tor_assert(cmux);
  355. tor_assert(pol_data);
  356. tor_assert(circ);
  357. tor_assert(pol_circ_data);
  358. tor_assert(n_cells > 0);
  359. pol = TO_EWMA_POL_DATA(pol_data);
  360. cdata = TO_EWMA_POL_CIRC_DATA(pol_circ_data);
  361. /* Rescale the EWMAs if needed */
  362. tor_gettimeofday_cached(&now_hires);
  363. tick = cell_ewma_tick_from_timeval(&now_hires, &fractional_tick);
  364. if (tick != pol->active_circuit_pqueue_last_recalibrated) {
  365. scale_active_circuits(pol, tick);
  366. }
  367. /* How much do we adjust the cell count in cell_ewma by? */
  368. ewma_increment =
  369. ((double)(n_cells)) * pow(ewma_scale_factor, -fractional_tick);
  370. /* Do the adjustment */
  371. cell_ewma = &(cdata->cell_ewma);
  372. cell_ewma->cell_count += ewma_increment;
  373. /*
  374. * Since we just sent on this circuit, it should be at the head of
  375. * the queue. Pop the head, assert that it matches, then re-add.
  376. */
  377. tmp = pop_first_cell_ewma(pol);
  378. tor_assert(tmp == cell_ewma);
  379. add_cell_ewma(pol, cell_ewma);
  380. }
  381. /**
  382. * Pick the preferred circuit to send from; this will be the one with
  383. * the lowest EWMA value in the priority queue. This used to be done
  384. * in channel_flush_from_first_active_circuit().
  385. */
  386. static circuit_t *
  387. ewma_pick_active_circuit(circuitmux_t *cmux,
  388. circuitmux_policy_data_t *pol_data)
  389. {
  390. ewma_policy_data_t *pol = NULL;
  391. circuit_t *circ = NULL;
  392. cell_ewma_t *cell_ewma = NULL;
  393. tor_assert(cmux);
  394. tor_assert(pol_data);
  395. pol = TO_EWMA_POL_DATA(pol_data);
  396. if (smartlist_len(pol->active_circuit_pqueue) > 0) {
  397. /* Get the head of the queue */
  398. cell_ewma = smartlist_get(pol->active_circuit_pqueue, 0);
  399. circ = cell_ewma_to_circuit(cell_ewma);
  400. }
  401. return circ;
  402. }
  403. /**
  404. * Compare two EWMA cmuxes, and return -1, 0 or 1 to indicate which should
  405. * be more preferred - see circuitmux_compare_muxes() of circuitmux.c.
  406. */
  407. static int
  408. ewma_cmp_cmux(circuitmux_t *cmux_1, circuitmux_policy_data_t *pol_data_1,
  409. circuitmux_t *cmux_2, circuitmux_policy_data_t *pol_data_2)
  410. {
  411. ewma_policy_data_t *p1 = NULL, *p2 = NULL;
  412. cell_ewma_t *ce1 = NULL, *ce2 = NULL;
  413. tor_assert(cmux_1);
  414. tor_assert(pol_data_1);
  415. tor_assert(cmux_2);
  416. tor_assert(pol_data_2);
  417. p1 = TO_EWMA_POL_DATA(pol_data_1);
  418. p2 = TO_EWMA_POL_DATA(pol_data_2);
  419. if (p1 != p2) {
  420. /* Get the head cell_ewma_t from each queue */
  421. if (smartlist_len(p1->active_circuit_pqueue) > 0) {
  422. ce1 = smartlist_get(p1->active_circuit_pqueue, 0);
  423. }
  424. if (smartlist_len(p2->active_circuit_pqueue) > 0) {
  425. ce2 = smartlist_get(p2->active_circuit_pqueue, 0);
  426. }
  427. /* Got both of them? */
  428. if (ce1 != NULL && ce2 != NULL) {
  429. /* Pick whichever one has the better best circuit */
  430. return compare_cell_ewma_counts(ce1, ce2);
  431. } else {
  432. if (ce1 != NULL ) {
  433. /* We only have a circuit on cmux_1, so prefer it */
  434. return -1;
  435. } else if (ce2 != NULL) {
  436. /* We only have a circuit on cmux_2, so prefer it */
  437. return 1;
  438. } else {
  439. /* No circuits at all; no preference */
  440. return 0;
  441. }
  442. }
  443. } else {
  444. /* We got identical params */
  445. return 0;
  446. }
  447. }
  448. /** Helper for sorting cell_ewma_t values in their priority queue. */
  449. static int
  450. compare_cell_ewma_counts(const void *p1, const void *p2)
  451. {
  452. const cell_ewma_t *e1 = p1, *e2 = p2;
  453. if (e1->cell_count < e2->cell_count)
  454. return -1;
  455. else if (e1->cell_count > e2->cell_count)
  456. return 1;
  457. else
  458. return 0;
  459. }
  460. /** Given a cell_ewma_t, return a pointer to the circuit containing it. */
  461. static circuit_t *
  462. cell_ewma_to_circuit(cell_ewma_t *ewma)
  463. {
  464. ewma_policy_circ_data_t *cdata = NULL;
  465. tor_assert(ewma);
  466. cdata = SUBTYPE_P(ewma, ewma_policy_circ_data_t, cell_ewma);
  467. tor_assert(cdata);
  468. return cdata->circ;
  469. }
  470. /* ==== Functions for scaling cell_ewma_t ====
  471. When choosing which cells to relay first, we favor circuits that have been
  472. quiet recently. This gives better latency on connections that aren't
  473. pushing lots of data, and makes the network feel more interactive.
  474. Conceptually, we take an exponentially weighted mean average of the number
  475. of cells a circuit has sent, and allow active circuits (those with cells to
  476. relay) to send cells in reverse order of their exponentially-weighted mean
  477. average (EWMA) cell count. [That is, a cell sent N seconds ago 'counts'
  478. F^N times as much as a cell sent now, for 0<F<1.0, and we favor the
  479. circuit that has sent the fewest cells]
  480. If 'double' had infinite precision, we could do this simply by counting a
  481. cell sent at startup as having weight 1.0, and a cell sent N seconds later
  482. as having weight F^-N. This way, we would never need to re-scale
  483. any already-sent cells.
  484. To prevent double from overflowing, we could count a cell sent now as
  485. having weight 1.0 and a cell sent N seconds ago as having weight F^N.
  486. This, however, would mean we'd need to re-scale *ALL* old circuits every
  487. time we wanted to send a cell.
  488. So as a compromise, we divide time into 'ticks' (currently, 10-second
  489. increments) and say that a cell sent at the start of a current tick is
  490. worth 1.0, a cell sent N seconds before the start of the current tick is
  491. worth F^N, and a cell sent N seconds after the start of the current tick is
  492. worth F^-N. This way we don't overflow, and we don't need to constantly
  493. rescale.
  494. */
  495. /** Given a timeval <b>now</b>, compute the cell_ewma tick in which it occurs
  496. * and the fraction of the tick that has elapsed between the start of the tick
  497. * and <b>now</b>. Return the former and store the latter in
  498. * *<b>remainder_out</b>.
  499. *
  500. * These tick values are not meant to be shared between Tor instances, or used
  501. * for other purposes. */
  502. static unsigned
  503. cell_ewma_tick_from_timeval(const struct timeval *now,
  504. double *remainder_out)
  505. {
  506. unsigned res = (unsigned) (now->tv_sec / EWMA_TICK_LEN);
  507. /* rem */
  508. double rem = (now->tv_sec % EWMA_TICK_LEN) +
  509. ((double)(now->tv_usec)) / 1.0e6;
  510. *remainder_out = rem / EWMA_TICK_LEN;
  511. return res;
  512. }
  513. /* Default value for the CircuitPriorityHalflifeMsec consensus parameter in
  514. * msec. */
  515. #define CMUX_PRIORITY_HALFLIFE_MSEC_DEFAULT 30000
  516. /* Minimum and maximum value for the CircuitPriorityHalflifeMsec consensus
  517. * parameter. */
  518. #define CMUX_PRIORITY_HALFLIFE_MSEC_MIN 1
  519. #define CMUX_PRIORITY_HALFLIFE_MSEC_MAX INT32_MAX
  520. /* Return the value of the circuit priority halflife from the options if
  521. * available or else from the consensus (in that order). If none can be found,
  522. * a default value is returned.
  523. *
  524. * The source_msg points to a string describing from where the value was
  525. * picked so it can be used for logging. */
  526. static double
  527. get_circuit_priority_halflife(const or_options_t *options,
  528. const networkstatus_t *consensus,
  529. const char **source_msg)
  530. {
  531. int32_t halflife_ms;
  532. double halflife;
  533. /* Compute the default value now. We might need it. */
  534. double halflife_default =
  535. ((double) CMUX_PRIORITY_HALFLIFE_MSEC_DEFAULT) / 1000.0;
  536. /* Try to get it from configuration file first. */
  537. if (options && options->CircuitPriorityHalflife >= -EPSILON) {
  538. halflife = options->CircuitPriorityHalflife;
  539. *source_msg = "CircuitPriorityHalflife in configuration";
  540. goto end;
  541. }
  542. /* Try to get the msec value from the consensus. */
  543. halflife_ms = networkstatus_get_param(consensus,
  544. "CircuitPriorityHalflifeMsec",
  545. CMUX_PRIORITY_HALFLIFE_MSEC_DEFAULT,
  546. CMUX_PRIORITY_HALFLIFE_MSEC_MIN,
  547. CMUX_PRIORITY_HALFLIFE_MSEC_MAX);
  548. halflife = ((double) halflife_ms) / 1000.0;
  549. *source_msg = "CircuitPriorityHalflifeMsec in consensus";
  550. end:
  551. /* We should never go below the EPSILON else we would consider it disabled
  552. * and we can't have that. */
  553. if (halflife < EPSILON) {
  554. log_warn(LD_CONFIG, "CircuitPriorityHalflife is too small (%f). "
  555. "Adjusting to the smallest value allowed: %f.",
  556. halflife, halflife_default);
  557. halflife = halflife_default;
  558. }
  559. return halflife;
  560. }
  561. /** Adjust the global cell scale factor based on <b>options</b> */
  562. void
  563. cmux_ewma_set_options(const or_options_t *options,
  564. const networkstatus_t *consensus)
  565. {
  566. double halflife;
  567. const char *source;
  568. /* Both options and consensus can be NULL. This assures us to either get a
  569. * valid configured value or the default one. */
  570. halflife = get_circuit_priority_halflife(options, consensus, &source);
  571. /* convert halflife into halflife-per-tick. */
  572. halflife /= EWMA_TICK_LEN;
  573. /* compute per-tick scale factor. */
  574. ewma_scale_factor = exp( LOG_ONEHALF / halflife );
  575. log_info(LD_OR,
  576. "Enabled cell_ewma algorithm because of value in %s; "
  577. "scale factor is %f per %d seconds",
  578. source, ewma_scale_factor, EWMA_TICK_LEN);
  579. }
  580. /** Return the multiplier necessary to convert the value of a cell sent in
  581. * 'from_tick' to one sent in 'to_tick'. */
  582. static inline double
  583. get_scale_factor(unsigned from_tick, unsigned to_tick)
  584. {
  585. /* This math can wrap around, but that's okay: unsigned overflow is
  586. well-defined */
  587. int diff = (int)(to_tick - from_tick);
  588. return pow(ewma_scale_factor, diff);
  589. }
  590. /** Adjust the cell count of <b>ewma</b> so that it is scaled with respect to
  591. * <b>cur_tick</b> */
  592. static void
  593. scale_single_cell_ewma(cell_ewma_t *ewma, unsigned cur_tick)
  594. {
  595. double factor = get_scale_factor(ewma->last_adjusted_tick, cur_tick);
  596. ewma->cell_count *= factor;
  597. ewma->last_adjusted_tick = cur_tick;
  598. }
  599. /** Adjust the cell count of every active circuit on <b>chan</b> so
  600. * that they are scaled with respect to <b>cur_tick</b> */
  601. static void
  602. scale_active_circuits(ewma_policy_data_t *pol, unsigned cur_tick)
  603. {
  604. double factor;
  605. tor_assert(pol);
  606. tor_assert(pol->active_circuit_pqueue);
  607. factor =
  608. get_scale_factor(
  609. pol->active_circuit_pqueue_last_recalibrated,
  610. cur_tick);
  611. /** Ordinarily it isn't okay to change the value of an element in a heap,
  612. * but it's okay here, since we are preserving the order. */
  613. SMARTLIST_FOREACH_BEGIN(
  614. pol->active_circuit_pqueue,
  615. cell_ewma_t *, e) {
  616. tor_assert(e->last_adjusted_tick ==
  617. pol->active_circuit_pqueue_last_recalibrated);
  618. e->cell_count *= factor;
  619. e->last_adjusted_tick = cur_tick;
  620. } SMARTLIST_FOREACH_END(e);
  621. pol->active_circuit_pqueue_last_recalibrated = cur_tick;
  622. }
  623. /** Rescale <b>ewma</b> to the same scale as <b>pol</b>, and add it to
  624. * <b>pol</b>'s priority queue of active circuits */
  625. static void
  626. add_cell_ewma(ewma_policy_data_t *pol, cell_ewma_t *ewma)
  627. {
  628. tor_assert(pol);
  629. tor_assert(pol->active_circuit_pqueue);
  630. tor_assert(ewma);
  631. tor_assert(ewma->heap_index == -1);
  632. scale_single_cell_ewma(
  633. ewma,
  634. pol->active_circuit_pqueue_last_recalibrated);
  635. smartlist_pqueue_add(pol->active_circuit_pqueue,
  636. compare_cell_ewma_counts,
  637. offsetof(cell_ewma_t, heap_index),
  638. ewma);
  639. }
  640. /** Remove <b>ewma</b> from <b>pol</b>'s priority queue of active circuits */
  641. static void
  642. remove_cell_ewma(ewma_policy_data_t *pol, cell_ewma_t *ewma)
  643. {
  644. tor_assert(pol);
  645. tor_assert(pol->active_circuit_pqueue);
  646. tor_assert(ewma);
  647. tor_assert(ewma->heap_index != -1);
  648. smartlist_pqueue_remove(pol->active_circuit_pqueue,
  649. compare_cell_ewma_counts,
  650. offsetof(cell_ewma_t, heap_index),
  651. ewma);
  652. }
  653. /** Remove and return the first cell_ewma_t from pol's priority queue of
  654. * active circuits. Requires that the priority queue is nonempty. */
  655. static cell_ewma_t *
  656. pop_first_cell_ewma(ewma_policy_data_t *pol)
  657. {
  658. tor_assert(pol);
  659. tor_assert(pol->active_circuit_pqueue);
  660. return smartlist_pqueue_pop(pol->active_circuit_pqueue,
  661. compare_cell_ewma_counts,
  662. offsetof(cell_ewma_t, heap_index));
  663. }