ServerStat.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. namespace aria2 {
  43. const std::string ServerStat::STATUS_STRING[] = {
  44. "OK",
  45. "ERROR"
  46. };
  47. ServerStat::ServerStat(const std::string& hostname, const std::string& protocol)
  48. : hostname_(hostname),
  49. protocol_(protocol),
  50. downloadSpeed_(0),
  51. singleConnectionAvgSpeed_(0),
  52. multiConnectionAvgSpeed_(0),
  53. counter_(0),
  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. A2_LOG_DEBUG(fmt("ServerStat:%s: resetting counter since single connection"
  94. " speed dropped",
  95. getHostname().c_str()));
  96. counter_ = 0;
  97. }
  98. A2_LOG_DEBUG(fmt("ServerStat:%s: singleConnectionAvgSpeed_ old:%.2fKB/s"
  99. " new:%.2fKB/s last:%.2fKB/s",
  100. getHostname().c_str(),
  101. (float) singleConnectionAvgSpeed_/1024,
  102. (float) avgDownloadSpeed/1024,
  103. (float) downloadSpeed / 1024));
  104. singleConnectionAvgSpeed_ = (int)avgDownloadSpeed;
  105. }
  106. void ServerStat::setMultiConnectionAvgSpeed
  107. (unsigned int multiConnectionAvgSpeed)
  108. {
  109. multiConnectionAvgSpeed_ = multiConnectionAvgSpeed;
  110. }
  111. void ServerStat::updateMultiConnectionAvgSpeed(unsigned int downloadSpeed)
  112. {
  113. float avgDownloadSpeed;
  114. if(counter_ == 0)
  115. return;
  116. if(counter_ < 5) {
  117. avgDownloadSpeed =
  118. ((((float)counter_-1)/(float)counter_)*(float)multiConnectionAvgSpeed_) +
  119. ((1.0/(float)counter_)*(float)downloadSpeed);
  120. }
  121. else {
  122. avgDownloadSpeed = ((4.0/5.0)*(float)multiConnectionAvgSpeed_) +
  123. ((1.0/5.0)*(float)downloadSpeed);
  124. }
  125. A2_LOG_DEBUG(fmt("ServerStat:%s: multiConnectionAvgSpeed_ old:%.2fKB/s"
  126. " new:%.2fKB/s last:%.2fKB/s",
  127. getHostname().c_str(),
  128. (float) multiConnectionAvgSpeed_/1024,
  129. (float) avgDownloadSpeed/1024,
  130. (float) downloadSpeed / 1024));
  131. multiConnectionAvgSpeed_ = (int)avgDownloadSpeed;
  132. }
  133. void ServerStat::increaseCounter()
  134. {
  135. ++counter_;
  136. }
  137. void ServerStat::setCounter(unsigned int value)
  138. {
  139. counter_ = value;
  140. }
  141. void ServerStat::setStatus(STATUS status)
  142. {
  143. status_ = status;
  144. }
  145. void ServerStat::setStatus(const std::string& status)
  146. {
  147. const std::string* p = std::find(vbegin(STATUS_STRING), vend(STATUS_STRING),
  148. status);
  149. if(p != vend(STATUS_STRING)) {
  150. status_ = static_cast<STATUS>(ServerStat::OK+
  151. std::distance(vbegin(STATUS_STRING), p));
  152. }
  153. }
  154. void ServerStat::setStatusInternal(STATUS status)
  155. {
  156. A2_LOG_DEBUG(fmt("ServerStat: set status %s for %s (%s)",
  157. STATUS_STRING[status].c_str(),
  158. hostname_.c_str(),
  159. protocol_.c_str()));
  160. status_ = status;
  161. lastUpdated_.reset();
  162. }
  163. void ServerStat::setOK()
  164. {
  165. setStatusInternal(OK);
  166. }
  167. void ServerStat::setError()
  168. {
  169. setStatusInternal(ERROR);
  170. }
  171. bool ServerStat::operator<(const ServerStat& serverStat) const
  172. {
  173. int c = hostname_.compare(serverStat.hostname_);
  174. if(c == 0) {
  175. return protocol_ < serverStat.protocol_;
  176. } else {
  177. return c < 0;
  178. }
  179. }
  180. bool ServerStat::operator==(const ServerStat& serverStat) const
  181. {
  182. return hostname_ == serverStat.hostname_ && protocol_ == serverStat.protocol_;
  183. }
  184. std::ostream& operator<<(std::ostream& o, const ServerStat& serverStat)
  185. {
  186. o << "host=" << serverStat.getHostname() << ", "
  187. << "protocol=" << serverStat.getProtocol() << ", "
  188. << "dl_speed=" << serverStat.getDownloadSpeed() << ", "
  189. << "sc_avg_speed=" << serverStat.getSingleConnectionAvgSpeed() << ", "
  190. << "mc_avg_speed=" << serverStat.getMultiConnectionAvgSpeed() << ", "
  191. << "last_updated=" << serverStat.getLastUpdated().getTime() << ", "
  192. << "counter=" << serverStat.getCounter() << ", "
  193. << "status=" << ServerStat::STATUS_STRING[serverStat.getStatus()];
  194. return o;
  195. }
  196. } // namespace aria2