hibernate.c File Reference

Functions to close listeners, stop allowing new circuits, etc in preparation for closing down or going dormant; and to track bandwidth and time intervals to know when to hibernate and when to stop hibernating. More...

#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


Detailed Description

Functions to close listeners, stop allowing new circuits, etc in preparation for closing down or going dormant; and to track bandwidth and time intervals to know when to hibernate and when to stop hibernating.


Define Documentation

#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().


Enumeration Type Documentation

Possible values of hibernate_state

Enumerator:
HIBERNATE_STATE_LIVE  We are running normally.
HIBERNATE_STATE_EXITING  We're trying to shut down cleanly, and we'll kill all active connections at shutdown_time.
HIBERNATE_STATE_LOWBANDWIDTH  We're running low on allocated bandwidth for this period, so we won't accept any new connections.
HIBERNATE_STATE_DORMANT  We are hibernating, and we won't wake up till there's more bandwidth to use.

Possible accounting periods.


Function Documentation

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 
)

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]

void configure_accounting ( time_t  now  ) 

void consider_hibernation ( time_t  now  ) 

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]

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]

static void hibernate_go_dormant ( time_t  now  )  [static]

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]

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   ) 


Variable Documentation

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]

time_t interval_start_time = 0 [static]

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]

uint64_t n_bytes_written_in_interval = 0 [static]

uint32_t n_seconds_active_in_interval = 0 [static]

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().


Generated on Tue May 25 00:30:44 2010 for tor by  doxygen 1.5.6