📄 hibernate.c
字号:
case UNIT_DAY:
if (before)
--tm.tm_mday;
if (get_end)
++tm.tm_mday;
break;
default:
tor_assert(0);
}
tm.tm_hour = cfg_start_hour;
tm.tm_min = cfg_start_min;
tm.tm_sec = 0;
tm.tm_isdst = -1; /* Autodetect DST */
return mktime(&tm);
}
/** Return the start of the accounting period containing the time
* <b>now</b>. */
static time_t
start_of_accounting_period_containing(time_t now)
{
return edge_of_accounting_period_containing(now, 0);
}
/** Return the start of the accounting period that comes after the one
* containing the time <b>now</b>. */
static time_t
start_of_accounting_period_after(time_t now)
{
return edge_of_accounting_period_containing(now, 1);
}
/** Initialize the accounting subsystem. */
void
configure_accounting(time_t 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. */
log_info(LD_ACCT, "Starting new accounting interval.");
reset_accounting(now);
} else if (interval_start_time ==
start_of_accounting_period_containing(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 {
log_warn(LD_ACCT,
"Mismatched accounting interval; starting a fresh one.");
reset_accounting(now);
}
accounting_set_wakeup_time();
}
/** Set expected_bandwidth_usage based on how much we sent/received
* per minute last interval (if we were up for at least 30 minutes),
* or based on our declared bandwidth otherwise. */
static void
update_expected_bandwidth(void)
{
uint64_t used, expected;
uint64_t max_configured = (get_options()->BandwidthRate * 60);
if (n_seconds_active_in_interval < 1800) {
/* If we haven't gotten enough data last interval, set 'expected'
* to 0. This will set our wakeup to the start of the interval.
* Next interval, we'll choose our starting time based on how much
* we sent this interval.
*/
expected = 0;
} else {
used = n_bytes_written_in_interval < n_bytes_read_in_interval ?
n_bytes_read_in_interval : n_bytes_written_in_interval;
expected = used / (n_seconds_active_in_interval / 60);
if (expected > max_configured)
expected = max_configured;
}
expected_bandwidth_usage = expected;
}
/** Called at the start of a new accounting interval: reset our
* expected bandwidth usage based on what happened last time, set up
* the start and end of the interval, and clear byte/time totals.
*/
static void
reset_accounting(time_t now)
{
log_info(LD_ACCT, "Starting new accounting interval.");
update_expected_bandwidth();
interval_start_time = start_of_accounting_period_containing(now);
interval_end_time = start_of_accounting_period_after(interval_start_time);
n_bytes_read_in_interval = 0;
n_bytes_written_in_interval = 0;
n_seconds_active_in_interval = 0;
}
/** Return true iff we should save our bandwidth usage to disk. */
static INLINE int
time_to_record_bandwidth_usage(time_t now)
{
/* Note every 600 sec */
#define NOTE_INTERVAL (600)
/* Or every 20 megabytes */
#define NOTE_BYTES 20*(1024*1024)
static uint64_t last_read_bytes_noted = 0;
static uint64_t last_written_bytes_noted = 0;
static time_t last_time_noted = 0;
if (last_time_noted + NOTE_INTERVAL <= now ||
last_read_bytes_noted + NOTE_BYTES <= n_bytes_read_in_interval ||
last_written_bytes_noted + NOTE_BYTES <= n_bytes_written_in_interval ||
(interval_end_time && interval_end_time <= now)) {
last_time_noted = now;
last_read_bytes_noted = n_bytes_read_in_interval;
last_written_bytes_noted = n_bytes_written_in_interval;
return 1;
}
return 0;
}
/** Invoked once per second. Checks whether it is time to hibernate,
* record bandwidth used, etc. */
void
accounting_run_housekeeping(time_t now)
{
if (now >= interval_end_time) {
configure_accounting(now);
}
if (time_to_record_bandwidth_usage(now)) {
if (accounting_record_bandwidth_usage(now, get_or_state())) {
log_warn(LD_FS, "Couldn't record bandwidth usage to disk.");
}
}
}
/** When we have no idea how fast we are, how long do we assume it will take
* us to exhaust our bandwidth? */
#define GUESS_TIME_TO_USE_BANDWIDTH (24*60*60)
/** Based on our interval and our estimated bandwidth, choose a
* deterministic (but random-ish) time to wake up. */
static void
accounting_set_wakeup_time(void)
{
char buf[ISO_TIME_LEN+1];
char digest[DIGEST_LEN];
crypto_digest_env_t *d_env;
int time_in_interval;
uint64_t time_to_exhaust_bw;
int time_to_consider;
if (! identity_key_is_set()) {
if (init_keys() < 0) {
log_err(LD_BUG, "Error initializing keys");
tor_assert(0);
}
}
format_iso_time(buf, interval_start_time);
crypto_pk_get_digest(get_identity_key(), digest);
d_env = crypto_new_digest_env();
crypto_digest_add_bytes(d_env, buf, ISO_TIME_LEN);
crypto_digest_add_bytes(d_env, digest, DIGEST_LEN);
crypto_digest_get_digest(d_env, digest, DIGEST_LEN);
crypto_free_digest_env(d_env);
if (!expected_bandwidth_usage) {
char buf1[ISO_TIME_LEN+1];
char buf2[ISO_TIME_LEN+1];
format_local_iso_time(buf1, interval_start_time);
format_local_iso_time(buf2, interval_end_time);
time_to_exhaust_bw = GUESS_TIME_TO_USE_BANDWIDTH;
interval_wakeup_time = interval_start_time;
log_notice(LD_ACCT,
"Configured hibernation. This interval begins at %s "
"and ends at %s. We have no prior estimate for bandwidth, so "
"we will start out awake and hibernate when we exhaust our quota.",
buf1, buf2);
return;
}
time_in_interval = (int)(interval_end_time - interval_start_time);
time_to_exhaust_bw =
(get_options()->AccountingMax/expected_bandwidth_usage)*60;
if (time_to_exhaust_bw > TIME_MAX) {
time_to_exhaust_bw = TIME_MAX;
time_to_consider = 0;
} else {
time_to_consider = time_in_interval - (int)time_to_exhaust_bw;
}
if (time_to_consider<=0) {
interval_wakeup_time = interval_start_time;
} else {
/* XXX can we simplify this just by picking a random (non-deterministic)
* time to be up? If we go down and come up, then we pick a new one. Is
* that good enough? -RD */
/* This is not a perfectly unbiased conversion, but it is good enough:
* in the worst case, the first half of the day is 0.06 percent likelier
* to be chosen than the last half. */
interval_wakeup_time = interval_start_time +
(get_uint32(digest) % time_to_consider);
format_iso_time(buf, interval_wakeup_time);
}
{
char buf1[ISO_TIME_LEN+1];
char buf2[ISO_TIME_LEN+1];
char buf3[ISO_TIME_LEN+1];
char buf4[ISO_TIME_LEN+1];
time_t down_time;
if (interval_wakeup_time+time_to_exhaust_bw > TIME_MAX)
down_time = TIME_MAX;
else
down_time = (time_t)(interval_wakeup_time+time_to_exhaust_bw);
if (down_time>interval_end_time)
down_time = interval_end_time;
format_local_iso_time(buf1, interval_start_time);
format_local_iso_time(buf2, interval_wakeup_time);
format_local_iso_time(buf3, down_time);
format_local_iso_time(buf4, interval_end_time);
log_notice(LD_ACCT,
"Configured hibernation. This interval began at %s; "
"the scheduled wake-up time %s %s; "
"we expect%s to exhaust our quota for this interval around %s; "
"the next interval begins at %s (all times local)",
buf1,
time(NULL)<interval_wakeup_time?"is":"was", buf2,
time(NULL)<down_time?"":"ed", buf3,
buf4);
}
}
/* This rounds 0 up to 1000, but that's actually a feature. */
#define ROUND_UP(x) (((x) + 0x3ff) & ~0x3ff)
/** Save all our bandwidth tracking information to disk. Return 0 on
* success, -1 on failure. */
int
accounting_record_bandwidth_usage(time_t now, or_state_t *state)
{
/* Just update the state */
state->AccountingIntervalStart = interval_start_time;
state->AccountingBytesReadInInterval = ROUND_UP(n_bytes_read_in_interval);
state->AccountingBytesWrittenInInterval =
ROUND_UP(n_bytes_written_in_interval);
state->AccountingSecondsActive = n_seconds_active_in_interval;
state->AccountingExpectedUsage = expected_bandwidth_usage;
or_state_mark_dirty(state,
now+(get_options()->AvoidDiskWrites ? 7200 : 60));
return 0;
}
#undef ROUND_UP
/** Read stored accounting information from disk. Return 0 on success;
* return -1 and change nothing on failure. */
static int
read_bandwidth_usage(void)
{
or_state_t *state = get_or_state();
{
char *fname = get_datadir_fname("bw_accounting");
unlink(fname);
tor_free(fname);
}
if (!state)
return 0;
/* Okay; it looks like the state file is more up-to-date than the
* bw_accounting file, or the bw_accounting file is nonexistant,
* or the bw_accounting file is corrupt.
*/
log_info(LD_ACCT, "Reading bandwdith accounting data from state file");
n_bytes_read_in_interval = state->AccountingBytesReadInInterval;
n_bytes_written_in_interval = state->AccountingBytesWrittenInInterval;
n_seconds_active_in_interval = state->AccountingSecondsActive;
interval_start_time = state->AccountingIntervalStart;
expected_bandwidth_usage = state->AccountingExpectedUsage;
{
char tbuf1[ISO_TIME_LEN+1];
char tbuf2[ISO_TIME_LEN+1];
format_iso_time(tbuf1, state->LastWritten);
format_iso_time(tbuf2, state->AccountingIntervalStart);
log_info(LD_ACCT,
"Successfully read bandwidth accounting info from state written at %s "
"for interval starting at %s. We have been active for %lu seconds in "
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -