Commit 4903ab1c authored by Nick Mathewson's avatar Nick Mathewson 👁
Browse files

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.
parent 8527a299
......@@ -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'");
}
......
......@@ -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)
......
......@@ -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;
......
......@@ -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) {
......
......@@ -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);
......
......@@ -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;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment