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