Introduce v3 stat formatting functions.

They will be merged with the v2 ones in later commits.
This commit is contained in:
George Kadianakis 2020-10-21 14:43:29 +03:00
parent d0be2ae7f9
commit 5c00bee1b1

View File

@ -2048,6 +2048,82 @@ rep_hist_hs_v2_stats_write(time_t now)
return start_of_hs_v2_stats_interval + WRITE_STATS_INTERVAL;
}
/** Allocate and return a string containing hidden service stats that
* are meant to be placed in the extra-info descriptor. */
STATIC char *
rep_hist_format_hs_v3_stats(time_t now)
{
char t[ISO_TIME_LEN+1];
char *hs_v3_stats_string;
int64_t obfuscated_onions_seen, obfuscated_cells_seen;
uint64_t rounded_cells_seen
= round_uint64_to_next_multiple_of(hs_v3_stats->rp_v3_relay_cells_seen,
REND_CELLS_BIN_SIZE);
rounded_cells_seen = MIN(rounded_cells_seen, INT64_MAX);
obfuscated_cells_seen = add_laplace_noise((int64_t)rounded_cells_seen,
crypto_rand_double(),
REND_CELLS_DELTA_F, REND_CELLS_EPSILON);
uint64_t rounded_onions_seen =
round_uint64_to_next_multiple_of((size_t)digestmap_size(
hs_v3_stats->v3_onions_seen_this_period),
ONIONS_SEEN_BIN_SIZE);
rounded_onions_seen = MIN(rounded_onions_seen, INT64_MAX);
obfuscated_onions_seen = add_laplace_noise((int64_t)rounded_onions_seen,
crypto_rand_double(), ONIONS_SEEN_DELTA_F,
ONIONS_SEEN_EPSILON);
format_iso_time(t, now);
tor_asprintf(&hs_v3_stats_string, "hidserv-v3-stats-end %s (%d s)\n"
"hidserv-rend-v3-relayed-cells %"PRId64" delta_f=%d "
"epsilon=%.2f bin_size=%d\n"
"hidserv-dir-v3-onions-seen %"PRId64" delta_f=%d "
"epsilon=%.2f bin_size=%d\n",
t, (unsigned) (now - start_of_hs_v3_stats_interval),
(obfuscated_cells_seen), REND_CELLS_DELTA_F,
REND_CELLS_EPSILON, REND_CELLS_BIN_SIZE,
(obfuscated_onions_seen), ONIONS_SEEN_DELTA_F,
ONIONS_SEEN_EPSILON, ONIONS_SEEN_BIN_SIZE);
return hs_v3_stats_string;
}
/** If 24 hours have passed since the beginning of the current HS
* stats period, write buffer stats to $DATADIR/stats/hidserv-v3-stats
* (possibly overwriting an existing file) and reset counters. Return
* when we would next want to write buffer stats or 0 if we never want to
* write. */
time_t
rep_hist_hs_v3_stats_write(time_t now)
{
char *str = NULL;
if (!start_of_hs_v3_stats_interval) {
return 0; /* Not initialized. */
}
if (start_of_hs_v3_stats_interval + WRITE_STATS_INTERVAL > now) {
goto done; /* Not ready to write */
}
/* Generate history string. */
str = rep_hist_format_hs_v3_stats(now);
/* Reset HS history. */
rep_hist_reset_hs_v3_stats(now);
/* Try to write to disk. */
if (!check_or_create_data_subdir("stats")) {
write_to_data_subdir("stats", "hidserv-v3-stats", str,
"hidden service stats");
}
done:
tor_free(str);
return start_of_hs_v3_stats_interval + WRITE_STATS_INTERVAL;
}
static uint64_t link_proto_count[MAX_LINK_PROTO+1][2];
/** Note that we negotiated link protocol version <b>link_proto</b>, on