DefaultPieceStorage.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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 "DefaultPieceStorage.h"
  36. #include <numeric>
  37. #include <algorithm>
  38. #include "DownloadContext.h"
  39. #include "Piece.h"
  40. #include "Peer.h"
  41. #include "LogFactory.h"
  42. #include "Logger.h"
  43. #include "prefs.h"
  44. #include "DirectDiskAdaptor.h"
  45. #include "MultiDiskAdaptor.h"
  46. #include "DiskWriter.h"
  47. #include "BitfieldMan.h"
  48. #include "message.h"
  49. #include "DefaultDiskWriterFactory.h"
  50. #include "FileEntry.h"
  51. #include "DlAbortEx.h"
  52. #include "util.h"
  53. #include "a2functional.h"
  54. #include "Option.h"
  55. #include "fmt.h"
  56. #include "RarestPieceSelector.h"
  57. #include "array_fun.h"
  58. #include "PieceStatMan.h"
  59. #include "wallclock.h"
  60. #include "bitfield.h"
  61. #ifdef ENABLE_BITTORRENT
  62. # include "bittorrent_helper.h"
  63. #endif // ENABLE_BITTORRENT
  64. namespace aria2 {
  65. DefaultPieceStorage::DefaultPieceStorage
  66. (const SharedHandle<DownloadContext>& downloadContext, const Option* option)
  67. : downloadContext_(downloadContext),
  68. bitfieldMan_(new BitfieldMan(downloadContext->getPieceLength(),
  69. downloadContext->getTotalLength())),
  70. diskWriterFactory_(new DefaultDiskWriterFactory()),
  71. endGame_(false),
  72. endGamePieceNum_(END_GAME_PIECE_NUM),
  73. option_(option),
  74. pieceStatMan_(new PieceStatMan(downloadContext->getNumPieces(), true)),
  75. pieceSelector_(new RarestPieceSelector(pieceStatMan_))
  76. {}
  77. DefaultPieceStorage::~DefaultPieceStorage()
  78. {
  79. delete bitfieldMan_;
  80. }
  81. SharedHandle<Piece> DefaultPieceStorage::checkOutPiece(size_t index)
  82. {
  83. bitfieldMan_->setUseBit(index);
  84. SharedHandle<Piece> piece = findUsedPiece(index);
  85. if(!piece) {
  86. piece.reset(new Piece(index, bitfieldMan_->getBlockLength(index)));
  87. #ifdef ENABLE_MESSAGE_DIGEST
  88. piece->setHashAlgo(downloadContext_->getPieceHashAlgo());
  89. #endif // ENABLE_MESSAGE_DIGEST
  90. addUsedPiece(piece);
  91. return piece;
  92. } else {
  93. return piece;
  94. }
  95. }
  96. /**
  97. * Newly instantiated piece is not added to usedPieces.
  98. * Because it is waste of memory and there is no chance to use them later.
  99. */
  100. SharedHandle<Piece> DefaultPieceStorage::getPiece(size_t index)
  101. {
  102. SharedHandle<Piece> piece;
  103. if(0 <= index && index <= bitfieldMan_->getMaxIndex()) {
  104. piece = findUsedPiece(index);
  105. if(!piece) {
  106. piece.reset(new Piece(index, bitfieldMan_->getBlockLength(index)));
  107. if(hasPiece(index)) {
  108. piece->setAllBlock();
  109. }
  110. }
  111. }
  112. return piece;
  113. }
  114. void DefaultPieceStorage::addUsedPiece(const SharedHandle<Piece>& piece)
  115. {
  116. std::deque<SharedHandle<Piece> >::iterator i =
  117. std::lower_bound(usedPieces_.begin(), usedPieces_.end(), piece,
  118. DerefLess<SharedHandle<Piece> >());
  119. usedPieces_.insert(i, piece);
  120. A2_LOG_DEBUG(fmt("usedPieces_.size()=%lu",
  121. static_cast<unsigned long>(usedPieces_.size())));
  122. }
  123. SharedHandle<Piece> DefaultPieceStorage::findUsedPiece(size_t index) const
  124. {
  125. SharedHandle<Piece> p(new Piece());
  126. p->setIndex(index);
  127. std::deque<SharedHandle<Piece> >::const_iterator i =
  128. std::lower_bound(usedPieces_.begin(), usedPieces_.end(), p,
  129. DerefLess<SharedHandle<Piece> >());
  130. if(i != usedPieces_.end() && *(*i) == *p) {
  131. return *i;
  132. } else {
  133. p.reset();
  134. return p;
  135. }
  136. }
  137. #ifdef ENABLE_BITTORRENT
  138. bool DefaultPieceStorage::hasMissingPiece(const SharedHandle<Peer>& peer)
  139. {
  140. return bitfieldMan_->hasMissingPiece(peer->getBitfield(),
  141. peer->getBitfieldLength());
  142. }
  143. void DefaultPieceStorage::getMissingPiece
  144. (std::vector<SharedHandle<Piece> >& pieces,
  145. size_t minMissingBlocks,
  146. const unsigned char* bitfield,
  147. size_t length)
  148. {
  149. const size_t mislen = bitfieldMan_->getBitfieldLength();
  150. array_ptr<unsigned char> misbitfield(new unsigned char[mislen]);
  151. size_t blocks = bitfieldMan_->countBlock();
  152. size_t misBlock = 0;
  153. if(isEndGame()) {
  154. bool r = bitfieldMan_->getAllMissingIndexes
  155. (misbitfield, mislen, bitfield, length);
  156. if(!r) {
  157. return;
  158. }
  159. std::vector<size_t> indexes;
  160. for(size_t i = 0; i < blocks; ++i) {
  161. if(bitfield::test(misbitfield, blocks, i)) {
  162. indexes.push_back(i);
  163. }
  164. }
  165. std::random_shuffle(indexes.begin(), indexes.end());
  166. for(std::vector<size_t>::const_iterator i = indexes.begin(),
  167. eoi = indexes.end(); i != eoi && misBlock < minMissingBlocks; ++i) {
  168. pieces.push_back(checkOutPiece(*i));
  169. misBlock += pieces.back()->countMissingBlock();
  170. }
  171. } else {
  172. bool r = bitfieldMan_->getAllMissingUnusedIndexes
  173. (misbitfield, mislen, bitfield, length);
  174. if(!r) {
  175. return;
  176. }
  177. while(misBlock < minMissingBlocks) {
  178. size_t index;
  179. if(pieceSelector_->select(index, misbitfield, blocks)) {
  180. pieces.push_back(checkOutPiece(index));
  181. bitfield::flipBit(misbitfield, blocks, index);
  182. misBlock += pieces.back()->countMissingBlock();
  183. } else {
  184. break;
  185. }
  186. }
  187. }
  188. }
  189. namespace {
  190. void unsetExcludedIndexes(BitfieldMan& bitfield,
  191. const std::vector<size_t>& excludedIndexes)
  192. {
  193. std::for_each(excludedIndexes.begin(), excludedIndexes.end(),
  194. std::bind1st(std::mem_fun(&BitfieldMan::unsetBit), &bitfield));
  195. }
  196. } // namespace
  197. void DefaultPieceStorage::createFastIndexBitfield
  198. (BitfieldMan& bitfield, const SharedHandle<Peer>& peer)
  199. {
  200. for(std::vector<size_t>::const_iterator itr =
  201. peer->getPeerAllowedIndexSet().begin(),
  202. eoi = peer->getPeerAllowedIndexSet().end(); itr != eoi; ++itr) {
  203. if(!bitfieldMan_->isBitSet(*itr) && peer->hasPiece(*itr)) {
  204. bitfield.setBit(*itr);
  205. }
  206. }
  207. }
  208. void DefaultPieceStorage::getMissingPiece
  209. (std::vector<SharedHandle<Piece> >& pieces,
  210. size_t minMissingBlocks,
  211. const SharedHandle<Peer>& peer)
  212. {
  213. getMissingPiece(pieces, minMissingBlocks,
  214. peer->getBitfield(), peer->getBitfieldLength());
  215. }
  216. void DefaultPieceStorage::getMissingPiece
  217. (std::vector<SharedHandle<Piece> >& pieces,
  218. size_t minMissingBlocks,
  219. const SharedHandle<Peer>& peer,
  220. const std::vector<size_t>& excludedIndexes)
  221. {
  222. BitfieldMan tempBitfield(bitfieldMan_->getBlockLength(),
  223. bitfieldMan_->getTotalLength());
  224. tempBitfield.setBitfield(peer->getBitfield(), peer->getBitfieldLength());
  225. unsetExcludedIndexes(tempBitfield, excludedIndexes);
  226. getMissingPiece(pieces, minMissingBlocks,
  227. tempBitfield.getBitfield(), tempBitfield.getBitfieldLength());
  228. }
  229. void DefaultPieceStorage::getMissingFastPiece
  230. (std::vector<SharedHandle<Piece> >& pieces,
  231. size_t minMissingBlocks,
  232. const SharedHandle<Peer>& peer)
  233. {
  234. if(peer->isFastExtensionEnabled() && peer->countPeerAllowedIndexSet() > 0) {
  235. BitfieldMan tempBitfield(bitfieldMan_->getBlockLength(),
  236. bitfieldMan_->getTotalLength());
  237. createFastIndexBitfield(tempBitfield, peer);
  238. getMissingPiece(pieces, minMissingBlocks,
  239. tempBitfield.getBitfield(),
  240. tempBitfield.getBitfieldLength());
  241. }
  242. }
  243. void DefaultPieceStorage::getMissingFastPiece
  244. (std::vector<SharedHandle<Piece> >& pieces,
  245. size_t minMissingBlocks,
  246. const SharedHandle<Peer>& peer,
  247. const std::vector<size_t>& excludedIndexes)
  248. {
  249. if(peer->isFastExtensionEnabled() && peer->countPeerAllowedIndexSet() > 0) {
  250. BitfieldMan tempBitfield(bitfieldMan_->getBlockLength(),
  251. bitfieldMan_->getTotalLength());
  252. createFastIndexBitfield(tempBitfield, peer);
  253. unsetExcludedIndexes(tempBitfield, excludedIndexes);
  254. getMissingPiece(pieces, minMissingBlocks,
  255. tempBitfield.getBitfield(),
  256. tempBitfield.getBitfieldLength());
  257. }
  258. }
  259. SharedHandle<Piece>
  260. DefaultPieceStorage::getMissingPiece(const SharedHandle<Peer>& peer)
  261. {
  262. std::vector<SharedHandle<Piece> > pieces;
  263. getMissingPiece(pieces, 1, peer);
  264. if(pieces.empty()) {
  265. return SharedHandle<Piece>();
  266. } else {
  267. return pieces.front();
  268. }
  269. }
  270. SharedHandle<Piece> DefaultPieceStorage::getMissingPiece
  271. (const SharedHandle<Peer>& peer, const std::vector<size_t>& excludedIndexes)
  272. {
  273. std::vector<SharedHandle<Piece> > pieces;
  274. getMissingPiece(pieces, 1, peer, excludedIndexes);
  275. if(pieces.empty()) {
  276. return SharedHandle<Piece>();
  277. } else {
  278. return pieces.front();
  279. }
  280. }
  281. SharedHandle<Piece> DefaultPieceStorage::getMissingFastPiece
  282. (const SharedHandle<Peer>& peer)
  283. {
  284. std::vector<SharedHandle<Piece> > pieces;
  285. getMissingFastPiece(pieces, 1, peer);
  286. if(pieces.empty()) {
  287. return SharedHandle<Piece>();
  288. } else {
  289. return pieces.front();
  290. }
  291. }
  292. SharedHandle<Piece> DefaultPieceStorage::getMissingFastPiece
  293. (const SharedHandle<Peer>& peer, const std::vector<size_t>& excludedIndexes)
  294. {
  295. std::vector<SharedHandle<Piece> > pieces;
  296. getMissingFastPiece(pieces, 1, peer, excludedIndexes);
  297. if(pieces.empty()) {
  298. return SharedHandle<Piece>();
  299. } else {
  300. return pieces.front();
  301. }
  302. }
  303. #endif // ENABLE_BITTORRENT
  304. bool DefaultPieceStorage::hasMissingUnusedPiece()
  305. {
  306. size_t index;
  307. return bitfieldMan_->getFirstMissingUnusedIndex(index);
  308. }
  309. SharedHandle<Piece> DefaultPieceStorage::getSparseMissingUnusedPiece
  310. (size_t minSplitSize, const unsigned char* ignoreBitfield, size_t length)
  311. {
  312. size_t index;
  313. if(bitfieldMan_->getSparseMissingUnusedIndex
  314. (index, minSplitSize, ignoreBitfield, length)) {
  315. return checkOutPiece(index);
  316. } else {
  317. return SharedHandle<Piece>();
  318. }
  319. }
  320. SharedHandle<Piece> DefaultPieceStorage::getMissingPiece(size_t index)
  321. {
  322. if(hasPiece(index) || isPieceUsed(index)) {
  323. return SharedHandle<Piece>();
  324. } else {
  325. return checkOutPiece(index);
  326. }
  327. }
  328. void DefaultPieceStorage::deleteUsedPiece(const SharedHandle<Piece>& piece)
  329. {
  330. if(!piece) {
  331. return;
  332. }
  333. std::deque<SharedHandle<Piece> >::iterator i =
  334. std::lower_bound(usedPieces_.begin(), usedPieces_.end(), piece,
  335. DerefLess<SharedHandle<Piece> >());
  336. if(i != usedPieces_.end() && *(*i) == *piece) {
  337. usedPieces_.erase(i);
  338. }
  339. }
  340. // void DefaultPieceStorage::reduceUsedPieces(size_t upperBound)
  341. // {
  342. // size_t usedPiecesSize = usedPieces.size();
  343. // if(usedPiecesSize <= upperBound) {
  344. // return;
  345. // }
  346. // size_t delNum = usedPiecesSize-upperBound;
  347. // int fillRate = 10;
  348. // while(delNum && fillRate <= 15) {
  349. // delNum -= deleteUsedPiecesByFillRate(fillRate, delNum);
  350. // fillRate += 5;
  351. // }
  352. // }
  353. // size_t DefaultPieceStorage::deleteUsedPiecesByFillRate(int fillRate,
  354. // size_t delNum)
  355. // {
  356. // size_t deleted = 0;
  357. // for(Pieces::iterator itr = usedPieces.begin();
  358. // itr != usedPieces.end() && deleted < delNum;) {
  359. // SharedHandle<Piece>& piece = *itr;
  360. // if(!bitfieldMan->isUseBitSet(piece->getIndex()) &&
  361. // piece->countCompleteBlock() <= piece->countBlock()*(fillRate/100.0)) {
  362. // logger->info(MSG_DELETING_USED_PIECE,
  363. // piece->getIndex(),
  364. // (piece->countCompleteBlock()*100)/piece->countBlock(),
  365. // fillRate);
  366. // itr = usedPieces.erase(itr);
  367. // ++deleted;
  368. // } else {
  369. // ++itr;
  370. // }
  371. // }
  372. // return deleted;
  373. // }
  374. void DefaultPieceStorage::completePiece(const SharedHandle<Piece>& piece)
  375. {
  376. if(!piece) {
  377. return;
  378. }
  379. deleteUsedPiece(piece);
  380. // if(!isEndGame()) {
  381. // reduceUsedPieces(100);
  382. // }
  383. if(allDownloadFinished()) {
  384. return;
  385. }
  386. bitfieldMan_->setBit(piece->getIndex());
  387. bitfieldMan_->unsetUseBit(piece->getIndex());
  388. addPieceStats(piece->getIndex());
  389. if(downloadFinished()) {
  390. downloadContext_->resetDownloadStopTime();
  391. if(isSelectiveDownloadingMode()) {
  392. A2_LOG_NOTICE(MSG_SELECTIVE_DOWNLOAD_COMPLETED);
  393. // following line was commented out in order to stop sending request
  394. // message after user-specified files were downloaded.
  395. //finishSelectiveDownloadingMode();
  396. } else {
  397. A2_LOG_INFO(MSG_DOWNLOAD_COMPLETED);
  398. }
  399. #ifdef ENABLE_BITTORRENT
  400. if(downloadContext_->hasAttribute(bittorrent::BITTORRENT)) {
  401. SharedHandle<TorrentAttribute> torrentAttrs =
  402. bittorrent::getTorrentAttrs(downloadContext_);
  403. if(!torrentAttrs->metadata.empty()) {
  404. util::executeHookByOptName(downloadContext_->getOwnerRequestGroup(),
  405. option_, PREF_ON_BT_DOWNLOAD_COMPLETE);
  406. }
  407. }
  408. #endif // ENABLE_BITTORRENT
  409. }
  410. }
  411. bool DefaultPieceStorage::isSelectiveDownloadingMode()
  412. {
  413. return bitfieldMan_->isFilterEnabled();
  414. }
  415. // not unittested
  416. void DefaultPieceStorage::cancelPiece(const SharedHandle<Piece>& piece)
  417. {
  418. if(!piece) {
  419. return;
  420. }
  421. bitfieldMan_->unsetUseBit(piece->getIndex());
  422. if(!isEndGame()) {
  423. if(piece->getCompletedLength() == 0) {
  424. deleteUsedPiece(piece);
  425. }
  426. }
  427. }
  428. bool DefaultPieceStorage::hasPiece(size_t index)
  429. {
  430. return bitfieldMan_->isBitSet(index);
  431. }
  432. bool DefaultPieceStorage::isPieceUsed(size_t index)
  433. {
  434. return bitfieldMan_->isUseBitSet(index);
  435. }
  436. uint64_t DefaultPieceStorage::getTotalLength()
  437. {
  438. return bitfieldMan_->getTotalLength();
  439. }
  440. uint64_t DefaultPieceStorage::getFilteredTotalLength()
  441. {
  442. return bitfieldMan_->getFilteredTotalLength();
  443. }
  444. uint64_t DefaultPieceStorage::getCompletedLength()
  445. {
  446. uint64_t completedLength =
  447. bitfieldMan_->getCompletedLength()+getInFlightPieceCompletedLength();
  448. uint64_t totalLength = getTotalLength();
  449. if(completedLength > totalLength) {
  450. completedLength = totalLength;
  451. }
  452. return completedLength;
  453. }
  454. uint64_t DefaultPieceStorage::getFilteredCompletedLength()
  455. {
  456. return bitfieldMan_->getFilteredCompletedLength()+
  457. getInFlightPieceCompletedLength();
  458. }
  459. size_t DefaultPieceStorage::getInFlightPieceCompletedLength() const
  460. {
  461. return std::accumulate(usedPieces_.begin(), usedPieces_.end(),
  462. 0, adopt2nd(std::plus<size_t>(),
  463. mem_fun_sh(&Piece::getCompletedLength)));
  464. }
  465. // not unittested
  466. void DefaultPieceStorage::setupFileFilter()
  467. {
  468. const std::vector<SharedHandle<FileEntry> >& fileEntries =
  469. downloadContext_->getFileEntries();
  470. bool allSelected = true;
  471. for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
  472. fileEntries.begin(), eoi = fileEntries.end();
  473. i != eoi; ++i) {
  474. if(!(*i)->isRequested()) {
  475. allSelected = false;
  476. break;
  477. }
  478. }
  479. if(allSelected) {
  480. return;
  481. }
  482. for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
  483. fileEntries.begin(), eoi = fileEntries.end(); i != eoi; ++i) {
  484. if((*i)->isRequested()) {
  485. bitfieldMan_->addFilter((*i)->getOffset(), (*i)->getLength());
  486. }
  487. }
  488. bitfieldMan_->enableFilter();
  489. }
  490. // not unittested
  491. void DefaultPieceStorage::clearFileFilter()
  492. {
  493. bitfieldMan_->clearFilter();
  494. }
  495. // not unittested
  496. bool DefaultPieceStorage::downloadFinished()
  497. {
  498. // TODO iterate all requested FileEntry and Call
  499. // bitfieldMan->isBitSetOffsetRange()
  500. return bitfieldMan_->isFilteredAllBitSet();
  501. }
  502. // not unittested
  503. bool DefaultPieceStorage::allDownloadFinished()
  504. {
  505. return bitfieldMan_->isAllBitSet();
  506. }
  507. // not unittested
  508. void DefaultPieceStorage::initStorage()
  509. {
  510. if(downloadContext_->getFileEntries().size() == 1) {
  511. A2_LOG_DEBUG("Instantiating DirectDiskAdaptor");
  512. DirectDiskAdaptorHandle directDiskAdaptor(new DirectDiskAdaptor());
  513. directDiskAdaptor->setTotalLength(downloadContext_->getTotalLength());
  514. directDiskAdaptor->setFileEntries
  515. (downloadContext_->getFileEntries().begin(),
  516. downloadContext_->getFileEntries().end());
  517. DiskWriterHandle writer =
  518. diskWriterFactory_->newDiskWriter(directDiskAdaptor->getFilePath());
  519. if(option_->getAsBool(PREF_ENABLE_DIRECT_IO)) {
  520. writer->allowDirectIO();
  521. }
  522. directDiskAdaptor->setDiskWriter(writer);
  523. diskAdaptor_ = directDiskAdaptor;
  524. } else {
  525. A2_LOG_DEBUG("Instantiating MultiDiskAdaptor");
  526. MultiDiskAdaptorHandle multiDiskAdaptor(new MultiDiskAdaptor());
  527. multiDiskAdaptor->setFileEntries(downloadContext_->getFileEntries().begin(),
  528. downloadContext_->getFileEntries().end());
  529. if(option_->getAsBool(PREF_ENABLE_DIRECT_IO)) {
  530. multiDiskAdaptor->allowDirectIO();
  531. }
  532. multiDiskAdaptor->setPieceLength(downloadContext_->getPieceLength());
  533. multiDiskAdaptor->setMaxOpenFiles
  534. (option_->getAsInt(PREF_BT_MAX_OPEN_FILES));
  535. diskAdaptor_ = multiDiskAdaptor;
  536. }
  537. if(option_->get(PREF_FILE_ALLOCATION) == V_FALLOC) {
  538. diskAdaptor_->enableFallocate();
  539. }
  540. }
  541. void DefaultPieceStorage::setBitfield(const unsigned char* bitfield,
  542. size_t bitfieldLength)
  543. {
  544. bitfieldMan_->setBitfield(bitfield, bitfieldLength);
  545. addPieceStats(bitfield, bitfieldLength);
  546. }
  547. size_t DefaultPieceStorage::getBitfieldLength()
  548. {
  549. return bitfieldMan_->getBitfieldLength();
  550. }
  551. const unsigned char* DefaultPieceStorage::getBitfield()
  552. {
  553. return bitfieldMan_->getBitfield();
  554. }
  555. DiskAdaptorHandle DefaultPieceStorage::getDiskAdaptor() {
  556. return diskAdaptor_;
  557. }
  558. size_t DefaultPieceStorage::getPieceLength(size_t index)
  559. {
  560. return bitfieldMan_->getBlockLength(index);
  561. }
  562. void DefaultPieceStorage::advertisePiece(cuid_t cuid, size_t index)
  563. {
  564. HaveEntry entry(cuid, index, global::wallclock);
  565. haves_.push_front(entry);
  566. }
  567. void
  568. DefaultPieceStorage::getAdvertisedPieceIndexes(std::vector<size_t>& indexes,
  569. cuid_t myCuid,
  570. const Timer& lastCheckTime)
  571. {
  572. for(std::deque<HaveEntry>::const_iterator itr = haves_.begin(),
  573. eoi = haves_.end(); itr != eoi; ++itr) {
  574. const HaveEntry& have = *itr;
  575. if(have.getCuid() == myCuid) {
  576. continue;
  577. }
  578. if(lastCheckTime > have.getRegisteredTime()) {
  579. break;
  580. }
  581. indexes.push_back(have.getIndex());
  582. }
  583. }
  584. namespace {
  585. class FindElapsedHave
  586. {
  587. private:
  588. time_t elapsed;
  589. public:
  590. FindElapsedHave(time_t elapsed):elapsed(elapsed) {}
  591. bool operator()(const HaveEntry& have) {
  592. if(have.getRegisteredTime().difference(global::wallclock) >= elapsed) {
  593. return true;
  594. } else {
  595. return false;
  596. }
  597. }
  598. };
  599. } // namespace
  600. void DefaultPieceStorage::removeAdvertisedPiece(time_t elapsed)
  601. {
  602. std::deque<HaveEntry>::iterator itr =
  603. std::find_if(haves_.begin(), haves_.end(), FindElapsedHave(elapsed));
  604. if(itr != haves_.end()) {
  605. A2_LOG_DEBUG(fmt(MSG_REMOVED_HAVE_ENTRY,
  606. static_cast<unsigned long>(haves_.end()-itr)));
  607. haves_.erase(itr, haves_.end());
  608. }
  609. }
  610. void DefaultPieceStorage::markAllPiecesDone()
  611. {
  612. bitfieldMan_->setAllBit();
  613. }
  614. void DefaultPieceStorage::markPiecesDone(uint64_t length)
  615. {
  616. if(length == bitfieldMan_->getTotalLength()) {
  617. bitfieldMan_->setAllBit();
  618. } else if(length == 0) {
  619. // TODO this would go to markAllPiecesUndone()
  620. bitfieldMan_->clearAllBit();
  621. usedPieces_.clear();
  622. } else {
  623. size_t numPiece = length/bitfieldMan_->getBlockLength();
  624. if(numPiece > 0) {
  625. bitfieldMan_->setBitRange(0, numPiece-1);
  626. }
  627. size_t r = (length%bitfieldMan_->getBlockLength())/Piece::BLOCK_LENGTH;
  628. if(r > 0) {
  629. SharedHandle<Piece> p
  630. (new Piece(numPiece, bitfieldMan_->getBlockLength(numPiece)));
  631. for(size_t i = 0; i < r; ++i) {
  632. p->completeBlock(i);
  633. }
  634. #ifdef ENABLE_MESSAGE_DIGEST
  635. p->setHashAlgo(downloadContext_->getPieceHashAlgo());
  636. #endif // ENABLE_MESSAGE_DIGEST
  637. addUsedPiece(p);
  638. }
  639. }
  640. }
  641. void DefaultPieceStorage::markPieceMissing(size_t index)
  642. {
  643. bitfieldMan_->unsetBit(index);
  644. }
  645. void DefaultPieceStorage::addInFlightPiece
  646. (const std::vector<SharedHandle<Piece> >& pieces)
  647. {
  648. usedPieces_.insert(usedPieces_.end(), pieces.begin(), pieces.end());
  649. std::sort(usedPieces_.begin(), usedPieces_.end(),
  650. DerefLess<SharedHandle<Piece> >());
  651. }
  652. size_t DefaultPieceStorage::countInFlightPiece()
  653. {
  654. return usedPieces_.size();
  655. }
  656. void DefaultPieceStorage::getInFlightPieces
  657. (std::vector<SharedHandle<Piece> >& pieces)
  658. {
  659. pieces.insert(pieces.end(), usedPieces_.begin(), usedPieces_.end());
  660. }
  661. void DefaultPieceStorage::setDiskWriterFactory
  662. (const DiskWriterFactoryHandle& diskWriterFactory)
  663. {
  664. diskWriterFactory_ = diskWriterFactory;
  665. }
  666. void DefaultPieceStorage::addPieceStats(const unsigned char* bitfield,
  667. size_t bitfieldLength)
  668. {
  669. pieceStatMan_->addPieceStats(bitfield, bitfieldLength);
  670. }
  671. void DefaultPieceStorage::subtractPieceStats(const unsigned char* bitfield,
  672. size_t bitfieldLength)
  673. {
  674. pieceStatMan_->subtractPieceStats(bitfield, bitfieldLength);
  675. }
  676. void DefaultPieceStorage::updatePieceStats(const unsigned char* newBitfield,
  677. size_t newBitfieldLength,
  678. const unsigned char* oldBitfield)
  679. {
  680. pieceStatMan_->updatePieceStats(newBitfield, newBitfieldLength,
  681. oldBitfield);
  682. }
  683. void DefaultPieceStorage::addPieceStats(size_t index)
  684. {
  685. pieceStatMan_->addPieceStats(index);
  686. }
  687. size_t DefaultPieceStorage::getNextUsedIndex(size_t index)
  688. {
  689. for(size_t i = index+1; i < bitfieldMan_->countBlock(); ++i) {
  690. if(bitfieldMan_->isUseBitSet(i) || bitfieldMan_->isBitSet(i)) {
  691. return i;
  692. }
  693. }
  694. return bitfieldMan_->countBlock();
  695. }
  696. } // namespace aria2