0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/util/mmap_posix.cpp

215 lines
6.8 KiB
C++
Raw Normal View History

// mmap_posix.cpp
/* Copyright 2009 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2010-04-27 21:27:52 +02:00
#include "pch.h"
#include "mmap.h"
#include "file_allocator.h"
#include "../db/concurrency.h"
#include <errno.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "../util/processinfo.h"
2010-12-30 19:37:32 +01:00
#include "mongoutils/str.h"
using namespace mongoutils;
namespace mongo {
MemoryMappedFile::MemoryMappedFile() {
fd = 0;
maphandle = 0;
len = 0;
created();
}
2011-01-04 06:40:41 +01:00
void MemoryMappedFile::close() {
mmmutex.assertExclusivelyLocked();
2010-10-01 22:45:04 +02:00
for( vector<void*>::iterator i = views.begin(); i != views.end(); i++ ) {
munmap(*i,len);
}
2011-01-26 15:28:33 +01:00
views.clear();
if ( fd )
::close(fd);
fd = 0;
destroyed(); // cleans up from the master list of mmaps
}
#ifndef O_NOATIME
2010-10-24 23:19:20 +02:00
#define O_NOATIME (0)
#endif
#ifndef MAP_NORESERVE
#define MAP_NORESERVE (0)
#endif
2011-04-04 18:38:38 +02:00
#if defined(__sunos__)
MAdvise::MAdvise(void *,unsigned, Advice) { }
2011-04-04 18:38:38 +02:00
MAdvise::~MAdvise() { }
#else
MAdvise::MAdvise(void *p, unsigned len, Advice a) : _p(p), _len(len) {
2011-04-04 18:38:38 +02:00
assert( a == Sequential ); // more later
madvise(_p,_len,MADV_SEQUENTIAL);
}
MAdvise::~MAdvise() {
madvise(_p,_len,MADV_NORMAL);
}
#endif
2010-09-09 13:46:22 +02:00
void* MemoryMappedFile::map(const char *filename, unsigned long long &length, int options) {
// length may be updated by callee.
2010-12-30 21:38:54 +01:00
setFilename(filename);
2011-01-09 07:45:11 +01:00
FileAllocator::get()->allocateAsap( filename, length );
len = length;
2009-12-23 21:55:07 +01:00
2010-10-24 23:19:20 +02:00
massert( 10446 , str::stream() << "mmap: can't map area of size 0 file: " << filename, length > 0 );
2011-01-04 06:40:41 +01:00
fd = open(filename, O_RDWR | O_NOATIME);
if ( fd <= 0 ) {
2010-10-24 23:19:20 +02:00
log() << "couldn't open " << filename << ' ' << errnoWithDescription() << endl;
fd = 0; // our sentinel for not opened
return 0;
}
2010-09-09 13:46:22 +02:00
unsigned long long filelen = lseek(fd, 0, SEEK_END);
2011-01-04 06:40:41 +01:00
uassert(10447, str::stream() << "map file alloc failed, wanted: " << length << " filelen: " << filelen << ' ' << sizeof(size_t), filelen == length );
lseek( fd, 0, SEEK_SET );
2011-01-04 06:40:41 +01:00
2010-10-01 22:45:04 +02:00
void * view = mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if ( view == MAP_FAILED ) {
2010-10-24 23:19:20 +02:00
error() << " mmap() failed for " << filename << " len:" << length << " " << errnoWithDescription() << endl;
if ( errno == ENOMEM ) {
2011-01-04 06:40:41 +01:00
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;
2009-12-01 22:48:40 +01:00
}
return 0;
}
2010-02-05 03:32:38 +01:00
2010-02-05 03:32:38 +01:00
#if defined(__sunos__)
#warning madvise not supported on solaris yet
#else
2011-01-04 06:40:41 +01:00
if ( options & SEQUENTIAL ) {
if ( madvise( view , length , MADV_SEQUENTIAL ) ) {
2010-10-24 23:19:20 +02:00
warning() << "map: madvise failed for " << filename << ' ' << errnoWithDescription() << endl;
2010-02-04 23:12:14 +01:00
}
}
2010-02-04 23:24:22 +01:00
#endif
views.push_back( view );
2011-01-04 06:40:41 +01:00
DEV if (! dbMutex.info().isLocked()) {
_unlock();
}
return view;
}
2010-11-15 22:00:41 +01:00
void* MemoryMappedFile::createReadOnlyMap() {
2011-01-04 06:40:41 +01:00
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;
2010-11-15 22:00:41 +01:00
}
2011-01-04 06:40:41 +01:00
2010-11-16 22:40:56 +01:00
void* MemoryMappedFile::createPrivateMap() {
void * x = mmap( /*start*/0 , len , PROT_READ|PROT_WRITE , MAP_PRIVATE|MAP_NORESERVE , fd , 0 );
2010-12-16 00:10:49 +01:00
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;
2011-01-04 06:40:41 +01:00
}
else {
2010-12-16 00:10:49 +01:00
error() << "mmap private failed with out of memory. (64 bit build)" << endl;
}
}
2011-01-25 21:24:18 +01:00
else {
error() << "mmap private failed " << errnoWithDescription() << endl;
}
2010-12-16 00:10:49 +01:00
return 0;
}
2011-01-25 21:24:18 +01:00
views.push_back(x);
2010-12-16 00:10:49 +01:00
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;
printMemInfo();
abort();
}
assert( x == oldPrivateAddr );
return x;
}
2011-01-04 06:40:41 +01:00
void MemoryMappedFile::flush(bool sync) {
2010-10-01 22:45:04 +02:00
if ( views.empty() || fd == 0 )
return;
2011-03-03 20:57:36 +01:00
if ( msync(viewForFlushing(), len, sync ? MS_SYNC : MS_ASYNC) )
2010-04-25 03:42:37 +02:00
problem() << "msync " << errnoWithDescription() << endl;
}
2011-01-04 06:40:41 +01:00
class PosixFlushable : public MemoryMappedFile::Flushable {
public:
PosixFlushable( void * view , HANDLE fd , long len )
2011-01-04 06:40:41 +01:00
: _view( view ) , _fd( fd ) , _len(len) {
}
2011-01-04 06:40:41 +01:00
void flush() {
if ( _view && _fd )
if ( msync(_view, _len, MS_SYNC ) )
problem() << "msync " << errnoWithDescription() << endl;
2011-01-04 06:40:41 +01:00
}
void * _view;
HANDLE _fd;
long _len;
};
2011-01-04 06:40:41 +01:00
MemoryMappedFile::Flushable * MemoryMappedFile::prepareFlush() {
2011-03-03 20:57:36 +01:00
return new PosixFlushable( viewForFlushing() , fd , len );
}
void MemoryMappedFile::_lock() {
if (! views.empty() && isMongoMMF() )
assert(mprotect(views[0], len, PROT_READ | PROT_WRITE) == 0);
}
void MemoryMappedFile::_unlock() {
if (! views.empty() && isMongoMMF() )
assert(mprotect(views[0], len, PROT_READ) == 0);
}
} // namespace mongo