#include "or.h"
Defines | |
| #define | NOTE_INTERVAL (600) | 
| #define | NOTE_BYTES 20*(1024*1024) | 
| #define | GUESS_TIME_TO_USE_BANDWIDTH (24*60*60) | 
| #define | ROUND_UP(x) (((x) + 0x3ff) & ~0x3ff) | 
Enumerations | |
| enum | hibernate_state_t { HIBERNATE_STATE_LIVE = 1, HIBERNATE_STATE_EXITING = 2, HIBERNATE_STATE_LOWBANDWIDTH = 3, HIBERNATE_STATE_DORMANT = 4 } | 
| enum | time_unit_t { UNIT_MONTH = 1, UNIT_WEEK = 2, UNIT_DAY = 3 } | 
Functions | |
| static void | reset_accounting (time_t now) | 
| static int | read_bandwidth_usage (void) | 
| static time_t | start_of_accounting_period_after (time_t now) | 
| static time_t | start_of_accounting_period_containing (time_t now) | 
| static void | accounting_set_wakeup_time (void) | 
| int | accounting_parse_options (or_options_t *options, int validate_only) | 
| int | accounting_is_enabled (or_options_t *options) | 
| void | accounting_add_bytes (size_t n_read, size_t n_written, int seconds) | 
| static time_t | edge_of_accounting_period_containing (time_t now, int get_end) | 
| void | configure_accounting (time_t now) | 
| static void | update_expected_bandwidth (void) | 
| static INLINE int | time_to_record_bandwidth_usage (time_t now) | 
| void | accounting_run_housekeeping (time_t now) | 
| int | accounting_record_bandwidth_usage (time_t now, or_state_t *state) | 
| static int | hibernate_hard_limit_reached (void) | 
| static int | hibernate_soft_limit_reached (void) | 
| static void | hibernate_begin (hibernate_state_t new_state, time_t now) | 
| static void | hibernate_end (hibernate_state_t new_state) | 
| void | hibernate_begin_shutdown (void) | 
| int | we_are_hibernating (void) | 
| static void | hibernate_go_dormant (time_t now) | 
| static void | hibernate_end_time_elapsed (time_t now) | 
| void | consider_hibernation (time_t now) | 
| int | getinfo_helper_accounting (control_connection_t *conn, const char *question, char **answer) | 
Variables | |
| long | stats_n_seconds_working | 
| static hibernate_state_t | hibernate_state = HIBERNATE_STATE_LIVE | 
| static time_t | hibernate_end_time = 0 | 
| static time_t | shutdown_time = 0 | 
| static uint64_t | n_bytes_read_in_interval = 0 | 
| static uint64_t | n_bytes_written_in_interval = 0 | 
| static uint32_t | n_seconds_active_in_interval = 0 | 
| static time_t | interval_start_time = 0 | 
| static time_t | interval_end_time = 0 | 
| static time_t | interval_wakeup_time = 0 | 
| static uint64_t | expected_bandwidth_usage = 0 | 
| static time_unit_t | cfg_unit = UNIT_MONTH | 
| static int | cfg_start_day = 0 | 
| static int | cfg_start_hour = 0 | 
| static int | cfg_start_min = 0 | 
| #define GUESS_TIME_TO_USE_BANDWIDTH (24*60*60) | 
When we have no idea how fast we are, how long do we assume it will take us to exhaust our bandwidth?
Referenced by accounting_set_wakeup_time().
| enum hibernate_state_t | 
Possible values of hibernate_state
| enum time_unit_t | 
Possible accounting periods.
| void accounting_add_bytes | ( | size_t | n_read, | |
| size_t | n_written, | |||
| int | seconds | |||
| ) | 
Called from main.c to tell us that seconds seconds have passed, n_read bytes have been read, and n_written bytes have been written.
References n_bytes_read_in_interval, n_bytes_written_in_interval, and n_seconds_active_in_interval.
Referenced by second_elapsed_callback().
| int accounting_is_enabled | ( | or_options_t * | options | ) | 
If we want to manage the accounting system and potentially hibernate, return 1, else return 0.
References or_options_t::AccountingMax.
Referenced by decide_to_advertise_dirport(), do_hup(), getinfo_helper_accounting(), options_act(), or_state_save(), run_scheduled_events(), second_elapsed_callback(), and tor_cleanup().
| int accounting_parse_options | ( | or_options_t * | options, | |
| int | validate_only | |||
| ) | 
Configure accounting start/end time settings based on options->AccountingStart. Return 0 on success, -1 on failure. If validate_only is true, do not change the current settings.
References or_options_t::AccountingStart, cfg_start_day, cfg_unit, LD_CONFIG, smartlist_create(), smartlist_free(), smartlist_split_string(), tor_assert, tor_free, and tor_parse_long().
Referenced by options_act().
| int accounting_record_bandwidth_usage | ( | time_t | now, | |
| or_state_t * | state | |||
| ) | 
Save all our bandwidth tracking information to disk. Return 0 on success, -1 on failure.
References or_state_t::AccountingBytesReadInInterval, or_state_t::AccountingBytesWrittenInInterval, or_state_t::AccountingExpectedUsage, or_state_t::AccountingIntervalStart, or_state_t::AccountingSecondsActive, expected_bandwidth_usage, get_options(), interval_start_time, n_bytes_read_in_interval, n_bytes_written_in_interval, n_seconds_active_in_interval, and or_state_mark_dirty().
Referenced by accounting_run_housekeeping(), do_hup(), hibernate_begin(), hibernate_go_dormant(), and tor_cleanup().
| void accounting_run_housekeeping | ( | time_t | now | ) | 
Invoked once per second. Checks whether it is time to hibernate, record bandwidth used, etc.
References accounting_record_bandwidth_usage(), configure_accounting(), get_or_state(), interval_end_time, LD_FS, and time_to_record_bandwidth_usage().
Referenced by hibernate_end_time_elapsed(), or_state_save(), and run_scheduled_events().
| static void accounting_set_wakeup_time | ( | void | ) |  [static] | 
        
