From 3a727e99ffc4fec1d7a7c492d68e8091ff85c009 Mon Sep 17 00:00:00 2001
From: Arvid Norberg
Date: Thu, 10 Jan 2008 22:13:23 +0000
Subject: [PATCH] expose more info through peer_info
---
docs/manual.html | 19 ++++++++++++++
docs/manual.rst | 23 +++++++++++++++++
examples/client_test.cpp | 10 +++++---
include/libtorrent/peer_connection.hpp | 2 --
include/libtorrent/peer_info.hpp | 10 ++++++--
src/peer_connection.cpp | 34 +++++++++++---------------
src/torrent.cpp | 2 ++
7 files changed, 72 insertions(+), 28 deletions(-)
diff --git a/docs/manual.html b/docs/manual.html
index 9298158d4..88d73c66c 100644
--- a/docs/manual.html
+++ b/docs/manual.html
@@ -2242,6 +2242,14 @@ struct peer_info
int upload_limit;
int download_limit;
+ time_duration last_request;
+ time_duration last_active;
+
+ int send_buffer_size;
+ int used_send_buffer;
+
+ int num_hashfails;
+
char country[2];
size_type load_balancing;
@@ -2249,6 +2257,8 @@ struct peer_info
int download_queue_length;
int upload_queue_length;
+ int failcount;
+
int downloading_piece_index;
int downloading_block_index;
int downloading_progress;
@@ -2383,6 +2393,12 @@ peer every second. It may be -1 if there's no local limit on the peer. The globa
limit and the torrent limit is always enforced anyway.
download_limit is the number of bytes per second this peer is allowed to
receive. -1 means it's unlimited.
+last_request and last_active is the time since we last sent a request
+to this peer and since any transfer occurred with this peer, respectively.
+send_buffer_size and used_send_buffer is the number of bytes allocated
+and used for the peer's send buffer, respectively.
+num_hashfails is the number of pieces this peer has participated in
+sending us that turned out to fail the hash check.
country is the two letter ISO 3166 country code for the country the peer
is connected from. If the country hasn't been resolved yet, both chars are set
to 0. If the resolution failed for some reason, the field is set to "--". If the
@@ -2398,6 +2414,9 @@ download.
that hasn't been answered with a piece yet.
upload_queue_length is the number of piece-requests we have received from this peer
that we haven't answered with a piece yet.
+failcount is the number of times this peer has "failed". i.e. failed to connect
+or disconnected us. The failcount is decremented when we see this peer in a tracker
+response or peer exchange message.
You can know which piece, and which part of that piece, that is currently being
downloaded from a specific peer by looking at the next four members.
downloading_piece_index is the index of the piece that is currently being downloaded.
diff --git a/docs/manual.rst b/docs/manual.rst
index f0f4e8f5d..74c380874 100644
--- a/docs/manual.rst
+++ b/docs/manual.rst
@@ -2231,6 +2231,14 @@ It contains the following fields::
int upload_limit;
int download_limit;
+ time_duration last_request;
+ time_duration last_active;
+
+ int send_buffer_size;
+ int used_send_buffer;
+
+ int num_hashfails;
+
char country[2];
size_type load_balancing;
@@ -2238,6 +2246,8 @@ It contains the following fields::
int download_queue_length;
int upload_queue_length;
+ int failcount;
+
int downloading_piece_index;
int downloading_block_index;
int downloading_progress;
@@ -2357,6 +2367,15 @@ limit and the torrent limit is always enforced anyway.
``download_limit`` is the number of bytes per second this peer is allowed to
receive. -1 means it's unlimited.
+``last_request`` and ``last_active`` is the time since we last sent a request
+to this peer and since any transfer occurred with this peer, respectively.
+
+``send_buffer_size`` and ``used_send_buffer`` is the number of bytes allocated
+and used for the peer's send buffer, respectively.
+
+``num_hashfails`` is the number of pieces this peer has participated in
+sending us that turned out to fail the hash check.
+
``country`` is the two letter `ISO 3166 country code`__ for the country the peer
is connected from. If the country hasn't been resolved yet, both chars are set
to 0. If the resolution failed for some reason, the field is set to "--". If the
@@ -2378,6 +2397,10 @@ that hasn't been answered with a piece yet.
``upload_queue_length`` is the number of piece-requests we have received from this peer
that we haven't answered with a piece yet.
+``failcount`` is the number of times this peer has "failed". i.e. failed to connect
+or disconnected us. The failcount is decremented when we see this peer in a tracker
+response or peer exchange message.
+
You can know which piece, and which part of that piece, that is currently being
downloaded from a specific peer by looking at the next four members.
``downloading_piece_index`` is the index of the piece that is currently being downloaded.
diff --git a/examples/client_test.cpp b/examples/client_test.cpp
index cb9e5265b..86ffd6893 100644
--- a/examples/client_test.cpp
+++ b/examples/client_test.cpp
@@ -306,7 +306,7 @@ int peer_index(libtorrent::tcp::endpoint addr, std::vector const& peers)
{
using namespace libtorrent;
- out << "IP down (total) up (total) sent-req recv flags source fail hshf sndb inactive wait disk quota block-progress "
+ out << "IP down (total) up (total) sent-req recv flags source fail hshf sndb inactive wait disk quota block-progress "
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
"country "
#endif
@@ -340,11 +340,13 @@ void print_peer_info(std::ostream& out, std::vector const
<< ((i->flags & peer_info::seed)?'s':'.')
<< ((i->flags & peer_info::on_parole)?'p':'.')
<< ((i->flags & peer_info::optimistic_unchoke)?'O':'.')
+ << ((i->flags & peer_info::reading)?'R':(i->flags & peer_info::waiting_read_quota)?'r':'.')
+ << ((i->flags & peer_info::writing)?'W':(i->flags & peer_info::waiting_write_quota)?'w':'.')
#ifndef TORRENT_DISABLE_ENCRYPTION
<< ((i->flags & peer_info::rc4_encrypted)?'E':
(i->flags & peer_info::plaintext_encrypted)?'e':'.')
#else
- << " "
+ << ".."
#endif
<< " "
<< ((i->source & peer_info::tracker)?"T":"_")
@@ -354,10 +356,10 @@ void print_peer_info(std::ostream& out, std::vector const
<< ((i->source & peer_info::resume_data)?"R":"_") << " "
<< to_string(i->failcount, 2) << " "
<< to_string(i->num_hashfails, 2) << " "
- << to_string(i->send_buffer_size, 6) << " "
+ << to_string(i->used_send_buffer, 6) << " ("<< add_suffix(i->send_buffer_size) << ") "
<< to_string(total_seconds(i->last_active), 8) << " "
<< to_string(total_seconds(i->last_request), 4) << " "
- << to_string(i->pending_disk_bytes, 6) << " "
+ << add_suffix(i->pending_disk_bytes) << " "
<< to_string(i->send_quota, 5) << " ";
if (i->downloading_piece_index >= 0)
diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp
index cced96c58..2a88ed334 100755
--- a/include/libtorrent/peer_connection.hpp
+++ b/include/libtorrent/peer_connection.hpp
@@ -669,10 +669,8 @@ namespace libtorrent
// request is in progress.
bool m_writing;
bool m_reading;
-#ifndef NDEBUG
bool m_requested_write_quota;
bool m_requested_read_quota;
-#endif
// if set to non-zero, this peer will always prefer
// to request entire n pieces, rather than blocks.
diff --git a/include/libtorrent/peer_info.hpp b/include/libtorrent/peer_info.hpp
index 94ab79815..e12a2404b 100755
--- a/include/libtorrent/peer_info.hpp
+++ b/include/libtorrent/peer_info.hpp
@@ -57,7 +57,11 @@ namespace libtorrent
queued = 0x100,
on_parole = 0x200,
seed = 0x400,
- optimistic_unchoke = 0x800
+ optimistic_unchoke = 0x800,
+ writing = 0x1000,
+ reading = 0x2000,
+ waiting_write_quota = 0x4000,
+ waiting_read_quota = 0x8000
#ifndef TORRENT_DISABLE_ENCRYPTION
, rc4_encrypted = 0x100000,
plaintext_encrypted = 0x200000
@@ -96,8 +100,10 @@ namespace libtorrent
// time since last download or upload
time_duration last_active;
- // the size of the send buffer for this peer
+ // the size of the send buffer for this peer, in bytes
int send_buffer_size;
+ // the number bytes that's actually used of the send buffer
+ int used_send_buffer;
// the number of failed hashes for this peer
int num_hashfails;
diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp
index faa13a9a6..f3db521e6 100755
--- a/src/peer_connection.cpp
+++ b/src/peer_connection.cpp
@@ -107,6 +107,8 @@ namespace libtorrent
, m_queued(true)
, m_writing(false)
, m_reading(false)
+ , m_requested_write_quota(false)
+ , m_requested_read_quota(false)
, m_prefer_whole_pieces(false)
, m_request_large_blocks(false)
, m_non_prioritized(false)
@@ -135,8 +137,6 @@ namespace libtorrent
#endif
#ifndef NDEBUG
piece_failed = false;
- m_requested_read_quota = false;
- m_requested_write_quota = false;
#endif
boost::shared_ptr t = m_torrent.lock();
@@ -190,6 +190,8 @@ namespace libtorrent
, m_queued(false)
, m_writing(false)
, m_reading(false)
+ , m_requested_write_quota(false)
+ , m_requested_read_quota(false)
, m_prefer_whole_pieces(false)
, m_request_large_blocks(false)
, m_non_prioritized(false)
@@ -234,8 +236,6 @@ namespace libtorrent
#ifndef NDEBUG
piece_failed = false;
- m_requested_read_quota = false;
- m_requested_write_quota = false;
#endif
std::fill(m_peer_id.begin(), m_peer_id.end(), 0);
}
@@ -2224,6 +2224,11 @@ namespace libtorrent
}
p.send_buffer_size = m_send_buffer.capacity();
+ p.used_send_buffer = m_send_buffer.size();
+ p.flags |= m_reading ? peer_info::reading : 0;
+ p.flags |= m_writing ? peer_info::writing : 0;
+ p.flags |= m_requested_write_quota ? peer_info::waiting_write_quota : 0;
+ p.flags |= m_requested_read_quota ? peer_info::waiting_read_quota : 0;
}
void peer_connection::cut_receive_buffer(int size, int packet_size)
@@ -2496,22 +2501,14 @@ namespace libtorrent
m_bandwidth_limit[channel].assign(amount);
if (channel == upload_channel)
{
- TORRENT_ASSERT(m_writing);
- m_writing = false;
-#ifndef NDEBUG
TORRENT_ASSERT(m_requested_write_quota);
m_requested_write_quota = false;
-#endif
setup_send();
}
else if (channel == download_channel)
{
- TORRENT_ASSERT(m_reading);
- m_reading = false;
-#ifndef NDEBUG
TORRENT_ASSERT(m_requested_read_quota);
m_requested_read_quota = false;
-#endif
setup_receive();
}
}
@@ -2537,7 +2534,7 @@ namespace libtorrent
INVARIANT_CHECK;
- if (m_writing) return;
+ if (m_writing || m_requested_write_quota) return;
shared_ptr t = m_torrent.lock();
@@ -2559,11 +2556,8 @@ namespace libtorrent
TORRENT_ASSERT(!m_writing);
// peers that we are not interested in are non-prioritized
- m_writing = true;
-#ifndef NDEBUG
TORRENT_ASSERT(!m_requested_write_quota);
m_requested_write_quota = true;
-#endif
t->request_bandwidth(upload_channel, self()
, !(is_interesting() && !has_peer_choked())
, m_send_buffer.size());
@@ -2604,7 +2598,7 @@ namespace libtorrent
#ifdef TORRENT_VERBOSE_LOGGING
(*m_logger) << "setup_receive: reading = " << m_reading << "\n";
#endif
- if (m_reading) return;
+ if (m_reading || m_requested_read_quota) return;
shared_ptr t = m_torrent.lock();
@@ -2618,11 +2612,8 @@ namespace libtorrent
#ifdef TORRENT_VERBOSE_LOGGING
(*m_logger) << "req bandwidth [ " << download_channel << " ]\n";
#endif
- m_reading = true;
-#ifndef NDEBUG
TORRENT_ASSERT(!m_requested_read_quota);
m_requested_read_quota = true;
-#endif
t->request_bandwidth(download_channel, self(), m_non_prioritized
, m_download_queue.size() * 16 * 1024 + 30);
}
@@ -3039,6 +3030,9 @@ namespace libtorrent
|| m_bandwidth_limit[i].throttle() == bandwidth_limit::inf);
}
+ TORRENT_ASSERT(int(m_reading) + int(m_requested_read_quota) <= 1);
+ TORRENT_ASSERT(int(m_writing) + int(m_requested_write_quota) <= 1);
+
std::set unique;
std::copy(m_download_queue.begin(), m_download_queue.end(), std::inserter(unique, unique.begin()));
std::copy(m_request_queue.begin(), m_request_queue.end(), std::inserter(unique, unique.begin()));
diff --git a/src/torrent.cpp b/src/torrent.cpp
index 1a2927306..d2d1155e1 100755
--- a/src/torrent.cpp
+++ b/src/torrent.cpp
@@ -2301,6 +2301,8 @@ namespace libtorrent
{
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
+ INVARIANT_CHECK;
+
TORRENT_ASSERT(amount > 0);
m_bandwidth_limit[channel].expire(amount);
queue_t tmp;