Skip to content

Commit

Permalink
Made HTTP/FTP download speed limiter more accurate
Browse files Browse the repository at this point in the history
We have to do the same thing for BitTorrent.
  • Loading branch information
tatsuhiro-t committed Oct 23, 2012
1 parent ea45600 commit 46bdaf0
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 51 deletions.
1 change: 0 additions & 1 deletion src/DownloadCommand.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ bool DownloadCommand::executeInternal() {
getSocketRecvBuffer()->shiftBuffer(bufSize);
peerStat_->updateDownloadLength(bufSize);
}
getSegmentMan()->updateDownloadSpeedFor(peerStat_);
bool segmentPartComplete = false;
// Note that GrowSegment::complete() always returns false.
if(sinkFilterOnly_) {
Expand Down
5 changes: 5 additions & 0 deletions src/PeerStat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,9 @@ void PeerStat::downloadStop()
status_ = PeerStat::IDLE;
}

bool PeerStat::affectsOverallSpeed() const
{
return !downloadSpeed_.isIntervalOver();
}

} // namespace aria2
4 changes: 4 additions & 0 deletions src/PeerStat.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ class PeerStat {
{
sessionDownloadLength_ += length;
}

// Returns true if the download speed of this object still affects
// overall download speed statistics.
bool affectsOverallSpeed() const;
};

} // namespace aria2
Expand Down
42 changes: 6 additions & 36 deletions src/SegmentMan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ SegmentMan::SegmentMan
: option_(option),
downloadContext_(downloadContext),
pieceStorage_(pieceStorage),
lastPeerStatDlspdMapUpdated_(0),
cachedDlspd_(0),
ignoreBitfield_(downloadContext->getPieceLength(),
downloadContext->getTotalLength())
{
Expand Down Expand Up @@ -350,13 +348,6 @@ int64_t SegmentMan::getDownloadLength() const {

void SegmentMan::registerPeerStat(const SharedHandle<PeerStat>& peerStat)
{
for(std::vector<SharedHandle<PeerStat> >::iterator i = peerStats_.begin(),
eoi = peerStats_.end(); i != eoi; ++i) {
if((*i)->getStatus() == PeerStat::IDLE) {
*i = peerStat;
return;
}
}
peerStats_.push_back(peerStat);
}

Expand Down Expand Up @@ -407,38 +398,17 @@ void SegmentMan::updateFastestPeerStat(const SharedHandle<PeerStat>& peerStat)
int SegmentMan::calculateDownloadSpeed()
{
int speed = 0;
if(lastPeerStatDlspdMapUpdated_.differenceInMillis(global::wallclock())+
A2_DELTA_MILLIS >= 250){
lastPeerStatDlspdMapUpdated_ = global::wallclock();
peerStatDlspdMap_.clear();
for(std::vector<SharedHandle<PeerStat> >::const_iterator i =
peerStats_.begin(), eoi = peerStats_.end(); i != eoi; ++i) {
if((*i)->getStatus() == PeerStat::ACTIVE) {
int s = (*i)->calculateDownloadSpeed();
peerStatDlspdMap_[(*i)->getCuid()] = s;
speed += s;
}
for(std::vector<SharedHandle<PeerStat> >::const_iterator i =
peerStats_.begin(), eoi = peerStats_.end(); i != eoi; ++i) {
// PeerStat which is IDLE but its last download speed calculation
// interval is not over must be added to the result.
if((*i)->getStatus() == PeerStat::ACTIVE || (*i)->affectsOverallSpeed()) {
speed += (*i)->calculateDownloadSpeed();
}
cachedDlspd_ = speed;
} else {
speed = cachedDlspd_;
}
return speed;
}

void SegmentMan::updateDownloadSpeedFor(const SharedHandle<PeerStat>& pstat)
{
int newspd = pstat->calculateDownloadSpeed();
int oldSpd = peerStatDlspdMap_[pstat->getCuid()];
if(cachedDlspd_ > oldSpd) {
cachedDlspd_ -= oldSpd;
cachedDlspd_ += newspd;
} else {
cachedDlspd_ = newspd;
}
peerStatDlspdMap_[pstat->getCuid()] = newspd;
}

namespace {
class PeerStatDownloadLengthOperator {
public:
Expand Down
9 changes: 0 additions & 9 deletions src/SegmentMan.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,6 @@ class SegmentMan {
// Keep track of fastest PeerStat for each server
std::vector<SharedHandle<PeerStat> > fastestPeerStats_;

// key: PeerStat's cuid, value: its download speed
std::map<cuid_t, int> peerStatDlspdMap_;

Timer lastPeerStatDlspdMapUpdated_;

int cachedDlspd_;

BitfieldMan ignoreBitfield_;

SharedHandle<Segment> checkoutSegment(cuid_t cuid,
Expand Down Expand Up @@ -232,8 +225,6 @@ class SegmentMan {
*/
int calculateDownloadSpeed();

void updateDownloadSpeedFor(const SharedHandle<PeerStat>& pstat);

/**
* Returns the downloaded bytes in this session.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/SpeedCalc.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ class SpeedCalc {
int64_t accumulatedLength_;
time_t nextInterval_;

bool isIntervalOver() const;

bool isIntervalOver(int64_t milliElapsed) const;

void changeSw();
public:
SpeedCalc();

bool isIntervalOver() const;

/**
* Returns download/upload speed in byte per sec
*/
Expand Down
3 changes: 0 additions & 3 deletions test/SegmentManTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,6 @@ void SegmentManTest::testRegisterPeerStat()
CPPUNIT_ASSERT_EQUAL((size_t)1, segman.getPeerStats().size());
SharedHandle<PeerStat> p2(new PeerStat(0, "host2", "http"));
segman.registerPeerStat(p2);
CPPUNIT_ASSERT_EQUAL((size_t)1, segman.getPeerStats().size());
p2->downloadStart();
segman.registerPeerStat(p1);
CPPUNIT_ASSERT_EQUAL((size_t)2, segman.getPeerStats().size());
}

Expand Down

0 comments on commit 46bdaf0

Please sign in to comment.