0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 17:10:48 +01:00
mongodb/util/mmap.cpp

198 lines
4.8 KiB
C++
Raw Normal View History

2008-06-06 15:43:15 +02:00
// mmap.cpp
/**
* Copyright (C) 2008 10gen Inc.
2008-12-29 02:28:49 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
2008-12-29 02:28:49 +01:00
*
* 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 Affero General Public License for more details.
2008-12-29 02:28:49 +01:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2008-06-06 15:43:15 +02:00
#include "stdafx.h"
#include "mmap.h"
set<MemoryMappedFile*> mmfiles;
2008-12-29 02:28:49 +01:00
MemoryMappedFile::~MemoryMappedFile() {
close();
mmfiles.erase(this);
}
2008-12-29 02:28:49 +01:00
/*static*/
int closingAllFiles = 0;
2008-12-29 02:28:49 +01:00
void MemoryMappedFile::closeAllFiles() {
if ( closingAllFiles ) {
cout << "warning closingAllFiles=" << closingAllFiles << endl;
return;
}
++closingAllFiles;
for ( set<MemoryMappedFile*>::iterator i = mmfiles.begin(); i != mmfiles.end(); i++ )
(*i)->close();
log() << " closeAllFiles() finished" << endl;
--closingAllFiles;
}
2008-12-29 02:28:49 +01:00
#if defined(_WIN32)
2008-06-06 15:43:15 +02:00
#include "windows.h"
MemoryMappedFile::MemoryMappedFile() {
2008-12-29 02:28:49 +01:00
fd = 0;
maphandle = 0;
view = 0;
mmfiles.insert(this);
2008-06-06 15:43:15 +02:00
}
void MemoryMappedFile::close() {
2008-12-29 02:28:49 +01:00
if ( view )
UnmapViewOfFile(view);
view = 0;
if ( maphandle )
CloseHandle(maphandle);
maphandle = 0;
if ( fd )
CloseHandle(fd);
fd = 0;
2008-06-06 15:43:15 +02:00
}
std::wstring toWideString(const char *s) {
2008-12-29 02:28:49 +01:00
std::basic_ostringstream<TCHAR> buf;
buf << s;
return buf.str();
2008-06-06 15:43:15 +02:00
}
unsigned mapped = 0;
void* MemoryMappedFile::map(const char *filename, int length) {
2008-12-29 02:28:49 +01:00
std::wstring filenamew = toWideString(filename);
2008-06-06 15:43:15 +02:00
2008-12-29 02:28:49 +01:00
fd = CreateFile(
filenamew.c_str(), GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if ( fd == INVALID_HANDLE_VALUE ) {
cout << "CreateFile failed " << filename << endl;
return 0;
}
2008-06-06 15:43:15 +02:00
#if defined(_WIN32)
2008-12-29 02:28:49 +01:00
if ( mapped > 500000000 ) {
cout << "WARNING: too much mem mapped for win32" << endl;
2008-06-24 23:31:51 +02:00
// if( length > 50000000 )
// length = 50000000;
2008-12-29 02:28:49 +01:00
}
mapped += length;
2008-06-06 15:43:15 +02:00
#endif
2008-12-29 02:28:49 +01:00
maphandle = CreateFileMapping(fd, NULL, PAGE_READWRITE, 0, length, NULL);
if ( maphandle == NULL ) {
cout << "CreateFileMapping failed " << filename << endl;
return 0;
}
2008-06-06 15:43:15 +02:00
2008-12-29 02:28:49 +01:00
view = MapViewOfFile(maphandle, FILE_MAP_ALL_ACCESS, 0, 0, 0);
if ( view == 0 ) {
cout << "MapViewOfFile failed " << filename << " errno:";
cout << GetLastError();
cout << endl;
}
2008-06-06 15:43:15 +02:00
2008-12-29 02:28:49 +01:00
return view;
2008-06-06 15:43:15 +02:00
}
void MemoryMappedFile::flush(bool) {
}
#else
#include <errno.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
MemoryMappedFile::MemoryMappedFile() {
2008-12-29 02:28:49 +01:00
fd = 0;
maphandle = 0;
view = 0;
len = 0;
mmfiles.insert(this);
2008-06-06 15:43:15 +02:00
}
void MemoryMappedFile::close() {
2008-12-29 02:28:49 +01:00
if ( view )
munmap(view, len);
view = 0;
2008-06-06 15:43:15 +02:00
2008-12-29 02:28:49 +01:00
if ( fd )
::close(fd);
fd = 0;
2008-06-06 15:43:15 +02:00
}
#ifndef O_NOATIME
#warning NO O_NOATIME
#define O_NOATIME 0
#endif
void* MemoryMappedFile::map(const char *filename, int length) {
2008-12-29 02:28:49 +01:00
len = length;
2008-06-06 15:43:15 +02:00
fd = open(filename, O_CREAT | O_RDWR | O_NOATIME, S_IRUSR | S_IWUSR);
2008-12-29 02:28:49 +01:00
if ( !fd ) {
cout << "couldn't open " << filename << ' ' << errno << endl;
return 0;
}
/* make sure the file is the full desired length */
off_t filelen = lseek(fd, 0, SEEK_END);
if ( filelen < length ) {
2008-12-29 17:47:20 +01:00
// log() << "map: file length=" << (unsigned) filelen << " want:"
// << length
// << endl;
2008-12-29 02:28:49 +01:00
if ( filelen != 0 ) {
2008-12-29 17:47:20 +01:00
problem() << "failure mapping new file " << filename << " length:" << length << endl;
2008-12-29 02:28:49 +01:00
return 0;
}
2008-12-29 17:47:20 +01:00
Logstream &l = log();
2008-12-31 16:01:55 +01:00
l << "new datafile " << filename << " filling with zeroes..."; l.flush();
2008-12-29 17:47:20 +01:00
Timer t;
2008-12-29 02:28:49 +01:00
int z = 8192;
char buf[z];
memset(buf, 0, z);
int left = length;
while ( 1 ) {
if ( left <= z ) {
write(fd, buf, left);
break;
}
write(fd, buf, z);
left -= z;
}
2008-12-31 16:01:55 +01:00
l << "done " << ((double)t.millis())/1000.0 << " secs" << endl;
2008-12-29 02:28:49 +01:00
}
lseek(fd, length, SEEK_SET);
write(fd, "", 1);
view = mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if ( view == MAP_FAILED ) {
cout << " mmap() failed for " << filename << " len:" << length << " errno:" << errno << endl;
return 0;
}
return view;
2008-06-06 15:43:15 +02:00
}
void MemoryMappedFile::flush(bool sync) {
2008-12-29 02:28:49 +01:00
if ( msync(view, len, sync ? MS_SYNC : MS_ASYNC) )
problem() << "msync error " << errno << endl;
2008-06-06 15:43:15 +02:00
}
#endif