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
This commit is contained in:
Nick Mathewson 2004-11-03 16:38:04 +00:00
parent 12e92eb82f
commit 4fdaa5de51
2 changed files with 19 additions and 25 deletions

View File

@ -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(&current_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;

View File

@ -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;
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(now.tv_sec > current_second) { /* the second has rolled over. check more stuff. */
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);
}