ServerStat.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 "ServerStat.h"
  36. #include <ostream>
  37. #include <algorithm>
  38. #include "array_fun.h"
  39. #include "Logger.h"
  40. #include "LogFactory.h"
  41. #include "fmt.h"
  42. #include "a2functional.h"
  43. #include "util.h"
  44. namespace aria2 {
  45. namespace {
  46. const char* STATUS_STRING[] = {"OK", "ERROR"};
  47. } // namespace
  48. ServerStat::ServerStat(const std::string& hostname, const std::string& protocol)
  49. : hostname_(hostname),
  50. protocol_(protocol),
  51. downloadSpeed_(0),
  52. singleConnectionAvgSpeed_(0),
  53. multiConnectionAvgSpeed_(0),
  54. counter_(0),
  55. status_(OK)
  56. {
  57. }
  58. ServerStat::~ServerStat() {}
  59. void ServerStat::setLastUpdated(const Time& time) { lastUpdated_ = time; }
  60. void ServerStat::setDownloadSpeed(int downloadSpeed)
  61. {
  62. downloadSpeed_ = downloadSpeed;
  63. }
  64. void ServerStat::updateDownloadSpeed(int downloadSpeed)
  65. {
  66. downloadSpeed_ = downloadSpeed;
  67. if (downloadSpeed > 0) {
  68. status_ = OK;
  69. }
  70. lastUpdated_.reset();
  71. }
  72. void ServerStat::setSingleConnectionAvgSpeed(int singleConnectionAvgSpeed)
  73. {
  74. singleConnectionAvgSpeed_ = singleConnectionAvgSpeed;
  75. }
  76. void ServerStat::updateSingleConnectionAvgSpeed(int downloadSpeed)
  77. {
  78. float avgDownloadSpeed;
  79. if (counter_ == 0)
  80. return;
  81. if (counter_ < 5) {
  82. avgDownloadSpeed = ((((float)counter_ - 1) / (float)counter_) *
  83. (float)singleConnectionAvgSpeed_) +
  84. ((1.0 / (float)counter_) * (float)downloadSpeed);
  85. }
  86. else {
  87. avgDownloadSpeed = ((4.0 / 5.0) * (float)singleConnectionAvgSpeed_) +
  88. ((1.0 / 5.0) * (float)downloadSpeed);
  89. }
  90. if (avgDownloadSpeed < (int)(0.80 * singleConnectionAvgSpeed_)) {
  91. A2_LOG_DEBUG(fmt("ServerStat:%s: resetting counter since single connection"
  92. " speed dropped",
  93. getHostname().c_str()));
  94. counter_ = 0;
  95. }
  96. A2_LOG_DEBUG(
  97. fmt("ServerStat:%s: singleConnectionAvgSpeed_ old:%.2fKB/s"
  98. " new:%.2fKB/s last:%.2fKB/s",
  99. getHostname().c_str(), (float)singleConnectionAvgSpeed_ / 1024,
  100. (float)avgDownloadSpeed / 1024, (float)downloadSpeed / 1024));
  101. singleConnectionAvgSpeed_ = (int)avgDownloadSpeed;
  102. }
  103. void ServerStat::setMultiConnectionAvgSpeed(int multiConnectionAvgSpeed)
  104. {
  105. multiConnectionAvgSpeed_ = multiConnectionAvgSpeed;
  106. }
  107. void ServerStat::updateMultiConnectionAvgSpeed(int downloadSpeed)
  108. {
  109. float avgDownloadSpeed;
  110. if (counter_ == 0)
  111. return;
  112. if (counter_ < 5) {
  113. avgDownloadSpeed = ((((float)counter_ - 1) / (float)counter_) *
  114. (float)multiConnectionAvgSpeed_) +
  115. ((1.0 / (float)counter_) * (float)downloadSpeed);
  116. }
  117. else {
  118. avgDownloadSpeed = ((4.0 / 5.0) * (float)multiConnectionAvgSpeed_) +
  119. ((1.0 / 5.0) * (float)downloadSpeed);
  120. }
  121. A2_LOG_DEBUG(
  122. fmt("ServerStat:%s: multiConnectionAvgSpeed_ old:%.2fKB/s"
  123. " new:%.2fKB/s last:%.2fKB/s",
  124. getHostname().c_str(), (float)multiConnectionAvgSpeed_ / 1024,
  125. (float)avgDownloadSpeed / 1024, (float)downloadSpeed / 1024));
  126. multiConnectionAvgSpeed_ = (int)avgDownloadSpeed;
  127. }
  128. void ServerStat::increaseCounter() { ++counter_; }
  129. void ServerStat::setCounter(int value) { counter_ = value; }
  130. void ServerStat::setStatus(STATUS status) { status_ = status; }
  131. void ServerStat::setStatus(const std::string& status)
  132. {
  133. for (int i = 0; i < MAX_STATUS; ++i) {
  134. if (strcmp(status.c_str(), STATUS_STRING[i]) == 0) {
  135. status_ = static_cast<STATUS>(i);
  136. break;
  137. }
  138. }
  139. }
  140. void ServerStat::setStatusInternal(STATUS status)
  141. {
  142. A2_LOG_DEBUG(fmt("ServerStat: set status %s for %s (%s)",
  143. STATUS_STRING[status], hostname_.c_str(),
  144. protocol_.c_str()));
  145. status_ = status;
  146. lastUpdated_.reset();
  147. }
  148. void ServerStat::setOK() { setStatusInternal(OK); }
  149. void ServerStat::setError() { setStatusInternal(A2_ERROR); }
  150. bool ServerStat::operator<(const ServerStat& serverStat) const
  151. {
  152. return hostname_ < serverStat.hostname_ ||
  153. (hostname_ == serverStat.hostname_ &&
  154. protocol_ < serverStat.protocol_);
  155. }
  156. bool ServerStat::operator==(const ServerStat& serverStat) const
  157. {
  158. return hostname_ == serverStat.hostname_ && protocol_ == serverStat.protocol_;
  159. }
  160. std::string ServerStat::toString() const
  161. {
  162. return fmt("host=%s, protocol=%s, dl_speed=%d, sc_avg_speed=%d,"
  163. " mc_avg_speed=%d, last_updated=%ld, counter=%d, status=%s",
  164. getHostname().c_str(), getProtocol().c_str(), getDownloadSpeed(),
  165. getSingleConnectionAvgSpeed(), getMultiConnectionAvgSpeed(),
  166. getLastUpdated().getTimeFromEpoch(), getCounter(),
  167. STATUS_STRING[getStatus()]);
  168. }
  169. } // namespace aria2