[or-cvs] [tor/master 1/2] GETINFO options for querying traffic usage

nickm at torproject.org nickm at torproject.org
Fri Jan 7 17:17:00 UTC 2011


Author: Damian Johnson <atagar at torproject.org>
Date: Thu, 6 Jan 2011 21:53:48 -0800
Subject: GETINFO options for querying traffic usage
Commit: 6661e16e7ca677cc6063b5aaf25da512d7cda8ad

This was originally a patch provided by pipe
(http://www.mail-archive.com/or-talk@freehaven.net/msg13085.html) to
provide a method for controllers to query the total amount of traffic
tor has handled (this is a frequently requested piece of information
by relay operators).
---
 changes/feature2345       |    4 ++++
 doc/spec/control-spec.txt |    4 ++++
 src/or/control.c          |    6 ++++++
 src/or/main.c             |   14 ++++++++++++++
 src/or/main.h             |    2 ++
 5 files changed, 30 insertions(+), 0 deletions(-)
 create mode 100644 changes/feature2345

diff --git a/changes/feature2345 b/changes/feature2345
new file mode 100644
index 0000000..5ab6a0f
--- /dev/null
+++ b/changes/feature2345
@@ -0,0 +1,4 @@
+  o Minor features (controller)
+    - Add GETINFO options to get total bytes read and written. Patch
+      from pipe, revised by atagar.  Resolves ticket 2345.
+
diff --git a/doc/spec/control-spec.txt b/doc/spec/control-spec.txt
index 45fa3e7..bd327db 100644
--- a/doc/spec/control-spec.txt
+++ b/doc/spec/control-spec.txt
@@ -517,6 +517,10 @@
        with a $.  This is an implementation error.  It would be nice to add
        the $ back in if we can do so without breaking compatibility.]
 
+    "traffic/read" -- Total bytes read (downloaded).
+
+    "traffic/written" -- Total bytes written (uploaded).
+
     "accounting/enabled"
     "accounting/hibernating"
     "accounting/bytes"
diff --git a/src/or/control.c b/src/or/control.c
index 58f4135..c895a70 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -1350,6 +1350,10 @@ getinfo_helper_misc(control_connection_t *conn, const char *question,
       return -1;
     }
     *answer = tor_dup_ip(addr);
+  } else if (!strcmp(question, "traffic/read")) {
+    tor_asprintf(answer, U64_FORMAT, U64_PRINTF_ARG(get_bytes_read()));
+  } else if (!strcmp(question, "traffic/written")) {
+    tor_asprintf(answer, U64_FORMAT, U64_PRINTF_ARG(get_bytes_written()));
   } else if (!strcmp(question, "process/pid")) {
     int myPid = -1;
 
@@ -1958,6 +1962,8 @@ static const getinfo_item_t getinfo_items[] = {
       "Number of versioning authorities agreeing on the status of the "
       "current version"),
   ITEM("address", misc, "IP address of this Tor host, if we can guess it."),
+  ITEM("traffic/read", misc, "Bytes read since the process was started."),
+  ITEM("traffic/written", misc, "Bytes written since the process was started."),
   ITEM("process/pid", misc, "Process id belonging to the main tor process."),
   ITEM("process/uid", misc, "User id running the tor process."),
   ITEM("process/user", misc,"Username under which the tor process is running."),
diff --git a/src/or/main.c b/src/or/main.c
index 4fcc715..aa97609 100644
--- a/src/or/main.c
+++ b/src/or/main.c
@@ -398,6 +398,20 @@ get_connection_array(void)
   return connection_array;
 }
 
+/** Provides the traffic read and written over the life of the process. */
+
+uint64_t
+get_bytes_read(void)
+{
+  return stats_n_bytes_read;
+}
+
+uint64_t
+get_bytes_written(void)
+{
+  return stats_n_bytes_written;
+}
+
 /** Set the event mask on <b>conn</b> to <b>events</b>.  (The event
  * mask is a bitmask whose bits are READ_EVENT and WRITE_EVENT)
  */
diff --git a/src/or/main.h b/src/or/main.h
index 550f993..4e15d4d 100644
--- a/src/or/main.h
+++ b/src/or/main.h
@@ -24,6 +24,8 @@ void add_connection_to_closeable_list(connection_t *conn);
 int connection_is_on_closeable_list(connection_t *conn);
 
 smartlist_t *get_connection_array(void);
+uint64_t get_bytes_read(void);
+uint64_t get_bytes_written(void);
 
 typedef enum watchable_events {
   /* Yes, it is intentional that these match Libevent's EV_READ and EV_WRITE */
-- 
1.7.1




More information about the tor-commits mailing list