ConsoleStatCalc.cc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. rg->getDownloadContext()->getAttribute(bittorrent::BITTORRENT)
  86. .containsKey(bittorrent::METADATA) &&
  87. rg->downloadFinished()) {
  88. o << "SEEDING" << "(" << "ratio:";
  89. if(rg->getCompletedLength() > 0) {
  90. o << std::fixed << std::setprecision(1)
  91. << ((stat.getAllTimeUploadLength()*10)/rg->getCompletedLength())/10.0;
  92. } else {
  93. o << "--";
  94. }
  95. o << ")";
  96. } else
  97. #endif // ENABLE_BITTORRENT
  98. {
  99. o << "SIZE:"
  100. << sizeFormatter(rg->getCompletedLength())
  101. << "B"
  102. << "/"
  103. << sizeFormatter(rg->getTotalLength())
  104. << "B";
  105. if(rg->getTotalLength() > 0) {
  106. o << "("
  107. << 100*rg->getCompletedLength()/rg->getTotalLength()
  108. << "%)";
  109. }
  110. }
  111. o << " "
  112. << "CN:"
  113. << rg->getNumConnection();
  114. #ifdef ENABLE_BITTORRENT
  115. SharedHandle<PeerStorage> ps =
  116. e->getBtRegistry()->get(rg->getGID())._peerStorage;
  117. if(!ps.isNull()) {
  118. std::vector<SharedHandle<Peer> > peers;
  119. ps->getActivePeers(peers);
  120. o << " " << "SEED:"
  121. << countSeeder(peers.begin(), peers.end());
  122. }
  123. #endif // ENABLE_BITTORRENT
  124. if(!rg->downloadFinished()) {
  125. o << " "
  126. << "SPD:"
  127. << sizeFormatter(stat.getDownloadSpeed()) << "Bs";
  128. }
  129. if(stat.getSessionUploadLength() > 0) {
  130. o << " "
  131. << "UP:"
  132. << sizeFormatter(stat.getUploadSpeed()) << "Bs"
  133. << "(" << sizeFormatter(stat.getAllTimeUploadLength()) << "B)";
  134. }
  135. if(eta > 0) {
  136. o << " "
  137. << "ETA:"
  138. << util::secfmt(eta);
  139. }
  140. o << "]";
  141. }
  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. static void printProgressSummary
  168. (const std::deque<SharedHandle<RequestGroup> >& groups, size_t cols,
  169. const DownloadEngine* e,
  170. const SizeFormatter& sizeFormatter)
  171. {
  172. const char SEP_CHAR = '=';
  173. time_t now;
  174. time(&now);
  175. std::cout << " *** Download Progress Summary";
  176. {
  177. time_t now;
  178. struct tm* staticNowtmPtr;
  179. char buf[26];
  180. if(time(&now) != (time_t)-1 && (staticNowtmPtr = localtime(&now)) != 0 &&
  181. asctime_r(staticNowtmPtr, buf) != 0) {
  182. char* lfptr = strchr(buf, '\n');
  183. if(lfptr) {
  184. *lfptr = '\0';
  185. }
  186. std::cout << " as of " << buf;
  187. }
  188. }
  189. std::cout << " *** " << "\n"
  190. << std::setfill(SEP_CHAR) << std::setw(cols) << SEP_CHAR << "\n";
  191. std::for_each(groups.begin(), groups.end(),
  192. PrintSummary(cols, e, sizeFormatter));
  193. }
  194. ConsoleStatCalc::ConsoleStatCalc(time_t summaryInterval, bool humanReadable):
  195. _summaryInterval(summaryInterval)
  196. {
  197. if(humanReadable) {
  198. _sizeFormatter.reset(new AbbrevSizeFormatter());
  199. } else {
  200. _sizeFormatter.reset(new PlainSizeFormatter());
  201. }
  202. }
  203. void
  204. ConsoleStatCalc::calculateStat(const DownloadEngine* e)
  205. {
  206. if(_cp.difference(global::wallclock) < 1) {
  207. return;
  208. }
  209. _cp = global::wallclock;
  210. const SizeFormatter& sizeFormatter = *_sizeFormatter.get();
  211. #ifdef __MINGW32__
  212. bool isTTY = true;
  213. // Windows terminal cannot handle at the end of line properly.
  214. unsigned short int cols = 79;
  215. #else // !__MINGW32__
  216. bool isTTY = isatty(STDOUT_FILENO) == 1;
  217. unsigned short int cols = 80;
  218. #endif // !__MINGW32__
  219. if(isTTY) {
  220. #ifndef __MINGW32__
  221. #ifdef HAVE_TERMIOS_H
  222. struct winsize size;
  223. if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) == 0) {
  224. cols = size.ws_col;
  225. }
  226. #endif // HAVE_TERMIOS_H
  227. #endif // !__MINGW32__
  228. std::cout << '\r' << std::setfill(' ') << std::setw(cols) << ' ' << '\r';
  229. }
  230. std::ostringstream o;
  231. if(e->getRequestGroupMan()->countRequestGroup() > 0) {
  232. if((_summaryInterval > 0) &&
  233. _lastSummaryNotified.difference(global::wallclock) >= _summaryInterval) {
  234. _lastSummaryNotified = global::wallclock;
  235. printProgressSummary(e->getRequestGroupMan()->getRequestGroups(), cols, e,
  236. sizeFormatter);
  237. std::cout << "\n";
  238. }
  239. SharedHandle<RequestGroup> firstRequestGroup =
  240. e->getRequestGroupMan()->getRequestGroup(0);
  241. printProgress(o, firstRequestGroup, e, sizeFormatter);
  242. if(e->getRequestGroupMan()->countRequestGroup() > 1) {
  243. o << "("
  244. << e->getRequestGroupMan()->countRequestGroup()-1
  245. << "more...)";
  246. }
  247. }
  248. if(e->getRequestGroupMan()->countRequestGroup() > 1 &&
  249. !e->getRequestGroupMan()->downloadFinished()) {
  250. TransferStat stat = e->getRequestGroupMan()->calculateStat();
  251. o << " "
  252. << "[TOTAL SPD:"
  253. << sizeFormatter(stat.getDownloadSpeed()) << "Bs" << "]";
  254. }
  255. {
  256. SharedHandle<FileAllocationEntry> entry =
  257. e->getFileAllocationMan()->getPickedEntry();
  258. if(!entry.isNull()) {
  259. o << " "
  260. << "[FileAlloc:"
  261. << "#" << entry->getRequestGroup()->getGID() << " "
  262. << sizeFormatter(entry->getCurrentLength())
  263. << "B"
  264. << "/"
  265. << sizeFormatter(entry->getTotalLength())
  266. << "B"
  267. << "(";
  268. if(entry->getTotalLength() > 0) {
  269. o << 100*entry->getCurrentLength()/entry->getTotalLength();
  270. } else {
  271. o << "--";
  272. }
  273. o << "%)"
  274. << "]";
  275. if(e->getFileAllocationMan()->hasNext()) {
  276. o << "("
  277. << e->getFileAllocationMan()->countEntryInQueue()
  278. << "waiting...)";
  279. }
  280. }
  281. }
  282. #ifdef ENABLE_MESSAGE_DIGEST
  283. {
  284. SharedHandle<CheckIntegrityEntry> entry =
  285. e->getCheckIntegrityMan()->getPickedEntry();
  286. if(!entry.isNull()) {
  287. o << " "
  288. << "[Checksum:"
  289. << "#" << entry->getRequestGroup()->getGID() << " "
  290. << sizeFormatter(entry->getCurrentLength())
  291. << "B"
  292. << "/"
  293. << sizeFormatter(entry->getTotalLength())
  294. << "B"
  295. << "("
  296. << 100*entry->getCurrentLength()/entry->getTotalLength()
  297. << "%)"
  298. << "]";
  299. if(e->getCheckIntegrityMan()->hasNext()) {
  300. o << "("
  301. << e->getCheckIntegrityMan()->countEntryInQueue()
  302. << "waiting...)";
  303. }
  304. }
  305. }
  306. #endif // ENABLE_MESSAGE_DIGEST
  307. std::string readout = o.str();
  308. if(isTTY) {
  309. std::string::iterator last = readout.begin();
  310. if(readout.size() > cols) {
  311. std::advance(last, cols);
  312. } else {
  313. last = readout.end();
  314. }
  315. std::copy(readout.begin(), last, std::ostream_iterator<char>(std::cout));
  316. std::cout << std::flush;
  317. } else {
  318. std::copy(readout.begin(), readout.end(), std::ostream_iterator<char>(std::cout));
  319. std::cout << std::endl;
  320. }
  321. }
  322. } // namespace aria2