Updating upload size and speed must be done separately

pull/538/head
Tatsuhiro Tsujikawa 2016-01-04 23:54:18 +09:00
parent b1132d6b10
commit 8246fd1ff5
15 changed files with 73 additions and 39 deletions

View File

@ -99,8 +99,8 @@ void BtPieceMessage::doReceivedAction()
} }
auto slot = getBtMessageDispatcher()->getOutstandingRequest(index_, begin_, auto slot = getBtMessageDispatcher()->getOutstandingRequest(index_, begin_,
blockLength_); blockLength_);
getPeer()->updateDownloadLength(blockLength_); getPeer()->updateDownload(blockLength_);
downloadContext_->updateDownloadLength(blockLength_); downloadContext_->updateDownload(blockLength_);
if (slot) { if (slot) {
getPeer()->snubbing(false); getPeer()->snubbing(false);
std::shared_ptr<Piece> piece = getPieceStorage()->getPiece(index_); std::shared_ptr<Piece> piece = getPieceStorage()->getPiece(index_);
@ -175,8 +175,9 @@ size_t BtPieceMessage::getMessageHeaderLength()
namespace { namespace {
struct PieceSendUpdate : public ProgressUpdate { struct PieceSendUpdate : public ProgressUpdate {
PieceSendUpdate(std::shared_ptr<Peer> peer, size_t headerLength) PieceSendUpdate(DownloadContext* dctx, std::shared_ptr<Peer> peer,
: peer(std::move(peer)), headerLength(headerLength) size_t headerLength)
: dctx(dctx), peer(std::move(peer)), headerLength(headerLength)
{ {
} }
virtual void update(size_t length, bool complete) CXX11_OVERRIDE virtual void update(size_t length, bool complete) CXX11_OVERRIDE
@ -187,7 +188,9 @@ struct PieceSendUpdate : public ProgressUpdate {
length -= m; length -= m;
} }
peer->updateUploadLength(length); peer->updateUploadLength(length);
dctx->updateUploadLength(length);
} }
DownloadContext* dctx;
std::shared_ptr<Peer> peer; std::shared_ptr<Peer> peer;
size_t headerLength; size_t headerLength;
}; };
@ -216,12 +219,13 @@ void BtPieceMessage::pushPieceData(int64_t offset, int32_t length) const
r = getPieceStorage()->getDiskAdaptor()->readData( r = getPieceStorage()->getDiskAdaptor()->readData(
buf.get() + MESSAGE_HEADER_LENGTH, length, offset); buf.get() + MESSAGE_HEADER_LENGTH, length, offset);
if (r == length) { if (r == length) {
const auto& peer = getPeer();
getPeerConnection()->pushBytes( getPeerConnection()->pushBytes(
buf.release(), length + MESSAGE_HEADER_LENGTH, buf.release(), length + MESSAGE_HEADER_LENGTH,
make_unique<PieceSendUpdate>(getPeer(), MESSAGE_HEADER_LENGTH)); make_unique<PieceSendUpdate>(downloadContext_, peer,
// To avoid upload rate overflow, we update the length here at MESSAGE_HEADER_LENGTH));
// once. peer->updateUploadSpeed(length);
downloadContext_->updateUploadLength(length); downloadContext_->updateUploadSpeed(length);
} }
else { else {
throw DL_ABORT_EX(EX_DATA_READ); throw DL_ABORT_EX(EX_DATA_READ);

View File

@ -194,8 +194,8 @@ bool DownloadCommand::executeInternal()
bufSize = streamFilter_->getBytesProcessed(); bufSize = streamFilter_->getBytesProcessed();
} }
getSocketRecvBuffer()->drain(bufSize); getSocketRecvBuffer()->drain(bufSize);
peerStat_->updateDownloadLength(bufSize); peerStat_->updateDownload(bufSize);
getDownloadContext()->updateDownloadLength(bufSize); getDownloadContext()->updateDownload(bufSize);
} }
bool segmentPartComplete = false; bool segmentPartComplete = false;
// Note that GrowSegment::complete() always returns false. // Note that GrowSegment::complete() always returns false.

View File

@ -288,19 +288,28 @@ void DownloadContext::setSignature(std::unique_ptr<Signature> signature)
signature_ = std::move(signature); signature_ = std::move(signature);
} }
void DownloadContext::updateDownloadLength(size_t bytes) void DownloadContext::updateDownload(size_t bytes)
{ {
netStat_.updateDownloadLength(bytes); netStat_.updateDownload(bytes);
RequestGroupMan* rgman = ownerRequestGroup_->getRequestGroupMan(); RequestGroupMan* rgman = ownerRequestGroup_->getRequestGroupMan();
if (rgman) { if (rgman) {
rgman->getNetStat().updateDownloadLength(bytes); rgman->getNetStat().updateDownload(bytes);
}
}
void DownloadContext::updateUploadSpeed(size_t bytes)
{
netStat_.updateUploadSpeed(bytes);
auto rgman = ownerRequestGroup_->getRequestGroupMan();
if (rgman) {
rgman->getNetStat().updateUploadSpeed(bytes);
} }
} }
void DownloadContext::updateUploadLength(size_t bytes) void DownloadContext::updateUploadLength(size_t bytes)
{ {
netStat_.updateUploadLength(bytes); netStat_.updateUploadLength(bytes);
RequestGroupMan* rgman = ownerRequestGroup_->getRequestGroupMan(); auto rgman = ownerRequestGroup_->getRequestGroupMan();
if (rgman) { if (rgman) {
rgman->getNetStat().updateUploadLength(bytes); rgman->getNetStat().updateUploadLength(bytes);
} }

View File

@ -225,11 +225,12 @@ public:
// This method also updates global download length held by // This method also updates global download length held by
// RequestGroupMan via getOwnerRequestGroup(). // RequestGroupMan via getOwnerRequestGroup().
void updateDownloadLength(size_t bytes); void updateDownload(size_t bytes);
// This method also updates global upload length held by // This method also updates global upload length held by
// RequestGroupMan via getOwnerRequestGroup(). // RequestGroupMan via getOwnerRequestGroup().
void updateUploadLength(size_t bytes); void updateUploadLength(size_t bytes);
void updateUploadSpeed(size_t bytes);
}; };
} // namespace aria2 } // namespace aria2

