ConsoleStatCalc.cc 10 KB

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