rephist.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /* Copyright 2004 Roger Dingledine */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /**
  5. * \file rephist.c
  6. * \brief Basic history functionality for reputation module.
  7. **/
  8. #include "or.h"
  9. static void bw_arrays_init(void);
  10. /** History of an OR-\>OR link. */
  11. typedef struct link_history_t {
  12. /** When did we start tracking this list? */
  13. time_t since;
  14. /** How many times did extending from OR1 to OR2 succeeed? */
  15. unsigned long n_extend_ok;
  16. /** How many times did extending from OR1 to OR2 fail? */
  17. unsigned long n_extend_fail;
  18. } link_history_t;
  19. /** History of an OR. */
  20. typedef struct or_history_t {
  21. /** When did we start tracking this OR? */
  22. time_t since;
  23. /** How many times did we successfully connect? */
  24. unsigned long n_conn_ok;
  25. /** How many times did we try to connect and fail?*/
  26. unsigned long n_conn_fail;
  27. /** How many seconds have we been connected to this OR before
  28. * 'up_since'? */
  29. unsigned long uptime;
  30. /** How many seconds have we been unable to connect to this OR before
  31. * 'down_since'? */
  32. unsigned long downtime;
  33. /** If nonzero, we have been connected since this time. */
  34. time_t up_since;
  35. /** If nonzero, we have been unable to connect since this time. */
  36. time_t down_since;
  37. /** Map from hex OR2 identity digest to a link_history_t for the link
  38. * from this OR to OR2. */
  39. strmap_t *link_history_map;
  40. } or_history_t;
  41. /** Map from hex OR identity digest to or_history_t. */
  42. static strmap_t *history_map = NULL;
  43. /** Return the or_history_t for the named OR, creating it if necessary.
  44. */
  45. static or_history_t *get_or_history(const char* id)
  46. {
  47. or_history_t *hist;
  48. char hexid[HEX_DIGEST_LEN+1];
  49. base16_encode(hexid, HEX_DIGEST_LEN+1, id, DIGEST_LEN);
  50. hist = (or_history_t*) strmap_get(history_map, hexid);
  51. if (!hist) {
  52. hist = tor_malloc_zero(sizeof(or_history_t));
  53. hist->link_history_map = strmap_new();
  54. hist->since = time(NULL);
  55. strmap_set(history_map, hexid, hist);
  56. }
  57. return hist;
  58. }
  59. /** Return the link_history_t for the link from the first named OR to
  60. * the second, creating it if necessary. (ORs are identified by
  61. * identity digest)
  62. */
  63. static link_history_t *get_link_history(const char *from_id,
  64. const char *to_id)
  65. {
  66. or_history_t *orhist;
  67. link_history_t *lhist;
  68. char to_hexid[HEX_DIGEST_LEN+1];
  69. orhist = get_or_history(from_id);
  70. base16_encode(to_hexid, HEX_DIGEST_LEN+1, to_id, DIGEST_LEN);
  71. lhist = (link_history_t*) strmap_get(orhist->link_history_map, to_hexid);
  72. if (!lhist) {
  73. lhist = tor_malloc_zero(sizeof(link_history_t));
  74. lhist->since = time(NULL);
  75. strmap_set(orhist->link_history_map, to_hexid, lhist);
  76. }
  77. return lhist;
  78. }
  79. /** Update an or_history_t object <b>hist</b> so that its uptime/downtime
  80. * count is up-to-date as of <b>when</b>.
  81. */
  82. static void update_or_history(or_history_t *hist, time_t when)
  83. {
  84. tor_assert(hist);
  85. if (hist->up_since) {
  86. tor_assert(!hist->down_since);
  87. hist->uptime += (when - hist->up_since);
  88. hist->up_since = when;
  89. } else if (hist->down_since) {
  90. hist->downtime += (when - hist->down_since);
  91. hist->down_since = when;
  92. }
  93. }
  94. /** Initialize the static data structures for tracking history.
  95. */
  96. void rep_hist_init(void)
  97. {
  98. history_map = strmap_new();
  99. bw_arrays_init();
  100. }
  101. /** Remember that an attempt to connect to the OR with identity digest
  102. * <b>id</b> failed at <b>when</b>.
  103. */
  104. void rep_hist_note_connect_failed(const char* id, time_t when)
  105. {
  106. or_history_t *hist;
  107. hist = get_or_history(id);
  108. ++hist->n_conn_fail;
  109. if (hist->up_since) {
  110. hist->uptime += (when - hist->up_since);
  111. hist->up_since = 0;
  112. }
  113. if (!hist->down_since)
  114. hist->down_since = when;
  115. }
  116. /** Remember that an attempt to connect to the OR with identity digest
  117. * <b>id</b> succeeded at <b>when</b>.
  118. */
  119. void rep_hist_note_connect_succeeded(const char* id, time_t when)
  120. {
  121. or_history_t *hist;
  122. hist = get_or_history(id);
  123. ++hist->n_conn_ok;
  124. if (hist->down_since) {
  125. hist->downtime += (when - hist->down_since);
  126. hist->down_since = 0;
  127. }
  128. if (!hist->up_since)
  129. hist->up_since = when;
  130. }
  131. /** Remember that we intentionally closed our connection to the OR
  132. * with identity digest <b>id</b> at <b>when</b>.
  133. */
  134. void rep_hist_note_disconnect(const char* id, time_t when)
  135. {
  136. or_history_t *hist;
  137. hist = get_or_history(id);
  138. ++hist->n_conn_ok;
  139. if (hist->up_since) {
  140. hist->uptime += (when - hist->up_since);
  141. hist->up_since = 0;
  142. }
  143. }
  144. /** Remember that our connection to the OR with identity digest
  145. * <b>id</b> had an error and stopped working at <b>when</b>.
  146. */
  147. void rep_hist_note_connection_died(const char* id, time_t when)
  148. {
  149. or_history_t *hist;
  150. if(!id) {
  151. /* XXXX008 not so. */
  152. /* If conn has no nickname, it's either an OP, or it is an OR
  153. * which didn't complete its handshake (or did and was unapproved).
  154. * Ignore it.
  155. */
  156. return;
  157. }
  158. hist = get_or_history(id);
  159. if (hist->up_since) {
  160. hist->uptime += (when - hist->up_since);
  161. hist->up_since = 0;
  162. }
  163. if (!hist->down_since)
  164. hist->down_since = when;
  165. }
  166. /** Remember that we successfully extended from the OR with identity
  167. * digest <b>from_id</b> to the OR with identity digest
  168. * <b>to_name</b>.
  169. */
  170. void rep_hist_note_extend_succeeded(const char *from_id,
  171. const char *to_id)
  172. {
  173. link_history_t *hist;
  174. /* log_fn(LOG_WARN, "EXTEND SUCCEEDED: %s->%s",from_name,to_name); */
  175. hist = get_link_history(from_id, to_id);
  176. ++hist->n_extend_ok;
  177. }
  178. /** Remember that we tried to extend from the OR with identity digest
  179. * <b>from_id</b> to the OR with identity digest <b>to_name</b>, but
  180. * failed.
  181. */
  182. void rep_hist_note_extend_failed(const char *from_id, const char *to_id)
  183. {
  184. link_history_t *hist;
  185. /* log_fn(LOG_WARN, "EXTEND FAILED: %s->%s",from_name,to_name); */
  186. hist = get_link_history(from_id, to_id);
  187. ++hist->n_extend_fail;
  188. }
  189. /** Log all the reliability data we have rememberred, with the chosen
  190. * severity.
  191. */
  192. void rep_hist_dump_stats(time_t now, int severity)
  193. {
  194. strmap_iter_t *lhist_it;
  195. strmap_iter_t *orhist_it;
  196. const char *name1, *name2, *hexdigest1, *hexdigest2;
  197. or_history_t *or_history;
  198. link_history_t *link_history;
  199. void *or_history_p, *link_history_p;
  200. double uptime;
  201. char buffer[2048];
  202. int len;
  203. unsigned long upt, downt;
  204. routerinfo_t *r;
  205. log(severity, "--------------- Dumping history information:");
  206. for (orhist_it = strmap_iter_init(history_map); !strmap_iter_done(orhist_it);
  207. orhist_it = strmap_iter_next(history_map,orhist_it)) {
  208. strmap_iter_get(orhist_it, &hexdigest1, &or_history_p);
  209. or_history = (or_history_t*) or_history_p;
  210. if ((r = router_get_by_hexdigest(hexdigest1)))
  211. name1 = r->nickname;
  212. else
  213. name1 = "(unknown)";
  214. update_or_history(or_history, now);
  215. upt = or_history->uptime;
  216. downt = or_history->downtime;
  217. if (upt+downt) {
  218. uptime = ((double)upt) / (upt+downt);
  219. } else {
  220. uptime=1.0;
  221. }
  222. log(severity,
  223. "OR %s [%s]: %ld/%ld good connections; uptime %ld/%ld sec (%.2f%%)",
  224. name1, hexdigest1,
  225. or_history->n_conn_ok, or_history->n_conn_fail+or_history->n_conn_ok,
  226. upt, upt+downt, uptime*100.0);
  227. strcpy(buffer, " Good extend attempts: ");
  228. len = strlen(buffer);
  229. for (lhist_it = strmap_iter_init(or_history->link_history_map);
  230. !strmap_iter_done(lhist_it);
  231. lhist_it = strmap_iter_next(or_history->link_history_map, lhist_it)) {
  232. strmap_iter_get(lhist_it, &hexdigest2, &link_history_p);
  233. if ((r = router_get_by_hexdigest(hexdigest2)))
  234. name2 = r->nickname;
  235. else
  236. name2 = "(unknown)";
  237. link_history = (link_history_t*) link_history_p;
  238. len += snprintf(buffer+len, 2048-len, "%s(%ld/%ld); ", name2,
  239. link_history->n_extend_ok,
  240. link_history->n_extend_ok+link_history->n_extend_fail);
  241. if (len >= 2048) {
  242. buffer[2047]='\0';
  243. break;
  244. }
  245. }
  246. log(severity, buffer);
  247. }
  248. }
  249. #if 0
  250. void write_rep_history(const char *filename)
  251. {
  252. FILE *f = NULL;
  253. char *tmpfile;
  254. int completed = 0;
  255. or_history_t *or_history;
  256. link_history_t *link_history;
  257. strmap_iter_t *lhist_it;
  258. strmap_iter_t *orhist_it;
  259. void *or_history_p, *link_history_p;
  260. const char *name1;
  261. tmpfile = tor_malloc(strlen(filename)+5);
  262. strcpy(tmpfile, filename);
  263. strcat(tmpfile, "_tmp");
  264. f = fopen(tmpfile, "w");
  265. if (!f) goto done;
  266. for (orhist_it = strmap_iter_init(history_map); !strmap_iter_done(orhist_it);
  267. orhist_it = strmap_iter_next(history_map,orhist_it)) {
  268. strmap_iter_get(orhist_it, &name1, &or_history_p);
  269. or_history = (or_history_t*) or_history_p;
  270. fprintf(f, "link %s connected:u%ld failed:%uld uptime:%uld",
  271. name1, or_history->since1,
  272. }
  273. done:
  274. if (f)
  275. fclose(f);
  276. if (completed)
  277. replace_file(filename, tmpfile);
  278. else
  279. unlink(tmpfile);
  280. tor_free(tmpfile);
  281. }
  282. #endif
  283. #define NUM_SECS_ROLLING_MEASURE 10
  284. #define NUM_SECS_BW_SUM_IS_VALID (12*60*60) /* half a day */
  285. #define NUM_SECS_BW_SUM_INTERVAL (15*60)
  286. #define NUM_TOTALS (NUM_SECS_BW_SUM_IS_VALID/NUM_SECS_BW_SUM_INTERVAL)
  287. /**
  288. * Structure to track bandwidth use, and remember the maxima for a given
  289. * time period.
  290. */
  291. typedef struct bw_array_t {
  292. /** Observation array: Total number of bytes transferred in each of the last
  293. * NUM_SECS_ROLLING_MEASURE seconds. This is used as a circular array. */
  294. int obs[NUM_SECS_ROLLING_MEASURE];
  295. int cur_obs_idx; /**< Current position in obs. */
  296. time_t cur_obs_time; /**< Time represented in obs[cur_obs_idx] */
  297. int total_obs; /**< Total for all members of obs except obs[cur_obs_idx] */
  298. int max_total; /**< Largest value that total_obs has taken on in the current
  299. * period. */
  300. /** When does the next period begin? */
  301. time_t next_period;
  302. /** Where in 'maxima' should the maximum bandwidth usage for the current
  303. * period be stored? */
  304. int next_max_idx;
  305. /** Circular array of the maximum bandwidth usage for the last NUM_TOTALS
  306. * periods */
  307. int maxima[NUM_TOTALS];
  308. } bw_array_t;
  309. /** Shift the current period of b forward by one.
  310. */
  311. static void commit_max(bw_array_t *b) {
  312. /* Store maximum from current period. */
  313. b->maxima[b->next_max_idx++] = b->max_total;
  314. /* Advance next_period and next_max_idx */
  315. b->next_period += NUM_SECS_BW_SUM_INTERVAL;
  316. if (b->next_max_idx == NUM_TOTALS)
  317. b->next_max_idx = 0;
  318. /* Reset max_total. */
  319. b->max_total = 0;
  320. }
  321. /** Shift the current observation time of 'b' forward by one second.
  322. */
  323. static INLINE void advance_obs(bw_array_t *b) {
  324. int nextidx;
  325. int total;
  326. /* Calculate the total bandwidth for the last NUM_SECS_ROLLING_MEASURE
  327. * seconds; adjust max_total as needed.*/
  328. total = b->total_obs + b->obs[b->cur_obs_idx];
  329. if (total > b->max_total)
  330. b->max_total = total;
  331. nextidx = b->cur_obs_idx+1;
  332. if (nextidx == NUM_SECS_ROLLING_MEASURE)
  333. nextidx = 0;
  334. b->total_obs = total - b->obs[nextidx];
  335. b->obs[nextidx]=0;
  336. b->cur_obs_idx = nextidx;
  337. if (++b->cur_obs_time >= b->next_period)
  338. commit_max(b);
  339. }
  340. /** Add 'n' bytes to the number of bytes in b for second 'when'.
  341. */
  342. static INLINE void add_obs(bw_array_t *b, time_t when, int n) {
  343. /* Don't record data in the past. */
  344. if (when<b->cur_obs_time)
  345. return;
  346. /* If we're currently adding observations for an earlier second than
  347. * 'when', advance b->cur_obs_time and b->cur_obs_idx by an
  348. * appropriate number of seconds, and do all the other housekeeping */
  349. while (when>b->cur_obs_time)
  350. advance_obs(b);
  351. b->obs[b->cur_obs_idx] += n;
  352. }
  353. /** Allocate, initialize, and return a new bw_array.
  354. */
  355. static bw_array_t *bw_array_new(void) {
  356. bw_array_t *b;
  357. time_t start;
  358. b = tor_malloc_zero(sizeof(bw_array_t));
  359. start = time(NULL);
  360. b->cur_obs_time = start;
  361. b->next_period = start + NUM_SECS_BW_SUM_INTERVAL;
  362. return b;
  363. }
  364. static bw_array_t *read_array = NULL;
  365. static bw_array_t *write_array = NULL;
  366. /** Set up read_array and write_array
  367. */
  368. static void bw_arrays_init(void)
  369. {
  370. read_array = bw_array_new();
  371. write_array = bw_array_new();
  372. }
  373. /** We read <b>num_bytes</b> more bytes in second <b>when</b>.
  374. *
  375. * Add num_bytes to the current running total for <b>when</b>.
  376. *
  377. * <b>when</b> can go back to time, but it's safe to ignore calls
  378. * earlier than the latest <b>when</b> you've heard of.
  379. */
  380. void rep_hist_note_bytes_written(int num_bytes, time_t when) {
  381. /* Maybe a circular array for recent seconds, and step to a new point
  382. * every time a new second shows up. Or simpler is to just to have
  383. * a normal array and push down each item every second; it's short.
  384. */
  385. /* When a new second has rolled over, compute the sum of the bytes we've
  386. * seen over when-1 to when-1-NUM_SECS_ROLLING_MEASURE, and stick it
  387. * somewhere. See rep_hist_bandwidth_assess() below.
  388. */
  389. add_obs(write_array, when, num_bytes);
  390. }
  391. /** We wrote <b>num_bytes</b> more bytes in second <b>when</b>.
  392. * (like rep_hist_note_bytes_written() above)
  393. */
  394. void rep_hist_note_bytes_read(int num_bytes, time_t when) {
  395. /* if we're smart, we can make this func and the one above share code */
  396. add_obs(read_array, when, num_bytes);
  397. }
  398. /** Helper: Return the largest value in b->maxima. (This is equal to the
  399. * most bandwidth used in any NUM_SECS_ROLLING_MEASURE period for the last
  400. * NUM_SECS_BW_SUM_IS_VALID seconds.)
  401. */
  402. static int find_largest_max(bw_array_t *b)
  403. {
  404. int i,max;
  405. max=0;
  406. for (i=0; i<NUM_TOTALS; ++i) {
  407. if (b->maxima[i]>max)
  408. max = b->maxima[i];
  409. }
  410. return max;
  411. }
  412. /**
  413. * Find the largest sums in the past NUM_SECS_BW_SUM_IS_VALID (roughly)
  414. * seconds. Find one sum for reading and one for writing. They don't have
  415. * to be at the same time).
  416. *
  417. * Return the smaller of these sums, divided by NUM_SECS_ROLLING_MEASURE.
  418. */
  419. int rep_hist_bandwidth_assess(time_t when) {
  420. int w,r;
  421. r = find_largest_max(read_array);
  422. w = find_largest_max(write_array);
  423. if (r>w)
  424. return (int)(w/(double)NUM_SECS_ROLLING_MEASURE);
  425. else
  426. return (int)(r/(double)NUM_SECS_ROLLING_MEASURE);
  427. return 0;
  428. }
  429. /*
  430. Local Variables:
  431. mode:c
  432. indent-tabs-mode:nil
  433. c-basic-offset:2
  434. End:
  435. */