Functions to track the total bytes sent over a rate limit group.

This commit is contained in:
Nick Mathewson 2010-03-21 13:16:31 -04:00
parent 33874b0528
commit fb366c1d88
3 changed files with 38 additions and 0 deletions

View File

@ -79,6 +79,13 @@ struct bufferevent_rate_limit_group {
* again. */ * again. */
unsigned pending_unsuspend_write : 1; 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. */ /** The number of bufferevents in the group. */
int n_members; int n_members;

View File

@ -282,6 +282,7 @@ _bufferevent_decrement_read_buckets(struct bufferevent_private *bev, int bytes)
if (bev->rate_limiting->group) { if (bev->rate_limiting->group) {
LOCK_GROUP(bev->rate_limiting->group); LOCK_GROUP(bev->rate_limiting->group);
bev->rate_limiting->group->rate_limit.read_limit -= bytes; 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) { if (bev->rate_limiting->group->rate_limit.read_limit <= 0) {
_bev_group_suspend_reading(bev->rate_limiting->group); _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) { if (bev->rate_limiting->group) {
LOCK_GROUP(bev->rate_limiting->group); LOCK_GROUP(bev->rate_limiting->group);
bev->rate_limiting->group->rate_limit.write_limit -= bytes; 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) { if (bev->rate_limiting->group->rate_limit.write_limit <= 0) {
_bev_group_suspend_writing(bev->rate_limiting->group); _bev_group_suspend_writing(bev->rate_limiting->group);
} }
@ -908,3 +910,19 @@ bufferevent_rate_limit_group_decrement_write(
return r; 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;
}

View File

@ -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 #ifdef __cplusplus
} }
#endif #endif