diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2007-06-20 14:43:34 +0000 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2007-06-20 14:43:34 +0000 |
| commit | d7155e8f6c1460eb5ff57164da6a05a470ca0e65 (patch) | |
| tree | 37fee8fa875375c2f8bf0653ff8e8c35a632b70e /src/SpeedCalc.cc | |
| parent | be9dec85357a96e1cf77f743ccf83941a9572aad (diff) | |
2007-06-20 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Reduce the number of calls to gettimeofday to lower CPU load.
* src/TimeA2.h, src/TimeA2.cc (differenceInMillis): New
function.
* src/SpeedCalc.h, src/SpeedCalc.cc (calculateSpeed): New
function.
* src/Peer.h (calculateUploadSpeed): New function.
(calculateDownloadSpeed): New function.
* src/DefaultPeerStorage.cc
(CalculateStat): Added _now variable.
In operator(), call Peer::calculateDownloadSpeed(const struct
timeval&)
and Peer::calculateUploadSpeed(const struct timeval&)
Drop connection if no request or piece message is exchanged in
a certain interval.
* src/DefaultBtInteractive.h
(btRuntime): New variable.
(inactiveCheckPoint): New variable.
(checkActiveInteraction): New function.
* src/DefaultBtInteractive.cc (receiveMessages): Reset timer
when
request or piece message is received.
(checkActiveInteraction): New function.
(doInteractionProcessing): Call checkActiveInteraction.
Fixed the bug that causes remote Metalink and Torrent files are
not
processed.
* src/MultiUrlRequestInfo.cc (createNextRequestInfo): Fixed the
bug.
Diffstat (limited to 'src/SpeedCalc.cc')
| -rw-r--r-- | src/SpeedCalc.cc | 34 |
1 files changed, 23 insertions, 11 deletions
diff --git a/src/SpeedCalc.cc b/src/SpeedCalc.cc index af3d2d7f..65346de4 100644 --- a/src/SpeedCalc.cc +++ b/src/SpeedCalc.cc @@ -57,12 +57,24 @@ void SpeedCalc::reset() { nextInterval = CHANGE_INTERVAL_SEC; } -int SpeedCalc::calculateSpeed() { - int milliElapsed = cpArray[sw].differenceInMillis(); +int32_t SpeedCalc::calculateSpeed() { + int32_t milliElapsed = cpArray[sw].differenceInMillis(); if(milliElapsed) { - int speed = lengthArray[sw]*1000/milliElapsed; + int32_t speed = lengthArray[sw]*1000/milliElapsed; prevSpeed = speed; - maxSpeed = max<int>(speed, maxSpeed); + maxSpeed = max<int32_t>(speed, maxSpeed); + return speed; + } else { + return prevSpeed; + } +} + +int32_t SpeedCalc::calculateSpeed(const struct timeval& now) { + int64_t milliElapsed = cpArray[sw].differenceInMillis(now); + if(milliElapsed) { + int32_t speed = lengthArray[sw]*1000/milliElapsed; + prevSpeed = speed; + maxSpeed = max<int32_t>(speed, maxSpeed); return speed; } else { return prevSpeed; @@ -71,16 +83,16 @@ int SpeedCalc::calculateSpeed() { class Plus { private: - int d; + int32_t d; public: - Plus(int d):d(d) {} + Plus(int32_t d):d(d) {} - void operator()(long long int& length) { + void operator()(int64_t& length) { length += d; } }; -void SpeedCalc::update(int bytes) { +void SpeedCalc::update(int32_t bytes) { accumulatedLength += bytes; for_each(&lengthArray[0], &lengthArray[2], Plus(bytes)); if(isIntervalOver()) { @@ -99,10 +111,10 @@ void SpeedCalc::changeSw() { nextInterval = cpArray[sw].difference()+CHANGE_INTERVAL_SEC; } -int SpeedCalc::getAvgSpeed() const { - int milliElapsed = start.differenceInMillis(); +int32_t SpeedCalc::getAvgSpeed() const { + int32_t milliElapsed = start.differenceInMillis(); if(milliElapsed) { - int speed = accumulatedLength*1000/milliElapsed; + int32_t speed = accumulatedLength*1000/milliElapsed; return speed; } else { return 0; |
