Commit 4fdaa5de authored by Nick Mathewson's avatar Nick Mathewson 🐘
Browse files

Streamline the two redundant "Has the second rolled over?" checks in...

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
parent 12e92eb8
Loading
Loading
Loading
Loading
+1 −11
Changes for src/or/connection.c: 1 added line, 11 removed lines.
Original line number Diff line number Diff line
@@ -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;
+13 −9
Changes for src/or/main.c: 13 added lines, 9 removed lines.
Original line number Diff line number Diff line
/* 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. */

    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();