commit 003e6595bf355fd13f85eb31e7850cec086ecd07 Author: Nick Mathewson nickm@torproject.org Date: Fri Apr 13 12:03:29 2018 -0400
Refactor "timestamp" not to be its own type coupled to token buffers
Really, the uint32_t is only an optimization; any kind of unit should work fine. Some users might want to use time_t or monotime_coarse_t or something like that. --- src/common/token_bucket.c | 19 ++++++++----------- src/common/token_bucket.h | 12 ++---------- src/test/test_bwmgt.c | 8 ++++---- 3 files changed, 14 insertions(+), 25 deletions(-)
diff --git a/src/common/token_bucket.c b/src/common/token_bucket.c index 8be377599..7c81264e2 100644 --- a/src/common/token_bucket.c +++ b/src/common/token_bucket.c @@ -48,12 +48,9 @@ token_bucket_cfg_init(token_bucket_cfg_t *cfg, */ void token_bucket_raw_reset(token_bucket_raw_t *bucket, - token_bucket_timestamp_t *stamp, - const token_bucket_cfg_t *cfg, - uint32_t now) + const token_bucket_cfg_t *cfg) { bucket->bucket = cfg->burst; - stamp->last_refilled_at = now; }
/** @@ -165,10 +162,9 @@ void token_bucket_rw_reset(token_bucket_rw_t *bucket, uint32_t now_ts) { - token_bucket_raw_reset(&bucket->read_bucket, &bucket->stamp, - &bucket->cfg, now_ts); - token_bucket_raw_reset(&bucket->write_bucket, &bucket->stamp, - &bucket->cfg, now_ts); + token_bucket_raw_reset(&bucket->read_bucket, &bucket->cfg); + token_bucket_raw_reset(&bucket->write_bucket, &bucket->cfg); + bucket->last_refilled_at_timestamp = now_ts; }
/** @@ -180,9 +176,10 @@ token_bucket_rw_reset(token_bucket_rw_t *bucket, */ int token_bucket_rw_refill(token_bucket_rw_t *bucket, - uint32_t now_ts) + uint32_t now_ts) { - const uint32_t elapsed_ticks = (now_ts - bucket->stamp.last_refilled_at); + const uint32_t elapsed_ticks = + (now_ts - bucket->last_refilled_at_timestamp); if (elapsed_ticks > UINT32_MAX-(300*1000)) { /* Either about 48 days have passed since the last refill, or the * monotonic clock has somehow moved backwards. (We're looking at you, @@ -208,7 +205,7 @@ token_bucket_rw_refill(token_bucket_rw_t *bucket, &bucket->cfg, elapsed_steps)) flags |= TB_WRITE;
- bucket->stamp.last_refilled_at = now_ts; + bucket->last_refilled_at_timestamp = now_ts; return flags; }
diff --git a/src/common/token_bucket.h b/src/common/token_bucket.h index 2507cd225..44315fb6b 100644 --- a/src/common/token_bucket.h +++ b/src/common/token_bucket.h @@ -27,12 +27,6 @@ typedef struct token_bucket_raw_t { int32_t bucket; } token_bucket_raw_t;
-/** A timestamp for a token bucket. The units of this timestamp are - * unspecified, but must match with the rate set in the token_bucket_cfg_t. */ -typedef struct token_bucket_timestamp_t { - uint32_t last_refilled_at; -} token_bucket_timestamp_t; - void token_bucket_cfg_init(token_bucket_cfg_t *cfg, uint32_t rate, uint32_t burst); @@ -41,9 +35,7 @@ void token_bucket_raw_adjust(token_bucket_raw_t *bucket, const token_bucket_cfg_t *cfg);
void token_bucket_raw_reset(token_bucket_raw_t *bucket, - token_bucket_timestamp_t *stamp, - const token_bucket_cfg_t *cfg, - uint32_t now); + const token_bucket_cfg_t *cfg);
int token_bucket_raw_dec(token_bucket_raw_t *bucket, ssize_t n); @@ -67,7 +59,7 @@ typedef struct token_bucket_rw_t { token_bucket_cfg_t cfg; token_bucket_raw_t read_bucket; token_bucket_raw_t write_bucket; - token_bucket_timestamp_t stamp; + uint32_t last_refilled_at_timestamp; } token_bucket_rw_t;
void token_bucket_rw_init(token_bucket_rw_t *bucket, diff --git a/src/test/test_bwmgt.c b/src/test/test_bwmgt.c index 17914aeb9..003671a42 100644 --- a/src/test/test_bwmgt.c +++ b/src/test/test_bwmgt.c @@ -36,7 +36,7 @@ test_bwmgt_token_buf_init(void *arg) tt_uint_op(rate_per_sec, OP_LT, 16*KB+160); } // Bucket starts out full: - tt_uint_op(b.stamp.last_refilled_at, OP_EQ, START_TS); + tt_uint_op(b.last_refilled_at_timestamp, OP_EQ, START_TS); tt_int_op(b.read_bucket.bucket, OP_EQ, 64*KB);
done: @@ -138,7 +138,7 @@ test_bwmgt_token_buf_refill(void *arg) tt_int_op(0, OP_EQ, token_bucket_rw_refill(&b, START_TS + SEC*3/2)); tt_int_op(b.read_bucket.bucket, OP_GT, 40*KB - 400); tt_int_op(b.read_bucket.bucket, OP_LT, 40*KB + 400); - tt_uint_op(b.stamp.last_refilled_at, OP_EQ, START_TS + SEC*3/2); + tt_uint_op(b.last_refilled_at_timestamp, OP_EQ, START_TS + SEC*3/2);
/* No time: nothing happens. */ { @@ -150,12 +150,12 @@ test_bwmgt_token_buf_refill(void *arg) /* Another 30 seconds: fill the bucket. */ tt_int_op(0, OP_EQ, token_bucket_rw_refill(&b, START_TS + SEC*3/2 + SEC*30)); tt_int_op(b.read_bucket.bucket, OP_EQ, b.cfg.burst); - tt_uint_op(b.stamp.last_refilled_at, OP_EQ, START_TS + SEC*3/2 + SEC*30); + tt_uint_op(b.last_refilled_at_timestamp, OP_EQ, START_TS + SEC*3/2 + SEC*30);
/* Another 30 seconds: nothing happens. */ tt_int_op(0, OP_EQ, token_bucket_rw_refill(&b, START_TS + SEC*3/2 + SEC*60)); tt_int_op(b.read_bucket.bucket, OP_EQ, b.cfg.burst); - tt_uint_op(b.stamp.last_refilled_at, OP_EQ, START_TS + SEC*3/2 + SEC*60); + tt_uint_op(b.last_refilled_at_timestamp, OP_EQ, START_TS + SEC*3/2 + SEC*60);
/* Empty the bucket, let two seconds pass, and make sure that a refill is * noticed. */