Tor  0.4.7.0-alpha-dev
Macros | Functions | Variables
bwhist.c File Reference

Tracking for relay bandwidth history. More...

#include "orconfig.h"
#include "core/or/or.h"
#include "feature/stats/bwhist.h"
#include "app/config/config.h"
#include "app/config/statefile.h"
#include "feature/relay/routermode.h"
#include "feature/stats/bw_array_st.h"
#include "app/config/or_state_st.h"
#include "app/config/or_options_st.h"

Go to the source code of this file.

Macros

#define BWHIST_PRIVATE
 
#define MAX_HIST_VALUE_LEN   (21*NUM_TOTALS)
 
#define UPDATE(arrname, st)
 
#define LOAD(arrname, st)
 

Functions

STATIC void commit_max (bw_array_t *b)
 
STATIC void advance_obs (bw_array_t *b)
 
STATIC void add_obs (bw_array_t *b, time_t when, uint64_t n)
 
STATIC bw_array_tbw_array_new (void)
 
STATIC void bw_array_free_ (bw_array_t *b)
 
void bwhist_init (void)
 
void bwhist_note_bytes_written (uint64_t num_bytes, time_t when, bool ipv6)
 
void bwhist_note_bytes_read (uint64_t num_bytes, time_t when, bool ipv6)
 
void bwhist_note_dir_bytes_written (uint64_t num_bytes, time_t when)
 
void bwhist_note_dir_bytes_read (uint64_t num_bytes, time_t when)
 
STATIC uint64_t find_largest_max (bw_array_t *b)
 
int bwhist_bandwidth_assess (void)
 
STATIC size_t bwhist_fill_bandwidth_history (char *buf, size_t len, const bw_array_t *b)
 
static void bwhist_get_one_bandwidth_line (buf_t *buf, const char *desc, const bw_array_t *b)
 
char * bwhist_get_bandwidth_lines (void)
 
static void bwhist_update_bwhist_state_section (or_state_t *state, const bw_array_t *b, smartlist_t **s_values, smartlist_t **s_maxima, time_t *s_begins, int *s_interval)
 
void bwhist_update_state (or_state_t *state)
 
static int bwhist_load_bwhist_state_section (bw_array_t *b, const smartlist_t *s_values, const smartlist_t *s_maxima, const time_t s_begins, const int s_interval)
 
int bwhist_load_state (or_state_t *state, char **err)
 
void bwhist_free_all (void)
 

Variables

static bw_array_tread_array = NULL
 
static bw_array_tread_array_ipv6 = NULL
 
STATIC bw_array_twrite_array = NULL
 
static bw_array_twrite_array_ipv6 = NULL
 
static bw_array_tdir_read_array = NULL
 
static bw_array_tdir_write_array = NULL
 

Detailed Description

Tracking for relay bandwidth history.

This module handles bandwidth usage history, used by relays to self-report how much bandwidth they've used for different purposes over last day or so, in order to generate the {dirreq-,}{read,write}-history lines in that they publish.

Definition in file bwhist.c.

Macro Definition Documentation

◆ LOAD

#define LOAD (   arrname,
  st 
)
Value:
(arrname), \
state->BWHistory ## st ## Values, \
state->BWHistory ## st ## Maxima, \
state->BWHistory ## st ## Ends, \
state->BWHistory ## st ## Interval)<0) \
all_ok = 0
static int bwhist_load_bwhist_state_section(bw_array_t *b, const smartlist_t *s_values, const smartlist_t *s_maxima, const time_t s_begins, const int s_interval)
Definition: bwhist.c:428

◆ UPDATE

#define UPDATE (   arrname,
  st 
)
Value:
(arrname),\
&state->BWHistory ## st ## Values, \
&state->BWHistory ## st ## Maxima, \
&state->BWHistory ## st ## Ends, \
&state->BWHistory ## st ## Interval)
static void bwhist_update_bwhist_state_section(or_state_t *state, const bw_array_t *b, smartlist_t **s_values, smartlist_t **s_maxima, time_t *s_begins, int *s_interval)
Definition: bwhist.c:342

