rephist.c 26 KB

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