From fb366c1d880e49a19c40c9d62cbd7cbef297c6e7 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Sun, 21 Mar 2010 13:16:31 -0400 Subject: [PATCH] Functions to track the total bytes sent over a rate limit group. --- bufferevent-internal.h | 7 +++++++ bufferevent_ratelim.c | 18 ++++++++++++++++++ include/event2/bufferevent.h | 13 +++++++++++++ 3 files changed, 38 insertions(+) diff --git a/bufferevent-internal.h b/bufferevent-internal.h index cd50856e..1b56c432 100644 --- a/bufferevent-internal.h +++ b/bufferevent-internal.h @@ -79,6 +79,13 @@ struct bufferevent_rate_limit_group { * again. */ unsigned pending_unsuspend_write : 1; + /*@{*/ + /** Total number of bytes read or written in this group since last + * reset. */ + ev_uint64_t total_read; + ev_uint64_t total_written; + /*@}*/ + /** The number of bufferevents in the group. */ int n_members; diff --git a/bufferevent_ratelim.c b/bufferevent_ratelim.c index c6b7e248..d32f63b3 100644 --- a/bufferevent_ratelim.c +++ b/bufferevent_ratelim.c @@ -282,6 +282,7 @@ _bufferevent_decrement_read_buckets(struct bufferevent_private *bev, int bytes) if (bev->rate_limiting->group) { LOCK_GROUP(bev->rate_limiting->group); bev->rate_limiting->group->rate_limit.read_limit -= bytes; + bev->rate_limiting->group->total_read += bytes; if (bev->rate_limiting->group->rate_limit.read_limit <= 0) { _bev_group_suspend_reading(bev->rate_limiting->group); } @@ -313,6 +314,7 @@ _bufferevent_decrement_write_buckets(struct bufferevent_private *bev, int bytes) if (bev->rate_limiting->group) { LOCK_GROUP(bev->rate_limiting->group); bev->rate_limiting->group->rate_limit.write_limit -= bytes; + bev->rate_limiting->group->total_written += bytes; if (bev->rate_limiting->group->rate_limit.write_limit <= 0) { _bev_group_suspend_writing(bev->rate_limiting->group); } @@ -908,3 +910,19 @@ bufferevent_rate_limit_group_decrement_write( return r; } +void +bufferevent_rate_limit_group_get_totals(struct bufferevent_rate_limit_group *grp, + ev_uint64_t *total_read_out, ev_uint64_t *total_written_out) +{ + EVUTIL_ASSERT(grp != NULL); + if (total_read_out) + *total_read_out = grp->total_read; + if (total_written_out) + *total_written_out = grp->total_written; +} + +void +bufferevent_rate_limit_group_reset_totals(struct bufferevent_rate_limit_group *grp) +{ + grp->total_read = grp->total_written = 0; +} diff --git a/include/event2/bufferevent.h b/include/event2/bufferevent.h index 15accf97..b1d7e43a 100644 --- a/include/event2/bufferevent.h +++ b/include/event2/bufferevent.h @@ -674,6 +674,19 @@ int bufferevent_rate_limit_group_decrement_write( /*@}*/ +/** Set the variable pointed to by total_read_out to the total number of bytes + * ever read on grp, and the variable pointed to by total_written_out to the + * total number of bytes ever written on grp. */ +void bufferevent_rate_limit_group_get_totals( + struct bufferevent_rate_limit_group *grp, + ev_uint64_t *total_read_out, ev_uint64_t *total_written_out); + +/** Reset the number of bytes read or written on grp as given by + * bufferevent_rate_limit_group_reset_totals(). */ +void +bufferevent_rate_limit_group_reset_totals( + struct bufferevent_rate_limit_group *grp); + #ifdef __cplusplus } #endif