rephist.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /* Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. const char rephist_c_id[] =
  5. "$Id$";
  6. /**
  7. * \file rephist.c
  8. * \brief Basic history and "reputation" functionality to remember
  9. * which servers have worked in the past, how much bandwidth we've
  10. * been using, which ports we tend to want, and so on.
  11. **/
  12. #include "or.h"
  13. static void bw_arrays_init(void);
  14. static void predicted_ports_init(void);
  15. uint64_t rephist_total_alloc=0;
  16. uint32_t rephist_total_num=0;
  17. /** History of an OR-\>OR link. */
  18. typedef struct link_history_t {
  19. /** When did we start tracking this list? */
  20. time_t since;
  21. /** When did we most recently note a change to this link */
  22. time_t changed;
  23. /** How many times did extending from OR1 to OR2 succeed? */
  24. unsigned long n_extend_ok;
  25. /** How many times did extending from OR1 to OR2 fail? */
  26. unsigned long n_extend_fail;
  27. } link_history_t;
  28. /** History of an OR. */
  29. typedef struct or_history_t {
  30. /** When did we start tracking this OR? */
  31. time_t since;
  32. /** When did we most recently note a change to this OR? */
  33. time_t changed;
  34. /** How many times did we successfully connect? */
  35. unsigned long n_conn_ok;
  36. /** How many times did we try to connect and fail?*/
  37. unsigned long n_conn_fail;
  38. /** How many seconds have we been connected to this OR before
  39. * 'up_since'? */
  40. unsigned long uptime;
  41. /** How many seconds have we been unable to connect to this OR before
  42. * 'down_since'? */
  43. unsigned long downtime;
  44. /** If nonzero, we have been connected since this time. */
  45. time_t up_since;
  46. /** If nonzero, we have been unable to connect since this time. */
  47. time_t down_since;
  48. /** Map from hex OR2 identity digest to a link_history_t for the link
  49. * from this OR to OR2. */
  50. digestmap_t *link_history_map;
  51. } or_history_t;
  52. /** Map from hex OR identity digest to or_history_t. */
  53. static digestmap_t *history_map = NULL;
  54. /** Return the or_history_t for the named OR, creating it if necessary.
  55. */
  56. static or_history_t *
  57. get_or_history(const char* id)
  58. {
  59. or_history_t *hist;
  60. if (!memcmp(id, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", DIGEST_LEN))
  61. return NULL;
  62. hist = digestmap_get(history_map, id);
  63. if (!hist) {
  64. hist = tor_malloc_zero(sizeof(or_history_t));
  65. rephist_total_alloc += sizeof(or_history_t);
  66. rephist_total_num++;
  67. hist->link_history_map = digestmap_new();
  68. hist->since = hist->changed = time(NULL);
  69. digestmap_set(history_map, id, hist);
  70. }
  71. return hist;
  72. }
  73. /** Return the link_history_t for the link from the first named OR to
  74. * the second, creating it if necessary. (ORs are identified by
  75. * identity digest)
  76. */
  77. static link_history_t *
  78. get_link_history(const char *from_id, const char *to_id)
  79. {
  80. or_history_t *orhist;
  81. link_history_t *lhist;
  82. orhist = get_or_history(from_id);
  83. if (!orhist)
  84. return NULL;
  85. if (!memcmp(to_id, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", DIGEST_LEN))
  86. return NULL;
  87. lhist = (link_history_t*) digestmap_get(orhist->link_history_map, to_id);
  88. if (!lhist) {
  89. lhist = tor_malloc_zero(sizeof(link_history_t));
  90. rephist_total_alloc += sizeof(link_history_t);
  91. lhist->since = lhist->changed = time(NULL);
  92. digestmap_set(orhist->link_history_map, to_id, lhist);
  93. }
  94. return lhist;
  95. }
  96. /** Helper: free storage held by a single link history entry */
  97. static void
  98. _free_link_history(void *val)
  99. {
  100. rephist_total_alloc -= sizeof(link_history_t);
  101. tor_free(val);
  102. }
  103. /** Helper: free storage held by a single OR history entry */
  104. static void
  105. free_or_history(void *_hist)
  106. {
  107. or_history_t *hist = _hist;
  108. digestmap_free(hist->link_history_map, _free_link_history);
  109. rephist_total_alloc -= sizeof(or_history_t);
  110. rephist_total_num--;
  111. tor_free(hist);
  112. }
  113. /** Update an or_history_t object <b>hist</b> so that its uptime/downtime
  114. * count is up-to-date as of <b>when</b>.
  115. */
  116. static void
  117. update_or_history(or_history_t *hist, time_t when)
  118. {
  119. tor_assert(hist);
  120. if (hist->up_since) {
  121. tor_assert(!hist->down_since);
  122. hist->uptime += (when - hist->up_since);
  123. hist->up_since = when;
  124. } else if (hist->down_since) {
  125. hist->downtime += (when - hist->down_since);
  126. hist->down_since = when;
  127. }
  128. }
  129. /** Initialize the static data structures for tracking history.
  130. */
  131. void
  132. rep_hist_init(void)
  133. {
  134. history_map = digestmap_new();
  135. bw_arrays_init();
  136. predicted_ports_init();
  137. }
  138. /** Remember that an attempt to connect to the OR with identity digest
  139. * <b>id</b> failed at <b>when</b>.
  140. */
  141. void
  142. rep_hist_note_connect_failed(const char* id, time_t when)
  143. {
  144. or_history_t *hist;
  145. hist = get_or_history(id);
  146. if (!hist)
  147. return;
  148. ++hist->n_conn_fail;
  149. if (hist->up_since) {
  150. hist->uptime += (when - hist->up_since);
  151. hist->up_since = 0;
  152. }
  153. if (!hist->down_since)
  154. hist->down_since = when;
  155. hist->changed = when;
  156. }
  157. /** Remember that an attempt to connect to the OR with identity digest
  158. * <b>id</b> succeeded at <b>when</b>.
  159. */
  160. void
  161. rep_hist_note_connect_succeeded(const char* id, time_t when)
  162. {
  163. or_history_t *hist;
  164. hist = get_or_history(id);
  165. if (!hist)
  166. return;
  167. ++hist->n_conn_ok;
  168. if (hist->down_since) {
  169. hist->downtime += (when - hist->down_since);
  170. hist->down_since = 0;
  171. }
  172. if (!hist->up_since)
  173. hist->up_since = when;
  174. hist->changed = when;
  175. }
  176. /** Remember that we intentionally closed our connection to the OR
  177. * with identity digest <b>id</b> at <b>when</b>.
  178. */
  179. void
  180. rep_hist_note_disconnect(const char* id, time_t when)
  181. {
  182. or_history_t *hist;
  183. hist = get_or_history(id);
  184. if (!hist)
  185. return;
  186. ++hist->n_conn_ok;
  187. if (hist->up_since) {
  188. hist->uptime += (when - hist->up_since);
  189. hist->up_since = 0;
  190. }
  191. hist->changed = when;
  192. }
  193. /** Remember that our connection to the OR with identity digest
  194. * <b>id</b> had an error and stopped working at <b>when</b>.
  195. */
  196. void
  197. rep_hist_note_connection_died(const char* id, time_t when)
  198. {
  199. or_history_t *hist;
  200. if (!id) {
  201. /* XXXX009 Well, everybody has an ID now. Hm. */
  202. /* If conn has no nickname, it's either an OP, or it is an OR
  203. * which didn't complete its handshake (or did and was unapproved).
  204. * Ignore it.
  205. */
  206. return;
  207. }
  208. hist = get_or_history(id);
  209. if (!hist)
  210. return;
  211. if (hist->up_since) {
  212. hist->uptime += (when - hist->up_since);
  213. hist->up_since = 0;
  214. }
  215. if (!hist->down_since)
  216. hist->down_since = when;
  217. hist->changed = when;
  218. }
  219. /** Remember that we successfully extended from the OR with identity
  220. * digest <b>from_id</b> to the OR with identity digest
  221. * <b>to_name</b>.
  222. */
  223. void
  224. rep_hist_note_extend_succeeded(const char *from_id, const char *to_id)
  225. {
  226. link_history_t *hist;
  227. /* log_fn(LOG_WARN, "EXTEND SUCCEEDED: %s->%s",from_name,to_name); */
  228. hist = get_link_history(from_id, to_id);
  229. if (!hist)
  230. return;
  231. ++hist->n_extend_ok;
  232. hist->changed = time(NULL);
  233. }
  234. /** Remember that we tried to extend from the OR with identity digest
  235. * <b>from_id</b> to the OR with identity digest <b>to_name</b>, but
  236. * failed.
  237. */
  238. void
  239. rep_hist_note_extend_failed(const char *from_id, const char *to_id)
  240. {
  241. link_history_t *hist;
  242. /* log_fn(LOG_WARN, "EXTEND FAILED: %s->%s",from_name,to_name); */
  243. hist = get_link_history(from_id, to_id);
  244. if (!hist)
  245. return;
  246. ++hist->n_extend_fail;
  247. hist->changed = time(NULL);
  248. }
  249. /** Log all the reliability data we have remembered, with the chosen
  250. * severity.
  251. */
  252. void
  253. rep_hist_dump_stats(time_t now, int severity)
  254. {
  255. digestmap_iter_t *lhist_it;
  256. digestmap_iter_t *orhist_it;
  257. const char *name1, *name2, *digest1, *digest2;
  258. char hexdigest1[HEX_DIGEST_LEN+1];
  259. or_history_t *or_history;
  260. link_history_t *link_history;
  261. void *or_history_p, *link_history_p;
  262. double uptime;
  263. char buffer[2048];
  264. size_t len;
  265. int ret;
  266. unsigned long upt, downt;
  267. routerinfo_t *r;
  268. rep_history_clean(now - get_options()->RephistTrackTime);
  269. log(severity, LD_GENERAL, "--------------- Dumping history information:");
  270. for (orhist_it = digestmap_iter_init(history_map);
  271. !digestmap_iter_done(orhist_it);
  272. orhist_it = digestmap_iter_next(history_map,orhist_it)) {
  273. digestmap_iter_get(orhist_it, &digest1, &or_history_p);
  274. or_history = (or_history_t*) or_history_p;
  275. if ((r = router_get_by_digest(digest1)))
  276. name1 = r->nickname;
  277. else
  278. name1 = "(unknown)";
  279. base16_encode(hexdigest1, sizeof(hexdigest1), digest1, DIGEST_LEN);
  280. update_or_history(or_history, now);
  281. upt = or_history->uptime;
  282. downt = or_history->downtime;
  283. if (upt+downt) {
  284. uptime = ((double)upt) / (upt+downt);
  285. } else {
  286. uptime=1.0;
  287. }
  288. log(severity, LD_GENERAL,
  289. "OR %s [%s]: %ld/%ld good connections; uptime %ld/%ld sec (%.2f%%)",
  290. name1, hexdigest1,
  291. or_history->n_conn_ok, or_history->n_conn_fail+or_history->n_conn_ok,
  292. upt, upt+downt, uptime*100.0);
  293. if (!digestmap_isempty(or_history->link_history_map)) {
  294. strlcpy(buffer, " Extend attempts: ", sizeof(buffer));
  295. len = strlen(buffer);
  296. for (lhist_it = digestmap_iter_init(or_history->link_history_map);
  297. !digestmap_iter_done(lhist_it);
  298. lhist_it = digestmap_iter_next(or_history->link_history_map,
  299. lhist_it)) {
  300. digestmap_iter_get(lhist_it, &digest2, &link_history_p);
  301. if ((r = router_get_by_digest(digest2)))
  302. name2 = r->nickname;
  303. else
  304. name2 = "(unknown)";
  305. link_history = (link_history_t*) link_history_p;
  306. ret = tor_snprintf(buffer+len, 2048-len, "%s(%ld/%ld); ", name2,
  307. link_history->n_extend_ok,
  308. link_history->n_extend_ok+link_history->n_extend_fail);
  309. if (ret<0)
  310. break;
  311. else
  312. len += ret;
  313. }
  314. log(severity, LD_GENERAL, "%s", buffer);
  315. }
  316. }
  317. }
  318. /** Remove history info for routers/links that haven't changed since
  319. * <b>before</b> */
  320. void
  321. rep_history_clean(time_t before)
  322. {
  323. or_history_t *or_history;
  324. link_history_t *link_history;
  325. void *or_history_p, *link_history_p;
  326. digestmap_iter_t *orhist_it, *lhist_it;
  327. const char *d1, *d2;
  328. orhist_it = digestmap_iter_init(history_map);
  329. while (!digestmap_iter_done(orhist_it)) {
  330. digestmap_iter_get(orhist_it, &d1, &or_history_p);
  331. or_history = or_history_p;
  332. if (or_history->changed < before) {
  333. orhist_it = digestmap_iter_next_rmv(history_map, orhist_it);
  334. free_or_history(or_history);
  335. continue;
  336. }
  337. for (lhist_it = digestmap_iter_init(or_history->link_history_map);
  338. !digestmap_iter_done(lhist_it); ) {
  339. digestmap_iter_get(lhist_it, &d2, &link_history_p);
  340. link_history = link_history_p;
  341. if (link_history->changed < before) {
  342. lhist_it = digestmap_iter_next_rmv(or_history->link_history_map,
  343. lhist_it);
  344. rephist_total_alloc -= sizeof(link_history_t);
  345. tor_free(link_history);
  346. continue;
  347. }
  348. lhist_it = digestmap_iter_next(or_history->link_history_map,lhist_it);
  349. }
  350. orhist_it = digestmap_iter_next(history_map, orhist_it);
  351. }
  352. }
  353. #define NUM_SECS_ROLLING_MEASURE 10
  354. #define NUM_SECS_BW_SUM_IS_VALID (24*60*60) /* one day */
  355. #define NUM_SECS_BW_SUM_INTERVAL (15*60)
  356. #define NUM_TOTALS (NUM_SECS_BW_SUM_IS_VALID/NUM_SECS_BW_SUM_INTERVAL)
  357. /**
  358. * Structure to track bandwidth use, and remember the maxima for a given
  359. * time period.
  360. */
  361. typedef struct bw_array_t {
  362. /** Observation array: Total number of bytes transferred in each of the last
  363. * NUM_SECS_ROLLING_MEASURE seconds. This is used as a circular array. */
  364. int obs[NUM_SECS_ROLLING_MEASURE];
  365. int cur_obs_idx; /**< Current position in obs. */
  366. time_t cur_obs_time; /**< Time represented in obs[cur_obs_idx] */
  367. int total_obs; /**< Total for all members of obs except obs[cur_obs_idx] */
  368. int max_total; /**< Largest value that total_obs has taken on in the current
  369. * period. */
  370. uint64_t total_in_period; /**< Total bytes transferred in the current
  371. * period. */
  372. /** When does the next period begin? */
  373. time_t next_period;
  374. /** Where in 'maxima' should the maximum bandwidth usage for the current
  375. * period be stored? */
  376. int next_max_idx;
  377. /** How many values in maxima/totals have been set ever? */
  378. int num_maxes_set;
  379. /** Circular array of the maximum
  380. * bandwidth-per-NUM_SECS_ROLLING_MEASURE usage for the last
  381. * NUM_TOTALS periods */
  382. int maxima[NUM_TOTALS];
  383. /** Circular array of the total bandwidth usage for the last NUM_TOTALS
  384. * periods */
  385. uint64_t totals[NUM_TOTALS];
  386. } bw_array_t;
  387. /** Shift the current period of b forward by one.
  388. */
  389. static void
  390. commit_max(bw_array_t *b)
  391. {
  392. /* Store total from current period. */
  393. b->totals[b->next_max_idx] = b->total_in_period;
  394. /* Store maximum from current period. */
  395. b->maxima[b->next_max_idx++] = b->max_total;
  396. /* Advance next_period and next_max_idx */
  397. b->next_period += NUM_SECS_BW_SUM_INTERVAL;
  398. if (b->next_max_idx == NUM_TOTALS)
  399. b->next_max_idx = 0;
  400. if (b->num_maxes_set < NUM_TOTALS)
  401. ++b->num_maxes_set;
  402. /* Reset max_total. */
  403. b->max_total = 0;
  404. /* Reset total_in_period. */
  405. b->total_in_period = 0;
  406. }
  407. /** Shift the current observation time of 'b' forward by one second.
  408. */
  409. static INLINE void
  410. advance_obs(bw_array_t *b)
  411. {
  412. int nextidx;
  413. int total;
  414. /* Calculate the total bandwidth for the last NUM_SECS_ROLLING_MEASURE
  415. * seconds; adjust max_total as needed.*/
  416. total = b->total_obs + b->obs[b->cur_obs_idx];
  417. if (total > b->max_total)
  418. b->max_total = total;
  419. nextidx = b->cur_obs_idx+1;
  420. if (nextidx == NUM_SECS_ROLLING_MEASURE)
  421. nextidx = 0;
  422. b->total_obs = total - b->obs[nextidx];
  423. b->obs[nextidx]=0;
  424. b->cur_obs_idx = nextidx;
  425. if (++b->cur_obs_time >= b->next_period)
  426. commit_max(b);
  427. }
  428. /** Add 'n' bytes to the number of bytes in b for second 'when'.
  429. */
  430. static INLINE void
  431. add_obs(bw_array_t *b, time_t when, int n)
  432. {
  433. /* Don't record data in the past. */
  434. if (when<b->cur_obs_time)
  435. return;
  436. /* If we're currently adding observations for an earlier second than
  437. * 'when', advance b->cur_obs_time and b->cur_obs_idx by an
  438. * appropriate number of seconds, and do all the other housekeeping */
  439. while (when>b->cur_obs_time)
  440. advance_obs(b);
  441. b->obs[b->cur_obs_idx] += n;
  442. b->total_in_period += n;
  443. }
  444. /** Allocate, initialize, and return a new bw_array.
  445. */
  446. static bw_array_t *
  447. bw_array_new(void)
  448. {
  449. bw_array_t *b;
  450. time_t start;
  451. b = tor_malloc_zero(sizeof(bw_array_t));
  452. rephist_total_alloc += sizeof(bw_array_t);
  453. start = time(NULL);
  454. b->cur_obs_time = start;
  455. b->next_period = start + NUM_SECS_BW_SUM_INTERVAL;
  456. return b;
  457. }
  458. static bw_array_t *read_array = NULL;
  459. static bw_array_t *write_array = NULL;
  460. /** Set up read_array and write_array
  461. */
  462. static void
  463. bw_arrays_init(void)
  464. {
  465. read_array = bw_array_new();
  466. write_array = bw_array_new();
  467. }
  468. /** We read <b>num_bytes</b> more bytes in second <b>when</b>.
  469. *
  470. * Add num_bytes to the current running total for <b>when</b>.
  471. *
  472. * <b>when</b> can go back to time, but it's safe to ignore calls
  473. * earlier than the latest <b>when</b> you've heard of.
  474. */
  475. void
  476. rep_hist_note_bytes_written(int num_bytes, time_t when)
  477. {
  478. /* Maybe a circular array for recent seconds, and step to a new point
  479. * every time a new second shows up. Or simpler is to just to have
  480. * a normal array and push down each item every second; it's short.
  481. */
  482. /* When a new second has rolled over, compute the sum of the bytes we've
  483. * seen over when-1 to when-1-NUM_SECS_ROLLING_MEASURE, and stick it
  484. * somewhere. See rep_hist_bandwidth_assess() below.
  485. */
  486. add_obs(write_array, when, num_bytes);
  487. }
  488. /** We wrote <b>num_bytes</b> more bytes in second <b>when</b>.
  489. * (like rep_hist_note_bytes_written() above)
  490. */
  491. void
  492. rep_hist_note_bytes_read(int num_bytes, time_t when)
  493. {
  494. /* if we're smart, we can make this func and the one above share code */
  495. add_obs(read_array, when, num_bytes);
  496. }
  497. /** Helper: Return the largest value in b->maxima. (This is equal to the
  498. * most bandwidth used in any NUM_SECS_ROLLING_MEASURE period for the last
  499. * NUM_SECS_BW_SUM_IS_VALID seconds.)
  500. */
  501. static int
  502. find_largest_max(bw_array_t *b)
  503. {
  504. int i,max;
  505. max=0;
  506. for (i=0; i<NUM_TOTALS; ++i) {
  507. if (b->maxima[i]>max)
  508. max = b->maxima[i];
  509. }
  510. return max;
  511. }
  512. /**
  513. * Find the largest sums in the past NUM_SECS_BW_SUM_IS_VALID (roughly)
  514. * seconds. Find one sum for reading and one for writing. They don't have
  515. * to be at the same time).
  516. *
  517. * Return the smaller of these sums, divided by NUM_SECS_ROLLING_MEASURE.
  518. */
  519. int
  520. rep_hist_bandwidth_assess(void)
  521. {
  522. int w,r;
  523. r = find_largest_max(read_array);
  524. w = find_largest_max(write_array);
  525. if (r>w)
  526. return (int)(w/(double)NUM_SECS_ROLLING_MEASURE);
  527. else
  528. return (int)(r/(double)NUM_SECS_ROLLING_MEASURE);
  529. return 0;
  530. }
  531. /**
  532. * Allocate and return lines for representing this server's bandwidth
  533. * history in its descriptor.
  534. */
  535. char *
  536. rep_hist_get_bandwidth_lines(void)
  537. {
  538. char *buf, *cp;
  539. char t[ISO_TIME_LEN+1];
  540. int r, i, n;
  541. bw_array_t *b;
  542. size_t len;
  543. /* opt (read|write)-history yyyy-mm-dd HH:MM:SS (n s) n,n,n,n,n... */
  544. len = (60+20*NUM_TOTALS)*2;
  545. buf = tor_malloc_zero(len);
  546. cp = buf;
  547. for (r=0;r<2;++r) {
  548. b = r?read_array:write_array;
  549. tor_assert(b);
  550. format_iso_time(t, b->next_period-NUM_SECS_BW_SUM_INTERVAL);
  551. tor_snprintf(cp, len-(cp-buf), "opt %s %s (%d s) ",
  552. r ? "read-history" : "write-history", t,
  553. NUM_SECS_BW_SUM_INTERVAL);
  554. cp += strlen(cp);
  555. if (b->num_maxes_set <= b->next_max_idx)
  556. /* We haven't been through the circular array yet; time starts at i=0.*/
  557. i = 0;
  558. else
  559. /* We've been around the array at least once. The next i to be
  560. overwritten is the oldest. */
  561. i = b->next_max_idx;
  562. for (n=0; n<b->num_maxes_set; ++n,++i) {
  563. while (i >= NUM_TOTALS) i -= NUM_TOTALS;
  564. if (n==(b->num_maxes_set-1))
  565. tor_snprintf(cp, len-(cp-buf), U64_FORMAT,
  566. U64_PRINTF_ARG(b->totals[i]));
  567. else
  568. tor_snprintf(cp, len-(cp-buf), U64_FORMAT",",
  569. U64_PRINTF_ARG(b->totals[i]));
  570. cp += strlen(cp);
  571. }
  572. strlcat(cp, "\n", len-(cp-buf));
  573. ++cp;
  574. }
  575. return buf;
  576. }
  577. /** A list of port numbers that have been used recently. */
  578. static smartlist_t *predicted_ports_list=NULL;
  579. /** The corresponding most recently used time for each port. */
  580. static smartlist_t *predicted_ports_times=NULL;
  581. /** DOCDOC */
  582. static void
  583. add_predicted_port(uint16_t port, time_t now)
  584. {
  585. /* XXXX we could just use uintptr_t here, I think. */
  586. uint16_t *tmp_port = tor_malloc(sizeof(uint16_t));
  587. time_t *tmp_time = tor_malloc(sizeof(time_t));
  588. *tmp_port = port;
  589. *tmp_time = now;
  590. rephist_total_alloc += sizeof(uint16_t) + sizeof(time_t);
  591. smartlist_add(predicted_ports_list, tmp_port);
  592. smartlist_add(predicted_ports_times, tmp_time);
  593. }
  594. /** DOCDOC */
  595. static void
  596. predicted_ports_init(void)
  597. {
  598. predicted_ports_list = smartlist_create();
  599. predicted_ports_times = smartlist_create();
  600. add_predicted_port(80, time(NULL)); /* add one to kickstart us */
  601. }
  602. /** DOCDOC */
  603. static void
  604. predicted_ports_free(void)
  605. {
  606. rephist_total_alloc -= smartlist_len(predicted_ports_list)*sizeof(uint16_t);
  607. SMARTLIST_FOREACH(predicted_ports_list, char *, cp, tor_free(cp));
  608. smartlist_free(predicted_ports_list);
  609. rephist_total_alloc -= smartlist_len(predicted_ports_times)*sizeof(time_t);
  610. SMARTLIST_FOREACH(predicted_ports_times, char *, cp, tor_free(cp));
  611. smartlist_free(predicted_ports_times);
  612. }
  613. /** Remember that <b>port</b> has been asked for as of time <b>now</b>.
  614. * This is used for predicting what sorts of streams we'll make in the
  615. * future and making exit circuits to anticipate that.
  616. */
  617. void
  618. rep_hist_note_used_port(uint16_t port, time_t now)
  619. {
  620. int i;
  621. uint16_t *tmp_port;
  622. time_t *tmp_time;
  623. tor_assert(predicted_ports_list);
  624. tor_assert(predicted_ports_times);
  625. if (!port) /* record nothing */
  626. return;
  627. for (i = 0; i < smartlist_len(predicted_ports_list); ++i) {
  628. tmp_port = smartlist_get(predicted_ports_list, i);
  629. tmp_time = smartlist_get(predicted_ports_times, i);
  630. if (*tmp_port == port) {
  631. *tmp_time = now;
  632. return;
  633. }
  634. }
  635. /* it's not there yet; we need to add it */
  636. add_predicted_port(port, now);
  637. }
  638. #define PREDICTED_CIRCS_RELEVANCE_TIME (3600) /* 1 hour */
  639. /** Return a pointer to the list of port numbers that
  640. * are likely to be asked for in the near future.
  641. *
  642. * The caller promises not to mess with it.
  643. */
  644. smartlist_t *
  645. rep_hist_get_predicted_ports(time_t now)
  646. {
  647. int i;
  648. uint16_t *tmp_port;
  649. time_t *tmp_time;
  650. tor_assert(predicted_ports_list);
  651. tor_assert(predicted_ports_times);
  652. /* clean out obsolete entries */
  653. for (i = 0; i < smartlist_len(predicted_ports_list); ++i) {
  654. tmp_time = smartlist_get(predicted_ports_times, i);
  655. if (*tmp_time + PREDICTED_CIRCS_RELEVANCE_TIME < now) {
  656. tmp_port = smartlist_get(predicted_ports_list, i);
  657. debug(LD_CIRC, "Expiring predicted port %d", *tmp_port);
  658. smartlist_del(predicted_ports_list, i);
  659. smartlist_del(predicted_ports_times, i);
  660. rephist_total_alloc -= sizeof(uint16_t)+sizeof(time_t);
  661. tor_free(tmp_port);
  662. tor_free(tmp_time);
  663. i--;
  664. }
  665. }
  666. return predicted_ports_list;
  667. }
  668. /** The user asked us to do a resolve. Rather than keeping track of
  669. * timings and such of resolves, we fake it for now by making treating
  670. * it the same way as a connection to port 80. This way we will continue
  671. * to have circuits lying around if the user only uses Tor for resolves.
  672. */
  673. void
  674. rep_hist_note_used_resolve(time_t now)
  675. {
  676. rep_hist_note_used_port(80, now);
  677. }
  678. #if 0
  679. int
  680. rep_hist_get_predicted_resolve(time_t now)
  681. {
  682. return 0;
  683. }
  684. #endif
  685. /** The last time at which we needed an internal circ. */
  686. static time_t predicted_internal_time = 0;
  687. /** The last time we needed an internal circ with good uptime. */
  688. static time_t predicted_internal_uptime_time = 0;
  689. /** The last time we needed an internal circ with good capacity. */
  690. static time_t predicted_internal_capacity_time = 0;
  691. /** Remember that we used an internal circ at time <b>now</b>. */
  692. void
  693. rep_hist_note_used_internal(time_t now, int need_uptime, int need_capacity)
  694. {
  695. predicted_internal_time = now;
  696. if (need_uptime)
  697. predicted_internal_uptime_time = now;
  698. if (need_capacity)
  699. predicted_internal_capacity_time = now;
  700. }
  701. /** Return 1 if we've used an internal circ recently; else return 0. */
  702. int
  703. rep_hist_get_predicted_internal(time_t now, int *need_uptime,
  704. int *need_capacity)
  705. {
  706. if (!predicted_internal_time) { /* initialize it */
  707. predicted_internal_time = now;
  708. predicted_internal_uptime_time = now;
  709. predicted_internal_capacity_time = now;
  710. }
  711. if (predicted_internal_time + PREDICTED_CIRCS_RELEVANCE_TIME < now)
  712. return 0; /* too long ago */
  713. if (predicted_internal_uptime_time + PREDICTED_CIRCS_RELEVANCE_TIME < now)
  714. *need_uptime = 1;
  715. if (predicted_internal_capacity_time + PREDICTED_CIRCS_RELEVANCE_TIME < now)
  716. *need_capacity = 1;
  717. return 1;
  718. }
  719. /** Free all storage held by the OR/link history caches, by the
  720. * bandwidth history arrays, or by the port history. */
  721. void
  722. rep_hist_free_all(void)
  723. {
  724. digestmap_free(history_map, free_or_history);
  725. tor_free(read_array);
  726. tor_free(write_array);
  727. predicted_ports_free();
  728. }