rephist.c 23 KB

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