Files
mongo/util/mmap.cpp

136 lines
3.9 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.
*/
2010-04-27 15:27:52 -04:00
#include "pch.h"
2008-06-06 09:43:15 -04:00
#include "mmap.h"
#include "processinfo.h"
2010-07-01 17:52:07 -04:00
#include "concurrency/rwlock.h"
2008-06-06 09:43:15 -04:00
2009-01-14 17:09:51 -05:00
namespace mongo {
2010-04-02 11:28:32 -04:00
/*static*/ void MemoryMappedFile::updateLength( const char *filename, long &length ) {
if ( !boost::filesystem::exists( filename ) )
return;
// make sure we map full length if preexisting file.
boost::uintmax_t l = boost::filesystem::file_size( filename );
assert( l <= 0x7fffffff );
length = (long) l;
}
2010-04-02 11:28:32 -04:00
void* MemoryMappedFile::map(const char *filename) {
boost::uintmax_t l = boost::filesystem::file_size( filename );
assert( l <= 0x7fffffff );
long i = (long)l;
return map( filename , i );
}
void printMemInfo( const char * where ){
cout << "mem info: ";
if ( where )
cout << where << " ";
ProcessInfo pi;
if ( ! pi.supported() ){
cout << " not supported" << endl;
return;
}
cout << "vsize: " << pi.getVirtualMemorySize() << " resident: " << pi.getResidentSize() << " mapped: " << ( MemoryMappedFile::totalMappedLength() / ( 1024 * 1024 ) ) << endl;
}
2010-04-02 11:28:32 -04:00
/* --- MongoFile -------------------------------------------------
this is the administrative stuff
*/
static set<MongoFile*> mmfiles;
2010-05-27 15:21:10 -04:00
static RWLock mmmutex("rw:mmmutex");
2010-04-02 11:28:32 -04:00
void MongoFile::destroyed() {
rwlock lk( mmmutex , true );
2010-04-02 11:28:32 -04:00
mmfiles.erase(this);
}
/*static*/
2010-04-02 11:28:32 -04:00
void MongoFile::closeAllFiles( stringstream &message ) {
2010-03-30 10:11:28 -04:00
static int closingAllFiles = 0;
if ( closingAllFiles ) {
message << "warning closingAllFiles=" << closingAllFiles << endl;
return;
}
++closingAllFiles;
rwlock lk( mmmutex , true );
ProgressMeter pm( mmfiles.size() , 2 , 1 );
2010-04-02 11:28:32 -04:00
for ( set<MongoFile*>::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;
}
2010-04-02 11:28:32 -04:00
/*static*/ long long MongoFile::totalMappedLength(){
2009-10-07 12:42:43 -04:00
unsigned long long total = 0;
rwlock lk( mmmutex , false );
2010-04-02 11:28:32 -04:00
for ( set<MongoFile*>::iterator i = mmfiles.begin(); i != mmfiles.end(); i++ )
2009-10-07 12:42:43 -04:00
total += (*i)->length();
return total;
}
2010-04-02 11:28:32 -04:00
/*static*/ int MongoFile::flushAll( bool sync ){
2009-11-11 13:41:45 -05:00
int num = 0;
rwlock lk( mmmutex , false );
2010-04-02 11:28:32 -04:00
for ( set<MongoFile*>::iterator i = mmfiles.begin(); i != mmfiles.end(); i++ ){
2009-11-11 13:41:45 -05:00
num++;
2010-04-02 11:28:32 -04:00
MongoFile * mmf = *i;
if ( ! mmf )
continue;
2010-04-08 15:57:40 -04:00
mmf->flush( sync );
2009-11-11 13:41:45 -05:00
}
return num;
2009-11-11 13:02:04 -05:00
}
2010-04-02 11:28:32 -04:00
void MongoFile::created(){
rwlock lk( mmmutex , true );
2010-04-02 11:28:32 -04:00
mmfiles.insert(this);
}
#ifdef _DEBUG
void MongoFile::lockAll() {
rwlock lk( mmmutex , false );
for ( set<MongoFile*>::iterator i = mmfiles.begin(); i != mmfiles.end(); i++ ){
MongoFile * mmf = *i;
if (mmf) mmf->_lock();
}
}
void MongoFile::unlockAll() {
rwlock lk( mmmutex , false );
for ( set<MongoFile*>::iterator i = mmfiles.begin(); i != mmfiles.end(); i++ ){
MongoFile * mmf = *i;
2010-06-26 07:20:00 -07:00
if (mmf) mmf->_unlock();
}
}
#endif
2009-01-14 17:09:51 -05:00
} // namespace mongo