Abstract v2/v3 "write stats to file" logic into a single function.

This commit is contained in:
George Kadianakis 2020-10-21 16:43:39 +03:00
parent 131da887d7
commit 6178a64fcf
3 changed files with 25 additions and 49 deletions

View File

@ -1937,11 +1937,11 @@ write_stats_file_callback(time_t now, const or_options_t *options)
next_time_to_write_stats_files = next_write;
}
if (options->HiddenServiceStatistics) {
time_t next_write = rep_hist_hs_v2_stats_write(now);
time_t next_write = rep_hist_hs_stats_write(now, false);
if (next_write && next_write < next_time_to_write_stats_files)
next_time_to_write_stats_files = next_write;
next_write = rep_hist_hs_v3_stats_write(now);
next_write = rep_hist_hs_stats_write(now, true);
if (next_write && next_write < next_time_to_write_stats_files)
next_time_to_write_stats_files = next_write;
}

View File

@ -2013,41 +2013,6 @@ rep_hist_format_hs_v2_stats(time_t now)
return hs_v2_stats_string;
}
/** If 24 hours have passed since the beginning of the current HS
* stats period, write buffer stats to $DATADIR/stats/hidserv-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_v2_stats_write(time_t now)
{
char *str = NULL;
if (!start_of_hs_v2_stats_interval) {
return 0; /* Not initialized. */
}
if (start_of_hs_v2_stats_interval + WRITE_STATS_INTERVAL > now) {
goto done; /* Not ready to write */
}
/* Generate history string. */
str = rep_hist_format_hs_v2_stats(now);
/* Reset HS history. */
rep_hist_reset_hs_v2_stats(now);
/* Try to write to disk. */
if (!check_or_create_data_subdir("stats")) {
write_to_data_subdir("stats", "hidserv-stats", str,
"hidden service stats");
}
done:
tor_free(str);
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 *
@ -2093,35 +2058,45 @@ rep_hist_format_hs_v3_stats(time_t now)
* 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. */
* write. Function works for both v2 and v3 stats depending on <b>is_v3</b>.
*/
time_t
rep_hist_hs_v3_stats_write(time_t now)
rep_hist_hs_stats_write(time_t now, bool is_v3)
{
char *str = NULL;
if (!start_of_hs_v3_stats_interval) {
time_t start_of_hs_stats_interval = is_v3 ?
start_of_hs_v3_stats_interval : start_of_hs_v2_stats_interval;
if (!start_of_hs_stats_interval) {
return 0; /* Not initialized. */
}
if (start_of_hs_v3_stats_interval + WRITE_STATS_INTERVAL > now) {
if (start_of_hs_stats_interval + WRITE_STATS_INTERVAL > now) {
goto done; /* Not ready to write */
}
/* Generate history string. */
str = rep_hist_format_hs_v3_stats(now);
str = is_v3 ?
rep_hist_format_hs_v3_stats(now) : rep_hist_format_hs_v2_stats(now);
/* Reset HS history. */
rep_hist_reset_hs_v3_stats(now);
if (is_v3) {
rep_hist_reset_hs_v3_stats(now);
} else {
rep_hist_reset_hs_v2_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");
write_to_data_subdir("stats",
is_v3 ? "hidserv-v3-stats" : "hidserv-stats",
str, "hidden service stats");
}
done:
tor_free(str);
return start_of_hs_v3_stats_interval + WRITE_STATS_INTERVAL;
return start_of_hs_stats_interval + WRITE_STATS_INTERVAL;
}
static uint64_t link_proto_count[MAX_LINK_PROTO+1][2];

View File

@ -63,13 +63,14 @@ void rep_hist_log_circuit_handshake_stats(time_t now);
MOCK_DECL(int, rep_hist_get_circuit_handshake_requested, (uint16_t type));
MOCK_DECL(int, rep_hist_get_circuit_handshake_assigned, (uint16_t type));
void rep_hist_hs_v2_stats_init(time_t now);
time_t rep_hist_hs_v2_stats_write(time_t now);
void rep_hist_hs_stats_init(time_t now);
void rep_hist_hs_stats_term(void);
time_t rep_hist_hs_stats_write(time_t now, bool is_v3);
char *rep_hist_get_hs_v2_stats_string(void);
void rep_hist_seen_new_rp_cell(bool is_v2);
void rep_hist_hsdir_stored_maybe_new_v2_onion(const crypto_pk_t *pubkey);
time_t rep_hist_hs_v3_stats_write(time_t now);
char *rep_hist_get_hs_v3_stats_string(void);
void rep_hist_hsdir_stored_maybe_new_v3_onion(const uint8_t *blinded_key);