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;
302 return &dns_PTR_stats;
304 return &dns_AAAA_stats;
317 if (BUG(!dns_stats)) {
323 return dns_stats->stats_n_error_none;
325 return dns_stats->stats_n_error_format;
326 case DNS_ERR_SERVERFAILED:
327 return dns_stats->stats_n_error_serverfailed;
328 case DNS_ERR_NOTEXIST:
329 return dns_stats->stats_n_error_notexist;
330 case DNS_ERR_NOTIMPL:
331 return dns_stats->stats_n_error_notimpl;
332 case DNS_ERR_REFUSED:
333 return dns_stats->stats_n_error_refused;
334 case DNS_ERR_TRUNCATED:
335 return dns_stats->stats_n_error_truncated;
336 case DNS_ERR_UNKNOWN:
337 return dns_stats->stats_n_error_unknown;
338 case DNS_ERR_TIMEOUT:
339 return dns_stats->stats_n_error_tor_timeout;
340 case DNS_ERR_SHUTDOWN:
341 return dns_stats->stats_n_error_shutdown;
343 return dns_stats->stats_n_error_cancel;
345 return dns_stats->stats_n_error_nodata;
358 if (BUG(!dns_stats)) {
361 return dns_stats->stats_n_request;
388 dns_stats->stats_n_error_none++;
391 dns_stats->stats_n_error_format++;
393 case DNS_ERR_SERVERFAILED:
394 dns_stats->stats_n_error_serverfailed++;
396 case DNS_ERR_NOTEXIST:
397 dns_stats->stats_n_error_notexist++;
399 case DNS_ERR_NOTIMPL:
400 dns_stats->stats_n_error_notimpl++;
402 case DNS_ERR_REFUSED:
403 dns_stats->stats_n_error_refused++;
405 case DNS_ERR_TRUNCATED:
406 dns_stats->stats_n_error_truncated++;
408 case DNS_ERR_UNKNOWN:
409 dns_stats->stats_n_error_unknown++;
411 case DNS_ERR_TIMEOUT:
412 dns_stats->stats_n_error_tor_timeout++;
414 case DNS_ERR_SHUTDOWN:
415 dns_stats->stats_n_error_shutdown++;
418 dns_stats->stats_n_error_cancel++;
421 dns_stats->stats_n_error_nodata++;
434 if (BUG(!dns_stats)) {
437 dns_stats->stats_n_request++;
447 if (overload_time >
approx_time() - 3600 * n_hours) {
454#define OVERLOAD_STATS_VERSION 1
467 return stats_n_write_limit_reached;
476 char tbuf[ISO_TIME_LEN+1];
482 OVERLOAD_STATS_VERSION, tbuf);
495 char tbuf[ISO_TIME_LEN+1];
502 "overload-ratelimits %d %s %" PRIu64
" %" PRIu64
503 " %" PRIu64
" %" PRIu64
"\n",
504 OVERLOAD_STATS_VERSION, tbuf,
515 OVERLOAD_STATS_VERSION, tbuf);
519 if (smartlist_len(chunks) == 0) {
527 smartlist_free(chunks);
532#define SET_TO_START_OF_HOUR(a) STMT_BEGIN \
533 (a) = approx_time() - (approx_time() % 3600); \
540 static time_t last_read_counted = 0;
541 static time_t last_write_counted = 0;
544 case OVERLOAD_GENERAL:
547 case OVERLOAD_READ: {
556 case OVERLOAD_WRITE: {
557 stats_n_write_limit_reached++;
565 case OVERLOAD_FD_EXHAUSTED:
631 const uint16_t at_port, time_t when)
635 char tbuf[ISO_TIME_LEN+1];
636 int addr_changed, port_changed;
639 tor_assert((!at_addr && !at_port) || (at_addr && at_port));
646 if (!started_tracking_stability)
647 started_tracking_stability = time(NULL);
652 if (hist->start_of_downtime) {
656 log_info(
LD_HIST,
"Router %s is now Running; it had been down since %s.",
659 log_info(
LD_HIST,
" (Paradoxically, it was already Running too.)");
661 down_length = when - hist->start_of_downtime;
662 hist->total_weighted_time += down_length;
663 hist->start_of_downtime = 0;
664 }
else if (addr_changed || port_changed) {
676 penalty = (int)(fresh_interval + live_interval) / 2;
679 log_info(
LD_HIST,
"Router %s still seems Running, but its address appears "
680 "to have changed since the last time it was reachable. I'm "
681 "going to treat it as having been down for %d seconds",
688 log_debug(
LD_HIST,
"Router %s is still Running; it has been Running "
691 log_info(
LD_HIST,
"Router %s is now Running; it was previously untracked",
706 char tbuf[ISO_TIME_LEN+1];
708 if (!started_tracking_stability)
709 started_tracking_stability = time(NULL);
720 if (run_length < 0) {
721 unsigned long penalty = -run_length;
722#define SUBTRACT_CLAMPED(var, penalty) \
723 do { (var) = (var) < (penalty) ? 0 : (var) - (penalty); } while (0)
726 SUBTRACT_CLAMPED(hist->weighted_uptime, penalty);
729 hist->weighted_uptime += run_length;
730 hist->total_weighted_time += run_length;
733 log_info(
LD_HIST,
"Router %s is now non-Running: it had previously been "
734 "Running since %s. Its total weighted uptime is %lu/%lu.",
736 hist->total_weighted_time);
738 if (!hist->start_of_downtime) {
739 hist->start_of_downtime = when;
742 log_info(
LD_HIST,
"Router %s is now non-Running; it was previously "
748 log_info(
LD_HIST,
"Router %s is still non-Running; it has been "
765 hist->weighted_uptime = 0;
773 digestmap_iter_t *orhist_it;
792 log_info(
LD_HIST,
"Discounting all old stability info by a factor of %f",
797 !digestmap_iter_done(orhist_it);
798 orhist_it = digestmap_iter_next(
history_map,orhist_it)) {
799 digestmap_iter_get(orhist_it, &digest1, &hist_p);
806 hist->weighted_uptime = (
unsigned long)(hist->weighted_uptime * alpha);
807 hist->total_weighted_time = (
unsigned long)
808 (hist->total_weighted_time * alpha);
825 total_weights += 1.0;
832 return total / total_weights;
840 long total = hist->total_weighted_time;
843 }
else if (hist->start_of_downtime) {
844 total += (when - hist->start_of_downtime);
854 long total = hist->total_weighted_time;
855 long up = hist->weighted_uptime;
861 }
else if (hist->start_of_downtime) {
862 total += (when - hist->start_of_downtime);
872 return ((
double) up) / total;
936 return started_tracking_stability < time(NULL) - 4*60*60;
945 digestmap_iter_t *orhist_it;
946 const char *name1, *digest1;
954 tor_log(severity,
LD_HIST,
"--------------- Dumping history information:");
957 !digestmap_iter_done(orhist_it);
958 orhist_it = digestmap_iter_next(
history_map,orhist_it)) {
961 digestmap_iter_get(orhist_it, &digest1, &or_history_p);
972 "OR %s [%s]: wmtbf %lu:%02lu:%02lu",
974 stability/3600, (stability/60)%60, stability%60);
987 digestmap_iter_t *orhist_it;
991 while (!digestmap_iter_done(orhist_it)) {
993 digestmap_iter_get(orhist_it, &d1, &or_history_p);
994 or_history = or_history_p;
996 should_remove = authority ?
999 : (or_history->
changed < before);
1000 if (should_remove) {
1001 orhist_it = digestmap_iter_next_rmv(
history_map, orhist_it);
1005 orhist_it = digestmap_iter_next(
history_map, orhist_it);
1017 char time_buf[ISO_TIME_LEN+1];
1019 digestmap_iter_t *orhist_it;
1027 char *filename = get_datadir_fname(
"router-stability");
1044#define PUT(s) STMT_BEGIN if (fputs((s),f)<0) goto err; STMT_END
1045#define PRINTF(args) STMT_BEGIN if (fprintf args <0) goto err; STMT_END
1050 PRINTF((f,
"stored-at %s\n", time_buf));
1052 if (started_tracking_stability) {
1054 PRINTF((f,
"tracked-since %s\n", time_buf));
1058 PRINTF((f,
"last-downrated %s\n", time_buf));
1066 for (orhist_it = digestmap_iter_init(
history_map);
1067 !digestmap_iter_done(orhist_it);
1068 orhist_it = digestmap_iter_next(
history_map,orhist_it)) {
1070 const char *t = NULL;
1071 digestmap_iter_get(orhist_it, &digest, &or_history_p);
1082 "Relay '%s' is listed as up in rephist, but it's not in "
1083 "our routerlist. Correcting.", dbuf);
1087 PRINTF((f,
"R %s\n", dbuf));
1092 PRINTF((f,
"+MTBF %lu %.5f%s%s\n",
1094 t ?
" S=" :
"", t ? t :
""));
1096 if (hist->start_of_downtime > 0) {
1100 PRINTF((f,
"+WFU %lu %lu%s%s\n",
1101 hist->weighted_uptime, hist->total_weighted_time,
1102 t ?
" S=" :
"", t ? t :
""));
1122 for ( ; i < smartlist_len(sl); ++i) {
1123 const char *line = smartlist_get(sl, i);
1141 strlcpy(b, s,
sizeof(b));
1158correct_time(time_t t, time_t now, time_t stored_at, time_t started_measuring)
1160 if (t < started_measuring - 24*60*60*365)
1162 else if (t < started_measuring)
1163 return started_measuring;
1164 else if (t > stored_at)
1167 long run_length = stored_at - t;
1168 t = (time_t)(now - run_length);
1169 if (t < started_measuring)
1170 t = started_measuring;
1182 const char *line = NULL;
1184 time_t last_downrated = 0, stored_at = 0, tracked_since = 0;
1185 time_t latest_possible_start = now;
1189 char *filename = get_datadir_fname(
"router-stability");
1200 const char *firstline;
1201 if (smartlist_len(lines)>4) {
1202 firstline = smartlist_get(lines, 0);
1205 10, -1, LONG_MAX, NULL, NULL);
1208 if (format != 1 && format != 2) {
1210 "Unrecognized format in mtbf history file. Skipping.");
1213 for (i = 1; i < smartlist_len(lines); ++i) {
1214 line = smartlist_get(lines, i);
1215 if (!strcmp(line,
"data"))
1218 if (
parse_iso_time(line+strlen(
"last-downrated "), &last_downrated)<0)
1219 log_warn(
LD_HIST,
"Couldn't parse downrate time in mtbf "
1224 log_warn(
LD_HIST,
"Couldn't parse stored time in mtbf "
1228 if (
parse_iso_time(line+strlen(
"tracked-since "), &tracked_since)<0)
1229 log_warn(
LD_HIST,
"Couldn't parse started-tracking time in mtbf "
1233 if (last_downrated > now)
1234 last_downrated = now;
1235 if (tracked_since > now)
1236 tracked_since = now;
1239 log_warn(
LD_HIST,
"No stored time recorded.");
1243 if (line && !strcmp(line,
"data"))
1248 for (; i < smartlist_len(lines); ++i) {
1251 char mtbf_timebuf[ISO_TIME_LEN+1];
1252 char wfu_timebuf[ISO_TIME_LEN+1];
1253 time_t start_of_run = 0;
1254 time_t start_of_downtime = 0;
1255 int have_mtbf = 0, have_wfu = 0;
1258 long wt_uptime = 0, total_wt_time = 0;
1261 line = smartlist_get(lines, i);
1262 if (!strcmp(line,
"."))
1265 mtbf_timebuf[0] =
'\0';
1266 wfu_timebuf[0] =
'\0';
1269 n =
tor_sscanf(line,
"%40s %ld %lf S=%10s %8s",
1270 hexbuf, &wrl, &trw, mtbf_timebuf, mtbf_timebuf+11);
1271 if (n != 3 && n != 5) {
1278 int mtbf_idx, wfu_idx;
1281 strlcpy(hexbuf, line+2,
sizeof(hexbuf));
1284 if (mtbf_idx >= 0) {
1285 const char *mtbfline = smartlist_get(lines, mtbf_idx);
1286 n =
tor_sscanf(mtbfline,
"+MTBF %lu %lf S=%10s %8s",
1287 &wrl, &trw, mtbf_timebuf, mtbf_timebuf+11);
1288 if (n == 2 || n == 4) {
1291 log_warn(
LD_HIST,
"Couldn't scan +MTBF line %s",
1296 const char *wfuline = smartlist_get(lines, wfu_idx);
1297 n =
tor_sscanf(wfuline,
"+WFU %lu %lu S=%10s %8s",
1298 &wt_uptime, &total_wt_time,
1299 wfu_timebuf, wfu_timebuf+11);
1300 if (n == 2 || n == 4) {
1303 log_warn(
LD_HIST,
"Couldn't scan +WFU line %s",
escaped(wfuline));
1321 if (mtbf_timebuf[0]) {
1322 mtbf_timebuf[10] =
' ';
1324 log_warn(
LD_HIST,
"Couldn't parse time %s",
1330 latest_possible_start = (time_t)(hist->
start_of_run - wrl);
1336 if (wfu_timebuf[0]) {
1337 wfu_timebuf[10] =
' ';
1339 log_warn(
LD_HIST,
"Couldn't parse time %s",
escaped(wfu_timebuf));
1342 hist->start_of_downtime =
correct_time(start_of_downtime, now, stored_at,
1344 hist->weighted_uptime = wt_uptime;
1345 hist->total_weighted_time = total_wt_time;
1347 if (strcmp(line,
"."))
1348 log_warn(
LD_HIST,
"Truncated MTBF file.");
1350 if (tracked_since < 86400*365)
1351 tracked_since = latest_possible_start;
1354 started_tracking_stability = tracked_since;
1361 smartlist_free(lines);
1369#define EXIT_STATS_ROUND_UP_BYTES 1024
1371#define EXIT_STATS_ROUND_UP_STREAMS 4
1373#define EXIT_STATS_NUM_PORTS 65536
1375#define EXIT_STATS_TOP_N_PORTS 10
1428 return (*(
int*)x - *(
int*)y);
1437 int i, j, top_elements = 0, cur_min_idx = 0, cur_port;
1440 uint64_t cur_bytes = 0, other_read = 0, other_written = 0,
1441 total_read = 0, total_written = 0;
1442 uint32_t total_streams = 0, other_streams = 0;
1443 smartlist_t *written_strings, *read_strings, *streams_strings;
1444 char *written_string, *read_string, *streams_string;
1445 char t[ISO_TIME_LEN+1];
1484 if (cur_bytes == 0) {
1488 top_bytes[top_elements] = cur_bytes;
1489 top_ports[top_elements++] = i;
1490 }
else if (cur_bytes > top_bytes[cur_min_idx]) {
1491 top_bytes[cur_min_idx] = cur_bytes;
1492 top_ports[cur_min_idx] = i;
1497 for (j = 1; j < top_elements; j++) {
1498 if (top_bytes[j] < top_bytes[cur_min_idx]) {
1508 other_read = total_read;
1509 other_written = total_written;
1510 other_streams = total_streams;
1513 qsort(top_ports, top_elements,
sizeof(
int),
compare_int_);
1514 for (j = 0; j < top_elements; j++) {
1515 cur_port = top_ports[j];
1546 other_written /= 1024;
1565 smartlist_free(written_strings);
1566 smartlist_free(read_strings);
1567 smartlist_free(streams_strings);
1572 "exit-kibibytes-written %s\n"
1573 "exit-kibibytes-read %s\n"
1574 "exit-streams-opened %s\n",
1599 log_info(
LD_HIST,
"Writing exit port statistics to disk.");
1627 log_debug(
LD_HIST,
"Written %lu bytes and read %lu bytes to/from an "
1628 "exit connection to port %d.",
1629 (
unsigned long)num_written, (
unsigned long)num_read, port);
1639 log_debug(
LD_HIST,
"Opened exit stream to port %d", port);
1656 case RELAY_COMMAND_BEGIN:
1659 case RELAY_COMMAND_BEGIN_DIR:
1662 case RELAY_COMMAND_RESOLVE:
1666 tor_assert_nonfatal_unreached_once();
1676 case RELAY_COMMAND_BEGIN:
1678 case RELAY_COMMAND_BEGIN_DIR:
1680 case RELAY_COMMAND_RESOLVE:
1689#define CONN_DIRECTION_INITIATED 0
1690#define CONN_DIRECTION_RECEIVED 1
1692#define CONN_DIRECTION(from_listener) \
1693 (from_listener) ? CONN_DIRECTION_RECEIVED : CONN_DIRECTION_INITIATED
1697static uint64_t conn_num_created_v6[2][CONN_TYPE_MAX_];
1700static uint64_t conn_num_opened_v6[2][CONN_TYPE_MAX_];
1703static uint64_t conn_num_rejected_v6[CONN_TYPE_MAX_];
1711 unsigned int dir = CONN_DIRECTION(from_listener);
1719 conn_num_created_v6[dir][type]++;
1720 conn_num_opened_v6[dir][type]++;
1734 unsigned int dir = CONN_DIRECTION(from_listener);
1743 if (conn_num_opened_v6[dir][type] > 0) {
1744 conn_num_opened_v6[dir][type]--;
1764 conn_num_rejected_v6[type]++;
1777 unsigned int dir = CONN_DIRECTION(from_listener);
1782 return conn_num_created_v6[dir][type];
1793 unsigned int dir = CONN_DIRECTION(from_listener);
1798 return conn_num_opened_v6[dir][type];
1813 return conn_num_rejected_v6[type];
1851 double mean_time_cells_in_queue, uint32_t processed_cells)
1871 time_t start_of_interval;
1872 int interval_length;
1874 double mean_num_cells_in_queue, mean_time_cells_in_queue;
1875 uint32_t processed_cells;
1885 interval_length = (int) (end_of_interval - start_of_interval);
1886 if (interval_length <= 0)
1891 (
double) interval_length / 1000.0 / 2.0;
1892 mean_time_cells_in_queue =
1898 mean_time_cells_in_queue,
1944 uint64_t processed_cells[SHARES];
1945 uint32_t circs_in_share[SHARES];
1946 int number_of_circuits, i;
1947 double queued_cells[SHARES], time_in_queue[SHARES];
1948 smartlist_t *processed_cells_strings, *queued_cells_strings,
1949 *time_in_queue_strings;
1950 char *processed_cells_string, *queued_cells_string,
1951 *time_in_queue_string;
1952 char t[ISO_TIME_LEN+1];
1961 memset(processed_cells, 0, SHARES *
sizeof(uint64_t));
1962 memset(circs_in_share, 0, SHARES *
sizeof(uint32_t));
1963 memset(queued_cells, 0, SHARES *
sizeof(
double));
1964 memset(time_in_queue, 0, SHARES *
sizeof(
double));
1968 if (number_of_circuits > 0) {
1975 int share = i++ * SHARES / number_of_circuits;
1976 processed_cells[share] += stats->processed_cells;
1977 queued_cells[share] += stats->mean_num_cells_in_queue;
1978 time_in_queue[share] += stats->mean_time_cells_in_queue;
1979 circs_in_share[share]++;
1981 SMARTLIST_FOREACH_END(stats);
1988 for (i = 0; i < SHARES; i++) {
1990 "%"PRIu64, !circs_in_share[i] ? 0 :
1991 (processed_cells[i] /
1992 circs_in_share[i]));
1994 for (i = 0; i < SHARES; i++) {
1996 circs_in_share[i] == 0 ? 0.0 :
1997 queued_cells[i] / (
double) circs_in_share[i]);
1999 for (i = 0; i < SHARES; i++) {
2001 circs_in_share[i] == 0 ? 0.0 :
2002 time_in_queue[i] / (
double) circs_in_share[i]);
2015 smartlist_free(processed_cells_strings);
2016 smartlist_free(queued_cells_strings);
2017 smartlist_free(time_in_queue_strings);
2022 "cell-processed-cells %s\n"
2023 "cell-queued-cells %s\n"
2024 "cell-time-in-queue %s\n"
2025 "cell-circuits-per-decile %d\n",
2027 processed_cells_string,
2028 queued_cells_string,
2029 time_in_queue_string,
2030 CEIL_DIV(number_of_circuits, SHARES));
2057 SMARTLIST_FOREACH_END(circ);
2094 log_warn(
LD_BUG,
"Called rep_hist_desc_stats_init() when desc stats were "
2095 "already initialized. This is probably harmless.");
2129 char t[ISO_TIME_LEN+1];
2132 digestmap_iter_t *iter;
2136 int *vals, max = 0, q3 = 0, md = 0, q1 = 0, min = 0;
2144 vals = tor_calloc(size,
sizeof(
int));
2146 !digestmap_iter_done(iter);
2149 digestmap_iter_get(iter, &key, &val);
2150 count = (uintptr_t)val;
2151 vals[n++] = (int)count;
2154 max = find_nth_int(vals, size, size-1);
2155 q3 = find_nth_int(vals, size, (3*size-1)/4);
2156 md = find_nth_int(vals, size, (size-1)/2);
2157 q1 = find_nth_int(vals, size, (size-1)/4);
2158 min = find_nth_int(vals, size, 0);
2165 "served-descs-stats-end %s (%d s) total=%lu unique=%u "
2166 "max=%d q3=%d md=%d q1=%d min=%d\n",
2170 size, max, q3, md, q1, min);
2183 char *filename = NULL, *str = NULL;
2196 filename = get_datadir_fname2(
"stats",
"served-desc-stats");
2198 log_warn(
LD_HIST,
"Unable to write served descs statistics to disk!");
2219 count = (uintptr_t)val;
2220 if (count != INT_MAX)
2246#define OVERLOAD_ONIONSKIN_NTOR_PERCENT_SCALE 1000.0
2247#define OVERLOAD_ONIONSKIN_NTOR_PERCENT_DEFAULT 1000
2248#define OVERLOAD_ONIONSKIN_NTOR_PERCENT_MIN 0
2249#define OVERLOAD_ONIONSKIN_NTOR_PERCENT_MAX 100000
2255 OVERLOAD_ONIONSKIN_NTOR_PERCENT_DEFAULT /
2256 OVERLOAD_ONIONSKIN_NTOR_PERCENT_SCALE / 100.0;
2261#define OVERLOAD_ONIONSKIN_NTOR_PERIOD_SECS_DEFAULT (60 * 60 * 6)
2262#define OVERLOAD_ONIONSKIN_NTOR_PERIOD_SECS_MIN 0
2263#define OVERLOAD_ONIONSKIN_NTOR_PERIOD_SECS_MAX INT32_MAX
2269 OVERLOAD_ONIONSKIN_NTOR_PERIOD_SECS_DEFAULT;
2296static inline uint16_t
2333 OVERLOAD_ONIONSKIN_NTOR_PERCENT_SCALE) {
2341 log_notice(
LD_HIST,
"General overload -> Ntor dropped (%" PRIu64
") "
2342 "fraction %.4f%% is above threshold of %.4f%%",
2369 if (stat == ONION_HANDSHAKE_TYPE_NTOR ||
2370 stat == ONION_HANDSHAKE_TYPE_NTOR_V3) {
2394 stats_n_onionskin_dropped[stat]++;
2397 if (stat == ONION_HANDSHAKE_TYPE_NTOR ||
2398 stat == ONION_HANDSHAKE_TYPE_NTOR_V3) {
2437 log_notice(
LD_HEARTBEAT,
"Circuit handshake stats since last time: "
2438 "%d/%d TAP, %d/%d NTor, %d/%d NTor (v3).",
2439 onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_TAP],
2441 onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_NTOR],
2443 onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_NTOR_V3],
2445 memset(onion_handshakes_assigned, 0,
sizeof(onion_handshakes_assigned));
2461static hs_v2_stats_t *
2464 hs_v2_stats_t *new_hs_v2_stats = tor_malloc_zero(
sizeof(hs_v2_stats_t));
2466 return new_hs_v2_stats;
2469#define hs_v2_stats_free(val) \
2470 FREE_AND_NULL(hs_v2_stats_t, hs_v2_stats_free_, (val))
2476 if (!victim_hs_v2_stats) {
2533static hs_v3_stats_t *
2536 hs_v3_stats_t *new_hs_v3_stats = tor_malloc_zero(
sizeof(hs_v3_stats_t));
2537 new_hs_v3_stats->v3_onions_seen_this_period = digest256map_new();
2539 return new_hs_v3_stats;
2542#define hs_v3_stats_free(val) \
2543 FREE_AND_NULL(hs_v3_stats_t, hs_v3_stats_free_, (val))
2549 if (!victim_hs_v3_stats) {
2553 digest256map_free(victim_hs_v3_stats->v3_onions_seen_this_period, NULL);
2566 digest256map_free(
hs_v3_stats->v3_onions_seen_this_period, NULL);
2567 hs_v3_stats->v3_onions_seen_this_period = digest256map_new();
2601 !!digest256map_get(
hs_v3_stats->v3_onions_seen_this_period,
2604 log_info(
LD_GENERAL,
"Considering v3 descriptor with %s (%sseen before)",
2605 safe_str(
hex_str((
char*)blinded_key, 32)),
2606 seen_before ?
"" :
"not ");
2610 digest256map_set(
hs_v3_stats->v3_onions_seen_this_period,
2611 blinded_key, (
void*)(uintptr_t)1);
2620 log_debug(
LD_GENERAL,
"New RP cell (%d)", is_v2);
2664#define REND_CELLS_DELTA_F 2048
2668#define REND_CELLS_EPSILON 0.3
2671#define REND_CELLS_BIN_SIZE 1024
2675#define ONIONS_SEEN_DELTA_F 8
2679#define ONIONS_SEEN_EPSILON 0.3
2682#define ONIONS_SEEN_BIN_SIZE 8
2691 char t[ISO_TIME_LEN+1];
2692 char *hs_stats_string;
2693 int64_t obfuscated_onions_seen, obfuscated_cells_seen;
2695 uint64_t rp_cells_seen = is_v3 ?
2697 size_t onions_seen = is_v3 ?
2698 digest256map_size(
hs_v3_stats->v3_onions_seen_this_period) : 0;
2699 time_t start_of_hs_stats_interval = is_v3 ?
2702 uint64_t rounded_cells_seen
2704 rounded_cells_seen = MIN(rounded_cells_seen, INT64_MAX);
2709 uint64_t rounded_onions_seen =
2711 rounded_onions_seen = MIN(rounded_onions_seen, INT64_MAX);
2714 ONIONS_SEEN_EPSILON);
2718 "%s %"PRId64
" delta_f=%d epsilon=%.2f bin_size=%d\n"
2719 "%s %"PRId64
" delta_f=%d epsilon=%.2f bin_size=%d\n",
2720 is_v3 ?
"hidserv-v3-stats-end" :
"hidserv-stats-end",
2721 t, (
unsigned) (now - start_of_hs_stats_interval),
2723 "hidserv-rend-v3-relayed-cells" :
"hidserv-rend-relayed-cells",
2725 REND_CELLS_EPSILON, REND_CELLS_BIN_SIZE,
2726 is_v3 ?
"hidserv-dir-v3-onions-seen" :
"hidserv-dir-onions-seen",
2727 obfuscated_onions_seen, ONIONS_SEEN_DELTA_F,
2728 ONIONS_SEEN_EPSILON, ONIONS_SEEN_BIN_SIZE);
2730 return hs_stats_string;
2744 time_t start_of_hs_stats_interval = is_v3 ?
2747 if (!start_of_hs_stats_interval) {
2751 if (start_of_hs_stats_interval + WRITE_STATS_INTERVAL > now) {
2768 is_v3 ?
"hidserv-v3-stats" :
"hidserv-stats",
2769 str,
"hidden service stats");
2774 return start_of_hs_stats_interval + WRITE_STATS_INTERVAL;
2777static uint64_t link_proto_count[MAX_LINK_PROTO+1][2];
2785 started_here = !!started_here;
2786 if (link_proto > MAX_LINK_PROTO) {
2787 log_warn(
LD_BUG,
"Can't log link protocol %u", link_proto);
2791 link_proto_count[link_proto][started_here]++;
2877#define MIN_CELL_COUNTS_TO_PUBLISH 1
2878#define ROUND_CELL_COUNTS_TO 10000
2880rep_hist_prep_published_padding_counts(time_t now)
2891#define ROUND_AND_SET_COUNT(x) (x) = round_uint64_to_next_multiple_of((x), \
2892 ROUND_CELL_COUNTS_TO)
2903#undef ROUND_AND_SET_COUNT
2913 char *result = NULL;
2922 " write-drop=%"PRIu64
2923 " write-pad=%"PRIu64
2924 " write-total=%"PRIu64
2925 " read-drop=%"PRIu64
2927 " read-total=%"PRIu64
2928 " enabled-read-pad=%"PRIu64
2929 " enabled-read-total=%"PRIu64
2930 " enabled-write-pad=%"PRIu64
2931 " enabled-write-total=%"PRIu64
2932 " max-chanpad-timers=%"PRIu64
2936 (uint64_t)ROUND_CELL_COUNTS_TO,
2961 for (
int i = 1; i <= MAX_LINK_PROTO; i++) {
2963 tor_asprintf(&line,
"initiated %"PRIu64
" and received "
2964 "%"PRIu64
" v%d connections", link_proto_count[i][1],
2965 link_proto_count[i][0], i);
2971 log_notice(
LD_HEARTBEAT,
"Since startup we %s.", log_line);
2974 smartlist_free(lines);
3013 OVERLOAD_ONIONSKIN_NTOR_PERCENT_DEFAULT,
3014 OVERLOAD_ONIONSKIN_NTOR_PERCENT_MIN,
3015 OVERLOAD_ONIONSKIN_NTOR_PERCENT_MAX) /
3016 OVERLOAD_ONIONSKIN_NTOR_PERCENT_SCALE / 100.0;
3020 OVERLOAD_ONIONSKIN_NTOR_PERIOD_SECS_DEFAULT,
3021 OVERLOAD_ONIONSKIN_NTOR_PERIOD_SECS_MIN,
3022 OVERLOAD_ONIONSKIN_NTOR_PERIOD_SECS_MAX);
3025#ifdef TOR_UNIT_TESTS
3027const hs_v2_stats_t *
3028rep_hist_get_hs_v2_stats(
void)
3034const hs_v3_stats_t *
3035rep_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)
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 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)