ConsoleStatCalc.cc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 "ConsoleStatCalc.h"
  36. #ifdef HAVE_TERMIOS_H
  37. #include <termios.h>
  38. #endif // HAVE_TERMIOS_H
  39. #ifdef HAVE_SYS_IOCTL_H
  40. #include <sys/ioctl.h>
  41. #endif // HAVE_SYS_IOCTL_H
  42. #include <unistd.h>
  43. #include <cstdio>
  44. #include <iomanip>
  45. #include <iostream>
  46. #include <algorithm>
  47. #include <cstring>
  48. #include <sstream>
  49. #include <iterator>
  50. #include "DownloadEngine.h"
  51. #include "RequestGroupMan.h"
  52. #include "RequestGroup.h"
  53. #include "FileAllocationMan.h"
  54. #include "FileAllocationEntry.h"
  55. #include "CheckIntegrityMan.h"
  56. #include "CheckIntegrityEntry.h"
  57. #include "util.h"
  58. #include "DownloadContext.h"
  59. #include "wallclock.h"
  60. #include "ServerStatMan.h"
  61. #ifdef ENABLE_BITTORRENT
  62. # include "bittorrent_helper.h"
  63. # include "Peer.h"
  64. # include "PeerStorage.h"
  65. # include "BtRegistry.h"
  66. # include "BtProgressInfoFile.h"
  67. # include "BtRuntime.h"
  68. # include "BtAnnounce.h"
  69. # include "PieceStorage.h"
  70. #endif // ENABLE_BITTORRENT
  71. namespace aria2 {
  72. static void printProgress
  73. (std::ostream& o, const SharedHandle<RequestGroup>& rg, const DownloadEngine* e,
  74. const SizeFormatter& sizeFormatter)
  75. {
  76. TransferStat stat = rg->calculateStat();
  77. unsigned int eta = 0;
  78. if(rg->getTotalLength() > 0 && stat.getDownloadSpeed() > 0) {
  79. eta = (rg->getTotalLength()-rg->getCompletedLength())/stat.getDownloadSpeed();
  80. }
  81. o << "["
  82. << "#" << rg->getGID() << " ";
  83. #ifdef ENABLE_BITTORRENT
  84. if(rg->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT) &&
  85. !bittorrent::getTorrentAttrs(rg->getDownloadContext())->metadata.empty() &&
  86. rg->downloadFinished()) {
  87. o << "SEEDING" << "(" << "ratio:";
  88. if(rg->getCompletedLength() > 0) {
  89. o << std::fixed << std::setprecision(1)
  90. << ((stat.getAllTimeUploadLength()*10)/rg->getCompletedLength())/10.0;
  91. } else {
  92. o << "--";
  93. }
  94. o << ")";
  95. } else
  96. #endif // ENABLE_BITTORRENT
  97. {
  98. o << "SIZE:"
  99. << sizeFormatter(rg->getCompletedLength())
  100. << "B"
  101. << "/"
  102. << sizeFormatter(rg->getTotalLength())
  103. << "B";
  104. if(rg->getTotalLength() > 0) {
  105. o << "("
  106. << 100*rg->getCompletedLength()/rg->getTotalLength()
  107. << "%)";
  108. }
  109. }
  110. o << " "
  111. << "CN:"
  112. << rg->getNumConnection();
  113. #ifdef ENABLE_BITTORRENT
  114. SharedHandle<PeerStorage> ps =
  115. e->getBtRegistry()->get(rg->getGID()).peerStorage_;
  116. if(!ps.isNull()) {
  117. std::vector<SharedHandle<Peer> > peers;
  118. ps->getActivePeers(peers);
  119. o << " " << "SEED:"
  120. << countSeeder(peers.begin(), peers.end());
  121. }
  122. #endif // ENABLE_BITTORRENT
  123. if(!rg->downloadFinished()) {
  124. o << " "
  125. << "SPD:"
  126. << sizeFormatter(stat.getDownloadSpeed()) << "Bs";
  127. }
  128. if(stat.getSessionUploadLength() > 0) {
  129. o << " "
  130. << "UP:"
  131. << sizeFormatter(stat.getUploadSpeed()) << "Bs"
  132. << "(" << sizeFormatter(stat.getAllTimeUploadLength()) << "B)";
  133. }
  134. if(eta > 0) {
  135. o << " "
  136. << "ETA:"
  137. << util::secfmt(eta);
  138. }
  139. o << "]";
  140. }
  141. namespace {
  142. class PrintSummary
  143. {
  144. private:
  145. size_t cols_;
  146. const DownloadEngine* e_;
  147. const SizeFormatter& sizeFormatter_;
  148. public:
  149. PrintSummary
  150. (size_t cols, const DownloadEngine* e,
  151. const SizeFormatter& sizeFormatter):
  152. cols_(cols), e_(e), sizeFormatter_(sizeFormatter) {}
  153. void operator()(const SharedHandle<RequestGroup>& rg)
  154. {
  155. const char SEP_CHAR = '-';
  156. printProgress(std::cout, rg, e_, sizeFormatter_);
  157. const std::vector<SharedHandle<FileEntry> >& fileEntries =
  158. rg->getDownloadContext()->getFileEntries();
  159. std::cout << "\n"
  160. << "FILE: ";
  161. writeFilePath(fileEntries.begin(), fileEntries.end(),
  162. std::cout, rg->inMemoryDownload());
  163. std::cout << "\n"
  164. << std::setfill(SEP_CHAR) << std::setw(cols_) << SEP_CHAR << "\n";
  165. }
  166. };
  167. }
  168. static void printProgressSummary
  169. (const std::deque<SharedHandle<RequestGroup> >& groups, size_t cols,
  170. const DownloadEngine* e,
  171. const SizeFormatter& sizeFormatter)
  172. {
  173. const char SEP_CHAR = '=';
  174. time_t now;
  175. time(&now);
  176. std::cout << " *** Download Progress Summary";
  177. {
  178. time_t now;
  179. struct tm* staticNowtmPtr;
  180. char buf[26];
  181. if(time(&now) != (time_t)-1 && (staticNowtmPtr = localtime(&now)) != 0 &&
  182. asctime_r(staticNowtmPtr, buf) != 0) {
  183. char* lfptr = strchr(buf, '\n');
  184. if(lfptr) {
  185. *lfptr = '\0';
  186. }
  187. std::cout << " as of " << buf;
  188. }
  189. }
  190. std::cout << " *** " << "\n"
  191. << std::setfill(SEP_CHAR) << std::setw(cols) << SEP_CHAR << "\n";
  192. std::for_each(groups.begin(), groups.end(),
  193. PrintSummary(cols, e, sizeFormatter));
  194. }
  195. ConsoleStatCalc::ConsoleStatCalc(time_t summaryInterval, bool humanReadable):
  196. summaryInterval_(summaryInterval)
  197. {
  198. if(humanReadable) {
  199. sizeFormatter_.reset(new AbbrevSizeFormatter());
  200. } else {
  201. sizeFormatter_.reset(new PlainSizeFormatter());
  202. }
  203. }
  204. void
  205. ConsoleStatCalc::calculateStat(const DownloadEngine* e)
  206. {
  207. if(cp_.differenceInMillis(global::wallclock) < 900) {
  208. return;
  209. }
  210. cp_ = global::wallclock;
  211. const SizeFormatter& sizeFormatter = *sizeFormatter_.get();
  212. #ifdef __MINGW32__
  213. bool isTTY = true;
  214. // Windows terminal cannot handle at the end of line properly.
  215. unsigned short int cols = 79;
  216. #else // !__MINGW32__
  217. bool isTTY = isatty(STDOUT_FILENO) == 1;
  218. unsigned short int cols = 80;
  219. #endif // !__MINGW32__
  220. if(isTTY) {
  221. #ifndef __MINGW32__
  222. #ifdef HAVE_TERMIOS_H
  223. struct winsize size;
  224. if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) == 0) {
  225. cols = size.ws_col;
  226. }
  227. #endif // HAVE_TERMIOS_H
  228. #endif // !__MINGW32__
  229. std::cout << '\r' << std::setfill(' ') << std::setw(cols) << ' ' << '\r';
  230. }
  231. std::ostringstream o;
  232. if(e->getRequestGroupMan()->countRequestGroup() > 0) {
  233. if((summaryInterval_ > 0) &&
  234. lastSummaryNotified_.difference(global::wallclock) >= summaryInterval_) {
  235. lastSummaryNotified_ = global::wallclock;
  236. printProgressSummary(e->getRequestGroupMan()->getRequestGroups(), cols, e,
  237. sizeFormatter);
  238. std::cout << "\n";
  239. }
  240. SharedHandle<RequestGroup> firstRequestGroup =
  241. e->getRequestGroupMan()->getRequestGroup(0);
  242. printProgress(o, firstRequestGroup, e, sizeFormatter);
  243. if(e->getRequestGroupMan()->countRequestGroup() > 1) {
  244. o << "("
  245. << e->getRequestGroupMan()->countRequestGroup()-1
  246. << "more...)";
  247. }
  248. }
  249. if(e->getRequestGroupMan()->countRequestGroup() > 1 &&
  250. !e->getRequestGroupMan()->downloadFinished()) {
  251. TransferStat stat = e->getRequestGroupMan()->calculateStat();
  252. o << " "
  253. << "[TOTAL SPD:"
  254. << sizeFormatter(stat.getDownloadSpeed()) << "Bs" << "]";
  255. }
  256. {
  257. SharedHandle<FileAllocationEntry> entry =
  258. e->getFileAllocationMan()->getPickedEntry();
  259. if(!entry.isNull()) {
  260. o << " "
  261. << "[FileAlloc:"
  262. << "#" << entry->getRequestGroup()->getGID() << " "
  263. << sizeFormatter(entry->getCurrentLength())
  264. << "B"
  265. << "/"
  266. << sizeFormatter(entry->getTotalLength())
  267. << "B"
  268. << "(";
  269. if(entry->getTotalLength() > 0) {
  270. o << 100*entry->getCurrentLength()/entry->getTotalLength();
  271. } else {
  272. o << "--";
  273. }
  274. o << "%)"
  275. << "]";
  276. if(e->getFileAllocationMan()->hasNext()) {
  277. o << "("
  278. << e->getFileAllocationMan()->countEntryInQueue()
  279. << "waiting...)";
  280. }
  281. }
  282. }
  283. #ifdef ENABLE_MESSAGE_DIGEST
  284. {
  285. SharedHandle<CheckIntegrityEntry> entry =
  286. e->getCheckIntegrityMan()->getPickedEntry();
  287. if(!entry.isNull()) {
  288. o << " "
  289. << "[Checksum:"
  290. << "#" << entry->getRequestGroup()->getGID() << " "
  291. << sizeFormatter(entry->getCurrentLength())
  292. << "B"
  293. << "/"
  294. << sizeFormatter(entry->getTotalLength())
  295. << "B"
  296. << "("
  297. << 100*entry->getCurrentLength()/entry->getTotalLength()
  298. << "%)"
  299. << "]";
  300. if(e->getCheckIntegrityMan()->hasNext()) {
  301. o << "("
  302. << e->getCheckIntegrityMan()->countEntryInQueue()
  303. << "waiting...)";
  304. }
  305. }
  306. }
  307. #endif // ENABLE_MESSAGE_DIGEST
  308. std::string readout = o.str();
  309. if(isTTY) {
  310. std::string::iterator last = readout.begin();
  311. if(readout.size() > cols) {
  312. std::advance(last, cols);
  313. } else {
  314. last = readout.end();
  315. }
  316. std::copy(readout.begin(), last, std::ostream_iterator<char>(std::cout));
  317. std::cout << std::flush;
  318. } else {
  319. std::copy(readout.begin(), readout.end(), std::ostream_iterator<char>(std::cout));
  320. std::cout << std::endl;
  321. }
  322. }
  323. } // namespace aria2