ConsoleStatCalc.cc 11 KB

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