rephist.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. hist = get_or_history(nickname);
  123. if (hist->up_since) {
  124. hist->uptime += (when - hist->up_since);
  125. hist->up_since = 0;
  126. }
  127. if (!hist->down_since)
  128. hist->down_since = when;
  129. }
  130. /* Remember that we successfully extended from the OR 'from_name' to
  131. * the OR 'to_name'.
  132. */
  133. void rep_hist_note_extend_succeeded(const char *from_name,
  134. const char *to_name)
  135. {
  136. link_history_t *hist;
  137. /* log_fn(LOG_WARN, "EXTEND SUCCEEDED: %s->%s",from_name,to_name); */
  138. hist = get_link_history(from_name, to_name);
  139. ++hist->n_extend_ok;
  140. }
  141. /* Remember that we tried to extend from the OR 'from_name' to the OR
  142. * 'to_name', but failed.
  143. */
  144. void rep_hist_note_extend_failed(const char *from_name, const char *to_name)
  145. {
  146. link_history_t *hist;
  147. /* log_fn(LOG_WARN, "EXTEND FAILED: %s->%s",from_name,to_name); */
  148. hist = get_link_history(from_name, to_name);
  149. ++hist->n_extend_fail;
  150. }
  151. /* Log all the reliability data we have rememberred, with the chosen
  152. * severity.
  153. */
  154. void rep_hist_dump_stats(time_t now, int severity)
  155. {
  156. strmap_iter_t *lhist_it;
  157. strmap_iter_t *orhist_it;
  158. const char *name1, *name2;
  159. or_history_t *or_history;
  160. link_history_t *link_history;
  161. double uptime;
  162. char buffer[2048];
  163. int len;
  164. unsigned long upt, downt;
  165. log(severity, "--------------- Dumping history information:");
  166. for (orhist_it = strmap_iter_init(history_map); !strmap_iter_done(orhist_it);
  167. orhist_it = strmap_iter_next(history_map,orhist_it)) {
  168. strmap_iter_get(orhist_it, &name1, (void**)&or_history);
  169. update_or_history(or_history, now);
  170. upt = or_history->uptime;
  171. downt = or_history->downtime;
  172. if (upt+downt) {
  173. uptime = ((double)upt) / (upt+downt);
  174. } else {
  175. uptime=1.0;
  176. }
  177. log(severity,
  178. "OR %s: %ld/%ld good connections; uptime %ld/%ld sec (%.2f%%)",
  179. name1,
  180. or_history->n_conn_ok, or_history->n_conn_fail+or_history->n_conn_ok,
  181. upt, upt+downt, uptime*100.0);
  182. strcpy(buffer, " Good extend attempts: ");
  183. len = strlen(buffer);
  184. for (lhist_it = strmap_iter_init(or_history->link_history_map);
  185. !strmap_iter_done(lhist_it);
  186. lhist_it = strmap_iter_next(or_history->link_history_map, lhist_it)) {
  187. strmap_iter_get(lhist_it, &name2, (void**)&link_history);
  188. len += snprintf(buffer+len, 2048-len, "%s(%ld/%ld); ", name2,
  189. link_history->n_extend_ok,
  190. link_history->n_extend_ok+link_history->n_extend_fail);
  191. if (len >= 2048) {
  192. buffer[2047]='\0';
  193. break;
  194. }
  195. }
  196. log(severity, buffer);
  197. }
  198. }
  199. /*
  200. Local Variables:
  201. mode:c
  202. indent-tabs-mode:nil
  203. c-basic-offset:2
  204. End:
  205. */