diff options
author | Nick Mathewson <nickm@torproject.org> | 2004-11-03 16:38:04 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2004-11-03 16:38:04 +0000 |
commit | 4fdaa5de51f6dbe6c56545553c617fc40eabf138 (patch) | |
tree | 30a3e61ca9272b992eacd60a3c97b564c3c1d526 | |
parent | 12e92eb82f7092112274691a775bc758bd82c646 (diff) | |
download | tor-4fdaa5de51f6dbe6c56545553c617fc40eabf138.tar tor-4fdaa5de51f6dbe6c56545553c617fc40eabf138.tar.gz |
Streamline the two redundant "Has the second rolled over?" checks in prepare_for_poll and connection_bucket_refill; also, generate BANDWIDTH control events
svn:r2657
-rw-r--r-- | src/or/connection.c | 12 | ||||
-rw-r--r-- | src/or/main.c | 34 |
2 files changed, 20 insertions, 26 deletions
diff --git a/src/or/connection.c b/src/or/connection.c index 2d8a8f319..79c5b997f 100644 --- a/src/or/connection.c +++ b/src/or/connection.c @@ -695,29 +695,19 @@ static void connection_read_bucket_decrement(connection_t *conn, int num_read) { } } -/** Keep a timeval to know when time has passed enough to refill buckets */ -static struct timeval current_time; - /** Initiatialize the global read bucket to options.BandwidthBurst, * and current_time to the current time. */ void connection_bucket_init(void) { - tor_gettimeofday(¤t_time); global_read_bucket = options.BandwidthBurst; /* start it at max traffic */ global_write_bucket = options.BandwidthBurst; /* start it at max traffic */ } -/** Some time has passed; increment buckets appropriately. */ +/** A second has rolled over; increment buckets appropriately. */ void connection_bucket_refill(struct timeval *now) { int i, n; connection_t *conn; connection_t **carray; - if(now->tv_sec <= current_time.tv_sec) - return; /* wait until the second has rolled over */ - - current_time.tv_sec = now->tv_sec; /* update current_time */ - /* (ignore usecs for now) */ - /* refill the global buckets */ if(global_read_bucket < options.BandwidthBurst) { global_read_bucket += options.BandwidthRate; diff --git a/src/or/main.c b/src/or/main.c index e2e71491a..bd5bc883b 100644 --- a/src/or/main.c +++ b/src/or/main.c @@ -1,4 +1,4 @@ -/* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */ +/* Copyright 2001,2002,2003,2004 Roger Dingledine, Matej Pfajfar. */ /* See LICENSE for licensing information */ /* $Id$ */ @@ -897,21 +897,25 @@ static int prepare_for_poll(void) { tor_gettimeofday(&now); - /* Check how much bandwidth we've consumed, and increment the token - * buckets. */ - stats_n_bytes_read += stats_prev_global_read_bucket - global_read_bucket; - stats_n_bytes_read_in_interval += stats_prev_global_read_bucket - global_read_bucket; - stats_n_bytes_written += stats_prev_global_write_bucket - global_write_bucket; - stats_n_bytes_written_in_interval += stats_prev_global_write_bucket - global_write_bucket; - - connection_bucket_refill(&now); - stats_prev_global_read_bucket = global_read_bucket; - stats_prev_global_write_bucket = global_write_bucket; - - if(now.tv_sec > current_second) { /* the second has rolled over. check more stuff. */ + if(now.tv_sec > current_second) { + /* the second has rolled over. check more stuff. */ + size_t bytes_written; + size_t bytes_read; + bytes_written = stats_prev_global_write_bucket - global_write_bucket; + bytes_read = stats_prev_global_read_bucket - global_read_bucket; + stats_n_bytes_read += bytes_read; + stats_n_bytes_read_in_interval += bytes_read; + stats_n_bytes_written += bytes_written; + stats_n_bytes_written_in_interval += bytes_written; + control_event_bandwidth_used((uint32_t)bytes_read,(uint32_t)bytes_written); + + connection_bucket_refill(&now); + stats_prev_global_read_bucket = global_read_bucket; + stats_prev_global_write_bucket = global_write_bucket; if(current_second) stats_n_seconds_uptime += (now.tv_sec - current_second); + assert_all_pending_dns_resolves_ok(); run_scheduled_events(now.tv_sec); assert_all_pending_dns_resolves_ok(); @@ -1242,11 +1246,11 @@ static void dumpstats(int severity) { if (stats_n_seconds_uptime) log(severity, - "Average bandwidth used: "U64_FORMAT"/%ld = %d bytes/sec", + "Average bandwidth used: "U64_FORMAT"/%ld = %d bytes/sec", U64_PRINTF_ARG(stats_n_bytes_read), stats_n_seconds_uptime, (int) (stats_n_bytes_read/stats_n_seconds_uptime)); - + rep_hist_dump_stats(now,severity); rend_service_dump_stats(severity); } |