commit 4903ab1caaf0b2631e6091b58b31eaff0c9f8424 Author: Nick Mathewson nickm@torproject.org Date: Tue Sep 23 08:46:35 2014 -0400
Avoid frequent strcmp() calls for AccountingRule
Generally, we don't like to parse the same thing over and over; it's best IMO to do it once at the start of the code. --- src/or/config.c | 14 ++++++++++---- src/or/hibernate.c | 9 +++++---- src/or/or.h | 9 +++++---- src/or/router.c | 2 +- src/or/status.c | 3 +-- src/test/test_accounting.c | 4 ++-- 6 files changed, 24 insertions(+), 17 deletions(-)
diff --git a/src/or/config.c b/src/or/config.c index c7c6154..8035afb 100644 --- a/src/or/config.c +++ b/src/or/config.c @@ -126,7 +126,7 @@ static config_abbrev_t option_abbrevs_[] = { */ static config_var_t option_vars_[] = { V(AccountingMax, MEMUNIT, "0 bytes"), - V(AccountingRule, STRING, "max"), + VAR("AccountingRule", STRING, AccountingRule_option, "max"), V(AccountingStart, STRING, NULL), V(Address, STRING, NULL), V(AllowDotExit, BOOL, "0"), @@ -3109,9 +3109,15 @@ options_validate(or_options_t *old_options, or_options_t *options, "risky: they will all turn off at the same time, which may " "alert observers that they are being run by the same party."); } - if (options->AccountingRule && - strcmp(options->AccountingRule, "sum") != 0 && - strcmp(options->AccountingRule, "max") != 0) + } + + options->AccountingRule = ACCT_MAX; + if (options->AccountingRule_option) { + if (!strcmp(options->AccountingRule_option, "sum")) + options->AccountingRule = ACCT_SUM; + else if (!strcmp(options->AccountingRule_option, "max")) + options->AccountingRule = ACCT_MAX; + else REJECT("AccountingRule must be 'sum' or 'max'"); }
diff --git a/src/or/hibernate.c b/src/or/hibernate.c index 6978f47..b3761cf 100644 --- a/src/or/hibernate.c +++ b/src/or/hibernate.c @@ -415,9 +415,10 @@ configure_accounting(time_t now) static uint64_t get_accounting_bytes(void) { - if (strcmp(get_options()->AccountingRule, "sum") == 0) + if (get_options()->AccountingRule == ACCT_SUM) return n_bytes_read_in_interval+n_bytes_written_in_interval; - return MAX(n_bytes_read_in_interval, n_bytes_written_in_interval); + else + return MAX(n_bytes_read_in_interval, n_bytes_written_in_interval); }
/** Set expected_bandwidth_usage based on how much we sent/received @@ -434,7 +435,7 @@ update_expected_bandwidth(void) /* max_configured is the larger of bytes read and bytes written * If we are accounting based on sum, worst case is both are * at max, doubling the expected sum of bandwidth */ - if (strcmp(get_options()->AccountingRule, "sum") == 0) + if (get_options()->AccountingRule == ACCT_SUM) max_configured *= 2;
#define MIN_TIME_FOR_MEASUREMENT (1800) @@ -1014,7 +1015,7 @@ getinfo_helper_accounting(control_connection_t *conn, U64_PRINTF_ARG(n_bytes_written_in_interval)); } else if (!strcmp(question, "accounting/bytes-left")) { uint64_t limit = get_options()->AccountingMax; - if (strcmp(get_options()->AccountingRule, "sum") == 0) { + if (get_options()->AccountingRule == ACCT_SUM) { uint64_t total_left = 0; uint64_t total_bytes = get_accounting_bytes(); if (total_bytes < limit) diff --git a/src/or/or.h b/src/or/or.h index 20cfa5b..54cee46 100644 --- a/src/or/or.h +++ b/src/or/or.h @@ -3775,10 +3775,11 @@ typedef struct { uint64_t AccountingMax; /**< How many bytes do we allow per accounting * interval before hibernation? 0 for "never * hibernate." */ - char *AccountingRule; /**< How do we determine when our AccountingMax - * has been reached? - * "max" for when in or out reaches AccountingMax - * "sum for when in plus out reaches AccountingMax */ + /** How do we determine when our AccountingMax has been reached? + * "max" for when in or out reaches AccountingMax + * "sum for when in plus out reaches AccountingMax */ + char *AccountingRule_option; + enum { ACCT_MAX, ACCT_SUM } AccountingRule;
/** Base64-encoded hash of accepted passwords for the control system. */ config_line_t *HashedControlPassword; diff --git a/src/or/router.c b/src/or/router.c index 96d16bb..dbe985a 100644 --- a/src/or/router.c +++ b/src/or/router.c @@ -1093,7 +1093,7 @@ decide_to_advertise_dirport(const or_options_t *options, uint16_t dir_port) interval_length);
acc_bytes = options->AccountingMax; - if (strcmp(options->AccountingRule, "sum") == 0) + if (get_options()->AccountingRule == ACCT_SUM) acc_bytes /= 2; if (effective_bw >= acc_bytes / interval_length) { diff --git a/src/or/status.c b/src/or/status.c index 4158df5..daae1d7 100644 --- a/src/or/status.c +++ b/src/or/status.c @@ -145,13 +145,12 @@ log_accounting(const time_t now, const or_options_t *options) or_state_t *state = get_or_state(); char *acc_rcvd = bytes_to_usage(state->AccountingBytesReadInInterval); char *acc_sent = bytes_to_usage(state->AccountingBytesWrittenInInterval); - const char *acc_rule = options->AccountingRule; uint64_t acc_bytes = options->AccountingMax; char *acc_max; time_t interval_end = accounting_get_end_time(); char end_buf[ISO_TIME_LEN + 1]; char *remaining = NULL; - if (strcmp(acc_rule, "sum") == 0) + if (options->AccountingRule == ACCT_SUM) acc_bytes *= 2; acc_max = bytes_to_usage(acc_bytes); format_local_iso_time(end_buf, interval_end); diff --git a/src/test/test_accounting.c b/src/test/test_accounting.c index a1a2ec6..25908e9 100644 --- a/src/test/test_accounting.c +++ b/src/test/test_accounting.c @@ -34,7 +34,7 @@ test_accounting_limits(void *arg) or_state = or_state_new();
options->AccountingMax = 100; - options->AccountingRule = tor_strdup("max"); + options->AccountingRule = ACCT_MAX;
tor_assert(accounting_is_enabled(options)); configure_accounting(fake_time); @@ -50,7 +50,7 @@ test_accounting_limits(void *arg) tor_assert(we_are_hibernating() == 1);
options->AccountingMax = 200; - options->AccountingRule = tor_strdup("sum"); + options->AccountingRule = ACCT_SUM;
accounting_add_bytes(0, 10, 1); fake_time += 1;
tor-commits@lists.torproject.org