mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-13 06:33:44 +01:00
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.
This commit is contained in:
parent
8527a29966
commit
4903ab1caa
@ -126,7 +126,7 @@ static config_abbrev_t option_abbrevs_[] = {
|
|||||||
*/
|
*/
|
||||||
static config_var_t option_vars_[] = {
|
static config_var_t option_vars_[] = {
|
||||||
V(AccountingMax, MEMUNIT, "0 bytes"),
|
V(AccountingMax, MEMUNIT, "0 bytes"),
|
||||||
V(AccountingRule, STRING, "max"),
|
VAR("AccountingRule", STRING, AccountingRule_option, "max"),
|
||||||
V(AccountingStart, STRING, NULL),
|
V(AccountingStart, STRING, NULL),
|
||||||
V(Address, STRING, NULL),
|
V(Address, STRING, NULL),
|
||||||
V(AllowDotExit, BOOL, "0"),
|
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 "
|
"risky: they will all turn off at the same time, which may "
|
||||||
"alert observers that they are being run by the same party.");
|
"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'");
|
REJECT("AccountingRule must be 'sum' or 'max'");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -415,9 +415,10 @@ configure_accounting(time_t now)
|
|||||||
static uint64_t
|
static uint64_t
|
||||||
get_accounting_bytes(void)
|
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 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
|
/** 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
|
/* max_configured is the larger of bytes read and bytes written
|
||||||
* If we are accounting based on sum, worst case is both are
|
* If we are accounting based on sum, worst case is both are
|
||||||
* at max, doubling the expected sum of bandwidth */
|
* at max, doubling the expected sum of bandwidth */
|
||||||
if (strcmp(get_options()->AccountingRule, "sum") == 0)
|
if (get_options()->AccountingRule == ACCT_SUM)
|
||||||
max_configured *= 2;
|
max_configured *= 2;
|
||||||
|
|
||||||
#define MIN_TIME_FOR_MEASUREMENT (1800)
|
#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));
|
U64_PRINTF_ARG(n_bytes_written_in_interval));
|
||||||
} else if (!strcmp(question, "accounting/bytes-left")) {
|
} else if (!strcmp(question, "accounting/bytes-left")) {
|
||||||
uint64_t limit = get_options()->AccountingMax;
|
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_left = 0;
|
||||||
uint64_t total_bytes = get_accounting_bytes();
|
uint64_t total_bytes = get_accounting_bytes();
|
||||||
if (total_bytes < limit)
|
if (total_bytes < limit)
|
||||||
|
@ -3775,10 +3775,11 @@ typedef struct {
|
|||||||
uint64_t AccountingMax; /**< How many bytes do we allow per accounting
|
uint64_t AccountingMax; /**< How many bytes do we allow per accounting
|
||||||
* interval before hibernation? 0 for "never
|
* interval before hibernation? 0 for "never
|
||||||
* hibernate." */
|
* hibernate." */
|
||||||
char *AccountingRule; /**< How do we determine when our AccountingMax
|
/** How do we determine when our AccountingMax has been reached?
|
||||||
* has been reached?
|
* "max" for when in or out reaches AccountingMax
|
||||||
* "max" for when in or out reaches AccountingMax
|
* "sum for when in plus 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. */
|
/** Base64-encoded hash of accepted passwords for the control system. */
|
||||||
config_line_t *HashedControlPassword;
|
config_line_t *HashedControlPassword;
|
||||||
|
@ -1093,7 +1093,7 @@ decide_to_advertise_dirport(const or_options_t *options, uint16_t dir_port)
|
|||||||
interval_length);
|
interval_length);
|
||||||
|
|
||||||
acc_bytes = options->AccountingMax;
|
acc_bytes = options->AccountingMax;
|
||||||
if (strcmp(options->AccountingRule, "sum") == 0)
|
if (get_options()->AccountingRule == ACCT_SUM)
|
||||||
acc_bytes /= 2;
|
acc_bytes /= 2;
|
||||||
if (effective_bw >=
|
if (effective_bw >=
|
||||||
acc_bytes / interval_length) {
|
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();
|
or_state_t *state = get_or_state();
|
||||||
char *acc_rcvd = bytes_to_usage(state->AccountingBytesReadInInterval);
|
char *acc_rcvd = bytes_to_usage(state->AccountingBytesReadInInterval);
|
||||||
char *acc_sent = bytes_to_usage(state->AccountingBytesWrittenInInterval);
|
char *acc_sent = bytes_to_usage(state->AccountingBytesWrittenInInterval);
|
||||||
const char *acc_rule = options->AccountingRule;
|
|
||||||
uint64_t acc_bytes = options->AccountingMax;
|
uint64_t acc_bytes = options->AccountingMax;
|
||||||
char *acc_max;
|
char *acc_max;
|
||||||
time_t interval_end = accounting_get_end_time();
|
time_t interval_end = accounting_get_end_time();
|
||||||
char end_buf[ISO_TIME_LEN + 1];
|
char end_buf[ISO_TIME_LEN + 1];
|
||||||
char *remaining = NULL;
|
char *remaining = NULL;
|
||||||
if (strcmp(acc_rule, "sum") == 0)
|
if (options->AccountingRule == ACCT_SUM)
|
||||||
acc_bytes *= 2;
|
acc_bytes *= 2;
|
||||||
acc_max = bytes_to_usage(acc_bytes);
|
acc_max = bytes_to_usage(acc_bytes);
|
||||||
format_local_iso_time(end_buf, interval_end);
|
format_local_iso_time(end_buf, interval_end);
|
||||||
|
@ -34,7 +34,7 @@ test_accounting_limits(void *arg)
|
|||||||
or_state = or_state_new();
|
or_state = or_state_new();
|
||||||
|
|
||||||
options->AccountingMax = 100;
|
options->AccountingMax = 100;
|
||||||
options->AccountingRule = tor_strdup("max");
|
options->AccountingRule = ACCT_MAX;
|
||||||
|
|
||||||
tor_assert(accounting_is_enabled(options));
|
tor_assert(accounting_is_enabled(options));
|
||||||
configure_accounting(fake_time);
|
configure_accounting(fake_time);
|
||||||
@ -50,7 +50,7 @@ test_accounting_limits(void *arg)
|
|||||||
tor_assert(we_are_hibernating() == 1);
|
tor_assert(we_are_hibernating() == 1);
|
||||||
|
|
||||||
options->AccountingMax = 200;
|
options->AccountingMax = 200;
|
||||||
options->AccountingRule = tor_strdup("sum");
|
options->AccountingRule = ACCT_SUM;
|
||||||
|
|
||||||
accounting_add_bytes(0, 10, 1);
|
accounting_add_bytes(0, 10, 1);
|
||||||
fake_time += 1;
|
fake_time += 1;
|
||||||
|
Loading…
Reference in New Issue
Block a user