making dht counters additive (#783)

make dht counters additive to support both IPv4 and IPv6
This commit is contained in:
Alden Torres 2016-06-04 14:04:29 -04:00 committed by Arvid Norberg
parent fa8232cd13
commit db65eb320a
9 changed files with 71 additions and 39 deletions

View File

@ -195,7 +195,7 @@ public:
void status(std::vector<dht_routing_bucket>& table void status(std::vector<dht_routing_bucket>& table
, std::vector<dht_lookup>& requests); , std::vector<dht_lookup>& requests);
void update_stats_counters(counters& c) const; boost::tuple<int, int, int> get_stats_counters() const;
#ifndef TORRENT_NO_DEPRECATE #ifndef TORRENT_NO_DEPRECATE
void status(libtorrent::session_status& s); void status(libtorrent::session_status& s);

View File

@ -47,7 +47,7 @@ namespace libtorrent
#ifndef TORRENT_NO_DEPRECATE #ifndef TORRENT_NO_DEPRECATE
// holds counters and gauges for the uTP sockets // holds counters and gauges for the uTP sockets
// deprecated in 1.1 in favor of session_stats counters, which is a more // deprecated in 1.1 in favor of session_stats counters, which is a more
// flexible, extensible and perfromant mechanism for stats. // flexible, extensible and performant mechanism for stats.
struct TORRENT_EXPORT utp_status struct TORRENT_EXPORT utp_status
{ {
// gauges. These are snapshots of the number of // gauges. These are snapshots of the number of
@ -76,7 +76,7 @@ namespace libtorrent
// contains session wide state and counters // contains session wide state and counters
// deprecated in 1.1 in favor of session_stats counters, which is a more // deprecated in 1.1 in favor of session_stats counters, which is a more
// flexible, extensible and perfromant mechanism for stats. // flexible, extensible and performant mechanism for stats.
struct TORRENT_EXPORT session_status struct TORRENT_EXPORT session_status
{ {
// false as long as no incoming connections have been // false as long as no incoming connections have been
@ -164,7 +164,7 @@ namespace libtorrent
// tells the number of // tells the number of
// seconds until the next optimistic unchoke change and the start of the next // seconds until the next optimistic unchoke change and the start of the next
// unchoke interval. These numbers may be reset prematurely if a peer that is // unchoke interval. These numbers may be reset prematurely if a peer that is
// unchoked disconnects or becomes notinterested. // unchoked disconnects or becomes not interested.
int optimistic_unchoke_counter; int optimistic_unchoke_counter;
int unchoke_counter; int unchoke_counter;

View File

@ -51,7 +51,7 @@ namespace libtorrent
bool numeric(char c) { return c >= '0' && c <= '9'; } bool numeric(char c) { return c >= '0' && c <= '9'; }
// finds the end of an integer and verifies that it looks valid this does // finds the end of an integer and verifies that it looks valid this does
// not detect all overflows, just the ones that are an order of magnitued // not detect all overflows, just the ones that are an order of magnitude
// beyond. Exact overflow checking is done when the integer value is queried // beyond. Exact overflow checking is done when the integer value is queried
// from a bdecode_node. // from a bdecode_node.
char const* check_integer(char const* start, char const* end char const* check_integer(char const* start, char const* end
@ -105,7 +105,7 @@ namespace libtorrent
stack_frame(int const t): token(t), state(0) {} stack_frame(int const t): token(t), state(0) {}
// this is an index into m_tokens // this is an index into m_tokens
std::uint32_t token:31; std::uint32_t token:31;
// this is used for doctionaries to indicate whether we're // this is used for dictionaries to indicate whether we're
// reading a key or a vale. 0 means key 1 is value // reading a key or a vale. 0 means key 1 is value
std::uint32_t state:1; std::uint32_t state:1;
}; };
@ -129,7 +129,7 @@ namespace libtorrent
// fills in 'val' with what the string between start and the // fills in 'val' with what the string between start and the
// first occurance of the delimiter is interpreted as an int. // first occurrence of the delimiter is interpreted as an int.
// return the pointer to the delimiter, or 0 if there is a // return the pointer to the delimiter, or 0 if there is a
// parse error. val should be initialized to zero // parse error. val should be initialized to zero
char const* parse_int(char const* start, char const* end, char delimiter char const* parse_int(char const* start, char const* end, char delimiter
@ -803,7 +803,7 @@ namespace libtorrent
&& stack[sp-1].state == 1) && stack[sp-1].state == 1)
{ {
// this means we're parsing a dictionary and about to parse a // this means we're parsing a dictionary and about to parse a
// value associated with a key. Instad, we got a termination // value associated with a key. Instead, we got a termination
TORRENT_FAIL_BDECODE(bdecode_errors::expected_value); TORRENT_FAIL_BDECODE(bdecode_errors::expected_value);
} }

View File

@ -50,6 +50,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/time.hpp" #include "libtorrent/time.hpp"
#include "libtorrent/performance_counters.hpp" // for counters #include "libtorrent/performance_counters.hpp" // for counters
#include "libtorrent/aux_/time.hpp" #include "libtorrent/aux_/time.hpp"
#include "libtorrent/session_status.hpp"
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
#include "libtorrent/hex.hpp" // to_hex #include "libtorrent/hex.hpp" // to_hex
@ -89,6 +90,16 @@ namespace libtorrent { namespace dht
return node_id(nid->string().c_str()); return node_id(nid->string().c_str());
} }
void add_dht_counters(node const& dht, counters& c)
{
int nodes, replacements, allocated_observers;
boost::tie(nodes, replacements, allocated_observers) = dht.get_stats_counters();
c.inc_stats_counter(counters::dht_nodes, nodes);
c.inc_stats_counter(counters::dht_node_cache, replacements);
c.inc_stats_counter(counters::dht_allocated_observers, allocated_observers);
}
} // anonymous namespace } // anonymous namespace
// class that puts the networking and the kademlia node in a single // class that puts the networking and the kademlia node in a single
@ -215,19 +226,47 @@ namespace libtorrent { namespace dht
#ifndef TORRENT_NO_DEPRECATE #ifndef TORRENT_NO_DEPRECATE
void dht_tracker::dht_status(session_status& s) void dht_tracker::dht_status(session_status& s)
{ {
m_dht.status(s); //TODO: What to do with m_dht6? s.dht_torrents += int(m_storage.num_torrents());
s.dht_nodes = 0;
s.dht_node_cache = 0;
s.dht_global_nodes = 0;
s.dht_torrents = 0;
s.active_requests.clear();
s.dht_total_allocations = 0;
m_dht.status(s);
#if TORRENT_USE_IPV6
m_dht6.status(s);
#endif
} }
#endif #endif
void dht_tracker::dht_status(std::vector<dht_routing_bucket>& table void dht_tracker::dht_status(std::vector<dht_routing_bucket>& table
, std::vector<dht_lookup>& requests) , std::vector<dht_lookup>& requests)
{ {
m_dht.status(table, requests); //TODO: What to do with m_dht6? m_dht.status(table, requests);
#if TORRENT_USE_IPV6
m_dht6.status(table, requests);
#endif
} }
void dht_tracker::update_stats_counters(counters& c) const void dht_tracker::update_stats_counters(counters& c) const
{ {
m_dht.update_stats_counters(c); //TODO: What to do with m_dht6? const dht_storage_counters& dht_cnt = m_storage.counters();
c.set_value(counters::dht_torrents, dht_cnt.torrents);
c.set_value(counters::dht_peers, dht_cnt.peers);
c.set_value(counters::dht_immutable_data, dht_cnt.immutable_data);
c.set_value(counters::dht_mutable_data, dht_cnt.mutable_data);
c.set_value(counters::dht_nodes, 0);
c.set_value(counters::dht_node_cache, 0);
c.set_value(counters::dht_allocated_observers, 0);
add_dht_counters(m_dht, c);
#if TORRENT_USE_IPV6
add_dht_counters(m_dht6, c);
#endif
} }
void dht_tracker::connection_timeout(node& n, error_code const& e) void dht_tracker::connection_timeout(node& n, error_code const& e)

View File

@ -743,22 +743,11 @@ void node::status(std::vector<dht_routing_bucket>& table
} }
} }
// TODO: in the future, this function should update all the boost::tuple<int, int, int> node::get_stats_counters() const
// dht related counter. For now, it just update the storage
// related ones.
void node::update_stats_counters(counters& c) const
{ {
const dht_storage_counters& dht_cnt = m_storage.counters();
c.set_value(counters::dht_torrents, dht_cnt.torrents);
c.set_value(counters::dht_peers, dht_cnt.peers);
c.set_value(counters::dht_immutable_data, dht_cnt.immutable_data);
c.set_value(counters::dht_mutable_data, dht_cnt.mutable_data);
int nodes, replacements; int nodes, replacements;
boost::tie(nodes, replacements, boost::tuples::ignore) = size(); boost::tie(nodes, replacements, boost::tuples::ignore) = size();
c.set_value(counters::dht_nodes, nodes); return boost::make_tuple(nodes, replacements, m_rpc.num_allocated_observers());
c.set_value(counters::dht_node_cache, replacements);
c.set_value(counters::dht_allocated_observers, m_rpc.num_allocated_observers());
} }
#ifndef TORRENT_NO_DEPRECATE #ifndef TORRENT_NO_DEPRECATE
@ -768,9 +757,7 @@ void node::status(session_status& s)
std::lock_guard<std::mutex> l(m_mutex); std::lock_guard<std::mutex> l(m_mutex);
m_table.status(s); m_table.status(s);
s.dht_torrents = int(m_storage.num_torrents()); s.dht_total_allocations += m_rpc.num_allocated_observers();
s.active_requests.clear();
s.dht_total_allocations = m_rpc.num_allocated_observers();
for (std::set<traversal_algorithm*>::iterator i = m_running_requests.begin() for (std::set<traversal_algorithm*>::iterator i = m_running_requests.begin()
, end(m_running_requests.end()); i != end; ++i) , end(m_running_requests.end()); i != end; ++i)
{ {

View File

@ -171,9 +171,18 @@ void routing_table::status(std::vector<dht_routing_bucket>& s) const
// TODO: 2 use the non deprecated function instead of this one // TODO: 2 use the non deprecated function instead of this one
void routing_table::status(session_status& s) const void routing_table::status(session_status& s) const
{ {
int dht_nodes;
int dht_node_cache;
int ignore; int ignore;
boost::tie(s.dht_nodes, s.dht_node_cache, ignore) = size(); boost::tie(dht_nodes, dht_node_cache, ignore) = size();
s.dht_global_nodes = num_global_nodes(); s.dht_nodes += dht_nodes;
s.dht_node_cache += dht_node_cache;
// TODO: arvidn note
// when it's across IPv4 and IPv6, adding (dht_global_nodes) would
// make sense. in the future though, where we may have one DHT node
// per external interface (which may be multiple of the same address
// family), then it becomes a bit trickier
s.dht_global_nodes += num_global_nodes();
for (table_t::const_iterator i = m_buckets.begin() for (table_t::const_iterator i = m_buckets.begin()
, end(m_buckets.end()); i != end; ++i) , end(m_buckets.end()); i != end; ++i)

View File

@ -5606,10 +5606,7 @@ namespace aux {
m_dht.reset(); m_dht.reset();
} }
if (m_dht_storage) m_dht_storage.reset();
{
m_dht_storage.reset();
}
} }
void session_impl::set_dht_settings(dht_settings const& settings) void session_impl::set_dht_settings(dht_settings const& settings)

View File

@ -2706,9 +2706,9 @@ TORRENT_TEST(node_id_bucket_distribution)
TORRENT_TEST(node_id_min_distance_exp) TORRENT_TEST(node_id_min_distance_exp)
{ {
node_id n1 = to_hash("0000000000000000000000000000000000000002"); node_id const n1 = to_hash("0000000000000000000000000000000000000002");
node_id n2 = to_hash("0000000000000000000000000000000000000004"); node_id const n2 = to_hash("0000000000000000000000000000000000000004");
node_id n3 = to_hash("0000000000000000000000000000000000000008"); node_id const n3 = to_hash("0000000000000000000000000000000000000008");
std::vector<node_id> ids; std::vector<node_id> ids;

View File

@ -314,9 +314,9 @@ TORRENT_TEST(update_node_ids)
boost::scoped_ptr<dht_storage_interface> s(dht_default_storage_constructor(sett)); boost::scoped_ptr<dht_storage_interface> s(dht_default_storage_constructor(sett));
TEST_CHECK(s.get() != NULL); TEST_CHECK(s.get() != NULL);
node_id n1 = to_hash("0000000000000000000000000000000000000200"); node_id const n1 = to_hash("0000000000000000000000000000000000000200");
node_id n2 = to_hash("0000000000000000000000000000000000000400"); node_id const n2 = to_hash("0000000000000000000000000000000000000400");
node_id n3 = to_hash("0000000000000000000000000000000000000800"); node_id const n3 = to_hash("0000000000000000000000000000000000000800");
std::vector<node_id> node_ids; std::vector<node_id> node_ids;
node_ids.push_back(n1); node_ids.push_back(n1);