rephist.c 18 KB

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