From 780d1b44cf24ad8ef321b99d8fc591f110456f98 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Tue, 17 Apr 2018 16:19:45 -0400 Subject: Move responsibility for recording read/written bytes Previously this was done as part of the refill callback, but there's no real reason to do it like that. Since we're trying to remove the refill callback completely, we can do this work as part of record_num_bytes_transferred_impl(), which already does quite a lot of this. --- src/or/connection.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/or/connection.c') diff --git a/src/or/connection.c b/src/or/connection.c index 7dc4ecf5c2..d80c680462 100644 --- a/src/or/connection.c +++ b/src/or/connection.c @@ -85,6 +85,7 @@ #include "ext_orport.h" #include "geoip.h" #include "main.h" +#include "hibernate.h" #include "hs_common.h" #include "hs_ident.h" #include "nodelist.h" @@ -2989,6 +2990,10 @@ global_write_bucket_low(connection_t *conn, size_t attempt, int priority) return 0; } +/** When did we last tell the accounting subsystem about transmitted + * bandwidth? */ +static time_t last_recorded_accounting_at = 0; + /** Helper: adjusts our bandwidth history and informs the controller as * appropriate, given that we have just read num_read bytes and written * num_written bytes on conn. */ @@ -3019,6 +3024,20 @@ record_num_bytes_transferred_impl(connection_t *conn, } if (conn->type == CONN_TYPE_EXIT) rep_hist_note_exit_bytes(conn->port, num_written, num_read); + + /* Remember these bytes towards statistics. */ + stats_increment_bytes_read_and_written(num_read, num_written); + + /* Remember these bytes towards accounting. */ + if (accounting_is_enabled(get_options())) { + if (now > last_recorded_accounting_at && last_recorded_accounting_at) { + accounting_add_bytes(num_read, num_written, + now - last_recorded_accounting_at); + } else { + accounting_add_bytes(num_read, num_written, 0); + } + last_recorded_accounting_at = now; + } } /** We just read num_read and wrote num_written bytes @@ -5196,6 +5215,7 @@ connection_free_all(void) tor_free(last_interface_ipv4); tor_free(last_interface_ipv6); + last_recorded_accounting_at = 0; } /** Log a warning, and possibly emit a control event, that received came -- cgit v1.2.3-54-g00ecf