Data Structures | Macros | Functions | Variables
connstats.c File Reference

Count bidirectional vs one-way connections. More...

#include "orconfig.h"
#include "core/or/or.h"
#include "feature/stats/connstats.h"
#include "app/config/config.h"

Go to the source code of this file.

Data Structures

struct  conn_counts_t
struct  bidi_map_entry_t


#define BIDI_THRESHOLD   20480
#define BIDI_FACTOR   10
#define BIDI_INTERVAL   10


void conn_stats_init (time_t now)
static HT_HEAD (bidimap, bidi_map_entry_t)
static unsigned bidi_map_ent_hash (const bidi_map_entry_t *entry)
 HT_PROTOTYPE (bidimap, bidi_map_entry_t, node, bidi_map_ent_hash, bidi_map_ent_eq)
 HT_GENERATE2 (bidimap, bidi_map_entry_t, node, bidi_map_ent_hash, bidi_map_ent_eq, 0.6, tor_reallocarray_, tor_free_)
void conn_stats_free_all (void)
void conn_stats_reset (time_t now)
void conn_stats_terminate (void)
static void add_entry_to_count (conn_counts_t *cnt, const bidi_map_entry_t *ent)
static void collect_period_statistics (void)
void conn_stats_note_or_conn_bytes (uint64_t conn_id, size_t num_read, size_t num_written, time_t when, bool is_ipv6)
char * conn_stats_format (time_t now)
time_t conn_stats_save (time_t now)


static time_t start_of_conn_stats_interval
static time_t bidi_next_interval = 0
static conn_counts_t counts
static conn_counts_t counts_ipv6

Detailed Description

Count bidirectional vs one-way connections.

Connection statistics, use to track one-way and bidirectional connections.

Note that this code counts concurrent connections in each BIDI_INTERVAL-second interval, not total connections. It can tell you what fraction of connections are bidirectional at each time, not necessarily what number are bidirectional.

Definition in file connstats.c.

Macro Definition Documentation


#define BIDI_FACTOR   10

Count connections that we read or wrote at least this factor as many bytes from/to than we wrote or read to/from as mostly reading or writing.

Definition at line 42 of file connstats.c.


#define BIDI_INTERVAL   10

Interval length in seconds for considering read and written bytes for connection stats.

Definition at line 46 of file connstats.c.


#define BIDI_THRESHOLD   20480

Count connections on which we read and wrote less than this many bytes as "below threshold."

Definition at line 37 of file connstats.c.

Function Documentation

◆ add_entry_to_count()

static void add_entry_to_count ( conn_counts_t cnt,
const bidi_map_entry_t ent 

Record a single entry ent in the counts structure cnt.

Definition at line 145 of file connstats.c.

Referenced by collect_period_statistics().

◆ bidi_map_ent_hash()

static unsigned bidi_map_ent_hash ( const bidi_map_entry_t entry)

Hashtable helper: compute a digest for the key of entry.

Definition at line 100 of file connstats.c.

◆ collect_period_statistics()

static void collect_period_statistics ( void  )

Count all the connection information we've received during the current period in 'bidimap', and store that information in the appropriate count structures.

Definition at line 163 of file connstats.c.

Referenced by conn_stats_note_or_conn_bytes().

◆ conn_stats_format()

char* conn_stats_format ( time_t  now)

Return a newly allocated string containing the connection statistics until now, or NULL if we're not collecting conn stats. Caller must ensure start_of_conn_stats_interval is in the past.

Definition at line 224 of file connstats.c.

◆ conn_stats_free_all()

void conn_stats_free_all ( void  )

Release all storage held in connstats.c

Definition at line 112 of file connstats.c.

Referenced by conn_stats_reset().

◆ conn_stats_init()

void conn_stats_init ( time_t  now)

Initialize connection stats.

Definition at line 30 of file connstats.c.

◆ conn_stats_note_or_conn_bytes()

void conn_stats_note_or_conn_bytes ( uint64_t  conn_id,
size_t  num_read,
size_t  num_written,
time_t  when,
bool  is_ipv6 

We read num_read bytes and wrote num_written from/to OR connection conn_id in second when. If this is the first observation in a new interval, sum up the last observations. Add bytes for this connection.

Definition at line 185 of file connstats.c.

Referenced by record_num_bytes_transferred_impl().

◆ conn_stats_reset()

void conn_stats_reset ( time_t  now)

Reset counters for conn statistics.

Definition at line 125 of file connstats.c.

Referenced by conn_stats_terminate().

◆ conn_stats_save()

time_t conn_stats_save ( time_t  now)

If 24 hours have passed since the beginning of the current conn stats period, write conn stats to $DATADIR/stats/conn-stats (possibly overwriting an existing file) and reset counters. Return when we would next want to write conn stats or 0 if we never want to write.

Definition at line 260 of file connstats.c.

◆ conn_stats_terminate()

void conn_stats_terminate ( void  )

Stop collecting connection stats in a way that we can re-start doing so in conn_stats_init().

Definition at line 136 of file connstats.c.


static HT_HEAD ( bidimap  ,

Map of OR connections together with the number of read and written bytes in the current BIDI_INTERVAL second interval.

Definition at line 88 of file connstats.c.

Variable Documentation

◆ bidi_next_interval

time_t bidi_next_interval = 0

Start of next BIDI_INTERVAL second interval.

Definition at line 49 of file connstats.c.

Referenced by conn_stats_note_or_conn_bytes().

◆ counts

conn_counts_t counts

A collection of connection counts, over all OR connections.

Definition at line 72 of file connstats.c.

Referenced by collect_period_statistics(), conn_stats_reset(), countbytes_close_fn(), geoip_get_client_history(), and tokenize_string().

◆ counts_ipv6

conn_counts_t counts_ipv6

A collection of connection counts, over IPv6 OR connections only.

Definition at line 74 of file connstats.c.

Referenced by collect_period_statistics(), and conn_stats_reset().

◆ start_of_conn_stats_interval

time_t start_of_conn_stats_interval

Start of the current connection stats interval or 0 if we're not collecting connection statistics.

Definition at line 26 of file connstats.c.

Referenced by conn_stats_init(), conn_stats_note_or_conn_bytes(), conn_stats_reset(), and conn_stats_save().