circuitmux_ewma.c 26 KB

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