fixed problem in piece_picker with filtered files, also optimzed it somewhat. Added more logging and added one test to the piece_picker test

This commit is contained in:
Arvid Norberg
2005-09-01 21:04:21 +00:00
parent 41d2c0d694
commit a15f589341
11 changed files with 133 additions and 59 deletions

View File

@@ -409,6 +409,10 @@ int main(int argc, char* argv[])
handles.back().set_max_connections(60); handles.back().set_max_connections(60);
handles.back().set_max_uploads(-1); handles.back().set_max_uploads(-1);
// handles.back().set_ratio(1.02f); // handles.back().set_ratio(1.02f);
// std::vector<bool> ffilter(t.num_files(), true);
// ffilter[0] = false;
// handles.back().filter_files(ffilter);
} }
catch (std::exception& e) catch (std::exception& e)

View File

@@ -103,12 +103,13 @@ namespace libtorrent
struct file_logger: libtorrent::logger struct file_logger: libtorrent::logger
{ {
public: public:
file_logger(boost::filesystem::path const& filename) file_logger(boost::filesystem::path const& filename, bool append = true)
{ {
using namespace boost::filesystem; using namespace boost::filesystem;
path dir(complete("libtorrent_logs")); path dir(complete("libtorrent_logs"));
if (!exists(dir)) create_directories(dir); if (!exists(dir)) create_directories(dir);
m_file.open(dir / filename); m_file.open(dir / filename, std::ios_base::out | (append ? std::ios_base::app : std::ios_base::out));
log("\n\n\n*** starting log ***\n");
} }
virtual void log(const char* text) { assert(text); m_file << text; } virtual void log(const char* text) { assert(text); m_file << text; }

View File

@@ -260,6 +260,7 @@ namespace libtorrent
}; };
void add(int index);
void move(bool downloading, bool filtered, int vec_index, int elem_index); void move(bool downloading, bool filtered, int vec_index, int elem_index);
void remove(bool downloading, bool filtered, int vec_index, int elem_index); void remove(bool downloading, bool filtered, int vec_index, int elem_index);
std::vector<std::vector<int> >& pick_piece_info_vector(bool downloading std::vector<std::vector<int> >& pick_piece_info_vector(bool downloading
@@ -294,14 +295,12 @@ namespace libtorrent
// during piece picking // during piece picking
std::vector<std::vector<int> > m_downloading_piece_info; std::vector<std::vector<int> > m_downloading_piece_info;
// this vector has the same structure as m_piece_info
// but only contains pieces we aren't interested in (filtered)
std::vector<std::vector<int> > m_filtered_piece_info;
// this maps indices to number of peers that has this piece and // this maps indices to number of peers that has this piece and
// index into the m_piece_info vectors. // index into the m_piece_info vectors.
// piece_pos::we_have_index means that we have the piece, so it // piece_pos::we_have_index means that we have the piece, so it
// doesn't exist in the piece_info buckets // doesn't exist in the piece_info buckets
// pieces with the filtered flag set doesn't have entries in
// the m_piece_info buckets either
std::vector<piece_pos> m_piece_map; std::vector<piece_pos> m_piece_map;
// each piece that's currently being downloaded // each piece that's currently being downloaded

View File

@@ -271,7 +271,7 @@ namespace libtorrent
void check_invariant(const char *place = 0); void check_invariant(const char *place = 0);
#endif #endif
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING) #if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
boost::shared_ptr<logger> create_log(std::string const& name); boost::shared_ptr<logger> create_log(std::string const& name, bool append = true);
boost::shared_ptr<logger> m_logger; boost::shared_ptr<logger> m_logger;
#endif #endif
}; };

View File

