rephist.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* Copyright 2004 Roger Dingledine */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. typedef struct link_history_t {
  6. time_t since;
  7. unsigned long n_extend_ok;
  8. unsigned long n_extend_fail;
  9. } link_history_t;
  10. typedef struct or_history_t {
  11. time_t since;
  12. unsigned long n_conn_ok;
  13. unsigned long n_conn_fail;
  14. unsigned long uptime;
  15. unsigned long downtime;
  16. time_t up_since;
  17. time_t down_since;
  18. strmap_t *link_history_map;
  19. } or_history_t;
  20. static strmap_t *history_map;
  21. /* Return the or_history_t for the named OR, creating it if necessary.
  22. */
  23. static or_history_t *get_or_history(const char* nickname)
  24. {
  25. or_history_t *hist;
  26. hist = (or_history_t*) strmap_get(history_map, nickname);
  27. if (!hist) {
  28. hist = tor_malloc_zero(sizeof(or_history_t));
  29. hist->link_history_map = strmap_new();
  30. hist->since = time(NULL);
  31. strmap_set(history_map, nickname, hist);
  32. }
  33. return hist;
  34. }
  35. /* Return the link_history_t for the link from the first named OR to
  36. * the second, creating it if necessary.
  37. */
  38. static link_history_t *get_link_history(const char *from_name,
  39. const char *to_name)
  40. {
  41. or_history_t *orhist;
  42. link_history_t *lhist;
  43. orhist = get_or_history(from_name);
  44. lhist = (link_history_t*) strmap_get(orhist->link_history_map, to_name);
  45. if (!lhist) {
  46. lhist = tor_malloc_zero(sizeof(link_history_t));
  47. lhist->since = time(NULL);
  48. strmap_set(orhist->link_history_map, to_name, lhist);
  49. }
  50. return lhist;
  51. }
  52. /* Update an or_history_t object so that its uptime/downtime count is
  53. * up-to-date.
  54. */
  55. static void update_or_history(or_history_t *hist, time_t when)
  56. {
  57. assert(hist);
  58. if (hist->up_since) {
  59. assert(!hist->down_since);
  60. hist->uptime += (when - hist->up_since);
  61. hist->up_since = when;
  62. } else if (hist->down_since) {
  63. hist->downtime += (when - hist->down_since);
  64. hist->down_since = when;
  65. }
  66. }
  67. /* Initialize the static data structures for tracking history.
  68. */
  69. void rep_hist_init(void)
  70. {
  71. history_map = strmap_new();
  72. }
  73. /* Remember that an attempt to connect to the OR 'nickname' failed at
  74. * 'when'.
  75. */
  76. void rep_hist_note_connect_failed(const char* nickname, time_t when)
  77. {
  78. or_history_t *hist;
  79. hist = get_or_history(nickname);
  80. ++hist->n_conn_fail;
  81. if (hist->up_since) {
  82. hist->uptime += (when - hist->up_since);
  83. hist->up_since = 0;
  84. }
  85. if (!hist->down_since)
  86. hist->down_since = when;
  87. }
  88. /* Remember that an attempt to connect to the OR 'nickname' succeeded
  89. * at 'when'.
  90. */
  91. void rep_hist_note_connect_succeeded(const char* nickname, time_t when)
  92. {
  93. or_history_t *hist;
  94. hist = get_or_history(nickname);
  95. ++hist->n_conn_ok;
  96. if (hist->down_since) {
  97. hist->downtime += (when - hist->down_since);
  98. hist->down_since = 0;
  99. }
  100. if (!hist->up_since)
  101. hist->up_since = when;
  102. }
  103. /* Remember that we intentionally closed our connection to the OR
  104. * 'nickname' at 'when'.
  105. */
  106. void rep_hist_note_disconnect(const char* nickname, time_t when)
  107. {
  108. or_history_t *hist;
  109. hist = get_or_history(nickname);
  110. ++hist->n_conn_ok;
  111. if (hist->up_since) {
  112. hist->uptime += (when - hist->up_since);
  113. hist->up_since = 0;
  114. }
  115. }
  116. /* Remember that our connection to the OR 'nickname' had an error and
  117. * stopped working at 'when'.
  118. */
  119. void rep_hist_note_connection_died(const char* nickname, time_t when)
  120. {
  121. or_history_t *hist;
  122. if(!nickname) {
  123. /* XXX
  124. * If conn has no nickname, it's either an OP, or it is an OR
  125. * which didn't complete its handshake (or did and was unapproved).
  126. * Ignore it. Is there anything better we could do?
  127. */
  128. return;
  129. }
  130. hist = get_or_history(nickname);
  131. if (hist->up_since) {
  132. hist->uptime += (when - hist->up_since);
  133. hist->up_since = 0;
  134. }
  135. if (!hist->down_since)
  136. hist->down_since = when;
  137. }
  138. /* Remember that we successfully extended from the OR 'from_name' to
  139. * the OR 'to_name'.
  140. */
  141. void rep_hist_note_extend_succeeded(const char *from_name,
  142. const char *to_name)
  143. {
  144. link_history_t *hist;
  145. /* log_fn(LOG_WARN, "EXTEND SUCCEEDED: %s->%s",from_name,to_name); */
  146. hist = get_link_history(from_name, to_name);
  147. ++hist->n_extend_ok;
  148. }
  149. /* Remember that we tried to extend from the OR 'from_name' to the OR
  150. * 'to_name', but failed.
  151. */
  152. void rep_hist_note_extend_failed(const char *from_name, const char *to_name)
  153. {
  154. link_history_t *hist;
  155. /* log_fn(LOG_WARN, "EXTEND FAILED: %s->%s",from_name,to_name); */
  156. hist = get_link_history(from_name, to_name);
  157. ++hist->n_extend_fail;
  158. }
  159. /* Log all the reliability data we have rememberred, with the chosen
  160. * severity.
  161. */
  162. void rep_hist_dump_stats(time_t now, int severity)
  163. {
  164. strmap_iter_t *lhist_it;
  165. strmap_iter_t *orhist_it;
  166. const char *name1, *name2;
  167. or_history_t *or_history;
  168. link_history_t *link_history;
  169. double uptime;
  170. char buffer[2048];
  171. int len;
  172. unsigned long upt, downt;
  173. log(severity, "--------------- Dumping history information:");
  174. for (orhist_it = strmap_iter_init(history_map); !strmap_iter_done(orhist_it);
  175. orhist_it = strmap_iter_next(history_map,orhist_it)) {
  176. strmap_iter_get(orhist_it, &name1, (void**)&or_history);
  177. update_or_history(or_history, now);
  178. upt = or_history->uptime;
  179. downt = or_history->downtime;
  180. if (upt+downt) {
  181. uptime = ((double)upt) / (upt+downt);
  182. } else {
  183. uptime=1.0;
  184. }
  185. log(severity,
  186. "OR %s: %ld/%ld good connections; uptime %ld/%ld sec (%.2f%%)",
  187. name1,
  188. or_history->n_conn_ok, or_history->n_conn_fail+or_history->n_conn_ok,
  189. upt, upt+downt, uptime*100.0);
  190. strcpy(buffer, " Good extend attempts: ");
  191. len = strlen(buffer);
  192. for (lhist_it = strmap_iter_init(or_history->link_history_map);
  193. !strmap_iter_done(lhist_it);
  194. lhist_it = strmap_iter_next(or_history->link_history_map, lhist_it)) {
  195. strmap_iter_get(lhist_it, &name2, (void**)&link_history);
  196. len += snprintf(buffer+len, 2048-len, "%s(%ld/%ld); ", name2,
  197. link_history->n_extend_ok,
  198. link_history->n_extend_ok+link_history->n_extend_fail);
  199. if (len >= 2048) {
  200. buffer[2047]='\0';
  201. break;
  202. }
  203. }
  204. log(severity, buffer);
  205. }
  206. }
  207. /*
  208. Local Variables:
  209. mode:c
  210. indent-tabs-mode:nil
  211. c-basic-offset:2
  212. End:
  213. */