aboutsummaryrefslogtreecommitdiff
path: root/src/or/hibernate.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2010-09-21 14:59:43 -0400
committerNick Mathewson <nickm@torproject.org>2010-09-21 14:59:43 -0400
commit52db5c2539413aded9a7fb855030c6a3a7a25798 (patch)
tree6e122adfa0cd68dc28bc9d6400d97e834d4e1c8c /src/or/hibernate.c
parent80b3de8753225f169e4269503d63c273562345e7 (diff)
downloadtor-52db5c2539413aded9a7fb855030c6a3a7a25798.tar
tor-52db5c2539413aded9a7fb855030c6a3a7a25798.tar.gz
Even more accurate handling for shifting accounting intervals
Roger correctly pointed out that my code was broken for accounting periods that shifted forwards, since start_of_accounting_period_containing(interval_start_time) would not be equal to interval_start_time, but potentially much earlier.
Diffstat (limited to 'src/or/hibernate.c')
-rw-r--r--src/or/hibernate.c29
1 files changed, 18 insertions, 11 deletions
diff --git a/src/or/hibernate.c b/src/or/hibernate.c
index 994b7ea93..eb9c1cf6d 100644
--- a/src/or/hibernate.c
+++ b/src/or/hibernate.c
@@ -354,31 +354,38 @@ length_of_accounting_period_containing(time_t now)
void
configure_accounting(time_t now)
{
+ time_t s_now;
/* Try to remember our recorded usage. */
if (!interval_start_time)
read_bandwidth_usage(); /* If we fail, we'll leave values at zero, and
* reset below.*/
- if (!interval_start_time ||
- start_of_accounting_period_after(interval_start_time) <= now) {
- /* We didn't have recorded usage, or we don't have recorded usage
- * for this interval. Start a new interval. */
+
+ s_now = start_of_accounting_period_containing(now);
+
+ if (!interval_start_time) {
+ /* We didn't have recorded usage; Start a new interval. */
log_info(LD_ACCT, "Starting new accounting interval.");
reset_accounting(now);
- } else if (interval_start_time ==
- start_of_accounting_period_containing(interval_start_time)) {
+ } else if (s_now == interval_start_time) {
log_info(LD_ACCT, "Continuing accounting interval.");
/* We are in the interval we thought we were in. Do nothing.*/
interval_end_time = start_of_accounting_period_after(interval_start_time);
} else {
- time_t s_now = start_of_accounting_period_containing(interval_start_time);
- long duration = length_of_accounting_period_containing(interval_start_time);
+ long duration = length_of_accounting_period_containing(now);
double delta = ((double)(s_now - interval_start_time)) / duration;
if (-0.50 <= delta && delta <= 0.50) {
- /* The start of the period is now a little later than we remembered.
- * That's fine; we might lose a little time, but that's ok. */
+ /* The start of the period is now a little later or earlier than we
+ * remembered. That's fine; we might lose some bytes we could otherwise
+ * have written, but better to err on the side of obeying people's
+ * accounting settings. */
log_info(LD_ACCT, "Accounting interval moved by %.02f%%; "
"that's fine.", delta*100);
- interval_end_time = start_of_accounting_period_after(interval_start_time);
+ interval_end_time = start_of_accounting_period_after(now);
+ } else if (delta >= 0.99) {
+ /* This is the regular time-moved-forward case; don't be too noisy
+ * about it or people will complain */
+ log_info(LD_ACCT, "Accounting interval elapsed; starting a new one");
+ reset_accounting(now);
} else {
log_warn(LD_ACCT,
"Mismatched accounting interval: moved by %.02f%%. "