@@ -158,6 +158,7 @@ namespace libtorrent
#ifdef TORRENT_VERBOSE_LOGGING #ifdef TORRENT_VERBOSE_LOGGING
m_logger = m_ses.create_log(s->sender().as_string().c_str()); m_logger = m_ses.create_log(s->sender().as_string().c_str());
(*m_logger) << "*** OUTGOING CONNECTION\n";
#endif #endif
std::fill(m_peer_id.begin(), m_peer_id.end(), 0); std::fill(m_peer_id.begin(), m_peer_id.end(), 0);
@@ -272,6 +273,7 @@ namespace libtorrent
#ifdef TORRENT_VERBOSE_LOGGING #ifdef TORRENT_VERBOSE_LOGGING
m_logger = m_ses.create_log(s->sender().as_string().c_str()); m_logger = m_ses.create_log(s->sender().as_string().c_str());
(*m_logger) << "*** INCOMING CONNECTION\n";
#endif #endif
// initialize the extension list to zero, since // initialize the extension list to zero, since
@@ -795,9 +797,6 @@ namespace libtorrent
// if we're a seed too, disconnect // if we're a seed too, disconnect
if (m_torrent->is_seed()) if (m_torrent->is_seed())
{ {
#ifdef TORRENT_VERBOSE_LOGGING
(*m_logger) << " we're also a seed, disconnecting\n";
#endif
throw protocol_error("seed to seed connection redundant, disconnecting"); throw protocol_error("seed to seed connection redundant, disconnecting");
} }
} }

View File