Function Documentation

◆ add_obs()

STATIC void add_obs ( bw_array_t b,
time_t  when,
uint64_t  n 
)

Add n bytes to the number of bytes in b for second when.

Definition at line 78 of file bwhist.c.

Referenced by bwhist_note_bytes_read(), bwhist_note_bytes_written(), bwhist_note_dir_bytes_read(), and bwhist_note_dir_bytes_written().

◆ advance_obs()

STATIC void advance_obs ( bw_array_t b)

Shift the current observation time of b forward by one second.

Definition at line 52 of file bwhist.c.

Referenced by add_obs().

◆ bw_array_free_()

STATIC void bw_array_free_ ( bw_array_t b)

Free storage held by bandwidth array b.

Definition at line 112 of file bwhist.c.

◆ bw_array_new()

STATIC bw_array_t* bw_array_new ( void  )

Allocate, initialize, and return a new bw_array.

Definition at line 99 of file bwhist.c.

Referenced by bwhist_init().

◆ bwhist_bandwidth_assess()

int bwhist_bandwidth_assess ( void  )

Find the largest sums in the past NUM_SECS_BW_SUM_IS_VALID (roughly) seconds. Find one sum for reading and one for writing. They don't have to be at the same time.

Return the smaller of these sums, divided by NUM_SECS_ROLLING_MEASURE.

Definition at line 233 of file bwhist.c.

Referenced by check_descriptor_bandwidth_changed().

◆ bwhist_fill_bandwidth_history()

STATIC size_t bwhist_fill_bandwidth_history ( char *  buf,
size_t  len,
const bw_array_t b 
)

Print the bandwidth history of b (either [dir-]read_array or [dir-]write_array) into the buffer pointed to by buf. The format is simply comma separated numbers, from oldest to newest.

It returns the number of bytes written.

Definition at line 251 of file bwhist.c.

◆ bwhist_get_bandwidth_lines()

char* bwhist_get_bandwidth_lines ( void  )

Allocate and return lines for representing this server's bandwidth history in its descriptor. We publish these lines in our extra-info descriptor.

Definition at line 323 of file bwhist.c.

◆ bwhist_get_one_bandwidth_line()

static void bwhist_get_one_bandwidth_line ( buf_t *  buf,
const char *  desc,
const bw_array_t b 
)
static

Encode a single bandwidth history line into buf.

Definition at line 297 of file bwhist.c.

Referenced by bwhist_get_bandwidth_lines().

◆ bwhist_init()

void bwhist_init ( void  )

Set up structures for bandwidth history, clearing them if they already exist.

Definition at line 139 of file bwhist.c.

Referenced by tor_init().

◆ bwhist_load_bwhist_state_section()

static int bwhist_load_bwhist_state_section ( bw_array_t b,
const smartlist_t s_values,
const smartlist_t s_maxima,
const time_t  s_begins,
const int  s_interval 
)
static

Load a single bw_array_t from its Values, Ends, Maxima, and Interval entries in an or_state_t. Done while reading the state file.

Definition at line 428 of file bwhist.c.

◆ bwhist_load_state()

int bwhist_load_state ( or_state_t state,
char **  err 
)

Set bandwidth history from the state file we just loaded.

Definition at line 504 of file bwhist.c.

◆ bwhist_note_bytes_read()

void bwhist_note_bytes_read ( uint64_t  num_bytes,
time_t  when,
bool  ipv6 
)

Remember that we wrote num_bytes bytes in second when. (like bwhist_note_bytes_written() above)

Definition at line 183 of file bwhist.c.

Referenced by record_num_bytes_transferred_impl().

◆ bwhist_note_bytes_written()

void bwhist_note_bytes_written ( uint64_t  num_bytes,
time_t  when,
bool  ipv6 
)

Remember that we read num_bytes bytes in second when.

Add num_bytes to the current running total for when.

when can go back to time, but it's safe to ignore calls earlier than the latest when you've heard of.

