Files
mongo/util/mmap.cpp

96 lines
2.8 KiB
C++
Raw Normal View History

2008-06-06 09:43:15 -04:00
// mmap.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.
*/
2008-06-06 09:43:15 -04:00
#include "stdafx.h"
#include "mmap.h"
2009-01-14 17:09:51 -05:00
namespace mongo {
set<MemoryMappedFile*> mmfiles;
boost::mutex mmmutex;
MemoryMappedFile::~MemoryMappedFile() {
close();
boostlock lk( mmmutex );
mmfiles.erase(this);
}
void MemoryMappedFile::created(){
boostlock lk( mmmutex );
mmfiles.insert(this);
}
/*static*/
int closingAllFiles = 0;
void MemoryMappedFile::closeAllFiles( stringstream &message ) {
if ( closingAllFiles ) {
message << "warning closingAllFiles=" << closingAllFiles << endl;
return;
}
++closingAllFiles;
ProgressMeter pm( mmfiles.size() , 2 , 1 );
for ( set<MemoryMappedFile*>::iterator i = mmfiles.begin(); i != mmfiles.end(); i++ ){
(*i)->close();
pm.hit();
}
2009-10-07 11:51:13 -04:00
message << " closeAllFiles() finished" << endl;
--closingAllFiles;
}
2009-10-07 12:42:43 -04:00
long long MemoryMappedFile::totalMappedLength(){
unsigned long long total = 0;
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;
boostlock lk( mmmutex );
2009-11-11 13:41:45 -05:00
for ( set<MemoryMappedFile*>::iterator i = mmfiles.begin(); i != mmfiles.end(); i++ ){
num++;
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 ) {
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
}
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;
return map( filename , i );
}
2009-01-14 17:09:51 -05:00
} // namespace mongo