rephist.c 22 KB

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