Просмотр исходного кода

r11813@catbus: nickm | 2007-02-14 11:42:58 -0500
Tweak stream_bw patch: Remove a couple of redundant checks, save 8 bytes per edge connection, fix spelling in the changelog; expand spec.


svn:r9586

Nick Mathewson 19 лет назад
Родитель
Сommit
6e35b11851
5 измененных файлов с 48 добавлено и 31 удалено
  1. 1 1
      ChangeLog
  2. 7 3
      doc/spec/control-spec.txt
  3. 6 10
      src/or/connection.c
  4. 32 13
      src/or/control.c
  5. 2 4
      src/or/or.h

+ 1 - 1
ChangeLog

@@ -45,7 +45,7 @@ Changes in version 0.1.2.8-alpha - 2007-??-??
       control protocol.  We're planning to remove support for it during
       control protocol.  We're planning to remove support for it during
       the next development series, so it's good to give people some
       the next development series, so it's good to give people some
       advance warning.
       advance warning.
-    - Add STREAM_BW events to reprot per-entry-stream bandwidth use. (Patch
+    - Add STREAM_BW events to report per-entry-stream bandwidth use. (Patch
       from Robert Hogan.)
       from Robert Hogan.)
 
 
   o Minor features:
   o Minor features:

+ 7 - 3
doc/spec/control-spec.txt

@@ -1271,16 +1271,20 @@ $Id$
 
 
   [First added in 0.1.2.3-alpha]
   [First added in 0.1.2.3-alpha]
 
 
-4.1.13. Bandwidth used on a stream
+4.1.13. Bandwidth used on an application stream
 
 
   The syntax is:
   The syntax is:
      "650" SP "STREAM_BW" SP StreamID SP BytesRead SP BytesWritten
      "650" SP "STREAM_BW" SP StreamID SP BytesRead SP BytesWritten
      BytesRead = 1*DIGIT
      BytesRead = 1*DIGIT
      BytesWritten = 1*DIGIT
      BytesWritten = 1*DIGIT
 
 
-  The number of bytes read and written since the last read or write event
-  on a stream.
+  BytesRead and BytesWritten are the number of bytes read and written since
+  the last STREAM_BW event on a stream.  These events are generated about
+  once per second per stream; No event is generated for streams that have not
+  read or written.
 
 
+  These events apply only to streams entering Tor (such as on a SOCKSPort,
+  TransPort, or so on).  They are not generated for exiting streams.
 
 
 5. Implementation notes
 5. Implementation notes
 
 

+ 6 - 10
src/or/connection.c

@@ -1571,11 +1571,9 @@ connection_read_to_buf(connection_t *conn, int *max_to_read)
     *max_to_read = at_most - n_read;
     *max_to_read = at_most - n_read;
   }
   }
 
 
-  if (CONN_IS_EDGE(conn)) {
-    if (conn->type == CONN_TYPE_AP) {
-        edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
-        edge_conn->n_read += n_read;
-    }
+  if (conn->type == CONN_TYPE_AP) {
+    edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
+    edge_conn->n_read += n_read;
   }
   }
 
 
   if (connection_is_rate_limited(conn)) {
   if (connection_is_rate_limited(conn)) {
@@ -1774,11 +1772,9 @@ connection_handle_write(connection_t *conn, int force)
     n_written = (size_t) result;
     n_written = (size_t) result;
   }
   }
 
 
