rephist.c 23 KB

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