[tor-commits] [tor/master] Tweak CIRC_BW event based on comments by nickm.

nickm at torproject.org nickm at torproject.org
Thu Oct 31 03:06:10 UTC 2013


commit b33b366a7f8bcab1c9b4859788e3b2c7d3dcf180
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date:   Sat May 25 13:04:33 2013 +0200

    Tweak CIRC_BW event based on comments by nickm.
    
    - Rename n_read and n_written in origin_circuit_t to make it clear that
      these are only used for CIRC_BW events.
    - Extract new code in control_update_global_event_mask to new
      clear_circ_bw_fields function.
---
 src/or/connection.c |   14 +++++++-------
 src/or/control.c    |   35 +++++++++++++++++++++--------------
 src/or/or.h         |   10 ++++++----
 3 files changed, 34 insertions(+), 25 deletions(-)

diff --git a/src/or/connection.c b/src/or/connection.c
index 2f2a421..88def49 100644
--- a/src/or/connection.c
+++ b/src/or/connection.c
@@ -3271,7 +3271,7 @@ connection_read_to_buf(connection_t *conn, ssize_t *max_to_read,
      /* change *max_to_read */
     *max_to_read = at_most - n_read;
 
-    /* Update edge_conn->n_read and ocirc->n_read */
+    /* Update edge_conn->n_read and ocirc->n_read_circ_bw */
     if (conn->type == CONN_TYPE_AP) {
       edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
       circuit_t *circ = circuit_get_by_edge_conn(edge_conn);
@@ -3285,10 +3285,10 @@ connection_read_to_buf(connection_t *conn, ssize_t *max_to_read,
 
       if (circ && CIRCUIT_IS_ORIGIN(circ)) {
         ocirc = TO_ORIGIN_CIRCUIT(circ);
-        if (PREDICT_LIKELY(UINT32_MAX - ocirc->n_read > n_read))
-          ocirc->n_read += (int)n_read;
+        if (PREDICT_LIKELY(UINT32_MAX - ocirc->n_read_circ_bw > n_read))
+          ocirc->n_read_circ_bw += (int)n_read;
         else
-          ocirc->n_read = UINT32_MAX;
+          ocirc->n_read_circ_bw = UINT32_MAX;
       }
     }
 
@@ -3752,10 +3752,10 @@ connection_handle_write_impl(connection_t *conn, int force)
 
     if (circ && CIRCUIT_IS_ORIGIN(circ)) {
       ocirc = TO_ORIGIN_CIRCUIT(circ);
-      if (PREDICT_LIKELY(UINT32_MAX - ocirc->n_written > n_written))
-        ocirc->n_written += (int)n_written;
+      if (PREDICT_LIKELY(UINT32_MAX - ocirc->n_written_circ_bw > n_written))
+        ocirc->n_written_circ_bw += (int)n_written;
       else
-        ocirc->n_written = UINT32_MAX;
+        ocirc->n_written_circ_bw = UINT32_MAX;
     }
   }
 
diff --git a/src/or/control.c b/src/or/control.c
index ac7be8d..5e2020f 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -237,6 +237,20 @@ log_severity_to_event(int severity)
   }
 }
 
+/** Helper: clear bandwidth counters of all origin circuits. */
+static void
+clear_circ_bw_fields(void)
+{
+  circuit_t *circ;
+  origin_circuit_t *ocirc;
+  for (circ = circuit_get_global_list_(); circ; circ = circ->next) {
+    if (!CIRCUIT_IS_ORIGIN(circ))
+      continue;
+    ocirc = TO_ORIGIN_CIRCUIT(circ);
+    ocirc->n_written_circ_bw = ocirc->n_read_circ_bw = 0;
+  }
+}
+
 /** Set <b>global_event_mask*</b> to the bitwise OR of each live control
  * connection's event_mask field. */
 void
@@ -276,14 +290,7 @@ control_update_global_event_mask(void)
   }
   if (! (old_mask & EVENT_CIRC_BANDWIDTH_USED) &&
       (new_mask & EVENT_CIRC_BANDWIDTH_USED)) {
-    circuit_t *circ;
-    origin_circuit_t *ocirc;
-    for (circ = circuit_get_global_list_(); circ; circ = circ->next) {
-      if (!CIRCUIT_IS_ORIGIN(circ))
-        continue;
-      ocirc = TO_ORIGIN_CIRCUIT(circ);
-      ocirc->n_written = ocirc->n_read = 0;
-    }
+    clear_circ_bw_fields();
   }
 }
 
@@ -3893,8 +3900,8 @@ control_event_stream_bandwidth(edge_connection_t *edge_conn)
     circ = circuit_get_by_edge_conn(edge_conn);
     if (circ && CIRCUIT_IS_ORIGIN(circ)) {
       ocirc = TO_ORIGIN_CIRCUIT(circ);
-      ocirc->n_read += edge_conn->n_read;
-      ocirc->n_written += edge_conn->n_written;
+      ocirc->n_read_circ_bw += edge_conn->n_read;
+      ocirc->n_written_circ_bw += edge_conn->n_written;
     }
     edge_conn->n_written = edge_conn->n_read = 0;
   }
@@ -3947,14 +3954,14 @@ control_event_circ_bandwidth_used(void)
     if (!CIRCUIT_IS_ORIGIN(circ))
       continue;
     ocirc = TO_ORIGIN_CIRCUIT(circ);
-    if (!ocirc->n_read && !ocirc->n_written)
+    if (!ocirc->n_read_circ_bw && !ocirc->n_written_circ_bw)
       continue;
     send_control_event(EVENT_CIRC_BANDWIDTH_USED, ALL_FORMATS,
                        "650 CIRC_BW ID=%d READ=%lu WRITTEN=%lu\r\n",
                        ocirc->global_identifier,
-                       (unsigned long)ocirc->n_read,
-                       (unsigned long)ocirc->n_written);
-    ocirc->n_written = ocirc->n_read = 0;
+                       (unsigned long)ocirc->n_read_circ_bw,
+                       (unsigned long)ocirc->n_written_circ_bw);
+    ocirc->n_written_circ_bw = ocirc->n_read_circ_bw = 0;
   }
 
   return 0;
diff --git a/src/or/or.h b/src/or/or.h
index 21a36c9..6dd3ce4 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -2971,12 +2971,14 @@ typedef struct origin_circuit_t {
   edge_connection_t *p_streams;
 
   /** Bytes read from any attached stream since last call to
-   * control_event_circ_bandwidth_used() */
-  uint32_t n_read;
+   * control_event_circ_bandwidth_used().  Only used if we're configured
+   * to emit CIRC_BW events. */
+  uint32_t n_read_circ_bw;
 
   /** Bytes written to any attached stream since last call to
-   * control_event_circ_bandwidth_used() */
-  uint32_t n_written;
+   * control_event_circ_bandwidth_used().  Only used if we're configured
+   * to emit CIRC_BW events. */
+  uint32_t n_written_circ_bw;
 
   /** Build state for this circuit. It includes the intended path
    * length, the chosen exit router, rendezvous information, etc.





More information about the tor-commits mailing list