diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2010-07-04 15:03:35 +0000 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2010-07-04 15:03:35 +0000 |
| commit | a5cc350dcf6cf83a84b5845e0f61230f1f42e7c7 (patch) | |
| tree | 38d73b2aebcdd3c26d76782690d6bb10fe29ad86 /src/AdaptiveFileAllocationIterator.cc | |
| parent | 8f10241e25d796655a8aaaf7cf69c7061d05bc43 (diff) | |
2010-07-04 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
When allocating disk space, for Linux system with fallocate()
system call, first check file system supports fallocate. This
just run fallocate with small chunk and see it succeeds or fails.
If it succeeds, use fallocate() to allocate entire file otherwise
fall back to traditional slower method: writing zeros. This
behavior is enabled in --file-allocation=prealloc, so this is
enabled by default for most modern Linux.
* configure.ac
* src/AbstractDiskWriter.cc
* src/AbstractDiskWriter.h
* src/AbstractSingleDiskAdaptor.cc
* src/AdaptiveFileAllocationIterator.cc
* src/AdaptiveFileAllocationIterator.h
* src/DefaultPieceStorage.cc
* src/DiskAdaptor.cc
* src/DiskAdaptor.h
* src/FallocFileAllocationIterator.cc
* src/Makefile.am
* src/MultiFileAllocationIterator.cc
* src/OptionHandlerFactory.cc
* test/FallocFileAllocationIteratorTest.cc
* test/Makefile.am
Diffstat (limited to 'src/AdaptiveFileAllocationIterator.cc')
| -rw-r--r-- | src/AdaptiveFileAllocationIterator.cc | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/src/AdaptiveFileAllocationIterator.cc b/src/AdaptiveFileAllocationIterator.cc new file mode 100644 index 00000000..3ec80fb6 --- /dev/null +++ b/src/AdaptiveFileAllocationIterator.cc @@ -0,0 +1,102 @@ +/* <!-- copyright */ +/* + * aria2 - The high speed download utility + * + * Copyright (C) 2010 Tatsuhiro Tsujikawa + * + * 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * In addition, as a special exception, the copyright holders give + * permission to link the code of portions of this program with the + * OpenSSL library under certain conditions as described in each + * individual source file, and distribute linked combinations + * including the two. + * You must obey the GNU General Public License in all respects + * for all of the code used other than OpenSSL. If you modify + * file(s) with this exception, you may extend this exception to your + * version of the file(s), but you are not obligated to do so. If you + * do not wish to do so, delete this exception statement from your + * version. If you delete this exception statement from all source + * files in the program, then also delete it here. + */ +/* copyright --> */ +#include "AdaptiveFileAllocationIterator.h" +#include "BinaryStream.h" +#ifdef HAVE_FALLOCATE +# include "FallocFileAllocationIterator.h" +#endif // HAVE_FALLOCATE +#include "SingleFileAllocationIterator.h" +#include "RecoverableException.h" +#include "LogFactory.h" +#include "Logger.h" + +namespace aria2 { + +AdaptiveFileAllocationIterator::AdaptiveFileAllocationIterator +(BinaryStream* stream, off_t offset, uint64_t totalLength): + stream_(stream), offset_(offset), totalLength_(totalLength), + logger_(LogFactory::getInstance()) {} + +AdaptiveFileAllocationIterator::~AdaptiveFileAllocationIterator() {} + +void AdaptiveFileAllocationIterator::allocateChunk() +{ + if(allocator_.isNull()) { +#ifdef HAVE_FALLOCATE + try { + if(logger_->debug()) { + logger_->debug("Testing file system supports fallocate."); + } + if(static_cast<uint64_t>(offset_) < totalLength_) { + off_t len = std::min(totalLength_-offset_, static_cast<uint64_t>(4096)); + stream_->allocate(offset_, len); + offset_ += len; + } + if(logger_->debug()) { + logger_->debug("File system supports fallocate."); + } + allocator_.reset + (new FallocFileAllocationIterator(stream_, offset_, totalLength_)); + } catch(RecoverableException& e) { + if(logger_->debug()) { + logger_->debug("File system does not support fallocate."); + } + SharedHandle<SingleFileAllocationIterator> salloc + (new SingleFileAllocationIterator(stream_, offset_, totalLength_)); + salloc->init(); + allocator_ = salloc; + } +#else // !HAVE_FALLOCATE + SharedHandle<SingleFileAllocationIterator> salloc + (new SingleFileAllocationIterator(stream_, offset_, totalLength_)); + salloc->init(); + allocator_ = salloc; +#endif // !HAVE_FALLOCATE + allocator_->allocateChunk(); + } else { + allocator_->allocateChunk(); + } +} + +bool AdaptiveFileAllocationIterator::finished() +{ + if(allocator_.isNull()) { + return false; + } else { + return allocator_->finished(); + } +} + +} // namespace aria2 |
