diff --git a/db/extsort.cpp b/db/extsort.cpp index beddc744068..d1adb872222 100644 --- a/db/extsort.cpp +++ b/db/extsort.cpp @@ -204,7 +204,7 @@ namespace mongo { BSONObjExternalSorter::FileIterator::FileIterator( string file ){ long length; - _buf = (char*)_file.map( file.c_str() , length ); + _buf = (char*)_file.map( file.c_str() , length , MemoryMappedFile::SEQUENTIAL ); massert( 10308 , "mmap failed" , _buf ); assert( (unsigned long)length == file_size( file ) ); _end = _buf + length; diff --git a/util/mmap.h b/util/mmap.h index ed4ca99c326..140f33a5bd1 100644 --- a/util/mmap.h +++ b/util/mmap.h @@ -22,6 +22,10 @@ namespace mongo { class MemoryMappedFile { public: + enum Options { + SEQUENTIAL = 1 + }; + MemoryMappedFile(); ~MemoryMappedFile(); /* closes the file if open */ void close(); @@ -32,7 +36,7 @@ namespace mongo { /* Creates with length if DNE, otherwise uses existing file length, passed length. */ - void* map(const char *filename, long &length); + void* map(const char *filename, long &length, int options = 0 ); void flush(bool sync); diff --git a/util/mmap_posix.cpp b/util/mmap_posix.cpp index 1237220df9b..eceb583dd19 100644 --- a/util/mmap_posix.cpp +++ b/util/mmap_posix.cpp @@ -49,7 +49,7 @@ namespace mongo { #define O_NOATIME 0 #endif - void* MemoryMappedFile::map(const char *filename, long &length) { + void* MemoryMappedFile::map(const char *filename, long &length, int options) { // length may be updated by callee. theFileAllocator().allocateAsap( filename, length ); len = length; @@ -79,9 +79,16 @@ namespace mongo { } return 0; } + + if ( options & SEQUENTIAL ){ + if ( madvise( view , length , MADV_SEQUENTIAL ) ){ + out() << " madvise failed for " << filename << " " << OUTPUT_ERRNO << endl; + } + } + return view; } - + void MemoryMappedFile::flush(bool sync) { if ( view == 0 || fd == 0 ) return; diff --git a/util/mmap_win.cpp b/util/mmap_win.cpp index 8a0d3064152..1c5065de780 100644 --- a/util/mmap_win.cpp +++ b/util/mmap_win.cpp @@ -69,9 +69,13 @@ namespace mongo { updateLength( filename, length ); std::wstring filenamew = toWideString(filename); + DWORD createOptions = FILE_ATTRIBUTE_NORMAL; + if ( options & SEQUENTIAL ) + createOptions |= FILE_FLAG_SEQUENTIAL_SCAN; + fd = CreateFile( filenamew.c_str(), GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ, - NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + NULL, OPEN_ALWAYS, createOptions , NULL); if ( fd == INVALID_HANDLE_VALUE ) { out() << "Create/OpenFile failed " << filename << ' ' << GetLastError() << endl; return 0;