diff --git a/ChangeLog b/ChangeLog index 9ba3afd9..e737d062 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,21 @@ +2007-03-16 Tatsuhiro Tsujikawa + + To reduce overhead to find commands whose socket is either + readable or writable in the download engine loop: + * src/Command.h, src/Command.cc + (STATUS): New enum. + (status): New variable. + (statusMatch): New function. + (setStatusActive): New function. + (setStatusInactive): New function. + * src/DownloadEngine.h, src/DownloadEngine.cc + (executeCommand): New function. + (run): Simplified. + (waitData): Call Command::setStatusActive() when command's socket is + readable or writable. + + + 2007-03-15 Tatsuhiro Tsujikawa To handle Segment as SegmentHandle: diff --git a/po/Makefile.in b/po/Makefile.in index 434ed7be..739d99cd 100644 --- a/po/Makefile.in +++ b/po/Makefile.in @@ -9,7 +9,7 @@ # General Public License and is *not* in the public domain. PACKAGE = aria2c -VERSION = 0.10.0+1 +VERSION = 0.10.1 SHELL = /bin/sh diff --git a/src/Command.cc b/src/Command.cc index 7f701883..de53c59a 100644 --- a/src/Command.cc +++ b/src/Command.cc @@ -35,3 +35,14 @@ #include "Command.h" int Command::uuidGen = 0; + +bool Command::statusMatch(Command::STATUS statusFilter) const +{ + if(statusFilter == STATUS_ALL) { + return true; + } else if(statusFilter == status) { + return true; + } else { + return false; + } +} diff --git a/src/Command.h b/src/Command.h index e0b9b57e..f9fcb222 100644 --- a/src/Command.h +++ b/src/Command.h @@ -41,14 +41,21 @@ typedef int CommandUuid; class Command { +public: + enum STATUS { + STATUS_ALL, + STATUS_ACTIVE, + STATUS_INACTIVE + }; private: CommandUuid uuid; static int uuidGen; + STATUS status; protected: int cuid; const Logger* logger; public: - Command(int cuid):uuid(uuidGen++), cuid(cuid) { + Command(int cuid):uuid(uuidGen++), status(STATUS_INACTIVE), cuid(cuid) { logger = LogFactory::getInstance(); } virtual ~Command() {} @@ -56,6 +63,12 @@ public: int getCuid() const { return cuid; } const CommandUuid& getUuid() const { return uuid; } + + void setStatusActive() { this->status = STATUS_ACTIVE; } + + void setStatusInactive() { this->status = STATUS_INACTIVE; } + + bool statusMatch(Command::STATUS statusFilter) const; }; #endif // _D_COMMAND_H_ diff --git a/src/DownloadCommand.cc b/src/DownloadCommand.cc index dd905f61..8b0cd6df 100644 --- a/src/DownloadCommand.cc +++ b/src/DownloadCommand.cc @@ -130,7 +130,6 @@ bool DownloadCommand::prepareForNextSegment() { while(1) { SegmentHandle nextSegment = e->segmentMan->getSegment(cuid, tempSegment->index+1); - cerr << nextSegment.isNull() << endl; if(nextSegment.isNull()) { break; } else { diff --git a/src/DownloadEngine.cc b/src/DownloadEngine.cc index 132d06dc..478291ec 100644 --- a/src/DownloadEngine.cc +++ b/src/DownloadEngine.cc @@ -59,6 +59,24 @@ void DownloadEngine::cleanQueue() { commands.clear(); } +void DownloadEngine::executeCommand(Command::STATUS statusFilter) +{ + int max = commands.size(); + for(int i = 0; i < max; i++) { + Command* com = commands.front(); + commands.pop_front(); + if(com->statusMatch(statusFilter)) { + if(com->execute()) { + delete com; + } else { + com->setStatusInactive(); + } + } else { + commands.push_back(com); + } + } +} + void DownloadEngine::run() { initStatistics(); Time cp; @@ -67,31 +85,13 @@ void DownloadEngine::run() { while(!commands.empty()) { if(cp.elapsed(1)) { cp.reset(); - int max = commands.size(); - for(int i = 0; i < max; i++) { - Command* com = commands.front(); - commands.pop_front(); - if(com->execute()) { - delete com; - } - } + executeCommand(Command::STATUS_ALL); } else { - for(Commands::iterator itr = activeCommands.begin(); - itr != activeCommands.end(); itr++) { - Commands::iterator comItr = find(commands.begin(), commands.end(), - *itr); - assert(comItr != commands.end()); - Command* command = *itr; - commands.erase(comItr); - if(command->execute()) { - delete command; - } - } + executeCommand(Command::STATUS_ACTIVE); } afterEachIteration(); - activeCommands.clear(); if(!noWait && !commands.empty()) { - waitData(activeCommands); + waitData(); } noWait = false; calculateStatistics(); @@ -108,7 +108,7 @@ void DownloadEngine::shortSleep() const { select(0, &rfds, NULL, NULL, &tv); } -void DownloadEngine::waitData(Commands& activeCommands) { +void DownloadEngine::waitData() { fd_set rfds; fd_set wfds; int retval = 0; @@ -126,9 +126,7 @@ void DownloadEngine::waitData(Commands& activeCommands) { SocketEntry& entry = *itr; if(FD_ISSET(entry.socket->getSockfd(), &rfds) || FD_ISSET(entry.socket->getSockfd(), &wfds)) { - if(find(activeCommands.begin(), activeCommands.end(), entry.command) == activeCommands.end()) { - activeCommands.push_back(entry.command); - } + entry.command->setStatusActive(); } } #ifdef ENABLE_ASYNC_DNS @@ -139,9 +137,7 @@ void DownloadEngine::waitData(Commands& activeCommands) { switch(entry.nameResolver->getStatus()) { case NameResolver::STATUS_SUCCESS: case NameResolver::STATUS_ERROR: - if(find(activeCommands.begin(), activeCommands.end(), entry.command) == activeCommands.end()) { - activeCommands.push_back(entry.command); - } + entry.command->setStatusActive(); break; default: break; diff --git a/src/DownloadEngine.h b/src/DownloadEngine.h index 403ffbe9..3d7256d3 100644 --- a/src/DownloadEngine.h +++ b/src/DownloadEngine.h @@ -97,7 +97,7 @@ typedef deque NameResolverEntries; class DownloadEngine { private: - void waitData(Commands& activeCommands); + void waitData(); SocketEntries socketEntries; #ifdef ENABLE_ASYNC_DNS NameResolverEntries nameResolverEntries; @@ -109,6 +109,7 @@ private: void shortSleep() const; bool addSocket(const SocketEntry& socketEntry); bool deleteSocket(const SocketEntry& socketEntry); + void executeCommand(Command::STATUS statusFilter); protected: const Logger* logger; virtual void initStatistics() = 0;