Simplified TransferStat struct

pull/31/head
Tatsuhiro Tsujikawa 2012-10-26 00:16:20 +09:00
parent 0ecfa19925
commit 21c3903af0
8 changed files with 58 additions and 140 deletions

View File

@ -81,7 +81,7 @@ bool ActivePeerConnectionCommand::execute() {
} }
if(checkPoint_.difference(global::wallclock()) >= interval_) { if(checkPoint_.difference(global::wallclock()) >= interval_) {
checkPoint_ = global::wallclock(); checkPoint_ = global::wallclock();
TransferStat tstat = requestGroup_->calculateStat(); NetStat& stat = requestGroup_->getDownloadContext()->getNetStat();
const int maxDownloadLimit = requestGroup_->getMaxDownloadSpeedLimit(); const int maxDownloadLimit = requestGroup_->getMaxDownloadSpeedLimit();
const int maxUploadLimit = requestGroup_->getMaxUploadSpeedLimit(); const int maxUploadLimit = requestGroup_->getMaxUploadSpeedLimit();
int thresholdSpeed; int thresholdSpeed;
@ -97,10 +97,11 @@ bool ActivePeerConnectionCommand::execute() {
} }
if(// for seeder state if(// for seeder state
(pieceStorage_->downloadFinished() && btRuntime_->lessThanMaxPeers() && (pieceStorage_->downloadFinished() && btRuntime_->lessThanMaxPeers() &&
(maxUploadLimit == 0 || tstat.getUploadSpeed() < maxUploadLimit*0.8)) || (maxUploadLimit == 0 ||
stat.calculateUploadSpeed() < maxUploadLimit*0.8)) ||
// for leecher state // for leecher state
(!pieceStorage_->downloadFinished() && (!pieceStorage_->downloadFinished() &&
(tstat.getDownloadSpeed() < thresholdSpeed || (stat.calculateDownloadSpeed() < thresholdSpeed ||
btRuntime_->lessThanMinPeers()))) { btRuntime_->lessThanMinPeers()))) {
int numConnection = 0; int numConnection = 0;

View File

@ -42,6 +42,7 @@
#include "util.h" #include "util.h"
#include "fmt.h" #include "fmt.h"
#include "DownloadEngine.h" #include "DownloadEngine.h"
#include "DownloadContext.h"
namespace aria2 { namespace aria2 {
@ -72,7 +73,8 @@ void BtStopDownloadCommand::preProcess()
void BtStopDownloadCommand::process() void BtStopDownloadCommand::process()
{ {
if(requestGroup_->calculateStat().getDownloadSpeed() > 0) { NetStat& stat = requestGroup_->getDownloadContext()->getNetStat();
if(stat.calculateDownloadSpeed() > 0) {
checkPoint_ = global::wallclock(); checkPoint_ = global::wallclock();
} }
} }

View File

@ -102,8 +102,8 @@ void printProgress
{ {
TransferStat stat = rg->calculateStat(); TransferStat stat = rg->calculateStat();
int eta = 0; int eta = 0;
if(rg->getTotalLength() > 0 && stat.getDownloadSpeed() > 0) { if(rg->getTotalLength() > 0 && stat.downloadSpeed > 0) {
eta = (rg->getTotalLength()-rg->getCompletedLength())/stat.getDownloadSpeed(); eta = (rg->getTotalLength()-rg->getCompletedLength())/stat.downloadSpeed;
} }
o << "[" o << "["
@ -116,7 +116,7 @@ void printProgress
o << "SEEDING" << "(" << "ratio:"; o << "SEEDING" << "(" << "ratio:";
if(rg->getCompletedLength() > 0) { if(rg->getCompletedLength() > 0) {
o << std::fixed << std::setprecision(1) o << std::fixed << std::setprecision(1)
<< ((stat.getAllTimeUploadLength()*10)/rg->getCompletedLength())/10.0; << ((stat.allTimeUploadLength*10)/rg->getCompletedLength())/10.0;
} else { } else {
o << "--"; o << "--";
} }
@ -152,13 +152,13 @@ void printProgress
if(!rg->downloadFinished()) { if(!rg->downloadFinished()) {
o << " " o << " "
<< "SPD:" << "SPD:"
<< sizeFormatter(stat.getDownloadSpeed()) << "Bs"; << sizeFormatter(stat.downloadSpeed) << "Bs";
} }
if(stat.getSessionUploadLength() > 0) { if(stat.sessionUploadLength > 0) {
o << " " o << " "
<< "UP:" << "UP:"
<< sizeFormatter(stat.getUploadSpeed()) << "Bs" << sizeFormatter(stat.uploadSpeed) << "Bs"
<< "(" << sizeFormatter(stat.getAllTimeUploadLength()) << "B)"; << "(" << sizeFormatter(stat.allTimeUploadLength) << "B)";
} }
if(eta > 0) { if(eta > 0) {
o << " " o << " "

View File

@ -125,8 +125,7 @@ bool PeerReceiveHandshakeCommand::executeInternal()
" Dropping connection."); " Dropping connection.");
return true; return true;
} }
TransferStat tstat = NetStat& stat = downloadContext->getNetStat();
downloadContext->getOwnerRequestGroup()->calculateStat();
const int maxDownloadLimit = const int maxDownloadLimit =
downloadContext->getOwnerRequestGroup()->getMaxDownloadSpeedLimit(); downloadContext->getOwnerRequestGroup()->getMaxDownloadSpeedLimit();
int thresholdSpeed = int thresholdSpeed =
@ -137,7 +136,7 @@ bool PeerReceiveHandshakeCommand::executeInternal()
} }
if((!pieceStorage->downloadFinished() && if((!pieceStorage->downloadFinished() &&
tstat.getDownloadSpeed() < thresholdSpeed) || stat.calculateDownloadSpeed() < thresholdSpeed) ||
btRuntime->lessThanMaxPeers()) { btRuntime->lessThanMaxPeers()) {
if(peerStorage->addPeer(getPeer())) { if(peerStorage->addPeer(getPeer())) {
getPeer()->usedBy(getCuid()); getPeer()->usedBy(getCuid());

View File

@ -1154,7 +1154,7 @@ SharedHandle<DownloadResult> RequestGroup::createDownloadResult() const
res->gid = gid_; res->gid = gid_;
res->fileEntries = downloadContext_->getFileEntries(); res->fileEntries = downloadContext_->getFileEntries();
res->inMemoryDownload = inMemoryDownload_; res->inMemoryDownload = inMemoryDownload_;
res->sessionDownloadLength = st.getSessionDownloadLength(); res->sessionDownloadLength = st.sessionDownloadLength;
res->sessionTime = downloadContext_->calculateSessionTime(); res->sessionTime = downloadContext_->calculateSessionTime();
res->result = downloadResult(); res->result = downloadResult();
res->followedBy = followedByGIDs_; res->followedBy = followedByGIDs_;
@ -1163,7 +1163,7 @@ SharedHandle<DownloadResult> RequestGroup::createDownloadResult() const
res->metadataInfo = metadataInfo_; res->metadataInfo = metadataInfo_;
res->totalLength = getTotalLength(); res->totalLength = getTotalLength();
res->completedLength = getCompletedLength(); res->completedLength = getCompletedLength();
res->uploadLength = st.getAllTimeUploadLength(); res->uploadLength = st.allTimeUploadLength;
if(pieceStorage_) { if(pieceStorage_) {
if(pieceStorage_->getBitfieldLength() > 0) { if(pieceStorage_->getBitfieldLength() > 0) {
res->bitfield.assign(pieceStorage_->getBitfield(), res->bitfield.assign(pieceStorage_->getBitfield(),
@ -1193,13 +1193,13 @@ void RequestGroup::reportDownloadFinished()
TransferStat stat = calculateStat(); TransferStat stat = calculateStat();
int64_t completedLength = getCompletedLength(); int64_t completedLength = getCompletedLength();
double shareRatio = completedLength == 0 ? 0.0 : double shareRatio = completedLength == 0 ? 0.0 :
1.0*stat.getAllTimeUploadLength()/completedLength; 1.0*stat.allTimeUploadLength/completedLength;
SharedHandle<TorrentAttribute> attrs = SharedHandle<TorrentAttribute> attrs =
bittorrent::getTorrentAttrs(downloadContext_); bittorrent::getTorrentAttrs(downloadContext_);
if(!attrs->metadata.empty()) { if(!attrs->metadata.empty()) {
A2_LOG_NOTICE(fmt(MSG_SHARE_RATIO_REPORT, A2_LOG_NOTICE(fmt(MSG_SHARE_RATIO_REPORT,
shareRatio, shareRatio,
util::abbrevSize(stat.getAllTimeUploadLength()).c_str(), util::abbrevSize(stat.allTimeUploadLength).c_str(),
util::abbrevSize(completedLength).c_str())); util::abbrevSize(completedLength).c_str()));
} }
} }
@ -1254,14 +1254,14 @@ void RequestGroup::setTimeout(time_t timeout)
bool RequestGroup::doesDownloadSpeedExceed() bool RequestGroup::doesDownloadSpeedExceed()
{ {
return maxDownloadSpeedLimit_ > 0 && int spd = downloadContext_->getNetStat().calculateDownloadSpeed();
maxDownloadSpeedLimit_ < calculateStat().getDownloadSpeed(); return maxDownloadSpeedLimit_ > 0 && maxDownloadSpeedLimit_ < spd;
} }
bool RequestGroup::doesUploadSpeedExceed() bool RequestGroup::doesUploadSpeedExceed()
{ {
return maxUploadSpeedLimit_ > 0 && int spd = downloadContext_->getNetStat().calculateUploadSpeed();
maxUploadSpeedLimit_ < calculateStat().getUploadSpeed(); return maxUploadSpeedLimit_ > 0 && maxUploadSpeedLimit_ < spd;
} }
void RequestGroup::saveControlFile() const void RequestGroup::saveControlFile() const

View File

@ -657,14 +657,14 @@ void gatherProgressCommon
} }
TransferStat stat = group->calculateStat(); TransferStat stat = group->calculateStat();
if(requested_key(keys, KEY_DOWNLOAD_SPEED)) { if(requested_key(keys, KEY_DOWNLOAD_SPEED)) {
entryDict->put(KEY_DOWNLOAD_SPEED, util::itos(stat.getDownloadSpeed())); entryDict->put(KEY_DOWNLOAD_SPEED, util::itos(stat.downloadSpeed));
} }
if(requested_key(keys, KEY_UPLOAD_SPEED)) { if(requested_key(keys, KEY_UPLOAD_SPEED)) {
entryDict->put(KEY_UPLOAD_SPEED, util::itos(stat.getUploadSpeed())); entryDict->put(KEY_UPLOAD_SPEED, util::itos(stat.uploadSpeed));
} }
if(requested_key(keys, KEY_UPLOAD_LENGTH)) { if(requested_key(keys, KEY_UPLOAD_LENGTH)) {
entryDict->put entryDict->put
(KEY_UPLOAD_LENGTH, util::itos(stat.getAllTimeUploadLength())); (KEY_UPLOAD_LENGTH, util::itos(stat.allTimeUploadLength));
} }
if(requested_key(keys, KEY_CONNECTIONS)) { if(requested_key(keys, KEY_CONNECTIONS)) {
entryDict->put(KEY_CONNECTIONS, util::itos(group->getNumConnection())); entryDict->put(KEY_CONNECTIONS, util::itos(group->getNumConnection()));

View File

@ -2,7 +2,7 @@
/* /*
* aria2 - The high speed download utility * aria2 - The high speed download utility
* *
* Copyright (C) 2006 Tatsuhiro Tsujikawa * Copyright (C) 2012 Tatsuhiro Tsujikawa
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -34,67 +34,46 @@
/* copyright --> */ /* copyright --> */
#include "TransferStat.h" #include "TransferStat.h"
#include <algorithm>
namespace aria2 { namespace aria2 {
TransferStat operator+(const TransferStat& a, const TransferStat& b) TransferStat operator+(const TransferStat& a, const TransferStat& b)
{ {
TransferStat c; TransferStat c(a);
c.downloadSpeed = a.downloadSpeed+b.downloadSpeed; c += b;
c.uploadSpeed = a.uploadSpeed+b.uploadSpeed;
c.sessionDownloadLength = a.sessionDownloadLength+b.sessionDownloadLength;
c.sessionUploadLength = a.sessionUploadLength+b.sessionUploadLength;
return c; return c;
} }
TransferStat operator-(const TransferStat& a, const TransferStat& b) TransferStat operator-(const TransferStat& a, const TransferStat& b)
{ {
TransferStat c; TransferStat c(a);
if(a.downloadSpeed > b.downloadSpeed) { c -= b;
c.downloadSpeed = a.downloadSpeed-b.downloadSpeed;
}
if(a.uploadSpeed > b.uploadSpeed) {
c.uploadSpeed = a.uploadSpeed-b.uploadSpeed;
}
if(a.sessionDownloadLength > b.sessionDownloadLength) {
c.sessionDownloadLength = a.sessionDownloadLength-b.sessionDownloadLength;
}
if(a.sessionUploadLength > b.sessionUploadLength) {
c.sessionUploadLength = a.sessionUploadLength-b.sessionUploadLength;
}
return c; return c;
} }
TransferStat& TransferStat::operator+=(const TransferStat& stat) TransferStat& TransferStat::operator+=(const TransferStat& b)
{ {
downloadSpeed += stat.downloadSpeed; downloadSpeed += b.downloadSpeed;
uploadSpeed += stat.uploadSpeed; uploadSpeed += b.uploadSpeed;
sessionDownloadLength += stat.sessionDownloadLength; sessionDownloadLength += b.sessionDownloadLength;
sessionUploadLength += stat.sessionUploadLength; sessionUploadLength += b.sessionUploadLength;
return *this; return *this;
} }
TransferStat& TransferStat::operator-=(const TransferStat& stat) TransferStat& TransferStat::operator-=(const TransferStat& b)
{ {
if(downloadSpeed > stat.downloadSpeed) { downloadSpeed -= b.downloadSpeed;
downloadSpeed -= stat.downloadSpeed; uploadSpeed -= b.uploadSpeed;
} else { sessionDownloadLength -= b.sessionDownloadLength;
downloadSpeed = 0; sessionUploadLength -= b.sessionUploadLength;
}
if(uploadSpeed > stat.uploadSpeed) { downloadSpeed = std::max(0, downloadSpeed);
uploadSpeed -= stat.uploadSpeed; uploadSpeed = std::max(0, uploadSpeed);
} else { sessionDownloadLength = std::max(static_cast<int64_t>(0),
uploadSpeed = 0; sessionDownloadLength);
} sessionUploadLength = std::max(static_cast<int64_t>(0),
if(sessionDownloadLength > stat.sessionDownloadLength) { sessionUploadLength);
sessionDownloadLength -= stat.sessionDownloadLength;
} else {
sessionDownloadLength = 0;
}
if(sessionUploadLength > stat.sessionUploadLength) {
sessionUploadLength -= stat.sessionUploadLength;
} else {
sessionUploadLength = 0;
}
return *this; return *this;
} }

View File

@ -2,7 +2,7 @@
/* /*
* aria2 - The high speed download utility * aria2 - The high speed download utility
* *
* Copyright (C) 2006 Tatsuhiro Tsujikawa * Copyright (C) 2012 Tatsuhiro Tsujikawa
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -36,93 +36,30 @@
#define D_TRANSFER_STAT_H #define D_TRANSFER_STAT_H
#include "common.h" #include "common.h"
#include <stdint.h>
namespace aria2 { namespace aria2 {
class TransferStat { struct TransferStat {
public:
int downloadSpeed;
int uploadSpeed;
int64_t sessionDownloadLength;
int64_t sessionUploadLength;
int64_t allTimeUploadLength;
void copy(const TransferStat& stat)
{
downloadSpeed = stat.downloadSpeed;
uploadSpeed = stat.uploadSpeed;
sessionDownloadLength = stat.sessionDownloadLength;
sessionUploadLength = stat.sessionUploadLength;
allTimeUploadLength = stat.allTimeUploadLength;
}
public:
TransferStat():downloadSpeed(0), uploadSpeed(0), TransferStat():downloadSpeed(0), uploadSpeed(0),
sessionDownloadLength(0), sessionUploadLength(0), sessionDownloadLength(0), sessionUploadLength(0),
allTimeUploadLength(0) {} allTimeUploadLength(0) {}
TransferStat(const TransferStat& stat)
{
copy(stat);
}
TransferStat& operator=(const TransferStat& stat)
{
if(this != &stat) {
copy(stat);
}
return *this;
}
friend TransferStat operator+(const TransferStat& a, const TransferStat& b);
friend TransferStat operator-(const TransferStat& a, const TransferStat& b);
TransferStat& operator+=(const TransferStat& stat); TransferStat& operator+=(const TransferStat& stat);
TransferStat& operator-=(const TransferStat& stat); TransferStat& operator-=(const TransferStat& stat);
int getDownloadSpeed() const { int downloadSpeed;
return downloadSpeed; int uploadSpeed;
}
void setDownloadSpeed(int s) { downloadSpeed = s; }
int getUploadSpeed() const {
return uploadSpeed;
}
void setUploadSpeed(int s) { uploadSpeed = s; }
/** /**
* Returns the number of bytes downloaded since the program started. * Returns the number of bytes downloaded since the program started.
* This is not the total number of bytes downloaded. * This is not the total number of bytes downloaded.
*/ */
int64_t getSessionDownloadLength() const { int64_t sessionDownloadLength;
return sessionDownloadLength;
}
void setSessionDownloadLength(int64_t s) { sessionDownloadLength = s; }
/** /**
* Returns the number of bytes uploaded since the program started. * Returns the number of bytes uploaded since the program started.
* This is not the total number of bytes uploaded. * This is not the total number of bytes uploaded.
*/ */
int64_t getSessionUploadLength() const { int64_t sessionUploadLength;
return sessionUploadLength; int64_t allTimeUploadLength;
}
void setSessionUploadLength(int64_t s) { sessionUploadLength = s; }
void setAllTimeUploadLength(int64_t s)
{
allTimeUploadLength = s;
}
int64_t getAllTimeUploadLength() const
{
return allTimeUploadLength;
}
}; };
TransferStat operator+(const TransferStat& a, const TransferStat& b); TransferStat operator+(const TransferStat& a, const TransferStat& b);