rephist.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 lowercased OR2 name 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 lowercased OR nickname 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* nickname)
  45. {
  46. or_history_t *hist;
  47. hist = (or_history_t*) strmap_get(history_map, nickname);
  48. if (!hist) {
  49. hist = tor_malloc_zero(sizeof(or_history_t));
  50. hist->link_history_map = strmap_new();
  51. hist->since = time(NULL);
  52. strmap_set(history_map, nickname, hist);
  53. }
  54. return hist;
  55. }
  56. /** Return the link_history_t for the link from the first named OR to
  57. * the second, creating it if necessary.
  58. */
  59. static link_history_t *get_link_history(const char *from_name,
  60. const char *to_name)
  61. {
  62. or_history_t *orhist;
  63. link_history_t *lhist;
  64. orhist = get_or_history(from_name);
  65. lhist = (link_history_t*) strmap_get(orhist->link_history_map, to_name);
  66. if (!lhist) {
  67. lhist = tor_malloc_zero(sizeof(link_history_t));
  68. lhist->since = time(NULL);
  69. strmap_set(orhist->link_history_map, to_name, lhist);
  70. }
  71. return lhist;
  72. }
  73. /** Update an or_history_t object <b>hist</b> so that its uptime/downtime
  74. * count is up-to-date as of <b>when</b>.
  75. */
  76. static void update_or_history(or_history_t *hist, time_t when)
  77. {
  78. tor_assert(hist);
  79. if (hist->up_since) {
  80. tor_assert(!hist->down_since);
  81. hist->uptime += (when - hist->up_since);
  82. hist->up_since = when;
  83. } else if (hist->down_since) {
  84. hist->downtime += (when - hist->down_since);
  85. hist->down_since = when;
  86. }
  87. }
  88. /** Initialize the static data structures for tracking history.
  89. */
  90. void rep_hist_init(void)
  91. {
  92. history_map = strmap_new();
  93. }
  94. /** Remember that an attempt to connect to the OR <b>nickname</b> failed at
  95. * <b>when</b>.
  96. */
  97. void rep_hist_note_connect_failed(const char* nickname, time_t when)
  98. {
  99. or_history_t *hist;
  100. hist = get_or_history(nickname);
  101. ++hist->n_conn_fail;
  102. if (hist->up_since) {
  103. hist->uptime += (when - hist->up_since);
  104. hist->up_since = 0;
  105. }
  106. if (!hist->down_since)
  107. hist->down_since = when;
  108. }
  109. /** Remember that an attempt to connect to the OR <b>nickname</b> succeeded
  110. * at <b>when</b>.
  111. */
  112. void rep_hist_note_connect_succeeded(const char* nickname, time_t when)
  113. {
  114. or_history_t *hist;
  115. hist = get_or_history(nickname);
  116. ++hist->n_conn_ok;
  117. if (hist->down_since) {
  118. hist->downtime += (when - hist->down_since);
  119. hist->down_since = 0;
  120. }
  121. if (!hist->up_since)
  122. hist->up_since = when;
  123. }
  124. /** Remember that we intentionally closed our connection to the OR
  125. * <b>nickname</b> at <b>when</b>.
  126. */
  127. void rep_hist_note_disconnect(const char* nickname, time_t when)
  128. {
  129. or_history_t *hist;
  130. hist = get_or_history(nickname);
  131. ++hist->n_conn_ok;
  132. if (hist->up_since) {
  133. hist->uptime += (when - hist->up_since);
  134. hist->up_since = 0;
  135. }
  136. }
  137. /** Remember that our connection to the OR <b>nickname</b> had an error and
  138. * stopped working at <b>when</b>.
  139. */
  140. void rep_hist_note_connection_died(const char* nickname, time_t when)
  141. {
  142. or_history_t *hist;
  143. if(!nickname) {
  144. /* If conn has no nickname, it's either an OP, or it is an OR
  145. * which didn't complete its handshake (or did and was unapproved).
  146. * Ignore it.
  147. */
  148. return;
  149. }
  150. hist = get_or_history(nickname);
  151. if (hist->up_since) {
  152. hist->uptime += (when - hist->up_since);
  153. hist->up_since = 0;
  154. }
  155. if (!hist->down_since)
  156. hist->down_since = when;
  157. }
  158. /** Remember that we successfully extended from the OR <b>from_name</b> to
  159. * the OR <b>to_name</b>.
  160. */
  161. void rep_hist_note_extend_succeeded(const char *from_name,
  162. const char *to_name)
  163. {
  164. link_history_t *hist;
  165. /* log_fn(LOG_WARN, "EXTEND SUCCEEDED: %s->%s",from_name,to_name); */
  166. hist = get_link_history(from_name, to_name);
  167. ++hist->n_extend_ok;
  168. }
  169. /** Remember that we tried to extend from the OR <b>from_name</b> to the OR
  170. * <b>to_name</b>, but failed.
  171. */
  172. void rep_hist_note_extend_failed(const char *from_name, const char *to_name)
  173. {
  174. link_history_t *hist;
  175. /* log_fn(LOG_WARN, "EXTEND FAILED: %s->%s",from_name,to_name); */
  176. hist = get_link_history(from_name, to_name);
  177. ++hist->n_extend_fail;
  178. }
  179. /** Log all the reliability data we have rememberred, with the chosen
  180. * severity.
  181. */
  182. void rep_hist_dump_stats(time_t now, int severity)
  183. {
  184. strmap_iter_t *lhist_it;
  185. strmap_iter_t *orhist_it;
  186. const char *name1, *name2;
  187. or_history_t *or_history;
  188. link_history_t *link_history;
  189. void *or_history_p, *link_history_p;
  190. double uptime;
  191. char buffer[2048];
  192. int len;
  193. unsigned long upt, downt;
  194. log(severity, "--------------- Dumping history information:");
  195. for (orhist_it = strmap_iter_init(history_map); !strmap_iter_done(orhist_it);
  196. orhist_it = strmap_iter_next(history_map,orhist_it)) {
  197. strmap_iter_get(orhist_it, &name1, &or_history_p);
  198. or_history = (or_history_t*) or_history_p;
  199. update_or_history(or_history, now);
  200. upt = or_history->uptime;
  201. downt = or_history->downtime;
  202. if (upt+downt) {
  203. uptime = ((double)upt) / (upt+downt);
  204. } else {
  205. uptime=1.0;
  206. }
  207. log(severity,
  208. "OR %s: %ld/%ld good connections; uptime %ld/%ld sec (%.2f%%)",
  209. name1,
  210. or_history->n_conn_ok, or_history->n_conn_fail+or_history->n_conn_ok,
  211. upt, upt+downt, uptime*100.0);
  212. strcpy(buffer, " Good extend attempts: ");
  213. len = strlen(buffer);
  214. for (lhist_it = strmap_iter_init(or_history->link_history_map);
  215. !strmap_iter_done(lhist_it);
  216. lhist_it = strmap_iter_next(or_history->link_history_map, lhist_it)) {
  217. strmap_iter_get(lhist_it, &name2, &link_history_p);
  218. link_history = (link_history_t*) link_history_p;
  219. len += snprintf(buffer+len, 2048-len, "%s(%ld/%ld); ", name2,
  220. link_history->n_extend_ok,
  221. link_history->n_extend_ok+link_history->n_extend_fail);
  222. if (len >= 2048) {
  223. buffer[2047]='\0';
  224. break;
  225. }
  226. }
  227. log(severity, buffer);
  228. }
  229. }
  230. #if 0
  231. void write_rep_history(const char *filename)
  232. {
  233. FILE *f = NULL;
  234. char *tmpfile;
  235. int completed = 0;
  236. or_history_t *or_history;
  237. link_history_t *link_history;
  238. strmap_iter_t *lhist_it;
  239. strmap_iter_t *orhist_it;
  240. void *or_history_p, *link_history_p;
  241. const char *name1;
  242. tmpfile = tor_malloc(strlen(filename)+5);
  243. strcpy(tmpfile, filename);
  244. strcat(tmpfile, "_tmp");
  245. f = fopen(tmpfile, "w");
  246. if (!f) goto done;
  247. for (orhist_it = strmap_iter_init(history_map); !strmap_iter_done(orhist_it);
  248. orhist_it = strmap_iter_next(history_map,orhist_it)) {
  249. strmap_iter_get(orhist_it, &name1, &or_history_p);
  250. or_history = (or_history_t*) or_history_p;
  251. fprintf(f, "link %s connected:u%ld failed:%uld uptime:%uld",
  252. name1, or_history->since1,
  253. }
  254. done:
  255. if (f)
  256. fclose(f);
  257. if (completed)
  258. replace_file(filename, tmpfile);
  259. else
  260. unlink(tmpfile);
  261. tor_free(tmpfile);
  262. }
  263. #endif
  264. /*
  265. Local Variables:
  266. mode:c
  267. indent-tabs-mode:nil
  268. c-basic-offset:2
  269. End:
  270. */