diff --git a/ChangeLog b/ChangeLog index d0fcf446..f756a2ed 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2010-04-11 Tatsuhiro Tsujikawa + + Use clock_gettime(CLOCK_MONOTONIC, ...) if it is available and + usable to prevent from aria2 from being affected by system time + change. + 2010-04-09 Tatsuhiro Tsujikawa Added aria2.pause and aria2.unpause XML-RPC method. aria2.pause diff --git a/config.h.in b/config.h.in index dbbd2b62..c248efbe 100644 --- a/config.h.in +++ b/config.h.in @@ -81,6 +81,9 @@ the CoreFoundation framework. */ #undef HAVE_CFPREFERENCESCOPYAPPVALUE +/* Define to 1 if you have the `clock_gettime' function. */ +#undef HAVE_CLOCK_GETTIME + /* Define to 1 if you have the `daemon' function. */ #undef HAVE_DAEMON @@ -418,6 +421,9 @@ /* Define to 1 if you have the `strtoull' function. */ #undef HAVE_STRTOULL +/* Define to 1 if the system has the type `struct timespec'. */ +#undef HAVE_STRUCT_TIMESPEC + /* Define to 1 if you have the header file. */ #undef HAVE_SYS_IOCTL_H diff --git a/configure b/configure index 0210dc8c..e5aa36ef 100755 --- a/configure +++ b/configure @@ -7431,6 +7431,63 @@ else fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing clock_gettime" >&5 +$as_echo_n "checking for library containing clock_gettime... " >&6; } +if test "${ac_cv_search_clock_gettime+set}" = set; then : + $as_echo_n "(cached) " >&6 +else + ac_func_search_save_LIBS=$LIBS +cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + +/* Override any GCC internal prototype to avoid an error. + Use char because int might match the return type of a GCC + builtin and then its argument prototype would still apply. */ +#ifdef __cplusplus +extern "C" +#endif +char clock_gettime (); +int +main () +{ +return clock_gettime (); + ; + return 0; +} +_ACEOF +for ac_lib in '' rt; do + if test -z "$ac_lib"; then + ac_res="none required" + else + ac_res=-l$ac_lib + LIBS="-l$ac_lib $ac_func_search_save_LIBS" + fi + if ac_fn_cxx_try_link "$LINENO"; then : + ac_cv_search_clock_gettime=$ac_res +fi +rm -f core conftest.err conftest.$ac_objext \ + conftest$ac_exeext + if test "${ac_cv_search_clock_gettime+set}" = set; then : + break +fi +done +if test "${ac_cv_search_clock_gettime+set}" = set; then : + +else + ac_cv_search_clock_gettime=no +fi +rm conftest.$ac_ext +LIBS=$ac_func_search_save_LIBS +fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_clock_gettime" >&5 +$as_echo "$ac_cv_search_clock_gettime" >&6; } +ac_res=$ac_cv_search_clock_gettime +if test "$ac_res" != no; then : + test "$ac_res" = "none required" || LIBS="$ac_res $LIBS" + +fi + + # Checks for header files. # The Ultrix 4.2 mips builtin alloca declared by alloca.h only works # for constant arguments. Useless! @@ -8295,6 +8352,15 @@ cat >>confdefs.h <<_ACEOF _ACEOF +fi +ac_fn_cxx_check_type "$LINENO" "struct timespec" "ac_cv_type_struct_timespec" "$ac_includes_default" +if test "x$ac_cv_type_struct_timespec" = x""yes; then : + +cat >>confdefs.h <<_ACEOF +#define HAVE_STRUCT_TIMESPEC 1 +_ACEOF + + fi { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether byte ordering is bigendian" >&5 @@ -14496,6 +14562,7 @@ for ac_func in __argz_count \ __argz_next \ __argz_stringify \ atexit \ + clock_gettime \ ftruncate \ getcwd \ gethostbyaddr \ diff --git a/configure.ac b/configure.ac index e2d3524f..f1ea59fc 100644 --- a/configure.ac +++ b/configure.ac @@ -208,6 +208,8 @@ AM_CONDITIONAL([HAVE_LIBZ], [test "x$have_libz" = "xyes"]) # Set conditional for sqlite3 AM_CONDITIONAL([HAVE_SQLITE3], [test "x$have_sqlite3" = "xyes"]) +AC_SEARCH_LIBS([clock_gettime], [rt]) + # Checks for header files. AC_FUNC_ALLOCA AC_HEADER_STDC @@ -268,7 +270,7 @@ AC_TYPE_UINT64_T AC_TYPE_UINT8_T AC_TYPE_PID_T AC_C_VOLATILE -AC_CHECK_TYPES([ptrdiff_t]) +AC_CHECK_TYPES([ptrdiff_t, struct timespec]) AC_C_BIGENDIAN AC_SYS_LARGEFILE @@ -291,6 +293,7 @@ AC_CHECK_FUNCS([__argz_count \ __argz_next \ __argz_stringify \ atexit \ + clock_gettime \ ftruncate \ getcwd \ gethostbyaddr \ diff --git a/src/AbstractCommand.cc b/src/AbstractCommand.cc index abe36b8b..5c84bbd9 100644 --- a/src/AbstractCommand.cc +++ b/src/AbstractCommand.cc @@ -75,7 +75,7 @@ AbstractCommand::AbstractCommand(cuid_t cuid, RequestGroup* requestGroup, DownloadEngine* e, const SocketHandle& s): - Command(cuid), _requestGroup(requestGroup), + Command(cuid), checkPoint(global::wallclock), _requestGroup(requestGroup), req(req), _fileEntry(fileEntry), e(e), socket(s), checkSocketIsReadable(false), checkSocketIsWritable(false), nameResolverCheck(false) diff --git a/src/AbstractCommand.h b/src/AbstractCommand.h index 8e7e6e98..fc793cee 100644 --- a/src/AbstractCommand.h +++ b/src/AbstractCommand.h @@ -37,7 +37,7 @@ #include "Command.h" #include "SharedHandle.h" -#include "TimeA2.h" +#include "TimerA2.h" #include "FileEntry.h" #include "RequestGroup.h" @@ -55,7 +55,7 @@ class AsyncNameResolver; class AbstractCommand : public Command { private: - Time checkPoint; + Timer checkPoint; time_t timeout; protected: RequestGroup* _requestGroup; diff --git a/src/ActivePeerConnectionCommand.h b/src/ActivePeerConnectionCommand.h index ff58b6ba..e4d081e4 100644 --- a/src/ActivePeerConnectionCommand.h +++ b/src/ActivePeerConnectionCommand.h @@ -37,7 +37,7 @@ #include "Command.h" #include "SharedHandle.h" -#include "TimeA2.h" +#include "TimerA2.h" namespace aria2 { @@ -59,7 +59,7 @@ private: time_t interval; // UNIT: sec DownloadEngine* e; - Time checkPoint; + Timer checkPoint; unsigned int _numNewConnection; // the number of the connection to establish. public: ActivePeerConnectionCommand(cuid_t cuid, diff --git a/src/AdaptiveURISelector.cc b/src/AdaptiveURISelector.cc index f1537429..fb0c83e6 100644 --- a/src/AdaptiveURISelector.cc +++ b/src/AdaptiveURISelector.cc @@ -51,7 +51,6 @@ #include "SimpleRandomizer.h" #include "SocketCore.h" #include "FileEntry.h" -#include "wallclock.h" namespace aria2 { @@ -330,7 +329,7 @@ std::string AdaptiveURISelector::getFirstToTestUri power = (int)pow(2.0, (float)counter); /* We test the mirror another time if it has not been * tested since 2^counter days */ - if(ss->getLastUpdated().difference(global::wallclock) > power*24*60*60) { + if(ss->getLastUpdated().difference() > power*24*60*60) { return *i; } } diff --git a/src/BtLeecherStateChoke.cc b/src/BtLeecherStateChoke.cc index 7fd6ca8c..266e5723 100644 --- a/src/BtLeecherStateChoke.cc +++ b/src/BtLeecherStateChoke.cc @@ -39,7 +39,6 @@ #include "Peer.h" #include "Logger.h" #include "LogFactory.h" -#include "a2time.h" #include "SimpleRandomizer.h" #include "wallclock.h" @@ -216,7 +215,7 @@ BtLeecherStateChoke::executeChoke } } -const Time& BtLeecherStateChoke::getLastRound() const +const Timer& BtLeecherStateChoke::getLastRound() const { return _lastRound; } diff --git a/src/BtLeecherStateChoke.h b/src/BtLeecherStateChoke.h index 062342d1..89914bec 100644 --- a/src/BtLeecherStateChoke.h +++ b/src/BtLeecherStateChoke.h @@ -40,7 +40,7 @@ #include #include "SharedHandle.h" -#include "TimeA2.h" +#include "TimerA2.h" namespace aria2 { @@ -51,7 +51,7 @@ class BtLeecherStateChoke { private: int _round; - Time _lastRound; + Timer _lastRound; Logger* _logger; @@ -95,7 +95,7 @@ public: void executeChoke(const std::vector >& peerSet); - const Time& getLastRound() const; + const Timer& getLastRound() const; }; } // namespace aria2 diff --git a/src/BtSeederStateChoke.cc b/src/BtSeederStateChoke.cc index 4d12fd37..48f28dfc 100644 --- a/src/BtSeederStateChoke.cc +++ b/src/BtSeederStateChoke.cc @@ -69,7 +69,7 @@ BtSeederStateChoke::PeerEntry::operator<(const PeerEntry& rhs) const return false; } if(this->_recentUnchoking && - this->_lastAmUnchoking.isNewer(rhs._lastAmUnchoking)) { + (this->_lastAmUnchoking > rhs._lastAmUnchoking)) { return true; } else if(rhs._recentUnchoking) { return false; diff --git a/src/BtSeederStateChoke.h b/src/BtSeederStateChoke.h index eec74728..932bc3f5 100644 --- a/src/BtSeederStateChoke.h +++ b/src/BtSeederStateChoke.h @@ -40,7 +40,7 @@ #include #include "SharedHandle.h" -#include "TimeA2.h" +#include "TimerA2.h" namespace aria2 { @@ -51,7 +51,7 @@ class BtSeederStateChoke { private: int _round; - Time _lastRound; + Timer _lastRound; Logger* _logger; @@ -59,7 +59,7 @@ private: private: SharedHandle _peer; size_t _outstandingUpload; - Time _lastAmUnchoking; + Timer _lastAmUnchoking; bool _recentUnchoking; unsigned int _uploadSpeed; @@ -87,7 +87,7 @@ public: void executeChoke(const std::vector >& peerSet); - const Time& getLastRound() const { return _lastRound; } + const Timer& getLastRound() const { return _lastRound; } }; } // namespace aria2 diff --git a/src/BtStopDownloadCommand.h b/src/BtStopDownloadCommand.h index 193b5439..715ccbcc 100644 --- a/src/BtStopDownloadCommand.h +++ b/src/BtStopDownloadCommand.h @@ -52,7 +52,7 @@ private: time_t _timeout; - Time _checkPoint; + Timer _checkPoint; SharedHandle _btRuntime; diff --git a/src/CheckIntegrityCommand.h b/src/CheckIntegrityCommand.h index b950a43b..7f48c725 100644 --- a/src/CheckIntegrityCommand.h +++ b/src/CheckIntegrityCommand.h @@ -37,7 +37,6 @@ #include "RealtimeCommand.h" #include "SharedHandle.h" -#include "TimeA2.h" namespace aria2 { @@ -46,7 +45,6 @@ class CheckIntegrityEntry; class CheckIntegrityCommand : public RealtimeCommand { private: SharedHandle _entry; - Time _timer; public: CheckIntegrityCommand(cuid_t cuid, RequestGroup* requestGroup, diff --git a/src/ConsoleStatCalc.h b/src/ConsoleStatCalc.h index 0501c3c0..62db9fe2 100644 --- a/src/ConsoleStatCalc.h +++ b/src/ConsoleStatCalc.h @@ -36,7 +36,7 @@ #define _D_CONSOLE_STAT_CALC_H_ #include "StatCalc.h" -#include "TimeA2.h" +#include "TimerA2.h" #include "util.h" namespace aria2 { @@ -72,9 +72,9 @@ protected: class ConsoleStatCalc:public StatCalc { private: - Time _cp; + Timer _cp; - Time _lastSummaryNotified; + Timer _lastSummaryNotified; time_t _summaryInterval; diff --git a/src/DHTBucket.h b/src/DHTBucket.h index 4b036862..4b55d490 100644 --- a/src/DHTBucket.h +++ b/src/DHTBucket.h @@ -43,7 +43,7 @@ #include "SharedHandle.h" #include "DHTConstants.h" -#include "TimeA2.h" +#include "TimerA2.h" namespace aria2 { @@ -68,7 +68,7 @@ private: // This is sorted by last time seen. std::deque > _cachedNodes; - Time _lastUpdated; + Timer _lastUpdated; Logger* _logger; diff --git a/src/DHTGetPeersCommand.h b/src/DHTGetPeersCommand.h index 7fbd7483..753dfda5 100644 --- a/src/DHTGetPeersCommand.h +++ b/src/DHTGetPeersCommand.h @@ -37,7 +37,7 @@ #include "Command.h" #include "SharedHandle.h" -#include "TimeA2.h" +#include "TimerA2.h" namespace aria2 { @@ -72,7 +72,7 @@ private: size_t _numRetry; - Time _lastGetPeerTime; + Timer _lastGetPeerTime; public: DHTGetPeersCommand(cuid_t cuid, RequestGroup* requestGroup, DownloadEngine* e); diff --git a/src/DHTMessageTrackerEntry.h b/src/DHTMessageTrackerEntry.h index bdc2f8f0..1cf979be 100644 --- a/src/DHTMessageTrackerEntry.h +++ b/src/DHTMessageTrackerEntry.h @@ -38,7 +38,7 @@ #include "common.h" #include "SharedHandle.h" #include "DHTConstants.h" -#include "TimeA2.h" +#include "TimerA2.h" #include namespace aria2 { @@ -57,7 +57,7 @@ private: SharedHandle _callback; - Time _dispatchedTime; + Timer _dispatchedTime; time_t _timeout; public: diff --git a/src/DHTNode.h b/src/DHTNode.h index ed2f21e3..aa78f409 100644 --- a/src/DHTNode.h +++ b/src/DHTNode.h @@ -38,7 +38,7 @@ #include "common.h" #include "SharedHandle.h" #include "DHTConstants.h" -#include "TimeA2.h" +#include "TimerA2.h" #include namespace aria2 { @@ -56,7 +56,7 @@ private: unsigned int _condition; - Time _lastContact; + Timer _lastContact; public: DHTNode(); diff --git a/src/DHTPeerAnnounceEntry.h b/src/DHTPeerAnnounceEntry.h index f957ba59..0bd8e318 100644 --- a/src/DHTPeerAnnounceEntry.h +++ b/src/DHTPeerAnnounceEntry.h @@ -42,7 +42,7 @@ #include "SharedHandle.h" #include "DHTConstants.h" #include "PeerAddrEntry.h" -#include "TimeA2.h" +#include "TimerA2.h" namespace aria2 { @@ -54,7 +54,7 @@ private: std::vector _peerAddrEntries; - Time _lastUpdated; + Timer _lastUpdated; public: DHTPeerAnnounceEntry(const unsigned char* infoHash); @@ -75,7 +75,7 @@ public: bool empty() const; - const Time& getLastUpdated() const + const Timer& getLastUpdated() const { return _lastUpdated; } diff --git a/src/DHTRoutingTableSerializer.cc b/src/DHTRoutingTableSerializer.cc index dfdee303..8af78793 100644 --- a/src/DHTRoutingTableSerializer.cc +++ b/src/DHTRoutingTableSerializer.cc @@ -46,6 +46,7 @@ #include "a2netcompat.h" #include "StringFormat.h" #include "util.h" +#include "TimeA2.h" namespace aria2 { diff --git a/src/DHTSetup.cc b/src/DHTSetup.cc index f5df82c2..bf64a5dc 100644 --- a/src/DHTSetup.cc +++ b/src/DHTSetup.cc @@ -192,7 +192,7 @@ void DHTSetup::setup(std::vector& commands, DownloadEngine* e) } if(!desnodes.empty() && deserializer.getSerializedTime(). - difference(global::wallclock) >= DHT_BUCKET_REFRESH_INTERVAL) { + difference() >= DHT_BUCKET_REFRESH_INTERVAL) { SharedHandle task (dynamic_pointer_cast(taskFactory->createBucketRefreshTask())); task->setForceRefresh(true); diff --git a/src/DefaultBtAnnounce.cc b/src/DefaultBtAnnounce.cc index cfecf3a2..09e7013b 100644 --- a/src/DefaultBtAnnounce.cc +++ b/src/DefaultBtAnnounce.cc @@ -61,6 +61,7 @@ DefaultBtAnnounce::DefaultBtAnnounce const Option* option): _downloadContext(downloadContext), trackers(0), + prevAnnounceTimer(0), interval(DEFAULT_ANNOUNCE_INTERVAL), minInterval(DEFAULT_ANNOUNCE_INTERVAL), _userDefinedInterval(0), @@ -70,9 +71,7 @@ DefaultBtAnnounce::DefaultBtAnnounce option(option), logger(LogFactory::getInstance()), _randomizer(SimpleRandomizer::getInstance()) -{ - prevAnnounceTime.setTimeInSec(0); -} +{} DefaultBtAnnounce::~DefaultBtAnnounce() { } @@ -80,7 +79,7 @@ DefaultBtAnnounce::~DefaultBtAnnounce() { bool DefaultBtAnnounce::isDefaultAnnounceReady() { return (trackers == 0 && - prevAnnounceTime. + prevAnnounceTimer. difference(global::wallclock) >= (_userDefinedInterval==0? minInterval:_userDefinedInterval) && !announceList.allTiersFailed()); @@ -207,7 +206,7 @@ bool DefaultBtAnnounce::isAllAnnounceFailed() { } void DefaultBtAnnounce::resetAnnounce() { - prevAnnounceTime = global::wallclock; + prevAnnounceTimer = global::wallclock; announceList.resetTier(); } diff --git a/src/DefaultBtAnnounce.h b/src/DefaultBtAnnounce.h index 78ccd2ac..35b2b045 100644 --- a/src/DefaultBtAnnounce.h +++ b/src/DefaultBtAnnounce.h @@ -36,7 +36,7 @@ #define _D_DEFAULT_BT_ANNOUNCE_H_ #include "BtAnnounce.h" -#include "TimeA2.h" +#include "TimerA2.h" #include "AnnounceList.h" namespace aria2 { @@ -53,7 +53,7 @@ class DefaultBtAnnounce : public BtAnnounce { private: SharedHandle _downloadContext; unsigned int trackers; - Time prevAnnounceTime; + Timer prevAnnounceTimer; time_t interval; time_t minInterval; time_t _userDefinedInterval; diff --git a/src/DefaultBtInteractive.cc b/src/DefaultBtInteractive.cc index c38c7b74..a17e6dca 100644 --- a/src/DefaultBtInteractive.cc +++ b/src/DefaultBtInteractive.cc @@ -163,11 +163,11 @@ BtMessageHandle DefaultBtInteractive::receiveAndSendHandshake() { } void DefaultBtInteractive::doPostHandshakeProcessing() { - // Set time 0 to haveCheckPoint to cache http/ftp download piece completion - haveCheckPoint.setTimeInSec(0); - keepAliveCheckPoint = global::wallclock; - floodingCheckPoint = global::wallclock; - _pexCheckPoint.setTimeInSec(0); + // Set time 0 to haveTimer to cache http/ftp download piece completion + haveTimer.reset(0); + keepAliveTimer = global::wallclock; + floodingTimer = global::wallclock; + _pexTimer.reset(0); if(peer->isExtendedMessagingEnabled()) { addHandshakeExtendedMessageToQueue(); } @@ -249,8 +249,8 @@ void DefaultBtInteractive::decideChoking() { void DefaultBtInteractive::checkHave() { std::vector indexes; - _pieceStorage->getAdvertisedPieceIndexes(indexes, cuid, haveCheckPoint); - haveCheckPoint = global::wallclock; + _pieceStorage->getAdvertisedPieceIndexes(indexes, cuid, haveTimer); + haveTimer = global::wallclock; if(indexes.size() >= 20) { if(peer->isFastExtensionEnabled() && _pieceStorage->allDownloadFinished()) { dispatcher->addMessageToQueue(messageFactory->createHaveAllMessage()); @@ -266,10 +266,10 @@ void DefaultBtInteractive::checkHave() { } void DefaultBtInteractive::sendKeepAlive() { - if(keepAliveCheckPoint.difference(global::wallclock) >= keepAliveInterval) { + if(keepAliveTimer.difference(global::wallclock) >= keepAliveInterval) { dispatcher->addMessageToQueue(messageFactory->createKeepAliveMessage()); dispatcher->sendMessages(); - keepAliveCheckPoint = global::wallclock; + keepAliveTimer = global::wallclock; } } @@ -311,7 +311,7 @@ size_t DefaultBtInteractive::receiveMessages() { _peerStorage->updateTransferStatFor(peer); // pass through case BtRequestMessage::ID: - inactiveCheckPoint = global::wallclock; + inactiveTimer = global::wallclock; break; } } @@ -422,7 +422,7 @@ void DefaultBtInteractive::sendPendingMessage() { } void DefaultBtInteractive::detectMessageFlooding() { - if(floodingCheckPoint. + if(floodingTimer. difference(global::wallclock) >= FLOODING_CHECK_INTERVAL) { if(floodingStat.getChokeUnchokeCount() >= 2 || floodingStat.getKeepAliveCount() >= 2) { @@ -430,13 +430,13 @@ void DefaultBtInteractive::detectMessageFlooding() { } else { floodingStat.reset(); } - floodingCheckPoint = global::wallclock; + floodingTimer = global::wallclock; } } void DefaultBtInteractive::checkActiveInteraction() { - time_t inactiveTime = inactiveCheckPoint.difference(global::wallclock); + time_t inactiveTime = inactiveTimer.difference(global::wallclock); // To allow aria2 to accept mutially interested peer, disconnect unintersted // peer. { @@ -463,7 +463,7 @@ void DefaultBtInteractive::checkActiveInteraction() void DefaultBtInteractive::addPeerExchangeMessage() { - if(_pexCheckPoint. + if(_pexTimer. difference(global::wallclock) >= UTPexExtensionMessage::DEFAULT_INTERVAL) { UTPexExtensionMessageHandle m (new UTPexExtensionMessage(peer->getExtensionMessageID("ut_pex"))); @@ -489,7 +489,7 @@ void DefaultBtInteractive::addPeerExchangeMessage() } BtMessageHandle msg = messageFactory->createBtExtendedMessage(m); dispatcher->addMessageToQueue(msg); - _pexCheckPoint = global::wallclock; + _pexTimer = global::wallclock; } } @@ -510,8 +510,8 @@ void DefaultBtInteractive::doInteractionProcessing() { _utMetadataRequestFactory->create(requests, num, _pieceStorage); dispatcher->addMessageToQueue(requests); } - if(_perSecCheckPoint.difference(global::wallclock) >= 1) { - _perSecCheckPoint = global::wallclock; + if(_perSecTimer.difference(global::wallclock) >= 1) { + _perSecTimer = global::wallclock; // Drop timeout request after queuing message to give a chance // to other connection to request piece. std::vector indexes = @@ -533,8 +533,8 @@ void DefaultBtInteractive::doInteractionProcessing() { detectMessageFlooding(); - if(_perSecCheckPoint.difference(global::wallclock) >= 1) { - _perSecCheckPoint = global::wallclock; + if(_perSecTimer.difference(global::wallclock) >= 1) { + _perSecTimer = global::wallclock; dispatcher->checkRequestSlotAndDoNecessaryThing(); } checkHave(); diff --git a/src/DefaultBtInteractive.h b/src/DefaultBtInteractive.h index 5b72f12d..4ec1a85d 100644 --- a/src/DefaultBtInteractive.h +++ b/src/DefaultBtInteractive.h @@ -39,7 +39,7 @@ #include -#include "TimeA2.h" +#include "TimerA2.h" #include "Command.h" namespace aria2 { @@ -126,13 +126,13 @@ private: Logger* logger; size_t allowedFastSetSize; - Time haveCheckPoint; - Time keepAliveCheckPoint; - Time floodingCheckPoint; + Timer haveTimer; + Timer keepAliveTimer; + Timer floodingTimer; FloodingStat floodingStat; - Time inactiveCheckPoint; - Time _pexCheckPoint; - Time _perSecCheckPoint; + Timer inactiveTimer; + Timer _pexTimer; + Timer _perSecTimer; time_t keepAliveInterval; bool _utPexEnabled; bool _dhtEnabled; diff --git a/src/DefaultPeerStorage.cc b/src/DefaultPeerStorage.cc index 5f9053b7..59e92abd 100644 --- a/src/DefaultPeerStorage.cc +++ b/src/DefaultPeerStorage.cc @@ -39,7 +39,6 @@ #include "LogFactory.h" #include "Logger.h" #include "message.h" -#include "a2time.h" #include "Peer.h" #include "BtRuntime.h" #include "BtSeederStateChoke.h" diff --git a/src/DefaultPeerStorage.h b/src/DefaultPeerStorage.h index 62bb6ad5..afb1c0fe 100644 --- a/src/DefaultPeerStorage.h +++ b/src/DefaultPeerStorage.h @@ -39,7 +39,7 @@ #include -#include "TimeA2.h" +#include "TimerA2.h" namespace aria2 { @@ -65,7 +65,7 @@ private: std::map _peerTransferStatMap; - Time _lastTransferStatMapUpdated; + Timer _lastTransferStatMapUpdated; TransferStat _cachedTransferStat; diff --git a/src/DefaultPieceStorage.cc b/src/DefaultPieceStorage.cc index dd963201..2807373a 100644 --- a/src/DefaultPieceStorage.cc +++ b/src/DefaultPieceStorage.cc @@ -537,14 +537,14 @@ size_t DefaultPieceStorage::getPieceLength(size_t index) void DefaultPieceStorage::advertisePiece(cuid_t cuid, size_t index) { - HaveEntry entry(cuid, index); + HaveEntry entry(cuid, index, global::wallclock); haves.push_front(entry); } void DefaultPieceStorage::getAdvertisedPieceIndexes(std::vector& indexes, cuid_t myCuid, - const Time& lastCheckTime) + const Timer& lastCheckTime) { for(std::deque::const_iterator itr = haves.begin(), eoi = haves.end(); itr != eoi; ++itr) { @@ -552,7 +552,7 @@ DefaultPieceStorage::getAdvertisedPieceIndexes(std::vector& indexes, if(have.getCuid() == myCuid) { continue; } - if(lastCheckTime.isNewer(have.getRegisteredTime())) { + if(lastCheckTime > have.getRegisteredTime()) { break; } indexes.push_back(have.getIndex()); diff --git a/src/DefaultPieceStorage.h b/src/DefaultPieceStorage.h index 9e6f06e8..01ed976d 100644 --- a/src/DefaultPieceStorage.h +++ b/src/DefaultPieceStorage.h @@ -56,17 +56,18 @@ class HaveEntry { private: cuid_t cuid; size_t index; - Time registeredTime; + Timer registeredTime; public: - HaveEntry(cuid_t cuid, size_t index): + HaveEntry(cuid_t cuid, size_t index, const Timer& registeredTime): cuid(cuid), - index(index) {} + index(index), + registeredTime(registeredTime) {} cuid_t getCuid() const { return cuid; } size_t getIndex() const { return index; } - const Time& getRegisteredTime() const { return registeredTime; } + const Timer& getRegisteredTime() const { return registeredTime; } }; class DefaultPieceStorage : public PieceStorage { @@ -195,7 +196,7 @@ public: virtual void getAdvertisedPieceIndexes(std::vector& indexes, - cuid_t myCuid, const Time& lastCheckTime); + cuid_t myCuid, const Timer& lastCheckTime); virtual void removeAdvertisedPiece(time_t elapsed); diff --git a/src/DownloadContext.cc b/src/DownloadContext.cc index 130f3795..3261bf3a 100644 --- a/src/DownloadContext.cc +++ b/src/DownloadContext.cc @@ -69,7 +69,7 @@ DownloadContext::DownloadContext(size_t pieceLength, void DownloadContext::resetDownloadStartTime() { _downloadStartTime = global::wallclock; - _downloadStopTime.setTimeInSec(0); + _downloadStopTime.reset(0); } void DownloadContext::resetDownloadStopTime() @@ -79,9 +79,9 @@ void DownloadContext::resetDownloadStopTime() int64_t DownloadContext::calculateSessionTime() const { - if(_downloadStopTime.isNewer(_downloadStartTime)) { + if(_downloadStopTime > _downloadStartTime) { return - _downloadStopTime.getTimeInMillis()-_downloadStartTime.getTimeInMillis(); + _downloadStartTime.differenceInMillis(_downloadStopTime); } else { return 0; } diff --git a/src/DownloadContext.h b/src/DownloadContext.h index adbe534b..4daa0e37 100644 --- a/src/DownloadContext.h +++ b/src/DownloadContext.h @@ -43,7 +43,7 @@ #include "SharedHandle.h" #include "Signature.h" -#include "TimeA2.h" +#include "TimerA2.h" #include "A2STR.h" #include "BDE.h" #include "IntSequence.h" @@ -78,9 +78,9 @@ private: BDE _attrs; - Time _downloadStartTime; + Timer _downloadStartTime; - Time _downloadStopTime; + Timer _downloadStopTime; SharedHandle _signature; @@ -233,7 +233,7 @@ public: void resetDownloadStopTime(); - const Time& getDownloadStopTime() const + const Timer& getDownloadStopTime() const { return _downloadStopTime; } diff --git a/src/DownloadEngine.cc b/src/DownloadEngine.cc index 2d3b0489..8f7d6b7d 100644 --- a/src/DownloadEngine.cc +++ b/src/DownloadEngine.cc @@ -48,8 +48,6 @@ #include "StatCalc.h" #include "LogFactory.h" #include "Logger.h" -#include "TimeA2.h" -#include "a2time.h" #include "Socket.h" #include "util.h" #include "a2functional.h" @@ -80,7 +78,7 @@ namespace global { // Global clock, this clock is reseted before executeCommand() call to // reduce the call gettimeofday() system call. -Time wallclock; +Timer wallclock; // 0 ... running // 1 ... stop signal detected @@ -141,8 +139,8 @@ static void executeCommand(std::deque& commands, void DownloadEngine::run() { - Time cp; - cp.setTimeInSec(0); + Timer cp; + cp.reset(0); while(!commands.empty() || !_routineCommands.empty()) { global::wallclock.reset(); if(cp.difference(global::wallclock) >= _refreshInterval) { diff --git a/src/DownloadEngine.h b/src/DownloadEngine.h index fff1c5fd..b29adca5 100644 --- a/src/DownloadEngine.h +++ b/src/DownloadEngine.h @@ -44,7 +44,7 @@ #include "SharedHandle.h" #include "a2netcompat.h" -#include "TimeA2.h" +#include "TimerA2.h" #include "a2io.h" #ifdef ENABLE_ASYNC_DNS # include "AsyncNameResolver.h" @@ -92,7 +92,7 @@ private: time_t _timeout; - Time _registeredTime; + Timer _registeredTime; public: SocketPoolEntry(const SharedHandle& socket, const std::map& option, @@ -116,7 +116,7 @@ private: // key = IP address:port, value = SocketPoolEntry std::multimap _socketPool; - Time _lastSocketPoolScan; + Timer _lastSocketPoolScan; bool _noWait; diff --git a/src/DownloadEngineFactory.cc b/src/DownloadEngineFactory.cc index d855f5af..e3f93646 100644 --- a/src/DownloadEngineFactory.cc +++ b/src/DownloadEngineFactory.cc @@ -107,7 +107,7 @@ DownloadEngineFactory::newDownloadEngine #ifdef ENABLE_MESSAGE_DIGEST e->_checkIntegrityMan.reset(new CheckIntegrityMan()); #endif // ENABLE_MESSAGE_DIGEST - e->addRoutineCommand(new FillRequestGroupCommand(e->newCUID(), e.get(), 1)); + e->addRoutineCommand(new FillRequestGroupCommand(e->newCUID(), e.get())); e->addRoutineCommand(new FileAllocationDispatcherCommand (e->newCUID(), e->_fileAllocationMan, e.get())); #ifdef ENABLE_MESSAGE_DIGEST diff --git a/src/EventPoll.h b/src/EventPoll.h index 431badc1..807f8886 100644 --- a/src/EventPoll.h +++ b/src/EventPoll.h @@ -37,7 +37,7 @@ #include "common.h" #include "SharedHandle.h" -#include "TimeA2.h" +#include "a2time.h" #include "a2netcompat.h" namespace aria2 { diff --git a/src/FileAllocationCommand.cc b/src/FileAllocationCommand.cc index aa1029b8..f3ca426a 100644 --- a/src/FileAllocationCommand.cc +++ b/src/FileAllocationCommand.cc @@ -45,6 +45,7 @@ #include "DownloadContext.h" #include "a2functional.h" #include "RecoverableException.h" +#include "wallclock.h" namespace aria2 { @@ -65,7 +66,7 @@ bool FileAllocationCommand::executeInternal() if(_fileAllocationEntry->finished()) { if(logger->debug()) { logger->debug(MSG_ALLOCATION_COMPLETED, - _timer.difference(), + _timer.difference(global::wallclock), util::itos(_requestGroup->getTotalLength(), true).c_str()); } _e->_fileAllocationMan->dropPickedEntry(); diff --git a/src/FileAllocationCommand.h b/src/FileAllocationCommand.h index 42177fa9..4aaecf73 100644 --- a/src/FileAllocationCommand.h +++ b/src/FileAllocationCommand.h @@ -37,7 +37,7 @@ #include "RealtimeCommand.h" #include "SharedHandle.h" -#include "TimeA2.h" +#include "TimerA2.h" namespace aria2 { @@ -46,7 +46,7 @@ class FileAllocationEntry; class FileAllocationCommand : public RealtimeCommand { private: SharedHandle _fileAllocationEntry; - Time _timer; + Timer _timer; public: FileAllocationCommand(cuid_t cuid, RequestGroup* requestGroup, DownloadEngine* e, diff --git a/src/FillRequestGroupCommand.cc b/src/FillRequestGroupCommand.cc index b8e74db0..3c8e0120 100644 --- a/src/FillRequestGroupCommand.cc +++ b/src/FillRequestGroupCommand.cc @@ -45,11 +45,9 @@ namespace aria2 { FillRequestGroupCommand::FillRequestGroupCommand(cuid_t cuid, - DownloadEngine* e, - time_t interval): + DownloadEngine* e): Command(cuid), - _e(e), - _interval(interval) + _e(e) { setStatusRealtime(); } diff --git a/src/FillRequestGroupCommand.h b/src/FillRequestGroupCommand.h index e6ba46ee..55cb115b 100644 --- a/src/FillRequestGroupCommand.h +++ b/src/FillRequestGroupCommand.h @@ -37,7 +37,7 @@ #include "Command.h" #include "SharedHandle.h" -#include "TimeA2.h" +#include "a2time.h" namespace aria2 { @@ -47,19 +47,12 @@ class DownloadEngine; class FillRequestGroupCommand : public Command { private: DownloadEngine* _e; - time_t _interval; - Time _checkPoint; public: - FillRequestGroupCommand(cuid_t cuid, DownloadEngine* e, time_t interval); + FillRequestGroupCommand(cuid_t cuid, DownloadEngine* e); virtual ~FillRequestGroupCommand(); virtual bool execute(); - - void setInterval(time_t interval) - { - _interval = interval; - } }; } // namespace aria2 diff --git a/src/HttpServerBodyCommand.cc b/src/HttpServerBodyCommand.cc index 5c15a2b3..1fdbfe67 100644 --- a/src/HttpServerBodyCommand.cc +++ b/src/HttpServerBodyCommand.cc @@ -81,7 +81,7 @@ bool HttpServerBodyCommand::execute() } try { if(_socket->isReadable(0) || _httpServer->getContentLength() == 0) { - _timeout = global::wallclock; + _timeoutTimer = global::wallclock; if(_httpServer->receiveBody()) { // Do something for requestpath and body @@ -108,7 +108,7 @@ bool HttpServerBodyCommand::execute() return false; } } else { - if(_timeout.difference(global::wallclock) >= 30) { + if(_timeoutTimer.difference(global::wallclock) >= 30) { logger->info("HTTP request body timeout."); return true; } else { diff --git a/src/HttpServerBodyCommand.h b/src/HttpServerBodyCommand.h index 1e5c3834..b117dbc1 100644 --- a/src/HttpServerBodyCommand.h +++ b/src/HttpServerBodyCommand.h @@ -37,7 +37,7 @@ #include "Command.h" #include "SharedHandle.h" -#include "TimeA2.h" +#include "TimerA2.h" namespace aria2 { @@ -50,7 +50,7 @@ private: DownloadEngine* _e; SharedHandle _socket; SharedHandle _httpServer; - Time _timeout; + Timer _timeoutTimer; public: HttpServerBodyCommand(cuid_t cuid, const SharedHandle& httpServer, diff --git a/src/HttpServerCommand.cc b/src/HttpServerCommand.cc index 13ba4730..6a65c8b0 100644 --- a/src/HttpServerCommand.cc +++ b/src/HttpServerCommand.cc @@ -93,7 +93,7 @@ bool HttpServerCommand::execute() } try { if(_socket->isReadable(0)) { - _timeout = global::wallclock; + _timeoutTimer = global::wallclock; SharedHandle header; header = _httpServer->receiveRequest(); @@ -128,7 +128,7 @@ bool HttpServerCommand::execute() _e->setNoWait(true); return true; } else { - if(_timeout.difference(global::wallclock) >= 30) { + if(_timeoutTimer.difference(global::wallclock) >= 30) { logger->info("HTTP request timeout."); return true; } else { diff --git a/src/HttpServerCommand.h b/src/HttpServerCommand.h index 09181f80..d1d6de30 100644 --- a/src/HttpServerCommand.h +++ b/src/HttpServerCommand.h @@ -37,7 +37,7 @@ #include "Command.h" #include "SharedHandle.h" -#include "TimeA2.h" +#include "TimerA2.h" namespace aria2 { @@ -50,7 +50,7 @@ private: DownloadEngine* _e; SharedHandle _socket; SharedHandle _httpServer; - Time _timeout; + Timer _timeoutTimer; public: HttpServerCommand(cuid_t cuid, DownloadEngine* e, const SharedHandle& socket); diff --git a/src/HttpServerResponseCommand.cc b/src/HttpServerResponseCommand.cc index 06d724c7..e3a163f5 100644 --- a/src/HttpServerResponseCommand.cc +++ b/src/HttpServerResponseCommand.cc @@ -93,7 +93,7 @@ bool HttpServerResponseCommand::execute() } return true; } else { - if(_timeout.difference(global::wallclock) >= 10) { + if(_timeoutTimer.difference(global::wallclock) >= 10) { if(logger->info()) { logger->info("CUID#%s - HttpServer: Timeout while trasmitting" " response.", util::itos(cuid).c_str()); diff --git a/src/HttpServerResponseCommand.h b/src/HttpServerResponseCommand.h index 7fc0eaca..ad654ae3 100644 --- a/src/HttpServerResponseCommand.h +++ b/src/HttpServerResponseCommand.h @@ -37,7 +37,7 @@ #include "Command.h" #include "SharedHandle.h" -#include "TimeA2.h" +#include "TimerA2.h" namespace aria2 { @@ -50,7 +50,7 @@ private: DownloadEngine* _e; SharedHandle _socket; SharedHandle _httpServer; - Time _timeout; + Timer _timeoutTimer; public: HttpServerResponseCommand(cuid_t cuid, const SharedHandle& httpServer, diff --git a/src/LpdMessageDispatcher.h b/src/LpdMessageDispatcher.h index bb6bad52..3b35f99e 100644 --- a/src/LpdMessageDispatcher.h +++ b/src/LpdMessageDispatcher.h @@ -38,7 +38,7 @@ #include #include "SharedHandle.h" -#include "TimeA2.h" +#include "TimerA2.h" namespace aria2 { @@ -52,7 +52,7 @@ private: uint16_t _port; std::string _multicastAddress; uint16_t _multicastPort; - Time _timer; + Timer _timer; time_t _interval; std::string _request; Logger* _logger; diff --git a/src/Makefile.am b/src/Makefile.am index bd527c6f..d1d89de4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -51,6 +51,7 @@ SRCS = Socket.h\ Base64.cc Base64.h\ base32.cc base32.h\ LogFactory.cc LogFactory.h\ + TimerA2.cc TimerA2.h\ TimeA2.cc TimeA2.h\ SharedHandle.h\ HandleRegistry.h\ diff --git a/src/Makefile.in b/src/Makefile.in index aca935f9..858c0658 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -341,9 +341,9 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \ DefaultDiskWriter.cc DefaultDiskWriter.h \ DefaultDiskWriterFactory.cc DefaultDiskWriterFactory.h File.cc \ File.h Option.cc Option.h Base64.cc Base64.h base32.cc \ - base32.h LogFactory.cc LogFactory.h TimeA2.cc TimeA2.h \ - SharedHandle.h HandleRegistry.h FeatureConfig.cc \ - FeatureConfig.h DownloadEngineFactory.cc \ + base32.h LogFactory.cc LogFactory.h TimerA2.cc TimerA2.h \ + TimeA2.cc TimeA2.h SharedHandle.h HandleRegistry.h \ + FeatureConfig.cc FeatureConfig.h DownloadEngineFactory.cc \ DownloadEngineFactory.h SpeedCalc.cc SpeedCalc.h PeerStat.h \ BitfieldMan.cc BitfieldMan.h Randomizer.h SimpleRandomizer.cc \ SimpleRandomizer.h HttpResponse.cc HttpResponse.h \ @@ -800,11 +800,12 @@ am__objects_27 = SocketCore.$(OBJEXT) Command.$(OBJEXT) \ SimpleLogger.$(OBJEXT) AbstractDiskWriter.$(OBJEXT) \ DefaultDiskWriter.$(OBJEXT) DefaultDiskWriterFactory.$(OBJEXT) \ File.$(OBJEXT) Option.$(OBJEXT) Base64.$(OBJEXT) \ - base32.$(OBJEXT) LogFactory.$(OBJEXT) TimeA2.$(OBJEXT) \ - FeatureConfig.$(OBJEXT) DownloadEngineFactory.$(OBJEXT) \ - SpeedCalc.$(OBJEXT) BitfieldMan.$(OBJEXT) \ - SimpleRandomizer.$(OBJEXT) HttpResponse.$(OBJEXT) \ - HttpRequest.$(OBJEXT) AbstractProxyRequestCommand.$(OBJEXT) \ + base32.$(OBJEXT) LogFactory.$(OBJEXT) TimerA2.$(OBJEXT) \ + TimeA2.$(OBJEXT) FeatureConfig.$(OBJEXT) \ + DownloadEngineFactory.$(OBJEXT) SpeedCalc.$(OBJEXT) \ + BitfieldMan.$(OBJEXT) SimpleRandomizer.$(OBJEXT) \ + HttpResponse.$(OBJEXT) HttpRequest.$(OBJEXT) \ + AbstractProxyRequestCommand.$(OBJEXT) \ AbstractProxyResponseCommand.$(OBJEXT) Netrc.$(OBJEXT) \ AuthConfig.$(OBJEXT) AbstractAuthResolver.$(OBJEXT) \ DefaultAuthResolver.$(OBJEXT) NetrcAuthResolver.$(OBJEXT) \ @@ -1103,9 +1104,9 @@ SRCS = Socket.h SocketCore.cc SocketCore.h BinaryStream.h Command.cc \ DefaultDiskWriter.cc DefaultDiskWriter.h \ DefaultDiskWriterFactory.cc DefaultDiskWriterFactory.h File.cc \ File.h Option.cc Option.h Base64.cc Base64.h base32.cc \ - base32.h LogFactory.cc LogFactory.h TimeA2.cc TimeA2.h \ - SharedHandle.h HandleRegistry.h FeatureConfig.cc \ - FeatureConfig.h DownloadEngineFactory.cc \ + base32.h LogFactory.cc LogFactory.h TimerA2.cc TimerA2.h \ + TimeA2.cc TimeA2.h SharedHandle.h HandleRegistry.h \ + FeatureConfig.cc FeatureConfig.h DownloadEngineFactory.cc \ DownloadEngineFactory.h SpeedCalc.cc SpeedCalc.h PeerStat.h \ BitfieldMan.cc BitfieldMan.h Randomizer.h SimpleRandomizer.cc \ SimpleRandomizer.h HttpResponse.cc HttpResponse.h \ @@ -1573,6 +1574,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TimeA2.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TimeBasedCommand.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TimedHaltCommand.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TimerA2.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TrackerWatcherCommand.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TransferStat.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/URIResult.Po@am__quote@ diff --git a/src/Peer.cc b/src/Peer.cc index ee2014a9..8913d254 100644 --- a/src/Peer.cc +++ b/src/Peer.cc @@ -370,13 +370,13 @@ bool Peer::isDHTEnabled() const return _res->dhtEnabled(); } -const Time& Peer::getLastDownloadUpdate() const +const Timer& Peer::getLastDownloadUpdate() const { assert(_res); return _res->getLastDownloadUpdate(); } -const Time& Peer::getLastAmUnchoking() const +const Timer& Peer::getLastAmUnchoking() const { assert(_res); return _res->getLastAmUnchoking(); @@ -393,7 +393,7 @@ void Peer::setIncomingPeer(bool incoming) _incoming = incoming; } -void Peer::setFirstContactTime(const Time& time) +void Peer::setFirstContactTime(const Timer& time) { _firstContactTime = time; } diff --git a/src/Peer.h b/src/Peer.h index 965f0b11..c32227a5 100644 --- a/src/Peer.h +++ b/src/Peer.h @@ -43,7 +43,7 @@ #include #include "SharedHandle.h" -#include "TimeA2.h" +#include "TimerA2.h" #include "BtConstants.h" #include "PeerStat.h" #include "a2functional.h" @@ -68,9 +68,9 @@ private: unsigned char _peerId[PEER_ID_LENGTH]; - Time _firstContactTime; + Timer _firstContactTime; - Time _badConditionStartTime; + Timer _badConditionStartTime; bool _seeder; @@ -146,14 +146,14 @@ public: void releaseSessionResource(); - const Time& getFirstContactTime() const + const Timer& getFirstContactTime() const { return _firstContactTime; } - void setFirstContactTime(const Time& time); + void setFirstContactTime(const Timer& time); - const Time& getBadConditionStartTime() const + const Timer& getBadConditionStartTime() const { return _badConditionStartTime; } @@ -269,9 +269,9 @@ public: void setExtension(const std::string& name, uint8_t id); - const Time& getLastDownloadUpdate() const; + const Timer& getLastDownloadUpdate() const; - const Time& getLastAmUnchoking() const; + const Timer& getLastAmUnchoking() const; uint64_t getCompletedLength() const; diff --git a/src/PeerAbstractCommand.h b/src/PeerAbstractCommand.h index f8618b7a..26098f72 100644 --- a/src/PeerAbstractCommand.h +++ b/src/PeerAbstractCommand.h @@ -37,7 +37,7 @@ #include "Command.h" #include "SharedHandle.h" -#include "TimeA2.h" +#include "TimerA2.h" namespace aria2 { @@ -48,7 +48,7 @@ class SocketCore; class PeerAbstractCommand : public Command { private: - Time checkPoint; + Timer checkPoint; time_t timeout; protected: DownloadEngine* e; diff --git a/src/PeerAddrEntry.h b/src/PeerAddrEntry.h index 9440a17e..8bc146e3 100644 --- a/src/PeerAddrEntry.h +++ b/src/PeerAddrEntry.h @@ -36,9 +36,11 @@ #define _D_DHT_PEER_ADDR_ENTRY_H_ #include "common.h" -#include "TimeA2.h" + #include +#include "TimerA2.h" + namespace aria2 { class PeerAddrEntry { @@ -47,9 +49,10 @@ private: uint16_t _port; - Time _lastUpdated; + Timer _lastUpdated; public: - PeerAddrEntry(const std::string& ipaddr, uint16_t port, Time updated = Time()): + PeerAddrEntry + (const std::string& ipaddr, uint16_t port, Timer updated = Timer()): _ipaddr(ipaddr), _port(port), _lastUpdated(updated) {} const std::string& getIPAddress() const @@ -62,7 +65,7 @@ public: return _port; } - const Time& getLastUpdated() const + const Timer& getLastUpdated() const { return _lastUpdated; } diff --git a/src/PeerSessionResource.h b/src/PeerSessionResource.h index 7e191275..1cdf358e 100644 --- a/src/PeerSessionResource.h +++ b/src/PeerSessionResource.h @@ -42,7 +42,7 @@ #include "BtConstants.h" #include "PeerStat.h" -#include "TimeA2.h" +#include "TimerA2.h" namespace aria2 { @@ -77,9 +77,9 @@ private: bool _dhtEnabled; PeerStat _peerStat; - Time _lastDownloadUpdate; + Timer _lastDownloadUpdate; - Time _lastAmUnchoking; + Timer _lastAmUnchoking; WeakHandle _dispatcher; public: @@ -216,12 +216,12 @@ public: void updateDownloadLength(size_t bytes); - const Time& getLastDownloadUpdate() const + const Timer& getLastDownloadUpdate() const { return _lastDownloadUpdate; } - const Time& getLastAmUnchoking() const + const Timer& getLastAmUnchoking() const { return _lastAmUnchoking; } diff --git a/src/PeerStat.h b/src/PeerStat.h index 11d2119d..087a4f17 100644 --- a/src/PeerStat.h +++ b/src/PeerStat.h @@ -57,7 +57,7 @@ private: std::string _protocol; SpeedCalc downloadSpeed; SpeedCalc uploadSpeed; - Time downloadStartTime; + Timer downloadStartTime; PeerStat::STATUS status; unsigned int _avgDownloadSpeed; unsigned int _avgUploadSpeed; @@ -147,7 +147,7 @@ public: status = PeerStat::IDLE; } - const Time& getDownloadStartTime() const { + const Timer& getDownloadStartTime() const { return downloadStartTime; } diff --git a/src/PieceStorage.h b/src/PieceStorage.h index 0c30139d..62734dd0 100644 --- a/src/PieceStorage.h +++ b/src/PieceStorage.h @@ -41,7 +41,7 @@ #include #include "SharedHandle.h" -#include "TimeA2.h" +#include "TimerA2.h" #include "Command.h" namespace aria2 { @@ -207,7 +207,7 @@ public: */ virtual void getAdvertisedPieceIndexes(std::vector& indexes, cuid_t myCuid, - const Time& lastCheckTime) = 0; + const Timer& lastCheckTime) = 0; /** * Removes have entry if specified seconds have elapsed since its diff --git a/src/RequestSlot.cc b/src/RequestSlot.cc index d10f15cc..4bf9cda6 100644 --- a/src/RequestSlot.cc +++ b/src/RequestSlot.cc @@ -39,12 +39,8 @@ namespace aria2 { RequestSlot RequestSlot::nullSlot = RequestSlot(); -void RequestSlot::setDispatchedTime() { - dispatchedTime = global::wallclock; -} - -void RequestSlot::setDispatchedTime(time_t secFromEpoch) { - dispatchedTime.setTimeInSec(secFromEpoch); +void RequestSlot::setDispatchedTime(time_t sec) { + dispatchedTime.reset(sec); } bool RequestSlot::isTimeout(time_t timeoutSec) const { diff --git a/src/RequestSlot.h b/src/RequestSlot.h index 372b4641..6d3d75a8 100644 --- a/src/RequestSlot.h +++ b/src/RequestSlot.h @@ -36,14 +36,15 @@ #define _D_REQUEST_SLOT_H_ #include "common.h" -#include "TimeA2.h" +#include "TimerA2.h" #include "Piece.h" +#include "wallclock.h" namespace aria2 { class RequestSlot { private: - Time dispatchedTime; + Timer dispatchedTime; size_t index; uint32_t begin; size_t length; @@ -70,6 +71,7 @@ public: RequestSlot(size_t index, uint32_t begin, size_t length, size_t blockIndex, const SharedHandle& piece = SharedHandle()): + dispatchedTime(global::wallclock), index(index), begin(begin), length(length), blockIndex(blockIndex), _piece(piece) {} @@ -113,7 +115,6 @@ public: } } - void setDispatchedTime(); void setDispatchedTime(time_t secFromEpoch); bool isTimeout(time_t timeoutSec) const; diff --git a/src/SegmentMan.h b/src/SegmentMan.h index ccac611c..b9e8f694 100644 --- a/src/SegmentMan.h +++ b/src/SegmentMan.h @@ -42,7 +42,7 @@ #include #include "SharedHandle.h" -#include "TimeA2.h" +#include "TimerA2.h" #include "Command.h" #include "BitfieldMan.h" @@ -98,7 +98,7 @@ private: // key: PeerStat's cuid, value: its download speed std::map _peerStatDlspdMap; - Time _lastPeerStatDlspdMapUpdated; + Timer _lastPeerStatDlspdMapUpdated; unsigned int _cachedDlspd; diff --git a/src/ServerStat.cc b/src/ServerStat.cc index b5c9a0a5..2103ed7f 100644 --- a/src/ServerStat.cc +++ b/src/ServerStat.cc @@ -39,7 +39,6 @@ #include "array_fun.h" #include "LogFactory.h" -#include "wallclock.h" namespace aria2 { @@ -78,7 +77,7 @@ void ServerStat::updateDownloadSpeed(unsigned int downloadSpeed) if(downloadSpeed > 0) { _status = OK; } - _lastUpdated = global::wallclock; + _lastUpdated.reset(); } void ServerStat::setSingleConnectionAvgSpeed @@ -183,7 +182,7 @@ void ServerStat::setStatusInternal(STATUS status) _hostname.c_str(), _protocol.c_str()); } _status = status; - _lastUpdated = global::wallclock; + _lastUpdated.reset(); } void ServerStat::setOK() diff --git a/src/ServerStatMan.cc b/src/ServerStatMan.cc index 23af57d5..ca27eb58 100644 --- a/src/ServerStatMan.cc +++ b/src/ServerStatMan.cc @@ -43,7 +43,6 @@ #include "ServerStat.h" #include "util.h" #include "RecoverableException.h" -#include "wallclock.h" namespace aria2 { @@ -144,12 +143,13 @@ bool ServerStatMan::load(std::istream& in) class FindStaleServerStat { private: time_t _timeout; + Time _time; public: FindStaleServerStat(time_t timeout):_timeout(timeout) {} bool operator()(const SharedHandle& ss) const { - return ss->getLastUpdated().difference(global::wallclock) >= _timeout; + return ss->getLastUpdated().difference(_time) >= _timeout; } }; diff --git a/src/ServerStatMan.h b/src/ServerStatMan.h index 8bd29a4f..4eb1fe06 100644 --- a/src/ServerStatMan.h +++ b/src/ServerStatMan.h @@ -35,11 +35,14 @@ #ifndef _D_SERVER_STAT_MAN_H_ #define _D_SERVER_STAT_MAN_H_ #include "common.h" -#include "SharedHandle.h" + #include #include #include +#include "SharedHandle.h" +#include "a2time.h" + namespace aria2 { class ServerStat; diff --git a/src/SleepCommand.cc b/src/SleepCommand.cc index 78811de1..e8a85ae5 100644 --- a/src/SleepCommand.cc +++ b/src/SleepCommand.cc @@ -44,7 +44,7 @@ SleepCommand::SleepCommand(cuid_t cuid, DownloadEngine* e, RequestGroup* requestGroup, Command* nextCommand, time_t wait): Command(cuid), engine(e), _requestGroup(requestGroup), - nextCommand(nextCommand), wait(wait) {} + nextCommand(nextCommand), wait(wait), checkPoint(global::wallclock) {} SleepCommand::~SleepCommand() { delete nextCommand; diff --git a/src/SleepCommand.h b/src/SleepCommand.h index 0bf80ef8..ccf34fe1 100644 --- a/src/SleepCommand.h +++ b/src/SleepCommand.h @@ -36,7 +36,7 @@ #define _D_SLEEP_COMMAND_H_ #include "Command.h" -#include "TimeA2.h" +#include "TimerA2.h" namespace aria2 { @@ -49,7 +49,7 @@ private: RequestGroup* _requestGroup; Command* nextCommand; time_t wait; - Time checkPoint; + Timer checkPoint; public: SleepCommand(cuid_t cuid, DownloadEngine* e, RequestGroup* requestGroup, Command* nextCommand, time_t wait); diff --git a/src/SpeedCalc.h b/src/SpeedCalc.h index 40a18e2c..d8fcae1f 100644 --- a/src/SpeedCalc.h +++ b/src/SpeedCalc.h @@ -36,7 +36,7 @@ #define _D_SPEED_CALC_H_ #include "common.h" -#include "TimeA2.h" +#include "TimerA2.h" namespace aria2 { @@ -44,10 +44,10 @@ class SpeedCalc { private: uint64_t lengthArray[2]; int sw; - Time cpArray[2]; + Timer cpArray[2]; unsigned int maxSpeed; unsigned int prevSpeed; - Time start; + Timer start; uint64_t accumulatedLength; time_t nextInterval; diff --git a/src/StreamCheckIntegrityEntry.h b/src/StreamCheckIntegrityEntry.h index 81f48a7c..c231e4c6 100644 --- a/src/StreamCheckIntegrityEntry.h +++ b/src/StreamCheckIntegrityEntry.h @@ -36,14 +36,11 @@ #define _D_STREAM_CHECK_INTEGRITY_ENTRY_H_ #include "PieceHashCheckIntegrityEntry.h" -#include "TimeA2.h" namespace aria2 { class StreamCheckIntegrityEntry:public PieceHashCheckIntegrityEntry { -private: - Time _timer; public: StreamCheckIntegrityEntry(RequestGroup* requestGroup, Command* nextCommand = 0); diff --git a/src/StreamFileAllocationEntry.h b/src/StreamFileAllocationEntry.h index e982d98f..e277705e 100644 --- a/src/StreamFileAllocationEntry.h +++ b/src/StreamFileAllocationEntry.h @@ -36,13 +36,10 @@ #define _D_STREAM_FILE_ALLOCATION_ENTRY_H_ #include "FileAllocationEntry.h" -#include "TimeA2.h" namespace aria2 { class StreamFileAllocationEntry : public FileAllocationEntry { -private: - Time _timer; public: StreamFileAllocationEntry(RequestGroup* requestGroup, Command* nextCommand = 0); diff --git a/src/TimeBasedCommand.h b/src/TimeBasedCommand.h index f08a9af1..dc5f0c38 100644 --- a/src/TimeBasedCommand.h +++ b/src/TimeBasedCommand.h @@ -36,7 +36,7 @@ #define _D_TIME_BASED_COMMAND_H_ #include "Command.h" -#include "TimeA2.h" +#include "TimerA2.h" namespace aria2 { @@ -60,7 +60,7 @@ protected: bool _routineCommand; private: - Time _checkPoint; + Timer _checkPoint; public: /** * preProcess() is called each time when excute() is called. diff --git a/src/TimeSeedCriteria.h b/src/TimeSeedCriteria.h index 1c74649e..53d56312 100644 --- a/src/TimeSeedCriteria.h +++ b/src/TimeSeedCriteria.h @@ -36,7 +36,7 @@ #define _D_TIME_SEED_CRITERIA_H_ #include "SeedCriteria.h" -#include "TimeA2.h" +#include "TimerA2.h" #include "wallclock.h" namespace aria2 { @@ -45,7 +45,7 @@ class TimeSeedCriteria : public SeedCriteria { private: // How much time the client does seeding in seconds. time_t duration; - Time watch; + Timer watch; public: TimeSeedCriteria(time_t duration):duration(duration) {} virtual ~TimeSeedCriteria() {} diff --git a/src/TimerA2.cc b/src/TimerA2.cc new file mode 100644 index 00000000..4da4d6fc --- /dev/null +++ b/src/TimerA2.cc @@ -0,0 +1,165 @@ +/* */ + +#include "TimerA2.h" + +#include + +#include "util.h" + +namespace aria2 { + +Timer::Timer() +{ + reset(); +} + +Timer::Timer(const Timer& timer):_tv(timer._tv) {} + +Timer::Timer(time_t sec) +{ + reset(sec); +} + +Timer::Timer(const struct timeval& tv):_tv(tv) {} + +static bool useClockGettime() +{ + static timespec ts; + static int r = clock_gettime(CLOCK_MONOTONIC, &ts); + return r == 0; +} + +Timer& Timer::operator=(const Timer& timer) +{ + if(this != &timer) { + _tv = timer._tv; + } + return *this; +} + +bool Timer::operator<(const Timer& timer) const +{ + return util::difftv(timer._tv, _tv) > 0; +} + +bool Timer::operator>(const Timer& timer) const +{ + return util::difftv(_tv, timer._tv) > 0; +} + +static timeval getCurrentTime() +{ + timeval tv; + if(useClockGettime()) { + timespec ts; + int r = clock_gettime(CLOCK_MONOTONIC, &ts); + assert(r == 0); + tv.tv_sec = ts.tv_sec+2678400; // 1month offset(24*3600*31) + tv.tv_usec = ts.tv_nsec/1000; + } else { + gettimeofday(&tv, 0); + } + return tv; +} + +void Timer::reset() +{ + _tv = getCurrentTime(); +} + +void Timer::reset(time_t sec) +{ + _tv.tv_sec = sec; + _tv.tv_usec = 0; +} + +bool Timer::elapsed(time_t sec) const +{ + return + util::difftv(getCurrentTime(), _tv) >= static_cast(sec)*1000000; +} + +bool Timer::elapsedInMillis(int64_t millis) const +{ + return util::difftv(getCurrentTime(), _tv)/1000 >= millis; +} + +time_t Timer::difference() const +{ + return util::difftv(getCurrentTime(), _tv)/1000000; +} + +time_t Timer::difference(const timeval& tv) const +{ + return util::difftv(tv, _tv)/1000000; +} + +int64_t Timer::differenceInMillis() const +{ + return util::difftv(getCurrentTime(), _tv)/1000; +} + +int64_t Timer::differenceInMillis(const timeval& tv) const +{ + return util::difftv(tv, _tv)/1000; +} + +bool Timer::isZero() const +{ + return _tv.tv_sec == 0 && _tv.tv_usec == 0; +} + +int64_t Timer::getTimeInMicros() const +{ + return (int64_t)_tv.tv_sec*1000*1000+_tv.tv_usec; +} + +int64_t Timer::getTimeInMillis() const +{ + return (int64_t)_tv.tv_sec*1000+_tv.tv_usec/1000; +} + +time_t Timer::getTime() const +{ + return _tv.tv_sec; +} + +void Timer::advance(time_t sec) +{ + _tv.tv_sec += sec; +} + +} // namespace aria2 diff --git a/src/TimerA2.h b/src/TimerA2.h new file mode 100644 index 00000000..0091e57a --- /dev/null +++ b/src/TimerA2.h @@ -0,0 +1,101 @@ +/* */ +#ifndef _D_TIMER_A2_H_ +#define _D_TIMER_A2_H_ + +#include "common.h" +#include "a2time.h" + +namespace aria2 { + +class Timer { +private: + timeval _tv; + + int64_t difference(const struct timeval& tv) const; + + int64_t differenceInMillis(const struct timeval& tv) const; +public: + // The time value is initialized so that it represents the time at which + // this object was created. + Timer(); + Timer(const Timer& time); + Timer(time_t sec); + Timer(const struct timeval& tv); + + Timer& operator=(const Timer& timer); + + bool operator<(const Timer& timer) const; + + bool operator>(const Timer& timer) const; + + void reset(); + + void reset(time_t sec); + + bool elapsed(time_t sec) const; + + bool elapsedInMillis(int64_t millis) const; + + time_t difference() const; + + time_t difference(const Timer& timer) const + { + return difference(timer._tv); + } + + int64_t differenceInMillis() const; + + int64_t differenceInMillis(const Timer& timer) const + { + return differenceInMillis(timer._tv); + } + + // Returns true if this object's time value is zero. + bool isZero() const; + + void advance(time_t sec); + + // Returns this object's time value in seconds. + time_t getTime() const; + + int64_t getTimeInMicros() const; + + int64_t getTimeInMillis() const; +}; + +} // namespace aria2 + +#endif // _D_TIMER_A2_H_ diff --git a/src/UTMetadataRequestTracker.h b/src/UTMetadataRequestTracker.h index 3b197d63..b7bd91a5 100644 --- a/src/UTMetadataRequestTracker.h +++ b/src/UTMetadataRequestTracker.h @@ -39,7 +39,7 @@ #include -#include "TimeA2.h" +#include "TimerA2.h" #include "wallclock.h" namespace aria2 { @@ -50,7 +50,7 @@ class UTMetadataRequestTracker { private: struct RequestEntry { size_t _index; - Time _dispatchedTime; + Timer _dispatchedTime; RequestEntry(size_t index):_index(index) {} diff --git a/src/UnknownLengthPieceStorage.h b/src/UnknownLengthPieceStorage.h index 9015689d..8a82e4c3 100644 --- a/src/UnknownLengthPieceStorage.h +++ b/src/UnknownLengthPieceStorage.h @@ -229,7 +229,7 @@ public: */ virtual void getAdvertisedPieceIndexes(std::vector& indexes, - cuid_t myCuid, const Time& lastCheckTime) + cuid_t myCuid, const Timer& lastCheckTime) {} /** diff --git a/src/a2time.h b/src/a2time.h index 04822803..bf9628d2 100644 --- a/src/a2time.h +++ b/src/a2time.h @@ -61,4 +61,14 @@ # define suseconds_t uint64_t #endif +#ifndef CLOCK_MONOTONIC +# define CLOCK_MONOTONIC 0 +#endif // !CLOCK_MONOTONIC +#ifndef HAVE_STRUCT_TIMESPEC +# define timespec int +#endif // !HAVE_STRUCT_TIMESPEC +#ifndef HAVE_CLOCK_GETTIME +# define clock_gettime(ID, TP) (-1) +#endif // !HAVE_CLOCK_GETTIME + #endif // _D_A2TIME_H_ diff --git a/src/wallclock.h b/src/wallclock.h index 5eab97ee..759c2768 100644 --- a/src/wallclock.h +++ b/src/wallclock.h @@ -32,14 +32,14 @@ * files in the program, then also delete it here. */ /* copyright --> */ -#include "TimeA2.h" +#include "TimerA2.h" namespace aria2 { namespace global { // wallclock is defined in DownloadEngine.cc -extern Time wallclock; +extern Timer wallclock; } // namespace global diff --git a/test/DHTPeerAnnounceEntryTest.cc b/test/DHTPeerAnnounceEntryTest.cc index fd53c35c..299af7df 100644 --- a/test/DHTPeerAnnounceEntryTest.cc +++ b/test/DHTPeerAnnounceEntryTest.cc @@ -36,9 +36,9 @@ void DHTPeerAnnounceEntryTest::testRemoveStalePeerAddrEntry() DHTPeerAnnounceEntry entry(infohash); entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6881)); - entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.2", 6882, Time(0))); + entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.2", 6882, Timer(0))); entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.3", 6883)); - entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.4", 6884, Time(0))); + entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.4", 6884, Timer(0))); entry.removeStalePeerAddrEntry(10); @@ -72,7 +72,7 @@ void DHTPeerAnnounceEntryTest::testAddPeerAddrEntry() memset(infohash, 0xff, DHT_ID_LENGTH); DHTPeerAnnounceEntry entry(infohash); - entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6881, Time(0))); + entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6881, Timer(0))); entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6882)); CPPUNIT_ASSERT_EQUAL((size_t)2, entry.countPeerAddrEntry()); @@ -95,7 +95,7 @@ void DHTPeerAnnounceEntryTest::testGetPeers() CPPUNIT_ASSERT_EQUAL((size_t)0, peers.size()); } - entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6881, Time(0))); + entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6881, Timer(0))); entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.2", 6882)); { diff --git a/test/MockPieceStorage.h b/test/MockPieceStorage.h index a6d09acf..00598455 100644 --- a/test/MockPieceStorage.h +++ b/test/MockPieceStorage.h @@ -214,7 +214,7 @@ public: virtual void getAdvertisedPieceIndexes(std::vector& indexes, cuid_t myCuid, - const Time& lastCheckTime) + const Timer& lastCheckTime) {} virtual void removeAdvertisedPiece(time_t elapsed) {} diff --git a/test/UTPexExtensionMessageTest.cc b/test/UTPexExtensionMessageTest.cc index 13a38b62..f4d1f056 100644 --- a/test/UTPexExtensionMessageTest.cc +++ b/test/UTPexExtensionMessageTest.cc @@ -206,7 +206,7 @@ void UTPexExtensionMessageTest::testAddFreshPeer() SharedHandle p1(new Peer("192.168.0.1", 6881)); CPPUNIT_ASSERT(msg.addFreshPeer(p1)); SharedHandle p2(new Peer("10.1.1.2", 9999)); - p2->setFirstContactTime(Time(Time().getTime()-61)); + p2->setFirstContactTime(Timer(Timer().getTime()-61)); CPPUNIT_ASSERT(!msg.addFreshPeer(p2)); SharedHandle p3(new Peer("10.1.1.3", 9999, true)); CPPUNIT_ASSERT(!msg.addFreshPeer(p3));