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

[tor-commits] [tor/master] connstats: extract connection type counts into a structure.



commit 0b5e19d22312291d5c9f8a5e8f8f3e6ad2ef4920
Author: Nick Mathewson <nickm@xxxxxxxxxxxxxx>
Date:   Fri Jul 10 09:32:30 2020 -0400

    connstats: extract connection type counts into a structure.
---
 src/feature/stats/connstats.c | 58 +++++++++++++++++++++++--------------------
 src/feature/stats/connstats.h |  2 +-
 2 files changed, 32 insertions(+), 28 deletions(-)

diff --git a/src/feature/stats/connstats.c b/src/feature/stats/connstats.c
index 82b1adcd9..2879a3018 100644
--- a/src/feature/stats/connstats.c
+++ b/src/feature/stats/connstats.c
@@ -48,22 +48,28 @@ conn_stats_init(time_t now)
 /** Start of next BIDI_INTERVAL second interval. */
 static time_t bidi_next_interval = 0;
 
-/** Number of connections that we read and wrote less than BIDI_THRESHOLD
- * bytes from/to in BIDI_INTERVAL seconds. */
-static uint32_t below_threshold = 0;
+/** A single grouped set of connection type counts. */
+typedef struct conn_counts_t {
+  /** Number of connections that we read and wrote less than BIDI_THRESHOLD
+   * bytes from/to in BIDI_INTERVAL seconds. */
+  uint32_t below_threshold;
 
-/** Number of connections that we read at least BIDI_FACTOR times more
- * bytes from than we wrote to in BIDI_INTERVAL seconds. */
-static uint32_t mostly_read = 0;
+  /** Number of connections that we read at least BIDI_FACTOR times more
+   * bytes from than we wrote to in BIDI_INTERVAL seconds. */
+  uint32_t mostly_read;
 
-/** Number of connections that we wrote at least BIDI_FACTOR times more
- * bytes to than we read from in BIDI_INTERVAL seconds. */
-static uint32_t mostly_written = 0;
+  /** Number of connections that we wrote at least BIDI_FACTOR times more
+   * bytes to than we read from in BIDI_INTERVAL seconds. */
+  uint32_t mostly_written;
 
-/** Number of connections that we read and wrote at least BIDI_THRESHOLD
- * bytes from/to, but not BIDI_FACTOR times more in either direction in
- * BIDI_INTERVAL seconds. */
-static uint32_t both_read_and_written = 0;
+  /** Number of connections that we read and wrote at least BIDI_THRESHOLD
+   * bytes from/to, but not BIDI_FACTOR times more in either direction in
+   * BIDI_INTERVAL seconds. */
+  uint32_t both_read_and_written;
+} conn_counts_t ;
+
+/** A collection of connection counts, over all OR connections. */
+static conn_counts_t counts;
 
 /** Entry in a map from connection ID to the number of read and written
  * bytes on this connection in a BIDI_INTERVAL second interval. */
@@ -116,10 +122,7 @@ void
 conn_stats_reset(time_t now)
 {
   start_of_conn_stats_interval = now;
-  below_threshold = 0;
-  mostly_read = 0;
-  mostly_written = 0;
-  both_read_and_written = 0;
+  memset(&counts, 0, sizeof(counts));
   conn_stats_free_all();
 }
 
@@ -147,16 +150,17 @@ conn_stats_note_or_conn_bytes(uint64_t conn_id, size_t num_read,
   /* Sum up last period's statistics */
   if (when >= bidi_next_interval) {
     bidi_map_entry_t **ptr, **next, *ent;
+    conn_counts_t *cnt = &counts;
     for (ptr = HT_START(bidimap, &bidi_map); ptr; ptr = next) {
       ent = *ptr;
       if (ent->read + ent->written < BIDI_THRESHOLD)
-        below_threshold++;
+        cnt->below_threshold++;
       else if (ent->read >= ent->written * BIDI_FACTOR)
-        mostly_read++;
+        cnt->mostly_read++;
       else if (ent->written >= ent->read * BIDI_FACTOR)
-        mostly_written++;
+        cnt->mostly_written++;
       else
-        both_read_and_written++;
+        cnt->both_read_and_written++;
       next = HT_NEXT_RMV(bidimap, &bidi_map, ptr);
       tor_free(ent);
     }
@@ -164,8 +168,8 @@ conn_stats_note_or_conn_bytes(uint64_t conn_id, size_t num_read,
       bidi_next_interval += BIDI_INTERVAL;
     log_info(LD_GENERAL, "%d below threshold, %d mostly read, "
              "%d mostly written, %d both read and written.",
-             below_threshold, mostly_read, mostly_written,
-             both_read_and_written);
+             cnt->below_threshold, cnt->mostly_read, cnt->mostly_written,
+             cnt->both_read_and_written);
   }
   /* Add this connection's bytes. */
   if (num_read > 0 || num_written > 0) {
@@ -202,10 +206,10 @@ conn_stats_format(time_t now)
   tor_asprintf(&result, "conn-bi-direct %s (%d s) %d,%d,%d,%d\n",
                written,
                (unsigned) (now - start_of_conn_stats_interval),
-               below_threshold,
-               mostly_read,
-               mostly_written,
-               both_read_and_written);
+               counts.below_threshold,
+               counts.mostly_read,
+               counts.mostly_written,
+               counts.both_read_and_written);
   return result;
 }
 
diff --git a/src/feature/stats/connstats.h b/src/feature/stats/connstats.h
index 26ff74cfd..43b654974 100644
--- a/src/feature/stats/connstats.h
+++ b/src/feature/stats/connstats.h
@@ -14,7 +14,7 @@
 
 void conn_stats_init(time_t now);
 void conn_stats_note_or_conn_bytes(uint64_t conn_id, size_t num_read,
-                                 size_t num_written, time_t when);
+                                   size_t num_written, time_t when);
 void conn_stats_reset(time_t now);
 char *conn_stats_format(time_t now);
 time_t conn_stats_save(time_t now);



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