69#define REPHIST_PRIVATE
86#include "core/or/or_circuit_st.h"
88#include <event2/dns.h>
101#define STABILITY_EPSILON 0.0001
104#define STABILITY_ALPHA 0.95
106#define STABILITY_INTERVAL (12*60*60)
136 time_t start_of_downtime;
137 unsigned long weighted_uptime;
138 unsigned long total_weighted_time;
184static time_t started_tracking_stability = 0;
195 time_t overload_general_time;
198 time_t overload_ratelimits_time;
200 uint64_t overload_read_count;
202 uint64_t overload_write_count;
205 time_t overload_fd_exhausted_time;
207 uint64_t overload_fd_exhausted;
216static uint64_t stats_n_write_limit_reached = 0;
249 uint64_t stats_n_error_none;
250 uint64_t stats_n_error_format;
251 uint64_t stats_n_error_serverfailed;
252 uint64_t stats_n_error_notexist;
253 uint64_t stats_n_error_notimpl;
254 uint64_t stats_n_error_refused;
257 uint64_t stats_n_error_truncated;
258 uint64_t stats_n_error_unknown;
259 uint64_t stats_n_error_tor_timeout;
260 uint64_t stats_n_error_shutdown;
261 uint64_t stats_n_error_cancel;
262 uint64_t stats_n_error_nodata;
267 uint64_t stats_n_request;
305 return &dns_PTR_stats;
307 return &dns_AAAA_stats;
320 if (BUG(!dns_stats)) {
326 return dns_stats->stats_n_error_none;
328 return dns_stats->stats_n_error_format;
329 case DNS_ERR_SERVERFAILED:
330 return dns_stats->stats_n_error_serverfailed;
331 case DNS_ERR_NOTEXIST:
332 return dns_stats->stats_n_error_notexist;
333 case DNS_ERR_NOTIMPL:
334 return dns_stats->stats_n_error_notimpl;
335 case DNS_ERR_REFUSED:
336 return dns_stats->stats_n_error_refused;
337 case DNS_ERR_TRUNCATED:
338 return dns_stats->stats_n_error_truncated;
339 case DNS_ERR_UNKNOWN:
340 return dns_stats->stats_n_error_unknown;
341 case DNS_ERR_TIMEOUT:
342 return dns_stats->stats_n_error_tor_timeout;
343 case DNS_ERR_SHUTDOWN:
344 return dns_stats->stats_n_error_shutdown;
346 return dns_stats->stats_n_error_cancel;
348 return dns_stats->stats_n_error_nodata;
361 if (BUG(!dns_stats)) {
364 return dns_stats->stats_n_request;
391 dns_stats->stats_n_error_none++;
394 dns_stats->stats_n_error_format++;
396 case DNS_ERR_SERVERFAILED:
397 dns_stats->stats_n_error_serverfailed++;
399 case DNS_ERR_NOTEXIST:
400 dns_stats->stats_n_error_notexist++;
402 case DNS_ERR_NOTIMPL:
403 dns_stats->stats_n_error_notimpl++;
405 case DNS_ERR_REFUSED:
406 dns_stats->stats_n_error_refused++;
408 case DNS_ERR_TRUNCATED:
409 dns_stats->stats_n_error_truncated++;
411 case DNS_ERR_UNKNOWN:
412 dns_stats->stats_n_error_unknown++;
414 case DNS_ERR_TIMEOUT:
415 dns_stats->stats_n_error_tor_timeout++;
417 case DNS_ERR_SHUTDOWN:
418 dns_stats->stats_n_error_shutdown++;
421 dns_stats->stats_n_error_cancel++;
424 dns_stats->stats_n_error_nodata++;
437 if (BUG(!dns_stats)) {
440 dns_stats->stats_n_request++;
450 if (overload_time >
approx_time() - 3600 * n_hours) {
457#define OVERLOAD_STATS_VERSION 1
470 return stats_n_write_limit_reached;
479 char tbuf[ISO_TIME_LEN+1];
485 OVERLOAD_STATS_VERSION, tbuf);
498 char tbuf[ISO_TIME_LEN+1];
505 "overload-ratelimits %d %s %" PRIu64
" %" PRIu64
506 " %" PRIu64
" %" PRIu64
"\n",
507 OVERLOAD_STATS_VERSION, tbuf,
518 OVERLOAD_STATS_VERSION, tbuf);
522 if (smartlist_len(chunks) == 0) {
530 smartlist_free(chunks);
535#define SET_TO_START_OF_HOUR(a) STMT_BEGIN \
536 (a) = approx_time() - (approx_time() % 3600); \
543 static time_t last_read_counted = 0;
544 static time_t last_write_counted = 0;
547 case OVERLOAD_GENERAL:
550 case OVERLOAD_READ: {
559 case OVERLOAD_WRITE: {
560 stats_n_write_limit_reached++;
568 case OVERLOAD_FD_EXHAUSTED:
634 const uint16_t at_port, time_t when)
638 char tbuf[ISO_TIME_LEN+1];
639 int addr_changed, port_changed;
642 tor_assert((!at_addr && !at_port) || (at_addr && at_port));
649 if (!started_tracking_stability)
650 started_tracking_stability = time(NULL);
655 if (hist->start_of_downtime) {
659 log_info(
LD_HIST,
"Router %s is now Running; it had been down since %s.",
662 log_info(
LD_HIST,
" (Paradoxically, it was already Running too.)");
664 down_length = when - hist->start_of_downtime;
665 hist->total_weighted_time += down_length;
666 hist->start_of_downtime = 0;
667 }
else if (addr_changed || port_changed) {
679 penalty = (int)(fresh_interval + live_interval) / 2;
682 log_info(
LD_HIST,
"Router %s still seems Running, but its address appears "
683 "to have changed since the last time it was reachable. I'm "
684 "going to treat it as having been down for %d seconds",
691 log_debug(
LD_HIST,
"Router %s is still Running; it has been Running "
694 log_info(
LD_HIST,
"Router %s is now Running; it was previously untracked",
709 char tbuf[ISO_TIME_LEN+1];
711 if (!started_tracking_stability)
712 started_tracking_stability = time(NULL);
723 if (run_length < 0) {
724 unsigned long penalty = -run_length;
725#define SUBTRACT_CLAMPED(var, penalty) \
726 do { (var) = (var) < (penalty) ? 0 : (var) - (penalty); } while (0)
729 SUBTRACT_CLAMPED(hist->weighted_uptime, penalty);
732 hist->weighted_uptime += run_length;
733 hist->total_weighted_time += run_length;
736 log_info(
LD_HIST,
"Router %s is now non-Running: it had previously been "
737 "Running since %s. Its total weighted uptime is %lu/%lu.",
739 hist->total_weighted_time);
741 if (!hist->start_of_downtime) {
742 hist->start_of_downtime = when;
745 log_info(
LD_HIST,
"Router %s is now non-Running; it was previously "
751 log_info(
LD_HIST,
"Router %s is still non-Running; it has been "
768 hist->weighted_uptime = 0;
776 digestmap_iter_t *orhist_it;
795 log_info(
LD_HIST,
"Discounting all old stability info by a factor of %f",
800 !digestmap_iter_done(orhist_it);
801 orhist_it = digestmap_iter_next(
history_map,orhist_it)) {
802 digestmap_iter_get(orhist_it, &digest1, &hist_p);
809 hist->weighted_uptime = (
unsigned long)(hist->weighted_uptime * alpha);
810 hist->total_weighted_time = (
unsigned long)
811 (hist->total_weighted_time * alpha);
828 total_weights += 1.0;
835 return total / total_weights;
843 long total = hist->total_weighted_time;
846 }
else if (hist->start_of_downtime) {
847 total += (when - hist->start_of_downtime);
857 long total = hist->total_weighted_time;
858 long up = hist->weighted_uptime;
864 }
else if (hist->start_of_downtime) {
865 total += (when - hist->start_of_downtime);
875 return ((
double) up) / total;
939 return started_tracking_stability < time(NULL) - 4*60*60;
948 digestmap_iter_t *orhist_it;
949 const char *name1, *digest1;
957 tor_log(severity,
LD_HIST,
"--------------- Dumping history information:");
960 !digestmap_iter_done(orhist_it);
961 orhist_it = digestmap_iter_next(
history_map,orhist_it)) {
964 digestmap_iter_get(orhist_it, &digest1, &or_history_p);
975 "OR %s [%s]: wmtbf %lu:%02lu:%02lu",
977 stability/3600, (stability/60)%60, stability%60);
990 digestmap_iter_t *orhist_it;
994 while (!digestmap_iter_done(orhist_it)) {
996 digestmap_iter_get(orhist_it, &d1, &or_history_p);
997 or_history = or_history_p;
999 should_remove = authority ?
1002 : (or_history->
changed < before);
1003 if (should_remove) {
1004 orhist_it = digestmap_iter_next_rmv(
history_map, orhist_it);
1008 orhist_it = digestmap_iter_next(
history_map, orhist_it);
1020 char time_buf[ISO_TIME_LEN+1];
1022 digestmap_iter_t *orhist_it;
1030 char *filename = get_datadir_fname(
"router-stability");
1047#define PUT(s) STMT_BEGIN if (fputs((s),f)<0) goto err; STMT_END
1048#define PRINTF(args) STMT_BEGIN if (fprintf args <0) goto err; STMT_END
1053 PRINTF((f,
"stored-at %s\n", time_buf));
1055 if (started_tracking_stability) {
1057 PRINTF((f,
"tracked-since %s\n", time_buf));
1061 PRINTF((f,
"last-downrated %s\n", time_buf));
1069 for (orhist_it = digestmap_iter_init(
history_map);
1070 !digestmap_iter_done(orhist_it);
1071 orhist_it = digestmap_iter_next(
history_map,orhist_it)) {
1073 const char *t = NULL;
1074 digestmap_iter_get(orhist_it, &digest, &or_history_p);
1085 "Relay '%s' is listed as up in rephist, but it's not in "
1086 "our routerlist. Correcting.", dbuf);
1090 PRINTF((f,
"R %s\n", dbuf));
1095 PRINTF((f,
"+MTBF %lu %.5f%s%s\n",
1097 t ?
" S=" :
"", t ? t :
""));
1099 if (hist->start_of_downtime > 0) {
1103 PRINTF((f,
"+WFU %lu %lu%s%s\n",
1104 hist->weighted_uptime, hist->total_weighted_time,
1105 t ?
" S=" :
"", t ? t :
""));
1125 for ( ; i < smartlist_len(sl); ++i) {
1126 const char *line = smartlist_get(sl, i);
1144 strlcpy(b, s,
sizeof(b));
1161correct_time(time_t t, time_t now, time_t stored_at, time_t started_measuring)
1163 if (t < started_measuring - 24*60*60*365)
1165 else if (t < started_measuring)
1166 return started_measuring;
1167 else if (t > stored_at)
1170 long run_length = stored_at - t;
1171 t = (time_t)(now - run_length);
1172 if (t < started_measuring)
1173 t = started_measuring;
1185 const char *line = NULL;
1187 time_t last_downrated = 0, stored_at = 0, tracked_since = 0;
1188 time_t latest_possible_start = now;
1192 char *filename = get_datadir_fname(
"router-stability");
1203 const char *firstline;
1204 if (smartlist_len(lines)>4) {
1205 firstline = smartlist_get(lines, 0);
1208 10, -1, LONG_MAX, NULL, NULL);
1211 if (format != 1 && format != 2) {
1213 "Unrecognized format in mtbf history file. Skipping.");
1216 for (i = 1; i < smartlist_len(lines); ++i) {
1217 line = smartlist_get(lines, i);
1218 if (!strcmp(line,
"data"))
1221 if (
parse_iso_time(line+strlen(
"last-downrated "), &last_downrated)<0)
1222 log_warn(
LD_HIST,
"Couldn't parse downrate time in mtbf "
1227 log_warn(
LD_HIST,
"Couldn't parse stored time in mtbf "
1231 if (
parse_iso_time(line+strlen(
"tracked-since "), &tracked_since)<0)
1232 log_warn(
LD_HIST,
"Couldn't parse started-tracking time in mtbf "
1236 if (last_downrated > now)
1237 last_downrated = now;
1238 if (tracked_since > now)
1239 tracked_since = now;
1242 log_warn(
LD_HIST,
"No stored time recorded.");
1246 if (line && !strcmp(line,
"data"))
1251 for (; i < smartlist_len(lines); ++i) {
1254 char mtbf_timebuf[ISO_TIME_LEN+1];
1255 char wfu_timebuf[ISO_TIME_LEN+1];
1256 time_t start_of_run = 0;
1257 time_t start_of_downtime = 0;
1258 int have_mtbf = 0, have_wfu = 0;
1261 long wt_uptime = 0, total_wt_time = 0;
1264 line = smartlist_get(lines, i);
1265 if (!strcmp(line,
"."))
1268 mtbf_timebuf[0] =
'\0';
1269 wfu_timebuf[0] =
'\0';
1272 n =
tor_sscanf(line,
"%40s %ld %lf S=%10s %8s",
1273 hexbuf, &wrl, &trw, mtbf_timebuf, mtbf_timebuf+11);
1274 if (n != 3 && n != 5) {
1281 int mtbf_idx, wfu_idx;
1284 strlcpy(hexbuf, line+2,
sizeof(hexbuf));
1287 if (mtbf_idx >= 0) {
1288 const char *mtbfline = smartlist_get(lines, mtbf_idx);
1289 n =
tor_sscanf(mtbfline,
"+MTBF %lu %lf S=%10s %8s",
1290 &wrl, &trw, mtbf_timebuf, mtbf_timebuf+11);
1291 if (n == 2 || n == 4) {
1294 log_warn(
LD_HIST,
"Couldn't scan +MTBF line %s",
1299 const char *wfuline = smartlist_get(lines, wfu_idx);
1300 n =
tor_sscanf(wfuline,
"+WFU %lu %lu S=%10s %8s",
1301 &wt_uptime, &total_wt_time,
1302 wfu_timebuf, wfu_timebuf+11);
1303 if (n == 2 || n == 4) {
1306 log_warn(
LD_HIST,
"Couldn't scan +WFU line %s",
escaped(wfuline));
1324 if (mtbf_timebuf[0]) {
1325 mtbf_timebuf[10] =
' ';
1327 log_warn(
LD_HIST,
"Couldn't parse time %s",
1333 latest_possible_start = (time_t)(hist->
start_of_run - wrl);
1339 if (wfu_timebuf[0]) {
1340 wfu_timebuf[10] =
' ';
1342 log_warn(
LD_HIST,
"Couldn't parse time %s",
escaped(wfu_timebuf));
1345 hist->start_of_downtime =
correct_time(start_of_downtime, now, stored_at,
1347 hist->weighted_uptime = wt_uptime;
1348 hist->total_weighted_time = total_wt_time;
1350 if (strcmp(line,
"."))
1351 log_warn(
LD_HIST,
"Truncated MTBF file.");
1353 if (tracked_since < 86400*365)
1354 tracked_since = latest_possible_start;
1357 started_tracking_stability = tracked_since;
1364 smartlist_free(lines);
1372#define EXIT_STATS_ROUND_UP_BYTES 1024
1374#define EXIT_STATS_ROUND_UP_STREAMS 4
1376#define EXIT_STATS_NUM_PORTS 65536
1378#define EXIT_STATS_TOP_N_PORTS 10
1431 return (*(
int*)x - *(
int*)y);
1440 int i, j, top_elements = 0, cur_min_idx = 0, cur_port;
1443 uint64_t cur_bytes = 0, other_read = 0, other_written = 0,
1444 total_read = 0, total_written = 0;
1445 uint32_t total_streams = 0, other_streams = 0;
1446 smartlist_t *written_strings, *read_strings, *streams_strings;
1447 char *written_string, *read_string, *streams_string;
1448 char t[ISO_TIME_LEN+1];
1487 if (cur_bytes == 0) {
1491 top_bytes[top_elements] = cur_bytes;
1492 top_ports[top_elements++] = i;
1493 }
else if (cur_bytes > top_bytes[cur_min_idx]) {
1494 top_bytes[cur_min_idx] = cur_bytes;
1495 top_ports[cur_min_idx] = i;
1500 for (j = 1; j < top_elements; j++) {
1501 if (top_bytes[j] < top_bytes[cur_min_idx]) {
1511 other_read = total_read;
1512 other_written = total_written;
1513 other_streams = total_streams;
1516 qsort(top_ports, top_elements,
sizeof(
int),
compare_int_);
1517 for (j = 0; j < top_elements; j++) {
1518 cur_port = top_ports[j];
1549 other_written /= 1024;
1568 smartlist_free(written_strings);
1569 smartlist_free(read_strings);
1570 smartlist_free(streams_strings);
1575 "exit-kibibytes-written %s\n"
1576 "exit-kibibytes-read %s\n"
1577 "exit-streams-opened %s\n",
1602 log_info(
LD_HIST,
"Writing exit port statistics to disk.");
1630 log_debug(
LD_HIST,
"Written %lu bytes and read %lu bytes to/from an "
1631 "exit connection to port %d.",
1632 (
unsigned long)num_written, (
unsigned long)num_read, port);
1642 log_debug(
LD_HIST,
"Opened exit stream to port %d", port);
1659 case RELAY_COMMAND_BEGIN:
1662 case RELAY_COMMAND_BEGIN_DIR:
1665 case RELAY_COMMAND_RESOLVE:
1669 tor_assert_nonfatal_unreached_once();
1679 case RELAY_COMMAND_BEGIN:
1681 case RELAY_COMMAND_BEGIN_DIR:
1683 case RELAY_COMMAND_RESOLVE:
1692#define CONN_DIRECTION_INITIATED 0
1693#define CONN_DIRECTION_RECEIVED 1
1695#define CONN_DIRECTION(from_listener) \
1696 (from_listener) ? CONN_DIRECTION_RECEIVED : CONN_DIRECTION_INITIATED
1700static uint64_t conn_num_created_v6[2][CONN_TYPE_MAX_];
1703static uint64_t conn_num_opened_v6[2][CONN_TYPE_MAX_];
1706static uint64_t conn_num_rejected_v6[CONN_TYPE_MAX_];
1714 unsigned int dir = CONN_DIRECTION(from_listener);
1722 conn_num_created_v6[dir][type]++;
1723 conn_num_opened_v6[dir][type]++;
1737 unsigned int dir = CONN_DIRECTION(from_listener);
1746 if (conn_num_opened_v6[dir][type] > 0) {
1747 conn_num_opened_v6[dir][type]--;
1767 conn_num_rejected_v6[type]++;
1780 unsigned int dir = CONN_DIRECTION(from_listener);
1785 return conn_num_created_v6[dir][type];
1796 unsigned int dir = CONN_DIRECTION(from_listener);
1801 return conn_num_opened_v6[dir][type];
1816 return conn_num_rejected_v6[type];
1854 double mean_time_cells_in_queue, uint32_t processed_cells)
1874 time_t start_of_interval;
1875 int interval_length;
1877 double mean_num_cells_in_queue, mean_time_cells_in_queue;
1878 uint32_t processed_cells;
1888 interval_length = (int) (end_of_interval - start_of_interval);
1889 if (interval_length <= 0)
1894 (
double) interval_length / 1000.0 / 2.0;
1895 mean_time_cells_in_queue =
1901 mean_time_cells_in_queue,
1947 uint64_t processed_cells[SHARES];
1948 uint32_t circs_in_share[SHARES];
1949 int number_of_circuits, i;
1950 double queued_cells[SHARES], time_in_queue[SHARES];
1951 smartlist_t *processed_cells_strings, *queued_cells_strings,
1952 *time_in_queue_strings;
1953 char *processed_cells_string, *queued_cells_string,
1954 *time_in_queue_string;
1955 char t[ISO_TIME_LEN+1];
1964 memset(processed_cells, 0, SHARES *
sizeof(uint64_t));
1965 memset(circs_in_share, 0, SHARES *
sizeof(uint32_t));
1966 memset(queued_cells, 0, SHARES *
sizeof(
double));
1967 memset(time_in_queue, 0, SHARES *
sizeof(
double));
1971 if (number_of_circuits > 0) {
1978 int share = i++ * SHARES / number_of_circuits;
1979 processed_cells[share] += stats->processed_cells;
1980 queued_cells[share] += stats->mean_num_cells_in_queue;
1981 time_in_queue[share] += stats->mean_time_cells_in_queue;
1982 circs_in_share[share]++;
1984 SMARTLIST_FOREACH_END(stats);
1991 for (i = 0; i < SHARES; i++) {
1993 "%"PRIu64, !circs_in_share[i] ? 0 :
1994 (processed_cells[i] /
1995 circs_in_share[i]));
1997 for (i = 0; i < SHARES; i++) {
1999 circs_in_share[i] == 0 ? 0.0 :
2000 queued_cells[i] / (
double) circs_in_share[i]);
2002 for (i = 0; i < SHARES; i++) {
2004 circs_in_share[i] == 0 ? 0.0 :
2005 time_in_queue[i] / (
double) circs_in_share[i]);
2018 smartlist_free(processed_cells_strings);
2019 smartlist_free(queued_cells_strings);
2020 smartlist_free(time_in_queue_strings);
2025 "cell-processed-cells %s\n"
2026 "cell-queued-cells %s\n"
2027 "cell-time-in-queue %s\n"
2028 "cell-circuits-per-decile %d\n",
2030 processed_cells_string,
2031 queued_cells_string,
2032 time_in_queue_string,
2033 CEIL_DIV(number_of_circuits, SHARES));
2060 SMARTLIST_FOREACH_END(circ);
2097 log_warn(
LD_BUG,
"Called rep_hist_desc_stats_init() when desc stats were "
2098 "already initialized. This is probably harmless.");
2132 char t[ISO_TIME_LEN+1];
2135 digestmap_iter_t *iter;
2139 int *vals, max = 0, q3 = 0, md = 0, q1 = 0, min = 0;
2147 vals = tor_calloc(size,
sizeof(
int));
2149 !digestmap_iter_done(iter);
2152 digestmap_iter_get(iter, &key, &val);
2153 count = (uintptr_t)val;
2154 vals[n++] = (int)count;
2157 max = find_nth_int(vals, size, size-1);
2158 q3 = find_nth_int(vals, size, (3*size-1)/4);
2159 md = find_nth_int(vals, size, (size-1)/2);
2160 q1 = find_nth_int(vals, size, (size-1)/4);
2161 min = find_nth_int(vals, size, 0);
2168 "served-descs-stats-end %s (%d s) total=%lu unique=%u "
2169 "max=%d q3=%d md=%d q1=%d min=%d\n",
2173 size, max, q3, md, q1, min);
2186 char *filename = NULL, *str = NULL;
2199 filename = get_datadir_fname2(
"stats",
"served-desc-stats");
2201 log_warn(
LD_HIST,
"Unable to write served descs statistics to disk!");
2222 count = (uintptr_t)val;
2223 if (count != INT_MAX)
2249#define OVERLOAD_ONIONSKIN_NTOR_PERCENT_SCALE 1000.0
2250#define OVERLOAD_ONIONSKIN_NTOR_PERCENT_DEFAULT 1000
2251#define OVERLOAD_ONIONSKIN_NTOR_PERCENT_MIN 0
2252#define OVERLOAD_ONIONSKIN_NTOR_PERCENT_MAX 100000
2258 OVERLOAD_ONIONSKIN_NTOR_PERCENT_DEFAULT /
2259 OVERLOAD_ONIONSKIN_NTOR_PERCENT_SCALE / 100.0;
2264#define OVERLOAD_ONIONSKIN_NTOR_PERIOD_SECS_DEFAULT (60 * 60 * 6)
2265#define OVERLOAD_ONIONSKIN_NTOR_PERIOD_SECS_MIN 0
2266#define OVERLOAD_ONIONSKIN_NTOR_PERIOD_SECS_MAX INT32_MAX
2272 OVERLOAD_ONIONSKIN_NTOR_PERIOD_SECS_DEFAULT;
2299static inline uint16_t
2336 OVERLOAD_ONIONSKIN_NTOR_PERCENT_SCALE) {
2344 log_notice(
LD_HIST,
"General overload -> Ntor dropped (%" PRIu64
") "
2345 "fraction %.4f%% is above threshold of %.4f%%",
2372 if (stat == ONION_HANDSHAKE_TYPE_NTOR ||
2373 stat == ONION_HANDSHAKE_TYPE_NTOR_V3) {
2397 stats_n_onionskin_dropped[stat]++;
2400 if (stat == ONION_HANDSHAKE_TYPE_NTOR ||
2401 stat == ONION_HANDSHAKE_TYPE_NTOR_V3) {
2440 log_notice(
LD_HEARTBEAT,
"Circuit handshake stats since last time: "
2441 "%d/%d TAP, %d/%d NTor, %d/%d NTor (v3).",
2442 onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_TAP],
2444 onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_NTOR],
2446 onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_NTOR_V3],
2448 memset(onion_handshakes_assigned, 0,
sizeof(onion_handshakes_assigned));
2464static hs_v2_stats_t *
2467 hs_v2_stats_t *new_hs_v2_stats = tor_malloc_zero(
sizeof(hs_v2_stats_t));
2469 return new_hs_v2_stats;
2472#define hs_v2_stats_free(val) \
2473 FREE_AND_NULL(hs_v2_stats_t, hs_v2_stats_free_, (val))
2479 if (!victim_hs_v2_stats) {
2536static hs_v3_stats_t *
2539 hs_v3_stats_t *new_hs_v3_stats = tor_malloc_zero(
sizeof(hs_v3_stats_t));
2540 new_hs_v3_stats->v3_onions_seen_this_period = digest256map_new();
2542 return new_hs_v3_stats;
2545#define hs_v3_stats_free(val) \
2546 FREE_AND_NULL(hs_v3_stats_t, hs_v3_stats_free_, (val))
2552 if (!victim_hs_v3_stats) {
2556 digest256map_free(victim_hs_v3_stats->v3_onions_seen_this_period, NULL);
2569 digest256map_free(
hs_v3_stats->v3_onions_seen_this_period, NULL);
2570 hs_v3_stats->v3_onions_seen_this_period = digest256map_new();
2604 !!digest256map_get(
hs_v3_stats->v3_onions_seen_this_period,
2607 log_info(
LD_GENERAL,
"Considering v3 descriptor with %s (%sseen before)",
2608 safe_str(
hex_str((
char*)blinded_key, 32)),
2609 seen_before ?
"" :
"not ");
2613 digest256map_set(
hs_v3_stats->v3_onions_seen_this_period,
2614 blinded_key, (
void*)(uintptr_t)1);
2623 log_debug(
LD_GENERAL,
"New RP cell (%d)", is_v2);
2667#define REND_CELLS_DELTA_F 2048
2671#define REND_CELLS_EPSILON 0.3
2674#define REND_CELLS_BIN_SIZE 1024
2678#define ONIONS_SEEN_DELTA_F 8
2682#define ONIONS_SEEN_EPSILON 0.3
2685#define ONIONS_SEEN_BIN_SIZE 8
2694 char t[ISO_TIME_LEN+1];
2695 char *hs_stats_string;
2696 int64_t obfuscated_onions_seen, obfuscated_cells_seen;
2698 uint64_t rp_cells_seen = is_v3 ?
2700 size_t onions_seen = is_v3 ?
2701 digest256map_size(
hs_v3_stats->v3_onions_seen_this_period) : 0;
2702 time_t start_of_hs_stats_interval = is_v3 ?
2705 uint64_t rounded_cells_seen
2707 rounded_cells_seen = MIN(rounded_cells_seen, INT64_MAX);
2712 uint64_t rounded_onions_seen =
2714 rounded_onions_seen = MIN(rounded_onions_seen, INT64_MAX);
2717 ONIONS_SEEN_EPSILON);
2721 "%s %"PRId64
" delta_f=%d epsilon=%.2f bin_size=%d\n"
2722 "%s %"PRId64
" delta_f=%d epsilon=%.2f bin_size=%d\n",
2723 is_v3 ?
"hidserv-v3-stats-end" :
"hidserv-stats-end",
2724 t, (
unsigned) (now - start_of_hs_stats_interval),
2726 "hidserv-rend-v3-relayed-cells" :
"hidserv-rend-relayed-cells",
2728 REND_CELLS_EPSILON, REND_CELLS_BIN_SIZE,
2729 is_v3 ?
"hidserv-dir-v3-onions-seen" :
"hidserv-dir-onions-seen",
2730 obfuscated_onions_seen, ONIONS_SEEN_DELTA_F,
2731 ONIONS_SEEN_EPSILON, ONIONS_SEEN_BIN_SIZE);
2733 return hs_stats_string;
2747 time_t start_of_hs_stats_interval = is_v3 ?
2750 if (!start_of_hs_stats_interval) {
2754 if (start_of_hs_stats_interval + WRITE_STATS_INTERVAL > now) {
2771 is_v3 ?
"hidserv-v3-stats" :
"hidserv-stats",
2772 str,
"hidden service stats");
2777 return start_of_hs_stats_interval + WRITE_STATS_INTERVAL;
2780static uint64_t link_proto_count[MAX_LINK_PROTO+1][2];
2788 started_here = !!started_here;
2789 if (link_proto > MAX_LINK_PROTO) {
2790 log_warn(
LD_BUG,
"Can't log link protocol %u", link_proto);
2794 link_proto_count[link_proto][started_here]++;
2882#define MIN_CELL_COUNTS_TO_PUBLISH 1
2883#define ROUND_CELL_COUNTS_TO 10000
2885rep_hist_prep_published_padding_counts(time_t now)
2896#define ROUND_AND_SET_COUNT(x) (x) = round_uint64_to_next_multiple_of((x), \
2897 ROUND_CELL_COUNTS_TO)
2908#undef ROUND_AND_SET_COUNT
2918 char *result = NULL;
2927 " write-drop=%"PRIu64
2928 " write-pad=%"PRIu64
2929 " write-total=%"PRIu64
2930 " read-drop=%"PRIu64
2932 " read-total=%"PRIu64
2933 " enabled-read-pad=%"PRIu64
2934 " enabled-read-total=%"PRIu64
2935 " enabled-write-pad=%"PRIu64
2936 " enabled-write-total=%"PRIu64
2937 " max-chanpad-timers=%"PRIu64
2941 (uint64_t)ROUND_CELL_COUNTS_TO,
2966 for (
int i = 1; i <= MAX_LINK_PROTO; i++) {
2968 tor_asprintf(&line,
"initiated %"PRIu64
" and received "
2969 "%"PRIu64
" v%d connections", link_proto_count[i][1],
2970 link_proto_count[i][0], i);
2976 log_notice(
LD_HEARTBEAT,
"Since startup we %s.", log_line);
2979 smartlist_free(lines);
3018 OVERLOAD_ONIONSKIN_NTOR_PERCENT_DEFAULT,
3019 OVERLOAD_ONIONSKIN_NTOR_PERCENT_MIN,
3020 OVERLOAD_ONIONSKIN_NTOR_PERCENT_MAX) /
3021 OVERLOAD_ONIONSKIN_NTOR_PERCENT_SCALE / 100.0;
3025 OVERLOAD_ONIONSKIN_NTOR_PERIOD_SECS_DEFAULT,
3026 OVERLOAD_ONIONSKIN_NTOR_PERIOD_SECS_MIN,
3027 OVERLOAD_ONIONSKIN_NTOR_PERIOD_SECS_MAX);
3037#ifdef TOR_UNIT_TESTS
3039const hs_v2_stats_t *
3040rep_hist_get_hs_v2_stats(
void)
3046const hs_v3_stats_t *
3047rep_hist_get_hs_v3_stats(
void)
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
void tor_addr_make_unspec(tor_addr_t *a)
int tor_addr_compare(const tor_addr_t *addr1, const tor_addr_t *addr2, tor_addr_comparison_t how)
int tor_addr_is_null(const tor_addr_t *addr)
int authdir_mode(const or_options_t *options)
Header file for directory authority mode.
const char * hex_str(const char *from, size_t fromlen)
int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
or_circuit_t * TO_OR_CIRCUIT(circuit_t *x)
smartlist_t * circuit_get_global_list(void)
Header file for circuitlist.c.
#define CIRCUIT_IS_ORIGIN(c)
int check_or_create_data_subdir(const char *subdir)
const or_options_t * get_options(void)
int write_to_data_subdir(const char *subdir, const char *fname, const char *str, const char *descr)
Header file for config.c.
Header file for connection.c.
int connection_or_digest_is_known_relay(const char *id_digest)
Header file for connection_or.c.
void conn_stats_free_all(void)
Header for feature/stats/connstats.c.
Common functions for using (pseudo-)random number generators.
double crypto_rand_double(void)
const char * escaped(const char *s)
#define RFTS_IGNORE_MISSING
int finish_writing_to_file(open_file_t *file_data)
int append_bytes_to_file(const char *fname, const char *str, size_t len, int bin)
FILE * start_writing_to_stdio_file(const char *fname, int open_flags, int mode, open_file_t **data_out)
int abort_writing_to_file(open_file_t *file_data)
time_t hs_get_start_time_of_next_time_period(time_t now)
int64_t add_laplace_noise(int64_t signal_, double random_, double delta_f, double epsilon)
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
uint32_t round_uint32_to_next_multiple_of(uint32_t number, uint32_t divisor)
uint64_t round_uint64_to_next_multiple_of(uint64_t number, uint64_t divisor)
networkstatus_t * networkstatus_get_latest_consensus(void)
int32_t networkstatus_get_param(const networkstatus_t *ns, const char *param_name, int32_t default_val, int32_t min_val, int32_t max_val)
Header file for networkstatus.c.
Networkstatus consensus/vote structure.
const node_t * node_get_by_id(const char *identity_digest)
const char * node_get_nickname(const node_t *node)
Header file for nodelist.c.
Master header file for Tor-specific functionality.
long tor_parse_long(const char *s, int base, long min, long max, int *ok, char **next)
void predicted_ports_free_all(void)
Header file for predict_ports.c.
int tor_asprintf(char **strp, const char *fmt,...)
void rep_hist_note_dns_error(int type, uint8_t error)
static dns_stats_t dns_all_stats
static int parse_possibly_bad_iso_time(const char *s, time_t *time_out)
void rep_hist_note_desc_served(const char *desc)
void rep_hist_consensus_has_changed(const networkstatus_t *ns)
uint64_t rep_hist_get_drop_cell_received_count(void)
void rep_hist_buffer_stats_term(void)
time_t rep_hist_downrate_old_runs(time_t now)
uint64_t rep_hist_get_conn_created(bool from_listener, unsigned int type, int af)
void rep_hist_note_exit_bytes(uint16_t port, size_t num_written, size_t num_read)
static time_t start_of_hs_v2_stats_interval
void rep_hist_note_negotiated_link_proto(unsigned link_proto, int started_here)
static uint64_t stats_n_onionskin_assigned[MAX_ONION_STAT_TYPE+1]
void rep_hist_note_router_reachable(const char *id, const tor_addr_t *at_addr, const uint16_t at_port, time_t when)
uint64_t rephist_total_alloc
void rep_hist_seen_new_rp_cell(bool is_v2)
void rep_hist_note_conn_closed(bool from_listener, unsigned int type, int af)
static padding_counts_t padding_current
char * rep_hist_get_overload_stats_lines(void)
void rep_hist_desc_stats_term(void)
static hs_v3_stats_t * hs_v3_stats
static time_t stability_last_downrated
static long get_total_weighted_time(or_history_t *hist, time_t when)
static digestmap_t * served_descs
uint64_t rep_hist_get_n_dns_request(int type)
void rep_hist_hs_stats_term(void)
static time_t start_of_served_descs_stats_interval
void rep_hist_reset_padding_counts(void)
double rep_hist_get_stability(const char *id, time_t when)
static bool overload_happened_recently(time_t overload_time, int n_hours)
static void hs_v2_stats_free_(hs_v2_stats_t *victim_hs_v2_stats)
static uint32_t * exit_streams
#define EXIT_STATS_TOP_N_PORTS
static or_history_t * get_or_history(const char *id)
int rep_hist_load_mtbf_data(time_t now)
void rep_hist_note_circuit_handshake_dropped(uint16_t type)
char * rep_hist_get_overload_general_line(void)
void rep_hist_free_all(void)
static uint64_t * exit_bytes_written
STATIC int onion_handshakes_requested[MAX_ONION_STAT_TYPE+1]
static overload_stats_t overload_stats
#define EXIT_STATS_ROUND_UP_BYTES
int rep_hist_get_circuit_handshake_assigned(uint16_t type)
time_t rep_hist_desc_stats_write(time_t now)
static hs_v3_stats_t * hs_v3_stats_new(void)
void rep_hist_note_exit_stream(unsigned int cmd)
void rep_hist_buffer_stats_init(time_t now)
static uint64_t streams_begin_seen
uint64_t rep_hist_get_conn_rejected(unsigned int type, int af)
void rep_hist_exit_stats_init(time_t now)
static void hs_v3_stats_free_(hs_v3_stats_t *victim_hs_v3_stats)
void rep_hist_note_exit_stream_opened(uint16_t port)
char * rep_hist_get_padding_count_lines(void)
double rep_hist_get_weighted_fractional_uptime(const char *id, time_t when)
static uint64_t stats_n_read_limit_reached
void rep_hist_buffer_stats_add_circ(circuit_t *circ, time_t end_of_interval)
static int find_next_with(smartlist_t *sl, int i, const char *prefix)
uint64_t rep_hist_get_circuit_n_handshake_assigned(uint16_t type)
static double overload_onionskin_ntor_fraction
void rep_hist_note_dns_request(int type)
void rep_hist_padding_count_write(padding_type_t type)
long rep_hist_get_uptime(const char *id, time_t when)
static int buffer_stats_compare_entries_(const void **_a, const void **_b)
static digestmap_t * history_map
long rep_hist_get_weighted_time_known(const char *id, time_t when)
static padding_counts_t padding_published
void rep_hist_note_conn_rejected(unsigned int type, int af)
static uint64_t * exit_bytes_read
static dns_stats_t * get_dns_stats_by_type(const int type)
static char * rep_hist_format_desc_stats(time_t now)
void rep_hist_dump_stats(time_t now, int severity)
static time_t start_of_exit_stats_interval
static uint16_t onionskin_type_to_stat(uint16_t type)
static uint64_t stats_n_tcp_exhaustion
void rep_hist_reset_buffer_stats(time_t now)
static time_t start_of_buffer_stats_interval
void rep_hist_note_router_unreachable(const char *id, time_t when)
void rep_hist_hs_stats_init(time_t now)
void rep_hist_hsdir_stored_maybe_new_v3_onion(const uint8_t *blinded_key)
static time_t start_of_hs_v3_stats_interval
static overload_dns_stats_t overload_dns_stats
static uint64_t conn_num_opened_v4[2][CONN_TYPE_MAX_]
static overload_onionskin_assessment_t overload_onionskin_assessment
void rep_hist_log_link_protocol_counts(void)
uint32_t rephist_total_num
#define SET_TO_START_OF_HOUR(a)
void rep_hist_add_buffer_stats(double mean_num_cells_in_queue, double mean_time_cells_in_queue, uint32_t processed_cells)
static double get_weighted_fractional_uptime(or_history_t *hist, time_t when)
uint64_t rep_hist_get_conn_opened(bool from_listener, unsigned int type, int af)
void rep_hist_note_circuit_handshake_requested(uint16_t type)
static uint64_t conn_num_rejected_v4[CONN_TYPE_MAX_]
int rep_hist_get_circuit_handshake_requested(uint16_t type)
void rep_hist_note_conn_opened(bool from_listener, unsigned int type, int af)
void rep_history_clean(time_t before)
static hs_v2_stats_t * hs_v2_stats
static uint64_t streams_resolve_seen
void rep_hist_desc_stats_init(time_t now)
char * rep_hist_format_buffer_stats(time_t now)
uint64_t rep_hist_get_n_dns_error(int type, uint8_t error)
static void free_or_history(void *_hist)
#define EXIT_STATS_ROUND_UP_STREAMS
uint64_t rep_hist_get_n_read_limit_reached(void)
static uint64_t relay_circ_n_drop_cell_received
static void rep_hist_reset_hs_v2_stats(time_t now)
#define REND_CELLS_DELTA_F
static unsigned long total_descriptor_downloads
uint64_t rep_hist_get_n_tcp_exhaustion(void)
#define STABILITY_EPSILON
static smartlist_t * circuits_for_buffer_stats
static int compare_int_(const void *x, const void *y)
static time_t correct_time(time_t t, time_t now, time_t stored_at, time_t started_measuring)
STATIC char * rep_hist_format_hs_stats(time_t now, bool is_v3)
int rep_hist_record_mtbf_data(time_t now, int missing_means_down)
char * rep_hist_format_exit_stats(time_t now)
static uint64_t streams_begindir_seen
#define EXIT_STATS_NUM_PORTS
void rep_hist_reset_exit_stats(time_t now)
void rep_hist_padding_count_timers(uint64_t num_timers)
time_t rep_hist_hs_stats_write(time_t now, bool is_v3)
time_t rep_hist_buffer_stats_write(time_t now)
static uint64_t conn_num_created_v4[2][CONN_TYPE_MAX_]
uint64_t rep_hist_get_circuit_n_handshake_dropped(uint16_t type)
void rep_hist_make_router_pessimal(const char *id, time_t when)
void rep_hist_exit_stats_term(void)
void rep_hist_note_overload(overload_type_t overload)
#define STABILITY_INTERVAL
static int32_t overload_onionskin_ntor_period_secs
STATIC bool should_collect_v3_stats(void)
uint64_t rep_hist_get_exit_stream_seen(unsigned int cmd)
void rep_hist_log_circuit_handshake_stats(time_t now)
void rep_hist_note_circuit_handshake_assigned(uint16_t type)
#define MIN_CELL_COUNTS_TO_PUBLISH
static void rep_hist_reset_hs_v3_stats(time_t now)
void rep_hist_note_tcp_exhaustion(void)
void rep_hist_padding_count_read(padding_type_t type)
int rep_hist_have_measured_enough_stability(void)
static void rep_hist_reset_desc_stats(time_t now)
uint64_t rep_hist_get_n_write_limit_reached(void)
static void overload_general_onionskin_assessment(void)
time_t rep_hist_exit_stats_write(time_t now)
static hs_v2_stats_t * hs_v2_stats_new(void)
static double get_stability(or_history_t *hist, time_t when)
Header file for rephist.c.
@ PADDING_TYPE_ENABLED_CELL
@ PADDING_TYPE_ENABLED_TOTAL
#define REPHIST_CELL_PADDING_COUNTS_INTERVAL
#define MAX_ONION_STAT_TYPE
int tor_sscanf(const char *buf, const char *pattern,...)
void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern,...)
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out)
void smartlist_sort(smartlist_t *sl, int(*compare)(const void **a, const void **b))
smartlist_t * smartlist_new(void)
void smartlist_add(smartlist_t *sl, void *element)
void smartlist_clear(smartlist_t *sl)
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
#define SMARTLIST_FOREACH(sl, type, var, cmd)
int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep, int flags, int max)
double mean_num_cells_in_queue
double mean_time_cells_in_queue
struct timeval timestamp_created
uint64_t total_cell_waiting_time
unsigned long weighted_run_length
uint16_t last_reached_port
tor_addr_t last_reached_addr
time_t next_assessment_time
uint64_t n_ntor_requested
time_t next_assessment_time
uint64_t maximum_chanpad_timers
uint64_t read_pad_cell_count
uint64_t enabled_read_pad_cell_count
uint64_t write_pad_cell_count
char first_published_at[ISO_TIME_LEN+1]
uint64_t enabled_write_cell_count
uint64_t write_drop_cell_count
uint64_t write_cell_count
uint64_t enabled_read_cell_count
uint64_t enabled_write_pad_cell_count
uint64_t read_drop_cell_count
#define MOCK_IMPL(rv, funcname, arglist)
int parse_iso_time(const char *cp, time_t *t)
void format_iso_time(char *buf, time_t t)
void format_local_iso_time(char *buf, time_t t)
int strcmpstart(const char *s1, const char *s2)
int tor_digest_is_zero(const char *digest)