@@ -48,7 +48,6 @@ namespace libtorrent
piece_picker::piece_picker(int blocks_per_piece, int total_num_blocks) piece_picker::piece_picker(int blocks_per_piece, int total_num_blocks)
: m_piece_info(2) : m_piece_info(2)
, m_downloading_piece_info(2) , m_downloading_piece_info(2)
, m_filtered_piece_info(2)
, m_piece_map((total_num_blocks + blocks_per_piece-1) / blocks_per_piece) , m_piece_map((total_num_blocks + blocks_per_piece-1) / blocks_per_piece)
, m_num_filtered(0) , m_num_filtered(0)
, m_num_have_filtered(0) , m_num_have_filtered(0)
@@ -84,6 +83,10 @@ namespace libtorrent
std::vector<int> piece_list; std::vector<int> piece_list;
piece_list.reserve(std::count(pieces.begin(), pieces.end(), false)); piece_list.reserve(std::count(pieces.begin(), pieces.end(), false));
#ifndef NDEBUG
integrity_check();
#endif
for (std::vector<bool>::const_iterator i = pieces.begin(); for (std::vector<bool>::const_iterator i = pieces.begin();
i != pieces.end(); ++i) i != pieces.end(); ++i)
{ {
@@ -93,10 +96,14 @@ namespace libtorrent
{ {
++m_num_filtered; ++m_num_filtered;
--m_num_have_filtered; --m_num_have_filtered;
m_piece_map[index].index = 0;
}
else
{
piece_list.push_back(index);
} }
piece_list.push_back(index);
} }
// random shuffle the list // random shuffle the list
std::random_shuffle(piece_list.begin(), piece_list.end()); std::random_shuffle(piece_list.begin(), piece_list.end());
@@ -107,20 +114,18 @@ namespace libtorrent
int index = *i; int index = *i;
assert(index >= 0); assert(index >= 0);
assert(index < (int)m_piece_map.size()); assert(index < (int)m_piece_map.size());
assert(m_piece_map[index].index == piece_pos::we_have_index); assert(m_piece_map[index].index == piece_pos::we_have_index);
assert(m_piece_map[index].peer_count == 0);
int peer_count = m_piece_map[index].peer_count;
assert(peer_count == 0);
assert(m_piece_info.size() == 2); assert(m_piece_info.size() == 2);
piece_pos& p = m_piece_map[index]; add(index);
std::vector<std::vector<int> >& dst_vec = pick_piece_info_vector( assert(m_piece_map[index].index != piece_pos::we_have_index);
p.downloading, p.filtered);
assert((int)dst_vec.size() > peer_count);
p.index = (int)dst_vec[peer_count].size();
dst_vec[peer_count].push_back(index);
} }
#ifndef NDEBUG
integrity_check();
#endif
// if we have fast resume info // if we have fast resume info
// use it // use it
if (!unfinished.empty()) if (!unfinished.empty())
@@ -225,7 +230,7 @@ namespace libtorrent
} }
} }
else else if (!i->filtered)
{ {
if (t != 0) if (t != 0)
assert(!t->have_piece(index)); assert(!t->have_piece(index));
@@ -276,24 +281,40 @@ namespace libtorrent
std::vector<std::vector<int> >& piece_picker::pick_piece_info_vector( std::vector<std::vector<int> >& piece_picker::pick_piece_info_vector(
bool downloading, bool filtered) bool downloading, bool filtered)
{ {
return filtered assert(!filtered);
?m_filtered_piece_info return downloading?m_downloading_piece_info:m_piece_info;
:(downloading?m_downloading_piece_info:m_piece_info);
} }
std::vector<std::vector<int> > const& piece_picker::pick_piece_info_vector( std::vector<std::vector<int> > const& piece_picker::pick_piece_info_vector(
bool downloading, bool filtered) const bool downloading, bool filtered) const
{ {
return filtered assert(!filtered);
?m_filtered_piece_info return downloading?m_downloading_piece_info:m_piece_info;
:(downloading?m_downloading_piece_info:m_piece_info);
} }
void piece_picker::add(int index)
{
assert(index >= 0);
assert(index < (int)m_piece_map.size());
piece_pos& p = m_piece_map[index];
assert(!p.filtered);
std::vector<std::vector<int> >& dst_vec = pick_piece_info_vector(
p.downloading, p.filtered);
if (dst_vec.size() <= p.peer_count)
dst_vec.resize(p.peer_count + 1);
assert(dst_vec.size() > p.peer_count);
p.index = (int)dst_vec[p.peer_count].size();
dst_vec[p.peer_count].push_back(index);
}
// will update the piece with the given properties (downloading, filtered, peer_count, elem_index) // will update the piece with the given properties (downloading, filtered, peer_count, elem_index)
// to place it at the correct position in the vectors. // to place it at the correct position in the vectors.
void piece_picker::move(bool downloading, bool filtered, int peer_count, int elem_index) void piece_picker::move(bool downloading, bool filtered, int peer_count, int elem_index)
{ {
assert(!filtered);
assert(peer_count >= 0); assert(peer_count >= 0);
assert(elem_index >= 0); assert(elem_index >= 0);
std::vector<std::vector<int> >& src_vec(pick_piece_info_vector(downloading, filtered)); std::vector<std::vector<int> >& src_vec(pick_piece_info_vector(downloading, filtered));
@@ -345,6 +366,7 @@ namespace libtorrent
void piece_picker::remove(bool downloading, bool filtered, int peer_count, int elem_index) void piece_picker::remove(bool downloading, bool filtered, int peer_count, int elem_index)
{ {
assert(!filtered);
assert(peer_count >= 0); assert(peer_count >= 0);
assert(elem_index >= 0); assert(elem_index >= 0);
@@ -354,7 +376,6 @@ namespace libtorrent
assert((int)src_vec[peer_count].size() > elem_index); assert((int)src_vec[peer_count].size() > elem_index);
int index = src_vec[peer_count][elem_index]; int index = src_vec[peer_count][elem_index];
m_piece_map[index].index = piece_pos::we_have_index;
if (downloading) if (downloading)
{ {
@@ -393,6 +414,7 @@ namespace libtorrent
m_piece_map[index].downloading = 0; m_piece_map[index].downloading = 0;
piece_pos& p = m_piece_map[index]; piece_pos& p = m_piece_map[index];
if (p.filtered) return;
move(true, p.filtered, p.peer_count, p.index); move(true, p.filtered, p.peer_count, p.index);
#ifndef NDEBUG #ifndef NDEBUG
@@ -412,11 +434,12 @@ namespace libtorrent
m_piece_map[i].peer_count++; m_piece_map[i].peer_count++;
assert(m_piece_map[i].peer_count != 0); assert(m_piece_map[i].peer_count != 0);
// if we have the piece, we don't have to move
// any entries in the piece_info vector
if (index == piece_pos::we_have_index) return;
piece_pos& p = m_piece_map[i]; piece_pos& p = m_piece_map[i];
// if we have the piece or if it's filtered
// we don't have to move any entries in the piece_info vector
if (index == piece_pos::we_have_index || p.filtered) return;
move(p.downloading, p.filtered, peer_count, index); move(p.downloading, p.filtered, peer_count, index);
#ifndef NDEBUG #ifndef NDEBUG
@@ -441,8 +464,10 @@ namespace libtorrent
if (m_piece_map[i].peer_count > 0) if (m_piece_map[i].peer_count > 0)
m_piece_map[i].peer_count--; m_piece_map[i].peer_count--;
if (index == piece_pos::we_have_index) return;
piece_pos& p = m_piece_map[i]; piece_pos& p = m_piece_map[i];
if (index == piece_pos::we_have_index || p.filtered) return;
move(p.downloading, p.filtered, peer_count, index); move(p.downloading, p.filtered, peer_count, index);
} }
@@ -466,8 +491,11 @@ namespace libtorrent
{ {
--m_num_filtered; --m_num_filtered;
++m_num_have_filtered; ++m_num_have_filtered;
return;
} }
if (info_index == piece_pos::we_have_index) return;
remove(p.downloading, p.filtered, peer_count, info_index); remove(p.downloading, p.filtered, peer_count, info_index);
p.index = piece_pos::we_have_index;
#ifndef NDEBUG #ifndef NDEBUG
integrity_check(); integrity_check();
#endif #endif
@@ -480,7 +508,7 @@ namespace libtorrent
assert(index < (int)m_piece_map.size()); assert(index < (int)m_piece_map.size());
#ifndef NDEBUG #ifndef NDEBUG
integrity_check(); // integrity_check();
#endif #endif
piece_pos& p = m_piece_map[index]; piece_pos& p = m_piece_map[index];
@@ -489,7 +517,8 @@ namespace libtorrent
if (p.index != piece_pos::we_have_index) if (p.index != piece_pos::we_have_index)
{ {
++m_num_filtered; ++m_num_filtered;
move(p.downloading, false, p.peer_count, p.index); remove(p.downloading, false, p.peer_count, p.index);
assert(p.filtered == 1);
} }
else else
{ {
@@ -518,7 +547,7 @@ namespace libtorrent
{ {
--m_num_filtered; --m_num_filtered;
assert(m_num_filtered >= 0); assert(m_num_filtered >= 0);
move(p.downloading, true, p.peer_count, p.index); add(index);
} }
else else
{ {
@@ -854,7 +883,7 @@ namespace libtorrent
assert(block.block_index < blocks_in_piece(block.piece_index)); assert(block.block_index < blocks_in_piece(block.piece_index));
piece_pos& p = m_piece_map[block.piece_index]; piece_pos& p = m_piece_map[block.piece_index];
if (p.index == piece_pos::we_have_index) return; if (p.index == piece_pos::we_have_index || p.filtered) return;
if (p.downloading == 0) if (p.downloading == 0)
{ {

View File

@@ -1147,6 +1147,10 @@ namespace libtorrent
peer *p = find_disconnect_candidate(); peer *p = find_disconnect_candidate();
if(!p) if(!p)
return false; return false;
#if defined(TORRENT_VERBOSE_LOGGING)
(*p->connection->m_logger) << "*** CLOSING CONNECTION 'too many connections'\n";
#endif
p->connection->disconnect(); p->connection->disconnect();
return true; return true;
} }

View File

@@ -234,7 +234,7 @@ namespace libtorrent { namespace detail
, m_incoming_connection(false) , m_incoming_connection(false)
{ {
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING) #if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
m_logger = create_log("main_session"); m_logger = create_log("main_session", false);
#endif #endif
std::fill(m_extension_enabled, m_extension_enabled std::fill(m_extension_enabled, m_extension_enabled
+ peer_connection::num_supported_extensions, true); + peer_connection::num_supported_extensions, true);
@@ -257,8 +257,7 @@ namespace libtorrent { namespace detail
// the random number // the random number
for (unsigned char* i = m_peer_id.begin() + print.length(); for (unsigned char* i = m_peer_id.begin() + print.length();
i != m_peer_id.end(); i != m_peer_id.end(); ++i)
++i)
{ {
*i = printable[rand() % (sizeof(printable)-1)]; *i = printable[rand() % (sizeof(printable)-1)];
} }
@@ -480,6 +479,10 @@ namespace libtorrent { namespace detail
, e.what())); , e.what()));
} }
#if defined(TORRENT_VERBOSE_LOGGING)
(*p->second->m_logger) << "*** CLOSING CONNECTION '" << e.what() << "'\n";
#endif
p->second->set_failed(); p->second->set_failed();
m_selector.remove(*i); m_selector.remove(*i);
m_connections.erase(p); m_connections.erase(p);
@@ -576,6 +579,11 @@ namespace libtorrent { namespace detail
, p->second->id() , p->second->id()
, e.what())); , e.what()));
} }
#if defined(TORRENT_VERBOSE_LOGGING)
(*p->second->m_logger) << "*** CLOSING CONNECTION '" << e.what() << "'\n";
#endif
// the connection wants to disconnect for some reason, remove it // the connection wants to disconnect for some reason, remove it
// from the connection-list // from the connection-list
p->second->set_failed(); p->second->set_failed();
@@ -615,6 +623,9 @@ namespace libtorrent { namespace detail
// the connection may have been disconnected in the receive or send phase // the connection may have been disconnected in the receive or send phase
if (p != m_connections.end()) if (p != m_connections.end())
{ {
#if defined(TORRENT_VERBOSE_LOGGING)
(*p->second->m_logger) << "*** CONNECTION EXCEPTION\n";
#endif
p->second->set_failed(); p->second->set_failed();
m_connections.erase(p); m_connections.erase(p);
} }
@@ -667,6 +678,10 @@ namespace libtorrent { namespace detail
, j->second->id() , j->second->id()
, "connection timed out")); , "connection timed out"));
} }
#if defined(TORRENT_VERBOSE_LOGGING)
(*j->second->m_logger) << "*** CONNECTION TIMED OUT\n";
#endif
j->second->set_failed(); j->second->set_failed();
m_selector.remove(j->first); m_selector.remove(j->first);
m_connections.erase(j); m_connections.erase(j);
@@ -810,10 +825,10 @@ namespace libtorrent { namespace detail
} }
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING) #if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
boost::shared_ptr<logger> session_impl::create_log(std::string const& name) boost::shared_ptr<logger> session_impl::create_log(std::string const& name, bool append)
{ {
// current options are file_logger, cout_logger and null_logger // current options are file_logger, cout_logger and null_logger
return boost::shared_ptr<logger>(new file_logger(name + ".log")); return boost::shared_ptr<logger>(new file_logger(name + ".log", append));
} }
#endif #endif
@@ -908,6 +923,9 @@ namespace libtorrent
if (m_impl.m_ip_filter.access(i->first->sender()) if (m_impl.m_ip_filter.access(i->first->sender())
& ip_filter::blocked) & ip_filter::blocked)
{ {
#if defined(TORRENT_VERBOSE_LOGGING)
(*i->second->m_logger) << "*** CONNECTION FILTERED'\n";
#endif
m_impl.m_connections.erase(i++); m_impl.m_connections.erase(i++);
} }
else ++i; else ++i;

