rephist.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. /** History of an OR-\>OR link. */
  10. typedef struct link_history_t {
  11. /** When did we start tracking this list? */
  12. time_t since;
  13. /** How many times did extending from OR1 to OR2 succeeed? */
  14. unsigned long n_extend_ok;
  15. /** How many times did extending from OR1 to OR2 fail? */
  16. unsigned long n_extend_fail;
  17. } link_history_t;
  18. /** History of an OR. */
  19. typedef struct or_history_t {
  20. /** When did we start tracking this OR? */
  21. time_t since;
  22. /** How many times did we successfully connect? */
  23. unsigned long n_conn_ok;
  24. /** How many times did we try to connect and fail?*/
  25. unsigned long n_conn_fail;
  26. /** How many seconds have we been connected to this OR before
  27. * 'up_since'? */
  28. unsigned long uptime;
  29. /** How many seconds have we been unable to connect to this OR before
  30. * 'down_since'? */
  31. unsigned long downtime;
  32. /** If nonzero, we have been connected since this time. */
  33. time_t up_since;
  34. /** If nonzero, we have been unable to connect since this time. */
  35. time_t down_since;
  36. /** Map from hex OR2 identity digest to a link_history_t for the link
  37. * from this OR to OR2. */
  38. strmap_t *link_history_map;
  39. } or_history_t;
  40. /** Map from hex OR identity digest to or_history_t. */
  41. static strmap_t *history_map = NULL;
  42. /** Return the or_history_t for the named OR, creating it if necessary.
  43. */
  44. static or_history_t *get_or_history(const char* id)
  45. {
  46. or_history_t *hist;
  47. char hexid[HEX_DIGEST_LEN+1];
  48. base16_encode(hexid, HEX_DIGEST_LEN+1, id, DIGEST_LEN);
  49. hist = (or_history_t*) strmap_get(history_map, hexid);
  50. if (!hist) {
  51. hist = tor_malloc_zero(sizeof(or_history_t));
  52. hist->link_history_map = strmap_new();
  53. hist->since = time(NULL);
  54. strmap_set(history_map, hexid, hist);
  55. }
  56. return hist;
  57. }
  58. /** Return the link_history_t for the link from the first named OR to
  59. * the second, creating it if necessary. (ORs are identified by
  60. * identity digest)
  61. */
  62. static link_history_t *get_link_history(const char *from_id,
  63. const char *to_id)
  64. {
  65. or_history_t *orhist;
  66. link_history_t *lhist;
  67. char to_hexid[HEX_DIGEST_LEN+1];
  68. orhist = get_or_history(from_id);
  69. base16_encode(to_hexid, HEX_DIGEST_LEN+1, to_id, DIGEST_LEN);
  70. lhist = (link_history_t*) strmap_get(orhist->link_history_map, to_hexid);
  71. if (!lhist) {
  72. lhist = tor_malloc_zero(sizeof(link_history_t));
  73. lhist->since = time(NULL);
  74. strmap_set(orhist->link_history_map, to_hexid, lhist);
  75. }
  76. return lhist;
  77. }
  78. /** Update an or_history_t object <b>hist</b> so that its uptime/downtime
  79. * count is up-to-date as of <b>when</b>.
  80. */
  81. static void update_or_history(or_history_t *hist, time_t when)
  82. {
  83. tor_assert(hist);
  84. if (hist->up_since) {
  85. tor_assert(!hist->down_since);
  86. hist->uptime += (when - hist->up_since);
  87. hist->up_since = when;
  88. } else if (hist->down_since) {
  89. hist->downtime += (when - hist->down_since);
  90. hist->down_since = when;
  91. }
  92. }
  93. /** Initialize the static data structures for tracking history.
  94. */
  95. void rep_hist_init(void)
  96. {
  97. history_map = strmap_new();
  98. }
  99. /** Remember that an attempt to connect to the OR with identity digest
  100. * <b>id</b> failed at <b>when</b>.
  101. */
  102. void rep_hist_note_connect_failed(const char* id, time_t when)
  103. {
  104. or_history_t *hist;
  105. hist = get_or_history(id);
  106. ++hist->n_conn_fail;
  107. if (hist->up_since) {
  108. hist->uptime += (when - hist->up_since);
  109. hist->up_since = 0;
  110. }
  111. if (!hist->down_since)
  112. hist->down_since = when;
  113. }
  114. /** Remember that an attempt to connect to the OR with identity digest
  115. * <b>id</b> succeeded at <b>when</b>.
  116. */
  117. void rep_hist_note_connect_succeeded(const char* id, time_t when)
  118. {
  119. or_history_t *hist;
  120. hist = get_or_history(id);
  121. ++hist->n_conn_ok;
  122. if (hist->down_since) {
  123. hist->downtime += (when - hist->down_since);
  124. hist->down_since = 0;
  125. }
  126. if (!hist->up_since)
  127. hist->up_since = when;
  128. }
  129. /** Remember that we intentionally closed our connection to the OR
  130. * with identity digest <b>id</b> at <b>when</b>.
  131. */
  132. void rep_hist_note_disconnect(const char* id, time_t when)
  133. {
  134. or_history_t *hist;
  135. hist = get_or_history(id);
  136. ++hist->n_conn_ok;
  137. if (hist->up_since) {
  138. hist->uptime += (when - hist->up_since);
  139. hist->up_since = 0;
  140. }
  141. }
  142. /** Remember that our connection to the OR with identity digest
  143. * <b>id</b> had an error and stopped working at <b>when</b>.
  144. */
  145. void rep_hist_note_connection_died(const char* id, time_t when)
  146. {
  147. or_history_t *hist;
  148. if(!id) {
  149. /* XXXX008 not so. */
  150. /* If conn has no nickname, it's either an OP, or it is an OR
  151. * which didn't complete its handshake (or did and was unapproved).
  152. * Ignore it.
  153. */
  154. return;
  155. }
  156. hist = get_or_history(id);
  157. if (hist->up_since) {
  158. hist->uptime += (when - hist->up_since);
  159. hist->up_since = 0;
  160. }
  161. if (!hist->down_since)
  162. hist->down_since = when;
  163. }
  164. /** Remember that we successfully extended from the OR with identity
  165. * digest <b>from_id</b> to the OR with identity digest
  166. * <b>to_name</b>.
  167. */
  168. void rep_hist_note_extend_succeeded(const char *from_id,
  169. const char *to_id)
  170. {
  171. link_history_t *hist;
  172. /* log_fn(LOG_WARN, "EXTEND SUCCEEDED: %s->%s",from_name,to_name); */
  173. hist = get_link_history(from_id, to_id);
  174. ++hist->n_extend_ok;
  175. }
  176. /** Remember that we tried to extend from the OR with identity digest
  177. * <b>from_id</b> to the OR with identity digest <b>to_name</b>, but
  178. * failed.
  179. */
  180. void rep_hist_note_extend_failed(const char *from_id, const char *to_id)
  181. {
  182. link_history_t *hist;
  183. /* log_fn(LOG_WARN, "EXTEND FAILED: %s->%s",from_name,to_name); */
  184. hist = get_link_history(from_id, to_id);
  185. ++hist->n_extend_fail;
  186. }
  187. /** Log all the reliability data we have rememberred, with the chosen
  188. * severity.
  189. */
  190. void rep_hist_dump_stats(time_t now, int severity)
  191. {
  192. strmap_iter_t *lhist_it;
  193. strmap_iter_t *orhist_it;
  194. const char *name1, *name2, *hexdigest1, *hexdigest2;
  195. or_history_t *or_history;
  196. link_history_t *link_history;
  197. void *or_history_p, *link_history_p;
  198. double uptime;
  199. char buffer[2048];
  200. int len;
  201. unsigned long upt, downt;
  202. routerinfo_t *r;
  203. log(severity, "--------------- Dumping history information:");
  204. for (orhist_it = strmap_iter_init(history_map); !strmap_iter_done(orhist_it);
  205. orhist_it = strmap_iter_next(history_map,orhist_it)) {
  206. strmap_iter_get(orhist_it, &hexdigest1, &or_history_p);
  207. or_history = (or_history_t*) or_history_p;
  208. if ((r = router_get_by_hexdigest(hexdigest1)))
  209. name1 = r->nickname;
  210. else
  211. name1 = "(unknown)";
  212. update_or_history(or_history, now);
  213. upt = or_history->uptime;
  214. downt = or_history->downtime;
  215. if (upt+downt) {
  216. uptime = ((double)upt) / (upt+downt);
  217. } else {
  218. uptime=1.0;
  219. }
  220. log(severity,
  221. "OR %s [%s]: %ld/%ld good connections; uptime %ld/%ld sec (%.2f%%)",
  222. name1, hexdigest1,
  223. or_history->n_conn_ok, or_history->n_conn_fail+or_history->n_conn_ok,
  224. upt, upt+downt, uptime*100.0);
  225. strcpy(buffer, " Good extend attempts: ");
  226. len = strlen(buffer);
  227. for (lhist_it = strmap_iter_init(or_history->link_history_map);
  228. !strmap_iter_done(lhist_it);
  229. lhist_it = strmap_iter_next(or_history->link_history_map, lhist_it)) {
  230. strmap_iter_get(lhist_it, &hexdigest2, &link_history_p);
  231. if ((r = router_get_by_hexdigest(hexdigest2)))
  232. name2 = r->nickname;
  233. else
  234. name2 = "(unknown)";
  235. link_history = (link_history_t*) link_history_p;
  236. len += snprintf(buffer+len, 2048-len, "%s(%ld/%ld); ", name2,
  237. link_history->n_extend_ok,
  238. link_history->n_extend_ok+link_history->n_extend_fail);
  239. if (len >= 2048) {
  240. buffer[2047]='\0';
  241. break;
  242. }
  243. }
  244. log(severity, buffer);
  245. }
  246. }
  247. #if 0
  248. void write_rep_history(const char *filename)
  249. {
  250. FILE *f = NULL;
  251. char *tmpfile;
  252. int completed = 0;
  253. or_history_t *or_history;
  254. link_history_t *link_history;
  255. strmap_iter_t *lhist_it;
  256. strmap_iter_t *orhist_it;
  257. void *or_history_p, *link_history_p;
  258. const char *name1;
  259. tmpfile = tor_malloc(strlen(filename)+5);
  260. strcpy(tmpfile, filename);
  261. strcat(tmpfile, "_tmp");
  262. f = fopen(tmpfile, "w");
  263. if (!f) goto done;
  264. for (orhist_it = strmap_iter_init(history_map); !strmap_iter_done(orhist_it);
  265. orhist_it = strmap_iter_next(history_map,orhist_it)) {
  266. strmap_iter_get(orhist_it, &name1, &or_history_p);
  267. or_history = (or_history_t*) or_history_p;
  268. fprintf(f, "link %s connected:u%ld failed:%uld uptime:%uld",
  269. name1, or_history->since1,
  270. }
  271. done:
  272. if (f)
  273. fclose(f);
  274. if (completed)
  275. replace_file(filename, tmpfile);
  276. else
  277. unlink(tmpfile);
  278. tor_free(tmpfile);
  279. }
  280. #endif
  281. #define NUM_SECS_ROLLING_MEASURE 10
  282. #define NUM_SECS_BW_SUM_IS_VALID (12*60*60) /* half a day */
  283. /** We read <b>num_bytes</b> more bytes in second <b>when</b>.
  284. *
  285. * Add num_bytes to the current running total for <b>when</b>.
  286. *
  287. * <b>when</b> can go back to time, but it's safe to ignore calls
  288. * earlier than the latest <b>when</b> you've heard of.
  289. */
  290. void rep_hist_note_bytes_written(int num_bytes, time_t when) {
  291. /* Maybe a circular array for recent seconds, and step to a new point
  292. * every time a new second shows up. Or simpler is to just to have
  293. * a normal array and push down each item every second; it's short.
  294. */
  295. /* When a new second has rolled over, compute the sum of the bytes we've
  296. * seen over when-1 to when-1-NUM_SECS_ROLLING_MEASURE, and stick it
  297. * somewhere. See rep_hist_bandwidth_assess() below.
  298. */
  299. }
  300. /** We wrote <b>num_bytes</b> more bytes in second <b>when</b>.
  301. * (like rep_hist_note_bytes_written() above)
  302. */
  303. void rep_hist_note_bytes_read(int num_bytes, time_t when) {
  304. /* if we're smart, we can make this func and the one above share code */
  305. }
  306. /**
  307. * Find the largest sums in the past NUM_SECS_BW_SUM_IS_VALID (roughly)
  308. * seconds. Find one sum for reading and one for writing. They don't have
  309. * to be at the same time).
  310. *
  311. * Return the smaller of these sums, divided by NUM_SECS_ROLLING_MEASURE.
  312. */
  313. int rep_hist_bandwidth_assess(time_t when) {
  314. /* To get a handle on space complexity, I promise I will call this
  315. * function at most every options.DirFetchPostPeriod seconds. So in
  316. * rep_hist_note_bytes_foo() above, you could keep a running max sum
  317. * for the current period, and when the period ends you can tuck its max away
  318. * in a circular array of more managable size. We lose a bit of precision,
  319. * but this is all guesswork anyway.
  320. */
  321. return 0;
  322. }
  323. /*
  324. Local Variables:
  325. mode:c
  326. indent-tabs-mode:nil
  327. c-basic-offset:2
  328. End:
  329. */