From eba657d8ad75d8aa4e5efd4f8470e26a77945d30 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 18 Oct 2010 00:10:33 +0000 Subject: [PATCH] reports redundant downloads to tracker, fixed downloaded calculation to be more stable when not including redundant. Improved redundant data accounting to be more accurate --- ChangeLog | 3 +++ include/libtorrent/tracker_manager.hpp | 6 ++++++ src/http_tracker_connection.cpp | 5 +++-- src/peer_connection.cpp | 14 +++++++++++++- src/torrent.cpp | 23 ++++++++++++++++++----- src/web_peer_connection.cpp | 4 ---- 6 files changed, 43 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0ddf03023..069797018 100644 --- a/ChangeLog +++ b/ChangeLog @@ -53,6 +53,9 @@ * added more detailed instrumentation of the disk I/O thread + * reports redundant downloads to tracker, fixed downloaded calculation to + be more stable when not including redundant. Improved redundant data accounting + to be more accurate * fixed bugs in http seed connection and added unit test for it * fixed error reporting when fallocate fails * deprecate support for separate proxies for separate kinds of connections diff --git a/include/libtorrent/tracker_manager.hpp b/include/libtorrent/tracker_manager.hpp index 8e939238a..525227857 100644 --- a/include/libtorrent/tracker_manager.hpp +++ b/include/libtorrent/tracker_manager.hpp @@ -79,6 +79,11 @@ namespace libtorrent { tracker_request() : kind(announce_request) + , downloaded(-1) + , uploaded(-1) + , left(-1) + , corrupt(0) + , redundant(0) , event(none) , key(0) , num_want(0) @@ -106,6 +111,7 @@ namespace libtorrent size_type uploaded; size_type left; size_type corrupt; + size_type redundant; unsigned short listen_port; event_t event; std::string url; diff --git a/src/http_tracker_connection.cpp b/src/http_tracker_connection.cpp index b8032fb79..ade7e945a 100644 --- a/src/http_tracker_connection.cpp +++ b/src/http_tracker_connection.cpp @@ -138,8 +138,8 @@ namespace libtorrent char str[1024]; const bool stats = tracker_req().send_stats; snprintf(str, sizeof(str), "&peer_id=%s&port=%d&uploaded=%"PRId64 - "&downloaded=%"PRId64"&left=%"PRId64"&corrupt=%"PRId64"&compact=1" - "&numwant=%d&key=%x&no_peer_id=1" + "&downloaded=%"PRId64"&left=%"PRId64"&corrupt=%"PRId64"&redundant=%"PRId64 + "&compact=1&numwant=%d&key=%x&no_peer_id=1" , escape_string((const char*)&tracker_req().pid[0], 20).c_str() // the i2p tracker seems to verify that the port is not 0, // even though it ignores it otherwise @@ -148,6 +148,7 @@ namespace libtorrent , stats ? tracker_req().downloaded : 0 , stats ? tracker_req().left : 0 , stats ? tracker_req().corrupt : 0 + , stats ? tracker_req().redundant: 0 , tracker_req().num_want , tracker_req().key); url += str; diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 5510376d8..97ff7ed71 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -3289,7 +3289,19 @@ namespace libtorrent if (t) { // make sure we keep all the stats! - if (!m_ignore_stats) t->add_stats(statistics()); + if (!m_ignore_stats) + { + t->add_stats(statistics()); + + // report any partially received payload as redundant + boost::optional pbp = downloading_piece_progress(); + if (pbp + && pbp->bytes_downloaded > 0 + && pbp->bytes_downloaded < pbp->full_block_bytes) + { + t->add_redundant_bytes(pbp->bytes_downloaded); + } + } if (t->has_picker()) { diff --git a/src/torrent.cpp b/src/torrent.cpp index 092626cc2..b62e4e047 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -1449,15 +1449,24 @@ namespace libtorrent tracker_request req; req.info_hash = m_torrent_file->info_hash(); req.pid = m_ses.get_peer_id(); - req.downloaded = m_stat.total_payload_download() - m_total_failed_bytes; req.uploaded = m_stat.total_payload_upload(); req.corrupt = m_total_failed_bytes; - req.left = bytes_left(); + req.redundant = m_total_redundant_bytes; + + if (settings().report_true_downloaded) + { + req.downloaded = m_stat.total_payload_download() - m_total_failed_bytes; + req.left = bytes_left(); + } + else + { + req.downloaded = quantized_bytes_done(); + TORRENT_ASSERT(!valid_metadata() || req.downloaded <= m_torrent_file->total_size()); + req.left = valid_metadata() ? m_torrent_file->total_size() - req.downloaded : -1; + } if (req.left == -1) req.left = 16*1024; - // exclude redundant bytes if we should - if (!settings().report_true_downloaded) - req.downloaded -= m_total_redundant_bytes; + TORRENT_ASSERT(req.downloaded >= 0); req.event = e; error_code ec; @@ -6838,6 +6847,8 @@ namespace libtorrent TORRENT_ASSERT(b > 0); m_total_redundant_bytes += b; m_ses.add_redundant_bytes(b); + TORRENT_ASSERT(m_total_redundant_bytes + m_total_failed_bytes + <= m_stat.total_payload_download()); } void torrent::add_failed_bytes(int b) @@ -6845,6 +6856,8 @@ namespace libtorrent TORRENT_ASSERT(b > 0); m_total_failed_bytes += b; m_ses.add_failed_bytes(b); + TORRENT_ASSERT(m_total_redundant_bytes + m_total_failed_bytes + <= m_stat.total_payload_download()); } int torrent::num_seeds() const diff --git a/src/web_peer_connection.cpp b/src/web_peer_connection.cpp index fd02a2ab4..51afe9255 100644 --- a/src/web_peer_connection.cpp +++ b/src/web_peer_connection.cpp @@ -96,10 +96,6 @@ namespace libtorrent void web_peer_connection::disconnect(error_code const& ec, int error) { boost::shared_ptr t = associated_torrent().lock(); - - if (t && m_block_pos) - t->add_redundant_bytes(m_block_pos); - peer_connection::disconnect(ec, error); if (t) t->disconnect_web_seed(this); }