rephist.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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;
  15. /** History of an OR-\>OR link. */
  16. typedef struct link_history_t {
  17. /** When did we start tracking this list? */
  18. time_t since;
  19. /** When did we most recently note a change to this link */
  20. time_t changed;
  21. /** How many times did extending from OR1 to OR2 succeed? */
  22. unsigned long n_extend_ok;
  23. /** How many times did extending from OR1 to OR2 fail? */
  24. unsigned long n_extend_fail;
  25. } link_history_t;
  26. /** History of an OR. */
  27. typedef struct or_history_t {
  28. /** When did we start tracking this OR? */
  29. time_t since;
  30. /** When did we most recently note a change to this OR? */
  31. time_t changed;
  32. /** How many times did we successfully connect? */
  33. unsigned long n_conn_ok;
  34. /** How many times did we try to connect and fail?*/
  35. unsigned long n_conn_fail;
  36. /** How many seconds have we been connected to this OR before
  37. * 'up_since'? */
  38. unsigned long uptime;
  39. /** How many seconds have we been unable to connect to this OR before
  40. * 'down_since'? */
  41. unsigned long downtime;
  42. /** If nonzero, we have been connected since this time. */
  43. time_t up_since;
  44. /** If nonzero, we have been unable to connect since this time. */
  45. time_t down_since;
  46. /** Map from hex OR2 identity digest to a link_history_t for the link
  47. * from this OR to OR2. */
  48. strmap_t *link_history_map;
  49. } or_history_t;
  50. /** Map from hex OR identity digest to or_history_t. */
  51. static strmap_t *history_map = NULL;
  52. /** Return the or_history_t for the named OR, creating it if necessary.
  53. */
  54. static or_history_t *
  55. get_or_history(const char* id)
  56. {
  57. or_history_t *hist;
  58. char hexid[HEX_DIGEST_LEN+1];
  59. base16_encode(hexid, HEX_DIGEST_LEN+1, id, DIGEST_LEN);
  60. if (!strcmp(hexid, "0000000000000000000000000000000000000000"))
  61. return NULL;
  62. hist = (or_history_t*) strmap_get(history_map, hexid);
  63. if (!hist) {
  64. hist = tor_malloc_zero(sizeof(or_history_t));
  65. rephist_total_alloc += sizeof(or_history_t);
  66. hist->link_history_map = strmap_new();
  67. hist->since = hist->changed = time(NULL);
  68. strmap_set(history_map, hexid, 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. char to_hexid[HEX_DIGEST_LEN+1];
  82. orhist = get_or_history(from_id);
  83. if (!orhist)
  84. return NULL;
  85. base16_encode(to_hexid, HEX_DIGEST_LEN+1, to_id, DIGEST_LEN);
  86. if (!strcmp(to_hexid, "0000000000000000000000000000000000000000"))
  87. return NULL;
  88. lhist = (link_history_t*) strmap_get(orhist->link_history_map, to_hexid);
  89. if (!lhist) {
  90. lhist = tor_malloc_zero(sizeof(link_history_t));
  91. rephist_total_alloc += sizeof(link_history_t);
  92. lhist->since = lhist->changed = time(NULL);
  93. strmap_set(orhist->link_history_map, to_hexid, lhist);
  94. }
  95. return lhist;
  96. }
  97. /** Helper: free storage held by a single link history entry */
  98. static void
  99. _free_link_history(void *val)
  100. {
  101. rephist_total_alloc -= sizeof(link_history_t);
  102. tor_free(val);
  103. }
  104. /** Helper: free storage held by a single OR history entry */
  105. static void
  106. free_or_history(void *_hist)
  107. {
  108. or_history_t *hist = _hist;
  109. strmap_free(hist->link_history_map, _free_link_history);
  110. rephist_total_alloc -= sizeof(or_history_t);
  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 = strmap_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. strmap_iter_t *lhist_it;
  256. strmap_iter_t *orhist_it;
  257. const char *name1, *name2, *hexdigest1, *hexdigest2;
  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-24*60*60);
  268. log(severity, "--------------- Dumping history information:");
  269. for (orhist_it = strmap_iter_init(history_map); !strmap_iter_done(orhist_it);
  270. orhist_it = strmap_iter_next(history_map,orhist_it)) {
  271. strmap_iter_get(orhist_it, &hexdigest1, &or_history_p);
  272. or_history = (or_history_t*) or_history_p;
  273. if ((r = router_get_by_hexdigest(hexdigest1)))
  274. name1 = r->nickname;
  275. else
  276. name1 = "(unknown)";
  277. update_or_history(or_history, now);
  278. upt = or_history->uptime;
  279. downt = or_history->downtime;
  280. if (upt+downt) {
  281. uptime = ((double)upt) / (upt+downt);
  282. } else {
  283. uptime=1.0;
  284. }
  285. log(severity,
  286. "OR %s [%s]: %ld/%ld good connections; uptime %ld/%ld sec (%.2f%%)",
  287. name1, hexdigest1,
  288. or_history->n_conn_ok, or_history->n_conn_fail+or_history->n_conn_ok,
  289. upt, upt+downt, uptime*100.0);
  290. if (!strmap_isempty(or_history->link_history_map)) {
  291. strlcpy(buffer, " Extend attempts: ", sizeof(buffer));
  292. len = strlen(buffer);
  293. for (lhist_it = strmap_iter_init(or_history->link_history_map);
  294. !strmap_iter_done(lhist_it);
  295. lhist_it = strmap_iter_next(or_history->link_history_map, lhist_it)) {
  296. strmap_iter_get(lhist_it, &hexdigest2, &link_history_p);
  297. if ((r = router_get_by_hexdigest(hexdigest2)))
  298. name2 = r->nickname;
  299. else
  300. name2 = "(unknown)";
  301. link_history = (link_history_t*) link_history_p;
  302. ret = tor_snprintf(buffer+len, 2048-len, "%s(%ld/%ld); ", name2,
  303. link_history->n_extend_ok,
  304. link_history->n_extend_ok+link_history->n_extend_fail);
  305. if (ret<0)
  306. break;
  307. else
  308. len += ret;
  309. }
  310. log(severity, "%s", buffer);
  311. }
  312. }
  313. }
  314. /** Remove history info for routers/links that haven't changed since
  315. * <b>before</b> */
  316. void
  317. rep_history_clean(time_t before)
  318. {
  319. or_history_t *or_history;
  320. link_history_t *link_history;
  321. void *or_history_p, *link_history_p;
  322. strmap_iter_t *orhist_it, *lhist_it;
  323. const char *hd1, *hd2;
  324. orhist_it = strmap_iter_init(history_map);
  325. while (!strmap_iter_done(orhist_it)) {
  326. strmap_iter_get(orhist_it, &hd1, &or_history_p);
  327. or_history = or_history_p;
  328. if (or_history->changed < before) {
  329. free_or_history(or_history);
  330. orhist_it = strmap_iter_next_rmv(history_map, orhist_it);
  331. continue;
  332. }
  333. for (lhist_it = strmap_iter_init(or_history->link_history_map);
  334. !strmap_iter_done(lhist_it); ) {
  335. strmap_iter_get(lhist_it, &hd2, &link_history_p);
  336. link_history = link_history_p;
  337. if (link_history->changed < before) {
  338. rephist_total_alloc -= sizeof(link_history_t);
  339. tor_free(link_history);
  340. lhist_it = strmap_iter_next_rmv(or_history->link_history_map,lhist_it);
  341. continue;
  342. }
  343. lhist_it = strmap_iter_next(or_history->link_history_map,lhist_it);
  344. }
  345. orhist_it = strmap_iter_next(history_map, orhist_it);
  346. }
  347. }
  348. #if 0
  349. void
  350. write_rep_history(const char *filename)
  351. {
  352. FILE *f = NULL;
  353. char *tmpfile;
  354. int completed = 0;
  355. or_history_t *or_history;
  356. link_history_t *link_history;
  357. strmap_iter_t *lhist_it;
  358. strmap_iter_t *orhist_it;
  359. void *or_history_p, *link_history_p;
  360. const char *name1;
  361. tmpfile = tor_malloc(strlen(filename)+5);
  362. tor_snprintf(tmpfile, strlen(filename)+5, "%s_tmp", filename);
  363. f = fopen(tmpfile, "w");
  364. if (!f) goto done;
  365. for (orhist_it = strmap_iter_init(history_map); !strmap_iter_done(orhist_it);
  366. orhist_it = strmap_iter_next(history_map,orhist_it)) {
  367. strmap_iter_get(orhist_it, &name1, &or_history_p);
  368. or_history = (or_history_t*) or_history_p;
  369. fprintf(f, "link %s connected:u%ld failed:%uld uptime:%uld",
  370. name1, or_history->since1,
  371. }
  372. done:
  373. if (f)
  374. fclose(f);
  375. if (completed)
  376. replace_file(filename, tmpfile);
  377. else
  378. unlink(tmpfile);
  379. tor_free(tmpfile);
  380. }
  381. #endif
  382. #define NUM_SECS_ROLLING_MEASURE 10
  383. #define NUM_SECS_BW_SUM_IS_VALID (24*60*60) /* one day */
  384. #define NUM_SECS_BW_SUM_INTERVAL (15*60)
  385. #define NUM_TOTALS (NUM_SECS_BW_SUM_IS_VALID/NUM_SECS_BW_SUM_INTERVAL)
  386. /**
  387. * Structure to track bandwidth use, and remember the maxima for a given
  388. * time period.
  389. */
  390. typedef struct bw_array_t {
  391. /** Observation array: Total number of bytes transferred in each of the last
  392. * NUM_SECS_ROLLING_MEASURE seconds. This is used as a circular array. */
  393. int obs[NUM_SECS_ROLLING_MEASURE];
  394. int cur_obs_idx; /**< Current position in obs. */
  395. time_t cur_obs_time; /**< Time represented in obs[cur_obs_idx] */
  396. int total_obs; /**< Total for all members of obs except obs[cur_obs_idx] */
  397. int max_total; /**< Largest value that total_obs has taken on in the current
  398. * period. */
  399. int total_in_period; /**< Total bytes transferred in the current period. */
  400. /** When does the next period begin? */
  401. time_t next_period;
  402. /** Where in 'maxima' should the maximum bandwidth usage for the current
  403. * period be stored? */
  404. int next_max_idx;
  405. /** How many values in maxima/totals have been set ever? */
  406. int num_maxes_set;
  407. /** Circular array of the maximum
  408. * bandwidth-per-NUM_SECS_ROLLING_MEASURE usage for the last
  409. * NUM_TOTALS periods */
  410. int maxima[NUM_TOTALS];
  411. /** Circular array of the total bandwidth usage for the last NUM_TOTALS
  412. * periods */
  413. int totals[NUM_TOTALS];
  414. } bw_array_t;
  415. /** Shift the current period of b forward by one.
  416. */
  417. static void
  418. commit_max(bw_array_t *b)
  419. {
  420. /* Store total from current period. */
  421. b->totals[b->next_max_idx] = b->total_in_period;
  422. /* Store maximum from current period. */
  423. b->maxima[b->next_max_idx++] = b->max_total;
  424. /* Advance next_period and next_max_idx */
  425. b->next_period += NUM_SECS_BW_SUM_INTERVAL;
  426. if (b->next_max_idx == NUM_TOTALS)
  427. b->next_max_idx = 0;
  428. if (b->num_maxes_set < NUM_TOTALS)
  429. ++b->num_maxes_set;
  430. /* Reset max_total. */
  431. b->max_total = 0;
  432. /* Reset total_in_period. */
  433. b->total_in_period = 0;
  434. }
  435. /** Shift the current observation time of 'b' forward by one second.
  436. */
  437. static INLINE void
  438. advance_obs(bw_array_t *b)
  439. {
  440. int nextidx;
  441. int total;
  442. /* Calculate the total bandwidth for the last NUM_SECS_ROLLING_MEASURE
  443. * seconds; adjust max_total as needed.*/
  444. total = b->total_obs + b->obs[b->cur_obs_idx];
  445. if (total > b->max_total)
  446. b->max_total = total;
  447. nextidx = b->cur_obs_idx+1;
  448. if (nextidx == NUM_SECS_ROLLING_MEASURE)
  449. nextidx = 0;
  450. b->total_obs = total - b->obs[nextidx];
  451. b->obs[nextidx]=0;
  452. b->cur_obs_idx = nextidx;
  453. if (++b->cur_obs_time >= b->next_period)
  454. commit_max(b);
  455. }
  456. /** Add 'n' bytes to the number of bytes in b for second 'when'.
  457. */
  458. static INLINE void
  459. add_obs(bw_array_t *b, time_t when, int n)
  460. {
  461. /* Don't record data in the past. */
  462. if (when<b->cur_obs_time)
  463. return;
  464. /* If we're currently adding observations for an earlier second than
  465. * 'when', advance b->cur_obs_time and b->cur_obs_idx by an
  466. * appropriate number of seconds, and do all the other housekeeping */
  467. while (when>b->cur_obs_time)
  468. advance_obs(b);
  469. b->obs[b->cur_obs_idx] += n;
  470. b->total_in_period += n;
  471. }
  472. /** Allocate, initialize, and return a new bw_array.
  473. */
  474. static bw_array_t *bw_array_new(void)
  475. {
  476. bw_array_t *b;
  477. time_t start;
  478. b = tor_malloc_zero(sizeof(bw_array_t));
  479. rephist_total_alloc += sizeof(bw_array_t);
  480. start = time(NULL);
  481. b->cur_obs_time = start;
  482. b->next_period = start + NUM_SECS_BW_SUM_INTERVAL;
  483. return b;
  484. }
  485. static bw_array_t *read_array = NULL;
  486. static bw_array_t *write_array = NULL;
  487. /** Set up read_array and write_array
  488. */
  489. static void
  490. bw_arrays_init(void)
  491. {
  492. read_array = bw_array_new();
  493. write_array = bw_array_new();
  494. }
  495. /** We read <b>num_bytes</b> more bytes in second <b>when</b>.
  496. *
  497. * Add num_bytes to the current running total for <b>when</b>.
  498. *
  499. * <b>when</b> can go back to time, but it's safe to ignore calls
  500. * earlier than the latest <b>when</b> you've heard of.
  501. */
  502. void
  503. rep_hist_note_bytes_written(int num_bytes, time_t when)
  504. {
  505. /* Maybe a circular array for recent seconds, and step to a new point
  506. * every time a new second shows up. Or simpler is to just to have
  507. * a normal array and push down each item every second; it's short.
  508. */
  509. /* When a new second has rolled over, compute the sum of the bytes we've
  510. * seen over when-1 to when-1-NUM_SECS_ROLLING_MEASURE, and stick it
  511. * somewhere. See rep_hist_bandwidth_assess() below.
  512. */
  513. add_obs(write_array, when, num_bytes);
  514. }
  515. /** We wrote <b>num_bytes</b> more bytes in second <b>when</b>.
  516. * (like rep_hist_note_bytes_written() above)
  517. */
  518. void
  519. rep_hist_note_bytes_read(int num_bytes, time_t when)
  520. {
  521. /* if we're smart, we can make this func and the one above share code */
  522. add_obs(read_array, when, num_bytes);
  523. }
  524. /** Helper: Return the largest value in b->maxima. (This is equal to the
  525. * most bandwidth used in any NUM_SECS_ROLLING_MEASURE period for the last
  526. * NUM_SECS_BW_SUM_IS_VALID seconds.)
  527. */
  528. static int
  529. find_largest_max(bw_array_t *b)
  530. {
  531. int i,max;
  532. max=0;
  533. for (i=0; i<NUM_TOTALS; ++i) {
  534. if (b->maxima[i]>max)
  535. max = b->maxima[i];
  536. }
  537. return max;
  538. }
  539. /**
  540. * Find the largest sums in the past NUM_SECS_BW_SUM_IS_VALID (roughly)
  541. * seconds. Find one sum for reading and one for writing. They don't have
  542. * to be at the same time).
  543. *
  544. * Return the smaller of these sums, divided by NUM_SECS_ROLLING_MEASURE.
  545. */
  546. int
  547. rep_hist_bandwidth_assess(void)
  548. {
  549. int w,r;
  550. r = find_largest_max(read_array);
  551. w = find_largest_max(write_array);
  552. if (r>w)
  553. return (int)(w/(double)NUM_SECS_ROLLING_MEASURE);
  554. else
  555. return (int)(r/(double)NUM_SECS_ROLLING_MEASURE);
  556. return 0;
  557. }
  558. /**
  559. * Allocate and return lines for representing this server's bandwidth
  560. * history in its descriptor.
  561. */
  562. char *
  563. rep_hist_get_bandwidth_lines(void)
  564. {
  565. char *buf, *cp;
  566. char t[ISO_TIME_LEN+1];
  567. int r, i, n;
  568. bw_array_t *b;
  569. size_t len;
  570. /* opt (read|write)-history yyyy-mm-dd HH:MM:SS (n s) n,n,n,n,n... */
  571. len = (60+12*NUM_TOTALS)*2;
  572. buf = tor_malloc_zero(len);
  573. cp = buf;
  574. for (r=0;r<2;++r) {
  575. b = r?read_array:write_array;
  576. tor_assert(b);
  577. format_iso_time(t, b->next_period-NUM_SECS_BW_SUM_INTERVAL);
  578. tor_snprintf(cp, len-(cp-buf), "opt %s %s (%d s) ", r?"read-history ":"write-history", t,
  579. NUM_SECS_BW_SUM_INTERVAL);
  580. cp += strlen(cp);
  581. if (b->num_maxes_set <= b->next_max_idx)
  582. /* We haven't been through the circular array yet; time starts at i=0.*/
  583. i = 0;
  584. else
  585. /* We've been around the array at least once. The next i to be
  586. overwritten is the oldest. */
  587. i = b->next_max_idx;
  588. for (n=0; n<b->num_maxes_set; ++n,++i) {
  589. while (i >= NUM_TOTALS) i -= NUM_TOTALS;
  590. if (n==(b->num_maxes_set-1))
  591. tor_snprintf(cp, len-(cp-buf), "%d", b->totals[i]);
  592. else
  593. tor_snprintf(cp, len-(cp-buf), "%d,", b->totals[i]);
  594. cp += strlen(cp);
  595. }
  596. strlcat(cp, "\n", len-(cp-buf));
  597. ++cp;
  598. }
  599. return buf;
  600. }
  601. /** A list of port numbers that have been used recently. */
  602. static smartlist_t *predicted_ports_list=NULL;
  603. /** The corresponding most recently used time for each port. */
  604. static smartlist_t *predicted_ports_times=NULL;
  605. /** DOCDOC */
  606. static void
  607. add_predicted_port(uint16_t port, time_t now)
  608. {
  609. /* XXXX we could just use uintptr_t here, I think. */
  610. uint16_t *tmp_port = tor_malloc(sizeof(uint16_t));
  611. time_t *tmp_time = tor_malloc(sizeof(time_t));
  612. *tmp_port = port;
  613. *tmp_time = now;
  614. rephist_total_alloc += sizeof(uint16_t) + sizeof(time_t);
  615. smartlist_add(predicted_ports_list, tmp_port);
  616. smartlist_add(predicted_ports_times, tmp_time);
  617. }
  618. /** DOCDOC */
  619. static void
  620. predicted_ports_init(void)
  621. {
  622. predicted_ports_list = smartlist_create();
  623. predicted_ports_times = smartlist_create();
  624. add_predicted_port(80, time(NULL)); /* add one to kickstart us */
  625. }
  626. /** DOCDOC */
  627. static void
  628. predicted_ports_free(void)
  629. {
  630. rephist_total_alloc -= smartlist_len(predicted_ports_list)*sizeof(uint16_t);
  631. SMARTLIST_FOREACH(predicted_ports_list, char *, cp, tor_free(cp));
  632. smartlist_free(predicted_ports_list);
  633. rephist_total_alloc -= smartlist_len(predicted_ports_times)*sizeof(time_t);
  634. SMARTLIST_FOREACH(predicted_ports_times, char *, cp, tor_free(cp));
  635. smartlist_free(predicted_ports_times);
  636. }
  637. /** Remember that <b>port</b> has been asked for as of time <b>now</b>.
  638. * This is used for predicting what sorts of streams we'll make in the
  639. * future and making circuits to anticipate that.
  640. */
  641. void
  642. rep_hist_note_used_port(uint16_t port, time_t now)
  643. {
  644. int i;
  645. uint16_t *tmp_port;
  646. time_t *tmp_time;
  647. tor_assert(predicted_ports_list);
  648. tor_assert(predicted_ports_times);
  649. if (!port) /* record nothing */
  650. return;
  651. for (i = 0; i < smartlist_len(predicted_ports_list); ++i) {
  652. tmp_port = smartlist_get(predicted_ports_list, i);
  653. tmp_time = smartlist_get(predicted_ports_times, i);
  654. if (*tmp_port == port) {
  655. *tmp_time = now;
  656. return;
  657. }
  658. }
  659. /* it's not there yet; we need to add it */
  660. add_predicted_port(port, now);
  661. }
  662. #define PREDICTED_CIRCS_RELEVANCE_TIME (3600) /* 1 hour */
  663. /** Return a pointer to the list of port numbers that
  664. * are likely to be asked for in the near future.
  665. *
  666. * The caller promises not to mess with it.
  667. */
  668. smartlist_t *
  669. rep_hist_get_predicted_ports(time_t now)
  670. {
  671. int i;
  672. uint16_t *tmp_port;
  673. time_t *tmp_time;
  674. tor_assert(predicted_ports_list);
  675. tor_assert(predicted_ports_times);
  676. /* clean out obsolete entries */
  677. for (i = 0; i < smartlist_len(predicted_ports_list); ++i) {
  678. tmp_time = smartlist_get(predicted_ports_times, i);
  679. if (*tmp_time + PREDICTED_CIRCS_RELEVANCE_TIME < now) {
  680. tmp_port = smartlist_get(predicted_ports_list, i);
  681. log_fn(LOG_DEBUG, "Expiring predicted port %d", *tmp_port);
  682. smartlist_del(predicted_ports_list, i);
  683. smartlist_del(predicted_ports_times, i);
  684. rephist_total_alloc -= sizeof(uint16_t)+sizeof(time_t);
  685. tor_free(tmp_port);
  686. tor_free(tmp_time);
  687. i--;
  688. }
  689. }
  690. return predicted_ports_list;
  691. }
  692. /** The last time at which we needed an internal circ. */
  693. static time_t predicted_hidserv_time = 0;
  694. /** The last time we needed an internal circ with good uptime. */
  695. static time_t predicted_hidserv_uptime_time = 0;
  696. /** The last time we needed an internal circ with good capacity. */
  697. static time_t predicted_hidserv_capacity_time = 0;
  698. /** Remember that we used an internal circ at time <b>now</b>. */
  699. void
  700. rep_hist_note_used_hidserv(time_t now, int need_uptime, int need_capacity)
  701. {
  702. predicted_hidserv_time = now;
  703. if (need_uptime)
  704. predicted_hidserv_uptime_time = now;
  705. if (need_capacity)
  706. predicted_hidserv_capacity_time = now;
  707. }
  708. /** Return 1 if we've used an internal circ recently; else return 0. */
  709. int
  710. rep_hist_get_predicted_hidserv(time_t now, int *need_uptime, int *need_capacity)
  711. {
  712. if (!predicted_hidserv_time) /* initialize it */
  713. predicted_hidserv_time = now;
  714. if (predicted_hidserv_time + PREDICTED_CIRCS_RELEVANCE_TIME < now)
  715. return 0; /* too long ago */
  716. if (predicted_hidserv_uptime_time + PREDICTED_CIRCS_RELEVANCE_TIME < now)
  717. *need_uptime = 1;
  718. if (predicted_hidserv_capacity_time + PREDICTED_CIRCS_RELEVANCE_TIME < now)
  719. *need_capacity = 1;
  720. return 1;
  721. }
  722. /* not used yet */
  723. void rep_hist_note_used_resolve(time_t now) { }
  724. int rep_hist_get_predicted_resolve(time_t now) { return 0; }
  725. /** Free all storage held by the OR/link history caches, by the
  726. * bandwidth history arrays, or by the port history. */
  727. void
  728. rep_hist_free_all(void)
  729. {
  730. strmap_free(history_map, free_or_history);
  731. tor_free(read_array);
  732. tor_free(write_array);
  733. predicted_ports_free();
  734. }