Definition at line 164 of file bwhist.c.

Referenced by record_num_bytes_transferred_impl().

◆ bwhist_note_dir_bytes_read()

void bwhist_note_dir_bytes_read ( uint64_t  num_bytes,
time_t  when 
)

Remember that we read num_bytes directory bytes in second when. (like bwhist_note_bytes_written() above)

Definition at line 204 of file bwhist.c.

Referenced by record_num_bytes_transferred_impl().

◆ bwhist_note_dir_bytes_written()

void bwhist_note_dir_bytes_written ( uint64_t  num_bytes,
time_t  when 
)

Remember that we wrote num_bytes directory bytes in second when. (like bwhist_note_bytes_written() above)

Definition at line 195 of file bwhist.c.

Referenced by record_num_bytes_transferred_impl().

◆ bwhist_update_bwhist_state_section()

static void bwhist_update_bwhist_state_section ( or_state_t state,
const bw_array_t b,
smartlist_t **  s_values,
smartlist_t **  s_maxima,
time_t *  s_begins,
int *  s_interval 
)
static

Write a single bw_array_t into the Values, Ends, Interval, and Maximum entries of an or_state_t. Done before writing out a new state file.

Definition at line 342 of file bwhist.c.

◆ bwhist_update_state()

void bwhist_update_state ( or_state_t state)

Update state with the newest bandwidth history. Done before writing out a new state file.

Definition at line 402 of file bwhist.c.

◆ commit_max()

STATIC void commit_max ( bw_array_t b)

Shift the current period of b forward by one.

Definition at line 32 of file bwhist.c.

Referenced by advance_obs().

◆ find_largest_max()

STATIC uint64_t find_largest_max ( bw_array_t b)

Helper: Return the largest value in b->maxima. (This is equal to the most bandwidth used in any NUM_SECS_ROLLING_MEASURE period for the last NUM_SECS_BW_SUM_IS_VALID seconds.)

Definition at line 214 of file bwhist.c.

Referenced by bwhist_bandwidth_assess().

Variable Documentation

◆ dir_read_array

bw_array_t* dir_read_array = NULL
static

Recent history of bandwidth observations for read operations for the directory protocol.

Definition at line 131 of file bwhist.c.

Referenced by bwhist_get_bandwidth_lines(), bwhist_init(), bwhist_load_state(), and bwhist_note_dir_bytes_read().

◆ dir_write_array

bw_array_t* dir_write_array = NULL
static

Recent history of bandwidth observations for write operations for the directory protocol.

Definition at line 134 of file bwhist.c.

Referenced by bwhist_get_bandwidth_lines(), bwhist_init(), bwhist_load_state(), and bwhist_note_dir_bytes_written().

◆ read_array

bw_array_t* read_array = NULL
static

Recent history of bandwidth observations for (all) read operations.

Definition at line 122 of file bwhist.c.

Referenced by bwhist_bandwidth_assess(), bwhist_get_bandwidth_lines(), bwhist_init(), bwhist_load_state(), and bwhist_note_bytes_read().

◆ read_array_ipv6

bw_array_t* read_array_ipv6 = NULL
static

Recent history of bandwidth observations for IPv6 read operations.

Definition at line 124 of file bwhist.c.

Referenced by bwhist_get_bandwidth_lines(), bwhist_init(), bwhist_load_state(), and bwhist_note_bytes_read().

◆ write_array

STATIC bw_array_t* write_array = NULL

Recent history of bandwidth observations for (all) write operations.

Definition at line 126 of file bwhist.c.

Referenced by bwhist_bandwidth_assess(), bwhist_get_bandwidth_lines(), bwhist_init(), bwhist_load_state(), and bwhist_note_bytes_written().

◆ write_array_ipv6

bw_array_t* write_array_ipv6 = NULL
static

Recent history of bandwidth observations for IPv6 write operations.

Definition at line 128 of file bwhist.c.

Referenced by bwhist_get_bandwidth_lines(), bwhist_init(), bwhist_load_state(), and bwhist_note_bytes_written().