View File

@ -68,18 +68,25 @@ int NetStat::calculateAvgUploadSpeed()
return avgUploadSpeed_ = uploadSpeed_.calculateAvgSpeed(); return avgUploadSpeed_ = uploadSpeed_.calculateAvgSpeed();
} }
void NetStat::updateDownloadLength(size_t bytes) void NetStat::updateDownload(size_t bytes)
{ {
downloadSpeed_.update(bytes); downloadSpeed_.update(bytes);
sessionDownloadLength_ += bytes; sessionDownloadLength_ += bytes;
} }
void NetStat::updateUploadLength(size_t bytes) void NetStat::updateUpload(size_t bytes)
{ {
uploadSpeed_.update(bytes); uploadSpeed_.update(bytes);
sessionUploadLength_ += bytes; sessionUploadLength_ += bytes;
} }
void NetStat::updateUploadSpeed(size_t bytes) { uploadSpeed_.update(bytes); }
void NetStat::updateUploadLength(size_t bytes)
{
sessionUploadLength_ += bytes;
}
int NetStat::getMaxDownloadSpeed() const int NetStat::getMaxDownloadSpeed() const
{ {
return downloadSpeed_.getMaxSpeed(); return downloadSpeed_.getMaxSpeed();

View File

@ -67,7 +67,11 @@ public:
int calculateAvgUploadSpeed(); int calculateAvgUploadSpeed();
void updateDownloadLength(size_t bytes); void updateDownload(size_t bytes);
void updateUpload(size_t bytes);
void updateUploadSpeed(size_t bytes);
void updateUploadLength(size_t bytes); void updateUploadLength(size_t bytes);

View File

@ -174,16 +174,22 @@ void Peer::snubbing(bool b)
res_->snubbing(b); res_->snubbing(b);
} }
void Peer::updateUploadSpeed(int32_t bytes)
{
assert(res_);
res_->updateUploadSpeed(bytes);
}
void Peer::updateUploadLength(int32_t bytes) void Peer::updateUploadLength(int32_t bytes)
{ {
assert(res_); assert(res_);
res_->updateUploadLength(bytes); res_->updateUploadLength(bytes);
} }
void Peer::updateDownloadLength(int32_t bytes) void Peer::updateDownload(int32_t bytes)
{ {
assert(res_); assert(res_);
res_->updateDownloadLength(bytes); res_->updateDownload(bytes);
} }
void Peer::updateSeeder() void Peer::updateSeeder()

View File

