fix test_optimistic_unchoking sim

This commit is contained in:
arvidn 2016-03-27 12:09:53 -04:00
parent 2b00eb1b38
commit 7649610807
3 changed files with 47 additions and 43 deletions

View File

@ -671,6 +671,10 @@ namespace libtorrent
private:
// return the settings value for int setting "n", if the value is
// negative, return INT_MAX
int get_int_setting(int n) const;
std::vector<torrent*> m_torrent_lists[num_torrent_lists];
peer_class_pool m_classes;

View File

@ -62,7 +62,8 @@ struct choke_state
TORRENT_TEST(optimistic_unchoke)
{
int const num_nodes = 20;
lt::time_duration const test_duration = libtorrent::seconds(num_nodes * 30 + 4);
lt::time_duration const test_duration
= libtorrent::seconds(num_nodes * 90);
dsl_config network_cfg;
sim::simulation sim{network_cfg};
@ -83,8 +84,7 @@ TORRENT_TEST(optimistic_unchoke)
session_proxy proxy;
auto ses = std::make_shared<lt::session>(
std::ref(pack), std::ref(ios));
auto ses = std::make_shared<lt::session>(std::ref(pack), std::ref(ios));
ses->async_add_torrent(atp);
std::vector<std::shared_ptr<sim::asio::io_service> > io_service;
@ -92,7 +92,7 @@ TORRENT_TEST(optimistic_unchoke)
print_alerts(*ses);
sim::timer t(sim, lt::seconds(2), [&](boost::system::error_code const& ec)
sim::timer t(sim, lt::seconds(0), [&](boost::system::error_code const& ec)
{
for (int i = 0; i < num_nodes; ++i)
{
@ -148,6 +148,7 @@ TORRENT_TEST(optimistic_unchoke)
{
p->abort();
}
ses->set_alert_notify([]{});
proxy = ses->abort();
ses.reset();
});
@ -157,11 +158,16 @@ TORRENT_TEST(optimistic_unchoke)
std::int64_t const duration_ms = lt::duration_cast<lt::milliseconds>(test_duration).count();
std::int64_t const average_unchoke_time = duration_ms / num_nodes;
printf("EXPECT: %" PRId64 " ms\n", average_unchoke_time);
for (auto const& cs : peer_choke_state)
for (auto& cs : peer_choke_state)
{
std::int64_t unchoke_duration = lt::duration_cast<lt::milliseconds>(cs.unchoke_duration).count();
if (!cs.choked)
{
cs.choked = true;
cs.unchoke_duration += lt::clock_type::now() - cs.last_unchoke;
}
std::int64_t const unchoke_duration = lt::duration_cast<lt::milliseconds>(cs.unchoke_duration).count();
printf("%" PRId64 " ms\n", unchoke_duration);
TEST_CHECK(std::abs(unchoke_duration - average_unchoke_time) < 1000);
TEST_CHECK(std::abs(unchoke_duration - average_unchoke_time) < 1500);
}
}

View File

@ -3773,6 +3773,13 @@ retry:
}
}
int session_impl::get_int_setting(int n) const
{
int const v = settings().get_int(n);
if (v < 0) return (std::numeric_limits<int>::max)();
return v;
}
void session_impl::recalculate_auto_managed_torrents()
{
INVARIANT_CHECK;
@ -3793,28 +3800,13 @@ retry:
// these counters are set to the number of torrents
// of each kind we're allowed to have active
int downloading_limit = settings().get_int(settings_pack::active_downloads);
int seeding_limit = settings().get_int(settings_pack::active_seeds);
int checking_limit = settings().get_int(settings_pack::active_checking);
int dht_limit = settings().get_int(settings_pack::active_dht_limit);
int tracker_limit = settings().get_int(settings_pack::active_tracker_limit);
int lsd_limit = settings().get_int(settings_pack::active_lsd_limit);
int hard_limit = settings().get_int(settings_pack::active_limit);
if (downloading_limit == -1)
downloading_limit = (std::numeric_limits<int>::max)();
if (seeding_limit == -1)
seeding_limit = (std::numeric_limits<int>::max)();
if (checking_limit == -1)
checking_limit = (std::numeric_limits<int>::max)();
if (hard_limit == -1)
hard_limit = (std::numeric_limits<int>::max)();
if (dht_limit == -1)
dht_limit = (std::numeric_limits<int>::max)();
if (lsd_limit == -1)
lsd_limit = (std::numeric_limits<int>::max)();
if (tracker_limit == -1)
tracker_limit = (std::numeric_limits<int>::max)();
int downloading_limit = get_int_setting(settings_pack::active_downloads);
int seeding_limit = get_int_setting(settings_pack::active_seeds);
int checking_limit = get_int_setting(settings_pack::active_checking);
int dht_limit = get_int_setting(settings_pack::active_dht_limit);
int tracker_limit = get_int_setting(settings_pack::active_tracker_limit);
int lsd_limit = get_int_setting(settings_pack::active_lsd_limit);
int hard_limit = get_int_setting(settings_pack::active_limit);
// if hard_limit is <= 0, all torrents in these lists should be paused.
// The order is not relevant
@ -3882,7 +3874,9 @@ retry:
// that are eligible for optimistic unchoke, similar to the torrents
// perhaps this could even iterate over the pool allocators of
// torrent_peer objects. It could probably be done in a single pass and
// collect the n best candidates
// collect the n best candidates. maybe just a queue of peers would make
// even more sense, just pick the next peer in the queue for unchoking. It
// would be O(1).
for (connection_map::iterator i = m_connections.begin()
, end(m_connections.end()); i != end; ++i)
{
@ -4171,8 +4165,8 @@ retry:
TORRENT_ASSERT(is_single_thread());
INVARIANT_CHECK;
time_point now = aux::time_now();
time_duration unchoke_interval = now - m_last_choke;
time_point const now = aux::time_now();
time_duration const unchoke_interval = now - m_last_choke;
m_last_choke = now;
// build list of all peers that are
@ -4186,8 +4180,8 @@ retry:
boost::shared_ptr<peer_connection> p = *i;
TORRENT_ASSERT(p);
++i;
torrent* t = p->associated_torrent().lock().get();
torrent_peer* pi = p->peer_info_struct();
torrent* const t = p->associated_torrent().lock().get();
torrent_peer* const pi = p->peer_info_struct();
if (p->ignore_unchoke_slots() || t == 0 || pi == 0
|| pi->web_seed || t->is_paused())
@ -4257,8 +4251,11 @@ retry:
, allowed_upload_slots);
#endif
int num_opt_unchoke = m_settings.get_int(settings_pack::num_optimistic_unchoke_slots);
if (num_opt_unchoke == 0) num_opt_unchoke = (std::max)(1, allowed_upload_slots / 5);
int const unchoked_counter_optimistic
= m_stats_counters[counters::num_peers_up_unchoked_optimistic];
int const num_opt_unchoke = (unchoked_counter_optimistic == 0)
? (std::max)(1, allowed_upload_slots / 5) : unchoked_counter_optimistic;
int unchoke_set_size = allowed_upload_slots - num_opt_unchoke;
// go through all the peers and unchoke the first ones and choke
@ -6190,10 +6187,7 @@ retry:
void session_impl::update_unchoke_limit()
{
int allowed_upload_slots = m_settings.get_int(settings_pack::unchoke_slots_limit);
if (allowed_upload_slots < 0)
allowed_upload_slots = (std::numeric_limits<int>::max)();
int const allowed_upload_slots = get_int_setting(settings_pack::unchoke_slots_limit);
m_stats_counters.set_value(counters::num_unchoke_slots
, allowed_upload_slots);
@ -7144,9 +7138,9 @@ retry:
settings_pack::num_optimistic_unchoke_slots));
}
int unchoked_counter_all = m_stats_counters[counters::num_peers_up_unchoked_all];
int unchoked_counter = m_stats_counters[counters::num_peers_up_unchoked];
int unchoked_counter_optimistic
int const unchoked_counter_all = m_stats_counters[counters::num_peers_up_unchoked_all];
int const unchoked_counter = m_stats_counters[counters::num_peers_up_unchoked];
int const unchoked_counter_optimistic
= m_stats_counters[counters::num_peers_up_unchoked_optimistic];
TORRENT_ASSERT_VAL(unchoked_counter_all == unchokes_all, unchokes_all);