Based on our interval and our estimated bandwidth, choose a deterministic (but random-ish) time to wake up.
References or_options_t::AccountingMax, crypto_digest_add_bytes(), crypto_digest_get_digest(), crypto_free_digest_env(), crypto_new_digest_env(), crypto_pk_get_digest(), DIGEST_LEN, expected_bandwidth_usage, format_iso_time(), format_local_iso_time(), get_identity_key(), get_options(), get_uint32(), GUESS_TIME_TO_USE_BANDWIDTH, identity_key_is_set(), init_keys(), interval_end_time, interval_start_time, interval_wakeup_time, LD_ACCT, LD_BUG, and tor_assert.
Referenced by configure_accounting().
| void configure_accounting | ( | time_t | now | ) | 
Initialize the accounting subsystem.
References accounting_set_wakeup_time(), interval_end_time, interval_start_time, LD_ACCT, read_bandwidth_usage(), reset_accounting(), start_of_accounting_period_after(), and start_of_accounting_period_containing().
Referenced by accounting_run_housekeeping(), and options_act().
| void consider_hibernation | ( | time_t | now | ) | 
Consider our environment and decide if it's time to start/stop hibernating.
References or_options_t::AccountingMax, format_local_iso_time(), get_options(), hibernate_begin(), hibernate_end_time, hibernate_end_time_elapsed(), hibernate_go_dormant(), hibernate_hard_limit_reached(), hibernate_soft_limit_reached(), hibernate_state, HIBERNATE_STATE_DORMANT, HIBERNATE_STATE_EXITING, HIBERNATE_STATE_LIVE, HIBERNATE_STATE_LOWBANDWIDTH, interval_wakeup_time, LD_ACCT, LD_GENERAL, shutdown_time, tor_assert, and tor_cleanup().
Referenced by run_scheduled_events().
| static time_t edge_of_accounting_period_containing | ( | time_t | now, | |
| int | get_end | |||
| ) |  [static] | 
        
If get_end, return the end of the accounting period that contains the time now. Else, return the start of the accounting period that contains the time now
References cfg_start_day, cfg_unit, and tor_assert.
Referenced by start_of_accounting_period_after(), and start_of_accounting_period_containing().
| int getinfo_helper_accounting | ( | control_connection_t * | conn, | |
| const char * | question, | |||
| char ** | answer | |||
| ) | 
Helper function: called when we get a GETINFO request for an accounting-related key on the control connection conn. If we can answer the request for question, then set *answer to a newly allocated string holding the result. Otherwise, set *answer to NULL.
References accounting_is_enabled(), or_options_t::AccountingMax, format_iso_time(), get_options(), hibernate_state, HIBERNATE_STATE_DORMANT, HIBERNATE_STATE_LOWBANDWIDTH, interval_end_time, interval_start_time, interval_wakeup_time, n_bytes_read_in_interval, n_bytes_written_in_interval, and tor_snprintf().
| static void hibernate_begin | ( | hibernate_state_t | new_state, | |
| time_t | now | |||
| ) |  [static] | 
        