@ -171,9 +171,11 @@ public:
void snubbing(bool b); void snubbing(bool b);
void updateUploadSpeed(int32_t bytes);
void updateUploadLength(int32_t bytes); void updateUploadLength(int32_t bytes);
void updateDownloadLength(int32_t bytes); void updateDownload(int32_t bytes);
/** /**
* Returns the transfer rate from localhost to remote host. * Returns the transfer rate from localhost to remote host.

View File

@ -196,6 +196,11 @@ int64_t PeerSessionResource::uploadLength() const
return netStat_.getSessionUploadLength(); return netStat_.getSessionUploadLength();
} }
void PeerSessionResource::updateUploadSpeed(int32_t bytes)
{
netStat_.updateUploadSpeed(bytes);
}
void PeerSessionResource::updateUploadLength(int32_t bytes) void PeerSessionResource::updateUploadLength(int32_t bytes)
{ {
netStat_.updateUploadLength(bytes); netStat_.updateUploadLength(bytes);
@ -206,9 +211,9 @@ int64_t PeerSessionResource::downloadLength() const
return netStat_.getSessionDownloadLength(); return netStat_.getSessionDownloadLength();
} }
void PeerSessionResource::updateDownloadLength(int32_t bytes) void PeerSessionResource::updateDownload(int32_t bytes)
{ {
netStat_.updateDownloadLength(bytes); netStat_.updateDownload(bytes);
lastDownloadUpdate_ = global::wallclock(); lastDownloadUpdate_ = global::wallclock();
} }

View File

@ -182,11 +182,13 @@ public:
int64_t uploadLength() const; int64_t uploadLength() const;
void updateUploadSpeed(int32_t bytes);
void updateUploadLength(int32_t bytes); void updateUploadLength(int32_t bytes);
int64_t downloadLength() const; int64_t downloadLength() const;
void updateDownloadLength(int32_t bytes); void updateDownload(int32_t bytes);
const Timer& getLastDownloadUpdate() const { return lastDownloadUpdate_; } const Timer& getLastDownloadUpdate() const { return lastDownloadUpdate_; }

View File

@ -67,15 +67,9 @@ int PeerStat::calculateAvgUploadSpeed()
return netStat_.calculateAvgUploadSpeed(); return netStat_.calculateAvgUploadSpeed();
} }
void PeerStat::updateDownloadLength(size_t bytes) void PeerStat::updateDownload(size_t bytes) { netStat_.updateDownload(bytes); }
{
netStat_.updateDownloadLength(bytes);
}
void PeerStat::updateUploadLength(size_t bytes) void PeerStat::updateUpload(size_t bytes) { netStat_.updateUpload(bytes); }
{
netStat_.updateUploadLength(bytes);
}
int PeerStat::getMaxDownloadSpeed() const int PeerStat::getMaxDownloadSpeed() const
{ {

View File

@ -68,9 +68,9 @@ public:
int calculateAvgUploadSpeed(); int calculateAvgUploadSpeed();
void updateDownloadLength(size_t bytes); void updateDownload(size_t bytes);
void updateUploadLength(size_t bytes); void updateUpload(size_t bytes);
int getMaxDownloadSpeed() const; int getMaxDownloadSpeed() const;

View File

@ -67,8 +67,8 @@ public:
torrentAttrs->infoHash.assign(std::begin(infoHash), std::end(infoHash)); torrentAttrs->infoHash.assign(std::begin(infoHash), std::end(infoHash));
dctx_->setAttribute(CTX_ATTR_BT, std::move(torrentAttrs)); dctx_->setAttribute(CTX_ATTR_BT, std::move(torrentAttrs));
} }
dctx_->getNetStat().updateDownloadLength(pieceLength * 5); dctx_->getNetStat().updateDownload(pieceLength * 5);
dctx_->getNetStat().updateUploadLength(pieceLength * 6); dctx_->getNetStat().updateUpload(pieceLength * 6);
bittorrent::setStaticPeerId(peerId); bittorrent::setStaticPeerId(peerId);
pieceStorage_.reset(new MockPieceStorage()); pieceStorage_.reset(new MockPieceStorage());

View File

@ -214,7 +214,7 @@ void DefaultBtProgressInfoFileTest::testSave()
initializeMembers(1_k, 80_k); initializeMembers(1_k, 80_k);
dctx_->setBasePath(A2_TEST_OUT_DIR "/save-temp"); dctx_->setBasePath(A2_TEST_OUT_DIR "/save-temp");
dctx_->getNetStat().updateUploadLength(768); dctx_->getNetStat().updateUpload(768);
btRuntime_->setUploadLengthAtStartup(256); btRuntime_->setUploadLengthAtStartup(256);
bitfield_->setAllBit(); bitfield_->setAllBit();
bitfield_->unsetBit(79); bitfield_->unsetBit(79);

View File

@ -117,8 +117,8 @@ void PeerSessionResourceTest::testUpdateDownloadLength()
PeerSessionResource res(1_k, 1_m); PeerSessionResource res(1_k, 1_m);
CPPUNIT_ASSERT_EQUAL((int64_t)0LL, res.downloadLength()); CPPUNIT_ASSERT_EQUAL((int64_t)0LL, res.downloadLength());
res.updateDownloadLength(100); res.updateDownload(100);
res.updateDownloadLength(200); res.updateDownload(200);
CPPUNIT_ASSERT_EQUAL((int64_t)300LL, res.downloadLength()); CPPUNIT_ASSERT_EQUAL((int64_t)300LL, res.downloadLength());
} }