ConsoleStatCalc.cc 9.7 KB

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