Called when we get a SIGINT, or when bandwidth soft limit is reached. Puts us into "loose hibernation": we don't accept new connections, but we continue handling old ones.
References accounting_record_bandwidth_usage(), CONN_TYPE_AP_DNS_LISTENER, CONN_TYPE_AP_LISTENER, CONN_TYPE_AP_NATD_LISTENER, CONN_TYPE_AP_TRANS_LISTENER, CONN_TYPE_DIR_LISTENER, CONN_TYPE_OR_LISTENER, connection_get_by_type(), get_options(), get_or_state(), hibernate_end_time, hibernate_state, HIBERNATE_STATE_EXITING, HIBERNATE_STATE_LIVE, interval_end_time, LD_GENERAL, LD_NET, or_state_mark_dirty(), shutdown_time, or_options_t::ShutdownWaitLength, tor_cleanup(), and connection_t::type.
Referenced by consider_hibernation(), hibernate_begin_shutdown(), and hibernate_go_dormant().
| void hibernate_begin_shutdown | ( | void | ) | 
A wrapper around hibernate_begin, for when we get SIGINT.
References hibernate_begin(), and HIBERNATE_STATE_EXITING.
Referenced by signal_callback().
| static void hibernate_end | ( | hibernate_state_t | new_state | ) |  [static] | 
        
Called when we've been hibernating and our timeout is reached.
References hibernate_end_time, hibernate_state, HIBERNATE_STATE_DORMANT, HIBERNATE_STATE_LOWBANDWIDTH, LD_ACCT, stats_n_seconds_working, and tor_assert.
Referenced by hibernate_end_time_elapsed().
| static void hibernate_end_time_elapsed | ( | time_t | now | ) |  [static] | 
        
Called when hibernate_end_time has arrived.
References accounting_run_housekeeping(), format_iso_time(), hibernate_end(), hibernate_end_time, hibernate_go_dormant(), hibernate_state, HIBERNATE_STATE_DORMANT, HIBERNATE_STATE_LIVE, interval_wakeup_time, and LD_ACCT.
Referenced by consider_hibernation().
| static void hibernate_go_dormant | ( | time_t | now | ) |  [static] | 
        
If we aren't currently dormant, close all connections and become dormant.
References accounting_record_bandwidth_usage(), CONN_IS_EDGE, CONN_TYPE_AP, CONN_TYPE_EXIT, CONN_TYPE_OR, connection_edge_end(), connection_get_by_type(), get_options(), get_or_state(), hibernate_begin(), hibernate_end_time, hibernate_state, HIBERNATE_STATE_DORMANT, HIBERNATE_STATE_LOWBANDWIDTH, interval_end_time, interval_wakeup_time, LD_ACCT, LD_NET, or_state_mark_dirty(), TO_EDGE_CONN(), and connection_t::type.
Referenced by consider_hibernation(), and hibernate_end_time_elapsed().
| static int hibernate_hard_limit_reached | ( | void | ) |  [static] | 
        
Return true iff we have sent/received all the bytes we are willing to send/receive this interval.
References or_options_t::AccountingMax, get_options(), n_bytes_read_in_interval, and n_bytes_written_in_interval.
Referenced by consider_hibernation().
| static int hibernate_soft_limit_reached | ( | void | ) |  [static] | 
        
Return true iff we have sent/received almost all the bytes we are willing to send/receive this interval.
References get_options(), n_bytes_read_in_interval, and n_bytes_written_in_interval.
Referenced by consider_hibernation().
| static int read_bandwidth_usage | ( | void | ) |  [static] | 
        
Read stored accounting information from disk. Return 0 on success; return -1 and change nothing on failure.
References or_state_t::AccountingBytesReadInInterval, or_state_t::AccountingBytesWrittenInInterval, or_state_t::AccountingExpectedUsage, or_state_t::AccountingIntervalStart, or_state_t::AccountingSecondsActive, expected_bandwidth_usage, format_iso_time(), get_datadir_fname, get_or_state(), interval_start_time, or_state_t::LastWritten, LD_ACCT, n_bytes_read_in_interval, n_bytes_written_in_interval, n_seconds_active_in_interval, and tor_free.
Referenced by configure_accounting().
| static void reset_accounting | ( | time_t | now | ) |  [static] | 
        
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.
References interval_end_time, interval_start_time, LD_ACCT, n_bytes_read_in_interval, n_bytes_written_in_interval, n_seconds_active_in_interval, start_of_accounting_period_after(), start_of_accounting_period_containing(), and update_expected_bandwidth().
Referenced by configure_accounting().
| static time_t start_of_accounting_period_after | ( | time_t | now | ) |  [static] | 
        
Return the start of the accounting period that comes after the one containing the time now.
References edge_of_accounting_period_containing().
Referenced by configure_accounting(), and reset_accounting().
| static time_t start_of_accounting_period_containing | ( | time_t | now | ) |  [static] | 
        
Return the start of the accounting period containing the time now.
References edge_of_accounting_period_containing().
Referenced by configure_accounting(), and reset_accounting().
| static INLINE int time_to_record_bandwidth_usage | ( | time_t | now | ) |  [static] | 
        
