diff options
author | Antonin Kral <a.kral@bobek.cz> | 2011-03-17 00:05:43 +0100 |
---|---|---|
committer | Antonin Kral <a.kral@bobek.cz> | 2011-03-17 00:05:43 +0100 |
commit | 582fc32574a3b158c81e49cb00e6ae59205e66ba (patch) | |
tree | ac64a3243e0d2121709f685695247052858115c8 /util/mmap_posix.cpp | |
parent | 2761bffa96595ac1698d86bbc2e95ebb0d4d6e93 (diff) | |
download | mongodb-582fc32574a3b158c81e49cb00e6ae59205e66ba.tar.gz |
Imported Upstream version 1.8.0
Diffstat (limited to 'util/mmap_posix.cpp')
-rw-r--r-- | util/mmap_posix.cpp | 132 |
1 files changed, 95 insertions, 37 deletions
diff --git a/util/mmap_posix.cpp b/util/mmap_posix.cpp index af1592c..f47a06f 100644 --- a/util/mmap_posix.cpp +++ b/util/mmap_posix.cpp @@ -26,20 +26,23 @@ #include <sys/stat.h> #include <fcntl.h> +#include "mongoutils/str.h" +using namespace mongoutils; + namespace mongo { MemoryMappedFile::MemoryMappedFile() { fd = 0; maphandle = 0; - view = 0; len = 0; created(); } void MemoryMappedFile::close() { - if ( view ) - munmap(view, len); - view = 0; + for( vector<void*>::iterator i = views.begin(); i != views.end(); i++ ) { + munmap(*i,len); + } + views.clear(); if ( fd ) ::close(fd); @@ -47,76 +50,129 @@ namespace mongo { } #ifndef O_NOATIME -#define O_NOATIME 0 +#define O_NOATIME (0) +#endif + +#ifndef MAP_NORESERVE +#define MAP_NORESERVE (0) #endif - void* MemoryMappedFile::map(const char *filename, long &length, int options) { + void* MemoryMappedFile::map(const char *filename, unsigned long long &length, int options) { // length may be updated by callee. - _filename = filename; - theFileAllocator().allocateAsap( filename, length ); + setFilename(filename); + FileAllocator::get()->allocateAsap( filename, length ); len = length; - massert( 10446 , (string)"mmap() can't map area of size 0 [" + filename + "]" , length > 0 ); + massert( 10446 , str::stream() << "mmap: can't map area of size 0 file: " << filename, length > 0 ); - fd = open(filename, O_RDWR | O_NOATIME); if ( fd <= 0 ) { - out() << "couldn't open " << filename << ' ' << errnoWithDescription() << endl; + log() << "couldn't open " << filename << ' ' << errnoWithDescription() << endl; return 0; } - off_t filelen = lseek(fd, 0, SEEK_END); - if ( filelen != length ){ - cout << "wanted length: " << length << " filelen: " << filelen << endl; - cout << sizeof(size_t) << endl; - massert( 10447 , "file size allocation failed", filelen == length ); - } + unsigned long long filelen = lseek(fd, 0, SEEK_END); + uassert(10447, str::stream() << "map file alloc failed, wanted: " << length << " filelen: " << filelen << ' ' << sizeof(size_t), filelen == length ); lseek( fd, 0, SEEK_SET ); - - view = mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + + void * view = mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); if ( view == MAP_FAILED ) { - out() << " mmap() failed for " << filename << " len:" << length << " " << errnoWithDescription() << endl; - if ( errno == ENOMEM ){ - out() << " mmap failed with out of memory, if you're using 32-bits, then you probably need to upgrade to 64" << endl; + error() << " mmap() failed for " << filename << " len:" << length << " " << errnoWithDescription() << endl; + if ( errno == ENOMEM ) { + if( sizeof(void*) == 4 ) + error() << "mmap failed with out of memory. You are using a 32-bit build and probably need to upgrade to 64" << endl; + else + error() << "mmap failed with out of memory. (64 bit build)" << endl; } return 0; } + #if defined(__sunos__) #warning madvise not supported on solaris yet #else - if ( options & SEQUENTIAL ){ - if ( madvise( view , length , MADV_SEQUENTIAL ) ){ - out() << " madvise failed for " << filename << " " << errnoWithDescription() << endl; + if ( options & SEQUENTIAL ) { + if ( madvise( view , length , MADV_SEQUENTIAL ) ) { + warning() << "map: madvise failed for " << filename << ' ' << errnoWithDescription() << endl; } } #endif - DEV if (! dbMutex.info().isLocked()){ + views.push_back( view ); + + DEV if (! dbMutex.info().isLocked()) { _unlock(); } return view; } - + + void* MemoryMappedFile::createReadOnlyMap() { + void * x = mmap( /*start*/0 , len , PROT_READ , MAP_SHARED , fd , 0 ); + if( x == MAP_FAILED ) { + if ( errno == ENOMEM ) { + if( sizeof(void*) == 4 ) + error() << "mmap ro failed with out of memory. You are using a 32-bit build and probably need to upgrade to 64" << endl; + else + error() << "mmap ro failed with out of memory. (64 bit build)" << endl; + } + return 0; + } + return x; + } + + void* MemoryMappedFile::createPrivateMap() { + void * x = mmap( /*start*/0 , len , PROT_READ|PROT_WRITE , MAP_PRIVATE|MAP_NORESERVE , fd , 0 ); + if( x == MAP_FAILED ) { + if ( errno == ENOMEM ) { + if( sizeof(void*) == 4 ) { + error() << "mmap private failed with out of memory. You are using a 32-bit build and probably need to upgrade to 64" << endl; + } + else { + error() << "mmap private failed with out of memory. (64 bit build)" << endl; + } + } + else { + error() << "mmap private failed " << errnoWithDescription() << endl; + } + return 0; + } + + views.push_back(x); + return x; + } + + void* MemoryMappedFile::remapPrivateView(void *oldPrivateAddr) { + // don't unmap, just mmap over the old region + void * x = mmap( oldPrivateAddr, len , PROT_READ|PROT_WRITE , MAP_PRIVATE|MAP_NORESERVE|MAP_FIXED , fd , 0 ); + if( x == MAP_FAILED ) { + int err = errno; + error() << "13601 Couldn't remap private view: " << errnoWithDescription(err) << endl; + log() << "aborting" << endl; + abort(); + } + assert( x == oldPrivateAddr ); + return x; + } + void MemoryMappedFile::flush(bool sync) { - if ( view == 0 || fd == 0 ) + if ( views.empty() || fd == 0 ) return; - if ( msync(view, len, sync ? MS_SYNC : MS_ASYNC) ) + if ( msync(views[0], len, sync ? MS_SYNC : MS_ASYNC) ) problem() << "msync " << errnoWithDescription() << endl; } - + class PosixFlushable : public MemoryMappedFile::Flushable { public: PosixFlushable( void * view , HANDLE fd , long len ) - : _view( view ) , _fd( fd ) , _len(len){ + : _view( view ) , _fd( fd ) , _len(len) { } - void flush(){ + void flush() { if ( _view && _fd ) if ( msync(_view, _len, MS_SYNC ) ) problem() << "msync " << errnoWithDescription() << endl; - + } void * _view; @@ -124,16 +180,18 @@ namespace mongo { long _len; }; - MemoryMappedFile::Flushable * MemoryMappedFile::prepareFlush(){ - return new PosixFlushable( view , fd , len ); + MemoryMappedFile::Flushable * MemoryMappedFile::prepareFlush() { + return new PosixFlushable( views.empty() ? 0 : views[0] , fd , len ); } void MemoryMappedFile::_lock() { - if (view) assert(mprotect(view, len, PROT_READ | PROT_WRITE) == 0); + if (! views.empty() && isMongoMMF() ) + assert(mprotect(views[0], len, PROT_READ | PROT_WRITE) == 0); } void MemoryMappedFile::_unlock() { - if (view) assert(mprotect(view, len, PROT_READ) == 0); + if (! views.empty() && isMongoMMF() ) + assert(mprotect(views[0], len, PROT_READ) == 0); } } // namespace mongo |