BtLeecherStateChoke.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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() {}
  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)
  59. : peer_(c.peer_),
  60. downloadSpeed_(c.downloadSpeed_),
  61. regularUnchoker_(c.regularUnchoker_)
  62. {
  63. }
  64. void BtLeecherStateChoke::PeerEntry::swap(PeerEntry& c)
  65. {
  66. using std::swap;
  67. swap(peer_, c.peer_);
  68. swap(downloadSpeed_, c.downloadSpeed_);
  69. swap(regularUnchoker_, c.regularUnchoker_);
  70. }
  71. BtLeecherStateChoke::PeerEntry& BtLeecherStateChoke::PeerEntry::
  72. operator=(const PeerEntry& c)
  73. {
  74. if (this != &c) {
  75. peer_ = c.peer_;
  76. downloadSpeed_ = c.downloadSpeed_;
  77. regularUnchoker_ = c.regularUnchoker_;
  78. }
  79. return *this;
  80. }
  81. BtLeecherStateChoke::PeerEntry::~PeerEntry() {}
  82. const std::shared_ptr<Peer>& BtLeecherStateChoke::PeerEntry::getPeer() const
  83. {
  84. return peer_;
  85. }
  86. int BtLeecherStateChoke::PeerEntry::getDownloadSpeed() const
  87. {
  88. return downloadSpeed_;
  89. }
  90. bool BtLeecherStateChoke::PeerEntry::isRegularUnchoker() const
  91. {
  92. return regularUnchoker_;
  93. }
  94. void BtLeecherStateChoke::PeerEntry::enableChokingRequired()
  95. {
  96. peer_->chokingRequired(true);
  97. }
  98. void BtLeecherStateChoke::PeerEntry::disableChokingRequired()
  99. {
  100. peer_->chokingRequired(false);
  101. }
  102. void BtLeecherStateChoke::PeerEntry::enableOptUnchoking()
  103. {
  104. peer_->optUnchoking(true);
  105. }
  106. void BtLeecherStateChoke::PeerEntry::disableOptUnchoking()
  107. {
  108. peer_->optUnchoking(false);
  109. }
  110. bool BtLeecherStateChoke::PeerEntry::operator<(const PeerEntry& peerEntry) const
  111. {
  112. return downloadSpeed_ > peerEntry.downloadSpeed_;
  113. }
  114. void swap(BtLeecherStateChoke::PeerEntry& a, BtLeecherStateChoke::PeerEntry& b)
  115. {
  116. a.swap(b);
  117. }
  118. bool BtLeecherStateChoke::PeerFilter::
  119. operator()(const PeerEntry& peerEntry) const
  120. {
  121. return peerEntry.getPeer()->amChoking() == amChoking_ &&
  122. peerEntry.getPeer()->peerInterested() == peerInterested_;
  123. }
  124. void BtLeecherStateChoke::plannedOptimisticUnchoke(
  125. std::vector<PeerEntry>& peerEntries)
  126. {
  127. std::for_each(std::begin(peerEntries), std::end(peerEntries),
  128. std::mem_fn(&PeerEntry::disableOptUnchoking));
  129. auto i = std::partition(std::begin(peerEntries), std::end(peerEntries),
  130. PeerFilter(true, true));
  131. if (i != std::begin(peerEntries)) {
  132. std::shuffle(std::begin(peerEntries), i, *SimpleRandomizer::getInstance());
  133. (*std::begin(peerEntries)).enableOptUnchoking();
  134. A2_LOG_INFO(
  135. fmt("POU: %s",
  136. (*std::begin(peerEntries)).getPeer()->getIPAddress().c_str()));
  137. }
  138. }
  139. void BtLeecherStateChoke::regularUnchoke(std::vector<PeerEntry>& peerEntries)
  140. {
  141. auto rest = std::partition(std::begin(peerEntries), std::end(peerEntries),
  142. std::mem_fn(&PeerEntry::isRegularUnchoker));
  143. std::sort(std::begin(peerEntries), rest);
  144. // the number of regular unchokers
  145. int count = 3;
  146. bool fastOptUnchoker = false;
  147. auto peerIter = std::begin(peerEntries);
  148. for (; peerIter != rest && count; ++peerIter, --count) {
  149. peerIter->disableChokingRequired();
  150. A2_LOG_INFO(fmt("RU: %s, dlspd=%d",
  151. (*peerIter).getPeer()->getIPAddress().c_str(),
  152. (*peerIter).getDownloadSpeed()));
  153. if (peerIter->getPeer()->optUnchoking()) {
  154. fastOptUnchoker = true;
  155. peerIter->disableOptUnchoking();
  156. }
  157. }
  158. if (fastOptUnchoker) {
  159. std::shuffle(peerIter, std::end(peerEntries),
  160. *SimpleRandomizer::getInstance());
  161. for (auto& p : peerEntries) {
  162. if (p.getPeer()->peerInterested()) {
  163. p.enableOptUnchoking();
  164. A2_LOG_INFO(fmt("OU: %s", p.getPeer()->getIPAddress().c_str()));
  165. break;
  166. }
  167. else {
  168. p.disableChokingRequired();
  169. A2_LOG_INFO(fmt("OU: %s", p.getPeer()->getIPAddress().c_str()));
  170. }
  171. }
  172. }
  173. }
  174. void BtLeecherStateChoke::executeChoke(const PeerSet& peerSet)
  175. {
  176. A2_LOG_INFO(fmt("Leecher state, %d choke round started", round_));
  177. lastRound_ = global::wallclock();
  178. std::vector<PeerEntry> peerEntries;
  179. for (const auto& p : peerSet) {
  180. if (p->isActive() && !p->snubbing()) {
  181. p->chokingRequired(true);
  182. peerEntries.push_back(PeerEntry(p));
  183. }
  184. }
  185. // planned optimistic unchoke
  186. if (round_ == 0) {
  187. plannedOptimisticUnchoke(peerEntries);
  188. }
  189. regularUnchoke(peerEntries);
  190. if (++round_ == 3) {
  191. round_ = 0;
  192. }
  193. }
  194. const Timer& BtLeecherStateChoke::getLastRound() const { return lastRound_; }
  195. } // namespace aria2