-  if (CONN_IS_EDGE(conn)) {
-    if (conn->type == CONN_TYPE_AP) {
-      edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
-      edge_conn->n_written += n_written;
-    }
+  if (conn->type == CONN_TYPE_AP) {
+    edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
+    edge_conn->n_written += n_written;
   }
   }
 
 
   if (connection_is_rate_limited(conn)) {
   if (connection_is_rate_limited(conn)) {

+ 32 - 13
src/or/control.c

@@ -126,9 +126,10 @@ static const char * CONTROL0_COMMANDS[_CONTROL0_CMD_MAX_RECOGNIZED+1] = {
  * has interest in without having to walk over the global connection
  * has interest in without having to walk over the global connection
  * list to find out.
  * list to find out.
  **/
  **/
-static uint32_t global_event_mask0 = 0;
-static uint32_t global_event_mask1long = 0;
-static uint32_t global_event_mask1short = 0;
+typedef uint32_t event_mask_t;
+static event_mask_t global_event_mask0 = 0;
+static event_mask_t global_event_mask1long = 0;
+static event_mask_t global_event_mask1short = 0;
 
 
 /** True iff we have disabled log messages from being sent to the controller */
 /** True iff we have disabled log messages from being sent to the controller */
 static int disable_log_messages = 0;
 static int disable_log_messages = 0;
@@ -276,6 +277,11 @@ control_update_global_event_mask(void)
 {
 {
   connection_t **conns;
   connection_t **conns;
   int n_conns, i;
   int n_conns, i;
+  event_mask_t old_mask, new_mask;
+  old_mask = global_event_mask0;
+  old_mask |= global_event_mask1short;
+  old_mask |= global_event_mask1long;
+
   global_event_mask0 = 0;
   global_event_mask0 = 0;
   global_event_mask1short = 0;
   global_event_mask1short = 0;
   global_event_mask1long = 0;
   global_event_mask1long = 0;
@@ -293,7 +299,25 @@ control_update_global_event_mask(void)
     }
     }
   }
   }
 
 
+  new_mask = global_event_mask0;
+  new_mask |= global_event_mask1short;
+  new_mask |= global_event_mask1long;
+
+  /* Handle the aftermath.  Set up the log callback to tell us only what
+   * we want to hear...*/
   control_adjust_event_log_severity();
   control_adjust_event_log_severity();
+
+  /* ...then, if we've started logging stream bw, clear the appropriate
+   * fields. */
+  if (! (old_mask & EVENT_STREAM_BANDWIDTH_USED) &&
+      (new_mask & EVENT_STREAM_BANDWIDTH_USED)) {
+    for (i = 0; i < n_conns; ++i) {
+      if (conns[i]->type == CONN_TYPE_AP) {
+        edge_connection_t *conn = TO_EDGE_CONN(conns[i]);
+        conn->n_written = conn->n_read = 0;
+      }
+    }
+  }
 }
 }
 
 
 /** Adjust the log severities that result in control_event_logmsg being called
 /** Adjust the log severities that result in control_event_logmsg being called
@@ -3416,12 +3440,11 @@ control_event_or_conn_status(or_connection_t *conn,or_conn_status_event_t tp,
 /** A second or more has elapsed: tell any interested control
 /** A second or more has elapsed: tell any interested control
  * connections how much bandwidth streams have used. */
  * connections how much bandwidth streams have used. */
 int
 int
-control_event_stream_bandwidth_used()
+control_event_stream_bandwidth_used(void)
 {
 {
   connection_t **carray;
   connection_t **carray;
   edge_connection_t *conn;
   edge_connection_t *conn;
   int n, i;
   int n, i;
-  uint32_t justread, justwritten;
 
 
   if (EVENT_IS_INTERESTING1(EVENT_STREAM_BANDWIDTH_USED)) {
   if (EVENT_IS_INTERESTING1(EVENT_STREAM_BANDWIDTH_USED)) {
 
 
@@ -3431,20 +3454,16 @@ control_event_stream_bandwidth_used()
         if (carray[i]->type != CONN_TYPE_AP)
         if (carray[i]->type != CONN_TYPE_AP)
           continue;
           continue;
         conn = TO_EDGE_CONN(carray[i]);
         conn = TO_EDGE_CONN(carray[i]);
-        if (conn->p_read == conn->n_read && conn->p_written == conn->n_written)
+        if (!conn->n_read && !conn->n_written)
           continue;
           continue;
 
 
-        justread = conn->n_read - conn->p_read;
-        conn->p_read = conn->n_read;
-        justwritten = conn->n_written - conn->p_written;
-        conn->p_written = conn->n_written;
-
         send_control1_event(EVENT_STREAM_BANDWIDTH_USED, ALL_NAMES,
         send_control1_event(EVENT_STREAM_BANDWIDTH_USED, ALL_NAMES,
                             "650 STREAM_BW %lu %lu %lu\r\n",
                             "650 STREAM_BW %lu %lu %lu\r\n",
                             (unsigned long)conn->global_identifier,
                             (unsigned long)conn->global_identifier,
-                            (unsigned long)justread,
-                            (unsigned long)justwritten);
+                            (unsigned long)conn->n_read,
+                            (unsigned long)conn->n_written);
 
 
+        conn->n_written = conn->n_read = 0;
     }
     }
   }
   }
 
 

+ 2 - 4
src/or/or.h

@@ -838,13 +838,11 @@ typedef struct edge_connection_t {
   /* XXXX NM This can get re-used after 2**32 streams */
   /* XXXX NM This can get re-used after 2**32 streams */
   uint32_t global_identifier;
   uint32_t global_identifier;
 
 
-  /** Bytes read */
+  /** Bytes read since last call to control_event_stream_bandwidth_used() */
   uint32_t n_read;
   uint32_t n_read;
-  uint32_t p_read;
 
 
-  /** Bytes written */
+  /** Bytes written since last call to control_event_stream_bandwidth_used() */
   uint32_t n_written;
   uint32_t n_written;
-  uint32_t p_written;
 
 
   /** Exit only: a dirserv connection that is tunneled over this connection
   /** Exit only: a dirserv connection that is tunneled over this connection
    * using a socketpair. */
    * using a socketpair. */