ConsoleStatCalc.cc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #ifdef ENABLE_BITTORRENT
  61. # include "bittorrent_helper.h"
  62. # include "Peer.h"
  63. # include "PeerStorage.h"
  64. # include "BtRegistry.h"
  65. # include "BtProgressInfoFile.h"
  66. # include "BtRuntime.h"
  67. # include "BtAnnounce.h"
  68. # include "PieceStorage.h"
  69. #endif // ENABLE_BITTORRENT
  70. namespace aria2 {
  71. static void printProgress
  72. (std::ostream& o, const SharedHandle<RequestGroup>& rg, const DownloadEngine* e,
  73. const SizeFormatter& sizeFormatter)
  74. {
  75. TransferStat stat = rg->calculateStat();
  76. unsigned int eta = 0;
  77. if(rg->getTotalLength() > 0 && stat.getDownloadSpeed() > 0) {
  78. eta = (rg->getTotalLength()-rg->getCompletedLength())/stat.getDownloadSpeed();
  79. }
  80. o << "["
  81. << "#" << rg->getGID() << " ";
  82. #ifdef ENABLE_BITTORRENT
  83. if(rg->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT) &&
  84. rg->getDownloadContext()->getAttribute(bittorrent::BITTORRENT)
  85. .containsKey(bittorrent::METADATA) &&
  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. class PrintSummary
  142. {
  143. private:
  144. size_t _cols;
  145. const DownloadEngine* _e;
  146. const SizeFormatter& _sizeFormatter;
  147. public:
  148. PrintSummary
  149. (size_t cols, const DownloadEngine* e,
  150. const SizeFormatter& sizeFormatter):
  151. _cols(cols), _e(e), _sizeFormatter(sizeFormatter) {}
  152. void operator()(const SharedHandle<RequestGroup>& rg)
  153. {
  154. const char SEP_CHAR = '-';
  155. printProgress(std::cout, rg, _e, _sizeFormatter);
  156. const std::vector<SharedHandle<FileEntry> >& fileEntries =
  157. rg->getDownloadContext()->getFileEntries();
  158. std::cout << "\n"
  159. << "FILE: ";
  160. writeFilePath(fileEntries.begin(), fileEntries.end(),
  161. std::cout, rg->inMemoryDownload());
  162. std::cout << "\n"
  163. << std::setfill(SEP_CHAR) << std::setw(_cols) << SEP_CHAR << "\n";
  164. }
  165. };
  166. static void printProgressSummary
  167. (const std::deque<SharedHandle<RequestGroup> >& groups, size_t cols,
  168. const DownloadEngine* e,
  169. const SizeFormatter& sizeFormatter)
  170. {
  171. const char SEP_CHAR = '=';
  172. time_t now;
  173. time(&now);
  174. std::cout << " *** Download Progress Summary";
  175. {
  176. time_t now;
  177. struct tm* staticNowtmPtr;
  178. char buf[26];
  179. if(time(&now) != (time_t)-1 && (staticNowtmPtr = localtime(&now)) != 0 &&
  180. asctime_r(staticNowtmPtr, buf) != 0) {
  181. char* lfptr = strchr(buf, '\n');
  182. if(lfptr) {
  183. *lfptr = '\0';
  184. }
  185. std::cout << " as of " << buf;
  186. }
  187. }
  188. std::cout << " *** " << "\n"
  189. << std::setfill(SEP_CHAR) << std::setw(cols) << SEP_CHAR << "\n";
  190. std::for_each(groups.begin(), groups.end(),
  191. PrintSummary(cols, e, sizeFormatter));
  192. }
  193. ConsoleStatCalc::ConsoleStatCalc(time_t summaryInterval, bool humanReadable):
  194. _summaryInterval(summaryInterval)
  195. {
  196. if(humanReadable) {
  197. _sizeFormatter.reset(new AbbrevSizeFormatter());
  198. } else {
  199. _sizeFormatter.reset(new PlainSizeFormatter());
  200. }
  201. }
  202. void
  203. ConsoleStatCalc::calculateStat(const DownloadEngine* e)
  204. {
  205. if(_cp.difference(global::wallclock) < 1) {
  206. return;
  207. }
  208. _cp = global::wallclock;
  209. const SizeFormatter& sizeFormatter = *_sizeFormatter.get();
  210. #ifdef __MINGW32__
  211. bool isTTY = true;
  212. // Windows terminal cannot handle at the end of line properly.
  213. unsigned short int cols = 79;
  214. #else // !__MINGW32__
  215. bool isTTY = isatty(STDOUT_FILENO) == 1;
  216. unsigned short int cols = 80;
  217. #endif // !__MINGW32__
  218. if(isTTY) {
  219. #ifndef __MINGW32__
  220. #ifdef HAVE_TERMIOS_H
  221. struct winsize size;
  222. if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) == 0) {
  223. cols = size.ws_col;
  224. }
  225. #endif // HAVE_TERMIOS_H
  226. #endif // !__MINGW32__
  227. std::cout << '\r' << std::setfill(' ') << std::setw(cols) << ' ' << '\r';
  228. }
  229. std::ostringstream o;
  230. if(e->_requestGroupMan->countRequestGroup() > 0) {
  231. if((_summaryInterval > 0) &&
  232. _lastSummaryNotified.difference(global::wallclock) >= _summaryInterval) {
  233. _lastSummaryNotified = global::wallclock;
  234. printProgressSummary(e->_requestGroupMan->getRequestGroups(), cols, e,
  235. sizeFormatter);
  236. std::cout << "\n";
  237. }
  238. SharedHandle<RequestGroup> firstRequestGroup =
  239. e->_requestGroupMan->getRequestGroup(0);
  240. printProgress(o, firstRequestGroup, e, sizeFormatter);
  241. if(e->_requestGroupMan->countRequestGroup() > 1) {
  242. o << "("
  243. << e->_requestGroupMan->countRequestGroup()-1
  244. << "more...)";
  245. }
  246. }
  247. if(e->_requestGroupMan->countRequestGroup() > 1 &&
  248. !e->_requestGroupMan->downloadFinished()) {
  249. TransferStat stat = e->_requestGroupMan->calculateStat();
  250. o << " "
  251. << "[TOTAL SPD:"
  252. << sizeFormatter(stat.getDownloadSpeed()) << "Bs" << "]";
  253. }
  254. {
  255. SharedHandle<FileAllocationEntry> entry=e->_fileAllocationMan->getPickedEntry();
  256. if(!entry.isNull()) {
  257. o << " "
  258. << "[FileAlloc:"
  259. << "#" << entry->getRequestGroup()->getGID() << " "
  260. << sizeFormatter(entry->getCurrentLength())
  261. << "B"
  262. << "/"
  263. << sizeFormatter(entry->getTotalLength())
  264. << "B"
  265. << "(";
  266. if(entry->getTotalLength() > 0) {
  267. o << 100*entry->getCurrentLength()/entry->getTotalLength();
  268. } else {
  269. o << "--";
  270. }
  271. o << "%)"
  272. << "]";
  273. if(e->_fileAllocationMan->hasNext()) {
  274. o << "("
  275. << e->_fileAllocationMan->countEntryInQueue()
  276. << "waiting...)";
  277. }
  278. }
  279. }
  280. #ifdef ENABLE_MESSAGE_DIGEST
  281. {
  282. SharedHandle<CheckIntegrityEntry> entry =
  283. e->_checkIntegrityMan->getPickedEntry();
  284. if(!entry.isNull()) {
  285. o << " "
  286. << "[Checksum:"
  287. << "#" << entry->getRequestGroup()->getGID() << " "
  288. << sizeFormatter(entry->getCurrentLength())
  289. << "B"
  290. << "/"
  291. << sizeFormatter(entry->getTotalLength())
  292. << "B"
  293. << "("
  294. << 100*entry->getCurrentLength()/entry->getTotalLength()
  295. << "%)"
  296. << "]";
  297. if(e->_checkIntegrityMan->hasNext()) {
  298. o << "("
  299. << e->_checkIntegrityMan->countEntryInQueue()
  300. << "waiting...)";
  301. }
  302. }
  303. }
  304. #endif // ENABLE_MESSAGE_DIGEST
  305. std::string readout = o.str();
  306. if(isTTY) {
  307. std::string::iterator last = readout.begin();
  308. if(readout.size() > cols) {
  309. std::advance(last, cols);
  310. } else {
  311. last = readout.end();
  312. }
  313. std::copy(readout.begin(), last, std::ostream_iterator<char>(std::cout));
  314. std::cout << std::flush;
  315. } else {
  316. std::copy(readout.begin(), readout.end(), std::ostream_iterator<char>(std::cout));
  317. std::cout << std::endl;
  318. }
  319. }
  320. } // namespace aria2