ServerStat.cc 6.5 KB

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