dht log optimization, consts and refactor (#1073)

dht log optimization and refactor
This commit is contained in:
Alden Torres 2016-09-08 19:13:47 -04:00 committed by Arvid Norberg
parent 01e9810f7a
commit 99da5c8017
12 changed files with 83 additions and 76 deletions

View File

@ -619,7 +619,8 @@ namespace libtorrent
, sha1_hash const& sent_target, udp::endpoint const& ep) override;
#ifndef TORRENT_DISABLE_LOGGING
virtual void log(libtorrent::dht::dht_logger::module_t m, char const* fmt, ...)
virtual bool should_log(module_t m) const override;
virtual void log(module_t m, char const* fmt, ...)
override TORRENT_FORMAT(3,4);
virtual void log_packet(message_direction_t dir, char const* pkt, int len
, udp::endpoint node) override;

View File

@ -57,6 +57,7 @@ namespace libtorrent { namespace dht
outgoing_message
};
virtual bool should_log(module_t m) const = 0;
virtual void log(module_t m, char const* fmt, ...) TORRENT_FORMAT(3,4) = 0;
virtual void log_packet(message_direction_t dir, char const* pkt, int len
, udp::endpoint node) = 0;

View File

@ -57,7 +57,7 @@ class node;
struct TORRENT_EXTRA_EXPORT traversal_algorithm : boost::noncopyable
, std::enable_shared_from_this<traversal_algorithm>
{
void traverse(node_id const& id, udp::endpoint addr);
void traverse(node_id const& id, udp::endpoint const& addr);
void finished(observer_ptr o);
enum flags_t { prevent_request = 1, short_timeout = 2 };
@ -71,7 +71,7 @@ struct TORRENT_EXTRA_EXPORT traversal_algorithm : boost::noncopyable
node_id const& target() const { return m_target; }
void resort_results();
void add_entry(node_id const& id, udp::endpoint addr, unsigned char flags);
void add_entry(node_id const& id, udp::endpoint const& addr, unsigned char flags);
traversal_algorithm(node & node, node_id target);
int invoke_count() const { return m_invoke_count; }
@ -128,4 +128,3 @@ struct traversal_observer : observer
} } // namespace libtorrent::dht
#endif // TRAVERSAL_ALGORITHM_050324_HPP

View File

