[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] Abstract v2/v3 "write stats to file" logic into a single function.



commit 6178a64fcf86b280890a222864ea1c09960d058f
Author: George Kadianakis <desnacked@xxxxxxxxxx>
Date:   Wed Oct 21 16:43:39 2020 +0300

    Abstract v2/v3 "write stats to file" logic into a single function.
---
 src/core/mainloop/mainloop.c |  4 +--
 src/feature/stats/rephist.c  | 63 +++++++++++++-------------------------------
 src/feature/stats/rephist.h  |  7 ++---
 3 files changed, 25 insertions(+), 49 deletions(-)

diff --git a/src/core/mainloop/mainloop.c b/src/core/mainloop/mainloop.c
index 25555a3f22..c64f0a8e82 100644
--- a/src/core/mainloop/mainloop.c
+++ b/src/core/mainloop/mainloop.c
@@ -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;
   }
diff --git a/src/feature/stats/rephist.c b/src/feature/stats/rephist.c
index ada19b447a..91def19019 100644
--- a/src/feature/stats/rephist.c
+++ b/src/feature/stats/rephist.c
@@ -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];
diff --git a/src/feature/stats/rephist.h b/src/feature/stats/rephist.h
index b2a4a5048d..c68b854242 100644
--- a/src/feature/stats/rephist.h
+++ b/src/feature/stats/rephist.h
@@ -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);
 



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits