2008-06-06 15:43:15 +02:00
|
|
|
// mmap.cpp
|
|
|
|
|
2009-10-27 20:58:27 +01:00
|
|
|
/* 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.
|
|
|
|
*/
|
2008-07-20 23:37:33 +02:00
|
|
|
|
2008-06-06 15:43:15 +02:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "mmap.h"
|
|
|
|
|
2009-01-14 23:09:51 +01:00
|
|
|
namespace mongo {
|
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
set<MemoryMappedFile*> mmfiles;
|
|
|
|
|
|
|
|
MemoryMappedFile::~MemoryMappedFile() {
|
|
|
|
close();
|
|
|
|
mmfiles.erase(this);
|
|
|
|
}
|
|
|
|
|
2009-01-29 23:22:59 +01:00
|
|
|
void MemoryMappedFile::created(){
|
|
|
|
mmfiles.insert(this);
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
/*static*/
|
|
|
|
int closingAllFiles = 0;
|
2009-02-02 15:52:14 +01:00
|
|
|
void MemoryMappedFile::closeAllFiles( stringstream &message ) {
|
2009-01-15 16:17:11 +01:00
|
|
|
if ( closingAllFiles ) {
|
2009-02-02 15:52:14 +01:00
|
|
|
message << "warning closingAllFiles=" << closingAllFiles << endl;
|
2009-01-15 16:17:11 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
++closingAllFiles;
|
2009-10-05 21:09:52 +02:00
|
|
|
ProgressMeter pm( mmfiles.size() , 2 , 1 );
|
|
|
|
for ( set<MemoryMappedFile*>::iterator i = mmfiles.begin(); i != mmfiles.end(); i++ ){
|
2009-01-15 16:17:11 +01:00
|
|
|
(*i)->close();
|
2009-10-05 21:09:52 +02:00
|
|
|
pm.hit();
|
|
|
|
}
|
2009-10-07 17:51:13 +02:00
|
|
|
message << " closeAllFiles() finished" << endl;
|
2009-01-15 16:17:11 +01:00
|
|
|
--closingAllFiles;
|
|
|
|
}
|
|
|
|
|
2009-10-07 18:42:43 +02:00
|
|
|
long long MemoryMappedFile::totalMappedLength(){
|
|
|
|
unsigned long long total = 0;
|
|
|
|
|
|
|
|
for ( set<MemoryMappedFile*>::iterator i = mmfiles.begin(); i != mmfiles.end(); i++ )
|
|
|
|
total += (*i)->length();
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2009-11-11 19:02:04 +01:00
|
|
|
void MemoryMappedFile::flushAll( bool sync ){
|
|
|
|
for ( set<MemoryMappedFile*>::iterator i = mmfiles.begin(); i != mmfiles.end(); i++ )
|
|
|
|
(*i)->flush( sync );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-09 22:56:34 +02:00
|
|
|
void MemoryMappedFile::updateLength( const char *filename, long &length ) {
|
2009-01-15 16:17:11 +01:00
|
|
|
if ( !boost::filesystem::exists( filename ) )
|
|
|
|
return;
|
|
|
|
// make sure we map full length if preexisting file.
|
2009-01-19 01:13:12 +01:00
|
|
|
boost::uintmax_t l = boost::filesystem::file_size( filename );
|
|
|
|
assert( l <= 0x7fffffff );
|
2009-08-13 18:28:30 +02:00
|
|
|
length = (long) l;
|
2008-12-29 02:28:49 +01:00
|
|
|
}
|
2009-01-12 16:22:58 +01:00
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
void* MemoryMappedFile::map(const char *filename) {
|
2009-01-19 01:13:12 +01:00
|
|
|
boost::uintmax_t l = boost::filesystem::file_size( filename );
|
|
|
|
assert( l <= 0x7fffffff );
|
2009-07-09 22:56:34 +02:00
|
|
|
long i = (long)l;
|
2009-04-15 22:11:54 +02:00
|
|
|
return map( filename , i );
|
2009-01-15 16:17:11 +01:00
|
|
|
}
|
2009-01-14 23:09:51 +01:00
|
|
|
|
|
|
|
} // namespace mongo
|