rephist.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /* Copyright 2004-2005 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 obs[cur_obs_idx] */
  368. uint64_t max_total; /**< Largest value that total_obs has taken on in the current
  369. * period. */
  370. uint64_t total_in_period; /**< Total bytes transferred in the current
  371. * period. */
  372. /** When does the next period begin? */
  373. time_t next_period;
  374. /** Where in 'maxima' should the maximum bandwidth usage for the current
  375. * period be stored? */
  376. int next_max_idx;
  377. /** How many values in maxima/totals have been set ever? */
  378. int num_maxes_set;
  379. /** Circular array of the maximum
  380. * bandwidth-per-NUM_SECS_ROLLING_MEASURE usage for the last
  381. * NUM_TOTALS periods */
  382. uint64_t maxima[NUM_TOTALS];
  383. /** Circular array of the total bandwidth usage for the last NUM_TOTALS
  384. * periods */
  385. uint64_t totals[NUM_TOTALS];
  386. } bw_array_t;
  387. /** Shift the current period of b forward by one.
  388. */
  389. static void
  390. commit_max(bw_array_t *b)
  391. {
  392. /* Store total from current period. */
  393. b->totals[b->next_max_idx] = b->total_in_period;
  394. /* Store maximum from current period. */
  395. b->maxima[b->next_max_idx++] = b->max_total;
  396. /* Advance next_period and next_max_idx */
  397. b->next_period += NUM_SECS_BW_SUM_INTERVAL;
  398. if (b->next_max_idx == NUM_TOTALS)
  399. b->next_max_idx = 0;
  400. if (b->num_maxes_set < NUM_TOTALS)
  401. ++b->num_maxes_set;
  402. /* Reset max_total. */
  403. b->max_total = 0;
  404. /* Reset total_in_period. */
  405. b->total_in_period = 0;
  406. }
  407. /** Shift the current observation time of 'b' forward by one second.
  408. */
  409. static INLINE void
  410. advance_obs(bw_array_t *b)
  411. {
  412. int nextidx;
  413. uint64_t total;
  414. /* Calculate the total bandwidth for the last NUM_SECS_ROLLING_MEASURE
  415. * seconds; adjust max_total as needed.*/
  416. total = b->total_obs + b->obs[b->cur_obs_idx];
  417. if (total > b->max_total)
  418. b->max_total = total;
  419. nextidx = b->cur_obs_idx+1;
  420. if (nextidx == NUM_SECS_ROLLING_MEASURE)
  421. nextidx = 0;
  422. b->total_obs = total - b->obs[nextidx];
  423. b->obs[nextidx]=0;
  424. b->cur_obs_idx = nextidx;
  425. if (++b->cur_obs_time >= b->next_period)
  426. commit_max(b);
  427. }
  428. /** Add 'n' bytes to the number of bytes in b for second 'when'.
  429. */
  430. static INLINE void
  431. add_obs(bw_array_t *b, time_t when, uint64_t n)
  432. {
  433. /* Don't record data in the past. */
  434. if (when<b->cur_obs_time)
  435. return;
  436. /* If we're currently adding observations for an earlier second than
  437. * 'when', advance b->cur_obs_time and b->cur_obs_idx by an
  438. * appropriate number of seconds, and do all the other housekeeping */
  439. while (when>b->cur_obs_time)
  440. advance_obs(b);
  441. b->obs[b->cur_obs_idx] += n;
  442. b->total_in_period += n;
  443. }
  444. /** Allocate, initialize, and return a new bw_array.
  445. */
  446. static bw_array_t *
  447. bw_array_new(void)
  448. {
  449. bw_array_t *b;
  450. time_t start;
  451. b = tor_malloc_zero(sizeof(bw_array_t));
  452. rephist_total_alloc += sizeof(bw_array_t);
  453. start = time(NULL);
  454. b->cur_obs_time = start;
  455. b->next_period = start + NUM_SECS_BW_SUM_INTERVAL;
  456. return b;
  457. }
  458. static bw_array_t *read_array = NULL;
  459. static bw_array_t *write_array = NULL;
  460. /** Set up read_array and write_array
  461. */
  462. static void
  463. bw_arrays_init(void)
  464. {
  465. read_array = bw_array_new();
  466. write_array = bw_array_new();
  467. }
  468. /** We read <b>num_bytes</b> more bytes in second <b>when</b>.
  469. *
  470. * Add num_bytes to the current running total for <b>when</b>.
  471. *
  472. * <b>when</b> can go back to time, but it's safe to ignore calls
  473. * earlier than the latest <b>when</b> you've heard of.
  474. */
  475. void
  476. rep_hist_note_bytes_written(int num_bytes, time_t when)
  477. {
  478. /* Maybe a circular array for recent seconds, and step to a new point
  479. * every time a new second shows up. Or simpler is to just to have
  480. * a normal array and push down each item every second; it's short.
  481. */
  482. /* When a new second has rolled over, compute the sum of the bytes we've
  483. * seen over when-1 to when-1-NUM_SECS_ROLLING_MEASURE, and stick it
  484. * somewhere. See rep_hist_bandwidth_assess() below.
  485. */
  486. add_obs(write_array, when, num_bytes);
  487. }
  488. /** We wrote <b>num_bytes</b> more bytes in second <b>when</b>.
  489. * (like rep_hist_note_bytes_written() above)
  490. */
  491. void
  492. rep_hist_note_bytes_read(int num_bytes, time_t when)
  493. {
  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 uint64_t
  502. find_largest_max(bw_array_t *b)
  503. {
  504. int i;
  505. uint64_t max;
  506. max=0;
  507. for (i=0; i<NUM_TOTALS; ++i) {
  508. if (b->maxima[i]>max)
  509. max = b->maxima[i];
  510. }
  511. return max;
  512. }
  513. /**
  514. * Find the largest sums in the past NUM_SECS_BW_SUM_IS_VALID (roughly)
  515. * seconds. Find one sum for reading and one for writing. They don't have
  516. * to be at the same time).
  517. *
  518. * Return the smaller of these sums, divided by NUM_SECS_ROLLING_MEASURE.
  519. */
  520. int
  521. rep_hist_bandwidth_assess(void)
  522. {
  523. uint64_t w,r;
  524. r = find_largest_max(read_array);
  525. w = find_largest_max(write_array);
  526. if (r>w)
  527. return (int)(w/(double)NUM_SECS_ROLLING_MEASURE);
  528. else
  529. return (int)(r/(double)NUM_SECS_ROLLING_MEASURE);
  530. return 0;
  531. }
  532. /**
  533. * Print the bandwidth history of b (either read_array or write_array)
  534. * into the buffer pointed to by buf. The format is simply comma
  535. * separated numbers, from oldest to newest.
  536. *
  537. * It returns the number of bytes written.
  538. */
  539. static size_t
  540. rep_hist_fill_bandwidth_history(char *buf, size_t len, bw_array_t *b)
  541. {
  542. char *cp = buf;
  543. int i, n;
  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. }
  552. for (n=0; n<b->num_maxes_set; ++n,++i) {
  553. while (i >= NUM_TOTALS) i -= NUM_TOTALS;
  554. if (n==(b->num_maxes_set-1))
  555. tor_snprintf(cp, len-(cp-buf), U64_FORMAT,
  556. U64_PRINTF_ARG(b->totals[i]));
  557. else
  558. tor_snprintf(cp, len-(cp-buf), U64_FORMAT",",
  559. U64_PRINTF_ARG(b->totals[i]));
  560. cp += strlen(cp);
  561. }
  562. return cp-buf;
  563. }
  564. /**
  565. * Allocate and return lines for representing this server's bandwidth
  566. * history in its descriptor.
  567. */
  568. char *
  569. rep_hist_get_bandwidth_lines(void)
  570. {
  571. char *buf, *cp;
  572. char t[ISO_TIME_LEN+1];
  573. int r;
  574. bw_array_t *b;
  575. size_t len;
  576. /* opt (read|write)-history yyyy-mm-dd HH:MM:SS (n s) n,n,n,n,n... */
  577. len = (60+20*NUM_TOTALS)*2;
  578. buf = tor_malloc_zero(len);
  579. cp = buf;
  580. for (r=0;r<2;++r) {
  581. b = r?read_array:write_array;
  582. tor_assert(b);
  583. format_iso_time(t, b->next_period-NUM_SECS_BW_SUM_INTERVAL);
  584. tor_snprintf(cp, len-(cp-buf), "opt %s %s (%d s) ",
  585. r ? "read-history" : "write-history", t,
  586. NUM_SECS_BW_SUM_INTERVAL);
  587. cp += strlen(cp);
  588. cp += rep_hist_fill_bandwidth_history(cp, len-(cp-buf), b);
  589. strlcat(cp, "\n", len-(cp-buf));
  590. ++cp;
  591. }
  592. return buf;
  593. }
  594. /** Update <b>state</b> with the newest bandwidth history. */
  595. void
  596. rep_hist_update_state(or_state_t *state)
  597. {
  598. int len, r;
  599. char *buf, *cp;
  600. smartlist_t **s_values;
  601. time_t *s_begins;
  602. int *s_interval;
  603. bw_array_t *b;
  604. len = 20*NUM_TOTALS+1;
  605. buf = tor_malloc_zero(len);
  606. for (r=0;r<2;++r) {
  607. b = r?read_array:write_array;
  608. s_begins = r?&state->BWHistoryReadEnds :&state->BWHistoryWriteEnds;
  609. s_interval= r?&state->BWHistoryReadInterval:&state->BWHistoryWriteInterval;
  610. s_values = r?&state->BWHistoryReadValues :&state->BWHistoryWriteValues;
  611. *s_begins = b->next_period;
  612. *s_interval = NUM_SECS_BW_SUM_INTERVAL;
  613. if (*s_values) {
  614. SMARTLIST_FOREACH(*s_values, char *, cp, tor_free(cp));
  615. smartlist_free(*s_values);
  616. }
  617. cp = buf;
  618. cp += rep_hist_fill_bandwidth_history(cp, len, b);
  619. tor_snprintf(cp, len-(cp-buf), cp == buf ? U64_FORMAT : ","U64_FORMAT,
  620. U64_PRINTF_ARG(b->total_in_period));
  621. *s_values = smartlist_create();
  622. if (server_mode(get_options()))
  623. smartlist_split_string(*s_values, buf, ",", SPLIT_SKIP_SPACE, 0);
  624. }
  625. tor_free(buf);
  626. state->dirty = 1;
  627. }
  628. /** Set bandwidth history from our saved state.
  629. */
  630. int
  631. rep_hist_load_state(or_state_t *state, const char **err)
  632. {
  633. time_t s_begins, start;
  634. time_t now = time(NULL);
  635. uint64_t v;
  636. int r,i,ok;
  637. int all_ok = 1;
  638. int s_interval;
  639. smartlist_t *s_values;
  640. bw_array_t *b;
  641. /* Assert they already have been malloced */
  642. tor_assert(read_array && write_array);
  643. for (r=0;r<2;++r) {
  644. b = r?read_array:write_array;
  645. s_begins = r?state->BWHistoryReadEnds:state->BWHistoryWriteEnds;
  646. s_interval = r?state->BWHistoryReadInterval:state->BWHistoryWriteInterval;
  647. s_values = r?state->BWHistoryReadValues:state->BWHistoryWriteValues;
  648. if (s_values && s_begins >= now - NUM_SECS_BW_SUM_INTERVAL*NUM_TOTALS) {
  649. start = s_begins - s_interval*(smartlist_len(s_values));
  650. b->cur_obs_time = start;
  651. b->next_period = start + NUM_SECS_BW_SUM_INTERVAL;
  652. SMARTLIST_FOREACH(s_values, char *, cp, {
  653. v = tor_parse_uint64(cp, 10, 0, UINT64_MAX, &ok, NULL);
  654. if (!ok) {
  655. all_ok=0;
  656. notice(LD_GENERAL, "Could not parse '%s' into a number.'", cp);
  657. }
  658. add_obs(b, start, v);
  659. start += NUM_SECS_BW_SUM_INTERVAL;
  660. });
  661. }
  662. /* Clean up maxima and observed */
  663. /* Do we really want to zero this for the purpose of max capacity? */
  664. for (i=0; i<NUM_SECS_ROLLING_MEASURE; ++i) {
  665. b->obs[i] = 0;
  666. }
  667. b->total_obs = 0;
  668. for (i=0; i<NUM_TOTALS; ++i) {
  669. b->maxima[i] = 0;
  670. }
  671. b->max_total = 0;
  672. }
  673. if (!all_ok) {
  674. if (err)
  675. *err = "Parsing of bandwidth history values failed";
  676. /* and create fresh arrays */
  677. tor_free(read_array);
  678. tor_free(write_array);
  679. read_array = bw_array_new();
  680. write_array = bw_array_new();
  681. return -1;
  682. }
  683. return 0;
  684. }
  685. /** A list of port numbers that have been used recently. */
  686. static smartlist_t *predicted_ports_list=NULL;
  687. /** The corresponding most recently used time for each port. */
  688. static smartlist_t *predicted_ports_times=NULL;
  689. /** DOCDOC */
  690. static void
  691. add_predicted_port(uint16_t port, time_t now)
  692. {
  693. /* XXXX we could just use uintptr_t here, I think. */
  694. uint16_t *tmp_port = tor_malloc(sizeof(uint16_t));
  695. time_t *tmp_time = tor_malloc(sizeof(time_t));
  696. *tmp_port = port;
  697. *tmp_time = now;
  698. rephist_total_alloc += sizeof(uint16_t) + sizeof(time_t);
  699. smartlist_add(predicted_ports_list, tmp_port);
  700. smartlist_add(predicted_ports_times, tmp_time);
  701. }
  702. /** DOCDOC */
  703. static void
  704. predicted_ports_init(void)
  705. {
  706. predicted_ports_list = smartlist_create();
  707. predicted_ports_times = smartlist_create();
  708. add_predicted_port(80, time(NULL)); /* add one to kickstart us */
  709. }
  710. /** DOCDOC */
  711. static void
  712. predicted_ports_free(void)
  713. {
  714. rephist_total_alloc -= smartlist_len(predicted_ports_list)*sizeof(uint16_t);
  715. SMARTLIST_FOREACH(predicted_ports_list, char *, cp, tor_free(cp));
  716. smartlist_free(predicted_ports_list);
  717. rephist_total_alloc -= smartlist_len(predicted_ports_times)*sizeof(time_t);
  718. SMARTLIST_FOREACH(predicted_ports_times, char *, cp, tor_free(cp));
  719. smartlist_free(predicted_ports_times);
  720. }
  721. /** Remember that <b>port</b> has been asked for as of time <b>now</b>.
  722. * This is used for predicting what sorts of streams we'll make in the
  723. * future and making exit circuits to anticipate that.
  724. */
  725. void
  726. rep_hist_note_used_port(uint16_t port, time_t now)
  727. {
  728. int i;
  729. uint16_t *tmp_port;
  730. time_t *tmp_time;
  731. tor_assert(predicted_ports_list);
  732. tor_assert(predicted_ports_times);
  733. if (!port) /* record nothing */
  734. return;
  735. for (i = 0; i < smartlist_len(predicted_ports_list); ++i) {
  736. tmp_port = smartlist_get(predicted_ports_list, i);
  737. tmp_time = smartlist_get(predicted_ports_times, i);
  738. if (*tmp_port == port) {
  739. *tmp_time = now;
  740. return;
  741. }
  742. }
  743. /* it's not there yet; we need to add it */
  744. add_predicted_port(port, now);
  745. }
  746. #define PREDICTED_CIRCS_RELEVANCE_TIME (3600) /* 1 hour */
  747. /** Return a pointer to the list of port numbers that
  748. * are likely to be asked for in the near future.
  749. *
  750. * The caller promises not to mess with it.
  751. */
  752. smartlist_t *
  753. rep_hist_get_predicted_ports(time_t now)
  754. {
  755. int i;
  756. uint16_t *tmp_port;
  757. time_t *tmp_time;
  758. tor_assert(predicted_ports_list);
  759. tor_assert(predicted_ports_times);
  760. /* clean out obsolete entries */
  761. for (i = 0; i < smartlist_len(predicted_ports_list); ++i) {
  762. tmp_time = smartlist_get(predicted_ports_times, i);
  763. if (*tmp_time + PREDICTED_CIRCS_RELEVANCE_TIME < now) {
  764. tmp_port = smartlist_get(predicted_ports_list, i);
  765. debug(LD_CIRC, "Expiring predicted port %d", *tmp_port);
  766. smartlist_del(predicted_ports_list, i);
  767. smartlist_del(predicted_ports_times, i);
  768. rephist_total_alloc -= sizeof(uint16_t)+sizeof(time_t);
  769. tor_free(tmp_port);
  770. tor_free(tmp_time);
  771. i--;
  772. }
  773. }
  774. return predicted_ports_list;
  775. }
  776. /** The user asked us to do a resolve. Rather than keeping track of
  777. * timings and such of resolves, we fake it for now by making treating
  778. * it the same way as a connection to port 80. This way we will continue
  779. * to have circuits lying around if the user only uses Tor for resolves.
  780. */
  781. void
  782. rep_hist_note_used_resolve(time_t now)
  783. {
  784. rep_hist_note_used_port(80, now);
  785. }
  786. #if 0
  787. int
  788. rep_hist_get_predicted_resolve(time_t now)
  789. {
  790. return 0;
  791. }
  792. #endif
  793. /** The last time at which we needed an internal circ. */
  794. static time_t predicted_internal_time = 0;
  795. /** The last time we needed an internal circ with good uptime. */
  796. static time_t predicted_internal_uptime_time = 0;
  797. /** The last time we needed an internal circ with good capacity. */
  798. static time_t predicted_internal_capacity_time = 0;
  799. /** Remember that we used an internal circ at time <b>now</b>. */
  800. void
  801. rep_hist_note_used_internal(time_t now, int need_uptime, int need_capacity)
  802. {
  803. predicted_internal_time = now;
  804. if (need_uptime)
  805. predicted_internal_uptime_time = now;
  806. if (need_capacity)
  807. predicted_internal_capacity_time = now;
  808. }
  809. /** Return 1 if we've used an internal circ recently; else return 0. */
  810. int
  811. rep_hist_get_predicted_internal(time_t now, int *need_uptime,
  812. int *need_capacity)
  813. {
  814. if (!predicted_internal_time) { /* initialize it */
  815. predicted_internal_time = now;
  816. predicted_internal_uptime_time = now;
  817. predicted_internal_capacity_time = now;
  818. }
  819. if (predicted_internal_time + PREDICTED_CIRCS_RELEVANCE_TIME < now)
  820. return 0; /* too long ago */
  821. if (predicted_internal_uptime_time + PREDICTED_CIRCS_RELEVANCE_TIME < now)
  822. *need_uptime = 1;
  823. if (predicted_internal_capacity_time + PREDICTED_CIRCS_RELEVANCE_TIME < now)
  824. *need_capacity = 1;
  825. return 1;
  826. }
  827. /** Free all storage held by the OR/link history caches, by the
  828. * bandwidth history arrays, or by the port history. */
  829. void
  830. rep_hist_free_all(void)
  831. {
  832. digestmap_free(history_map, free_or_history);
  833. tor_free(read_array);
  834. tor_free(write_array);
  835. predicted_ports_free();
  836. }