Return true iff we should save our bandwidth usage to disk.
References interval_end_time, n_bytes_read_in_interval, and n_bytes_written_in_interval.
Referenced by accounting_run_housekeeping().
| static void update_expected_bandwidth | ( | void | ) |  [static] | 
        
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.
References or_options_t::BandwidthRate, expected_bandwidth_usage, get_options(), n_bytes_read_in_interval, n_bytes_written_in_interval, and n_seconds_active_in_interval.
Referenced by reset_accounting().
| int we_are_hibernating | ( | void | ) | 
Return true iff we are currently hibernating.
References hibernate_state, and HIBERNATE_STATE_LIVE.
Referenced by check_descriptor_bandwidth_changed(), check_whether_dirport_reachable(), command_process_create_cell(), connection_exit_begin_conn(), decide_to_advertise_dirport(), directory_info_has_arrived(), dirserv_set_router_is_running(), router_dump_router_to_string(), router_rebuild_descriptor(), router_set_status(), run_connection_housekeeping(), run_scheduled_events(), and second_elapsed_callback().
int cfg_start_day = 0 [static]           | 
        
How many days,hours,minutes into each unit does our accounting interval start?
Referenced by accounting_parse_options(), and edge_of_accounting_period_containing().
time_unit_t cfg_unit = UNIT_MONTH [static]           | 
        
What unit are we using for our accounting?
Referenced by accounting_parse_options(), and edge_of_accounting_period_containing().
uint64_t expected_bandwidth_usage = 0 [static]           | 
        
How much bandwidth do we 'expect' to use per minute? (0 if we have no info from the last period.)
Referenced by accounting_record_bandwidth_usage(), accounting_set_wakeup_time(), read_bandwidth_usage(), and update_expected_bandwidth().
time_t hibernate_end_time = 0 [static]           | 
        
If are hibernating, when do we plan to wake up? Set to 0 if we aren't hibernating.
Referenced by consider_hibernation(), hibernate_begin(), hibernate_end(), hibernate_end_time_elapsed(), and hibernate_go_dormant().
hibernate_state_t hibernate_state = HIBERNATE_STATE_LIVE [static]           | 
        
Are we currently awake, asleep, running out of bandwidth, or shutting down?
Referenced by consider_hibernation(), getinfo_helper_accounting(), hibernate_begin(), hibernate_end(), hibernate_end_time_elapsed(), hibernate_go_dormant(), and we_are_hibernating().
time_t interval_end_time = 0 [static]           | 
        
When will this accounting interval end?
Referenced by accounting_run_housekeeping(), accounting_set_wakeup_time(), configure_accounting(), getinfo_helper_accounting(), hibernate_begin(), hibernate_go_dormant(), reset_accounting(), and time_to_record_bandwidth_usage().
time_t interval_start_time = 0 [static]           | 
        
When did this accounting interval start?
Referenced by accounting_record_bandwidth_usage(), accounting_set_wakeup_time(), configure_accounting(), getinfo_helper_accounting(), read_bandwidth_usage(), and reset_accounting().
time_t interval_wakeup_time = 0 [static]           | 
        
How far into the accounting interval should we hibernate?
Referenced by accounting_set_wakeup_time(), consider_hibernation(), getinfo_helper_accounting(), hibernate_end_time_elapsed(), and hibernate_go_dormant().
uint64_t n_bytes_read_in_interval = 0 [static]           | 
        
How many bytes have we read in this accounting interval?
Referenced by accounting_add_bytes(), accounting_record_bandwidth_usage(), getinfo_helper_accounting(), hibernate_hard_limit_reached(), hibernate_soft_limit_reached(), read_bandwidth_usage(), reset_accounting(), time_to_record_bandwidth_usage(), and update_expected_bandwidth().
uint64_t n_bytes_written_in_interval = 0 [static]           | 
        
How many bytes have we written in this accounting interval?
Referenced by accounting_add_bytes(), accounting_record_bandwidth_usage(), getinfo_helper_accounting(), hibernate_hard_limit_reached(), hibernate_soft_limit_reached(), read_bandwidth_usage(), reset_accounting(), time_to_record_bandwidth_usage(), and update_expected_bandwidth().
uint32_t n_seconds_active_in_interval = 0 [static]           | 
        
How many seconds have we been running this interval?
Referenced by accounting_add_bytes(), accounting_record_bandwidth_usage(), read_bandwidth_usage(), reset_accounting(), and update_expected_bandwidth().
time_t shutdown_time = 0 [static]           | 
        
If we are shutting down, when do we plan finally exit? Set to 0 if we aren't shutting down.
Referenced by consider_hibernation(), and hibernate_begin().
How many seconds have we been running?
Referenced by directory_all_unreachable(), hibernate_end(), ip_address_changed(), router_dump_router_to_string(), run_scheduled_events(), and second_elapsed_callback().
 1.5.6