View File

@@ -30,11 +30,7 @@ POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <ios>
#include <ctime> #include <ctime>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <iterator> #include <iterator>
#include <algorithm> #include <algorithm>
#include <set> #include <set>
@@ -70,6 +66,13 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/file.hpp" #include "libtorrent/file.hpp"
#include "libtorrent/invariant_check.hpp" #include "libtorrent/invariant_check.hpp"
#ifndef NDEBUG
#include <ios>
#include <iostream>
#include <iomanip>
#include <cstdio>
#endif
#if defined(_WIN32) && defined(UNICODE) #if defined(_WIN32) && defined(UNICODE)
#include <windows.h> #include <windows.h>

View File

@@ -579,6 +579,10 @@ namespace libtorrent
, "banning peer because of too many corrupt pieces")); , "banning peer because of too many corrupt pieces"));
} }
m_policy->ban_peer(*p->second); m_policy->ban_peer(*p->second);
#if defined(TORRENT_VERBOSE_LOGGING)
(*p->second->m_logger) << "*** BANNING PEER 'too many corrupt pieces'\n";
#endif
p->second->disconnect(); p->second->disconnect();
} }
} }
@@ -661,21 +665,23 @@ namespace libtorrent
// TODO: update peer's interesting-bit // TODO: update peer's interesting-bit
std::vector<std::pair<int, bool> > state; std::vector<int> state;
state.reserve(100); state.reserve(100);
int index = 0; int index = 0;
for (std::vector<bool>::const_iterator i = bitmask.begin() for (std::vector<bool>::const_iterator i = bitmask.begin()
, end(bitmask.end()); i != end; ++i, ++index) , end(bitmask.end()); i != end; ++i, ++index)
{ {
if (m_picker->is_filtered(index) == *i) continue; if (m_picker->is_filtered(index) == *i) continue;
state.push_back(std::make_pair(index, *i)); if (*i)
m_picker->mark_as_filtered(index);
else
state.push_back(index);
} }
std::random_shuffle(state.begin(), state.end()); std::random_shuffle(state.begin(), state.end());
for (std::vector<std::pair<int, bool> >::iterator i = state.begin(); for (std::vector<int>::iterator i = state.begin();
i != state.end(); ++i) i != state.end(); ++i)
{ {
if (i->second) m_picker->mark_as_filtered(i->first); m_picker->mark_as_unfiltered(*i);
else m_picker->mark_as_unfiltered(i->first);
} }
} }
@@ -892,10 +898,16 @@ namespace libtorrent
void torrent::disconnect_all() void torrent::disconnect_all()
{ {
for (peer_iterator i = m_connections.begin(); for (peer_iterator i = m_connections.begin();
i != m_connections.end(); i != m_connections.end(); ++i)
++i)
{ {
assert(i->second->associated_torrent() == this); assert(i->second->associated_torrent() == this);
#if defined(TORRENT_VERBOSE_LOGGING)
if (m_abort)
(*i->second->m_logger) << "*** CLOSING CONNECTION 'aborting'\n";
else
(*i->second->m_logger) << "*** CLOSING CONNECTION 'pausing'\n";
#endif
i->second->disconnect(); i->second->disconnect();
} }
} }
@@ -916,7 +928,12 @@ namespace libtorrent
{ {
assert(i->second->associated_torrent() == this); assert(i->second->associated_torrent() == this);
if (i->second->is_seed()) if (i->second->is_seed())
{
#if defined(TORRENT_VERBOSE_LOGGING)
(*i->second->m_logger) << "*** SEED, CLOSING CONNECTION\n";
#endif
i->second->disconnect(); i->second->disconnect();
}
} }
m_storage->release_files(); m_storage->release_files();

View File

@@ -243,9 +243,9 @@ int test_main()
TEST_CHECK(std::equal(picked.begin() TEST_CHECK(std::equal(picked.begin()
, picked.end(), expected6)); , picked.end(), expected6));
// make sure the piece picker allows filtered pieces
// TODO: make sure there are no duplicates // to become available
// in the picked blocks! p.mark_as_finished(piece_block(4, 2), address());
} }
return 0; return 0;