BtSeederStateChoke.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 "BtSeederStateChoke.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. BtSeederStateChoke::BtSeederStateChoke() : round_(0), lastRound_(Timer::zero())
  45. {
  46. }
  47. BtSeederStateChoke::~BtSeederStateChoke() {}
  48. namespace {
  49. constexpr auto TIME_FRAME = 20_s;
  50. } // namespace
  51. BtSeederStateChoke::PeerEntry::PeerEntry(const std::shared_ptr<Peer>& peer)
  52. : peer_(peer),
  53. outstandingUpload_(peer->countOutstandingUpload()),
  54. lastAmUnchoking_(peer->getLastAmUnchoking()),
  55. recentUnchoking_(lastAmUnchoking_.difference(global::wallclock()) <
  56. TIME_FRAME),
  57. uploadSpeed_(peer->calculateUploadSpeed())
  58. {
  59. }
  60. BtSeederStateChoke::PeerEntry::PeerEntry(const PeerEntry& c)
  61. : peer_(c.peer_),
  62. outstandingUpload_(c.outstandingUpload_),
  63. lastAmUnchoking_(c.lastAmUnchoking_),
  64. recentUnchoking_(c.recentUnchoking_),
  65. uploadSpeed_(c.uploadSpeed_)
  66. {
  67. }
  68. BtSeederStateChoke::PeerEntry::~PeerEntry() {}
  69. void BtSeederStateChoke::PeerEntry::swap(PeerEntry& c)
  70. {
  71. using std::swap;
  72. swap(peer_, c.peer_);
  73. swap(outstandingUpload_, c.outstandingUpload_);
  74. swap(lastAmUnchoking_, c.lastAmUnchoking_);
  75. swap(recentUnchoking_, c.recentUnchoking_);
  76. swap(uploadSpeed_, c.uploadSpeed_);
  77. }
  78. BtSeederStateChoke::PeerEntry& BtSeederStateChoke::PeerEntry::
  79. operator=(const PeerEntry& c)
  80. {
  81. if (this != &c) {
  82. peer_ = c.peer_;
  83. outstandingUpload_ = c.outstandingUpload_;
  84. lastAmUnchoking_ = c.lastAmUnchoking_;
  85. recentUnchoking_ = c.recentUnchoking_;
  86. uploadSpeed_ = c.uploadSpeed_;
  87. }
  88. return *this;
  89. }
  90. bool BtSeederStateChoke::PeerEntry::operator<(const PeerEntry& rhs) const
  91. {
  92. if (this->outstandingUpload_ && !rhs.outstandingUpload_) {
  93. return true;
  94. }
  95. else if (!this->outstandingUpload_ && rhs.outstandingUpload_) {
  96. return false;
  97. }
  98. if (this->recentUnchoking_ &&
  99. (this->lastAmUnchoking_ > rhs.lastAmUnchoking_)) {
  100. return true;
  101. }
  102. else if (rhs.recentUnchoking_) {
  103. return false;
  104. }
  105. else {
  106. return this->uploadSpeed_ > rhs.uploadSpeed_;
  107. }
  108. }
  109. void BtSeederStateChoke::PeerEntry::disableOptUnchoking()
  110. {
  111. peer_->optUnchoking(false);
  112. }
  113. void BtSeederStateChoke::unchoke(
  114. std::vector<BtSeederStateChoke::PeerEntry>& peers)
  115. {
  116. int count = (round_ == 2) ? 4 : 3;
  117. std::sort(std::begin(peers), std::end(peers));
  118. auto r = std::begin(peers);
  119. for (; r != std::end(peers) && count; ++r, --count) {
  120. auto& peer = (*r).getPeer();
  121. peer->chokingRequired(false);
  122. A2_LOG_INFO(fmt("RU: %s:%u, ulspd=%d", peer->getIPAddress().c_str(),
  123. peer->getPort(), (*r).getUploadSpeed()));
  124. }
  125. if (round_ < 2) {
  126. std::for_each(std::begin(peers), std::end(peers),
  127. std::mem_fn(&PeerEntry::disableOptUnchoking));
  128. if (r != std::end(peers)) {
  129. std::shuffle(r, std::end(peers), *SimpleRandomizer::getInstance());
  130. auto& peer = (*r).getPeer();
  131. peer->optUnchoking(true);
  132. A2_LOG_INFO(
  133. fmt("POU: %s:%u", peer->getIPAddress().c_str(), peer->getPort()));
  134. }
  135. }
  136. }
  137. void BtSeederStateChoke::executeChoke(const PeerSet& peerSet)
  138. {
  139. A2_LOG_INFO(fmt("Seeder state, %d choke round started", round_));
  140. lastRound_ = global::wallclock();
  141. std::vector<PeerEntry> peerEntries;
  142. for (const auto& p : peerSet) {
  143. if (!p->isActive()) {
  144. continue;
  145. }
  146. p->chokingRequired(true);
  147. if (p->peerInterested()) {
  148. peerEntries.push_back(PeerEntry(p));
  149. continue;
  150. }
  151. p->optUnchoking(false);
  152. }
  153. unchoke(peerEntries);
  154. if (++round_ == 3) {
  155. round_ = 0;
  156. }
  157. }
  158. void swap(BtSeederStateChoke::PeerEntry& a, BtSeederStateChoke::PeerEntry& b)
  159. {
  160. a.swap(b);
  161. }
  162. } // namespace aria2