From 1a0c6f5e61913f434e4fa6453ea20770c499d307 Mon Sep 17 00:00:00 2001 From: Alden Torres Date: Sun, 11 Sep 2016 20:49:15 -0400 Subject: [PATCH] more dht log optimizations (#1079) --- include/libtorrent/kademlia/find_data.hpp | 2 +- include/libtorrent/kademlia/node_entry.hpp | 1 - .../kademlia/traversal_algorithm.hpp | 2 +- src/kademlia/find_data.cpp | 34 +++++++++++-------- src/kademlia/get_peers.cpp | 14 +++++--- src/kademlia/node_entry.cpp | 4 +-- 6 files changed, 32 insertions(+), 25 deletions(-) diff --git a/include/libtorrent/kademlia/find_data.hpp b/include/libtorrent/kademlia/find_data.hpp index bd8f0368d..35eeca907 100644 --- a/include/libtorrent/kademlia/find_data.hpp +++ b/include/libtorrent/kademlia/find_data.hpp @@ -57,7 +57,7 @@ struct find_data : traversal_algorithm { typedef std::function> const&)> nodes_callback; - find_data(node& node, node_id const& target + find_data(node& dht_node, node_id const& target , nodes_callback const& ncallback); void got_write_token(node_id const& n, std::string write_token); diff --git a/include/libtorrent/kademlia/node_entry.hpp b/include/libtorrent/kademlia/node_entry.hpp index 53fbabf15..4a87547f1 100644 --- a/include/libtorrent/kademlia/node_entry.hpp +++ b/include/libtorrent/kademlia/node_entry.hpp @@ -82,4 +82,3 @@ struct TORRENT_EXTRA_EXPORT node_entry } } // namespace libtorrent::dht #endif - diff --git a/include/libtorrent/kademlia/traversal_algorithm.hpp b/include/libtorrent/kademlia/traversal_algorithm.hpp index 22c0763c6..c28e52907 100644 --- a/include/libtorrent/kademlia/traversal_algorithm.hpp +++ b/include/libtorrent/kademlia/traversal_algorithm.hpp @@ -73,7 +73,7 @@ struct TORRENT_EXTRA_EXPORT traversal_algorithm : boost::noncopyable void resort_results(); void add_entry(node_id const& id, udp::endpoint const& addr, unsigned char flags); - traversal_algorithm(node& node, node_id const& target); + traversal_algorithm(node& dht_node, node_id const& target); int invoke_count() const { return m_invoke_count; } int branch_factor() const { return m_branch_factor; } diff --git a/src/kademlia/find_data.cpp b/src/kademlia/find_data.cpp index b29c8e49f..bacd46dc6 100644 --- a/src/kademlia/find_data.cpp +++ b/src/kademlia/find_data.cpp @@ -97,10 +97,9 @@ void find_data::start() std::vector nodes; m_node.m_table.find_node(m_target, nodes, routing_table::include_failed); - for (std::vector::iterator i = nodes.begin() - , end(nodes.end()); i != end; ++i) + for (auto const& n : nodes) { - add_entry(i->id, i->ep(), observer::flag_initial); + add_entry(n.id, n.ep(), observer::flag_initial); } } @@ -110,10 +109,12 @@ void find_data::start() void find_data::got_write_token(node_id const& n, std::string write_token) { #ifndef TORRENT_DISABLE_LOGGING - get_node().observer()->log(dht_logger::traversal - , "[%p] adding write token '%s' under id '%s'" - , static_cast(this), aux::to_hex(write_token).c_str() - , aux::to_hex(n).c_str()); + auto logger = get_node().observer(); + if (logger != nullptr && logger->should_log(dht_logger::traversal)) + logger->log(dht_logger::traversal + , "[%p] adding write token '%s' under id '%s'" + , static_cast(this), aux::to_hex(write_token).c_str() + , aux::to_hex(n).c_str()); #endif m_write_tokens[n] = std::move(write_token); } @@ -135,8 +136,10 @@ void find_data::done() m_done = true; #ifndef TORRENT_DISABLE_LOGGING - get_node().observer()->log(dht_logger::traversal, "[%p] %s DONE" - , static_cast(this), name()); + auto logger = get_node().observer(); + if (logger != nullptr) + logger->log(dht_logger::traversal, "[%p] %s DONE" + , static_cast(this), name()); #endif std::vector > results; @@ -148,8 +151,9 @@ void find_data::done() if ((o->flags & observer::flag_alive) == 0) { #ifndef TORRENT_DISABLE_LOGGING - get_node().observer()->log(dht_logger::traversal, "[%p] not alive: %s" - , static_cast(this), print_endpoint(o->target_ep()).c_str()); + if (logger != nullptr && logger->should_log(dht_logger::traversal)) + logger->log(dht_logger::traversal, "[%p] not alive: %s" + , static_cast(this), print_endpoint(o->target_ep()).c_str()); #endif continue; } @@ -157,14 +161,16 @@ void find_data::done() if (j == m_write_tokens.end()) { #ifndef TORRENT_DISABLE_LOGGING - get_node().observer()->log(dht_logger::traversal, "[%p] no write token: %s" - , static_cast(this), print_endpoint(o->target_ep()).c_str()); + if (logger != nullptr && logger->should_log(dht_logger::traversal)) + logger->log(dht_logger::traversal, "[%p] no write token: %s" + , static_cast(this), print_endpoint(o->target_ep()).c_str()); #endif continue; } results.push_back(std::make_pair(node_entry(o->id(), o->target_ep()), j->second)); #ifndef TORRENT_DISABLE_LOGGING - get_node().observer()->log(dht_logger::traversal, "[%p] %s" + if (logger != nullptr && logger->should_log(dht_logger::traversal)) + logger->log(dht_logger::traversal, "[%p] %s" , static_cast(this), print_endpoint(o->target_ep()).c_str()); #endif --num_results; diff --git a/src/kademlia/get_peers.cpp b/src/kademlia/get_peers.cpp index 9ec81f29b..44e98bd2a 100644 --- a/src/kademlia/get_peers.cpp +++ b/src/kademlia/get_peers.cpp @@ -72,10 +72,12 @@ void get_peers_observer::reply(msg const& m) char const* end = peers + n.list_at(0).string_length(); #ifndef TORRENT_DISABLE_LOGGING + auto logger = get_observer(); bdecode_node const id = r.dict_find_string("id"); - if (id && id.string_length() == 20) + if (logger != nullptr && logger->should_log(dht_logger::traversal) + && id && id.string_length() == 20) { - get_observer()->log(dht_logger::traversal, "[%p] PEERS " + logger->log(dht_logger::traversal, "[%p] PEERS " "invoke-count: %d branch-factor: %d addr: %s id: %s distance: %d p: %d" , static_cast(algorithm()) , algorithm()->invoke_count() @@ -94,10 +96,12 @@ void get_peers_observer::reply(msg const& m) // assume it's uTorrent/libtorrent format read_endpoint_list(n, peer_list); #ifndef TORRENT_DISABLE_LOGGING + auto logger = get_observer(); bdecode_node const id = r.dict_find_string("id"); - if (id && id.string_length() == 20) + if (logger != nullptr && logger->should_log(dht_logger::traversal) + && id && id.string_length() == 20) { - get_observer()->log(dht_logger::traversal, "[%p] PEERS " + logger->log(dht_logger::traversal, "[%p] PEERS " "invoke-count: %d branch-factor: %d addr: %s id: %s distance: %d p: %d" , static_cast(algorithm()) , algorithm()->invoke_count() @@ -105,7 +109,7 @@ void get_peers_observer::reply(msg const& m) , print_endpoint(m.addr).c_str() , aux::to_hex({id.string_ptr(), size_t(id.string_length())}).c_str() , distance_exp(algorithm()->target(), node_id(id.string_ptr())) - , int(n.list_size())); + , n.list_size()); } #endif } diff --git a/src/kademlia/node_entry.cpp b/src/kademlia/node_entry.cpp index 424a9c4ee..b4235c7e1 100644 --- a/src/kademlia/node_entry.cpp +++ b/src/kademlia/node_entry.cpp @@ -79,9 +79,7 @@ namespace libtorrent { namespace dht TORRENT_ASSERT(new_rtt >= 0); if (new_rtt == 0xffff) return; if (rtt == 0xffff) rtt = new_rtt; - else rtt = int(rtt) * 2 / 3 + int(new_rtt) / 3; + else rtt = int(rtt) * 2 / 3 + new_rtt / 3; } }} - -