@ -385,7 +385,7 @@ namespace libtorrent
enum seed_choking_algorithm_t
{
// round-robins the peers that are unchoked when seeding. This
// distributes the upload bandwidht uniformly and fairly. It minimizes
// distributes the upload bandwidth uniformly and fairly. It minimizes
// the ability for a peer to download everything without
// redistributing it.
round_robin,
@ -811,7 +811,7 @@ namespace libtorrent
// if this is true, disk read operations are sorted by their physical
// offset on disk before issued to the operating system. This is useful
// if async I/O is not supported. It defaults to true if async I/O is not
// supported and fals otherwise. disk I/O operations are likely to be
// supported and fails otherwise. disk I/O operations are likely to be
// reordered regardless of this setting when async I/O is supported by
// the OS.
bool allow_reordered_disk_operations;
@ -1173,7 +1173,7 @@ namespace libtorrent
// if true, the ``&ip=`` argument in tracker requests (unless otherwise
// specified) will be set to the intermediate IP address, if the user is
// double NATed. If ther user is not double NATed, this option has no
// double NATed. If the user is not double NATed, this option has no
// affect.
bool announce_double_nat;
@ -1348,7 +1348,7 @@ namespace libtorrent
// active by the queuing mechanism. A torrent whose download rate is less
// than ``inactive_down_rate`` and whose upload rate is less than
// ``inactive_up_rate`` for ``auto_manage_startup`` seconds, is
// considered inactive, and another queued torrent may be startert.
// considered inactive, and another queued torrent may be started.
// This logic is disabled if ``dont_count_slow_torrents`` is false.
int inactive_down_rate;
int inactive_up_rate;

View File

@ -69,6 +69,7 @@ struct obs : dht::dht_observer
, sha1_hash const& /* sent_target */, udp::endpoint const& /* ep */) override {}
void announce(sha1_hash const& /* ih */
, address const& /* addr */, int /* port */) override {}
bool should_log(module_t) const override { return true; }
void log(dht_logger::module_t l, char const* fmt, ...) override
{
va_list v;

View File

@ -214,7 +214,7 @@ void node::bootstrap(std::vector<udp::endpoint> const& nodes
#ifndef TORRENT_DISABLE_LOGGING
++count;
#endif
r->add_entry(node_id(nullptr), n, observer::flag_initial);
r->add_entry(node_id(), n, observer::flag_initial);
}
// make us start as far away from our node ID as possible

View File

@ -46,8 +46,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include <libtorrent/hex.hpp> // to_hex
#endif
#include <functional>
using namespace std::placeholders;
namespace libtorrent { namespace dht
@ -97,12 +95,13 @@ traversal_algorithm::traversal_algorithm(
, m_timeouts(0)
{
#ifndef TORRENT_DISABLE_LOGGING
if (get_node().observer())
dht_observer* logger = get_node().observer();
if (logger != nullptr && logger->should_log(dht_logger::traversal))
{
char hex_target[41];
aux::to_hex(target, hex_target);
get_node().observer()->log(dht_logger::traversal, "[%p] NEW target: %s k: %d"
, static_cast<void*>(this), hex_target, int(m_node.m_table.bucket_size()));
logger->log(dht_logger::traversal, "[%p] NEW target: %s k: %d"
, static_cast<void*>(this), hex_target, m_node.m_table.bucket_size());
}
#endif
}
@ -114,14 +113,15 @@ void traversal_algorithm::resort_results()
{ return compare_ref(lhs->id(), rhs->id(), m_target); });
}
void traversal_algorithm::add_entry(node_id const& id, udp::endpoint addr, unsigned char flags)
void traversal_algorithm::add_entry(node_id const& id
, udp::endpoint const& addr, unsigned char const flags)
{
TORRENT_ASSERT(m_node.m_rpc.allocation_size() >= sizeof(find_data_observer));
auto o = new_observer(addr, id);
if (!o)
{
#ifndef TORRENT_DISABLE_LOGGING
if (get_node().observer())
if (get_node().observer() != nullptr)
{
get_node().observer()->log(dht_logger::traversal, "[%p] failed to allocate memory or observer. aborting!"
, static_cast<void*>(this));
@ -142,8 +142,7 @@ void traversal_algorithm::add_entry(node_id const& id, udp::endpoint addr, unsig
, [this](observer_ptr const& lhs, observer_ptr const& rhs)
{ return compare_ref(lhs->id(), rhs->id(), m_target); }));
std::vector<observer_ptr>::iterator iter = std::lower_bound(
m_results.begin(), m_results.end(), o
auto iter = std::lower_bound(m_results.begin(), m_results.end(), o
, [this](observer_ptr const& lhs, observer_ptr const& rhs)
{ return compare_ref(lhs->id(), rhs->id(), m_target); });
@ -177,11 +176,12 @@ void traversal_algorithm::add_entry(node_id const& id, udp::endpoint addr, unsig
// close to this one. We know that it's not the same, because
// it claims a different node-ID. Ignore this to avoid attacks
#ifndef TORRENT_DISABLE_LOGGING
if (get_node().observer())
dht_observer* logger = get_node().observer();
if (logger != nullptr && logger->should_log(dht_logger::traversal))
{
char hex_id[41];
aux::to_hex(o->id(), hex_id);
get_node().observer()->log(dht_logger::traversal
logger->log(dht_logger::traversal
, "[%p] traversal DUPLICATE node. id: %s addr: %s type: %s"
, static_cast<void*>(this), hex_id, print_address(o->target_addr()).c_str(), name());
}
@ -196,11 +196,12 @@ void traversal_algorithm::add_entry(node_id const& id, udp::endpoint addr, unsig
, [&id](observer_ptr const& ob) { return ob->id() == id; }));
#ifndef TORRENT_DISABLE_LOGGING
if (get_node().observer())
dht_observer* logger = get_node().observer();
if (logger != nullptr && logger->should_log(dht_logger::traversal))
{
char hex_id[41];
aux::to_hex(id, hex_id);
get_node().observer()->log(dht_logger::traversal
logger->log(dht_logger::traversal
, "[%p] ADD id: %s addr: %s distance: %d invoke-count: %d type: %s"
, static_cast<void*>(this), hex_id, print_endpoint(addr).c_str()
, distance_exp(m_target, id), m_invoke_count, name());
@ -250,10 +251,10 @@ char const* traversal_algorithm::name() const
return "traversal_algorithm";
}
void traversal_algorithm::traverse(node_id const& id, udp::endpoint addr)
void traversal_algorithm::traverse(node_id const& id, udp::endpoint const& addr)
{
#ifndef TORRENT_DISABLE_LOGGING
if (id.is_all_zeros() && get_node().observer())
if (id.is_all_zeros() && get_node().observer() != nullptr)
{
get_node().observer()->log(dht_logger::traversal
, "[%p] WARNING node returned a list which included a node with id 0"
@ -317,11 +318,12 @@ void traversal_algorithm::failed(observer_ptr o, int flags)
++m_branch_factor;
o->flags |= observer::flag_short_timeout;
#ifndef TORRENT_DISABLE_LOGGING
if (get_node().observer())
dht_observer* logger = get_node().observer();
if (logger != nullptr && logger->should_log(dht_logger::traversal))
{
char hex_id[41];
aux::to_hex(o->id(), hex_id);
get_node().observer()->log(dht_logger::traversal
logger->log(dht_logger::traversal
, "[%p] 1ST_TIMEOUT id: %s distance: %d addr: %s branch-factor: %d "
"invoke-count: %d type: %s"
, static_cast<void*>(this), hex_id, distance_exp(m_target, o->id())
@ -339,11 +341,12 @@ void traversal_algorithm::failed(observer_ptr o, int flags)
--m_branch_factor;
#ifndef TORRENT_DISABLE_LOGGING
if (get_node().observer())
dht_observer* logger = get_node().observer();
if (logger != nullptr && logger->should_log(dht_logger::traversal))
{
char hex_id[41];
aux::to_hex(o->id(), hex_id);
get_node().observer()->log(dht_logger::traversal
logger->log(dht_logger::traversal
, "[%p] TIMEOUT id: %s distance: %d addr: %s branch-factor: %d "
"invoke-count: %d type: %s"
, static_cast<void*>(this), hex_id, distance_exp(m_target, o->id())
@ -384,12 +387,14 @@ void traversal_algorithm::done()
}
#ifndef TORRENT_DISABLE_LOGGING
if (results_target > 0 && (o->flags & observer::flag_alive) && get_node().observer())
dht_observer* logger = get_node().observer();
if (results_target > 0 && (o->flags & observer::flag_alive)
&& logger != nullptr && logger->should_log(dht_logger::traversal))
{
TORRENT_ASSERT(o->flags & observer::flag_queried);
char hex_id[41];
aux::to_hex(o->id(), hex_id);
get_node().observer()->log(dht_logger::traversal
logger->log(dht_logger::traversal
, "[%p] id: %s distance: %d addr: %s"
, static_cast<void*>(this), hex_id, closest_target
, print_endpoint(o->target_ep()).c_str());
@ -402,7 +407,7 @@ void traversal_algorithm::done()
}
#ifndef TORRENT_DISABLE_LOGGING
if (get_node().observer())
if (get_node().observer() != nullptr)
{
get_node().observer()->log(dht_logger::traversal
, "[%p] COMPLETED distance: %d type: %s"
@ -465,11 +470,12 @@ bool traversal_algorithm::add_requests()
}
#ifndef TORRENT_DISABLE_LOGGING
if (get_node().observer())
dht_observer* logger = get_node().observer();
if (logger != nullptr && logger->should_log(dht_logger::traversal))
{
char hex_id[41];
aux::to_hex(o->id(), hex_id);
get_node().observer()->log(dht_logger::traversal
logger->log(dht_logger::traversal
, "[%p] INVOKE nodes-left: %d top-invoke-count: %d "
"invoke-count: %d branch-factor: %d "
"distance: %d id: %s addr: %s type: %s"
@ -503,9 +509,10 @@ bool traversal_algorithm::add_requests()
void traversal_algorithm::add_router_entries()
{
#ifndef TORRENT_DISABLE_LOGGING
if (get_node().observer())
dht_observer* logger = get_node().observer();
if (logger != nullptr && logger->should_log(dht_logger::traversal))
{
get_node().observer()->log(dht_logger::traversal
logger->log(dht_logger::traversal
, "[%p] using router nodes to initiate traversal algorithm %d routers"
, static_cast<void*>(this), int(std::distance(m_node.m_table.begin(), m_node.m_table.end())));
}
@ -537,10 +544,9 @@ void traversal_algorithm::status(dht_lookup& l)
int last_sent = INT_MAX;
time_point now = aux::time_now();
for (std::vector<observer_ptr>::iterator i = m_results.begin()
, end(m_results.end()); i != end; ++i)
for (auto const& r : m_results)
{
observer& o = **i;
observer const& o = *r;
if (o.flags & observer::flag_queried)
{
last_sent = (std::min)(last_sent, int(total_seconds(now - o.sent())));
@ -569,12 +575,13 @@ void traversal_observer::reply(msg const& m)
}
#ifndef TORRENT_DISABLE_LOGGING
if (get_observer())
dht_observer* logger = get_observer();
if (logger != nullptr && logger->should_log(dht_logger::traversal))
{
bdecode_node nid = r.dict_find_string("id");
char hex_id[41];
aux::to_hex({nid.string_ptr(), 20}, hex_id);
get_observer()->log(dht_logger::traversal
logger->log(dht_logger::traversal
, "[%p] RESPONSE id: %s invoke-count: %d addr: %s type: %s"
, static_cast<void*>(algorithm()), hex_id, algorithm()->invoke_count()
, print_endpoint(target_ep()).c_str(), algorithm()->name());

View File

@ -91,7 +91,7 @@ namespace libtorrent
return r;
}
void session_handle::save_state(entry& e, std::uint32_t flags) const
void session_handle::save_state(entry& e, std::uint32_t const flags) const
{
entry* ep = &e;
sync_call(&session_impl::save_state, ep, flags);
@ -107,10 +107,9 @@ namespace libtorrent
void session_handle::get_torrent_status(std::vector<torrent_status>* ret
, std::function<bool(torrent_status const&)> const& pred
, std::uint32_t flags) const
, std::uint32_t const flags) const
{
auto predr = std::ref(pred);
sync_call(&session_impl::get_torrent_status, ret, predr, flags);
sync_call(&session_impl::get_torrent_status, ret, pred, flags);
}
void session_handle::refresh_torrent_status(std::vector<torrent_status>* ret

View File

@ -592,7 +592,7 @@ namespace aux {
}
void session_impl::load_state(bdecode_node const* e
, std::uint32_t const flags = 0xffffffff)
, std::uint32_t const flags)
{
TORRENT_ASSERT(is_single_thread());
@ -601,7 +601,6 @@ namespace aux {
#ifndef TORRENT_DISABLE_DHT
bool need_update_dht = false;
// load from the old settings names
if (flags & session::save_dht_settings)
{
settings = e->dict_find_dict("dht");
@ -939,9 +938,8 @@ namespace aux {
m_port_filter.add_rule(0, 1024, port_filter::blocked);
// Close connections whose endpoint is filtered
// by the new ip-filter
for (torrent_map::iterator i = m_torrents.begin()
, end(m_torrents.end()); i != end; ++i)
i->second->port_filter_updated();
for (auto const& t : m_torrents)
t.second->port_filter_updated();
}
void session_impl::set_ip_filter(std::shared_ptr<ip_filter> const& f)
@ -1771,11 +1769,10 @@ namespace aux {
#ifndef TORRENT_DISABLE_LOGGING
session_log("closing all listen sockets");
#endif
for (std::list<listen_socket_t>::iterator i = m_listen_sockets.begin()
, end(m_listen_sockets.end()); i != end; ++i)
for (auto const& s : m_listen_sockets)
{
if (i->sock) i->sock->close(ec);
if (i->udp_sock) i->udp_sock->close();
if (s.sock) s.sock->close(ec);
if (s.udp_sock) s.udp_sock->close();
}
m_listen_sockets.clear();
@ -1923,11 +1920,10 @@ namespace aux {
ec.clear();
// initiate accepting on the listen sockets
for (std::list<listen_socket_t>::iterator i = m_listen_sockets.begin()
, end(m_listen_sockets.end()); i != end; ++i)
for (auto& s : m_listen_sockets)
{
if (i->sock) async_accept(i->sock, i->ssl);
remap_ports(remap_natpmp_and_upnp, *i);
if (s.sock) async_accept(s.sock, s.ssl);
remap_ports(remap_natpmp_and_upnp, s);
}
open_new_incoming_socks_connection();
@ -6570,8 +6566,13 @@ namespace aux {
}
#ifndef TORRENT_DISABLE_LOGGING
bool session_impl::should_log(module_t) const
{
return m_alerts.should_post<dht_log_alert>();
}
TORRENT_FORMAT(3,4)
void session_impl::log(libtorrent::dht::dht_logger::module_t m, char const* fmt, ...)
void session_impl::log(module_t m, char const* fmt, ...)
{
if (!m_alerts.should_post<dht_log_alert>()) return;

View File

@ -577,60 +577,57 @@ namespace libtorrent
typedef void (aux::session_impl::*fun_t)();
std::vector<fun_t> callbacks;
for (std::vector<std::pair<std::uint16_t, std::string> >::const_iterator i = pack->m_strings.begin()
, end(pack->m_strings.end()); i != end; ++i)
for (auto const& p : pack->m_strings)
{
// disregard setting indices that are not string types
if ((i->first & settings_pack::type_mask) != settings_pack::string_type_base)
if ((p.first & settings_pack::type_mask) != settings_pack::string_type_base)
continue;
// ignore settings that are out of bounds
int const index = i->first & settings_pack::index_mask;
int const index = p.first & settings_pack::index_mask;
TORRENT_ASSERT_PRECOND(index >= 0 && index < settings_pack::num_string_settings);
if (index < 0 || index >= settings_pack::num_string_settings)
continue;
sett.set_str(i->first, i->second);
sett.set_str(p.first, p.second);
str_setting_entry_t const& sa = str_settings[index];
if (sa.fun && ses
&& std::find(callbacks.begin(), callbacks.end(), sa.fun) == callbacks.end())
callbacks.push_back(sa.fun);
}
for (std::vector<std::pair<std::uint16_t, int> >::const_iterator i = pack->m_ints.begin()
, end(pack->m_ints.end()); i != end; ++i)
for (auto const& p : pack->m_ints)
{
// disregard setting indices that are not int types
if ((i->first & settings_pack::type_mask) != settings_pack::int_type_base)
if ((p.first & settings_pack::type_mask) != settings_pack::int_type_base)
continue;
// ignore settings that are out of bounds
int const index = i->first & settings_pack::index_mask;
int const index = p.first & settings_pack::index_mask;
TORRENT_ASSERT_PRECOND(index >= 0 && index < settings_pack::num_int_settings);
if (index < 0 || index >= settings_pack::num_int_settings)
continue;
sett.set_int(i->first, i->second);
sett.set_int(p.first, p.second);
int_setting_entry_t const& sa = int_settings[index];
if (sa.fun && ses
&& std::find(callbacks.begin(), callbacks.end(), sa.fun) == callbacks.end())
callbacks.push_back(sa.fun);
}
for (std::vector<std::pair<std::uint16_t, bool> >::const_iterator i = pack->m_bools.begin()
, end(pack->m_bools.end()); i != end; ++i)
for (auto const& p : pack->m_bools)
{
// disregard setting indices that are not bool types
if ((i->first & settings_pack::type_mask) != settings_pack::bool_type_base)
if ((p.first & settings_pack::type_mask) != settings_pack::bool_type_base)
continue;
// ignore settings that are out of bounds
int const index = i->first & settings_pack::index_mask;
int const index = p.first & settings_pack::index_mask;
TORRENT_ASSERT_PRECOND(index >= 0 && index < settings_pack::num_bool_settings);
if (index < 0 || index >= settings_pack::num_bool_settings)
continue;
sett.set_bool(i->first, i->second);
sett.set_bool(p.first, p.second);
bool_setting_entry_t const& sa = bool_settings[index];
if (sa.fun && ses
&& std::find(callbacks.begin(), callbacks.end(), sa.fun) == callbacks.end())
@ -639,10 +636,8 @@ namespace libtorrent
// call the callbacks once all the settings have been applied, and
// only once per callback
for (std::vector<fun_t>::iterator i = callbacks.begin(), end(callbacks.end());
i != end; ++i)
for (auto const& f : callbacks)
{
fun_t const& f = *i;
(ses->*f)();
}
}

View File

@ -486,6 +486,7 @@ struct obs : dht::dht_observer
, sha1_hash const& sent_target, udp::endpoint const& ep) override {}
void announce(sha1_hash const& ih, address const& addr, int port) override {}
#ifndef TORRENT_DISABLE_LOGGING
bool should_log(module_t) const override { return true; }
void log(dht_logger::module_t l, char const* fmt, ...) override
{
va_list v;

View File

@ -45,6 +45,8 @@ using namespace libtorrent;
#ifndef TORRENT_DISABLE_LOGGING
struct log_t : libtorrent::dht::dht_logger
{
bool should_log(module_t) const override { return true; }
void log(dht_logger::module_t m, char const* fmt, ...)
override TORRENT_FORMAT(3, 4)
{