diff options
author | David Goulet <dgoulet@torproject.org> | 2022-10-11 13:17:35 -0400 |
---|---|---|
committer | David Goulet <dgoulet@torproject.org> | 2022-10-12 09:25:19 -0400 |
commit | 5603baf2570a427c2ea56145ed5607aae504d6b7 (patch) | |
tree | 80010152a5373ce3fa2a69552abfae5ceef6f8e6 /src/feature/stats/rephist.c | |
parent | d09414858e2f5c5efa2ece4b3a32a6b47374dfdd (diff) | |
download | tor-5603baf2570a427c2ea56145ed5607aae504d6b7.tar.gz tor-5603baf2570a427c2ea56145ed5607aae504d6b7.zip |
conn: Keep stats of opened and closed connections
Related to #40194
Signed-off-by: David Goulet <dgoulet@torproject.org>
Diffstat (limited to 'src/feature/stats/rephist.c')
-rw-r--r-- | src/feature/stats/rephist.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/feature/stats/rephist.c b/src/feature/stats/rephist.c index 52bd94aba9..b272f27909 100644 --- a/src/feature/stats/rephist.c +++ b/src/feature/stats/rephist.c @@ -69,6 +69,7 @@ #define REPHIST_PRIVATE #include "core/or/or.h" #include "app/config/config.h" +#include "core/mainloop/connection.h" #include "core/or/circuitlist.h" #include "core/or/connection_or.h" #include "feature/dirauth/authmode.h" @@ -1638,6 +1639,62 @@ rep_hist_note_exit_stream_opened(uint16_t port) log_debug(LD_HIST, "Opened exit stream to port %d", port); } +/******* Connections statistics *******/ + +#define CONN_DIRECTION_INITIATED 0 +#define CONN_DIRECTION_RECEIVED 1 + +#define CONN_DIRECTION(from_listener) \ + (from_listener) ? CONN_DIRECTION_RECEIVED : CONN_DIRECTION_INITIATED + +/** Number of connections created as in seen per direction per type. */ +static uint64_t conn_num_created[2][CONN_TYPE_MAX_]; +/** Number of connections opened per direction per type. */ +static uint64_t conn_num_opened[2][CONN_TYPE_MAX_]; + +/** Note that a connection has opened of the given type. */ +void +rep_hist_note_conn_opened(bool from_listener, unsigned int type) +{ + tor_assert(type <= CONN_TYPE_MAX_); + + unsigned int dir = CONN_DIRECTION(from_listener); + + conn_num_created[dir][type]++; + conn_num_opened[dir][type]++; +} + +/** Note that a connection has closed of the given type. */ +void +rep_hist_note_conn_closed(bool from_listener, unsigned int type) +{ + tor_assert(type <= CONN_TYPE_MAX_); + + unsigned int dir = CONN_DIRECTION(from_listener); + + if (conn_num_opened[dir][type] > 0) { + conn_num_opened[dir][type]--; + } +} + +/** Return number of created connections of the given type. */ +uint64_t +rep_hist_get_conn_created(bool from_listener, unsigned int type) +{ + tor_assert(type <= CONN_TYPE_MAX_); + unsigned int dir = CONN_DIRECTION(from_listener); + return conn_num_created[dir][type]; +} + +/** Return number of opened connections of the given type. */ +uint64_t +rep_hist_get_conn_opened(bool from_listener, unsigned int type) +{ + tor_assert(type <= CONN_TYPE_MAX_); + unsigned int dir = CONN_DIRECTION(from_listener); + return conn_num_opened[dir][type]; +} + /*** cell statistics ***/ /** Start of the current buffer stats interval or 0 if we're not |