Create dht_storage_counters to avoid internal counter in future public API.

This commit is contained in:
Alden Torres 2015-09-17 11:11:46 -04:00
parent 2cf0349021
commit 25ed70b977
8 changed files with 60 additions and 20 deletions

View File

@ -50,6 +50,15 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent {
namespace dht
{
// This structure hold the relevant counters for the storage
struct TORRENT_EXPORT dht_storage_counters
{
boost::int32_t torrents;
boost::int32_t peers;
boost::int32_t immutable_data;
boost::int32_t mutable_data;
};
// The DHT storage interface is a pure virtual class that can
// be implemented to customize how the data for the DHT is stored.
//
@ -186,12 +195,13 @@ namespace dht
//
virtual void tick() = 0;
virtual dht_storage_counters counters() const = 0;
virtual ~dht_storage_interface() {}
};
TORRENT_EXPORT dht_storage_interface* dht_default_storage_constructor(sha1_hash const& id
, dht_settings const& settings
, counters& counters);
, dht_settings const& settings);
} } // namespace libtorrent::dht

View File

@ -114,6 +114,7 @@ namespace libtorrent { namespace dht
#endif
void dht_status(std::vector<dht_routing_bucket>& table
, std::vector<dht_lookup>& requests);
void update_stats_counters(counters& c) const;
// translate bittorrent kademlia message into the generic kademlia message
// used by the library

View File

@ -214,6 +214,8 @@ public:
void status(std::vector<dht_routing_bucket>& table
, std::vector<dht_lookup>& requests);
void update_stats_counters(counters& c) const;
#ifndef TORRENT_NO_DEPRECATE
void status(libtorrent::session_status& s);
#endif

View File

@ -57,6 +57,8 @@ POSSIBILITY OF SUCH DAMAGE.
#include <libtorrent/kademlia/item.hpp>
#include <libtorrent/kademlia/node_id.hpp>
#include <string.h> // for memset
namespace libtorrent {
namespace dht {
namespace
@ -198,12 +200,11 @@ namespace
public:
dht_default_storage(sha1_hash const& id, dht_settings const& settings
, counters& cnt)
dht_default_storage(sha1_hash const& id, dht_settings const& settings)
: m_id(id)
, m_settings(settings)
, m_counters(cnt)
{
memset(&m_counters, 0, sizeof(m_counters));
}
~dht_default_storage() {}
@ -295,9 +296,9 @@ namespace
candidate = i;
}
m_map.erase(candidate);
m_counters.inc_stats_counter(counters::dht_torrents, -1);
m_counters.torrents -= 1;
}
m_counters.inc_stats_counter(counters::dht_torrents);
m_counters.torrents += 1;
v = &m_map[info_hash];
}
else
@ -353,7 +354,7 @@ namespace
TORRENT_ASSERT(j != m_immutable_table.end());
free(j->second.value);
m_immutable_table.erase(j);
m_counters.inc_stats_counter(counters::dht_immutable_data, -1);
m_counters.immutable_data -= 1;
}
dht_immutable_item to_add;
to_add.value = static_cast<char*>(malloc(size));
@ -362,7 +363,7 @@ namespace
boost::tie(i, boost::tuples::ignore) = m_immutable_table.insert(
std::make_pair(target, to_add));
m_counters.inc_stats_counter(counters::dht_immutable_data);
m_counters.immutable_data += 1;
}
// fprintf(stderr, "added immutable item (%d)\n", int(m_immutable_table.size()));
@ -423,7 +424,7 @@ namespace
free(j->second.value);
free(j->second.salt);
m_mutable_table.erase(j);
m_counters.inc_stats_counter(counters::dht_mutable_data, -1);
m_counters.mutable_data -= 1;
}
dht_mutable_item to_add;
to_add.value = static_cast<char*>(malloc(size));
@ -443,7 +444,7 @@ namespace
boost::tie(i, boost::tuples::ignore) = m_mutable_table.insert(
std::make_pair(target, to_add));
m_counters.inc_stats_counter(counters::dht_mutable_data);
m_counters.mutable_data += 1;
// fprintf(stderr, "added mutable item (%d)\n", int(m_mutable_table.size()));
}
@ -483,7 +484,7 @@ namespace
}
free(i->second.value);
m_immutable_table.erase(i++);
m_counters.inc_stats_counter(counters::dht_immutable_data, -1);
m_counters.immutable_data -= 1;
}
// look through all peers and see if any have timed out
@ -501,15 +502,20 @@ namespace
if (it != m_map.end())
{
m_map.erase(it);
m_counters.inc_stats_counter(counters::dht_torrents, -1);
m_counters.torrents -= 1;
}
}
}
virtual dht_storage_counters counters() const TORRENT_OVERRIDE
{
return m_counters;
}
private:
sha1_hash m_id;
dht_settings const& m_settings;
counters& m_counters;
dht_storage_counters m_counters;
table_t m_map;
dht_immutable_table_t m_immutable_table;
@ -518,10 +524,9 @@ namespace
}
dht_storage_interface* dht_default_storage_constructor(sha1_hash const& id
, dht_settings const& settings
, counters& counters)
, dht_settings const& settings)
{
return new dht_default_storage(id, settings, counters);
return new dht_default_storage(id, settings);
}
} } // namespace libtorrent::dht

View File

@ -171,6 +171,11 @@ namespace libtorrent { namespace dht
m_dht.status(table, requests);
}
void dht_tracker::update_stats_counters(counters& c) const
{
m_dht.update_stats_counters(c);
}
void dht_tracker::connection_timeout(error_code const& e)
{
if (e || m_abort) return;

View File

@ -98,7 +98,7 @@ node::node(udp_socket_interface* sock
, m_last_self_refresh(min_time())
, m_sock(sock)
, m_counters(cnt)
, m_storage(dht_default_storage_constructor(m_id, m_settings, m_counters))
, m_storage(dht_default_storage_constructor(m_id, m_settings))
{
m_secret[0] = random();
m_secret[1] = random();
@ -603,6 +603,18 @@ void node::status(std::vector<dht_routing_bucket>& table
}
}
// TODO: in the future, this function should update all the
// 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);
}
#ifndef TORRENT_NO_DEPRECATE
// TODO: 2 use the non deprecated function instead of this one
void node::status(session_status& s)
@ -1154,4 +1166,5 @@ void node::incoming_request(msg const& m, entry& e)
return;
}
}
} } // namespace libtorrent::dht

View File

@ -4417,6 +4417,11 @@ retry:
{
m_disk_thread.update_stats_counters(m_stats_counters);
#ifndef TORRENT_DISABLE_DHT
if (m_dht)
m_dht->update_stats_counters(m_stats_counters);
#endif
m_stats_counters.set_value(counters::sent_ip_overhead_bytes
, m_stat.total_transfer(stat::upload_ip_protocol));

View File

@ -74,8 +74,7 @@ namespace
TORRENT_TEST(dht_storage)
{
dht_settings sett = test_settings();
counters cnt;
dht_storage_interface* s = dht_default_storage_constructor(node_id(0), sett, cnt);
dht_storage_interface* s = dht_default_storage_constructor(node_id(0), sett);
TEST_CHECK(s != NULL);