ServerStat.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 "LogFactory.h"
  40. namespace aria2 {
  41. const std::string ServerStat::STATUS_STRING[] = {
  42. "OK",
  43. "ERROR"
  44. };
  45. ServerStat::ServerStat(const std::string& hostname, const std::string& protocol)
  46. :
  47. hostname_(hostname),
  48. protocol_(protocol),
  49. downloadSpeed_(0),
  50. singleConnectionAvgSpeed_(0),
  51. multiConnectionAvgSpeed_(0),
  52. counter_(0),
  53. logger_(LogFactory::getInstance()),
  54. status_(OK)
  55. {}
  56. ServerStat::~ServerStat() {}
  57. void ServerStat::setLastUpdated(const Time& time)
  58. {
  59. lastUpdated_ = time;
  60. }
  61. void ServerStat::setDownloadSpeed(unsigned int downloadSpeed)
  62. {
  63. downloadSpeed_ = downloadSpeed;
  64. }
  65. void ServerStat::updateDownloadSpeed(unsigned int downloadSpeed)
  66. {
  67. downloadSpeed_ = downloadSpeed;
  68. if(downloadSpeed > 0) {
  69. status_ = OK;
  70. }
  71. lastUpdated_.reset();
  72. }
  73. void ServerStat::setSingleConnectionAvgSpeed
  74. (unsigned int singleConnectionAvgSpeed)
  75. {
  76. singleConnectionAvgSpeed_ = singleConnectionAvgSpeed;
  77. }
  78. void ServerStat::updateSingleConnectionAvgSpeed(unsigned int downloadSpeed)
  79. {
  80. float avgDownloadSpeed;
  81. if(counter_ == 0)
  82. return;
  83. if(counter_ < 5) {
  84. avgDownloadSpeed =
  85. ((((float)counter_-1)/(float)counter_)*(float)singleConnectionAvgSpeed_)+
  86. ((1.0/(float)counter_)*(float)downloadSpeed);
  87. }
  88. else {
  89. avgDownloadSpeed = ((4.0/5.0)*(float)singleConnectionAvgSpeed_) +
  90. ((1.0/5.0)*(float)downloadSpeed);
  91. }
  92. if(avgDownloadSpeed < (int)(0.80*singleConnectionAvgSpeed_)) {
  93. if(logger_->debug()) {
  94. logger_->debug("ServerStat:%s: resetting counter since single connection"
  95. " speed dropped", getHostname().c_str());
  96. }
  97. counter_ = 0;
  98. }
  99. if(logger_->debug()) {
  100. logger_->debug("ServerStat:%s: singleConnectionAvgSpeed_ old:%.2fKB/s"
  101. " new:%.2fKB/s last:%.2fKB/s",
  102. getHostname().c_str(),
  103. (float) singleConnectionAvgSpeed_/1024,
  104. (float) avgDownloadSpeed/1024,
  105. (float) downloadSpeed / 1024);
  106. }
  107. singleConnectionAvgSpeed_ = (int)avgDownloadSpeed;
  108. }
  109. void ServerStat::setMultiConnectionAvgSpeed
  110. (unsigned int multiConnectionAvgSpeed)
  111. {
  112. multiConnectionAvgSpeed_ = multiConnectionAvgSpeed;
  113. }
  114. void ServerStat::updateMultiConnectionAvgSpeed(unsigned int downloadSpeed)
  115. {
  116. float avgDownloadSpeed;
  117. if(counter_ == 0)
  118. return;
  119. if(counter_ < 5) {
  120. avgDownloadSpeed =
  121. ((((float)counter_-1)/(float)counter_)*(float)multiConnectionAvgSpeed_) +
  122. ((1.0/(float)counter_)*(float)downloadSpeed);
  123. }
  124. else {
  125. avgDownloadSpeed = ((4.0/5.0)*(float)multiConnectionAvgSpeed_) +
  126. ((1.0/5.0)*(float)downloadSpeed);
  127. }
  128. if(logger_->debug()) {
  129. logger_->debug("ServerStat:%s: multiConnectionAvgSpeed_ old:%.2fKB/s"
  130. " new:%.2fKB/s last:%.2fKB/s",
  131. getHostname().c_str(),
  132. (float) multiConnectionAvgSpeed_/1024,
  133. (float) avgDownloadSpeed/1024,
  134. (float) downloadSpeed / 1024);
  135. }
  136. multiConnectionAvgSpeed_ = (int)avgDownloadSpeed;
  137. }
  138. void ServerStat::increaseCounter()
  139. {
  140. ++counter_;
  141. }
  142. void ServerStat::setCounter(unsigned int value)
  143. {
  144. counter_ = value;
  145. }
  146. void ServerStat::setStatus(STATUS status)
  147. {
  148. status_ = status;
  149. }
  150. void ServerStat::setStatus(const std::string& status)
  151. {
  152. const std::string* p = std::find(vbegin(STATUS_STRING), vend(STATUS_STRING),
  153. status);
  154. if(p != vend(STATUS_STRING)) {
  155. status_ = static_cast<STATUS>(ServerStat::OK+
  156. std::distance(vbegin(STATUS_STRING), p));
  157. }
  158. }
  159. void ServerStat::setStatusInternal(STATUS status)
  160. {
  161. if(logger_->debug()) {
  162. logger_->debug("ServerStat: set status %s for %s (%s)",
  163. STATUS_STRING[status].c_str(),
  164. hostname_.c_str(), protocol_.c_str());
  165. }
  166. status_ = status;
  167. lastUpdated_.reset();
  168. }
  169. void ServerStat::setOK()
  170. {
  171. setStatusInternal(OK);
  172. }
  173. void ServerStat::setError()
  174. {
  175. setStatusInternal(ERROR);
  176. }
  177. bool ServerStat::operator<(const ServerStat& serverStat) const
  178. {
  179. int c = hostname_.compare(serverStat.hostname_);
  180. if(c == 0) {
  181. return protocol_ < serverStat.protocol_;
  182. } else {
  183. return c < 0;
  184. }
  185. }
  186. bool ServerStat::operator==(const ServerStat& serverStat) const
  187. {
  188. return hostname_ == serverStat.hostname_ && protocol_ == serverStat.protocol_;
  189. }
  190. std::ostream& operator<<(std::ostream& o, const ServerStat& serverStat)
  191. {
  192. o << "host=" << serverStat.getHostname() << ", "
  193. << "protocol=" << serverStat.getProtocol() << ", "
  194. << "dl_speed=" << serverStat.getDownloadSpeed() << ", "
  195. << "sc_avg_speed=" << serverStat.getSingleConnectionAvgSpeed() << ", "
  196. << "mc_avg_speed=" << serverStat.getMultiConnectionAvgSpeed() << ", "
  197. << "last_updated=" << serverStat.getLastUpdated().getTime() << ", "
  198. << "counter=" << serverStat.getCounter() << ", "
  199. << "status=" << ServerStat::STATUS_STRING[serverStat.getStatus()];
  200. return o;
  201. }
  202. } // namespace aria2