BtLeecherStateChoke.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "BtLeecherStateChoke.h"
  36. #include <algorithm>
  37. #include "Peer.h"
  38. #include "Logger.h"
  39. #include "LogFactory.h"
  40. #include "SimpleRandomizer.h"
  41. #include "wallclock.h"
  42. #include "fmt.h"
  43. namespace aria2 {
  44. BtLeecherStateChoke::BtLeecherStateChoke()
  45. : round_(0), lastRound_(Timer::zero())
  46. {
  47. }
  48. BtLeecherStateChoke::~BtLeecherStateChoke() = default;
  49. BtLeecherStateChoke::PeerEntry::PeerEntry(const std::shared_ptr<Peer>& peer)
  50. : peer_(peer),
  51. downloadSpeed_(peer->calculateDownloadSpeed()),
  52. // peer must be interested to us and sent block in the last 30 seconds
  53. regularUnchoker_(
  54. peer->peerInterested() &&
  55. peer->getLastDownloadUpdate().difference(global::wallclock()) < 30_s)
  56. {
  57. }
  58. BtLeecherStateChoke::PeerEntry::PeerEntry(const PeerEntry& c) = default;
  59. void BtLeecherStateChoke::PeerEntry::swap(PeerEntry& c)
  60. {
  61. using std::swap;
  62. swap(peer_, c.peer_);
  63. swap(downloadSpeed_, c.downloadSpeed_);
  64. swap(regularUnchoker_, c.regularUnchoker_);
  65. }
  66. BtLeecherStateChoke::PeerEntry& BtLeecherStateChoke::PeerEntry::
  67. operator=(const PeerEntry& c)
  68. {
  69. if (this != &c) {
  70. peer_ = c.peer_;
  71. downloadSpeed_ = c.downloadSpeed_;
  72. regularUnchoker_ = c.regularUnchoker_;
  73. }
  74. return *this;
  75. }
  76. BtLeecherStateChoke::PeerEntry::~PeerEntry() = default;
  77. const std::shared_ptr<Peer>& BtLeecherStateChoke::PeerEntry::getPeer() const
  78. {
  79. return peer_;
  80. }
  81. int BtLeecherStateChoke::PeerEntry::getDownloadSpeed() const
  82. {
  83. return downloadSpeed_;
  84. }
  85. bool BtLeecherStateChoke::PeerEntry::isRegularUnchoker() const
  86. {
  87. return regularUnchoker_;
  88. }
  89. void BtLeecherStateChoke::PeerEntry::enableChokingRequired()
  90. {
  91. peer_->chokingRequired(true);
  92. }
  93. void BtLeecherStateChoke::PeerEntry::disableChokingRequired()
  94. {
  95. peer_->chokingRequired(false);
  96. }
  97. void BtLeecherStateChoke::PeerEntry::enableOptUnchoking()
  98. {
  99. peer_->optUnchoking(true);
  100. }
  101. void BtLeecherStateChoke::PeerEntry::disableOptUnchoking()
  102. {
  103. peer_->optUnchoking(false);
  104. }
  105. bool BtLeecherStateChoke::PeerEntry::operator<(const PeerEntry& peerEntry) const
  106. {
  107. return downloadSpeed_ > peerEntry.downloadSpeed_;
  108. }
  109. void swap(BtLeecherStateChoke::PeerEntry& a, BtLeecherStateChoke::PeerEntry& b)
  110. {
  111. a.swap(b);
  112. }
  113. bool BtLeecherStateChoke::PeerFilter::
  114. operator()(const PeerEntry& peerEntry) const
  115. {
  116. return peerEntry.getPeer()->amChoking() == amChoking_ &&
  117. peerEntry.getPeer()->peerInterested() == peerInterested_;
  118. }
  119. void BtLeecherStateChoke::plannedOptimisticUnchoke(
  120. std::vector<PeerEntry>& peerEntries)
  121. {
  122. std::for_each(std::begin(peerEntries), std::end(peerEntries),
  123. std::mem_fn(&PeerEntry::disableOptUnchoking));
  124. auto i = std::partition(std::begin(peerEntries), std::end(peerEntries),
  125. PeerFilter(true, true));
  126. if (i != std::begin(peerEntries)) {
  127. std::shuffle(std::begin(peerEntries), i, *SimpleRandomizer::getInstance());
  128. auto& ent = *std::begin(peerEntries);
  129. auto& peer = ent.getPeer();
  130. ent.enableOptUnchoking();
  131. A2_LOG_INFO(
  132. fmt("POU: %s:%u", peer->getIPAddress().c_str(), peer->getPort()));
  133. }
  134. }
  135. void BtLeecherStateChoke::regularUnchoke(std::vector<PeerEntry>& peerEntries)
  136. {
  137. auto rest = std::partition(std::begin(peerEntries), std::end(peerEntries),
  138. std::mem_fn(&PeerEntry::isRegularUnchoker));
  139. std::sort(std::begin(peerEntries), rest);
  140. std::shuffle(rest, std::end(peerEntries), *SimpleRandomizer::getInstance());
  141. // the number of regular unchokers
  142. int count = 3;
  143. bool fastOptUnchoker = false;
  144. auto peerIter = std::begin(peerEntries);
  145. for (; peerIter != std::end(peerEntries) && count; ++peerIter, --count) {
  146. auto& peer = peerIter->getPeer();
  147. if (!peer->peerInterested()) {
  148. continue;
  149. }
  150. peerIter->disableChokingRequired();
  151. A2_LOG_INFO(fmt("RU: %s:%u, dlspd=%d", peer->getIPAddress().c_str(),
  152. peer->getPort(), (*peerIter).getDownloadSpeed()));
  153. if (peer->optUnchoking()) {
  154. fastOptUnchoker = true;
  155. peerIter->disableOptUnchoking();
  156. }
  157. }
  158. if (fastOptUnchoker) {
  159. for (auto& p : peerEntries) {
  160. if (!p.getPeer()->peerInterested()) {
  161. continue;
  162. }
  163. p.enableOptUnchoking();
  164. auto& peer = p.getPeer();
  165. A2_LOG_INFO(
  166. fmt("OU: %s:%u", peer->getIPAddress().c_str(), peer->getPort()));
  167. break;
  168. }
  169. }
  170. }
  171. void BtLeecherStateChoke::executeChoke(const PeerSet& peerSet)
  172. {
  173. A2_LOG_INFO(fmt("Leecher state, %d choke round started", round_));
  174. lastRound_ = global::wallclock();
  175. std::vector<PeerEntry> peerEntries;
  176. for (const auto& p : peerSet) {
  177. if (!p->isActive()) {
  178. continue;
  179. }
  180. p->chokingRequired(true);
  181. if (p->snubbing()) {
  182. p->optUnchoking(false);
  183. continue;
  184. }
  185. peerEntries.push_back(PeerEntry(p));
  186. }
  187. // planned optimistic unchoke
  188. if (round_ == 0) {
  189. plannedOptimisticUnchoke(peerEntries);
  190. }
  191. regularUnchoke(peerEntries);
  192. if (++round_ == 3) {
  193. round_ = 0;
  194. }
  195. }
  196. const Timer& BtLeecherStateChoke::getLastRound() const { return lastRound_; }
  197. } // namespace aria2